xref: /freebsd/sys/netinet/sctp_indata.c (revision 544e35bd976d33a1d95df0809d05b2f552e592ca)
1f8829a4aSRandall Stewart /*-
2b1006367SRandall Stewart  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3f8829a4aSRandall Stewart  *
4f8829a4aSRandall Stewart  * Redistribution and use in source and binary forms, with or without
5f8829a4aSRandall Stewart  * modification, are permitted provided that the following conditions are met:
6f8829a4aSRandall Stewart  *
7f8829a4aSRandall Stewart  * a) Redistributions of source code must retain the above copyright notice,
8f8829a4aSRandall Stewart  *   this list of conditions and the following disclaimer.
9f8829a4aSRandall Stewart  *
10f8829a4aSRandall Stewart  * b) Redistributions in binary form must reproduce the above copyright
11f8829a4aSRandall Stewart  *    notice, this list of conditions and the following disclaimer in
12f8829a4aSRandall Stewart  *   the documentation and/or other materials provided with the distribution.
13f8829a4aSRandall Stewart  *
14f8829a4aSRandall Stewart  * c) Neither the name of Cisco Systems, Inc. nor the names of its
15f8829a4aSRandall Stewart  *    contributors may be used to endorse or promote products derived
16f8829a4aSRandall Stewart  *    from this software without specific prior written permission.
17f8829a4aSRandall Stewart  *
18f8829a4aSRandall Stewart  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19f8829a4aSRandall Stewart  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20f8829a4aSRandall Stewart  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21f8829a4aSRandall Stewart  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22f8829a4aSRandall Stewart  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23f8829a4aSRandall Stewart  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24f8829a4aSRandall Stewart  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25f8829a4aSRandall Stewart  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26f8829a4aSRandall Stewart  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27f8829a4aSRandall Stewart  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28f8829a4aSRandall Stewart  * THE POSSIBILITY OF SUCH DAMAGE.
29f8829a4aSRandall Stewart  */
30f8829a4aSRandall Stewart 
31a5d547adSRandall Stewart /* $KAME: sctp_indata.c,v 1.36 2005/03/06 16:04:17 itojun Exp $	 */
32f8829a4aSRandall Stewart 
33f8829a4aSRandall Stewart #include <sys/cdefs.h>
34f8829a4aSRandall Stewart __FBSDID("$FreeBSD$");
35f8829a4aSRandall Stewart 
36f8829a4aSRandall Stewart #include <netinet/sctp_os.h>
37f8829a4aSRandall Stewart #include <netinet/sctp_var.h>
3842551e99SRandall Stewart #include <netinet/sctp_sysctl.h>
39f8829a4aSRandall Stewart #include <netinet/sctp_pcb.h>
40f8829a4aSRandall Stewart #include <netinet/sctp_header.h>
41f8829a4aSRandall Stewart #include <netinet/sctputil.h>
42f8829a4aSRandall Stewart #include <netinet/sctp_output.h>
43f8829a4aSRandall Stewart #include <netinet/sctp_input.h>
44f8829a4aSRandall Stewart #include <netinet/sctp_indata.h>
45f8829a4aSRandall Stewart #include <netinet/sctp_uio.h>
46f8829a4aSRandall Stewart #include <netinet/sctp_timer.h>
47f8829a4aSRandall Stewart 
48f8829a4aSRandall Stewart 
49f8829a4aSRandall Stewart /*
50f8829a4aSRandall Stewart  * NOTES: On the outbound side of things I need to check the sack timer to
51f8829a4aSRandall Stewart  * see if I should generate a sack into the chunk queue (if I have data to
52f8829a4aSRandall Stewart  * send that is and will be sending it .. for bundling.
53f8829a4aSRandall Stewart  *
54f8829a4aSRandall Stewart  * The callback in sctp_usrreq.c will get called when the socket is read from.
55f8829a4aSRandall Stewart  * This will cause sctp_service_queues() to get called on the top entry in
56f8829a4aSRandall Stewart  * the list.
57f8829a4aSRandall Stewart  */
58f8829a4aSRandall Stewart 
5972fb6fdbSRandall Stewart void
60f8829a4aSRandall Stewart sctp_set_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
61f8829a4aSRandall Stewart {
62b3f1ea41SRandall Stewart 	asoc->my_rwnd = sctp_calc_rwnd(stcb, asoc);
63f8829a4aSRandall Stewart }
64f8829a4aSRandall Stewart 
65f8829a4aSRandall Stewart /* Calculate what the rwnd would be */
6672fb6fdbSRandall Stewart uint32_t
67f8829a4aSRandall Stewart sctp_calc_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
68f8829a4aSRandall Stewart {
69b3f1ea41SRandall Stewart 	uint32_t calc = 0;
70f8829a4aSRandall Stewart 
71f8829a4aSRandall Stewart 	/*
72f8829a4aSRandall Stewart 	 * This is really set wrong with respect to a 1-2-m socket. Since
73f8829a4aSRandall Stewart 	 * the sb_cc is the count that everyone as put up. When we re-write
74f8829a4aSRandall Stewart 	 * sctp_soreceive then we will fix this so that ONLY this
75f8829a4aSRandall Stewart 	 * associations data is taken into account.
76f8829a4aSRandall Stewart 	 */
77f8829a4aSRandall Stewart 	if (stcb->sctp_socket == NULL)
78f8829a4aSRandall Stewart 		return (calc);
79f8829a4aSRandall Stewart 
80f8829a4aSRandall Stewart 	if (stcb->asoc.sb_cc == 0 &&
81f8829a4aSRandall Stewart 	    asoc->size_on_reasm_queue == 0 &&
82f8829a4aSRandall Stewart 	    asoc->size_on_all_streams == 0) {
83f8829a4aSRandall Stewart 		/* Full rwnd granted */
84b3f1ea41SRandall Stewart 		calc = max(SCTP_SB_LIMIT_RCV(stcb->sctp_socket), SCTP_MINIMAL_RWND);
85f8829a4aSRandall Stewart 		return (calc);
86f8829a4aSRandall Stewart 	}
87f8829a4aSRandall Stewart 	/* get actual space */
88f8829a4aSRandall Stewart 	calc = (uint32_t) sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv);
89f8829a4aSRandall Stewart 
90f8829a4aSRandall Stewart 	/*
91f8829a4aSRandall Stewart 	 * take out what has NOT been put on socket queue and we yet hold
92f8829a4aSRandall Stewart 	 * for putting up.
93f8829a4aSRandall Stewart 	 */
94f8829a4aSRandall Stewart 	calc = sctp_sbspace_sub(calc, (uint32_t) asoc->size_on_reasm_queue);
95f8829a4aSRandall Stewart 	calc = sctp_sbspace_sub(calc, (uint32_t) asoc->size_on_all_streams);
96f8829a4aSRandall Stewart 
97f8829a4aSRandall Stewart 	if (calc == 0) {
98f8829a4aSRandall Stewart 		/* out of space */
99f8829a4aSRandall Stewart 		return (calc);
100f8829a4aSRandall Stewart 	}
101f8829a4aSRandall Stewart 	/* what is the overhead of all these rwnd's */
1022afb3e84SRandall Stewart 	calc = sctp_sbspace_sub(calc, stcb->asoc.my_rwnd_control_len);
103b3f1ea41SRandall Stewart 	/*
104b3f1ea41SRandall Stewart 	 * If the window gets too small due to ctrl-stuff, reduce it to 1,
105b3f1ea41SRandall Stewart 	 * even it is 0. SWS engaged
106f8829a4aSRandall Stewart 	 */
107b3f1ea41SRandall Stewart 	if (calc < stcb->asoc.my_rwnd_control_len) {
108b3f1ea41SRandall Stewart 		calc = 1;
1092afb3e84SRandall Stewart 	}
110b3f1ea41SRandall Stewart 	return (calc);
111f8829a4aSRandall Stewart }
112f8829a4aSRandall Stewart 
113f8829a4aSRandall Stewart 
114f8829a4aSRandall Stewart 
115f8829a4aSRandall Stewart /*
116f8829a4aSRandall Stewart  * Build out our readq entry based on the incoming packet.
117f8829a4aSRandall Stewart  */
118f8829a4aSRandall Stewart struct sctp_queued_to_read *
119f8829a4aSRandall Stewart sctp_build_readq_entry(struct sctp_tcb *stcb,
120f8829a4aSRandall Stewart     struct sctp_nets *net,
121f8829a4aSRandall Stewart     uint32_t tsn, uint32_t ppid,
122f8829a4aSRandall Stewart     uint32_t context, uint16_t stream_no,
123f8829a4aSRandall Stewart     uint16_t stream_seq, uint8_t flags,
124f8829a4aSRandall Stewart     struct mbuf *dm)
125f8829a4aSRandall Stewart {
126f8829a4aSRandall Stewart 	struct sctp_queued_to_read *read_queue_e = NULL;
127f8829a4aSRandall Stewart 
128f8829a4aSRandall Stewart 	sctp_alloc_a_readq(stcb, read_queue_e);
129f8829a4aSRandall Stewart 	if (read_queue_e == NULL) {
130f8829a4aSRandall Stewart 		goto failed_build;
131f8829a4aSRandall Stewart 	}
132f8829a4aSRandall Stewart 	read_queue_e->sinfo_stream = stream_no;
133f8829a4aSRandall Stewart 	read_queue_e->sinfo_ssn = stream_seq;
134f8829a4aSRandall Stewart 	read_queue_e->sinfo_flags = (flags << 8);
135f8829a4aSRandall Stewart 	read_queue_e->sinfo_ppid = ppid;
136f8829a4aSRandall Stewart 	read_queue_e->sinfo_context = stcb->asoc.context;
137f8829a4aSRandall Stewart 	read_queue_e->sinfo_timetolive = 0;
138f8829a4aSRandall Stewart 	read_queue_e->sinfo_tsn = tsn;
139f8829a4aSRandall Stewart 	read_queue_e->sinfo_cumtsn = tsn;
140f8829a4aSRandall Stewart 	read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
141f8829a4aSRandall Stewart 	read_queue_e->whoFrom = net;
142f8829a4aSRandall Stewart 	read_queue_e->length = 0;
143f8829a4aSRandall Stewart 	atomic_add_int(&net->ref_count, 1);
144f8829a4aSRandall Stewart 	read_queue_e->data = dm;
145139bc87fSRandall Stewart 	read_queue_e->spec_flags = 0;
146f8829a4aSRandall Stewart 	read_queue_e->tail_mbuf = NULL;
14717205eccSRandall Stewart 	read_queue_e->aux_data = NULL;
148f8829a4aSRandall Stewart 	read_queue_e->stcb = stcb;
149f8829a4aSRandall Stewart 	read_queue_e->port_from = stcb->rport;
150f8829a4aSRandall Stewart 	read_queue_e->do_not_ref_stcb = 0;
151f8829a4aSRandall Stewart 	read_queue_e->end_added = 0;
1529a6142d8SRandall Stewart 	read_queue_e->some_taken = 0;
15303b0b021SRandall Stewart 	read_queue_e->pdapi_aborted = 0;
154f8829a4aSRandall Stewart failed_build:
155f8829a4aSRandall Stewart 	return (read_queue_e);
156f8829a4aSRandall Stewart }
157f8829a4aSRandall Stewart 
158f8829a4aSRandall Stewart 
159f8829a4aSRandall Stewart /*
160f8829a4aSRandall Stewart  * Build out our readq entry based on the incoming packet.
161f8829a4aSRandall Stewart  */
162f8829a4aSRandall Stewart static struct sctp_queued_to_read *
163f8829a4aSRandall Stewart sctp_build_readq_entry_chk(struct sctp_tcb *stcb,
164f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk)
165f8829a4aSRandall Stewart {
166f8829a4aSRandall Stewart 	struct sctp_queued_to_read *read_queue_e = NULL;
167f8829a4aSRandall Stewart 
168f8829a4aSRandall Stewart 	sctp_alloc_a_readq(stcb, read_queue_e);
169f8829a4aSRandall Stewart 	if (read_queue_e == NULL) {
170f8829a4aSRandall Stewart 		goto failed_build;
171f8829a4aSRandall Stewart 	}
172f8829a4aSRandall Stewart 	read_queue_e->sinfo_stream = chk->rec.data.stream_number;
173f8829a4aSRandall Stewart 	read_queue_e->sinfo_ssn = chk->rec.data.stream_seq;
174f8829a4aSRandall Stewart 	read_queue_e->sinfo_flags = (chk->rec.data.rcv_flags << 8);
175f8829a4aSRandall Stewart 	read_queue_e->sinfo_ppid = chk->rec.data.payloadtype;
176f8829a4aSRandall Stewart 	read_queue_e->sinfo_context = stcb->asoc.context;
177f8829a4aSRandall Stewart 	read_queue_e->sinfo_timetolive = 0;
178f8829a4aSRandall Stewart 	read_queue_e->sinfo_tsn = chk->rec.data.TSN_seq;
179f8829a4aSRandall Stewart 	read_queue_e->sinfo_cumtsn = chk->rec.data.TSN_seq;
180f8829a4aSRandall Stewart 	read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
181f8829a4aSRandall Stewart 	read_queue_e->whoFrom = chk->whoTo;
18217205eccSRandall Stewart 	read_queue_e->aux_data = NULL;
183f8829a4aSRandall Stewart 	read_queue_e->length = 0;
184f8829a4aSRandall Stewart 	atomic_add_int(&chk->whoTo->ref_count, 1);
185f8829a4aSRandall Stewart 	read_queue_e->data = chk->data;
186f8829a4aSRandall Stewart 	read_queue_e->tail_mbuf = NULL;
187f8829a4aSRandall Stewart 	read_queue_e->stcb = stcb;
188f8829a4aSRandall Stewart 	read_queue_e->port_from = stcb->rport;
189139bc87fSRandall Stewart 	read_queue_e->spec_flags = 0;
190f8829a4aSRandall Stewart 	read_queue_e->do_not_ref_stcb = 0;
191f8829a4aSRandall Stewart 	read_queue_e->end_added = 0;
1929a6142d8SRandall Stewart 	read_queue_e->some_taken = 0;
19303b0b021SRandall Stewart 	read_queue_e->pdapi_aborted = 0;
194f8829a4aSRandall Stewart failed_build:
195f8829a4aSRandall Stewart 	return (read_queue_e);
196f8829a4aSRandall Stewart }
197f8829a4aSRandall Stewart 
198f8829a4aSRandall Stewart 
199f8829a4aSRandall Stewart struct mbuf *
200f8829a4aSRandall Stewart sctp_build_ctl_nchunk(struct sctp_inpcb *inp,
201f8829a4aSRandall Stewart     struct sctp_sndrcvinfo *sinfo)
202f8829a4aSRandall Stewart {
203f8829a4aSRandall Stewart 	struct sctp_sndrcvinfo *outinfo;
204f8829a4aSRandall Stewart 	struct cmsghdr *cmh;
205f8829a4aSRandall Stewart 	struct mbuf *ret;
206f8829a4aSRandall Stewart 	int len;
207f8829a4aSRandall Stewart 	int use_extended = 0;
208f8829a4aSRandall Stewart 
209f8829a4aSRandall Stewart 	if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
210f8829a4aSRandall Stewart 		/* user does not want the sndrcv ctl */
211f8829a4aSRandall Stewart 		return (NULL);
212f8829a4aSRandall Stewart 	}
213f8829a4aSRandall Stewart 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
214f8829a4aSRandall Stewart 		use_extended = 1;
215f8829a4aSRandall Stewart 		len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
216f8829a4aSRandall Stewart 	} else {
217f8829a4aSRandall Stewart 		len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
218f8829a4aSRandall Stewart 	}
219f8829a4aSRandall Stewart 
220f8829a4aSRandall Stewart 
221f8829a4aSRandall Stewart 	ret = sctp_get_mbuf_for_msg(len,
222139bc87fSRandall Stewart 	    0, M_DONTWAIT, 1, MT_DATA);
223f8829a4aSRandall Stewart 
224f8829a4aSRandall Stewart 	if (ret == NULL) {
225f8829a4aSRandall Stewart 		/* No space */
226f8829a4aSRandall Stewart 		return (ret);
227f8829a4aSRandall Stewart 	}
228f8829a4aSRandall Stewart 	/* We need a CMSG header followed by the struct  */
229f8829a4aSRandall Stewart 	cmh = mtod(ret, struct cmsghdr *);
230f8829a4aSRandall Stewart 	outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
231f8829a4aSRandall Stewart 	cmh->cmsg_level = IPPROTO_SCTP;
232f8829a4aSRandall Stewart 	if (use_extended) {
233f8829a4aSRandall Stewart 		cmh->cmsg_type = SCTP_EXTRCV;
234f8829a4aSRandall Stewart 		cmh->cmsg_len = len;
235f8829a4aSRandall Stewart 		memcpy(outinfo, sinfo, len);
236f8829a4aSRandall Stewart 	} else {
237f8829a4aSRandall Stewart 		cmh->cmsg_type = SCTP_SNDRCV;
238f8829a4aSRandall Stewart 		cmh->cmsg_len = len;
239f8829a4aSRandall Stewart 		*outinfo = *sinfo;
240f8829a4aSRandall Stewart 	}
241139bc87fSRandall Stewart 	SCTP_BUF_LEN(ret) = cmh->cmsg_len;
242f8829a4aSRandall Stewart 	return (ret);
243f8829a4aSRandall Stewart }
244f8829a4aSRandall Stewart 
245139bc87fSRandall Stewart 
24617205eccSRandall Stewart char *
24717205eccSRandall Stewart sctp_build_ctl_cchunk(struct sctp_inpcb *inp,
24817205eccSRandall Stewart     int *control_len,
24917205eccSRandall Stewart     struct sctp_sndrcvinfo *sinfo)
25017205eccSRandall Stewart {
25117205eccSRandall Stewart 	struct sctp_sndrcvinfo *outinfo;
25217205eccSRandall Stewart 	struct cmsghdr *cmh;
25317205eccSRandall Stewart 	char *buf;
25417205eccSRandall Stewart 	int len;
25517205eccSRandall Stewart 	int use_extended = 0;
25617205eccSRandall Stewart 
25717205eccSRandall Stewart 	if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
25817205eccSRandall Stewart 		/* user does not want the sndrcv ctl */
25917205eccSRandall Stewart 		return (NULL);
26017205eccSRandall Stewart 	}
26117205eccSRandall Stewart 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
26217205eccSRandall Stewart 		use_extended = 1;
26317205eccSRandall Stewart 		len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
26417205eccSRandall Stewart 	} else {
26517205eccSRandall Stewart 		len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
26617205eccSRandall Stewart 	}
267207304d4SRandall Stewart 	SCTP_MALLOC(buf, char *, len, SCTP_M_CMSG);
26817205eccSRandall Stewart 	if (buf == NULL) {
26917205eccSRandall Stewart 		/* No space */
27017205eccSRandall Stewart 		return (buf);
27117205eccSRandall Stewart 	}
27217205eccSRandall Stewart 	/* We need a CMSG header followed by the struct  */
27317205eccSRandall Stewart 	cmh = (struct cmsghdr *)buf;
27417205eccSRandall Stewart 	outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
27517205eccSRandall Stewart 	cmh->cmsg_level = IPPROTO_SCTP;
27617205eccSRandall Stewart 	if (use_extended) {
27717205eccSRandall Stewart 		cmh->cmsg_type = SCTP_EXTRCV;
27817205eccSRandall Stewart 		cmh->cmsg_len = len;
27917205eccSRandall Stewart 		memcpy(outinfo, sinfo, len);
28017205eccSRandall Stewart 	} else {
28117205eccSRandall Stewart 		cmh->cmsg_type = SCTP_SNDRCV;
28217205eccSRandall Stewart 		cmh->cmsg_len = len;
28317205eccSRandall Stewart 		*outinfo = *sinfo;
28417205eccSRandall Stewart 	}
28517205eccSRandall Stewart 	*control_len = len;
28617205eccSRandall Stewart 	return (buf);
28717205eccSRandall Stewart }
28817205eccSRandall Stewart 
28917205eccSRandall Stewart 
290f8829a4aSRandall Stewart /*
291f8829a4aSRandall Stewart  * We are delivering currently from the reassembly queue. We must continue to
292f8829a4aSRandall Stewart  * deliver until we either: 1) run out of space. 2) run out of sequential
293f8829a4aSRandall Stewart  * TSN's 3) hit the SCTP_DATA_LAST_FRAG flag.
294f8829a4aSRandall Stewart  */
295f8829a4aSRandall Stewart static void
296f8829a4aSRandall Stewart sctp_service_reassembly(struct sctp_tcb *stcb, struct sctp_association *asoc)
297f8829a4aSRandall Stewart {
298f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
299f8829a4aSRandall Stewart 	uint16_t nxt_todel;
300f8829a4aSRandall Stewart 	uint16_t stream_no;
301f8829a4aSRandall Stewart 	int end = 0;
302f8829a4aSRandall Stewart 	int cntDel;
303830d754dSRandall Stewart 
304830d754dSRandall Stewart 	/* EY if any out-of-order delivered, then tag it nr on nr_map */
305830d754dSRandall Stewart 	uint32_t nr_tsn, nr_gap;
306830d754dSRandall Stewart 
307f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control, *ctl, *ctlat;
308f8829a4aSRandall Stewart 
309ad81507eSRandall Stewart 	if (stcb == NULL)
310ad81507eSRandall Stewart 		return;
311ad81507eSRandall Stewart 
312f42a358aSRandall Stewart 	cntDel = stream_no = 0;
313ad81507eSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
314851b7298SRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
315ad81507eSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
316851b7298SRandall Stewart 		/* socket above is long gone or going.. */
317851b7298SRandall Stewart abandon:
318f8829a4aSRandall Stewart 		asoc->fragmented_delivery_inprogress = 0;
319f8829a4aSRandall Stewart 		chk = TAILQ_FIRST(&asoc->reasmqueue);
320f8829a4aSRandall Stewart 		while (chk) {
321f8829a4aSRandall Stewart 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
322f8829a4aSRandall Stewart 			asoc->size_on_reasm_queue -= chk->send_size;
323f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_reasm_queue);
324f8829a4aSRandall Stewart 			/*
325f8829a4aSRandall Stewart 			 * Lose the data pointer, since its in the socket
326f8829a4aSRandall Stewart 			 * buffer
327f8829a4aSRandall Stewart 			 */
328f8829a4aSRandall Stewart 			if (chk->data) {
329f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
330f8829a4aSRandall Stewart 				chk->data = NULL;
331f8829a4aSRandall Stewart 			}
332f8829a4aSRandall Stewart 			/* Now free the address and data */
333f8829a4aSRandall Stewart 			sctp_free_a_chunk(stcb, chk);
3343c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
335f8829a4aSRandall Stewart 			chk = TAILQ_FIRST(&asoc->reasmqueue);
336f8829a4aSRandall Stewart 		}
337f8829a4aSRandall Stewart 		return;
338f8829a4aSRandall Stewart 	}
339f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
340f8829a4aSRandall Stewart 	do {
341f8829a4aSRandall Stewart 		chk = TAILQ_FIRST(&asoc->reasmqueue);
342f8829a4aSRandall Stewart 		if (chk == NULL) {
343f8829a4aSRandall Stewart 			return;
344f8829a4aSRandall Stewart 		}
345f8829a4aSRandall Stewart 		if (chk->rec.data.TSN_seq != (asoc->tsn_last_delivered + 1)) {
346f8829a4aSRandall Stewart 			/* Can't deliver more :< */
347f8829a4aSRandall Stewart 			return;
348f8829a4aSRandall Stewart 		}
349f8829a4aSRandall Stewart 		stream_no = chk->rec.data.stream_number;
350f8829a4aSRandall Stewart 		nxt_todel = asoc->strmin[stream_no].last_sequence_delivered + 1;
351f8829a4aSRandall Stewart 		if (nxt_todel != chk->rec.data.stream_seq &&
352f8829a4aSRandall Stewart 		    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
353f8829a4aSRandall Stewart 			/*
354f8829a4aSRandall Stewart 			 * Not the next sequence to deliver in its stream OR
355f8829a4aSRandall Stewart 			 * unordered
356f8829a4aSRandall Stewart 			 */
357f8829a4aSRandall Stewart 			return;
358f8829a4aSRandall Stewart 		}
359f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
360f8829a4aSRandall Stewart 
361f8829a4aSRandall Stewart 			control = sctp_build_readq_entry_chk(stcb, chk);
362f8829a4aSRandall Stewart 			if (control == NULL) {
363f8829a4aSRandall Stewart 				/* out of memory? */
364f8829a4aSRandall Stewart 				return;
365f8829a4aSRandall Stewart 			}
366f8829a4aSRandall Stewart 			/* save it off for our future deliveries */
367f8829a4aSRandall Stewart 			stcb->asoc.control_pdapi = control;
368f8829a4aSRandall Stewart 			if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
369f8829a4aSRandall Stewart 				end = 1;
370f8829a4aSRandall Stewart 			else
371f8829a4aSRandall Stewart 				end = 0;
372f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep,
373ceaad40aSRandall Stewart 			    stcb, control, &stcb->sctp_socket->so_rcv, end, SCTP_SO_NOT_LOCKED);
374f8829a4aSRandall Stewart 			cntDel++;
375f8829a4aSRandall Stewart 		} else {
376f8829a4aSRandall Stewart 			if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
377f8829a4aSRandall Stewart 				end = 1;
378f8829a4aSRandall Stewart 			else
379f8829a4aSRandall Stewart 				end = 0;
380f8829a4aSRandall Stewart 			if (sctp_append_to_readq(stcb->sctp_ep, stcb,
381f8829a4aSRandall Stewart 			    stcb->asoc.control_pdapi,
382f8829a4aSRandall Stewart 			    chk->data, end, chk->rec.data.TSN_seq,
383f8829a4aSRandall Stewart 			    &stcb->sctp_socket->so_rcv)) {
384f8829a4aSRandall Stewart 				/*
385f8829a4aSRandall Stewart 				 * something is very wrong, either
386f8829a4aSRandall Stewart 				 * control_pdapi is NULL, or the tail_mbuf
387f8829a4aSRandall Stewart 				 * is corrupt, or there is a EOM already on
388f8829a4aSRandall Stewart 				 * the mbuf chain.
389f8829a4aSRandall Stewart 				 */
390851b7298SRandall Stewart 				if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
391851b7298SRandall Stewart 					goto abandon;
392851b7298SRandall Stewart 				} else {
393df6e0cc3SRandall Stewart #ifdef INVARIANTS
394ad81507eSRandall Stewart 					if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
395f8829a4aSRandall Stewart 						panic("This should not happen control_pdapi NULL?");
396f8829a4aSRandall Stewart 					}
397f8829a4aSRandall Stewart 					/* if we did not panic, it was a EOM */
398f8829a4aSRandall Stewart 					panic("Bad chunking ??");
399df6e0cc3SRandall Stewart #else
400df6e0cc3SRandall Stewart 					if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
401df6e0cc3SRandall Stewart 						SCTP_PRINTF("This should not happen control_pdapi NULL?\n");
402df6e0cc3SRandall Stewart 					}
403df6e0cc3SRandall Stewart 					SCTP_PRINTF("Bad chunking ??\n");
404df6e0cc3SRandall Stewart 					SCTP_PRINTF("Dumping re-assembly queue this will probably hose the association\n");
405df6e0cc3SRandall Stewart 
406df6e0cc3SRandall Stewart #endif
407df6e0cc3SRandall Stewart 					goto abandon;
408f8829a4aSRandall Stewart 				}
409851b7298SRandall Stewart 			}
410f8829a4aSRandall Stewart 			cntDel++;
411f8829a4aSRandall Stewart 		}
412f8829a4aSRandall Stewart 		/* pull it we did it */
413f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
414830d754dSRandall Stewart 		/*
415830d754dSRandall Stewart 		 * EY this is the chunk that should be tagged nr gapped
416830d754dSRandall Stewart 		 * calculate the gap and such then tag this TSN nr
417830d754dSRandall Stewart 		 * chk->rec.data.TSN_seq
418830d754dSRandall Stewart 		 */
419830d754dSRandall Stewart 		/*
420830d754dSRandall Stewart 		 * EY!-TODO- this tsn should be tagged nr only if it is
421830d754dSRandall Stewart 		 * out-of-order, the if statement should be modified
422830d754dSRandall Stewart 		 */
423830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
424830d754dSRandall Stewart 
425830d754dSRandall Stewart 			nr_tsn = chk->rec.data.TSN_seq;
4268933fa13SRandall Stewart 			if ((compare_with_wrap(nr_tsn, asoc->nr_mapping_array_base_tsn, MAX_TSN)) ||
4278933fa13SRandall Stewart 			    (nr_tsn == asoc->nr_mapping_array_base_tsn)) {
428830d754dSRandall Stewart 				nr_gap = nr_tsn - asoc->nr_mapping_array_base_tsn;
429830d754dSRandall Stewart 			} else {
430830d754dSRandall Stewart 				nr_gap = (MAX_TSN - asoc->nr_mapping_array_base_tsn) + nr_tsn + 1;
431830d754dSRandall Stewart 			}
4328933fa13SRandall Stewart 			if ((nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3)) ||
433830d754dSRandall Stewart 			    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
434830d754dSRandall Stewart 				/*
435830d754dSRandall Stewart 				 * EY The 1st should never happen, as in
436830d754dSRandall Stewart 				 * process_a_data_chunk method this check
437830d754dSRandall Stewart 				 * should be done
438830d754dSRandall Stewart 				 */
439830d754dSRandall Stewart 				/*
440830d754dSRandall Stewart 				 * EY The 2nd should never happen, because
441830d754dSRandall Stewart 				 * nr_mapping_array is always expanded when
442830d754dSRandall Stewart 				 * mapping_array is expanded
443830d754dSRandall Stewart 				 */
4448933fa13SRandall Stewart 				printf("Impossible nr_gap ack range failed\n");
445830d754dSRandall Stewart 			} else {
446830d754dSRandall Stewart 				SCTP_TCB_LOCK_ASSERT(stcb);
447830d754dSRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
4488933fa13SRandall Stewart 				if (compare_with_wrap(nr_tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
449830d754dSRandall Stewart 					asoc->highest_tsn_inside_nr_map = nr_tsn;
450830d754dSRandall Stewart 			}
451830d754dSRandall Stewart 		}
452f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
453f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 0;
454f8829a4aSRandall Stewart 			if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
455f8829a4aSRandall Stewart 				asoc->strmin[stream_no].last_sequence_delivered++;
456f8829a4aSRandall Stewart 			}
457f8829a4aSRandall Stewart 			if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
458f8829a4aSRandall Stewart 				SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
459f8829a4aSRandall Stewart 			}
460f8829a4aSRandall Stewart 		} else if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
461f8829a4aSRandall Stewart 			/*
462f8829a4aSRandall Stewart 			 * turn the flag back on since we just  delivered
463f8829a4aSRandall Stewart 			 * yet another one.
464f8829a4aSRandall Stewart 			 */
465f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 1;
466f8829a4aSRandall Stewart 		}
467f8829a4aSRandall Stewart 		asoc->tsn_of_pdapi_last_delivered = chk->rec.data.TSN_seq;
468f8829a4aSRandall Stewart 		asoc->last_flags_delivered = chk->rec.data.rcv_flags;
469f8829a4aSRandall Stewart 		asoc->last_strm_seq_delivered = chk->rec.data.stream_seq;
470f8829a4aSRandall Stewart 		asoc->last_strm_no_delivered = chk->rec.data.stream_number;
471f8829a4aSRandall Stewart 
472f8829a4aSRandall Stewart 		asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
473f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue -= chk->send_size;
474f8829a4aSRandall Stewart 		sctp_ucount_decr(asoc->cnt_on_reasm_queue);
475f8829a4aSRandall Stewart 		/* free up the chk */
476f8829a4aSRandall Stewart 		chk->data = NULL;
477f8829a4aSRandall Stewart 		sctp_free_a_chunk(stcb, chk);
478f8829a4aSRandall Stewart 
479f8829a4aSRandall Stewart 		if (asoc->fragmented_delivery_inprogress == 0) {
480f8829a4aSRandall Stewart 			/*
481f8829a4aSRandall Stewart 			 * Now lets see if we can deliver the next one on
482f8829a4aSRandall Stewart 			 * the stream
483f8829a4aSRandall Stewart 			 */
484f8829a4aSRandall Stewart 			struct sctp_stream_in *strm;
485f8829a4aSRandall Stewart 
486f8829a4aSRandall Stewart 			strm = &asoc->strmin[stream_no];
487f8829a4aSRandall Stewart 			nxt_todel = strm->last_sequence_delivered + 1;
488f8829a4aSRandall Stewart 			ctl = TAILQ_FIRST(&strm->inqueue);
489f8829a4aSRandall Stewart 			if (ctl && (nxt_todel == ctl->sinfo_ssn)) {
490f8829a4aSRandall Stewart 				while (ctl != NULL) {
491f8829a4aSRandall Stewart 					/* Deliver more if we can. */
492f8829a4aSRandall Stewart 					if (nxt_todel == ctl->sinfo_ssn) {
493f8829a4aSRandall Stewart 						ctlat = TAILQ_NEXT(ctl, next);
494f8829a4aSRandall Stewart 						TAILQ_REMOVE(&strm->inqueue, ctl, next);
495f8829a4aSRandall Stewart 						asoc->size_on_all_streams -= ctl->length;
496f8829a4aSRandall Stewart 						sctp_ucount_decr(asoc->cnt_on_all_streams);
497f8829a4aSRandall Stewart 						strm->last_sequence_delivered++;
498830d754dSRandall Stewart 						/*
499830d754dSRandall Stewart 						 * EY will be used to
500830d754dSRandall Stewart 						 * calculate nr-gap
501830d754dSRandall Stewart 						 */
502830d754dSRandall Stewart 						nr_tsn = ctl->sinfo_tsn;
503f8829a4aSRandall Stewart 						sctp_add_to_readq(stcb->sctp_ep, stcb,
504f8829a4aSRandall Stewart 						    ctl,
505ceaad40aSRandall Stewart 						    &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
506830d754dSRandall Stewart 						/*
507830d754dSRandall Stewart 						 * EY -now something is
508830d754dSRandall Stewart 						 * delivered, calculate
509830d754dSRandall Stewart 						 * nr_gap and tag this tsn
510830d754dSRandall Stewart 						 * NR
511830d754dSRandall Stewart 						 */
512830d754dSRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
513830d754dSRandall Stewart 
514830d754dSRandall Stewart 							if (nr_tsn >= asoc->nr_mapping_array_base_tsn) {
515830d754dSRandall Stewart 								nr_gap = nr_tsn - asoc->nr_mapping_array_base_tsn;
516830d754dSRandall Stewart 							} else {
517830d754dSRandall Stewart 								nr_gap = (MAX_TSN - asoc->nr_mapping_array_base_tsn) + nr_tsn + 1;
518830d754dSRandall Stewart 							}
519830d754dSRandall Stewart 							if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
520830d754dSRandall Stewart 							    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
521830d754dSRandall Stewart 								/*
522830d754dSRandall Stewart 								 * EY The
523830d754dSRandall Stewart 								 * 1st
524830d754dSRandall Stewart 								 * should
525830d754dSRandall Stewart 								 * never
526830d754dSRandall Stewart 								 * happen,
527830d754dSRandall Stewart 								 * as in
528830d754dSRandall Stewart 								 * process_a_
529830d754dSRandall Stewart 								 * data_chunk
530830d754dSRandall Stewart 								 *  method
531830d754dSRandall Stewart 								 * this
532830d754dSRandall Stewart 								 * check
533830d754dSRandall Stewart 								 * should be
534830d754dSRandall Stewart 								 * done
535830d754dSRandall Stewart 								 */
536830d754dSRandall Stewart 								/*
537830d754dSRandall Stewart 								 * EY The
538830d754dSRandall Stewart 								 * 2nd
539830d754dSRandall Stewart 								 * should
540830d754dSRandall Stewart 								 * never
541830d754dSRandall Stewart 								 * happen,
542830d754dSRandall Stewart 								 * because
543830d754dSRandall Stewart 								 * nr_mapping
544830d754dSRandall Stewart 								 * _array is
545830d754dSRandall Stewart 								 * always
546830d754dSRandall Stewart 								 * expanded
547830d754dSRandall Stewart 								 * when
548830d754dSRandall Stewart 								 * mapping_ar
549830d754dSRandall Stewart 								 * ray is
550830d754dSRandall Stewart 								 * expanded
551830d754dSRandall Stewart 								 */
552830d754dSRandall Stewart 							} else {
553830d754dSRandall Stewart 								SCTP_TCB_LOCK_ASSERT(stcb);
554830d754dSRandall Stewart 								SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
5558933fa13SRandall Stewart 								if (compare_with_wrap(nr_tsn,
5568933fa13SRandall Stewart 								    asoc->highest_tsn_inside_nr_map,
5578933fa13SRandall Stewart 								    MAX_TSN))
558830d754dSRandall Stewart 									asoc->highest_tsn_inside_nr_map = nr_tsn;
559830d754dSRandall Stewart 							}
560830d754dSRandall Stewart 						}
561f8829a4aSRandall Stewart 						ctl = ctlat;
562f8829a4aSRandall Stewart 					} else {
563f8829a4aSRandall Stewart 						break;
564f8829a4aSRandall Stewart 					}
565f8829a4aSRandall Stewart 					nxt_todel = strm->last_sequence_delivered + 1;
566f8829a4aSRandall Stewart 				}
567f8829a4aSRandall Stewart 			}
568139bc87fSRandall Stewart 			break;
569f8829a4aSRandall Stewart 		}
5703c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
571f8829a4aSRandall Stewart 		chk = TAILQ_FIRST(&asoc->reasmqueue);
572f8829a4aSRandall Stewart 	} while (chk);
573f8829a4aSRandall Stewart }
574f8829a4aSRandall Stewart 
575f8829a4aSRandall Stewart /*
576f8829a4aSRandall Stewart  * Queue the chunk either right into the socket buffer if it is the next one
577f8829a4aSRandall Stewart  * to go OR put it in the correct place in the delivery queue.  If we do
578f8829a4aSRandall Stewart  * append to the so_buf, keep doing so until we are out of order. One big
579f8829a4aSRandall Stewart  * question still remains, what to do when the socket buffer is FULL??
580f8829a4aSRandall Stewart  */
581f8829a4aSRandall Stewart static void
582f8829a4aSRandall Stewart sctp_queue_data_to_stream(struct sctp_tcb *stcb, struct sctp_association *asoc,
583f8829a4aSRandall Stewart     struct sctp_queued_to_read *control, int *abort_flag)
584f8829a4aSRandall Stewart {
585f8829a4aSRandall Stewart 	/*
586f8829a4aSRandall Stewart 	 * FIX-ME maybe? What happens when the ssn wraps? If we are getting
587f8829a4aSRandall Stewart 	 * all the data in one stream this could happen quite rapidly. One
588f8829a4aSRandall Stewart 	 * could use the TSN to keep track of things, but this scheme breaks
589f8829a4aSRandall Stewart 	 * down in the other type of stream useage that could occur. Send a
590f8829a4aSRandall Stewart 	 * single msg to stream 0, send 4Billion messages to stream 1, now
591f8829a4aSRandall Stewart 	 * send a message to stream 0. You have a situation where the TSN
592f8829a4aSRandall Stewart 	 * has wrapped but not in the stream. Is this worth worrying about
593f8829a4aSRandall Stewart 	 * or should we just change our queue sort at the bottom to be by
594f8829a4aSRandall Stewart 	 * TSN.
595f8829a4aSRandall Stewart 	 *
596f8829a4aSRandall Stewart 	 * Could it also be legal for a peer to send ssn 1 with TSN 2 and ssn 2
597f8829a4aSRandall Stewart 	 * with TSN 1? If the peer is doing some sort of funky TSN/SSN
598f8829a4aSRandall Stewart 	 * assignment this could happen... and I don't see how this would be
599f8829a4aSRandall Stewart 	 * a violation. So for now I am undecided an will leave the sort by
600f8829a4aSRandall Stewart 	 * SSN alone. Maybe a hybred approach is the answer
601f8829a4aSRandall Stewart 	 *
602f8829a4aSRandall Stewart 	 */
603f8829a4aSRandall Stewart 	struct sctp_stream_in *strm;
604f8829a4aSRandall Stewart 	struct sctp_queued_to_read *at;
605f8829a4aSRandall Stewart 	int queue_needed;
606f8829a4aSRandall Stewart 	uint16_t nxt_todel;
607f8829a4aSRandall Stewart 	struct mbuf *oper;
608f8829a4aSRandall Stewart 
609830d754dSRandall Stewart 	/* EY- will be used to calculate nr-gap for a tsn */
610830d754dSRandall Stewart 	uint32_t nr_tsn, nr_gap;
611830d754dSRandall Stewart 
612f8829a4aSRandall Stewart 	queue_needed = 1;
613f8829a4aSRandall Stewart 	asoc->size_on_all_streams += control->length;
614f8829a4aSRandall Stewart 	sctp_ucount_incr(asoc->cnt_on_all_streams);
615f8829a4aSRandall Stewart 	strm = &asoc->strmin[control->sinfo_stream];
616f8829a4aSRandall Stewart 	nxt_todel = strm->last_sequence_delivered + 1;
617b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
618f8829a4aSRandall Stewart 		sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INTO_STRD);
61980fefe0aSRandall Stewart 	}
620ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INDATA1,
621ad81507eSRandall Stewart 	    "queue to stream called for ssn:%u lastdel:%u nxt:%u\n",
622f8829a4aSRandall Stewart 	    (uint32_t) control->sinfo_stream,
623ad81507eSRandall Stewart 	    (uint32_t) strm->last_sequence_delivered,
624ad81507eSRandall Stewart 	    (uint32_t) nxt_todel);
625f8829a4aSRandall Stewart 	if (compare_with_wrap(strm->last_sequence_delivered,
626f8829a4aSRandall Stewart 	    control->sinfo_ssn, MAX_SEQ) ||
627f8829a4aSRandall Stewart 	    (strm->last_sequence_delivered == control->sinfo_ssn)) {
628f8829a4aSRandall Stewart 		/* The incoming sseq is behind where we last delivered? */
629ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1, "Duplicate S-SEQ:%d delivered:%d from peer, Abort  association\n",
630ad81507eSRandall Stewart 		    control->sinfo_ssn, strm->last_sequence_delivered);
631c40e9cf2SRandall Stewart protocol_error:
632f8829a4aSRandall Stewart 		/*
633f8829a4aSRandall Stewart 		 * throw it in the stream so it gets cleaned up in
634f8829a4aSRandall Stewart 		 * association destruction
635f8829a4aSRandall Stewart 		 */
636f8829a4aSRandall Stewart 		TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
637139bc87fSRandall Stewart 		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
638f8829a4aSRandall Stewart 		    0, M_DONTWAIT, 1, MT_DATA);
639f8829a4aSRandall Stewart 		if (oper) {
640f8829a4aSRandall Stewart 			struct sctp_paramhdr *ph;
641f8829a4aSRandall Stewart 			uint32_t *ippp;
642f8829a4aSRandall Stewart 
643139bc87fSRandall Stewart 			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
644f8829a4aSRandall Stewart 			    (sizeof(uint32_t) * 3);
645f8829a4aSRandall Stewart 			ph = mtod(oper, struct sctp_paramhdr *);
646f8829a4aSRandall Stewart 			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
647139bc87fSRandall Stewart 			ph->param_length = htons(SCTP_BUF_LEN(oper));
648f8829a4aSRandall Stewart 			ippp = (uint32_t *) (ph + 1);
649a5d547adSRandall Stewart 			*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_1);
650f8829a4aSRandall Stewart 			ippp++;
651f8829a4aSRandall Stewart 			*ippp = control->sinfo_tsn;
652f8829a4aSRandall Stewart 			ippp++;
653f8829a4aSRandall Stewart 			*ippp = ((control->sinfo_stream << 16) | control->sinfo_ssn);
654f8829a4aSRandall Stewart 		}
655a5d547adSRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_1;
656f8829a4aSRandall Stewart 		sctp_abort_an_association(stcb->sctp_ep, stcb,
657ceaad40aSRandall Stewart 		    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
658f8829a4aSRandall Stewart 
659f8829a4aSRandall Stewart 		*abort_flag = 1;
660f8829a4aSRandall Stewart 		return;
661f8829a4aSRandall Stewart 
662f8829a4aSRandall Stewart 	}
663f8829a4aSRandall Stewart 	if (nxt_todel == control->sinfo_ssn) {
664f8829a4aSRandall Stewart 		/* can be delivered right away? */
665b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
666f8829a4aSRandall Stewart 			sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_IMMED_DEL);
66780fefe0aSRandall Stewart 		}
668830d754dSRandall Stewart 		/* EY it wont be queued if it could be delivered directly */
669f8829a4aSRandall Stewart 		queue_needed = 0;
670f8829a4aSRandall Stewart 		asoc->size_on_all_streams -= control->length;
671f8829a4aSRandall Stewart 		sctp_ucount_decr(asoc->cnt_on_all_streams);
672f8829a4aSRandall Stewart 		strm->last_sequence_delivered++;
673830d754dSRandall Stewart 		/* EY will be used to calculate nr-gap */
674830d754dSRandall Stewart 		nr_tsn = control->sinfo_tsn;
675f8829a4aSRandall Stewart 		sctp_add_to_readq(stcb->sctp_ep, stcb,
676f8829a4aSRandall Stewart 		    control,
677ceaad40aSRandall Stewart 		    &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
678830d754dSRandall Stewart 
679830d754dSRandall Stewart 		/*
680830d754dSRandall Stewart 		 * EY this is the chunk that should be tagged nr gapped
681830d754dSRandall Stewart 		 * calculate the gap and such then tag this TSN nr
682830d754dSRandall Stewart 		 * chk->rec.data.TSN_seq
683830d754dSRandall Stewart 		 */
684830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
685830d754dSRandall Stewart 
686830d754dSRandall Stewart 			if (nr_tsn >= asoc->nr_mapping_array_base_tsn) {
687830d754dSRandall Stewart 				nr_gap = nr_tsn - asoc->nr_mapping_array_base_tsn;
688830d754dSRandall Stewart 			} else {
689830d754dSRandall Stewart 				nr_gap = (MAX_TSN - asoc->nr_mapping_array_base_tsn) + nr_tsn + 1;
690830d754dSRandall Stewart 			}
691830d754dSRandall Stewart 			if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
692830d754dSRandall Stewart 			    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
693830d754dSRandall Stewart 				/*
694830d754dSRandall Stewart 				 * EY The 1st should never happen, as in
695830d754dSRandall Stewart 				 * process_a_data_chunk method this check
696830d754dSRandall Stewart 				 * should be done
697830d754dSRandall Stewart 				 */
698830d754dSRandall Stewart 				/*
699830d754dSRandall Stewart 				 * EY The 2nd should never happen, because
700830d754dSRandall Stewart 				 * nr_mapping_array is always expanded when
701830d754dSRandall Stewart 				 * mapping_array is expanded
702830d754dSRandall Stewart 				 */
703830d754dSRandall Stewart 			} else {
704830d754dSRandall Stewart 				SCTP_TCB_LOCK_ASSERT(stcb);
705830d754dSRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
7068933fa13SRandall Stewart 				if (compare_with_wrap(nr_tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
707830d754dSRandall Stewart 					asoc->highest_tsn_inside_nr_map = nr_tsn;
708830d754dSRandall Stewart 			}
709830d754dSRandall Stewart 		}
710f8829a4aSRandall Stewart 		control = TAILQ_FIRST(&strm->inqueue);
711f8829a4aSRandall Stewart 		while (control != NULL) {
712f8829a4aSRandall Stewart 			/* all delivered */
713f8829a4aSRandall Stewart 			nxt_todel = strm->last_sequence_delivered + 1;
714f8829a4aSRandall Stewart 			if (nxt_todel == control->sinfo_ssn) {
715f8829a4aSRandall Stewart 				at = TAILQ_NEXT(control, next);
716f8829a4aSRandall Stewart 				TAILQ_REMOVE(&strm->inqueue, control, next);
717f8829a4aSRandall Stewart 				asoc->size_on_all_streams -= control->length;
718f8829a4aSRandall Stewart 				sctp_ucount_decr(asoc->cnt_on_all_streams);
719f8829a4aSRandall Stewart 				strm->last_sequence_delivered++;
720f8829a4aSRandall Stewart 				/*
721f8829a4aSRandall Stewart 				 * We ignore the return of deliver_data here
722f8829a4aSRandall Stewart 				 * since we always can hold the chunk on the
723f8829a4aSRandall Stewart 				 * d-queue. And we have a finite number that
724f8829a4aSRandall Stewart 				 * can be delivered from the strq.
725f8829a4aSRandall Stewart 				 */
726b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
727f8829a4aSRandall Stewart 					sctp_log_strm_del(control, NULL,
728f8829a4aSRandall Stewart 					    SCTP_STR_LOG_FROM_IMMED_DEL);
72980fefe0aSRandall Stewart 				}
730830d754dSRandall Stewart 				/* EY will be used to calculate nr-gap */
731830d754dSRandall Stewart 				nr_tsn = control->sinfo_tsn;
732f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
733f8829a4aSRandall Stewart 				    control,
734ceaad40aSRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
735830d754dSRandall Stewart 				/*
736830d754dSRandall Stewart 				 * EY this is the chunk that should be
737830d754dSRandall Stewart 				 * tagged nr gapped calculate the gap and
738830d754dSRandall Stewart 				 * such then tag this TSN nr
739830d754dSRandall Stewart 				 * chk->rec.data.TSN_seq
740830d754dSRandall Stewart 				 */
741830d754dSRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
742830d754dSRandall Stewart 
743830d754dSRandall Stewart 					if (nr_tsn >= asoc->nr_mapping_array_base_tsn) {
744830d754dSRandall Stewart 						nr_gap = nr_tsn - asoc->nr_mapping_array_base_tsn;
745830d754dSRandall Stewart 					} else {
746830d754dSRandall Stewart 						nr_gap = (MAX_TSN - asoc->nr_mapping_array_base_tsn) + nr_tsn + 1;
747830d754dSRandall Stewart 					}
748830d754dSRandall Stewart 					if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
749830d754dSRandall Stewart 					    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
750830d754dSRandall Stewart 						/*
751830d754dSRandall Stewart 						 * EY The 1st should never
752830d754dSRandall Stewart 						 * happen, as in
753830d754dSRandall Stewart 						 * process_a_data_chunk
754830d754dSRandall Stewart 						 * method this check should
755830d754dSRandall Stewart 						 * be done
756830d754dSRandall Stewart 						 */
757830d754dSRandall Stewart 						/*
758830d754dSRandall Stewart 						 * EY The 2nd should never
759830d754dSRandall Stewart 						 * happen, because
760830d754dSRandall Stewart 						 * nr_mapping_array is
761830d754dSRandall Stewart 						 * always expanded when
762830d754dSRandall Stewart 						 * mapping_array is expanded
763830d754dSRandall Stewart 						 */
764830d754dSRandall Stewart 					} else {
765830d754dSRandall Stewart 						SCTP_TCB_LOCK_ASSERT(stcb);
766830d754dSRandall Stewart 						SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
7678933fa13SRandall Stewart 						if (compare_with_wrap(nr_tsn, asoc->highest_tsn_inside_nr_map,
7688933fa13SRandall Stewart 						    MAX_TSN))
769830d754dSRandall Stewart 							asoc->highest_tsn_inside_nr_map = nr_tsn;
770830d754dSRandall Stewart 					}
771830d754dSRandall Stewart 				}
772f8829a4aSRandall Stewart 				control = at;
773f8829a4aSRandall Stewart 				continue;
774f8829a4aSRandall Stewart 			}
775f8829a4aSRandall Stewart 			break;
776f8829a4aSRandall Stewart 		}
777f8829a4aSRandall Stewart 	}
778f8829a4aSRandall Stewart 	if (queue_needed) {
779f8829a4aSRandall Stewart 		/*
780f8829a4aSRandall Stewart 		 * Ok, we did not deliver this guy, find the correct place
781f8829a4aSRandall Stewart 		 * to put it on the queue.
782f8829a4aSRandall Stewart 		 */
783c40e9cf2SRandall Stewart 		if ((compare_with_wrap(asoc->cumulative_tsn,
784c40e9cf2SRandall Stewart 		    control->sinfo_tsn, MAX_TSN)) ||
785c40e9cf2SRandall Stewart 		    (control->sinfo_tsn == asoc->cumulative_tsn)) {
786c40e9cf2SRandall Stewart 			goto protocol_error;
787c40e9cf2SRandall Stewart 		}
788f8829a4aSRandall Stewart 		if (TAILQ_EMPTY(&strm->inqueue)) {
789f8829a4aSRandall Stewart 			/* Empty queue */
790b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
791f8829a4aSRandall Stewart 				sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INSERT_HD);
79280fefe0aSRandall Stewart 			}
793f8829a4aSRandall Stewart 			TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
794f8829a4aSRandall Stewart 		} else {
795f8829a4aSRandall Stewart 			TAILQ_FOREACH(at, &strm->inqueue, next) {
796f8829a4aSRandall Stewart 				if (compare_with_wrap(at->sinfo_ssn,
797f8829a4aSRandall Stewart 				    control->sinfo_ssn, MAX_SEQ)) {
798f8829a4aSRandall Stewart 					/*
799f8829a4aSRandall Stewart 					 * one in queue is bigger than the
800f8829a4aSRandall Stewart 					 * new one, insert before this one
801f8829a4aSRandall Stewart 					 */
802b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
803f8829a4aSRandall Stewart 						sctp_log_strm_del(control, at,
804f8829a4aSRandall Stewart 						    SCTP_STR_LOG_FROM_INSERT_MD);
80580fefe0aSRandall Stewart 					}
806f8829a4aSRandall Stewart 					TAILQ_INSERT_BEFORE(at, control, next);
807f8829a4aSRandall Stewart 					break;
808f8829a4aSRandall Stewart 				} else if (at->sinfo_ssn == control->sinfo_ssn) {
809f8829a4aSRandall Stewart 					/*
810f8829a4aSRandall Stewart 					 * Gak, He sent me a duplicate str
811f8829a4aSRandall Stewart 					 * seq number
812f8829a4aSRandall Stewart 					 */
813f8829a4aSRandall Stewart 					/*
814f8829a4aSRandall Stewart 					 * foo bar, I guess I will just free
815f8829a4aSRandall Stewart 					 * this new guy, should we abort
816f8829a4aSRandall Stewart 					 * too? FIX ME MAYBE? Or it COULD be
817f8829a4aSRandall Stewart 					 * that the SSN's have wrapped.
818f8829a4aSRandall Stewart 					 * Maybe I should compare to TSN
819f8829a4aSRandall Stewart 					 * somehow... sigh for now just blow
820f8829a4aSRandall Stewart 					 * away the chunk!
821f8829a4aSRandall Stewart 					 */
822f8829a4aSRandall Stewart 
823f8829a4aSRandall Stewart 					if (control->data)
824f8829a4aSRandall Stewart 						sctp_m_freem(control->data);
825f8829a4aSRandall Stewart 					control->data = NULL;
826f8829a4aSRandall Stewart 					asoc->size_on_all_streams -= control->length;
827f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->cnt_on_all_streams);
8285bead436SRandall Stewart 					if (control->whoFrom)
829f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
8305bead436SRandall Stewart 					control->whoFrom = NULL;
831f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
832f8829a4aSRandall Stewart 					return;
833f8829a4aSRandall Stewart 				} else {
834f8829a4aSRandall Stewart 					if (TAILQ_NEXT(at, next) == NULL) {
835f8829a4aSRandall Stewart 						/*
836f8829a4aSRandall Stewart 						 * We are at the end, insert
837f8829a4aSRandall Stewart 						 * it after this one
838f8829a4aSRandall Stewart 						 */
839b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
840f8829a4aSRandall Stewart 							sctp_log_strm_del(control, at,
841f8829a4aSRandall Stewart 							    SCTP_STR_LOG_FROM_INSERT_TL);
84280fefe0aSRandall Stewart 						}
843f8829a4aSRandall Stewart 						TAILQ_INSERT_AFTER(&strm->inqueue,
844f8829a4aSRandall Stewart 						    at, control, next);
845f8829a4aSRandall Stewart 						break;
846f8829a4aSRandall Stewart 					}
847f8829a4aSRandall Stewart 				}
848f8829a4aSRandall Stewart 			}
849f8829a4aSRandall Stewart 		}
850f8829a4aSRandall Stewart 	}
851f8829a4aSRandall Stewart }
852f8829a4aSRandall Stewart 
853f8829a4aSRandall Stewart /*
854f8829a4aSRandall Stewart  * Returns two things: You get the total size of the deliverable parts of the
855f8829a4aSRandall Stewart  * first fragmented message on the reassembly queue. And you get a 1 back if
856f8829a4aSRandall Stewart  * all of the message is ready or a 0 back if the message is still incomplete
857f8829a4aSRandall Stewart  */
858f8829a4aSRandall Stewart static int
859f8829a4aSRandall Stewart sctp_is_all_msg_on_reasm(struct sctp_association *asoc, uint32_t * t_size)
860f8829a4aSRandall Stewart {
861f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
862f8829a4aSRandall Stewart 	uint32_t tsn;
863f8829a4aSRandall Stewart 
864f8829a4aSRandall Stewart 	*t_size = 0;
865f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
866f8829a4aSRandall Stewart 	if (chk == NULL) {
867f8829a4aSRandall Stewart 		/* nothing on the queue */
868f8829a4aSRandall Stewart 		return (0);
869f8829a4aSRandall Stewart 	}
870f8829a4aSRandall Stewart 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
871f8829a4aSRandall Stewart 		/* Not a first on the queue */
872f8829a4aSRandall Stewart 		return (0);
873f8829a4aSRandall Stewart 	}
874f8829a4aSRandall Stewart 	tsn = chk->rec.data.TSN_seq;
875f8829a4aSRandall Stewart 	while (chk) {
876f8829a4aSRandall Stewart 		if (tsn != chk->rec.data.TSN_seq) {
877f8829a4aSRandall Stewart 			return (0);
878f8829a4aSRandall Stewart 		}
879f8829a4aSRandall Stewart 		*t_size += chk->send_size;
880f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
881f8829a4aSRandall Stewart 			return (1);
882f8829a4aSRandall Stewart 		}
883f8829a4aSRandall Stewart 		tsn++;
884f8829a4aSRandall Stewart 		chk = TAILQ_NEXT(chk, sctp_next);
885f8829a4aSRandall Stewart 	}
886f8829a4aSRandall Stewart 	return (0);
887f8829a4aSRandall Stewart }
888f8829a4aSRandall Stewart 
889f8829a4aSRandall Stewart static void
890f8829a4aSRandall Stewart sctp_deliver_reasm_check(struct sctp_tcb *stcb, struct sctp_association *asoc)
891f8829a4aSRandall Stewart {
892f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
893f8829a4aSRandall Stewart 	uint16_t nxt_todel;
894f8829a4aSRandall Stewart 	uint32_t tsize;
895f8829a4aSRandall Stewart 
896139bc87fSRandall Stewart doit_again:
897f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
898f8829a4aSRandall Stewart 	if (chk == NULL) {
899f8829a4aSRandall Stewart 		/* Huh? */
900f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = 0;
901f8829a4aSRandall Stewart 		asoc->cnt_on_reasm_queue = 0;
902f8829a4aSRandall Stewart 		return;
903f8829a4aSRandall Stewart 	}
904f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress == 0) {
905f8829a4aSRandall Stewart 		nxt_todel =
906f8829a4aSRandall Stewart 		    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
907f8829a4aSRandall Stewart 		if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
908f8829a4aSRandall Stewart 		    (nxt_todel == chk->rec.data.stream_seq ||
909f8829a4aSRandall Stewart 		    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
910f8829a4aSRandall Stewart 			/*
911f8829a4aSRandall Stewart 			 * Yep the first one is here and its ok to deliver
912f8829a4aSRandall Stewart 			 * but should we?
913f8829a4aSRandall Stewart 			 */
914f8829a4aSRandall Stewart 			if ((sctp_is_all_msg_on_reasm(asoc, &tsize) ||
915c4739e2fSRandall Stewart 			    (tsize >= stcb->sctp_ep->partial_delivery_point))) {
916f8829a4aSRandall Stewart 
917f8829a4aSRandall Stewart 				/*
918f8829a4aSRandall Stewart 				 * Yes, we setup to start reception, by
919f8829a4aSRandall Stewart 				 * backing down the TSN just in case we
920f8829a4aSRandall Stewart 				 * can't deliver. If we
921f8829a4aSRandall Stewart 				 */
922f8829a4aSRandall Stewart 				asoc->fragmented_delivery_inprogress = 1;
923f8829a4aSRandall Stewart 				asoc->tsn_last_delivered =
924f8829a4aSRandall Stewart 				    chk->rec.data.TSN_seq - 1;
925f8829a4aSRandall Stewart 				asoc->str_of_pdapi =
926f8829a4aSRandall Stewart 				    chk->rec.data.stream_number;
927f8829a4aSRandall Stewart 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
928f8829a4aSRandall Stewart 				asoc->pdapi_ppid = chk->rec.data.payloadtype;
929f8829a4aSRandall Stewart 				asoc->fragment_flags = chk->rec.data.rcv_flags;
930f8829a4aSRandall Stewart 				sctp_service_reassembly(stcb, asoc);
931f8829a4aSRandall Stewart 			}
932f8829a4aSRandall Stewart 		}
933f8829a4aSRandall Stewart 	} else {
934139bc87fSRandall Stewart 		/*
935139bc87fSRandall Stewart 		 * Service re-assembly will deliver stream data queued at
936139bc87fSRandall Stewart 		 * the end of fragmented delivery.. but it wont know to go
937139bc87fSRandall Stewart 		 * back and call itself again... we do that here with the
938139bc87fSRandall Stewart 		 * got doit_again
939139bc87fSRandall Stewart 		 */
940f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
941139bc87fSRandall Stewart 		if (asoc->fragmented_delivery_inprogress == 0) {
942139bc87fSRandall Stewart 			/*
943139bc87fSRandall Stewart 			 * finished our Fragmented delivery, could be more
944139bc87fSRandall Stewart 			 * waiting?
945139bc87fSRandall Stewart 			 */
946139bc87fSRandall Stewart 			goto doit_again;
947139bc87fSRandall Stewart 		}
948f8829a4aSRandall Stewart 	}
949f8829a4aSRandall Stewart }
950f8829a4aSRandall Stewart 
951f8829a4aSRandall Stewart /*
952f8829a4aSRandall Stewart  * Dump onto the re-assembly queue, in its proper place. After dumping on the
953f8829a4aSRandall Stewart  * queue, see if anthing can be delivered. If so pull it off (or as much as
954f8829a4aSRandall Stewart  * we can. If we run out of space then we must dump what we can and set the
955f8829a4aSRandall Stewart  * appropriate flag to say we queued what we could.
956f8829a4aSRandall Stewart  */
957f8829a4aSRandall Stewart static void
958f8829a4aSRandall Stewart sctp_queue_data_for_reasm(struct sctp_tcb *stcb, struct sctp_association *asoc,
959f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk, int *abort_flag)
960f8829a4aSRandall Stewart {
961f8829a4aSRandall Stewart 	struct mbuf *oper;
962f8829a4aSRandall Stewart 	uint32_t cum_ackp1, last_tsn, prev_tsn, post_tsn;
963f8829a4aSRandall Stewart 	u_char last_flags;
964f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *at, *prev, *next;
965f8829a4aSRandall Stewart 
966f8829a4aSRandall Stewart 	prev = next = NULL;
967f8829a4aSRandall Stewart 	cum_ackp1 = asoc->tsn_last_delivered + 1;
968f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->reasmqueue)) {
969f8829a4aSRandall Stewart 		/* This is the first one on the queue */
970f8829a4aSRandall Stewart 		TAILQ_INSERT_HEAD(&asoc->reasmqueue, chk, sctp_next);
971f8829a4aSRandall Stewart 		/*
972f8829a4aSRandall Stewart 		 * we do not check for delivery of anything when only one
973f8829a4aSRandall Stewart 		 * fragment is here
974f8829a4aSRandall Stewart 		 */
975f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = chk->send_size;
976f8829a4aSRandall Stewart 		sctp_ucount_incr(asoc->cnt_on_reasm_queue);
977f8829a4aSRandall Stewart 		if (chk->rec.data.TSN_seq == cum_ackp1) {
978f8829a4aSRandall Stewart 			if (asoc->fragmented_delivery_inprogress == 0 &&
979f8829a4aSRandall Stewart 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) !=
980f8829a4aSRandall Stewart 			    SCTP_DATA_FIRST_FRAG) {
981f8829a4aSRandall Stewart 				/*
982f8829a4aSRandall Stewart 				 * An empty queue, no delivery inprogress,
983f8829a4aSRandall Stewart 				 * we hit the next one and it does NOT have
984f8829a4aSRandall Stewart 				 * a FIRST fragment mark.
985f8829a4aSRandall Stewart 				 */
986ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not first, no fragmented delivery in progress\n");
987139bc87fSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
988f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
989f8829a4aSRandall Stewart 
990f8829a4aSRandall Stewart 				if (oper) {
991f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
992f8829a4aSRandall Stewart 					uint32_t *ippp;
993f8829a4aSRandall Stewart 
994139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) =
995f8829a4aSRandall Stewart 					    sizeof(struct sctp_paramhdr) +
996f8829a4aSRandall Stewart 					    (sizeof(uint32_t) * 3);
997f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
998f8829a4aSRandall Stewart 					ph->param_type =
999f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1000139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
1001f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
1002a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_2);
1003f8829a4aSRandall Stewart 					ippp++;
1004f8829a4aSRandall Stewart 					*ippp = chk->rec.data.TSN_seq;
1005f8829a4aSRandall Stewart 					ippp++;
1006f8829a4aSRandall Stewart 					*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1007f8829a4aSRandall Stewart 
1008f8829a4aSRandall Stewart 				}
1009a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_2;
1010f8829a4aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb,
1011ceaad40aSRandall Stewart 				    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1012f8829a4aSRandall Stewart 				*abort_flag = 1;
1013f8829a4aSRandall Stewart 			} else if (asoc->fragmented_delivery_inprogress &&
1014f8829a4aSRandall Stewart 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
1015f8829a4aSRandall Stewart 				/*
1016f8829a4aSRandall Stewart 				 * We are doing a partial delivery and the
1017f8829a4aSRandall Stewart 				 * NEXT chunk MUST be either the LAST or
1018f8829a4aSRandall Stewart 				 * MIDDLE fragment NOT a FIRST
1019f8829a4aSRandall Stewart 				 */
1020ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS a first and fragmented delivery in progress\n");
1021139bc87fSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1022f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
1023f8829a4aSRandall Stewart 				if (oper) {
1024f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
1025f8829a4aSRandall Stewart 					uint32_t *ippp;
1026f8829a4aSRandall Stewart 
1027139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) =
1028f8829a4aSRandall Stewart 					    sizeof(struct sctp_paramhdr) +
1029f8829a4aSRandall Stewart 					    (3 * sizeof(uint32_t));
1030f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
1031f8829a4aSRandall Stewart 					ph->param_type =
1032f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1033139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
1034f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
1035a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_3);
1036f8829a4aSRandall Stewart 					ippp++;
1037f8829a4aSRandall Stewart 					*ippp = chk->rec.data.TSN_seq;
1038f8829a4aSRandall Stewart 					ippp++;
1039f8829a4aSRandall Stewart 					*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1040f8829a4aSRandall Stewart 				}
1041a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_3;
1042f8829a4aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb,
1043ceaad40aSRandall Stewart 				    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1044f8829a4aSRandall Stewart 				*abort_flag = 1;
1045f8829a4aSRandall Stewart 			} else if (asoc->fragmented_delivery_inprogress) {
1046f8829a4aSRandall Stewart 				/*
1047f8829a4aSRandall Stewart 				 * Here we are ok with a MIDDLE or LAST
1048f8829a4aSRandall Stewart 				 * piece
1049f8829a4aSRandall Stewart 				 */
1050f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
1051f8829a4aSRandall Stewart 				    asoc->str_of_pdapi) {
1052f8829a4aSRandall Stewart 					/* Got to be the right STR No */
1053ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream number %d vs %d\n",
1054f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
1055f8829a4aSRandall Stewart 					    asoc->str_of_pdapi);
1056139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1057f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1058f8829a4aSRandall Stewart 					if (oper) {
1059f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1060f8829a4aSRandall Stewart 						uint32_t *ippp;
1061f8829a4aSRandall Stewart 
1062139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1063f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1064f8829a4aSRandall Stewart 						    (sizeof(uint32_t) * 3);
1065f8829a4aSRandall Stewart 						ph = mtod(oper,
1066f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1067f8829a4aSRandall Stewart 						ph->param_type =
1068f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1069f8829a4aSRandall Stewart 						ph->param_length =
1070139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1071f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1072a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_4);
1073f8829a4aSRandall Stewart 						ippp++;
1074f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1075f8829a4aSRandall Stewart 						ippp++;
1076f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1077f8829a4aSRandall Stewart 					}
1078a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_4;
1079f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1080ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1081f8829a4aSRandall Stewart 					*abort_flag = 1;
1082f8829a4aSRandall Stewart 				} else if ((asoc->fragment_flags & SCTP_DATA_UNORDERED) !=
1083f8829a4aSRandall Stewart 					    SCTP_DATA_UNORDERED &&
1084f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq !=
1085f8829a4aSRandall Stewart 				    asoc->ssn_of_pdapi) {
1086f8829a4aSRandall Stewart 					/* Got to be the right STR Seq */
1087ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream seq %d vs %d\n",
1088f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
1089f8829a4aSRandall Stewart 					    asoc->ssn_of_pdapi);
1090139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1091f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1092f8829a4aSRandall Stewart 					if (oper) {
1093f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1094f8829a4aSRandall Stewart 						uint32_t *ippp;
1095f8829a4aSRandall Stewart 
1096139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1097f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1098f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1099f8829a4aSRandall Stewart 						ph = mtod(oper,
1100f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1101f8829a4aSRandall Stewart 						ph->param_type =
1102f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1103f8829a4aSRandall Stewart 						ph->param_length =
1104139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1105f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1106a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_5);
1107f8829a4aSRandall Stewart 						ippp++;
1108f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1109f8829a4aSRandall Stewart 						ippp++;
1110f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1111f8829a4aSRandall Stewart 
1112f8829a4aSRandall Stewart 					}
1113a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_5;
1114f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1115ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1116f8829a4aSRandall Stewart 					*abort_flag = 1;
1117f8829a4aSRandall Stewart 				}
1118f8829a4aSRandall Stewart 			}
1119f8829a4aSRandall Stewart 		}
1120f8829a4aSRandall Stewart 		return;
1121f8829a4aSRandall Stewart 	}
1122f8829a4aSRandall Stewart 	/* Find its place */
1123f8829a4aSRandall Stewart 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
1124f8829a4aSRandall Stewart 		if (compare_with_wrap(at->rec.data.TSN_seq,
1125f8829a4aSRandall Stewart 		    chk->rec.data.TSN_seq, MAX_TSN)) {
1126f8829a4aSRandall Stewart 			/*
1127f8829a4aSRandall Stewart 			 * one in queue is bigger than the new one, insert
1128f8829a4aSRandall Stewart 			 * before this one
1129f8829a4aSRandall Stewart 			 */
1130f8829a4aSRandall Stewart 			/* A check */
1131f8829a4aSRandall Stewart 			asoc->size_on_reasm_queue += chk->send_size;
1132f8829a4aSRandall Stewart 			sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1133f8829a4aSRandall Stewart 			next = at;
1134f8829a4aSRandall Stewart 			TAILQ_INSERT_BEFORE(at, chk, sctp_next);
1135f8829a4aSRandall Stewart 			break;
1136f8829a4aSRandall Stewart 		} else if (at->rec.data.TSN_seq == chk->rec.data.TSN_seq) {
1137f8829a4aSRandall Stewart 			/* Gak, He sent me a duplicate str seq number */
1138f8829a4aSRandall Stewart 			/*
1139f8829a4aSRandall Stewart 			 * foo bar, I guess I will just free this new guy,
1140f8829a4aSRandall Stewart 			 * should we abort too? FIX ME MAYBE? Or it COULD be
1141f8829a4aSRandall Stewart 			 * that the SSN's have wrapped. Maybe I should
1142f8829a4aSRandall Stewart 			 * compare to TSN somehow... sigh for now just blow
1143f8829a4aSRandall Stewart 			 * away the chunk!
1144f8829a4aSRandall Stewart 			 */
1145f8829a4aSRandall Stewart 			if (chk->data) {
1146f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
1147f8829a4aSRandall Stewart 				chk->data = NULL;
1148f8829a4aSRandall Stewart 			}
1149f8829a4aSRandall Stewart 			sctp_free_a_chunk(stcb, chk);
1150f8829a4aSRandall Stewart 			return;
1151f8829a4aSRandall Stewart 		} else {
1152f8829a4aSRandall Stewart 			last_flags = at->rec.data.rcv_flags;
1153f8829a4aSRandall Stewart 			last_tsn = at->rec.data.TSN_seq;
1154f8829a4aSRandall Stewart 			prev = at;
1155f8829a4aSRandall Stewart 			if (TAILQ_NEXT(at, sctp_next) == NULL) {
1156f8829a4aSRandall Stewart 				/*
1157f8829a4aSRandall Stewart 				 * We are at the end, insert it after this
1158f8829a4aSRandall Stewart 				 * one
1159f8829a4aSRandall Stewart 				 */
1160f8829a4aSRandall Stewart 				/* check it first */
1161f8829a4aSRandall Stewart 				asoc->size_on_reasm_queue += chk->send_size;
1162f8829a4aSRandall Stewart 				sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1163f8829a4aSRandall Stewart 				TAILQ_INSERT_AFTER(&asoc->reasmqueue, at, chk, sctp_next);
1164f8829a4aSRandall Stewart 				break;
1165f8829a4aSRandall Stewart 			}
1166f8829a4aSRandall Stewart 		}
1167f8829a4aSRandall Stewart 	}
1168f8829a4aSRandall Stewart 	/* Now the audits */
1169f8829a4aSRandall Stewart 	if (prev) {
1170f8829a4aSRandall Stewart 		prev_tsn = chk->rec.data.TSN_seq - 1;
1171f8829a4aSRandall Stewart 		if (prev_tsn == prev->rec.data.TSN_seq) {
1172f8829a4aSRandall Stewart 			/*
1173f8829a4aSRandall Stewart 			 * Ok the one I am dropping onto the end is the
1174f8829a4aSRandall Stewart 			 * NEXT. A bit of valdiation here.
1175f8829a4aSRandall Stewart 			 */
1176f8829a4aSRandall Stewart 			if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1177f8829a4aSRandall Stewart 			    SCTP_DATA_FIRST_FRAG ||
1178f8829a4aSRandall Stewart 			    (prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1179f8829a4aSRandall Stewart 			    SCTP_DATA_MIDDLE_FRAG) {
1180f8829a4aSRandall Stewart 				/*
1181f8829a4aSRandall Stewart 				 * Insert chk MUST be a MIDDLE or LAST
1182f8829a4aSRandall Stewart 				 * fragment
1183f8829a4aSRandall Stewart 				 */
1184f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1185f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1186ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - It can be a midlle or last but not a first\n");
1187ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it's a FIRST!\n");
1188139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1189f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1190f8829a4aSRandall Stewart 					if (oper) {
1191f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1192f8829a4aSRandall Stewart 						uint32_t *ippp;
1193f8829a4aSRandall Stewart 
1194139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1195f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1196f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1197f8829a4aSRandall Stewart 						ph = mtod(oper,
1198f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1199f8829a4aSRandall Stewart 						ph->param_type =
1200f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1201f8829a4aSRandall Stewart 						ph->param_length =
1202139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1203f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1204a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_6);
1205f8829a4aSRandall Stewart 						ippp++;
1206f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1207f8829a4aSRandall Stewart 						ippp++;
1208f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1209f8829a4aSRandall Stewart 
1210f8829a4aSRandall Stewart 					}
1211a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_6;
1212f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1213ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1214f8829a4aSRandall Stewart 					*abort_flag = 1;
1215f8829a4aSRandall Stewart 					return;
1216f8829a4aSRandall Stewart 				}
1217f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
1218f8829a4aSRandall Stewart 				    prev->rec.data.stream_number) {
1219f8829a4aSRandall Stewart 					/*
1220f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1221f8829a4aSRandall Stewart 					 * they must be the same.
1222f8829a4aSRandall Stewart 					 */
1223ad81507eSRandall Stewart 					SCTP_PRINTF("Prev check - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1224f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
1225f8829a4aSRandall Stewart 					    prev->rec.data.stream_number);
1226139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1227f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1228f8829a4aSRandall Stewart 					if (oper) {
1229f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1230f8829a4aSRandall Stewart 						uint32_t *ippp;
1231f8829a4aSRandall Stewart 
1232139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1233f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1234f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1235f8829a4aSRandall Stewart 						ph = mtod(oper,
1236f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1237f8829a4aSRandall Stewart 						ph->param_type =
1238f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1239f8829a4aSRandall Stewart 						ph->param_length =
1240139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1241f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1242a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_7);
1243f8829a4aSRandall Stewart 						ippp++;
1244f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1245f8829a4aSRandall Stewart 						ippp++;
1246f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1247f8829a4aSRandall Stewart 					}
1248a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_7;
1249f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1250ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1251f8829a4aSRandall Stewart 
1252f8829a4aSRandall Stewart 					*abort_flag = 1;
1253f8829a4aSRandall Stewart 					return;
1254f8829a4aSRandall Stewart 				}
1255f8829a4aSRandall Stewart 				if ((prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1256f8829a4aSRandall Stewart 				    chk->rec.data.stream_seq !=
1257f8829a4aSRandall Stewart 				    prev->rec.data.stream_seq) {
1258f8829a4aSRandall Stewart 					/*
1259f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1260f8829a4aSRandall Stewart 					 * they must be the same.
1261f8829a4aSRandall Stewart 					 */
1262ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1263f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
1264f8829a4aSRandall Stewart 					    prev->rec.data.stream_seq);
1265139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1266f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1267f8829a4aSRandall Stewart 					if (oper) {
1268f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1269f8829a4aSRandall Stewart 						uint32_t *ippp;
1270f8829a4aSRandall Stewart 
1271139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1272f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1273f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1274f8829a4aSRandall Stewart 						ph = mtod(oper,
1275f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1276f8829a4aSRandall Stewart 						ph->param_type =
1277f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1278f8829a4aSRandall Stewart 						ph->param_length =
1279139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1280f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1281a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_8);
1282f8829a4aSRandall Stewart 						ippp++;
1283f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1284f8829a4aSRandall Stewart 						ippp++;
1285f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1286f8829a4aSRandall Stewart 					}
1287a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_8;
1288f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1289ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1290f8829a4aSRandall Stewart 
1291f8829a4aSRandall Stewart 					*abort_flag = 1;
1292f8829a4aSRandall Stewart 					return;
1293f8829a4aSRandall Stewart 				}
1294f8829a4aSRandall Stewart 			} else if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1295f8829a4aSRandall Stewart 			    SCTP_DATA_LAST_FRAG) {
1296f8829a4aSRandall Stewart 				/* Insert chk MUST be a FIRST */
1297f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1298f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1299ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, evil plot, its not FIRST and it must be!\n");
1300139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1301f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1302f8829a4aSRandall Stewart 					if (oper) {
1303f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1304f8829a4aSRandall Stewart 						uint32_t *ippp;
1305f8829a4aSRandall Stewart 
1306139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1307f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1308f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1309f8829a4aSRandall Stewart 						ph = mtod(oper,
1310f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1311f8829a4aSRandall Stewart 						ph->param_type =
1312f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1313f8829a4aSRandall Stewart 						ph->param_length =
1314139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1315f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1316a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_9);
1317f8829a4aSRandall Stewart 						ippp++;
1318f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1319f8829a4aSRandall Stewart 						ippp++;
1320f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1321f8829a4aSRandall Stewart 
1322f8829a4aSRandall Stewart 					}
1323a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_9;
1324f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1325ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1326f8829a4aSRandall Stewart 
1327f8829a4aSRandall Stewart 					*abort_flag = 1;
1328f8829a4aSRandall Stewart 					return;
1329f8829a4aSRandall Stewart 				}
1330f8829a4aSRandall Stewart 			}
1331f8829a4aSRandall Stewart 		}
1332f8829a4aSRandall Stewart 	}
1333f8829a4aSRandall Stewart 	if (next) {
1334f8829a4aSRandall Stewart 		post_tsn = chk->rec.data.TSN_seq + 1;
1335f8829a4aSRandall Stewart 		if (post_tsn == next->rec.data.TSN_seq) {
1336f8829a4aSRandall Stewart 			/*
1337f8829a4aSRandall Stewart 			 * Ok the one I am inserting ahead of is my NEXT
1338f8829a4aSRandall Stewart 			 * one. A bit of valdiation here.
1339f8829a4aSRandall Stewart 			 */
1340f8829a4aSRandall Stewart 			if (next->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
1341f8829a4aSRandall Stewart 				/* Insert chk MUST be a last fragment */
1342f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK)
1343f8829a4aSRandall Stewart 				    != SCTP_DATA_LAST_FRAG) {
1344ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is FIRST, we must be LAST\n");
1345ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not a last!\n");
1346139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1347f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1348f8829a4aSRandall Stewart 					if (oper) {
1349f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1350f8829a4aSRandall Stewart 						uint32_t *ippp;
1351f8829a4aSRandall Stewart 
1352139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1353f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1354f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1355f8829a4aSRandall Stewart 						ph = mtod(oper,
1356f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1357f8829a4aSRandall Stewart 						ph->param_type =
1358f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1359f8829a4aSRandall Stewart 						ph->param_length =
1360139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1361f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1362a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_10);
1363f8829a4aSRandall Stewart 						ippp++;
1364f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1365f8829a4aSRandall Stewart 						ippp++;
1366f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1367f8829a4aSRandall Stewart 					}
1368a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_10;
1369f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1370ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1371f8829a4aSRandall Stewart 
1372f8829a4aSRandall Stewart 					*abort_flag = 1;
1373f8829a4aSRandall Stewart 					return;
1374f8829a4aSRandall Stewart 				}
1375f8829a4aSRandall Stewart 			} else if ((next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1376f8829a4aSRandall Stewart 				    SCTP_DATA_MIDDLE_FRAG ||
1377f8829a4aSRandall Stewart 				    (next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1378f8829a4aSRandall Stewart 			    SCTP_DATA_LAST_FRAG) {
1379f8829a4aSRandall Stewart 				/*
1380f8829a4aSRandall Stewart 				 * Insert chk CAN be MIDDLE or FIRST NOT
1381f8829a4aSRandall Stewart 				 * LAST
1382f8829a4aSRandall Stewart 				 */
1383f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1384f8829a4aSRandall Stewart 				    SCTP_DATA_LAST_FRAG) {
1385ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is a MIDDLE/LAST\n");
1386ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, new prev chunk is a LAST\n");
1387139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1388f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1389f8829a4aSRandall Stewart 					if (oper) {
1390f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1391f8829a4aSRandall Stewart 						uint32_t *ippp;
1392f8829a4aSRandall Stewart 
1393139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1394f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1395f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1396f8829a4aSRandall Stewart 						ph = mtod(oper,
1397f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1398f8829a4aSRandall Stewart 						ph->param_type =
1399f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1400f8829a4aSRandall Stewart 						ph->param_length =
1401139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1402f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1403a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_11);
1404f8829a4aSRandall Stewart 						ippp++;
1405f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1406f8829a4aSRandall Stewart 						ippp++;
1407f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1408f8829a4aSRandall Stewart 
1409f8829a4aSRandall Stewart 					}
1410a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_11;
1411f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1412ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1413f8829a4aSRandall Stewart 
1414f8829a4aSRandall Stewart 					*abort_flag = 1;
1415f8829a4aSRandall Stewart 					return;
1416f8829a4aSRandall Stewart 				}
1417f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
1418f8829a4aSRandall Stewart 				    next->rec.data.stream_number) {
1419f8829a4aSRandall Stewart 					/*
1420f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1421f8829a4aSRandall Stewart 					 * they must be the same.
1422f8829a4aSRandall Stewart 					 */
1423ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1424f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
1425f8829a4aSRandall Stewart 					    next->rec.data.stream_number);
1426139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1427f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1428f8829a4aSRandall Stewart 					if (oper) {
1429f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1430f8829a4aSRandall Stewart 						uint32_t *ippp;
1431f8829a4aSRandall Stewart 
1432139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1433f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1434f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1435f8829a4aSRandall Stewart 						ph = mtod(oper,
1436f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1437f8829a4aSRandall Stewart 						ph->param_type =
1438f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1439f8829a4aSRandall Stewart 						ph->param_length =
1440139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1441f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1442a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_12);
1443f8829a4aSRandall Stewart 						ippp++;
1444f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1445f8829a4aSRandall Stewart 						ippp++;
1446f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1447f8829a4aSRandall Stewart 
1448f8829a4aSRandall Stewart 					}
1449a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_12;
1450f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1451ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1452f8829a4aSRandall Stewart 
1453f8829a4aSRandall Stewart 					*abort_flag = 1;
1454f8829a4aSRandall Stewart 					return;
1455f8829a4aSRandall Stewart 				}
1456f8829a4aSRandall Stewart 				if ((next->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1457f8829a4aSRandall Stewart 				    chk->rec.data.stream_seq !=
1458f8829a4aSRandall Stewart 				    next->rec.data.stream_seq) {
1459f8829a4aSRandall Stewart 					/*
1460f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1461f8829a4aSRandall Stewart 					 * they must be the same.
1462f8829a4aSRandall Stewart 					 */
1463ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1464f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
1465f8829a4aSRandall Stewart 					    next->rec.data.stream_seq);
1466139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1467f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1468f8829a4aSRandall Stewart 					if (oper) {
1469f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1470f8829a4aSRandall Stewart 						uint32_t *ippp;
1471f8829a4aSRandall Stewart 
1472139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1473f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1474f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1475f8829a4aSRandall Stewart 						ph = mtod(oper,
1476f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1477f8829a4aSRandall Stewart 						ph->param_type =
1478f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1479f8829a4aSRandall Stewart 						ph->param_length =
1480139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1481f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1482a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_13);
1483f8829a4aSRandall Stewart 						ippp++;
1484f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1485f8829a4aSRandall Stewart 						ippp++;
1486f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1487f8829a4aSRandall Stewart 					}
1488a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_13;
1489f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1490ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1491f8829a4aSRandall Stewart 
1492f8829a4aSRandall Stewart 					*abort_flag = 1;
1493f8829a4aSRandall Stewart 					return;
1494f8829a4aSRandall Stewart 				}
1495f8829a4aSRandall Stewart 			}
1496f8829a4aSRandall Stewart 		}
1497f8829a4aSRandall Stewart 	}
1498f8829a4aSRandall Stewart 	/* Do we need to do some delivery? check */
1499f8829a4aSRandall Stewart 	sctp_deliver_reasm_check(stcb, asoc);
1500f8829a4aSRandall Stewart }
1501f8829a4aSRandall Stewart 
1502f8829a4aSRandall Stewart /*
1503f8829a4aSRandall Stewart  * This is an unfortunate routine. It checks to make sure a evil guy is not
1504f8829a4aSRandall Stewart  * stuffing us full of bad packet fragments. A broken peer could also do this
1505f8829a4aSRandall Stewart  * but this is doubtful. It is to bad I must worry about evil crackers sigh
1506f8829a4aSRandall Stewart  * :< more cycles.
1507f8829a4aSRandall Stewart  */
1508f8829a4aSRandall Stewart static int
1509f8829a4aSRandall Stewart sctp_does_tsn_belong_to_reasm(struct sctp_association *asoc,
1510f8829a4aSRandall Stewart     uint32_t TSN_seq)
1511f8829a4aSRandall Stewart {
1512f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *at;
1513f8829a4aSRandall Stewart 	uint32_t tsn_est;
1514f8829a4aSRandall Stewart 
1515f8829a4aSRandall Stewart 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
1516f8829a4aSRandall Stewart 		if (compare_with_wrap(TSN_seq,
1517f8829a4aSRandall Stewart 		    at->rec.data.TSN_seq, MAX_TSN)) {
1518f8829a4aSRandall Stewart 			/* is it one bigger? */
1519f8829a4aSRandall Stewart 			tsn_est = at->rec.data.TSN_seq + 1;
1520f8829a4aSRandall Stewart 			if (tsn_est == TSN_seq) {
1521f8829a4aSRandall Stewart 				/* yep. It better be a last then */
1522f8829a4aSRandall Stewart 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1523f8829a4aSRandall Stewart 				    SCTP_DATA_LAST_FRAG) {
1524f8829a4aSRandall Stewart 					/*
1525f8829a4aSRandall Stewart 					 * Ok this guy belongs next to a guy
1526f8829a4aSRandall Stewart 					 * that is NOT last, it should be a
1527f8829a4aSRandall Stewart 					 * middle/last, not a complete
1528f8829a4aSRandall Stewart 					 * chunk.
1529f8829a4aSRandall Stewart 					 */
1530f8829a4aSRandall Stewart 					return (1);
1531f8829a4aSRandall Stewart 				} else {
1532f8829a4aSRandall Stewart 					/*
1533f8829a4aSRandall Stewart 					 * This guy is ok since its a LAST
1534f8829a4aSRandall Stewart 					 * and the new chunk is a fully
1535f8829a4aSRandall Stewart 					 * self- contained one.
1536f8829a4aSRandall Stewart 					 */
1537f8829a4aSRandall Stewart 					return (0);
1538f8829a4aSRandall Stewart 				}
1539f8829a4aSRandall Stewart 			}
1540f8829a4aSRandall Stewart 		} else if (TSN_seq == at->rec.data.TSN_seq) {
1541f8829a4aSRandall Stewart 			/* Software error since I have a dup? */
1542f8829a4aSRandall Stewart 			return (1);
1543f8829a4aSRandall Stewart 		} else {
1544f8829a4aSRandall Stewart 			/*
1545f8829a4aSRandall Stewart 			 * Ok, 'at' is larger than new chunk but does it
1546f8829a4aSRandall Stewart 			 * need to be right before it.
1547f8829a4aSRandall Stewart 			 */
1548f8829a4aSRandall Stewart 			tsn_est = TSN_seq + 1;
1549f8829a4aSRandall Stewart 			if (tsn_est == at->rec.data.TSN_seq) {
1550f8829a4aSRandall Stewart 				/* Yep, It better be a first */
1551f8829a4aSRandall Stewart 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1552f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1553f8829a4aSRandall Stewart 					return (1);
1554f8829a4aSRandall Stewart 				} else {
1555f8829a4aSRandall Stewart 					return (0);
1556f8829a4aSRandall Stewart 				}
1557f8829a4aSRandall Stewart 			}
1558f8829a4aSRandall Stewart 		}
1559f8829a4aSRandall Stewart 	}
1560f8829a4aSRandall Stewart 	return (0);
1561f8829a4aSRandall Stewart }
1562f8829a4aSRandall Stewart 
1563f8829a4aSRandall Stewart 
1564f8829a4aSRandall Stewart static int
1565f8829a4aSRandall Stewart sctp_process_a_data_chunk(struct sctp_tcb *stcb, struct sctp_association *asoc,
1566f8829a4aSRandall Stewart     struct mbuf **m, int offset, struct sctp_data_chunk *ch, int chk_length,
1567f8829a4aSRandall Stewart     struct sctp_nets *net, uint32_t * high_tsn, int *abort_flag,
1568f8829a4aSRandall Stewart     int *break_flag, int last_chunk)
1569f8829a4aSRandall Stewart {
1570f8829a4aSRandall Stewart 	/* Process a data chunk */
1571f8829a4aSRandall Stewart 	/* struct sctp_tmit_chunk *chk; */
1572f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
1573f8829a4aSRandall Stewart 	uint32_t tsn, gap;
1574830d754dSRandall Stewart 
1575830d754dSRandall Stewart 	/* EY - for nr_sack */
1576830d754dSRandall Stewart 	uint32_t nr_gap;
1577f8829a4aSRandall Stewart 	struct mbuf *dmbuf;
1578f8829a4aSRandall Stewart 	int indx, the_len;
1579139bc87fSRandall Stewart 	int need_reasm_check = 0;
1580f8829a4aSRandall Stewart 	uint16_t strmno, strmseq;
1581f8829a4aSRandall Stewart 	struct mbuf *oper;
1582f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
1583f42a358aSRandall Stewart 	int ordered;
1584f42a358aSRandall Stewart 	uint32_t protocol_id;
1585f42a358aSRandall Stewart 	uint8_t chunk_flags;
158617205eccSRandall Stewart 	struct sctp_stream_reset_list *liste;
1587f8829a4aSRandall Stewart 
1588f8829a4aSRandall Stewart 	chk = NULL;
1589f8829a4aSRandall Stewart 	tsn = ntohl(ch->dp.tsn);
1590f42a358aSRandall Stewart 	chunk_flags = ch->ch.chunk_flags;
1591b3f1ea41SRandall Stewart 	if ((chunk_flags & SCTP_DATA_SACK_IMMEDIATELY) == SCTP_DATA_SACK_IMMEDIATELY) {
1592b3f1ea41SRandall Stewart 		asoc->send_sack = 1;
1593b3f1ea41SRandall Stewart 	}
1594f42a358aSRandall Stewart 	protocol_id = ch->dp.protocol_id;
1595f42a358aSRandall Stewart 	ordered = ((ch->ch.chunk_flags & SCTP_DATA_UNORDERED) == 0);
1596b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1597c4739e2fSRandall Stewart 		sctp_log_map(tsn, asoc->cumulative_tsn, asoc->highest_tsn_inside_map, SCTP_MAP_TSN_ENTERS);
159880fefe0aSRandall Stewart 	}
1599ad81507eSRandall Stewart 	if (stcb == NULL) {
1600ad81507eSRandall Stewart 		return (0);
1601ad81507eSRandall Stewart 	}
160280fefe0aSRandall Stewart 	SCTP_LTRACE_CHK(stcb->sctp_ep, stcb, ch->ch.chunk_type, tsn);
1603f8829a4aSRandall Stewart 	if (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN) ||
1604f8829a4aSRandall Stewart 	    asoc->cumulative_tsn == tsn) {
1605f8829a4aSRandall Stewart 		/* It is a duplicate */
1606f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvdupdata);
1607f8829a4aSRandall Stewart 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1608f8829a4aSRandall Stewart 			/* Record a dup for the next outbound sack */
1609f8829a4aSRandall Stewart 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1610f8829a4aSRandall Stewart 			asoc->numduptsns++;
1611f8829a4aSRandall Stewart 		}
1612b201f536SRandall Stewart 		asoc->send_sack = 1;
1613f8829a4aSRandall Stewart 		return (0);
1614f8829a4aSRandall Stewart 	}
1615f8829a4aSRandall Stewart 	/* Calculate the number of TSN's between the base and this TSN */
1616f8829a4aSRandall Stewart 	if (tsn >= asoc->mapping_array_base_tsn) {
1617f8829a4aSRandall Stewart 		gap = tsn - asoc->mapping_array_base_tsn;
1618f8829a4aSRandall Stewart 	} else {
1619f8829a4aSRandall Stewart 		gap = (MAX_TSN - asoc->mapping_array_base_tsn) + tsn + 1;
1620f8829a4aSRandall Stewart 	}
1621f8829a4aSRandall Stewart 	if (gap >= (SCTP_MAPPING_ARRAY << 3)) {
1622f8829a4aSRandall Stewart 		/* Can't hold the bit in the mapping at max array, toss it */
1623f8829a4aSRandall Stewart 		return (0);
1624f8829a4aSRandall Stewart 	}
1625f8829a4aSRandall Stewart 	if (gap >= (uint32_t) (asoc->mapping_array_size << 3)) {
1626207304d4SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
16270696e120SRandall Stewart 		if (sctp_expand_mapping_array(asoc, gap)) {
1628f8829a4aSRandall Stewart 			/* Can't expand, drop it */
1629f8829a4aSRandall Stewart 			return (0);
1630f8829a4aSRandall Stewart 		}
1631f8829a4aSRandall Stewart 	}
1632830d754dSRandall Stewart 	/* EY - for nr_sack */
1633830d754dSRandall Stewart 	nr_gap = gap;
1634830d754dSRandall Stewart 
1635f8829a4aSRandall Stewart 	if (compare_with_wrap(tsn, *high_tsn, MAX_TSN)) {
1636f8829a4aSRandall Stewart 		*high_tsn = tsn;
1637f8829a4aSRandall Stewart 	}
1638f8829a4aSRandall Stewart 	/* See if we have received this one already */
1639f8829a4aSRandall Stewart 	if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
1640f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvdupdata);
1641f8829a4aSRandall Stewart 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1642f8829a4aSRandall Stewart 			/* Record a dup for the next outbound sack */
1643f8829a4aSRandall Stewart 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1644f8829a4aSRandall Stewart 			asoc->numduptsns++;
1645f8829a4aSRandall Stewart 		}
164642551e99SRandall Stewart 		asoc->send_sack = 1;
1647f8829a4aSRandall Stewart 		return (0);
1648f8829a4aSRandall Stewart 	}
1649f8829a4aSRandall Stewart 	/*
1650f8829a4aSRandall Stewart 	 * Check to see about the GONE flag, duplicates would cause a sack
1651f8829a4aSRandall Stewart 	 * to be sent up above
1652f8829a4aSRandall Stewart 	 */
1653ad81507eSRandall Stewart 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1654f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1655f8829a4aSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET))
1656f8829a4aSRandall Stewart 	    ) {
1657f8829a4aSRandall Stewart 		/*
1658f8829a4aSRandall Stewart 		 * wait a minute, this guy is gone, there is no longer a
1659f8829a4aSRandall Stewart 		 * receiver. Send peer an ABORT!
1660f8829a4aSRandall Stewart 		 */
1661f8829a4aSRandall Stewart 		struct mbuf *op_err;
1662f8829a4aSRandall Stewart 
1663f8829a4aSRandall Stewart 		op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1664ceaad40aSRandall Stewart 		sctp_abort_an_association(stcb->sctp_ep, stcb, 0, op_err, SCTP_SO_NOT_LOCKED);
1665f8829a4aSRandall Stewart 		*abort_flag = 1;
1666f8829a4aSRandall Stewart 		return (0);
1667f8829a4aSRandall Stewart 	}
1668f8829a4aSRandall Stewart 	/*
1669f8829a4aSRandall Stewart 	 * Now before going further we see if there is room. If NOT then we
1670f8829a4aSRandall Stewart 	 * MAY let one through only IF this TSN is the one we are waiting
1671f8829a4aSRandall Stewart 	 * for on a partial delivery API.
1672f8829a4aSRandall Stewart 	 */
1673f8829a4aSRandall Stewart 
1674f8829a4aSRandall Stewart 	/* now do the tests */
1675f8829a4aSRandall Stewart 	if (((asoc->cnt_on_all_streams +
1676f8829a4aSRandall Stewart 	    asoc->cnt_on_reasm_queue +
1677b3f1ea41SRandall Stewart 	    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) ||
1678f8829a4aSRandall Stewart 	    (((int)asoc->my_rwnd) <= 0)) {
1679f8829a4aSRandall Stewart 		/*
1680f8829a4aSRandall Stewart 		 * When we have NO room in the rwnd we check to make sure
1681f8829a4aSRandall Stewart 		 * the reader is doing its job...
1682f8829a4aSRandall Stewart 		 */
1683f8829a4aSRandall Stewart 		if (stcb->sctp_socket->so_rcv.sb_cc) {
1684f8829a4aSRandall Stewart 			/* some to read, wake-up */
1685ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1686ceaad40aSRandall Stewart 			struct socket *so;
1687ceaad40aSRandall Stewart 
1688ceaad40aSRandall Stewart 			so = SCTP_INP_SO(stcb->sctp_ep);
1689ceaad40aSRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, 1);
1690ceaad40aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
1691ceaad40aSRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
1692ceaad40aSRandall Stewart 			SCTP_TCB_LOCK(stcb);
1693ceaad40aSRandall Stewart 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
1694ceaad40aSRandall Stewart 			if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1695ceaad40aSRandall Stewart 				/* assoc was freed while we were unlocked */
1696ceaad40aSRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
1697ceaad40aSRandall Stewart 				return (0);
1698ceaad40aSRandall Stewart 			}
1699ceaad40aSRandall Stewart #endif
1700f8829a4aSRandall Stewart 			sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1701ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1702ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
1703ceaad40aSRandall Stewart #endif
1704f8829a4aSRandall Stewart 		}
1705f8829a4aSRandall Stewart 		/* now is it in the mapping array of what we have accepted? */
1706b3f1ea41SRandall Stewart 		if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
1707f8829a4aSRandall Stewart 			/* Nope not in the valid range dump it */
1708f8829a4aSRandall Stewart 			sctp_set_rwnd(stcb, asoc);
1709f8829a4aSRandall Stewart 			if ((asoc->cnt_on_all_streams +
1710f8829a4aSRandall Stewart 			    asoc->cnt_on_reasm_queue +
1711b3f1ea41SRandall Stewart 			    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) {
1712f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_datadropchklmt);
1713f8829a4aSRandall Stewart 			} else {
1714f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_datadroprwnd);
1715f8829a4aSRandall Stewart 			}
1716f8829a4aSRandall Stewart 			indx = *break_flag;
1717f8829a4aSRandall Stewart 			*break_flag = 1;
1718f8829a4aSRandall Stewart 			return (0);
1719f8829a4aSRandall Stewart 		}
1720f8829a4aSRandall Stewart 	}
1721f8829a4aSRandall Stewart 	strmno = ntohs(ch->dp.stream_id);
1722f8829a4aSRandall Stewart 	if (strmno >= asoc->streamincnt) {
1723f8829a4aSRandall Stewart 		struct sctp_paramhdr *phdr;
1724f8829a4aSRandall Stewart 		struct mbuf *mb;
1725f8829a4aSRandall Stewart 
1726f8829a4aSRandall Stewart 		mb = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) * 2),
1727139bc87fSRandall Stewart 		    0, M_DONTWAIT, 1, MT_DATA);
1728f8829a4aSRandall Stewart 		if (mb != NULL) {
1729f8829a4aSRandall Stewart 			/* add some space up front so prepend will work well */
1730139bc87fSRandall Stewart 			SCTP_BUF_RESV_UF(mb, sizeof(struct sctp_chunkhdr));
1731f8829a4aSRandall Stewart 			phdr = mtod(mb, struct sctp_paramhdr *);
1732f8829a4aSRandall Stewart 			/*
1733f8829a4aSRandall Stewart 			 * Error causes are just param's and this one has
1734f8829a4aSRandall Stewart 			 * two back to back phdr, one with the error type
1735f8829a4aSRandall Stewart 			 * and size, the other with the streamid and a rsvd
1736f8829a4aSRandall Stewart 			 */
1737139bc87fSRandall Stewart 			SCTP_BUF_LEN(mb) = (sizeof(struct sctp_paramhdr) * 2);
1738f8829a4aSRandall Stewart 			phdr->param_type = htons(SCTP_CAUSE_INVALID_STREAM);
1739f8829a4aSRandall Stewart 			phdr->param_length =
1740f8829a4aSRandall Stewart 			    htons(sizeof(struct sctp_paramhdr) * 2);
1741f8829a4aSRandall Stewart 			phdr++;
1742f8829a4aSRandall Stewart 			/* We insert the stream in the type field */
1743f8829a4aSRandall Stewart 			phdr->param_type = ch->dp.stream_id;
1744f8829a4aSRandall Stewart 			/* And set the length to 0 for the rsvd field */
1745f8829a4aSRandall Stewart 			phdr->param_length = 0;
1746f8829a4aSRandall Stewart 			sctp_queue_op_err(stcb, mb);
1747f8829a4aSRandall Stewart 		}
1748f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_badsid);
1749207304d4SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
1750d06c82f1SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
1751830d754dSRandall Stewart 		/* EY set this tsn present in  nr_sack's nr_mapping_array */
1752830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
1753830d754dSRandall Stewart 			SCTP_TCB_LOCK_ASSERT(stcb);
1754830d754dSRandall Stewart 			SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
1755830d754dSRandall Stewart 		}
1756d06c82f1SRandall Stewart 		if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
1757d06c82f1SRandall Stewart 			/* we have a new high score */
1758d06c82f1SRandall Stewart 			asoc->highest_tsn_inside_map = tsn;
1759830d754dSRandall Stewart 			/* EY nr_sack version of the above */
1760830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack)
1761830d754dSRandall Stewart 				asoc->highest_tsn_inside_nr_map = tsn;
1762b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1763d06c82f1SRandall Stewart 				sctp_log_map(0, 2, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
176480fefe0aSRandall Stewart 			}
1765d06c82f1SRandall Stewart 		}
1766d06c82f1SRandall Stewart 		if (tsn == (asoc->cumulative_tsn + 1)) {
1767d06c82f1SRandall Stewart 			/* Update cum-ack */
1768d06c82f1SRandall Stewart 			asoc->cumulative_tsn = tsn;
1769d06c82f1SRandall Stewart 		}
1770f8829a4aSRandall Stewart 		return (0);
1771f8829a4aSRandall Stewart 	}
1772f8829a4aSRandall Stewart 	/*
1773f8829a4aSRandall Stewart 	 * Before we continue lets validate that we are not being fooled by
1774f8829a4aSRandall Stewart 	 * an evil attacker. We can only have 4k chunks based on our TSN
1775f8829a4aSRandall Stewart 	 * spread allowed by the mapping array 512 * 8 bits, so there is no
1776f8829a4aSRandall Stewart 	 * way our stream sequence numbers could have wrapped. We of course
1777f8829a4aSRandall Stewart 	 * only validate the FIRST fragment so the bit must be set.
1778f8829a4aSRandall Stewart 	 */
1779f8829a4aSRandall Stewart 	strmseq = ntohs(ch->dp.stream_sequence);
1780f42a358aSRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
178118e198d3SRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
178218e198d3SRandall Stewart 	if (asoc->tsn_in_at >= SCTP_TSN_LOG_SIZE) {
178318e198d3SRandall Stewart 		asoc->tsn_in_at = 0;
178418e198d3SRandall Stewart 		asoc->tsn_in_wrapped = 1;
178518e198d3SRandall Stewart 	}
1786f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].tsn = tsn;
1787f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].strm = strmno;
1788f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].seq = strmseq;
1789f1f73e57SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].sz = chk_length;
1790f1f73e57SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].flgs = chunk_flags;
179118e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].stcb = (void *)stcb;
179218e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].in_pos = asoc->tsn_in_at;
179318e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].in_out = 1;
1794f42a358aSRandall Stewart 	asoc->tsn_in_at++;
1795f42a358aSRandall Stewart #endif
1796f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_FIRST_FRAG) &&
1797d61a0ae0SRandall Stewart 	    (TAILQ_EMPTY(&asoc->resetHead)) &&
1798f42a358aSRandall Stewart 	    (chunk_flags & SCTP_DATA_UNORDERED) == 0 &&
1799f8829a4aSRandall Stewart 	    (compare_with_wrap(asoc->strmin[strmno].last_sequence_delivered,
1800f8829a4aSRandall Stewart 	    strmseq, MAX_SEQ) ||
1801f8829a4aSRandall Stewart 	    asoc->strmin[strmno].last_sequence_delivered == strmseq)) {
1802f8829a4aSRandall Stewart 		/* The incoming sseq is behind where we last delivered? */
1803ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1, "EVIL/Broken-Dup S-SEQ:%d delivered:%d from peer, Abort!\n",
1804ad81507eSRandall Stewart 		    strmseq, asoc->strmin[strmno].last_sequence_delivered);
1805139bc87fSRandall Stewart 		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1806f8829a4aSRandall Stewart 		    0, M_DONTWAIT, 1, MT_DATA);
1807f8829a4aSRandall Stewart 		if (oper) {
1808f8829a4aSRandall Stewart 			struct sctp_paramhdr *ph;
1809f8829a4aSRandall Stewart 			uint32_t *ippp;
1810f8829a4aSRandall Stewart 
1811139bc87fSRandall Stewart 			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
1812f8829a4aSRandall Stewart 			    (3 * sizeof(uint32_t));
1813f8829a4aSRandall Stewart 			ph = mtod(oper, struct sctp_paramhdr *);
1814f8829a4aSRandall Stewart 			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1815139bc87fSRandall Stewart 			ph->param_length = htons(SCTP_BUF_LEN(oper));
1816f8829a4aSRandall Stewart 			ippp = (uint32_t *) (ph + 1);
1817a5d547adSRandall Stewart 			*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_14);
1818f8829a4aSRandall Stewart 			ippp++;
1819f8829a4aSRandall Stewart 			*ippp = tsn;
1820f8829a4aSRandall Stewart 			ippp++;
1821f8829a4aSRandall Stewart 			*ippp = ((strmno << 16) | strmseq);
1822f8829a4aSRandall Stewart 
1823f8829a4aSRandall Stewart 		}
1824a5d547adSRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_14;
1825f8829a4aSRandall Stewart 		sctp_abort_an_association(stcb->sctp_ep, stcb,
1826ceaad40aSRandall Stewart 		    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1827f8829a4aSRandall Stewart 		*abort_flag = 1;
1828f8829a4aSRandall Stewart 		return (0);
1829f8829a4aSRandall Stewart 	}
1830f42a358aSRandall Stewart 	/************************************
1831f42a358aSRandall Stewart 	 * From here down we may find ch-> invalid
1832f42a358aSRandall Stewart 	 * so its a good idea NOT to use it.
1833f42a358aSRandall Stewart 	 *************************************/
1834f42a358aSRandall Stewart 
1835f8829a4aSRandall Stewart 	the_len = (chk_length - sizeof(struct sctp_data_chunk));
1836f8829a4aSRandall Stewart 	if (last_chunk == 0) {
183744b7479bSRandall Stewart 		dmbuf = SCTP_M_COPYM(*m,
1838f8829a4aSRandall Stewart 		    (offset + sizeof(struct sctp_data_chunk)),
1839f8829a4aSRandall Stewart 		    the_len, M_DONTWAIT);
1840f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
1841b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
1842f8829a4aSRandall Stewart 			struct mbuf *mat;
1843f8829a4aSRandall Stewart 
1844f8829a4aSRandall Stewart 			mat = dmbuf;
1845f8829a4aSRandall Stewart 			while (mat) {
1846139bc87fSRandall Stewart 				if (SCTP_BUF_IS_EXTENDED(mat)) {
1847f8829a4aSRandall Stewart 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
1848f8829a4aSRandall Stewart 				}
1849139bc87fSRandall Stewart 				mat = SCTP_BUF_NEXT(mat);
1850f8829a4aSRandall Stewart 			}
1851f8829a4aSRandall Stewart 		}
1852f8829a4aSRandall Stewart #endif
1853f8829a4aSRandall Stewart 	} else {
1854f8829a4aSRandall Stewart 		/* We can steal the last chunk */
1855139bc87fSRandall Stewart 		int l_len;
1856139bc87fSRandall Stewart 
1857f8829a4aSRandall Stewart 		dmbuf = *m;
1858f8829a4aSRandall Stewart 		/* lop off the top part */
1859f8829a4aSRandall Stewart 		m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk)));
1860139bc87fSRandall Stewart 		if (SCTP_BUF_NEXT(dmbuf) == NULL) {
1861139bc87fSRandall Stewart 			l_len = SCTP_BUF_LEN(dmbuf);
1862139bc87fSRandall Stewart 		} else {
1863139bc87fSRandall Stewart 			/*
1864139bc87fSRandall Stewart 			 * need to count up the size hopefully does not hit
1865139bc87fSRandall Stewart 			 * this to often :-0
1866139bc87fSRandall Stewart 			 */
1867139bc87fSRandall Stewart 			struct mbuf *lat;
1868139bc87fSRandall Stewart 
1869139bc87fSRandall Stewart 			l_len = 0;
1870139bc87fSRandall Stewart 			lat = dmbuf;
1871139bc87fSRandall Stewart 			while (lat) {
1872139bc87fSRandall Stewart 				l_len += SCTP_BUF_LEN(lat);
1873139bc87fSRandall Stewart 				lat = SCTP_BUF_NEXT(lat);
1874139bc87fSRandall Stewart 			}
1875139bc87fSRandall Stewart 		}
1876139bc87fSRandall Stewart 		if (l_len > the_len) {
1877f8829a4aSRandall Stewart 			/* Trim the end round bytes off  too */
1878139bc87fSRandall Stewart 			m_adj(dmbuf, -(l_len - the_len));
1879f8829a4aSRandall Stewart 		}
1880f8829a4aSRandall Stewart 	}
1881f8829a4aSRandall Stewart 	if (dmbuf == NULL) {
1882f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_nomem);
1883f8829a4aSRandall Stewart 		return (0);
1884f8829a4aSRandall Stewart 	}
1885f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG &&
1886f8829a4aSRandall Stewart 	    asoc->fragmented_delivery_inprogress == 0 &&
1887f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->resetHead) &&
1888f42a358aSRandall Stewart 	    ((ordered == 0) ||
1889f8829a4aSRandall Stewart 	    ((asoc->strmin[strmno].last_sequence_delivered + 1) == strmseq &&
1890f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->strmin[strmno].inqueue)))) {
1891f8829a4aSRandall Stewart 		/* Candidate for express delivery */
1892f8829a4aSRandall Stewart 		/*
1893f8829a4aSRandall Stewart 		 * Its not fragmented, No PD-API is up, Nothing in the
1894f8829a4aSRandall Stewart 		 * delivery queue, Its un-ordered OR ordered and the next to
1895f8829a4aSRandall Stewart 		 * deliver AND nothing else is stuck on the stream queue,
1896f8829a4aSRandall Stewart 		 * And there is room for it in the socket buffer. Lets just
1897f8829a4aSRandall Stewart 		 * stuff it up the buffer....
1898f8829a4aSRandall Stewart 		 */
1899f8829a4aSRandall Stewart 
1900f8829a4aSRandall Stewart 		/* It would be nice to avoid this copy if we could :< */
1901f8829a4aSRandall Stewart 		sctp_alloc_a_readq(stcb, control);
1902f8829a4aSRandall Stewart 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1903f42a358aSRandall Stewart 		    protocol_id,
1904f8829a4aSRandall Stewart 		    stcb->asoc.context,
1905f8829a4aSRandall Stewart 		    strmno, strmseq,
1906f42a358aSRandall Stewart 		    chunk_flags,
1907f8829a4aSRandall Stewart 		    dmbuf);
1908f8829a4aSRandall Stewart 		if (control == NULL) {
1909f8829a4aSRandall Stewart 			goto failed_express_del;
1910f8829a4aSRandall Stewart 		}
1911ceaad40aSRandall Stewart 		sctp_add_to_readq(stcb->sctp_ep, stcb, control, &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
1912830d754dSRandall Stewart 
1913830d754dSRandall Stewart 		/*
1914830d754dSRandall Stewart 		 * EY here I should check if this delivered tsn is
1915830d754dSRandall Stewart 		 * out_of_order, if yes then update the nr_map
1916830d754dSRandall Stewart 		 */
1917830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
1918830d754dSRandall Stewart 			/*
1919830d754dSRandall Stewart 			 * EY check if the mapping_array and nr_mapping
1920830d754dSRandall Stewart 			 * array are consistent
1921830d754dSRandall Stewart 			 */
1922830d754dSRandall Stewart 			if (asoc->mapping_array_base_tsn != asoc->nr_mapping_array_base_tsn)
1923830d754dSRandall Stewart 				/*
1924830d754dSRandall Stewart 				 * printf("EY-IN
1925830d754dSRandall Stewart 				 * sctp_process_a_data_chunk(5): Something
1926830d754dSRandall Stewart 				 * is wrong the map base tsn" "\nEY-and
1927830d754dSRandall Stewart 				 * nr_map base tsn should be equal.");
1928830d754dSRandall Stewart 				 */
1929830d754dSRandall Stewart 				/* EY debugging block */
1930830d754dSRandall Stewart 			{
1931830d754dSRandall Stewart 				/*
1932830d754dSRandall Stewart 				 * printf("\nEY-Calculating an
1933830d754dSRandall Stewart 				 * nr_gap!!\nmapping_array_size = %d
1934830d754dSRandall Stewart 				 * nr_mapping_array_size = %d"
1935830d754dSRandall Stewart 				 * "\nEY-mapping_array_base = %d
1936830d754dSRandall Stewart 				 * nr_mapping_array_base =
1937830d754dSRandall Stewart 				 * %d\nEY-highest_tsn_inside_map = %d"
1938830d754dSRandall Stewart 				 * "highest_tsn_inside_nr_map = %d\nEY-TSN =
1939830d754dSRandall Stewart 				 * %d nr_gap = %d",asoc->mapping_array_size,
1940830d754dSRandall Stewart 				 * asoc->nr_mapping_array_size,
1941830d754dSRandall Stewart 				 * asoc->mapping_array_base_tsn,
1942830d754dSRandall Stewart 				 * asoc->nr_mapping_array_base_tsn,
1943830d754dSRandall Stewart 				 * asoc->highest_tsn_inside_map,
1944830d754dSRandall Stewart 				 * asoc->highest_tsn_inside_nr_map,tsn,nr_gap
1945830d754dSRandall Stewart 				 * );
1946830d754dSRandall Stewart 				 */
1947830d754dSRandall Stewart 			}
1948830d754dSRandall Stewart 			/* EY - not %100 sure about the lock thing */
1949830d754dSRandall Stewart 			SCTP_TCB_LOCK_ASSERT(stcb);
1950830d754dSRandall Stewart 			SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
1951830d754dSRandall Stewart 			if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
1952830d754dSRandall Stewart 				asoc->highest_tsn_inside_nr_map = tsn;
1953830d754dSRandall Stewart 		}
1954f42a358aSRandall Stewart 		if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1955f8829a4aSRandall Stewart 			/* for ordered, bump what we delivered */
1956f8829a4aSRandall Stewart 			asoc->strmin[strmno].last_sequence_delivered++;
1957f8829a4aSRandall Stewart 		}
1958f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvexpress);
1959b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
19606a91f103SRandall Stewart 			sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno,
1961f8829a4aSRandall Stewart 			    SCTP_STR_LOG_FROM_EXPRS_DEL);
196280fefe0aSRandall Stewart 		}
1963f8829a4aSRandall Stewart 		control = NULL;
1964f8829a4aSRandall Stewart 		goto finish_express_del;
1965f8829a4aSRandall Stewart 	}
1966f8829a4aSRandall Stewart failed_express_del:
1967f8829a4aSRandall Stewart 	/* If we reach here this is a new chunk */
1968f8829a4aSRandall Stewart 	chk = NULL;
1969f8829a4aSRandall Stewart 	control = NULL;
1970f8829a4aSRandall Stewart 	/* Express for fragmented delivery? */
1971f8829a4aSRandall Stewart 	if ((asoc->fragmented_delivery_inprogress) &&
1972f8829a4aSRandall Stewart 	    (stcb->asoc.control_pdapi) &&
1973f8829a4aSRandall Stewart 	    (asoc->str_of_pdapi == strmno) &&
1974f8829a4aSRandall Stewart 	    (asoc->ssn_of_pdapi == strmseq)
1975f8829a4aSRandall Stewart 	    ) {
1976f8829a4aSRandall Stewart 		control = stcb->asoc.control_pdapi;
1977f42a358aSRandall Stewart 		if ((chunk_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
1978f8829a4aSRandall Stewart 			/* Can't be another first? */
1979f8829a4aSRandall Stewart 			goto failed_pdapi_express_del;
1980f8829a4aSRandall Stewart 		}
1981f8829a4aSRandall Stewart 		if (tsn == (control->sinfo_tsn + 1)) {
1982f8829a4aSRandall Stewart 			/* Yep, we can add it on */
1983f8829a4aSRandall Stewart 			int end = 0;
1984f8829a4aSRandall Stewart 			uint32_t cumack;
1985f8829a4aSRandall Stewart 
1986f42a358aSRandall Stewart 			if (chunk_flags & SCTP_DATA_LAST_FRAG) {
1987f8829a4aSRandall Stewart 				end = 1;
1988f8829a4aSRandall Stewart 			}
1989f8829a4aSRandall Stewart 			cumack = asoc->cumulative_tsn;
1990f8829a4aSRandall Stewart 			if ((cumack + 1) == tsn)
1991f8829a4aSRandall Stewart 				cumack = tsn;
1992f8829a4aSRandall Stewart 
1993f8829a4aSRandall Stewart 			if (sctp_append_to_readq(stcb->sctp_ep, stcb, control, dmbuf, end,
1994f8829a4aSRandall Stewart 			    tsn,
1995f8829a4aSRandall Stewart 			    &stcb->sctp_socket->so_rcv)) {
1996ad81507eSRandall Stewart 				SCTP_PRINTF("Append fails end:%d\n", end);
1997f8829a4aSRandall Stewart 				goto failed_pdapi_express_del;
1998f8829a4aSRandall Stewart 			}
1999830d754dSRandall Stewart 			/*
2000830d754dSRandall Stewart 			 * EY It is appended to the read queue in prev if
2001830d754dSRandall Stewart 			 * block here I should check if this delivered tsn
2002830d754dSRandall Stewart 			 * is out_of_order, if yes then update the nr_map
2003830d754dSRandall Stewart 			 */
2004830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
2005830d754dSRandall Stewart 				/* EY debugging block */
2006830d754dSRandall Stewart 				{
2007830d754dSRandall Stewart 					/*
2008830d754dSRandall Stewart 					 * printf("\nEY-Calculating an
2009830d754dSRandall Stewart 					 * nr_gap!!\nEY-mapping_array_size =
2010830d754dSRandall Stewart 					 * %d nr_mapping_array_size = %d"
2011830d754dSRandall Stewart 					 * "\nEY-mapping_array_base = %d
2012830d754dSRandall Stewart 					 * nr_mapping_array_base =
2013830d754dSRandall Stewart 					 * %d\nEY-highest_tsn_inside_map =
2014830d754dSRandall Stewart 					 * %d" "highest_tsn_inside_nr_map =
2015830d754dSRandall Stewart 					 * %d\nEY-TSN = %d nr_gap =
2016830d754dSRandall Stewart 					 * %d",asoc->mapping_array_size,
2017830d754dSRandall Stewart 					 * asoc->nr_mapping_array_size,
2018830d754dSRandall Stewart 					 * asoc->mapping_array_base_tsn,
2019830d754dSRandall Stewart 					 * asoc->nr_mapping_array_base_tsn,
2020830d754dSRandall Stewart 					 * asoc->highest_tsn_inside_map,
2021830d754dSRandall Stewart 					 * asoc->highest_tsn_inside_nr_map,ts
2022830d754dSRandall Stewart 					 * n,nr_gap);
2023830d754dSRandall Stewart 					 */
2024830d754dSRandall Stewart 				}
2025830d754dSRandall Stewart 				/* EY - not %100 sure about the lock thing */
2026830d754dSRandall Stewart 				SCTP_TCB_LOCK_ASSERT(stcb);
2027830d754dSRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
2028830d754dSRandall Stewart 				if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
2029830d754dSRandall Stewart 					asoc->highest_tsn_inside_nr_map = tsn;
2030830d754dSRandall Stewart 			}
2031f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvexpressm);
2032f8829a4aSRandall Stewart 			control->sinfo_tsn = tsn;
2033f8829a4aSRandall Stewart 			asoc->tsn_last_delivered = tsn;
2034f42a358aSRandall Stewart 			asoc->fragment_flags = chunk_flags;
2035f8829a4aSRandall Stewart 			asoc->tsn_of_pdapi_last_delivered = tsn;
2036f42a358aSRandall Stewart 			asoc->last_flags_delivered = chunk_flags;
2037f8829a4aSRandall Stewart 			asoc->last_strm_seq_delivered = strmseq;
2038f8829a4aSRandall Stewart 			asoc->last_strm_no_delivered = strmno;
2039f8829a4aSRandall Stewart 			if (end) {
2040f8829a4aSRandall Stewart 				/* clean up the flags and such */
2041f8829a4aSRandall Stewart 				asoc->fragmented_delivery_inprogress = 0;
2042f42a358aSRandall Stewart 				if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
2043f8829a4aSRandall Stewart 					asoc->strmin[strmno].last_sequence_delivered++;
2044a5d547adSRandall Stewart 				}
2045f8829a4aSRandall Stewart 				stcb->asoc.control_pdapi = NULL;
2046139bc87fSRandall Stewart 				if (TAILQ_EMPTY(&asoc->reasmqueue) == 0) {
2047139bc87fSRandall Stewart 					/*
2048139bc87fSRandall Stewart 					 * There could be another message
2049139bc87fSRandall Stewart 					 * ready
2050139bc87fSRandall Stewart 					 */
2051139bc87fSRandall Stewart 					need_reasm_check = 1;
2052139bc87fSRandall Stewart 				}
2053f8829a4aSRandall Stewart 			}
2054f8829a4aSRandall Stewart 			control = NULL;
2055f8829a4aSRandall Stewart 			goto finish_express_del;
2056f8829a4aSRandall Stewart 		}
2057f8829a4aSRandall Stewart 	}
2058f8829a4aSRandall Stewart failed_pdapi_express_del:
2059f8829a4aSRandall Stewart 	control = NULL;
2060f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
2061f8829a4aSRandall Stewart 		sctp_alloc_a_chunk(stcb, chk);
2062f8829a4aSRandall Stewart 		if (chk == NULL) {
2063f8829a4aSRandall Stewart 			/* No memory so we drop the chunk */
2064f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_nomem);
2065f8829a4aSRandall Stewart 			if (last_chunk == 0) {
2066f8829a4aSRandall Stewart 				/* we copied it, free the copy */
2067f8829a4aSRandall Stewart 				sctp_m_freem(dmbuf);
2068f8829a4aSRandall Stewart 			}
2069f8829a4aSRandall Stewart 			return (0);
2070f8829a4aSRandall Stewart 		}
2071f8829a4aSRandall Stewart 		chk->rec.data.TSN_seq = tsn;
2072f8829a4aSRandall Stewart 		chk->no_fr_allowed = 0;
2073f8829a4aSRandall Stewart 		chk->rec.data.stream_seq = strmseq;
2074f8829a4aSRandall Stewart 		chk->rec.data.stream_number = strmno;
2075f42a358aSRandall Stewart 		chk->rec.data.payloadtype = protocol_id;
2076f8829a4aSRandall Stewart 		chk->rec.data.context = stcb->asoc.context;
2077f8829a4aSRandall Stewart 		chk->rec.data.doing_fast_retransmit = 0;
2078f42a358aSRandall Stewart 		chk->rec.data.rcv_flags = chunk_flags;
2079f8829a4aSRandall Stewart 		chk->asoc = asoc;
2080f8829a4aSRandall Stewart 		chk->send_size = the_len;
2081f8829a4aSRandall Stewart 		chk->whoTo = net;
2082f8829a4aSRandall Stewart 		atomic_add_int(&net->ref_count, 1);
2083f8829a4aSRandall Stewart 		chk->data = dmbuf;
2084f8829a4aSRandall Stewart 	} else {
2085f8829a4aSRandall Stewart 		sctp_alloc_a_readq(stcb, control);
2086f8829a4aSRandall Stewart 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
2087f42a358aSRandall Stewart 		    protocol_id,
2088f8829a4aSRandall Stewart 		    stcb->asoc.context,
2089f8829a4aSRandall Stewart 		    strmno, strmseq,
2090f42a358aSRandall Stewart 		    chunk_flags,
2091f8829a4aSRandall Stewart 		    dmbuf);
2092f8829a4aSRandall Stewart 		if (control == NULL) {
2093f8829a4aSRandall Stewart 			/* No memory so we drop the chunk */
2094f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_nomem);
2095f8829a4aSRandall Stewart 			if (last_chunk == 0) {
2096f8829a4aSRandall Stewart 				/* we copied it, free the copy */
2097f8829a4aSRandall Stewart 				sctp_m_freem(dmbuf);
2098f8829a4aSRandall Stewart 			}
2099f8829a4aSRandall Stewart 			return (0);
2100f8829a4aSRandall Stewart 		}
2101f8829a4aSRandall Stewart 		control->length = the_len;
2102f8829a4aSRandall Stewart 	}
2103f8829a4aSRandall Stewart 
2104f8829a4aSRandall Stewart 	/* Mark it as received */
2105f8829a4aSRandall Stewart 	/* Now queue it where it belongs */
2106f8829a4aSRandall Stewart 	if (control != NULL) {
2107f8829a4aSRandall Stewart 		/* First a sanity check */
2108f8829a4aSRandall Stewart 		if (asoc->fragmented_delivery_inprogress) {
2109f8829a4aSRandall Stewart 			/*
2110f8829a4aSRandall Stewart 			 * Ok, we have a fragmented delivery in progress if
2111f8829a4aSRandall Stewart 			 * this chunk is next to deliver OR belongs in our
2112f8829a4aSRandall Stewart 			 * view to the reassembly, the peer is evil or
2113f8829a4aSRandall Stewart 			 * broken.
2114f8829a4aSRandall Stewart 			 */
2115f8829a4aSRandall Stewart 			uint32_t estimate_tsn;
2116f8829a4aSRandall Stewart 
2117f8829a4aSRandall Stewart 			estimate_tsn = asoc->tsn_last_delivered + 1;
2118f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->reasmqueue) &&
2119f8829a4aSRandall Stewart 			    (estimate_tsn == control->sinfo_tsn)) {
2120f8829a4aSRandall Stewart 				/* Evil/Broke peer */
2121f8829a4aSRandall Stewart 				sctp_m_freem(control->data);
2122f8829a4aSRandall Stewart 				control->data = NULL;
21235bead436SRandall Stewart 				if (control->whoFrom) {
2124f8829a4aSRandall Stewart 					sctp_free_remote_addr(control->whoFrom);
21255bead436SRandall Stewart 					control->whoFrom = NULL;
21265bead436SRandall Stewart 				}
2127f8829a4aSRandall Stewart 				sctp_free_a_readq(stcb, control);
2128139bc87fSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
2129f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
2130f8829a4aSRandall Stewart 				if (oper) {
2131f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
2132f8829a4aSRandall Stewart 					uint32_t *ippp;
2133f8829a4aSRandall Stewart 
2134139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) =
2135f8829a4aSRandall Stewart 					    sizeof(struct sctp_paramhdr) +
2136f8829a4aSRandall Stewart 					    (3 * sizeof(uint32_t));
2137f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
2138f8829a4aSRandall Stewart 					ph->param_type =
2139f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2140139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
2141f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
2142a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_15);
2143f8829a4aSRandall Stewart 					ippp++;
2144f8829a4aSRandall Stewart 					*ippp = tsn;
2145f8829a4aSRandall Stewart 					ippp++;
2146f8829a4aSRandall Stewart 					*ippp = ((strmno << 16) | strmseq);
2147f8829a4aSRandall Stewart 				}
2148a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_15;
2149f8829a4aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb,
2150ceaad40aSRandall Stewart 				    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
2151f8829a4aSRandall Stewart 
2152f8829a4aSRandall Stewart 				*abort_flag = 1;
2153f8829a4aSRandall Stewart 				return (0);
2154f8829a4aSRandall Stewart 			} else {
2155f8829a4aSRandall Stewart 				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
2156f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
2157f8829a4aSRandall Stewart 					control->data = NULL;
21585bead436SRandall Stewart 					if (control->whoFrom) {
2159f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
21605bead436SRandall Stewart 						control->whoFrom = NULL;
21615bead436SRandall Stewart 					}
2162f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
2163f8829a4aSRandall Stewart 
2164139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
2165f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
2166f8829a4aSRandall Stewart 					if (oper) {
2167f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
2168f8829a4aSRandall Stewart 						uint32_t *ippp;
2169f8829a4aSRandall Stewart 
2170139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
2171f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
2172f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
2173f8829a4aSRandall Stewart 						ph = mtod(oper,
2174f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
2175f8829a4aSRandall Stewart 						ph->param_type =
2176f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2177f8829a4aSRandall Stewart 						ph->param_length =
2178139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
2179f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
2180a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_16);
2181f8829a4aSRandall Stewart 						ippp++;
2182f8829a4aSRandall Stewart 						*ippp = tsn;
2183f8829a4aSRandall Stewart 						ippp++;
2184f8829a4aSRandall Stewart 						*ippp = ((strmno << 16) | strmseq);
2185f8829a4aSRandall Stewart 					}
2186a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_16;
2187f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
2188ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
2189f8829a4aSRandall Stewart 
2190f8829a4aSRandall Stewart 					*abort_flag = 1;
2191f8829a4aSRandall Stewart 					return (0);
2192f8829a4aSRandall Stewart 				}
2193f8829a4aSRandall Stewart 			}
2194f8829a4aSRandall Stewart 		} else {
2195f8829a4aSRandall Stewart 			/* No PDAPI running */
2196f8829a4aSRandall Stewart 			if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
2197f8829a4aSRandall Stewart 				/*
2198f8829a4aSRandall Stewart 				 * Reassembly queue is NOT empty validate
2199f8829a4aSRandall Stewart 				 * that this tsn does not need to be in
2200f8829a4aSRandall Stewart 				 * reasembly queue. If it does then our peer
2201f8829a4aSRandall Stewart 				 * is broken or evil.
2202f8829a4aSRandall Stewart 				 */
2203f8829a4aSRandall Stewart 				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
2204f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
2205f8829a4aSRandall Stewart 					control->data = NULL;
22065bead436SRandall Stewart 					if (control->whoFrom) {
2207f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
22085bead436SRandall Stewart 						control->whoFrom = NULL;
22095bead436SRandall Stewart 					}
2210f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
2211139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
2212f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
2213f8829a4aSRandall Stewart 					if (oper) {
2214f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
2215f8829a4aSRandall Stewart 						uint32_t *ippp;
2216f8829a4aSRandall Stewart 
2217139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
2218f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
2219f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
2220f8829a4aSRandall Stewart 						ph = mtod(oper,
2221f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
2222f8829a4aSRandall Stewart 						ph->param_type =
2223f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2224f8829a4aSRandall Stewart 						ph->param_length =
2225139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
2226f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
2227a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_17);
2228f8829a4aSRandall Stewart 						ippp++;
2229f8829a4aSRandall Stewart 						*ippp = tsn;
2230f8829a4aSRandall Stewart 						ippp++;
2231f8829a4aSRandall Stewart 						*ippp = ((strmno << 16) | strmseq);
2232f8829a4aSRandall Stewart 					}
2233a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_17;
2234f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
2235ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
2236f8829a4aSRandall Stewart 
2237f8829a4aSRandall Stewart 					*abort_flag = 1;
2238f8829a4aSRandall Stewart 					return (0);
2239f8829a4aSRandall Stewart 				}
2240f8829a4aSRandall Stewart 			}
2241f8829a4aSRandall Stewart 		}
2242f8829a4aSRandall Stewart 		/* ok, if we reach here we have passed the sanity checks */
2243f42a358aSRandall Stewart 		if (chunk_flags & SCTP_DATA_UNORDERED) {
2244f8829a4aSRandall Stewart 			/* queue directly into socket buffer */
2245f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep, stcb,
2246f8829a4aSRandall Stewart 			    control,
2247ceaad40aSRandall Stewart 			    &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
2248830d754dSRandall Stewart 
2249830d754dSRandall Stewart 			/*
2250830d754dSRandall Stewart 			 * EY It is added to the read queue in prev if block
2251830d754dSRandall Stewart 			 * here I should check if this delivered tsn is
2252830d754dSRandall Stewart 			 * out_of_order, if yes then update the nr_map
2253830d754dSRandall Stewart 			 */
2254830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
2255830d754dSRandall Stewart 				/*
2256830d754dSRandall Stewart 				 * EY check if the mapping_array and
2257830d754dSRandall Stewart 				 * nr_mapping array are consistent
2258830d754dSRandall Stewart 				 */
2259830d754dSRandall Stewart 				if (asoc->mapping_array_base_tsn != asoc->nr_mapping_array_base_tsn)
2260830d754dSRandall Stewart 					/*
2261830d754dSRandall Stewart 					 * printf("EY-IN
2262830d754dSRandall Stewart 					 * sctp_process_a_data_chunk(6):
2263830d754dSRandall Stewart 					 * Something is wrong the map base
2264830d754dSRandall Stewart 					 * tsn" "\nEY-and nr_map base tsn
2265830d754dSRandall Stewart 					 * should be equal.");
2266830d754dSRandall Stewart 					 */
2267830d754dSRandall Stewart 					/*
2268830d754dSRandall Stewart 					 * EY - not %100 sure about the lock
2269830d754dSRandall Stewart 					 * thing, i think we don't need the
2270830d754dSRandall Stewart 					 * below,
2271830d754dSRandall Stewart 					 */
2272830d754dSRandall Stewart 					/* SCTP_TCB_LOCK_ASSERT(stcb); */
2273830d754dSRandall Stewart 				{
2274830d754dSRandall Stewart 					/*
2275830d754dSRandall Stewart 					 * printf("\nEY-Calculating an
2276830d754dSRandall Stewart 					 * nr_gap!!\nEY-mapping_array_size =
2277830d754dSRandall Stewart 					 * %d nr_mapping_array_size = %d"
2278830d754dSRandall Stewart 					 * "\nEY-mapping_array_base = %d
2279830d754dSRandall Stewart 					 * nr_mapping_array_base =
2280830d754dSRandall Stewart 					 * %d\nEY-highest_tsn_inside_map =
2281830d754dSRandall Stewart 					 * %d" "highest_tsn_inside_nr_map =
2282830d754dSRandall Stewart 					 * %d\nEY-TSN = %d nr_gap =
2283830d754dSRandall Stewart 					 * %d",asoc->mapping_array_size,
2284830d754dSRandall Stewart 					 * asoc->nr_mapping_array_size,
2285830d754dSRandall Stewart 					 * asoc->mapping_array_base_tsn,
2286830d754dSRandall Stewart 					 * asoc->nr_mapping_array_base_tsn,
2287830d754dSRandall Stewart 					 * asoc->highest_tsn_inside_map,
2288830d754dSRandall Stewart 					 * asoc->highest_tsn_inside_nr_map,ts
2289830d754dSRandall Stewart 					 * n,nr_gap);
2290830d754dSRandall Stewart 					 */
2291830d754dSRandall Stewart 				}
2292830d754dSRandall Stewart 				SCTP_TCB_LOCK_ASSERT(stcb);
2293830d754dSRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
2294830d754dSRandall Stewart 				if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
2295830d754dSRandall Stewart 					asoc->highest_tsn_inside_nr_map = tsn;
2296830d754dSRandall Stewart 			}
2297f8829a4aSRandall Stewart 		} else {
2298f8829a4aSRandall Stewart 			/*
2299f8829a4aSRandall Stewart 			 * Special check for when streams are resetting. We
2300f8829a4aSRandall Stewart 			 * could be more smart about this and check the
2301f8829a4aSRandall Stewart 			 * actual stream to see if it is not being reset..
2302f8829a4aSRandall Stewart 			 * that way we would not create a HOLB when amongst
2303f8829a4aSRandall Stewart 			 * streams being reset and those not being reset.
2304f8829a4aSRandall Stewart 			 *
2305f8829a4aSRandall Stewart 			 * We take complete messages that have a stream reset
2306f8829a4aSRandall Stewart 			 * intervening (aka the TSN is after where our
2307f8829a4aSRandall Stewart 			 * cum-ack needs to be) off and put them on a
2308f8829a4aSRandall Stewart 			 * pending_reply_queue. The reassembly ones we do
2309f8829a4aSRandall Stewart 			 * not have to worry about since they are all sorted
2310f8829a4aSRandall Stewart 			 * and proceessed by TSN order. It is only the
2311f8829a4aSRandall Stewart 			 * singletons I must worry about.
2312f8829a4aSRandall Stewart 			 */
2313f8829a4aSRandall Stewart 			if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2314d61a0ae0SRandall Stewart 			    ((compare_with_wrap(tsn, liste->tsn, MAX_TSN)))
2315f8829a4aSRandall Stewart 			    ) {
2316f8829a4aSRandall Stewart 				/*
2317f8829a4aSRandall Stewart 				 * yep its past where we need to reset... go
2318f8829a4aSRandall Stewart 				 * ahead and queue it.
2319f8829a4aSRandall Stewart 				 */
2320f8829a4aSRandall Stewart 				if (TAILQ_EMPTY(&asoc->pending_reply_queue)) {
2321f8829a4aSRandall Stewart 					/* first one on */
2322f8829a4aSRandall Stewart 					TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2323f8829a4aSRandall Stewart 				} else {
2324f8829a4aSRandall Stewart 					struct sctp_queued_to_read *ctlOn;
2325f8829a4aSRandall Stewart 					unsigned char inserted = 0;
2326f8829a4aSRandall Stewart 
2327f8829a4aSRandall Stewart 					ctlOn = TAILQ_FIRST(&asoc->pending_reply_queue);
2328f8829a4aSRandall Stewart 					while (ctlOn) {
2329f8829a4aSRandall Stewart 						if (compare_with_wrap(control->sinfo_tsn,
2330f8829a4aSRandall Stewart 						    ctlOn->sinfo_tsn, MAX_TSN)) {
2331f8829a4aSRandall Stewart 							ctlOn = TAILQ_NEXT(ctlOn, next);
2332f8829a4aSRandall Stewart 						} else {
2333f8829a4aSRandall Stewart 							/* found it */
2334f8829a4aSRandall Stewart 							TAILQ_INSERT_BEFORE(ctlOn, control, next);
2335f8829a4aSRandall Stewart 							inserted = 1;
2336f8829a4aSRandall Stewart 							break;
2337f8829a4aSRandall Stewart 						}
2338f8829a4aSRandall Stewart 					}
2339f8829a4aSRandall Stewart 					if (inserted == 0) {
2340f8829a4aSRandall Stewart 						/*
2341f8829a4aSRandall Stewart 						 * must be put at end, use
2342f8829a4aSRandall Stewart 						 * prevP (all setup from
2343f8829a4aSRandall Stewart 						 * loop) to setup nextP.
2344f8829a4aSRandall Stewart 						 */
2345f8829a4aSRandall Stewart 						TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2346f8829a4aSRandall Stewart 					}
2347f8829a4aSRandall Stewart 				}
2348f8829a4aSRandall Stewart 			} else {
2349f8829a4aSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, control, abort_flag);
2350f8829a4aSRandall Stewart 				if (*abort_flag) {
2351f8829a4aSRandall Stewart 					return (0);
2352f8829a4aSRandall Stewart 				}
2353f8829a4aSRandall Stewart 			}
2354f8829a4aSRandall Stewart 		}
2355f8829a4aSRandall Stewart 	} else {
2356f8829a4aSRandall Stewart 		/* Into the re-assembly queue */
2357f8829a4aSRandall Stewart 		sctp_queue_data_for_reasm(stcb, asoc, chk, abort_flag);
2358f8829a4aSRandall Stewart 		if (*abort_flag) {
2359a5d547adSRandall Stewart 			/*
2360a5d547adSRandall Stewart 			 * the assoc is now gone and chk was put onto the
2361a5d547adSRandall Stewart 			 * reasm queue, which has all been freed.
2362a5d547adSRandall Stewart 			 */
2363a5d547adSRandall Stewart 			*m = NULL;
2364f8829a4aSRandall Stewart 			return (0);
2365f8829a4aSRandall Stewart 		}
2366f8829a4aSRandall Stewart 	}
2367f8829a4aSRandall Stewart finish_express_del:
2368f8829a4aSRandall Stewart 	if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
2369f8829a4aSRandall Stewart 		/* we have a new high score */
2370f8829a4aSRandall Stewart 		asoc->highest_tsn_inside_map = tsn;
2371b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2372f8829a4aSRandall Stewart 			sctp_log_map(0, 2, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
237380fefe0aSRandall Stewart 		}
2374f8829a4aSRandall Stewart 	}
2375f8829a4aSRandall Stewart 	if (tsn == (asoc->cumulative_tsn + 1)) {
2376f8829a4aSRandall Stewart 		/* Update cum-ack */
2377f8829a4aSRandall Stewart 		asoc->cumulative_tsn = tsn;
2378f8829a4aSRandall Stewart 	}
2379f8829a4aSRandall Stewart 	if (last_chunk) {
2380f8829a4aSRandall Stewart 		*m = NULL;
2381f8829a4aSRandall Stewart 	}
2382f42a358aSRandall Stewart 	if (ordered) {
2383f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_inorderchunks);
2384f8829a4aSRandall Stewart 	} else {
2385f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_inunorderchunks);
2386f8829a4aSRandall Stewart 	}
2387f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvdata);
2388f8829a4aSRandall Stewart 	/* Set it present please */
2389b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
23906a91f103SRandall Stewart 		sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno, SCTP_STR_LOG_FROM_MARK_TSN);
239180fefe0aSRandall Stewart 	}
2392b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2393f8829a4aSRandall Stewart 		sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2394f8829a4aSRandall Stewart 		    asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE);
239580fefe0aSRandall Stewart 	}
2396207304d4SRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
2397f8829a4aSRandall Stewart 	SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
23988933fa13SRandall Stewart 
23998933fa13SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) &&
24008933fa13SRandall Stewart 	    asoc->peer_supports_nr_sack &&
24018933fa13SRandall Stewart 	    (SCTP_BASE_SYSCTL(sctp_do_drain) == 0)) {
24028933fa13SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
24038933fa13SRandall Stewart 		if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN)) {
24048933fa13SRandall Stewart 			asoc->highest_tsn_inside_nr_map = tsn;
24058933fa13SRandall Stewart 		}
24068933fa13SRandall Stewart 	}
240717205eccSRandall Stewart 	/* check the special flag for stream resets */
240817205eccSRandall Stewart 	if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2409d61a0ae0SRandall Stewart 	    ((compare_with_wrap(asoc->cumulative_tsn, liste->tsn, MAX_TSN)) ||
2410d61a0ae0SRandall Stewart 	    (asoc->cumulative_tsn == liste->tsn))
241117205eccSRandall Stewart 	    ) {
241217205eccSRandall Stewart 		/*
241317205eccSRandall Stewart 		 * we have finished working through the backlogged TSN's now
241417205eccSRandall Stewart 		 * time to reset streams. 1: call reset function. 2: free
241517205eccSRandall Stewart 		 * pending_reply space 3: distribute any chunks in
241617205eccSRandall Stewart 		 * pending_reply_queue.
241717205eccSRandall Stewart 		 */
241817205eccSRandall Stewart 		struct sctp_queued_to_read *ctl;
241917205eccSRandall Stewart 
242017205eccSRandall Stewart 		sctp_reset_in_stream(stcb, liste->number_entries, liste->req.list_of_streams);
242117205eccSRandall Stewart 		TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
2422207304d4SRandall Stewart 		SCTP_FREE(liste, SCTP_M_STRESET);
24233c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
242417205eccSRandall Stewart 		liste = TAILQ_FIRST(&asoc->resetHead);
242517205eccSRandall Stewart 		ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
242617205eccSRandall Stewart 		if (ctl && (liste == NULL)) {
242717205eccSRandall Stewart 			/* All can be removed */
242817205eccSRandall Stewart 			while (ctl) {
242917205eccSRandall Stewart 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
243017205eccSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
243117205eccSRandall Stewart 				if (*abort_flag) {
243217205eccSRandall Stewart 					return (0);
243317205eccSRandall Stewart 				}
243417205eccSRandall Stewart 				ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
243517205eccSRandall Stewart 			}
243617205eccSRandall Stewart 		} else if (ctl) {
243717205eccSRandall Stewart 			/* more than one in queue */
2438d61a0ae0SRandall Stewart 			while (!compare_with_wrap(ctl->sinfo_tsn, liste->tsn, MAX_TSN)) {
243917205eccSRandall Stewart 				/*
244017205eccSRandall Stewart 				 * if ctl->sinfo_tsn is <= liste->tsn we can
244117205eccSRandall Stewart 				 * process it which is the NOT of
244217205eccSRandall Stewart 				 * ctl->sinfo_tsn > liste->tsn
244317205eccSRandall Stewart 				 */
244417205eccSRandall Stewart 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
244517205eccSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
244617205eccSRandall Stewart 				if (*abort_flag) {
244717205eccSRandall Stewart 					return (0);
244817205eccSRandall Stewart 				}
244917205eccSRandall Stewart 				ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
245017205eccSRandall Stewart 			}
245117205eccSRandall Stewart 		}
245217205eccSRandall Stewart 		/*
245317205eccSRandall Stewart 		 * Now service re-assembly to pick up anything that has been
245417205eccSRandall Stewart 		 * held on reassembly queue?
245517205eccSRandall Stewart 		 */
245617205eccSRandall Stewart 		sctp_deliver_reasm_check(stcb, asoc);
245717205eccSRandall Stewart 		need_reasm_check = 0;
245817205eccSRandall Stewart 	}
2459139bc87fSRandall Stewart 	if (need_reasm_check) {
2460139bc87fSRandall Stewart 		/* Another one waits ? */
2461139bc87fSRandall Stewart 		sctp_deliver_reasm_check(stcb, asoc);
2462139bc87fSRandall Stewart 	}
2463f8829a4aSRandall Stewart 	return (1);
2464f8829a4aSRandall Stewart }
2465f8829a4aSRandall Stewart 
2466f8829a4aSRandall Stewart int8_t sctp_map_lookup_tab[256] = {
2467f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2468f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2469f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2470f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 4,
2471f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2472f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2473f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2474f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 5,
2475f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2476f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2477f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2478f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 4,
2479f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2480f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2481f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2482f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 6,
2483f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2484f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2485f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2486f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 4,
2487f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2488f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2489f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2490f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 5,
2491f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2492f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2493f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2494f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 4,
2495f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2496f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2497f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2498f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 7,
2499f8829a4aSRandall Stewart };
2500f8829a4aSRandall Stewart 
2501f8829a4aSRandall Stewart 
2502f8829a4aSRandall Stewart void
2503f8829a4aSRandall Stewart sctp_sack_check(struct sctp_tcb *stcb, int ok_to_sack, int was_a_gap, int *abort_flag)
2504f8829a4aSRandall Stewart {
2505f8829a4aSRandall Stewart 	/*
2506f8829a4aSRandall Stewart 	 * Now we also need to check the mapping array in a couple of ways.
2507f8829a4aSRandall Stewart 	 * 1) Did we move the cum-ack point?
2508f8829a4aSRandall Stewart 	 */
2509f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2510b3f1ea41SRandall Stewart 	int at;
2511c4739e2fSRandall Stewart 	int last_all_ones = 0;
2512f8829a4aSRandall Stewart 	int slide_from, slide_end, lgap, distance;
2513830d754dSRandall Stewart 
2514830d754dSRandall Stewart 	/* EY nr_mapping array variables */
25158933fa13SRandall Stewart 	/* int nr_at; */
25168933fa13SRandall Stewart 	/* int nr_last_all_ones = 0; */
25178933fa13SRandall Stewart 	/* int nr_slide_from, nr_slide_end, nr_lgap, nr_distance; */
2518830d754dSRandall Stewart 
2519f8829a4aSRandall Stewart 	uint32_t old_cumack, old_base, old_highest;
2520f8829a4aSRandall Stewart 	unsigned char aux_array[64];
2521f8829a4aSRandall Stewart 
2522830d754dSRandall Stewart 	/*
2523830d754dSRandall Stewart 	 * EY! Don't think this is required but I am immitating the code for
2524830d754dSRandall Stewart 	 * map just to make sure
2525830d754dSRandall Stewart 	 */
2526830d754dSRandall Stewart 	unsigned char nr_aux_array[64];
2527f8829a4aSRandall Stewart 
2528f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2529f8829a4aSRandall Stewart 	at = 0;
2530f8829a4aSRandall Stewart 
2531f8829a4aSRandall Stewart 	old_cumack = asoc->cumulative_tsn;
2532f8829a4aSRandall Stewart 	old_base = asoc->mapping_array_base_tsn;
2533f8829a4aSRandall Stewart 	old_highest = asoc->highest_tsn_inside_map;
2534f8829a4aSRandall Stewart 	if (asoc->mapping_array_size < 64)
2535f8829a4aSRandall Stewart 		memcpy(aux_array, asoc->mapping_array,
2536f8829a4aSRandall Stewart 		    asoc->mapping_array_size);
2537f8829a4aSRandall Stewart 	else
2538f8829a4aSRandall Stewart 		memcpy(aux_array, asoc->mapping_array, 64);
2539830d754dSRandall Stewart 	/* EY do the same for nr_mapping_array */
2540830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
2541f8829a4aSRandall Stewart 
2542830d754dSRandall Stewart 		if (asoc->nr_mapping_array_size != asoc->mapping_array_size) {
2543830d754dSRandall Stewart 			/*
2544830d754dSRandall Stewart 			 * printf("\nEY-IN sack_check method: \nEY-" "The
2545830d754dSRandall Stewart 			 * size of map and nr_map are inconsitent")
2546830d754dSRandall Stewart 			 */ ;
2547830d754dSRandall Stewart 		}
2548830d754dSRandall Stewart 		if (asoc->nr_mapping_array_base_tsn != asoc->mapping_array_base_tsn) {
2549830d754dSRandall Stewart 			/*
2550830d754dSRandall Stewart 			 * printf("\nEY-IN sack_check method VERY CRUCIAL
2551830d754dSRandall Stewart 			 * error: \nEY-" "The base tsns of map and nr_map
2552830d754dSRandall Stewart 			 * are inconsitent")
2553830d754dSRandall Stewart 			 */ ;
2554830d754dSRandall Stewart 		}
2555830d754dSRandall Stewart 		/* EY! just immitating the above code */
2556830d754dSRandall Stewart 		if (asoc->nr_mapping_array_size < 64)
2557830d754dSRandall Stewart 			memcpy(nr_aux_array, asoc->nr_mapping_array,
2558830d754dSRandall Stewart 			    asoc->nr_mapping_array_size);
2559830d754dSRandall Stewart 		else
2560830d754dSRandall Stewart 			memcpy(aux_array, asoc->nr_mapping_array, 64);
2561830d754dSRandall Stewart 	}
2562f8829a4aSRandall Stewart 	/*
2563f8829a4aSRandall Stewart 	 * We could probably improve this a small bit by calculating the
2564f8829a4aSRandall Stewart 	 * offset of the current cum-ack as the starting point.
2565f8829a4aSRandall Stewart 	 */
2566f8829a4aSRandall Stewart 	at = 0;
2567b3f1ea41SRandall Stewart 	for (slide_from = 0; slide_from < stcb->asoc.mapping_array_size; slide_from++) {
2568d06c82f1SRandall Stewart 
2569b3f1ea41SRandall Stewart 		if (asoc->mapping_array[slide_from] == 0xff) {
2570f8829a4aSRandall Stewart 			at += 8;
257193164cf9SRandall Stewart 			last_all_ones = 1;
2572f8829a4aSRandall Stewart 		} else {
2573f8829a4aSRandall Stewart 			/* there is a 0 bit */
2574b3f1ea41SRandall Stewart 			at += sctp_map_lookup_tab[asoc->mapping_array[slide_from]];
257593164cf9SRandall Stewart 			last_all_ones = 0;
2576f8829a4aSRandall Stewart 			break;
2577f8829a4aSRandall Stewart 		}
2578f8829a4aSRandall Stewart 	}
257993164cf9SRandall Stewart 	asoc->cumulative_tsn = asoc->mapping_array_base_tsn + (at - last_all_ones);
2580f8829a4aSRandall Stewart 	/* at is one off, since in the table a embedded -1 is present */
2581f8829a4aSRandall Stewart 	at++;
2582f8829a4aSRandall Stewart 
2583f8829a4aSRandall Stewart 	if (compare_with_wrap(asoc->cumulative_tsn,
2584f8829a4aSRandall Stewart 	    asoc->highest_tsn_inside_map,
2585f8829a4aSRandall Stewart 	    MAX_TSN)) {
2586a5d547adSRandall Stewart #ifdef INVARIANTS
2587ceaad40aSRandall Stewart 		panic("huh, cumack 0x%x greater than high-tsn 0x%x in map",
2588ceaad40aSRandall Stewart 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2589f8829a4aSRandall Stewart #else
2590ceaad40aSRandall Stewart 		SCTP_PRINTF("huh, cumack 0x%x greater than high-tsn 0x%x in map - should panic?\n",
2591ceaad40aSRandall Stewart 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2592b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2593b3f1ea41SRandall Stewart 			sctp_log_map(0, 6, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
2594b3f1ea41SRandall Stewart 		}
2595f8829a4aSRandall Stewart 		asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2596830d754dSRandall Stewart 		asoc->highest_tsn_inside_nr_map = asoc->cumulative_tsn;
2597f8829a4aSRandall Stewart #endif
2598f8829a4aSRandall Stewart 	}
2599c4739e2fSRandall Stewart 	if ((asoc->cumulative_tsn == asoc->highest_tsn_inside_map) && (at >= 8)) {
2600f8829a4aSRandall Stewart 		/* The complete array was completed by a single FR */
2601f8829a4aSRandall Stewart 		/* higest becomes the cum-ack */
2602f8829a4aSRandall Stewart 		int clr;
2603f8829a4aSRandall Stewart 
2604f8829a4aSRandall Stewart 		asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
2605f8829a4aSRandall Stewart 		/* clear the array */
2606f8829a4aSRandall Stewart 		clr = (at >> 3) + 1;
2607c4739e2fSRandall Stewart 		if (clr > asoc->mapping_array_size) {
2608f8829a4aSRandall Stewart 			clr = asoc->mapping_array_size;
2609f8829a4aSRandall Stewart 		}
2610f8829a4aSRandall Stewart 		memset(asoc->mapping_array, 0, clr);
2611f8829a4aSRandall Stewart 		/* base becomes one ahead of the cum-ack */
2612f8829a4aSRandall Stewart 		asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
2613830d754dSRandall Stewart 
2614830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
2615830d754dSRandall Stewart 
2616830d754dSRandall Stewart 			if (clr > asoc->nr_mapping_array_size)
2617830d754dSRandall Stewart 				clr = asoc->nr_mapping_array_size;
2618830d754dSRandall Stewart 
2619830d754dSRandall Stewart 			memset(asoc->nr_mapping_array, 0, clr);
2620830d754dSRandall Stewart 			/* base becomes one ahead of the cum-ack */
2621830d754dSRandall Stewart 			asoc->nr_mapping_array_base_tsn = asoc->cumulative_tsn + 1;
2622830d754dSRandall Stewart 			asoc->highest_tsn_inside_nr_map = asoc->cumulative_tsn;
2623830d754dSRandall Stewart 		}
2624b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2625f8829a4aSRandall Stewart 			sctp_log_map(old_base, old_cumack, old_highest,
2626f8829a4aSRandall Stewart 			    SCTP_MAP_PREPARE_SLIDE);
2627f8829a4aSRandall Stewart 			sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2628f8829a4aSRandall Stewart 			    asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_CLEARED);
262980fefe0aSRandall Stewart 		}
2630f8829a4aSRandall Stewart 	} else if (at >= 8) {
2631f8829a4aSRandall Stewart 		/* we can slide the mapping array down */
2632b3f1ea41SRandall Stewart 		/* slide_from holds where we hit the first NON 0xff byte */
2633b3f1ea41SRandall Stewart 
2634f8829a4aSRandall Stewart 		/*
2635f8829a4aSRandall Stewart 		 * now calculate the ceiling of the move using our highest
2636f8829a4aSRandall Stewart 		 * TSN value
2637f8829a4aSRandall Stewart 		 */
2638f8829a4aSRandall Stewart 		if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
2639f8829a4aSRandall Stewart 			lgap = asoc->highest_tsn_inside_map -
2640f8829a4aSRandall Stewart 			    asoc->mapping_array_base_tsn;
2641f8829a4aSRandall Stewart 		} else {
2642f8829a4aSRandall Stewart 			lgap = (MAX_TSN - asoc->mapping_array_base_tsn) +
2643f8829a4aSRandall Stewart 			    asoc->highest_tsn_inside_map + 1;
2644f8829a4aSRandall Stewart 		}
2645f8829a4aSRandall Stewart 		slide_end = lgap >> 3;
2646f8829a4aSRandall Stewart 		if (slide_end < slide_from) {
2647d55b0b1bSRandall Stewart #ifdef INVARIANTS
2648f8829a4aSRandall Stewart 			panic("impossible slide");
2649d55b0b1bSRandall Stewart #else
2650d55b0b1bSRandall Stewart 			printf("impossible slide?\n");
2651d55b0b1bSRandall Stewart 			return;
2652d55b0b1bSRandall Stewart #endif
2653f8829a4aSRandall Stewart 		}
2654b3f1ea41SRandall Stewart 		if (slide_end > asoc->mapping_array_size) {
2655b3f1ea41SRandall Stewart #ifdef INVARIANTS
2656b3f1ea41SRandall Stewart 			panic("would overrun buffer");
2657b3f1ea41SRandall Stewart #else
2658b3f1ea41SRandall Stewart 			printf("Gak, would have overrun map end:%d slide_end:%d\n",
2659b3f1ea41SRandall Stewart 			    asoc->mapping_array_size, slide_end);
2660b3f1ea41SRandall Stewart 			slide_end = asoc->mapping_array_size;
2661b3f1ea41SRandall Stewart #endif
2662b3f1ea41SRandall Stewart 		}
2663f8829a4aSRandall Stewart 		distance = (slide_end - slide_from) + 1;
2664b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2665f8829a4aSRandall Stewart 			sctp_log_map(old_base, old_cumack, old_highest,
2666f8829a4aSRandall Stewart 			    SCTP_MAP_PREPARE_SLIDE);
2667f8829a4aSRandall Stewart 			sctp_log_map((uint32_t) slide_from, (uint32_t) slide_end,
2668f8829a4aSRandall Stewart 			    (uint32_t) lgap, SCTP_MAP_SLIDE_FROM);
266980fefe0aSRandall Stewart 		}
2670f8829a4aSRandall Stewart 		if (distance + slide_from > asoc->mapping_array_size ||
2671f8829a4aSRandall Stewart 		    distance < 0) {
2672f8829a4aSRandall Stewart 			/*
2673f8829a4aSRandall Stewart 			 * Here we do NOT slide forward the array so that
2674f8829a4aSRandall Stewart 			 * hopefully when more data comes in to fill it up
2675f8829a4aSRandall Stewart 			 * we will be able to slide it forward. Really I
2676f8829a4aSRandall Stewart 			 * don't think this should happen :-0
2677f8829a4aSRandall Stewart 			 */
2678f8829a4aSRandall Stewart 
2679b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2680f8829a4aSRandall Stewart 				sctp_log_map((uint32_t) distance, (uint32_t) slide_from,
2681f8829a4aSRandall Stewart 				    (uint32_t) asoc->mapping_array_size,
2682f8829a4aSRandall Stewart 				    SCTP_MAP_SLIDE_NONE);
268380fefe0aSRandall Stewart 			}
2684f8829a4aSRandall Stewart 		} else {
2685f8829a4aSRandall Stewart 			int ii;
2686f8829a4aSRandall Stewart 
2687f8829a4aSRandall Stewart 			for (ii = 0; ii < distance; ii++) {
2688f8829a4aSRandall Stewart 				asoc->mapping_array[ii] =
2689f8829a4aSRandall Stewart 				    asoc->mapping_array[slide_from + ii];
2690f8829a4aSRandall Stewart 			}
2691f8829a4aSRandall Stewart 			for (ii = distance; ii <= slide_end; ii++) {
2692f8829a4aSRandall Stewart 				asoc->mapping_array[ii] = 0;
2693f8829a4aSRandall Stewart 			}
2694f8829a4aSRandall Stewart 			asoc->mapping_array_base_tsn += (slide_from << 3);
2695b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2696f8829a4aSRandall Stewart 				sctp_log_map(asoc->mapping_array_base_tsn,
2697f8829a4aSRandall Stewart 				    asoc->cumulative_tsn, asoc->highest_tsn_inside_map,
2698f8829a4aSRandall Stewart 				    SCTP_MAP_SLIDE_RESULT);
269980fefe0aSRandall Stewart 			}
2700f8829a4aSRandall Stewart 			/*
27018933fa13SRandall Stewart 			 * EY if doing nr_sacks then slide the
27028933fa13SRandall Stewart 			 * nr_mapping_array accordingly please
2703830d754dSRandall Stewart 			 */
2704830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
27058933fa13SRandall Stewart 				for (ii = 0; ii < distance; ii++) {
2706830d754dSRandall Stewart 					asoc->nr_mapping_array[ii] =
27078933fa13SRandall Stewart 					    asoc->nr_mapping_array[slide_from + ii];
2708830d754dSRandall Stewart 				}
27098933fa13SRandall Stewart 				for (ii = distance; ii <= slide_end; ii++) {
2710830d754dSRandall Stewart 					asoc->nr_mapping_array[ii] = 0;
2711830d754dSRandall Stewart 				}
27128933fa13SRandall Stewart 				asoc->nr_mapping_array_base_tsn += (slide_from << 3);
2713830d754dSRandall Stewart 			}
2714830d754dSRandall Stewart 		}
2715830d754dSRandall Stewart 	}
2716830d754dSRandall Stewart 	/*
2717f8829a4aSRandall Stewart 	 * Now we need to see if we need to queue a sack or just start the
2718f8829a4aSRandall Stewart 	 * timer (if allowed).
2719f8829a4aSRandall Stewart 	 */
2720f8829a4aSRandall Stewart 	if (ok_to_sack) {
2721f8829a4aSRandall Stewart 		if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2722f8829a4aSRandall Stewart 			/*
2723f8829a4aSRandall Stewart 			 * Ok special case, in SHUTDOWN-SENT case. here we
2724f8829a4aSRandall Stewart 			 * maker sure SACK timer is off and instead send a
2725f8829a4aSRandall Stewart 			 * SHUTDOWN and a SACK
2726f8829a4aSRandall Stewart 			 */
2727139bc87fSRandall Stewart 			if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2728f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
2729a5d547adSRandall Stewart 				    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_INDATA + SCTP_LOC_18);
2730f8829a4aSRandall Stewart 			}
2731f8829a4aSRandall Stewart 			sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
2732830d754dSRandall Stewart 			/*
2733830d754dSRandall Stewart 			 * EY if nr_sacks used then send an nr-sack , a sack
2734830d754dSRandall Stewart 			 * otherwise
2735830d754dSRandall Stewart 			 */
27368933fa13SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack)
2737830d754dSRandall Stewart 				sctp_send_nr_sack(stcb);
2738830d754dSRandall Stewart 			else
2739f8829a4aSRandall Stewart 				sctp_send_sack(stcb);
2740f8829a4aSRandall Stewart 		} else {
2741f8829a4aSRandall Stewart 			int is_a_gap;
2742f8829a4aSRandall Stewart 
2743f8829a4aSRandall Stewart 			/* is there a gap now ? */
2744f8829a4aSRandall Stewart 			is_a_gap = compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
2745f8829a4aSRandall Stewart 			    stcb->asoc.cumulative_tsn, MAX_TSN);
2746f8829a4aSRandall Stewart 
2747f8829a4aSRandall Stewart 			/*
2748f8829a4aSRandall Stewart 			 * CMT DAC algorithm: increase number of packets
2749f8829a4aSRandall Stewart 			 * received since last ack
2750f8829a4aSRandall Stewart 			 */
2751f8829a4aSRandall Stewart 			stcb->asoc.cmt_dac_pkts_rcvd++;
2752f8829a4aSRandall Stewart 
275342551e99SRandall Stewart 			if ((stcb->asoc.send_sack == 1) ||	/* We need to send a
275442551e99SRandall Stewart 								 * SACK */
2755f8829a4aSRandall Stewart 			    ((was_a_gap) && (is_a_gap == 0)) ||	/* was a gap, but no
2756f8829a4aSRandall Stewart 								 * longer is one */
2757f8829a4aSRandall Stewart 			    (stcb->asoc.numduptsns) ||	/* we have dup's */
2758f8829a4aSRandall Stewart 			    (is_a_gap) ||	/* is still a gap */
275942551e99SRandall Stewart 			    (stcb->asoc.delayed_ack == 0) ||	/* Delayed sack disabled */
276042551e99SRandall Stewart 			    (stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq)	/* hit limit of pkts */
2761f8829a4aSRandall Stewart 			    ) {
2762f8829a4aSRandall Stewart 
2763b3f1ea41SRandall Stewart 				if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off)) &&
2764b3f1ea41SRandall Stewart 				    (SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) &&
276542551e99SRandall Stewart 				    (stcb->asoc.send_sack == 0) &&
2766f8829a4aSRandall Stewart 				    (stcb->asoc.numduptsns == 0) &&
2767f8829a4aSRandall Stewart 				    (stcb->asoc.delayed_ack) &&
2768139bc87fSRandall Stewart 				    (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer))) {
2769f8829a4aSRandall Stewart 
2770f8829a4aSRandall Stewart 					/*
2771f8829a4aSRandall Stewart 					 * CMT DAC algorithm: With CMT,
2772f8829a4aSRandall Stewart 					 * delay acks even in the face of
2773f8829a4aSRandall Stewart 					 *
2774f8829a4aSRandall Stewart 					 * reordering. Therefore, if acks that
2775f8829a4aSRandall Stewart 					 * do not have to be sent because of
2776f8829a4aSRandall Stewart 					 * the above reasons, will be
2777f8829a4aSRandall Stewart 					 * delayed. That is, acks that would
2778f8829a4aSRandall Stewart 					 * have been sent due to gap reports
2779f8829a4aSRandall Stewart 					 * will be delayed with DAC. Start
2780f8829a4aSRandall Stewart 					 * the delayed ack timer.
2781f8829a4aSRandall Stewart 					 */
2782f8829a4aSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2783f8829a4aSRandall Stewart 					    stcb->sctp_ep, stcb, NULL);
2784f8829a4aSRandall Stewart 				} else {
2785f8829a4aSRandall Stewart 					/*
2786f8829a4aSRandall Stewart 					 * Ok we must build a SACK since the
2787f8829a4aSRandall Stewart 					 * timer is pending, we got our
2788f8829a4aSRandall Stewart 					 * first packet OR there are gaps or
2789f8829a4aSRandall Stewart 					 * duplicates.
2790f8829a4aSRandall Stewart 					 */
2791ad81507eSRandall Stewart 					(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2792830d754dSRandall Stewart 					/*
2793830d754dSRandall Stewart 					 * EY if nr_sacks used then send an
2794830d754dSRandall Stewart 					 * nr-sack , a sack otherwise
2795830d754dSRandall Stewart 					 */
2796830d754dSRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack)
2797830d754dSRandall Stewart 						sctp_send_nr_sack(stcb);
2798830d754dSRandall Stewart 					else
2799f8829a4aSRandall Stewart 						sctp_send_sack(stcb);
2800f8829a4aSRandall Stewart 				}
2801f8829a4aSRandall Stewart 			} else {
280242551e99SRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2803f8829a4aSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2804f8829a4aSRandall Stewart 					    stcb->sctp_ep, stcb, NULL);
2805f8829a4aSRandall Stewart 				}
2806f8829a4aSRandall Stewart 			}
2807f8829a4aSRandall Stewart 		}
2808f8829a4aSRandall Stewart 	}
280942551e99SRandall Stewart }
2810f8829a4aSRandall Stewart 
2811f8829a4aSRandall Stewart void
2812f8829a4aSRandall Stewart sctp_service_queues(struct sctp_tcb *stcb, struct sctp_association *asoc)
2813f8829a4aSRandall Stewart {
2814f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
2815f8829a4aSRandall Stewart 	uint32_t tsize;
2816f8829a4aSRandall Stewart 	uint16_t nxt_todel;
2817f8829a4aSRandall Stewart 
2818f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
2819f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
2820f8829a4aSRandall Stewart 	}
2821f8829a4aSRandall Stewart 	/* Can we proceed further, i.e. the PD-API is complete */
2822f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
2823f8829a4aSRandall Stewart 		/* no */
2824f8829a4aSRandall Stewart 		return;
2825f8829a4aSRandall Stewart 	}
2826f8829a4aSRandall Stewart 	/*
2827f8829a4aSRandall Stewart 	 * Now is there some other chunk I can deliver from the reassembly
2828f8829a4aSRandall Stewart 	 * queue.
2829f8829a4aSRandall Stewart 	 */
2830139bc87fSRandall Stewart doit_again:
2831f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
2832f8829a4aSRandall Stewart 	if (chk == NULL) {
2833f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = 0;
2834f8829a4aSRandall Stewart 		asoc->cnt_on_reasm_queue = 0;
2835f8829a4aSRandall Stewart 		return;
2836f8829a4aSRandall Stewart 	}
2837f8829a4aSRandall Stewart 	nxt_todel = asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
2838f8829a4aSRandall Stewart 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
2839f8829a4aSRandall Stewart 	    ((nxt_todel == chk->rec.data.stream_seq) ||
2840f8829a4aSRandall Stewart 	    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
2841f8829a4aSRandall Stewart 		/*
2842f8829a4aSRandall Stewart 		 * Yep the first one is here. We setup to start reception,
2843f8829a4aSRandall Stewart 		 * by backing down the TSN just in case we can't deliver.
2844f8829a4aSRandall Stewart 		 */
2845f8829a4aSRandall Stewart 
2846f8829a4aSRandall Stewart 		/*
2847f8829a4aSRandall Stewart 		 * Before we start though either all of the message should
2848f8829a4aSRandall Stewart 		 * be here or 1/4 the socket buffer max or nothing on the
2849f8829a4aSRandall Stewart 		 * delivery queue and something can be delivered.
2850f8829a4aSRandall Stewart 		 */
2851f8829a4aSRandall Stewart 		if ((sctp_is_all_msg_on_reasm(asoc, &tsize) ||
2852c4739e2fSRandall Stewart 		    (tsize >= stcb->sctp_ep->partial_delivery_point))) {
2853f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 1;
2854f8829a4aSRandall Stewart 			asoc->tsn_last_delivered = chk->rec.data.TSN_seq - 1;
2855f8829a4aSRandall Stewart 			asoc->str_of_pdapi = chk->rec.data.stream_number;
2856f8829a4aSRandall Stewart 			asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
2857f8829a4aSRandall Stewart 			asoc->pdapi_ppid = chk->rec.data.payloadtype;
2858f8829a4aSRandall Stewart 			asoc->fragment_flags = chk->rec.data.rcv_flags;
2859f8829a4aSRandall Stewart 			sctp_service_reassembly(stcb, asoc);
2860139bc87fSRandall Stewart 			if (asoc->fragmented_delivery_inprogress == 0) {
2861139bc87fSRandall Stewart 				goto doit_again;
2862139bc87fSRandall Stewart 			}
2863f8829a4aSRandall Stewart 		}
2864f8829a4aSRandall Stewart 	}
2865f8829a4aSRandall Stewart }
2866f8829a4aSRandall Stewart 
2867f8829a4aSRandall Stewart int
2868f8829a4aSRandall Stewart sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length,
2869f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2870f8829a4aSRandall Stewart     struct sctp_nets *net, uint32_t * high_tsn)
2871f8829a4aSRandall Stewart {
2872f8829a4aSRandall Stewart 	struct sctp_data_chunk *ch, chunk_buf;
2873f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2874f8829a4aSRandall Stewart 	int num_chunks = 0;	/* number of control chunks processed */
2875f8829a4aSRandall Stewart 	int stop_proc = 0;
2876f8829a4aSRandall Stewart 	int chk_length, break_flag, last_chunk;
2877f8829a4aSRandall Stewart 	int abort_flag = 0, was_a_gap = 0;
2878f8829a4aSRandall Stewart 	struct mbuf *m;
2879f8829a4aSRandall Stewart 
2880f8829a4aSRandall Stewart 	/* set the rwnd */
2881f8829a4aSRandall Stewart 	sctp_set_rwnd(stcb, &stcb->asoc);
2882f8829a4aSRandall Stewart 
2883f8829a4aSRandall Stewart 	m = *mm;
2884f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
2885f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2886f8829a4aSRandall Stewart 	if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
2887f8829a4aSRandall Stewart 	    stcb->asoc.cumulative_tsn, MAX_TSN)) {
2888f8829a4aSRandall Stewart 		/* there was a gap before this data was processed */
2889f8829a4aSRandall Stewart 		was_a_gap = 1;
2890f8829a4aSRandall Stewart 	}
2891f8829a4aSRandall Stewart 	/*
2892f8829a4aSRandall Stewart 	 * setup where we got the last DATA packet from for any SACK that
2893f8829a4aSRandall Stewart 	 * may need to go out. Don't bump the net. This is done ONLY when a
2894f8829a4aSRandall Stewart 	 * chunk is assigned.
2895f8829a4aSRandall Stewart 	 */
2896f8829a4aSRandall Stewart 	asoc->last_data_chunk_from = net;
2897f8829a4aSRandall Stewart 
2898d06c82f1SRandall Stewart 	/*-
2899f8829a4aSRandall Stewart 	 * Now before we proceed we must figure out if this is a wasted
2900f8829a4aSRandall Stewart 	 * cluster... i.e. it is a small packet sent in and yet the driver
2901f8829a4aSRandall Stewart 	 * underneath allocated a full cluster for it. If so we must copy it
2902f8829a4aSRandall Stewart 	 * to a smaller mbuf and free up the cluster mbuf. This will help
2903d06c82f1SRandall Stewart 	 * with cluster starvation. Note for __Panda__ we don't do this
2904d06c82f1SRandall Stewart 	 * since it has clusters all the way down to 64 bytes.
2905f8829a4aSRandall Stewart 	 */
290644b7479bSRandall Stewart 	if (SCTP_BUF_LEN(m) < (long)MLEN && SCTP_BUF_NEXT(m) == NULL) {
2907f8829a4aSRandall Stewart 		/* we only handle mbufs that are singletons.. not chains */
2908139bc87fSRandall Stewart 		m = sctp_get_mbuf_for_msg(SCTP_BUF_LEN(m), 0, M_DONTWAIT, 1, MT_DATA);
2909f8829a4aSRandall Stewart 		if (m) {
2910f8829a4aSRandall Stewart 			/* ok lets see if we can copy the data up */
2911f8829a4aSRandall Stewart 			caddr_t *from, *to;
2912f8829a4aSRandall Stewart 
2913f8829a4aSRandall Stewart 			/* get the pointers and copy */
2914f8829a4aSRandall Stewart 			to = mtod(m, caddr_t *);
2915f8829a4aSRandall Stewart 			from = mtod((*mm), caddr_t *);
2916139bc87fSRandall Stewart 			memcpy(to, from, SCTP_BUF_LEN((*mm)));
2917f8829a4aSRandall Stewart 			/* copy the length and free up the old */
2918139bc87fSRandall Stewart 			SCTP_BUF_LEN(m) = SCTP_BUF_LEN((*mm));
2919f8829a4aSRandall Stewart 			sctp_m_freem(*mm);
2920f8829a4aSRandall Stewart 			/* sucess, back copy */
2921f8829a4aSRandall Stewart 			*mm = m;
2922f8829a4aSRandall Stewart 		} else {
2923f8829a4aSRandall Stewart 			/* We are in trouble in the mbuf world .. yikes */
2924f8829a4aSRandall Stewart 			m = *mm;
2925f8829a4aSRandall Stewart 		}
2926f8829a4aSRandall Stewart 	}
2927f8829a4aSRandall Stewart 	/* get pointer to the first chunk header */
2928f8829a4aSRandall Stewart 	ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2929f8829a4aSRandall Stewart 	    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2930f8829a4aSRandall Stewart 	if (ch == NULL) {
2931f8829a4aSRandall Stewart 		return (1);
2932f8829a4aSRandall Stewart 	}
2933f8829a4aSRandall Stewart 	/*
2934f8829a4aSRandall Stewart 	 * process all DATA chunks...
2935f8829a4aSRandall Stewart 	 */
2936f8829a4aSRandall Stewart 	*high_tsn = asoc->cumulative_tsn;
2937f8829a4aSRandall Stewart 	break_flag = 0;
293842551e99SRandall Stewart 	asoc->data_pkts_seen++;
2939f8829a4aSRandall Stewart 	while (stop_proc == 0) {
2940f8829a4aSRandall Stewart 		/* validate chunk length */
2941f8829a4aSRandall Stewart 		chk_length = ntohs(ch->ch.chunk_length);
2942f8829a4aSRandall Stewart 		if (length - *offset < chk_length) {
2943f8829a4aSRandall Stewart 			/* all done, mutulated chunk */
2944f8829a4aSRandall Stewart 			stop_proc = 1;
2945f8829a4aSRandall Stewart 			break;
2946f8829a4aSRandall Stewart 		}
2947f8829a4aSRandall Stewart 		if (ch->ch.chunk_type == SCTP_DATA) {
2948f8829a4aSRandall Stewart 			if ((size_t)chk_length < sizeof(struct sctp_data_chunk) + 1) {
2949f8829a4aSRandall Stewart 				/*
2950f8829a4aSRandall Stewart 				 * Need to send an abort since we had a
2951f8829a4aSRandall Stewart 				 * invalid data chunk.
2952f8829a4aSRandall Stewart 				 */
2953f8829a4aSRandall Stewart 				struct mbuf *op_err;
2954f8829a4aSRandall Stewart 
2955139bc87fSRandall Stewart 				op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 2 * sizeof(uint32_t)),
2956f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
2957f8829a4aSRandall Stewart 
2958f8829a4aSRandall Stewart 				if (op_err) {
2959f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
2960f8829a4aSRandall Stewart 					uint32_t *ippp;
2961f8829a4aSRandall Stewart 
2962139bc87fSRandall Stewart 					SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr) +
2963f8829a4aSRandall Stewart 					    (2 * sizeof(uint32_t));
2964f8829a4aSRandall Stewart 					ph = mtod(op_err, struct sctp_paramhdr *);
2965f8829a4aSRandall Stewart 					ph->param_type =
2966f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2967139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(op_err));
2968f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
2969a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_19);
2970f8829a4aSRandall Stewart 					ippp++;
2971f8829a4aSRandall Stewart 					*ippp = asoc->cumulative_tsn;
2972f8829a4aSRandall Stewart 
2973f8829a4aSRandall Stewart 				}
2974a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_19;
2975f8829a4aSRandall Stewart 				sctp_abort_association(inp, stcb, m, iphlen, sh,
2976c54a18d2SRandall Stewart 				    op_err, 0, net->port);
2977f8829a4aSRandall Stewart 				return (2);
2978f8829a4aSRandall Stewart 			}
2979f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
2980f8829a4aSRandall Stewart 			sctp_audit_log(0xB1, 0);
2981f8829a4aSRandall Stewart #endif
2982f8829a4aSRandall Stewart 			if (SCTP_SIZE32(chk_length) == (length - *offset)) {
2983f8829a4aSRandall Stewart 				last_chunk = 1;
2984f8829a4aSRandall Stewart 			} else {
2985f8829a4aSRandall Stewart 				last_chunk = 0;
2986f8829a4aSRandall Stewart 			}
2987f8829a4aSRandall Stewart 			if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset, ch,
2988f8829a4aSRandall Stewart 			    chk_length, net, high_tsn, &abort_flag, &break_flag,
2989f8829a4aSRandall Stewart 			    last_chunk)) {
2990f8829a4aSRandall Stewart 				num_chunks++;
2991f8829a4aSRandall Stewart 			}
2992f8829a4aSRandall Stewart 			if (abort_flag)
2993f8829a4aSRandall Stewart 				return (2);
2994f8829a4aSRandall Stewart 
2995f8829a4aSRandall Stewart 			if (break_flag) {
2996f8829a4aSRandall Stewart 				/*
2997f8829a4aSRandall Stewart 				 * Set because of out of rwnd space and no
2998f8829a4aSRandall Stewart 				 * drop rep space left.
2999f8829a4aSRandall Stewart 				 */
3000f8829a4aSRandall Stewart 				stop_proc = 1;
3001f8829a4aSRandall Stewart 				break;
3002f8829a4aSRandall Stewart 			}
3003f8829a4aSRandall Stewart 		} else {
3004f8829a4aSRandall Stewart 			/* not a data chunk in the data region */
3005f8829a4aSRandall Stewart 			switch (ch->ch.chunk_type) {
3006f8829a4aSRandall Stewart 			case SCTP_INITIATION:
3007f8829a4aSRandall Stewart 			case SCTP_INITIATION_ACK:
3008f8829a4aSRandall Stewart 			case SCTP_SELECTIVE_ACK:
3009830d754dSRandall Stewart 			case SCTP_NR_SELECTIVE_ACK:	/* EY */
3010f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_REQUEST:
3011f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_ACK:
3012f8829a4aSRandall Stewart 			case SCTP_ABORT_ASSOCIATION:
3013f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN:
3014f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_ACK:
3015f8829a4aSRandall Stewart 			case SCTP_OPERATION_ERROR:
3016f8829a4aSRandall Stewart 			case SCTP_COOKIE_ECHO:
3017f8829a4aSRandall Stewart 			case SCTP_COOKIE_ACK:
3018f8829a4aSRandall Stewart 			case SCTP_ECN_ECHO:
3019f8829a4aSRandall Stewart 			case SCTP_ECN_CWR:
3020f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_COMPLETE:
3021f8829a4aSRandall Stewart 			case SCTP_AUTHENTICATION:
3022f8829a4aSRandall Stewart 			case SCTP_ASCONF_ACK:
3023f8829a4aSRandall Stewart 			case SCTP_PACKET_DROPPED:
3024f8829a4aSRandall Stewart 			case SCTP_STREAM_RESET:
3025f8829a4aSRandall Stewart 			case SCTP_FORWARD_CUM_TSN:
3026f8829a4aSRandall Stewart 			case SCTP_ASCONF:
3027f8829a4aSRandall Stewart 				/*
3028f8829a4aSRandall Stewart 				 * Now, what do we do with KNOWN chunks that
3029f8829a4aSRandall Stewart 				 * are NOT in the right place?
3030f8829a4aSRandall Stewart 				 *
3031f8829a4aSRandall Stewart 				 * For now, I do nothing but ignore them. We
3032f8829a4aSRandall Stewart 				 * may later want to add sysctl stuff to
3033f8829a4aSRandall Stewart 				 * switch out and do either an ABORT() or
3034f8829a4aSRandall Stewart 				 * possibly process them.
3035f8829a4aSRandall Stewart 				 */
3036b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_strict_data_order)) {
3037f8829a4aSRandall Stewart 					struct mbuf *op_err;
3038f8829a4aSRandall Stewart 
3039f8829a4aSRandall Stewart 					op_err = sctp_generate_invmanparam(SCTP_CAUSE_PROTOCOL_VIOLATION);
3040c54a18d2SRandall Stewart 					sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 0, net->port);
3041f8829a4aSRandall Stewart 					return (2);
3042f8829a4aSRandall Stewart 				}
3043f8829a4aSRandall Stewart 				break;
3044f8829a4aSRandall Stewart 			default:
3045f8829a4aSRandall Stewart 				/* unknown chunk type, use bit rules */
3046f8829a4aSRandall Stewart 				if (ch->ch.chunk_type & 0x40) {
3047f8829a4aSRandall Stewart 					/* Add a error report to the queue */
3048d61a0ae0SRandall Stewart 					struct mbuf *merr;
3049f8829a4aSRandall Stewart 					struct sctp_paramhdr *phd;
3050f8829a4aSRandall Stewart 
3051d61a0ae0SRandall Stewart 					merr = sctp_get_mbuf_for_msg(sizeof(*phd), 0, M_DONTWAIT, 1, MT_DATA);
3052d61a0ae0SRandall Stewart 					if (merr) {
3053d61a0ae0SRandall Stewart 						phd = mtod(merr, struct sctp_paramhdr *);
3054f8829a4aSRandall Stewart 						/*
3055f8829a4aSRandall Stewart 						 * We cheat and use param
3056f8829a4aSRandall Stewart 						 * type since we did not
3057f8829a4aSRandall Stewart 						 * bother to define a error
3058f8829a4aSRandall Stewart 						 * cause struct. They are
3059f8829a4aSRandall Stewart 						 * the same basic format
3060f8829a4aSRandall Stewart 						 * with different names.
3061f8829a4aSRandall Stewart 						 */
3062f8829a4aSRandall Stewart 						phd->param_type =
3063f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_UNRECOG_CHUNK);
3064f8829a4aSRandall Stewart 						phd->param_length =
3065f8829a4aSRandall Stewart 						    htons(chk_length + sizeof(*phd));
3066d61a0ae0SRandall Stewart 						SCTP_BUF_LEN(merr) = sizeof(*phd);
3067d61a0ae0SRandall Stewart 						SCTP_BUF_NEXT(merr) = SCTP_M_COPYM(m, *offset,
3068f8829a4aSRandall Stewart 						    SCTP_SIZE32(chk_length),
3069f8829a4aSRandall Stewart 						    M_DONTWAIT);
3070d61a0ae0SRandall Stewart 						if (SCTP_BUF_NEXT(merr)) {
3071d61a0ae0SRandall Stewart 							sctp_queue_op_err(stcb, merr);
3072f8829a4aSRandall Stewart 						} else {
3073d61a0ae0SRandall Stewart 							sctp_m_freem(merr);
3074f8829a4aSRandall Stewart 						}
3075f8829a4aSRandall Stewart 					}
3076f8829a4aSRandall Stewart 				}
3077f8829a4aSRandall Stewart 				if ((ch->ch.chunk_type & 0x80) == 0) {
3078f8829a4aSRandall Stewart 					/* discard the rest of this packet */
3079f8829a4aSRandall Stewart 					stop_proc = 1;
3080f8829a4aSRandall Stewart 				}	/* else skip this bad chunk and
3081f8829a4aSRandall Stewart 					 * continue... */
3082f8829a4aSRandall Stewart 				break;
3083f8829a4aSRandall Stewart 			};	/* switch of chunk type */
3084f8829a4aSRandall Stewart 		}
3085f8829a4aSRandall Stewart 		*offset += SCTP_SIZE32(chk_length);
3086f8829a4aSRandall Stewart 		if ((*offset >= length) || stop_proc) {
3087f8829a4aSRandall Stewart 			/* no more data left in the mbuf chain */
3088f8829a4aSRandall Stewart 			stop_proc = 1;
3089f8829a4aSRandall Stewart 			continue;
3090f8829a4aSRandall Stewart 		}
3091f8829a4aSRandall Stewart 		ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
3092f8829a4aSRandall Stewart 		    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
3093f8829a4aSRandall Stewart 		if (ch == NULL) {
3094f8829a4aSRandall Stewart 			*offset = length;
3095f8829a4aSRandall Stewart 			stop_proc = 1;
3096f8829a4aSRandall Stewart 			break;
3097f8829a4aSRandall Stewart 
3098f8829a4aSRandall Stewart 		}
3099f8829a4aSRandall Stewart 	}			/* while */
3100f8829a4aSRandall Stewart 	if (break_flag) {
3101f8829a4aSRandall Stewart 		/*
3102f8829a4aSRandall Stewart 		 * we need to report rwnd overrun drops.
3103f8829a4aSRandall Stewart 		 */
3104f8829a4aSRandall Stewart 		sctp_send_packet_dropped(stcb, net, *mm, iphlen, 0);
3105f8829a4aSRandall Stewart 	}
3106f8829a4aSRandall Stewart 	if (num_chunks) {
3107f8829a4aSRandall Stewart 		/*
3108ceaad40aSRandall Stewart 		 * Did we get data, if so update the time for auto-close and
3109f8829a4aSRandall Stewart 		 * give peer credit for being alive.
3110f8829a4aSRandall Stewart 		 */
3111f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvpktwithdata);
3112b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
3113c4739e2fSRandall Stewart 			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
3114c4739e2fSRandall Stewart 			    stcb->asoc.overall_error_count,
3115c4739e2fSRandall Stewart 			    0,
3116c4739e2fSRandall Stewart 			    SCTP_FROM_SCTP_INDATA,
3117c4739e2fSRandall Stewart 			    __LINE__);
3118c4739e2fSRandall Stewart 		}
3119f8829a4aSRandall Stewart 		stcb->asoc.overall_error_count = 0;
31206e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
3121f8829a4aSRandall Stewart 	}
3122f8829a4aSRandall Stewart 	/* now service all of the reassm queue if needed */
3123f8829a4aSRandall Stewart 	if (!(TAILQ_EMPTY(&asoc->reasmqueue)))
3124f8829a4aSRandall Stewart 		sctp_service_queues(stcb, asoc);
3125f8829a4aSRandall Stewart 
3126f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
312742551e99SRandall Stewart 		/* Assure that we ack right away */
312842551e99SRandall Stewart 		stcb->asoc.send_sack = 1;
3129f8829a4aSRandall Stewart 	}
3130f8829a4aSRandall Stewart 	/* Start a sack timer or QUEUE a SACK for sending */
3131f8829a4aSRandall Stewart 	if ((stcb->asoc.cumulative_tsn == stcb->asoc.highest_tsn_inside_map) &&
313242551e99SRandall Stewart 	    (stcb->asoc.mapping_array[0] != 0xff)) {
313342551e99SRandall Stewart 		if ((stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq) ||
313442551e99SRandall Stewart 		    (stcb->asoc.delayed_ack == 0) ||
3135b201f536SRandall Stewart 		    (stcb->asoc.numduptsns) ||
313642551e99SRandall Stewart 		    (stcb->asoc.send_sack == 1)) {
3137139bc87fSRandall Stewart 			if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
3138ad81507eSRandall Stewart 				(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
313942551e99SRandall Stewart 			}
3140830d754dSRandall Stewart 			/*
3141830d754dSRandall Stewart 			 * EY if nr_sacks used then send an nr-sack , a sack
3142830d754dSRandall Stewart 			 * otherwise
3143830d754dSRandall Stewart 			 */
3144830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack)
3145830d754dSRandall Stewart 				sctp_send_nr_sack(stcb);
3146830d754dSRandall Stewart 			else
3147f8829a4aSRandall Stewart 				sctp_send_sack(stcb);
3148f8829a4aSRandall Stewart 		} else {
314942551e99SRandall Stewart 			if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
3150f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
3151f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, NULL);
3152f8829a4aSRandall Stewart 			}
3153f8829a4aSRandall Stewart 		}
3154f8829a4aSRandall Stewart 	} else {
3155f8829a4aSRandall Stewart 		sctp_sack_check(stcb, 1, was_a_gap, &abort_flag);
3156f8829a4aSRandall Stewart 	}
3157f8829a4aSRandall Stewart 	if (abort_flag)
3158f8829a4aSRandall Stewart 		return (2);
3159f8829a4aSRandall Stewart 
3160f8829a4aSRandall Stewart 	return (0);
3161f8829a4aSRandall Stewart }
3162f8829a4aSRandall Stewart 
3163f8829a4aSRandall Stewart static void
3164458303daSRandall Stewart sctp_handle_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
3165f8829a4aSRandall Stewart     struct sctp_sack_chunk *ch, uint32_t last_tsn, uint32_t * biggest_tsn_acked,
3166139bc87fSRandall Stewart     uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
3167139bc87fSRandall Stewart     int num_seg, int *ecn_seg_sums)
3168f8829a4aSRandall Stewart {
3169f8829a4aSRandall Stewart 	/************************************************/
3170f8829a4aSRandall Stewart 	/* process fragments and update sendqueue        */
3171f8829a4aSRandall Stewart 	/************************************************/
3172f8829a4aSRandall Stewart 	struct sctp_sack *sack;
3173458303daSRandall Stewart 	struct sctp_gap_ack_block *frag, block;
3174f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
3175a1e13272SRandall Stewart 	int i, j;
3176a1e13272SRandall Stewart 	unsigned int theTSN;
3177f8829a4aSRandall Stewart 	int num_frs = 0;
3178f8829a4aSRandall Stewart 
3179f8829a4aSRandall Stewart 	uint16_t frag_strt, frag_end, primary_flag_set;
3180f8829a4aSRandall Stewart 	u_long last_frag_high;
3181f8829a4aSRandall Stewart 
3182f8829a4aSRandall Stewart 	/*
3183f8829a4aSRandall Stewart 	 * @@@ JRI : TODO: This flag is not used anywhere .. remove?
3184f8829a4aSRandall Stewart 	 */
3185f8829a4aSRandall Stewart 	if (asoc->primary_destination->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
3186f8829a4aSRandall Stewart 		primary_flag_set = 1;
3187f8829a4aSRandall Stewart 	} else {
3188f8829a4aSRandall Stewart 		primary_flag_set = 0;
3189f8829a4aSRandall Stewart 	}
3190f8829a4aSRandall Stewart 	sack = &ch->sack;
3191458303daSRandall Stewart 
3192458303daSRandall Stewart 	frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
3193458303daSRandall Stewart 	    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
3194458303daSRandall Stewart 	*offset += sizeof(block);
3195458303daSRandall Stewart 	if (frag == NULL) {
3196458303daSRandall Stewart 		return;
3197458303daSRandall Stewart 	}
3198f8829a4aSRandall Stewart 	tp1 = NULL;
3199f8829a4aSRandall Stewart 	last_frag_high = 0;
3200f8829a4aSRandall Stewart 	for (i = 0; i < num_seg; i++) {
3201f8829a4aSRandall Stewart 		frag_strt = ntohs(frag->start);
3202f8829a4aSRandall Stewart 		frag_end = ntohs(frag->end);
3203830d754dSRandall Stewart 		/* some sanity checks on the fragment offsets */
3204f8829a4aSRandall Stewart 		if (frag_strt > frag_end) {
3205f8829a4aSRandall Stewart 			/* this one is malformed, skip */
3206f8829a4aSRandall Stewart 			frag++;
3207f8829a4aSRandall Stewart 			continue;
3208f8829a4aSRandall Stewart 		}
3209f8829a4aSRandall Stewart 		if (compare_with_wrap((frag_end + last_tsn), *biggest_tsn_acked,
3210f8829a4aSRandall Stewart 		    MAX_TSN))
3211f8829a4aSRandall Stewart 			*biggest_tsn_acked = frag_end + last_tsn;
3212f8829a4aSRandall Stewart 
3213f8829a4aSRandall Stewart 		/* mark acked dgs and find out the highestTSN being acked */
3214f8829a4aSRandall Stewart 		if (tp1 == NULL) {
3215f8829a4aSRandall Stewart 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
3216f8829a4aSRandall Stewart 
3217f8829a4aSRandall Stewart 			/* save the locations of the last frags */
3218f8829a4aSRandall Stewart 			last_frag_high = frag_end + last_tsn;
3219f8829a4aSRandall Stewart 		} else {
3220f8829a4aSRandall Stewart 			/*
3221f8829a4aSRandall Stewart 			 * now lets see if we need to reset the queue due to
3222f8829a4aSRandall Stewart 			 * a out-of-order SACK fragment
3223f8829a4aSRandall Stewart 			 */
3224f8829a4aSRandall Stewart 			if (compare_with_wrap(frag_strt + last_tsn,
3225f8829a4aSRandall Stewart 			    last_frag_high, MAX_TSN)) {
3226f8829a4aSRandall Stewart 				/*
3227f8829a4aSRandall Stewart 				 * if the new frag starts after the last TSN
3228f8829a4aSRandall Stewart 				 * frag covered, we are ok and this one is
3229f8829a4aSRandall Stewart 				 * beyond the last one
3230f8829a4aSRandall Stewart 				 */
3231f8829a4aSRandall Stewart 				;
3232f8829a4aSRandall Stewart 			} else {
3233f8829a4aSRandall Stewart 				/*
3234f8829a4aSRandall Stewart 				 * ok, they have reset us, so we need to
3235f8829a4aSRandall Stewart 				 * reset the queue this will cause extra
3236f8829a4aSRandall Stewart 				 * hunting but hey, they chose the
3237f8829a4aSRandall Stewart 				 * performance hit when they failed to order
3238830d754dSRandall Stewart 				 * their gaps
3239f8829a4aSRandall Stewart 				 */
3240f8829a4aSRandall Stewart 				tp1 = TAILQ_FIRST(&asoc->sent_queue);
3241f8829a4aSRandall Stewart 			}
3242f8829a4aSRandall Stewart 			last_frag_high = frag_end + last_tsn;
3243f8829a4aSRandall Stewart 		}
3244a1e13272SRandall Stewart 		for (j = frag_strt; j <= frag_end; j++) {
3245a1e13272SRandall Stewart 			theTSN = j + last_tsn;
3246f8829a4aSRandall Stewart 			while (tp1) {
3247f8829a4aSRandall Stewart 				if (tp1->rec.data.doing_fast_retransmit)
3248f8829a4aSRandall Stewart 					num_frs++;
3249f8829a4aSRandall Stewart 
3250f8829a4aSRandall Stewart 				/*
3251f8829a4aSRandall Stewart 				 * CMT: CUCv2 algorithm. For each TSN being
3252f8829a4aSRandall Stewart 				 * processed from the sent queue, track the
3253f8829a4aSRandall Stewart 				 * next expected pseudo-cumack, or
3254f8829a4aSRandall Stewart 				 * rtx_pseudo_cumack, if required. Separate
3255f8829a4aSRandall Stewart 				 * cumack trackers for first transmissions,
3256f8829a4aSRandall Stewart 				 * and retransmissions.
3257f8829a4aSRandall Stewart 				 */
3258f8829a4aSRandall Stewart 				if ((tp1->whoTo->find_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
3259f8829a4aSRandall Stewart 				    (tp1->snd_count == 1)) {
3260f8829a4aSRandall Stewart 					tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
3261f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 0;
3262f8829a4aSRandall Stewart 				}
3263f8829a4aSRandall Stewart 				if ((tp1->whoTo->find_rtx_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
3264f8829a4aSRandall Stewart 				    (tp1->snd_count > 1)) {
3265f8829a4aSRandall Stewart 					tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
3266f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 0;
3267f8829a4aSRandall Stewart 				}
3268a1e13272SRandall Stewart 				if (tp1->rec.data.TSN_seq == theTSN) {
3269f8829a4aSRandall Stewart 					if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
3270f8829a4aSRandall Stewart 						/*
3271f8829a4aSRandall Stewart 						 * must be held until
3272f8829a4aSRandall Stewart 						 * cum-ack passes
3273f8829a4aSRandall Stewart 						 */
3274f8829a4aSRandall Stewart 						/*
3275f8829a4aSRandall Stewart 						 * ECN Nonce: Add the nonce
3276f8829a4aSRandall Stewart 						 * value to the sender's
3277f8829a4aSRandall Stewart 						 * nonce sum
3278f8829a4aSRandall Stewart 						 */
3279c105859eSRandall Stewart 						if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3280c105859eSRandall Stewart 							/*-
3281c105859eSRandall Stewart 							 * If it is less than RESEND, it is
3282c105859eSRandall Stewart 							 * now no-longer in flight.
3283c105859eSRandall Stewart 							 * Higher values may already be set
3284c105859eSRandall Stewart 							 * via previous Gap Ack Blocks...
3285c105859eSRandall Stewart 							 * i.e. ACKED or RESEND.
3286f8829a4aSRandall Stewart 							 */
3287f8829a4aSRandall Stewart 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
3288f8829a4aSRandall Stewart 							    *biggest_newly_acked_tsn, MAX_TSN)) {
3289f8829a4aSRandall Stewart 								*biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
3290f8829a4aSRandall Stewart 							}
3291f8829a4aSRandall Stewart 							/*
3292f8829a4aSRandall Stewart 							 * CMT: SFR algo
3293f8829a4aSRandall Stewart 							 * (and HTNA) - set
3294f8829a4aSRandall Stewart 							 * saw_newack to 1
3295f8829a4aSRandall Stewart 							 * for dest being
3296f8829a4aSRandall Stewart 							 * newly acked.
3297f8829a4aSRandall Stewart 							 * update
3298f8829a4aSRandall Stewart 							 * this_sack_highest_
3299f8829a4aSRandall Stewart 							 * newack if
3300f8829a4aSRandall Stewart 							 * appropriate.
3301f8829a4aSRandall Stewart 							 */
3302f8829a4aSRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0)
3303f8829a4aSRandall Stewart 								tp1->whoTo->saw_newack = 1;
3304f8829a4aSRandall Stewart 
3305f8829a4aSRandall Stewart 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
3306f8829a4aSRandall Stewart 							    tp1->whoTo->this_sack_highest_newack,
3307f8829a4aSRandall Stewart 							    MAX_TSN)) {
3308f8829a4aSRandall Stewart 								tp1->whoTo->this_sack_highest_newack =
3309f8829a4aSRandall Stewart 								    tp1->rec.data.TSN_seq;
3310f8829a4aSRandall Stewart 							}
3311f8829a4aSRandall Stewart 							/*
3312f8829a4aSRandall Stewart 							 * CMT DAC algo:
3313f8829a4aSRandall Stewart 							 * also update
3314f8829a4aSRandall Stewart 							 * this_sack_lowest_n
3315f8829a4aSRandall Stewart 							 * ewack
3316f8829a4aSRandall Stewart 							 */
3317f8829a4aSRandall Stewart 							if (*this_sack_lowest_newack == 0) {
3318b3f1ea41SRandall Stewart 								if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3319f8829a4aSRandall Stewart 									sctp_log_sack(*this_sack_lowest_newack,
3320d0cf96b4SMax Laier 									    last_tsn,
3321f8829a4aSRandall Stewart 									    tp1->rec.data.TSN_seq,
3322f8829a4aSRandall Stewart 									    0,
3323f8829a4aSRandall Stewart 									    0,
3324f8829a4aSRandall Stewart 									    SCTP_LOG_TSN_ACKED);
332580fefe0aSRandall Stewart 								}
3326f8829a4aSRandall Stewart 								*this_sack_lowest_newack = tp1->rec.data.TSN_seq;
3327f8829a4aSRandall Stewart 							}
3328f8829a4aSRandall Stewart 							/*
3329f8829a4aSRandall Stewart 							 * CMT: CUCv2
3330f8829a4aSRandall Stewart 							 * algorithm. If
3331f8829a4aSRandall Stewart 							 * (rtx-)pseudo-cumac
3332f8829a4aSRandall Stewart 							 * k for corresp
3333f8829a4aSRandall Stewart 							 * dest is being
3334f8829a4aSRandall Stewart 							 * acked, then we
3335f8829a4aSRandall Stewart 							 * have a new
3336f8829a4aSRandall Stewart 							 * (rtx-)pseudo-cumac
3337f8829a4aSRandall Stewart 							 * k. Set
3338f8829a4aSRandall Stewart 							 * new_(rtx_)pseudo_c
3339f8829a4aSRandall Stewart 							 * umack to TRUE so
3340f8829a4aSRandall Stewart 							 * that the cwnd for
3341f8829a4aSRandall Stewart 							 * this dest can be
3342f8829a4aSRandall Stewart 							 * updated. Also
3343f8829a4aSRandall Stewart 							 * trigger search
3344f8829a4aSRandall Stewart 							 * for the next
3345f8829a4aSRandall Stewart 							 * expected
3346f8829a4aSRandall Stewart 							 * (rtx-)pseudo-cumac
3347f8829a4aSRandall Stewart 							 * k. Separate
3348f8829a4aSRandall Stewart 							 * pseudo_cumack
3349f8829a4aSRandall Stewart 							 * trackers for
3350f8829a4aSRandall Stewart 							 * first
3351f8829a4aSRandall Stewart 							 * transmissions and
3352f8829a4aSRandall Stewart 							 * retransmissions.
3353f8829a4aSRandall Stewart 							 */
3354f8829a4aSRandall Stewart 							if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
3355f8829a4aSRandall Stewart 								if (tp1->rec.data.chunk_was_revoked == 0) {
3356f8829a4aSRandall Stewart 									tp1->whoTo->new_pseudo_cumack = 1;
3357f8829a4aSRandall Stewart 								}
3358f8829a4aSRandall Stewart 								tp1->whoTo->find_pseudo_cumack = 1;
3359f8829a4aSRandall Stewart 							}
3360b3f1ea41SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
3361f8829a4aSRandall Stewart 								sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
336280fefe0aSRandall Stewart 							}
3363f8829a4aSRandall Stewart 							if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
3364f8829a4aSRandall Stewart 								if (tp1->rec.data.chunk_was_revoked == 0) {
3365f8829a4aSRandall Stewart 									tp1->whoTo->new_pseudo_cumack = 1;
3366f8829a4aSRandall Stewart 								}
3367f8829a4aSRandall Stewart 								tp1->whoTo->find_rtx_pseudo_cumack = 1;
3368f8829a4aSRandall Stewart 							}
3369b3f1ea41SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3370f8829a4aSRandall Stewart 								sctp_log_sack(*biggest_newly_acked_tsn,
3371f8829a4aSRandall Stewart 								    last_tsn,
3372f8829a4aSRandall Stewart 								    tp1->rec.data.TSN_seq,
3373f8829a4aSRandall Stewart 								    frag_strt,
3374f8829a4aSRandall Stewart 								    frag_end,
3375f8829a4aSRandall Stewart 								    SCTP_LOG_TSN_ACKED);
337680fefe0aSRandall Stewart 							}
3377b3f1ea41SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3378c105859eSRandall Stewart 								sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
3379a5d547adSRandall Stewart 								    tp1->whoTo->flight_size,
3380a5d547adSRandall Stewart 								    tp1->book_size,
3381c105859eSRandall Stewart 								    (uintptr_t) tp1->whoTo,
3382a5d547adSRandall Stewart 								    tp1->rec.data.TSN_seq);
338380fefe0aSRandall Stewart 							}
3384c105859eSRandall Stewart 							sctp_flight_size_decrease(tp1);
3385c105859eSRandall Stewart 							sctp_total_flight_decrease(stcb, tp1);
3386f8829a4aSRandall Stewart 
3387f8829a4aSRandall Stewart 							tp1->whoTo->net_ack += tp1->send_size;
3388f8829a4aSRandall Stewart 							if (tp1->snd_count < 2) {
3389f8829a4aSRandall Stewart 								/*
3390a5d547adSRandall Stewart 								 * True
3391a5d547adSRandall Stewart 								 * non-retran
3392a5d547adSRandall Stewart 								 * smited
3393a5d547adSRandall Stewart 								 * chunk */
3394f8829a4aSRandall Stewart 								tp1->whoTo->net_ack2 += tp1->send_size;
3395f8829a4aSRandall Stewart 
3396f8829a4aSRandall Stewart 								/*
3397a5d547adSRandall Stewart 								 * update RTO
3398a5d547adSRandall Stewart 								 * too ? */
3399f8829a4aSRandall Stewart 								if (tp1->do_rtt) {
3400f8829a4aSRandall Stewart 									tp1->whoTo->RTO =
3401f8829a4aSRandall Stewart 									    sctp_calculate_rto(stcb,
3402f8829a4aSRandall Stewart 									    asoc,
3403f8829a4aSRandall Stewart 									    tp1->whoTo,
340418e198d3SRandall Stewart 									    &tp1->sent_rcv_time,
340518e198d3SRandall Stewart 									    sctp_align_safe_nocopy);
3406f8829a4aSRandall Stewart 									tp1->do_rtt = 0;
3407f8829a4aSRandall Stewart 								}
3408f8829a4aSRandall Stewart 							}
3409f8829a4aSRandall Stewart 						}
3410c105859eSRandall Stewart 						if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
3411c105859eSRandall Stewart 							(*ecn_seg_sums) += tp1->rec.data.ect_nonce;
3412c105859eSRandall Stewart 							(*ecn_seg_sums) &= SCTP_SACK_NONCE_SUM;
3413c105859eSRandall Stewart 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
3414f8829a4aSRandall Stewart 							    asoc->this_sack_highest_gap,
3415f8829a4aSRandall Stewart 							    MAX_TSN)) {
3416f8829a4aSRandall Stewart 								asoc->this_sack_highest_gap =
3417f8829a4aSRandall Stewart 								    tp1->rec.data.TSN_seq;
3418f8829a4aSRandall Stewart 							}
3419f8829a4aSRandall Stewart 							if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3420f8829a4aSRandall Stewart 								sctp_ucount_decr(asoc->sent_queue_retran_cnt);
3421f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
3422f8829a4aSRandall Stewart 								sctp_audit_log(0xB2,
3423f8829a4aSRandall Stewart 								    (asoc->sent_queue_retran_cnt & 0x000000ff));
3424f8829a4aSRandall Stewart #endif
3425f8829a4aSRandall Stewart 							}
3426c105859eSRandall Stewart 						}
3427c105859eSRandall Stewart 						/*
3428c105859eSRandall Stewart 						 * All chunks NOT UNSENT
3429c105859eSRandall Stewart 						 * fall through here and are
34308933fa13SRandall Stewart 						 * marked (leave PR-SCTP
34318933fa13SRandall Stewart 						 * ones that are to skip
34328933fa13SRandall Stewart 						 * alone though)
3433c105859eSRandall Stewart 						 */
34348933fa13SRandall Stewart 						if (tp1->sent != SCTP_FORWARD_TSN_SKIP)
3435f8829a4aSRandall Stewart 							tp1->sent = SCTP_DATAGRAM_MARKED;
34368933fa13SRandall Stewart 
343742551e99SRandall Stewart 						if (tp1->rec.data.chunk_was_revoked) {
343842551e99SRandall Stewart 							/* deflate the cwnd */
343942551e99SRandall Stewart 							tp1->whoTo->cwnd -= tp1->book_size;
344042551e99SRandall Stewart 							tp1->rec.data.chunk_was_revoked = 0;
344142551e99SRandall Stewart 						}
3442f8829a4aSRandall Stewart 					}
3443f8829a4aSRandall Stewart 					break;
3444a1e13272SRandall Stewart 				}	/* if (tp1->TSN_seq == theTSN) */
3445a1e13272SRandall Stewart 				if (compare_with_wrap(tp1->rec.data.TSN_seq, theTSN,
3446f8829a4aSRandall Stewart 				    MAX_TSN))
3447f8829a4aSRandall Stewart 					break;
3448f8829a4aSRandall Stewart 
3449f8829a4aSRandall Stewart 				tp1 = TAILQ_NEXT(tp1, sctp_next);
3450f8829a4aSRandall Stewart 			}	/* end while (tp1) */
3451f8829a4aSRandall Stewart 		}		/* end for (j = fragStart */
3452458303daSRandall Stewart 		frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
3453458303daSRandall Stewart 		    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
3454458303daSRandall Stewart 		*offset += sizeof(block);
3455458303daSRandall Stewart 		if (frag == NULL) {
3456458303daSRandall Stewart 			break;
3457458303daSRandall Stewart 		}
3458f8829a4aSRandall Stewart 	}
3459b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
346080fefe0aSRandall Stewart 		if (num_frs)
346180fefe0aSRandall Stewart 			sctp_log_fr(*biggest_tsn_acked,
346280fefe0aSRandall Stewart 			    *biggest_newly_acked_tsn,
346380fefe0aSRandall Stewart 			    last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
346480fefe0aSRandall Stewart 	}
3465f8829a4aSRandall Stewart }
3466f8829a4aSRandall Stewart 
3467f8829a4aSRandall Stewart static void
3468c105859eSRandall Stewart sctp_check_for_revoked(struct sctp_tcb *stcb,
3469c105859eSRandall Stewart     struct sctp_association *asoc, uint32_t cumack,
3470f8829a4aSRandall Stewart     u_long biggest_tsn_acked)
3471f8829a4aSRandall Stewart {
3472f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
3473f8829a4aSRandall Stewart 	int tot_revoked = 0;
3474f8829a4aSRandall Stewart 
3475f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
3476f8829a4aSRandall Stewart 	while (tp1) {
3477f8829a4aSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq, cumack,
3478f8829a4aSRandall Stewart 		    MAX_TSN)) {
3479f8829a4aSRandall Stewart 			/*
3480f8829a4aSRandall Stewart 			 * ok this guy is either ACK or MARKED. If it is
3481f8829a4aSRandall Stewart 			 * ACKED it has been previously acked but not this
3482f8829a4aSRandall Stewart 			 * time i.e. revoked.  If it is MARKED it was ACK'ed
3483f8829a4aSRandall Stewart 			 * again.
3484f8829a4aSRandall Stewart 			 */
3485d06c82f1SRandall Stewart 			if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
3486d06c82f1SRandall Stewart 			    MAX_TSN))
3487d06c82f1SRandall Stewart 				break;
3488d06c82f1SRandall Stewart 
3489d06c82f1SRandall Stewart 
3490f8829a4aSRandall Stewart 			if (tp1->sent == SCTP_DATAGRAM_ACKED) {
3491f8829a4aSRandall Stewart 				/* it has been revoked */
3492f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_SENT;
3493f8829a4aSRandall Stewart 				tp1->rec.data.chunk_was_revoked = 1;
3494a5d547adSRandall Stewart 				/*
349542551e99SRandall Stewart 				 * We must add this stuff back in to assure
349642551e99SRandall Stewart 				 * timers and such get started.
3497a5d547adSRandall Stewart 				 */
3498b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3499c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
3500c105859eSRandall Stewart 					    tp1->whoTo->flight_size,
3501c105859eSRandall Stewart 					    tp1->book_size,
3502c105859eSRandall Stewart 					    (uintptr_t) tp1->whoTo,
3503c105859eSRandall Stewart 					    tp1->rec.data.TSN_seq);
350480fefe0aSRandall Stewart 				}
3505c105859eSRandall Stewart 				sctp_flight_size_increase(tp1);
3506c105859eSRandall Stewart 				sctp_total_flight_increase(stcb, tp1);
350742551e99SRandall Stewart 				/*
350842551e99SRandall Stewart 				 * We inflate the cwnd to compensate for our
350942551e99SRandall Stewart 				 * artificial inflation of the flight_size.
351042551e99SRandall Stewart 				 */
351142551e99SRandall Stewart 				tp1->whoTo->cwnd += tp1->book_size;
3512f8829a4aSRandall Stewart 				tot_revoked++;
3513b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3514f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
3515f8829a4aSRandall Stewart 					    cumack,
3516f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
3517f8829a4aSRandall Stewart 					    0,
3518f8829a4aSRandall Stewart 					    0,
3519f8829a4aSRandall Stewart 					    SCTP_LOG_TSN_REVOKED);
352080fefe0aSRandall Stewart 				}
3521f8829a4aSRandall Stewart 			} else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
3522f8829a4aSRandall Stewart 				/* it has been re-acked in this SACK */
3523f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
3524f8829a4aSRandall Stewart 			}
3525f8829a4aSRandall Stewart 		}
3526f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT)
3527f8829a4aSRandall Stewart 			break;
3528f8829a4aSRandall Stewart 		tp1 = TAILQ_NEXT(tp1, sctp_next);
3529f8829a4aSRandall Stewart 	}
3530f8829a4aSRandall Stewart 	if (tot_revoked > 0) {
3531f8829a4aSRandall Stewart 		/*
3532f8829a4aSRandall Stewart 		 * Setup the ecn nonce re-sync point. We do this since once
3533f8829a4aSRandall Stewart 		 * data is revoked we begin to retransmit things, which do
3534f8829a4aSRandall Stewart 		 * NOT have the ECN bits set. This means we are now out of
3535f8829a4aSRandall Stewart 		 * sync and must wait until we get back in sync with the
3536f8829a4aSRandall Stewart 		 * peer to check ECN bits.
3537f8829a4aSRandall Stewart 		 */
3538f8829a4aSRandall Stewart 		tp1 = TAILQ_FIRST(&asoc->send_queue);
3539f8829a4aSRandall Stewart 		if (tp1 == NULL) {
3540f8829a4aSRandall Stewart 			asoc->nonce_resync_tsn = asoc->sending_seq;
3541f8829a4aSRandall Stewart 		} else {
3542f8829a4aSRandall Stewart 			asoc->nonce_resync_tsn = tp1->rec.data.TSN_seq;
3543f8829a4aSRandall Stewart 		}
3544f8829a4aSRandall Stewart 		asoc->nonce_wait_for_ecne = 0;
3545f8829a4aSRandall Stewart 		asoc->nonce_sum_check = 0;
3546f8829a4aSRandall Stewart 	}
3547f8829a4aSRandall Stewart }
3548f8829a4aSRandall Stewart 
3549830d754dSRandall Stewart 
3550f8829a4aSRandall Stewart static void
3551f8829a4aSRandall Stewart sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
3552f8829a4aSRandall Stewart     u_long biggest_tsn_acked, u_long biggest_tsn_newly_acked, u_long this_sack_lowest_newack, int accum_moved)
3553f8829a4aSRandall Stewart {
3554f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
3555f8829a4aSRandall Stewart 	int strike_flag = 0;
3556f8829a4aSRandall Stewart 	struct timeval now;
3557f8829a4aSRandall Stewart 	int tot_retrans = 0;
3558f8829a4aSRandall Stewart 	uint32_t sending_seq;
3559f8829a4aSRandall Stewart 	struct sctp_nets *net;
3560f8829a4aSRandall Stewart 	int num_dests_sacked = 0;
3561f8829a4aSRandall Stewart 
3562f8829a4aSRandall Stewart 	/*
3563f8829a4aSRandall Stewart 	 * select the sending_seq, this is either the next thing ready to be
3564f8829a4aSRandall Stewart 	 * sent but not transmitted, OR, the next seq we assign.
3565f8829a4aSRandall Stewart 	 */
3566f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
3567f8829a4aSRandall Stewart 	if (tp1 == NULL) {
3568f8829a4aSRandall Stewart 		sending_seq = asoc->sending_seq;
3569f8829a4aSRandall Stewart 	} else {
3570f8829a4aSRandall Stewart 		sending_seq = tp1->rec.data.TSN_seq;
3571f8829a4aSRandall Stewart 	}
3572f8829a4aSRandall Stewart 
3573f8829a4aSRandall Stewart 	/* CMT DAC algo: finding out if SACK is a mixed SACK */
3574b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3575f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3576f8829a4aSRandall Stewart 			if (net->saw_newack)
3577f8829a4aSRandall Stewart 				num_dests_sacked++;
3578f8829a4aSRandall Stewart 		}
3579f8829a4aSRandall Stewart 	}
3580f8829a4aSRandall Stewart 	if (stcb->asoc.peer_supports_prsctp) {
35816e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
3582f8829a4aSRandall Stewart 	}
3583f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
3584f8829a4aSRandall Stewart 	while (tp1) {
3585f8829a4aSRandall Stewart 		strike_flag = 0;
3586f8829a4aSRandall Stewart 		if (tp1->no_fr_allowed) {
3587f8829a4aSRandall Stewart 			/* this one had a timeout or something */
3588f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3589f8829a4aSRandall Stewart 			continue;
3590f8829a4aSRandall Stewart 		}
3591b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3592f8829a4aSRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND)
3593f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3594f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3595f8829a4aSRandall Stewart 				    tp1->sent,
3596f8829a4aSRandall Stewart 				    SCTP_FR_LOG_CHECK_STRIKE);
359780fefe0aSRandall Stewart 		}
3598f8829a4aSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
3599f8829a4aSRandall Stewart 		    MAX_TSN) ||
3600f8829a4aSRandall Stewart 		    tp1->sent == SCTP_DATAGRAM_UNSENT) {
3601f8829a4aSRandall Stewart 			/* done */
3602f8829a4aSRandall Stewart 			break;
3603f8829a4aSRandall Stewart 		}
3604f8829a4aSRandall Stewart 		if (stcb->asoc.peer_supports_prsctp) {
3605f8829a4aSRandall Stewart 			if ((PR_SCTP_TTL_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3606f8829a4aSRandall Stewart 				/* Is it expired? */
36075e54f665SRandall Stewart 				if (
3608fc14de76SRandall Stewart 				/*
3609fc14de76SRandall Stewart 				 * TODO sctp_constants.h needs alternative
3610fc14de76SRandall Stewart 				 * time macros when _KERNEL is undefined.
3611fc14de76SRandall Stewart 				 */
36125e54f665SRandall Stewart 				    (timevalcmp(&now, &tp1->rec.data.timetodrop, >))
36135e54f665SRandall Stewart 				    ) {
3614f8829a4aSRandall Stewart 					/* Yes so drop it */
3615f8829a4aSRandall Stewart 					if (tp1->data != NULL) {
3616ad81507eSRandall Stewart 						(void)sctp_release_pr_sctp_chunk(stcb, tp1,
3617f8829a4aSRandall Stewart 						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
36180c0982b8SRandall Stewart 						    SCTP_SO_NOT_LOCKED);
3619f8829a4aSRandall Stewart 					}
3620f8829a4aSRandall Stewart 					tp1 = TAILQ_NEXT(tp1, sctp_next);
3621f8829a4aSRandall Stewart 					continue;
3622f8829a4aSRandall Stewart 				}
3623f8829a4aSRandall Stewart 			}
3624f8829a4aSRandall Stewart 		}
3625f8829a4aSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq,
3626f8829a4aSRandall Stewart 		    asoc->this_sack_highest_gap, MAX_TSN)) {
3627f8829a4aSRandall Stewart 			/* we are beyond the tsn in the sack  */
3628f8829a4aSRandall Stewart 			break;
3629f8829a4aSRandall Stewart 		}
3630f8829a4aSRandall Stewart 		if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
3631f8829a4aSRandall Stewart 			/* either a RESEND, ACKED, or MARKED */
3632f8829a4aSRandall Stewart 			/* skip */
3633f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3634f8829a4aSRandall Stewart 			continue;
3635f8829a4aSRandall Stewart 		}
3636f8829a4aSRandall Stewart 		/*
3637f8829a4aSRandall Stewart 		 * CMT : SFR algo (covers part of DAC and HTNA as well)
3638f8829a4aSRandall Stewart 		 */
3639ad81507eSRandall Stewart 		if (tp1->whoTo && tp1->whoTo->saw_newack == 0) {
3640f8829a4aSRandall Stewart 			/*
3641f8829a4aSRandall Stewart 			 * No new acks were receieved for data sent to this
3642f8829a4aSRandall Stewart 			 * dest. Therefore, according to the SFR algo for
3643f8829a4aSRandall Stewart 			 * CMT, no data sent to this dest can be marked for
3644c105859eSRandall Stewart 			 * FR using this SACK.
3645f8829a4aSRandall Stewart 			 */
3646f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3647f8829a4aSRandall Stewart 			continue;
3648ad81507eSRandall Stewart 		} else if (tp1->whoTo && compare_with_wrap(tp1->rec.data.TSN_seq,
3649f8829a4aSRandall Stewart 		    tp1->whoTo->this_sack_highest_newack, MAX_TSN)) {
3650f8829a4aSRandall Stewart 			/*
3651f8829a4aSRandall Stewart 			 * CMT: New acks were receieved for data sent to
3652f8829a4aSRandall Stewart 			 * this dest. But no new acks were seen for data
3653f8829a4aSRandall Stewart 			 * sent after tp1. Therefore, according to the SFR
3654f8829a4aSRandall Stewart 			 * algo for CMT, tp1 cannot be marked for FR using
3655f8829a4aSRandall Stewart 			 * this SACK. This step covers part of the DAC algo
3656f8829a4aSRandall Stewart 			 * and the HTNA algo as well.
3657f8829a4aSRandall Stewart 			 */
3658f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3659f8829a4aSRandall Stewart 			continue;
3660f8829a4aSRandall Stewart 		}
3661f8829a4aSRandall Stewart 		/*
3662f8829a4aSRandall Stewart 		 * Here we check to see if we were have already done a FR
3663f8829a4aSRandall Stewart 		 * and if so we see if the biggest TSN we saw in the sack is
3664f8829a4aSRandall Stewart 		 * smaller than the recovery point. If so we don't strike
3665f8829a4aSRandall Stewart 		 * the tsn... otherwise we CAN strike the TSN.
3666f8829a4aSRandall Stewart 		 */
3667f8829a4aSRandall Stewart 		/*
366842551e99SRandall Stewart 		 * @@@ JRI: Check for CMT if (accum_moved &&
366942551e99SRandall Stewart 		 * asoc->fast_retran_loss_recovery && (sctp_cmt_on_off ==
367042551e99SRandall Stewart 		 * 0)) {
3671f8829a4aSRandall Stewart 		 */
367242551e99SRandall Stewart 		if (accum_moved && asoc->fast_retran_loss_recovery) {
3673f8829a4aSRandall Stewart 			/*
3674f8829a4aSRandall Stewart 			 * Strike the TSN if in fast-recovery and cum-ack
3675f8829a4aSRandall Stewart 			 * moved.
3676f8829a4aSRandall Stewart 			 */
3677b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3678f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3679f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3680f8829a4aSRandall Stewart 				    tp1->sent,
3681f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
368280fefe0aSRandall Stewart 			}
36835e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3684f8829a4aSRandall Stewart 				tp1->sent++;
36855e54f665SRandall Stewart 			}
3686b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3687f8829a4aSRandall Stewart 				/*
3688f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
3689f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
3690f8829a4aSRandall Stewart 				 * because it would have been set to the
3691f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
3692f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
3693f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
3694c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
3695c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3696c105859eSRandall Stewart 				 * indicates that two packets have been
3697c105859eSRandall Stewart 				 * received after this missing TSN.
36985e54f665SRandall Stewart 				 */
36995e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3700f8829a4aSRandall Stewart 				    compare_with_wrap(this_sack_lowest_newack, tp1->rec.data.TSN_seq, MAX_TSN)) {
3701b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3702f8829a4aSRandall Stewart 						sctp_log_fr(16 + num_dests_sacked,
3703f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3704f8829a4aSRandall Stewart 						    tp1->sent,
3705f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
370680fefe0aSRandall Stewart 					}
3707f8829a4aSRandall Stewart 					tp1->sent++;
3708f8829a4aSRandall Stewart 				}
3709f8829a4aSRandall Stewart 			}
3710b3f1ea41SRandall Stewart 		} else if ((tp1->rec.data.doing_fast_retransmit) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
3711f8829a4aSRandall Stewart 			/*
3712f8829a4aSRandall Stewart 			 * For those that have done a FR we must take
3713f8829a4aSRandall Stewart 			 * special consideration if we strike. I.e the
3714f8829a4aSRandall Stewart 			 * biggest_newly_acked must be higher than the
3715f8829a4aSRandall Stewart 			 * sending_seq at the time we did the FR.
3716f8829a4aSRandall Stewart 			 */
37175e54f665SRandall Stewart 			if (
3718f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3719f8829a4aSRandall Stewart 			/*
3720f8829a4aSRandall Stewart 			 * If FR's go to new networks, then we must only do
3721f8829a4aSRandall Stewart 			 * this for singly homed asoc's. However if the FR's
3722f8829a4aSRandall Stewart 			 * go to the same network (Armando's work) then its
3723f8829a4aSRandall Stewart 			 * ok to FR multiple times.
3724f8829a4aSRandall Stewart 			 */
37255e54f665SRandall Stewart 			    (asoc->numnets < 2)
3726f8829a4aSRandall Stewart #else
37275e54f665SRandall Stewart 			    (1)
3728f8829a4aSRandall Stewart #endif
37295e54f665SRandall Stewart 			    ) {
37305e54f665SRandall Stewart 
3731f8829a4aSRandall Stewart 				if ((compare_with_wrap(biggest_tsn_newly_acked,
3732f8829a4aSRandall Stewart 				    tp1->rec.data.fast_retran_tsn, MAX_TSN)) ||
3733f8829a4aSRandall Stewart 				    (biggest_tsn_newly_acked ==
3734f8829a4aSRandall Stewart 				    tp1->rec.data.fast_retran_tsn)) {
3735f8829a4aSRandall Stewart 					/*
3736f8829a4aSRandall Stewart 					 * Strike the TSN, since this ack is
3737f8829a4aSRandall Stewart 					 * beyond where things were when we
3738f8829a4aSRandall Stewart 					 * did a FR.
3739f8829a4aSRandall Stewart 					 */
3740b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3741f8829a4aSRandall Stewart 						sctp_log_fr(biggest_tsn_newly_acked,
3742f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3743f8829a4aSRandall Stewart 						    tp1->sent,
3744f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
374580fefe0aSRandall Stewart 					}
37465e54f665SRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3747f8829a4aSRandall Stewart 						tp1->sent++;
37485e54f665SRandall Stewart 					}
3749f8829a4aSRandall Stewart 					strike_flag = 1;
3750b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3751f8829a4aSRandall Stewart 						/*
3752f8829a4aSRandall Stewart 						 * CMT DAC algorithm: If
3753f8829a4aSRandall Stewart 						 * SACK flag is set to 0,
3754f8829a4aSRandall Stewart 						 * then lowest_newack test
3755f8829a4aSRandall Stewart 						 * will not pass because it
3756f8829a4aSRandall Stewart 						 * would have been set to
3757f8829a4aSRandall Stewart 						 * the cumack earlier. If
3758f8829a4aSRandall Stewart 						 * not already to be rtx'd,
3759f8829a4aSRandall Stewart 						 * If not a mixed sack and
3760f8829a4aSRandall Stewart 						 * if tp1 is not between two
3761f8829a4aSRandall Stewart 						 * sacked TSNs, then mark by
3762c105859eSRandall Stewart 						 * one more. NOTE that we
3763c105859eSRandall Stewart 						 * are marking by one
3764c105859eSRandall Stewart 						 * additional time since the
3765c105859eSRandall Stewart 						 * SACK DAC flag indicates
3766c105859eSRandall Stewart 						 * that two packets have
3767c105859eSRandall Stewart 						 * been received after this
3768c105859eSRandall Stewart 						 * missing TSN.
3769f8829a4aSRandall Stewart 						 */
37705e54f665SRandall Stewart 						if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
37715e54f665SRandall Stewart 						    (num_dests_sacked == 1) &&
37725e54f665SRandall Stewart 						    compare_with_wrap(this_sack_lowest_newack,
37735e54f665SRandall Stewart 						    tp1->rec.data.TSN_seq, MAX_TSN)) {
3774b3f1ea41SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3775f8829a4aSRandall Stewart 								sctp_log_fr(32 + num_dests_sacked,
3776f8829a4aSRandall Stewart 								    tp1->rec.data.TSN_seq,
3777f8829a4aSRandall Stewart 								    tp1->sent,
3778f8829a4aSRandall Stewart 								    SCTP_FR_LOG_STRIKE_CHUNK);
377980fefe0aSRandall Stewart 							}
37805e54f665SRandall Stewart 							if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3781f8829a4aSRandall Stewart 								tp1->sent++;
37825e54f665SRandall Stewart 							}
3783f8829a4aSRandall Stewart 						}
3784f8829a4aSRandall Stewart 					}
3785f8829a4aSRandall Stewart 				}
3786f8829a4aSRandall Stewart 			}
3787f8829a4aSRandall Stewart 			/*
378842551e99SRandall Stewart 			 * JRI: TODO: remove code for HTNA algo. CMT's SFR
378942551e99SRandall Stewart 			 * algo covers HTNA.
3790f8829a4aSRandall Stewart 			 */
3791f8829a4aSRandall Stewart 		} else if (compare_with_wrap(tp1->rec.data.TSN_seq,
3792f8829a4aSRandall Stewart 		    biggest_tsn_newly_acked, MAX_TSN)) {
3793f8829a4aSRandall Stewart 			/*
3794f8829a4aSRandall Stewart 			 * We don't strike these: This is the  HTNA
3795f8829a4aSRandall Stewart 			 * algorithm i.e. we don't strike If our TSN is
3796f8829a4aSRandall Stewart 			 * larger than the Highest TSN Newly Acked.
3797f8829a4aSRandall Stewart 			 */
3798f8829a4aSRandall Stewart 			;
3799f8829a4aSRandall Stewart 		} else {
3800f8829a4aSRandall Stewart 			/* Strike the TSN */
3801b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3802f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3803f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3804f8829a4aSRandall Stewart 				    tp1->sent,
3805f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
380680fefe0aSRandall Stewart 			}
38075e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3808f8829a4aSRandall Stewart 				tp1->sent++;
38095e54f665SRandall Stewart 			}
3810b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3811f8829a4aSRandall Stewart 				/*
3812f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
3813f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
3814f8829a4aSRandall Stewart 				 * because it would have been set to the
3815f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
3816f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
3817f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
3818c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
3819c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3820c105859eSRandall Stewart 				 * indicates that two packets have been
3821c105859eSRandall Stewart 				 * received after this missing TSN.
3822f8829a4aSRandall Stewart 				 */
38235e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3824f8829a4aSRandall Stewart 				    compare_with_wrap(this_sack_lowest_newack, tp1->rec.data.TSN_seq, MAX_TSN)) {
3825b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3826f8829a4aSRandall Stewart 						sctp_log_fr(48 + num_dests_sacked,
3827f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3828f8829a4aSRandall Stewart 						    tp1->sent,
3829f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
383080fefe0aSRandall Stewart 					}
3831f8829a4aSRandall Stewart 					tp1->sent++;
3832f8829a4aSRandall Stewart 				}
3833f8829a4aSRandall Stewart 			}
3834f8829a4aSRandall Stewart 		}
3835f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3836f8829a4aSRandall Stewart 			struct sctp_nets *alt;
3837f8829a4aSRandall Stewart 
3838544e35bdSRandall Stewart 			/* fix counts and things */
3839544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3840544e35bdSRandall Stewart 				sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND,
3841544e35bdSRandall Stewart 				    (tp1->whoTo ? (tp1->whoTo->flight_size) : 0),
3842544e35bdSRandall Stewart 				    tp1->book_size,
3843544e35bdSRandall Stewart 				    (uintptr_t) tp1->whoTo,
3844544e35bdSRandall Stewart 				    tp1->rec.data.TSN_seq);
3845544e35bdSRandall Stewart 			}
3846544e35bdSRandall Stewart 			if (tp1->whoTo) {
3847544e35bdSRandall Stewart 				tp1->whoTo->net_ack++;
3848544e35bdSRandall Stewart 				sctp_flight_size_decrease(tp1);
3849544e35bdSRandall Stewart 			}
3850544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
3851544e35bdSRandall Stewart 				sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
3852544e35bdSRandall Stewart 				    asoc->peers_rwnd, tp1->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3853544e35bdSRandall Stewart 			}
3854544e35bdSRandall Stewart 			/* add back to the rwnd */
3855544e35bdSRandall Stewart 			asoc->peers_rwnd += (tp1->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3856544e35bdSRandall Stewart 
3857544e35bdSRandall Stewart 			/* remove from the total flight */
3858544e35bdSRandall Stewart 			sctp_total_flight_decrease(stcb, tp1);
3859544e35bdSRandall Stewart 
3860abe15ad6SRandall Stewart 			if ((stcb->asoc.peer_supports_prsctp) &&
3861abe15ad6SRandall Stewart 			    (PR_SCTP_RTX_ENABLED(tp1->flags))) {
3862abe15ad6SRandall Stewart 				/*
3863abe15ad6SRandall Stewart 				 * Has it been retransmitted tv_sec times? -
3864abe15ad6SRandall Stewart 				 * we store the retran count there.
3865abe15ad6SRandall Stewart 				 */
3866abe15ad6SRandall Stewart 				if (tp1->snd_count > tp1->rec.data.timetodrop.tv_sec) {
3867abe15ad6SRandall Stewart 					/* Yes, so drop it */
3868abe15ad6SRandall Stewart 					if (tp1->data != NULL) {
3869abe15ad6SRandall Stewart 						(void)sctp_release_pr_sctp_chunk(stcb, tp1,
3870abe15ad6SRandall Stewart 						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3871abe15ad6SRandall Stewart 						    SCTP_SO_NOT_LOCKED);
3872abe15ad6SRandall Stewart 					}
3873abe15ad6SRandall Stewart 					/* Make sure to flag we had a FR */
3874abe15ad6SRandall Stewart 					tp1->whoTo->net_ack++;
3875abe15ad6SRandall Stewart 					tp1 = TAILQ_NEXT(tp1, sctp_next);
3876abe15ad6SRandall Stewart 					continue;
3877abe15ad6SRandall Stewart 				}
3878abe15ad6SRandall Stewart 			}
3879f8829a4aSRandall Stewart 			/* printf("OK, we are now ready to FR this guy\n"); */
3880b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3881f8829a4aSRandall Stewart 				sctp_log_fr(tp1->rec.data.TSN_seq, tp1->snd_count,
3882f8829a4aSRandall Stewart 				    0, SCTP_FR_MARKED);
388380fefe0aSRandall Stewart 			}
3884f8829a4aSRandall Stewart 			if (strike_flag) {
3885f8829a4aSRandall Stewart 				/* This is a subsequent FR */
3886f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_sendmultfastretrans);
3887f8829a4aSRandall Stewart 			}
38885e54f665SRandall Stewart 			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3889b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
3890f8829a4aSRandall Stewart 				/*
3891f8829a4aSRandall Stewart 				 * CMT: Using RTX_SSTHRESH policy for CMT.
3892f8829a4aSRandall Stewart 				 * If CMT is being used, then pick dest with
3893f8829a4aSRandall Stewart 				 * largest ssthresh for any retransmission.
3894f8829a4aSRandall Stewart 				 */
3895f8829a4aSRandall Stewart 				tp1->no_fr_allowed = 1;
3896f8829a4aSRandall Stewart 				alt = tp1->whoTo;
38973c503c28SRandall Stewart 				/* sa_ignore NO_NULL_CHK */
3898b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
3899b54d3a6cSRandall Stewart 					/*
3900b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If CMT PF is on,
3901b54d3a6cSRandall Stewart 					 * use the PF version of
3902b54d3a6cSRandall Stewart 					 * find_alt_net()
3903b54d3a6cSRandall Stewart 					 */
3904b54d3a6cSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 2);
3905b54d3a6cSRandall Stewart 				} else {
3906b54d3a6cSRandall Stewart 					/*
3907b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If only CMT is on,
3908b54d3a6cSRandall Stewart 					 * use the CMT version of
3909b54d3a6cSRandall Stewart 					 * find_alt_net()
3910b54d3a6cSRandall Stewart 					 */
391152be287eSRandall Stewart 					/* sa_ignore NO_NULL_CHK */
3912f8829a4aSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 1);
3913b54d3a6cSRandall Stewart 				}
3914ad81507eSRandall Stewart 				if (alt == NULL) {
3915ad81507eSRandall Stewart 					alt = tp1->whoTo;
3916ad81507eSRandall Stewart 				}
3917f8829a4aSRandall Stewart 				/*
3918f8829a4aSRandall Stewart 				 * CUCv2: If a different dest is picked for
3919f8829a4aSRandall Stewart 				 * the retransmission, then new
3920f8829a4aSRandall Stewart 				 * (rtx-)pseudo_cumack needs to be tracked
3921f8829a4aSRandall Stewart 				 * for orig dest. Let CUCv2 track new (rtx-)
3922f8829a4aSRandall Stewart 				 * pseudo-cumack always.
3923f8829a4aSRandall Stewart 				 */
3924ad81507eSRandall Stewart 				if (tp1->whoTo) {
3925f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
3926f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
3927ad81507eSRandall Stewart 				}
3928f8829a4aSRandall Stewart 			} else {/* CMT is OFF */
3929f8829a4aSRandall Stewart 
3930f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3931f8829a4aSRandall Stewart 				/* Can we find an alternate? */
3932f8829a4aSRandall Stewart 				alt = sctp_find_alternate_net(stcb, tp1->whoTo, 0);
3933f8829a4aSRandall Stewart #else
3934f8829a4aSRandall Stewart 				/*
3935f8829a4aSRandall Stewart 				 * default behavior is to NOT retransmit
3936f8829a4aSRandall Stewart 				 * FR's to an alternate. Armando Caro's
3937f8829a4aSRandall Stewart 				 * paper details why.
3938f8829a4aSRandall Stewart 				 */
3939f8829a4aSRandall Stewart 				alt = tp1->whoTo;
3940f8829a4aSRandall Stewart #endif
3941f8829a4aSRandall Stewart 			}
3942f8829a4aSRandall Stewart 
3943f8829a4aSRandall Stewart 			tp1->rec.data.doing_fast_retransmit = 1;
3944f8829a4aSRandall Stewart 			tot_retrans++;
3945f8829a4aSRandall Stewart 			/* mark the sending seq for possible subsequent FR's */
3946f8829a4aSRandall Stewart 			/*
3947f8829a4aSRandall Stewart 			 * printf("Marking TSN for FR new value %x\n",
3948f8829a4aSRandall Stewart 			 * (uint32_t)tpi->rec.data.TSN_seq);
3949f8829a4aSRandall Stewart 			 */
3950f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->send_queue)) {
3951f8829a4aSRandall Stewart 				/*
3952f8829a4aSRandall Stewart 				 * If the queue of send is empty then its
3953f8829a4aSRandall Stewart 				 * the next sequence number that will be
3954f8829a4aSRandall Stewart 				 * assigned so we subtract one from this to
3955f8829a4aSRandall Stewart 				 * get the one we last sent.
3956f8829a4aSRandall Stewart 				 */
3957f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn = sending_seq;
3958f8829a4aSRandall Stewart 			} else {
3959f8829a4aSRandall Stewart 				/*
3960f8829a4aSRandall Stewart 				 * If there are chunks on the send queue
3961f8829a4aSRandall Stewart 				 * (unsent data that has made it from the
3962f8829a4aSRandall Stewart 				 * stream queues but not out the door, we
3963f8829a4aSRandall Stewart 				 * take the first one (which will have the
3964f8829a4aSRandall Stewart 				 * lowest TSN) and subtract one to get the
3965f8829a4aSRandall Stewart 				 * one we last sent.
3966f8829a4aSRandall Stewart 				 */
3967f8829a4aSRandall Stewart 				struct sctp_tmit_chunk *ttt;
3968f8829a4aSRandall Stewart 
3969f8829a4aSRandall Stewart 				ttt = TAILQ_FIRST(&asoc->send_queue);
3970f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn =
3971f8829a4aSRandall Stewart 				    ttt->rec.data.TSN_seq;
3972f8829a4aSRandall Stewart 			}
3973f8829a4aSRandall Stewart 
3974f8829a4aSRandall Stewart 			if (tp1->do_rtt) {
3975f8829a4aSRandall Stewart 				/*
3976f8829a4aSRandall Stewart 				 * this guy had a RTO calculation pending on
3977f8829a4aSRandall Stewart 				 * it, cancel it
3978f8829a4aSRandall Stewart 				 */
3979f8829a4aSRandall Stewart 				tp1->do_rtt = 0;
3980f8829a4aSRandall Stewart 			}
3981f8829a4aSRandall Stewart 			if (alt != tp1->whoTo) {
3982f8829a4aSRandall Stewart 				/* yes, there is an alternate. */
3983f8829a4aSRandall Stewart 				sctp_free_remote_addr(tp1->whoTo);
39843c503c28SRandall Stewart 				/* sa_ignore FREED_MEMORY */
3985f8829a4aSRandall Stewart 				tp1->whoTo = alt;
3986f8829a4aSRandall Stewart 				atomic_add_int(&alt->ref_count, 1);
3987f8829a4aSRandall Stewart 			}
3988f8829a4aSRandall Stewart 		}
3989f8829a4aSRandall Stewart 		tp1 = TAILQ_NEXT(tp1, sctp_next);
3990f8829a4aSRandall Stewart 	}			/* while (tp1) */
3991f8829a4aSRandall Stewart 
3992f8829a4aSRandall Stewart 	if (tot_retrans > 0) {
3993f8829a4aSRandall Stewart 		/*
3994f8829a4aSRandall Stewart 		 * Setup the ecn nonce re-sync point. We do this since once
3995f8829a4aSRandall Stewart 		 * we go to FR something we introduce a Karn's rule scenario
3996f8829a4aSRandall Stewart 		 * and won't know the totals for the ECN bits.
3997f8829a4aSRandall Stewart 		 */
3998f8829a4aSRandall Stewart 		asoc->nonce_resync_tsn = sending_seq;
3999f8829a4aSRandall Stewart 		asoc->nonce_wait_for_ecne = 0;
4000f8829a4aSRandall Stewart 		asoc->nonce_sum_check = 0;
4001f8829a4aSRandall Stewart 	}
4002f8829a4aSRandall Stewart }
4003f8829a4aSRandall Stewart 
4004f8829a4aSRandall Stewart struct sctp_tmit_chunk *
4005f8829a4aSRandall Stewart sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
4006f8829a4aSRandall Stewart     struct sctp_association *asoc)
4007f8829a4aSRandall Stewart {
4008f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2, *a_adv = NULL;
4009f8829a4aSRandall Stewart 	struct timeval now;
4010f8829a4aSRandall Stewart 	int now_filled = 0;
4011f8829a4aSRandall Stewart 
4012f8829a4aSRandall Stewart 	if (asoc->peer_supports_prsctp == 0) {
4013f8829a4aSRandall Stewart 		return (NULL);
4014f8829a4aSRandall Stewart 	}
4015f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
4016f8829a4aSRandall Stewart 	while (tp1) {
4017f8829a4aSRandall Stewart 		if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
4018f8829a4aSRandall Stewart 		    tp1->sent != SCTP_DATAGRAM_RESEND) {
4019f8829a4aSRandall Stewart 			/* no chance to advance, out of here */
4020f8829a4aSRandall Stewart 			break;
4021f8829a4aSRandall Stewart 		}
40220c0982b8SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
40230c0982b8SRandall Stewart 			if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
40240c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
40250c0982b8SRandall Stewart 				    asoc->advanced_peer_ack_point,
40260c0982b8SRandall Stewart 				    tp1->rec.data.TSN_seq, 0, 0);
40270c0982b8SRandall Stewart 			}
40280c0982b8SRandall Stewart 		}
4029f8829a4aSRandall Stewart 		if (!PR_SCTP_ENABLED(tp1->flags)) {
4030f8829a4aSRandall Stewart 			/*
4031f8829a4aSRandall Stewart 			 * We can't fwd-tsn past any that are reliable aka
4032f8829a4aSRandall Stewart 			 * retransmitted until the asoc fails.
4033f8829a4aSRandall Stewart 			 */
4034f8829a4aSRandall Stewart 			break;
4035f8829a4aSRandall Stewart 		}
4036f8829a4aSRandall Stewart 		if (!now_filled) {
40376e55db54SRandall Stewart 			(void)SCTP_GETTIME_TIMEVAL(&now);
4038f8829a4aSRandall Stewart 			now_filled = 1;
4039f8829a4aSRandall Stewart 		}
4040f8829a4aSRandall Stewart 		tp2 = TAILQ_NEXT(tp1, sctp_next);
4041f8829a4aSRandall Stewart 		/*
4042f8829a4aSRandall Stewart 		 * now we got a chunk which is marked for another
4043f8829a4aSRandall Stewart 		 * retransmission to a PR-stream but has run out its chances
4044f8829a4aSRandall Stewart 		 * already maybe OR has been marked to skip now. Can we skip
4045f8829a4aSRandall Stewart 		 * it if its a resend?
4046f8829a4aSRandall Stewart 		 */
4047f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND &&
4048f8829a4aSRandall Stewart 		    (PR_SCTP_TTL_ENABLED(tp1->flags))) {
4049f8829a4aSRandall Stewart 			/*
4050f8829a4aSRandall Stewart 			 * Now is this one marked for resend and its time is
4051f8829a4aSRandall Stewart 			 * now up?
4052f8829a4aSRandall Stewart 			 */
4053f8829a4aSRandall Stewart 			if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
4054f8829a4aSRandall Stewart 				/* Yes so drop it */
4055f8829a4aSRandall Stewart 				if (tp1->data) {
4056ad81507eSRandall Stewart 					(void)sctp_release_pr_sctp_chunk(stcb, tp1,
4057f8829a4aSRandall Stewart 					    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
40580c0982b8SRandall Stewart 					    SCTP_SO_NOT_LOCKED);
4059f8829a4aSRandall Stewart 				}
4060f8829a4aSRandall Stewart 			} else {
4061f8829a4aSRandall Stewart 				/*
4062f8829a4aSRandall Stewart 				 * No, we are done when hit one for resend
4063f8829a4aSRandall Stewart 				 * whos time as not expired.
4064f8829a4aSRandall Stewart 				 */
4065f8829a4aSRandall Stewart 				break;
4066f8829a4aSRandall Stewart 			}
4067f8829a4aSRandall Stewart 		}
4068f8829a4aSRandall Stewart 		/*
4069f8829a4aSRandall Stewart 		 * Ok now if this chunk is marked to drop it we can clean up
4070f8829a4aSRandall Stewart 		 * the chunk, advance our peer ack point and we can check
4071f8829a4aSRandall Stewart 		 * the next chunk.
4072f8829a4aSRandall Stewart 		 */
4073f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
4074f8829a4aSRandall Stewart 			/* advance PeerAckPoint goes forward */
40750c0982b8SRandall Stewart 			if (compare_with_wrap(tp1->rec.data.TSN_seq,
40760c0982b8SRandall Stewart 			    asoc->advanced_peer_ack_point,
40770c0982b8SRandall Stewart 			    MAX_TSN)) {
40780c0982b8SRandall Stewart 
4079f8829a4aSRandall Stewart 				asoc->advanced_peer_ack_point = tp1->rec.data.TSN_seq;
4080f8829a4aSRandall Stewart 				a_adv = tp1;
40810c0982b8SRandall Stewart 			} else if (tp1->rec.data.TSN_seq == asoc->advanced_peer_ack_point) {
40820c0982b8SRandall Stewart 				/* No update but we do save the chk */
40830c0982b8SRandall Stewart 				a_adv = tp1;
40840c0982b8SRandall Stewart 			}
4085f8829a4aSRandall Stewart 		} else {
4086f8829a4aSRandall Stewart 			/*
4087f8829a4aSRandall Stewart 			 * If it is still in RESEND we can advance no
4088f8829a4aSRandall Stewart 			 * further
4089f8829a4aSRandall Stewart 			 */
4090f8829a4aSRandall Stewart 			break;
4091f8829a4aSRandall Stewart 		}
4092f8829a4aSRandall Stewart 		/*
4093f8829a4aSRandall Stewart 		 * If we hit here we just dumped tp1, move to next tsn on
4094f8829a4aSRandall Stewart 		 * sent queue.
4095f8829a4aSRandall Stewart 		 */
4096f8829a4aSRandall Stewart 		tp1 = tp2;
4097f8829a4aSRandall Stewart 	}
4098f8829a4aSRandall Stewart 	return (a_adv);
4099f8829a4aSRandall Stewart }
4100f8829a4aSRandall Stewart 
41010c0982b8SRandall Stewart static int
4102c105859eSRandall Stewart sctp_fs_audit(struct sctp_association *asoc)
4103bff64a4dSRandall Stewart {
4104bff64a4dSRandall Stewart 	struct sctp_tmit_chunk *chk;
4105bff64a4dSRandall Stewart 	int inflight = 0, resend = 0, inbetween = 0, acked = 0, above = 0;
41060c0982b8SRandall Stewart 	int entry_flight, entry_cnt, ret;
41070c0982b8SRandall Stewart 
41080c0982b8SRandall Stewart 	entry_flight = asoc->total_flight;
41090c0982b8SRandall Stewart 	entry_cnt = asoc->total_flight_count;
41100c0982b8SRandall Stewart 	ret = 0;
41110c0982b8SRandall Stewart 
41120c0982b8SRandall Stewart 	if (asoc->pr_sctp_cnt >= asoc->sent_queue_cnt)
41130c0982b8SRandall Stewart 		return (0);
4114bff64a4dSRandall Stewart 
4115bff64a4dSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
4116bff64a4dSRandall Stewart 		if (chk->sent < SCTP_DATAGRAM_RESEND) {
41170c0982b8SRandall Stewart 			printf("Chk TSN:%u size:%d inflight cnt:%d\n",
41180c0982b8SRandall Stewart 			    chk->rec.data.TSN_seq,
41190c0982b8SRandall Stewart 			    chk->send_size,
41200c0982b8SRandall Stewart 			    chk->snd_count
41210c0982b8SRandall Stewart 			    );
4122bff64a4dSRandall Stewart 			inflight++;
4123bff64a4dSRandall Stewart 		} else if (chk->sent == SCTP_DATAGRAM_RESEND) {
4124bff64a4dSRandall Stewart 			resend++;
4125bff64a4dSRandall Stewart 		} else if (chk->sent < SCTP_DATAGRAM_ACKED) {
4126bff64a4dSRandall Stewart 			inbetween++;
4127bff64a4dSRandall Stewart 		} else if (chk->sent > SCTP_DATAGRAM_ACKED) {
4128bff64a4dSRandall Stewart 			above++;
4129bff64a4dSRandall Stewart 		} else {
4130bff64a4dSRandall Stewart 			acked++;
4131bff64a4dSRandall Stewart 		}
4132bff64a4dSRandall Stewart 	}
4133f1f73e57SRandall Stewart 
4134c105859eSRandall Stewart 	if ((inflight > 0) || (inbetween > 0)) {
4135f1f73e57SRandall Stewart #ifdef INVARIANTS
4136c105859eSRandall Stewart 		panic("Flight size-express incorrect? \n");
4137f1f73e57SRandall Stewart #else
41380c0982b8SRandall Stewart 		printf("asoc->total_flight:%d cnt:%d\n",
41390c0982b8SRandall Stewart 		    entry_flight, entry_cnt);
41400c0982b8SRandall Stewart 
41410c0982b8SRandall Stewart 		SCTP_PRINTF("Flight size-express incorrect F:%d I:%d R:%d Ab:%d ACK:%d\n",
41420c0982b8SRandall Stewart 		    inflight, inbetween, resend, above, acked);
41430c0982b8SRandall Stewart 		ret = 1;
4144f1f73e57SRandall Stewart #endif
4145bff64a4dSRandall Stewart 	}
41460c0982b8SRandall Stewart 	return (ret);
4147c105859eSRandall Stewart }
4148c105859eSRandall Stewart 
4149c105859eSRandall Stewart 
4150c105859eSRandall Stewart static void
4151c105859eSRandall Stewart sctp_window_probe_recovery(struct sctp_tcb *stcb,
4152c105859eSRandall Stewart     struct sctp_association *asoc,
4153c105859eSRandall Stewart     struct sctp_nets *net,
4154c105859eSRandall Stewart     struct sctp_tmit_chunk *tp1)
4155c105859eSRandall Stewart {
4156dfb11ef8SRandall Stewart 	tp1->window_probe = 0;
41575171328bSRandall Stewart 	if ((tp1->sent >= SCTP_DATAGRAM_ACKED) || (tp1->data == NULL)) {
4158dfb11ef8SRandall Stewart 		/* TSN's skipped we do NOT move back. */
4159dfb11ef8SRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DWN_WP_FWD,
4160dfb11ef8SRandall Stewart 		    tp1->whoTo->flight_size,
4161dfb11ef8SRandall Stewart 		    tp1->book_size,
4162dfb11ef8SRandall Stewart 		    (uintptr_t) tp1->whoTo,
4163dfb11ef8SRandall Stewart 		    tp1->rec.data.TSN_seq);
4164dfb11ef8SRandall Stewart 		return;
4165dfb11ef8SRandall Stewart 	}
41665171328bSRandall Stewart 	/* First setup this by shrinking flight */
41675171328bSRandall Stewart 	sctp_flight_size_decrease(tp1);
41685171328bSRandall Stewart 	sctp_total_flight_decrease(stcb, tp1);
41695171328bSRandall Stewart 	/* Now mark for resend */
41705171328bSRandall Stewart 	tp1->sent = SCTP_DATAGRAM_RESEND;
41715171328bSRandall Stewart 	asoc->sent_queue_retran_cnt++;
4172b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4173c105859eSRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_WP,
4174c105859eSRandall Stewart 		    tp1->whoTo->flight_size,
4175c105859eSRandall Stewart 		    tp1->book_size,
4176c105859eSRandall Stewart 		    (uintptr_t) tp1->whoTo,
4177c105859eSRandall Stewart 		    tp1->rec.data.TSN_seq);
417880fefe0aSRandall Stewart 	}
4179c105859eSRandall Stewart }
4180c105859eSRandall Stewart 
4181f8829a4aSRandall Stewart void
4182f8829a4aSRandall Stewart sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack,
4183f8829a4aSRandall Stewart     uint32_t rwnd, int nonce_sum_flag, int *abort_now)
4184f8829a4aSRandall Stewart {
4185f8829a4aSRandall Stewart 	struct sctp_nets *net;
4186f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4187f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
41885e54f665SRandall Stewart 	uint32_t old_rwnd;
41895e54f665SRandall Stewart 	int win_probe_recovery = 0;
4190c105859eSRandall Stewart 	int win_probe_recovered = 0;
4191d06c82f1SRandall Stewart 	int j, done_once = 0;
4192f8829a4aSRandall Stewart 
4193b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
4194d06c82f1SRandall Stewart 		sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
4195d06c82f1SRandall Stewart 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
419680fefe0aSRandall Stewart 	}
4197f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
419818e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
419918e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
420018e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
420118e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
420218e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
420318e198d3SRandall Stewart 	}
420418e198d3SRandall Stewart #endif
4205f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
4206d06c82f1SRandall Stewart 	old_rwnd = asoc->peers_rwnd;
42075e54f665SRandall Stewart 	if (compare_with_wrap(asoc->last_acked_seq, cumack, MAX_TSN)) {
42085e54f665SRandall Stewart 		/* old ack */
42095e54f665SRandall Stewart 		return;
4210d06c82f1SRandall Stewart 	} else if (asoc->last_acked_seq == cumack) {
4211d06c82f1SRandall Stewart 		/* Window update sack */
4212d06c82f1SRandall Stewart 		asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
4213b3f1ea41SRandall Stewart 		    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4214d06c82f1SRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4215d06c82f1SRandall Stewart 			/* SWS sender side engages */
4216d06c82f1SRandall Stewart 			asoc->peers_rwnd = 0;
4217d06c82f1SRandall Stewart 		}
4218d06c82f1SRandall Stewart 		if (asoc->peers_rwnd > old_rwnd) {
4219d06c82f1SRandall Stewart 			goto again;
4220d06c82f1SRandall Stewart 		}
4221d06c82f1SRandall Stewart 		return;
42225e54f665SRandall Stewart 	}
4223f8829a4aSRandall Stewart 	/* First setup for CC stuff */
4224f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4225f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
4226f8829a4aSRandall Stewart 		net->net_ack = 0;
4227f8829a4aSRandall Stewart 		net->net_ack2 = 0;
4228132dea7dSRandall Stewart 
4229132dea7dSRandall Stewart 		/*
4230132dea7dSRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
4231132dea7dSRandall Stewart 		 * SACK processing
4232132dea7dSRandall Stewart 		 */
4233132dea7dSRandall Stewart 		net->new_pseudo_cumack = 0;
4234132dea7dSRandall Stewart 		net->will_exit_fast_recovery = 0;
4235f8829a4aSRandall Stewart 	}
4236b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4237139bc87fSRandall Stewart 		uint32_t send_s;
4238139bc87fSRandall Stewart 
4239c105859eSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4240c105859eSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
4241c105859eSRandall Stewart 			    sctpchunk_listhead);
4242c105859eSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
4243139bc87fSRandall Stewart 		} else {
4244c105859eSRandall Stewart 			send_s = asoc->sending_seq;
4245139bc87fSRandall Stewart 		}
4246139bc87fSRandall Stewart 		if ((cumack == send_s) ||
4247139bc87fSRandall Stewart 		    compare_with_wrap(cumack, send_s, MAX_TSN)) {
4248c105859eSRandall Stewart #ifndef INVARIANTS
4249139bc87fSRandall Stewart 			struct mbuf *oper;
4250139bc87fSRandall Stewart 
4251c105859eSRandall Stewart #endif
4252c105859eSRandall Stewart #ifdef INVARIANTS
4253c105859eSRandall Stewart 			panic("Impossible sack 1");
4254c105859eSRandall Stewart #else
4255139bc87fSRandall Stewart 			*abort_now = 1;
4256139bc87fSRandall Stewart 			/* XXX */
4257139bc87fSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4258139bc87fSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
4259139bc87fSRandall Stewart 			if (oper) {
4260139bc87fSRandall Stewart 				struct sctp_paramhdr *ph;
4261139bc87fSRandall Stewart 				uint32_t *ippp;
4262139bc87fSRandall Stewart 
4263139bc87fSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4264139bc87fSRandall Stewart 				    sizeof(uint32_t);
4265139bc87fSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
4266139bc87fSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4267139bc87fSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
4268139bc87fSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
4269139bc87fSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
4270139bc87fSRandall Stewart 			}
4271139bc87fSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4272ceaad40aSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
4273139bc87fSRandall Stewart 			return;
4274139bc87fSRandall Stewart #endif
4275139bc87fSRandall Stewart 		}
4276139bc87fSRandall Stewart 	}
4277f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = cumack;
4278b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4279c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4280c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
4281c4739e2fSRandall Stewart 		    0,
4282c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
4283c4739e2fSRandall Stewart 		    __LINE__);
4284c4739e2fSRandall Stewart 	}
4285f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
42865e54f665SRandall Stewart 	if (compare_with_wrap(cumack, asoc->last_acked_seq, MAX_TSN)) {
4287f8829a4aSRandall Stewart 		/* process the new consecutive TSN first */
4288f8829a4aSRandall Stewart 		tp1 = TAILQ_FIRST(&asoc->sent_queue);
4289f8829a4aSRandall Stewart 		while (tp1) {
4290f8829a4aSRandall Stewart 			tp2 = TAILQ_NEXT(tp1, sctp_next);
4291f8829a4aSRandall Stewart 			if (compare_with_wrap(cumack, tp1->rec.data.TSN_seq,
4292f8829a4aSRandall Stewart 			    MAX_TSN) ||
4293f8829a4aSRandall Stewart 			    cumack == tp1->rec.data.TSN_seq) {
429418e198d3SRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
429518e198d3SRandall Stewart 					printf("Warning, an unsent is now acked?\n");
429618e198d3SRandall Stewart 				}
4297f8829a4aSRandall Stewart 				/*
429818e198d3SRandall Stewart 				 * ECN Nonce: Add the nonce to the sender's
429918e198d3SRandall Stewart 				 * nonce sum
4300f8829a4aSRandall Stewart 				 */
4301f8829a4aSRandall Stewart 				asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
4302f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4303f8829a4aSRandall Stewart 					/*
430418e198d3SRandall Stewart 					 * If it is less than ACKED, it is
430518e198d3SRandall Stewart 					 * now no-longer in flight. Higher
430618e198d3SRandall Stewart 					 * values may occur during marking
4307f8829a4aSRandall Stewart 					 */
4308c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4309b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4310c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4311a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
4312a5d547adSRandall Stewart 							    tp1->book_size,
4313c105859eSRandall Stewart 							    (uintptr_t) tp1->whoTo,
4314a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
431580fefe0aSRandall Stewart 						}
4316c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
431704ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
4318c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
4319f8829a4aSRandall Stewart 					}
4320f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
4321f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
4322f8829a4aSRandall Stewart 						/*
432318e198d3SRandall Stewart 						 * True non-retransmited
4324f8829a4aSRandall Stewart 						 * chunk
4325f8829a4aSRandall Stewart 						 */
4326f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
4327f8829a4aSRandall Stewart 						    tp1->send_size;
4328f8829a4aSRandall Stewart 
4329f8829a4aSRandall Stewart 						/* update RTO too? */
433062c1ff9cSRandall Stewart 						if (tp1->do_rtt) {
4331f8829a4aSRandall Stewart 							tp1->whoTo->RTO =
433204ee05e8SRandall Stewart 							/*
433304ee05e8SRandall Stewart 							 * sa_ignore
433404ee05e8SRandall Stewart 							 * NO_NULL_CHK
433504ee05e8SRandall Stewart 							 */
4336f8829a4aSRandall Stewart 							    sctp_calculate_rto(stcb,
4337f8829a4aSRandall Stewart 							    asoc, tp1->whoTo,
433818e198d3SRandall Stewart 							    &tp1->sent_rcv_time,
433918e198d3SRandall Stewart 							    sctp_align_safe_nocopy);
4340f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
4341f8829a4aSRandall Stewart 						}
4342f8829a4aSRandall Stewart 					}
4343132dea7dSRandall Stewart 					/*
434418e198d3SRandall Stewart 					 * CMT: CUCv2 algorithm. From the
434518e198d3SRandall Stewart 					 * cumack'd TSNs, for each TSN being
434618e198d3SRandall Stewart 					 * acked for the first time, set the
434718e198d3SRandall Stewart 					 * following variables for the
434818e198d3SRandall Stewart 					 * corresp destination.
434918e198d3SRandall Stewart 					 * new_pseudo_cumack will trigger a
435018e198d3SRandall Stewart 					 * cwnd update.
435118e198d3SRandall Stewart 					 * find_(rtx_)pseudo_cumack will
435218e198d3SRandall Stewart 					 * trigger search for the next
435318e198d3SRandall Stewart 					 * expected (rtx-)pseudo-cumack.
4354132dea7dSRandall Stewart 					 */
4355132dea7dSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
4356132dea7dSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
4357132dea7dSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
4358132dea7dSRandall Stewart 
4359b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
436004ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
4361f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
436280fefe0aSRandall Stewart 					}
4363f8829a4aSRandall Stewart 				}
4364f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4365f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4366f8829a4aSRandall Stewart 				}
436742551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
436842551e99SRandall Stewart 					/* deflate the cwnd */
436942551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
437042551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
437142551e99SRandall Stewart 				}
4372f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
4373f8829a4aSRandall Stewart 				TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4374f8829a4aSRandall Stewart 				if (tp1->data) {
437504ee05e8SRandall Stewart 					/* sa_ignore NO_NULL_CHK */
4376f8829a4aSRandall Stewart 					sctp_free_bufspace(stcb, asoc, tp1, 1);
4377f8829a4aSRandall Stewart 					sctp_m_freem(tp1->data);
4378f8829a4aSRandall Stewart 				}
4379b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4380f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
4381f8829a4aSRandall Stewart 					    cumack,
4382f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
4383f8829a4aSRandall Stewart 					    0,
4384f8829a4aSRandall Stewart 					    0,
4385f8829a4aSRandall Stewart 					    SCTP_LOG_FREE_SENT);
438680fefe0aSRandall Stewart 				}
4387f8829a4aSRandall Stewart 				tp1->data = NULL;
4388f8829a4aSRandall Stewart 				asoc->sent_queue_cnt--;
4389f8829a4aSRandall Stewart 				sctp_free_a_chunk(stcb, tp1);
4390f8829a4aSRandall Stewart 				tp1 = tp2;
439118e198d3SRandall Stewart 			} else {
439218e198d3SRandall Stewart 				break;
4393f8829a4aSRandall Stewart 			}
43945e54f665SRandall Stewart 		}
439518e198d3SRandall Stewart 
439618e198d3SRandall Stewart 	}
439704ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
4398f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
4399ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4400ceaad40aSRandall Stewart 		struct socket *so;
4401ceaad40aSRandall Stewart 
4402ceaad40aSRandall Stewart #endif
4403ceaad40aSRandall Stewart 
4404f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4405b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
440604ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
4407f8829a4aSRandall Stewart 			sctp_wakeup_log(stcb, cumack, 1, SCTP_WAKESND_FROM_SACK);
440880fefe0aSRandall Stewart 		}
4409ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4410ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
4411ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
4412ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
4413ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
4414ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
4415ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4416ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4417ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
4418ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
4419ceaad40aSRandall Stewart 			return;
4420ceaad40aSRandall Stewart 		}
4421ceaad40aSRandall Stewart #endif
4422f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4423ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4424ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4425ceaad40aSRandall Stewart #endif
4426f8829a4aSRandall Stewart 	} else {
4427b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4428f8829a4aSRandall Stewart 			sctp_wakeup_log(stcb, cumack, 1, SCTP_NOWAKE_FROM_SACK);
442980fefe0aSRandall Stewart 		}
4430f8829a4aSRandall Stewart 	}
4431f8829a4aSRandall Stewart 
4432b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
4433f8829a4aSRandall Stewart 	if (asoc->last_acked_seq != cumack)
4434b54d3a6cSRandall Stewart 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
44355e54f665SRandall Stewart 
4436f8829a4aSRandall Stewart 	asoc->last_acked_seq = cumack;
44375e54f665SRandall Stewart 
4438f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
4439f8829a4aSRandall Stewart 		/* nothing left in-flight */
4440f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4441f8829a4aSRandall Stewart 			net->flight_size = 0;
4442f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4443f8829a4aSRandall Stewart 		}
4444f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4445f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4446f8829a4aSRandall Stewart 	}
4447f8829a4aSRandall Stewart 	/* ECN Nonce updates */
4448f8829a4aSRandall Stewart 	if (asoc->ecn_nonce_allowed) {
4449f8829a4aSRandall Stewart 		if (asoc->nonce_sum_check) {
4450f8829a4aSRandall Stewart 			if (nonce_sum_flag != ((asoc->nonce_sum_expect_base) & SCTP_SACK_NONCE_SUM)) {
4451f8829a4aSRandall Stewart 				if (asoc->nonce_wait_for_ecne == 0) {
4452f8829a4aSRandall Stewart 					struct sctp_tmit_chunk *lchk;
4453f8829a4aSRandall Stewart 
4454f8829a4aSRandall Stewart 					lchk = TAILQ_FIRST(&asoc->send_queue);
4455f8829a4aSRandall Stewart 					asoc->nonce_wait_for_ecne = 1;
4456f8829a4aSRandall Stewart 					if (lchk) {
4457f8829a4aSRandall Stewart 						asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
4458f8829a4aSRandall Stewart 					} else {
4459f8829a4aSRandall Stewart 						asoc->nonce_wait_tsn = asoc->sending_seq;
4460f8829a4aSRandall Stewart 					}
4461f8829a4aSRandall Stewart 				} else {
4462f8829a4aSRandall Stewart 					if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
4463f8829a4aSRandall Stewart 					    (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
4464f8829a4aSRandall Stewart 						/*
4465f8829a4aSRandall Stewart 						 * Misbehaving peer. We need
4466f8829a4aSRandall Stewart 						 * to react to this guy
4467f8829a4aSRandall Stewart 						 */
4468f8829a4aSRandall Stewart 						asoc->ecn_allowed = 0;
4469f8829a4aSRandall Stewart 						asoc->ecn_nonce_allowed = 0;
4470f8829a4aSRandall Stewart 					}
4471f8829a4aSRandall Stewart 				}
4472f8829a4aSRandall Stewart 			}
4473f8829a4aSRandall Stewart 		} else {
4474f8829a4aSRandall Stewart 			/* See if Resynchronization Possible */
4475f8829a4aSRandall Stewart 			if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
4476f8829a4aSRandall Stewart 				asoc->nonce_sum_check = 1;
4477f8829a4aSRandall Stewart 				/*
4478f8829a4aSRandall Stewart 				 * now we must calculate what the base is.
4479f8829a4aSRandall Stewart 				 * We do this based on two things, we know
4480f8829a4aSRandall Stewart 				 * the total's for all the segments
4481f8829a4aSRandall Stewart 				 * gap-acked in the SACK (none), We also
4482f8829a4aSRandall Stewart 				 * know the SACK's nonce sum, its in
4483f8829a4aSRandall Stewart 				 * nonce_sum_flag. So we can build a truth
4484f8829a4aSRandall Stewart 				 * table to back-calculate the new value of
4485f8829a4aSRandall Stewart 				 * asoc->nonce_sum_expect_base:
4486f8829a4aSRandall Stewart 				 *
4487f8829a4aSRandall Stewart 				 * SACK-flag-Value         Seg-Sums Base 0 0 0
4488f8829a4aSRandall Stewart 				 * 1                    0 1 0 1 1 1
4489f8829a4aSRandall Stewart 				 * 1 0
4490f8829a4aSRandall Stewart 				 */
4491f8829a4aSRandall Stewart 				asoc->nonce_sum_expect_base = (0 ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
4492f8829a4aSRandall Stewart 			}
4493f8829a4aSRandall Stewart 		}
4494f8829a4aSRandall Stewart 	}
4495f8829a4aSRandall Stewart 	/* RWND update */
4496f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
4497b3f1ea41SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4498f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4499f8829a4aSRandall Stewart 		/* SWS sender side engages */
4500f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
4501f8829a4aSRandall Stewart 	}
45025e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
45035e54f665SRandall Stewart 		win_probe_recovery = 1;
45045e54f665SRandall Stewart 	}
4505f8829a4aSRandall Stewart 	/* Now assure a timer where data is queued at */
4506a5d547adSRandall Stewart again:
4507a5d547adSRandall Stewart 	j = 0;
4508f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
45095171328bSRandall Stewart 		int to_ticks;
45105171328bSRandall Stewart 
45115e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
4512c105859eSRandall Stewart 			win_probe_recovered = 1;
45135e54f665SRandall Stewart 			/*
45145e54f665SRandall Stewart 			 * Find first chunk that was used with window probe
45155e54f665SRandall Stewart 			 * and clear the sent
45165e54f665SRandall Stewart 			 */
45173c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
45185e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
45195e54f665SRandall Stewart 				if (tp1->window_probe) {
4520c105859eSRandall Stewart 					sctp_window_probe_recovery(stcb, asoc, net, tp1);
45215e54f665SRandall Stewart 					break;
45225e54f665SRandall Stewart 				}
45235e54f665SRandall Stewart 			}
45245e54f665SRandall Stewart 		}
4525f8829a4aSRandall Stewart 		if (net->RTO == 0) {
4526f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
4527f8829a4aSRandall Stewart 		} else {
4528f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
4529f8829a4aSRandall Stewart 		}
45305171328bSRandall Stewart 		if (net->flight_size) {
4531a5d547adSRandall Stewart 			j++;
4532ad81507eSRandall Stewart 			(void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
4533f8829a4aSRandall Stewart 			    sctp_timeout_handler, &net->rxt_timer);
45345171328bSRandall Stewart 			if (net->window_probe) {
45355171328bSRandall Stewart 				net->window_probe = 0;
45365171328bSRandall Stewart 			}
4537f8829a4aSRandall Stewart 		} else {
45385171328bSRandall Stewart 			if (net->window_probe) {
45395171328bSRandall Stewart 				/*
45405171328bSRandall Stewart 				 * In window probes we must assure a timer
45415171328bSRandall Stewart 				 * is still running there
45425171328bSRandall Stewart 				 */
45435171328bSRandall Stewart 				net->window_probe = 0;
45445171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
45455171328bSRandall Stewart 					SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
45465171328bSRandall Stewart 					    sctp_timeout_handler, &net->rxt_timer);
45475171328bSRandall Stewart 				}
45485171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4549f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4550a5d547adSRandall Stewart 				    stcb, net,
4551a5d547adSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
4552f8829a4aSRandall Stewart 			}
4553b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
4554139bc87fSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4555f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
4556a5d547adSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4557a5d547adSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
4558f8829a4aSRandall Stewart 				}
4559f8829a4aSRandall Stewart 			}
4560f8829a4aSRandall Stewart 		}
4561f8829a4aSRandall Stewart 	}
4562bff64a4dSRandall Stewart 	if ((j == 0) &&
4563bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
4564bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
4565c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
4566bff64a4dSRandall Stewart 	    (done_once == 0)) {
45670c0982b8SRandall Stewart 		/*
45680c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
45690c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
45700c0982b8SRandall Stewart 		 */
45710c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
4572a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
45730c0982b8SRandall Stewart 				if (net->flight_size) {
4574a5d547adSRandall Stewart 					net->flight_size = 0;
4575a5d547adSRandall Stewart 				}
45760c0982b8SRandall Stewart 			}
4577a5d547adSRandall Stewart 			asoc->total_flight = 0;
4578a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
4579a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4580a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4581a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4582c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
4583c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
4584a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4585a5d547adSRandall Stewart 					asoc->sent_queue_retran_cnt++;
4586a5d547adSRandall Stewart 				}
4587a5d547adSRandall Stewart 			}
45880c0982b8SRandall Stewart 		}
4589bff64a4dSRandall Stewart 		done_once = 1;
4590a5d547adSRandall Stewart 		goto again;
4591a5d547adSRandall Stewart 	}
4592f8829a4aSRandall Stewart 	/**********************************/
4593f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
4594f8829a4aSRandall Stewart 	/**********************************/
4595f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4596f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
4597f8829a4aSRandall Stewart 		/* clean up */
4598f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
4599f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4600f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4601f8829a4aSRandall Stewart 		    (asoc->locked_on_sending)
4602f8829a4aSRandall Stewart 		    ) {
4603f8829a4aSRandall Stewart 			struct sctp_stream_queue_pending *sp;
4604f8829a4aSRandall Stewart 
4605f8829a4aSRandall Stewart 			/*
4606f8829a4aSRandall Stewart 			 * I may be in a state where we got all across.. but
4607f8829a4aSRandall Stewart 			 * cannot write more due to a shutdown... we abort
4608f8829a4aSRandall Stewart 			 * since the user did not indicate EOR in this case.
4609f8829a4aSRandall Stewart 			 * The sp will be cleaned during free of the asoc.
4610f8829a4aSRandall Stewart 			 */
4611f8829a4aSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4612f8829a4aSRandall Stewart 			    sctp_streamhead);
46132afb3e84SRandall Stewart 			if ((sp) && (sp->length == 0)) {
46142afb3e84SRandall Stewart 				/* Let cleanup code purge it */
46152afb3e84SRandall Stewart 				if (sp->msg_is_complete) {
46162afb3e84SRandall Stewart 					asoc->stream_queue_cnt--;
46172afb3e84SRandall Stewart 				} else {
4618f8829a4aSRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
4619f8829a4aSRandall Stewart 					asoc->locked_on_sending = NULL;
4620f8829a4aSRandall Stewart 					asoc->stream_queue_cnt--;
4621f8829a4aSRandall Stewart 				}
4622f8829a4aSRandall Stewart 			}
46232afb3e84SRandall Stewart 		}
4624f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4625f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4626f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4627f8829a4aSRandall Stewart 				/* Need to abort here */
4628f8829a4aSRandall Stewart 				struct mbuf *oper;
4629f8829a4aSRandall Stewart 
4630f8829a4aSRandall Stewart 		abort_out_now:
4631f8829a4aSRandall Stewart 				*abort_now = 1;
4632f8829a4aSRandall Stewart 				/* XXX */
4633f8829a4aSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4634f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
4635f8829a4aSRandall Stewart 				if (oper) {
4636f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
4637f8829a4aSRandall Stewart 					uint32_t *ippp;
4638f8829a4aSRandall Stewart 
4639139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4640f8829a4aSRandall Stewart 					    sizeof(uint32_t);
4641f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
4642f8829a4aSRandall Stewart 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4643139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
4644f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
4645a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_24);
4646f8829a4aSRandall Stewart 				}
4647a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
4648ceaad40aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
4649f8829a4aSRandall Stewart 			} else {
4650f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4651f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4652f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4653f42a358aSRandall Stewart 				}
4654c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4655b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4656f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
4657f8829a4aSRandall Stewart 				sctp_send_shutdown(stcb,
4658f8829a4aSRandall Stewart 				    stcb->asoc.primary_destination);
4659f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4660f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
4661f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4662f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
4663f8829a4aSRandall Stewart 			}
4664f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4665f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4666f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4667f8829a4aSRandall Stewart 				goto abort_out_now;
4668f8829a4aSRandall Stewart 			}
4669f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4670c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4671b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4672f8829a4aSRandall Stewart 			sctp_send_shutdown_ack(stcb,
4673f8829a4aSRandall Stewart 			    stcb->asoc.primary_destination);
4674f8829a4aSRandall Stewart 
4675f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4676f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb, asoc->primary_destination);
4677f8829a4aSRandall Stewart 		}
4678f8829a4aSRandall Stewart 	}
4679dfb11ef8SRandall Stewart 	/*********************************************/
4680dfb11ef8SRandall Stewart 	/* Here we perform PR-SCTP procedures        */
4681dfb11ef8SRandall Stewart 	/* (section 4.2)                             */
4682dfb11ef8SRandall Stewart 	/*********************************************/
4683dfb11ef8SRandall Stewart 	/* C1. update advancedPeerAckPoint */
4684dfb11ef8SRandall Stewart 	if (compare_with_wrap(cumack, asoc->advanced_peer_ack_point, MAX_TSN)) {
4685dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cumack;
4686dfb11ef8SRandall Stewart 	}
4687830d754dSRandall Stewart 	/* PR-Sctp issues need to be addressed too */
4688830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
4689830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
4690830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
4691830d754dSRandall Stewart 
4692830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
4693830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
4694830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
4695830d754dSRandall Stewart 		if (compare_with_wrap(asoc->advanced_peer_ack_point, cumack,
4696830d754dSRandall Stewart 		    MAX_TSN)) {
4697830d754dSRandall Stewart 			/*
4698830d754dSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing for notes
4699830d754dSRandall Stewart 			 * on issues that will occur when the ECN NONCE
4700830d754dSRandall Stewart 			 * stuff is put into SCTP for cross checking.
4701830d754dSRandall Stewart 			 */
4702830d754dSRandall Stewart 			if (compare_with_wrap(asoc->advanced_peer_ack_point, old_adv_peer_ack_point,
4703830d754dSRandall Stewart 			    MAX_TSN)) {
4704830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
4705830d754dSRandall Stewart 				/*
4706830d754dSRandall Stewart 				 * ECN Nonce: Disable Nonce Sum check when
4707830d754dSRandall Stewart 				 * FWD TSN is sent and store resync tsn
4708830d754dSRandall Stewart 				 */
4709830d754dSRandall Stewart 				asoc->nonce_sum_check = 0;
4710830d754dSRandall Stewart 				asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
47110c0982b8SRandall Stewart 			} else if (lchk) {
47120c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
47130c0982b8SRandall Stewart 				lchk->rec.data.fwd_tsn_cnt++;
47140c0982b8SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt > 3) {
47150c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
47160c0982b8SRandall Stewart 					lchk->rec.data.fwd_tsn_cnt = 0;
47170c0982b8SRandall Stewart 				}
4718830d754dSRandall Stewart 			}
4719830d754dSRandall Stewart 		}
4720830d754dSRandall Stewart 		if (lchk) {
4721830d754dSRandall Stewart 			/* Assure a timer is up */
4722830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4723830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
4724830d754dSRandall Stewart 		}
4725830d754dSRandall Stewart 	}
4726b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
4727f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4728f8829a4aSRandall Stewart 		    rwnd,
4729f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
4730f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
4731f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
473280fefe0aSRandall Stewart 	}
4733f8829a4aSRandall Stewart }
4734f8829a4aSRandall Stewart 
4735f8829a4aSRandall Stewart void
4736458303daSRandall Stewart sctp_handle_sack(struct mbuf *m, int offset,
4737458303daSRandall Stewart     struct sctp_sack_chunk *ch, struct sctp_tcb *stcb,
4738d06c82f1SRandall Stewart     struct sctp_nets *net_from, int *abort_now, int sack_len, uint32_t rwnd)
4739f8829a4aSRandall Stewart {
4740f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4741f8829a4aSRandall Stewart 	struct sctp_sack *sack;
4742f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
4743f8829a4aSRandall Stewart 	uint32_t cum_ack, last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked,
4744f8829a4aSRandall Stewart 	         this_sack_lowest_newack;
4745bff64a4dSRandall Stewart 	uint32_t sav_cum_ack;
4746f8829a4aSRandall Stewart 	uint16_t num_seg, num_dup;
4747f8829a4aSRandall Stewart 	uint16_t wake_him = 0;
4748f8829a4aSRandall Stewart 	unsigned int sack_length;
4749c105859eSRandall Stewart 	uint32_t send_s = 0;
4750f8829a4aSRandall Stewart 	long j;
4751f8829a4aSRandall Stewart 	int accum_moved = 0;
4752f8829a4aSRandall Stewart 	int will_exit_fast_recovery = 0;
47535e54f665SRandall Stewart 	uint32_t a_rwnd, old_rwnd;
47545e54f665SRandall Stewart 	int win_probe_recovery = 0;
4755c105859eSRandall Stewart 	int win_probe_recovered = 0;
4756f8829a4aSRandall Stewart 	struct sctp_nets *net = NULL;
4757f8829a4aSRandall Stewart 	int nonce_sum_flag, ecn_seg_sums = 0;
4758bff64a4dSRandall Stewart 	int done_once;
4759f8829a4aSRandall Stewart 	uint8_t reneged_all = 0;
4760f8829a4aSRandall Stewart 	uint8_t cmt_dac_flag;
4761f8829a4aSRandall Stewart 
4762f8829a4aSRandall Stewart 	/*
4763f8829a4aSRandall Stewart 	 * we take any chance we can to service our queues since we cannot
4764f8829a4aSRandall Stewart 	 * get awoken when the socket is read from :<
4765f8829a4aSRandall Stewart 	 */
4766f8829a4aSRandall Stewart 	/*
4767f8829a4aSRandall Stewart 	 * Now perform the actual SACK handling: 1) Verify that it is not an
4768f8829a4aSRandall Stewart 	 * old sack, if so discard. 2) If there is nothing left in the send
4769f8829a4aSRandall Stewart 	 * queue (cum-ack is equal to last acked) then you have a duplicate
4770f8829a4aSRandall Stewart 	 * too, update any rwnd change and verify no timers are running.
4771f8829a4aSRandall Stewart 	 * then return. 3) Process any new consequtive data i.e. cum-ack
4772f8829a4aSRandall Stewart 	 * moved process these first and note that it moved. 4) Process any
4773f8829a4aSRandall Stewart 	 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
4774f8829a4aSRandall Stewart 	 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
4775f8829a4aSRandall Stewart 	 * sync up flightsizes and things, stop all timers and also check
4776f8829a4aSRandall Stewart 	 * for shutdown_pending state. If so then go ahead and send off the
4777f8829a4aSRandall Stewart 	 * shutdown. If in shutdown recv, send off the shutdown-ack and
4778f8829a4aSRandall Stewart 	 * start that timer, Ret. 9) Strike any non-acked things and do FR
4779f8829a4aSRandall Stewart 	 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
4780f8829a4aSRandall Stewart 	 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
4781f8829a4aSRandall Stewart 	 * if in shutdown_recv state.
4782f8829a4aSRandall Stewart 	 */
4783f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
4784f8829a4aSRandall Stewart 	sack = &ch->sack;
4785f8829a4aSRandall Stewart 	/* CMT DAC algo */
4786f8829a4aSRandall Stewart 	this_sack_lowest_newack = 0;
4787f8829a4aSRandall Stewart 	j = 0;
4788d06c82f1SRandall Stewart 	sack_length = (unsigned int)sack_len;
4789f8829a4aSRandall Stewart 	/* ECN Nonce */
4790f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_slowpath_sack);
4791f8829a4aSRandall Stewart 	nonce_sum_flag = ch->ch.chunk_flags & SCTP_SACK_NONCE_SUM;
4792f8829a4aSRandall Stewart 	cum_ack = last_tsn = ntohl(sack->cum_tsn_ack);
479318e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
479418e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
479518e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
479618e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
479718e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
479818e198d3SRandall Stewart 	}
479918e198d3SRandall Stewart #endif
4800f8829a4aSRandall Stewart 	num_seg = ntohs(sack->num_gap_ack_blks);
4801d06c82f1SRandall Stewart 	a_rwnd = rwnd;
4802f8829a4aSRandall Stewart 
4803f8829a4aSRandall Stewart 	/* CMT DAC algo */
4804f8829a4aSRandall Stewart 	cmt_dac_flag = ch->ch.chunk_flags & SCTP_SACK_CMT_DAC;
4805f8829a4aSRandall Stewart 	num_dup = ntohs(sack->num_dup_tsns);
4806f8829a4aSRandall Stewart 
48075e54f665SRandall Stewart 	old_rwnd = stcb->asoc.peers_rwnd;
4808b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4809c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4810c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
4811c4739e2fSRandall Stewart 		    0,
4812c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
4813c4739e2fSRandall Stewart 		    __LINE__);
4814c4739e2fSRandall Stewart 	}
4815f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
4816f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
4817b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4818f8829a4aSRandall Stewart 		sctp_log_sack(asoc->last_acked_seq,
4819f8829a4aSRandall Stewart 		    cum_ack,
4820f8829a4aSRandall Stewart 		    0,
4821f8829a4aSRandall Stewart 		    num_seg,
4822f8829a4aSRandall Stewart 		    num_dup,
4823f8829a4aSRandall Stewart 		    SCTP_LOG_NEW_SACK);
482480fefe0aSRandall Stewart 	}
4825b3f1ea41SRandall Stewart 	if ((num_dup) && (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_FR_LOGGING_ENABLE | SCTP_EARLYFR_LOGGING_ENABLE))) {
4826f8829a4aSRandall Stewart 		int off_to_dup, iii;
4827458303daSRandall Stewart 		uint32_t *dupdata, dblock;
4828f8829a4aSRandall Stewart 
4829f8829a4aSRandall Stewart 		off_to_dup = (num_seg * sizeof(struct sctp_gap_ack_block)) + sizeof(struct sctp_sack_chunk);
4830f8829a4aSRandall Stewart 		if ((off_to_dup + (num_dup * sizeof(uint32_t))) <= sack_length) {
4831458303daSRandall Stewart 			dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
4832458303daSRandall Stewart 			    sizeof(uint32_t), (uint8_t *) & dblock);
4833458303daSRandall Stewart 			off_to_dup += sizeof(uint32_t);
4834458303daSRandall Stewart 			if (dupdata) {
4835f8829a4aSRandall Stewart 				for (iii = 0; iii < num_dup; iii++) {
4836f8829a4aSRandall Stewart 					sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
4837458303daSRandall Stewart 					dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
4838458303daSRandall Stewart 					    sizeof(uint32_t), (uint8_t *) & dblock);
4839458303daSRandall Stewart 					if (dupdata == NULL)
4840458303daSRandall Stewart 						break;
4841458303daSRandall Stewart 					off_to_dup += sizeof(uint32_t);
4842458303daSRandall Stewart 				}
4843f8829a4aSRandall Stewart 			}
4844f8829a4aSRandall Stewart 		} else {
4845ad81507eSRandall Stewart 			SCTP_PRINTF("Size invalid offset to dups:%d number dups:%d sack_len:%d num gaps:%d\n",
4846f8829a4aSRandall Stewart 			    off_to_dup, num_dup, sack_length, num_seg);
4847f8829a4aSRandall Stewart 		}
4848f8829a4aSRandall Stewart 	}
4849b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4850c105859eSRandall Stewart 		/* reality check */
4851c105859eSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4852c105859eSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
4853c105859eSRandall Stewart 			    sctpchunk_listhead);
4854c105859eSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
4855c105859eSRandall Stewart 		} else {
4856c105859eSRandall Stewart 			send_s = asoc->sending_seq;
4857c105859eSRandall Stewart 		}
4858f8829a4aSRandall Stewart 		if (cum_ack == send_s ||
4859f8829a4aSRandall Stewart 		    compare_with_wrap(cum_ack, send_s, MAX_TSN)) {
4860c105859eSRandall Stewart #ifndef INVARIANTS
4861c105859eSRandall Stewart 			struct mbuf *oper;
4862c105859eSRandall Stewart 
4863c105859eSRandall Stewart #endif
4864c105859eSRandall Stewart #ifdef INVARIANTS
4865139bc87fSRandall Stewart 	hopeless_peer:
4866139bc87fSRandall Stewart 			panic("Impossible sack 1");
4867139bc87fSRandall Stewart #else
4868c105859eSRandall Stewart 
4869f8829a4aSRandall Stewart 
4870f8829a4aSRandall Stewart 			/*
4871f8829a4aSRandall Stewart 			 * no way, we have not even sent this TSN out yet.
4872f8829a4aSRandall Stewart 			 * Peer is hopelessly messed up with us.
4873f8829a4aSRandall Stewart 			 */
4874f8829a4aSRandall Stewart 	hopeless_peer:
4875f8829a4aSRandall Stewart 			*abort_now = 1;
4876f8829a4aSRandall Stewart 			/* XXX */
4877f8829a4aSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4878f8829a4aSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
4879f8829a4aSRandall Stewart 			if (oper) {
4880f8829a4aSRandall Stewart 				struct sctp_paramhdr *ph;
4881f8829a4aSRandall Stewart 				uint32_t *ippp;
4882f8829a4aSRandall Stewart 
4883139bc87fSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4884f8829a4aSRandall Stewart 				    sizeof(uint32_t);
4885f8829a4aSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
4886f8829a4aSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4887139bc87fSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
4888f8829a4aSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
4889a5d547adSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
4890f8829a4aSRandall Stewart 			}
4891a5d547adSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4892ceaad40aSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
4893f8829a4aSRandall Stewart 			return;
4894139bc87fSRandall Stewart #endif
4895f8829a4aSRandall Stewart 		}
4896f8829a4aSRandall Stewart 	}
4897f8829a4aSRandall Stewart 	/**********************/
4898f8829a4aSRandall Stewart 	/* 1) check the range */
4899f8829a4aSRandall Stewart 	/**********************/
4900f8829a4aSRandall Stewart 	if (compare_with_wrap(asoc->last_acked_seq, last_tsn, MAX_TSN)) {
4901f8829a4aSRandall Stewart 		/* acking something behind */
4902f8829a4aSRandall Stewart 		return;
4903f8829a4aSRandall Stewart 	}
4904bff64a4dSRandall Stewart 	sav_cum_ack = asoc->last_acked_seq;
4905bff64a4dSRandall Stewart 
4906f8829a4aSRandall Stewart 	/* update the Rwnd of the peer */
4907f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue) &&
4908f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->send_queue) &&
4909f8829a4aSRandall Stewart 	    (asoc->stream_queue_cnt == 0)
4910f8829a4aSRandall Stewart 	    ) {
4911f8829a4aSRandall Stewart 		/* nothing left on send/sent and strmq */
4912b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4913f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4914f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
491580fefe0aSRandall Stewart 		}
4916f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
4917f8829a4aSRandall Stewart 		if (asoc->sent_queue_retran_cnt) {
4918f8829a4aSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4919f8829a4aSRandall Stewart 		}
4920f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4921f8829a4aSRandall Stewart 			/* SWS sender side engages */
4922f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
4923f8829a4aSRandall Stewart 		}
4924f8829a4aSRandall Stewart 		/* stop any timers */
4925f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4926f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4927a5d547adSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4928b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
4929139bc87fSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4930f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck1);
4931a5d547adSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4932a5d547adSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4933f8829a4aSRandall Stewart 				}
4934f8829a4aSRandall Stewart 			}
4935f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4936f8829a4aSRandall Stewart 			net->flight_size = 0;
4937f8829a4aSRandall Stewart 		}
4938f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4939f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4940f8829a4aSRandall Stewart 		return;
4941f8829a4aSRandall Stewart 	}
4942f8829a4aSRandall Stewart 	/*
4943f8829a4aSRandall Stewart 	 * We init netAckSz and netAckSz2 to 0. These are used to track 2
4944f8829a4aSRandall Stewart 	 * things. The total byte count acked is tracked in netAckSz AND
4945f8829a4aSRandall Stewart 	 * netAck2 is used to track the total bytes acked that are un-
4946f8829a4aSRandall Stewart 	 * amibguious and were never retransmitted. We track these on a per
4947f8829a4aSRandall Stewart 	 * destination address basis.
4948f8829a4aSRandall Stewart 	 */
4949f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4950f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
4951f8829a4aSRandall Stewart 		net->net_ack = 0;
4952f8829a4aSRandall Stewart 		net->net_ack2 = 0;
4953f8829a4aSRandall Stewart 
4954f8829a4aSRandall Stewart 		/*
495542551e99SRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
495642551e99SRandall Stewart 		 * SACK processing
4957f8829a4aSRandall Stewart 		 */
4958f8829a4aSRandall Stewart 		net->new_pseudo_cumack = 0;
4959f8829a4aSRandall Stewart 		net->will_exit_fast_recovery = 0;
4960f8829a4aSRandall Stewart 	}
4961f8829a4aSRandall Stewart 	/* process the new consecutive TSN first */
4962f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
4963f8829a4aSRandall Stewart 	while (tp1) {
4964f8829a4aSRandall Stewart 		if (compare_with_wrap(last_tsn, tp1->rec.data.TSN_seq,
4965f8829a4aSRandall Stewart 		    MAX_TSN) ||
4966f8829a4aSRandall Stewart 		    last_tsn == tp1->rec.data.TSN_seq) {
4967f8829a4aSRandall Stewart 			if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4968f8829a4aSRandall Stewart 				/*
4969f8829a4aSRandall Stewart 				 * ECN Nonce: Add the nonce to the sender's
4970f8829a4aSRandall Stewart 				 * nonce sum
4971f8829a4aSRandall Stewart 				 */
4972f8829a4aSRandall Stewart 				asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
4973f8829a4aSRandall Stewart 				accum_moved = 1;
4974f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4975f8829a4aSRandall Stewart 					/*
4976f8829a4aSRandall Stewart 					 * If it is less than ACKED, it is
4977f8829a4aSRandall Stewart 					 * now no-longer in flight. Higher
4978f8829a4aSRandall Stewart 					 * values may occur during marking
4979f8829a4aSRandall Stewart 					 */
4980f8829a4aSRandall Stewart 					if ((tp1->whoTo->dest_state &
4981f8829a4aSRandall Stewart 					    SCTP_ADDR_UNCONFIRMED) &&
4982f8829a4aSRandall Stewart 					    (tp1->snd_count < 2)) {
4983f8829a4aSRandall Stewart 						/*
4984f8829a4aSRandall Stewart 						 * If there was no retran
4985f8829a4aSRandall Stewart 						 * and the address is
4986f8829a4aSRandall Stewart 						 * un-confirmed and we sent
4987f8829a4aSRandall Stewart 						 * there and are now
4988f8829a4aSRandall Stewart 						 * sacked.. its confirmed,
4989f8829a4aSRandall Stewart 						 * mark it so.
4990f8829a4aSRandall Stewart 						 */
4991f8829a4aSRandall Stewart 						tp1->whoTo->dest_state &=
4992f8829a4aSRandall Stewart 						    ~SCTP_ADDR_UNCONFIRMED;
4993f8829a4aSRandall Stewart 					}
4994c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4995b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4996c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4997a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
4998a5d547adSRandall Stewart 							    tp1->book_size,
4999c105859eSRandall Stewart 							    (uintptr_t) tp1->whoTo,
5000a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
500180fefe0aSRandall Stewart 						}
5002c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
5003c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
5004f8829a4aSRandall Stewart 					}
5005f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
5006f8829a4aSRandall Stewart 
5007f8829a4aSRandall Stewart 					/* CMT SFR and DAC algos */
5008f8829a4aSRandall Stewart 					this_sack_lowest_newack = tp1->rec.data.TSN_seq;
5009f8829a4aSRandall Stewart 					tp1->whoTo->saw_newack = 1;
5010f8829a4aSRandall Stewart 
5011f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
5012f8829a4aSRandall Stewart 						/*
5013f8829a4aSRandall Stewart 						 * True non-retransmited
5014f8829a4aSRandall Stewart 						 * chunk
5015f8829a4aSRandall Stewart 						 */
5016f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
5017f8829a4aSRandall Stewart 						    tp1->send_size;
5018f8829a4aSRandall Stewart 
5019f8829a4aSRandall Stewart 						/* update RTO too? */
5020f8829a4aSRandall Stewart 						if (tp1->do_rtt) {
5021f8829a4aSRandall Stewart 							tp1->whoTo->RTO =
5022f8829a4aSRandall Stewart 							    sctp_calculate_rto(stcb,
5023f8829a4aSRandall Stewart 							    asoc, tp1->whoTo,
502418e198d3SRandall Stewart 							    &tp1->sent_rcv_time,
502518e198d3SRandall Stewart 							    sctp_align_safe_nocopy);
5026f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
5027f8829a4aSRandall Stewart 						}
5028f8829a4aSRandall Stewart 					}
5029f8829a4aSRandall Stewart 					/*
5030f8829a4aSRandall Stewart 					 * CMT: CUCv2 algorithm. From the
5031f8829a4aSRandall Stewart 					 * cumack'd TSNs, for each TSN being
5032f8829a4aSRandall Stewart 					 * acked for the first time, set the
5033f8829a4aSRandall Stewart 					 * following variables for the
5034f8829a4aSRandall Stewart 					 * corresp destination.
5035f8829a4aSRandall Stewart 					 * new_pseudo_cumack will trigger a
5036f8829a4aSRandall Stewart 					 * cwnd update.
5037f8829a4aSRandall Stewart 					 * find_(rtx_)pseudo_cumack will
5038f8829a4aSRandall Stewart 					 * trigger search for the next
5039f8829a4aSRandall Stewart 					 * expected (rtx-)pseudo-cumack.
5040f8829a4aSRandall Stewart 					 */
5041f8829a4aSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
5042f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
5043f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
5044f8829a4aSRandall Stewart 
5045f8829a4aSRandall Stewart 
5046b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
5047f8829a4aSRandall Stewart 						sctp_log_sack(asoc->last_acked_seq,
5048f8829a4aSRandall Stewart 						    cum_ack,
5049f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
5050f8829a4aSRandall Stewart 						    0,
5051f8829a4aSRandall Stewart 						    0,
5052f8829a4aSRandall Stewart 						    SCTP_LOG_TSN_ACKED);
505380fefe0aSRandall Stewart 					}
5054b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
5055f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
505680fefe0aSRandall Stewart 					}
5057f8829a4aSRandall Stewart 				}
5058f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5059f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
5060f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
5061f8829a4aSRandall Stewart 					sctp_audit_log(0xB3,
5062f8829a4aSRandall Stewart 					    (asoc->sent_queue_retran_cnt & 0x000000ff));
5063f8829a4aSRandall Stewart #endif
5064f8829a4aSRandall Stewart 				}
506542551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
506642551e99SRandall Stewart 					/* deflate the cwnd */
506742551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
506842551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
506942551e99SRandall Stewart 				}
5070f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
5071f8829a4aSRandall Stewart 			}
5072f8829a4aSRandall Stewart 		} else {
5073f8829a4aSRandall Stewart 			break;
5074f8829a4aSRandall Stewart 		}
5075f8829a4aSRandall Stewart 		tp1 = TAILQ_NEXT(tp1, sctp_next);
5076f8829a4aSRandall Stewart 	}
5077f8829a4aSRandall Stewart 	biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
5078f8829a4aSRandall Stewart 	/* always set this up to cum-ack */
5079f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = last_tsn;
5080f8829a4aSRandall Stewart 
5081458303daSRandall Stewart 	/* Move offset up to point to gaps/dups */
5082458303daSRandall Stewart 	offset += sizeof(struct sctp_sack_chunk);
5083f8829a4aSRandall Stewart 	if (((num_seg * (sizeof(struct sctp_gap_ack_block))) + sizeof(struct sctp_sack_chunk)) > sack_length) {
5084f8829a4aSRandall Stewart 
5085f8829a4aSRandall Stewart 		/* skip corrupt segments */
5086f8829a4aSRandall Stewart 		goto skip_segments;
5087f8829a4aSRandall Stewart 	}
5088f8829a4aSRandall Stewart 	if (num_seg > 0) {
5089f8829a4aSRandall Stewart 
5090f8829a4aSRandall Stewart 		/*
5091f8829a4aSRandall Stewart 		 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
5092f8829a4aSRandall Stewart 		 * to be greater than the cumack. Also reset saw_newack to 0
5093f8829a4aSRandall Stewart 		 * for all dests.
5094f8829a4aSRandall Stewart 		 */
5095f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5096f8829a4aSRandall Stewart 			net->saw_newack = 0;
5097f8829a4aSRandall Stewart 			net->this_sack_highest_newack = last_tsn;
5098f8829a4aSRandall Stewart 		}
5099f8829a4aSRandall Stewart 
5100f8829a4aSRandall Stewart 		/*
5101f8829a4aSRandall Stewart 		 * thisSackHighestGap will increase while handling NEW
5102f8829a4aSRandall Stewart 		 * segments this_sack_highest_newack will increase while
5103f8829a4aSRandall Stewart 		 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
5104f8829a4aSRandall Stewart 		 * used for CMT DAC algo. saw_newack will also change.
5105f8829a4aSRandall Stewart 		 */
5106458303daSRandall Stewart 		sctp_handle_segments(m, &offset, stcb, asoc, ch, last_tsn,
5107f8829a4aSRandall Stewart 		    &biggest_tsn_acked, &biggest_tsn_newly_acked, &this_sack_lowest_newack,
5108f8829a4aSRandall Stewart 		    num_seg, &ecn_seg_sums);
5109f8829a4aSRandall Stewart 
5110b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
5111f8829a4aSRandall Stewart 			/*
5112f8829a4aSRandall Stewart 			 * validate the biggest_tsn_acked in the gap acks if
5113f8829a4aSRandall Stewart 			 * strict adherence is wanted.
5114f8829a4aSRandall Stewart 			 */
5115f8829a4aSRandall Stewart 			if ((biggest_tsn_acked == send_s) ||
5116f8829a4aSRandall Stewart 			    (compare_with_wrap(biggest_tsn_acked, send_s, MAX_TSN))) {
5117f8829a4aSRandall Stewart 				/*
5118f8829a4aSRandall Stewart 				 * peer is either confused or we are under
5119f8829a4aSRandall Stewart 				 * attack. We must abort.
5120f8829a4aSRandall Stewart 				 */
5121f8829a4aSRandall Stewart 				goto hopeless_peer;
5122f8829a4aSRandall Stewart 			}
5123f8829a4aSRandall Stewart 		}
5124f8829a4aSRandall Stewart 	}
5125f8829a4aSRandall Stewart skip_segments:
5126f8829a4aSRandall Stewart 	/*******************************************/
5127f8829a4aSRandall Stewart 	/* cancel ALL T3-send timer if accum moved */
5128f8829a4aSRandall Stewart 	/*******************************************/
5129b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
5130f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5131f8829a4aSRandall Stewart 			if (net->new_pseudo_cumack)
5132f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5133a5d547adSRandall Stewart 				    stcb, net,
5134a5d547adSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
5135f8829a4aSRandall Stewart 
5136f8829a4aSRandall Stewart 		}
5137f8829a4aSRandall Stewart 	} else {
5138f8829a4aSRandall Stewart 		if (accum_moved) {
5139f8829a4aSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5140f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5141a5d547adSRandall Stewart 				    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
5142f8829a4aSRandall Stewart 			}
5143f8829a4aSRandall Stewart 		}
5144f8829a4aSRandall Stewart 	}
5145f8829a4aSRandall Stewart 	/********************************************/
5146f8829a4aSRandall Stewart 	/* drop the acked chunks from the sendqueue */
5147f8829a4aSRandall Stewart 	/********************************************/
5148f8829a4aSRandall Stewart 	asoc->last_acked_seq = cum_ack;
5149f8829a4aSRandall Stewart 
5150f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
5151f8829a4aSRandall Stewart 	if (tp1 == NULL)
5152f8829a4aSRandall Stewart 		goto done_with_it;
5153f8829a4aSRandall Stewart 	do {
5154f8829a4aSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq, cum_ack,
5155f8829a4aSRandall Stewart 		    MAX_TSN)) {
5156f8829a4aSRandall Stewart 			break;
5157f8829a4aSRandall Stewart 		}
5158f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
5159f8829a4aSRandall Stewart 			/* no more sent on list */
516018e198d3SRandall Stewart 			printf("Warning, tp1->sent == %d and its now acked?\n",
516118e198d3SRandall Stewart 			    tp1->sent);
5162f8829a4aSRandall Stewart 		}
5163f8829a4aSRandall Stewart 		tp2 = TAILQ_NEXT(tp1, sctp_next);
5164f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
5165f8829a4aSRandall Stewart 		if (tp1->pr_sctp_on) {
5166f8829a4aSRandall Stewart 			if (asoc->pr_sctp_cnt != 0)
5167f8829a4aSRandall Stewart 				asoc->pr_sctp_cnt--;
5168f8829a4aSRandall Stewart 		}
5169f8829a4aSRandall Stewart 		if ((TAILQ_FIRST(&asoc->sent_queue) == NULL) &&
5170f8829a4aSRandall Stewart 		    (asoc->total_flight > 0)) {
5171f1f73e57SRandall Stewart #ifdef INVARIANTS
5172c105859eSRandall Stewart 			panic("Warning flight size is postive and should be 0");
5173f1f73e57SRandall Stewart #else
5174ad81507eSRandall Stewart 			SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
5175f8829a4aSRandall Stewart 			    asoc->total_flight);
5176f1f73e57SRandall Stewart #endif
5177f8829a4aSRandall Stewart 			asoc->total_flight = 0;
5178f8829a4aSRandall Stewart 		}
5179f8829a4aSRandall Stewart 		if (tp1->data) {
518004ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
5181f8829a4aSRandall Stewart 			sctp_free_bufspace(stcb, asoc, tp1, 1);
5182f8829a4aSRandall Stewart 			sctp_m_freem(tp1->data);
5183f8829a4aSRandall Stewart 			if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
5184f8829a4aSRandall Stewart 				asoc->sent_queue_cnt_removeable--;
5185f8829a4aSRandall Stewart 			}
5186f8829a4aSRandall Stewart 		}
5187b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
5188f8829a4aSRandall Stewart 			sctp_log_sack(asoc->last_acked_seq,
5189f8829a4aSRandall Stewart 			    cum_ack,
5190f8829a4aSRandall Stewart 			    tp1->rec.data.TSN_seq,
5191f8829a4aSRandall Stewart 			    0,
5192f8829a4aSRandall Stewart 			    0,
5193f8829a4aSRandall Stewart 			    SCTP_LOG_FREE_SENT);
519480fefe0aSRandall Stewart 		}
5195f8829a4aSRandall Stewart 		tp1->data = NULL;
5196f8829a4aSRandall Stewart 		asoc->sent_queue_cnt--;
5197f8829a4aSRandall Stewart 		sctp_free_a_chunk(stcb, tp1);
5198f8829a4aSRandall Stewart 		wake_him++;
5199f8829a4aSRandall Stewart 		tp1 = tp2;
5200f8829a4aSRandall Stewart 	} while (tp1 != NULL);
5201f8829a4aSRandall Stewart 
5202f8829a4aSRandall Stewart done_with_it:
520304ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
5204f8829a4aSRandall Stewart 	if ((wake_him) && (stcb->sctp_socket)) {
5205ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5206ceaad40aSRandall Stewart 		struct socket *so;
5207ceaad40aSRandall Stewart 
5208ceaad40aSRandall Stewart #endif
5209f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
5210b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
5211f8829a4aSRandall Stewart 			sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_WAKESND_FROM_SACK);
521280fefe0aSRandall Stewart 		}
5213ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5214ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
5215ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
5216ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
5217ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
5218ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
5219ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
5220ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
5221ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
5222ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
5223ceaad40aSRandall Stewart 			return;
5224ceaad40aSRandall Stewart 		}
5225ceaad40aSRandall Stewart #endif
5226f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
5227ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5228ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
5229ceaad40aSRandall Stewart #endif
5230f8829a4aSRandall Stewart 	} else {
5231b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
5232f8829a4aSRandall Stewart 			sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_NOWAKE_FROM_SACK);
523380fefe0aSRandall Stewart 		}
5234f8829a4aSRandall Stewart 	}
5235f8829a4aSRandall Stewart 
523642551e99SRandall Stewart 	if (asoc->fast_retran_loss_recovery && accum_moved) {
5237f8829a4aSRandall Stewart 		if (compare_with_wrap(asoc->last_acked_seq,
5238f8829a4aSRandall Stewart 		    asoc->fast_recovery_tsn, MAX_TSN) ||
5239f8829a4aSRandall Stewart 		    asoc->last_acked_seq == asoc->fast_recovery_tsn) {
5240f8829a4aSRandall Stewart 			/* Setup so we will exit RFC2582 fast recovery */
5241f8829a4aSRandall Stewart 			will_exit_fast_recovery = 1;
5242f8829a4aSRandall Stewart 		}
5243f8829a4aSRandall Stewart 	}
5244f8829a4aSRandall Stewart 	/*
5245f8829a4aSRandall Stewart 	 * Check for revoked fragments:
5246f8829a4aSRandall Stewart 	 *
5247f8829a4aSRandall Stewart 	 * if Previous sack - Had no frags then we can't have any revoked if
5248f8829a4aSRandall Stewart 	 * Previous sack - Had frag's then - If we now have frags aka
5249f8829a4aSRandall Stewart 	 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
5250f8829a4aSRandall Stewart 	 * some of them. else - The peer revoked all ACKED fragments, since
5251f8829a4aSRandall Stewart 	 * we had some before and now we have NONE.
5252f8829a4aSRandall Stewart 	 */
5253f8829a4aSRandall Stewart 
5254f42a358aSRandall Stewart 	if (num_seg)
5255c105859eSRandall Stewart 		sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
5256f8829a4aSRandall Stewart 	else if (asoc->saw_sack_with_frags) {
5257f8829a4aSRandall Stewart 		int cnt_revoked = 0;
5258f8829a4aSRandall Stewart 
5259f8829a4aSRandall Stewart 		tp1 = TAILQ_FIRST(&asoc->sent_queue);
5260f8829a4aSRandall Stewart 		if (tp1 != NULL) {
5261f8829a4aSRandall Stewart 			/* Peer revoked all dg's marked or acked */
5262f8829a4aSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5263f8829a4aSRandall Stewart 				if ((tp1->sent > SCTP_DATAGRAM_RESEND) &&
5264f8829a4aSRandall Stewart 				    (tp1->sent < SCTP_FORWARD_TSN_SKIP)) {
5265f8829a4aSRandall Stewart 					tp1->sent = SCTP_DATAGRAM_SENT;
5266b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
5267c105859eSRandall Stewart 						sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
5268c105859eSRandall Stewart 						    tp1->whoTo->flight_size,
5269c105859eSRandall Stewart 						    tp1->book_size,
5270c105859eSRandall Stewart 						    (uintptr_t) tp1->whoTo,
5271c105859eSRandall Stewart 						    tp1->rec.data.TSN_seq);
527280fefe0aSRandall Stewart 					}
5273c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
5274c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
5275a5d547adSRandall Stewart 					tp1->rec.data.chunk_was_revoked = 1;
527642551e99SRandall Stewart 					/*
527742551e99SRandall Stewart 					 * To ensure that this increase in
527842551e99SRandall Stewart 					 * flightsize, which is artificial,
527942551e99SRandall Stewart 					 * does not throttle the sender, we
528042551e99SRandall Stewart 					 * also increase the cwnd
528142551e99SRandall Stewart 					 * artificially.
528242551e99SRandall Stewart 					 */
528342551e99SRandall Stewart 					tp1->whoTo->cwnd += tp1->book_size;
5284f8829a4aSRandall Stewart 					cnt_revoked++;
5285f8829a4aSRandall Stewart 				}
5286f8829a4aSRandall Stewart 			}
5287f8829a4aSRandall Stewart 			if (cnt_revoked) {
5288f8829a4aSRandall Stewart 				reneged_all = 1;
5289f8829a4aSRandall Stewart 			}
5290f8829a4aSRandall Stewart 		}
5291f8829a4aSRandall Stewart 		asoc->saw_sack_with_frags = 0;
5292f8829a4aSRandall Stewart 	}
5293f8829a4aSRandall Stewart 	if (num_seg)
5294f8829a4aSRandall Stewart 		asoc->saw_sack_with_frags = 1;
5295f8829a4aSRandall Stewart 	else
5296f8829a4aSRandall Stewart 		asoc->saw_sack_with_frags = 0;
5297f8829a4aSRandall Stewart 
5298b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
5299b54d3a6cSRandall Stewart 	asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
5300f8829a4aSRandall Stewart 
5301f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
5302f8829a4aSRandall Stewart 		/* nothing left in-flight */
5303f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5304f8829a4aSRandall Stewart 			/* stop all timers */
5305b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
5306139bc87fSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
5307f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
5308a5d547adSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
5309a5d547adSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_29);
5310f8829a4aSRandall Stewart 				}
5311f8829a4aSRandall Stewart 			}
5312f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5313a5d547adSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
5314f8829a4aSRandall Stewart 			net->flight_size = 0;
5315f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
5316f8829a4aSRandall Stewart 		}
5317f8829a4aSRandall Stewart 		asoc->total_flight = 0;
5318f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
5319f8829a4aSRandall Stewart 	}
5320f8829a4aSRandall Stewart 	/**********************************/
5321f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
5322f8829a4aSRandall Stewart 	/**********************************/
5323f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
5324f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
5325b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
5326f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
5327f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
532880fefe0aSRandall Stewart 		}
5329f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
5330f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5331f8829a4aSRandall Stewart 			/* SWS sender side engages */
5332f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
5333f8829a4aSRandall Stewart 		}
5334f8829a4aSRandall Stewart 		/* clean up */
5335f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
5336f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
5337f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
5338f8829a4aSRandall Stewart 		    (asoc->locked_on_sending)
5339f8829a4aSRandall Stewart 		    ) {
5340f8829a4aSRandall Stewart 			struct sctp_stream_queue_pending *sp;
5341f8829a4aSRandall Stewart 
5342f8829a4aSRandall Stewart 			/*
5343f8829a4aSRandall Stewart 			 * I may be in a state where we got all across.. but
5344f8829a4aSRandall Stewart 			 * cannot write more due to a shutdown... we abort
5345f8829a4aSRandall Stewart 			 * since the user did not indicate EOR in this case.
5346f8829a4aSRandall Stewart 			 */
5347f8829a4aSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
5348f8829a4aSRandall Stewart 			    sctp_streamhead);
53492afb3e84SRandall Stewart 			if ((sp) && (sp->length == 0)) {
5350f8829a4aSRandall Stewart 				asoc->locked_on_sending = NULL;
53512afb3e84SRandall Stewart 				if (sp->msg_is_complete) {
5352f8829a4aSRandall Stewart 					asoc->stream_queue_cnt--;
53532afb3e84SRandall Stewart 				} else {
53542afb3e84SRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
53552afb3e84SRandall Stewart 					asoc->stream_queue_cnt--;
53562afb3e84SRandall Stewart 				}
5357f8829a4aSRandall Stewart 			}
5358f8829a4aSRandall Stewart 		}
5359f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
5360f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
5361f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
5362f8829a4aSRandall Stewart 				/* Need to abort here */
5363f8829a4aSRandall Stewart 				struct mbuf *oper;
5364f8829a4aSRandall Stewart 
5365f8829a4aSRandall Stewart 		abort_out_now:
5366f8829a4aSRandall Stewart 				*abort_now = 1;
5367f8829a4aSRandall Stewart 				/* XXX */
5368f8829a4aSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
5369f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
5370f8829a4aSRandall Stewart 				if (oper) {
5371f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
5372f8829a4aSRandall Stewart 					uint32_t *ippp;
5373f8829a4aSRandall Stewart 
5374139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
5375f8829a4aSRandall Stewart 					    sizeof(uint32_t);
5376f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
5377f8829a4aSRandall Stewart 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
5378139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
5379f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
5380a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_31);
5381f8829a4aSRandall Stewart 				}
5382a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
5383ceaad40aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
5384f8829a4aSRandall Stewart 				return;
5385f8829a4aSRandall Stewart 			} else {
5386f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
5387f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
5388f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5389f42a358aSRandall Stewart 				}
5390c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
5391b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
5392f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
5393f8829a4aSRandall Stewart 				sctp_send_shutdown(stcb,
5394f8829a4aSRandall Stewart 				    stcb->asoc.primary_destination);
5395f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
5396f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
5397f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
5398f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
5399f8829a4aSRandall Stewart 			}
5400f8829a4aSRandall Stewart 			return;
5401f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
5402f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
5403f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
5404f8829a4aSRandall Stewart 				goto abort_out_now;
5405f8829a4aSRandall Stewart 			}
5406f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5407c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
5408b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
5409f8829a4aSRandall Stewart 			sctp_send_shutdown_ack(stcb,
5410f8829a4aSRandall Stewart 			    stcb->asoc.primary_destination);
5411f8829a4aSRandall Stewart 
5412f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
5413f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb, asoc->primary_destination);
5414f8829a4aSRandall Stewart 			return;
5415f8829a4aSRandall Stewart 		}
5416f8829a4aSRandall Stewart 	}
5417f8829a4aSRandall Stewart 	/*
5418f8829a4aSRandall Stewart 	 * Now here we are going to recycle net_ack for a different use...
5419f8829a4aSRandall Stewart 	 * HEADS UP.
5420f8829a4aSRandall Stewart 	 */
5421f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5422f8829a4aSRandall Stewart 		net->net_ack = 0;
5423f8829a4aSRandall Stewart 	}
5424f8829a4aSRandall Stewart 
5425f8829a4aSRandall Stewart 	/*
5426f8829a4aSRandall Stewart 	 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
5427f8829a4aSRandall Stewart 	 * to be done. Setting this_sack_lowest_newack to the cum_ack will
5428f8829a4aSRandall Stewart 	 * automatically ensure that.
5429f8829a4aSRandall Stewart 	 */
5430b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac) && (cmt_dac_flag == 0)) {
5431f8829a4aSRandall Stewart 		this_sack_lowest_newack = cum_ack;
5432f8829a4aSRandall Stewart 	}
5433f8829a4aSRandall Stewart 	if (num_seg > 0) {
5434f8829a4aSRandall Stewart 		sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
5435f8829a4aSRandall Stewart 		    biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
5436f8829a4aSRandall Stewart 	}
5437b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
5438b54d3a6cSRandall Stewart 	asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
5439f8829a4aSRandall Stewart 
5440f8829a4aSRandall Stewart 	/******************************************************************
5441f8829a4aSRandall Stewart 	 *  Here we do the stuff with ECN Nonce checking.
5442f8829a4aSRandall Stewart 	 *  We basically check to see if the nonce sum flag was incorrect
5443f8829a4aSRandall Stewart 	 *  or if resynchronization needs to be done. Also if we catch a
5444f8829a4aSRandall Stewart 	 *  misbehaving receiver we give him the kick.
5445f8829a4aSRandall Stewart 	 ******************************************************************/
5446f8829a4aSRandall Stewart 
5447f8829a4aSRandall Stewart 	if (asoc->ecn_nonce_allowed) {
5448f8829a4aSRandall Stewart 		if (asoc->nonce_sum_check) {
5449f8829a4aSRandall Stewart 			if (nonce_sum_flag != ((asoc->nonce_sum_expect_base + ecn_seg_sums) & SCTP_SACK_NONCE_SUM)) {
5450f8829a4aSRandall Stewart 				if (asoc->nonce_wait_for_ecne == 0) {
5451f8829a4aSRandall Stewart 					struct sctp_tmit_chunk *lchk;
5452f8829a4aSRandall Stewart 
5453f8829a4aSRandall Stewart 					lchk = TAILQ_FIRST(&asoc->send_queue);
5454f8829a4aSRandall Stewart 					asoc->nonce_wait_for_ecne = 1;
5455f8829a4aSRandall Stewart 					if (lchk) {
5456f8829a4aSRandall Stewart 						asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
5457f8829a4aSRandall Stewart 					} else {
5458f8829a4aSRandall Stewart 						asoc->nonce_wait_tsn = asoc->sending_seq;
5459f8829a4aSRandall Stewart 					}
5460f8829a4aSRandall Stewart 				} else {
5461f8829a4aSRandall Stewart 					if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
5462f8829a4aSRandall Stewart 					    (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
5463f8829a4aSRandall Stewart 						/*
5464f8829a4aSRandall Stewart 						 * Misbehaving peer. We need
5465f8829a4aSRandall Stewart 						 * to react to this guy
5466f8829a4aSRandall Stewart 						 */
5467f8829a4aSRandall Stewart 						asoc->ecn_allowed = 0;
5468f8829a4aSRandall Stewart 						asoc->ecn_nonce_allowed = 0;
5469f8829a4aSRandall Stewart 					}
5470f8829a4aSRandall Stewart 				}
5471f8829a4aSRandall Stewart 			}
5472f8829a4aSRandall Stewart 		} else {
5473f8829a4aSRandall Stewart 			/* See if Resynchronization Possible */
5474f8829a4aSRandall Stewart 			if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
5475f8829a4aSRandall Stewart 				asoc->nonce_sum_check = 1;
5476f8829a4aSRandall Stewart 				/*
5477f8829a4aSRandall Stewart 				 * now we must calculate what the base is.
5478f8829a4aSRandall Stewart 				 * We do this based on two things, we know
5479f8829a4aSRandall Stewart 				 * the total's for all the segments
5480f8829a4aSRandall Stewart 				 * gap-acked in the SACK, its stored in
5481f8829a4aSRandall Stewart 				 * ecn_seg_sums. We also know the SACK's
5482f8829a4aSRandall Stewart 				 * nonce sum, its in nonce_sum_flag. So we
5483f8829a4aSRandall Stewart 				 * can build a truth table to back-calculate
5484f8829a4aSRandall Stewart 				 * the new value of
5485f8829a4aSRandall Stewart 				 * asoc->nonce_sum_expect_base:
5486f8829a4aSRandall Stewart 				 *
5487f8829a4aSRandall Stewart 				 * SACK-flag-Value         Seg-Sums Base 0 0 0
5488f8829a4aSRandall Stewart 				 * 1                    0 1 0 1 1 1
5489f8829a4aSRandall Stewart 				 * 1 0
5490f8829a4aSRandall Stewart 				 */
5491f8829a4aSRandall Stewart 				asoc->nonce_sum_expect_base = (ecn_seg_sums ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
5492f8829a4aSRandall Stewart 			}
5493f8829a4aSRandall Stewart 		}
5494f8829a4aSRandall Stewart 	}
5495f8829a4aSRandall Stewart 	/* Now are we exiting loss recovery ? */
5496f8829a4aSRandall Stewart 	if (will_exit_fast_recovery) {
5497f8829a4aSRandall Stewart 		/* Ok, we must exit fast recovery */
5498f8829a4aSRandall Stewart 		asoc->fast_retran_loss_recovery = 0;
5499f8829a4aSRandall Stewart 	}
5500f8829a4aSRandall Stewart 	if ((asoc->sat_t3_loss_recovery) &&
5501f8829a4aSRandall Stewart 	    ((compare_with_wrap(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn,
5502f8829a4aSRandall Stewart 	    MAX_TSN) ||
5503f8829a4aSRandall Stewart 	    (asoc->last_acked_seq == asoc->sat_t3_recovery_tsn)))) {
5504f8829a4aSRandall Stewart 		/* end satellite t3 loss recovery */
5505f8829a4aSRandall Stewart 		asoc->sat_t3_loss_recovery = 0;
5506f8829a4aSRandall Stewart 	}
550742551e99SRandall Stewart 	/*
550842551e99SRandall Stewart 	 * CMT Fast recovery
550942551e99SRandall Stewart 	 */
5510f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5511f8829a4aSRandall Stewart 		if (net->will_exit_fast_recovery) {
5512f8829a4aSRandall Stewart 			/* Ok, we must exit fast recovery */
5513f8829a4aSRandall Stewart 			net->fast_retran_loss_recovery = 0;
5514f8829a4aSRandall Stewart 		}
5515f8829a4aSRandall Stewart 	}
5516f8829a4aSRandall Stewart 
5517f8829a4aSRandall Stewart 	/* Adjust and set the new rwnd value */
5518b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
5519f8829a4aSRandall Stewart 		sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
5520b3f1ea41SRandall Stewart 		    asoc->peers_rwnd, asoc->total_flight, (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)), a_rwnd);
552180fefe0aSRandall Stewart 	}
5522f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
5523b3f1ea41SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
5524f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5525f8829a4aSRandall Stewart 		/* SWS sender side engages */
5526f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
5527f8829a4aSRandall Stewart 	}
55285e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
55295e54f665SRandall Stewart 		win_probe_recovery = 1;
55305e54f665SRandall Stewart 	}
5531f8829a4aSRandall Stewart 	/*
5532f8829a4aSRandall Stewart 	 * Now we must setup so we have a timer up for anyone with
5533f8829a4aSRandall Stewart 	 * outstanding data.
5534f8829a4aSRandall Stewart 	 */
5535bff64a4dSRandall Stewart 	done_once = 0;
5536a5d547adSRandall Stewart again:
5537a5d547adSRandall Stewart 	j = 0;
5538f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
55395e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
5540c105859eSRandall Stewart 			win_probe_recovered = 1;
55415e54f665SRandall Stewart 			/*-
55425e54f665SRandall Stewart 			 * Find first chunk that was used with
55435e54f665SRandall Stewart 			 * window probe and clear the event. Put
55445e54f665SRandall Stewart 			 * it back into the send queue as if has
55455e54f665SRandall Stewart 			 * not been sent.
55465e54f665SRandall Stewart 			 */
55475e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
55485e54f665SRandall Stewart 				if (tp1->window_probe) {
5549c105859eSRandall Stewart 					sctp_window_probe_recovery(stcb, asoc, net, tp1);
55505e54f665SRandall Stewart 					break;
55515e54f665SRandall Stewart 				}
55525e54f665SRandall Stewart 			}
55535e54f665SRandall Stewart 		}
5554f8829a4aSRandall Stewart 		if (net->flight_size) {
5555a5d547adSRandall Stewart 			j++;
5556f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5557f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb, net);
55585171328bSRandall Stewart 			if (net->window_probe) {
55595171328bSRandall Stewart 			}
5560c105859eSRandall Stewart 		} else {
55615171328bSRandall Stewart 			if (net->window_probe) {
55625171328bSRandall Stewart 				/*
55635171328bSRandall Stewart 				 * In window probes we must assure a timer
55645171328bSRandall Stewart 				 * is still running there
55655171328bSRandall Stewart 				 */
55665171328bSRandall Stewart 
55675171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
55685171328bSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_SEND,
55695171328bSRandall Stewart 					    stcb->sctp_ep, stcb, net);
55705171328bSRandall Stewart 
55715171328bSRandall Stewart 				}
55725171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5573c105859eSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5574c105859eSRandall Stewart 				    stcb, net,
5575c105859eSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
5576c105859eSRandall Stewart 			}
5577b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
5578c105859eSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
5579c105859eSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
5580c105859eSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
5581c105859eSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
5582c105859eSRandall Stewart 				}
5583c105859eSRandall Stewart 			}
5584f8829a4aSRandall Stewart 		}
5585f8829a4aSRandall Stewart 	}
5586bff64a4dSRandall Stewart 	if ((j == 0) &&
5587bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
5588bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
5589c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
5590bff64a4dSRandall Stewart 	    (done_once == 0)) {
55910c0982b8SRandall Stewart 		/*
55920c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
55930c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
55940c0982b8SRandall Stewart 		 */
55950c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
5596a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5597a5d547adSRandall Stewart 				net->flight_size = 0;
5598a5d547adSRandall Stewart 			}
5599a5d547adSRandall Stewart 			asoc->total_flight = 0;
5600a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
5601a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
5602a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5603a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5604c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
5605c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
5606a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5607a5d547adSRandall Stewart 					asoc->sent_queue_retran_cnt++;
5608a5d547adSRandall Stewart 				}
5609a5d547adSRandall Stewart 			}
56100c0982b8SRandall Stewart 		}
5611bff64a4dSRandall Stewart 		done_once = 1;
5612a5d547adSRandall Stewart 		goto again;
5613a5d547adSRandall Stewart 	}
5614dfb11ef8SRandall Stewart 	/* Fix up the a-p-a-p for future PR-SCTP sends */
5615dfb11ef8SRandall Stewart 	if (compare_with_wrap(cum_ack, asoc->advanced_peer_ack_point, MAX_TSN)) {
5616dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cum_ack;
5617dfb11ef8SRandall Stewart 	}
5618830d754dSRandall Stewart 	/* C2. try to further move advancedPeerAckPoint ahead */
5619830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
5620830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
5621830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
5622830d754dSRandall Stewart 
5623830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
5624830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
5625830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
5626830d754dSRandall Stewart 		if (compare_with_wrap(asoc->advanced_peer_ack_point, cum_ack,
5627830d754dSRandall Stewart 		    MAX_TSN)) {
5628830d754dSRandall Stewart 			/*
5629830d754dSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing for notes
5630830d754dSRandall Stewart 			 * on issues that will occur when the ECN NONCE
5631830d754dSRandall Stewart 			 * stuff is put into SCTP for cross checking.
5632830d754dSRandall Stewart 			 */
56330c0982b8SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
56340c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
56350c0982b8SRandall Stewart 				    0xee, cum_ack, asoc->advanced_peer_ack_point,
56360c0982b8SRandall Stewart 				    old_adv_peer_ack_point);
56370c0982b8SRandall Stewart 			}
5638830d754dSRandall Stewart 			if (compare_with_wrap(asoc->advanced_peer_ack_point, old_adv_peer_ack_point,
5639830d754dSRandall Stewart 			    MAX_TSN)) {
5640830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
5641830d754dSRandall Stewart 				/*
5642830d754dSRandall Stewart 				 * ECN Nonce: Disable Nonce Sum check when
5643830d754dSRandall Stewart 				 * FWD TSN is sent and store resync tsn
5644830d754dSRandall Stewart 				 */
5645830d754dSRandall Stewart 				asoc->nonce_sum_check = 0;
5646830d754dSRandall Stewart 				asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
56470c0982b8SRandall Stewart 			} else if (lchk) {
56480c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
56490c0982b8SRandall Stewart 				lchk->rec.data.fwd_tsn_cnt++;
56500c0982b8SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt > 3) {
56510c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
56520c0982b8SRandall Stewart 					lchk->rec.data.fwd_tsn_cnt = 0;
56530c0982b8SRandall Stewart 				}
5654830d754dSRandall Stewart 			}
5655830d754dSRandall Stewart 		}
5656830d754dSRandall Stewart 		if (lchk) {
5657830d754dSRandall Stewart 			/* Assure a timer is up */
5658830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5659830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
5660830d754dSRandall Stewart 		}
5661830d754dSRandall Stewart 	}
5662b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
5663f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
5664f8829a4aSRandall Stewart 		    a_rwnd,
5665f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
5666f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
5667f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
566880fefe0aSRandall Stewart 	}
5669f8829a4aSRandall Stewart }
5670f8829a4aSRandall Stewart 
5671f8829a4aSRandall Stewart void
5672f8829a4aSRandall Stewart sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp,
5673f8829a4aSRandall Stewart     struct sctp_nets *netp, int *abort_flag)
5674f8829a4aSRandall Stewart {
5675f8829a4aSRandall Stewart 	/* Copy cum-ack */
5676f8829a4aSRandall Stewart 	uint32_t cum_ack, a_rwnd;
5677f8829a4aSRandall Stewart 
5678f8829a4aSRandall Stewart 	cum_ack = ntohl(cp->cumulative_tsn_ack);
5679f8829a4aSRandall Stewart 	/* Arrange so a_rwnd does NOT change */
5680f8829a4aSRandall Stewart 	a_rwnd = stcb->asoc.peers_rwnd + stcb->asoc.total_flight;
5681f8829a4aSRandall Stewart 
5682f8829a4aSRandall Stewart 	/* Now call the express sack handling */
5683f8829a4aSRandall Stewart 	sctp_express_handle_sack(stcb, cum_ack, a_rwnd, 0, abort_flag);
5684f8829a4aSRandall Stewart }
5685f8829a4aSRandall Stewart 
5686f8829a4aSRandall Stewart static void
5687f8829a4aSRandall Stewart sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
5688f8829a4aSRandall Stewart     struct sctp_stream_in *strmin)
5689f8829a4aSRandall Stewart {
5690f8829a4aSRandall Stewart 	struct sctp_queued_to_read *ctl, *nctl;
5691f8829a4aSRandall Stewart 	struct sctp_association *asoc;
5692f8829a4aSRandall Stewart 	int tt;
5693f8829a4aSRandall Stewart 
5694830d754dSRandall Stewart 	/* EY -used to calculate nr_gap information */
5695830d754dSRandall Stewart 	uint32_t nr_tsn, nr_gap;
5696830d754dSRandall Stewart 
5697f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
5698f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered;
5699f8829a4aSRandall Stewart 	/*
5700f8829a4aSRandall Stewart 	 * First deliver anything prior to and including the stream no that
5701f8829a4aSRandall Stewart 	 * came in
5702f8829a4aSRandall Stewart 	 */
5703f8829a4aSRandall Stewart 	ctl = TAILQ_FIRST(&strmin->inqueue);
5704f8829a4aSRandall Stewart 	while (ctl) {
5705f8829a4aSRandall Stewart 		nctl = TAILQ_NEXT(ctl, next);
5706f8829a4aSRandall Stewart 		if (compare_with_wrap(tt, ctl->sinfo_ssn, MAX_SEQ) ||
5707f8829a4aSRandall Stewart 		    (tt == ctl->sinfo_ssn)) {
5708f8829a4aSRandall Stewart 			/* this is deliverable now */
5709f8829a4aSRandall Stewart 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5710f8829a4aSRandall Stewart 			/* subtract pending on streams */
5711f8829a4aSRandall Stewart 			asoc->size_on_all_streams -= ctl->length;
5712f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_all_streams);
5713f8829a4aSRandall Stewart 			/* deliver it to at least the delivery-q */
5714f8829a4aSRandall Stewart 			if (stcb->sctp_socket) {
5715830d754dSRandall Stewart 				/* EY need the tsn info for calculating nr */
5716830d754dSRandall Stewart 				nr_tsn = ctl->sinfo_tsn;
5717f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
5718f8829a4aSRandall Stewart 				    ctl,
5719ceaad40aSRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
5720830d754dSRandall Stewart 				/*
5721830d754dSRandall Stewart 				 * EY this is the chunk that should be
5722830d754dSRandall Stewart 				 * tagged nr gapped calculate the gap and
5723830d754dSRandall Stewart 				 * such then tag this TSN nr
5724830d754dSRandall Stewart 				 * chk->rec.data.TSN_seq
5725830d754dSRandall Stewart 				 */
5726830d754dSRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
5727830d754dSRandall Stewart 
5728830d754dSRandall Stewart 					if (nr_tsn >= asoc->nr_mapping_array_base_tsn) {
5729830d754dSRandall Stewart 						nr_gap = nr_tsn - asoc->nr_mapping_array_base_tsn;
5730830d754dSRandall Stewart 					} else {
5731830d754dSRandall Stewart 						nr_gap = (MAX_TSN - asoc->nr_mapping_array_base_tsn) + nr_tsn + 1;
5732830d754dSRandall Stewart 					}
5733830d754dSRandall Stewart 					if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
5734830d754dSRandall Stewart 					    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
5735830d754dSRandall Stewart 						/*
5736830d754dSRandall Stewart 						 * EY These should never
5737830d754dSRandall Stewart 						 * happen- explained before
5738830d754dSRandall Stewart 						 */
5739830d754dSRandall Stewart 					} else {
5740830d754dSRandall Stewart 						SCTP_TCB_LOCK_ASSERT(stcb);
5741830d754dSRandall Stewart 						SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
57428933fa13SRandall Stewart 						if (compare_with_wrap(nr_tsn,
57438933fa13SRandall Stewart 						    asoc->highest_tsn_inside_nr_map,
57448933fa13SRandall Stewart 						    MAX_TSN))
5745830d754dSRandall Stewart 							asoc->highest_tsn_inside_nr_map = nr_tsn;
5746830d754dSRandall Stewart 					}
5747830d754dSRandall Stewart 
5748830d754dSRandall Stewart 					if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, nr_gap))
5749830d754dSRandall Stewart 						/*
5750830d754dSRandall Stewart 						 * printf("In
5751830d754dSRandall Stewart 						 * sctp_kick_prsctp_reorder_q
5752830d754dSRandall Stewart 						 * ueue(7): Something wrong,
5753830d754dSRandall Stewart 						 * the TSN to be tagged"
5754830d754dSRandall Stewart 						 * "\nas NR is not even in
5755830d754dSRandall Stewart 						 * the mapping_array, or map
5756830d754dSRandall Stewart 						 * and nr_map are
5757830d754dSRandall Stewart 						 * inconsistent");
5758830d754dSRandall Stewart 						 */
5759830d754dSRandall Stewart 						/*
5760830d754dSRandall Stewart 						 * EY - not %100 sure about
5761830d754dSRandall Stewart 						 * the lock thing, don't
5762830d754dSRandall Stewart 						 * think its required
5763830d754dSRandall Stewart 						 */
5764830d754dSRandall Stewart 						/*
5765830d754dSRandall Stewart 						 * SCTP_TCB_LOCK_ASSERT(stcb)
5766830d754dSRandall Stewart 						 * ;
5767830d754dSRandall Stewart 						 */
5768830d754dSRandall Stewart 					{
5769830d754dSRandall Stewart 						/*
5770830d754dSRandall Stewart 						 * printf("\nCalculating an
5771830d754dSRandall Stewart 						 * nr_gap!!\nmapping_array_si
5772830d754dSRandall Stewart 						 * ze = %d
5773830d754dSRandall Stewart 						 * nr_mapping_array_size =
5774830d754dSRandall Stewart 						 * %d" "\nmapping_array_base
5775830d754dSRandall Stewart 						 * = %d
5776830d754dSRandall Stewart 						 * nr_mapping_array_base =
5777830d754dSRandall Stewart 						 * %d\nhighest_tsn_inside_map
5778830d754dSRandall Stewart 						 *  = %d"
5779830d754dSRandall Stewart 						 * "highest_tsn_inside_nr_map
5780830d754dSRandall Stewart 						 *  = %d\nTSN = %d nr_gap =
5781830d754dSRandall Stewart 						 * %d",asoc->mapping_array_si
5782830d754dSRandall Stewart 						 * ze,
5783830d754dSRandall Stewart 						 * asoc->nr_mapping_array_siz
5784830d754dSRandall Stewart 						 * e,
5785830d754dSRandall Stewart 						 * asoc->mapping_array_base_t
5786830d754dSRandall Stewart 						 * sn,
5787830d754dSRandall Stewart 						 * asoc->nr_mapping_array_bas
5788830d754dSRandall Stewart 						 * e_tsn,
5789830d754dSRandall Stewart 						 * asoc->highest_tsn_inside_m
5790830d754dSRandall Stewart 						 * ap,
5791830d754dSRandall Stewart 						 * asoc->highest_tsn_inside_n
5792830d754dSRandall Stewart 						 * r_map,tsn,nr_gap);
5793830d754dSRandall Stewart 						 */
5794830d754dSRandall Stewart 					}
5795830d754dSRandall Stewart 				}
5796f8829a4aSRandall Stewart 			}
5797f8829a4aSRandall Stewart 		} else {
5798f8829a4aSRandall Stewart 			/* no more delivery now. */
5799f8829a4aSRandall Stewart 			break;
5800f8829a4aSRandall Stewart 		}
5801f8829a4aSRandall Stewart 		ctl = nctl;
5802f8829a4aSRandall Stewart 	}
5803f8829a4aSRandall Stewart 	/*
5804f8829a4aSRandall Stewart 	 * now we must deliver things in queue the normal way  if any are
5805f8829a4aSRandall Stewart 	 * now ready.
5806f8829a4aSRandall Stewart 	 */
5807f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered + 1;
5808f8829a4aSRandall Stewart 	ctl = TAILQ_FIRST(&strmin->inqueue);
5809f8829a4aSRandall Stewart 	while (ctl) {
5810f8829a4aSRandall Stewart 		nctl = TAILQ_NEXT(ctl, next);
5811f8829a4aSRandall Stewart 		if (tt == ctl->sinfo_ssn) {
5812f8829a4aSRandall Stewart 			/* this is deliverable now */
5813f8829a4aSRandall Stewart 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5814f8829a4aSRandall Stewart 			/* subtract pending on streams */
5815f8829a4aSRandall Stewart 			asoc->size_on_all_streams -= ctl->length;
5816f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_all_streams);
5817f8829a4aSRandall Stewart 			/* deliver it to at least the delivery-q */
5818f8829a4aSRandall Stewart 			strmin->last_sequence_delivered = ctl->sinfo_ssn;
5819f8829a4aSRandall Stewart 			if (stcb->sctp_socket) {
5820830d754dSRandall Stewart 				/* EY */
5821830d754dSRandall Stewart 				nr_tsn = ctl->sinfo_tsn;
5822f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
5823f8829a4aSRandall Stewart 				    ctl,
5824ceaad40aSRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
5825830d754dSRandall Stewart 				/*
5826830d754dSRandall Stewart 				 * EY this is the chunk that should be
5827830d754dSRandall Stewart 				 * tagged nr gapped calculate the gap and
5828830d754dSRandall Stewart 				 * such then tag this TSN nr
5829830d754dSRandall Stewart 				 * chk->rec.data.TSN_seq
5830830d754dSRandall Stewart 				 */
5831830d754dSRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
5832830d754dSRandall Stewart 
5833830d754dSRandall Stewart 					if (nr_tsn >= asoc->nr_mapping_array_base_tsn) {
5834830d754dSRandall Stewart 						nr_gap = nr_tsn - asoc->nr_mapping_array_base_tsn;
5835830d754dSRandall Stewart 					} else {
5836830d754dSRandall Stewart 						nr_gap = (MAX_TSN - asoc->nr_mapping_array_base_tsn) + nr_tsn + 1;
5837830d754dSRandall Stewart 					}
5838830d754dSRandall Stewart 					if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
5839830d754dSRandall Stewart 					    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
5840830d754dSRandall Stewart 						/*
5841830d754dSRandall Stewart 						 * EY These should never
5842830d754dSRandall Stewart 						 * happen, explained before
5843830d754dSRandall Stewart 						 */
5844830d754dSRandall Stewart 					} else {
5845830d754dSRandall Stewart 						SCTP_TCB_LOCK_ASSERT(stcb);
5846830d754dSRandall Stewart 						SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
58478933fa13SRandall Stewart 						if (compare_with_wrap(nr_tsn, asoc->highest_tsn_inside_nr_map,
58488933fa13SRandall Stewart 						    MAX_TSN))
5849830d754dSRandall Stewart 							asoc->highest_tsn_inside_nr_map = nr_tsn;
5850830d754dSRandall Stewart 					}
5851830d754dSRandall Stewart 
5852830d754dSRandall Stewart 
5853830d754dSRandall Stewart 					if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, nr_gap))
5854830d754dSRandall Stewart 						/*
5855830d754dSRandall Stewart 						 * printf("In
5856830d754dSRandall Stewart 						 * sctp_kick_prsctp_reorder_q
5857830d754dSRandall Stewart 						 * ueue(8): Something wrong,
5858830d754dSRandall Stewart 						 * the TSN to be tagged"
5859830d754dSRandall Stewart 						 * "\nas NR is not even in
5860830d754dSRandall Stewart 						 * the mapping_array, or map
5861830d754dSRandall Stewart 						 * and nr_map are
5862830d754dSRandall Stewart 						 * inconsistent");
5863830d754dSRandall Stewart 						 */
5864830d754dSRandall Stewart 						/*
5865830d754dSRandall Stewart 						 * EY - not %100 sure about
5866830d754dSRandall Stewart 						 * the lock thing, don't
5867830d754dSRandall Stewart 						 * think its required
5868830d754dSRandall Stewart 						 */
5869830d754dSRandall Stewart 						/*
5870830d754dSRandall Stewart 						 * SCTP_TCB_LOCK_ASSERT(stcb)
5871830d754dSRandall Stewart 						 * ;
5872830d754dSRandall Stewart 						 */
5873830d754dSRandall Stewart 					{
5874830d754dSRandall Stewart 						/*
5875830d754dSRandall Stewart 						 * printf("\nCalculating an
5876830d754dSRandall Stewart 						 * nr_gap!!\nmapping_array_si
5877830d754dSRandall Stewart 						 * ze = %d
5878830d754dSRandall Stewart 						 * nr_mapping_array_size =
5879830d754dSRandall Stewart 						 * %d" "\nmapping_array_base
5880830d754dSRandall Stewart 						 * = %d
5881830d754dSRandall Stewart 						 * nr_mapping_array_base =
5882830d754dSRandall Stewart 						 * %d\nhighest_tsn_inside_map
5883830d754dSRandall Stewart 						 *  = %d"
5884830d754dSRandall Stewart 						 * "highest_tsn_inside_nr_map
5885830d754dSRandall Stewart 						 *  = %d\nTSN = %d nr_gap =
5886830d754dSRandall Stewart 						 * %d",asoc->mapping_array_si
5887830d754dSRandall Stewart 						 * ze,
5888830d754dSRandall Stewart 						 * asoc->nr_mapping_array_siz
5889830d754dSRandall Stewart 						 * e,
5890830d754dSRandall Stewart 						 * asoc->mapping_array_base_t
5891830d754dSRandall Stewart 						 * sn,
5892830d754dSRandall Stewart 						 * asoc->nr_mapping_array_bas
5893830d754dSRandall Stewart 						 * e_tsn,
5894830d754dSRandall Stewart 						 * asoc->highest_tsn_inside_m
5895830d754dSRandall Stewart 						 * ap,
5896830d754dSRandall Stewart 						 * asoc->highest_tsn_inside_n
5897830d754dSRandall Stewart 						 * r_map,tsn,nr_gap);
5898830d754dSRandall Stewart 						 */
5899830d754dSRandall Stewart 					}
5900830d754dSRandall Stewart 				}
5901f8829a4aSRandall Stewart 			}
5902f8829a4aSRandall Stewart 			tt = strmin->last_sequence_delivered + 1;
5903f8829a4aSRandall Stewart 		} else {
5904f8829a4aSRandall Stewart 			break;
5905f8829a4aSRandall Stewart 		}
5906f8829a4aSRandall Stewart 		ctl = nctl;
5907f8829a4aSRandall Stewart 	}
5908f8829a4aSRandall Stewart }
5909f8829a4aSRandall Stewart 
59108933fa13SRandall Stewart static void
59118933fa13SRandall Stewart sctp_flush_reassm_for_str_seq(struct sctp_tcb *stcb,
59128933fa13SRandall Stewart     struct sctp_association *asoc,
59138933fa13SRandall Stewart     uint16_t stream, uint16_t seq)
59148933fa13SRandall Stewart {
59158933fa13SRandall Stewart 	struct sctp_tmit_chunk *chk, *at;
59168933fa13SRandall Stewart 
59178933fa13SRandall Stewart 	if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
59188933fa13SRandall Stewart 		/* For each one on here see if we need to toss it */
59198933fa13SRandall Stewart 		/*
59208933fa13SRandall Stewart 		 * For now large messages held on the reasmqueue that are
59218933fa13SRandall Stewart 		 * complete will be tossed too. We could in theory do more
59228933fa13SRandall Stewart 		 * work to spin through and stop after dumping one msg aka
59238933fa13SRandall Stewart 		 * seeing the start of a new msg at the head, and call the
59248933fa13SRandall Stewart 		 * delivery function... to see if it can be delivered... But
59258933fa13SRandall Stewart 		 * for now we just dump everything on the queue.
59268933fa13SRandall Stewart 		 */
59278933fa13SRandall Stewart 		chk = TAILQ_FIRST(&asoc->reasmqueue);
59288933fa13SRandall Stewart 		while (chk) {
59298933fa13SRandall Stewart 			at = TAILQ_NEXT(chk, sctp_next);
59308933fa13SRandall Stewart 			if (chk->rec.data.stream_number != stream) {
59318933fa13SRandall Stewart 				chk = at;
59328933fa13SRandall Stewart 				continue;
59338933fa13SRandall Stewart 			}
59348933fa13SRandall Stewart 			if (chk->rec.data.stream_seq == seq) {
59358933fa13SRandall Stewart 				/* It needs to be tossed */
59368933fa13SRandall Stewart 				TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
59378933fa13SRandall Stewart 				if (compare_with_wrap(chk->rec.data.TSN_seq,
59388933fa13SRandall Stewart 				    asoc->tsn_last_delivered, MAX_TSN)) {
59398933fa13SRandall Stewart 					asoc->tsn_last_delivered =
59408933fa13SRandall Stewart 					    chk->rec.data.TSN_seq;
59418933fa13SRandall Stewart 					asoc->str_of_pdapi =
59428933fa13SRandall Stewart 					    chk->rec.data.stream_number;
59438933fa13SRandall Stewart 					asoc->ssn_of_pdapi =
59448933fa13SRandall Stewart 					    chk->rec.data.stream_seq;
59458933fa13SRandall Stewart 					asoc->fragment_flags =
59468933fa13SRandall Stewart 					    chk->rec.data.rcv_flags;
59478933fa13SRandall Stewart 				}
59488933fa13SRandall Stewart 				asoc->size_on_reasm_queue -= chk->send_size;
59498933fa13SRandall Stewart 				sctp_ucount_decr(asoc->cnt_on_reasm_queue);
59508933fa13SRandall Stewart 
59518933fa13SRandall Stewart 				/* Clear up any stream problem */
59528933fa13SRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) !=
59538933fa13SRandall Stewart 				    SCTP_DATA_UNORDERED &&
59548933fa13SRandall Stewart 				    (compare_with_wrap(chk->rec.data.stream_seq,
59558933fa13SRandall Stewart 				    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered,
59568933fa13SRandall Stewart 				    MAX_SEQ))) {
59578933fa13SRandall Stewart 					/*
59588933fa13SRandall Stewart 					 * We must dump forward this streams
59598933fa13SRandall Stewart 					 * sequence number if the chunk is
59608933fa13SRandall Stewart 					 * not unordered that is being
59618933fa13SRandall Stewart 					 * skipped. There is a chance that
59628933fa13SRandall Stewart 					 * if the peer does not include the
59638933fa13SRandall Stewart 					 * last fragment in its FWD-TSN we
59648933fa13SRandall Stewart 					 * WILL have a problem here since
59658933fa13SRandall Stewart 					 * you would have a partial chunk in
59668933fa13SRandall Stewart 					 * queue that may not be
59678933fa13SRandall Stewart 					 * deliverable. Also if a Partial
59688933fa13SRandall Stewart 					 * delivery API as started the user
59698933fa13SRandall Stewart 					 * may get a partial chunk. The next
59708933fa13SRandall Stewart 					 * read returning a new chunk...
59718933fa13SRandall Stewart 					 * really ugly but I see no way
59728933fa13SRandall Stewart 					 * around it! Maybe a notify??
59738933fa13SRandall Stewart 					 */
59748933fa13SRandall Stewart 					asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered =
59758933fa13SRandall Stewart 					    chk->rec.data.stream_seq;
59768933fa13SRandall Stewart 				}
59778933fa13SRandall Stewart 				if (chk->data) {
59788933fa13SRandall Stewart 					sctp_m_freem(chk->data);
59798933fa13SRandall Stewart 					chk->data = NULL;
59808933fa13SRandall Stewart 				}
59818933fa13SRandall Stewart 				sctp_free_a_chunk(stcb, chk);
59828933fa13SRandall Stewart 			} else if (compare_with_wrap(chk->rec.data.stream_seq, seq, MAX_SEQ)) {
59838933fa13SRandall Stewart 				/*
59848933fa13SRandall Stewart 				 * If the stream_seq is > than the purging
59858933fa13SRandall Stewart 				 * one, we are done
59868933fa13SRandall Stewart 				 */
59878933fa13SRandall Stewart 				break;
59888933fa13SRandall Stewart 			}
59898933fa13SRandall Stewart 			chk = at;
59908933fa13SRandall Stewart 		}
59918933fa13SRandall Stewart 	}
59928933fa13SRandall Stewart }
59938933fa13SRandall Stewart 
59948933fa13SRandall Stewart 
5995f8829a4aSRandall Stewart void
5996f8829a4aSRandall Stewart sctp_handle_forward_tsn(struct sctp_tcb *stcb,
5997671d309cSRandall Stewart     struct sctp_forward_tsn_chunk *fwd, int *abort_flag, struct mbuf *m, int offset)
5998f8829a4aSRandall Stewart {
5999f8829a4aSRandall Stewart 	/*
6000f8829a4aSRandall Stewart 	 * ISSUES that MUST be fixed for ECN! When we are the sender of the
6001f8829a4aSRandall Stewart 	 * forward TSN, when the SACK comes back that acknowledges the
6002f8829a4aSRandall Stewart 	 * FWD-TSN we must reset the NONCE sum to match correctly. This will
6003f8829a4aSRandall Stewart 	 * get quite tricky since we may have sent more data interveneing
6004f8829a4aSRandall Stewart 	 * and must carefully account for what the SACK says on the nonce
6005f8829a4aSRandall Stewart 	 * and any gaps that are reported. This work will NOT be done here,
6006f8829a4aSRandall Stewart 	 * but I note it here since it is really related to PR-SCTP and
6007f8829a4aSRandall Stewart 	 * FWD-TSN's
6008f8829a4aSRandall Stewart 	 */
6009f8829a4aSRandall Stewart 
6010f8829a4aSRandall Stewart 	/* The pr-sctp fwd tsn */
6011f8829a4aSRandall Stewart 	/*
6012f8829a4aSRandall Stewart 	 * here we will perform all the data receiver side steps for
6013f8829a4aSRandall Stewart 	 * processing FwdTSN, as required in by pr-sctp draft:
6014f8829a4aSRandall Stewart 	 *
6015f8829a4aSRandall Stewart 	 * Assume we get FwdTSN(x):
6016f8829a4aSRandall Stewart 	 *
6017f8829a4aSRandall Stewart 	 * 1) update local cumTSN to x 2) try to further advance cumTSN to x +
6018f8829a4aSRandall Stewart 	 * others we have 3) examine and update re-ordering queue on
6019f8829a4aSRandall Stewart 	 * pr-in-streams 4) clean up re-assembly queue 5) Send a sack to
6020f8829a4aSRandall Stewart 	 * report where we are.
6021f8829a4aSRandall Stewart 	 */
6022f8829a4aSRandall Stewart 	struct sctp_association *asoc;
60232afb3e84SRandall Stewart 	uint32_t new_cum_tsn, gap;
60248933fa13SRandall Stewart 	unsigned int i, fwd_sz, cumack_set_flag, m_size;
60258933fa13SRandall Stewart 	uint32_t str_seq;
6026f8829a4aSRandall Stewart 	struct sctp_stream_in *strm;
6027f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk, *at;
60288933fa13SRandall Stewart 	struct sctp_queued_to_read *ctl, *sv;
6029f8829a4aSRandall Stewart 
6030f8829a4aSRandall Stewart 	cumack_set_flag = 0;
6031f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
6032f8829a4aSRandall Stewart 	if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
6033ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1,
6034ad81507eSRandall Stewart 		    "Bad size too small/big fwd-tsn\n");
6035f8829a4aSRandall Stewart 		return;
6036f8829a4aSRandall Stewart 	}
6037f8829a4aSRandall Stewart 	m_size = (stcb->asoc.mapping_array_size << 3);
6038f8829a4aSRandall Stewart 	/*************************************************************/
6039f8829a4aSRandall Stewart 	/* 1. Here we update local cumTSN and shift the bitmap array */
6040f8829a4aSRandall Stewart 	/*************************************************************/
6041f8829a4aSRandall Stewart 	new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
6042f8829a4aSRandall Stewart 
6043f8829a4aSRandall Stewart 	if (compare_with_wrap(asoc->cumulative_tsn, new_cum_tsn, MAX_TSN) ||
6044f8829a4aSRandall Stewart 	    asoc->cumulative_tsn == new_cum_tsn) {
6045f8829a4aSRandall Stewart 		/* Already got there ... */
6046f8829a4aSRandall Stewart 		return;
6047f8829a4aSRandall Stewart 	}
6048f8829a4aSRandall Stewart 	if (compare_with_wrap(new_cum_tsn, asoc->highest_tsn_inside_map,
6049f8829a4aSRandall Stewart 	    MAX_TSN)) {
6050f8829a4aSRandall Stewart 		asoc->highest_tsn_inside_map = new_cum_tsn;
6051830d754dSRandall Stewart 		/* EY nr_mapping_array version of the above */
6052830d754dSRandall Stewart 		/*
6053830d754dSRandall Stewart 		 * if(SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) &&
6054830d754dSRandall Stewart 		 * asoc->peer_supports_nr_sack)
6055830d754dSRandall Stewart 		 */
6056830d754dSRandall Stewart 		asoc->highest_tsn_inside_nr_map = new_cum_tsn;
6057b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
6058f8829a4aSRandall Stewart 			sctp_log_map(0, 0, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
605980fefe0aSRandall Stewart 		}
6060f8829a4aSRandall Stewart 	}
6061f8829a4aSRandall Stewart 	/*
6062f8829a4aSRandall Stewart 	 * now we know the new TSN is more advanced, let's find the actual
6063f8829a4aSRandall Stewart 	 * gap
6064f8829a4aSRandall Stewart 	 */
6065f8829a4aSRandall Stewart 	if ((compare_with_wrap(new_cum_tsn, asoc->mapping_array_base_tsn,
6066f8829a4aSRandall Stewart 	    MAX_TSN)) ||
6067f8829a4aSRandall Stewart 	    (new_cum_tsn == asoc->mapping_array_base_tsn)) {
6068f8829a4aSRandall Stewart 		gap = new_cum_tsn - asoc->mapping_array_base_tsn;
6069f8829a4aSRandall Stewart 	} else {
6070f8829a4aSRandall Stewart 		/* try to prevent underflow here */
6071f8829a4aSRandall Stewart 		gap = new_cum_tsn + (MAX_TSN - asoc->mapping_array_base_tsn) + 1;
6072f8829a4aSRandall Stewart 	}
6073f8829a4aSRandall Stewart 
60742afb3e84SRandall Stewart 	if (gap >= m_size) {
6075b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
6076c4739e2fSRandall Stewart 			sctp_log_map(0, 0, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
6077c4739e2fSRandall Stewart 		}
6078f8829a4aSRandall Stewart 		if ((long)gap > sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv)) {
607917205eccSRandall Stewart 			struct mbuf *oper;
608017205eccSRandall Stewart 
6081f8829a4aSRandall Stewart 			/*
6082f8829a4aSRandall Stewart 			 * out of range (of single byte chunks in the rwnd I
608317205eccSRandall Stewart 			 * give out). This must be an attacker.
6084f8829a4aSRandall Stewart 			 */
608517205eccSRandall Stewart 			*abort_flag = 1;
608617205eccSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
608717205eccSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
608817205eccSRandall Stewart 			if (oper) {
608917205eccSRandall Stewart 				struct sctp_paramhdr *ph;
609017205eccSRandall Stewart 				uint32_t *ippp;
609117205eccSRandall Stewart 
609217205eccSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
609317205eccSRandall Stewart 				    (sizeof(uint32_t) * 3);
609417205eccSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
609517205eccSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
609617205eccSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
609717205eccSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
609817205eccSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_33);
609917205eccSRandall Stewart 				ippp++;
610017205eccSRandall Stewart 				*ippp = asoc->highest_tsn_inside_map;
610117205eccSRandall Stewart 				ippp++;
610217205eccSRandall Stewart 				*ippp = new_cum_tsn;
610317205eccSRandall Stewart 			}
610417205eccSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_33;
610517205eccSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb,
6106ceaad40aSRandall Stewart 			    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
6107f8829a4aSRandall Stewart 			return;
6108f8829a4aSRandall Stewart 		}
6109207304d4SRandall Stewart 		SCTP_STAT_INCR(sctps_fwdtsn_map_over);
61102afb3e84SRandall Stewart 		memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
6111f8829a4aSRandall Stewart 		cumack_set_flag = 1;
61122afb3e84SRandall Stewart 		asoc->mapping_array_base_tsn = new_cum_tsn + 1;
61132afb3e84SRandall Stewart 		asoc->cumulative_tsn = asoc->highest_tsn_inside_map = new_cum_tsn;
6114830d754dSRandall Stewart 		/* EY - nr_sack: nr_mapping_array version of the above */
6115830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
6116830d754dSRandall Stewart 			memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.nr_mapping_array_size);
6117830d754dSRandall Stewart 			asoc->nr_mapping_array_base_tsn = new_cum_tsn + 1;
6118830d754dSRandall Stewart 			asoc->highest_tsn_inside_nr_map = new_cum_tsn;
6119830d754dSRandall Stewart 			if (asoc->nr_mapping_array_size != asoc->mapping_array_size) {
6120830d754dSRandall Stewart 				/*
6121830d754dSRandall Stewart 				 * printf("IN sctp_handle_forward_tsn:
6122830d754dSRandall Stewart 				 * Something is wrong the size of" "map and
6123830d754dSRandall Stewart 				 * nr_map should be equal!")
6124830d754dSRandall Stewart 				 */ ;
6125830d754dSRandall Stewart 			}
6126830d754dSRandall Stewart 		}
6127b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
6128f8829a4aSRandall Stewart 			sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
612980fefe0aSRandall Stewart 		}
6130f8829a4aSRandall Stewart 		asoc->last_echo_tsn = asoc->highest_tsn_inside_map;
61312afb3e84SRandall Stewart 	} else {
61322afb3e84SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
61332afb3e84SRandall Stewart 		for (i = 0; i <= gap; i++) {
61342afb3e84SRandall Stewart 			SCTP_SET_TSN_PRESENT(asoc->mapping_array, i);
61358933fa13SRandall Stewart 			/*
61368933fa13SRandall Stewart 			 * EY if drain is off then every gap-ack is an
61378933fa13SRandall Stewart 			 * nr-gap-ack
61388933fa13SRandall Stewart 			 */
61398933fa13SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack
61408933fa13SRandall Stewart 			    && SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
61418933fa13SRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, i);
61428933fa13SRandall Stewart 			}
6143f8829a4aSRandall Stewart 		}
61442afb3e84SRandall Stewart 		/*
61452afb3e84SRandall Stewart 		 * Now after marking all, slide thing forward but no sack
61462afb3e84SRandall Stewart 		 * please.
61472afb3e84SRandall Stewart 		 */
61482afb3e84SRandall Stewart 		sctp_sack_check(stcb, 0, 0, abort_flag);
61492afb3e84SRandall Stewart 		if (*abort_flag)
61502afb3e84SRandall Stewart 			return;
61512afb3e84SRandall Stewart 	}
6152f8829a4aSRandall Stewart 	/*************************************************************/
6153f8829a4aSRandall Stewart 	/* 2. Clear up re-assembly queue                             */
6154f8829a4aSRandall Stewart 	/*************************************************************/
6155f8829a4aSRandall Stewart 	/*
6156f8829a4aSRandall Stewart 	 * First service it if pd-api is up, just in case we can progress it
6157f8829a4aSRandall Stewart 	 * forward
6158f8829a4aSRandall Stewart 	 */
6159f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
6160f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
6161f8829a4aSRandall Stewart 	}
6162f8829a4aSRandall Stewart 	if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
6163f8829a4aSRandall Stewart 		/* For each one on here see if we need to toss it */
6164f8829a4aSRandall Stewart 		/*
6165f8829a4aSRandall Stewart 		 * For now large messages held on the reasmqueue that are
6166f8829a4aSRandall Stewart 		 * complete will be tossed too. We could in theory do more
6167f8829a4aSRandall Stewart 		 * work to spin through and stop after dumping one msg aka
6168f8829a4aSRandall Stewart 		 * seeing the start of a new msg at the head, and call the
6169f8829a4aSRandall Stewart 		 * delivery function... to see if it can be delivered... But
6170f8829a4aSRandall Stewart 		 * for now we just dump everything on the queue.
6171f8829a4aSRandall Stewart 		 */
6172f8829a4aSRandall Stewart 		chk = TAILQ_FIRST(&asoc->reasmqueue);
6173f8829a4aSRandall Stewart 		while (chk) {
6174f8829a4aSRandall Stewart 			at = TAILQ_NEXT(chk, sctp_next);
61750c0982b8SRandall Stewart 			if ((compare_with_wrap(new_cum_tsn,
61760c0982b8SRandall Stewart 			    chk->rec.data.TSN_seq, MAX_TSN)) ||
61770c0982b8SRandall Stewart 			    (new_cum_tsn == chk->rec.data.TSN_seq)) {
6178f8829a4aSRandall Stewart 				/* It needs to be tossed */
6179f8829a4aSRandall Stewart 				TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
6180f8829a4aSRandall Stewart 				if (compare_with_wrap(chk->rec.data.TSN_seq,
6181f8829a4aSRandall Stewart 				    asoc->tsn_last_delivered, MAX_TSN)) {
6182f8829a4aSRandall Stewart 					asoc->tsn_last_delivered =
6183f8829a4aSRandall Stewart 					    chk->rec.data.TSN_seq;
6184f8829a4aSRandall Stewart 					asoc->str_of_pdapi =
6185f8829a4aSRandall Stewart 					    chk->rec.data.stream_number;
6186f8829a4aSRandall Stewart 					asoc->ssn_of_pdapi =
6187f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq;
6188f8829a4aSRandall Stewart 					asoc->fragment_flags =
6189f8829a4aSRandall Stewart 					    chk->rec.data.rcv_flags;
6190f8829a4aSRandall Stewart 				}
6191f8829a4aSRandall Stewart 				asoc->size_on_reasm_queue -= chk->send_size;
6192f8829a4aSRandall Stewart 				sctp_ucount_decr(asoc->cnt_on_reasm_queue);
6193f8829a4aSRandall Stewart 
6194f8829a4aSRandall Stewart 				/* Clear up any stream problem */
6195f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) !=
6196f8829a4aSRandall Stewart 				    SCTP_DATA_UNORDERED &&
6197f8829a4aSRandall Stewart 				    (compare_with_wrap(chk->rec.data.stream_seq,
6198f8829a4aSRandall Stewart 				    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered,
6199f8829a4aSRandall Stewart 				    MAX_SEQ))) {
6200f8829a4aSRandall Stewart 					/*
6201f8829a4aSRandall Stewart 					 * We must dump forward this streams
6202f8829a4aSRandall Stewart 					 * sequence number if the chunk is
6203f8829a4aSRandall Stewart 					 * not unordered that is being
6204f8829a4aSRandall Stewart 					 * skipped. There is a chance that
6205f8829a4aSRandall Stewart 					 * if the peer does not include the
6206f8829a4aSRandall Stewart 					 * last fragment in its FWD-TSN we
6207f8829a4aSRandall Stewart 					 * WILL have a problem here since
6208f8829a4aSRandall Stewart 					 * you would have a partial chunk in
6209f8829a4aSRandall Stewart 					 * queue that may not be
6210f8829a4aSRandall Stewart 					 * deliverable. Also if a Partial
6211f8829a4aSRandall Stewart 					 * delivery API as started the user
6212f8829a4aSRandall Stewart 					 * may get a partial chunk. The next
6213f8829a4aSRandall Stewart 					 * read returning a new chunk...
6214f8829a4aSRandall Stewart 					 * really ugly but I see no way
6215f8829a4aSRandall Stewart 					 * around it! Maybe a notify??
6216f8829a4aSRandall Stewart 					 */
6217f8829a4aSRandall Stewart 					asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered =
6218f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq;
6219f8829a4aSRandall Stewart 				}
6220f8829a4aSRandall Stewart 				if (chk->data) {
6221f8829a4aSRandall Stewart 					sctp_m_freem(chk->data);
6222f8829a4aSRandall Stewart 					chk->data = NULL;
6223f8829a4aSRandall Stewart 				}
6224f8829a4aSRandall Stewart 				sctp_free_a_chunk(stcb, chk);
6225f8829a4aSRandall Stewart 			} else {
6226f8829a4aSRandall Stewart 				/*
6227f8829a4aSRandall Stewart 				 * Ok we have gone beyond the end of the
62288933fa13SRandall Stewart 				 * fwd-tsn's mark.
6229f8829a4aSRandall Stewart 				 */
6230f8829a4aSRandall Stewart 				break;
6231f8829a4aSRandall Stewart 			}
6232f8829a4aSRandall Stewart 			chk = at;
6233f8829a4aSRandall Stewart 		}
6234f8829a4aSRandall Stewart 	}
62358933fa13SRandall Stewart 	/*******************************************************/
62368933fa13SRandall Stewart 	/* 3. Update the PR-stream re-ordering queues and fix  */
62378933fa13SRandall Stewart 	/* delivery issues as needed.                       */
62388933fa13SRandall Stewart 	/*******************************************************/
6239f8829a4aSRandall Stewart 	fwd_sz -= sizeof(*fwd);
6240671d309cSRandall Stewart 	if (m && fwd_sz) {
6241f8829a4aSRandall Stewart 		/* New method. */
6242d61a0ae0SRandall Stewart 		unsigned int num_str;
6243671d309cSRandall Stewart 		struct sctp_strseq *stseq, strseqbuf;
6244671d309cSRandall Stewart 
6245671d309cSRandall Stewart 		offset += sizeof(*fwd);
6246f8829a4aSRandall Stewart 
62478933fa13SRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
6248f8829a4aSRandall Stewart 		num_str = fwd_sz / sizeof(struct sctp_strseq);
6249f8829a4aSRandall Stewart 		for (i = 0; i < num_str; i++) {
6250f8829a4aSRandall Stewart 			uint16_t st;
6251f8829a4aSRandall Stewart 
6252671d309cSRandall Stewart 			stseq = (struct sctp_strseq *)sctp_m_getptr(m, offset,
6253671d309cSRandall Stewart 			    sizeof(struct sctp_strseq),
6254671d309cSRandall Stewart 			    (uint8_t *) & strseqbuf);
6255671d309cSRandall Stewart 			offset += sizeof(struct sctp_strseq);
62562afb3e84SRandall Stewart 			if (stseq == NULL) {
6257671d309cSRandall Stewart 				break;
62582afb3e84SRandall Stewart 			}
6259f8829a4aSRandall Stewart 			/* Convert */
6260851b7298SRandall Stewart 			st = ntohs(stseq->stream);
6261851b7298SRandall Stewart 			stseq->stream = st;
6262851b7298SRandall Stewart 			st = ntohs(stseq->sequence);
6263851b7298SRandall Stewart 			stseq->sequence = st;
62648933fa13SRandall Stewart 
6265f8829a4aSRandall Stewart 			/* now process */
62668933fa13SRandall Stewart 
62678933fa13SRandall Stewart 			/*
62688933fa13SRandall Stewart 			 * Ok we now look for the stream/seq on the read
62698933fa13SRandall Stewart 			 * queue where its not all delivered. If we find it
62708933fa13SRandall Stewart 			 * we transmute the read entry into a PDI_ABORTED.
62718933fa13SRandall Stewart 			 */
6272851b7298SRandall Stewart 			if (stseq->stream >= asoc->streamincnt) {
62732afb3e84SRandall Stewart 				/* screwed up streams, stop!  */
62742afb3e84SRandall Stewart 				break;
6275f8829a4aSRandall Stewart 			}
62768933fa13SRandall Stewart 			if ((asoc->str_of_pdapi == stseq->stream) &&
62778933fa13SRandall Stewart 			    (asoc->ssn_of_pdapi == stseq->sequence)) {
62788933fa13SRandall Stewart 				/*
62798933fa13SRandall Stewart 				 * If this is the one we were partially
62808933fa13SRandall Stewart 				 * delivering now then we no longer are.
62818933fa13SRandall Stewart 				 * Note this will change with the reassembly
62828933fa13SRandall Stewart 				 * re-write.
62838933fa13SRandall Stewart 				 */
62848933fa13SRandall Stewart 				asoc->fragmented_delivery_inprogress = 0;
62858933fa13SRandall Stewart 			}
62868933fa13SRandall Stewart 			sctp_flush_reassm_for_str_seq(stcb, asoc, stseq->stream, stseq->sequence);
62878933fa13SRandall Stewart 			TAILQ_FOREACH(ctl, &stcb->sctp_ep->read_queue, next) {
62888933fa13SRandall Stewart 				if ((ctl->sinfo_stream == stseq->stream) &&
62898933fa13SRandall Stewart 				    (ctl->sinfo_ssn == stseq->sequence)) {
62908933fa13SRandall Stewart 					str_seq = (stseq->stream << 16) | stseq->sequence;
62918933fa13SRandall Stewart 					ctl->end_added = 1;
62928933fa13SRandall Stewart 					ctl->pdapi_aborted = 1;
62938933fa13SRandall Stewart 					sv = stcb->asoc.control_pdapi;
62948933fa13SRandall Stewart 					stcb->asoc.control_pdapi = ctl;
62958933fa13SRandall Stewart 					sctp_notify_partial_delivery_indication(stcb,
62968933fa13SRandall Stewart 					    SCTP_PARTIAL_DELIVERY_ABORTED,
62978933fa13SRandall Stewart 					    SCTP_HOLDS_LOCK,
62988933fa13SRandall Stewart 					    str_seq);
62998933fa13SRandall Stewart 					stcb->asoc.control_pdapi = sv;
63008933fa13SRandall Stewart 					break;
63018933fa13SRandall Stewart 				} else if ((ctl->sinfo_stream == stseq->stream) &&
63028933fa13SRandall Stewart 				    (compare_with_wrap(ctl->sinfo_ssn, stseq->sequence, MAX_SEQ))) {
63038933fa13SRandall Stewart 					/* We are past our victim SSN */
63048933fa13SRandall Stewart 					break;
63058933fa13SRandall Stewart 				}
63068933fa13SRandall Stewart 			}
6307851b7298SRandall Stewart 			strm = &asoc->strmin[stseq->stream];
6308851b7298SRandall Stewart 			if (compare_with_wrap(stseq->sequence,
6309f8829a4aSRandall Stewart 			    strm->last_sequence_delivered, MAX_SEQ)) {
6310f8829a4aSRandall Stewart 				/* Update the sequence number */
6311f8829a4aSRandall Stewart 				strm->last_sequence_delivered =
6312851b7298SRandall Stewart 				    stseq->sequence;
6313f8829a4aSRandall Stewart 			}
6314f8829a4aSRandall Stewart 			/* now kick the stream the new way */
631504ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
6316f8829a4aSRandall Stewart 			sctp_kick_prsctp_reorder_queue(stcb, strm);
6317f8829a4aSRandall Stewart 		}
63188933fa13SRandall Stewart 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
6319f8829a4aSRandall Stewart 	}
6320139bc87fSRandall Stewart 	if (TAILQ_FIRST(&asoc->reasmqueue)) {
6321139bc87fSRandall Stewart 		/* now lets kick out and check for more fragmented delivery */
632204ee05e8SRandall Stewart 		/* sa_ignore NO_NULL_CHK */
6323139bc87fSRandall Stewart 		sctp_deliver_reasm_check(stcb, &stcb->asoc);
6324139bc87fSRandall Stewart 	}
6325f8829a4aSRandall Stewart }
6326830d754dSRandall Stewart 
6327830d754dSRandall Stewart /* EY fully identical to sctp_express_handle_sack, duplicated for only naming convention */
6328830d754dSRandall Stewart void
6329830d754dSRandall Stewart sctp_express_handle_nr_sack(struct sctp_tcb *stcb, uint32_t cumack,
6330830d754dSRandall Stewart     uint32_t rwnd, int nonce_sum_flag, int *abort_now)
6331830d754dSRandall Stewart {
6332830d754dSRandall Stewart 	struct sctp_nets *net;
6333830d754dSRandall Stewart 	struct sctp_association *asoc;
6334830d754dSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
6335830d754dSRandall Stewart 	uint32_t old_rwnd;
6336830d754dSRandall Stewart 	int win_probe_recovery = 0;
6337830d754dSRandall Stewart 	int win_probe_recovered = 0;
6338830d754dSRandall Stewart 	int j, done_once = 0;
6339830d754dSRandall Stewart 
6340830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
6341830d754dSRandall Stewart 		sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
6342830d754dSRandall Stewart 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
6343830d754dSRandall Stewart 	}
6344830d754dSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
6345830d754dSRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
6346830d754dSRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
6347830d754dSRandall Stewart 	stcb->asoc.cumack_log_at++;
6348830d754dSRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
6349830d754dSRandall Stewart 		stcb->asoc.cumack_log_at = 0;
6350830d754dSRandall Stewart 	}
6351830d754dSRandall Stewart #endif
6352830d754dSRandall Stewart 	asoc = &stcb->asoc;
6353830d754dSRandall Stewart 	old_rwnd = asoc->peers_rwnd;
6354830d754dSRandall Stewart 	if (compare_with_wrap(asoc->last_acked_seq, cumack, MAX_TSN)) {
6355830d754dSRandall Stewart 		/* old ack */
6356830d754dSRandall Stewart 		return;
6357830d754dSRandall Stewart 	} else if (asoc->last_acked_seq == cumack) {
6358830d754dSRandall Stewart 		/* Window update sack */
6359830d754dSRandall Stewart 		asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
6360830d754dSRandall Stewart 		    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
6361830d754dSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6362830d754dSRandall Stewart 			/* SWS sender side engages */
6363830d754dSRandall Stewart 			asoc->peers_rwnd = 0;
6364830d754dSRandall Stewart 		}
6365830d754dSRandall Stewart 		if (asoc->peers_rwnd > old_rwnd) {
6366830d754dSRandall Stewart 			goto again;
6367830d754dSRandall Stewart 		}
6368830d754dSRandall Stewart 		return;
6369830d754dSRandall Stewart 	}
6370830d754dSRandall Stewart 	/* First setup for CC stuff */
6371830d754dSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6372830d754dSRandall Stewart 		net->prev_cwnd = net->cwnd;
6373830d754dSRandall Stewart 		net->net_ack = 0;
6374830d754dSRandall Stewart 		net->net_ack2 = 0;
6375830d754dSRandall Stewart 
6376830d754dSRandall Stewart 		/*
6377830d754dSRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
6378830d754dSRandall Stewart 		 * SACK processing
6379830d754dSRandall Stewart 		 */
6380830d754dSRandall Stewart 		net->new_pseudo_cumack = 0;
6381830d754dSRandall Stewart 		net->will_exit_fast_recovery = 0;
6382830d754dSRandall Stewart 	}
6383830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
6384830d754dSRandall Stewart 		uint32_t send_s;
6385830d754dSRandall Stewart 
6386830d754dSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
6387830d754dSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
6388830d754dSRandall Stewart 			    sctpchunk_listhead);
6389830d754dSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
6390830d754dSRandall Stewart 		} else {
6391830d754dSRandall Stewart 			send_s = asoc->sending_seq;
6392830d754dSRandall Stewart 		}
6393830d754dSRandall Stewart 		if ((cumack == send_s) ||
6394830d754dSRandall Stewart 		    compare_with_wrap(cumack, send_s, MAX_TSN)) {
6395830d754dSRandall Stewart #ifndef INVARIANTS
6396830d754dSRandall Stewart 			struct mbuf *oper;
6397830d754dSRandall Stewart 
6398830d754dSRandall Stewart #endif
6399830d754dSRandall Stewart #ifdef INVARIANTS
6400830d754dSRandall Stewart 			panic("Impossible sack 1");
6401830d754dSRandall Stewart #else
6402830d754dSRandall Stewart 			*abort_now = 1;
6403830d754dSRandall Stewart 			/* XXX */
6404830d754dSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
6405830d754dSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
6406830d754dSRandall Stewart 			if (oper) {
6407830d754dSRandall Stewart 				struct sctp_paramhdr *ph;
6408830d754dSRandall Stewart 				uint32_t *ippp;
6409830d754dSRandall Stewart 
6410830d754dSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
6411830d754dSRandall Stewart 				    sizeof(uint32_t);
6412830d754dSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
6413830d754dSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
6414830d754dSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
6415830d754dSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
6416830d754dSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
6417830d754dSRandall Stewart 			}
6418830d754dSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
6419830d754dSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
6420830d754dSRandall Stewart 			return;
6421830d754dSRandall Stewart #endif
6422830d754dSRandall Stewart 		}
6423830d754dSRandall Stewart 	}
6424830d754dSRandall Stewart 	asoc->this_sack_highest_gap = cumack;
6425830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
6426830d754dSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
6427830d754dSRandall Stewart 		    stcb->asoc.overall_error_count,
6428830d754dSRandall Stewart 		    0,
6429830d754dSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
6430830d754dSRandall Stewart 		    __LINE__);
6431830d754dSRandall Stewart 	}
6432830d754dSRandall Stewart 	stcb->asoc.overall_error_count = 0;
6433830d754dSRandall Stewart 	if (compare_with_wrap(cumack, asoc->last_acked_seq, MAX_TSN)) {
6434830d754dSRandall Stewart 		/* process the new consecutive TSN first */
6435830d754dSRandall Stewart 		tp1 = TAILQ_FIRST(&asoc->sent_queue);
6436830d754dSRandall Stewart 		while (tp1) {
6437830d754dSRandall Stewart 			tp2 = TAILQ_NEXT(tp1, sctp_next);
6438830d754dSRandall Stewart 			if (compare_with_wrap(cumack, tp1->rec.data.TSN_seq,
6439830d754dSRandall Stewart 			    MAX_TSN) ||
6440830d754dSRandall Stewart 			    cumack == tp1->rec.data.TSN_seq) {
6441830d754dSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
6442830d754dSRandall Stewart 					printf("Warning, an unsent is now acked?\n");
6443830d754dSRandall Stewart 				}
6444830d754dSRandall Stewart 				/*
6445830d754dSRandall Stewart 				 * ECN Nonce: Add the nonce to the sender's
6446830d754dSRandall Stewart 				 * nonce sum
6447830d754dSRandall Stewart 				 */
6448830d754dSRandall Stewart 				asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
6449830d754dSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
6450830d754dSRandall Stewart 					/*
6451830d754dSRandall Stewart 					 * If it is less than ACKED, it is
6452830d754dSRandall Stewart 					 * now no-longer in flight. Higher
6453830d754dSRandall Stewart 					 * values may occur during marking
6454830d754dSRandall Stewart 					 */
6455830d754dSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
6456830d754dSRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
6457830d754dSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
6458830d754dSRandall Stewart 							    tp1->whoTo->flight_size,
6459830d754dSRandall Stewart 							    tp1->book_size,
6460830d754dSRandall Stewart 							    (uintptr_t) tp1->whoTo,
6461830d754dSRandall Stewart 							    tp1->rec.data.TSN_seq);
6462830d754dSRandall Stewart 						}
6463830d754dSRandall Stewart 						sctp_flight_size_decrease(tp1);
6464830d754dSRandall Stewart 						/* sa_ignore NO_NULL_CHK */
6465830d754dSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
6466830d754dSRandall Stewart 					}
6467830d754dSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
6468830d754dSRandall Stewart 					if (tp1->snd_count < 2) {
6469830d754dSRandall Stewart 						/*
6470830d754dSRandall Stewart 						 * True non-retransmited
6471830d754dSRandall Stewart 						 * chunk
6472830d754dSRandall Stewart 						 */
6473830d754dSRandall Stewart 						tp1->whoTo->net_ack2 +=
6474830d754dSRandall Stewart 						    tp1->send_size;
6475830d754dSRandall Stewart 
6476830d754dSRandall Stewart 						/* update RTO too? */
6477830d754dSRandall Stewart 						if (tp1->do_rtt) {
6478830d754dSRandall Stewart 							tp1->whoTo->RTO =
6479830d754dSRandall Stewart 							/*
6480830d754dSRandall Stewart 							 * sa_ignore
6481830d754dSRandall Stewart 							 * NO_NULL_CHK
6482830d754dSRandall Stewart 							 */
6483830d754dSRandall Stewart 							    sctp_calculate_rto(stcb,
6484830d754dSRandall Stewart 							    asoc, tp1->whoTo,
6485830d754dSRandall Stewart 							    &tp1->sent_rcv_time,
6486830d754dSRandall Stewart 							    sctp_align_safe_nocopy);
6487830d754dSRandall Stewart 							tp1->do_rtt = 0;
6488830d754dSRandall Stewart 						}
6489830d754dSRandall Stewart 					}
6490830d754dSRandall Stewart 					/*
6491830d754dSRandall Stewart 					 * CMT: CUCv2 algorithm. From the
6492830d754dSRandall Stewart 					 * cumack'd TSNs, for each TSN being
6493830d754dSRandall Stewart 					 * acked for the first time, set the
6494830d754dSRandall Stewart 					 * following variables for the
6495830d754dSRandall Stewart 					 * corresp destination.
6496830d754dSRandall Stewart 					 * new_pseudo_cumack will trigger a
6497830d754dSRandall Stewart 					 * cwnd update.
6498830d754dSRandall Stewart 					 * find_(rtx_)pseudo_cumack will
6499830d754dSRandall Stewart 					 * trigger search for the next
6500830d754dSRandall Stewart 					 * expected (rtx-)pseudo-cumack.
6501830d754dSRandall Stewart 					 */
6502830d754dSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
6503830d754dSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
6504830d754dSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
6505830d754dSRandall Stewart 
6506830d754dSRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
6507830d754dSRandall Stewart 						/* sa_ignore NO_NULL_CHK */
6508830d754dSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
6509830d754dSRandall Stewart 					}
6510830d754dSRandall Stewart 				}
6511830d754dSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
6512830d754dSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
6513830d754dSRandall Stewart 				}
6514830d754dSRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
6515830d754dSRandall Stewart 					/* deflate the cwnd */
6516830d754dSRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
6517830d754dSRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
6518830d754dSRandall Stewart 				}
6519830d754dSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
6520830d754dSRandall Stewart 				TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
6521830d754dSRandall Stewart 				if (tp1->data) {
6522830d754dSRandall Stewart 					/* sa_ignore NO_NULL_CHK */
6523830d754dSRandall Stewart 					sctp_free_bufspace(stcb, asoc, tp1, 1);
6524830d754dSRandall Stewart 					sctp_m_freem(tp1->data);
6525830d754dSRandall Stewart 				}
6526830d754dSRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
6527830d754dSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
6528830d754dSRandall Stewart 					    cumack,
6529830d754dSRandall Stewart 					    tp1->rec.data.TSN_seq,
6530830d754dSRandall Stewart 					    0,
6531830d754dSRandall Stewart 					    0,
6532830d754dSRandall Stewart 					    SCTP_LOG_FREE_SENT);
6533830d754dSRandall Stewart 				}
6534830d754dSRandall Stewart 				tp1->data = NULL;
6535830d754dSRandall Stewart 				asoc->sent_queue_cnt--;
6536830d754dSRandall Stewart 				sctp_free_a_chunk(stcb, tp1);
6537830d754dSRandall Stewart 				tp1 = tp2;
6538830d754dSRandall Stewart 			} else {
6539830d754dSRandall Stewart 				break;
6540830d754dSRandall Stewart 			}
6541830d754dSRandall Stewart 		}
6542830d754dSRandall Stewart 
6543830d754dSRandall Stewart 	}
6544830d754dSRandall Stewart 	/* sa_ignore NO_NULL_CHK */
6545830d754dSRandall Stewart 	if (stcb->sctp_socket) {
6546830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
6547830d754dSRandall Stewart 		struct socket *so;
6548830d754dSRandall Stewart 
6549830d754dSRandall Stewart #endif
6550830d754dSRandall Stewart 
6551830d754dSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
6552830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
6553830d754dSRandall Stewart 			/* sa_ignore NO_NULL_CHK */
6554830d754dSRandall Stewart 			sctp_wakeup_log(stcb, cumack, 1, SCTP_WAKESND_FROM_SACK);
6555830d754dSRandall Stewart 		}
6556830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
6557830d754dSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
6558830d754dSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
6559830d754dSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
6560830d754dSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
6561830d754dSRandall Stewart 		SCTP_TCB_LOCK(stcb);
6562830d754dSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
6563830d754dSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
6564830d754dSRandall Stewart 			/* assoc was freed while we were unlocked */
6565830d754dSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
6566830d754dSRandall Stewart 			return;
6567830d754dSRandall Stewart 		}
6568830d754dSRandall Stewart #endif
6569830d754dSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
6570830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
6571830d754dSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
6572830d754dSRandall Stewart #endif
6573830d754dSRandall Stewart 	} else {
6574830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
6575830d754dSRandall Stewart 			sctp_wakeup_log(stcb, cumack, 1, SCTP_NOWAKE_FROM_SACK);
6576830d754dSRandall Stewart 		}
6577830d754dSRandall Stewart 	}
6578830d754dSRandall Stewart 
6579830d754dSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
6580830d754dSRandall Stewart 	if (asoc->last_acked_seq != cumack)
6581830d754dSRandall Stewart 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
6582830d754dSRandall Stewart 
6583830d754dSRandall Stewart 	asoc->last_acked_seq = cumack;
6584830d754dSRandall Stewart 
6585830d754dSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
6586830d754dSRandall Stewart 		/* nothing left in-flight */
6587830d754dSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6588830d754dSRandall Stewart 			net->flight_size = 0;
6589830d754dSRandall Stewart 			net->partial_bytes_acked = 0;
6590830d754dSRandall Stewart 		}
6591830d754dSRandall Stewart 		asoc->total_flight = 0;
6592830d754dSRandall Stewart 		asoc->total_flight_count = 0;
6593830d754dSRandall Stewart 	}
6594830d754dSRandall Stewart 	/* Fix up the a-p-a-p for future PR-SCTP sends */
6595830d754dSRandall Stewart 	if (compare_with_wrap(cumack, asoc->advanced_peer_ack_point, MAX_TSN)) {
6596830d754dSRandall Stewart 		asoc->advanced_peer_ack_point = cumack;
6597830d754dSRandall Stewart 	}
6598830d754dSRandall Stewart 	/* ECN Nonce updates */
6599830d754dSRandall Stewart 	if (asoc->ecn_nonce_allowed) {
6600830d754dSRandall Stewart 		if (asoc->nonce_sum_check) {
6601830d754dSRandall Stewart 			if (nonce_sum_flag != ((asoc->nonce_sum_expect_base) & SCTP_SACK_NONCE_SUM)) {
6602830d754dSRandall Stewart 				if (asoc->nonce_wait_for_ecne == 0) {
6603830d754dSRandall Stewart 					struct sctp_tmit_chunk *lchk;
6604830d754dSRandall Stewart 
6605830d754dSRandall Stewart 					lchk = TAILQ_FIRST(&asoc->send_queue);
6606830d754dSRandall Stewart 					asoc->nonce_wait_for_ecne = 1;
6607830d754dSRandall Stewart 					if (lchk) {
6608830d754dSRandall Stewart 						asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
6609830d754dSRandall Stewart 					} else {
6610830d754dSRandall Stewart 						asoc->nonce_wait_tsn = asoc->sending_seq;
6611830d754dSRandall Stewart 					}
6612830d754dSRandall Stewart 				} else {
6613830d754dSRandall Stewart 					if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
6614830d754dSRandall Stewart 					    (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
6615830d754dSRandall Stewart 						/*
6616830d754dSRandall Stewart 						 * Misbehaving peer. We need
6617830d754dSRandall Stewart 						 * to react to this guy
6618830d754dSRandall Stewart 						 */
6619830d754dSRandall Stewart 						asoc->ecn_allowed = 0;
6620830d754dSRandall Stewart 						asoc->ecn_nonce_allowed = 0;
6621830d754dSRandall Stewart 					}
6622830d754dSRandall Stewart 				}
6623830d754dSRandall Stewart 			}
6624830d754dSRandall Stewart 		} else {
6625830d754dSRandall Stewart 			/* See if Resynchronization Possible */
6626830d754dSRandall Stewart 			if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
6627830d754dSRandall Stewart 				asoc->nonce_sum_check = 1;
6628830d754dSRandall Stewart 				/*
6629830d754dSRandall Stewart 				 * now we must calculate what the base is.
6630830d754dSRandall Stewart 				 * We do this based on two things, we know
6631830d754dSRandall Stewart 				 * the total's for all the segments
6632830d754dSRandall Stewart 				 * gap-acked in the SACK (none), We also
6633830d754dSRandall Stewart 				 * know the SACK's nonce sum, its in
6634830d754dSRandall Stewart 				 * nonce_sum_flag. So we can build a truth
6635830d754dSRandall Stewart 				 * table to back-calculate the new value of
6636830d754dSRandall Stewart 				 * asoc->nonce_sum_expect_base:
6637830d754dSRandall Stewart 				 *
6638830d754dSRandall Stewart 				 * SACK-flag-Value         Seg-Sums Base 0 0 0
6639830d754dSRandall Stewart 				 * 1                    0 1 0 1 1 1 1 0
6640830d754dSRandall Stewart 				 */
6641830d754dSRandall Stewart 				asoc->nonce_sum_expect_base = (0 ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
6642830d754dSRandall Stewart 			}
6643830d754dSRandall Stewart 		}
6644830d754dSRandall Stewart 	}
6645830d754dSRandall Stewart 	/* RWND update */
6646830d754dSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
6647830d754dSRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
6648830d754dSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6649830d754dSRandall Stewart 		/* SWS sender side engages */
6650830d754dSRandall Stewart 		asoc->peers_rwnd = 0;
6651830d754dSRandall Stewart 	}
6652830d754dSRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
6653830d754dSRandall Stewart 		win_probe_recovery = 1;
6654830d754dSRandall Stewart 	}
6655830d754dSRandall Stewart 	/* Now assure a timer where data is queued at */
6656830d754dSRandall Stewart again:
6657830d754dSRandall Stewart 	j = 0;
6658830d754dSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
66595171328bSRandall Stewart 		int to_ticks;
66605171328bSRandall Stewart 
6661830d754dSRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
6662830d754dSRandall Stewart 			win_probe_recovered = 1;
6663830d754dSRandall Stewart 			/*
6664830d754dSRandall Stewart 			 * Find first chunk that was used with window probe
6665830d754dSRandall Stewart 			 * and clear the sent
6666830d754dSRandall Stewart 			 */
6667830d754dSRandall Stewart 			/* sa_ignore FREED_MEMORY */
6668830d754dSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
6669830d754dSRandall Stewart 				if (tp1->window_probe) {
6670830d754dSRandall Stewart 					/* move back to data send queue */
6671830d754dSRandall Stewart 					sctp_window_probe_recovery(stcb, asoc, net, tp1);
6672830d754dSRandall Stewart 					break;
6673830d754dSRandall Stewart 				}
6674830d754dSRandall Stewart 			}
6675830d754dSRandall Stewart 		}
6676830d754dSRandall Stewart 		if (net->RTO == 0) {
6677830d754dSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
6678830d754dSRandall Stewart 		} else {
6679830d754dSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
6680830d754dSRandall Stewart 		}
66815171328bSRandall Stewart 		if (net->flight_size) {
66825171328bSRandall Stewart 
6683830d754dSRandall Stewart 			j++;
6684830d754dSRandall Stewart 			(void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
6685830d754dSRandall Stewart 			    sctp_timeout_handler, &net->rxt_timer);
66865171328bSRandall Stewart 			if (net->window_probe) {
66875171328bSRandall Stewart 				net->window_probe = 0;
66885171328bSRandall Stewart 			}
6689830d754dSRandall Stewart 		} else {
66905171328bSRandall Stewart 			if (net->window_probe) {
66915171328bSRandall Stewart 				/*
66925171328bSRandall Stewart 				 * In window probes we must assure a timer
66935171328bSRandall Stewart 				 * is still running there
66945171328bSRandall Stewart 				 */
66955171328bSRandall Stewart 				net->window_probe = 0;
66965171328bSRandall Stewart 				(void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
66975171328bSRandall Stewart 				    sctp_timeout_handler, &net->rxt_timer);
66985171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
6699830d754dSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
6700830d754dSRandall Stewart 				    stcb, net,
6701830d754dSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
6702830d754dSRandall Stewart 			}
6703830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
6704830d754dSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
6705830d754dSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
6706830d754dSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
6707830d754dSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
6708830d754dSRandall Stewart 				}
6709830d754dSRandall Stewart 			}
6710830d754dSRandall Stewart 		}
6711830d754dSRandall Stewart 	}
6712830d754dSRandall Stewart 	if ((j == 0) &&
6713830d754dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
6714830d754dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
6715830d754dSRandall Stewart 	    (win_probe_recovered == 0) &&
6716830d754dSRandall Stewart 	    (done_once == 0)) {
67170c0982b8SRandall Stewart 		/*
67180c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
67190c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
67200c0982b8SRandall Stewart 		 */
67210c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
6722830d754dSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6723830d754dSRandall Stewart 				net->flight_size = 0;
6724830d754dSRandall Stewart 			}
6725830d754dSRandall Stewart 			asoc->total_flight = 0;
6726830d754dSRandall Stewart 			asoc->total_flight_count = 0;
6727830d754dSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
6728830d754dSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
6729830d754dSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
6730830d754dSRandall Stewart 					sctp_flight_size_increase(tp1);
6731830d754dSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
6732830d754dSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
6733830d754dSRandall Stewart 					asoc->sent_queue_retran_cnt++;
6734830d754dSRandall Stewart 				}
6735830d754dSRandall Stewart 			}
67360c0982b8SRandall Stewart 		}
6737830d754dSRandall Stewart 		done_once = 1;
6738830d754dSRandall Stewart 		goto again;
6739830d754dSRandall Stewart 	}
6740830d754dSRandall Stewart 	/**********************************/
6741830d754dSRandall Stewart 	/* Now what about shutdown issues */
6742830d754dSRandall Stewart 	/**********************************/
6743830d754dSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
6744830d754dSRandall Stewart 		/* nothing left on sendqueue.. consider done */
6745830d754dSRandall Stewart 		/* clean up */
6746830d754dSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
6747830d754dSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
6748830d754dSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
6749830d754dSRandall Stewart 		    (asoc->locked_on_sending)
6750830d754dSRandall Stewart 		    ) {
6751830d754dSRandall Stewart 			struct sctp_stream_queue_pending *sp;
6752830d754dSRandall Stewart 
6753830d754dSRandall Stewart 			/*
6754830d754dSRandall Stewart 			 * I may be in a state where we got all across.. but
6755830d754dSRandall Stewart 			 * cannot write more due to a shutdown... we abort
6756830d754dSRandall Stewart 			 * since the user did not indicate EOR in this case.
6757830d754dSRandall Stewart 			 * The sp will be cleaned during free of the asoc.
6758830d754dSRandall Stewart 			 */
6759830d754dSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
6760830d754dSRandall Stewart 			    sctp_streamhead);
6761830d754dSRandall Stewart 			if ((sp) && (sp->length == 0)) {
6762830d754dSRandall Stewart 				/* Let cleanup code purge it */
6763830d754dSRandall Stewart 				if (sp->msg_is_complete) {
6764830d754dSRandall Stewart 					asoc->stream_queue_cnt--;
6765830d754dSRandall Stewart 				} else {
6766830d754dSRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
6767830d754dSRandall Stewart 					asoc->locked_on_sending = NULL;
6768830d754dSRandall Stewart 					asoc->stream_queue_cnt--;
6769830d754dSRandall Stewart 				}
6770830d754dSRandall Stewart 			}
6771830d754dSRandall Stewart 		}
6772830d754dSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
6773830d754dSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
6774830d754dSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
6775830d754dSRandall Stewart 				/* Need to abort here */
6776830d754dSRandall Stewart 				struct mbuf *oper;
6777830d754dSRandall Stewart 
6778830d754dSRandall Stewart 		abort_out_now:
6779830d754dSRandall Stewart 				*abort_now = 1;
6780830d754dSRandall Stewart 				/* XXX */
6781830d754dSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
6782830d754dSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
6783830d754dSRandall Stewart 				if (oper) {
6784830d754dSRandall Stewart 					struct sctp_paramhdr *ph;
6785830d754dSRandall Stewart 					uint32_t *ippp;
6786830d754dSRandall Stewart 
6787830d754dSRandall Stewart 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
6788830d754dSRandall Stewart 					    sizeof(uint32_t);
6789830d754dSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
6790830d754dSRandall Stewart 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6791830d754dSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
6792830d754dSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
6793830d754dSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_24);
6794830d754dSRandall Stewart 				}
6795830d754dSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
6796830d754dSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
6797830d754dSRandall Stewart 			} else {
6798830d754dSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
6799830d754dSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
6800830d754dSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6801830d754dSRandall Stewart 				}
6802830d754dSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
6803830d754dSRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6804830d754dSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
6805830d754dSRandall Stewart 				sctp_send_shutdown(stcb,
6806830d754dSRandall Stewart 				    stcb->asoc.primary_destination);
6807830d754dSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
6808830d754dSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
6809830d754dSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
6810830d754dSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
6811830d754dSRandall Stewart 			}
6812830d754dSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
6813830d754dSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
6814830d754dSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
6815830d754dSRandall Stewart 				goto abort_out_now;
6816830d754dSRandall Stewart 			}
6817830d754dSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6818830d754dSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
6819830d754dSRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6820830d754dSRandall Stewart 			sctp_send_shutdown_ack(stcb,
6821830d754dSRandall Stewart 			    stcb->asoc.primary_destination);
6822830d754dSRandall Stewart 
6823830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
6824830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, asoc->primary_destination);
6825830d754dSRandall Stewart 		}
6826830d754dSRandall Stewart 	}
6827830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
6828830d754dSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
6829830d754dSRandall Stewart 		    rwnd,
6830830d754dSRandall Stewart 		    stcb->asoc.peers_rwnd,
6831830d754dSRandall Stewart 		    stcb->asoc.total_flight,
6832830d754dSRandall Stewart 		    stcb->asoc.total_output_queue_size);
6833830d754dSRandall Stewart 	}
6834830d754dSRandall Stewart }
6835830d754dSRandall Stewart 
6836830d754dSRandall Stewart /* EY! nr_sack version of sctp_handle_segments, nr-gapped TSNs get removed from RtxQ in this method*/
6837830d754dSRandall Stewart static void
6838830d754dSRandall Stewart sctp_handle_nr_sack_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
6839830d754dSRandall Stewart     struct sctp_nr_sack_chunk *ch, uint32_t last_tsn, uint32_t * biggest_tsn_acked,
6840830d754dSRandall Stewart     uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
6841830d754dSRandall Stewart     uint32_t num_seg, uint32_t num_nr_seg, int *ecn_seg_sums)
6842830d754dSRandall Stewart {
6843830d754dSRandall Stewart 	/************************************************/
6844830d754dSRandall Stewart 	/* process fragments and update sendqueue        */
6845830d754dSRandall Stewart 	/************************************************/
6846830d754dSRandall Stewart 	struct sctp_nr_sack *nr_sack;
6847830d754dSRandall Stewart 	struct sctp_gap_ack_block *frag, block;
6848830d754dSRandall Stewart 	struct sctp_nr_gap_ack_block *nr_frag, nr_block;
6849830d754dSRandall Stewart 	struct sctp_tmit_chunk *tp1;
6850830d754dSRandall Stewart 	uint32_t i, j, all_bit;
6851830d754dSRandall Stewart 	int wake_him = 0;
6852830d754dSRandall Stewart 	uint32_t theTSN;
6853830d754dSRandall Stewart 	int num_frs = 0;
6854830d754dSRandall Stewart 
6855830d754dSRandall Stewart 	uint16_t frag_strt, frag_end, primary_flag_set;
6856830d754dSRandall Stewart 	uint16_t nr_frag_strt, nr_frag_end;
6857830d754dSRandall Stewart 
6858830d754dSRandall Stewart 	uint32_t last_frag_high;
6859830d754dSRandall Stewart 	uint32_t last_nr_frag_high;
6860830d754dSRandall Stewart 
6861830d754dSRandall Stewart 	all_bit = ch->ch.chunk_flags & SCTP_NR_SACK_ALL_BIT;
6862830d754dSRandall Stewart 
6863830d754dSRandall Stewart 	/*
6864830d754dSRandall Stewart 	 * @@@ JRI : TODO: This flag is not used anywhere .. remove?
6865830d754dSRandall Stewart 	 */
6866830d754dSRandall Stewart 	if (asoc->primary_destination->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
6867830d754dSRandall Stewart 		primary_flag_set = 1;
6868830d754dSRandall Stewart 	} else {
6869830d754dSRandall Stewart 		primary_flag_set = 0;
6870830d754dSRandall Stewart 	}
6871830d754dSRandall Stewart 	nr_sack = &ch->nr_sack;
6872830d754dSRandall Stewart 
6873830d754dSRandall Stewart 	/*
6874830d754dSRandall Stewart 	 * EY! - I will process nr_gaps similarly,by going to this position
6875830d754dSRandall Stewart 	 * again if All bit is set
6876830d754dSRandall Stewart 	 */
6877830d754dSRandall Stewart 	frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
6878830d754dSRandall Stewart 	    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
6879830d754dSRandall Stewart 	*offset += sizeof(block);
6880830d754dSRandall Stewart 	if (frag == NULL) {
6881830d754dSRandall Stewart 		return;
6882830d754dSRandall Stewart 	}
6883830d754dSRandall Stewart 	tp1 = NULL;
6884830d754dSRandall Stewart 	last_frag_high = 0;
6885830d754dSRandall Stewart 	for (i = 0; i < num_seg; i++) {
6886830d754dSRandall Stewart 		frag_strt = ntohs(frag->start);
6887830d754dSRandall Stewart 		frag_end = ntohs(frag->end);
6888830d754dSRandall Stewart 		/* some sanity checks on the fargment offsets */
6889830d754dSRandall Stewart 		if (frag_strt > frag_end) {
6890830d754dSRandall Stewart 			/* this one is malformed, skip */
6891830d754dSRandall Stewart 			frag++;
6892830d754dSRandall Stewart 			continue;
6893830d754dSRandall Stewart 		}
6894830d754dSRandall Stewart 		if (compare_with_wrap((frag_end + last_tsn), *biggest_tsn_acked,
6895830d754dSRandall Stewart 		    MAX_TSN))
6896830d754dSRandall Stewart 			*biggest_tsn_acked = frag_end + last_tsn;
6897830d754dSRandall Stewart 
6898830d754dSRandall Stewart 		/* mark acked dgs and find out the highestTSN being acked */
6899830d754dSRandall Stewart 		if (tp1 == NULL) {
6900830d754dSRandall Stewart 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
6901830d754dSRandall Stewart 
6902830d754dSRandall Stewart 			/* save the locations of the last frags */
6903830d754dSRandall Stewart 			last_frag_high = frag_end + last_tsn;
6904830d754dSRandall Stewart 		} else {
6905830d754dSRandall Stewart 			/*
6906830d754dSRandall Stewart 			 * now lets see if we need to reset the queue due to
6907830d754dSRandall Stewart 			 * a out-of-order SACK fragment
6908830d754dSRandall Stewart 			 */
6909830d754dSRandall Stewart 			if (compare_with_wrap(frag_strt + last_tsn,
6910830d754dSRandall Stewart 			    last_frag_high, MAX_TSN)) {
6911830d754dSRandall Stewart 				/*
6912830d754dSRandall Stewart 				 * if the new frag starts after the last TSN
6913830d754dSRandall Stewart 				 * frag covered, we are ok and this one is
6914830d754dSRandall Stewart 				 * beyond the last one
6915830d754dSRandall Stewart 				 */
6916830d754dSRandall Stewart 				;
6917830d754dSRandall Stewart 			} else {
6918830d754dSRandall Stewart 				/*
6919830d754dSRandall Stewart 				 * ok, they have reset us, so we need to
6920830d754dSRandall Stewart 				 * reset the queue this will cause extra
6921830d754dSRandall Stewart 				 * hunting but hey, they chose the
6922830d754dSRandall Stewart 				 * performance hit when they failed to order
6923830d754dSRandall Stewart 				 * there gaps..
6924830d754dSRandall Stewart 				 */
6925830d754dSRandall Stewart 				tp1 = TAILQ_FIRST(&asoc->sent_queue);
6926830d754dSRandall Stewart 			}
6927830d754dSRandall Stewart 			last_frag_high = frag_end + last_tsn;
6928830d754dSRandall Stewart 		}
6929830d754dSRandall Stewart 		for (j = frag_strt; j <= frag_end; j++) {
6930830d754dSRandall Stewart 			theTSN = j + last_tsn;
6931830d754dSRandall Stewart 			while (tp1) {
6932830d754dSRandall Stewart 				if (tp1->rec.data.doing_fast_retransmit)
6933830d754dSRandall Stewart 					num_frs++;
6934830d754dSRandall Stewart 
6935830d754dSRandall Stewart 				/*
6936830d754dSRandall Stewart 				 * CMT: CUCv2 algorithm. For each TSN being
6937830d754dSRandall Stewart 				 * processed from the sent queue, track the
6938830d754dSRandall Stewart 				 * next expected pseudo-cumack, or
6939830d754dSRandall Stewart 				 * rtx_pseudo_cumack, if required. Separate
6940830d754dSRandall Stewart 				 * cumack trackers for first transmissions,
6941830d754dSRandall Stewart 				 * and retransmissions.
6942830d754dSRandall Stewart 				 */
6943830d754dSRandall Stewart 				if ((tp1->whoTo->find_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
6944830d754dSRandall Stewart 				    (tp1->snd_count == 1)) {
6945830d754dSRandall Stewart 					tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
6946830d754dSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 0;
6947830d754dSRandall Stewart 				}
6948830d754dSRandall Stewart 				if ((tp1->whoTo->find_rtx_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
6949830d754dSRandall Stewart 				    (tp1->snd_count > 1)) {
6950830d754dSRandall Stewart 					tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
6951830d754dSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 0;
6952830d754dSRandall Stewart 				}
6953830d754dSRandall Stewart 				if (tp1->rec.data.TSN_seq == theTSN) {
6954830d754dSRandall Stewart 					if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
6955830d754dSRandall Stewart 						/*
6956830d754dSRandall Stewart 						 * must be held until
6957830d754dSRandall Stewart 						 * cum-ack passes
6958830d754dSRandall Stewart 						 */
6959830d754dSRandall Stewart 						/*
6960830d754dSRandall Stewart 						 * ECN Nonce: Add the nonce
6961830d754dSRandall Stewart 						 * value to the sender's
6962830d754dSRandall Stewart 						 * nonce sum
6963830d754dSRandall Stewart 						 */
6964830d754dSRandall Stewart 						if (tp1->sent < SCTP_DATAGRAM_RESEND) {
6965830d754dSRandall Stewart 							/*-
6966830d754dSRandall Stewart 							 * If it is less than RESEND, it is
6967830d754dSRandall Stewart 							 * now no-longer in flight.
6968830d754dSRandall Stewart 							 * Higher values may already be set
6969830d754dSRandall Stewart 							 * via previous Gap Ack Blocks...
6970830d754dSRandall Stewart 							 * i.e. ACKED or RESEND.
6971830d754dSRandall Stewart 							 */
6972830d754dSRandall Stewart 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
6973830d754dSRandall Stewart 							    *biggest_newly_acked_tsn, MAX_TSN)) {
6974830d754dSRandall Stewart 								*biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
6975830d754dSRandall Stewart 							}
6976830d754dSRandall Stewart 							/*
6977830d754dSRandall Stewart 							 * CMT: SFR algo
6978830d754dSRandall Stewart 							 * (and HTNA) - set
6979830d754dSRandall Stewart 							 * saw_newack to 1
6980830d754dSRandall Stewart 							 * for dest being
6981830d754dSRandall Stewart 							 * newly acked.
6982830d754dSRandall Stewart 							 * update
6983830d754dSRandall Stewart 							 * this_sack_highest_
6984830d754dSRandall Stewart 							 * newack if
6985830d754dSRandall Stewart 							 * appropriate.
6986830d754dSRandall Stewart 							 */
6987830d754dSRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0)
6988830d754dSRandall Stewart 								tp1->whoTo->saw_newack = 1;
6989830d754dSRandall Stewart 
6990830d754dSRandall Stewart 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
6991830d754dSRandall Stewart 							    tp1->whoTo->this_sack_highest_newack,
6992830d754dSRandall Stewart 							    MAX_TSN)) {
6993830d754dSRandall Stewart 								tp1->whoTo->this_sack_highest_newack =
6994830d754dSRandall Stewart 								    tp1->rec.data.TSN_seq;
6995830d754dSRandall Stewart 							}
6996830d754dSRandall Stewart 							/*
6997830d754dSRandall Stewart 							 * CMT DAC algo:
6998830d754dSRandall Stewart 							 * also update
6999830d754dSRandall Stewart 							 * this_sack_lowest_n
7000830d754dSRandall Stewart 							 * ewack
7001830d754dSRandall Stewart 							 */
7002830d754dSRandall Stewart 							if (*this_sack_lowest_newack == 0) {
7003830d754dSRandall Stewart 								if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
7004830d754dSRandall Stewart 									sctp_log_sack(*this_sack_lowest_newack,
7005830d754dSRandall Stewart 									    last_tsn,
7006830d754dSRandall Stewart 									    tp1->rec.data.TSN_seq,
7007830d754dSRandall Stewart 									    0,
7008830d754dSRandall Stewart 									    0,
7009830d754dSRandall Stewart 									    SCTP_LOG_TSN_ACKED);
7010830d754dSRandall Stewart 								}
7011830d754dSRandall Stewart 								*this_sack_lowest_newack = tp1->rec.data.TSN_seq;
7012830d754dSRandall Stewart 							}
7013830d754dSRandall Stewart 							/*
7014830d754dSRandall Stewart 							 * CMT: CUCv2
7015830d754dSRandall Stewart 							 * algorithm. If
7016830d754dSRandall Stewart 							 * (rtx-)pseudo-cumac
7017830d754dSRandall Stewart 							 * k for corresp
7018830d754dSRandall Stewart 							 * dest is being
7019830d754dSRandall Stewart 							 * acked, then we
7020830d754dSRandall Stewart 							 * have a new
7021830d754dSRandall Stewart 							 * (rtx-)pseudo-cumac
7022830d754dSRandall Stewart 							 * k. Set
7023830d754dSRandall Stewart 							 * new_(rtx_)pseudo_c
7024830d754dSRandall Stewart 							 * umack to TRUE so
7025830d754dSRandall Stewart 							 * that the cwnd for
7026830d754dSRandall Stewart 							 * this dest can be
7027830d754dSRandall Stewart 							 * updated. Also
7028830d754dSRandall Stewart 							 * trigger search
7029830d754dSRandall Stewart 							 * for the next
7030830d754dSRandall Stewart 							 * expected
7031830d754dSRandall Stewart 							 * (rtx-)pseudo-cumac
7032830d754dSRandall Stewart 							 * k. Separate
7033830d754dSRandall Stewart 							 * pseudo_cumack
7034830d754dSRandall Stewart 							 * trackers for
7035830d754dSRandall Stewart 							 * first
7036830d754dSRandall Stewart 							 * transmissions and
7037830d754dSRandall Stewart 							 * retransmissions.
7038830d754dSRandall Stewart 							 */
7039830d754dSRandall Stewart 							if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
7040830d754dSRandall Stewart 								if (tp1->rec.data.chunk_was_revoked == 0) {
7041830d754dSRandall Stewart 									tp1->whoTo->new_pseudo_cumack = 1;
7042830d754dSRandall Stewart 								}
7043830d754dSRandall Stewart 								tp1->whoTo->find_pseudo_cumack = 1;
7044830d754dSRandall Stewart 							}
7045830d754dSRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7046830d754dSRandall Stewart 								sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
7047830d754dSRandall Stewart 							}
7048830d754dSRandall Stewart 							if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
7049830d754dSRandall Stewart 								if (tp1->rec.data.chunk_was_revoked == 0) {
7050830d754dSRandall Stewart 									tp1->whoTo->new_pseudo_cumack = 1;
7051830d754dSRandall Stewart 								}
7052830d754dSRandall Stewart 								tp1->whoTo->find_rtx_pseudo_cumack = 1;
7053830d754dSRandall Stewart 							}
7054830d754dSRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
7055830d754dSRandall Stewart 								sctp_log_sack(*biggest_newly_acked_tsn,
7056830d754dSRandall Stewart 								    last_tsn,
7057830d754dSRandall Stewart 								    tp1->rec.data.TSN_seq,
7058830d754dSRandall Stewart 								    frag_strt,
7059830d754dSRandall Stewart 								    frag_end,
7060830d754dSRandall Stewart 								    SCTP_LOG_TSN_ACKED);
7061830d754dSRandall Stewart 							}
7062830d754dSRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
7063830d754dSRandall Stewart 								sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
7064830d754dSRandall Stewart 								    tp1->whoTo->flight_size,
7065830d754dSRandall Stewart 								    tp1->book_size,
7066830d754dSRandall Stewart 								    (uintptr_t) tp1->whoTo,
7067830d754dSRandall Stewart 								    tp1->rec.data.TSN_seq);
7068830d754dSRandall Stewart 							}
7069830d754dSRandall Stewart 							sctp_flight_size_decrease(tp1);
7070830d754dSRandall Stewart 							sctp_total_flight_decrease(stcb, tp1);
7071830d754dSRandall Stewart 
7072830d754dSRandall Stewart 							tp1->whoTo->net_ack += tp1->send_size;
7073830d754dSRandall Stewart 							if (tp1->snd_count < 2) {
7074830d754dSRandall Stewart 								/*
7075830d754dSRandall Stewart 								 * True
7076830d754dSRandall Stewart 								 * non-retran
7077830d754dSRandall Stewart 								 * smited
7078830d754dSRandall Stewart 								 * chunk
7079830d754dSRandall Stewart 								 */
7080830d754dSRandall Stewart 								tp1->whoTo->net_ack2 += tp1->send_size;
7081830d754dSRandall Stewart 
7082830d754dSRandall Stewart 								/*
7083830d754dSRandall Stewart 								 * update
7084830d754dSRandall Stewart 								 * RTO too ?
7085830d754dSRandall Stewart 								 */
7086830d754dSRandall Stewart 								if (tp1->do_rtt) {
7087830d754dSRandall Stewart 									tp1->whoTo->RTO =
7088830d754dSRandall Stewart 									    sctp_calculate_rto(stcb,
7089830d754dSRandall Stewart 									    asoc,
7090830d754dSRandall Stewart 									    tp1->whoTo,
7091830d754dSRandall Stewart 									    &tp1->sent_rcv_time,
7092830d754dSRandall Stewart 									    sctp_align_safe_nocopy);
7093830d754dSRandall Stewart 									tp1->do_rtt = 0;
7094830d754dSRandall Stewart 								}
7095830d754dSRandall Stewart 							}
7096830d754dSRandall Stewart 						}
7097830d754dSRandall Stewart 						if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
7098830d754dSRandall Stewart 							(*ecn_seg_sums) += tp1->rec.data.ect_nonce;
7099830d754dSRandall Stewart 							(*ecn_seg_sums) &= SCTP_SACK_NONCE_SUM;
7100830d754dSRandall Stewart 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
7101830d754dSRandall Stewart 							    asoc->this_sack_highest_gap,
7102830d754dSRandall Stewart 							    MAX_TSN)) {
7103830d754dSRandall Stewart 								asoc->this_sack_highest_gap =
7104830d754dSRandall Stewart 								    tp1->rec.data.TSN_seq;
7105830d754dSRandall Stewart 							}
7106830d754dSRandall Stewart 							if (tp1->sent == SCTP_DATAGRAM_RESEND) {
7107830d754dSRandall Stewart 								sctp_ucount_decr(asoc->sent_queue_retran_cnt);
7108830d754dSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
7109830d754dSRandall Stewart 								sctp_audit_log(0xB2,
7110830d754dSRandall Stewart 								    (asoc->sent_queue_retran_cnt & 0x000000ff));
7111830d754dSRandall Stewart #endif
7112830d754dSRandall Stewart 							}
7113830d754dSRandall Stewart 						}
7114830d754dSRandall Stewart 						/*
7115830d754dSRandall Stewart 						 * All chunks NOT UNSENT
7116830d754dSRandall Stewart 						 * fall through here and are
7117830d754dSRandall Stewart 						 * marked
7118830d754dSRandall Stewart 						 */
71198933fa13SRandall Stewart 						if (tp1->sent != SCTP_FORWARD_TSN_SKIP)
71208933fa13SRandall Stewart 							tp1->sent = SCTP_DATAGRAM_NR_MARKED;
7121830d754dSRandall Stewart 						if (tp1->rec.data.chunk_was_revoked) {
7122830d754dSRandall Stewart 							/* deflate the cwnd */
7123830d754dSRandall Stewart 							tp1->whoTo->cwnd -= tp1->book_size;
7124830d754dSRandall Stewart 							tp1->rec.data.chunk_was_revoked = 0;
7125830d754dSRandall Stewart 						}
7126830d754dSRandall Stewart 						/*
7127830d754dSRandall Stewart 						 * EY - if all bit is set
7128830d754dSRandall Stewart 						 * then this TSN is
7129830d754dSRandall Stewart 						 * nr_marked
7130830d754dSRandall Stewart 						 */
7131830d754dSRandall Stewart 						if (all_bit) {
71328933fa13SRandall Stewart 							if (tp1->sent != SCTP_FORWARD_TSN_SKIP)
7133830d754dSRandall Stewart 								tp1->sent = SCTP_DATAGRAM_NR_MARKED;
7134830d754dSRandall Stewart 							/*
7135830d754dSRandall Stewart 							 * TAILQ_REMOVE(&asoc
7136830d754dSRandall Stewart 							 * ->sent_queue,
7137830d754dSRandall Stewart 							 * tp1, sctp_next);
7138830d754dSRandall Stewart 							 */
7139830d754dSRandall Stewart 							if (tp1->data) {
7140830d754dSRandall Stewart 								/*
7141830d754dSRandall Stewart 								 * sa_ignore
7142830d754dSRandall Stewart 								 * NO_NULL_CH
7143830d754dSRandall Stewart 								 * K
7144830d754dSRandall Stewart 								 */
7145830d754dSRandall Stewart 								sctp_free_bufspace(stcb, asoc, tp1, 1);
7146830d754dSRandall Stewart 								sctp_m_freem(tp1->data);
7147830d754dSRandall Stewart 							}
7148830d754dSRandall Stewart 							tp1->data = NULL;
7149830d754dSRandall Stewart 							/*
7150830d754dSRandall Stewart 							 * asoc->sent_queue_c
7151830d754dSRandall Stewart 							 * nt--;
7152830d754dSRandall Stewart 							 */
7153830d754dSRandall Stewart 							/*
7154830d754dSRandall Stewart 							 * sctp_free_a_chunk(
7155830d754dSRandall Stewart 							 * stcb, tp1);
7156830d754dSRandall Stewart 							 */
7157830d754dSRandall Stewart 							wake_him++;
7158830d754dSRandall Stewart 						}
7159830d754dSRandall Stewart 					}
7160830d754dSRandall Stewart 					break;
7161830d754dSRandall Stewart 				}	/* if (tp1->TSN_seq == theTSN) */
7162830d754dSRandall Stewart 				if (compare_with_wrap(tp1->rec.data.TSN_seq, theTSN,
7163830d754dSRandall Stewart 				    MAX_TSN))
7164830d754dSRandall Stewart 					break;
7165830d754dSRandall Stewart 
7166830d754dSRandall Stewart 				tp1 = TAILQ_NEXT(tp1, sctp_next);
7167830d754dSRandall Stewart 			}	/* end while (tp1) */
7168830d754dSRandall Stewart 		}		/* end for (j = fragStart */
7169830d754dSRandall Stewart 		frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
7170830d754dSRandall Stewart 		    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
7171830d754dSRandall Stewart 		*offset += sizeof(block);
7172830d754dSRandall Stewart 		if (frag == NULL) {
7173830d754dSRandall Stewart 			break;
7174830d754dSRandall Stewart 		}
7175830d754dSRandall Stewart 	}
7176830d754dSRandall Stewart 
7177830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
7178830d754dSRandall Stewart 		if (num_frs)
7179830d754dSRandall Stewart 			sctp_log_fr(*biggest_tsn_acked,
7180830d754dSRandall Stewart 			    *biggest_newly_acked_tsn,
7181830d754dSRandall Stewart 			    last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
7182830d754dSRandall Stewart 	}
7183830d754dSRandall Stewart 	/*
7184830d754dSRandall Stewart 	 * EY - if all bit is not set then there should be other loops to
7185830d754dSRandall Stewart 	 * identify nr TSNs
7186830d754dSRandall Stewart 	 */
7187830d754dSRandall Stewart 	if (!all_bit) {
7188830d754dSRandall Stewart 
7189830d754dSRandall Stewart 		nr_frag = (struct sctp_nr_gap_ack_block *)sctp_m_getptr(m, *offset,
7190830d754dSRandall Stewart 		    sizeof(struct sctp_nr_gap_ack_block), (uint8_t *) & nr_block);
7191830d754dSRandall Stewart 		*offset += sizeof(nr_block);
7192830d754dSRandall Stewart 
7193830d754dSRandall Stewart 
7194830d754dSRandall Stewart 
7195830d754dSRandall Stewart 		if (nr_frag == NULL) {
7196830d754dSRandall Stewart 			return;
7197830d754dSRandall Stewart 		}
7198830d754dSRandall Stewart 		tp1 = NULL;
7199830d754dSRandall Stewart 		last_nr_frag_high = 0;
7200830d754dSRandall Stewart 
7201830d754dSRandall Stewart 		for (i = 0; i < num_nr_seg; i++) {
7202830d754dSRandall Stewart 
7203830d754dSRandall Stewart 			nr_frag_strt = ntohs(nr_frag->start);
7204830d754dSRandall Stewart 			nr_frag_end = ntohs(nr_frag->end);
7205830d754dSRandall Stewart 
7206830d754dSRandall Stewart 			/* some sanity checks on the nr fargment offsets */
7207830d754dSRandall Stewart 			if (nr_frag_strt > nr_frag_end) {
7208830d754dSRandall Stewart 				/* this one is malformed, skip */
7209830d754dSRandall Stewart 				nr_frag++;
7210830d754dSRandall Stewart 				continue;
7211830d754dSRandall Stewart 			}
7212830d754dSRandall Stewart 			/*
7213830d754dSRandall Stewart 			 * mark acked dgs and find out the highestTSN being
7214830d754dSRandall Stewart 			 * acked
7215830d754dSRandall Stewart 			 */
7216830d754dSRandall Stewart 			if (tp1 == NULL) {
7217830d754dSRandall Stewart 				tp1 = TAILQ_FIRST(&asoc->sent_queue);
7218830d754dSRandall Stewart 
7219830d754dSRandall Stewart 				/* save the locations of the last frags */
7220830d754dSRandall Stewart 				last_nr_frag_high = nr_frag_end + last_tsn;
7221830d754dSRandall Stewart 			} else {
7222830d754dSRandall Stewart 				/*
7223830d754dSRandall Stewart 				 * now lets see if we need to reset the
7224830d754dSRandall Stewart 				 * queue due to a out-of-order SACK fragment
7225830d754dSRandall Stewart 				 */
7226830d754dSRandall Stewart 				if (compare_with_wrap(nr_frag_strt + last_tsn,
7227830d754dSRandall Stewart 				    last_nr_frag_high, MAX_TSN)) {
7228830d754dSRandall Stewart 					/*
7229830d754dSRandall Stewart 					 * if the new frag starts after the
7230830d754dSRandall Stewart 					 * last TSN frag covered, we are ok
7231830d754dSRandall Stewart 					 * and this one is beyond the last
7232830d754dSRandall Stewart 					 * one
7233830d754dSRandall Stewart 					 */
7234830d754dSRandall Stewart 					;
7235830d754dSRandall Stewart 				} else {
7236830d754dSRandall Stewart 					/*
7237830d754dSRandall Stewart 					 * ok, they have reset us, so we
7238830d754dSRandall Stewart 					 * need to reset the queue this will
7239830d754dSRandall Stewart 					 * cause extra hunting but hey, they
7240830d754dSRandall Stewart 					 * chose the performance hit when
7241830d754dSRandall Stewart 					 * they failed to order there gaps..
7242830d754dSRandall Stewart 					 */
7243830d754dSRandall Stewart 					tp1 = TAILQ_FIRST(&asoc->sent_queue);
7244830d754dSRandall Stewart 				}
7245830d754dSRandall Stewart 				last_nr_frag_high = nr_frag_end + last_tsn;
7246830d754dSRandall Stewart 			}
7247830d754dSRandall Stewart 
7248830d754dSRandall Stewart 			for (j = nr_frag_strt + last_tsn; (compare_with_wrap((nr_frag_end + last_tsn), j, MAX_TSN)); j++) {
7249830d754dSRandall Stewart 				while (tp1) {
7250830d754dSRandall Stewart 					if (tp1->rec.data.TSN_seq == j) {
7251830d754dSRandall Stewart 						if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
72528933fa13SRandall Stewart 							if (tp1->sent != SCTP_FORWARD_TSN_SKIP)
7253830d754dSRandall Stewart 								tp1->sent = SCTP_DATAGRAM_NR_MARKED;
7254830d754dSRandall Stewart 							/*
7255830d754dSRandall Stewart 							 * TAILQ_REMOVE(&asoc
7256830d754dSRandall Stewart 							 * ->sent_queue,
7257830d754dSRandall Stewart 							 * tp1, sctp_next);
7258830d754dSRandall Stewart 							 */
7259830d754dSRandall Stewart 							if (tp1->data) {
7260830d754dSRandall Stewart 								/*
7261830d754dSRandall Stewart 								 * sa_ignore
7262830d754dSRandall Stewart 								 * NO_NULL_CH
7263830d754dSRandall Stewart 								 * K
7264830d754dSRandall Stewart 								 */
7265830d754dSRandall Stewart 								sctp_free_bufspace(stcb, asoc, tp1, 1);
7266830d754dSRandall Stewart 								sctp_m_freem(tp1->data);
7267830d754dSRandall Stewart 							}
7268830d754dSRandall Stewart 							tp1->data = NULL;
7269830d754dSRandall Stewart 							/*
7270830d754dSRandall Stewart 							 * asoc->sent_queue_c
7271830d754dSRandall Stewart 							 * nt--;
7272830d754dSRandall Stewart 							 */
7273830d754dSRandall Stewart 							/*
7274830d754dSRandall Stewart 							 * sctp_free_a_chunk(
7275830d754dSRandall Stewart 							 * stcb, tp1);
7276830d754dSRandall Stewart 							 */
7277830d754dSRandall Stewart 							wake_him++;
7278830d754dSRandall Stewart 						}
7279830d754dSRandall Stewart 						break;
7280830d754dSRandall Stewart 					}	/* if (tp1->TSN_seq == j) */
7281830d754dSRandall Stewart 					if (compare_with_wrap(tp1->rec.data.TSN_seq, j,
7282830d754dSRandall Stewart 					    MAX_TSN))
7283830d754dSRandall Stewart 						break;
7284830d754dSRandall Stewart 					tp1 = TAILQ_NEXT(tp1, sctp_next);
7285830d754dSRandall Stewart 				}	/* end while (tp1) */
7286830d754dSRandall Stewart 
7287830d754dSRandall Stewart 			}	/* end for (j = nrFragStart */
7288830d754dSRandall Stewart 
7289830d754dSRandall Stewart 			nr_frag = (struct sctp_nr_gap_ack_block *)sctp_m_getptr(m, *offset,
7290830d754dSRandall Stewart 			    sizeof(struct sctp_nr_gap_ack_block), (uint8_t *) & nr_block);
7291830d754dSRandall Stewart 			*offset += sizeof(nr_block);
7292830d754dSRandall Stewart 			if (nr_frag == NULL) {
7293830d754dSRandall Stewart 				break;
7294830d754dSRandall Stewart 			}
7295830d754dSRandall Stewart 		}		/* end of if(!all_bit) */
7296830d754dSRandall Stewart 	}
7297830d754dSRandall Stewart 	/*
7298830d754dSRandall Stewart 	 * EY- wake up the socket if things have been removed from the sent
7299830d754dSRandall Stewart 	 * queue
7300830d754dSRandall Stewart 	 */
7301830d754dSRandall Stewart 	if ((wake_him) && (stcb->sctp_socket)) {
7302830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7303830d754dSRandall Stewart 		struct socket *so;
7304830d754dSRandall Stewart 
7305830d754dSRandall Stewart #endif
7306830d754dSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
7307830d754dSRandall Stewart 		/*
7308830d754dSRandall Stewart 		 * if (SCTP_BASE_SYSCTL(sctp_logging_level) &
7309830d754dSRandall Stewart 		 * SCTP_WAKE_LOGGING_ENABLE) { sctp_wakeup_log(stcb,
7310830d754dSRandall Stewart 		 * cum_ack, wake_him, SCTP_WAKESND_FROM_SACK);}
7311830d754dSRandall Stewart 		 */
7312830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7313830d754dSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
7314830d754dSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
7315830d754dSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
7316830d754dSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
7317830d754dSRandall Stewart 		SCTP_TCB_LOCK(stcb);
7318830d754dSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
7319830d754dSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
7320830d754dSRandall Stewart 			/* assoc was freed while we were unlocked */
7321830d754dSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
7322830d754dSRandall Stewart 			return;
7323830d754dSRandall Stewart 		}
7324830d754dSRandall Stewart #endif
7325830d754dSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
7326830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7327830d754dSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
7328830d754dSRandall Stewart #endif
7329830d754dSRandall Stewart 	}			/* else { if
7330830d754dSRandall Stewart 				 * (SCTP_BASE_SYSCTL(sctp_logging_level) &
7331830d754dSRandall Stewart 				 * SCTP_WAKE_LOGGING_ENABLE) {
7332830d754dSRandall Stewart 				 * sctp_wakeup_log(stcb, cum_ack, wake_him,
7333830d754dSRandall Stewart 				 * SCTP_NOWAKE_FROM_SACK); } } */
7334830d754dSRandall Stewart }
7335830d754dSRandall Stewart 
7336830d754dSRandall Stewart /* EY- nr_sack */
7337830d754dSRandall Stewart /* Identifies the non-renegable tsns that are revoked*/
7338830d754dSRandall Stewart static void
7339830d754dSRandall Stewart sctp_check_for_nr_revoked(struct sctp_tcb *stcb,
7340830d754dSRandall Stewart     struct sctp_association *asoc, uint32_t cumack,
7341830d754dSRandall Stewart     u_long biggest_tsn_acked)
7342830d754dSRandall Stewart {
7343830d754dSRandall Stewart 	struct sctp_tmit_chunk *tp1;
7344830d754dSRandall Stewart 
7345830d754dSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
7346830d754dSRandall Stewart 	while (tp1) {
7347830d754dSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq, cumack,
7348830d754dSRandall Stewart 		    MAX_TSN)) {
7349830d754dSRandall Stewart 			/*
7350830d754dSRandall Stewart 			 * ok this guy is either ACK or MARKED. If it is
7351830d754dSRandall Stewart 			 * ACKED it has been previously acked but not this
7352830d754dSRandall Stewart 			 * time i.e. revoked.  If it is MARKED it was ACK'ed
7353830d754dSRandall Stewart 			 * again.
7354830d754dSRandall Stewart 			 */
7355830d754dSRandall Stewart 			if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
7356830d754dSRandall Stewart 			    MAX_TSN))
7357830d754dSRandall Stewart 				break;
7358830d754dSRandall Stewart 
7359830d754dSRandall Stewart 
7360830d754dSRandall Stewart 			if (tp1->sent == SCTP_DATAGRAM_NR_ACKED) {
7361830d754dSRandall Stewart 				/*
7362830d754dSRandall Stewart 				 * EY! a non-renegable TSN is revoked, need
7363830d754dSRandall Stewart 				 * to abort the association
7364830d754dSRandall Stewart 				 */
7365830d754dSRandall Stewart 				/*
7366830d754dSRandall Stewart 				 * EY TODO: put in the code to abort the
7367830d754dSRandall Stewart 				 * assoc.
7368830d754dSRandall Stewart 				 */
7369830d754dSRandall Stewart 				return;
7370830d754dSRandall Stewart 			} else if (tp1->sent == SCTP_DATAGRAM_NR_MARKED) {
7371830d754dSRandall Stewart 				/* it has been re-acked in this SACK */
7372830d754dSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_NR_ACKED;
7373830d754dSRandall Stewart 			}
7374830d754dSRandall Stewart 		}
7375830d754dSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT)
7376830d754dSRandall Stewart 			break;
7377830d754dSRandall Stewart 		tp1 = TAILQ_NEXT(tp1, sctp_next);
7378830d754dSRandall Stewart 	}
7379830d754dSRandall Stewart }
7380830d754dSRandall Stewart 
7381830d754dSRandall Stewart /* EY! nr_sack version of sctp_handle_sack, nr_gap_ack processing should be added to this method*/
7382830d754dSRandall Stewart void
7383830d754dSRandall Stewart sctp_handle_nr_sack(struct mbuf *m, int offset,
7384830d754dSRandall Stewart     struct sctp_nr_sack_chunk *ch, struct sctp_tcb *stcb,
7385830d754dSRandall Stewart     struct sctp_nets *net_from, int *abort_now, int nr_sack_len, uint32_t rwnd)
7386830d754dSRandall Stewart {
7387830d754dSRandall Stewart 	struct sctp_association *asoc;
7388830d754dSRandall Stewart 
7389830d754dSRandall Stewart 	/* EY sack */
7390830d754dSRandall Stewart 	struct sctp_nr_sack *nr_sack;
7391830d754dSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
7392830d754dSRandall Stewart 	uint32_t cum_ack, last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked,
7393830d754dSRandall Stewart 	         this_sack_lowest_newack;
7394830d754dSRandall Stewart 	uint32_t sav_cum_ack;
7395830d754dSRandall Stewart 
7396830d754dSRandall Stewart 	/* EY num_seg */
7397830d754dSRandall Stewart 	uint16_t num_seg, num_nr_seg, num_dup;
7398830d754dSRandall Stewart 	uint16_t wake_him = 0;
7399830d754dSRandall Stewart 	unsigned int nr_sack_length;
7400830d754dSRandall Stewart 	uint32_t send_s = 0;
7401830d754dSRandall Stewart 	long j;
7402830d754dSRandall Stewart 	int accum_moved = 0;
7403830d754dSRandall Stewart 	int will_exit_fast_recovery = 0;
7404830d754dSRandall Stewart 	uint32_t a_rwnd, old_rwnd;
7405830d754dSRandall Stewart 	int win_probe_recovery = 0;
7406830d754dSRandall Stewart 	int win_probe_recovered = 0;
7407830d754dSRandall Stewart 	struct sctp_nets *net = NULL;
7408830d754dSRandall Stewart 	int nonce_sum_flag, ecn_seg_sums = 0, all_bit;
7409830d754dSRandall Stewart 	int done_once;
7410830d754dSRandall Stewart 	uint8_t reneged_all = 0;
7411830d754dSRandall Stewart 	uint8_t cmt_dac_flag;
7412830d754dSRandall Stewart 
7413830d754dSRandall Stewart 	/*
7414830d754dSRandall Stewart 	 * we take any chance we can to service our queues since we cannot
7415830d754dSRandall Stewart 	 * get awoken when the socket is read from :<
7416830d754dSRandall Stewart 	 */
7417830d754dSRandall Stewart 	/*
7418830d754dSRandall Stewart 	 * Now perform the actual SACK handling: 1) Verify that it is not an
7419830d754dSRandall Stewart 	 * old sack, if so discard. 2) If there is nothing left in the send
7420830d754dSRandall Stewart 	 * queue (cum-ack is equal to last acked) then you have a duplicate
7421830d754dSRandall Stewart 	 * too, update any rwnd change and verify no timers are running.
7422830d754dSRandall Stewart 	 * then return. 3) Process any new consequtive data i.e. cum-ack
7423830d754dSRandall Stewart 	 * moved process these first and note that it moved. 4) Process any
7424830d754dSRandall Stewart 	 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
7425830d754dSRandall Stewart 	 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
7426830d754dSRandall Stewart 	 * sync up flightsizes and things, stop all timers and also check
7427830d754dSRandall Stewart 	 * for shutdown_pending state. If so then go ahead and send off the
7428830d754dSRandall Stewart 	 * shutdown. If in shutdown recv, send off the shutdown-ack and
7429830d754dSRandall Stewart 	 * start that timer, Ret. 9) Strike any non-acked things and do FR
7430830d754dSRandall Stewart 	 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
7431830d754dSRandall Stewart 	 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
7432830d754dSRandall Stewart 	 * if in shutdown_recv state.
7433830d754dSRandall Stewart 	 */
7434830d754dSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
7435830d754dSRandall Stewart 	nr_sack = &ch->nr_sack;
7436830d754dSRandall Stewart 	/* CMT DAC algo */
7437830d754dSRandall Stewart 	this_sack_lowest_newack = 0;
7438830d754dSRandall Stewart 	j = 0;
7439830d754dSRandall Stewart 	nr_sack_length = (unsigned int)nr_sack_len;
7440830d754dSRandall Stewart 	/* ECN Nonce */
7441830d754dSRandall Stewart 	SCTP_STAT_INCR(sctps_slowpath_sack);
7442830d754dSRandall Stewart 	nonce_sum_flag = ch->ch.chunk_flags & SCTP_SACK_NONCE_SUM;
7443830d754dSRandall Stewart 	cum_ack = last_tsn = ntohl(nr_sack->cum_tsn_ack);
7444830d754dSRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
7445830d754dSRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
7446830d754dSRandall Stewart 	stcb->asoc.cumack_log_at++;
7447830d754dSRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
7448830d754dSRandall Stewart 		stcb->asoc.cumack_log_at = 0;
7449830d754dSRandall Stewart 	}
7450830d754dSRandall Stewart #endif
7451830d754dSRandall Stewart 	all_bit = ch->ch.chunk_flags & SCTP_NR_SACK_ALL_BIT;
7452830d754dSRandall Stewart 	num_seg = ntohs(nr_sack->num_gap_ack_blks);
7453830d754dSRandall Stewart 	num_nr_seg = ntohs(nr_sack->num_nr_gap_ack_blks);
7454830d754dSRandall Stewart 	if (all_bit)
7455830d754dSRandall Stewart 		num_seg = num_nr_seg;
7456830d754dSRandall Stewart 	a_rwnd = rwnd;
7457830d754dSRandall Stewart 
7458830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
7459830d754dSRandall Stewart 		sctp_misc_ints(SCTP_SACK_LOG_NORMAL, cum_ack,
7460830d754dSRandall Stewart 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
7461830d754dSRandall Stewart 	}
7462830d754dSRandall Stewart 	/* CMT DAC algo */
7463830d754dSRandall Stewart 	cmt_dac_flag = ch->ch.chunk_flags & SCTP_SACK_CMT_DAC;
7464830d754dSRandall Stewart 	num_dup = ntohs(nr_sack->num_dup_tsns);
7465830d754dSRandall Stewart 
7466830d754dSRandall Stewart 	old_rwnd = stcb->asoc.peers_rwnd;
7467830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
7468830d754dSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
7469830d754dSRandall Stewart 		    stcb->asoc.overall_error_count,
7470830d754dSRandall Stewart 		    0,
7471830d754dSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
7472830d754dSRandall Stewart 		    __LINE__);
7473830d754dSRandall Stewart 	}
7474830d754dSRandall Stewart 	stcb->asoc.overall_error_count = 0;
7475830d754dSRandall Stewart 	asoc = &stcb->asoc;
7476830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
7477830d754dSRandall Stewart 		sctp_log_sack(asoc->last_acked_seq,
7478830d754dSRandall Stewart 		    cum_ack,
7479830d754dSRandall Stewart 		    0,
7480830d754dSRandall Stewart 		    num_seg,
7481830d754dSRandall Stewart 		    num_dup,
7482830d754dSRandall Stewart 		    SCTP_LOG_NEW_SACK);
7483830d754dSRandall Stewart 	}
7484830d754dSRandall Stewart 	if ((num_dup) && (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_FR_LOGGING_ENABLE | SCTP_EARLYFR_LOGGING_ENABLE))) {
7485830d754dSRandall Stewart 		int off_to_dup, iii;
7486830d754dSRandall Stewart 		uint32_t *dupdata, dblock;
7487830d754dSRandall Stewart 
7488830d754dSRandall Stewart 		/* EY! gotta be careful here */
7489830d754dSRandall Stewart 		if (all_bit) {
7490830d754dSRandall Stewart 			off_to_dup = (num_nr_seg * sizeof(struct sctp_nr_gap_ack_block)) +
7491830d754dSRandall Stewart 			    sizeof(struct sctp_nr_sack_chunk);
7492830d754dSRandall Stewart 		} else {
7493830d754dSRandall Stewart 			off_to_dup = (num_seg * sizeof(struct sctp_gap_ack_block)) +
7494830d754dSRandall Stewart 			    (num_nr_seg * sizeof(struct sctp_nr_gap_ack_block)) + sizeof(struct sctp_nr_sack_chunk);
7495830d754dSRandall Stewart 		}
7496830d754dSRandall Stewart 		if ((off_to_dup + (num_dup * sizeof(uint32_t))) <= nr_sack_length) {
7497830d754dSRandall Stewart 			dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
7498830d754dSRandall Stewart 			    sizeof(uint32_t), (uint8_t *) & dblock);
7499830d754dSRandall Stewart 			off_to_dup += sizeof(uint32_t);
7500830d754dSRandall Stewart 			if (dupdata) {
7501830d754dSRandall Stewart 				for (iii = 0; iii < num_dup; iii++) {
7502830d754dSRandall Stewart 					sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
7503830d754dSRandall Stewart 					dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
7504830d754dSRandall Stewart 					    sizeof(uint32_t), (uint8_t *) & dblock);
7505830d754dSRandall Stewart 					if (dupdata == NULL)
7506830d754dSRandall Stewart 						break;
7507830d754dSRandall Stewart 					off_to_dup += sizeof(uint32_t);
7508830d754dSRandall Stewart 				}
7509830d754dSRandall Stewart 			}
7510830d754dSRandall Stewart 		} else {
7511830d754dSRandall Stewart 			SCTP_PRINTF("Size invalid offset to dups:%d number dups:%d nr_sack_len:%d num gaps:%d num nr_gaps:%d\n",
7512830d754dSRandall Stewart 			    off_to_dup, num_dup, nr_sack_length, num_seg, num_nr_seg);
7513830d754dSRandall Stewart 		}
7514830d754dSRandall Stewart 	}
7515830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
7516830d754dSRandall Stewart 		/* reality check */
7517830d754dSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
7518830d754dSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
7519830d754dSRandall Stewart 			    sctpchunk_listhead);
7520830d754dSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
7521830d754dSRandall Stewart 		} else {
7522830d754dSRandall Stewart 			send_s = asoc->sending_seq;
7523830d754dSRandall Stewart 		}
7524830d754dSRandall Stewart 		if (cum_ack == send_s ||
7525830d754dSRandall Stewart 		    compare_with_wrap(cum_ack, send_s, MAX_TSN)) {
7526830d754dSRandall Stewart #ifndef INVARIANTS
7527830d754dSRandall Stewart 			struct mbuf *oper;
7528830d754dSRandall Stewart 
7529830d754dSRandall Stewart #endif
7530830d754dSRandall Stewart #ifdef INVARIANTS
7531830d754dSRandall Stewart 	hopeless_peer:
7532830d754dSRandall Stewart 			panic("Impossible sack 1");
7533830d754dSRandall Stewart #else
7534830d754dSRandall Stewart 
7535830d754dSRandall Stewart 
7536830d754dSRandall Stewart 			/*
7537830d754dSRandall Stewart 			 * no way, we have not even sent this TSN out yet.
7538830d754dSRandall Stewart 			 * Peer is hopelessly messed up with us.
7539830d754dSRandall Stewart 			 */
7540830d754dSRandall Stewart 	hopeless_peer:
7541830d754dSRandall Stewart 			*abort_now = 1;
7542830d754dSRandall Stewart 			/* XXX */
7543830d754dSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
7544830d754dSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
7545830d754dSRandall Stewart 			if (oper) {
7546830d754dSRandall Stewart 				struct sctp_paramhdr *ph;
7547830d754dSRandall Stewart 				uint32_t *ippp;
7548830d754dSRandall Stewart 
7549830d754dSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
7550830d754dSRandall Stewart 				    sizeof(uint32_t);
7551830d754dSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
7552830d754dSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
7553830d754dSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
7554830d754dSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
7555830d754dSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
7556830d754dSRandall Stewart 			}
7557830d754dSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
7558830d754dSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
7559830d754dSRandall Stewart 			return;
7560830d754dSRandall Stewart #endif
7561830d754dSRandall Stewart 		}
7562830d754dSRandall Stewart 	}
7563830d754dSRandall Stewart 	/**********************/
7564830d754dSRandall Stewart 	/* 1) check the range */
7565830d754dSRandall Stewart 	/**********************/
7566830d754dSRandall Stewart 	if (compare_with_wrap(asoc->last_acked_seq, last_tsn, MAX_TSN)) {
7567830d754dSRandall Stewart 		/* acking something behind */
7568830d754dSRandall Stewart 		return;
7569830d754dSRandall Stewart 	}
7570830d754dSRandall Stewart 	sav_cum_ack = asoc->last_acked_seq;
7571830d754dSRandall Stewart 
7572830d754dSRandall Stewart 	/* update the Rwnd of the peer */
7573830d754dSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue) &&
7574830d754dSRandall Stewart 	    TAILQ_EMPTY(&asoc->send_queue) &&
7575830d754dSRandall Stewart 	    (asoc->stream_queue_cnt == 0)
7576830d754dSRandall Stewart 	    ) {
7577830d754dSRandall Stewart 		/* nothing left on send/sent and strmq */
7578830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
7579830d754dSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
7580830d754dSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
7581830d754dSRandall Stewart 		}
7582830d754dSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
7583830d754dSRandall Stewart 		if (asoc->sent_queue_retran_cnt) {
7584830d754dSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
7585830d754dSRandall Stewart 		}
7586830d754dSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
7587830d754dSRandall Stewart 			/* SWS sender side engages */
7588830d754dSRandall Stewart 			asoc->peers_rwnd = 0;
7589830d754dSRandall Stewart 		}
7590830d754dSRandall Stewart 		/* stop any timers */
7591830d754dSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7592830d754dSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
7593830d754dSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
7594830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
7595830d754dSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
7596830d754dSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck1);
7597830d754dSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
7598830d754dSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
7599830d754dSRandall Stewart 				}
7600830d754dSRandall Stewart 			}
7601830d754dSRandall Stewart 			net->partial_bytes_acked = 0;
7602830d754dSRandall Stewart 			net->flight_size = 0;
7603830d754dSRandall Stewart 		}
7604830d754dSRandall Stewart 		asoc->total_flight = 0;
7605830d754dSRandall Stewart 		asoc->total_flight_count = 0;
7606830d754dSRandall Stewart 		return;
7607830d754dSRandall Stewart 	}
7608830d754dSRandall Stewart 	/*
7609830d754dSRandall Stewart 	 * We init netAckSz and netAckSz2 to 0. These are used to track 2
7610830d754dSRandall Stewart 	 * things. The total byte count acked is tracked in netAckSz AND
7611830d754dSRandall Stewart 	 * netAck2 is used to track the total bytes acked that are un-
7612830d754dSRandall Stewart 	 * amibguious and were never retransmitted. We track these on a per
7613830d754dSRandall Stewart 	 * destination address basis.
7614830d754dSRandall Stewart 	 */
7615830d754dSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7616830d754dSRandall Stewart 		net->prev_cwnd = net->cwnd;
7617830d754dSRandall Stewart 		net->net_ack = 0;
7618830d754dSRandall Stewart 		net->net_ack2 = 0;
7619830d754dSRandall Stewart 
7620830d754dSRandall Stewart 		/*
7621830d754dSRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
7622830d754dSRandall Stewart 		 * SACK processing
7623830d754dSRandall Stewart 		 */
7624830d754dSRandall Stewart 		net->new_pseudo_cumack = 0;
7625830d754dSRandall Stewart 		net->will_exit_fast_recovery = 0;
7626830d754dSRandall Stewart 	}
7627830d754dSRandall Stewart 	/* process the new consecutive TSN first */
7628830d754dSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
7629830d754dSRandall Stewart 	while (tp1) {
7630830d754dSRandall Stewart 		if (compare_with_wrap(last_tsn, tp1->rec.data.TSN_seq,
7631830d754dSRandall Stewart 		    MAX_TSN) ||
7632830d754dSRandall Stewart 		    last_tsn == tp1->rec.data.TSN_seq) {
7633830d754dSRandall Stewart 			if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
7634830d754dSRandall Stewart 				/*
7635830d754dSRandall Stewart 				 * ECN Nonce: Add the nonce to the sender's
7636830d754dSRandall Stewart 				 * nonce sum
7637830d754dSRandall Stewart 				 */
7638830d754dSRandall Stewart 				asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
7639830d754dSRandall Stewart 				accum_moved = 1;
7640830d754dSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
7641830d754dSRandall Stewart 					/*
7642830d754dSRandall Stewart 					 * If it is less than ACKED, it is
7643830d754dSRandall Stewart 					 * now no-longer in flight. Higher
7644830d754dSRandall Stewart 					 * values may occur during marking
7645830d754dSRandall Stewart 					 */
7646830d754dSRandall Stewart 					if ((tp1->whoTo->dest_state &
7647830d754dSRandall Stewart 					    SCTP_ADDR_UNCONFIRMED) &&
7648830d754dSRandall Stewart 					    (tp1->snd_count < 2)) {
7649830d754dSRandall Stewart 						/*
7650830d754dSRandall Stewart 						 * If there was no retran
7651830d754dSRandall Stewart 						 * and the address is
7652830d754dSRandall Stewart 						 * un-confirmed and we sent
7653830d754dSRandall Stewart 						 * there and are now
7654830d754dSRandall Stewart 						 * sacked.. its confirmed,
7655830d754dSRandall Stewart 						 * mark it so.
7656830d754dSRandall Stewart 						 */
7657830d754dSRandall Stewart 						tp1->whoTo->dest_state &=
7658830d754dSRandall Stewart 						    ~SCTP_ADDR_UNCONFIRMED;
7659830d754dSRandall Stewart 					}
7660830d754dSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
7661830d754dSRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
7662830d754dSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
7663830d754dSRandall Stewart 							    tp1->whoTo->flight_size,
7664830d754dSRandall Stewart 							    tp1->book_size,
7665830d754dSRandall Stewart 							    (uintptr_t) tp1->whoTo,
7666830d754dSRandall Stewart 							    tp1->rec.data.TSN_seq);
7667830d754dSRandall Stewart 						}
7668830d754dSRandall Stewart 						sctp_flight_size_decrease(tp1);
7669830d754dSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
7670830d754dSRandall Stewart 					}
7671830d754dSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
7672830d754dSRandall Stewart 
7673830d754dSRandall Stewart 					/* CMT SFR and DAC algos */
7674830d754dSRandall Stewart 					this_sack_lowest_newack = tp1->rec.data.TSN_seq;
7675830d754dSRandall Stewart 					tp1->whoTo->saw_newack = 1;
7676830d754dSRandall Stewart 
7677830d754dSRandall Stewart 					if (tp1->snd_count < 2) {
7678830d754dSRandall Stewart 						/*
7679830d754dSRandall Stewart 						 * True non-retransmited
7680830d754dSRandall Stewart 						 * chunk
7681830d754dSRandall Stewart 						 */
7682830d754dSRandall Stewart 						tp1->whoTo->net_ack2 +=
7683830d754dSRandall Stewart 						    tp1->send_size;
7684830d754dSRandall Stewart 
7685830d754dSRandall Stewart 						/* update RTO too? */
7686830d754dSRandall Stewart 						if (tp1->do_rtt) {
7687830d754dSRandall Stewart 							tp1->whoTo->RTO =
7688830d754dSRandall Stewart 							    sctp_calculate_rto(stcb,
7689830d754dSRandall Stewart 							    asoc, tp1->whoTo,
7690830d754dSRandall Stewart 							    &tp1->sent_rcv_time,
7691830d754dSRandall Stewart 							    sctp_align_safe_nocopy);
7692830d754dSRandall Stewart 							tp1->do_rtt = 0;
7693830d754dSRandall Stewart 						}
7694830d754dSRandall Stewart 					}
7695830d754dSRandall Stewart 					/*
7696830d754dSRandall Stewart 					 * CMT: CUCv2 algorithm. From the
7697830d754dSRandall Stewart 					 * cumack'd TSNs, for each TSN being
7698830d754dSRandall Stewart 					 * acked for the first time, set the
7699830d754dSRandall Stewart 					 * following variables for the
7700830d754dSRandall Stewart 					 * corresp destination.
7701830d754dSRandall Stewart 					 * new_pseudo_cumack will trigger a
7702830d754dSRandall Stewart 					 * cwnd update.
7703830d754dSRandall Stewart 					 * find_(rtx_)pseudo_cumack will
7704830d754dSRandall Stewart 					 * trigger search for the next
7705830d754dSRandall Stewart 					 * expected (rtx-)pseudo-cumack.
7706830d754dSRandall Stewart 					 */
7707830d754dSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
7708830d754dSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
7709830d754dSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
7710830d754dSRandall Stewart 
7711830d754dSRandall Stewart 
7712830d754dSRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
7713830d754dSRandall Stewart 						sctp_log_sack(asoc->last_acked_seq,
7714830d754dSRandall Stewart 						    cum_ack,
7715830d754dSRandall Stewart 						    tp1->rec.data.TSN_seq,
7716830d754dSRandall Stewart 						    0,
7717830d754dSRandall Stewart 						    0,
7718830d754dSRandall Stewart 						    SCTP_LOG_TSN_ACKED);
7719830d754dSRandall Stewart 					}
7720830d754dSRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7721830d754dSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
7722830d754dSRandall Stewart 					}
7723830d754dSRandall Stewart 				}
7724830d754dSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
7725830d754dSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
7726830d754dSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
7727830d754dSRandall Stewart 					sctp_audit_log(0xB3,
7728830d754dSRandall Stewart 					    (asoc->sent_queue_retran_cnt & 0x000000ff));
7729830d754dSRandall Stewart #endif
7730830d754dSRandall Stewart 				}
7731830d754dSRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
7732830d754dSRandall Stewart 					/* deflate the cwnd */
7733830d754dSRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
7734830d754dSRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
7735830d754dSRandall Stewart 				}
7736830d754dSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
7737830d754dSRandall Stewart 			}
7738830d754dSRandall Stewart 		} else {
7739830d754dSRandall Stewart 			break;
7740830d754dSRandall Stewart 		}
7741830d754dSRandall Stewart 		tp1 = TAILQ_NEXT(tp1, sctp_next);
7742830d754dSRandall Stewart 	}
7743830d754dSRandall Stewart 	biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
7744830d754dSRandall Stewart 	/* always set this up to cum-ack */
7745830d754dSRandall Stewart 	asoc->this_sack_highest_gap = last_tsn;
7746830d754dSRandall Stewart 
7747830d754dSRandall Stewart 	/* Move offset up to point to gaps/dups */
7748830d754dSRandall Stewart 	offset += sizeof(struct sctp_nr_sack_chunk);
7749830d754dSRandall Stewart 	if (((num_seg * (sizeof(struct sctp_gap_ack_block))) + sizeof(struct sctp_nr_sack_chunk)) > nr_sack_length) {
7750830d754dSRandall Stewart 
7751830d754dSRandall Stewart 		/* skip corrupt segments */
7752830d754dSRandall Stewart 		goto skip_segments;
7753830d754dSRandall Stewart 	}
7754830d754dSRandall Stewart 	if (num_seg > 0) {
7755830d754dSRandall Stewart 
7756830d754dSRandall Stewart 		/*
7757830d754dSRandall Stewart 		 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
7758830d754dSRandall Stewart 		 * to be greater than the cumack. Also reset saw_newack to 0
7759830d754dSRandall Stewart 		 * for all dests.
7760830d754dSRandall Stewart 		 */
7761830d754dSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7762830d754dSRandall Stewart 			net->saw_newack = 0;
7763830d754dSRandall Stewart 			net->this_sack_highest_newack = last_tsn;
7764830d754dSRandall Stewart 		}
7765830d754dSRandall Stewart 
7766830d754dSRandall Stewart 		/*
7767830d754dSRandall Stewart 		 * thisSackHighestGap will increase while handling NEW
7768830d754dSRandall Stewart 		 * segments this_sack_highest_newack will increase while
7769830d754dSRandall Stewart 		 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
7770830d754dSRandall Stewart 		 * used for CMT DAC algo. saw_newack will also change.
7771830d754dSRandall Stewart 		 */
7772830d754dSRandall Stewart 
7773830d754dSRandall Stewart 		sctp_handle_nr_sack_segments(m, &offset, stcb, asoc, ch, last_tsn,
7774830d754dSRandall Stewart 		    &biggest_tsn_acked, &biggest_tsn_newly_acked, &this_sack_lowest_newack,
7775830d754dSRandall Stewart 		    num_seg, num_nr_seg, &ecn_seg_sums);
7776830d754dSRandall Stewart 
7777830d754dSRandall Stewart 
7778830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
7779830d754dSRandall Stewart 			/*
7780830d754dSRandall Stewart 			 * validate the biggest_tsn_acked in the gap acks if
7781830d754dSRandall Stewart 			 * strict adherence is wanted.
7782830d754dSRandall Stewart 			 */
7783830d754dSRandall Stewart 			if ((biggest_tsn_acked == send_s) ||
7784830d754dSRandall Stewart 			    (compare_with_wrap(biggest_tsn_acked, send_s, MAX_TSN))) {
7785830d754dSRandall Stewart 				/*
7786830d754dSRandall Stewart 				 * peer is either confused or we are under
7787830d754dSRandall Stewart 				 * attack. We must abort.
7788830d754dSRandall Stewart 				 */
7789830d754dSRandall Stewart 				goto hopeless_peer;
7790830d754dSRandall Stewart 			}
7791830d754dSRandall Stewart 		}
7792830d754dSRandall Stewart 	}
7793830d754dSRandall Stewart skip_segments:
7794830d754dSRandall Stewart 	/*******************************************/
7795830d754dSRandall Stewart 	/* cancel ALL T3-send timer if accum moved */
7796830d754dSRandall Stewart 	/*******************************************/
7797830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
7798830d754dSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7799830d754dSRandall Stewart 			if (net->new_pseudo_cumack)
7800830d754dSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
7801830d754dSRandall Stewart 				    stcb, net,
7802830d754dSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
7803830d754dSRandall Stewart 
7804830d754dSRandall Stewart 		}
7805830d754dSRandall Stewart 	} else {
7806830d754dSRandall Stewart 		if (accum_moved) {
7807830d754dSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7808830d754dSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
7809830d754dSRandall Stewart 				    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
7810830d754dSRandall Stewart 			}
7811830d754dSRandall Stewart 		}
7812830d754dSRandall Stewart 	}
7813830d754dSRandall Stewart 	/********************************************/
7814830d754dSRandall Stewart 	/* drop the acked chunks from the sendqueue */
7815830d754dSRandall Stewart 	/********************************************/
7816830d754dSRandall Stewart 	asoc->last_acked_seq = cum_ack;
7817830d754dSRandall Stewart 
7818830d754dSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
7819830d754dSRandall Stewart 	if (tp1 == NULL)
7820830d754dSRandall Stewart 		goto done_with_it;
7821830d754dSRandall Stewart 	do {
7822830d754dSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq, cum_ack,
7823830d754dSRandall Stewart 		    MAX_TSN)) {
7824830d754dSRandall Stewart 			break;
7825830d754dSRandall Stewart 		}
7826830d754dSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
7827830d754dSRandall Stewart 			/* no more sent on list */
7828830d754dSRandall Stewart 			printf("Warning, tp1->sent == %d and its now acked?\n",
7829830d754dSRandall Stewart 			    tp1->sent);
7830830d754dSRandall Stewart 		}
7831830d754dSRandall Stewart 		tp2 = TAILQ_NEXT(tp1, sctp_next);
7832830d754dSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
7833830d754dSRandall Stewart 		if (tp1->pr_sctp_on) {
7834830d754dSRandall Stewart 			if (asoc->pr_sctp_cnt != 0)
7835830d754dSRandall Stewart 				asoc->pr_sctp_cnt--;
7836830d754dSRandall Stewart 		}
7837830d754dSRandall Stewart 		if ((TAILQ_FIRST(&asoc->sent_queue) == NULL) &&
7838830d754dSRandall Stewart 		    (asoc->total_flight > 0)) {
7839830d754dSRandall Stewart #ifdef INVARIANTS
7840830d754dSRandall Stewart 			panic("Warning flight size is postive and should be 0");
7841830d754dSRandall Stewart #else
7842830d754dSRandall Stewart 			SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
7843830d754dSRandall Stewart 			    asoc->total_flight);
7844830d754dSRandall Stewart #endif
7845830d754dSRandall Stewart 			asoc->total_flight = 0;
7846830d754dSRandall Stewart 		}
7847830d754dSRandall Stewart 		if (tp1->data) {
7848830d754dSRandall Stewart 			/* sa_ignore NO_NULL_CHK */
7849830d754dSRandall Stewart 			sctp_free_bufspace(stcb, asoc, tp1, 1);
7850830d754dSRandall Stewart 			sctp_m_freem(tp1->data);
7851830d754dSRandall Stewart 			if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
7852830d754dSRandall Stewart 				asoc->sent_queue_cnt_removeable--;
7853830d754dSRandall Stewart 			}
7854830d754dSRandall Stewart 		}
7855830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
7856830d754dSRandall Stewart 			sctp_log_sack(asoc->last_acked_seq,
7857830d754dSRandall Stewart 			    cum_ack,
7858830d754dSRandall Stewart 			    tp1->rec.data.TSN_seq,
7859830d754dSRandall Stewart 			    0,
7860830d754dSRandall Stewart 			    0,
7861830d754dSRandall Stewart 			    SCTP_LOG_FREE_SENT);
7862830d754dSRandall Stewart 		}
7863830d754dSRandall Stewart 		tp1->data = NULL;
7864830d754dSRandall Stewart 		asoc->sent_queue_cnt--;
7865830d754dSRandall Stewart 		sctp_free_a_chunk(stcb, tp1);
7866830d754dSRandall Stewart 		wake_him++;
7867830d754dSRandall Stewart 		tp1 = tp2;
7868830d754dSRandall Stewart 	} while (tp1 != NULL);
7869830d754dSRandall Stewart 
7870830d754dSRandall Stewart done_with_it:
7871830d754dSRandall Stewart 	/* sa_ignore NO_NULL_CHK */
7872830d754dSRandall Stewart 	if ((wake_him) && (stcb->sctp_socket)) {
7873830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7874830d754dSRandall Stewart 		struct socket *so;
7875830d754dSRandall Stewart 
7876830d754dSRandall Stewart #endif
7877830d754dSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
7878830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
7879830d754dSRandall Stewart 			sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_WAKESND_FROM_SACK);
7880830d754dSRandall Stewart 		}
7881830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7882830d754dSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
7883830d754dSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
7884830d754dSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
7885830d754dSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
7886830d754dSRandall Stewart 		SCTP_TCB_LOCK(stcb);
7887830d754dSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
7888830d754dSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
7889830d754dSRandall Stewart 			/* assoc was freed while we were unlocked */
7890830d754dSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
7891830d754dSRandall Stewart 			return;
7892830d754dSRandall Stewart 		}
7893830d754dSRandall Stewart #endif
7894830d754dSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
7895830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7896830d754dSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
7897830d754dSRandall Stewart #endif
7898830d754dSRandall Stewart 	} else {
7899830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
7900830d754dSRandall Stewart 			sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_NOWAKE_FROM_SACK);
7901830d754dSRandall Stewart 		}
7902830d754dSRandall Stewart 	}
7903830d754dSRandall Stewart 
7904830d754dSRandall Stewart 	if (asoc->fast_retran_loss_recovery && accum_moved) {
7905830d754dSRandall Stewart 		if (compare_with_wrap(asoc->last_acked_seq,
7906830d754dSRandall Stewart 		    asoc->fast_recovery_tsn, MAX_TSN) ||
7907830d754dSRandall Stewart 		    asoc->last_acked_seq == asoc->fast_recovery_tsn) {
7908830d754dSRandall Stewart 			/* Setup so we will exit RFC2582 fast recovery */
7909830d754dSRandall Stewart 			will_exit_fast_recovery = 1;
7910830d754dSRandall Stewart 		}
7911830d754dSRandall Stewart 	}
7912830d754dSRandall Stewart 	/*
7913830d754dSRandall Stewart 	 * Check for revoked fragments:
7914830d754dSRandall Stewart 	 *
7915830d754dSRandall Stewart 	 * if Previous sack - Had no frags then we can't have any revoked if
7916830d754dSRandall Stewart 	 * Previous sack - Had frag's then - If we now have frags aka
7917830d754dSRandall Stewart 	 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
7918830d754dSRandall Stewart 	 * some of them. else - The peer revoked all ACKED fragments, since
7919830d754dSRandall Stewart 	 * we had some before and now we have NONE.
7920830d754dSRandall Stewart 	 */
7921830d754dSRandall Stewart 
7922830d754dSRandall Stewart 	if (num_seg)
7923830d754dSRandall Stewart 		sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
7924830d754dSRandall Stewart 
7925830d754dSRandall Stewart 	else if (asoc->saw_sack_with_frags) {
7926830d754dSRandall Stewart 		int cnt_revoked = 0;
7927830d754dSRandall Stewart 
7928830d754dSRandall Stewart 		tp1 = TAILQ_FIRST(&asoc->sent_queue);
7929830d754dSRandall Stewart 		if (tp1 != NULL) {
7930830d754dSRandall Stewart 			/* Peer revoked all dg's marked or acked */
7931830d754dSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
7932830d754dSRandall Stewart 				/*
7933830d754dSRandall Stewart 				 * EY- maybe check only if it is nr_acked
7934830d754dSRandall Stewart 				 * nr_marked may not be possible
7935830d754dSRandall Stewart 				 */
7936830d754dSRandall Stewart 				if ((tp1->sent == SCTP_DATAGRAM_NR_ACKED) ||
7937830d754dSRandall Stewart 				    (tp1->sent == SCTP_DATAGRAM_NR_MARKED)) {
7938830d754dSRandall Stewart 					/*
7939830d754dSRandall Stewart 					 * EY! - TODO: Something previously
7940830d754dSRandall Stewart 					 * nr_gapped is reneged, abort the
7941830d754dSRandall Stewart 					 * association
7942830d754dSRandall Stewart 					 */
7943830d754dSRandall Stewart 					return;
7944830d754dSRandall Stewart 				}
7945830d754dSRandall Stewart 				if ((tp1->sent > SCTP_DATAGRAM_RESEND) &&
7946830d754dSRandall Stewart 				    (tp1->sent < SCTP_FORWARD_TSN_SKIP)) {
7947830d754dSRandall Stewart 					tp1->sent = SCTP_DATAGRAM_SENT;
7948830d754dSRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
7949830d754dSRandall Stewart 						sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
7950830d754dSRandall Stewart 						    tp1->whoTo->flight_size,
7951830d754dSRandall Stewart 						    tp1->book_size,
7952830d754dSRandall Stewart 						    (uintptr_t) tp1->whoTo,
7953830d754dSRandall Stewart 						    tp1->rec.data.TSN_seq);
7954830d754dSRandall Stewart 					}
7955830d754dSRandall Stewart 					sctp_flight_size_increase(tp1);
7956830d754dSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
7957830d754dSRandall Stewart 					tp1->rec.data.chunk_was_revoked = 1;
7958830d754dSRandall Stewart 					/*
7959830d754dSRandall Stewart 					 * To ensure that this increase in
7960830d754dSRandall Stewart 					 * flightsize, which is artificial,
7961830d754dSRandall Stewart 					 * does not throttle the sender, we
7962830d754dSRandall Stewart 					 * also increase the cwnd
7963830d754dSRandall Stewart 					 * artificially.
7964830d754dSRandall Stewart 					 */
7965830d754dSRandall Stewart 					tp1->whoTo->cwnd += tp1->book_size;
7966830d754dSRandall Stewart 					cnt_revoked++;
7967830d754dSRandall Stewart 				}
7968830d754dSRandall Stewart 			}
7969830d754dSRandall Stewart 			if (cnt_revoked) {
7970830d754dSRandall Stewart 				reneged_all = 1;
7971830d754dSRandall Stewart 			}
7972830d754dSRandall Stewart 		}
7973830d754dSRandall Stewart 		asoc->saw_sack_with_frags = 0;
7974830d754dSRandall Stewart 	}
7975830d754dSRandall Stewart 	if (num_seg)
7976830d754dSRandall Stewart 		asoc->saw_sack_with_frags = 1;
7977830d754dSRandall Stewart 	else
7978830d754dSRandall Stewart 		asoc->saw_sack_with_frags = 0;
7979830d754dSRandall Stewart 
7980830d754dSRandall Stewart 	/* EY! - not sure about if there should be an IF */
7981830d754dSRandall Stewart 	if (num_nr_seg)
7982830d754dSRandall Stewart 		sctp_check_for_nr_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
7983830d754dSRandall Stewart 	else if (asoc->saw_sack_with_nr_frags) {
7984830d754dSRandall Stewart 		/*
7985830d754dSRandall Stewart 		 * EY!- TODO: all previously nr_gapped chunks have been
7986830d754dSRandall Stewart 		 * reneged abort the association
7987830d754dSRandall Stewart 		 */
7988830d754dSRandall Stewart 		asoc->saw_sack_with_nr_frags = 0;
7989830d754dSRandall Stewart 	}
7990830d754dSRandall Stewart 	if (num_nr_seg)
7991830d754dSRandall Stewart 		asoc->saw_sack_with_nr_frags = 1;
7992830d754dSRandall Stewart 	else
7993830d754dSRandall Stewart 		asoc->saw_sack_with_nr_frags = 0;
7994830d754dSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
7995830d754dSRandall Stewart 	asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
7996830d754dSRandall Stewart 
7997830d754dSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
7998830d754dSRandall Stewart 		/* nothing left in-flight */
7999830d754dSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8000830d754dSRandall Stewart 			/* stop all timers */
8001830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
8002830d754dSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8003830d754dSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
8004830d754dSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
8005830d754dSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_29);
8006830d754dSRandall Stewart 				}
8007830d754dSRandall Stewart 			}
8008830d754dSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
8009830d754dSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
8010830d754dSRandall Stewart 			net->flight_size = 0;
8011830d754dSRandall Stewart 			net->partial_bytes_acked = 0;
8012830d754dSRandall Stewart 		}
8013830d754dSRandall Stewart 		asoc->total_flight = 0;
8014830d754dSRandall Stewart 		asoc->total_flight_count = 0;
8015830d754dSRandall Stewart 	}
8016830d754dSRandall Stewart 	/**********************************/
8017830d754dSRandall Stewart 	/* Now what about shutdown issues */
8018830d754dSRandall Stewart 	/**********************************/
8019830d754dSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
8020830d754dSRandall Stewart 		/* nothing left on sendqueue.. consider done */
8021830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
8022830d754dSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
8023830d754dSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
8024830d754dSRandall Stewart 		}
8025830d754dSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
8026830d754dSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
8027830d754dSRandall Stewart 			/* SWS sender side engages */
8028830d754dSRandall Stewart 			asoc->peers_rwnd = 0;
8029830d754dSRandall Stewart 		}
8030830d754dSRandall Stewart 		/* clean up */
8031830d754dSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
8032830d754dSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
8033830d754dSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
8034830d754dSRandall Stewart 		    (asoc->locked_on_sending)
8035830d754dSRandall Stewart 		    ) {
8036830d754dSRandall Stewart 			struct sctp_stream_queue_pending *sp;
8037830d754dSRandall Stewart 
8038830d754dSRandall Stewart 			/*
8039830d754dSRandall Stewart 			 * I may be in a state where we got all across.. but
8040830d754dSRandall Stewart 			 * cannot write more due to a shutdown... we abort
8041830d754dSRandall Stewart 			 * since the user did not indicate EOR in this case.
8042830d754dSRandall Stewart 			 */
8043830d754dSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
8044830d754dSRandall Stewart 			    sctp_streamhead);
8045830d754dSRandall Stewart 			if ((sp) && (sp->length == 0)) {
8046830d754dSRandall Stewart 				asoc->locked_on_sending = NULL;
8047830d754dSRandall Stewart 				if (sp->msg_is_complete) {
8048830d754dSRandall Stewart 					asoc->stream_queue_cnt--;
8049830d754dSRandall Stewart 				} else {
8050830d754dSRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
8051830d754dSRandall Stewart 					asoc->stream_queue_cnt--;
8052830d754dSRandall Stewart 				}
8053830d754dSRandall Stewart 			}
8054830d754dSRandall Stewart 		}
8055830d754dSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
8056830d754dSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
8057830d754dSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
8058830d754dSRandall Stewart 				/* Need to abort here */
8059830d754dSRandall Stewart 				struct mbuf *oper;
8060830d754dSRandall Stewart 
8061830d754dSRandall Stewart 		abort_out_now:
8062830d754dSRandall Stewart 				*abort_now = 1;
8063830d754dSRandall Stewart 				/* XXX */
8064830d754dSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
8065830d754dSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
8066830d754dSRandall Stewart 				if (oper) {
8067830d754dSRandall Stewart 					struct sctp_paramhdr *ph;
8068830d754dSRandall Stewart 					uint32_t *ippp;
8069830d754dSRandall Stewart 
8070830d754dSRandall Stewart 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
8071830d754dSRandall Stewart 					    sizeof(uint32_t);
8072830d754dSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
8073830d754dSRandall Stewart 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
8074830d754dSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
8075830d754dSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
8076830d754dSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_31);
8077830d754dSRandall Stewart 				}
8078830d754dSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
8079830d754dSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
8080830d754dSRandall Stewart 				return;
8081830d754dSRandall Stewart 			} else {
8082830d754dSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
8083830d754dSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
8084830d754dSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
8085830d754dSRandall Stewart 				}
8086830d754dSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
8087830d754dSRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
8088830d754dSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
8089830d754dSRandall Stewart 				sctp_send_shutdown(stcb,
8090830d754dSRandall Stewart 				    stcb->asoc.primary_destination);
8091830d754dSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
8092830d754dSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
8093830d754dSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
8094830d754dSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
8095830d754dSRandall Stewart 			}
8096830d754dSRandall Stewart 			return;
8097830d754dSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
8098830d754dSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
8099830d754dSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
8100830d754dSRandall Stewart 				goto abort_out_now;
8101830d754dSRandall Stewart 			}
8102830d754dSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
8103830d754dSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
8104830d754dSRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
8105830d754dSRandall Stewart 			sctp_send_shutdown_ack(stcb,
8106830d754dSRandall Stewart 			    stcb->asoc.primary_destination);
8107830d754dSRandall Stewart 
8108830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
8109830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, asoc->primary_destination);
8110830d754dSRandall Stewart 			return;
8111830d754dSRandall Stewart 		}
8112830d754dSRandall Stewart 	}
8113830d754dSRandall Stewart 	/*
8114830d754dSRandall Stewart 	 * Now here we are going to recycle net_ack for a different use...
8115830d754dSRandall Stewart 	 * HEADS UP.
8116830d754dSRandall Stewart 	 */
8117830d754dSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8118830d754dSRandall Stewart 		net->net_ack = 0;
8119830d754dSRandall Stewart 	}
8120830d754dSRandall Stewart 
8121830d754dSRandall Stewart 	/*
8122830d754dSRandall Stewart 	 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
8123830d754dSRandall Stewart 	 * to be done. Setting this_sack_lowest_newack to the cum_ack will
8124830d754dSRandall Stewart 	 * automatically ensure that.
8125830d754dSRandall Stewart 	 */
8126830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac) && (cmt_dac_flag == 0)) {
8127830d754dSRandall Stewart 		this_sack_lowest_newack = cum_ack;
8128830d754dSRandall Stewart 	}
8129830d754dSRandall Stewart 	if (num_seg > 0) {
8130830d754dSRandall Stewart 		sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
8131830d754dSRandall Stewart 		    biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
8132830d754dSRandall Stewart 	}
8133830d754dSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
8134830d754dSRandall Stewart 	asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
8135830d754dSRandall Stewart 
8136830d754dSRandall Stewart 	/******************************************************************
8137830d754dSRandall Stewart 	 *  Here we do the stuff with ECN Nonce checking.
8138830d754dSRandall Stewart 	 *  We basically check to see if the nonce sum flag was incorrect
8139830d754dSRandall Stewart 	 *  or if resynchronization needs to be done. Also if we catch a
8140830d754dSRandall Stewart 	 *  misbehaving receiver we give him the kick.
8141830d754dSRandall Stewart 	 ******************************************************************/
8142830d754dSRandall Stewart 
8143830d754dSRandall Stewart 	if (asoc->ecn_nonce_allowed) {
8144830d754dSRandall Stewart 		if (asoc->nonce_sum_check) {
8145830d754dSRandall Stewart 			if (nonce_sum_flag != ((asoc->nonce_sum_expect_base + ecn_seg_sums) & SCTP_SACK_NONCE_SUM)) {
8146830d754dSRandall Stewart 				if (asoc->nonce_wait_for_ecne == 0) {
8147830d754dSRandall Stewart 					struct sctp_tmit_chunk *lchk;
8148830d754dSRandall Stewart 
8149830d754dSRandall Stewart 					lchk = TAILQ_FIRST(&asoc->send_queue);
8150830d754dSRandall Stewart 					asoc->nonce_wait_for_ecne = 1;
8151830d754dSRandall Stewart 					if (lchk) {
8152830d754dSRandall Stewart 						asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
8153830d754dSRandall Stewart 					} else {
8154830d754dSRandall Stewart 						asoc->nonce_wait_tsn = asoc->sending_seq;
8155830d754dSRandall Stewart 					}
8156830d754dSRandall Stewart 				} else {
8157830d754dSRandall Stewart 					if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
8158830d754dSRandall Stewart 					    (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
8159830d754dSRandall Stewart 						/*
8160830d754dSRandall Stewart 						 * Misbehaving peer. We need
8161830d754dSRandall Stewart 						 * to react to this guy
8162830d754dSRandall Stewart 						 */
8163830d754dSRandall Stewart 						asoc->ecn_allowed = 0;
8164830d754dSRandall Stewart 						asoc->ecn_nonce_allowed = 0;
8165830d754dSRandall Stewart 					}
8166830d754dSRandall Stewart 				}
8167830d754dSRandall Stewart 			}
8168830d754dSRandall Stewart 		} else {
8169830d754dSRandall Stewart 			/* See if Resynchronization Possible */
8170830d754dSRandall Stewart 			if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
8171830d754dSRandall Stewart 				asoc->nonce_sum_check = 1;
8172830d754dSRandall Stewart 				/*
8173830d754dSRandall Stewart 				 * now we must calculate what the base is.
8174830d754dSRandall Stewart 				 * We do this based on two things, we know
8175830d754dSRandall Stewart 				 * the total's for all the segments
8176830d754dSRandall Stewart 				 * gap-acked in the SACK, its stored in
8177830d754dSRandall Stewart 				 * ecn_seg_sums. We also know the SACK's
8178830d754dSRandall Stewart 				 * nonce sum, its in nonce_sum_flag. So we
8179830d754dSRandall Stewart 				 * can build a truth table to back-calculate
8180830d754dSRandall Stewart 				 * the new value of
8181830d754dSRandall Stewart 				 * asoc->nonce_sum_expect_base:
8182830d754dSRandall Stewart 				 *
8183830d754dSRandall Stewart 				 * SACK-flag-Value         Seg-Sums Base 0 0 0
8184830d754dSRandall Stewart 				 * 1                    0 1 0 1 1 1 1 0
8185830d754dSRandall Stewart 				 */
8186830d754dSRandall Stewart 				asoc->nonce_sum_expect_base = (ecn_seg_sums ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
8187830d754dSRandall Stewart 			}
8188830d754dSRandall Stewart 		}
8189830d754dSRandall Stewart 	}
8190830d754dSRandall Stewart 	/* Now are we exiting loss recovery ? */
8191830d754dSRandall Stewart 	if (will_exit_fast_recovery) {
8192830d754dSRandall Stewart 		/* Ok, we must exit fast recovery */
8193830d754dSRandall Stewart 		asoc->fast_retran_loss_recovery = 0;
8194830d754dSRandall Stewart 	}
8195830d754dSRandall Stewart 	if ((asoc->sat_t3_loss_recovery) &&
8196830d754dSRandall Stewart 	    ((compare_with_wrap(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn,
8197830d754dSRandall Stewart 	    MAX_TSN) ||
8198830d754dSRandall Stewart 	    (asoc->last_acked_seq == asoc->sat_t3_recovery_tsn)))) {
8199830d754dSRandall Stewart 		/* end satellite t3 loss recovery */
8200830d754dSRandall Stewart 		asoc->sat_t3_loss_recovery = 0;
8201830d754dSRandall Stewart 	}
8202830d754dSRandall Stewart 	/*
8203830d754dSRandall Stewart 	 * CMT Fast recovery
8204830d754dSRandall Stewart 	 */
8205830d754dSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8206830d754dSRandall Stewart 		if (net->will_exit_fast_recovery) {
8207830d754dSRandall Stewart 			/* Ok, we must exit fast recovery */
8208830d754dSRandall Stewart 			net->fast_retran_loss_recovery = 0;
8209830d754dSRandall Stewart 		}
8210830d754dSRandall Stewart 	}
8211830d754dSRandall Stewart 
8212830d754dSRandall Stewart 	/* Adjust and set the new rwnd value */
8213830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
8214830d754dSRandall Stewart 		sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
8215830d754dSRandall Stewart 		    asoc->peers_rwnd, asoc->total_flight, (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)), a_rwnd);
8216830d754dSRandall Stewart 	}
8217830d754dSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
8218830d754dSRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
8219830d754dSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
8220830d754dSRandall Stewart 		/* SWS sender side engages */
8221830d754dSRandall Stewart 		asoc->peers_rwnd = 0;
8222830d754dSRandall Stewart 	}
8223830d754dSRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
8224830d754dSRandall Stewart 		win_probe_recovery = 1;
8225830d754dSRandall Stewart 	}
8226830d754dSRandall Stewart 	/*
8227830d754dSRandall Stewart 	 * Now we must setup so we have a timer up for anyone with
8228830d754dSRandall Stewart 	 * outstanding data.
8229830d754dSRandall Stewart 	 */
8230830d754dSRandall Stewart 	done_once = 0;
8231830d754dSRandall Stewart again:
8232830d754dSRandall Stewart 	j = 0;
8233830d754dSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8234830d754dSRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
8235830d754dSRandall Stewart 			win_probe_recovered = 1;
8236830d754dSRandall Stewart 			/*-
8237830d754dSRandall Stewart 			 * Find first chunk that was used with
8238830d754dSRandall Stewart 			 * window probe and clear the event. Put
8239830d754dSRandall Stewart 			 * it back into the send queue as if has
8240830d754dSRandall Stewart 			 * not been sent.
8241830d754dSRandall Stewart 			 */
8242830d754dSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
8243830d754dSRandall Stewart 				if (tp1->window_probe) {
8244830d754dSRandall Stewart 					sctp_window_probe_recovery(stcb, asoc, net, tp1);
8245830d754dSRandall Stewart 					break;
8246830d754dSRandall Stewart 				}
8247830d754dSRandall Stewart 			}
8248830d754dSRandall Stewart 		}
8249830d754dSRandall Stewart 		if (net->flight_size) {
8250830d754dSRandall Stewart 			j++;
8251830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
8252830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, net);
82535171328bSRandall Stewart 			if (net->window_probe) {
82545171328bSRandall Stewart 				net->window_probe = 0;
82555171328bSRandall Stewart 			}
8256830d754dSRandall Stewart 		} else {
82575171328bSRandall Stewart 			if (net->window_probe) {
82585171328bSRandall Stewart 				net->window_probe = 0;
82595171328bSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SEND,
82605171328bSRandall Stewart 				    stcb->sctp_ep, stcb, net);
82615171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8262830d754dSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
8263830d754dSRandall Stewart 				    stcb, net,
8264830d754dSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
8265830d754dSRandall Stewart 			}
8266830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
8267830d754dSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8268830d754dSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
8269830d754dSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
8270830d754dSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
8271830d754dSRandall Stewart 				}
8272830d754dSRandall Stewart 			}
8273830d754dSRandall Stewart 		}
8274830d754dSRandall Stewart 	}
8275830d754dSRandall Stewart 	if ((j == 0) &&
8276830d754dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
8277830d754dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
8278830d754dSRandall Stewart 	    (win_probe_recovered == 0) &&
8279830d754dSRandall Stewart 	    (done_once == 0)) {
82800c0982b8SRandall Stewart 		/*
82810c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
82820c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
82830c0982b8SRandall Stewart 		 */
82840c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
8285830d754dSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8286830d754dSRandall Stewart 				net->flight_size = 0;
8287830d754dSRandall Stewart 			}
8288830d754dSRandall Stewart 			asoc->total_flight = 0;
8289830d754dSRandall Stewart 			asoc->total_flight_count = 0;
8290830d754dSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
8291830d754dSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
8292830d754dSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
8293830d754dSRandall Stewart 					sctp_flight_size_increase(tp1);
8294830d754dSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
8295830d754dSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
8296830d754dSRandall Stewart 					asoc->sent_queue_retran_cnt++;
8297830d754dSRandall Stewart 				}
8298830d754dSRandall Stewart 			}
82990c0982b8SRandall Stewart 		}
8300830d754dSRandall Stewart 		done_once = 1;
8301830d754dSRandall Stewart 		goto again;
8302830d754dSRandall Stewart 	}
8303830d754dSRandall Stewart 	/*********************************************/
8304830d754dSRandall Stewart 	/* Here we perform PR-SCTP procedures        */
8305830d754dSRandall Stewart 	/* (section 4.2)                             */
8306830d754dSRandall Stewart 	/*********************************************/
8307830d754dSRandall Stewart 	/* C1. update advancedPeerAckPoint */
8308830d754dSRandall Stewart 	if (compare_with_wrap(cum_ack, asoc->advanced_peer_ack_point, MAX_TSN)) {
8309830d754dSRandall Stewart 		asoc->advanced_peer_ack_point = cum_ack;
8310830d754dSRandall Stewart 	}
8311830d754dSRandall Stewart 	/* C2. try to further move advancedPeerAckPoint ahead */
8312830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
8313830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
8314830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
8315830d754dSRandall Stewart 
8316830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
8317830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
8318830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
8319830d754dSRandall Stewart 		if (compare_with_wrap(asoc->advanced_peer_ack_point, cum_ack,
8320830d754dSRandall Stewart 		    MAX_TSN)) {
8321830d754dSRandall Stewart 			/*
8322830d754dSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing for notes
8323830d754dSRandall Stewart 			 * on issues that will occur when the ECN NONCE
8324830d754dSRandall Stewart 			 * stuff is put into SCTP for cross checking.
8325830d754dSRandall Stewart 			 */
8326830d754dSRandall Stewart 			if (compare_with_wrap(asoc->advanced_peer_ack_point, old_adv_peer_ack_point,
8327830d754dSRandall Stewart 			    MAX_TSN)) {
8328830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
8329830d754dSRandall Stewart 				/*
8330830d754dSRandall Stewart 				 * ECN Nonce: Disable Nonce Sum check when
8331830d754dSRandall Stewart 				 * FWD TSN is sent and store resync tsn
8332830d754dSRandall Stewart 				 */
8333830d754dSRandall Stewart 				asoc->nonce_sum_check = 0;
8334830d754dSRandall Stewart 				asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
83350c0982b8SRandall Stewart 			} else if (lchk) {
83360c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
83370c0982b8SRandall Stewart 				lchk->rec.data.fwd_tsn_cnt++;
83380c0982b8SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt > 3) {
83390c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
83400c0982b8SRandall Stewart 					lchk->rec.data.fwd_tsn_cnt = 0;
83410c0982b8SRandall Stewart 				}
8342830d754dSRandall Stewart 			}
8343830d754dSRandall Stewart 		}
8344830d754dSRandall Stewart 		if (lchk) {
8345830d754dSRandall Stewart 			/* Assure a timer is up */
8346830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
8347830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
8348830d754dSRandall Stewart 		}
8349830d754dSRandall Stewart 	}
8350830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
8351830d754dSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
8352830d754dSRandall Stewart 		    a_rwnd,
8353830d754dSRandall Stewart 		    stcb->asoc.peers_rwnd,
8354830d754dSRandall Stewart 		    stcb->asoc.total_flight,
8355830d754dSRandall Stewart 		    stcb->asoc.total_output_queue_size);
8356830d754dSRandall Stewart 	}
8357830d754dSRandall Stewart }
8358