xref: /freebsd/sys/netinet/sctp_indata.c (revision 0fa753b3fb9e37bcf56a68c54e99565a0b4ead5c)
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 
48d50c1d79SRandall Stewart #define SCTP_CALC_TSN_TO_GAP(gap, tsn, mapping_tsn) do { \
49d50c1d79SRandall Stewart 					if ((compare_with_wrap(tsn, mapping_tsn, MAX_TSN)) || \
50d50c1d79SRandall Stewart                         (tsn == mapping_tsn)) { \
51d50c1d79SRandall Stewart 						gap = tsn - mapping_tsn; \
52d50c1d79SRandall Stewart 					} else { \
53d50c1d79SRandall Stewart 						gap = (MAX_TSN - mapping_tsn) + tsn + 1; \
54d50c1d79SRandall Stewart 					} \
55d50c1d79SRandall Stewart                   } while(0)
56d50c1d79SRandall Stewart 
57d50c1d79SRandall Stewart #define SCTP_REVERSE_OUT_TSN_PRES(nr_gap, tsn, asoc) do { \
58d50c1d79SRandall Stewart                     if (asoc->mapping_array_base_tsn == asoc->nr_mapping_array_base_tsn) { \
59d50c1d79SRandall Stewart                        SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, nr_gap); \
60d50c1d79SRandall Stewart                     } else {\
61d50c1d79SRandall Stewart                        int lgap; \
62d50c1d79SRandall Stewart                        SCTP_CALC_TSN_TO_GAP(lgap, tsn, asoc->mapping_array_base_tsn); \
63d50c1d79SRandall Stewart                        SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, lgap); \
64d50c1d79SRandall Stewart                     } \
65d50c1d79SRandall Stewart                   } while(0)
66f8829a4aSRandall Stewart 
67f8829a4aSRandall Stewart /*
68f8829a4aSRandall Stewart  * NOTES: On the outbound side of things I need to check the sack timer to
69f8829a4aSRandall Stewart  * see if I should generate a sack into the chunk queue (if I have data to
70f8829a4aSRandall Stewart  * send that is and will be sending it .. for bundling.
71f8829a4aSRandall Stewart  *
72f8829a4aSRandall Stewart  * The callback in sctp_usrreq.c will get called when the socket is read from.
73f8829a4aSRandall Stewart  * This will cause sctp_service_queues() to get called on the top entry in
74f8829a4aSRandall Stewart  * the list.
75f8829a4aSRandall Stewart  */
76f8829a4aSRandall Stewart 
7772fb6fdbSRandall Stewart void
78f8829a4aSRandall Stewart sctp_set_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
79f8829a4aSRandall Stewart {
80b3f1ea41SRandall Stewart 	asoc->my_rwnd = sctp_calc_rwnd(stcb, asoc);
81f8829a4aSRandall Stewart }
82f8829a4aSRandall Stewart 
83f8829a4aSRandall Stewart /* Calculate what the rwnd would be */
8472fb6fdbSRandall Stewart uint32_t
85f8829a4aSRandall Stewart sctp_calc_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
86f8829a4aSRandall Stewart {
87b3f1ea41SRandall Stewart 	uint32_t calc = 0;
88f8829a4aSRandall Stewart 
89f8829a4aSRandall Stewart 	/*
90f8829a4aSRandall Stewart 	 * This is really set wrong with respect to a 1-2-m socket. Since
91f8829a4aSRandall Stewart 	 * the sb_cc is the count that everyone as put up. When we re-write
92f8829a4aSRandall Stewart 	 * sctp_soreceive then we will fix this so that ONLY this
93f8829a4aSRandall Stewart 	 * associations data is taken into account.
94f8829a4aSRandall Stewart 	 */
95f8829a4aSRandall Stewart 	if (stcb->sctp_socket == NULL)
96f8829a4aSRandall Stewart 		return (calc);
97f8829a4aSRandall Stewart 
98f8829a4aSRandall Stewart 	if (stcb->asoc.sb_cc == 0 &&
99f8829a4aSRandall Stewart 	    asoc->size_on_reasm_queue == 0 &&
100f8829a4aSRandall Stewart 	    asoc->size_on_all_streams == 0) {
101f8829a4aSRandall Stewart 		/* Full rwnd granted */
102b3f1ea41SRandall Stewart 		calc = max(SCTP_SB_LIMIT_RCV(stcb->sctp_socket), SCTP_MINIMAL_RWND);
103f8829a4aSRandall Stewart 		return (calc);
104f8829a4aSRandall Stewart 	}
105f8829a4aSRandall Stewart 	/* get actual space */
106f8829a4aSRandall Stewart 	calc = (uint32_t) sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv);
107f8829a4aSRandall Stewart 
108f8829a4aSRandall Stewart 	/*
109f8829a4aSRandall Stewart 	 * take out what has NOT been put on socket queue and we yet hold
110f8829a4aSRandall Stewart 	 * for putting up.
111f8829a4aSRandall Stewart 	 */
112f8829a4aSRandall Stewart 	calc = sctp_sbspace_sub(calc, (uint32_t) asoc->size_on_reasm_queue);
113f8829a4aSRandall Stewart 	calc = sctp_sbspace_sub(calc, (uint32_t) asoc->size_on_all_streams);
114f8829a4aSRandall Stewart 
115f8829a4aSRandall Stewart 	if (calc == 0) {
116f8829a4aSRandall Stewart 		/* out of space */
117f8829a4aSRandall Stewart 		return (calc);
118f8829a4aSRandall Stewart 	}
119f8829a4aSRandall Stewart 	/* what is the overhead of all these rwnd's */
1202afb3e84SRandall Stewart 	calc = sctp_sbspace_sub(calc, stcb->asoc.my_rwnd_control_len);
121b3f1ea41SRandall Stewart 	/*
122b3f1ea41SRandall Stewart 	 * If the window gets too small due to ctrl-stuff, reduce it to 1,
123b3f1ea41SRandall Stewart 	 * even it is 0. SWS engaged
124f8829a4aSRandall Stewart 	 */
125b3f1ea41SRandall Stewart 	if (calc < stcb->asoc.my_rwnd_control_len) {
126b3f1ea41SRandall Stewart 		calc = 1;
1272afb3e84SRandall Stewart 	}
128b3f1ea41SRandall Stewart 	return (calc);
129f8829a4aSRandall Stewart }
130f8829a4aSRandall Stewart 
131f8829a4aSRandall Stewart 
132f8829a4aSRandall Stewart 
133f8829a4aSRandall Stewart /*
134f8829a4aSRandall Stewart  * Build out our readq entry based on the incoming packet.
135f8829a4aSRandall Stewart  */
136f8829a4aSRandall Stewart struct sctp_queued_to_read *
137f8829a4aSRandall Stewart sctp_build_readq_entry(struct sctp_tcb *stcb,
138f8829a4aSRandall Stewart     struct sctp_nets *net,
139f8829a4aSRandall Stewart     uint32_t tsn, uint32_t ppid,
140f8829a4aSRandall Stewart     uint32_t context, uint16_t stream_no,
141f8829a4aSRandall Stewart     uint16_t stream_seq, uint8_t flags,
142f8829a4aSRandall Stewart     struct mbuf *dm)
143f8829a4aSRandall Stewart {
144f8829a4aSRandall Stewart 	struct sctp_queued_to_read *read_queue_e = NULL;
145f8829a4aSRandall Stewart 
146f8829a4aSRandall Stewart 	sctp_alloc_a_readq(stcb, read_queue_e);
147f8829a4aSRandall Stewart 	if (read_queue_e == NULL) {
148f8829a4aSRandall Stewart 		goto failed_build;
149f8829a4aSRandall Stewart 	}
150f8829a4aSRandall Stewart 	read_queue_e->sinfo_stream = stream_no;
151f8829a4aSRandall Stewart 	read_queue_e->sinfo_ssn = stream_seq;
152f8829a4aSRandall Stewart 	read_queue_e->sinfo_flags = (flags << 8);
153f8829a4aSRandall Stewart 	read_queue_e->sinfo_ppid = ppid;
154f8829a4aSRandall Stewart 	read_queue_e->sinfo_context = stcb->asoc.context;
155f8829a4aSRandall Stewart 	read_queue_e->sinfo_timetolive = 0;
156f8829a4aSRandall Stewart 	read_queue_e->sinfo_tsn = tsn;
157f8829a4aSRandall Stewart 	read_queue_e->sinfo_cumtsn = tsn;
158f8829a4aSRandall Stewart 	read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
159f8829a4aSRandall Stewart 	read_queue_e->whoFrom = net;
160f8829a4aSRandall Stewart 	read_queue_e->length = 0;
161f8829a4aSRandall Stewart 	atomic_add_int(&net->ref_count, 1);
162f8829a4aSRandall Stewart 	read_queue_e->data = dm;
163139bc87fSRandall Stewart 	read_queue_e->spec_flags = 0;
164f8829a4aSRandall Stewart 	read_queue_e->tail_mbuf = NULL;
16517205eccSRandall Stewart 	read_queue_e->aux_data = NULL;
166f8829a4aSRandall Stewart 	read_queue_e->stcb = stcb;
167f8829a4aSRandall Stewart 	read_queue_e->port_from = stcb->rport;
168f8829a4aSRandall Stewart 	read_queue_e->do_not_ref_stcb = 0;
169f8829a4aSRandall Stewart 	read_queue_e->end_added = 0;
1709a6142d8SRandall Stewart 	read_queue_e->some_taken = 0;
17103b0b021SRandall Stewart 	read_queue_e->pdapi_aborted = 0;
172f8829a4aSRandall Stewart failed_build:
173f8829a4aSRandall Stewart 	return (read_queue_e);
174f8829a4aSRandall Stewart }
175f8829a4aSRandall Stewart 
176f8829a4aSRandall Stewart 
177f8829a4aSRandall Stewart /*
178f8829a4aSRandall Stewart  * Build out our readq entry based on the incoming packet.
179f8829a4aSRandall Stewart  */
180f8829a4aSRandall Stewart static struct sctp_queued_to_read *
181f8829a4aSRandall Stewart sctp_build_readq_entry_chk(struct sctp_tcb *stcb,
182f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk)
183f8829a4aSRandall Stewart {
184f8829a4aSRandall Stewart 	struct sctp_queued_to_read *read_queue_e = NULL;
185f8829a4aSRandall Stewart 
186f8829a4aSRandall Stewart 	sctp_alloc_a_readq(stcb, read_queue_e);
187f8829a4aSRandall Stewart 	if (read_queue_e == NULL) {
188f8829a4aSRandall Stewart 		goto failed_build;
189f8829a4aSRandall Stewart 	}
190f8829a4aSRandall Stewart 	read_queue_e->sinfo_stream = chk->rec.data.stream_number;
191f8829a4aSRandall Stewart 	read_queue_e->sinfo_ssn = chk->rec.data.stream_seq;
192f8829a4aSRandall Stewart 	read_queue_e->sinfo_flags = (chk->rec.data.rcv_flags << 8);
193f8829a4aSRandall Stewart 	read_queue_e->sinfo_ppid = chk->rec.data.payloadtype;
194f8829a4aSRandall Stewart 	read_queue_e->sinfo_context = stcb->asoc.context;
195f8829a4aSRandall Stewart 	read_queue_e->sinfo_timetolive = 0;
196f8829a4aSRandall Stewart 	read_queue_e->sinfo_tsn = chk->rec.data.TSN_seq;
197f8829a4aSRandall Stewart 	read_queue_e->sinfo_cumtsn = chk->rec.data.TSN_seq;
198f8829a4aSRandall Stewart 	read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
199f8829a4aSRandall Stewart 	read_queue_e->whoFrom = chk->whoTo;
20017205eccSRandall Stewart 	read_queue_e->aux_data = NULL;
201f8829a4aSRandall Stewart 	read_queue_e->length = 0;
202f8829a4aSRandall Stewart 	atomic_add_int(&chk->whoTo->ref_count, 1);
203f8829a4aSRandall Stewart 	read_queue_e->data = chk->data;
204f8829a4aSRandall Stewart 	read_queue_e->tail_mbuf = NULL;
205f8829a4aSRandall Stewart 	read_queue_e->stcb = stcb;
206f8829a4aSRandall Stewart 	read_queue_e->port_from = stcb->rport;
207139bc87fSRandall Stewart 	read_queue_e->spec_flags = 0;
208f8829a4aSRandall Stewart 	read_queue_e->do_not_ref_stcb = 0;
209f8829a4aSRandall Stewart 	read_queue_e->end_added = 0;
2109a6142d8SRandall Stewart 	read_queue_e->some_taken = 0;
21103b0b021SRandall Stewart 	read_queue_e->pdapi_aborted = 0;
212f8829a4aSRandall Stewart failed_build:
213f8829a4aSRandall Stewart 	return (read_queue_e);
214f8829a4aSRandall Stewart }
215f8829a4aSRandall Stewart 
216f8829a4aSRandall Stewart 
217f8829a4aSRandall Stewart struct mbuf *
218f8829a4aSRandall Stewart sctp_build_ctl_nchunk(struct sctp_inpcb *inp,
219f8829a4aSRandall Stewart     struct sctp_sndrcvinfo *sinfo)
220f8829a4aSRandall Stewart {
221f8829a4aSRandall Stewart 	struct sctp_sndrcvinfo *outinfo;
222f8829a4aSRandall Stewart 	struct cmsghdr *cmh;
223f8829a4aSRandall Stewart 	struct mbuf *ret;
224f8829a4aSRandall Stewart 	int len;
225f8829a4aSRandall Stewart 	int use_extended = 0;
226f8829a4aSRandall Stewart 
227f8829a4aSRandall Stewart 	if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
228f8829a4aSRandall Stewart 		/* user does not want the sndrcv ctl */
229f8829a4aSRandall Stewart 		return (NULL);
230f8829a4aSRandall Stewart 	}
231f8829a4aSRandall Stewart 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
232f8829a4aSRandall Stewart 		use_extended = 1;
233f8829a4aSRandall Stewart 		len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
234f8829a4aSRandall Stewart 	} else {
235f8829a4aSRandall Stewart 		len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
236f8829a4aSRandall Stewart 	}
237f8829a4aSRandall Stewart 
238f8829a4aSRandall Stewart 
239f8829a4aSRandall Stewart 	ret = sctp_get_mbuf_for_msg(len,
240139bc87fSRandall Stewart 	    0, M_DONTWAIT, 1, MT_DATA);
241f8829a4aSRandall Stewart 
242f8829a4aSRandall Stewart 	if (ret == NULL) {
243f8829a4aSRandall Stewart 		/* No space */
244f8829a4aSRandall Stewart 		return (ret);
245f8829a4aSRandall Stewart 	}
246f8829a4aSRandall Stewart 	/* We need a CMSG header followed by the struct  */
247f8829a4aSRandall Stewart 	cmh = mtod(ret, struct cmsghdr *);
248f8829a4aSRandall Stewart 	outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
249f8829a4aSRandall Stewart 	cmh->cmsg_level = IPPROTO_SCTP;
250f8829a4aSRandall Stewart 	if (use_extended) {
251f8829a4aSRandall Stewart 		cmh->cmsg_type = SCTP_EXTRCV;
252f8829a4aSRandall Stewart 		cmh->cmsg_len = len;
253f8829a4aSRandall Stewart 		memcpy(outinfo, sinfo, len);
254f8829a4aSRandall Stewart 	} else {
255f8829a4aSRandall Stewart 		cmh->cmsg_type = SCTP_SNDRCV;
256f8829a4aSRandall Stewart 		cmh->cmsg_len = len;
257f8829a4aSRandall Stewart 		*outinfo = *sinfo;
258f8829a4aSRandall Stewart 	}
259139bc87fSRandall Stewart 	SCTP_BUF_LEN(ret) = cmh->cmsg_len;
260f8829a4aSRandall Stewart 	return (ret);
261f8829a4aSRandall Stewart }
262f8829a4aSRandall Stewart 
263139bc87fSRandall Stewart 
26417205eccSRandall Stewart char *
26517205eccSRandall Stewart sctp_build_ctl_cchunk(struct sctp_inpcb *inp,
26617205eccSRandall Stewart     int *control_len,
26717205eccSRandall Stewart     struct sctp_sndrcvinfo *sinfo)
26817205eccSRandall Stewart {
26917205eccSRandall Stewart 	struct sctp_sndrcvinfo *outinfo;
27017205eccSRandall Stewart 	struct cmsghdr *cmh;
27117205eccSRandall Stewart 	char *buf;
27217205eccSRandall Stewart 	int len;
27317205eccSRandall Stewart 	int use_extended = 0;
27417205eccSRandall Stewart 
27517205eccSRandall Stewart 	if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
27617205eccSRandall Stewart 		/* user does not want the sndrcv ctl */
27717205eccSRandall Stewart 		return (NULL);
27817205eccSRandall Stewart 	}
27917205eccSRandall Stewart 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
28017205eccSRandall Stewart 		use_extended = 1;
28117205eccSRandall Stewart 		len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
28217205eccSRandall Stewart 	} else {
28317205eccSRandall Stewart 		len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
28417205eccSRandall Stewart 	}
285207304d4SRandall Stewart 	SCTP_MALLOC(buf, char *, len, SCTP_M_CMSG);
28617205eccSRandall Stewart 	if (buf == NULL) {
28717205eccSRandall Stewart 		/* No space */
28817205eccSRandall Stewart 		return (buf);
28917205eccSRandall Stewart 	}
29017205eccSRandall Stewart 	/* We need a CMSG header followed by the struct  */
29117205eccSRandall Stewart 	cmh = (struct cmsghdr *)buf;
29217205eccSRandall Stewart 	outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
29317205eccSRandall Stewart 	cmh->cmsg_level = IPPROTO_SCTP;
29417205eccSRandall Stewart 	if (use_extended) {
29517205eccSRandall Stewart 		cmh->cmsg_type = SCTP_EXTRCV;
29617205eccSRandall Stewart 		cmh->cmsg_len = len;
29717205eccSRandall Stewart 		memcpy(outinfo, sinfo, len);
29817205eccSRandall Stewart 	} else {
29917205eccSRandall Stewart 		cmh->cmsg_type = SCTP_SNDRCV;
30017205eccSRandall Stewart 		cmh->cmsg_len = len;
30117205eccSRandall Stewart 		*outinfo = *sinfo;
30217205eccSRandall Stewart 	}
30317205eccSRandall Stewart 	*control_len = len;
30417205eccSRandall Stewart 	return (buf);
30517205eccSRandall Stewart }
30617205eccSRandall Stewart 
30717205eccSRandall Stewart 
308f8829a4aSRandall Stewart /*
309f8829a4aSRandall Stewart  * We are delivering currently from the reassembly queue. We must continue to
310f8829a4aSRandall Stewart  * deliver until we either: 1) run out of space. 2) run out of sequential
311f8829a4aSRandall Stewart  * TSN's 3) hit the SCTP_DATA_LAST_FRAG flag.
312f8829a4aSRandall Stewart  */
313f8829a4aSRandall Stewart static void
314f8829a4aSRandall Stewart sctp_service_reassembly(struct sctp_tcb *stcb, struct sctp_association *asoc)
315f8829a4aSRandall Stewart {
316f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
317f8829a4aSRandall Stewart 	uint16_t nxt_todel;
318f8829a4aSRandall Stewart 	uint16_t stream_no;
319f8829a4aSRandall Stewart 	int end = 0;
320f8829a4aSRandall Stewart 	int cntDel;
321830d754dSRandall Stewart 
322830d754dSRandall Stewart 	/* EY if any out-of-order delivered, then tag it nr on nr_map */
323830d754dSRandall Stewart 	uint32_t nr_tsn, nr_gap;
324830d754dSRandall Stewart 
325f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control, *ctl, *ctlat;
326f8829a4aSRandall Stewart 
327ad81507eSRandall Stewart 	if (stcb == NULL)
328ad81507eSRandall Stewart 		return;
329ad81507eSRandall Stewart 
330f42a358aSRandall Stewart 	cntDel = stream_no = 0;
331ad81507eSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
332851b7298SRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
333ad81507eSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
334851b7298SRandall Stewart 		/* socket above is long gone or going.. */
335851b7298SRandall Stewart abandon:
336f8829a4aSRandall Stewart 		asoc->fragmented_delivery_inprogress = 0;
337f8829a4aSRandall Stewart 		chk = TAILQ_FIRST(&asoc->reasmqueue);
338f8829a4aSRandall Stewart 		while (chk) {
339f8829a4aSRandall Stewart 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
340f8829a4aSRandall Stewart 			asoc->size_on_reasm_queue -= chk->send_size;
341f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_reasm_queue);
342f8829a4aSRandall Stewart 			/*
343f8829a4aSRandall Stewart 			 * Lose the data pointer, since its in the socket
344f8829a4aSRandall Stewart 			 * buffer
345f8829a4aSRandall Stewart 			 */
346f8829a4aSRandall Stewart 			if (chk->data) {
347f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
348f8829a4aSRandall Stewart 				chk->data = NULL;
349f8829a4aSRandall Stewart 			}
350f8829a4aSRandall Stewart 			/* Now free the address and data */
351f8829a4aSRandall Stewart 			sctp_free_a_chunk(stcb, chk);
3523c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
353f8829a4aSRandall Stewart 			chk = TAILQ_FIRST(&asoc->reasmqueue);
354f8829a4aSRandall Stewart 		}
355f8829a4aSRandall Stewart 		return;
356f8829a4aSRandall Stewart 	}
357f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
358f8829a4aSRandall Stewart 	do {
359f8829a4aSRandall Stewart 		chk = TAILQ_FIRST(&asoc->reasmqueue);
360f8829a4aSRandall Stewart 		if (chk == NULL) {
361f8829a4aSRandall Stewart 			return;
362f8829a4aSRandall Stewart 		}
363f8829a4aSRandall Stewart 		if (chk->rec.data.TSN_seq != (asoc->tsn_last_delivered + 1)) {
364f8829a4aSRandall Stewart 			/* Can't deliver more :< */
365f8829a4aSRandall Stewart 			return;
366f8829a4aSRandall Stewart 		}
367f8829a4aSRandall Stewart 		stream_no = chk->rec.data.stream_number;
368f8829a4aSRandall Stewart 		nxt_todel = asoc->strmin[stream_no].last_sequence_delivered + 1;
369f8829a4aSRandall Stewart 		if (nxt_todel != chk->rec.data.stream_seq &&
370f8829a4aSRandall Stewart 		    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
371f8829a4aSRandall Stewart 			/*
372f8829a4aSRandall Stewart 			 * Not the next sequence to deliver in its stream OR
373f8829a4aSRandall Stewart 			 * unordered
374f8829a4aSRandall Stewart 			 */
375f8829a4aSRandall Stewart 			return;
376f8829a4aSRandall Stewart 		}
377f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
378f8829a4aSRandall Stewart 
379f8829a4aSRandall Stewart 			control = sctp_build_readq_entry_chk(stcb, chk);
380f8829a4aSRandall Stewart 			if (control == NULL) {
381f8829a4aSRandall Stewart 				/* out of memory? */
382f8829a4aSRandall Stewart 				return;
383f8829a4aSRandall Stewart 			}
384f8829a4aSRandall Stewart 			/* save it off for our future deliveries */
385f8829a4aSRandall Stewart 			stcb->asoc.control_pdapi = control;
386f8829a4aSRandall Stewart 			if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
387f8829a4aSRandall Stewart 				end = 1;
388f8829a4aSRandall Stewart 			else
389f8829a4aSRandall Stewart 				end = 0;
390f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep,
391cfde3ff7SRandall Stewart 			    stcb, control, &stcb->sctp_socket->so_rcv, end,
392cfde3ff7SRandall Stewart 			    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
393f8829a4aSRandall Stewart 			cntDel++;
394f8829a4aSRandall Stewart 		} else {
395f8829a4aSRandall Stewart 			if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
396f8829a4aSRandall Stewart 				end = 1;
397f8829a4aSRandall Stewart 			else
398f8829a4aSRandall Stewart 				end = 0;
399f8829a4aSRandall Stewart 			if (sctp_append_to_readq(stcb->sctp_ep, stcb,
400f8829a4aSRandall Stewart 			    stcb->asoc.control_pdapi,
401f8829a4aSRandall Stewart 			    chk->data, end, chk->rec.data.TSN_seq,
402f8829a4aSRandall Stewart 			    &stcb->sctp_socket->so_rcv)) {
403f8829a4aSRandall Stewart 				/*
404f8829a4aSRandall Stewart 				 * something is very wrong, either
405f8829a4aSRandall Stewart 				 * control_pdapi is NULL, or the tail_mbuf
406f8829a4aSRandall Stewart 				 * is corrupt, or there is a EOM already on
407f8829a4aSRandall Stewart 				 * the mbuf chain.
408f8829a4aSRandall Stewart 				 */
409851b7298SRandall Stewart 				if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
410851b7298SRandall Stewart 					goto abandon;
411851b7298SRandall Stewart 				} else {
412df6e0cc3SRandall Stewart #ifdef INVARIANTS
413ad81507eSRandall Stewart 					if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
414f8829a4aSRandall Stewart 						panic("This should not happen control_pdapi NULL?");
415f8829a4aSRandall Stewart 					}
416f8829a4aSRandall Stewart 					/* if we did not panic, it was a EOM */
417f8829a4aSRandall Stewart 					panic("Bad chunking ??");
418df6e0cc3SRandall Stewart #else
419df6e0cc3SRandall Stewart 					if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
420df6e0cc3SRandall Stewart 						SCTP_PRINTF("This should not happen control_pdapi NULL?\n");
421df6e0cc3SRandall Stewart 					}
422df6e0cc3SRandall Stewart 					SCTP_PRINTF("Bad chunking ??\n");
423df6e0cc3SRandall Stewart 					SCTP_PRINTF("Dumping re-assembly queue this will probably hose the association\n");
424df6e0cc3SRandall Stewart 
425df6e0cc3SRandall Stewart #endif
426df6e0cc3SRandall Stewart 					goto abandon;
427f8829a4aSRandall Stewart 				}
428851b7298SRandall Stewart 			}
429f8829a4aSRandall Stewart 			cntDel++;
430f8829a4aSRandall Stewart 		}
431f8829a4aSRandall Stewart 		/* pull it we did it */
432f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
433830d754dSRandall Stewart 		/*
434830d754dSRandall Stewart 		 * EY this is the chunk that should be tagged nr gapped
435830d754dSRandall Stewart 		 * calculate the gap and such then tag this TSN nr
436830d754dSRandall Stewart 		 * chk->rec.data.TSN_seq
437830d754dSRandall Stewart 		 */
438830d754dSRandall Stewart 		/*
439830d754dSRandall Stewart 		 * EY!-TODO- this tsn should be tagged nr only if it is
440830d754dSRandall Stewart 		 * out-of-order, the if statement should be modified
441830d754dSRandall Stewart 		 */
442830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
443830d754dSRandall Stewart 
444830d754dSRandall Stewart 			nr_tsn = chk->rec.data.TSN_seq;
445d50c1d79SRandall Stewart 			SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
4468933fa13SRandall Stewart 			if ((nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3)) ||
447830d754dSRandall Stewart 			    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
448830d754dSRandall Stewart 				/*
449830d754dSRandall Stewart 				 * EY The 1st should never happen, as in
450830d754dSRandall Stewart 				 * process_a_data_chunk method this check
451830d754dSRandall Stewart 				 * should be done
452830d754dSRandall Stewart 				 */
453830d754dSRandall Stewart 				/*
454830d754dSRandall Stewart 				 * EY The 2nd should never happen, because
455830d754dSRandall Stewart 				 * nr_mapping_array is always expanded when
456830d754dSRandall Stewart 				 * mapping_array is expanded
457830d754dSRandall Stewart 				 */
4588933fa13SRandall Stewart 				printf("Impossible nr_gap ack range failed\n");
459830d754dSRandall Stewart 			} else {
460830d754dSRandall Stewart 				SCTP_TCB_LOCK_ASSERT(stcb);
461830d754dSRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
462d50c1d79SRandall Stewart 				SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
4638933fa13SRandall Stewart 				if (compare_with_wrap(nr_tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
464830d754dSRandall Stewart 					asoc->highest_tsn_inside_nr_map = nr_tsn;
465830d754dSRandall Stewart 			}
466830d754dSRandall Stewart 		}
467f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
468f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 0;
469f8829a4aSRandall Stewart 			if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
470f8829a4aSRandall Stewart 				asoc->strmin[stream_no].last_sequence_delivered++;
471f8829a4aSRandall Stewart 			}
472f8829a4aSRandall Stewart 			if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
473f8829a4aSRandall Stewart 				SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
474f8829a4aSRandall Stewart 			}
475f8829a4aSRandall Stewart 		} else if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
476f8829a4aSRandall Stewart 			/*
477f8829a4aSRandall Stewart 			 * turn the flag back on since we just  delivered
478f8829a4aSRandall Stewart 			 * yet another one.
479f8829a4aSRandall Stewart 			 */
480f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 1;
481f8829a4aSRandall Stewart 		}
482f8829a4aSRandall Stewart 		asoc->tsn_of_pdapi_last_delivered = chk->rec.data.TSN_seq;
483f8829a4aSRandall Stewart 		asoc->last_flags_delivered = chk->rec.data.rcv_flags;
484f8829a4aSRandall Stewart 		asoc->last_strm_seq_delivered = chk->rec.data.stream_seq;
485f8829a4aSRandall Stewart 		asoc->last_strm_no_delivered = chk->rec.data.stream_number;
486f8829a4aSRandall Stewart 
487f8829a4aSRandall Stewart 		asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
488f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue -= chk->send_size;
489f8829a4aSRandall Stewart 		sctp_ucount_decr(asoc->cnt_on_reasm_queue);
490f8829a4aSRandall Stewart 		/* free up the chk */
491f8829a4aSRandall Stewart 		chk->data = NULL;
492f8829a4aSRandall Stewart 		sctp_free_a_chunk(stcb, chk);
493f8829a4aSRandall Stewart 
494f8829a4aSRandall Stewart 		if (asoc->fragmented_delivery_inprogress == 0) {
495f8829a4aSRandall Stewart 			/*
496f8829a4aSRandall Stewart 			 * Now lets see if we can deliver the next one on
497f8829a4aSRandall Stewart 			 * the stream
498f8829a4aSRandall Stewart 			 */
499f8829a4aSRandall Stewart 			struct sctp_stream_in *strm;
500f8829a4aSRandall Stewart 
501f8829a4aSRandall Stewart 			strm = &asoc->strmin[stream_no];
502f8829a4aSRandall Stewart 			nxt_todel = strm->last_sequence_delivered + 1;
503f8829a4aSRandall Stewart 			ctl = TAILQ_FIRST(&strm->inqueue);
504f8829a4aSRandall Stewart 			if (ctl && (nxt_todel == ctl->sinfo_ssn)) {
505f8829a4aSRandall Stewart 				while (ctl != NULL) {
506f8829a4aSRandall Stewart 					/* Deliver more if we can. */
507f8829a4aSRandall Stewart 					if (nxt_todel == ctl->sinfo_ssn) {
508f8829a4aSRandall Stewart 						ctlat = TAILQ_NEXT(ctl, next);
509f8829a4aSRandall Stewart 						TAILQ_REMOVE(&strm->inqueue, ctl, next);
510f8829a4aSRandall Stewart 						asoc->size_on_all_streams -= ctl->length;
511f8829a4aSRandall Stewart 						sctp_ucount_decr(asoc->cnt_on_all_streams);
512f8829a4aSRandall Stewart 						strm->last_sequence_delivered++;
513830d754dSRandall Stewart 						/*
514830d754dSRandall Stewart 						 * EY will be used to
515830d754dSRandall Stewart 						 * calculate nr-gap
516830d754dSRandall Stewart 						 */
517830d754dSRandall Stewart 						nr_tsn = ctl->sinfo_tsn;
518f8829a4aSRandall Stewart 						sctp_add_to_readq(stcb->sctp_ep, stcb,
519f8829a4aSRandall Stewart 						    ctl,
520cfde3ff7SRandall Stewart 						    &stcb->sctp_socket->so_rcv, 1,
521cfde3ff7SRandall Stewart 						    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
522830d754dSRandall Stewart 						/*
523830d754dSRandall Stewart 						 * EY -now something is
524830d754dSRandall Stewart 						 * delivered, calculate
525830d754dSRandall Stewart 						 * nr_gap and tag this tsn
526830d754dSRandall Stewart 						 * NR
527830d754dSRandall Stewart 						 */
528830d754dSRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
529d50c1d79SRandall Stewart 							SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
530830d754dSRandall Stewart 							if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
531830d754dSRandall Stewart 							    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
532d50c1d79SRandall Stewart 								printf("Impossible NR gap calculation?\n");
533830d754dSRandall Stewart 								/*
534830d754dSRandall Stewart 								 * EY The
535830d754dSRandall Stewart 								 * 1st
536830d754dSRandall Stewart 								 * should
537830d754dSRandall Stewart 								 * never
538830d754dSRandall Stewart 								 * happen,
539830d754dSRandall Stewart 								 * as in
540830d754dSRandall Stewart 								 * process_a_
541830d754dSRandall Stewart 								 * data_chunk
542830d754dSRandall Stewart 								 *  method
543830d754dSRandall Stewart 								 * this
544830d754dSRandall Stewart 								 * check
545830d754dSRandall Stewart 								 * should be
546830d754dSRandall Stewart 								 * done
547830d754dSRandall Stewart 								 */
548830d754dSRandall Stewart 								/*
549830d754dSRandall Stewart 								 * EY The
550830d754dSRandall Stewart 								 * 2nd
551830d754dSRandall Stewart 								 * should
552830d754dSRandall Stewart 								 * never
553830d754dSRandall Stewart 								 * happen,
554830d754dSRandall Stewart 								 * because
555830d754dSRandall Stewart 								 * nr_mapping
556830d754dSRandall Stewart 								 * _array is
557830d754dSRandall Stewart 								 * always
558830d754dSRandall Stewart 								 * expanded
559830d754dSRandall Stewart 								 * when
560830d754dSRandall Stewart 								 * mapping_ar
561830d754dSRandall Stewart 								 * ray is
562830d754dSRandall Stewart 								 * expanded
563830d754dSRandall Stewart 								 */
564830d754dSRandall Stewart 							} else {
565830d754dSRandall Stewart 								SCTP_TCB_LOCK_ASSERT(stcb);
566830d754dSRandall Stewart 								SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
567d50c1d79SRandall Stewart 								SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
5688933fa13SRandall Stewart 								if (compare_with_wrap(nr_tsn,
5698933fa13SRandall Stewart 								    asoc->highest_tsn_inside_nr_map,
5708933fa13SRandall Stewart 								    MAX_TSN))
571830d754dSRandall Stewart 									asoc->highest_tsn_inside_nr_map = nr_tsn;
572830d754dSRandall Stewart 							}
573830d754dSRandall Stewart 						}
574f8829a4aSRandall Stewart 						ctl = ctlat;
575f8829a4aSRandall Stewart 					} else {
576f8829a4aSRandall Stewart 						break;
577f8829a4aSRandall Stewart 					}
578f8829a4aSRandall Stewart 					nxt_todel = strm->last_sequence_delivered + 1;
579f8829a4aSRandall Stewart 				}
580f8829a4aSRandall Stewart 			}
581139bc87fSRandall Stewart 			break;
582f8829a4aSRandall Stewart 		}
5833c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
584f8829a4aSRandall Stewart 		chk = TAILQ_FIRST(&asoc->reasmqueue);
585f8829a4aSRandall Stewart 	} while (chk);
586f8829a4aSRandall Stewart }
587f8829a4aSRandall Stewart 
588f8829a4aSRandall Stewart /*
589f8829a4aSRandall Stewart  * Queue the chunk either right into the socket buffer if it is the next one
590f8829a4aSRandall Stewart  * to go OR put it in the correct place in the delivery queue.  If we do
591f8829a4aSRandall Stewart  * append to the so_buf, keep doing so until we are out of order. One big
592f8829a4aSRandall Stewart  * question still remains, what to do when the socket buffer is FULL??
593f8829a4aSRandall Stewart  */
594f8829a4aSRandall Stewart static void
595f8829a4aSRandall Stewart sctp_queue_data_to_stream(struct sctp_tcb *stcb, struct sctp_association *asoc,
596f8829a4aSRandall Stewart     struct sctp_queued_to_read *control, int *abort_flag)
597f8829a4aSRandall Stewart {
598f8829a4aSRandall Stewart 	/*
599f8829a4aSRandall Stewart 	 * FIX-ME maybe? What happens when the ssn wraps? If we are getting
600f8829a4aSRandall Stewart 	 * all the data in one stream this could happen quite rapidly. One
601f8829a4aSRandall Stewart 	 * could use the TSN to keep track of things, but this scheme breaks
602f8829a4aSRandall Stewart 	 * down in the other type of stream useage that could occur. Send a
603f8829a4aSRandall Stewart 	 * single msg to stream 0, send 4Billion messages to stream 1, now
604f8829a4aSRandall Stewart 	 * send a message to stream 0. You have a situation where the TSN
605f8829a4aSRandall Stewart 	 * has wrapped but not in the stream. Is this worth worrying about
606f8829a4aSRandall Stewart 	 * or should we just change our queue sort at the bottom to be by
607f8829a4aSRandall Stewart 	 * TSN.
608f8829a4aSRandall Stewart 	 *
609f8829a4aSRandall Stewart 	 * Could it also be legal for a peer to send ssn 1 with TSN 2 and ssn 2
610f8829a4aSRandall Stewart 	 * with TSN 1? If the peer is doing some sort of funky TSN/SSN
611f8829a4aSRandall Stewart 	 * assignment this could happen... and I don't see how this would be
612f8829a4aSRandall Stewart 	 * a violation. So for now I am undecided an will leave the sort by
613f8829a4aSRandall Stewart 	 * SSN alone. Maybe a hybred approach is the answer
614f8829a4aSRandall Stewart 	 *
615f8829a4aSRandall Stewart 	 */
616f8829a4aSRandall Stewart 	struct sctp_stream_in *strm;
617f8829a4aSRandall Stewart 	struct sctp_queued_to_read *at;
618f8829a4aSRandall Stewart 	int queue_needed;
619f8829a4aSRandall Stewart 	uint16_t nxt_todel;
620f8829a4aSRandall Stewart 	struct mbuf *oper;
621f8829a4aSRandall Stewart 
622830d754dSRandall Stewart 	/* EY- will be used to calculate nr-gap for a tsn */
623830d754dSRandall Stewart 	uint32_t nr_tsn, nr_gap;
624830d754dSRandall Stewart 
625f8829a4aSRandall Stewart 	queue_needed = 1;
626f8829a4aSRandall Stewart 	asoc->size_on_all_streams += control->length;
627f8829a4aSRandall Stewart 	sctp_ucount_incr(asoc->cnt_on_all_streams);
628f8829a4aSRandall Stewart 	strm = &asoc->strmin[control->sinfo_stream];
629f8829a4aSRandall Stewart 	nxt_todel = strm->last_sequence_delivered + 1;
630b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
631f8829a4aSRandall Stewart 		sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INTO_STRD);
63280fefe0aSRandall Stewart 	}
633ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INDATA1,
634ad81507eSRandall Stewart 	    "queue to stream called for ssn:%u lastdel:%u nxt:%u\n",
635f8829a4aSRandall Stewart 	    (uint32_t) control->sinfo_stream,
636ad81507eSRandall Stewart 	    (uint32_t) strm->last_sequence_delivered,
637ad81507eSRandall Stewart 	    (uint32_t) nxt_todel);
638f8829a4aSRandall Stewart 	if (compare_with_wrap(strm->last_sequence_delivered,
639f8829a4aSRandall Stewart 	    control->sinfo_ssn, MAX_SEQ) ||
640f8829a4aSRandall Stewart 	    (strm->last_sequence_delivered == control->sinfo_ssn)) {
641f8829a4aSRandall Stewart 		/* The incoming sseq is behind where we last delivered? */
642ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1, "Duplicate S-SEQ:%d delivered:%d from peer, Abort  association\n",
643ad81507eSRandall Stewart 		    control->sinfo_ssn, strm->last_sequence_delivered);
644c40e9cf2SRandall Stewart protocol_error:
645f8829a4aSRandall Stewart 		/*
646f8829a4aSRandall Stewart 		 * throw it in the stream so it gets cleaned up in
647f8829a4aSRandall Stewart 		 * association destruction
648f8829a4aSRandall Stewart 		 */
649f8829a4aSRandall Stewart 		TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
650139bc87fSRandall Stewart 		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
651f8829a4aSRandall Stewart 		    0, M_DONTWAIT, 1, MT_DATA);
652f8829a4aSRandall Stewart 		if (oper) {
653f8829a4aSRandall Stewart 			struct sctp_paramhdr *ph;
654f8829a4aSRandall Stewart 			uint32_t *ippp;
655f8829a4aSRandall Stewart 
656139bc87fSRandall Stewart 			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
657f8829a4aSRandall Stewart 			    (sizeof(uint32_t) * 3);
658f8829a4aSRandall Stewart 			ph = mtod(oper, struct sctp_paramhdr *);
659f8829a4aSRandall Stewart 			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
660139bc87fSRandall Stewart 			ph->param_length = htons(SCTP_BUF_LEN(oper));
661f8829a4aSRandall Stewart 			ippp = (uint32_t *) (ph + 1);
662a5d547adSRandall Stewart 			*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_1);
663f8829a4aSRandall Stewart 			ippp++;
664f8829a4aSRandall Stewart 			*ippp = control->sinfo_tsn;
665f8829a4aSRandall Stewart 			ippp++;
666f8829a4aSRandall Stewart 			*ippp = ((control->sinfo_stream << 16) | control->sinfo_ssn);
667f8829a4aSRandall Stewart 		}
668a5d547adSRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_1;
669f8829a4aSRandall Stewart 		sctp_abort_an_association(stcb->sctp_ep, stcb,
670ceaad40aSRandall Stewart 		    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
671f8829a4aSRandall Stewart 
672f8829a4aSRandall Stewart 		*abort_flag = 1;
673f8829a4aSRandall Stewart 		return;
674f8829a4aSRandall Stewart 
675f8829a4aSRandall Stewart 	}
676f8829a4aSRandall Stewart 	if (nxt_todel == control->sinfo_ssn) {
677f8829a4aSRandall Stewart 		/* can be delivered right away? */
678b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
679f8829a4aSRandall Stewart 			sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_IMMED_DEL);
68080fefe0aSRandall Stewart 		}
681830d754dSRandall Stewart 		/* EY it wont be queued if it could be delivered directly */
682f8829a4aSRandall Stewart 		queue_needed = 0;
683f8829a4aSRandall Stewart 		asoc->size_on_all_streams -= control->length;
684f8829a4aSRandall Stewart 		sctp_ucount_decr(asoc->cnt_on_all_streams);
685f8829a4aSRandall Stewart 		strm->last_sequence_delivered++;
686830d754dSRandall Stewart 		/* EY will be used to calculate nr-gap */
687830d754dSRandall Stewart 		nr_tsn = control->sinfo_tsn;
688f8829a4aSRandall Stewart 		sctp_add_to_readq(stcb->sctp_ep, stcb,
689f8829a4aSRandall Stewart 		    control,
690cfde3ff7SRandall Stewart 		    &stcb->sctp_socket->so_rcv, 1,
691cfde3ff7SRandall Stewart 		    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
692830d754dSRandall Stewart 		/*
693830d754dSRandall Stewart 		 * EY this is the chunk that should be tagged nr gapped
694830d754dSRandall Stewart 		 * calculate the gap and such then tag this TSN nr
695830d754dSRandall Stewart 		 * chk->rec.data.TSN_seq
696830d754dSRandall Stewart 		 */
697830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
698d50c1d79SRandall Stewart 			SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
699830d754dSRandall Stewart 			if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
700830d754dSRandall Stewart 			    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
701d50c1d79SRandall Stewart 				printf("Impossible nr_tsn set 2?\n");
702830d754dSRandall Stewart 				/*
703830d754dSRandall Stewart 				 * EY The 1st should never happen, as in
704830d754dSRandall Stewart 				 * process_a_data_chunk method this check
705830d754dSRandall Stewart 				 * should be done
706830d754dSRandall Stewart 				 */
707830d754dSRandall Stewart 				/*
708830d754dSRandall Stewart 				 * EY The 2nd should never happen, because
709830d754dSRandall Stewart 				 * nr_mapping_array is always expanded when
710830d754dSRandall Stewart 				 * mapping_array is expanded
711830d754dSRandall Stewart 				 */
712830d754dSRandall Stewart 			} else {
713830d754dSRandall Stewart 				SCTP_TCB_LOCK_ASSERT(stcb);
714830d754dSRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
715d50c1d79SRandall Stewart 				SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
7168933fa13SRandall Stewart 				if (compare_with_wrap(nr_tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
717830d754dSRandall Stewart 					asoc->highest_tsn_inside_nr_map = nr_tsn;
718830d754dSRandall Stewart 			}
719830d754dSRandall Stewart 		}
720f8829a4aSRandall Stewart 		control = TAILQ_FIRST(&strm->inqueue);
721f8829a4aSRandall Stewart 		while (control != NULL) {
722f8829a4aSRandall Stewart 			/* all delivered */
723f8829a4aSRandall Stewart 			nxt_todel = strm->last_sequence_delivered + 1;
724f8829a4aSRandall Stewart 			if (nxt_todel == control->sinfo_ssn) {
725f8829a4aSRandall Stewart 				at = TAILQ_NEXT(control, next);
726f8829a4aSRandall Stewart 				TAILQ_REMOVE(&strm->inqueue, control, next);
727f8829a4aSRandall Stewart 				asoc->size_on_all_streams -= control->length;
728f8829a4aSRandall Stewart 				sctp_ucount_decr(asoc->cnt_on_all_streams);
729f8829a4aSRandall Stewart 				strm->last_sequence_delivered++;
730f8829a4aSRandall Stewart 				/*
731f8829a4aSRandall Stewart 				 * We ignore the return of deliver_data here
732f8829a4aSRandall Stewart 				 * since we always can hold the chunk on the
733f8829a4aSRandall Stewart 				 * d-queue. And we have a finite number that
734f8829a4aSRandall Stewart 				 * can be delivered from the strq.
735f8829a4aSRandall Stewart 				 */
736b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
737f8829a4aSRandall Stewart 					sctp_log_strm_del(control, NULL,
738f8829a4aSRandall Stewart 					    SCTP_STR_LOG_FROM_IMMED_DEL);
73980fefe0aSRandall Stewart 				}
740830d754dSRandall Stewart 				/* EY will be used to calculate nr-gap */
741830d754dSRandall Stewart 				nr_tsn = control->sinfo_tsn;
742f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
743f8829a4aSRandall Stewart 				    control,
744cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1,
745cfde3ff7SRandall Stewart 				    SCTP_READ_LOCK_NOT_HELD,
746cfde3ff7SRandall Stewart 				    SCTP_SO_NOT_LOCKED);
747830d754dSRandall Stewart 				/*
748830d754dSRandall Stewart 				 * EY this is the chunk that should be
749830d754dSRandall Stewart 				 * tagged nr gapped calculate the gap and
750830d754dSRandall Stewart 				 * such then tag this TSN nr
751830d754dSRandall Stewart 				 * chk->rec.data.TSN_seq
752830d754dSRandall Stewart 				 */
753830d754dSRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
754d50c1d79SRandall Stewart 					SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
755830d754dSRandall Stewart 					if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
756830d754dSRandall Stewart 					    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
757d50c1d79SRandall Stewart 						printf("Impossible nr TSN set 3?\n");
758830d754dSRandall Stewart 						/*
759830d754dSRandall Stewart 						 * EY The 1st should never
760830d754dSRandall Stewart 						 * happen, as in
761830d754dSRandall Stewart 						 * process_a_data_chunk
762830d754dSRandall Stewart 						 * method this check should
763830d754dSRandall Stewart 						 * be done
764830d754dSRandall Stewart 						 */
765830d754dSRandall Stewart 						/*
766830d754dSRandall Stewart 						 * EY The 2nd should never
767830d754dSRandall Stewart 						 * happen, because
768830d754dSRandall Stewart 						 * nr_mapping_array is
769830d754dSRandall Stewart 						 * always expanded when
770830d754dSRandall Stewart 						 * mapping_array is expanded
771830d754dSRandall Stewart 						 */
772830d754dSRandall Stewart 					} else {
773830d754dSRandall Stewart 						SCTP_TCB_LOCK_ASSERT(stcb);
774d50c1d79SRandall Stewart 						SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
775830d754dSRandall Stewart 						SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
7768933fa13SRandall Stewart 						if (compare_with_wrap(nr_tsn, asoc->highest_tsn_inside_nr_map,
7778933fa13SRandall Stewart 						    MAX_TSN))
778830d754dSRandall Stewart 							asoc->highest_tsn_inside_nr_map = nr_tsn;
779830d754dSRandall Stewart 					}
780830d754dSRandall Stewart 				}
781f8829a4aSRandall Stewart 				control = at;
782f8829a4aSRandall Stewart 				continue;
783f8829a4aSRandall Stewart 			}
784f8829a4aSRandall Stewart 			break;
785f8829a4aSRandall Stewart 		}
786f8829a4aSRandall Stewart 	}
787f8829a4aSRandall Stewart 	if (queue_needed) {
788f8829a4aSRandall Stewart 		/*
789f8829a4aSRandall Stewart 		 * Ok, we did not deliver this guy, find the correct place
790f8829a4aSRandall Stewart 		 * to put it on the queue.
791f8829a4aSRandall Stewart 		 */
792c40e9cf2SRandall Stewart 		if ((compare_with_wrap(asoc->cumulative_tsn,
793c40e9cf2SRandall Stewart 		    control->sinfo_tsn, MAX_TSN)) ||
794c40e9cf2SRandall Stewart 		    (control->sinfo_tsn == asoc->cumulative_tsn)) {
795c40e9cf2SRandall Stewart 			goto protocol_error;
796c40e9cf2SRandall Stewart 		}
797f8829a4aSRandall Stewart 		if (TAILQ_EMPTY(&strm->inqueue)) {
798f8829a4aSRandall Stewart 			/* Empty queue */
799b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
800f8829a4aSRandall Stewart 				sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INSERT_HD);
80180fefe0aSRandall Stewart 			}
802f8829a4aSRandall Stewart 			TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
803f8829a4aSRandall Stewart 		} else {
804f8829a4aSRandall Stewart 			TAILQ_FOREACH(at, &strm->inqueue, next) {
805f8829a4aSRandall Stewart 				if (compare_with_wrap(at->sinfo_ssn,
806f8829a4aSRandall Stewart 				    control->sinfo_ssn, MAX_SEQ)) {
807f8829a4aSRandall Stewart 					/*
808f8829a4aSRandall Stewart 					 * one in queue is bigger than the
809f8829a4aSRandall Stewart 					 * new one, insert before this one
810f8829a4aSRandall Stewart 					 */
811b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
812f8829a4aSRandall Stewart 						sctp_log_strm_del(control, at,
813f8829a4aSRandall Stewart 						    SCTP_STR_LOG_FROM_INSERT_MD);
81480fefe0aSRandall Stewart 					}
815f8829a4aSRandall Stewart 					TAILQ_INSERT_BEFORE(at, control, next);
816f8829a4aSRandall Stewart 					break;
817f8829a4aSRandall Stewart 				} else if (at->sinfo_ssn == control->sinfo_ssn) {
818f8829a4aSRandall Stewart 					/*
819f8829a4aSRandall Stewart 					 * Gak, He sent me a duplicate str
820f8829a4aSRandall Stewart 					 * seq number
821f8829a4aSRandall Stewart 					 */
822f8829a4aSRandall Stewart 					/*
823f8829a4aSRandall Stewart 					 * foo bar, I guess I will just free
824f8829a4aSRandall Stewart 					 * this new guy, should we abort
825f8829a4aSRandall Stewart 					 * too? FIX ME MAYBE? Or it COULD be
826f8829a4aSRandall Stewart 					 * that the SSN's have wrapped.
827f8829a4aSRandall Stewart 					 * Maybe I should compare to TSN
828f8829a4aSRandall Stewart 					 * somehow... sigh for now just blow
829f8829a4aSRandall Stewart 					 * away the chunk!
830f8829a4aSRandall Stewart 					 */
831f8829a4aSRandall Stewart 
832f8829a4aSRandall Stewart 					if (control->data)
833f8829a4aSRandall Stewart 						sctp_m_freem(control->data);
834f8829a4aSRandall Stewart 					control->data = NULL;
835f8829a4aSRandall Stewart 					asoc->size_on_all_streams -= control->length;
836f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->cnt_on_all_streams);
8375bead436SRandall Stewart 					if (control->whoFrom)
838f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
8395bead436SRandall Stewart 					control->whoFrom = NULL;
840f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
841f8829a4aSRandall Stewart 					return;
842f8829a4aSRandall Stewart 				} else {
843f8829a4aSRandall Stewart 					if (TAILQ_NEXT(at, next) == NULL) {
844f8829a4aSRandall Stewart 						/*
845f8829a4aSRandall Stewart 						 * We are at the end, insert
846f8829a4aSRandall Stewart 						 * it after this one
847f8829a4aSRandall Stewart 						 */
848b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
849f8829a4aSRandall Stewart 							sctp_log_strm_del(control, at,
850f8829a4aSRandall Stewart 							    SCTP_STR_LOG_FROM_INSERT_TL);
85180fefe0aSRandall Stewart 						}
852f8829a4aSRandall Stewart 						TAILQ_INSERT_AFTER(&strm->inqueue,
853f8829a4aSRandall Stewart 						    at, control, next);
854f8829a4aSRandall Stewart 						break;
855f8829a4aSRandall Stewart 					}
856f8829a4aSRandall Stewart 				}
857f8829a4aSRandall Stewart 			}
858f8829a4aSRandall Stewart 		}
859f8829a4aSRandall Stewart 	}
860f8829a4aSRandall Stewart }
861f8829a4aSRandall Stewart 
862f8829a4aSRandall Stewart /*
863f8829a4aSRandall Stewart  * Returns two things: You get the total size of the deliverable parts of the
864f8829a4aSRandall Stewart  * first fragmented message on the reassembly queue. And you get a 1 back if
865f8829a4aSRandall Stewart  * all of the message is ready or a 0 back if the message is still incomplete
866f8829a4aSRandall Stewart  */
867f8829a4aSRandall Stewart static int
868f8829a4aSRandall Stewart sctp_is_all_msg_on_reasm(struct sctp_association *asoc, uint32_t * t_size)
869f8829a4aSRandall Stewart {
870f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
871f8829a4aSRandall Stewart 	uint32_t tsn;
872f8829a4aSRandall Stewart 
873f8829a4aSRandall Stewart 	*t_size = 0;
874f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
875f8829a4aSRandall Stewart 	if (chk == NULL) {
876f8829a4aSRandall Stewart 		/* nothing on the queue */
877f8829a4aSRandall Stewart 		return (0);
878f8829a4aSRandall Stewart 	}
879f8829a4aSRandall Stewart 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
880f8829a4aSRandall Stewart 		/* Not a first on the queue */
881f8829a4aSRandall Stewart 		return (0);
882f8829a4aSRandall Stewart 	}
883f8829a4aSRandall Stewart 	tsn = chk->rec.data.TSN_seq;
884f8829a4aSRandall Stewart 	while (chk) {
885f8829a4aSRandall Stewart 		if (tsn != chk->rec.data.TSN_seq) {
886f8829a4aSRandall Stewart 			return (0);
887f8829a4aSRandall Stewart 		}
888f8829a4aSRandall Stewart 		*t_size += chk->send_size;
889f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
890f8829a4aSRandall Stewart 			return (1);
891f8829a4aSRandall Stewart 		}
892f8829a4aSRandall Stewart 		tsn++;
893f8829a4aSRandall Stewart 		chk = TAILQ_NEXT(chk, sctp_next);
894f8829a4aSRandall Stewart 	}
895f8829a4aSRandall Stewart 	return (0);
896f8829a4aSRandall Stewart }
897f8829a4aSRandall Stewart 
898f8829a4aSRandall Stewart static void
899f8829a4aSRandall Stewart sctp_deliver_reasm_check(struct sctp_tcb *stcb, struct sctp_association *asoc)
900f8829a4aSRandall Stewart {
901f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
902f8829a4aSRandall Stewart 	uint16_t nxt_todel;
903810ec536SMichael Tuexen 	uint32_t tsize, pd_point;
904f8829a4aSRandall Stewart 
905139bc87fSRandall Stewart doit_again:
906f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
907f8829a4aSRandall Stewart 	if (chk == NULL) {
908f8829a4aSRandall Stewart 		/* Huh? */
909f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = 0;
910f8829a4aSRandall Stewart 		asoc->cnt_on_reasm_queue = 0;
911f8829a4aSRandall Stewart 		return;
912f8829a4aSRandall Stewart 	}
913f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress == 0) {
914f8829a4aSRandall Stewart 		nxt_todel =
915f8829a4aSRandall Stewart 		    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
916f8829a4aSRandall Stewart 		if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
917f8829a4aSRandall Stewart 		    (nxt_todel == chk->rec.data.stream_seq ||
918f8829a4aSRandall Stewart 		    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
919f8829a4aSRandall Stewart 			/*
920f8829a4aSRandall Stewart 			 * Yep the first one is here and its ok to deliver
921f8829a4aSRandall Stewart 			 * but should we?
922f8829a4aSRandall Stewart 			 */
923810ec536SMichael Tuexen 			if (stcb->sctp_socket) {
924810ec536SMichael Tuexen 				pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket) >> SCTP_PARTIAL_DELIVERY_SHIFT,
925810ec536SMichael Tuexen 				    stcb->sctp_ep->partial_delivery_point);
926810ec536SMichael Tuexen 			} else {
927810ec536SMichael Tuexen 				pd_point = stcb->sctp_ep->partial_delivery_point;
928810ec536SMichael Tuexen 			}
929810ec536SMichael Tuexen 			if (sctp_is_all_msg_on_reasm(asoc, &tsize) || (tsize >= pd_point)) {
930f8829a4aSRandall Stewart 
931f8829a4aSRandall Stewart 				/*
932f8829a4aSRandall Stewart 				 * Yes, we setup to start reception, by
933f8829a4aSRandall Stewart 				 * backing down the TSN just in case we
934f8829a4aSRandall Stewart 				 * can't deliver. If we
935f8829a4aSRandall Stewart 				 */
936f8829a4aSRandall Stewart 				asoc->fragmented_delivery_inprogress = 1;
937f8829a4aSRandall Stewart 				asoc->tsn_last_delivered =
938f8829a4aSRandall Stewart 				    chk->rec.data.TSN_seq - 1;
939f8829a4aSRandall Stewart 				asoc->str_of_pdapi =
940f8829a4aSRandall Stewart 				    chk->rec.data.stream_number;
941f8829a4aSRandall Stewart 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
942f8829a4aSRandall Stewart 				asoc->pdapi_ppid = chk->rec.data.payloadtype;
943f8829a4aSRandall Stewart 				asoc->fragment_flags = chk->rec.data.rcv_flags;
944f8829a4aSRandall Stewart 				sctp_service_reassembly(stcb, asoc);
945f8829a4aSRandall Stewart 			}
946f8829a4aSRandall Stewart 		}
947f8829a4aSRandall Stewart 	} else {
948139bc87fSRandall Stewart 		/*
949139bc87fSRandall Stewart 		 * Service re-assembly will deliver stream data queued at
950139bc87fSRandall Stewart 		 * the end of fragmented delivery.. but it wont know to go
951139bc87fSRandall Stewart 		 * back and call itself again... we do that here with the
952139bc87fSRandall Stewart 		 * got doit_again
953139bc87fSRandall Stewart 		 */
954f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
955139bc87fSRandall Stewart 		if (asoc->fragmented_delivery_inprogress == 0) {
956139bc87fSRandall Stewart 			/*
957139bc87fSRandall Stewart 			 * finished our Fragmented delivery, could be more
958139bc87fSRandall Stewart 			 * waiting?
959139bc87fSRandall Stewart 			 */
960139bc87fSRandall Stewart 			goto doit_again;
961139bc87fSRandall Stewart 		}
962f8829a4aSRandall Stewart 	}
963f8829a4aSRandall Stewart }
964f8829a4aSRandall Stewart 
965f8829a4aSRandall Stewart /*
966f8829a4aSRandall Stewart  * Dump onto the re-assembly queue, in its proper place. After dumping on the
967f8829a4aSRandall Stewart  * queue, see if anthing can be delivered. If so pull it off (or as much as
968f8829a4aSRandall Stewart  * we can. If we run out of space then we must dump what we can and set the
969f8829a4aSRandall Stewart  * appropriate flag to say we queued what we could.
970f8829a4aSRandall Stewart  */
971f8829a4aSRandall Stewart static void
972f8829a4aSRandall Stewart sctp_queue_data_for_reasm(struct sctp_tcb *stcb, struct sctp_association *asoc,
973f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk, int *abort_flag)
974f8829a4aSRandall Stewart {
975f8829a4aSRandall Stewart 	struct mbuf *oper;
976f8829a4aSRandall Stewart 	uint32_t cum_ackp1, last_tsn, prev_tsn, post_tsn;
977f8829a4aSRandall Stewart 	u_char last_flags;
978f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *at, *prev, *next;
979f8829a4aSRandall Stewart 
980f8829a4aSRandall Stewart 	prev = next = NULL;
981f8829a4aSRandall Stewart 	cum_ackp1 = asoc->tsn_last_delivered + 1;
982f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->reasmqueue)) {
983f8829a4aSRandall Stewart 		/* This is the first one on the queue */
984f8829a4aSRandall Stewart 		TAILQ_INSERT_HEAD(&asoc->reasmqueue, chk, sctp_next);
985f8829a4aSRandall Stewart 		/*
986f8829a4aSRandall Stewart 		 * we do not check for delivery of anything when only one
987f8829a4aSRandall Stewart 		 * fragment is here
988f8829a4aSRandall Stewart 		 */
989f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = chk->send_size;
990f8829a4aSRandall Stewart 		sctp_ucount_incr(asoc->cnt_on_reasm_queue);
991f8829a4aSRandall Stewart 		if (chk->rec.data.TSN_seq == cum_ackp1) {
992f8829a4aSRandall Stewart 			if (asoc->fragmented_delivery_inprogress == 0 &&
993f8829a4aSRandall Stewart 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) !=
994f8829a4aSRandall Stewart 			    SCTP_DATA_FIRST_FRAG) {
995f8829a4aSRandall Stewart 				/*
996f8829a4aSRandall Stewart 				 * An empty queue, no delivery inprogress,
997f8829a4aSRandall Stewart 				 * we hit the next one and it does NOT have
998f8829a4aSRandall Stewart 				 * a FIRST fragment mark.
999f8829a4aSRandall Stewart 				 */
1000ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not first, no fragmented delivery in progress\n");
1001139bc87fSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1002f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
1003f8829a4aSRandall Stewart 
1004f8829a4aSRandall Stewart 				if (oper) {
1005f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
1006f8829a4aSRandall Stewart 					uint32_t *ippp;
1007f8829a4aSRandall Stewart 
1008139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) =
1009f8829a4aSRandall Stewart 					    sizeof(struct sctp_paramhdr) +
1010f8829a4aSRandall Stewart 					    (sizeof(uint32_t) * 3);
1011f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
1012f8829a4aSRandall Stewart 					ph->param_type =
1013f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1014139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
1015f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
1016a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_2);
1017f8829a4aSRandall Stewart 					ippp++;
1018f8829a4aSRandall Stewart 					*ippp = chk->rec.data.TSN_seq;
1019f8829a4aSRandall Stewart 					ippp++;
1020f8829a4aSRandall Stewart 					*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1021f8829a4aSRandall Stewart 
1022f8829a4aSRandall Stewart 				}
1023a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_2;
1024f8829a4aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb,
1025ceaad40aSRandall Stewart 				    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1026f8829a4aSRandall Stewart 				*abort_flag = 1;
1027f8829a4aSRandall Stewart 			} else if (asoc->fragmented_delivery_inprogress &&
1028f8829a4aSRandall Stewart 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
1029f8829a4aSRandall Stewart 				/*
1030f8829a4aSRandall Stewart 				 * We are doing a partial delivery and the
1031f8829a4aSRandall Stewart 				 * NEXT chunk MUST be either the LAST or
1032f8829a4aSRandall Stewart 				 * MIDDLE fragment NOT a FIRST
1033f8829a4aSRandall Stewart 				 */
1034ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS a first and fragmented delivery in progress\n");
1035139bc87fSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1036f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
1037f8829a4aSRandall Stewart 				if (oper) {
1038f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
1039f8829a4aSRandall Stewart 					uint32_t *ippp;
1040f8829a4aSRandall Stewart 
1041139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) =
1042f8829a4aSRandall Stewart 					    sizeof(struct sctp_paramhdr) +
1043f8829a4aSRandall Stewart 					    (3 * sizeof(uint32_t));
1044f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
1045f8829a4aSRandall Stewart 					ph->param_type =
1046f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1047139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
1048f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
1049a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_3);
1050f8829a4aSRandall Stewart 					ippp++;
1051f8829a4aSRandall Stewart 					*ippp = chk->rec.data.TSN_seq;
1052f8829a4aSRandall Stewart 					ippp++;
1053f8829a4aSRandall Stewart 					*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1054f8829a4aSRandall Stewart 				}
1055a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_3;
1056f8829a4aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb,
1057ceaad40aSRandall Stewart 				    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1058f8829a4aSRandall Stewart 				*abort_flag = 1;
1059f8829a4aSRandall Stewart 			} else if (asoc->fragmented_delivery_inprogress) {
1060f8829a4aSRandall Stewart 				/*
1061f8829a4aSRandall Stewart 				 * Here we are ok with a MIDDLE or LAST
1062f8829a4aSRandall Stewart 				 * piece
1063f8829a4aSRandall Stewart 				 */
1064f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
1065f8829a4aSRandall Stewart 				    asoc->str_of_pdapi) {
1066f8829a4aSRandall Stewart 					/* Got to be the right STR No */
1067ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream number %d vs %d\n",
1068f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
1069f8829a4aSRandall Stewart 					    asoc->str_of_pdapi);
1070139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1071f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1072f8829a4aSRandall Stewart 					if (oper) {
1073f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1074f8829a4aSRandall Stewart 						uint32_t *ippp;
1075f8829a4aSRandall Stewart 
1076139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1077f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1078f8829a4aSRandall Stewart 						    (sizeof(uint32_t) * 3);
1079f8829a4aSRandall Stewart 						ph = mtod(oper,
1080f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1081f8829a4aSRandall Stewart 						ph->param_type =
1082f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1083f8829a4aSRandall Stewart 						ph->param_length =
1084139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1085f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1086a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_4);
1087f8829a4aSRandall Stewart 						ippp++;
1088f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1089f8829a4aSRandall Stewart 						ippp++;
1090f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1091f8829a4aSRandall Stewart 					}
1092a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_4;
1093f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1094ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1095f8829a4aSRandall Stewart 					*abort_flag = 1;
1096f8829a4aSRandall Stewart 				} else if ((asoc->fragment_flags & SCTP_DATA_UNORDERED) !=
1097f8829a4aSRandall Stewart 					    SCTP_DATA_UNORDERED &&
1098f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq !=
1099f8829a4aSRandall Stewart 				    asoc->ssn_of_pdapi) {
1100f8829a4aSRandall Stewart 					/* Got to be the right STR Seq */
1101ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream seq %d vs %d\n",
1102f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
1103f8829a4aSRandall Stewart 					    asoc->ssn_of_pdapi);
1104139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1105f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1106f8829a4aSRandall Stewart 					if (oper) {
1107f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1108f8829a4aSRandall Stewart 						uint32_t *ippp;
1109f8829a4aSRandall Stewart 
1110139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1111f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1112f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1113f8829a4aSRandall Stewart 						ph = mtod(oper,
1114f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1115f8829a4aSRandall Stewart 						ph->param_type =
1116f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1117f8829a4aSRandall Stewart 						ph->param_length =
1118139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1119f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1120a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_5);
1121f8829a4aSRandall Stewart 						ippp++;
1122f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1123f8829a4aSRandall Stewart 						ippp++;
1124f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1125f8829a4aSRandall Stewart 
1126f8829a4aSRandall Stewart 					}
1127a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_5;
1128f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1129ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1130f8829a4aSRandall Stewart 					*abort_flag = 1;
1131f8829a4aSRandall Stewart 				}
1132f8829a4aSRandall Stewart 			}
1133f8829a4aSRandall Stewart 		}
1134f8829a4aSRandall Stewart 		return;
1135f8829a4aSRandall Stewart 	}
1136f8829a4aSRandall Stewart 	/* Find its place */
1137f8829a4aSRandall Stewart 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
1138f8829a4aSRandall Stewart 		if (compare_with_wrap(at->rec.data.TSN_seq,
1139f8829a4aSRandall Stewart 		    chk->rec.data.TSN_seq, MAX_TSN)) {
1140f8829a4aSRandall Stewart 			/*
1141f8829a4aSRandall Stewart 			 * one in queue is bigger than the new one, insert
1142f8829a4aSRandall Stewart 			 * before this one
1143f8829a4aSRandall Stewart 			 */
1144f8829a4aSRandall Stewart 			/* A check */
1145f8829a4aSRandall Stewart 			asoc->size_on_reasm_queue += chk->send_size;
1146f8829a4aSRandall Stewart 			sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1147f8829a4aSRandall Stewart 			next = at;
1148f8829a4aSRandall Stewart 			TAILQ_INSERT_BEFORE(at, chk, sctp_next);
1149f8829a4aSRandall Stewart 			break;
1150f8829a4aSRandall Stewart 		} else if (at->rec.data.TSN_seq == chk->rec.data.TSN_seq) {
1151f8829a4aSRandall Stewart 			/* Gak, He sent me a duplicate str seq number */
1152f8829a4aSRandall Stewart 			/*
1153f8829a4aSRandall Stewart 			 * foo bar, I guess I will just free this new guy,
1154f8829a4aSRandall Stewart 			 * should we abort too? FIX ME MAYBE? Or it COULD be
1155f8829a4aSRandall Stewart 			 * that the SSN's have wrapped. Maybe I should
1156f8829a4aSRandall Stewart 			 * compare to TSN somehow... sigh for now just blow
1157f8829a4aSRandall Stewart 			 * away the chunk!
1158f8829a4aSRandall Stewart 			 */
1159f8829a4aSRandall Stewart 			if (chk->data) {
1160f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
1161f8829a4aSRandall Stewart 				chk->data = NULL;
1162f8829a4aSRandall Stewart 			}
1163f8829a4aSRandall Stewart 			sctp_free_a_chunk(stcb, chk);
1164f8829a4aSRandall Stewart 			return;
1165f8829a4aSRandall Stewart 		} else {
1166f8829a4aSRandall Stewart 			last_flags = at->rec.data.rcv_flags;
1167f8829a4aSRandall Stewart 			last_tsn = at->rec.data.TSN_seq;
1168f8829a4aSRandall Stewart 			prev = at;
1169f8829a4aSRandall Stewart 			if (TAILQ_NEXT(at, sctp_next) == NULL) {
1170f8829a4aSRandall Stewart 				/*
1171f8829a4aSRandall Stewart 				 * We are at the end, insert it after this
1172f8829a4aSRandall Stewart 				 * one
1173f8829a4aSRandall Stewart 				 */
1174f8829a4aSRandall Stewart 				/* check it first */
1175f8829a4aSRandall Stewart 				asoc->size_on_reasm_queue += chk->send_size;
1176f8829a4aSRandall Stewart 				sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1177f8829a4aSRandall Stewart 				TAILQ_INSERT_AFTER(&asoc->reasmqueue, at, chk, sctp_next);
1178f8829a4aSRandall Stewart 				break;
1179f8829a4aSRandall Stewart 			}
1180f8829a4aSRandall Stewart 		}
1181f8829a4aSRandall Stewart 	}
1182f8829a4aSRandall Stewart 	/* Now the audits */
1183f8829a4aSRandall Stewart 	if (prev) {
1184f8829a4aSRandall Stewart 		prev_tsn = chk->rec.data.TSN_seq - 1;
1185f8829a4aSRandall Stewart 		if (prev_tsn == prev->rec.data.TSN_seq) {
1186f8829a4aSRandall Stewart 			/*
1187f8829a4aSRandall Stewart 			 * Ok the one I am dropping onto the end is the
1188f8829a4aSRandall Stewart 			 * NEXT. A bit of valdiation here.
1189f8829a4aSRandall Stewart 			 */
1190f8829a4aSRandall Stewart 			if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1191f8829a4aSRandall Stewart 			    SCTP_DATA_FIRST_FRAG ||
1192f8829a4aSRandall Stewart 			    (prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1193f8829a4aSRandall Stewart 			    SCTP_DATA_MIDDLE_FRAG) {
1194f8829a4aSRandall Stewart 				/*
1195f8829a4aSRandall Stewart 				 * Insert chk MUST be a MIDDLE or LAST
1196f8829a4aSRandall Stewart 				 * fragment
1197f8829a4aSRandall Stewart 				 */
1198f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1199f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1200ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - It can be a midlle or last but not a first\n");
1201ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it's a FIRST!\n");
1202139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1203f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1204f8829a4aSRandall Stewart 					if (oper) {
1205f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1206f8829a4aSRandall Stewart 						uint32_t *ippp;
1207f8829a4aSRandall Stewart 
1208139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1209f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1210f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1211f8829a4aSRandall Stewart 						ph = mtod(oper,
1212f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1213f8829a4aSRandall Stewart 						ph->param_type =
1214f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1215f8829a4aSRandall Stewart 						ph->param_length =
1216139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1217f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1218a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_6);
1219f8829a4aSRandall Stewart 						ippp++;
1220f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1221f8829a4aSRandall Stewart 						ippp++;
1222f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1223f8829a4aSRandall Stewart 
1224f8829a4aSRandall Stewart 					}
1225a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_6;
1226f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1227ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1228f8829a4aSRandall Stewart 					*abort_flag = 1;
1229f8829a4aSRandall Stewart 					return;
1230f8829a4aSRandall Stewart 				}
1231f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
1232f8829a4aSRandall Stewart 				    prev->rec.data.stream_number) {
1233f8829a4aSRandall Stewart 					/*
1234f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1235f8829a4aSRandall Stewart 					 * they must be the same.
1236f8829a4aSRandall Stewart 					 */
1237ad81507eSRandall Stewart 					SCTP_PRINTF("Prev check - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1238f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
1239f8829a4aSRandall Stewart 					    prev->rec.data.stream_number);
1240139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1241f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1242f8829a4aSRandall Stewart 					if (oper) {
1243f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1244f8829a4aSRandall Stewart 						uint32_t *ippp;
1245f8829a4aSRandall Stewart 
1246139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1247f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1248f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1249f8829a4aSRandall Stewart 						ph = mtod(oper,
1250f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1251f8829a4aSRandall Stewart 						ph->param_type =
1252f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1253f8829a4aSRandall Stewart 						ph->param_length =
1254139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1255f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1256a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_7);
1257f8829a4aSRandall Stewart 						ippp++;
1258f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1259f8829a4aSRandall Stewart 						ippp++;
1260f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1261f8829a4aSRandall Stewart 					}
1262a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_7;
1263f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1264ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1265f8829a4aSRandall Stewart 
1266f8829a4aSRandall Stewart 					*abort_flag = 1;
1267f8829a4aSRandall Stewart 					return;
1268f8829a4aSRandall Stewart 				}
1269f8829a4aSRandall Stewart 				if ((prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1270f8829a4aSRandall Stewart 				    chk->rec.data.stream_seq !=
1271f8829a4aSRandall Stewart 				    prev->rec.data.stream_seq) {
1272f8829a4aSRandall Stewart 					/*
1273f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1274f8829a4aSRandall Stewart 					 * they must be the same.
1275f8829a4aSRandall Stewart 					 */
1276ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1277f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
1278f8829a4aSRandall Stewart 					    prev->rec.data.stream_seq);
1279139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1280f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1281f8829a4aSRandall Stewart 					if (oper) {
1282f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1283f8829a4aSRandall Stewart 						uint32_t *ippp;
1284f8829a4aSRandall Stewart 
1285139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1286f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1287f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1288f8829a4aSRandall Stewart 						ph = mtod(oper,
1289f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1290f8829a4aSRandall Stewart 						ph->param_type =
1291f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1292f8829a4aSRandall Stewart 						ph->param_length =
1293139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1294f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1295a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_8);
1296f8829a4aSRandall Stewart 						ippp++;
1297f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1298f8829a4aSRandall Stewart 						ippp++;
1299f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1300f8829a4aSRandall Stewart 					}
1301a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_8;
1302f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1303ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1304f8829a4aSRandall Stewart 
1305f8829a4aSRandall Stewart 					*abort_flag = 1;
1306f8829a4aSRandall Stewart 					return;
1307f8829a4aSRandall Stewart 				}
1308f8829a4aSRandall Stewart 			} else if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1309f8829a4aSRandall Stewart 			    SCTP_DATA_LAST_FRAG) {
1310f8829a4aSRandall Stewart 				/* Insert chk MUST be a FIRST */
1311f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1312f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1313ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, evil plot, its not FIRST and it must be!\n");
1314139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1315f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1316f8829a4aSRandall Stewart 					if (oper) {
1317f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1318f8829a4aSRandall Stewart 						uint32_t *ippp;
1319f8829a4aSRandall Stewart 
1320139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1321f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1322f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1323f8829a4aSRandall Stewart 						ph = mtod(oper,
1324f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1325f8829a4aSRandall Stewart 						ph->param_type =
1326f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1327f8829a4aSRandall Stewart 						ph->param_length =
1328139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1329f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1330a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_9);
1331f8829a4aSRandall Stewart 						ippp++;
1332f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1333f8829a4aSRandall Stewart 						ippp++;
1334f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1335f8829a4aSRandall Stewart 
1336f8829a4aSRandall Stewart 					}
1337a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_9;
1338f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1339ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1340f8829a4aSRandall Stewart 
1341f8829a4aSRandall Stewart 					*abort_flag = 1;
1342f8829a4aSRandall Stewart 					return;
1343f8829a4aSRandall Stewart 				}
1344f8829a4aSRandall Stewart 			}
1345f8829a4aSRandall Stewart 		}
1346f8829a4aSRandall Stewart 	}
1347f8829a4aSRandall Stewart 	if (next) {
1348f8829a4aSRandall Stewart 		post_tsn = chk->rec.data.TSN_seq + 1;
1349f8829a4aSRandall Stewart 		if (post_tsn == next->rec.data.TSN_seq) {
1350f8829a4aSRandall Stewart 			/*
1351f8829a4aSRandall Stewart 			 * Ok the one I am inserting ahead of is my NEXT
1352f8829a4aSRandall Stewart 			 * one. A bit of valdiation here.
1353f8829a4aSRandall Stewart 			 */
1354f8829a4aSRandall Stewart 			if (next->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
1355f8829a4aSRandall Stewart 				/* Insert chk MUST be a last fragment */
1356f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK)
1357f8829a4aSRandall Stewart 				    != SCTP_DATA_LAST_FRAG) {
1358ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is FIRST, we must be LAST\n");
1359ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not a last!\n");
1360139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1361f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1362f8829a4aSRandall Stewart 					if (oper) {
1363f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1364f8829a4aSRandall Stewart 						uint32_t *ippp;
1365f8829a4aSRandall Stewart 
1366139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1367f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1368f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1369f8829a4aSRandall Stewart 						ph = mtod(oper,
1370f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1371f8829a4aSRandall Stewart 						ph->param_type =
1372f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1373f8829a4aSRandall Stewart 						ph->param_length =
1374139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1375f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1376a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_10);
1377f8829a4aSRandall Stewart 						ippp++;
1378f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1379f8829a4aSRandall Stewart 						ippp++;
1380f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1381f8829a4aSRandall Stewart 					}
1382a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_10;
1383f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1384ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1385f8829a4aSRandall Stewart 
1386f8829a4aSRandall Stewart 					*abort_flag = 1;
1387f8829a4aSRandall Stewart 					return;
1388f8829a4aSRandall Stewart 				}
1389f8829a4aSRandall Stewart 			} else if ((next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1390f8829a4aSRandall Stewart 				    SCTP_DATA_MIDDLE_FRAG ||
1391f8829a4aSRandall Stewart 				    (next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1392f8829a4aSRandall Stewart 			    SCTP_DATA_LAST_FRAG) {
1393f8829a4aSRandall Stewart 				/*
1394f8829a4aSRandall Stewart 				 * Insert chk CAN be MIDDLE or FIRST NOT
1395f8829a4aSRandall Stewart 				 * LAST
1396f8829a4aSRandall Stewart 				 */
1397f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1398f8829a4aSRandall Stewart 				    SCTP_DATA_LAST_FRAG) {
1399ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is a MIDDLE/LAST\n");
1400ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, new prev chunk is a LAST\n");
1401139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1402f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1403f8829a4aSRandall Stewart 					if (oper) {
1404f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1405f8829a4aSRandall Stewart 						uint32_t *ippp;
1406f8829a4aSRandall Stewart 
1407139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1408f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1409f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1410f8829a4aSRandall Stewart 						ph = mtod(oper,
1411f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1412f8829a4aSRandall Stewart 						ph->param_type =
1413f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1414f8829a4aSRandall Stewart 						ph->param_length =
1415139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1416f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1417a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_11);
1418f8829a4aSRandall Stewart 						ippp++;
1419f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1420f8829a4aSRandall Stewart 						ippp++;
1421f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1422f8829a4aSRandall Stewart 
1423f8829a4aSRandall Stewart 					}
1424a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_11;
1425f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1426ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1427f8829a4aSRandall Stewart 
1428f8829a4aSRandall Stewart 					*abort_flag = 1;
1429f8829a4aSRandall Stewart 					return;
1430f8829a4aSRandall Stewart 				}
1431f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
1432f8829a4aSRandall Stewart 				    next->rec.data.stream_number) {
1433f8829a4aSRandall Stewart 					/*
1434f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1435f8829a4aSRandall Stewart 					 * they must be the same.
1436f8829a4aSRandall Stewart 					 */
1437ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1438f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
1439f8829a4aSRandall Stewart 					    next->rec.data.stream_number);
1440139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1441f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1442f8829a4aSRandall Stewart 					if (oper) {
1443f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1444f8829a4aSRandall Stewart 						uint32_t *ippp;
1445f8829a4aSRandall Stewart 
1446139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1447f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1448f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1449f8829a4aSRandall Stewart 						ph = mtod(oper,
1450f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1451f8829a4aSRandall Stewart 						ph->param_type =
1452f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1453f8829a4aSRandall Stewart 						ph->param_length =
1454139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1455f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1456a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_12);
1457f8829a4aSRandall Stewart 						ippp++;
1458f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1459f8829a4aSRandall Stewart 						ippp++;
1460f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1461f8829a4aSRandall Stewart 
1462f8829a4aSRandall Stewart 					}
1463a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_12;
1464f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1465ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1466f8829a4aSRandall Stewart 
1467f8829a4aSRandall Stewart 					*abort_flag = 1;
1468f8829a4aSRandall Stewart 					return;
1469f8829a4aSRandall Stewart 				}
1470f8829a4aSRandall Stewart 				if ((next->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1471f8829a4aSRandall Stewart 				    chk->rec.data.stream_seq !=
1472f8829a4aSRandall Stewart 				    next->rec.data.stream_seq) {
1473f8829a4aSRandall Stewart 					/*
1474f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1475f8829a4aSRandall Stewart 					 * they must be the same.
1476f8829a4aSRandall Stewart 					 */
1477ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1478f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
1479f8829a4aSRandall Stewart 					    next->rec.data.stream_seq);
1480139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1481f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1482f8829a4aSRandall Stewart 					if (oper) {
1483f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1484f8829a4aSRandall Stewart 						uint32_t *ippp;
1485f8829a4aSRandall Stewart 
1486139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1487f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1488f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1489f8829a4aSRandall Stewart 						ph = mtod(oper,
1490f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1491f8829a4aSRandall Stewart 						ph->param_type =
1492f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1493f8829a4aSRandall Stewart 						ph->param_length =
1494139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1495f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1496a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_13);
1497f8829a4aSRandall Stewart 						ippp++;
1498f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1499f8829a4aSRandall Stewart 						ippp++;
1500f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1501f8829a4aSRandall Stewart 					}
1502a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_13;
1503f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1504ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1505f8829a4aSRandall Stewart 
1506f8829a4aSRandall Stewart 					*abort_flag = 1;
1507f8829a4aSRandall Stewart 					return;
1508f8829a4aSRandall Stewart 				}
1509f8829a4aSRandall Stewart 			}
1510f8829a4aSRandall Stewart 		}
1511f8829a4aSRandall Stewart 	}
1512f8829a4aSRandall Stewart 	/* Do we need to do some delivery? check */
1513f8829a4aSRandall Stewart 	sctp_deliver_reasm_check(stcb, asoc);
1514f8829a4aSRandall Stewart }
1515f8829a4aSRandall Stewart 
1516f8829a4aSRandall Stewart /*
1517f8829a4aSRandall Stewart  * This is an unfortunate routine. It checks to make sure a evil guy is not
1518f8829a4aSRandall Stewart  * stuffing us full of bad packet fragments. A broken peer could also do this
1519f8829a4aSRandall Stewart  * but this is doubtful. It is to bad I must worry about evil crackers sigh
1520f8829a4aSRandall Stewart  * :< more cycles.
1521f8829a4aSRandall Stewart  */
1522f8829a4aSRandall Stewart static int
1523f8829a4aSRandall Stewart sctp_does_tsn_belong_to_reasm(struct sctp_association *asoc,
1524f8829a4aSRandall Stewart     uint32_t TSN_seq)
1525f8829a4aSRandall Stewart {
1526f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *at;
1527f8829a4aSRandall Stewart 	uint32_t tsn_est;
1528f8829a4aSRandall Stewart 
1529f8829a4aSRandall Stewart 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
1530f8829a4aSRandall Stewart 		if (compare_with_wrap(TSN_seq,
1531f8829a4aSRandall Stewart 		    at->rec.data.TSN_seq, MAX_TSN)) {
1532f8829a4aSRandall Stewart 			/* is it one bigger? */
1533f8829a4aSRandall Stewart 			tsn_est = at->rec.data.TSN_seq + 1;
1534f8829a4aSRandall Stewart 			if (tsn_est == TSN_seq) {
1535f8829a4aSRandall Stewart 				/* yep. It better be a last then */
1536f8829a4aSRandall Stewart 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1537f8829a4aSRandall Stewart 				    SCTP_DATA_LAST_FRAG) {
1538f8829a4aSRandall Stewart 					/*
1539f8829a4aSRandall Stewart 					 * Ok this guy belongs next to a guy
1540f8829a4aSRandall Stewart 					 * that is NOT last, it should be a
1541f8829a4aSRandall Stewart 					 * middle/last, not a complete
1542f8829a4aSRandall Stewart 					 * chunk.
1543f8829a4aSRandall Stewart 					 */
1544f8829a4aSRandall Stewart 					return (1);
1545f8829a4aSRandall Stewart 				} else {
1546f8829a4aSRandall Stewart 					/*
1547f8829a4aSRandall Stewart 					 * This guy is ok since its a LAST
1548f8829a4aSRandall Stewart 					 * and the new chunk is a fully
1549f8829a4aSRandall Stewart 					 * self- contained one.
1550f8829a4aSRandall Stewart 					 */
1551f8829a4aSRandall Stewart 					return (0);
1552f8829a4aSRandall Stewart 				}
1553f8829a4aSRandall Stewart 			}
1554f8829a4aSRandall Stewart 		} else if (TSN_seq == at->rec.data.TSN_seq) {
1555f8829a4aSRandall Stewart 			/* Software error since I have a dup? */
1556f8829a4aSRandall Stewart 			return (1);
1557f8829a4aSRandall Stewart 		} else {
1558f8829a4aSRandall Stewart 			/*
1559f8829a4aSRandall Stewart 			 * Ok, 'at' is larger than new chunk but does it
1560f8829a4aSRandall Stewart 			 * need to be right before it.
1561f8829a4aSRandall Stewart 			 */
1562f8829a4aSRandall Stewart 			tsn_est = TSN_seq + 1;
1563f8829a4aSRandall Stewart 			if (tsn_est == at->rec.data.TSN_seq) {
1564f8829a4aSRandall Stewart 				/* Yep, It better be a first */
1565f8829a4aSRandall Stewart 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1566f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1567f8829a4aSRandall Stewart 					return (1);
1568f8829a4aSRandall Stewart 				} else {
1569f8829a4aSRandall Stewart 					return (0);
1570f8829a4aSRandall Stewart 				}
1571f8829a4aSRandall Stewart 			}
1572f8829a4aSRandall Stewart 		}
1573f8829a4aSRandall Stewart 	}
1574f8829a4aSRandall Stewart 	return (0);
1575f8829a4aSRandall Stewart }
1576f8829a4aSRandall Stewart 
1577f8829a4aSRandall Stewart 
1578f8829a4aSRandall Stewart static int
1579f8829a4aSRandall Stewart sctp_process_a_data_chunk(struct sctp_tcb *stcb, struct sctp_association *asoc,
1580f8829a4aSRandall Stewart     struct mbuf **m, int offset, struct sctp_data_chunk *ch, int chk_length,
1581f8829a4aSRandall Stewart     struct sctp_nets *net, uint32_t * high_tsn, int *abort_flag,
1582f8829a4aSRandall Stewart     int *break_flag, int last_chunk)
1583f8829a4aSRandall Stewart {
1584f8829a4aSRandall Stewart 	/* Process a data chunk */
1585f8829a4aSRandall Stewart 	/* struct sctp_tmit_chunk *chk; */
1586f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
1587f8829a4aSRandall Stewart 	uint32_t tsn, gap;
1588830d754dSRandall Stewart 
1589830d754dSRandall Stewart 	/* EY - for nr_sack */
1590830d754dSRandall Stewart 	uint32_t nr_gap;
1591f8829a4aSRandall Stewart 	struct mbuf *dmbuf;
1592f8829a4aSRandall Stewart 	int indx, the_len;
1593139bc87fSRandall Stewart 	int need_reasm_check = 0;
1594f8829a4aSRandall Stewart 	uint16_t strmno, strmseq;
1595f8829a4aSRandall Stewart 	struct mbuf *oper;
1596f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
1597f42a358aSRandall Stewart 	int ordered;
1598f42a358aSRandall Stewart 	uint32_t protocol_id;
1599f42a358aSRandall Stewart 	uint8_t chunk_flags;
160017205eccSRandall Stewart 	struct sctp_stream_reset_list *liste;
1601f8829a4aSRandall Stewart 
1602f8829a4aSRandall Stewart 	chk = NULL;
1603f8829a4aSRandall Stewart 	tsn = ntohl(ch->dp.tsn);
1604f42a358aSRandall Stewart 	chunk_flags = ch->ch.chunk_flags;
1605b3f1ea41SRandall Stewart 	if ((chunk_flags & SCTP_DATA_SACK_IMMEDIATELY) == SCTP_DATA_SACK_IMMEDIATELY) {
1606b3f1ea41SRandall Stewart 		asoc->send_sack = 1;
1607b3f1ea41SRandall Stewart 	}
1608f42a358aSRandall Stewart 	protocol_id = ch->dp.protocol_id;
1609f42a358aSRandall Stewart 	ordered = ((ch->ch.chunk_flags & SCTP_DATA_UNORDERED) == 0);
1610b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1611c4739e2fSRandall Stewart 		sctp_log_map(tsn, asoc->cumulative_tsn, asoc->highest_tsn_inside_map, SCTP_MAP_TSN_ENTERS);
161280fefe0aSRandall Stewart 	}
1613ad81507eSRandall Stewart 	if (stcb == NULL) {
1614ad81507eSRandall Stewart 		return (0);
1615ad81507eSRandall Stewart 	}
161680fefe0aSRandall Stewart 	SCTP_LTRACE_CHK(stcb->sctp_ep, stcb, ch->ch.chunk_type, tsn);
1617f8829a4aSRandall Stewart 	if (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN) ||
1618f8829a4aSRandall Stewart 	    asoc->cumulative_tsn == tsn) {
1619f8829a4aSRandall Stewart 		/* It is a duplicate */
1620f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvdupdata);
1621f8829a4aSRandall Stewart 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1622f8829a4aSRandall Stewart 			/* Record a dup for the next outbound sack */
1623f8829a4aSRandall Stewart 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1624f8829a4aSRandall Stewart 			asoc->numduptsns++;
1625f8829a4aSRandall Stewart 		}
1626b201f536SRandall Stewart 		asoc->send_sack = 1;
1627f8829a4aSRandall Stewart 		return (0);
1628f8829a4aSRandall Stewart 	}
1629f8829a4aSRandall Stewart 	/* Calculate the number of TSN's between the base and this TSN */
1630d50c1d79SRandall Stewart 	SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
1631f8829a4aSRandall Stewart 	if (gap >= (SCTP_MAPPING_ARRAY << 3)) {
1632f8829a4aSRandall Stewart 		/* Can't hold the bit in the mapping at max array, toss it */
1633f8829a4aSRandall Stewart 		return (0);
1634f8829a4aSRandall Stewart 	}
1635f8829a4aSRandall Stewart 	if (gap >= (uint32_t) (asoc->mapping_array_size << 3)) {
1636207304d4SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
16370696e120SRandall Stewart 		if (sctp_expand_mapping_array(asoc, gap)) {
1638f8829a4aSRandall Stewart 			/* Can't expand, drop it */
1639f8829a4aSRandall Stewart 			return (0);
1640f8829a4aSRandall Stewart 		}
1641f8829a4aSRandall Stewart 	}
1642830d754dSRandall Stewart 	/* EY - for nr_sack */
1643830d754dSRandall Stewart 	nr_gap = gap;
1644830d754dSRandall Stewart 
1645f8829a4aSRandall Stewart 	if (compare_with_wrap(tsn, *high_tsn, MAX_TSN)) {
1646f8829a4aSRandall Stewart 		*high_tsn = tsn;
1647f8829a4aSRandall Stewart 	}
1648f8829a4aSRandall Stewart 	/* See if we have received this one already */
1649f8829a4aSRandall Stewart 	if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
1650f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvdupdata);
1651f8829a4aSRandall Stewart 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1652f8829a4aSRandall Stewart 			/* Record a dup for the next outbound sack */
1653f8829a4aSRandall Stewart 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1654f8829a4aSRandall Stewart 			asoc->numduptsns++;
1655f8829a4aSRandall Stewart 		}
165642551e99SRandall Stewart 		asoc->send_sack = 1;
1657f8829a4aSRandall Stewart 		return (0);
1658f8829a4aSRandall Stewart 	}
1659f8829a4aSRandall Stewart 	/*
1660f8829a4aSRandall Stewart 	 * Check to see about the GONE flag, duplicates would cause a sack
1661f8829a4aSRandall Stewart 	 * to be sent up above
1662f8829a4aSRandall Stewart 	 */
1663ad81507eSRandall Stewart 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1664f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1665f8829a4aSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET))
1666f8829a4aSRandall Stewart 	    ) {
1667f8829a4aSRandall Stewart 		/*
1668f8829a4aSRandall Stewart 		 * wait a minute, this guy is gone, there is no longer a
1669f8829a4aSRandall Stewart 		 * receiver. Send peer an ABORT!
1670f8829a4aSRandall Stewart 		 */
1671f8829a4aSRandall Stewart 		struct mbuf *op_err;
1672f8829a4aSRandall Stewart 
1673f8829a4aSRandall Stewart 		op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1674ceaad40aSRandall Stewart 		sctp_abort_an_association(stcb->sctp_ep, stcb, 0, op_err, SCTP_SO_NOT_LOCKED);
1675f8829a4aSRandall Stewart 		*abort_flag = 1;
1676f8829a4aSRandall Stewart 		return (0);
1677f8829a4aSRandall Stewart 	}
1678f8829a4aSRandall Stewart 	/*
1679f8829a4aSRandall Stewart 	 * Now before going further we see if there is room. If NOT then we
1680f8829a4aSRandall Stewart 	 * MAY let one through only IF this TSN is the one we are waiting
1681f8829a4aSRandall Stewart 	 * for on a partial delivery API.
1682f8829a4aSRandall Stewart 	 */
1683f8829a4aSRandall Stewart 
1684f8829a4aSRandall Stewart 	/* now do the tests */
1685f8829a4aSRandall Stewart 	if (((asoc->cnt_on_all_streams +
1686f8829a4aSRandall Stewart 	    asoc->cnt_on_reasm_queue +
1687b3f1ea41SRandall Stewart 	    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) ||
1688f8829a4aSRandall Stewart 	    (((int)asoc->my_rwnd) <= 0)) {
1689f8829a4aSRandall Stewart 		/*
1690f8829a4aSRandall Stewart 		 * When we have NO room in the rwnd we check to make sure
1691f8829a4aSRandall Stewart 		 * the reader is doing its job...
1692f8829a4aSRandall Stewart 		 */
1693f8829a4aSRandall Stewart 		if (stcb->sctp_socket->so_rcv.sb_cc) {
1694f8829a4aSRandall Stewart 			/* some to read, wake-up */
1695ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1696ceaad40aSRandall Stewart 			struct socket *so;
1697ceaad40aSRandall Stewart 
1698ceaad40aSRandall Stewart 			so = SCTP_INP_SO(stcb->sctp_ep);
1699ceaad40aSRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, 1);
1700ceaad40aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
1701ceaad40aSRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
1702ceaad40aSRandall Stewart 			SCTP_TCB_LOCK(stcb);
1703ceaad40aSRandall Stewart 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
1704ceaad40aSRandall Stewart 			if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1705ceaad40aSRandall Stewart 				/* assoc was freed while we were unlocked */
1706ceaad40aSRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
1707ceaad40aSRandall Stewart 				return (0);
1708ceaad40aSRandall Stewart 			}
1709ceaad40aSRandall Stewart #endif
1710f8829a4aSRandall Stewart 			sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1711ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1712ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
1713ceaad40aSRandall Stewart #endif
1714f8829a4aSRandall Stewart 		}
1715f8829a4aSRandall Stewart 		/* now is it in the mapping array of what we have accepted? */
1716b3f1ea41SRandall Stewart 		if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
1717f8829a4aSRandall Stewart 			/* Nope not in the valid range dump it */
1718f8829a4aSRandall Stewart 			sctp_set_rwnd(stcb, asoc);
1719f8829a4aSRandall Stewart 			if ((asoc->cnt_on_all_streams +
1720f8829a4aSRandall Stewart 			    asoc->cnt_on_reasm_queue +
1721b3f1ea41SRandall Stewart 			    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) {
1722f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_datadropchklmt);
1723f8829a4aSRandall Stewart 			} else {
1724f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_datadroprwnd);
1725f8829a4aSRandall Stewart 			}
1726f8829a4aSRandall Stewart 			indx = *break_flag;
1727f8829a4aSRandall Stewart 			*break_flag = 1;
1728f8829a4aSRandall Stewart 			return (0);
1729f8829a4aSRandall Stewart 		}
1730f8829a4aSRandall Stewart 	}
1731f8829a4aSRandall Stewart 	strmno = ntohs(ch->dp.stream_id);
1732f8829a4aSRandall Stewart 	if (strmno >= asoc->streamincnt) {
1733f8829a4aSRandall Stewart 		struct sctp_paramhdr *phdr;
1734f8829a4aSRandall Stewart 		struct mbuf *mb;
1735f8829a4aSRandall Stewart 
1736f8829a4aSRandall Stewart 		mb = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) * 2),
1737139bc87fSRandall Stewart 		    0, M_DONTWAIT, 1, MT_DATA);
1738f8829a4aSRandall Stewart 		if (mb != NULL) {
1739f8829a4aSRandall Stewart 			/* add some space up front so prepend will work well */
1740139bc87fSRandall Stewart 			SCTP_BUF_RESV_UF(mb, sizeof(struct sctp_chunkhdr));
1741f8829a4aSRandall Stewart 			phdr = mtod(mb, struct sctp_paramhdr *);
1742f8829a4aSRandall Stewart 			/*
1743f8829a4aSRandall Stewart 			 * Error causes are just param's and this one has
1744f8829a4aSRandall Stewart 			 * two back to back phdr, one with the error type
1745f8829a4aSRandall Stewart 			 * and size, the other with the streamid and a rsvd
1746f8829a4aSRandall Stewart 			 */
1747139bc87fSRandall Stewart 			SCTP_BUF_LEN(mb) = (sizeof(struct sctp_paramhdr) * 2);
1748f8829a4aSRandall Stewart 			phdr->param_type = htons(SCTP_CAUSE_INVALID_STREAM);
1749f8829a4aSRandall Stewart 			phdr->param_length =
1750f8829a4aSRandall Stewart 			    htons(sizeof(struct sctp_paramhdr) * 2);
1751f8829a4aSRandall Stewart 			phdr++;
1752f8829a4aSRandall Stewart 			/* We insert the stream in the type field */
1753f8829a4aSRandall Stewart 			phdr->param_type = ch->dp.stream_id;
1754f8829a4aSRandall Stewart 			/* And set the length to 0 for the rsvd field */
1755f8829a4aSRandall Stewart 			phdr->param_length = 0;
1756f8829a4aSRandall Stewart 			sctp_queue_op_err(stcb, mb);
1757f8829a4aSRandall Stewart 		}
1758f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_badsid);
1759207304d4SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
1760d06c82f1SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
1761830d754dSRandall Stewart 		/* EY set this tsn present in  nr_sack's nr_mapping_array */
1762830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
1763830d754dSRandall Stewart 			SCTP_TCB_LOCK_ASSERT(stcb);
1764830d754dSRandall Stewart 			SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
1765d50c1d79SRandall Stewart 			SCTP_REVERSE_OUT_TSN_PRES(gap, tsn, asoc);
1766830d754dSRandall Stewart 		}
1767d06c82f1SRandall Stewart 		if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
1768d06c82f1SRandall Stewart 			/* we have a new high score */
1769d06c82f1SRandall Stewart 			asoc->highest_tsn_inside_map = tsn;
1770830d754dSRandall Stewart 			/* EY nr_sack version of the above */
1771830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack)
1772830d754dSRandall Stewart 				asoc->highest_tsn_inside_nr_map = tsn;
1773b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1774d06c82f1SRandall Stewart 				sctp_log_map(0, 2, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
177580fefe0aSRandall Stewart 			}
1776d06c82f1SRandall Stewart 		}
1777d06c82f1SRandall Stewart 		if (tsn == (asoc->cumulative_tsn + 1)) {
1778d06c82f1SRandall Stewart 			/* Update cum-ack */
1779d06c82f1SRandall Stewart 			asoc->cumulative_tsn = tsn;
1780d06c82f1SRandall Stewart 		}
1781f8829a4aSRandall Stewart 		return (0);
1782f8829a4aSRandall Stewart 	}
1783f8829a4aSRandall Stewart 	/*
1784f8829a4aSRandall Stewart 	 * Before we continue lets validate that we are not being fooled by
1785f8829a4aSRandall Stewart 	 * an evil attacker. We can only have 4k chunks based on our TSN
1786f8829a4aSRandall Stewart 	 * spread allowed by the mapping array 512 * 8 bits, so there is no
1787f8829a4aSRandall Stewart 	 * way our stream sequence numbers could have wrapped. We of course
1788f8829a4aSRandall Stewart 	 * only validate the FIRST fragment so the bit must be set.
1789f8829a4aSRandall Stewart 	 */
1790f8829a4aSRandall Stewart 	strmseq = ntohs(ch->dp.stream_sequence);
1791f42a358aSRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
179218e198d3SRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
179318e198d3SRandall Stewart 	if (asoc->tsn_in_at >= SCTP_TSN_LOG_SIZE) {
179418e198d3SRandall Stewart 		asoc->tsn_in_at = 0;
179518e198d3SRandall Stewart 		asoc->tsn_in_wrapped = 1;
179618e198d3SRandall Stewart 	}
1797f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].tsn = tsn;
1798f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].strm = strmno;
1799f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].seq = strmseq;
1800f1f73e57SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].sz = chk_length;
1801f1f73e57SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].flgs = chunk_flags;
180218e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].stcb = (void *)stcb;
180318e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].in_pos = asoc->tsn_in_at;
180418e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].in_out = 1;
1805f42a358aSRandall Stewart 	asoc->tsn_in_at++;
1806f42a358aSRandall Stewart #endif
1807f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_FIRST_FRAG) &&
1808d61a0ae0SRandall Stewart 	    (TAILQ_EMPTY(&asoc->resetHead)) &&
1809f42a358aSRandall Stewart 	    (chunk_flags & SCTP_DATA_UNORDERED) == 0 &&
1810f8829a4aSRandall Stewart 	    (compare_with_wrap(asoc->strmin[strmno].last_sequence_delivered,
1811f8829a4aSRandall Stewart 	    strmseq, MAX_SEQ) ||
1812f8829a4aSRandall Stewart 	    asoc->strmin[strmno].last_sequence_delivered == strmseq)) {
1813f8829a4aSRandall Stewart 		/* The incoming sseq is behind where we last delivered? */
1814ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1, "EVIL/Broken-Dup S-SEQ:%d delivered:%d from peer, Abort!\n",
1815ad81507eSRandall Stewart 		    strmseq, asoc->strmin[strmno].last_sequence_delivered);
1816139bc87fSRandall Stewart 		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1817f8829a4aSRandall Stewart 		    0, M_DONTWAIT, 1, MT_DATA);
1818f8829a4aSRandall Stewart 		if (oper) {
1819f8829a4aSRandall Stewart 			struct sctp_paramhdr *ph;
1820f8829a4aSRandall Stewart 			uint32_t *ippp;
1821f8829a4aSRandall Stewart 
1822139bc87fSRandall Stewart 			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
1823f8829a4aSRandall Stewart 			    (3 * sizeof(uint32_t));
1824f8829a4aSRandall Stewart 			ph = mtod(oper, struct sctp_paramhdr *);
1825f8829a4aSRandall Stewart 			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1826139bc87fSRandall Stewart 			ph->param_length = htons(SCTP_BUF_LEN(oper));
1827f8829a4aSRandall Stewart 			ippp = (uint32_t *) (ph + 1);
1828a5d547adSRandall Stewart 			*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_14);
1829f8829a4aSRandall Stewart 			ippp++;
1830f8829a4aSRandall Stewart 			*ippp = tsn;
1831f8829a4aSRandall Stewart 			ippp++;
1832f8829a4aSRandall Stewart 			*ippp = ((strmno << 16) | strmseq);
1833f8829a4aSRandall Stewart 
1834f8829a4aSRandall Stewart 		}
1835a5d547adSRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_14;
1836f8829a4aSRandall Stewart 		sctp_abort_an_association(stcb->sctp_ep, stcb,
1837ceaad40aSRandall Stewart 		    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1838f8829a4aSRandall Stewart 		*abort_flag = 1;
1839f8829a4aSRandall Stewart 		return (0);
1840f8829a4aSRandall Stewart 	}
1841f42a358aSRandall Stewart 	/************************************
1842f42a358aSRandall Stewart 	 * From here down we may find ch-> invalid
1843f42a358aSRandall Stewart 	 * so its a good idea NOT to use it.
1844f42a358aSRandall Stewart 	 *************************************/
1845f42a358aSRandall Stewart 
1846f8829a4aSRandall Stewart 	the_len = (chk_length - sizeof(struct sctp_data_chunk));
1847f8829a4aSRandall Stewart 	if (last_chunk == 0) {
184844b7479bSRandall Stewart 		dmbuf = SCTP_M_COPYM(*m,
1849f8829a4aSRandall Stewart 		    (offset + sizeof(struct sctp_data_chunk)),
1850f8829a4aSRandall Stewart 		    the_len, M_DONTWAIT);
1851f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
1852b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
1853f8829a4aSRandall Stewart 			struct mbuf *mat;
1854f8829a4aSRandall Stewart 
1855f8829a4aSRandall Stewart 			mat = dmbuf;
1856f8829a4aSRandall Stewart 			while (mat) {
1857139bc87fSRandall Stewart 				if (SCTP_BUF_IS_EXTENDED(mat)) {
1858f8829a4aSRandall Stewart 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
1859f8829a4aSRandall Stewart 				}
1860139bc87fSRandall Stewart 				mat = SCTP_BUF_NEXT(mat);
1861f8829a4aSRandall Stewart 			}
1862f8829a4aSRandall Stewart 		}
1863f8829a4aSRandall Stewart #endif
1864f8829a4aSRandall Stewart 	} else {
1865f8829a4aSRandall Stewart 		/* We can steal the last chunk */
1866139bc87fSRandall Stewart 		int l_len;
1867139bc87fSRandall Stewart 
1868f8829a4aSRandall Stewart 		dmbuf = *m;
1869f8829a4aSRandall Stewart 		/* lop off the top part */
1870f8829a4aSRandall Stewart 		m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk)));
1871139bc87fSRandall Stewart 		if (SCTP_BUF_NEXT(dmbuf) == NULL) {
1872139bc87fSRandall Stewart 			l_len = SCTP_BUF_LEN(dmbuf);
1873139bc87fSRandall Stewart 		} else {
1874139bc87fSRandall Stewart 			/*
1875139bc87fSRandall Stewart 			 * need to count up the size hopefully does not hit
1876139bc87fSRandall Stewart 			 * this to often :-0
1877139bc87fSRandall Stewart 			 */
1878139bc87fSRandall Stewart 			struct mbuf *lat;
1879139bc87fSRandall Stewart 
1880139bc87fSRandall Stewart 			l_len = 0;
1881139bc87fSRandall Stewart 			lat = dmbuf;
1882139bc87fSRandall Stewart 			while (lat) {
1883139bc87fSRandall Stewart 				l_len += SCTP_BUF_LEN(lat);
1884139bc87fSRandall Stewart 				lat = SCTP_BUF_NEXT(lat);
1885139bc87fSRandall Stewart 			}
1886139bc87fSRandall Stewart 		}
1887139bc87fSRandall Stewart 		if (l_len > the_len) {
1888f8829a4aSRandall Stewart 			/* Trim the end round bytes off  too */
1889139bc87fSRandall Stewart 			m_adj(dmbuf, -(l_len - the_len));
1890f8829a4aSRandall Stewart 		}
1891f8829a4aSRandall Stewart 	}
1892f8829a4aSRandall Stewart 	if (dmbuf == NULL) {
1893f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_nomem);
1894f8829a4aSRandall Stewart 		return (0);
1895f8829a4aSRandall Stewart 	}
1896f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG &&
1897f8829a4aSRandall Stewart 	    asoc->fragmented_delivery_inprogress == 0 &&
1898f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->resetHead) &&
1899f42a358aSRandall Stewart 	    ((ordered == 0) ||
1900f8829a4aSRandall Stewart 	    ((asoc->strmin[strmno].last_sequence_delivered + 1) == strmseq &&
1901f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->strmin[strmno].inqueue)))) {
1902f8829a4aSRandall Stewart 		/* Candidate for express delivery */
1903f8829a4aSRandall Stewart 		/*
1904f8829a4aSRandall Stewart 		 * Its not fragmented, No PD-API is up, Nothing in the
1905f8829a4aSRandall Stewart 		 * delivery queue, Its un-ordered OR ordered and the next to
1906f8829a4aSRandall Stewart 		 * deliver AND nothing else is stuck on the stream queue,
1907f8829a4aSRandall Stewart 		 * And there is room for it in the socket buffer. Lets just
1908f8829a4aSRandall Stewart 		 * stuff it up the buffer....
1909f8829a4aSRandall Stewart 		 */
1910f8829a4aSRandall Stewart 
1911f8829a4aSRandall Stewart 		/* It would be nice to avoid this copy if we could :< */
1912f8829a4aSRandall Stewart 		sctp_alloc_a_readq(stcb, control);
1913f8829a4aSRandall Stewart 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1914f42a358aSRandall Stewart 		    protocol_id,
1915f8829a4aSRandall Stewart 		    stcb->asoc.context,
1916f8829a4aSRandall Stewart 		    strmno, strmseq,
1917f42a358aSRandall Stewart 		    chunk_flags,
1918f8829a4aSRandall Stewart 		    dmbuf);
1919f8829a4aSRandall Stewart 		if (control == NULL) {
1920f8829a4aSRandall Stewart 			goto failed_express_del;
1921f8829a4aSRandall Stewart 		}
1922cfde3ff7SRandall Stewart 		sctp_add_to_readq(stcb->sctp_ep, stcb,
1923cfde3ff7SRandall Stewart 		    control, &stcb->sctp_socket->so_rcv,
1924cfde3ff7SRandall Stewart 		    1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
1925830d754dSRandall Stewart 
1926830d754dSRandall Stewart 		/*
1927830d754dSRandall Stewart 		 * EY here I should check if this delivered tsn is
1928830d754dSRandall Stewart 		 * out_of_order, if yes then update the nr_map
1929830d754dSRandall Stewart 		 */
1930830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
1931830d754dSRandall Stewart 			/*
1932830d754dSRandall Stewart 			 * EY check if the mapping_array and nr_mapping
1933830d754dSRandall Stewart 			 * array are consistent
1934830d754dSRandall Stewart 			 */
1935830d754dSRandall Stewart 			if (asoc->mapping_array_base_tsn != asoc->nr_mapping_array_base_tsn)
1936830d754dSRandall Stewart 				/*
1937830d754dSRandall Stewart 				 * printf("EY-IN
1938830d754dSRandall Stewart 				 * sctp_process_a_data_chunk(5): Something
1939830d754dSRandall Stewart 				 * is wrong the map base tsn" "\nEY-and
1940830d754dSRandall Stewart 				 * nr_map base tsn should be equal.");
1941830d754dSRandall Stewart 				 */
1942830d754dSRandall Stewart 				/* EY debugging block */
1943830d754dSRandall Stewart 			{
1944830d754dSRandall Stewart 				/*
1945830d754dSRandall Stewart 				 * printf("\nEY-Calculating an
1946830d754dSRandall Stewart 				 * nr_gap!!\nmapping_array_size = %d
1947830d754dSRandall Stewart 				 * nr_mapping_array_size = %d"
1948830d754dSRandall Stewart 				 * "\nEY-mapping_array_base = %d
1949830d754dSRandall Stewart 				 * nr_mapping_array_base =
1950830d754dSRandall Stewart 				 * %d\nEY-highest_tsn_inside_map = %d"
1951830d754dSRandall Stewart 				 * "highest_tsn_inside_nr_map = %d\nEY-TSN =
1952830d754dSRandall Stewart 				 * %d nr_gap = %d",asoc->mapping_array_size,
1953830d754dSRandall Stewart 				 * asoc->nr_mapping_array_size,
1954830d754dSRandall Stewart 				 * asoc->mapping_array_base_tsn,
1955830d754dSRandall Stewart 				 * asoc->nr_mapping_array_base_tsn,
1956830d754dSRandall Stewart 				 * asoc->highest_tsn_inside_map,
1957830d754dSRandall Stewart 				 * asoc->highest_tsn_inside_nr_map,tsn,nr_gap
1958830d754dSRandall Stewart 				 * );
1959830d754dSRandall Stewart 				 */
1960830d754dSRandall Stewart 			}
1961830d754dSRandall Stewart 			/* EY - not %100 sure about the lock thing */
1962830d754dSRandall Stewart 			SCTP_TCB_LOCK_ASSERT(stcb);
1963830d754dSRandall Stewart 			SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
1964d50c1d79SRandall Stewart 			SCTP_REVERSE_OUT_TSN_PRES(nr_gap, tsn, asoc);
1965830d754dSRandall Stewart 			if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
1966830d754dSRandall Stewart 				asoc->highest_tsn_inside_nr_map = tsn;
1967830d754dSRandall Stewart 		}
1968f42a358aSRandall Stewart 		if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1969f8829a4aSRandall Stewart 			/* for ordered, bump what we delivered */
1970f8829a4aSRandall Stewart 			asoc->strmin[strmno].last_sequence_delivered++;
1971f8829a4aSRandall Stewart 		}
1972f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvexpress);
1973b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
19746a91f103SRandall Stewart 			sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno,
1975f8829a4aSRandall Stewart 			    SCTP_STR_LOG_FROM_EXPRS_DEL);
197680fefe0aSRandall Stewart 		}
1977f8829a4aSRandall Stewart 		control = NULL;
1978f8829a4aSRandall Stewart 		goto finish_express_del;
1979f8829a4aSRandall Stewart 	}
1980f8829a4aSRandall Stewart failed_express_del:
1981f8829a4aSRandall Stewart 	/* If we reach here this is a new chunk */
1982f8829a4aSRandall Stewart 	chk = NULL;
1983f8829a4aSRandall Stewart 	control = NULL;
1984f8829a4aSRandall Stewart 	/* Express for fragmented delivery? */
1985f8829a4aSRandall Stewart 	if ((asoc->fragmented_delivery_inprogress) &&
1986f8829a4aSRandall Stewart 	    (stcb->asoc.control_pdapi) &&
1987f8829a4aSRandall Stewart 	    (asoc->str_of_pdapi == strmno) &&
1988f8829a4aSRandall Stewart 	    (asoc->ssn_of_pdapi == strmseq)
1989f8829a4aSRandall Stewart 	    ) {
1990f8829a4aSRandall Stewart 		control = stcb->asoc.control_pdapi;
1991f42a358aSRandall Stewart 		if ((chunk_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
1992f8829a4aSRandall Stewart 			/* Can't be another first? */
1993f8829a4aSRandall Stewart 			goto failed_pdapi_express_del;
1994f8829a4aSRandall Stewart 		}
1995f8829a4aSRandall Stewart 		if (tsn == (control->sinfo_tsn + 1)) {
1996f8829a4aSRandall Stewart 			/* Yep, we can add it on */
1997f8829a4aSRandall Stewart 			int end = 0;
1998f8829a4aSRandall Stewart 			uint32_t cumack;
1999f8829a4aSRandall Stewart 
2000f42a358aSRandall Stewart 			if (chunk_flags & SCTP_DATA_LAST_FRAG) {
2001f8829a4aSRandall Stewart 				end = 1;
2002f8829a4aSRandall Stewart 			}
2003f8829a4aSRandall Stewart 			cumack = asoc->cumulative_tsn;
2004f8829a4aSRandall Stewart 			if ((cumack + 1) == tsn)
2005f8829a4aSRandall Stewart 				cumack = tsn;
2006f8829a4aSRandall Stewart 
2007f8829a4aSRandall Stewart 			if (sctp_append_to_readq(stcb->sctp_ep, stcb, control, dmbuf, end,
2008f8829a4aSRandall Stewart 			    tsn,
2009f8829a4aSRandall Stewart 			    &stcb->sctp_socket->so_rcv)) {
2010ad81507eSRandall Stewart 				SCTP_PRINTF("Append fails end:%d\n", end);
2011f8829a4aSRandall Stewart 				goto failed_pdapi_express_del;
2012f8829a4aSRandall Stewart 			}
2013830d754dSRandall Stewart 			/*
2014830d754dSRandall Stewart 			 * EY It is appended to the read queue in prev if
2015830d754dSRandall Stewart 			 * block here I should check if this delivered tsn
2016830d754dSRandall Stewart 			 * is out_of_order, if yes then update the nr_map
2017830d754dSRandall Stewart 			 */
2018830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
2019830d754dSRandall Stewart 				/* EY debugging block */
2020830d754dSRandall Stewart 				{
2021830d754dSRandall Stewart 					/*
2022830d754dSRandall Stewart 					 * printf("\nEY-Calculating an
2023830d754dSRandall Stewart 					 * nr_gap!!\nEY-mapping_array_size =
2024830d754dSRandall Stewart 					 * %d nr_mapping_array_size = %d"
2025830d754dSRandall Stewart 					 * "\nEY-mapping_array_base = %d
2026830d754dSRandall Stewart 					 * nr_mapping_array_base =
2027830d754dSRandall Stewart 					 * %d\nEY-highest_tsn_inside_map =
2028830d754dSRandall Stewart 					 * %d" "highest_tsn_inside_nr_map =
2029830d754dSRandall Stewart 					 * %d\nEY-TSN = %d nr_gap =
2030830d754dSRandall Stewart 					 * %d",asoc->mapping_array_size,
2031830d754dSRandall Stewart 					 * asoc->nr_mapping_array_size,
2032830d754dSRandall Stewart 					 * asoc->mapping_array_base_tsn,
2033830d754dSRandall Stewart 					 * asoc->nr_mapping_array_base_tsn,
2034830d754dSRandall Stewart 					 * asoc->highest_tsn_inside_map,
2035830d754dSRandall Stewart 					 * asoc->highest_tsn_inside_nr_map,ts
2036830d754dSRandall Stewart 					 * n,nr_gap);
2037830d754dSRandall Stewart 					 */
2038830d754dSRandall Stewart 				}
2039830d754dSRandall Stewart 				/* EY - not %100 sure about the lock thing */
2040830d754dSRandall Stewart 				SCTP_TCB_LOCK_ASSERT(stcb);
2041830d754dSRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
2042d50c1d79SRandall Stewart 				SCTP_REVERSE_OUT_TSN_PRES(nr_gap, tsn, asoc);
2043830d754dSRandall Stewart 				if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
2044830d754dSRandall Stewart 					asoc->highest_tsn_inside_nr_map = tsn;
2045830d754dSRandall Stewart 			}
2046f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvexpressm);
2047f8829a4aSRandall Stewart 			control->sinfo_tsn = tsn;
2048f8829a4aSRandall Stewart 			asoc->tsn_last_delivered = tsn;
2049f42a358aSRandall Stewart 			asoc->fragment_flags = chunk_flags;
2050f8829a4aSRandall Stewart 			asoc->tsn_of_pdapi_last_delivered = tsn;
2051f42a358aSRandall Stewart 			asoc->last_flags_delivered = chunk_flags;
2052f8829a4aSRandall Stewart 			asoc->last_strm_seq_delivered = strmseq;
2053f8829a4aSRandall Stewart 			asoc->last_strm_no_delivered = strmno;
2054f8829a4aSRandall Stewart 			if (end) {
2055f8829a4aSRandall Stewart 				/* clean up the flags and such */
2056f8829a4aSRandall Stewart 				asoc->fragmented_delivery_inprogress = 0;
2057f42a358aSRandall Stewart 				if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
2058f8829a4aSRandall Stewart 					asoc->strmin[strmno].last_sequence_delivered++;
2059a5d547adSRandall Stewart 				}
2060f8829a4aSRandall Stewart 				stcb->asoc.control_pdapi = NULL;
2061139bc87fSRandall Stewart 				if (TAILQ_EMPTY(&asoc->reasmqueue) == 0) {
2062139bc87fSRandall Stewart 					/*
2063139bc87fSRandall Stewart 					 * There could be another message
2064139bc87fSRandall Stewart 					 * ready
2065139bc87fSRandall Stewart 					 */
2066139bc87fSRandall Stewart 					need_reasm_check = 1;
2067139bc87fSRandall Stewart 				}
2068f8829a4aSRandall Stewart 			}
2069f8829a4aSRandall Stewart 			control = NULL;
2070f8829a4aSRandall Stewart 			goto finish_express_del;
2071f8829a4aSRandall Stewart 		}
2072f8829a4aSRandall Stewart 	}
2073f8829a4aSRandall Stewart failed_pdapi_express_del:
2074f8829a4aSRandall Stewart 	control = NULL;
2075f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
2076f8829a4aSRandall Stewart 		sctp_alloc_a_chunk(stcb, chk);
2077f8829a4aSRandall Stewart 		if (chk == NULL) {
2078f8829a4aSRandall Stewart 			/* No memory so we drop the chunk */
2079f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_nomem);
2080f8829a4aSRandall Stewart 			if (last_chunk == 0) {
2081f8829a4aSRandall Stewart 				/* we copied it, free the copy */
2082f8829a4aSRandall Stewart 				sctp_m_freem(dmbuf);
2083f8829a4aSRandall Stewart 			}
2084f8829a4aSRandall Stewart 			return (0);
2085f8829a4aSRandall Stewart 		}
2086f8829a4aSRandall Stewart 		chk->rec.data.TSN_seq = tsn;
2087f8829a4aSRandall Stewart 		chk->no_fr_allowed = 0;
2088f8829a4aSRandall Stewart 		chk->rec.data.stream_seq = strmseq;
2089f8829a4aSRandall Stewart 		chk->rec.data.stream_number = strmno;
2090f42a358aSRandall Stewart 		chk->rec.data.payloadtype = protocol_id;
2091f8829a4aSRandall Stewart 		chk->rec.data.context = stcb->asoc.context;
2092f8829a4aSRandall Stewart 		chk->rec.data.doing_fast_retransmit = 0;
2093f42a358aSRandall Stewart 		chk->rec.data.rcv_flags = chunk_flags;
2094f8829a4aSRandall Stewart 		chk->asoc = asoc;
2095f8829a4aSRandall Stewart 		chk->send_size = the_len;
2096f8829a4aSRandall Stewart 		chk->whoTo = net;
2097f8829a4aSRandall Stewart 		atomic_add_int(&net->ref_count, 1);
2098f8829a4aSRandall Stewart 		chk->data = dmbuf;
2099f8829a4aSRandall Stewart 	} else {
2100f8829a4aSRandall Stewart 		sctp_alloc_a_readq(stcb, control);
2101f8829a4aSRandall Stewart 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
2102f42a358aSRandall Stewart 		    protocol_id,
2103f8829a4aSRandall Stewart 		    stcb->asoc.context,
2104f8829a4aSRandall Stewart 		    strmno, strmseq,
2105f42a358aSRandall Stewart 		    chunk_flags,
2106f8829a4aSRandall Stewart 		    dmbuf);
2107f8829a4aSRandall Stewart 		if (control == NULL) {
2108f8829a4aSRandall Stewart 			/* No memory so we drop the chunk */
2109f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_nomem);
2110f8829a4aSRandall Stewart 			if (last_chunk == 0) {
2111f8829a4aSRandall Stewart 				/* we copied it, free the copy */
2112f8829a4aSRandall Stewart 				sctp_m_freem(dmbuf);
2113f8829a4aSRandall Stewart 			}
2114f8829a4aSRandall Stewart 			return (0);
2115f8829a4aSRandall Stewart 		}
2116f8829a4aSRandall Stewart 		control->length = the_len;
2117f8829a4aSRandall Stewart 	}
2118f8829a4aSRandall Stewart 
2119f8829a4aSRandall Stewart 	/* Mark it as received */
2120f8829a4aSRandall Stewart 	/* Now queue it where it belongs */
2121f8829a4aSRandall Stewart 	if (control != NULL) {
2122f8829a4aSRandall Stewart 		/* First a sanity check */
2123f8829a4aSRandall Stewart 		if (asoc->fragmented_delivery_inprogress) {
2124f8829a4aSRandall Stewart 			/*
2125f8829a4aSRandall Stewart 			 * Ok, we have a fragmented delivery in progress if
2126f8829a4aSRandall Stewart 			 * this chunk is next to deliver OR belongs in our
2127f8829a4aSRandall Stewart 			 * view to the reassembly, the peer is evil or
2128f8829a4aSRandall Stewart 			 * broken.
2129f8829a4aSRandall Stewart 			 */
2130f8829a4aSRandall Stewart 			uint32_t estimate_tsn;
2131f8829a4aSRandall Stewart 
2132f8829a4aSRandall Stewart 			estimate_tsn = asoc->tsn_last_delivered + 1;
2133f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->reasmqueue) &&
2134f8829a4aSRandall Stewart 			    (estimate_tsn == control->sinfo_tsn)) {
2135f8829a4aSRandall Stewart 				/* Evil/Broke peer */
2136f8829a4aSRandall Stewart 				sctp_m_freem(control->data);
2137f8829a4aSRandall Stewart 				control->data = NULL;
21385bead436SRandall Stewart 				if (control->whoFrom) {
2139f8829a4aSRandall Stewart 					sctp_free_remote_addr(control->whoFrom);
21405bead436SRandall Stewart 					control->whoFrom = NULL;
21415bead436SRandall Stewart 				}
2142f8829a4aSRandall Stewart 				sctp_free_a_readq(stcb, control);
2143139bc87fSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
2144f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
2145f8829a4aSRandall Stewart 				if (oper) {
2146f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
2147f8829a4aSRandall Stewart 					uint32_t *ippp;
2148f8829a4aSRandall Stewart 
2149139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) =
2150f8829a4aSRandall Stewart 					    sizeof(struct sctp_paramhdr) +
2151f8829a4aSRandall Stewart 					    (3 * sizeof(uint32_t));
2152f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
2153f8829a4aSRandall Stewart 					ph->param_type =
2154f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2155139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
2156f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
2157a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_15);
2158f8829a4aSRandall Stewart 					ippp++;
2159f8829a4aSRandall Stewart 					*ippp = tsn;
2160f8829a4aSRandall Stewart 					ippp++;
2161f8829a4aSRandall Stewart 					*ippp = ((strmno << 16) | strmseq);
2162f8829a4aSRandall Stewart 				}
2163a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_15;
2164f8829a4aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb,
2165ceaad40aSRandall Stewart 				    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
2166f8829a4aSRandall Stewart 
2167f8829a4aSRandall Stewart 				*abort_flag = 1;
2168f8829a4aSRandall Stewart 				return (0);
2169f8829a4aSRandall Stewart 			} else {
2170f8829a4aSRandall Stewart 				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
2171f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
2172f8829a4aSRandall Stewart 					control->data = NULL;
21735bead436SRandall Stewart 					if (control->whoFrom) {
2174f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
21755bead436SRandall Stewart 						control->whoFrom = NULL;
21765bead436SRandall Stewart 					}
2177f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
2178f8829a4aSRandall Stewart 
2179139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
2180f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
2181f8829a4aSRandall Stewart 					if (oper) {
2182f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
2183f8829a4aSRandall Stewart 						uint32_t *ippp;
2184f8829a4aSRandall Stewart 
2185139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
2186f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
2187f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
2188f8829a4aSRandall Stewart 						ph = mtod(oper,
2189f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
2190f8829a4aSRandall Stewart 						ph->param_type =
2191f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2192f8829a4aSRandall Stewart 						ph->param_length =
2193139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
2194f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
2195a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_16);
2196f8829a4aSRandall Stewart 						ippp++;
2197f8829a4aSRandall Stewart 						*ippp = tsn;
2198f8829a4aSRandall Stewart 						ippp++;
2199f8829a4aSRandall Stewart 						*ippp = ((strmno << 16) | strmseq);
2200f8829a4aSRandall Stewart 					}
2201a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_16;
2202f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
2203ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
2204f8829a4aSRandall Stewart 
2205f8829a4aSRandall Stewart 					*abort_flag = 1;
2206f8829a4aSRandall Stewart 					return (0);
2207f8829a4aSRandall Stewart 				}
2208f8829a4aSRandall Stewart 			}
2209f8829a4aSRandall Stewart 		} else {
2210f8829a4aSRandall Stewart 			/* No PDAPI running */
2211f8829a4aSRandall Stewart 			if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
2212f8829a4aSRandall Stewart 				/*
2213f8829a4aSRandall Stewart 				 * Reassembly queue is NOT empty validate
2214f8829a4aSRandall Stewart 				 * that this tsn does not need to be in
2215f8829a4aSRandall Stewart 				 * reasembly queue. If it does then our peer
2216f8829a4aSRandall Stewart 				 * is broken or evil.
2217f8829a4aSRandall Stewart 				 */
2218f8829a4aSRandall Stewart 				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
2219f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
2220f8829a4aSRandall Stewart 					control->data = NULL;
22215bead436SRandall Stewart 					if (control->whoFrom) {
2222f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
22235bead436SRandall Stewart 						control->whoFrom = NULL;
22245bead436SRandall Stewart 					}
2225f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
2226139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
2227f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
2228f8829a4aSRandall Stewart 					if (oper) {
2229f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
2230f8829a4aSRandall Stewart 						uint32_t *ippp;
2231f8829a4aSRandall Stewart 
2232139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
2233f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
2234f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
2235f8829a4aSRandall Stewart 						ph = mtod(oper,
2236f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
2237f8829a4aSRandall Stewart 						ph->param_type =
2238f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2239f8829a4aSRandall Stewart 						ph->param_length =
2240139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
2241f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
2242a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_17);
2243f8829a4aSRandall Stewart 						ippp++;
2244f8829a4aSRandall Stewart 						*ippp = tsn;
2245f8829a4aSRandall Stewart 						ippp++;
2246f8829a4aSRandall Stewart 						*ippp = ((strmno << 16) | strmseq);
2247f8829a4aSRandall Stewart 					}
2248a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_17;
2249f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
2250ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
2251f8829a4aSRandall Stewart 
2252f8829a4aSRandall Stewart 					*abort_flag = 1;
2253f8829a4aSRandall Stewart 					return (0);
2254f8829a4aSRandall Stewart 				}
2255f8829a4aSRandall Stewart 			}
2256f8829a4aSRandall Stewart 		}
2257f8829a4aSRandall Stewart 		/* ok, if we reach here we have passed the sanity checks */
2258f42a358aSRandall Stewart 		if (chunk_flags & SCTP_DATA_UNORDERED) {
2259f8829a4aSRandall Stewart 			/* queue directly into socket buffer */
2260f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep, stcb,
2261f8829a4aSRandall Stewart 			    control,
2262cfde3ff7SRandall Stewart 			    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
2263830d754dSRandall Stewart 
2264830d754dSRandall Stewart 			/*
2265830d754dSRandall Stewart 			 * EY It is added to the read queue in prev if block
2266830d754dSRandall Stewart 			 * here I should check if this delivered tsn is
2267830d754dSRandall Stewart 			 * out_of_order, if yes then update the nr_map
2268830d754dSRandall Stewart 			 */
2269830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
2270830d754dSRandall Stewart 				/*
2271830d754dSRandall Stewart 				 * EY check if the mapping_array and
2272830d754dSRandall Stewart 				 * nr_mapping array are consistent
2273830d754dSRandall Stewart 				 */
2274830d754dSRandall Stewart 				if (asoc->mapping_array_base_tsn != asoc->nr_mapping_array_base_tsn)
2275830d754dSRandall Stewart 					/*
2276830d754dSRandall Stewart 					 * printf("EY-IN
2277830d754dSRandall Stewart 					 * sctp_process_a_data_chunk(6):
2278830d754dSRandall Stewart 					 * Something is wrong the map base
2279830d754dSRandall Stewart 					 * tsn" "\nEY-and nr_map base tsn
2280830d754dSRandall Stewart 					 * should be equal.");
2281830d754dSRandall Stewart 					 */
2282830d754dSRandall Stewart 					/*
2283830d754dSRandall Stewart 					 * EY - not %100 sure about the lock
2284830d754dSRandall Stewart 					 * thing, i think we don't need the
2285830d754dSRandall Stewart 					 * below,
2286830d754dSRandall Stewart 					 */
2287830d754dSRandall Stewart 					/* SCTP_TCB_LOCK_ASSERT(stcb); */
2288830d754dSRandall Stewart 				{
2289830d754dSRandall Stewart 					/*
2290830d754dSRandall Stewart 					 * printf("\nEY-Calculating an
2291830d754dSRandall Stewart 					 * nr_gap!!\nEY-mapping_array_size =
2292830d754dSRandall Stewart 					 * %d nr_mapping_array_size = %d"
2293830d754dSRandall Stewart 					 * "\nEY-mapping_array_base = %d
2294830d754dSRandall Stewart 					 * nr_mapping_array_base =
2295830d754dSRandall Stewart 					 * %d\nEY-highest_tsn_inside_map =
2296830d754dSRandall Stewart 					 * %d" "highest_tsn_inside_nr_map =
2297830d754dSRandall Stewart 					 * %d\nEY-TSN = %d nr_gap =
2298830d754dSRandall Stewart 					 * %d",asoc->mapping_array_size,
2299830d754dSRandall Stewart 					 * asoc->nr_mapping_array_size,
2300830d754dSRandall Stewart 					 * asoc->mapping_array_base_tsn,
2301830d754dSRandall Stewart 					 * asoc->nr_mapping_array_base_tsn,
2302830d754dSRandall Stewart 					 * asoc->highest_tsn_inside_map,
2303830d754dSRandall Stewart 					 * asoc->highest_tsn_inside_nr_map,ts
2304830d754dSRandall Stewart 					 * n,nr_gap);
2305830d754dSRandall Stewart 					 */
2306830d754dSRandall Stewart 				}
2307830d754dSRandall Stewart 				SCTP_TCB_LOCK_ASSERT(stcb);
2308830d754dSRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
2309d50c1d79SRandall Stewart 				SCTP_REVERSE_OUT_TSN_PRES(nr_gap, tsn, asoc);
2310830d754dSRandall Stewart 				if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
2311830d754dSRandall Stewart 					asoc->highest_tsn_inside_nr_map = tsn;
2312830d754dSRandall Stewart 			}
2313f8829a4aSRandall Stewart 		} else {
2314f8829a4aSRandall Stewart 			/*
2315f8829a4aSRandall Stewart 			 * Special check for when streams are resetting. We
2316f8829a4aSRandall Stewart 			 * could be more smart about this and check the
2317f8829a4aSRandall Stewart 			 * actual stream to see if it is not being reset..
2318f8829a4aSRandall Stewart 			 * that way we would not create a HOLB when amongst
2319f8829a4aSRandall Stewart 			 * streams being reset and those not being reset.
2320f8829a4aSRandall Stewart 			 *
2321f8829a4aSRandall Stewart 			 * We take complete messages that have a stream reset
2322f8829a4aSRandall Stewart 			 * intervening (aka the TSN is after where our
2323f8829a4aSRandall Stewart 			 * cum-ack needs to be) off and put them on a
2324f8829a4aSRandall Stewart 			 * pending_reply_queue. The reassembly ones we do
2325f8829a4aSRandall Stewart 			 * not have to worry about since they are all sorted
2326f8829a4aSRandall Stewart 			 * and proceessed by TSN order. It is only the
2327f8829a4aSRandall Stewart 			 * singletons I must worry about.
2328f8829a4aSRandall Stewart 			 */
2329f8829a4aSRandall Stewart 			if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2330d61a0ae0SRandall Stewart 			    ((compare_with_wrap(tsn, liste->tsn, MAX_TSN)))
2331f8829a4aSRandall Stewart 			    ) {
2332f8829a4aSRandall Stewart 				/*
2333f8829a4aSRandall Stewart 				 * yep its past where we need to reset... go
2334f8829a4aSRandall Stewart 				 * ahead and queue it.
2335f8829a4aSRandall Stewart 				 */
2336f8829a4aSRandall Stewart 				if (TAILQ_EMPTY(&asoc->pending_reply_queue)) {
2337f8829a4aSRandall Stewart 					/* first one on */
2338f8829a4aSRandall Stewart 					TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2339f8829a4aSRandall Stewart 				} else {
2340f8829a4aSRandall Stewart 					struct sctp_queued_to_read *ctlOn;
2341f8829a4aSRandall Stewart 					unsigned char inserted = 0;
2342f8829a4aSRandall Stewart 
2343f8829a4aSRandall Stewart 					ctlOn = TAILQ_FIRST(&asoc->pending_reply_queue);
2344f8829a4aSRandall Stewart 					while (ctlOn) {
2345f8829a4aSRandall Stewart 						if (compare_with_wrap(control->sinfo_tsn,
2346f8829a4aSRandall Stewart 						    ctlOn->sinfo_tsn, MAX_TSN)) {
2347f8829a4aSRandall Stewart 							ctlOn = TAILQ_NEXT(ctlOn, next);
2348f8829a4aSRandall Stewart 						} else {
2349f8829a4aSRandall Stewart 							/* found it */
2350f8829a4aSRandall Stewart 							TAILQ_INSERT_BEFORE(ctlOn, control, next);
2351f8829a4aSRandall Stewart 							inserted = 1;
2352f8829a4aSRandall Stewart 							break;
2353f8829a4aSRandall Stewart 						}
2354f8829a4aSRandall Stewart 					}
2355f8829a4aSRandall Stewart 					if (inserted == 0) {
2356f8829a4aSRandall Stewart 						/*
2357f8829a4aSRandall Stewart 						 * must be put at end, use
2358f8829a4aSRandall Stewart 						 * prevP (all setup from
2359f8829a4aSRandall Stewart 						 * loop) to setup nextP.
2360f8829a4aSRandall Stewart 						 */
2361f8829a4aSRandall Stewart 						TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2362f8829a4aSRandall Stewart 					}
2363f8829a4aSRandall Stewart 				}
2364f8829a4aSRandall Stewart 			} else {
2365f8829a4aSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, control, abort_flag);
2366f8829a4aSRandall Stewart 				if (*abort_flag) {
2367f8829a4aSRandall Stewart 					return (0);
2368f8829a4aSRandall Stewart 				}
2369f8829a4aSRandall Stewart 			}
2370f8829a4aSRandall Stewart 		}
2371f8829a4aSRandall Stewart 	} else {
2372f8829a4aSRandall Stewart 		/* Into the re-assembly queue */
2373f8829a4aSRandall Stewart 		sctp_queue_data_for_reasm(stcb, asoc, chk, abort_flag);
2374f8829a4aSRandall Stewart 		if (*abort_flag) {
2375a5d547adSRandall Stewart 			/*
2376a5d547adSRandall Stewart 			 * the assoc is now gone and chk was put onto the
2377a5d547adSRandall Stewart 			 * reasm queue, which has all been freed.
2378a5d547adSRandall Stewart 			 */
2379a5d547adSRandall Stewart 			*m = NULL;
2380f8829a4aSRandall Stewart 			return (0);
2381f8829a4aSRandall Stewart 		}
2382f8829a4aSRandall Stewart 	}
2383f8829a4aSRandall Stewart finish_express_del:
2384f8829a4aSRandall Stewart 	if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
2385f8829a4aSRandall Stewart 		/* we have a new high score */
2386f8829a4aSRandall Stewart 		asoc->highest_tsn_inside_map = tsn;
2387b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2388f8829a4aSRandall Stewart 			sctp_log_map(0, 2, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
238980fefe0aSRandall Stewart 		}
2390f8829a4aSRandall Stewart 	}
2391f8829a4aSRandall Stewart 	if (tsn == (asoc->cumulative_tsn + 1)) {
2392f8829a4aSRandall Stewart 		/* Update cum-ack */
2393f8829a4aSRandall Stewart 		asoc->cumulative_tsn = tsn;
2394f8829a4aSRandall Stewart 	}
2395f8829a4aSRandall Stewart 	if (last_chunk) {
2396f8829a4aSRandall Stewart 		*m = NULL;
2397f8829a4aSRandall Stewart 	}
2398f42a358aSRandall Stewart 	if (ordered) {
2399f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_inorderchunks);
2400f8829a4aSRandall Stewart 	} else {
2401f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_inunorderchunks);
2402f8829a4aSRandall Stewart 	}
2403f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvdata);
2404f8829a4aSRandall Stewart 	/* Set it present please */
2405b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
24066a91f103SRandall Stewart 		sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno, SCTP_STR_LOG_FROM_MARK_TSN);
240780fefe0aSRandall Stewart 	}
2408b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2409f8829a4aSRandall Stewart 		sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2410f8829a4aSRandall Stewart 		    asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE);
241180fefe0aSRandall Stewart 	}
2412207304d4SRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
2413f8829a4aSRandall Stewart 	SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
24148933fa13SRandall Stewart 
24158933fa13SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) &&
24168933fa13SRandall Stewart 	    asoc->peer_supports_nr_sack &&
24178933fa13SRandall Stewart 	    (SCTP_BASE_SYSCTL(sctp_do_drain) == 0)) {
24188933fa13SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
2419d50c1d79SRandall Stewart 		SCTP_REVERSE_OUT_TSN_PRES(nr_gap, tsn, asoc);
24208933fa13SRandall Stewart 		if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN)) {
24218933fa13SRandall Stewart 			asoc->highest_tsn_inside_nr_map = tsn;
24228933fa13SRandall Stewart 		}
24238933fa13SRandall Stewart 	}
242417205eccSRandall Stewart 	/* check the special flag for stream resets */
242517205eccSRandall Stewart 	if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2426d61a0ae0SRandall Stewart 	    ((compare_with_wrap(asoc->cumulative_tsn, liste->tsn, MAX_TSN)) ||
2427d61a0ae0SRandall Stewart 	    (asoc->cumulative_tsn == liste->tsn))
242817205eccSRandall Stewart 	    ) {
242917205eccSRandall Stewart 		/*
243017205eccSRandall Stewart 		 * we have finished working through the backlogged TSN's now
243117205eccSRandall Stewart 		 * time to reset streams. 1: call reset function. 2: free
243217205eccSRandall Stewart 		 * pending_reply space 3: distribute any chunks in
243317205eccSRandall Stewart 		 * pending_reply_queue.
243417205eccSRandall Stewart 		 */
243517205eccSRandall Stewart 		struct sctp_queued_to_read *ctl;
243617205eccSRandall Stewart 
243717205eccSRandall Stewart 		sctp_reset_in_stream(stcb, liste->number_entries, liste->req.list_of_streams);
243817205eccSRandall Stewart 		TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
2439207304d4SRandall Stewart 		SCTP_FREE(liste, SCTP_M_STRESET);
24403c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
244117205eccSRandall Stewart 		liste = TAILQ_FIRST(&asoc->resetHead);
244217205eccSRandall Stewart 		ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
244317205eccSRandall Stewart 		if (ctl && (liste == NULL)) {
244417205eccSRandall Stewart 			/* All can be removed */
244517205eccSRandall Stewart 			while (ctl) {
244617205eccSRandall Stewart 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
244717205eccSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
244817205eccSRandall Stewart 				if (*abort_flag) {
244917205eccSRandall Stewart 					return (0);
245017205eccSRandall Stewart 				}
245117205eccSRandall Stewart 				ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
245217205eccSRandall Stewart 			}
245317205eccSRandall Stewart 		} else if (ctl) {
245417205eccSRandall Stewart 			/* more than one in queue */
2455d61a0ae0SRandall Stewart 			while (!compare_with_wrap(ctl->sinfo_tsn, liste->tsn, MAX_TSN)) {
245617205eccSRandall Stewart 				/*
245717205eccSRandall Stewart 				 * if ctl->sinfo_tsn is <= liste->tsn we can
245817205eccSRandall Stewart 				 * process it which is the NOT of
245917205eccSRandall Stewart 				 * ctl->sinfo_tsn > liste->tsn
246017205eccSRandall Stewart 				 */
246117205eccSRandall Stewart 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
246217205eccSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
246317205eccSRandall Stewart 				if (*abort_flag) {
246417205eccSRandall Stewart 					return (0);
246517205eccSRandall Stewart 				}
246617205eccSRandall Stewart 				ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
246717205eccSRandall Stewart 			}
246817205eccSRandall Stewart 		}
246917205eccSRandall Stewart 		/*
247017205eccSRandall Stewart 		 * Now service re-assembly to pick up anything that has been
247117205eccSRandall Stewart 		 * held on reassembly queue?
247217205eccSRandall Stewart 		 */
247317205eccSRandall Stewart 		sctp_deliver_reasm_check(stcb, asoc);
247417205eccSRandall Stewart 		need_reasm_check = 0;
247517205eccSRandall Stewart 	}
2476139bc87fSRandall Stewart 	if (need_reasm_check) {
2477139bc87fSRandall Stewart 		/* Another one waits ? */
2478139bc87fSRandall Stewart 		sctp_deliver_reasm_check(stcb, asoc);
2479139bc87fSRandall Stewart 	}
2480f8829a4aSRandall Stewart 	return (1);
2481f8829a4aSRandall Stewart }
2482f8829a4aSRandall Stewart 
2483f8829a4aSRandall Stewart int8_t sctp_map_lookup_tab[256] = {
2484f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2485f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2486f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2487f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 4,
2488f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2489f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2490f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2491f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 5,
2492f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2493f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2494f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2495f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 4,
2496f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2497f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2498f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2499f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 6,
2500f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2501f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2502f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2503f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 4,
2504f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2505f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2506f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2507f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 5,
2508f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2509f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2510f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2511f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 4,
2512f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2513f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2514f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2515f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 7,
2516f8829a4aSRandall Stewart };
2517f8829a4aSRandall Stewart 
2518f8829a4aSRandall Stewart 
2519f8829a4aSRandall Stewart void
2520f8829a4aSRandall Stewart sctp_sack_check(struct sctp_tcb *stcb, int ok_to_sack, int was_a_gap, int *abort_flag)
2521f8829a4aSRandall Stewart {
2522f8829a4aSRandall Stewart 	/*
2523f8829a4aSRandall Stewart 	 * Now we also need to check the mapping array in a couple of ways.
2524f8829a4aSRandall Stewart 	 * 1) Did we move the cum-ack point?
2525f8829a4aSRandall Stewart 	 */
2526f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2527b3f1ea41SRandall Stewart 	int at;
25280fa753b3SRandall Stewart 	uint8_t comb_byte;
2529c4739e2fSRandall Stewart 	int last_all_ones = 0;
2530f8829a4aSRandall Stewart 	int slide_from, slide_end, lgap, distance;
2531830d754dSRandall Stewart 
2532830d754dSRandall Stewart 	/* EY nr_mapping array variables */
25338933fa13SRandall Stewart 	/* int nr_at; */
25348933fa13SRandall Stewart 	/* int nr_last_all_ones = 0; */
25358933fa13SRandall Stewart 	/* int nr_slide_from, nr_slide_end, nr_lgap, nr_distance; */
2536830d754dSRandall Stewart 
2537f8829a4aSRandall Stewart 	uint32_t old_cumack, old_base, old_highest;
2538f8829a4aSRandall Stewart 	unsigned char aux_array[64];
2539f8829a4aSRandall Stewart 
2540830d754dSRandall Stewart 	/*
2541830d754dSRandall Stewart 	 * EY! Don't think this is required but I am immitating the code for
2542830d754dSRandall Stewart 	 * map just to make sure
2543830d754dSRandall Stewart 	 */
2544830d754dSRandall Stewart 	unsigned char nr_aux_array[64];
2545f8829a4aSRandall Stewart 
2546f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2547f8829a4aSRandall Stewart 	at = 0;
2548f8829a4aSRandall Stewart 
2549f8829a4aSRandall Stewart 	old_cumack = asoc->cumulative_tsn;
2550f8829a4aSRandall Stewart 	old_base = asoc->mapping_array_base_tsn;
2551f8829a4aSRandall Stewart 	old_highest = asoc->highest_tsn_inside_map;
2552f8829a4aSRandall Stewart 	if (asoc->mapping_array_size < 64)
2553f8829a4aSRandall Stewart 		memcpy(aux_array, asoc->mapping_array,
2554f8829a4aSRandall Stewart 		    asoc->mapping_array_size);
2555f8829a4aSRandall Stewart 	else
2556f8829a4aSRandall Stewart 		memcpy(aux_array, asoc->mapping_array, 64);
2557830d754dSRandall Stewart 	/* EY do the same for nr_mapping_array */
2558830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
2559830d754dSRandall Stewart 		if (asoc->nr_mapping_array_size != asoc->mapping_array_size) {
2560830d754dSRandall Stewart 			/*
2561830d754dSRandall Stewart 			 * printf("\nEY-IN sack_check method: \nEY-" "The
2562830d754dSRandall Stewart 			 * size of map and nr_map are inconsitent")
2563830d754dSRandall Stewart 			 */ ;
2564830d754dSRandall Stewart 		}
2565830d754dSRandall Stewart 		if (asoc->nr_mapping_array_base_tsn != asoc->mapping_array_base_tsn) {
2566830d754dSRandall Stewart 			/*
2567830d754dSRandall Stewart 			 * printf("\nEY-IN sack_check method VERY CRUCIAL
2568830d754dSRandall Stewart 			 * error: \nEY-" "The base tsns of map and nr_map
2569830d754dSRandall Stewart 			 * are inconsitent")
2570830d754dSRandall Stewart 			 */ ;
2571830d754dSRandall Stewart 		}
2572830d754dSRandall Stewart 		/* EY! just immitating the above code */
2573830d754dSRandall Stewart 		if (asoc->nr_mapping_array_size < 64)
2574830d754dSRandall Stewart 			memcpy(nr_aux_array, asoc->nr_mapping_array,
2575830d754dSRandall Stewart 			    asoc->nr_mapping_array_size);
2576830d754dSRandall Stewart 		else
2577830d754dSRandall Stewart 			memcpy(aux_array, asoc->nr_mapping_array, 64);
2578830d754dSRandall Stewart 	}
2579f8829a4aSRandall Stewart 	/*
2580f8829a4aSRandall Stewart 	 * We could probably improve this a small bit by calculating the
2581f8829a4aSRandall Stewart 	 * offset of the current cum-ack as the starting point.
2582f8829a4aSRandall Stewart 	 */
2583f8829a4aSRandall Stewart 	at = 0;
2584b3f1ea41SRandall Stewart 	for (slide_from = 0; slide_from < stcb->asoc.mapping_array_size; slide_from++) {
25850fa753b3SRandall Stewart 		/*
25860fa753b3SRandall Stewart 		 * We must combine the renegable and non-renegable arrays
25870fa753b3SRandall Stewart 		 * here to form a unified view of what is acked right now
25880fa753b3SRandall Stewart 		 * (since they are kept separate
25890fa753b3SRandall Stewart 		 */
25900fa753b3SRandall Stewart 		comb_byte = asoc->mapping_array[slide_from] | asoc->nr_mapping_array[slide_from];
25910fa753b3SRandall Stewart 		if (comb_byte == 0xff) {
2592f8829a4aSRandall Stewart 			at += 8;
259393164cf9SRandall Stewart 			last_all_ones = 1;
2594f8829a4aSRandall Stewart 		} else {
2595f8829a4aSRandall Stewart 			/* there is a 0 bit */
25960fa753b3SRandall Stewart 			at += sctp_map_lookup_tab[comb_byte];
259793164cf9SRandall Stewart 			last_all_ones = 0;
2598f8829a4aSRandall Stewart 			break;
2599f8829a4aSRandall Stewart 		}
2600f8829a4aSRandall Stewart 	}
260193164cf9SRandall Stewart 	asoc->cumulative_tsn = asoc->mapping_array_base_tsn + (at - last_all_ones);
2602f8829a4aSRandall Stewart 	/* at is one off, since in the table a embedded -1 is present */
2603f8829a4aSRandall Stewart 	at++;
2604f8829a4aSRandall Stewart 
2605f8829a4aSRandall Stewart 	if (compare_with_wrap(asoc->cumulative_tsn,
2606f8829a4aSRandall Stewart 	    asoc->highest_tsn_inside_map,
2607f8829a4aSRandall Stewart 	    MAX_TSN)) {
2608a5d547adSRandall Stewart #ifdef INVARIANTS
2609ceaad40aSRandall Stewart 		panic("huh, cumack 0x%x greater than high-tsn 0x%x in map",
2610ceaad40aSRandall Stewart 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2611f8829a4aSRandall Stewart #else
2612ceaad40aSRandall Stewart 		SCTP_PRINTF("huh, cumack 0x%x greater than high-tsn 0x%x in map - should panic?\n",
2613ceaad40aSRandall Stewart 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2614b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2615b3f1ea41SRandall Stewart 			sctp_log_map(0, 6, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
2616b3f1ea41SRandall Stewart 		}
2617f8829a4aSRandall Stewart 		asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2618830d754dSRandall Stewart 		asoc->highest_tsn_inside_nr_map = asoc->cumulative_tsn;
2619f8829a4aSRandall Stewart #endif
2620f8829a4aSRandall Stewart 	}
2621c4739e2fSRandall Stewart 	if ((asoc->cumulative_tsn == asoc->highest_tsn_inside_map) && (at >= 8)) {
2622f8829a4aSRandall Stewart 		/* The complete array was completed by a single FR */
2623f8829a4aSRandall Stewart 		/* higest becomes the cum-ack */
2624f8829a4aSRandall Stewart 		int clr;
2625f8829a4aSRandall Stewart 
2626f8829a4aSRandall Stewart 		asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
2627f8829a4aSRandall Stewart 		/* clear the array */
2628f8829a4aSRandall Stewart 		clr = (at >> 3) + 1;
2629c4739e2fSRandall Stewart 		if (clr > asoc->mapping_array_size) {
2630f8829a4aSRandall Stewart 			clr = asoc->mapping_array_size;
2631f8829a4aSRandall Stewart 		}
2632f8829a4aSRandall Stewart 		memset(asoc->mapping_array, 0, clr);
2633f8829a4aSRandall Stewart 		/* base becomes one ahead of the cum-ack */
2634f8829a4aSRandall Stewart 		asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
2635830d754dSRandall Stewart 
2636830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
2637830d754dSRandall Stewart 
2638830d754dSRandall Stewart 			if (clr > asoc->nr_mapping_array_size)
2639830d754dSRandall Stewart 				clr = asoc->nr_mapping_array_size;
2640830d754dSRandall Stewart 
2641830d754dSRandall Stewart 			memset(asoc->nr_mapping_array, 0, clr);
2642830d754dSRandall Stewart 			/* base becomes one ahead of the cum-ack */
2643830d754dSRandall Stewart 			asoc->nr_mapping_array_base_tsn = asoc->cumulative_tsn + 1;
2644830d754dSRandall Stewart 			asoc->highest_tsn_inside_nr_map = asoc->cumulative_tsn;
2645830d754dSRandall Stewart 		}
2646b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2647f8829a4aSRandall Stewart 			sctp_log_map(old_base, old_cumack, old_highest,
2648f8829a4aSRandall Stewart 			    SCTP_MAP_PREPARE_SLIDE);
2649f8829a4aSRandall Stewart 			sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2650f8829a4aSRandall Stewart 			    asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_CLEARED);
265180fefe0aSRandall Stewart 		}
2652f8829a4aSRandall Stewart 	} else if (at >= 8) {
2653f8829a4aSRandall Stewart 		/* we can slide the mapping array down */
2654b3f1ea41SRandall Stewart 		/* slide_from holds where we hit the first NON 0xff byte */
2655b3f1ea41SRandall Stewart 
2656f8829a4aSRandall Stewart 		/*
2657f8829a4aSRandall Stewart 		 * now calculate the ceiling of the move using our highest
2658f8829a4aSRandall Stewart 		 * TSN value
2659f8829a4aSRandall Stewart 		 */
2660f8829a4aSRandall Stewart 		if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
2661f8829a4aSRandall Stewart 			lgap = asoc->highest_tsn_inside_map -
2662f8829a4aSRandall Stewart 			    asoc->mapping_array_base_tsn;
2663f8829a4aSRandall Stewart 		} else {
2664f8829a4aSRandall Stewart 			lgap = (MAX_TSN - asoc->mapping_array_base_tsn) +
2665f8829a4aSRandall Stewart 			    asoc->highest_tsn_inside_map + 1;
2666f8829a4aSRandall Stewart 		}
2667f8829a4aSRandall Stewart 		slide_end = lgap >> 3;
2668f8829a4aSRandall Stewart 		if (slide_end < slide_from) {
2669d55b0b1bSRandall Stewart #ifdef INVARIANTS
2670f8829a4aSRandall Stewart 			panic("impossible slide");
2671d55b0b1bSRandall Stewart #else
2672d55b0b1bSRandall Stewart 			printf("impossible slide?\n");
2673d55b0b1bSRandall Stewart 			return;
2674d55b0b1bSRandall Stewart #endif
2675f8829a4aSRandall Stewart 		}
2676b3f1ea41SRandall Stewart 		if (slide_end > asoc->mapping_array_size) {
2677b3f1ea41SRandall Stewart #ifdef INVARIANTS
2678b3f1ea41SRandall Stewart 			panic("would overrun buffer");
2679b3f1ea41SRandall Stewart #else
2680b3f1ea41SRandall Stewart 			printf("Gak, would have overrun map end:%d slide_end:%d\n",
2681b3f1ea41SRandall Stewart 			    asoc->mapping_array_size, slide_end);
2682b3f1ea41SRandall Stewart 			slide_end = asoc->mapping_array_size;
2683b3f1ea41SRandall Stewart #endif
2684b3f1ea41SRandall Stewart 		}
2685f8829a4aSRandall Stewart 		distance = (slide_end - slide_from) + 1;
2686b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2687f8829a4aSRandall Stewart 			sctp_log_map(old_base, old_cumack, old_highest,
2688f8829a4aSRandall Stewart 			    SCTP_MAP_PREPARE_SLIDE);
2689f8829a4aSRandall Stewart 			sctp_log_map((uint32_t) slide_from, (uint32_t) slide_end,
2690f8829a4aSRandall Stewart 			    (uint32_t) lgap, SCTP_MAP_SLIDE_FROM);
269180fefe0aSRandall Stewart 		}
2692f8829a4aSRandall Stewart 		if (distance + slide_from > asoc->mapping_array_size ||
2693f8829a4aSRandall Stewart 		    distance < 0) {
2694f8829a4aSRandall Stewart 			/*
2695f8829a4aSRandall Stewart 			 * Here we do NOT slide forward the array so that
2696f8829a4aSRandall Stewart 			 * hopefully when more data comes in to fill it up
2697f8829a4aSRandall Stewart 			 * we will be able to slide it forward. Really I
2698f8829a4aSRandall Stewart 			 * don't think this should happen :-0
2699f8829a4aSRandall Stewart 			 */
2700f8829a4aSRandall Stewart 
2701b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2702f8829a4aSRandall Stewart 				sctp_log_map((uint32_t) distance, (uint32_t) slide_from,
2703f8829a4aSRandall Stewart 				    (uint32_t) asoc->mapping_array_size,
2704f8829a4aSRandall Stewart 				    SCTP_MAP_SLIDE_NONE);
270580fefe0aSRandall Stewart 			}
2706f8829a4aSRandall Stewart 		} else {
2707f8829a4aSRandall Stewart 			int ii;
2708f8829a4aSRandall Stewart 
2709f8829a4aSRandall Stewart 			for (ii = 0; ii < distance; ii++) {
2710f8829a4aSRandall Stewart 				asoc->mapping_array[ii] =
2711f8829a4aSRandall Stewart 				    asoc->mapping_array[slide_from + ii];
2712f8829a4aSRandall Stewart 			}
2713f8829a4aSRandall Stewart 			for (ii = distance; ii <= slide_end; ii++) {
2714f8829a4aSRandall Stewart 				asoc->mapping_array[ii] = 0;
2715f8829a4aSRandall Stewart 			}
2716f8829a4aSRandall Stewart 			asoc->mapping_array_base_tsn += (slide_from << 3);
2717b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2718f8829a4aSRandall Stewart 				sctp_log_map(asoc->mapping_array_base_tsn,
2719f8829a4aSRandall Stewart 				    asoc->cumulative_tsn, asoc->highest_tsn_inside_map,
2720f8829a4aSRandall Stewart 				    SCTP_MAP_SLIDE_RESULT);
272180fefe0aSRandall Stewart 			}
2722f8829a4aSRandall Stewart 			/*
27238933fa13SRandall Stewart 			 * EY if doing nr_sacks then slide the
27248933fa13SRandall Stewart 			 * nr_mapping_array accordingly please
2725830d754dSRandall Stewart 			 */
2726830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
27278933fa13SRandall Stewart 				for (ii = 0; ii < distance; ii++) {
2728830d754dSRandall Stewart 					asoc->nr_mapping_array[ii] =
27298933fa13SRandall Stewart 					    asoc->nr_mapping_array[slide_from + ii];
2730830d754dSRandall Stewart 				}
27318933fa13SRandall Stewart 				for (ii = distance; ii <= slide_end; ii++) {
2732830d754dSRandall Stewart 					asoc->nr_mapping_array[ii] = 0;
2733830d754dSRandall Stewart 				}
27348933fa13SRandall Stewart 				asoc->nr_mapping_array_base_tsn += (slide_from << 3);
2735830d754dSRandall Stewart 			}
2736830d754dSRandall Stewart 		}
2737830d754dSRandall Stewart 	}
2738830d754dSRandall Stewart 	/*
2739f8829a4aSRandall Stewart 	 * Now we need to see if we need to queue a sack or just start the
2740f8829a4aSRandall Stewart 	 * timer (if allowed).
2741f8829a4aSRandall Stewart 	 */
2742f8829a4aSRandall Stewart 	if (ok_to_sack) {
2743f8829a4aSRandall Stewart 		if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2744f8829a4aSRandall Stewart 			/*
2745f8829a4aSRandall Stewart 			 * Ok special case, in SHUTDOWN-SENT case. here we
2746f8829a4aSRandall Stewart 			 * maker sure SACK timer is off and instead send a
2747f8829a4aSRandall Stewart 			 * SHUTDOWN and a SACK
2748f8829a4aSRandall Stewart 			 */
2749139bc87fSRandall Stewart 			if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2750f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
2751a5d547adSRandall Stewart 				    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_INDATA + SCTP_LOC_18);
2752f8829a4aSRandall Stewart 			}
2753f8829a4aSRandall Stewart 			sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
2754830d754dSRandall Stewart 			/*
2755830d754dSRandall Stewart 			 * EY if nr_sacks used then send an nr-sack , a sack
2756830d754dSRandall Stewart 			 * otherwise
2757830d754dSRandall Stewart 			 */
27588933fa13SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack)
2759830d754dSRandall Stewart 				sctp_send_nr_sack(stcb);
2760830d754dSRandall Stewart 			else
2761f8829a4aSRandall Stewart 				sctp_send_sack(stcb);
2762f8829a4aSRandall Stewart 		} else {
2763f8829a4aSRandall Stewart 			int is_a_gap;
2764f8829a4aSRandall Stewart 
2765f8829a4aSRandall Stewart 			/* is there a gap now ? */
2766f8829a4aSRandall Stewart 			is_a_gap = compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
2767f8829a4aSRandall Stewart 			    stcb->asoc.cumulative_tsn, MAX_TSN);
2768f8829a4aSRandall Stewart 
2769f8829a4aSRandall Stewart 			/*
2770f8829a4aSRandall Stewart 			 * CMT DAC algorithm: increase number of packets
2771f8829a4aSRandall Stewart 			 * received since last ack
2772f8829a4aSRandall Stewart 			 */
2773f8829a4aSRandall Stewart 			stcb->asoc.cmt_dac_pkts_rcvd++;
2774f8829a4aSRandall Stewart 
277542551e99SRandall Stewart 			if ((stcb->asoc.send_sack == 1) ||	/* We need to send a
277642551e99SRandall Stewart 								 * SACK */
2777f8829a4aSRandall Stewart 			    ((was_a_gap) && (is_a_gap == 0)) ||	/* was a gap, but no
2778f8829a4aSRandall Stewart 								 * longer is one */
2779f8829a4aSRandall Stewart 			    (stcb->asoc.numduptsns) ||	/* we have dup's */
2780f8829a4aSRandall Stewart 			    (is_a_gap) ||	/* is still a gap */
278142551e99SRandall Stewart 			    (stcb->asoc.delayed_ack == 0) ||	/* Delayed sack disabled */
278242551e99SRandall Stewart 			    (stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq)	/* hit limit of pkts */
2783f8829a4aSRandall Stewart 			    ) {
2784f8829a4aSRandall Stewart 
2785b3f1ea41SRandall Stewart 				if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off)) &&
2786b3f1ea41SRandall Stewart 				    (SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) &&
278742551e99SRandall Stewart 				    (stcb->asoc.send_sack == 0) &&
2788f8829a4aSRandall Stewart 				    (stcb->asoc.numduptsns == 0) &&
2789f8829a4aSRandall Stewart 				    (stcb->asoc.delayed_ack) &&
2790139bc87fSRandall Stewart 				    (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer))) {
2791f8829a4aSRandall Stewart 
2792f8829a4aSRandall Stewart 					/*
2793f8829a4aSRandall Stewart 					 * CMT DAC algorithm: With CMT,
2794f8829a4aSRandall Stewart 					 * delay acks even in the face of
2795f8829a4aSRandall Stewart 					 *
2796f8829a4aSRandall Stewart 					 * reordering. Therefore, if acks that
2797f8829a4aSRandall Stewart 					 * do not have to be sent because of
2798f8829a4aSRandall Stewart 					 * the above reasons, will be
2799f8829a4aSRandall Stewart 					 * delayed. That is, acks that would
2800f8829a4aSRandall Stewart 					 * have been sent due to gap reports
2801f8829a4aSRandall Stewart 					 * will be delayed with DAC. Start
2802f8829a4aSRandall Stewart 					 * the delayed ack timer.
2803f8829a4aSRandall Stewart 					 */
2804f8829a4aSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2805f8829a4aSRandall Stewart 					    stcb->sctp_ep, stcb, NULL);
2806f8829a4aSRandall Stewart 				} else {
2807f8829a4aSRandall Stewart 					/*
2808f8829a4aSRandall Stewart 					 * Ok we must build a SACK since the
2809f8829a4aSRandall Stewart 					 * timer is pending, we got our
2810f8829a4aSRandall Stewart 					 * first packet OR there are gaps or
2811f8829a4aSRandall Stewart 					 * duplicates.
2812f8829a4aSRandall Stewart 					 */
2813ad81507eSRandall Stewart 					(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2814830d754dSRandall Stewart 					/*
2815830d754dSRandall Stewart 					 * EY if nr_sacks used then send an
2816830d754dSRandall Stewart 					 * nr-sack , a sack otherwise
2817830d754dSRandall Stewart 					 */
2818830d754dSRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack)
2819830d754dSRandall Stewart 						sctp_send_nr_sack(stcb);
2820830d754dSRandall Stewart 					else
2821f8829a4aSRandall Stewart 						sctp_send_sack(stcb);
2822f8829a4aSRandall Stewart 				}
2823f8829a4aSRandall Stewart 			} else {
282442551e99SRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2825f8829a4aSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2826f8829a4aSRandall Stewart 					    stcb->sctp_ep, stcb, NULL);
2827f8829a4aSRandall Stewart 				}
2828f8829a4aSRandall Stewart 			}
2829f8829a4aSRandall Stewart 		}
2830f8829a4aSRandall Stewart 	}
283142551e99SRandall Stewart }
2832f8829a4aSRandall Stewart 
2833f8829a4aSRandall Stewart void
2834f8829a4aSRandall Stewart sctp_service_queues(struct sctp_tcb *stcb, struct sctp_association *asoc)
2835f8829a4aSRandall Stewart {
2836f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
2837810ec536SMichael Tuexen 	uint32_t tsize, pd_point;
2838f8829a4aSRandall Stewart 	uint16_t nxt_todel;
2839f8829a4aSRandall Stewart 
2840f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
2841f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
2842f8829a4aSRandall Stewart 	}
2843f8829a4aSRandall Stewart 	/* Can we proceed further, i.e. the PD-API is complete */
2844f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
2845f8829a4aSRandall Stewart 		/* no */
2846f8829a4aSRandall Stewart 		return;
2847f8829a4aSRandall Stewart 	}
2848f8829a4aSRandall Stewart 	/*
2849f8829a4aSRandall Stewart 	 * Now is there some other chunk I can deliver from the reassembly
2850f8829a4aSRandall Stewart 	 * queue.
2851f8829a4aSRandall Stewart 	 */
2852139bc87fSRandall Stewart doit_again:
2853f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
2854f8829a4aSRandall Stewart 	if (chk == NULL) {
2855f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = 0;
2856f8829a4aSRandall Stewart 		asoc->cnt_on_reasm_queue = 0;
2857f8829a4aSRandall Stewart 		return;
2858f8829a4aSRandall Stewart 	}
2859f8829a4aSRandall Stewart 	nxt_todel = asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
2860f8829a4aSRandall Stewart 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
2861f8829a4aSRandall Stewart 	    ((nxt_todel == chk->rec.data.stream_seq) ||
2862f8829a4aSRandall Stewart 	    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
2863f8829a4aSRandall Stewart 		/*
2864f8829a4aSRandall Stewart 		 * Yep the first one is here. We setup to start reception,
2865f8829a4aSRandall Stewart 		 * by backing down the TSN just in case we can't deliver.
2866f8829a4aSRandall Stewart 		 */
2867f8829a4aSRandall Stewart 
2868f8829a4aSRandall Stewart 		/*
2869f8829a4aSRandall Stewart 		 * Before we start though either all of the message should
2870f8829a4aSRandall Stewart 		 * be here or 1/4 the socket buffer max or nothing on the
2871f8829a4aSRandall Stewart 		 * delivery queue and something can be delivered.
2872f8829a4aSRandall Stewart 		 */
2873810ec536SMichael Tuexen 		if (stcb->sctp_socket) {
2874810ec536SMichael Tuexen 			pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket) >> SCTP_PARTIAL_DELIVERY_SHIFT,
2875810ec536SMichael Tuexen 			    stcb->sctp_ep->partial_delivery_point);
2876810ec536SMichael Tuexen 		} else {
2877810ec536SMichael Tuexen 			pd_point = stcb->sctp_ep->partial_delivery_point;
2878810ec536SMichael Tuexen 		}
2879810ec536SMichael Tuexen 		if (sctp_is_all_msg_on_reasm(asoc, &tsize) || (tsize >= pd_point)) {
2880f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 1;
2881f8829a4aSRandall Stewart 			asoc->tsn_last_delivered = chk->rec.data.TSN_seq - 1;
2882f8829a4aSRandall Stewart 			asoc->str_of_pdapi = chk->rec.data.stream_number;
2883f8829a4aSRandall Stewart 			asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
2884f8829a4aSRandall Stewart 			asoc->pdapi_ppid = chk->rec.data.payloadtype;
2885f8829a4aSRandall Stewart 			asoc->fragment_flags = chk->rec.data.rcv_flags;
2886f8829a4aSRandall Stewart 			sctp_service_reassembly(stcb, asoc);
2887139bc87fSRandall Stewart 			if (asoc->fragmented_delivery_inprogress == 0) {
2888139bc87fSRandall Stewart 				goto doit_again;
2889139bc87fSRandall Stewart 			}
2890f8829a4aSRandall Stewart 		}
2891f8829a4aSRandall Stewart 	}
2892f8829a4aSRandall Stewart }
2893f8829a4aSRandall Stewart 
2894f8829a4aSRandall Stewart int
2895f8829a4aSRandall Stewart sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length,
2896f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2897f8829a4aSRandall Stewart     struct sctp_nets *net, uint32_t * high_tsn)
2898f8829a4aSRandall Stewart {
2899f8829a4aSRandall Stewart 	struct sctp_data_chunk *ch, chunk_buf;
2900f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2901f8829a4aSRandall Stewart 	int num_chunks = 0;	/* number of control chunks processed */
2902f8829a4aSRandall Stewart 	int stop_proc = 0;
2903f8829a4aSRandall Stewart 	int chk_length, break_flag, last_chunk;
2904f8829a4aSRandall Stewart 	int abort_flag = 0, was_a_gap = 0;
2905f8829a4aSRandall Stewart 	struct mbuf *m;
2906f8829a4aSRandall Stewart 
2907f8829a4aSRandall Stewart 	/* set the rwnd */
2908f8829a4aSRandall Stewart 	sctp_set_rwnd(stcb, &stcb->asoc);
2909f8829a4aSRandall Stewart 
2910f8829a4aSRandall Stewart 	m = *mm;
2911f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
2912f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2913f8829a4aSRandall Stewart 	if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
2914f8829a4aSRandall Stewart 	    stcb->asoc.cumulative_tsn, MAX_TSN)) {
2915f8829a4aSRandall Stewart 		/* there was a gap before this data was processed */
2916f8829a4aSRandall Stewart 		was_a_gap = 1;
2917f8829a4aSRandall Stewart 	}
2918f8829a4aSRandall Stewart 	/*
2919f8829a4aSRandall Stewart 	 * setup where we got the last DATA packet from for any SACK that
2920f8829a4aSRandall Stewart 	 * may need to go out. Don't bump the net. This is done ONLY when a
2921f8829a4aSRandall Stewart 	 * chunk is assigned.
2922f8829a4aSRandall Stewart 	 */
2923f8829a4aSRandall Stewart 	asoc->last_data_chunk_from = net;
2924f8829a4aSRandall Stewart 
2925d06c82f1SRandall Stewart 	/*-
2926f8829a4aSRandall Stewart 	 * Now before we proceed we must figure out if this is a wasted
2927f8829a4aSRandall Stewart 	 * cluster... i.e. it is a small packet sent in and yet the driver
2928f8829a4aSRandall Stewart 	 * underneath allocated a full cluster for it. If so we must copy it
2929f8829a4aSRandall Stewart 	 * to a smaller mbuf and free up the cluster mbuf. This will help
2930d06c82f1SRandall Stewart 	 * with cluster starvation. Note for __Panda__ we don't do this
2931d06c82f1SRandall Stewart 	 * since it has clusters all the way down to 64 bytes.
2932f8829a4aSRandall Stewart 	 */
293344b7479bSRandall Stewart 	if (SCTP_BUF_LEN(m) < (long)MLEN && SCTP_BUF_NEXT(m) == NULL) {
2934f8829a4aSRandall Stewart 		/* we only handle mbufs that are singletons.. not chains */
2935139bc87fSRandall Stewart 		m = sctp_get_mbuf_for_msg(SCTP_BUF_LEN(m), 0, M_DONTWAIT, 1, MT_DATA);
2936f8829a4aSRandall Stewart 		if (m) {
2937f8829a4aSRandall Stewart 			/* ok lets see if we can copy the data up */
2938f8829a4aSRandall Stewart 			caddr_t *from, *to;
2939f8829a4aSRandall Stewart 
2940f8829a4aSRandall Stewart 			/* get the pointers and copy */
2941f8829a4aSRandall Stewart 			to = mtod(m, caddr_t *);
2942f8829a4aSRandall Stewart 			from = mtod((*mm), caddr_t *);
2943139bc87fSRandall Stewart 			memcpy(to, from, SCTP_BUF_LEN((*mm)));
2944f8829a4aSRandall Stewart 			/* copy the length and free up the old */
2945139bc87fSRandall Stewart 			SCTP_BUF_LEN(m) = SCTP_BUF_LEN((*mm));
2946f8829a4aSRandall Stewart 			sctp_m_freem(*mm);
2947f8829a4aSRandall Stewart 			/* sucess, back copy */
2948f8829a4aSRandall Stewart 			*mm = m;
2949f8829a4aSRandall Stewart 		} else {
2950f8829a4aSRandall Stewart 			/* We are in trouble in the mbuf world .. yikes */
2951f8829a4aSRandall Stewart 			m = *mm;
2952f8829a4aSRandall Stewart 		}
2953f8829a4aSRandall Stewart 	}
2954f8829a4aSRandall Stewart 	/* get pointer to the first chunk header */
2955f8829a4aSRandall Stewart 	ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2956f8829a4aSRandall Stewart 	    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2957f8829a4aSRandall Stewart 	if (ch == NULL) {
2958f8829a4aSRandall Stewart 		return (1);
2959f8829a4aSRandall Stewart 	}
2960f8829a4aSRandall Stewart 	/*
2961f8829a4aSRandall Stewart 	 * process all DATA chunks...
2962f8829a4aSRandall Stewart 	 */
2963f8829a4aSRandall Stewart 	*high_tsn = asoc->cumulative_tsn;
2964f8829a4aSRandall Stewart 	break_flag = 0;
296542551e99SRandall Stewart 	asoc->data_pkts_seen++;
2966f8829a4aSRandall Stewart 	while (stop_proc == 0) {
2967f8829a4aSRandall Stewart 		/* validate chunk length */
2968f8829a4aSRandall Stewart 		chk_length = ntohs(ch->ch.chunk_length);
2969f8829a4aSRandall Stewart 		if (length - *offset < chk_length) {
2970f8829a4aSRandall Stewart 			/* all done, mutulated chunk */
2971f8829a4aSRandall Stewart 			stop_proc = 1;
2972f8829a4aSRandall Stewart 			break;
2973f8829a4aSRandall Stewart 		}
2974f8829a4aSRandall Stewart 		if (ch->ch.chunk_type == SCTP_DATA) {
2975f8829a4aSRandall Stewart 			if ((size_t)chk_length < sizeof(struct sctp_data_chunk) + 1) {
2976f8829a4aSRandall Stewart 				/*
2977f8829a4aSRandall Stewart 				 * Need to send an abort since we had a
2978f8829a4aSRandall Stewart 				 * invalid data chunk.
2979f8829a4aSRandall Stewart 				 */
2980f8829a4aSRandall Stewart 				struct mbuf *op_err;
2981f8829a4aSRandall Stewart 
2982139bc87fSRandall Stewart 				op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 2 * sizeof(uint32_t)),
2983f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
2984f8829a4aSRandall Stewart 
2985f8829a4aSRandall Stewart 				if (op_err) {
2986f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
2987f8829a4aSRandall Stewart 					uint32_t *ippp;
2988f8829a4aSRandall Stewart 
2989139bc87fSRandall Stewart 					SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr) +
2990f8829a4aSRandall Stewart 					    (2 * sizeof(uint32_t));
2991f8829a4aSRandall Stewart 					ph = mtod(op_err, struct sctp_paramhdr *);
2992f8829a4aSRandall Stewart 					ph->param_type =
2993f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2994139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(op_err));
2995f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
2996a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_19);
2997f8829a4aSRandall Stewart 					ippp++;
2998f8829a4aSRandall Stewart 					*ippp = asoc->cumulative_tsn;
2999f8829a4aSRandall Stewart 
3000f8829a4aSRandall Stewart 				}
3001a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_19;
3002f8829a4aSRandall Stewart 				sctp_abort_association(inp, stcb, m, iphlen, sh,
3003c54a18d2SRandall Stewart 				    op_err, 0, net->port);
3004f8829a4aSRandall Stewart 				return (2);
3005f8829a4aSRandall Stewart 			}
3006f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
3007f8829a4aSRandall Stewart 			sctp_audit_log(0xB1, 0);
3008f8829a4aSRandall Stewart #endif
3009f8829a4aSRandall Stewart 			if (SCTP_SIZE32(chk_length) == (length - *offset)) {
3010f8829a4aSRandall Stewart 				last_chunk = 1;
3011f8829a4aSRandall Stewart 			} else {
3012f8829a4aSRandall Stewart 				last_chunk = 0;
3013f8829a4aSRandall Stewart 			}
3014f8829a4aSRandall Stewart 			if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset, ch,
3015f8829a4aSRandall Stewart 			    chk_length, net, high_tsn, &abort_flag, &break_flag,
3016f8829a4aSRandall Stewart 			    last_chunk)) {
3017f8829a4aSRandall Stewart 				num_chunks++;
3018f8829a4aSRandall Stewart 			}
3019f8829a4aSRandall Stewart 			if (abort_flag)
3020f8829a4aSRandall Stewart 				return (2);
3021f8829a4aSRandall Stewart 
3022f8829a4aSRandall Stewart 			if (break_flag) {
3023f8829a4aSRandall Stewart 				/*
3024f8829a4aSRandall Stewart 				 * Set because of out of rwnd space and no
3025f8829a4aSRandall Stewart 				 * drop rep space left.
3026f8829a4aSRandall Stewart 				 */
3027f8829a4aSRandall Stewart 				stop_proc = 1;
3028f8829a4aSRandall Stewart 				break;
3029f8829a4aSRandall Stewart 			}
3030f8829a4aSRandall Stewart 		} else {
3031f8829a4aSRandall Stewart 			/* not a data chunk in the data region */
3032f8829a4aSRandall Stewart 			switch (ch->ch.chunk_type) {
3033f8829a4aSRandall Stewart 			case SCTP_INITIATION:
3034f8829a4aSRandall Stewart 			case SCTP_INITIATION_ACK:
3035f8829a4aSRandall Stewart 			case SCTP_SELECTIVE_ACK:
3036830d754dSRandall Stewart 			case SCTP_NR_SELECTIVE_ACK:	/* EY */
3037f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_REQUEST:
3038f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_ACK:
3039f8829a4aSRandall Stewart 			case SCTP_ABORT_ASSOCIATION:
3040f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN:
3041f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_ACK:
3042f8829a4aSRandall Stewart 			case SCTP_OPERATION_ERROR:
3043f8829a4aSRandall Stewart 			case SCTP_COOKIE_ECHO:
3044f8829a4aSRandall Stewart 			case SCTP_COOKIE_ACK:
3045f8829a4aSRandall Stewart 			case SCTP_ECN_ECHO:
3046f8829a4aSRandall Stewart 			case SCTP_ECN_CWR:
3047f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_COMPLETE:
3048f8829a4aSRandall Stewart 			case SCTP_AUTHENTICATION:
3049f8829a4aSRandall Stewart 			case SCTP_ASCONF_ACK:
3050f8829a4aSRandall Stewart 			case SCTP_PACKET_DROPPED:
3051f8829a4aSRandall Stewart 			case SCTP_STREAM_RESET:
3052f8829a4aSRandall Stewart 			case SCTP_FORWARD_CUM_TSN:
3053f8829a4aSRandall Stewart 			case SCTP_ASCONF:
3054f8829a4aSRandall Stewart 				/*
3055f8829a4aSRandall Stewart 				 * Now, what do we do with KNOWN chunks that
3056f8829a4aSRandall Stewart 				 * are NOT in the right place?
3057f8829a4aSRandall Stewart 				 *
3058f8829a4aSRandall Stewart 				 * For now, I do nothing but ignore them. We
3059f8829a4aSRandall Stewart 				 * may later want to add sysctl stuff to
3060f8829a4aSRandall Stewart 				 * switch out and do either an ABORT() or
3061f8829a4aSRandall Stewart 				 * possibly process them.
3062f8829a4aSRandall Stewart 				 */
3063b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_strict_data_order)) {
3064f8829a4aSRandall Stewart 					struct mbuf *op_err;
3065f8829a4aSRandall Stewart 
3066f8829a4aSRandall Stewart 					op_err = sctp_generate_invmanparam(SCTP_CAUSE_PROTOCOL_VIOLATION);
3067c54a18d2SRandall Stewart 					sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 0, net->port);
3068f8829a4aSRandall Stewart 					return (2);
3069f8829a4aSRandall Stewart 				}
3070f8829a4aSRandall Stewart 				break;
3071f8829a4aSRandall Stewart 			default:
3072f8829a4aSRandall Stewart 				/* unknown chunk type, use bit rules */
3073f8829a4aSRandall Stewart 				if (ch->ch.chunk_type & 0x40) {
3074f8829a4aSRandall Stewart 					/* Add a error report to the queue */
3075d61a0ae0SRandall Stewart 					struct mbuf *merr;
3076f8829a4aSRandall Stewart 					struct sctp_paramhdr *phd;
3077f8829a4aSRandall Stewart 
3078d61a0ae0SRandall Stewart 					merr = sctp_get_mbuf_for_msg(sizeof(*phd), 0, M_DONTWAIT, 1, MT_DATA);
3079d61a0ae0SRandall Stewart 					if (merr) {
3080d61a0ae0SRandall Stewart 						phd = mtod(merr, struct sctp_paramhdr *);
3081f8829a4aSRandall Stewart 						/*
3082f8829a4aSRandall Stewart 						 * We cheat and use param
3083f8829a4aSRandall Stewart 						 * type since we did not
3084f8829a4aSRandall Stewart 						 * bother to define a error
3085f8829a4aSRandall Stewart 						 * cause struct. They are
3086f8829a4aSRandall Stewart 						 * the same basic format
3087f8829a4aSRandall Stewart 						 * with different names.
3088f8829a4aSRandall Stewart 						 */
3089f8829a4aSRandall Stewart 						phd->param_type =
3090f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_UNRECOG_CHUNK);
3091f8829a4aSRandall Stewart 						phd->param_length =
3092f8829a4aSRandall Stewart 						    htons(chk_length + sizeof(*phd));
3093d61a0ae0SRandall Stewart 						SCTP_BUF_LEN(merr) = sizeof(*phd);
3094d61a0ae0SRandall Stewart 						SCTP_BUF_NEXT(merr) = SCTP_M_COPYM(m, *offset,
3095f8829a4aSRandall Stewart 						    SCTP_SIZE32(chk_length),
3096f8829a4aSRandall Stewart 						    M_DONTWAIT);
3097d61a0ae0SRandall Stewart 						if (SCTP_BUF_NEXT(merr)) {
3098d61a0ae0SRandall Stewart 							sctp_queue_op_err(stcb, merr);
3099f8829a4aSRandall Stewart 						} else {
3100d61a0ae0SRandall Stewart 							sctp_m_freem(merr);
3101f8829a4aSRandall Stewart 						}
3102f8829a4aSRandall Stewart 					}
3103f8829a4aSRandall Stewart 				}
3104f8829a4aSRandall Stewart 				if ((ch->ch.chunk_type & 0x80) == 0) {
3105f8829a4aSRandall Stewart 					/* discard the rest of this packet */
3106f8829a4aSRandall Stewart 					stop_proc = 1;
3107f8829a4aSRandall Stewart 				}	/* else skip this bad chunk and
3108f8829a4aSRandall Stewart 					 * continue... */
3109f8829a4aSRandall Stewart 				break;
3110f8829a4aSRandall Stewart 			};	/* switch of chunk type */
3111f8829a4aSRandall Stewart 		}
3112f8829a4aSRandall Stewart 		*offset += SCTP_SIZE32(chk_length);
3113f8829a4aSRandall Stewart 		if ((*offset >= length) || stop_proc) {
3114f8829a4aSRandall Stewart 			/* no more data left in the mbuf chain */
3115f8829a4aSRandall Stewart 			stop_proc = 1;
3116f8829a4aSRandall Stewart 			continue;
3117f8829a4aSRandall Stewart 		}
3118f8829a4aSRandall Stewart 		ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
3119f8829a4aSRandall Stewart 		    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
3120f8829a4aSRandall Stewart 		if (ch == NULL) {
3121f8829a4aSRandall Stewart 			*offset = length;
3122f8829a4aSRandall Stewart 			stop_proc = 1;
3123f8829a4aSRandall Stewart 			break;
3124f8829a4aSRandall Stewart 
3125f8829a4aSRandall Stewart 		}
3126f8829a4aSRandall Stewart 	}			/* while */
3127f8829a4aSRandall Stewart 	if (break_flag) {
3128f8829a4aSRandall Stewart 		/*
3129f8829a4aSRandall Stewart 		 * we need to report rwnd overrun drops.
3130f8829a4aSRandall Stewart 		 */
3131f8829a4aSRandall Stewart 		sctp_send_packet_dropped(stcb, net, *mm, iphlen, 0);
3132f8829a4aSRandall Stewart 	}
3133f8829a4aSRandall Stewart 	if (num_chunks) {
3134f8829a4aSRandall Stewart 		/*
3135ceaad40aSRandall Stewart 		 * Did we get data, if so update the time for auto-close and
3136f8829a4aSRandall Stewart 		 * give peer credit for being alive.
3137f8829a4aSRandall Stewart 		 */
3138f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvpktwithdata);
3139b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
3140c4739e2fSRandall Stewart 			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
3141c4739e2fSRandall Stewart 			    stcb->asoc.overall_error_count,
3142c4739e2fSRandall Stewart 			    0,
3143c4739e2fSRandall Stewart 			    SCTP_FROM_SCTP_INDATA,
3144c4739e2fSRandall Stewart 			    __LINE__);
3145c4739e2fSRandall Stewart 		}
3146f8829a4aSRandall Stewart 		stcb->asoc.overall_error_count = 0;
31476e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
3148f8829a4aSRandall Stewart 	}
3149f8829a4aSRandall Stewart 	/* now service all of the reassm queue if needed */
3150f8829a4aSRandall Stewart 	if (!(TAILQ_EMPTY(&asoc->reasmqueue)))
3151f8829a4aSRandall Stewart 		sctp_service_queues(stcb, asoc);
3152f8829a4aSRandall Stewart 
3153f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
315442551e99SRandall Stewart 		/* Assure that we ack right away */
315542551e99SRandall Stewart 		stcb->asoc.send_sack = 1;
3156f8829a4aSRandall Stewart 	}
3157f8829a4aSRandall Stewart 	/* Start a sack timer or QUEUE a SACK for sending */
3158f8829a4aSRandall Stewart 	if ((stcb->asoc.cumulative_tsn == stcb->asoc.highest_tsn_inside_map) &&
315942551e99SRandall Stewart 	    (stcb->asoc.mapping_array[0] != 0xff)) {
316042551e99SRandall Stewart 		if ((stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq) ||
316142551e99SRandall Stewart 		    (stcb->asoc.delayed_ack == 0) ||
3162b201f536SRandall Stewart 		    (stcb->asoc.numduptsns) ||
316342551e99SRandall Stewart 		    (stcb->asoc.send_sack == 1)) {
3164139bc87fSRandall Stewart 			if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
3165ad81507eSRandall Stewart 				(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
316642551e99SRandall Stewart 			}
3167830d754dSRandall Stewart 			/*
3168830d754dSRandall Stewart 			 * EY if nr_sacks used then send an nr-sack , a sack
3169830d754dSRandall Stewart 			 * otherwise
3170830d754dSRandall Stewart 			 */
3171830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack)
3172830d754dSRandall Stewart 				sctp_send_nr_sack(stcb);
3173830d754dSRandall Stewart 			else
3174f8829a4aSRandall Stewart 				sctp_send_sack(stcb);
3175f8829a4aSRandall Stewart 		} else {
317642551e99SRandall Stewart 			if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
3177f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
3178f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, NULL);
3179f8829a4aSRandall Stewart 			}
3180f8829a4aSRandall Stewart 		}
3181f8829a4aSRandall Stewart 	} else {
3182f8829a4aSRandall Stewart 		sctp_sack_check(stcb, 1, was_a_gap, &abort_flag);
3183f8829a4aSRandall Stewart 	}
3184f8829a4aSRandall Stewart 	if (abort_flag)
3185f8829a4aSRandall Stewart 		return (2);
3186f8829a4aSRandall Stewart 
3187f8829a4aSRandall Stewart 	return (0);
3188f8829a4aSRandall Stewart }
3189f8829a4aSRandall Stewart 
31900fa753b3SRandall Stewart static int
31910fa753b3SRandall Stewart sctp_process_segment_range(struct sctp_tcb *stcb, struct sctp_tmit_chunk **p_tp1, uint32_t last_tsn,
31920fa753b3SRandall Stewart     uint16_t frag_strt, uint16_t frag_end, int nr_sacking,
31930fa753b3SRandall Stewart     int *num_frs,
31940fa753b3SRandall Stewart     uint32_t * biggest_newly_acked_tsn,
31950fa753b3SRandall Stewart     uint32_t * this_sack_lowest_newack,
31960fa753b3SRandall Stewart     int *ecn_seg_sums)
31970fa753b3SRandall Stewart {
31980fa753b3SRandall Stewart 	struct sctp_tmit_chunk *tp1;
31990fa753b3SRandall Stewart 	unsigned int theTSN;
32000fa753b3SRandall Stewart 	int j, wake_him = 0;
32010fa753b3SRandall Stewart 
32020fa753b3SRandall Stewart 	/* Recover the tp1 we last saw */
32030fa753b3SRandall Stewart 	tp1 = *p_tp1;
32040fa753b3SRandall Stewart 	if (tp1 == NULL) {
32050fa753b3SRandall Stewart 		tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
32060fa753b3SRandall Stewart 	}
32070fa753b3SRandall Stewart 	for (j = frag_strt; j <= frag_end; j++) {
32080fa753b3SRandall Stewart 		theTSN = j + last_tsn;
32090fa753b3SRandall Stewart 		while (tp1) {
32100fa753b3SRandall Stewart 			if (tp1->rec.data.doing_fast_retransmit)
32110fa753b3SRandall Stewart 				(*num_frs) += 1;
32120fa753b3SRandall Stewart 
32130fa753b3SRandall Stewart 			/*-
32140fa753b3SRandall Stewart 			 * CMT: CUCv2 algorithm. For each TSN being
32150fa753b3SRandall Stewart 			 * processed from the sent queue, track the
32160fa753b3SRandall Stewart 			 * next expected pseudo-cumack, or
32170fa753b3SRandall Stewart 			 * rtx_pseudo_cumack, if required. Separate
32180fa753b3SRandall Stewart 			 * cumack trackers for first transmissions,
32190fa753b3SRandall Stewart 			 * and retransmissions.
32200fa753b3SRandall Stewart 			 */
32210fa753b3SRandall Stewart 			if ((tp1->whoTo->find_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
32220fa753b3SRandall Stewart 			    (tp1->snd_count == 1)) {
32230fa753b3SRandall Stewart 				tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
32240fa753b3SRandall Stewart 				tp1->whoTo->find_pseudo_cumack = 0;
32250fa753b3SRandall Stewart 			}
32260fa753b3SRandall Stewart 			if ((tp1->whoTo->find_rtx_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
32270fa753b3SRandall Stewart 			    (tp1->snd_count > 1)) {
32280fa753b3SRandall Stewart 				tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
32290fa753b3SRandall Stewart 				tp1->whoTo->find_rtx_pseudo_cumack = 0;
32300fa753b3SRandall Stewart 			}
32310fa753b3SRandall Stewart 			if (tp1->rec.data.TSN_seq == theTSN) {
32320fa753b3SRandall Stewart 				if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
32330fa753b3SRandall Stewart 					/*-
32340fa753b3SRandall Stewart 					 * must be held until
32350fa753b3SRandall Stewart 					 * cum-ack passes
32360fa753b3SRandall Stewart 					 */
32370fa753b3SRandall Stewart 					/*-
32380fa753b3SRandall Stewart 					 * ECN Nonce: Add the nonce
32390fa753b3SRandall Stewart 					 * value to the sender's
32400fa753b3SRandall Stewart 					 * nonce sum
32410fa753b3SRandall Stewart 					 */
32420fa753b3SRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
32430fa753b3SRandall Stewart 						/*-
32440fa753b3SRandall Stewart 						 * If it is less than RESEND, it is
32450fa753b3SRandall Stewart 						 * now no-longer in flight.
32460fa753b3SRandall Stewart 						 * Higher values may already be set
32470fa753b3SRandall Stewart 						 * via previous Gap Ack Blocks...
32480fa753b3SRandall Stewart 						 * i.e. ACKED or RESEND.
32490fa753b3SRandall Stewart 						 */
32500fa753b3SRandall Stewart 						if (compare_with_wrap(tp1->rec.data.TSN_seq,
32510fa753b3SRandall Stewart 						    *biggest_newly_acked_tsn, MAX_TSN)) {
32520fa753b3SRandall Stewart 							*biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
32530fa753b3SRandall Stewart 						}
32540fa753b3SRandall Stewart 						/*-
32550fa753b3SRandall Stewart 						 * CMT: SFR algo (and HTNA) - set
32560fa753b3SRandall Stewart 						 * saw_newack to 1 for dest being
32570fa753b3SRandall Stewart 						 * newly acked. update
32580fa753b3SRandall Stewart 						 * this_sack_highest_newack if
32590fa753b3SRandall Stewart 						 * appropriate.
32600fa753b3SRandall Stewart 						 */
32610fa753b3SRandall Stewart 						if (tp1->rec.data.chunk_was_revoked == 0)
32620fa753b3SRandall Stewart 							tp1->whoTo->saw_newack = 1;
32630fa753b3SRandall Stewart 
32640fa753b3SRandall Stewart 						if (compare_with_wrap(tp1->rec.data.TSN_seq,
32650fa753b3SRandall Stewart 						    tp1->whoTo->this_sack_highest_newack,
32660fa753b3SRandall Stewart 						    MAX_TSN)) {
32670fa753b3SRandall Stewart 							tp1->whoTo->this_sack_highest_newack =
32680fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq;
32690fa753b3SRandall Stewart 						}
32700fa753b3SRandall Stewart 						/*-
32710fa753b3SRandall Stewart 						 * CMT DAC algo: also update
32720fa753b3SRandall Stewart 						 * this_sack_lowest_newack
32730fa753b3SRandall Stewart 						 */
32740fa753b3SRandall Stewart 						if (*this_sack_lowest_newack == 0) {
32750fa753b3SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
32760fa753b3SRandall Stewart 								sctp_log_sack(*this_sack_lowest_newack,
32770fa753b3SRandall Stewart 								    last_tsn,
32780fa753b3SRandall Stewart 								    tp1->rec.data.TSN_seq,
32790fa753b3SRandall Stewart 								    0,
32800fa753b3SRandall Stewart 								    0,
32810fa753b3SRandall Stewart 								    SCTP_LOG_TSN_ACKED);
32820fa753b3SRandall Stewart 							}
32830fa753b3SRandall Stewart 							*this_sack_lowest_newack = tp1->rec.data.TSN_seq;
32840fa753b3SRandall Stewart 						}
32850fa753b3SRandall Stewart 						/*-
32860fa753b3SRandall Stewart 						 * CMT: CUCv2 algorithm. If (rtx-)pseudo-cumack for corresp
32870fa753b3SRandall Stewart 						 * dest is being acked, then we have a new (rtx-)pseudo-cumack. Set
32880fa753b3SRandall Stewart 						 * new_(rtx_)pseudo_cumack to TRUE so that the cwnd for this dest can be
32890fa753b3SRandall Stewart 						 * updated. Also trigger search for the next expected (rtx-)pseudo-cumack.
32900fa753b3SRandall Stewart 						 * Separate pseudo_cumack trackers for first transmissions and
32910fa753b3SRandall Stewart 						 * retransmissions.
32920fa753b3SRandall Stewart 						 */
32930fa753b3SRandall Stewart 						if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
32940fa753b3SRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0) {
32950fa753b3SRandall Stewart 								tp1->whoTo->new_pseudo_cumack = 1;
32960fa753b3SRandall Stewart 							}
32970fa753b3SRandall Stewart 							tp1->whoTo->find_pseudo_cumack = 1;
32980fa753b3SRandall Stewart 						}
32990fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
33000fa753b3SRandall Stewart 							sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
33010fa753b3SRandall Stewart 						}
33020fa753b3SRandall Stewart 						if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
33030fa753b3SRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0) {
33040fa753b3SRandall Stewart 								tp1->whoTo->new_pseudo_cumack = 1;
33050fa753b3SRandall Stewart 							}
33060fa753b3SRandall Stewart 							tp1->whoTo->find_rtx_pseudo_cumack = 1;
33070fa753b3SRandall Stewart 						}
33080fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
33090fa753b3SRandall Stewart 							sctp_log_sack(*biggest_newly_acked_tsn,
33100fa753b3SRandall Stewart 							    last_tsn,
33110fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq,
33120fa753b3SRandall Stewart 							    frag_strt,
33130fa753b3SRandall Stewart 							    frag_end,
33140fa753b3SRandall Stewart 							    SCTP_LOG_TSN_ACKED);
33150fa753b3SRandall Stewart 						}
33160fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
33170fa753b3SRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
33180fa753b3SRandall Stewart 							    tp1->whoTo->flight_size,
33190fa753b3SRandall Stewart 							    tp1->book_size,
33200fa753b3SRandall Stewart 							    (uintptr_t) tp1->whoTo,
33210fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq);
33220fa753b3SRandall Stewart 						}
33230fa753b3SRandall Stewart 						sctp_flight_size_decrease(tp1);
33240fa753b3SRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
33250fa753b3SRandall Stewart 
33260fa753b3SRandall Stewart 						tp1->whoTo->net_ack += tp1->send_size;
33270fa753b3SRandall Stewart 						if (tp1->snd_count < 2) {
33280fa753b3SRandall Stewart 							/*-
33290fa753b3SRandall Stewart 							 * True non-retransmited chunk
33300fa753b3SRandall Stewart 							 */
33310fa753b3SRandall Stewart 							tp1->whoTo->net_ack2 += tp1->send_size;
33320fa753b3SRandall Stewart 
33330fa753b3SRandall Stewart 							/*-
33340fa753b3SRandall Stewart 							 * update RTO too ?
33350fa753b3SRandall Stewart 							 */
33360fa753b3SRandall Stewart 							if (tp1->do_rtt) {
33370fa753b3SRandall Stewart 								tp1->whoTo->RTO =
33380fa753b3SRandall Stewart 								    sctp_calculate_rto(stcb,
33390fa753b3SRandall Stewart 								    &stcb->asoc,
33400fa753b3SRandall Stewart 								    tp1->whoTo,
33410fa753b3SRandall Stewart 								    &tp1->sent_rcv_time,
33420fa753b3SRandall Stewart 								    sctp_align_safe_nocopy);
33430fa753b3SRandall Stewart 								tp1->do_rtt = 0;
33440fa753b3SRandall Stewart 							}
33450fa753b3SRandall Stewart 						}
33460fa753b3SRandall Stewart 					}
33470fa753b3SRandall Stewart 					if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
33480fa753b3SRandall Stewart 						(*ecn_seg_sums) += tp1->rec.data.ect_nonce;
33490fa753b3SRandall Stewart 						(*ecn_seg_sums) &= SCTP_SACK_NONCE_SUM;
33500fa753b3SRandall Stewart 						if (compare_with_wrap(tp1->rec.data.TSN_seq,
33510fa753b3SRandall Stewart 						    stcb->asoc.this_sack_highest_gap,
33520fa753b3SRandall Stewart 						    MAX_TSN)) {
33530fa753b3SRandall Stewart 							stcb->asoc.this_sack_highest_gap =
33540fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq;
33550fa753b3SRandall Stewart 						}
33560fa753b3SRandall Stewart 						if (tp1->sent == SCTP_DATAGRAM_RESEND) {
33570fa753b3SRandall Stewart 							sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
33580fa753b3SRandall Stewart #ifdef SCTP_AUDITING_ENABLED
33590fa753b3SRandall Stewart 							sctp_audit_log(0xB2,
33600fa753b3SRandall Stewart 							    (stcb->asoc.sent_queue_retran_cnt & 0x000000ff));
33610fa753b3SRandall Stewart #endif
33620fa753b3SRandall Stewart 						}
33630fa753b3SRandall Stewart 					}
33640fa753b3SRandall Stewart 					/*-
33650fa753b3SRandall Stewart 					 * All chunks NOT UNSENT fall through here and are marked
33660fa753b3SRandall Stewart 					 * (leave PR-SCTP ones that are to skip alone though)
33670fa753b3SRandall Stewart 					 */
33680fa753b3SRandall Stewart 					if (tp1->sent != SCTP_FORWARD_TSN_SKIP)
33690fa753b3SRandall Stewart 						tp1->sent = SCTP_DATAGRAM_MARKED;
33700fa753b3SRandall Stewart 
33710fa753b3SRandall Stewart 					if (tp1->rec.data.chunk_was_revoked) {
33720fa753b3SRandall Stewart 						/* deflate the cwnd */
33730fa753b3SRandall Stewart 						tp1->whoTo->cwnd -= tp1->book_size;
33740fa753b3SRandall Stewart 						tp1->rec.data.chunk_was_revoked = 0;
33750fa753b3SRandall Stewart 					}
33760fa753b3SRandall Stewart 					/* NR Sack code here */
33770fa753b3SRandall Stewart 					if (nr_sacking) {
33780fa753b3SRandall Stewart 						if (tp1->sent != SCTP_FORWARD_TSN_SKIP)
33790fa753b3SRandall Stewart 							tp1->sent = SCTP_DATAGRAM_NR_MARKED;
33800fa753b3SRandall Stewart 						/*
33810fa753b3SRandall Stewart 						 * TAILQ_REMOVE(&asoc->sent_q
33820fa753b3SRandall Stewart 						 * ueue, tp1, sctp_next);
33830fa753b3SRandall Stewart 						 */
33840fa753b3SRandall Stewart 						if (tp1->data) {
33850fa753b3SRandall Stewart 							/*
33860fa753b3SRandall Stewart 							 * sa_ignore
33870fa753b3SRandall Stewart 							 * NO_NULL_CHK
33880fa753b3SRandall Stewart 							 */
33890fa753b3SRandall Stewart 							sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
33900fa753b3SRandall Stewart 							sctp_m_freem(tp1->data);
33910fa753b3SRandall Stewart 						}
33920fa753b3SRandall Stewart 						tp1->data = NULL;
33930fa753b3SRandall Stewart 						/* asoc->sent_queue_cnt--; */
33940fa753b3SRandall Stewart 						/*
33950fa753b3SRandall Stewart 						 * sctp_free_a_chunk(stcb,
33960fa753b3SRandall Stewart 						 * tp1);
33970fa753b3SRandall Stewart 						 */
33980fa753b3SRandall Stewart 						wake_him++;
33990fa753b3SRandall Stewart 					}
34000fa753b3SRandall Stewart 				}
34010fa753b3SRandall Stewart 				break;
34020fa753b3SRandall Stewart 			}	/* if (tp1->TSN_seq == theTSN) */
34030fa753b3SRandall Stewart 			if (compare_with_wrap(tp1->rec.data.TSN_seq, theTSN,
34040fa753b3SRandall Stewart 			    MAX_TSN))
34050fa753b3SRandall Stewart 				break;
34060fa753b3SRandall Stewart 
34070fa753b3SRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
34080fa753b3SRandall Stewart 		}		/* end while (tp1) */
34090fa753b3SRandall Stewart 		/* In case the fragments were not in order we must reset */
34100fa753b3SRandall Stewart 		if (tp1 == NULL) {
34110fa753b3SRandall Stewart 			tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
34120fa753b3SRandall Stewart 		}
34130fa753b3SRandall Stewart 	}			/* end for (j = fragStart */
34140fa753b3SRandall Stewart 	*p_tp1 = tp1;
34150fa753b3SRandall Stewart 	return (wake_him);	/* Return value only used for nr-sack */
34160fa753b3SRandall Stewart }
34170fa753b3SRandall Stewart 
34180fa753b3SRandall Stewart 
3419f8829a4aSRandall Stewart static void
3420458303daSRandall Stewart sctp_handle_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
3421f8829a4aSRandall Stewart     struct sctp_sack_chunk *ch, uint32_t last_tsn, uint32_t * biggest_tsn_acked,
3422139bc87fSRandall Stewart     uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
3423139bc87fSRandall Stewart     int num_seg, int *ecn_seg_sums)
3424f8829a4aSRandall Stewart {
3425f8829a4aSRandall Stewart 	/************************************************/
3426f8829a4aSRandall Stewart 	/* process fragments and update sendqueue        */
3427f8829a4aSRandall Stewart 	/************************************************/
3428f8829a4aSRandall Stewart 	struct sctp_sack *sack;
3429458303daSRandall Stewart 	struct sctp_gap_ack_block *frag, block;
3430f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
34310fa753b3SRandall Stewart 	int i;
3432f8829a4aSRandall Stewart 	int num_frs = 0;
3433f8829a4aSRandall Stewart 
3434f8829a4aSRandall Stewart 	uint16_t frag_strt, frag_end, primary_flag_set;
3435f8829a4aSRandall Stewart 	u_long last_frag_high;
3436f8829a4aSRandall Stewart 
3437f8829a4aSRandall Stewart 	/*
3438f8829a4aSRandall Stewart 	 * @@@ JRI : TODO: This flag is not used anywhere .. remove?
3439f8829a4aSRandall Stewart 	 */
3440f8829a4aSRandall Stewart 	if (asoc->primary_destination->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
3441f8829a4aSRandall Stewart 		primary_flag_set = 1;
3442f8829a4aSRandall Stewart 	} else {
3443f8829a4aSRandall Stewart 		primary_flag_set = 0;
3444f8829a4aSRandall Stewart 	}
3445f8829a4aSRandall Stewart 	sack = &ch->sack;
3446458303daSRandall Stewart 
3447458303daSRandall Stewart 	frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
3448458303daSRandall Stewart 	    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
3449458303daSRandall Stewart 	*offset += sizeof(block);
3450458303daSRandall Stewart 	if (frag == NULL) {
3451458303daSRandall Stewart 		return;
3452458303daSRandall Stewart 	}
3453f8829a4aSRandall Stewart 	tp1 = NULL;
3454f8829a4aSRandall Stewart 	last_frag_high = 0;
3455f8829a4aSRandall Stewart 	for (i = 0; i < num_seg; i++) {
3456f8829a4aSRandall Stewart 		frag_strt = ntohs(frag->start);
3457f8829a4aSRandall Stewart 		frag_end = ntohs(frag->end);
3458830d754dSRandall Stewart 		/* some sanity checks on the fragment offsets */
3459f8829a4aSRandall Stewart 		if (frag_strt > frag_end) {
3460f8829a4aSRandall Stewart 			/* this one is malformed, skip */
3461f8829a4aSRandall Stewart 			frag++;
3462f8829a4aSRandall Stewart 			continue;
3463f8829a4aSRandall Stewart 		}
3464f8829a4aSRandall Stewart 		if (compare_with_wrap((frag_end + last_tsn), *biggest_tsn_acked,
3465f8829a4aSRandall Stewart 		    MAX_TSN))
3466f8829a4aSRandall Stewart 			*biggest_tsn_acked = frag_end + last_tsn;
3467f8829a4aSRandall Stewart 
3468f8829a4aSRandall Stewart 		/* mark acked dgs and find out the highestTSN being acked */
3469f8829a4aSRandall Stewart 		if (tp1 == NULL) {
3470f8829a4aSRandall Stewart 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
3471f8829a4aSRandall Stewart 
3472f8829a4aSRandall Stewart 			/* save the locations of the last frags */
3473f8829a4aSRandall Stewart 			last_frag_high = frag_end + last_tsn;
3474f8829a4aSRandall Stewart 		} else {
3475f8829a4aSRandall Stewart 			/*
3476f8829a4aSRandall Stewart 			 * now lets see if we need to reset the queue due to
3477f8829a4aSRandall Stewart 			 * a out-of-order SACK fragment
3478f8829a4aSRandall Stewart 			 */
3479f8829a4aSRandall Stewart 			if (compare_with_wrap(frag_strt + last_tsn,
3480f8829a4aSRandall Stewart 			    last_frag_high, MAX_TSN)) {
3481f8829a4aSRandall Stewart 				/*
3482f8829a4aSRandall Stewart 				 * if the new frag starts after the last TSN
3483f8829a4aSRandall Stewart 				 * frag covered, we are ok and this one is
3484f8829a4aSRandall Stewart 				 * beyond the last one
3485f8829a4aSRandall Stewart 				 */
3486f8829a4aSRandall Stewart 				;
3487f8829a4aSRandall Stewart 			} else {
3488f8829a4aSRandall Stewart 				/*
3489f8829a4aSRandall Stewart 				 * ok, they have reset us, so we need to
3490f8829a4aSRandall Stewart 				 * reset the queue this will cause extra
3491f8829a4aSRandall Stewart 				 * hunting but hey, they chose the
3492f8829a4aSRandall Stewart 				 * performance hit when they failed to order
3493830d754dSRandall Stewart 				 * their gaps
3494f8829a4aSRandall Stewart 				 */
3495f8829a4aSRandall Stewart 				tp1 = TAILQ_FIRST(&asoc->sent_queue);
3496f8829a4aSRandall Stewart 			}
3497f8829a4aSRandall Stewart 			last_frag_high = frag_end + last_tsn;
3498f8829a4aSRandall Stewart 		}
34990fa753b3SRandall Stewart 		sctp_process_segment_range(stcb, &tp1, last_tsn, frag_strt, frag_end,
35000fa753b3SRandall Stewart 		    0, &num_frs, biggest_newly_acked_tsn,
35010fa753b3SRandall Stewart 		    this_sack_lowest_newack, ecn_seg_sums);
3502458303daSRandall Stewart 		frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
3503458303daSRandall Stewart 		    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
3504458303daSRandall Stewart 		*offset += sizeof(block);
3505458303daSRandall Stewart 		if (frag == NULL) {
3506458303daSRandall Stewart 			break;
3507458303daSRandall Stewart 		}
3508f8829a4aSRandall Stewart 	}
3509b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
351080fefe0aSRandall Stewart 		if (num_frs)
351180fefe0aSRandall Stewart 			sctp_log_fr(*biggest_tsn_acked,
351280fefe0aSRandall Stewart 			    *biggest_newly_acked_tsn,
351380fefe0aSRandall Stewart 			    last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
351480fefe0aSRandall Stewart 	}
3515f8829a4aSRandall Stewart }
3516f8829a4aSRandall Stewart 
3517f8829a4aSRandall Stewart static void
3518c105859eSRandall Stewart sctp_check_for_revoked(struct sctp_tcb *stcb,
3519c105859eSRandall Stewart     struct sctp_association *asoc, uint32_t cumack,
3520f8829a4aSRandall Stewart     u_long biggest_tsn_acked)
3521f8829a4aSRandall Stewart {
3522f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
3523f8829a4aSRandall Stewart 	int tot_revoked = 0;
3524f8829a4aSRandall Stewart 
3525f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
3526f8829a4aSRandall Stewart 	while (tp1) {
3527f8829a4aSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq, cumack,
3528f8829a4aSRandall Stewart 		    MAX_TSN)) {
3529f8829a4aSRandall Stewart 			/*
3530f8829a4aSRandall Stewart 			 * ok this guy is either ACK or MARKED. If it is
3531f8829a4aSRandall Stewart 			 * ACKED it has been previously acked but not this
3532f8829a4aSRandall Stewart 			 * time i.e. revoked.  If it is MARKED it was ACK'ed
3533f8829a4aSRandall Stewart 			 * again.
3534f8829a4aSRandall Stewart 			 */
3535d06c82f1SRandall Stewart 			if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
3536d06c82f1SRandall Stewart 			    MAX_TSN))
3537d06c82f1SRandall Stewart 				break;
3538d06c82f1SRandall Stewart 
3539d06c82f1SRandall Stewart 
3540f8829a4aSRandall Stewart 			if (tp1->sent == SCTP_DATAGRAM_ACKED) {
3541f8829a4aSRandall Stewart 				/* it has been revoked */
3542f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_SENT;
3543f8829a4aSRandall Stewart 				tp1->rec.data.chunk_was_revoked = 1;
3544a5d547adSRandall Stewart 				/*
354542551e99SRandall Stewart 				 * We must add this stuff back in to assure
354642551e99SRandall Stewart 				 * timers and such get started.
3547a5d547adSRandall Stewart 				 */
3548b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3549c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
3550c105859eSRandall Stewart 					    tp1->whoTo->flight_size,
3551c105859eSRandall Stewart 					    tp1->book_size,
3552c105859eSRandall Stewart 					    (uintptr_t) tp1->whoTo,
3553c105859eSRandall Stewart 					    tp1->rec.data.TSN_seq);
355480fefe0aSRandall Stewart 				}
3555c105859eSRandall Stewart 				sctp_flight_size_increase(tp1);
3556c105859eSRandall Stewart 				sctp_total_flight_increase(stcb, tp1);
355742551e99SRandall Stewart 				/*
355842551e99SRandall Stewart 				 * We inflate the cwnd to compensate for our
355942551e99SRandall Stewart 				 * artificial inflation of the flight_size.
356042551e99SRandall Stewart 				 */
356142551e99SRandall Stewart 				tp1->whoTo->cwnd += tp1->book_size;
3562f8829a4aSRandall Stewart 				tot_revoked++;
3563b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3564f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
3565f8829a4aSRandall Stewart 					    cumack,
3566f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
3567f8829a4aSRandall Stewart 					    0,
3568f8829a4aSRandall Stewart 					    0,
3569f8829a4aSRandall Stewart 					    SCTP_LOG_TSN_REVOKED);
357080fefe0aSRandall Stewart 				}
3571f8829a4aSRandall Stewart 			} else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
3572f8829a4aSRandall Stewart 				/* it has been re-acked in this SACK */
3573f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
3574f8829a4aSRandall Stewart 			}
3575f8829a4aSRandall Stewart 		}
3576f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT)
3577f8829a4aSRandall Stewart 			break;
3578f8829a4aSRandall Stewart 		tp1 = TAILQ_NEXT(tp1, sctp_next);
3579f8829a4aSRandall Stewart 	}
3580f8829a4aSRandall Stewart 	if (tot_revoked > 0) {
3581f8829a4aSRandall Stewart 		/*
3582f8829a4aSRandall Stewart 		 * Setup the ecn nonce re-sync point. We do this since once
3583f8829a4aSRandall Stewart 		 * data is revoked we begin to retransmit things, which do
3584f8829a4aSRandall Stewart 		 * NOT have the ECN bits set. This means we are now out of
3585f8829a4aSRandall Stewart 		 * sync and must wait until we get back in sync with the
3586f8829a4aSRandall Stewart 		 * peer to check ECN bits.
3587f8829a4aSRandall Stewart 		 */
3588f8829a4aSRandall Stewart 		tp1 = TAILQ_FIRST(&asoc->send_queue);
3589f8829a4aSRandall Stewart 		if (tp1 == NULL) {
3590f8829a4aSRandall Stewart 			asoc->nonce_resync_tsn = asoc->sending_seq;
3591f8829a4aSRandall Stewart 		} else {
3592f8829a4aSRandall Stewart 			asoc->nonce_resync_tsn = tp1->rec.data.TSN_seq;
3593f8829a4aSRandall Stewart 		}
3594f8829a4aSRandall Stewart 		asoc->nonce_wait_for_ecne = 0;
3595f8829a4aSRandall Stewart 		asoc->nonce_sum_check = 0;
3596f8829a4aSRandall Stewart 	}
3597f8829a4aSRandall Stewart }
3598f8829a4aSRandall Stewart 
3599830d754dSRandall Stewart 
3600f8829a4aSRandall Stewart static void
3601f8829a4aSRandall Stewart sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
3602f8829a4aSRandall Stewart     u_long biggest_tsn_acked, u_long biggest_tsn_newly_acked, u_long this_sack_lowest_newack, int accum_moved)
3603f8829a4aSRandall Stewart {
3604f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
3605f8829a4aSRandall Stewart 	int strike_flag = 0;
3606f8829a4aSRandall Stewart 	struct timeval now;
3607f8829a4aSRandall Stewart 	int tot_retrans = 0;
3608f8829a4aSRandall Stewart 	uint32_t sending_seq;
3609f8829a4aSRandall Stewart 	struct sctp_nets *net;
3610f8829a4aSRandall Stewart 	int num_dests_sacked = 0;
3611f8829a4aSRandall Stewart 
3612f8829a4aSRandall Stewart 	/*
3613f8829a4aSRandall Stewart 	 * select the sending_seq, this is either the next thing ready to be
3614f8829a4aSRandall Stewart 	 * sent but not transmitted, OR, the next seq we assign.
3615f8829a4aSRandall Stewart 	 */
3616f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
3617f8829a4aSRandall Stewart 	if (tp1 == NULL) {
3618f8829a4aSRandall Stewart 		sending_seq = asoc->sending_seq;
3619f8829a4aSRandall Stewart 	} else {
3620f8829a4aSRandall Stewart 		sending_seq = tp1->rec.data.TSN_seq;
3621f8829a4aSRandall Stewart 	}
3622f8829a4aSRandall Stewart 
3623f8829a4aSRandall Stewart 	/* CMT DAC algo: finding out if SACK is a mixed SACK */
3624b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3625f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3626f8829a4aSRandall Stewart 			if (net->saw_newack)
3627f8829a4aSRandall Stewart 				num_dests_sacked++;
3628f8829a4aSRandall Stewart 		}
3629f8829a4aSRandall Stewart 	}
3630f8829a4aSRandall Stewart 	if (stcb->asoc.peer_supports_prsctp) {
36316e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
3632f8829a4aSRandall Stewart 	}
3633f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
3634f8829a4aSRandall Stewart 	while (tp1) {
3635f8829a4aSRandall Stewart 		strike_flag = 0;
3636f8829a4aSRandall Stewart 		if (tp1->no_fr_allowed) {
3637f8829a4aSRandall Stewart 			/* this one had a timeout or something */
3638f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3639f8829a4aSRandall Stewart 			continue;
3640f8829a4aSRandall Stewart 		}
3641b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3642f8829a4aSRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND)
3643f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3644f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3645f8829a4aSRandall Stewart 				    tp1->sent,
3646f8829a4aSRandall Stewart 				    SCTP_FR_LOG_CHECK_STRIKE);
364780fefe0aSRandall Stewart 		}
3648f8829a4aSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
3649f8829a4aSRandall Stewart 		    MAX_TSN) ||
3650f8829a4aSRandall Stewart 		    tp1->sent == SCTP_DATAGRAM_UNSENT) {
3651f8829a4aSRandall Stewart 			/* done */
3652f8829a4aSRandall Stewart 			break;
3653f8829a4aSRandall Stewart 		}
3654f8829a4aSRandall Stewart 		if (stcb->asoc.peer_supports_prsctp) {
3655f8829a4aSRandall Stewart 			if ((PR_SCTP_TTL_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3656f8829a4aSRandall Stewart 				/* Is it expired? */
36575e54f665SRandall Stewart 				if (
3658fc14de76SRandall Stewart 				/*
3659fc14de76SRandall Stewart 				 * TODO sctp_constants.h needs alternative
3660fc14de76SRandall Stewart 				 * time macros when _KERNEL is undefined.
3661fc14de76SRandall Stewart 				 */
36625e54f665SRandall Stewart 				    (timevalcmp(&now, &tp1->rec.data.timetodrop, >))
36635e54f665SRandall Stewart 				    ) {
3664f8829a4aSRandall Stewart 					/* Yes so drop it */
3665f8829a4aSRandall Stewart 					if (tp1->data != NULL) {
3666ad81507eSRandall Stewart 						(void)sctp_release_pr_sctp_chunk(stcb, tp1,
3667f8829a4aSRandall Stewart 						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
36680c0982b8SRandall Stewart 						    SCTP_SO_NOT_LOCKED);
3669f8829a4aSRandall Stewart 					}
3670f8829a4aSRandall Stewart 					tp1 = TAILQ_NEXT(tp1, sctp_next);
3671f8829a4aSRandall Stewart 					continue;
3672f8829a4aSRandall Stewart 				}
3673f8829a4aSRandall Stewart 			}
3674f8829a4aSRandall Stewart 		}
3675f8829a4aSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq,
3676f8829a4aSRandall Stewart 		    asoc->this_sack_highest_gap, MAX_TSN)) {
3677f8829a4aSRandall Stewart 			/* we are beyond the tsn in the sack  */
3678f8829a4aSRandall Stewart 			break;
3679f8829a4aSRandall Stewart 		}
3680f8829a4aSRandall Stewart 		if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
3681f8829a4aSRandall Stewart 			/* either a RESEND, ACKED, or MARKED */
3682f8829a4aSRandall Stewart 			/* skip */
3683f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3684f8829a4aSRandall Stewart 			continue;
3685f8829a4aSRandall Stewart 		}
3686f8829a4aSRandall Stewart 		/*
3687f8829a4aSRandall Stewart 		 * CMT : SFR algo (covers part of DAC and HTNA as well)
3688f8829a4aSRandall Stewart 		 */
3689ad81507eSRandall Stewart 		if (tp1->whoTo && tp1->whoTo->saw_newack == 0) {
3690f8829a4aSRandall Stewart 			/*
3691f8829a4aSRandall Stewart 			 * No new acks were receieved for data sent to this
3692f8829a4aSRandall Stewart 			 * dest. Therefore, according to the SFR algo for
3693f8829a4aSRandall Stewart 			 * CMT, no data sent to this dest can be marked for
3694c105859eSRandall Stewart 			 * FR using this SACK.
3695f8829a4aSRandall Stewart 			 */
3696f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3697f8829a4aSRandall Stewart 			continue;
3698ad81507eSRandall Stewart 		} else if (tp1->whoTo && compare_with_wrap(tp1->rec.data.TSN_seq,
3699f8829a4aSRandall Stewart 		    tp1->whoTo->this_sack_highest_newack, MAX_TSN)) {
3700f8829a4aSRandall Stewart 			/*
3701f8829a4aSRandall Stewart 			 * CMT: New acks were receieved for data sent to
3702f8829a4aSRandall Stewart 			 * this dest. But no new acks were seen for data
3703f8829a4aSRandall Stewart 			 * sent after tp1. Therefore, according to the SFR
3704f8829a4aSRandall Stewart 			 * algo for CMT, tp1 cannot be marked for FR using
3705f8829a4aSRandall Stewart 			 * this SACK. This step covers part of the DAC algo
3706f8829a4aSRandall Stewart 			 * and the HTNA algo as well.
3707f8829a4aSRandall Stewart 			 */
3708f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3709f8829a4aSRandall Stewart 			continue;
3710f8829a4aSRandall Stewart 		}
3711f8829a4aSRandall Stewart 		/*
3712f8829a4aSRandall Stewart 		 * Here we check to see if we were have already done a FR
3713f8829a4aSRandall Stewart 		 * and if so we see if the biggest TSN we saw in the sack is
3714f8829a4aSRandall Stewart 		 * smaller than the recovery point. If so we don't strike
3715f8829a4aSRandall Stewart 		 * the tsn... otherwise we CAN strike the TSN.
3716f8829a4aSRandall Stewart 		 */
3717f8829a4aSRandall Stewart 		/*
371842551e99SRandall Stewart 		 * @@@ JRI: Check for CMT if (accum_moved &&
371942551e99SRandall Stewart 		 * asoc->fast_retran_loss_recovery && (sctp_cmt_on_off ==
372042551e99SRandall Stewart 		 * 0)) {
3721f8829a4aSRandall Stewart 		 */
372242551e99SRandall Stewart 		if (accum_moved && asoc->fast_retran_loss_recovery) {
3723f8829a4aSRandall Stewart 			/*
3724f8829a4aSRandall Stewart 			 * Strike the TSN if in fast-recovery and cum-ack
3725f8829a4aSRandall Stewart 			 * moved.
3726f8829a4aSRandall Stewart 			 */
3727b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3728f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3729f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3730f8829a4aSRandall Stewart 				    tp1->sent,
3731f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
373280fefe0aSRandall Stewart 			}
37335e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3734f8829a4aSRandall Stewart 				tp1->sent++;
37355e54f665SRandall Stewart 			}
3736b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3737f8829a4aSRandall Stewart 				/*
3738f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
3739f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
3740f8829a4aSRandall Stewart 				 * because it would have been set to the
3741f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
3742f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
3743f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
3744c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
3745c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3746c105859eSRandall Stewart 				 * indicates that two packets have been
3747c105859eSRandall Stewart 				 * received after this missing TSN.
37485e54f665SRandall Stewart 				 */
37495e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3750f8829a4aSRandall Stewart 				    compare_with_wrap(this_sack_lowest_newack, tp1->rec.data.TSN_seq, MAX_TSN)) {
3751b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3752f8829a4aSRandall Stewart 						sctp_log_fr(16 + num_dests_sacked,
3753f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3754f8829a4aSRandall Stewart 						    tp1->sent,
3755f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
375680fefe0aSRandall Stewart 					}
3757f8829a4aSRandall Stewart 					tp1->sent++;
3758f8829a4aSRandall Stewart 				}
3759f8829a4aSRandall Stewart 			}
3760b3f1ea41SRandall Stewart 		} else if ((tp1->rec.data.doing_fast_retransmit) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
3761f8829a4aSRandall Stewart 			/*
3762f8829a4aSRandall Stewart 			 * For those that have done a FR we must take
3763f8829a4aSRandall Stewart 			 * special consideration if we strike. I.e the
3764f8829a4aSRandall Stewart 			 * biggest_newly_acked must be higher than the
3765f8829a4aSRandall Stewart 			 * sending_seq at the time we did the FR.
3766f8829a4aSRandall Stewart 			 */
37675e54f665SRandall Stewart 			if (
3768f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3769f8829a4aSRandall Stewart 			/*
3770f8829a4aSRandall Stewart 			 * If FR's go to new networks, then we must only do
3771f8829a4aSRandall Stewart 			 * this for singly homed asoc's. However if the FR's
3772f8829a4aSRandall Stewart 			 * go to the same network (Armando's work) then its
3773f8829a4aSRandall Stewart 			 * ok to FR multiple times.
3774f8829a4aSRandall Stewart 			 */
37755e54f665SRandall Stewart 			    (asoc->numnets < 2)
3776f8829a4aSRandall Stewart #else
37775e54f665SRandall Stewart 			    (1)
3778f8829a4aSRandall Stewart #endif
37795e54f665SRandall Stewart 			    ) {
37805e54f665SRandall Stewart 
3781f8829a4aSRandall Stewart 				if ((compare_with_wrap(biggest_tsn_newly_acked,
3782f8829a4aSRandall Stewart 				    tp1->rec.data.fast_retran_tsn, MAX_TSN)) ||
3783f8829a4aSRandall Stewart 				    (biggest_tsn_newly_acked ==
3784f8829a4aSRandall Stewart 				    tp1->rec.data.fast_retran_tsn)) {
3785f8829a4aSRandall Stewart 					/*
3786f8829a4aSRandall Stewart 					 * Strike the TSN, since this ack is
3787f8829a4aSRandall Stewart 					 * beyond where things were when we
3788f8829a4aSRandall Stewart 					 * did a FR.
3789f8829a4aSRandall Stewart 					 */
3790b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3791f8829a4aSRandall Stewart 						sctp_log_fr(biggest_tsn_newly_acked,
3792f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3793f8829a4aSRandall Stewart 						    tp1->sent,
3794f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
379580fefe0aSRandall Stewart 					}
37965e54f665SRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3797f8829a4aSRandall Stewart 						tp1->sent++;
37985e54f665SRandall Stewart 					}
3799f8829a4aSRandall Stewart 					strike_flag = 1;
3800b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3801f8829a4aSRandall Stewart 						/*
3802f8829a4aSRandall Stewart 						 * CMT DAC algorithm: If
3803f8829a4aSRandall Stewart 						 * SACK flag is set to 0,
3804f8829a4aSRandall Stewart 						 * then lowest_newack test
3805f8829a4aSRandall Stewart 						 * will not pass because it
3806f8829a4aSRandall Stewart 						 * would have been set to
3807f8829a4aSRandall Stewart 						 * the cumack earlier. If
3808f8829a4aSRandall Stewart 						 * not already to be rtx'd,
3809f8829a4aSRandall Stewart 						 * If not a mixed sack and
3810f8829a4aSRandall Stewart 						 * if tp1 is not between two
3811f8829a4aSRandall Stewart 						 * sacked TSNs, then mark by
3812c105859eSRandall Stewart 						 * one more. NOTE that we
3813c105859eSRandall Stewart 						 * are marking by one
3814c105859eSRandall Stewart 						 * additional time since the
3815c105859eSRandall Stewart 						 * SACK DAC flag indicates
3816c105859eSRandall Stewart 						 * that two packets have
3817c105859eSRandall Stewart 						 * been received after this
3818c105859eSRandall Stewart 						 * missing TSN.
3819f8829a4aSRandall Stewart 						 */
38205e54f665SRandall Stewart 						if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
38215e54f665SRandall Stewart 						    (num_dests_sacked == 1) &&
38225e54f665SRandall Stewart 						    compare_with_wrap(this_sack_lowest_newack,
38235e54f665SRandall Stewart 						    tp1->rec.data.TSN_seq, MAX_TSN)) {
3824b3f1ea41SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3825f8829a4aSRandall Stewart 								sctp_log_fr(32 + num_dests_sacked,
3826f8829a4aSRandall Stewart 								    tp1->rec.data.TSN_seq,
3827f8829a4aSRandall Stewart 								    tp1->sent,
3828f8829a4aSRandall Stewart 								    SCTP_FR_LOG_STRIKE_CHUNK);
382980fefe0aSRandall Stewart 							}
38305e54f665SRandall Stewart 							if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3831f8829a4aSRandall Stewart 								tp1->sent++;
38325e54f665SRandall Stewart 							}
3833f8829a4aSRandall Stewart 						}
3834f8829a4aSRandall Stewart 					}
3835f8829a4aSRandall Stewart 				}
3836f8829a4aSRandall Stewart 			}
3837f8829a4aSRandall Stewart 			/*
383842551e99SRandall Stewart 			 * JRI: TODO: remove code for HTNA algo. CMT's SFR
383942551e99SRandall Stewart 			 * algo covers HTNA.
3840f8829a4aSRandall Stewart 			 */
3841f8829a4aSRandall Stewart 		} else if (compare_with_wrap(tp1->rec.data.TSN_seq,
3842f8829a4aSRandall Stewart 		    biggest_tsn_newly_acked, MAX_TSN)) {
3843f8829a4aSRandall Stewart 			/*
3844f8829a4aSRandall Stewart 			 * We don't strike these: This is the  HTNA
3845f8829a4aSRandall Stewart 			 * algorithm i.e. we don't strike If our TSN is
3846f8829a4aSRandall Stewart 			 * larger than the Highest TSN Newly Acked.
3847f8829a4aSRandall Stewart 			 */
3848f8829a4aSRandall Stewart 			;
3849f8829a4aSRandall Stewart 		} else {
3850f8829a4aSRandall Stewart 			/* Strike the TSN */
3851b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3852f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3853f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3854f8829a4aSRandall Stewart 				    tp1->sent,
3855f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
385680fefe0aSRandall Stewart 			}
38575e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3858f8829a4aSRandall Stewart 				tp1->sent++;
38595e54f665SRandall Stewart 			}
3860b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3861f8829a4aSRandall Stewart 				/*
3862f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
3863f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
3864f8829a4aSRandall Stewart 				 * because it would have been set to the
3865f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
3866f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
3867f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
3868c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
3869c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3870c105859eSRandall Stewart 				 * indicates that two packets have been
3871c105859eSRandall Stewart 				 * received after this missing TSN.
3872f8829a4aSRandall Stewart 				 */
38735e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3874f8829a4aSRandall Stewart 				    compare_with_wrap(this_sack_lowest_newack, tp1->rec.data.TSN_seq, MAX_TSN)) {
3875b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3876f8829a4aSRandall Stewart 						sctp_log_fr(48 + num_dests_sacked,
3877f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3878f8829a4aSRandall Stewart 						    tp1->sent,
3879f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
388080fefe0aSRandall Stewart 					}
3881f8829a4aSRandall Stewart 					tp1->sent++;
3882f8829a4aSRandall Stewart 				}
3883f8829a4aSRandall Stewart 			}
3884f8829a4aSRandall Stewart 		}
3885f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3886f8829a4aSRandall Stewart 			struct sctp_nets *alt;
3887f8829a4aSRandall Stewart 
3888544e35bdSRandall Stewart 			/* fix counts and things */
3889544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3890544e35bdSRandall Stewart 				sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND,
3891544e35bdSRandall Stewart 				    (tp1->whoTo ? (tp1->whoTo->flight_size) : 0),
3892544e35bdSRandall Stewart 				    tp1->book_size,
3893544e35bdSRandall Stewart 				    (uintptr_t) tp1->whoTo,
3894544e35bdSRandall Stewart 				    tp1->rec.data.TSN_seq);
3895544e35bdSRandall Stewart 			}
3896544e35bdSRandall Stewart 			if (tp1->whoTo) {
3897544e35bdSRandall Stewart 				tp1->whoTo->net_ack++;
3898544e35bdSRandall Stewart 				sctp_flight_size_decrease(tp1);
3899544e35bdSRandall Stewart 			}
3900544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
3901544e35bdSRandall Stewart 				sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
3902544e35bdSRandall Stewart 				    asoc->peers_rwnd, tp1->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3903544e35bdSRandall Stewart 			}
3904544e35bdSRandall Stewart 			/* add back to the rwnd */
3905544e35bdSRandall Stewart 			asoc->peers_rwnd += (tp1->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3906544e35bdSRandall Stewart 
3907544e35bdSRandall Stewart 			/* remove from the total flight */
3908544e35bdSRandall Stewart 			sctp_total_flight_decrease(stcb, tp1);
3909544e35bdSRandall Stewart 
3910abe15ad6SRandall Stewart 			if ((stcb->asoc.peer_supports_prsctp) &&
3911abe15ad6SRandall Stewart 			    (PR_SCTP_RTX_ENABLED(tp1->flags))) {
3912abe15ad6SRandall Stewart 				/*
3913abe15ad6SRandall Stewart 				 * Has it been retransmitted tv_sec times? -
3914abe15ad6SRandall Stewart 				 * we store the retran count there.
3915abe15ad6SRandall Stewart 				 */
3916abe15ad6SRandall Stewart 				if (tp1->snd_count > tp1->rec.data.timetodrop.tv_sec) {
3917abe15ad6SRandall Stewart 					/* Yes, so drop it */
3918abe15ad6SRandall Stewart 					if (tp1->data != NULL) {
3919abe15ad6SRandall Stewart 						(void)sctp_release_pr_sctp_chunk(stcb, tp1,
3920abe15ad6SRandall Stewart 						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3921abe15ad6SRandall Stewart 						    SCTP_SO_NOT_LOCKED);
3922abe15ad6SRandall Stewart 					}
3923abe15ad6SRandall Stewart 					/* Make sure to flag we had a FR */
3924abe15ad6SRandall Stewart 					tp1->whoTo->net_ack++;
3925abe15ad6SRandall Stewart 					tp1 = TAILQ_NEXT(tp1, sctp_next);
3926abe15ad6SRandall Stewart 					continue;
3927abe15ad6SRandall Stewart 				}
3928abe15ad6SRandall Stewart 			}
3929f8829a4aSRandall Stewart 			/* printf("OK, we are now ready to FR this guy\n"); */
3930b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3931f8829a4aSRandall Stewart 				sctp_log_fr(tp1->rec.data.TSN_seq, tp1->snd_count,
3932f8829a4aSRandall Stewart 				    0, SCTP_FR_MARKED);
393380fefe0aSRandall Stewart 			}
3934f8829a4aSRandall Stewart 			if (strike_flag) {
3935f8829a4aSRandall Stewart 				/* This is a subsequent FR */
3936f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_sendmultfastretrans);
3937f8829a4aSRandall Stewart 			}
39385e54f665SRandall Stewart 			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3939b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
3940f8829a4aSRandall Stewart 				/*
3941f8829a4aSRandall Stewart 				 * CMT: Using RTX_SSTHRESH policy for CMT.
3942f8829a4aSRandall Stewart 				 * If CMT is being used, then pick dest with
3943f8829a4aSRandall Stewart 				 * largest ssthresh for any retransmission.
3944f8829a4aSRandall Stewart 				 */
3945f8829a4aSRandall Stewart 				tp1->no_fr_allowed = 1;
3946f8829a4aSRandall Stewart 				alt = tp1->whoTo;
39473c503c28SRandall Stewart 				/* sa_ignore NO_NULL_CHK */
3948b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
3949b54d3a6cSRandall Stewart 					/*
3950b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If CMT PF is on,
3951b54d3a6cSRandall Stewart 					 * use the PF version of
3952b54d3a6cSRandall Stewart 					 * find_alt_net()
3953b54d3a6cSRandall Stewart 					 */
3954b54d3a6cSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 2);
3955b54d3a6cSRandall Stewart 				} else {
3956b54d3a6cSRandall Stewart 					/*
3957b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If only CMT is on,
3958b54d3a6cSRandall Stewart 					 * use the CMT version of
3959b54d3a6cSRandall Stewart 					 * find_alt_net()
3960b54d3a6cSRandall Stewart 					 */
396152be287eSRandall Stewart 					/* sa_ignore NO_NULL_CHK */
3962f8829a4aSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 1);
3963b54d3a6cSRandall Stewart 				}
3964ad81507eSRandall Stewart 				if (alt == NULL) {
3965ad81507eSRandall Stewart 					alt = tp1->whoTo;
3966ad81507eSRandall Stewart 				}
3967f8829a4aSRandall Stewart 				/*
3968f8829a4aSRandall Stewart 				 * CUCv2: If a different dest is picked for
3969f8829a4aSRandall Stewart 				 * the retransmission, then new
3970f8829a4aSRandall Stewart 				 * (rtx-)pseudo_cumack needs to be tracked
3971f8829a4aSRandall Stewart 				 * for orig dest. Let CUCv2 track new (rtx-)
3972f8829a4aSRandall Stewart 				 * pseudo-cumack always.
3973f8829a4aSRandall Stewart 				 */
3974ad81507eSRandall Stewart 				if (tp1->whoTo) {
3975f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
3976f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
3977ad81507eSRandall Stewart 				}
3978f8829a4aSRandall Stewart 			} else {/* CMT is OFF */
3979f8829a4aSRandall Stewart 
3980f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3981f8829a4aSRandall Stewart 				/* Can we find an alternate? */
3982f8829a4aSRandall Stewart 				alt = sctp_find_alternate_net(stcb, tp1->whoTo, 0);
3983f8829a4aSRandall Stewart #else
3984f8829a4aSRandall Stewart 				/*
3985f8829a4aSRandall Stewart 				 * default behavior is to NOT retransmit
3986f8829a4aSRandall Stewart 				 * FR's to an alternate. Armando Caro's
3987f8829a4aSRandall Stewart 				 * paper details why.
3988f8829a4aSRandall Stewart 				 */
3989f8829a4aSRandall Stewart 				alt = tp1->whoTo;
3990f8829a4aSRandall Stewart #endif
3991f8829a4aSRandall Stewart 			}
3992f8829a4aSRandall Stewart 
3993f8829a4aSRandall Stewart 			tp1->rec.data.doing_fast_retransmit = 1;
3994f8829a4aSRandall Stewart 			tot_retrans++;
3995f8829a4aSRandall Stewart 			/* mark the sending seq for possible subsequent FR's */
3996f8829a4aSRandall Stewart 			/*
3997f8829a4aSRandall Stewart 			 * printf("Marking TSN for FR new value %x\n",
3998f8829a4aSRandall Stewart 			 * (uint32_t)tpi->rec.data.TSN_seq);
3999f8829a4aSRandall Stewart 			 */
4000f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->send_queue)) {
4001f8829a4aSRandall Stewart 				/*
4002f8829a4aSRandall Stewart 				 * If the queue of send is empty then its
4003f8829a4aSRandall Stewart 				 * the next sequence number that will be
4004f8829a4aSRandall Stewart 				 * assigned so we subtract one from this to
4005f8829a4aSRandall Stewart 				 * get the one we last sent.
4006f8829a4aSRandall Stewart 				 */
4007f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn = sending_seq;
4008f8829a4aSRandall Stewart 			} else {
4009f8829a4aSRandall Stewart 				/*
4010f8829a4aSRandall Stewart 				 * If there are chunks on the send queue
4011f8829a4aSRandall Stewart 				 * (unsent data that has made it from the
4012f8829a4aSRandall Stewart 				 * stream queues but not out the door, we
4013f8829a4aSRandall Stewart 				 * take the first one (which will have the
4014f8829a4aSRandall Stewart 				 * lowest TSN) and subtract one to get the
4015f8829a4aSRandall Stewart 				 * one we last sent.
4016f8829a4aSRandall Stewart 				 */
4017f8829a4aSRandall Stewart 				struct sctp_tmit_chunk *ttt;
4018f8829a4aSRandall Stewart 
4019f8829a4aSRandall Stewart 				ttt = TAILQ_FIRST(&asoc->send_queue);
4020f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn =
4021f8829a4aSRandall Stewart 				    ttt->rec.data.TSN_seq;
4022f8829a4aSRandall Stewart 			}
4023f8829a4aSRandall Stewart 
4024f8829a4aSRandall Stewart 			if (tp1->do_rtt) {
4025f8829a4aSRandall Stewart 				/*
4026f8829a4aSRandall Stewart 				 * this guy had a RTO calculation pending on
4027f8829a4aSRandall Stewart 				 * it, cancel it
4028f8829a4aSRandall Stewart 				 */
4029f8829a4aSRandall Stewart 				tp1->do_rtt = 0;
4030f8829a4aSRandall Stewart 			}
4031f8829a4aSRandall Stewart 			if (alt != tp1->whoTo) {
4032f8829a4aSRandall Stewart 				/* yes, there is an alternate. */
4033f8829a4aSRandall Stewart 				sctp_free_remote_addr(tp1->whoTo);
40343c503c28SRandall Stewart 				/* sa_ignore FREED_MEMORY */
4035f8829a4aSRandall Stewart 				tp1->whoTo = alt;
4036f8829a4aSRandall Stewart 				atomic_add_int(&alt->ref_count, 1);
4037f8829a4aSRandall Stewart 			}
4038f8829a4aSRandall Stewart 		}
4039f8829a4aSRandall Stewart 		tp1 = TAILQ_NEXT(tp1, sctp_next);
4040f8829a4aSRandall Stewart 	}			/* while (tp1) */
4041f8829a4aSRandall Stewart 
4042f8829a4aSRandall Stewart 	if (tot_retrans > 0) {
4043f8829a4aSRandall Stewart 		/*
4044f8829a4aSRandall Stewart 		 * Setup the ecn nonce re-sync point. We do this since once
4045f8829a4aSRandall Stewart 		 * we go to FR something we introduce a Karn's rule scenario
4046f8829a4aSRandall Stewart 		 * and won't know the totals for the ECN bits.
4047f8829a4aSRandall Stewart 		 */
4048f8829a4aSRandall Stewart 		asoc->nonce_resync_tsn = sending_seq;
4049f8829a4aSRandall Stewart 		asoc->nonce_wait_for_ecne = 0;
4050f8829a4aSRandall Stewart 		asoc->nonce_sum_check = 0;
4051f8829a4aSRandall Stewart 	}
4052f8829a4aSRandall Stewart }
4053f8829a4aSRandall Stewart 
4054f8829a4aSRandall Stewart struct sctp_tmit_chunk *
4055f8829a4aSRandall Stewart sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
4056f8829a4aSRandall Stewart     struct sctp_association *asoc)
4057f8829a4aSRandall Stewart {
4058f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2, *a_adv = NULL;
4059f8829a4aSRandall Stewart 	struct timeval now;
4060f8829a4aSRandall Stewart 	int now_filled = 0;
4061f8829a4aSRandall Stewart 
4062f8829a4aSRandall Stewart 	if (asoc->peer_supports_prsctp == 0) {
4063f8829a4aSRandall Stewart 		return (NULL);
4064f8829a4aSRandall Stewart 	}
4065f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
4066f8829a4aSRandall Stewart 	while (tp1) {
4067f8829a4aSRandall Stewart 		if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
4068f8829a4aSRandall Stewart 		    tp1->sent != SCTP_DATAGRAM_RESEND) {
4069f8829a4aSRandall Stewart 			/* no chance to advance, out of here */
4070f8829a4aSRandall Stewart 			break;
4071f8829a4aSRandall Stewart 		}
40720c0982b8SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
40730c0982b8SRandall Stewart 			if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
40740c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
40750c0982b8SRandall Stewart 				    asoc->advanced_peer_ack_point,
40760c0982b8SRandall Stewart 				    tp1->rec.data.TSN_seq, 0, 0);
40770c0982b8SRandall Stewart 			}
40780c0982b8SRandall Stewart 		}
4079f8829a4aSRandall Stewart 		if (!PR_SCTP_ENABLED(tp1->flags)) {
4080f8829a4aSRandall Stewart 			/*
4081f8829a4aSRandall Stewart 			 * We can't fwd-tsn past any that are reliable aka
4082f8829a4aSRandall Stewart 			 * retransmitted until the asoc fails.
4083f8829a4aSRandall Stewart 			 */
4084f8829a4aSRandall Stewart 			break;
4085f8829a4aSRandall Stewart 		}
4086f8829a4aSRandall Stewart 		if (!now_filled) {
40876e55db54SRandall Stewart 			(void)SCTP_GETTIME_TIMEVAL(&now);
4088f8829a4aSRandall Stewart 			now_filled = 1;
4089f8829a4aSRandall Stewart 		}
4090f8829a4aSRandall Stewart 		tp2 = TAILQ_NEXT(tp1, sctp_next);
4091f8829a4aSRandall Stewart 		/*
4092f8829a4aSRandall Stewart 		 * now we got a chunk which is marked for another
4093f8829a4aSRandall Stewart 		 * retransmission to a PR-stream but has run out its chances
4094f8829a4aSRandall Stewart 		 * already maybe OR has been marked to skip now. Can we skip
4095f8829a4aSRandall Stewart 		 * it if its a resend?
4096f8829a4aSRandall Stewart 		 */
4097f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND &&
4098f8829a4aSRandall Stewart 		    (PR_SCTP_TTL_ENABLED(tp1->flags))) {
4099f8829a4aSRandall Stewart 			/*
4100f8829a4aSRandall Stewart 			 * Now is this one marked for resend and its time is
4101f8829a4aSRandall Stewart 			 * now up?
4102f8829a4aSRandall Stewart 			 */
4103f8829a4aSRandall Stewart 			if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
4104f8829a4aSRandall Stewart 				/* Yes so drop it */
4105f8829a4aSRandall Stewart 				if (tp1->data) {
4106ad81507eSRandall Stewart 					(void)sctp_release_pr_sctp_chunk(stcb, tp1,
4107f8829a4aSRandall Stewart 					    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
41080c0982b8SRandall Stewart 					    SCTP_SO_NOT_LOCKED);
4109f8829a4aSRandall Stewart 				}
4110f8829a4aSRandall Stewart 			} else {
4111f8829a4aSRandall Stewart 				/*
4112f8829a4aSRandall Stewart 				 * No, we are done when hit one for resend
4113f8829a4aSRandall Stewart 				 * whos time as not expired.
4114f8829a4aSRandall Stewart 				 */
4115f8829a4aSRandall Stewart 				break;
4116f8829a4aSRandall Stewart 			}
4117f8829a4aSRandall Stewart 		}
4118f8829a4aSRandall Stewart 		/*
4119f8829a4aSRandall Stewart 		 * Ok now if this chunk is marked to drop it we can clean up
4120f8829a4aSRandall Stewart 		 * the chunk, advance our peer ack point and we can check
4121f8829a4aSRandall Stewart 		 * the next chunk.
4122f8829a4aSRandall Stewart 		 */
4123f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
4124f8829a4aSRandall Stewart 			/* advance PeerAckPoint goes forward */
41250c0982b8SRandall Stewart 			if (compare_with_wrap(tp1->rec.data.TSN_seq,
41260c0982b8SRandall Stewart 			    asoc->advanced_peer_ack_point,
41270c0982b8SRandall Stewart 			    MAX_TSN)) {
41280c0982b8SRandall Stewart 
4129f8829a4aSRandall Stewart 				asoc->advanced_peer_ack_point = tp1->rec.data.TSN_seq;
4130f8829a4aSRandall Stewart 				a_adv = tp1;
41310c0982b8SRandall Stewart 			} else if (tp1->rec.data.TSN_seq == asoc->advanced_peer_ack_point) {
41320c0982b8SRandall Stewart 				/* No update but we do save the chk */
41330c0982b8SRandall Stewart 				a_adv = tp1;
41340c0982b8SRandall Stewart 			}
4135f8829a4aSRandall Stewart 		} else {
4136f8829a4aSRandall Stewart 			/*
4137f8829a4aSRandall Stewart 			 * If it is still in RESEND we can advance no
4138f8829a4aSRandall Stewart 			 * further
4139f8829a4aSRandall Stewart 			 */
4140f8829a4aSRandall Stewart 			break;
4141f8829a4aSRandall Stewart 		}
4142f8829a4aSRandall Stewart 		/*
4143f8829a4aSRandall Stewart 		 * If we hit here we just dumped tp1, move to next tsn on
4144f8829a4aSRandall Stewart 		 * sent queue.
4145f8829a4aSRandall Stewart 		 */
4146f8829a4aSRandall Stewart 		tp1 = tp2;
4147f8829a4aSRandall Stewart 	}
4148f8829a4aSRandall Stewart 	return (a_adv);
4149f8829a4aSRandall Stewart }
4150f8829a4aSRandall Stewart 
41510c0982b8SRandall Stewart static int
4152c105859eSRandall Stewart sctp_fs_audit(struct sctp_association *asoc)
4153bff64a4dSRandall Stewart {
4154bff64a4dSRandall Stewart 	struct sctp_tmit_chunk *chk;
4155bff64a4dSRandall Stewart 	int inflight = 0, resend = 0, inbetween = 0, acked = 0, above = 0;
41560c0982b8SRandall Stewart 	int entry_flight, entry_cnt, ret;
41570c0982b8SRandall Stewart 
41580c0982b8SRandall Stewart 	entry_flight = asoc->total_flight;
41590c0982b8SRandall Stewart 	entry_cnt = asoc->total_flight_count;
41600c0982b8SRandall Stewart 	ret = 0;
41610c0982b8SRandall Stewart 
41620c0982b8SRandall Stewart 	if (asoc->pr_sctp_cnt >= asoc->sent_queue_cnt)
41630c0982b8SRandall Stewart 		return (0);
4164bff64a4dSRandall Stewart 
4165bff64a4dSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
4166bff64a4dSRandall Stewart 		if (chk->sent < SCTP_DATAGRAM_RESEND) {
41670c0982b8SRandall Stewart 			printf("Chk TSN:%u size:%d inflight cnt:%d\n",
41680c0982b8SRandall Stewart 			    chk->rec.data.TSN_seq,
41690c0982b8SRandall Stewart 			    chk->send_size,
41700c0982b8SRandall Stewart 			    chk->snd_count
41710c0982b8SRandall Stewart 			    );
4172bff64a4dSRandall Stewart 			inflight++;
4173bff64a4dSRandall Stewart 		} else if (chk->sent == SCTP_DATAGRAM_RESEND) {
4174bff64a4dSRandall Stewart 			resend++;
4175bff64a4dSRandall Stewart 		} else if (chk->sent < SCTP_DATAGRAM_ACKED) {
4176bff64a4dSRandall Stewart 			inbetween++;
4177bff64a4dSRandall Stewart 		} else if (chk->sent > SCTP_DATAGRAM_ACKED) {
4178bff64a4dSRandall Stewart 			above++;
4179bff64a4dSRandall Stewart 		} else {
4180bff64a4dSRandall Stewart 			acked++;
4181bff64a4dSRandall Stewart 		}
4182bff64a4dSRandall Stewart 	}
4183f1f73e57SRandall Stewart 
4184c105859eSRandall Stewart 	if ((inflight > 0) || (inbetween > 0)) {
4185f1f73e57SRandall Stewart #ifdef INVARIANTS
4186c105859eSRandall Stewart 		panic("Flight size-express incorrect? \n");
4187f1f73e57SRandall Stewart #else
41880c0982b8SRandall Stewart 		printf("asoc->total_flight:%d cnt:%d\n",
41890c0982b8SRandall Stewart 		    entry_flight, entry_cnt);
41900c0982b8SRandall Stewart 
41910c0982b8SRandall Stewart 		SCTP_PRINTF("Flight size-express incorrect F:%d I:%d R:%d Ab:%d ACK:%d\n",
41920c0982b8SRandall Stewart 		    inflight, inbetween, resend, above, acked);
41930c0982b8SRandall Stewart 		ret = 1;
4194f1f73e57SRandall Stewart #endif
4195bff64a4dSRandall Stewart 	}
41960c0982b8SRandall Stewart 	return (ret);
4197c105859eSRandall Stewart }
4198c105859eSRandall Stewart 
4199c105859eSRandall Stewart 
4200c105859eSRandall Stewart static void
4201c105859eSRandall Stewart sctp_window_probe_recovery(struct sctp_tcb *stcb,
4202c105859eSRandall Stewart     struct sctp_association *asoc,
4203c105859eSRandall Stewart     struct sctp_nets *net,
4204c105859eSRandall Stewart     struct sctp_tmit_chunk *tp1)
4205c105859eSRandall Stewart {
4206dfb11ef8SRandall Stewart 	tp1->window_probe = 0;
42075171328bSRandall Stewart 	if ((tp1->sent >= SCTP_DATAGRAM_ACKED) || (tp1->data == NULL)) {
4208dfb11ef8SRandall Stewart 		/* TSN's skipped we do NOT move back. */
4209dfb11ef8SRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DWN_WP_FWD,
4210dfb11ef8SRandall Stewart 		    tp1->whoTo->flight_size,
4211dfb11ef8SRandall Stewart 		    tp1->book_size,
4212dfb11ef8SRandall Stewart 		    (uintptr_t) tp1->whoTo,
4213dfb11ef8SRandall Stewart 		    tp1->rec.data.TSN_seq);
4214dfb11ef8SRandall Stewart 		return;
4215dfb11ef8SRandall Stewart 	}
42165171328bSRandall Stewart 	/* First setup this by shrinking flight */
42175171328bSRandall Stewart 	sctp_flight_size_decrease(tp1);
42185171328bSRandall Stewart 	sctp_total_flight_decrease(stcb, tp1);
42195171328bSRandall Stewart 	/* Now mark for resend */
42205171328bSRandall Stewart 	tp1->sent = SCTP_DATAGRAM_RESEND;
42215171328bSRandall Stewart 	asoc->sent_queue_retran_cnt++;
4222b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4223c105859eSRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_WP,
4224c105859eSRandall Stewart 		    tp1->whoTo->flight_size,
4225c105859eSRandall Stewart 		    tp1->book_size,
4226c105859eSRandall Stewart 		    (uintptr_t) tp1->whoTo,
4227c105859eSRandall Stewart 		    tp1->rec.data.TSN_seq);
422880fefe0aSRandall Stewart 	}
4229c105859eSRandall Stewart }
4230c105859eSRandall Stewart 
4231f8829a4aSRandall Stewart void
4232f8829a4aSRandall Stewart sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack,
4233f8829a4aSRandall Stewart     uint32_t rwnd, int nonce_sum_flag, int *abort_now)
4234f8829a4aSRandall Stewart {
4235f8829a4aSRandall Stewart 	struct sctp_nets *net;
4236f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4237f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
42385e54f665SRandall Stewart 	uint32_t old_rwnd;
42395e54f665SRandall Stewart 	int win_probe_recovery = 0;
4240c105859eSRandall Stewart 	int win_probe_recovered = 0;
4241d06c82f1SRandall Stewart 	int j, done_once = 0;
4242f8829a4aSRandall Stewart 
4243b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
4244d06c82f1SRandall Stewart 		sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
4245d06c82f1SRandall Stewart 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
424680fefe0aSRandall Stewart 	}
4247f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
424818e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
424918e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
425018e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
425118e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
425218e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
425318e198d3SRandall Stewart 	}
425418e198d3SRandall Stewart #endif
4255f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
4256d06c82f1SRandall Stewart 	old_rwnd = asoc->peers_rwnd;
42575e54f665SRandall Stewart 	if (compare_with_wrap(asoc->last_acked_seq, cumack, MAX_TSN)) {
42585e54f665SRandall Stewart 		/* old ack */
42595e54f665SRandall Stewart 		return;
4260d06c82f1SRandall Stewart 	} else if (asoc->last_acked_seq == cumack) {
4261d06c82f1SRandall Stewart 		/* Window update sack */
4262d06c82f1SRandall Stewart 		asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
4263b3f1ea41SRandall Stewart 		    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4264d06c82f1SRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4265d06c82f1SRandall Stewart 			/* SWS sender side engages */
4266d06c82f1SRandall Stewart 			asoc->peers_rwnd = 0;
4267d06c82f1SRandall Stewart 		}
4268d06c82f1SRandall Stewart 		if (asoc->peers_rwnd > old_rwnd) {
4269d06c82f1SRandall Stewart 			goto again;
4270d06c82f1SRandall Stewart 		}
4271d06c82f1SRandall Stewart 		return;
42725e54f665SRandall Stewart 	}
4273f8829a4aSRandall Stewart 	/* First setup for CC stuff */
4274f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4275f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
4276f8829a4aSRandall Stewart 		net->net_ack = 0;
4277f8829a4aSRandall Stewart 		net->net_ack2 = 0;
4278132dea7dSRandall Stewart 
4279132dea7dSRandall Stewart 		/*
4280132dea7dSRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
4281132dea7dSRandall Stewart 		 * SACK processing
4282132dea7dSRandall Stewart 		 */
4283132dea7dSRandall Stewart 		net->new_pseudo_cumack = 0;
4284132dea7dSRandall Stewart 		net->will_exit_fast_recovery = 0;
4285f8829a4aSRandall Stewart 	}
4286b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4287139bc87fSRandall Stewart 		uint32_t send_s;
4288139bc87fSRandall Stewart 
4289c105859eSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4290c105859eSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
4291c105859eSRandall Stewart 			    sctpchunk_listhead);
4292c105859eSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
4293139bc87fSRandall Stewart 		} else {
4294c105859eSRandall Stewart 			send_s = asoc->sending_seq;
4295139bc87fSRandall Stewart 		}
4296139bc87fSRandall Stewart 		if ((cumack == send_s) ||
4297139bc87fSRandall Stewart 		    compare_with_wrap(cumack, send_s, MAX_TSN)) {
4298c105859eSRandall Stewart #ifndef INVARIANTS
4299139bc87fSRandall Stewart 			struct mbuf *oper;
4300139bc87fSRandall Stewart 
4301c105859eSRandall Stewart #endif
4302c105859eSRandall Stewart #ifdef INVARIANTS
4303c105859eSRandall Stewart 			panic("Impossible sack 1");
4304c105859eSRandall Stewart #else
4305139bc87fSRandall Stewart 			*abort_now = 1;
4306139bc87fSRandall Stewart 			/* XXX */
4307139bc87fSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4308139bc87fSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
4309139bc87fSRandall Stewart 			if (oper) {
4310139bc87fSRandall Stewart 				struct sctp_paramhdr *ph;
4311139bc87fSRandall Stewart 				uint32_t *ippp;
4312139bc87fSRandall Stewart 
4313139bc87fSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4314139bc87fSRandall Stewart 				    sizeof(uint32_t);
4315139bc87fSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
4316139bc87fSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4317139bc87fSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
4318139bc87fSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
4319139bc87fSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
4320139bc87fSRandall Stewart 			}
4321139bc87fSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4322ceaad40aSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
4323139bc87fSRandall Stewart 			return;
4324139bc87fSRandall Stewart #endif
4325139bc87fSRandall Stewart 		}
4326139bc87fSRandall Stewart 	}
4327f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = cumack;
4328b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4329c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4330c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
4331c4739e2fSRandall Stewart 		    0,
4332c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
4333c4739e2fSRandall Stewart 		    __LINE__);
4334c4739e2fSRandall Stewart 	}
4335f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
43365e54f665SRandall Stewart 	if (compare_with_wrap(cumack, asoc->last_acked_seq, MAX_TSN)) {
4337f8829a4aSRandall Stewart 		/* process the new consecutive TSN first */
4338f8829a4aSRandall Stewart 		tp1 = TAILQ_FIRST(&asoc->sent_queue);
4339f8829a4aSRandall Stewart 		while (tp1) {
4340f8829a4aSRandall Stewart 			tp2 = TAILQ_NEXT(tp1, sctp_next);
4341f8829a4aSRandall Stewart 			if (compare_with_wrap(cumack, tp1->rec.data.TSN_seq,
4342f8829a4aSRandall Stewart 			    MAX_TSN) ||
4343f8829a4aSRandall Stewart 			    cumack == tp1->rec.data.TSN_seq) {
434418e198d3SRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
434518e198d3SRandall Stewart 					printf("Warning, an unsent is now acked?\n");
434618e198d3SRandall Stewart 				}
4347f8829a4aSRandall Stewart 				/*
434818e198d3SRandall Stewart 				 * ECN Nonce: Add the nonce to the sender's
434918e198d3SRandall Stewart 				 * nonce sum
4350f8829a4aSRandall Stewart 				 */
4351f8829a4aSRandall Stewart 				asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
4352f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4353f8829a4aSRandall Stewart 					/*
435418e198d3SRandall Stewart 					 * If it is less than ACKED, it is
435518e198d3SRandall Stewart 					 * now no-longer in flight. Higher
435618e198d3SRandall Stewart 					 * values may occur during marking
4357f8829a4aSRandall Stewart 					 */
4358c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4359b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4360c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4361a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
4362a5d547adSRandall Stewart 							    tp1->book_size,
4363c105859eSRandall Stewart 							    (uintptr_t) tp1->whoTo,
4364a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
436580fefe0aSRandall Stewart 						}
4366c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
436704ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
4368c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
4369f8829a4aSRandall Stewart 					}
4370f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
4371f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
4372f8829a4aSRandall Stewart 						/*
437318e198d3SRandall Stewart 						 * True non-retransmited
4374f8829a4aSRandall Stewart 						 * chunk
4375f8829a4aSRandall Stewart 						 */
4376f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
4377f8829a4aSRandall Stewart 						    tp1->send_size;
4378f8829a4aSRandall Stewart 
4379f8829a4aSRandall Stewart 						/* update RTO too? */
438062c1ff9cSRandall Stewart 						if (tp1->do_rtt) {
4381f8829a4aSRandall Stewart 							tp1->whoTo->RTO =
438204ee05e8SRandall Stewart 							/*
438304ee05e8SRandall Stewart 							 * sa_ignore
438404ee05e8SRandall Stewart 							 * NO_NULL_CHK
438504ee05e8SRandall Stewart 							 */
4386f8829a4aSRandall Stewart 							    sctp_calculate_rto(stcb,
4387f8829a4aSRandall Stewart 							    asoc, tp1->whoTo,
438818e198d3SRandall Stewart 							    &tp1->sent_rcv_time,
438918e198d3SRandall Stewart 							    sctp_align_safe_nocopy);
4390f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
4391f8829a4aSRandall Stewart 						}
4392f8829a4aSRandall Stewart 					}
4393132dea7dSRandall Stewart 					/*
439418e198d3SRandall Stewart 					 * CMT: CUCv2 algorithm. From the
439518e198d3SRandall Stewart 					 * cumack'd TSNs, for each TSN being
439618e198d3SRandall Stewart 					 * acked for the first time, set the
439718e198d3SRandall Stewart 					 * following variables for the
439818e198d3SRandall Stewart 					 * corresp destination.
439918e198d3SRandall Stewart 					 * new_pseudo_cumack will trigger a
440018e198d3SRandall Stewart 					 * cwnd update.
440118e198d3SRandall Stewart 					 * find_(rtx_)pseudo_cumack will
440218e198d3SRandall Stewart 					 * trigger search for the next
440318e198d3SRandall Stewart 					 * expected (rtx-)pseudo-cumack.
4404132dea7dSRandall Stewart 					 */
4405132dea7dSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
4406132dea7dSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
4407132dea7dSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
4408132dea7dSRandall Stewart 
4409b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
441004ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
4411f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
441280fefe0aSRandall Stewart 					}
4413f8829a4aSRandall Stewart 				}
4414f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4415f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4416f8829a4aSRandall Stewart 				}
441742551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
441842551e99SRandall Stewart 					/* deflate the cwnd */
441942551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
442042551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
442142551e99SRandall Stewart 				}
4422f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
4423f8829a4aSRandall Stewart 				TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4424f8829a4aSRandall Stewart 				if (tp1->data) {
442504ee05e8SRandall Stewart 					/* sa_ignore NO_NULL_CHK */
4426f8829a4aSRandall Stewart 					sctp_free_bufspace(stcb, asoc, tp1, 1);
4427f8829a4aSRandall Stewart 					sctp_m_freem(tp1->data);
4428f8829a4aSRandall Stewart 				}
4429b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4430f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
4431f8829a4aSRandall Stewart 					    cumack,
4432f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
4433f8829a4aSRandall Stewart 					    0,
4434f8829a4aSRandall Stewart 					    0,
4435f8829a4aSRandall Stewart 					    SCTP_LOG_FREE_SENT);
443680fefe0aSRandall Stewart 				}
4437f8829a4aSRandall Stewart 				tp1->data = NULL;
4438f8829a4aSRandall Stewart 				asoc->sent_queue_cnt--;
4439f8829a4aSRandall Stewart 				sctp_free_a_chunk(stcb, tp1);
4440f8829a4aSRandall Stewart 				tp1 = tp2;
444118e198d3SRandall Stewart 			} else {
444218e198d3SRandall Stewart 				break;
4443f8829a4aSRandall Stewart 			}
44445e54f665SRandall Stewart 		}
444518e198d3SRandall Stewart 
444618e198d3SRandall Stewart 	}
444704ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
4448f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
4449ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4450ceaad40aSRandall Stewart 		struct socket *so;
4451ceaad40aSRandall Stewart 
4452ceaad40aSRandall Stewart #endif
4453ceaad40aSRandall Stewart 
4454f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4455b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
445604ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
4457f8829a4aSRandall Stewart 			sctp_wakeup_log(stcb, cumack, 1, SCTP_WAKESND_FROM_SACK);
445880fefe0aSRandall Stewart 		}
4459ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4460ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
4461ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
4462ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
4463ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
4464ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
4465ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4466ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4467ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
4468ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
4469ceaad40aSRandall Stewart 			return;
4470ceaad40aSRandall Stewart 		}
4471ceaad40aSRandall Stewart #endif
4472f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4473ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4474ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4475ceaad40aSRandall Stewart #endif
4476f8829a4aSRandall Stewart 	} else {
4477b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4478f8829a4aSRandall Stewart 			sctp_wakeup_log(stcb, cumack, 1, SCTP_NOWAKE_FROM_SACK);
447980fefe0aSRandall Stewart 		}
4480f8829a4aSRandall Stewart 	}
4481f8829a4aSRandall Stewart 
4482b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
4483f8829a4aSRandall Stewart 	if (asoc->last_acked_seq != cumack)
4484b54d3a6cSRandall Stewart 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
44855e54f665SRandall Stewart 
4486f8829a4aSRandall Stewart 	asoc->last_acked_seq = cumack;
44875e54f665SRandall Stewart 
4488f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
4489f8829a4aSRandall Stewart 		/* nothing left in-flight */
4490f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4491f8829a4aSRandall Stewart 			net->flight_size = 0;
4492f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4493f8829a4aSRandall Stewart 		}
4494f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4495f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4496f8829a4aSRandall Stewart 	}
4497f8829a4aSRandall Stewart 	/* ECN Nonce updates */
4498f8829a4aSRandall Stewart 	if (asoc->ecn_nonce_allowed) {
4499f8829a4aSRandall Stewart 		if (asoc->nonce_sum_check) {
4500f8829a4aSRandall Stewart 			if (nonce_sum_flag != ((asoc->nonce_sum_expect_base) & SCTP_SACK_NONCE_SUM)) {
4501f8829a4aSRandall Stewart 				if (asoc->nonce_wait_for_ecne == 0) {
4502f8829a4aSRandall Stewart 					struct sctp_tmit_chunk *lchk;
4503f8829a4aSRandall Stewart 
4504f8829a4aSRandall Stewart 					lchk = TAILQ_FIRST(&asoc->send_queue);
4505f8829a4aSRandall Stewart 					asoc->nonce_wait_for_ecne = 1;
4506f8829a4aSRandall Stewart 					if (lchk) {
4507f8829a4aSRandall Stewart 						asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
4508f8829a4aSRandall Stewart 					} else {
4509f8829a4aSRandall Stewart 						asoc->nonce_wait_tsn = asoc->sending_seq;
4510f8829a4aSRandall Stewart 					}
4511f8829a4aSRandall Stewart 				} else {
4512f8829a4aSRandall Stewart 					if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
4513f8829a4aSRandall Stewart 					    (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
4514f8829a4aSRandall Stewart 						/*
4515f8829a4aSRandall Stewart 						 * Misbehaving peer. We need
4516f8829a4aSRandall Stewart 						 * to react to this guy
4517f8829a4aSRandall Stewart 						 */
4518f8829a4aSRandall Stewart 						asoc->ecn_allowed = 0;
4519f8829a4aSRandall Stewart 						asoc->ecn_nonce_allowed = 0;
4520f8829a4aSRandall Stewart 					}
4521f8829a4aSRandall Stewart 				}
4522f8829a4aSRandall Stewart 			}
4523f8829a4aSRandall Stewart 		} else {
4524f8829a4aSRandall Stewart 			/* See if Resynchronization Possible */
4525f8829a4aSRandall Stewart 			if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
4526f8829a4aSRandall Stewart 				asoc->nonce_sum_check = 1;
4527f8829a4aSRandall Stewart 				/*
4528f8829a4aSRandall Stewart 				 * now we must calculate what the base is.
4529f8829a4aSRandall Stewart 				 * We do this based on two things, we know
4530f8829a4aSRandall Stewart 				 * the total's for all the segments
4531f8829a4aSRandall Stewart 				 * gap-acked in the SACK (none), We also
4532f8829a4aSRandall Stewart 				 * know the SACK's nonce sum, its in
4533f8829a4aSRandall Stewart 				 * nonce_sum_flag. So we can build a truth
4534f8829a4aSRandall Stewart 				 * table to back-calculate the new value of
4535f8829a4aSRandall Stewart 				 * asoc->nonce_sum_expect_base:
4536f8829a4aSRandall Stewart 				 *
4537f8829a4aSRandall Stewart 				 * SACK-flag-Value         Seg-Sums Base 0 0 0
4538f8829a4aSRandall Stewart 				 * 1                    0 1 0 1 1 1
4539f8829a4aSRandall Stewart 				 * 1 0
4540f8829a4aSRandall Stewart 				 */
4541f8829a4aSRandall Stewart 				asoc->nonce_sum_expect_base = (0 ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
4542f8829a4aSRandall Stewart 			}
4543f8829a4aSRandall Stewart 		}
4544f8829a4aSRandall Stewart 	}
4545f8829a4aSRandall Stewart 	/* RWND update */
4546f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
4547b3f1ea41SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4548f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4549f8829a4aSRandall Stewart 		/* SWS sender side engages */
4550f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
4551f8829a4aSRandall Stewart 	}
45525e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
45535e54f665SRandall Stewart 		win_probe_recovery = 1;
45545e54f665SRandall Stewart 	}
4555f8829a4aSRandall Stewart 	/* Now assure a timer where data is queued at */
4556a5d547adSRandall Stewart again:
4557a5d547adSRandall Stewart 	j = 0;
4558f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
45595171328bSRandall Stewart 		int to_ticks;
45605171328bSRandall Stewart 
45615e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
4562c105859eSRandall Stewart 			win_probe_recovered = 1;
45635e54f665SRandall Stewart 			/*
45645e54f665SRandall Stewart 			 * Find first chunk that was used with window probe
45655e54f665SRandall Stewart 			 * and clear the sent
45665e54f665SRandall Stewart 			 */
45673c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
45685e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
45695e54f665SRandall Stewart 				if (tp1->window_probe) {
4570c105859eSRandall Stewart 					sctp_window_probe_recovery(stcb, asoc, net, tp1);
45715e54f665SRandall Stewart 					break;
45725e54f665SRandall Stewart 				}
45735e54f665SRandall Stewart 			}
45745e54f665SRandall Stewart 		}
4575f8829a4aSRandall Stewart 		if (net->RTO == 0) {
4576f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
4577f8829a4aSRandall Stewart 		} else {
4578f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
4579f8829a4aSRandall Stewart 		}
45805171328bSRandall Stewart 		if (net->flight_size) {
4581a5d547adSRandall Stewart 			j++;
4582ad81507eSRandall Stewart 			(void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
4583f8829a4aSRandall Stewart 			    sctp_timeout_handler, &net->rxt_timer);
45845171328bSRandall Stewart 			if (net->window_probe) {
45855171328bSRandall Stewart 				net->window_probe = 0;
45865171328bSRandall Stewart 			}
4587f8829a4aSRandall Stewart 		} else {
45885171328bSRandall Stewart 			if (net->window_probe) {
45895171328bSRandall Stewart 				/*
45905171328bSRandall Stewart 				 * In window probes we must assure a timer
45915171328bSRandall Stewart 				 * is still running there
45925171328bSRandall Stewart 				 */
45935171328bSRandall Stewart 				net->window_probe = 0;
45945171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
45955171328bSRandall Stewart 					SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
45965171328bSRandall Stewart 					    sctp_timeout_handler, &net->rxt_timer);
45975171328bSRandall Stewart 				}
45985171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4599f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4600a5d547adSRandall Stewart 				    stcb, net,
4601a5d547adSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
4602f8829a4aSRandall Stewart 			}
4603b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
4604139bc87fSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4605f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
4606a5d547adSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4607a5d547adSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
4608f8829a4aSRandall Stewart 				}
4609f8829a4aSRandall Stewart 			}
4610f8829a4aSRandall Stewart 		}
4611f8829a4aSRandall Stewart 	}
4612bff64a4dSRandall Stewart 	if ((j == 0) &&
4613bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
4614bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
4615c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
4616bff64a4dSRandall Stewart 	    (done_once == 0)) {
46170c0982b8SRandall Stewart 		/*
46180c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
46190c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
46200c0982b8SRandall Stewart 		 */
46210c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
4622a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
46230c0982b8SRandall Stewart 				if (net->flight_size) {
4624a5d547adSRandall Stewart 					net->flight_size = 0;
4625a5d547adSRandall Stewart 				}
46260c0982b8SRandall Stewart 			}
4627a5d547adSRandall Stewart 			asoc->total_flight = 0;
4628a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
4629a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4630a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4631a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4632c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
4633c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
4634a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4635a5d547adSRandall Stewart 					asoc->sent_queue_retran_cnt++;
4636a5d547adSRandall Stewart 				}
4637a5d547adSRandall Stewart 			}
46380c0982b8SRandall Stewart 		}
4639bff64a4dSRandall Stewart 		done_once = 1;
4640a5d547adSRandall Stewart 		goto again;
4641a5d547adSRandall Stewart 	}
4642f8829a4aSRandall Stewart 	/**********************************/
4643f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
4644f8829a4aSRandall Stewart 	/**********************************/
4645f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4646f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
4647f8829a4aSRandall Stewart 		/* clean up */
4648f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
4649f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4650f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4651f8829a4aSRandall Stewart 		    (asoc->locked_on_sending)
4652f8829a4aSRandall Stewart 		    ) {
4653f8829a4aSRandall Stewart 			struct sctp_stream_queue_pending *sp;
4654f8829a4aSRandall Stewart 
4655f8829a4aSRandall Stewart 			/*
4656f8829a4aSRandall Stewart 			 * I may be in a state where we got all across.. but
4657f8829a4aSRandall Stewart 			 * cannot write more due to a shutdown... we abort
4658f8829a4aSRandall Stewart 			 * since the user did not indicate EOR in this case.
4659f8829a4aSRandall Stewart 			 * The sp will be cleaned during free of the asoc.
4660f8829a4aSRandall Stewart 			 */
4661f8829a4aSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4662f8829a4aSRandall Stewart 			    sctp_streamhead);
46632afb3e84SRandall Stewart 			if ((sp) && (sp->length == 0)) {
46642afb3e84SRandall Stewart 				/* Let cleanup code purge it */
46652afb3e84SRandall Stewart 				if (sp->msg_is_complete) {
46662afb3e84SRandall Stewart 					asoc->stream_queue_cnt--;
46672afb3e84SRandall Stewart 				} else {
4668f8829a4aSRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
4669f8829a4aSRandall Stewart 					asoc->locked_on_sending = NULL;
4670f8829a4aSRandall Stewart 					asoc->stream_queue_cnt--;
4671f8829a4aSRandall Stewart 				}
4672f8829a4aSRandall Stewart 			}
46732afb3e84SRandall Stewart 		}
4674f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4675f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4676f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4677f8829a4aSRandall Stewart 				/* Need to abort here */
4678f8829a4aSRandall Stewart 				struct mbuf *oper;
4679f8829a4aSRandall Stewart 
4680f8829a4aSRandall Stewart 		abort_out_now:
4681f8829a4aSRandall Stewart 				*abort_now = 1;
4682f8829a4aSRandall Stewart 				/* XXX */
4683f8829a4aSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4684f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
4685f8829a4aSRandall Stewart 				if (oper) {
4686f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
4687f8829a4aSRandall Stewart 					uint32_t *ippp;
4688f8829a4aSRandall Stewart 
4689139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4690f8829a4aSRandall Stewart 					    sizeof(uint32_t);
4691f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
4692f8829a4aSRandall Stewart 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4693139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
4694f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
4695a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_24);
4696f8829a4aSRandall Stewart 				}
4697a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
4698ceaad40aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
4699f8829a4aSRandall Stewart 			} else {
4700f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4701f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4702f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4703f42a358aSRandall Stewart 				}
4704c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4705b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4706f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
4707f8829a4aSRandall Stewart 				sctp_send_shutdown(stcb,
4708f8829a4aSRandall Stewart 				    stcb->asoc.primary_destination);
4709f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4710f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
4711f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4712f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
4713f8829a4aSRandall Stewart 			}
4714f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4715f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4716f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4717f8829a4aSRandall Stewart 				goto abort_out_now;
4718f8829a4aSRandall Stewart 			}
4719f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4720c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4721b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4722f8829a4aSRandall Stewart 			sctp_send_shutdown_ack(stcb,
4723f8829a4aSRandall Stewart 			    stcb->asoc.primary_destination);
4724f8829a4aSRandall Stewart 
4725f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4726f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb, asoc->primary_destination);
4727f8829a4aSRandall Stewart 		}
4728f8829a4aSRandall Stewart 	}
4729dfb11ef8SRandall Stewart 	/*********************************************/
4730dfb11ef8SRandall Stewart 	/* Here we perform PR-SCTP procedures        */
4731dfb11ef8SRandall Stewart 	/* (section 4.2)                             */
4732dfb11ef8SRandall Stewart 	/*********************************************/
4733dfb11ef8SRandall Stewart 	/* C1. update advancedPeerAckPoint */
4734dfb11ef8SRandall Stewart 	if (compare_with_wrap(cumack, asoc->advanced_peer_ack_point, MAX_TSN)) {
4735dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cumack;
4736dfb11ef8SRandall Stewart 	}
4737830d754dSRandall Stewart 	/* PR-Sctp issues need to be addressed too */
4738830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
4739830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
4740830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
4741830d754dSRandall Stewart 
4742830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
4743830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
4744830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
4745830d754dSRandall Stewart 		if (compare_with_wrap(asoc->advanced_peer_ack_point, cumack,
4746830d754dSRandall Stewart 		    MAX_TSN)) {
4747830d754dSRandall Stewart 			/*
4748830d754dSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing for notes
4749830d754dSRandall Stewart 			 * on issues that will occur when the ECN NONCE
4750830d754dSRandall Stewart 			 * stuff is put into SCTP for cross checking.
4751830d754dSRandall Stewart 			 */
4752830d754dSRandall Stewart 			if (compare_with_wrap(asoc->advanced_peer_ack_point, old_adv_peer_ack_point,
4753830d754dSRandall Stewart 			    MAX_TSN)) {
4754830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
4755830d754dSRandall Stewart 				/*
4756830d754dSRandall Stewart 				 * ECN Nonce: Disable Nonce Sum check when
4757830d754dSRandall Stewart 				 * FWD TSN is sent and store resync tsn
4758830d754dSRandall Stewart 				 */
4759830d754dSRandall Stewart 				asoc->nonce_sum_check = 0;
4760830d754dSRandall Stewart 				asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
47610c0982b8SRandall Stewart 			} else if (lchk) {
47620c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
47630c0982b8SRandall Stewart 				lchk->rec.data.fwd_tsn_cnt++;
47640c0982b8SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt > 3) {
47650c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
47660c0982b8SRandall Stewart 					lchk->rec.data.fwd_tsn_cnt = 0;
47670c0982b8SRandall Stewart 				}
4768830d754dSRandall Stewart 			}
4769830d754dSRandall Stewart 		}
4770830d754dSRandall Stewart 		if (lchk) {
4771830d754dSRandall Stewart 			/* Assure a timer is up */
4772830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4773830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
4774830d754dSRandall Stewart 		}
4775830d754dSRandall Stewart 	}
4776b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
4777f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4778f8829a4aSRandall Stewart 		    rwnd,
4779f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
4780f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
4781f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
478280fefe0aSRandall Stewart 	}
4783f8829a4aSRandall Stewart }
4784f8829a4aSRandall Stewart 
4785f8829a4aSRandall Stewart void
4786458303daSRandall Stewart sctp_handle_sack(struct mbuf *m, int offset,
4787458303daSRandall Stewart     struct sctp_sack_chunk *ch, struct sctp_tcb *stcb,
4788d06c82f1SRandall Stewart     struct sctp_nets *net_from, int *abort_now, int sack_len, uint32_t rwnd)
4789f8829a4aSRandall Stewart {
4790f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4791f8829a4aSRandall Stewart 	struct sctp_sack *sack;
4792f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
4793f8829a4aSRandall Stewart 	uint32_t cum_ack, last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked,
4794f8829a4aSRandall Stewart 	         this_sack_lowest_newack;
4795bff64a4dSRandall Stewart 	uint32_t sav_cum_ack;
4796f8829a4aSRandall Stewart 	uint16_t num_seg, num_dup;
4797f8829a4aSRandall Stewart 	uint16_t wake_him = 0;
4798f8829a4aSRandall Stewart 	unsigned int sack_length;
4799c105859eSRandall Stewart 	uint32_t send_s = 0;
4800f8829a4aSRandall Stewart 	long j;
4801f8829a4aSRandall Stewart 	int accum_moved = 0;
4802f8829a4aSRandall Stewart 	int will_exit_fast_recovery = 0;
48035e54f665SRandall Stewart 	uint32_t a_rwnd, old_rwnd;
48045e54f665SRandall Stewart 	int win_probe_recovery = 0;
4805c105859eSRandall Stewart 	int win_probe_recovered = 0;
4806f8829a4aSRandall Stewart 	struct sctp_nets *net = NULL;
4807f8829a4aSRandall Stewart 	int nonce_sum_flag, ecn_seg_sums = 0;
4808bff64a4dSRandall Stewart 	int done_once;
4809f8829a4aSRandall Stewart 	uint8_t reneged_all = 0;
4810f8829a4aSRandall Stewart 	uint8_t cmt_dac_flag;
4811f8829a4aSRandall Stewart 
4812f8829a4aSRandall Stewart 	/*
4813f8829a4aSRandall Stewart 	 * we take any chance we can to service our queues since we cannot
4814f8829a4aSRandall Stewart 	 * get awoken when the socket is read from :<
4815f8829a4aSRandall Stewart 	 */
4816f8829a4aSRandall Stewart 	/*
4817f8829a4aSRandall Stewart 	 * Now perform the actual SACK handling: 1) Verify that it is not an
4818f8829a4aSRandall Stewart 	 * old sack, if so discard. 2) If there is nothing left in the send
4819f8829a4aSRandall Stewart 	 * queue (cum-ack is equal to last acked) then you have a duplicate
4820f8829a4aSRandall Stewart 	 * too, update any rwnd change and verify no timers are running.
4821f8829a4aSRandall Stewart 	 * then return. 3) Process any new consequtive data i.e. cum-ack
4822f8829a4aSRandall Stewart 	 * moved process these first and note that it moved. 4) Process any
4823f8829a4aSRandall Stewart 	 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
4824f8829a4aSRandall Stewart 	 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
4825f8829a4aSRandall Stewart 	 * sync up flightsizes and things, stop all timers and also check
4826f8829a4aSRandall Stewart 	 * for shutdown_pending state. If so then go ahead and send off the
4827f8829a4aSRandall Stewart 	 * shutdown. If in shutdown recv, send off the shutdown-ack and
4828f8829a4aSRandall Stewart 	 * start that timer, Ret. 9) Strike any non-acked things and do FR
4829f8829a4aSRandall Stewart 	 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
4830f8829a4aSRandall Stewart 	 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
4831f8829a4aSRandall Stewart 	 * if in shutdown_recv state.
4832f8829a4aSRandall Stewart 	 */
4833f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
4834f8829a4aSRandall Stewart 	sack = &ch->sack;
4835f8829a4aSRandall Stewart 	/* CMT DAC algo */
4836f8829a4aSRandall Stewart 	this_sack_lowest_newack = 0;
4837f8829a4aSRandall Stewart 	j = 0;
4838d06c82f1SRandall Stewart 	sack_length = (unsigned int)sack_len;
4839f8829a4aSRandall Stewart 	/* ECN Nonce */
4840f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_slowpath_sack);
4841f8829a4aSRandall Stewart 	nonce_sum_flag = ch->ch.chunk_flags & SCTP_SACK_NONCE_SUM;
4842f8829a4aSRandall Stewart 	cum_ack = last_tsn = ntohl(sack->cum_tsn_ack);
484318e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
484418e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
484518e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
484618e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
484718e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
484818e198d3SRandall Stewart 	}
484918e198d3SRandall Stewart #endif
4850f8829a4aSRandall Stewart 	num_seg = ntohs(sack->num_gap_ack_blks);
4851d06c82f1SRandall Stewart 	a_rwnd = rwnd;
4852f8829a4aSRandall Stewart 
4853f8829a4aSRandall Stewart 	/* CMT DAC algo */
4854f8829a4aSRandall Stewart 	cmt_dac_flag = ch->ch.chunk_flags & SCTP_SACK_CMT_DAC;
4855f8829a4aSRandall Stewart 	num_dup = ntohs(sack->num_dup_tsns);
4856f8829a4aSRandall Stewart 
48575e54f665SRandall Stewart 	old_rwnd = stcb->asoc.peers_rwnd;
4858b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4859c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4860c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
4861c4739e2fSRandall Stewart 		    0,
4862c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
4863c4739e2fSRandall Stewart 		    __LINE__);
4864c4739e2fSRandall Stewart 	}
4865f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
4866f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
4867b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4868f8829a4aSRandall Stewart 		sctp_log_sack(asoc->last_acked_seq,
4869f8829a4aSRandall Stewart 		    cum_ack,
4870f8829a4aSRandall Stewart 		    0,
4871f8829a4aSRandall Stewart 		    num_seg,
4872f8829a4aSRandall Stewart 		    num_dup,
4873f8829a4aSRandall Stewart 		    SCTP_LOG_NEW_SACK);
487480fefe0aSRandall Stewart 	}
4875b3f1ea41SRandall Stewart 	if ((num_dup) && (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_FR_LOGGING_ENABLE | SCTP_EARLYFR_LOGGING_ENABLE))) {
4876f8829a4aSRandall Stewart 		int off_to_dup, iii;
4877458303daSRandall Stewart 		uint32_t *dupdata, dblock;
4878f8829a4aSRandall Stewart 
4879f8829a4aSRandall Stewart 		off_to_dup = (num_seg * sizeof(struct sctp_gap_ack_block)) + sizeof(struct sctp_sack_chunk);
4880f8829a4aSRandall Stewart 		if ((off_to_dup + (num_dup * sizeof(uint32_t))) <= sack_length) {
4881458303daSRandall Stewart 			dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
4882458303daSRandall Stewart 			    sizeof(uint32_t), (uint8_t *) & dblock);
4883458303daSRandall Stewart 			off_to_dup += sizeof(uint32_t);
4884458303daSRandall Stewart 			if (dupdata) {
4885f8829a4aSRandall Stewart 				for (iii = 0; iii < num_dup; iii++) {
4886f8829a4aSRandall Stewart 					sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
4887458303daSRandall Stewart 					dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
4888458303daSRandall Stewart 					    sizeof(uint32_t), (uint8_t *) & dblock);
4889458303daSRandall Stewart 					if (dupdata == NULL)
4890458303daSRandall Stewart 						break;
4891458303daSRandall Stewart 					off_to_dup += sizeof(uint32_t);
4892458303daSRandall Stewart 				}
4893f8829a4aSRandall Stewart 			}
4894f8829a4aSRandall Stewart 		} else {
4895ad81507eSRandall Stewart 			SCTP_PRINTF("Size invalid offset to dups:%d number dups:%d sack_len:%d num gaps:%d\n",
4896f8829a4aSRandall Stewart 			    off_to_dup, num_dup, sack_length, num_seg);
4897f8829a4aSRandall Stewart 		}
4898f8829a4aSRandall Stewart 	}
4899b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4900c105859eSRandall Stewart 		/* reality check */
4901c105859eSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4902c105859eSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
4903c105859eSRandall Stewart 			    sctpchunk_listhead);
4904c105859eSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
4905c105859eSRandall Stewart 		} else {
4906c105859eSRandall Stewart 			send_s = asoc->sending_seq;
4907c105859eSRandall Stewart 		}
4908f8829a4aSRandall Stewart 		if (cum_ack == send_s ||
4909f8829a4aSRandall Stewart 		    compare_with_wrap(cum_ack, send_s, MAX_TSN)) {
4910c105859eSRandall Stewart #ifndef INVARIANTS
4911c105859eSRandall Stewart 			struct mbuf *oper;
4912c105859eSRandall Stewart 
4913c105859eSRandall Stewart #endif
4914c105859eSRandall Stewart #ifdef INVARIANTS
4915139bc87fSRandall Stewart 	hopeless_peer:
4916139bc87fSRandall Stewart 			panic("Impossible sack 1");
4917139bc87fSRandall Stewart #else
4918c105859eSRandall Stewart 
4919f8829a4aSRandall Stewart 
4920f8829a4aSRandall Stewart 			/*
4921f8829a4aSRandall Stewart 			 * no way, we have not even sent this TSN out yet.
4922f8829a4aSRandall Stewart 			 * Peer is hopelessly messed up with us.
4923f8829a4aSRandall Stewart 			 */
4924f8829a4aSRandall Stewart 	hopeless_peer:
4925f8829a4aSRandall Stewart 			*abort_now = 1;
4926f8829a4aSRandall Stewart 			/* XXX */
4927f8829a4aSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4928f8829a4aSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
4929f8829a4aSRandall Stewart 			if (oper) {
4930f8829a4aSRandall Stewart 				struct sctp_paramhdr *ph;
4931f8829a4aSRandall Stewart 				uint32_t *ippp;
4932f8829a4aSRandall Stewart 
4933139bc87fSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4934f8829a4aSRandall Stewart 				    sizeof(uint32_t);
4935f8829a4aSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
4936f8829a4aSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4937139bc87fSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
4938f8829a4aSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
4939a5d547adSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
4940f8829a4aSRandall Stewart 			}
4941a5d547adSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4942ceaad40aSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
4943f8829a4aSRandall Stewart 			return;
4944139bc87fSRandall Stewart #endif
4945f8829a4aSRandall Stewart 		}
4946f8829a4aSRandall Stewart 	}
4947f8829a4aSRandall Stewart 	/**********************/
4948f8829a4aSRandall Stewart 	/* 1) check the range */
4949f8829a4aSRandall Stewart 	/**********************/
4950f8829a4aSRandall Stewart 	if (compare_with_wrap(asoc->last_acked_seq, last_tsn, MAX_TSN)) {
4951f8829a4aSRandall Stewart 		/* acking something behind */
4952f8829a4aSRandall Stewart 		return;
4953f8829a4aSRandall Stewart 	}
4954bff64a4dSRandall Stewart 	sav_cum_ack = asoc->last_acked_seq;
4955bff64a4dSRandall Stewart 
4956f8829a4aSRandall Stewart 	/* update the Rwnd of the peer */
4957f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue) &&
4958f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->send_queue) &&
4959f8829a4aSRandall Stewart 	    (asoc->stream_queue_cnt == 0)
4960f8829a4aSRandall Stewart 	    ) {
4961f8829a4aSRandall Stewart 		/* nothing left on send/sent and strmq */
4962b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4963f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4964f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
496580fefe0aSRandall Stewart 		}
4966f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
4967f8829a4aSRandall Stewart 		if (asoc->sent_queue_retran_cnt) {
4968f8829a4aSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4969f8829a4aSRandall Stewart 		}
4970f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4971f8829a4aSRandall Stewart 			/* SWS sender side engages */
4972f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
4973f8829a4aSRandall Stewart 		}
4974f8829a4aSRandall Stewart 		/* stop any timers */
4975f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4976f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4977a5d547adSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4978b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
4979139bc87fSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4980f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck1);
4981a5d547adSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4982a5d547adSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4983f8829a4aSRandall Stewart 				}
4984f8829a4aSRandall Stewart 			}
4985f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4986f8829a4aSRandall Stewart 			net->flight_size = 0;
4987f8829a4aSRandall Stewart 		}
4988f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4989f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4990f8829a4aSRandall Stewart 		return;
4991f8829a4aSRandall Stewart 	}
4992f8829a4aSRandall Stewart 	/*
4993f8829a4aSRandall Stewart 	 * We init netAckSz and netAckSz2 to 0. These are used to track 2
4994f8829a4aSRandall Stewart 	 * things. The total byte count acked is tracked in netAckSz AND
4995f8829a4aSRandall Stewart 	 * netAck2 is used to track the total bytes acked that are un-
4996f8829a4aSRandall Stewart 	 * amibguious and were never retransmitted. We track these on a per
4997f8829a4aSRandall Stewart 	 * destination address basis.
4998f8829a4aSRandall Stewart 	 */
4999f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5000f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
5001f8829a4aSRandall Stewart 		net->net_ack = 0;
5002f8829a4aSRandall Stewart 		net->net_ack2 = 0;
5003f8829a4aSRandall Stewart 
5004f8829a4aSRandall Stewart 		/*
500542551e99SRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
500642551e99SRandall Stewart 		 * SACK processing
5007f8829a4aSRandall Stewart 		 */
5008f8829a4aSRandall Stewart 		net->new_pseudo_cumack = 0;
5009f8829a4aSRandall Stewart 		net->will_exit_fast_recovery = 0;
5010f8829a4aSRandall Stewart 	}
5011f8829a4aSRandall Stewart 	/* process the new consecutive TSN first */
5012f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
5013f8829a4aSRandall Stewart 	while (tp1) {
5014f8829a4aSRandall Stewart 		if (compare_with_wrap(last_tsn, tp1->rec.data.TSN_seq,
5015f8829a4aSRandall Stewart 		    MAX_TSN) ||
5016f8829a4aSRandall Stewart 		    last_tsn == tp1->rec.data.TSN_seq) {
5017f8829a4aSRandall Stewart 			if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
5018f8829a4aSRandall Stewart 				/*
5019f8829a4aSRandall Stewart 				 * ECN Nonce: Add the nonce to the sender's
5020f8829a4aSRandall Stewart 				 * nonce sum
5021f8829a4aSRandall Stewart 				 */
5022f8829a4aSRandall Stewart 				asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
5023f8829a4aSRandall Stewart 				accum_moved = 1;
5024f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
5025f8829a4aSRandall Stewart 					/*
5026f8829a4aSRandall Stewart 					 * If it is less than ACKED, it is
5027f8829a4aSRandall Stewart 					 * now no-longer in flight. Higher
5028f8829a4aSRandall Stewart 					 * values may occur during marking
5029f8829a4aSRandall Stewart 					 */
5030f8829a4aSRandall Stewart 					if ((tp1->whoTo->dest_state &
5031f8829a4aSRandall Stewart 					    SCTP_ADDR_UNCONFIRMED) &&
5032f8829a4aSRandall Stewart 					    (tp1->snd_count < 2)) {
5033f8829a4aSRandall Stewart 						/*
5034f8829a4aSRandall Stewart 						 * If there was no retran
5035f8829a4aSRandall Stewart 						 * and the address is
5036f8829a4aSRandall Stewart 						 * un-confirmed and we sent
5037f8829a4aSRandall Stewart 						 * there and are now
5038f8829a4aSRandall Stewart 						 * sacked.. its confirmed,
5039f8829a4aSRandall Stewart 						 * mark it so.
5040f8829a4aSRandall Stewart 						 */
5041f8829a4aSRandall Stewart 						tp1->whoTo->dest_state &=
5042f8829a4aSRandall Stewart 						    ~SCTP_ADDR_UNCONFIRMED;
5043f8829a4aSRandall Stewart 					}
5044c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5045b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
5046c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
5047a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
5048a5d547adSRandall Stewart 							    tp1->book_size,
5049c105859eSRandall Stewart 							    (uintptr_t) tp1->whoTo,
5050a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
505180fefe0aSRandall Stewart 						}
5052c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
5053c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
5054f8829a4aSRandall Stewart 					}
5055f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
5056f8829a4aSRandall Stewart 
5057f8829a4aSRandall Stewart 					/* CMT SFR and DAC algos */
5058f8829a4aSRandall Stewart 					this_sack_lowest_newack = tp1->rec.data.TSN_seq;
5059f8829a4aSRandall Stewart 					tp1->whoTo->saw_newack = 1;
5060f8829a4aSRandall Stewart 
5061f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
5062f8829a4aSRandall Stewart 						/*
5063f8829a4aSRandall Stewart 						 * True non-retransmited
5064f8829a4aSRandall Stewart 						 * chunk
5065f8829a4aSRandall Stewart 						 */
5066f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
5067f8829a4aSRandall Stewart 						    tp1->send_size;
5068f8829a4aSRandall Stewart 
5069f8829a4aSRandall Stewart 						/* update RTO too? */
5070f8829a4aSRandall Stewart 						if (tp1->do_rtt) {
5071f8829a4aSRandall Stewart 							tp1->whoTo->RTO =
5072f8829a4aSRandall Stewart 							    sctp_calculate_rto(stcb,
5073f8829a4aSRandall Stewart 							    asoc, tp1->whoTo,
507418e198d3SRandall Stewart 							    &tp1->sent_rcv_time,
507518e198d3SRandall Stewart 							    sctp_align_safe_nocopy);
5076f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
5077f8829a4aSRandall Stewart 						}
5078f8829a4aSRandall Stewart 					}
5079f8829a4aSRandall Stewart 					/*
5080f8829a4aSRandall Stewart 					 * CMT: CUCv2 algorithm. From the
5081f8829a4aSRandall Stewart 					 * cumack'd TSNs, for each TSN being
5082f8829a4aSRandall Stewart 					 * acked for the first time, set the
5083f8829a4aSRandall Stewart 					 * following variables for the
5084f8829a4aSRandall Stewart 					 * corresp destination.
5085f8829a4aSRandall Stewart 					 * new_pseudo_cumack will trigger a
5086f8829a4aSRandall Stewart 					 * cwnd update.
5087f8829a4aSRandall Stewart 					 * find_(rtx_)pseudo_cumack will
5088f8829a4aSRandall Stewart 					 * trigger search for the next
5089f8829a4aSRandall Stewart 					 * expected (rtx-)pseudo-cumack.
5090f8829a4aSRandall Stewart 					 */
5091f8829a4aSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
5092f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
5093f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
5094f8829a4aSRandall Stewart 
5095f8829a4aSRandall Stewart 
5096b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
5097f8829a4aSRandall Stewart 						sctp_log_sack(asoc->last_acked_seq,
5098f8829a4aSRandall Stewart 						    cum_ack,
5099f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
5100f8829a4aSRandall Stewart 						    0,
5101f8829a4aSRandall Stewart 						    0,
5102f8829a4aSRandall Stewart 						    SCTP_LOG_TSN_ACKED);
510380fefe0aSRandall Stewart 					}
5104b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
5105f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
510680fefe0aSRandall Stewart 					}
5107f8829a4aSRandall Stewart 				}
5108f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5109f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
5110f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
5111f8829a4aSRandall Stewart 					sctp_audit_log(0xB3,
5112f8829a4aSRandall Stewart 					    (asoc->sent_queue_retran_cnt & 0x000000ff));
5113f8829a4aSRandall Stewart #endif
5114f8829a4aSRandall Stewart 				}
511542551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
511642551e99SRandall Stewart 					/* deflate the cwnd */
511742551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
511842551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
511942551e99SRandall Stewart 				}
5120f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
5121f8829a4aSRandall Stewart 			}
5122f8829a4aSRandall Stewart 		} else {
5123f8829a4aSRandall Stewart 			break;
5124f8829a4aSRandall Stewart 		}
5125f8829a4aSRandall Stewart 		tp1 = TAILQ_NEXT(tp1, sctp_next);
5126f8829a4aSRandall Stewart 	}
5127f8829a4aSRandall Stewart 	biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
5128f8829a4aSRandall Stewart 	/* always set this up to cum-ack */
5129f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = last_tsn;
5130f8829a4aSRandall Stewart 
5131458303daSRandall Stewart 	/* Move offset up to point to gaps/dups */
5132458303daSRandall Stewart 	offset += sizeof(struct sctp_sack_chunk);
5133f8829a4aSRandall Stewart 	if (((num_seg * (sizeof(struct sctp_gap_ack_block))) + sizeof(struct sctp_sack_chunk)) > sack_length) {
5134f8829a4aSRandall Stewart 
5135f8829a4aSRandall Stewart 		/* skip corrupt segments */
5136f8829a4aSRandall Stewart 		goto skip_segments;
5137f8829a4aSRandall Stewart 	}
5138f8829a4aSRandall Stewart 	if (num_seg > 0) {
5139f8829a4aSRandall Stewart 
5140f8829a4aSRandall Stewart 		/*
5141f8829a4aSRandall Stewart 		 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
5142f8829a4aSRandall Stewart 		 * to be greater than the cumack. Also reset saw_newack to 0
5143f8829a4aSRandall Stewart 		 * for all dests.
5144f8829a4aSRandall Stewart 		 */
5145f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5146f8829a4aSRandall Stewart 			net->saw_newack = 0;
5147f8829a4aSRandall Stewart 			net->this_sack_highest_newack = last_tsn;
5148f8829a4aSRandall Stewart 		}
5149f8829a4aSRandall Stewart 
5150f8829a4aSRandall Stewart 		/*
5151f8829a4aSRandall Stewart 		 * thisSackHighestGap will increase while handling NEW
5152f8829a4aSRandall Stewart 		 * segments this_sack_highest_newack will increase while
5153f8829a4aSRandall Stewart 		 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
5154f8829a4aSRandall Stewart 		 * used for CMT DAC algo. saw_newack will also change.
5155f8829a4aSRandall Stewart 		 */
5156458303daSRandall Stewart 		sctp_handle_segments(m, &offset, stcb, asoc, ch, last_tsn,
5157f8829a4aSRandall Stewart 		    &biggest_tsn_acked, &biggest_tsn_newly_acked, &this_sack_lowest_newack,
5158f8829a4aSRandall Stewart 		    num_seg, &ecn_seg_sums);
5159f8829a4aSRandall Stewart 
5160b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
5161f8829a4aSRandall Stewart 			/*
5162f8829a4aSRandall Stewart 			 * validate the biggest_tsn_acked in the gap acks if
5163f8829a4aSRandall Stewart 			 * strict adherence is wanted.
5164f8829a4aSRandall Stewart 			 */
5165f8829a4aSRandall Stewart 			if ((biggest_tsn_acked == send_s) ||
5166f8829a4aSRandall Stewart 			    (compare_with_wrap(biggest_tsn_acked, send_s, MAX_TSN))) {
5167f8829a4aSRandall Stewart 				/*
5168f8829a4aSRandall Stewart 				 * peer is either confused or we are under
5169f8829a4aSRandall Stewart 				 * attack. We must abort.
5170f8829a4aSRandall Stewart 				 */
5171f8829a4aSRandall Stewart 				goto hopeless_peer;
5172f8829a4aSRandall Stewart 			}
5173f8829a4aSRandall Stewart 		}
5174f8829a4aSRandall Stewart 	}
5175f8829a4aSRandall Stewart skip_segments:
5176f8829a4aSRandall Stewart 	/*******************************************/
5177f8829a4aSRandall Stewart 	/* cancel ALL T3-send timer if accum moved */
5178f8829a4aSRandall Stewart 	/*******************************************/
5179b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
5180f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5181f8829a4aSRandall Stewart 			if (net->new_pseudo_cumack)
5182f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5183a5d547adSRandall Stewart 				    stcb, net,
5184a5d547adSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
5185f8829a4aSRandall Stewart 
5186f8829a4aSRandall Stewart 		}
5187f8829a4aSRandall Stewart 	} else {
5188f8829a4aSRandall Stewart 		if (accum_moved) {
5189f8829a4aSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5190f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5191a5d547adSRandall Stewart 				    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
5192f8829a4aSRandall Stewart 			}
5193f8829a4aSRandall Stewart 		}
5194f8829a4aSRandall Stewart 	}
5195f8829a4aSRandall Stewart 	/********************************************/
5196f8829a4aSRandall Stewart 	/* drop the acked chunks from the sendqueue */
5197f8829a4aSRandall Stewart 	/********************************************/
5198f8829a4aSRandall Stewart 	asoc->last_acked_seq = cum_ack;
5199f8829a4aSRandall Stewart 
5200f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
5201f8829a4aSRandall Stewart 	if (tp1 == NULL)
5202f8829a4aSRandall Stewart 		goto done_with_it;
5203f8829a4aSRandall Stewart 	do {
5204f8829a4aSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq, cum_ack,
5205f8829a4aSRandall Stewart 		    MAX_TSN)) {
5206f8829a4aSRandall Stewart 			break;
5207f8829a4aSRandall Stewart 		}
5208f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
5209f8829a4aSRandall Stewart 			/* no more sent on list */
521018e198d3SRandall Stewart 			printf("Warning, tp1->sent == %d and its now acked?\n",
521118e198d3SRandall Stewart 			    tp1->sent);
5212f8829a4aSRandall Stewart 		}
5213f8829a4aSRandall Stewart 		tp2 = TAILQ_NEXT(tp1, sctp_next);
5214f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
5215f8829a4aSRandall Stewart 		if (tp1->pr_sctp_on) {
5216f8829a4aSRandall Stewart 			if (asoc->pr_sctp_cnt != 0)
5217f8829a4aSRandall Stewart 				asoc->pr_sctp_cnt--;
5218f8829a4aSRandall Stewart 		}
5219f8829a4aSRandall Stewart 		if ((TAILQ_FIRST(&asoc->sent_queue) == NULL) &&
5220f8829a4aSRandall Stewart 		    (asoc->total_flight > 0)) {
5221f1f73e57SRandall Stewart #ifdef INVARIANTS
5222c105859eSRandall Stewart 			panic("Warning flight size is postive and should be 0");
5223f1f73e57SRandall Stewart #else
5224ad81507eSRandall Stewart 			SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
5225f8829a4aSRandall Stewart 			    asoc->total_flight);
5226f1f73e57SRandall Stewart #endif
5227f8829a4aSRandall Stewart 			asoc->total_flight = 0;
5228f8829a4aSRandall Stewart 		}
5229f8829a4aSRandall Stewart 		if (tp1->data) {
523004ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
5231f8829a4aSRandall Stewart 			sctp_free_bufspace(stcb, asoc, tp1, 1);
5232f8829a4aSRandall Stewart 			sctp_m_freem(tp1->data);
5233810ec536SMichael Tuexen 			if (asoc->peer_supports_prsctp && PR_SCTP_BUF_ENABLED(tp1->flags)) {
5234f8829a4aSRandall Stewart 				asoc->sent_queue_cnt_removeable--;
5235f8829a4aSRandall Stewart 			}
5236f8829a4aSRandall Stewart 		}
5237b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
5238f8829a4aSRandall Stewart 			sctp_log_sack(asoc->last_acked_seq,
5239f8829a4aSRandall Stewart 			    cum_ack,
5240f8829a4aSRandall Stewart 			    tp1->rec.data.TSN_seq,
5241f8829a4aSRandall Stewart 			    0,
5242f8829a4aSRandall Stewart 			    0,
5243f8829a4aSRandall Stewart 			    SCTP_LOG_FREE_SENT);
524480fefe0aSRandall Stewart 		}
5245f8829a4aSRandall Stewart 		tp1->data = NULL;
5246f8829a4aSRandall Stewart 		asoc->sent_queue_cnt--;
5247f8829a4aSRandall Stewart 		sctp_free_a_chunk(stcb, tp1);
5248f8829a4aSRandall Stewart 		wake_him++;
5249f8829a4aSRandall Stewart 		tp1 = tp2;
5250f8829a4aSRandall Stewart 	} while (tp1 != NULL);
5251f8829a4aSRandall Stewart 
5252f8829a4aSRandall Stewart done_with_it:
525304ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
5254f8829a4aSRandall Stewart 	if ((wake_him) && (stcb->sctp_socket)) {
5255ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5256ceaad40aSRandall Stewart 		struct socket *so;
5257ceaad40aSRandall Stewart 
5258ceaad40aSRandall Stewart #endif
5259f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
5260b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
5261f8829a4aSRandall Stewart 			sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_WAKESND_FROM_SACK);
526280fefe0aSRandall Stewart 		}
5263ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5264ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
5265ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
5266ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
5267ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
5268ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
5269ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
5270ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
5271ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
5272ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
5273ceaad40aSRandall Stewart 			return;
5274ceaad40aSRandall Stewart 		}
5275ceaad40aSRandall Stewart #endif
5276f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
5277ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5278ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
5279ceaad40aSRandall Stewart #endif
5280f8829a4aSRandall Stewart 	} else {
5281b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
5282f8829a4aSRandall Stewart 			sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_NOWAKE_FROM_SACK);
528380fefe0aSRandall Stewart 		}
5284f8829a4aSRandall Stewart 	}
5285f8829a4aSRandall Stewart 
528642551e99SRandall Stewart 	if (asoc->fast_retran_loss_recovery && accum_moved) {
5287f8829a4aSRandall Stewart 		if (compare_with_wrap(asoc->last_acked_seq,
5288f8829a4aSRandall Stewart 		    asoc->fast_recovery_tsn, MAX_TSN) ||
5289f8829a4aSRandall Stewart 		    asoc->last_acked_seq == asoc->fast_recovery_tsn) {
5290f8829a4aSRandall Stewart 			/* Setup so we will exit RFC2582 fast recovery */
5291f8829a4aSRandall Stewart 			will_exit_fast_recovery = 1;
5292f8829a4aSRandall Stewart 		}
5293f8829a4aSRandall Stewart 	}
5294f8829a4aSRandall Stewart 	/*
5295f8829a4aSRandall Stewart 	 * Check for revoked fragments:
5296f8829a4aSRandall Stewart 	 *
5297f8829a4aSRandall Stewart 	 * if Previous sack - Had no frags then we can't have any revoked if
5298f8829a4aSRandall Stewart 	 * Previous sack - Had frag's then - If we now have frags aka
5299f8829a4aSRandall Stewart 	 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
5300f8829a4aSRandall Stewart 	 * some of them. else - The peer revoked all ACKED fragments, since
5301f8829a4aSRandall Stewart 	 * we had some before and now we have NONE.
5302f8829a4aSRandall Stewart 	 */
5303f8829a4aSRandall Stewart 
5304f42a358aSRandall Stewart 	if (num_seg)
5305c105859eSRandall Stewart 		sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
5306f8829a4aSRandall Stewart 	else if (asoc->saw_sack_with_frags) {
5307f8829a4aSRandall Stewart 		int cnt_revoked = 0;
5308f8829a4aSRandall Stewart 
5309f8829a4aSRandall Stewart 		tp1 = TAILQ_FIRST(&asoc->sent_queue);
5310f8829a4aSRandall Stewart 		if (tp1 != NULL) {
5311f8829a4aSRandall Stewart 			/* Peer revoked all dg's marked or acked */
5312f8829a4aSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5313f8829a4aSRandall Stewart 				if ((tp1->sent > SCTP_DATAGRAM_RESEND) &&
5314f8829a4aSRandall Stewart 				    (tp1->sent < SCTP_FORWARD_TSN_SKIP)) {
5315f8829a4aSRandall Stewart 					tp1->sent = SCTP_DATAGRAM_SENT;
5316b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
5317c105859eSRandall Stewart 						sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
5318c105859eSRandall Stewart 						    tp1->whoTo->flight_size,
5319c105859eSRandall Stewart 						    tp1->book_size,
5320c105859eSRandall Stewart 						    (uintptr_t) tp1->whoTo,
5321c105859eSRandall Stewart 						    tp1->rec.data.TSN_seq);
532280fefe0aSRandall Stewart 					}
5323c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
5324c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
5325a5d547adSRandall Stewart 					tp1->rec.data.chunk_was_revoked = 1;
532642551e99SRandall Stewart 					/*
532742551e99SRandall Stewart 					 * To ensure that this increase in
532842551e99SRandall Stewart 					 * flightsize, which is artificial,
532942551e99SRandall Stewart 					 * does not throttle the sender, we
533042551e99SRandall Stewart 					 * also increase the cwnd
533142551e99SRandall Stewart 					 * artificially.
533242551e99SRandall Stewart 					 */
533342551e99SRandall Stewart 					tp1->whoTo->cwnd += tp1->book_size;
5334f8829a4aSRandall Stewart 					cnt_revoked++;
5335f8829a4aSRandall Stewart 				}
5336f8829a4aSRandall Stewart 			}
5337f8829a4aSRandall Stewart 			if (cnt_revoked) {
5338f8829a4aSRandall Stewart 				reneged_all = 1;
5339f8829a4aSRandall Stewart 			}
5340f8829a4aSRandall Stewart 		}
5341f8829a4aSRandall Stewart 		asoc->saw_sack_with_frags = 0;
5342f8829a4aSRandall Stewart 	}
5343f8829a4aSRandall Stewart 	if (num_seg)
5344f8829a4aSRandall Stewart 		asoc->saw_sack_with_frags = 1;
5345f8829a4aSRandall Stewart 	else
5346f8829a4aSRandall Stewart 		asoc->saw_sack_with_frags = 0;
5347f8829a4aSRandall Stewart 
5348b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
5349b54d3a6cSRandall Stewart 	asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
5350f8829a4aSRandall Stewart 
5351f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
5352f8829a4aSRandall Stewart 		/* nothing left in-flight */
5353f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5354f8829a4aSRandall Stewart 			/* stop all timers */
5355b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
5356139bc87fSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
5357f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
5358a5d547adSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
5359a5d547adSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_29);
5360f8829a4aSRandall Stewart 				}
5361f8829a4aSRandall Stewart 			}
5362f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5363a5d547adSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
5364f8829a4aSRandall Stewart 			net->flight_size = 0;
5365f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
5366f8829a4aSRandall Stewart 		}
5367f8829a4aSRandall Stewart 		asoc->total_flight = 0;
5368f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
5369f8829a4aSRandall Stewart 	}
5370f8829a4aSRandall Stewart 	/**********************************/
5371f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
5372f8829a4aSRandall Stewart 	/**********************************/
5373f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
5374f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
5375b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
5376f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
5377f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
537880fefe0aSRandall Stewart 		}
5379f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
5380f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5381f8829a4aSRandall Stewart 			/* SWS sender side engages */
5382f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
5383f8829a4aSRandall Stewart 		}
5384f8829a4aSRandall Stewart 		/* clean up */
5385f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
5386f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
5387f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
5388f8829a4aSRandall Stewart 		    (asoc->locked_on_sending)
5389f8829a4aSRandall Stewart 		    ) {
5390f8829a4aSRandall Stewart 			struct sctp_stream_queue_pending *sp;
5391f8829a4aSRandall Stewart 
5392f8829a4aSRandall Stewart 			/*
5393f8829a4aSRandall Stewart 			 * I may be in a state where we got all across.. but
5394f8829a4aSRandall Stewart 			 * cannot write more due to a shutdown... we abort
5395f8829a4aSRandall Stewart 			 * since the user did not indicate EOR in this case.
5396f8829a4aSRandall Stewart 			 */
5397f8829a4aSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
5398f8829a4aSRandall Stewart 			    sctp_streamhead);
53992afb3e84SRandall Stewart 			if ((sp) && (sp->length == 0)) {
5400f8829a4aSRandall Stewart 				asoc->locked_on_sending = NULL;
54012afb3e84SRandall Stewart 				if (sp->msg_is_complete) {
5402f8829a4aSRandall Stewart 					asoc->stream_queue_cnt--;
54032afb3e84SRandall Stewart 				} else {
54042afb3e84SRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
54052afb3e84SRandall Stewart 					asoc->stream_queue_cnt--;
54062afb3e84SRandall Stewart 				}
5407f8829a4aSRandall Stewart 			}
5408f8829a4aSRandall Stewart 		}
5409f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
5410f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
5411f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
5412f8829a4aSRandall Stewart 				/* Need to abort here */
5413f8829a4aSRandall Stewart 				struct mbuf *oper;
5414f8829a4aSRandall Stewart 
5415f8829a4aSRandall Stewart 		abort_out_now:
5416f8829a4aSRandall Stewart 				*abort_now = 1;
5417f8829a4aSRandall Stewart 				/* XXX */
5418f8829a4aSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
5419f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
5420f8829a4aSRandall Stewart 				if (oper) {
5421f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
5422f8829a4aSRandall Stewart 					uint32_t *ippp;
5423f8829a4aSRandall Stewart 
5424139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
5425f8829a4aSRandall Stewart 					    sizeof(uint32_t);
5426f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
5427f8829a4aSRandall Stewart 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
5428139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
5429f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
5430a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_31);
5431f8829a4aSRandall Stewart 				}
5432a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
5433ceaad40aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
5434f8829a4aSRandall Stewart 				return;
5435f8829a4aSRandall Stewart 			} else {
5436f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
5437f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
5438f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5439f42a358aSRandall Stewart 				}
5440c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
5441b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
5442f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
5443f8829a4aSRandall Stewart 				sctp_send_shutdown(stcb,
5444f8829a4aSRandall Stewart 				    stcb->asoc.primary_destination);
5445f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
5446f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
5447f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
5448f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
5449f8829a4aSRandall Stewart 			}
5450f8829a4aSRandall Stewart 			return;
5451f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
5452f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
5453f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
5454f8829a4aSRandall Stewart 				goto abort_out_now;
5455f8829a4aSRandall Stewart 			}
5456f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5457c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
5458b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
5459f8829a4aSRandall Stewart 			sctp_send_shutdown_ack(stcb,
5460f8829a4aSRandall Stewart 			    stcb->asoc.primary_destination);
5461f8829a4aSRandall Stewart 
5462f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
5463f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb, asoc->primary_destination);
5464f8829a4aSRandall Stewart 			return;
5465f8829a4aSRandall Stewart 		}
5466f8829a4aSRandall Stewart 	}
5467f8829a4aSRandall Stewart 	/*
5468f8829a4aSRandall Stewart 	 * Now here we are going to recycle net_ack for a different use...
5469f8829a4aSRandall Stewart 	 * HEADS UP.
5470f8829a4aSRandall Stewart 	 */
5471f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5472f8829a4aSRandall Stewart 		net->net_ack = 0;
5473f8829a4aSRandall Stewart 	}
5474f8829a4aSRandall Stewart 
5475f8829a4aSRandall Stewart 	/*
5476f8829a4aSRandall Stewart 	 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
5477f8829a4aSRandall Stewart 	 * to be done. Setting this_sack_lowest_newack to the cum_ack will
5478f8829a4aSRandall Stewart 	 * automatically ensure that.
5479f8829a4aSRandall Stewart 	 */
5480b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac) && (cmt_dac_flag == 0)) {
5481f8829a4aSRandall Stewart 		this_sack_lowest_newack = cum_ack;
5482f8829a4aSRandall Stewart 	}
5483f8829a4aSRandall Stewart 	if (num_seg > 0) {
5484f8829a4aSRandall Stewart 		sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
5485f8829a4aSRandall Stewart 		    biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
5486f8829a4aSRandall Stewart 	}
5487b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
5488b54d3a6cSRandall Stewart 	asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
5489f8829a4aSRandall Stewart 
5490f8829a4aSRandall Stewart 	/******************************************************************
5491f8829a4aSRandall Stewart 	 *  Here we do the stuff with ECN Nonce checking.
5492f8829a4aSRandall Stewart 	 *  We basically check to see if the nonce sum flag was incorrect
5493f8829a4aSRandall Stewart 	 *  or if resynchronization needs to be done. Also if we catch a
5494f8829a4aSRandall Stewart 	 *  misbehaving receiver we give him the kick.
5495f8829a4aSRandall Stewart 	 ******************************************************************/
5496f8829a4aSRandall Stewart 
5497f8829a4aSRandall Stewart 	if (asoc->ecn_nonce_allowed) {
5498f8829a4aSRandall Stewart 		if (asoc->nonce_sum_check) {
5499f8829a4aSRandall Stewart 			if (nonce_sum_flag != ((asoc->nonce_sum_expect_base + ecn_seg_sums) & SCTP_SACK_NONCE_SUM)) {
5500f8829a4aSRandall Stewart 				if (asoc->nonce_wait_for_ecne == 0) {
5501f8829a4aSRandall Stewart 					struct sctp_tmit_chunk *lchk;
5502f8829a4aSRandall Stewart 
5503f8829a4aSRandall Stewart 					lchk = TAILQ_FIRST(&asoc->send_queue);
5504f8829a4aSRandall Stewart 					asoc->nonce_wait_for_ecne = 1;
5505f8829a4aSRandall Stewart 					if (lchk) {
5506f8829a4aSRandall Stewart 						asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
5507f8829a4aSRandall Stewart 					} else {
5508f8829a4aSRandall Stewart 						asoc->nonce_wait_tsn = asoc->sending_seq;
5509f8829a4aSRandall Stewart 					}
5510f8829a4aSRandall Stewart 				} else {
5511f8829a4aSRandall Stewart 					if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
5512f8829a4aSRandall Stewart 					    (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
5513f8829a4aSRandall Stewart 						/*
5514f8829a4aSRandall Stewart 						 * Misbehaving peer. We need
5515f8829a4aSRandall Stewart 						 * to react to this guy
5516f8829a4aSRandall Stewart 						 */
5517f8829a4aSRandall Stewart 						asoc->ecn_allowed = 0;
5518f8829a4aSRandall Stewart 						asoc->ecn_nonce_allowed = 0;
5519f8829a4aSRandall Stewart 					}
5520f8829a4aSRandall Stewart 				}
5521f8829a4aSRandall Stewart 			}
5522f8829a4aSRandall Stewart 		} else {
5523f8829a4aSRandall Stewart 			/* See if Resynchronization Possible */
5524f8829a4aSRandall Stewart 			if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
5525f8829a4aSRandall Stewart 				asoc->nonce_sum_check = 1;
5526f8829a4aSRandall Stewart 				/*
5527f8829a4aSRandall Stewart 				 * now we must calculate what the base is.
5528f8829a4aSRandall Stewart 				 * We do this based on two things, we know
5529f8829a4aSRandall Stewart 				 * the total's for all the segments
5530f8829a4aSRandall Stewart 				 * gap-acked in the SACK, its stored in
5531f8829a4aSRandall Stewart 				 * ecn_seg_sums. We also know the SACK's
5532f8829a4aSRandall Stewart 				 * nonce sum, its in nonce_sum_flag. So we
5533f8829a4aSRandall Stewart 				 * can build a truth table to back-calculate
5534f8829a4aSRandall Stewart 				 * the new value of
5535f8829a4aSRandall Stewart 				 * asoc->nonce_sum_expect_base:
5536f8829a4aSRandall Stewart 				 *
5537f8829a4aSRandall Stewart 				 * SACK-flag-Value         Seg-Sums Base 0 0 0
5538f8829a4aSRandall Stewart 				 * 1                    0 1 0 1 1 1
5539f8829a4aSRandall Stewart 				 * 1 0
5540f8829a4aSRandall Stewart 				 */
5541f8829a4aSRandall Stewart 				asoc->nonce_sum_expect_base = (ecn_seg_sums ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
5542f8829a4aSRandall Stewart 			}
5543f8829a4aSRandall Stewart 		}
5544f8829a4aSRandall Stewart 	}
5545f8829a4aSRandall Stewart 	/* Now are we exiting loss recovery ? */
5546f8829a4aSRandall Stewart 	if (will_exit_fast_recovery) {
5547f8829a4aSRandall Stewart 		/* Ok, we must exit fast recovery */
5548f8829a4aSRandall Stewart 		asoc->fast_retran_loss_recovery = 0;
5549f8829a4aSRandall Stewart 	}
5550f8829a4aSRandall Stewart 	if ((asoc->sat_t3_loss_recovery) &&
5551f8829a4aSRandall Stewart 	    ((compare_with_wrap(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn,
5552f8829a4aSRandall Stewart 	    MAX_TSN) ||
5553f8829a4aSRandall Stewart 	    (asoc->last_acked_seq == asoc->sat_t3_recovery_tsn)))) {
5554f8829a4aSRandall Stewart 		/* end satellite t3 loss recovery */
5555f8829a4aSRandall Stewart 		asoc->sat_t3_loss_recovery = 0;
5556f8829a4aSRandall Stewart 	}
555742551e99SRandall Stewart 	/*
555842551e99SRandall Stewart 	 * CMT Fast recovery
555942551e99SRandall Stewart 	 */
5560f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5561f8829a4aSRandall Stewart 		if (net->will_exit_fast_recovery) {
5562f8829a4aSRandall Stewart 			/* Ok, we must exit fast recovery */
5563f8829a4aSRandall Stewart 			net->fast_retran_loss_recovery = 0;
5564f8829a4aSRandall Stewart 		}
5565f8829a4aSRandall Stewart 	}
5566f8829a4aSRandall Stewart 
5567f8829a4aSRandall Stewart 	/* Adjust and set the new rwnd value */
5568b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
5569f8829a4aSRandall Stewart 		sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
5570b3f1ea41SRandall Stewart 		    asoc->peers_rwnd, asoc->total_flight, (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)), a_rwnd);
557180fefe0aSRandall Stewart 	}
5572f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
5573b3f1ea41SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
5574f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5575f8829a4aSRandall Stewart 		/* SWS sender side engages */
5576f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
5577f8829a4aSRandall Stewart 	}
55785e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
55795e54f665SRandall Stewart 		win_probe_recovery = 1;
55805e54f665SRandall Stewart 	}
5581f8829a4aSRandall Stewart 	/*
5582f8829a4aSRandall Stewart 	 * Now we must setup so we have a timer up for anyone with
5583f8829a4aSRandall Stewart 	 * outstanding data.
5584f8829a4aSRandall Stewart 	 */
5585bff64a4dSRandall Stewart 	done_once = 0;
5586a5d547adSRandall Stewart again:
5587a5d547adSRandall Stewart 	j = 0;
5588f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
55895e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
5590c105859eSRandall Stewart 			win_probe_recovered = 1;
55915e54f665SRandall Stewart 			/*-
55925e54f665SRandall Stewart 			 * Find first chunk that was used with
55935e54f665SRandall Stewart 			 * window probe and clear the event. Put
55945e54f665SRandall Stewart 			 * it back into the send queue as if has
55955e54f665SRandall Stewart 			 * not been sent.
55965e54f665SRandall Stewart 			 */
55975e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
55985e54f665SRandall Stewart 				if (tp1->window_probe) {
5599c105859eSRandall Stewart 					sctp_window_probe_recovery(stcb, asoc, net, tp1);
56005e54f665SRandall Stewart 					break;
56015e54f665SRandall Stewart 				}
56025e54f665SRandall Stewart 			}
56035e54f665SRandall Stewart 		}
5604f8829a4aSRandall Stewart 		if (net->flight_size) {
5605a5d547adSRandall Stewart 			j++;
5606f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5607f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb, net);
56085171328bSRandall Stewart 			if (net->window_probe) {
56095171328bSRandall Stewart 			}
5610c105859eSRandall Stewart 		} else {
56115171328bSRandall Stewart 			if (net->window_probe) {
56125171328bSRandall Stewart 				/*
56135171328bSRandall Stewart 				 * In window probes we must assure a timer
56145171328bSRandall Stewart 				 * is still running there
56155171328bSRandall Stewart 				 */
56165171328bSRandall Stewart 
56175171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
56185171328bSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_SEND,
56195171328bSRandall Stewart 					    stcb->sctp_ep, stcb, net);
56205171328bSRandall Stewart 
56215171328bSRandall Stewart 				}
56225171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5623c105859eSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5624c105859eSRandall Stewart 				    stcb, net,
5625c105859eSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
5626c105859eSRandall Stewart 			}
5627b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
5628c105859eSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
5629c105859eSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
5630c105859eSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
5631c105859eSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
5632c105859eSRandall Stewart 				}
5633c105859eSRandall Stewart 			}
5634f8829a4aSRandall Stewart 		}
5635f8829a4aSRandall Stewart 	}
5636bff64a4dSRandall Stewart 	if ((j == 0) &&
5637bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
5638bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
5639c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
5640bff64a4dSRandall Stewart 	    (done_once == 0)) {
56410c0982b8SRandall Stewart 		/*
56420c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
56430c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
56440c0982b8SRandall Stewart 		 */
56450c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
5646a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5647a5d547adSRandall Stewart 				net->flight_size = 0;
5648a5d547adSRandall Stewart 			}
5649a5d547adSRandall Stewart 			asoc->total_flight = 0;
5650a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
5651a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
5652a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5653a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5654c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
5655c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
5656a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5657a5d547adSRandall Stewart 					asoc->sent_queue_retran_cnt++;
5658a5d547adSRandall Stewart 				}
5659a5d547adSRandall Stewart 			}
56600c0982b8SRandall Stewart 		}
5661bff64a4dSRandall Stewart 		done_once = 1;
5662a5d547adSRandall Stewart 		goto again;
5663a5d547adSRandall Stewart 	}
5664dfb11ef8SRandall Stewart 	/* Fix up the a-p-a-p for future PR-SCTP sends */
5665dfb11ef8SRandall Stewart 	if (compare_with_wrap(cum_ack, asoc->advanced_peer_ack_point, MAX_TSN)) {
5666dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cum_ack;
5667dfb11ef8SRandall Stewart 	}
5668830d754dSRandall Stewart 	/* C2. try to further move advancedPeerAckPoint ahead */
5669830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
5670830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
5671830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
5672830d754dSRandall Stewart 
5673830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
5674830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
5675830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
5676830d754dSRandall Stewart 		if (compare_with_wrap(asoc->advanced_peer_ack_point, cum_ack,
5677830d754dSRandall Stewart 		    MAX_TSN)) {
5678830d754dSRandall Stewart 			/*
5679830d754dSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing for notes
5680830d754dSRandall Stewart 			 * on issues that will occur when the ECN NONCE
5681830d754dSRandall Stewart 			 * stuff is put into SCTP for cross checking.
5682830d754dSRandall Stewart 			 */
56830c0982b8SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
56840c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
56850c0982b8SRandall Stewart 				    0xee, cum_ack, asoc->advanced_peer_ack_point,
56860c0982b8SRandall Stewart 				    old_adv_peer_ack_point);
56870c0982b8SRandall Stewart 			}
5688830d754dSRandall Stewart 			if (compare_with_wrap(asoc->advanced_peer_ack_point, old_adv_peer_ack_point,
5689830d754dSRandall Stewart 			    MAX_TSN)) {
5690830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
5691830d754dSRandall Stewart 				/*
5692830d754dSRandall Stewart 				 * ECN Nonce: Disable Nonce Sum check when
5693830d754dSRandall Stewart 				 * FWD TSN is sent and store resync tsn
5694830d754dSRandall Stewart 				 */
5695830d754dSRandall Stewart 				asoc->nonce_sum_check = 0;
5696830d754dSRandall Stewart 				asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
56970c0982b8SRandall Stewart 			} else if (lchk) {
56980c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
56990c0982b8SRandall Stewart 				lchk->rec.data.fwd_tsn_cnt++;
57000c0982b8SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt > 3) {
57010c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
57020c0982b8SRandall Stewart 					lchk->rec.data.fwd_tsn_cnt = 0;
57030c0982b8SRandall Stewart 				}
5704830d754dSRandall Stewart 			}
5705830d754dSRandall Stewart 		}
5706830d754dSRandall Stewart 		if (lchk) {
5707830d754dSRandall Stewart 			/* Assure a timer is up */
5708830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5709830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
5710830d754dSRandall Stewart 		}
5711830d754dSRandall Stewart 	}
5712b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
5713f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
5714f8829a4aSRandall Stewart 		    a_rwnd,
5715f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
5716f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
5717f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
571880fefe0aSRandall Stewart 	}
5719f8829a4aSRandall Stewart }
5720f8829a4aSRandall Stewart 
5721f8829a4aSRandall Stewart void
5722f8829a4aSRandall Stewart sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp,
5723f8829a4aSRandall Stewart     struct sctp_nets *netp, int *abort_flag)
5724f8829a4aSRandall Stewart {
5725f8829a4aSRandall Stewart 	/* Copy cum-ack */
5726f8829a4aSRandall Stewart 	uint32_t cum_ack, a_rwnd;
5727f8829a4aSRandall Stewart 
5728f8829a4aSRandall Stewart 	cum_ack = ntohl(cp->cumulative_tsn_ack);
5729f8829a4aSRandall Stewart 	/* Arrange so a_rwnd does NOT change */
5730f8829a4aSRandall Stewart 	a_rwnd = stcb->asoc.peers_rwnd + stcb->asoc.total_flight;
5731f8829a4aSRandall Stewart 
5732f8829a4aSRandall Stewart 	/* Now call the express sack handling */
5733f8829a4aSRandall Stewart 	sctp_express_handle_sack(stcb, cum_ack, a_rwnd, 0, abort_flag);
5734f8829a4aSRandall Stewart }
5735f8829a4aSRandall Stewart 
5736f8829a4aSRandall Stewart static void
5737f8829a4aSRandall Stewart sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
5738f8829a4aSRandall Stewart     struct sctp_stream_in *strmin)
5739f8829a4aSRandall Stewart {
5740f8829a4aSRandall Stewart 	struct sctp_queued_to_read *ctl, *nctl;
5741f8829a4aSRandall Stewart 	struct sctp_association *asoc;
5742f8829a4aSRandall Stewart 	int tt;
5743f8829a4aSRandall Stewart 
5744830d754dSRandall Stewart 	/* EY -used to calculate nr_gap information */
5745830d754dSRandall Stewart 	uint32_t nr_tsn, nr_gap;
5746830d754dSRandall Stewart 
5747f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
5748f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered;
5749f8829a4aSRandall Stewart 	/*
5750f8829a4aSRandall Stewart 	 * First deliver anything prior to and including the stream no that
5751f8829a4aSRandall Stewart 	 * came in
5752f8829a4aSRandall Stewart 	 */
5753f8829a4aSRandall Stewart 	ctl = TAILQ_FIRST(&strmin->inqueue);
5754f8829a4aSRandall Stewart 	while (ctl) {
5755f8829a4aSRandall Stewart 		nctl = TAILQ_NEXT(ctl, next);
5756f8829a4aSRandall Stewart 		if (compare_with_wrap(tt, ctl->sinfo_ssn, MAX_SEQ) ||
5757f8829a4aSRandall Stewart 		    (tt == ctl->sinfo_ssn)) {
5758f8829a4aSRandall Stewart 			/* this is deliverable now */
5759f8829a4aSRandall Stewart 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5760f8829a4aSRandall Stewart 			/* subtract pending on streams */
5761f8829a4aSRandall Stewart 			asoc->size_on_all_streams -= ctl->length;
5762f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_all_streams);
5763f8829a4aSRandall Stewart 			/* deliver it to at least the delivery-q */
5764f8829a4aSRandall Stewart 			if (stcb->sctp_socket) {
5765830d754dSRandall Stewart 				/* EY need the tsn info for calculating nr */
5766830d754dSRandall Stewart 				nr_tsn = ctl->sinfo_tsn;
5767f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
5768f8829a4aSRandall Stewart 				    ctl,
5769cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
5770830d754dSRandall Stewart 				/*
5771830d754dSRandall Stewart 				 * EY this is the chunk that should be
5772830d754dSRandall Stewart 				 * tagged nr gapped calculate the gap and
5773830d754dSRandall Stewart 				 * such then tag this TSN nr
5774830d754dSRandall Stewart 				 * chk->rec.data.TSN_seq
5775830d754dSRandall Stewart 				 */
5776830d754dSRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
5777830d754dSRandall Stewart 
5778d50c1d79SRandall Stewart 					SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
5779830d754dSRandall Stewart 					if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
5780830d754dSRandall Stewart 					    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
5781830d754dSRandall Stewart 						/*
5782830d754dSRandall Stewart 						 * EY These should never
5783830d754dSRandall Stewart 						 * happen- explained before
5784830d754dSRandall Stewart 						 */
5785830d754dSRandall Stewart 					} else {
5786830d754dSRandall Stewart 						SCTP_TCB_LOCK_ASSERT(stcb);
5787830d754dSRandall Stewart 						SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
5788d50c1d79SRandall Stewart 						SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
57898933fa13SRandall Stewart 						if (compare_with_wrap(nr_tsn,
57908933fa13SRandall Stewart 						    asoc->highest_tsn_inside_nr_map,
57918933fa13SRandall Stewart 						    MAX_TSN))
5792830d754dSRandall Stewart 							asoc->highest_tsn_inside_nr_map = nr_tsn;
5793830d754dSRandall Stewart 					}
5794830d754dSRandall Stewart 					if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, nr_gap))
5795830d754dSRandall Stewart 						/*
5796830d754dSRandall Stewart 						 * printf("In
5797830d754dSRandall Stewart 						 * sctp_kick_prsctp_reorder_q
5798830d754dSRandall Stewart 						 * ueue(7): Something wrong,
5799830d754dSRandall Stewart 						 * the TSN to be tagged"
5800830d754dSRandall Stewart 						 * "\nas NR is not even in
5801830d754dSRandall Stewart 						 * the mapping_array, or map
5802830d754dSRandall Stewart 						 * and nr_map are
5803830d754dSRandall Stewart 						 * inconsistent");
5804830d754dSRandall Stewart 						 */
5805830d754dSRandall Stewart 						/*
5806830d754dSRandall Stewart 						 * EY - not %100 sure about
5807830d754dSRandall Stewart 						 * the lock thing, don't
5808830d754dSRandall Stewart 						 * think its required
5809830d754dSRandall Stewart 						 */
5810830d754dSRandall Stewart 						/*
5811830d754dSRandall Stewart 						 * SCTP_TCB_LOCK_ASSERT(stcb)
5812830d754dSRandall Stewart 						 * ;
5813830d754dSRandall Stewart 						 */
5814830d754dSRandall Stewart 					{
5815830d754dSRandall Stewart 						/*
5816830d754dSRandall Stewart 						 * printf("\nCalculating an
5817830d754dSRandall Stewart 						 * nr_gap!!\nmapping_array_si
5818830d754dSRandall Stewart 						 * ze = %d
5819830d754dSRandall Stewart 						 * nr_mapping_array_size =
5820830d754dSRandall Stewart 						 * %d" "\nmapping_array_base
5821830d754dSRandall Stewart 						 * = %d
5822830d754dSRandall Stewart 						 * nr_mapping_array_base =
5823830d754dSRandall Stewart 						 * %d\nhighest_tsn_inside_map
5824830d754dSRandall Stewart 						 *  = %d"
5825830d754dSRandall Stewart 						 * "highest_tsn_inside_nr_map
5826830d754dSRandall Stewart 						 *  = %d\nTSN = %d nr_gap =
5827830d754dSRandall Stewart 						 * %d",asoc->mapping_array_si
5828830d754dSRandall Stewart 						 * ze,
5829830d754dSRandall Stewart 						 * asoc->nr_mapping_array_siz
5830830d754dSRandall Stewart 						 * e,
5831830d754dSRandall Stewart 						 * asoc->mapping_array_base_t
5832830d754dSRandall Stewart 						 * sn,
5833830d754dSRandall Stewart 						 * asoc->nr_mapping_array_bas
5834830d754dSRandall Stewart 						 * e_tsn,
5835830d754dSRandall Stewart 						 * asoc->highest_tsn_inside_m
5836830d754dSRandall Stewart 						 * ap,
5837830d754dSRandall Stewart 						 * asoc->highest_tsn_inside_n
5838830d754dSRandall Stewart 						 * r_map,tsn,nr_gap);
5839830d754dSRandall Stewart 						 */
5840830d754dSRandall Stewart 					}
5841830d754dSRandall Stewart 				}
5842f8829a4aSRandall Stewart 			}
5843f8829a4aSRandall Stewart 		} else {
5844f8829a4aSRandall Stewart 			/* no more delivery now. */
5845f8829a4aSRandall Stewart 			break;
5846f8829a4aSRandall Stewart 		}
5847f8829a4aSRandall Stewart 		ctl = nctl;
5848f8829a4aSRandall Stewart 	}
5849f8829a4aSRandall Stewart 	/*
5850f8829a4aSRandall Stewart 	 * now we must deliver things in queue the normal way  if any are
5851f8829a4aSRandall Stewart 	 * now ready.
5852f8829a4aSRandall Stewart 	 */
5853f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered + 1;
5854f8829a4aSRandall Stewart 	ctl = TAILQ_FIRST(&strmin->inqueue);
5855f8829a4aSRandall Stewart 	while (ctl) {
5856f8829a4aSRandall Stewart 		nctl = TAILQ_NEXT(ctl, next);
5857f8829a4aSRandall Stewart 		if (tt == ctl->sinfo_ssn) {
5858f8829a4aSRandall Stewart 			/* this is deliverable now */
5859f8829a4aSRandall Stewart 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5860f8829a4aSRandall Stewart 			/* subtract pending on streams */
5861f8829a4aSRandall Stewart 			asoc->size_on_all_streams -= ctl->length;
5862f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_all_streams);
5863f8829a4aSRandall Stewart 			/* deliver it to at least the delivery-q */
5864f8829a4aSRandall Stewart 			strmin->last_sequence_delivered = ctl->sinfo_ssn;
5865f8829a4aSRandall Stewart 			if (stcb->sctp_socket) {
5866830d754dSRandall Stewart 				/* EY */
5867830d754dSRandall Stewart 				nr_tsn = ctl->sinfo_tsn;
5868f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
5869f8829a4aSRandall Stewart 				    ctl,
5870cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
5871830d754dSRandall Stewart 				/*
5872830d754dSRandall Stewart 				 * EY this is the chunk that should be
5873830d754dSRandall Stewart 				 * tagged nr gapped calculate the gap and
5874830d754dSRandall Stewart 				 * such then tag this TSN nr
5875830d754dSRandall Stewart 				 * chk->rec.data.TSN_seq
5876830d754dSRandall Stewart 				 */
5877830d754dSRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
5878d50c1d79SRandall Stewart 					SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
5879830d754dSRandall Stewart 					if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
5880830d754dSRandall Stewart 					    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
5881830d754dSRandall Stewart 						/*
5882830d754dSRandall Stewart 						 * EY These should never
5883830d754dSRandall Stewart 						 * happen, explained before
5884830d754dSRandall Stewart 						 */
5885830d754dSRandall Stewart 					} else {
5886830d754dSRandall Stewart 						SCTP_TCB_LOCK_ASSERT(stcb);
5887830d754dSRandall Stewart 						SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
5888d50c1d79SRandall Stewart 						SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
58898933fa13SRandall Stewart 						if (compare_with_wrap(nr_tsn, asoc->highest_tsn_inside_nr_map,
58908933fa13SRandall Stewart 						    MAX_TSN))
5891830d754dSRandall Stewart 							asoc->highest_tsn_inside_nr_map = nr_tsn;
5892830d754dSRandall Stewart 					}
5893830d754dSRandall Stewart 					if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, nr_gap))
5894830d754dSRandall Stewart 						/*
5895830d754dSRandall Stewart 						 * printf("In
5896830d754dSRandall Stewart 						 * sctp_kick_prsctp_reorder_q
5897830d754dSRandall Stewart 						 * ueue(8): Something wrong,
5898830d754dSRandall Stewart 						 * the TSN to be tagged"
5899830d754dSRandall Stewart 						 * "\nas NR is not even in
5900830d754dSRandall Stewart 						 * the mapping_array, or map
5901830d754dSRandall Stewart 						 * and nr_map are
5902830d754dSRandall Stewart 						 * inconsistent");
5903830d754dSRandall Stewart 						 */
5904830d754dSRandall Stewart 						/*
5905830d754dSRandall Stewart 						 * EY - not %100 sure about
5906830d754dSRandall Stewart 						 * the lock thing, don't
5907830d754dSRandall Stewart 						 * think its required
5908830d754dSRandall Stewart 						 */
5909830d754dSRandall Stewart 						/*
5910830d754dSRandall Stewart 						 * SCTP_TCB_LOCK_ASSERT(stcb)
5911830d754dSRandall Stewart 						 * ;
5912830d754dSRandall Stewart 						 */
5913830d754dSRandall Stewart 					{
5914830d754dSRandall Stewart 						/*
5915830d754dSRandall Stewart 						 * printf("\nCalculating an
5916830d754dSRandall Stewart 						 * nr_gap!!\nmapping_array_si
5917830d754dSRandall Stewart 						 * ze = %d
5918830d754dSRandall Stewart 						 * nr_mapping_array_size =
5919830d754dSRandall Stewart 						 * %d" "\nmapping_array_base
5920830d754dSRandall Stewart 						 * = %d
5921830d754dSRandall Stewart 						 * nr_mapping_array_base =
5922830d754dSRandall Stewart 						 * %d\nhighest_tsn_inside_map
5923830d754dSRandall Stewart 						 *  = %d"
5924830d754dSRandall Stewart 						 * "highest_tsn_inside_nr_map
5925830d754dSRandall Stewart 						 *  = %d\nTSN = %d nr_gap =
5926830d754dSRandall Stewart 						 * %d",asoc->mapping_array_si
5927830d754dSRandall Stewart 						 * ze,
5928830d754dSRandall Stewart 						 * asoc->nr_mapping_array_siz
5929830d754dSRandall Stewart 						 * e,
5930830d754dSRandall Stewart 						 * asoc->mapping_array_base_t
5931830d754dSRandall Stewart 						 * sn,
5932830d754dSRandall Stewart 						 * asoc->nr_mapping_array_bas
5933830d754dSRandall Stewart 						 * e_tsn,
5934830d754dSRandall Stewart 						 * asoc->highest_tsn_inside_m
5935830d754dSRandall Stewart 						 * ap,
5936830d754dSRandall Stewart 						 * asoc->highest_tsn_inside_n
5937830d754dSRandall Stewart 						 * r_map,tsn,nr_gap);
5938830d754dSRandall Stewart 						 */
5939830d754dSRandall Stewart 					}
5940830d754dSRandall Stewart 				}
5941f8829a4aSRandall Stewart 			}
5942f8829a4aSRandall Stewart 			tt = strmin->last_sequence_delivered + 1;
5943f8829a4aSRandall Stewart 		} else {
5944f8829a4aSRandall Stewart 			break;
5945f8829a4aSRandall Stewart 		}
5946f8829a4aSRandall Stewart 		ctl = nctl;
5947f8829a4aSRandall Stewart 	}
5948f8829a4aSRandall Stewart }
5949f8829a4aSRandall Stewart 
59508933fa13SRandall Stewart static void
59518933fa13SRandall Stewart sctp_flush_reassm_for_str_seq(struct sctp_tcb *stcb,
59528933fa13SRandall Stewart     struct sctp_association *asoc,
59538933fa13SRandall Stewart     uint16_t stream, uint16_t seq)
59548933fa13SRandall Stewart {
59558933fa13SRandall Stewart 	struct sctp_tmit_chunk *chk, *at;
59568933fa13SRandall Stewart 
59578933fa13SRandall Stewart 	if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
59588933fa13SRandall Stewart 		/* For each one on here see if we need to toss it */
59598933fa13SRandall Stewart 		/*
59608933fa13SRandall Stewart 		 * For now large messages held on the reasmqueue that are
59618933fa13SRandall Stewart 		 * complete will be tossed too. We could in theory do more
59628933fa13SRandall Stewart 		 * work to spin through and stop after dumping one msg aka
59638933fa13SRandall Stewart 		 * seeing the start of a new msg at the head, and call the
59648933fa13SRandall Stewart 		 * delivery function... to see if it can be delivered... But
59658933fa13SRandall Stewart 		 * for now we just dump everything on the queue.
59668933fa13SRandall Stewart 		 */
59678933fa13SRandall Stewart 		chk = TAILQ_FIRST(&asoc->reasmqueue);
59688933fa13SRandall Stewart 		while (chk) {
59698933fa13SRandall Stewart 			at = TAILQ_NEXT(chk, sctp_next);
59708e71b694SMichael Tuexen 			/*
59718e71b694SMichael Tuexen 			 * Do not toss it if on a different stream or marked
59728e71b694SMichael Tuexen 			 * for unordered delivery in which case the stream
59738e71b694SMichael Tuexen 			 * sequence number has no meaning.
59748e71b694SMichael Tuexen 			 */
59758e71b694SMichael Tuexen 			if ((chk->rec.data.stream_number != stream) ||
59768e71b694SMichael Tuexen 			    ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == SCTP_DATA_UNORDERED)) {
59778933fa13SRandall Stewart 				chk = at;
59788933fa13SRandall Stewart 				continue;
59798933fa13SRandall Stewart 			}
59808933fa13SRandall Stewart 			if (chk->rec.data.stream_seq == seq) {
59818933fa13SRandall Stewart 				/* It needs to be tossed */
59828933fa13SRandall Stewart 				TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
59838933fa13SRandall Stewart 				if (compare_with_wrap(chk->rec.data.TSN_seq,
59848933fa13SRandall Stewart 				    asoc->tsn_last_delivered, MAX_TSN)) {
59858933fa13SRandall Stewart 					asoc->tsn_last_delivered =
59868933fa13SRandall Stewart 					    chk->rec.data.TSN_seq;
59878933fa13SRandall Stewart 					asoc->str_of_pdapi =
59888933fa13SRandall Stewart 					    chk->rec.data.stream_number;
59898933fa13SRandall Stewart 					asoc->ssn_of_pdapi =
59908933fa13SRandall Stewart 					    chk->rec.data.stream_seq;
59918933fa13SRandall Stewart 					asoc->fragment_flags =
59928933fa13SRandall Stewart 					    chk->rec.data.rcv_flags;
59938933fa13SRandall Stewart 				}
59948933fa13SRandall Stewart 				asoc->size_on_reasm_queue -= chk->send_size;
59958933fa13SRandall Stewart 				sctp_ucount_decr(asoc->cnt_on_reasm_queue);
59968933fa13SRandall Stewart 
59978933fa13SRandall Stewart 				/* Clear up any stream problem */
59988933fa13SRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) !=
59998933fa13SRandall Stewart 				    SCTP_DATA_UNORDERED &&
60008933fa13SRandall Stewart 				    (compare_with_wrap(chk->rec.data.stream_seq,
60018933fa13SRandall Stewart 				    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered,
60028933fa13SRandall Stewart 				    MAX_SEQ))) {
60038933fa13SRandall Stewart 					/*
60048933fa13SRandall Stewart 					 * We must dump forward this streams
60058933fa13SRandall Stewart 					 * sequence number if the chunk is
60068933fa13SRandall Stewart 					 * not unordered that is being
60078933fa13SRandall Stewart 					 * skipped. There is a chance that
60088933fa13SRandall Stewart 					 * if the peer does not include the
60098933fa13SRandall Stewart 					 * last fragment in its FWD-TSN we
60108933fa13SRandall Stewart 					 * WILL have a problem here since
60118933fa13SRandall Stewart 					 * you would have a partial chunk in
60128933fa13SRandall Stewart 					 * queue that may not be
60138933fa13SRandall Stewart 					 * deliverable. Also if a Partial
60148933fa13SRandall Stewart 					 * delivery API as started the user
60158933fa13SRandall Stewart 					 * may get a partial chunk. The next
60168933fa13SRandall Stewart 					 * read returning a new chunk...
60178933fa13SRandall Stewart 					 * really ugly but I see no way
60188933fa13SRandall Stewart 					 * around it! Maybe a notify??
60198933fa13SRandall Stewart 					 */
60208933fa13SRandall Stewart 					asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered =
60218933fa13SRandall Stewart 					    chk->rec.data.stream_seq;
60228933fa13SRandall Stewart 				}
60238933fa13SRandall Stewart 				if (chk->data) {
60248933fa13SRandall Stewart 					sctp_m_freem(chk->data);
60258933fa13SRandall Stewart 					chk->data = NULL;
60268933fa13SRandall Stewart 				}
60278933fa13SRandall Stewart 				sctp_free_a_chunk(stcb, chk);
60288933fa13SRandall Stewart 			} else if (compare_with_wrap(chk->rec.data.stream_seq, seq, MAX_SEQ)) {
60298933fa13SRandall Stewart 				/*
60308933fa13SRandall Stewart 				 * If the stream_seq is > than the purging
60318933fa13SRandall Stewart 				 * one, we are done
60328933fa13SRandall Stewart 				 */
60338933fa13SRandall Stewart 				break;
60348933fa13SRandall Stewart 			}
60358933fa13SRandall Stewart 			chk = at;
60368933fa13SRandall Stewart 		}
60378933fa13SRandall Stewart 	}
60388933fa13SRandall Stewart }
60398933fa13SRandall Stewart 
60408933fa13SRandall Stewart 
6041f8829a4aSRandall Stewart void
6042f8829a4aSRandall Stewart sctp_handle_forward_tsn(struct sctp_tcb *stcb,
6043671d309cSRandall Stewart     struct sctp_forward_tsn_chunk *fwd, int *abort_flag, struct mbuf *m, int offset)
6044f8829a4aSRandall Stewart {
6045f8829a4aSRandall Stewart 	/*
6046f8829a4aSRandall Stewart 	 * ISSUES that MUST be fixed for ECN! When we are the sender of the
6047f8829a4aSRandall Stewart 	 * forward TSN, when the SACK comes back that acknowledges the
6048f8829a4aSRandall Stewart 	 * FWD-TSN we must reset the NONCE sum to match correctly. This will
6049f8829a4aSRandall Stewart 	 * get quite tricky since we may have sent more data interveneing
6050f8829a4aSRandall Stewart 	 * and must carefully account for what the SACK says on the nonce
6051f8829a4aSRandall Stewart 	 * and any gaps that are reported. This work will NOT be done here,
6052f8829a4aSRandall Stewart 	 * but I note it here since it is really related to PR-SCTP and
6053f8829a4aSRandall Stewart 	 * FWD-TSN's
6054f8829a4aSRandall Stewart 	 */
6055f8829a4aSRandall Stewart 
6056f8829a4aSRandall Stewart 	/* The pr-sctp fwd tsn */
6057f8829a4aSRandall Stewart 	/*
6058f8829a4aSRandall Stewart 	 * here we will perform all the data receiver side steps for
6059f8829a4aSRandall Stewart 	 * processing FwdTSN, as required in by pr-sctp draft:
6060f8829a4aSRandall Stewart 	 *
6061f8829a4aSRandall Stewart 	 * Assume we get FwdTSN(x):
6062f8829a4aSRandall Stewart 	 *
6063f8829a4aSRandall Stewart 	 * 1) update local cumTSN to x 2) try to further advance cumTSN to x +
6064f8829a4aSRandall Stewart 	 * others we have 3) examine and update re-ordering queue on
6065f8829a4aSRandall Stewart 	 * pr-in-streams 4) clean up re-assembly queue 5) Send a sack to
6066f8829a4aSRandall Stewart 	 * report where we are.
6067f8829a4aSRandall Stewart 	 */
6068f8829a4aSRandall Stewart 	struct sctp_association *asoc;
60692afb3e84SRandall Stewart 	uint32_t new_cum_tsn, gap;
60708933fa13SRandall Stewart 	unsigned int i, fwd_sz, cumack_set_flag, m_size;
60718933fa13SRandall Stewart 	uint32_t str_seq;
6072f8829a4aSRandall Stewart 	struct sctp_stream_in *strm;
6073f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk, *at;
60748933fa13SRandall Stewart 	struct sctp_queued_to_read *ctl, *sv;
6075f8829a4aSRandall Stewart 
6076f8829a4aSRandall Stewart 	cumack_set_flag = 0;
6077f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
6078f8829a4aSRandall Stewart 	if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
6079ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1,
6080ad81507eSRandall Stewart 		    "Bad size too small/big fwd-tsn\n");
6081f8829a4aSRandall Stewart 		return;
6082f8829a4aSRandall Stewart 	}
6083f8829a4aSRandall Stewart 	m_size = (stcb->asoc.mapping_array_size << 3);
6084f8829a4aSRandall Stewart 	/*************************************************************/
6085f8829a4aSRandall Stewart 	/* 1. Here we update local cumTSN and shift the bitmap array */
6086f8829a4aSRandall Stewart 	/*************************************************************/
6087f8829a4aSRandall Stewart 	new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
6088f8829a4aSRandall Stewart 
6089f8829a4aSRandall Stewart 	if (compare_with_wrap(asoc->cumulative_tsn, new_cum_tsn, MAX_TSN) ||
6090f8829a4aSRandall Stewart 	    asoc->cumulative_tsn == new_cum_tsn) {
6091f8829a4aSRandall Stewart 		/* Already got there ... */
6092f8829a4aSRandall Stewart 		return;
6093f8829a4aSRandall Stewart 	}
6094f8829a4aSRandall Stewart 	if (compare_with_wrap(new_cum_tsn, asoc->highest_tsn_inside_map,
6095f8829a4aSRandall Stewart 	    MAX_TSN)) {
6096f8829a4aSRandall Stewart 		asoc->highest_tsn_inside_map = new_cum_tsn;
6097830d754dSRandall Stewart 		/* EY nr_mapping_array version of the above */
6098830d754dSRandall Stewart 		/*
6099830d754dSRandall Stewart 		 * if(SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) &&
6100830d754dSRandall Stewart 		 * asoc->peer_supports_nr_sack)
6101830d754dSRandall Stewart 		 */
6102830d754dSRandall Stewart 		asoc->highest_tsn_inside_nr_map = new_cum_tsn;
6103b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
6104f8829a4aSRandall Stewart 			sctp_log_map(0, 0, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
610580fefe0aSRandall Stewart 		}
6106f8829a4aSRandall Stewart 	}
6107f8829a4aSRandall Stewart 	/*
6108f8829a4aSRandall Stewart 	 * now we know the new TSN is more advanced, let's find the actual
6109f8829a4aSRandall Stewart 	 * gap
6110f8829a4aSRandall Stewart 	 */
6111d50c1d79SRandall Stewart 	SCTP_CALC_TSN_TO_GAP(gap, new_cum_tsn, asoc->mapping_array_base_tsn);
61122afb3e84SRandall Stewart 	if (gap >= m_size) {
6113b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
6114c4739e2fSRandall Stewart 			sctp_log_map(0, 0, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
6115c4739e2fSRandall Stewart 		}
6116f8829a4aSRandall Stewart 		if ((long)gap > sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv)) {
611717205eccSRandall Stewart 			struct mbuf *oper;
611817205eccSRandall Stewart 
6119f8829a4aSRandall Stewart 			/*
6120f8829a4aSRandall Stewart 			 * out of range (of single byte chunks in the rwnd I
612117205eccSRandall Stewart 			 * give out). This must be an attacker.
6122f8829a4aSRandall Stewart 			 */
612317205eccSRandall Stewart 			*abort_flag = 1;
612417205eccSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
612517205eccSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
612617205eccSRandall Stewart 			if (oper) {
612717205eccSRandall Stewart 				struct sctp_paramhdr *ph;
612817205eccSRandall Stewart 				uint32_t *ippp;
612917205eccSRandall Stewart 
613017205eccSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
613117205eccSRandall Stewart 				    (sizeof(uint32_t) * 3);
613217205eccSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
613317205eccSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
613417205eccSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
613517205eccSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
613617205eccSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_33);
613717205eccSRandall Stewart 				ippp++;
613817205eccSRandall Stewart 				*ippp = asoc->highest_tsn_inside_map;
613917205eccSRandall Stewart 				ippp++;
614017205eccSRandall Stewart 				*ippp = new_cum_tsn;
614117205eccSRandall Stewart 			}
614217205eccSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_33;
614317205eccSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb,
6144ceaad40aSRandall Stewart 			    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
6145f8829a4aSRandall Stewart 			return;
6146f8829a4aSRandall Stewart 		}
6147207304d4SRandall Stewart 		SCTP_STAT_INCR(sctps_fwdtsn_map_over);
61482afb3e84SRandall Stewart 		memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
6149f8829a4aSRandall Stewart 		cumack_set_flag = 1;
61502afb3e84SRandall Stewart 		asoc->mapping_array_base_tsn = new_cum_tsn + 1;
61512afb3e84SRandall Stewart 		asoc->cumulative_tsn = asoc->highest_tsn_inside_map = new_cum_tsn;
6152830d754dSRandall Stewart 		/* EY - nr_sack: nr_mapping_array version of the above */
6153830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
6154830d754dSRandall Stewart 			memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.nr_mapping_array_size);
6155830d754dSRandall Stewart 			asoc->nr_mapping_array_base_tsn = new_cum_tsn + 1;
6156830d754dSRandall Stewart 			asoc->highest_tsn_inside_nr_map = new_cum_tsn;
6157830d754dSRandall Stewart 			if (asoc->nr_mapping_array_size != asoc->mapping_array_size) {
6158830d754dSRandall Stewart 				/*
6159830d754dSRandall Stewart 				 * printf("IN sctp_handle_forward_tsn:
6160830d754dSRandall Stewart 				 * Something is wrong the size of" "map and
6161830d754dSRandall Stewart 				 * nr_map should be equal!")
6162830d754dSRandall Stewart 				 */ ;
6163830d754dSRandall Stewart 			}
6164830d754dSRandall Stewart 		}
6165b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
6166f8829a4aSRandall Stewart 			sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
616780fefe0aSRandall Stewart 		}
6168f8829a4aSRandall Stewart 		asoc->last_echo_tsn = asoc->highest_tsn_inside_map;
61692afb3e84SRandall Stewart 	} else {
61702afb3e84SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
61712afb3e84SRandall Stewart 		for (i = 0; i <= gap; i++) {
61728933fa13SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack
61738933fa13SRandall Stewart 			    && SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
61748933fa13SRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, i);
6175d50c1d79SRandall Stewart 			} else {
6176d50c1d79SRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->mapping_array, i);
61778933fa13SRandall Stewart 			}
6178f8829a4aSRandall Stewart 		}
61792afb3e84SRandall Stewart 		/*
61802afb3e84SRandall Stewart 		 * Now after marking all, slide thing forward but no sack
61812afb3e84SRandall Stewart 		 * please.
61822afb3e84SRandall Stewart 		 */
61832afb3e84SRandall Stewart 		sctp_sack_check(stcb, 0, 0, abort_flag);
61842afb3e84SRandall Stewart 		if (*abort_flag)
61852afb3e84SRandall Stewart 			return;
61862afb3e84SRandall Stewart 	}
6187f8829a4aSRandall Stewart 	/*************************************************************/
6188f8829a4aSRandall Stewart 	/* 2. Clear up re-assembly queue                             */
6189f8829a4aSRandall Stewart 	/*************************************************************/
6190f8829a4aSRandall Stewart 	/*
6191f8829a4aSRandall Stewart 	 * First service it if pd-api is up, just in case we can progress it
6192f8829a4aSRandall Stewart 	 * forward
6193f8829a4aSRandall Stewart 	 */
6194f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
6195f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
6196f8829a4aSRandall Stewart 	}
6197f8829a4aSRandall Stewart 	if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
6198f8829a4aSRandall Stewart 		/* For each one on here see if we need to toss it */
6199f8829a4aSRandall Stewart 		/*
6200f8829a4aSRandall Stewart 		 * For now large messages held on the reasmqueue that are
6201f8829a4aSRandall Stewart 		 * complete will be tossed too. We could in theory do more
6202f8829a4aSRandall Stewart 		 * work to spin through and stop after dumping one msg aka
6203f8829a4aSRandall Stewart 		 * seeing the start of a new msg at the head, and call the
6204f8829a4aSRandall Stewart 		 * delivery function... to see if it can be delivered... But
6205f8829a4aSRandall Stewart 		 * for now we just dump everything on the queue.
6206f8829a4aSRandall Stewart 		 */
6207f8829a4aSRandall Stewart 		chk = TAILQ_FIRST(&asoc->reasmqueue);
6208f8829a4aSRandall Stewart 		while (chk) {
6209f8829a4aSRandall Stewart 			at = TAILQ_NEXT(chk, sctp_next);
62100c0982b8SRandall Stewart 			if ((compare_with_wrap(new_cum_tsn,
62110c0982b8SRandall Stewart 			    chk->rec.data.TSN_seq, MAX_TSN)) ||
62120c0982b8SRandall Stewart 			    (new_cum_tsn == chk->rec.data.TSN_seq)) {
6213f8829a4aSRandall Stewart 				/* It needs to be tossed */
6214f8829a4aSRandall Stewart 				TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
6215f8829a4aSRandall Stewart 				if (compare_with_wrap(chk->rec.data.TSN_seq,
6216f8829a4aSRandall Stewart 				    asoc->tsn_last_delivered, MAX_TSN)) {
6217f8829a4aSRandall Stewart 					asoc->tsn_last_delivered =
6218f8829a4aSRandall Stewart 					    chk->rec.data.TSN_seq;
6219f8829a4aSRandall Stewart 					asoc->str_of_pdapi =
6220f8829a4aSRandall Stewart 					    chk->rec.data.stream_number;
6221f8829a4aSRandall Stewart 					asoc->ssn_of_pdapi =
6222f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq;
6223f8829a4aSRandall Stewart 					asoc->fragment_flags =
6224f8829a4aSRandall Stewart 					    chk->rec.data.rcv_flags;
6225f8829a4aSRandall Stewart 				}
6226f8829a4aSRandall Stewart 				asoc->size_on_reasm_queue -= chk->send_size;
6227f8829a4aSRandall Stewart 				sctp_ucount_decr(asoc->cnt_on_reasm_queue);
6228f8829a4aSRandall Stewart 
6229f8829a4aSRandall Stewart 				/* Clear up any stream problem */
6230f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) !=
6231f8829a4aSRandall Stewart 				    SCTP_DATA_UNORDERED &&
6232f8829a4aSRandall Stewart 				    (compare_with_wrap(chk->rec.data.stream_seq,
6233f8829a4aSRandall Stewart 				    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered,
6234f8829a4aSRandall Stewart 				    MAX_SEQ))) {
6235f8829a4aSRandall Stewart 					/*
6236f8829a4aSRandall Stewart 					 * We must dump forward this streams
6237f8829a4aSRandall Stewart 					 * sequence number if the chunk is
6238f8829a4aSRandall Stewart 					 * not unordered that is being
6239f8829a4aSRandall Stewart 					 * skipped. There is a chance that
6240f8829a4aSRandall Stewart 					 * if the peer does not include the
6241f8829a4aSRandall Stewart 					 * last fragment in its FWD-TSN we
6242f8829a4aSRandall Stewart 					 * WILL have a problem here since
6243f8829a4aSRandall Stewart 					 * you would have a partial chunk in
6244f8829a4aSRandall Stewart 					 * queue that may not be
6245f8829a4aSRandall Stewart 					 * deliverable. Also if a Partial
6246f8829a4aSRandall Stewart 					 * delivery API as started the user
6247f8829a4aSRandall Stewart 					 * may get a partial chunk. The next
6248f8829a4aSRandall Stewart 					 * read returning a new chunk...
6249f8829a4aSRandall Stewart 					 * really ugly but I see no way
6250f8829a4aSRandall Stewart 					 * around it! Maybe a notify??
6251f8829a4aSRandall Stewart 					 */
6252f8829a4aSRandall Stewart 					asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered =
6253f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq;
6254f8829a4aSRandall Stewart 				}
6255f8829a4aSRandall Stewart 				if (chk->data) {
6256f8829a4aSRandall Stewart 					sctp_m_freem(chk->data);
6257f8829a4aSRandall Stewart 					chk->data = NULL;
6258f8829a4aSRandall Stewart 				}
6259f8829a4aSRandall Stewart 				sctp_free_a_chunk(stcb, chk);
6260f8829a4aSRandall Stewart 			} else {
6261f8829a4aSRandall Stewart 				/*
6262f8829a4aSRandall Stewart 				 * Ok we have gone beyond the end of the
62638933fa13SRandall Stewart 				 * fwd-tsn's mark.
6264f8829a4aSRandall Stewart 				 */
6265f8829a4aSRandall Stewart 				break;
6266f8829a4aSRandall Stewart 			}
6267f8829a4aSRandall Stewart 			chk = at;
6268f8829a4aSRandall Stewart 		}
6269f8829a4aSRandall Stewart 	}
62708933fa13SRandall Stewart 	/*******************************************************/
62718933fa13SRandall Stewart 	/* 3. Update the PR-stream re-ordering queues and fix  */
62728933fa13SRandall Stewart 	/* delivery issues as needed.                       */
62738933fa13SRandall Stewart 	/*******************************************************/
6274f8829a4aSRandall Stewart 	fwd_sz -= sizeof(*fwd);
6275671d309cSRandall Stewart 	if (m && fwd_sz) {
6276f8829a4aSRandall Stewart 		/* New method. */
6277d61a0ae0SRandall Stewart 		unsigned int num_str;
6278671d309cSRandall Stewart 		struct sctp_strseq *stseq, strseqbuf;
6279671d309cSRandall Stewart 
6280671d309cSRandall Stewart 		offset += sizeof(*fwd);
6281f8829a4aSRandall Stewart 
62828933fa13SRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
6283f8829a4aSRandall Stewart 		num_str = fwd_sz / sizeof(struct sctp_strseq);
6284f8829a4aSRandall Stewart 		for (i = 0; i < num_str; i++) {
6285f8829a4aSRandall Stewart 			uint16_t st;
6286f8829a4aSRandall Stewart 
6287671d309cSRandall Stewart 			stseq = (struct sctp_strseq *)sctp_m_getptr(m, offset,
6288671d309cSRandall Stewart 			    sizeof(struct sctp_strseq),
6289671d309cSRandall Stewart 			    (uint8_t *) & strseqbuf);
6290671d309cSRandall Stewart 			offset += sizeof(struct sctp_strseq);
62912afb3e84SRandall Stewart 			if (stseq == NULL) {
6292671d309cSRandall Stewart 				break;
62932afb3e84SRandall Stewart 			}
6294f8829a4aSRandall Stewart 			/* Convert */
6295851b7298SRandall Stewart 			st = ntohs(stseq->stream);
6296851b7298SRandall Stewart 			stseq->stream = st;
6297851b7298SRandall Stewart 			st = ntohs(stseq->sequence);
6298851b7298SRandall Stewart 			stseq->sequence = st;
62998933fa13SRandall Stewart 
6300f8829a4aSRandall Stewart 			/* now process */
63018933fa13SRandall Stewart 
63028933fa13SRandall Stewart 			/*
63038933fa13SRandall Stewart 			 * Ok we now look for the stream/seq on the read
63048933fa13SRandall Stewart 			 * queue where its not all delivered. If we find it
63058933fa13SRandall Stewart 			 * we transmute the read entry into a PDI_ABORTED.
63068933fa13SRandall Stewart 			 */
6307851b7298SRandall Stewart 			if (stseq->stream >= asoc->streamincnt) {
63082afb3e84SRandall Stewart 				/* screwed up streams, stop!  */
63092afb3e84SRandall Stewart 				break;
6310f8829a4aSRandall Stewart 			}
63118933fa13SRandall Stewart 			if ((asoc->str_of_pdapi == stseq->stream) &&
63128933fa13SRandall Stewart 			    (asoc->ssn_of_pdapi == stseq->sequence)) {
63138933fa13SRandall Stewart 				/*
63148933fa13SRandall Stewart 				 * If this is the one we were partially
63158933fa13SRandall Stewart 				 * delivering now then we no longer are.
63168933fa13SRandall Stewart 				 * Note this will change with the reassembly
63178933fa13SRandall Stewart 				 * re-write.
63188933fa13SRandall Stewart 				 */
63198933fa13SRandall Stewart 				asoc->fragmented_delivery_inprogress = 0;
63208933fa13SRandall Stewart 			}
63218933fa13SRandall Stewart 			sctp_flush_reassm_for_str_seq(stcb, asoc, stseq->stream, stseq->sequence);
63228933fa13SRandall Stewart 			TAILQ_FOREACH(ctl, &stcb->sctp_ep->read_queue, next) {
63238933fa13SRandall Stewart 				if ((ctl->sinfo_stream == stseq->stream) &&
63248933fa13SRandall Stewart 				    (ctl->sinfo_ssn == stseq->sequence)) {
63258933fa13SRandall Stewart 					str_seq = (stseq->stream << 16) | stseq->sequence;
63268933fa13SRandall Stewart 					ctl->end_added = 1;
63278933fa13SRandall Stewart 					ctl->pdapi_aborted = 1;
63288933fa13SRandall Stewart 					sv = stcb->asoc.control_pdapi;
63298933fa13SRandall Stewart 					stcb->asoc.control_pdapi = ctl;
6330810ec536SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
6331810ec536SMichael Tuexen 					    stcb,
63328933fa13SRandall Stewart 					    SCTP_PARTIAL_DELIVERY_ABORTED,
6333810ec536SMichael Tuexen 					    (void *)&str_seq,
6334810ec536SMichael Tuexen 					    SCTP_SO_NOT_LOCKED);
63358933fa13SRandall Stewart 					stcb->asoc.control_pdapi = sv;
63368933fa13SRandall Stewart 					break;
63378933fa13SRandall Stewart 				} else if ((ctl->sinfo_stream == stseq->stream) &&
63388933fa13SRandall Stewart 				    (compare_with_wrap(ctl->sinfo_ssn, stseq->sequence, MAX_SEQ))) {
63398933fa13SRandall Stewart 					/* We are past our victim SSN */
63408933fa13SRandall Stewart 					break;
63418933fa13SRandall Stewart 				}
63428933fa13SRandall Stewart 			}
6343851b7298SRandall Stewart 			strm = &asoc->strmin[stseq->stream];
6344851b7298SRandall Stewart 			if (compare_with_wrap(stseq->sequence,
6345f8829a4aSRandall Stewart 			    strm->last_sequence_delivered, MAX_SEQ)) {
6346f8829a4aSRandall Stewart 				/* Update the sequence number */
6347f8829a4aSRandall Stewart 				strm->last_sequence_delivered =
6348851b7298SRandall Stewart 				    stseq->sequence;
6349f8829a4aSRandall Stewart 			}
6350f8829a4aSRandall Stewart 			/* now kick the stream the new way */
635104ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
6352f8829a4aSRandall Stewart 			sctp_kick_prsctp_reorder_queue(stcb, strm);
6353f8829a4aSRandall Stewart 		}
63548933fa13SRandall Stewart 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
6355f8829a4aSRandall Stewart 	}
6356139bc87fSRandall Stewart 	if (TAILQ_FIRST(&asoc->reasmqueue)) {
6357139bc87fSRandall Stewart 		/* now lets kick out and check for more fragmented delivery */
635804ee05e8SRandall Stewart 		/* sa_ignore NO_NULL_CHK */
6359139bc87fSRandall Stewart 		sctp_deliver_reasm_check(stcb, &stcb->asoc);
6360139bc87fSRandall Stewart 	}
6361f8829a4aSRandall Stewart }
6362830d754dSRandall Stewart 
6363830d754dSRandall Stewart /* EY fully identical to sctp_express_handle_sack, duplicated for only naming convention */
6364830d754dSRandall Stewart void
6365830d754dSRandall Stewart sctp_express_handle_nr_sack(struct sctp_tcb *stcb, uint32_t cumack,
6366830d754dSRandall Stewart     uint32_t rwnd, int nonce_sum_flag, int *abort_now)
6367830d754dSRandall Stewart {
6368830d754dSRandall Stewart 	struct sctp_nets *net;
6369830d754dSRandall Stewart 	struct sctp_association *asoc;
6370830d754dSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
6371830d754dSRandall Stewart 	uint32_t old_rwnd;
6372830d754dSRandall Stewart 	int win_probe_recovery = 0;
6373830d754dSRandall Stewart 	int win_probe_recovered = 0;
6374830d754dSRandall Stewart 	int j, done_once = 0;
6375830d754dSRandall Stewart 
6376830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
6377830d754dSRandall Stewart 		sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
6378830d754dSRandall Stewart 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
6379830d754dSRandall Stewart 	}
6380830d754dSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
6381830d754dSRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
6382830d754dSRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
6383830d754dSRandall Stewart 	stcb->asoc.cumack_log_at++;
6384830d754dSRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
6385830d754dSRandall Stewart 		stcb->asoc.cumack_log_at = 0;
6386830d754dSRandall Stewart 	}
6387830d754dSRandall Stewart #endif
6388830d754dSRandall Stewart 	asoc = &stcb->asoc;
6389830d754dSRandall Stewart 	old_rwnd = asoc->peers_rwnd;
6390830d754dSRandall Stewart 	if (compare_with_wrap(asoc->last_acked_seq, cumack, MAX_TSN)) {
6391830d754dSRandall Stewart 		/* old ack */
6392830d754dSRandall Stewart 		return;
6393830d754dSRandall Stewart 	} else if (asoc->last_acked_seq == cumack) {
6394830d754dSRandall Stewart 		/* Window update sack */
6395830d754dSRandall Stewart 		asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
6396830d754dSRandall Stewart 		    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
6397830d754dSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6398830d754dSRandall Stewart 			/* SWS sender side engages */
6399830d754dSRandall Stewart 			asoc->peers_rwnd = 0;
6400830d754dSRandall Stewart 		}
6401830d754dSRandall Stewart 		if (asoc->peers_rwnd > old_rwnd) {
6402830d754dSRandall Stewart 			goto again;
6403830d754dSRandall Stewart 		}
6404830d754dSRandall Stewart 		return;
6405830d754dSRandall Stewart 	}
6406830d754dSRandall Stewart 	/* First setup for CC stuff */
6407830d754dSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6408830d754dSRandall Stewart 		net->prev_cwnd = net->cwnd;
6409830d754dSRandall Stewart 		net->net_ack = 0;
6410830d754dSRandall Stewart 		net->net_ack2 = 0;
6411830d754dSRandall Stewart 
6412830d754dSRandall Stewart 		/*
6413830d754dSRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
6414830d754dSRandall Stewart 		 * SACK processing
6415830d754dSRandall Stewart 		 */
6416830d754dSRandall Stewart 		net->new_pseudo_cumack = 0;
6417830d754dSRandall Stewart 		net->will_exit_fast_recovery = 0;
6418830d754dSRandall Stewart 	}
6419830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
6420830d754dSRandall Stewart 		uint32_t send_s;
6421830d754dSRandall Stewart 
6422830d754dSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
6423830d754dSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
6424830d754dSRandall Stewart 			    sctpchunk_listhead);
6425830d754dSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
6426830d754dSRandall Stewart 		} else {
6427830d754dSRandall Stewart 			send_s = asoc->sending_seq;
6428830d754dSRandall Stewart 		}
6429830d754dSRandall Stewart 		if ((cumack == send_s) ||
6430830d754dSRandall Stewart 		    compare_with_wrap(cumack, send_s, MAX_TSN)) {
6431830d754dSRandall Stewart #ifndef INVARIANTS
6432830d754dSRandall Stewart 			struct mbuf *oper;
6433830d754dSRandall Stewart 
6434830d754dSRandall Stewart #endif
6435830d754dSRandall Stewart #ifdef INVARIANTS
6436830d754dSRandall Stewart 			panic("Impossible sack 1");
6437830d754dSRandall Stewart #else
6438830d754dSRandall Stewart 			*abort_now = 1;
6439830d754dSRandall Stewart 			/* XXX */
6440830d754dSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
6441830d754dSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
6442830d754dSRandall Stewart 			if (oper) {
6443830d754dSRandall Stewart 				struct sctp_paramhdr *ph;
6444830d754dSRandall Stewart 				uint32_t *ippp;
6445830d754dSRandall Stewart 
6446830d754dSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
6447830d754dSRandall Stewart 				    sizeof(uint32_t);
6448830d754dSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
6449830d754dSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
6450830d754dSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
6451830d754dSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
6452830d754dSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
6453830d754dSRandall Stewart 			}
6454830d754dSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
6455830d754dSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
6456830d754dSRandall Stewart 			return;
6457830d754dSRandall Stewart #endif
6458830d754dSRandall Stewart 		}
6459830d754dSRandall Stewart 	}
6460830d754dSRandall Stewart 	asoc->this_sack_highest_gap = cumack;
6461830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
6462830d754dSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
6463830d754dSRandall Stewart 		    stcb->asoc.overall_error_count,
6464830d754dSRandall Stewart 		    0,
6465830d754dSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
6466830d754dSRandall Stewart 		    __LINE__);
6467830d754dSRandall Stewart 	}
6468830d754dSRandall Stewart 	stcb->asoc.overall_error_count = 0;
6469830d754dSRandall Stewart 	if (compare_with_wrap(cumack, asoc->last_acked_seq, MAX_TSN)) {
6470830d754dSRandall Stewart 		/* process the new consecutive TSN first */
6471830d754dSRandall Stewart 		tp1 = TAILQ_FIRST(&asoc->sent_queue);
6472830d754dSRandall Stewart 		while (tp1) {
6473830d754dSRandall Stewart 			tp2 = TAILQ_NEXT(tp1, sctp_next);
6474830d754dSRandall Stewart 			if (compare_with_wrap(cumack, tp1->rec.data.TSN_seq,
6475830d754dSRandall Stewart 			    MAX_TSN) ||
6476830d754dSRandall Stewart 			    cumack == tp1->rec.data.TSN_seq) {
6477830d754dSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
6478830d754dSRandall Stewart 					printf("Warning, an unsent is now acked?\n");
6479830d754dSRandall Stewart 				}
6480830d754dSRandall Stewart 				/*
6481830d754dSRandall Stewart 				 * ECN Nonce: Add the nonce to the sender's
6482830d754dSRandall Stewart 				 * nonce sum
6483830d754dSRandall Stewart 				 */
6484830d754dSRandall Stewart 				asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
6485830d754dSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
6486830d754dSRandall Stewart 					/*
6487830d754dSRandall Stewart 					 * If it is less than ACKED, it is
6488830d754dSRandall Stewart 					 * now no-longer in flight. Higher
6489830d754dSRandall Stewart 					 * values may occur during marking
6490830d754dSRandall Stewart 					 */
6491830d754dSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
6492830d754dSRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
6493830d754dSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
6494830d754dSRandall Stewart 							    tp1->whoTo->flight_size,
6495830d754dSRandall Stewart 							    tp1->book_size,
6496830d754dSRandall Stewart 							    (uintptr_t) tp1->whoTo,
6497830d754dSRandall Stewart 							    tp1->rec.data.TSN_seq);
6498830d754dSRandall Stewart 						}
6499830d754dSRandall Stewart 						sctp_flight_size_decrease(tp1);
6500830d754dSRandall Stewart 						/* sa_ignore NO_NULL_CHK */
6501830d754dSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
6502830d754dSRandall Stewart 					}
6503830d754dSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
6504830d754dSRandall Stewart 					if (tp1->snd_count < 2) {
6505830d754dSRandall Stewart 						/*
6506830d754dSRandall Stewart 						 * True non-retransmited
6507830d754dSRandall Stewart 						 * chunk
6508830d754dSRandall Stewart 						 */
6509830d754dSRandall Stewart 						tp1->whoTo->net_ack2 +=
6510830d754dSRandall Stewart 						    tp1->send_size;
6511830d754dSRandall Stewart 
6512830d754dSRandall Stewart 						/* update RTO too? */
6513830d754dSRandall Stewart 						if (tp1->do_rtt) {
6514830d754dSRandall Stewart 							tp1->whoTo->RTO =
6515830d754dSRandall Stewart 							/*
6516830d754dSRandall Stewart 							 * sa_ignore
6517830d754dSRandall Stewart 							 * NO_NULL_CHK
6518830d754dSRandall Stewart 							 */
6519830d754dSRandall Stewart 							    sctp_calculate_rto(stcb,
6520830d754dSRandall Stewart 							    asoc, tp1->whoTo,
6521830d754dSRandall Stewart 							    &tp1->sent_rcv_time,
6522830d754dSRandall Stewart 							    sctp_align_safe_nocopy);
6523830d754dSRandall Stewart 							tp1->do_rtt = 0;
6524830d754dSRandall Stewart 						}
6525830d754dSRandall Stewart 					}
6526830d754dSRandall Stewart 					/*
6527830d754dSRandall Stewart 					 * CMT: CUCv2 algorithm. From the
6528830d754dSRandall Stewart 					 * cumack'd TSNs, for each TSN being
6529830d754dSRandall Stewart 					 * acked for the first time, set the
6530830d754dSRandall Stewart 					 * following variables for the
6531830d754dSRandall Stewart 					 * corresp destination.
6532830d754dSRandall Stewart 					 * new_pseudo_cumack will trigger a
6533830d754dSRandall Stewart 					 * cwnd update.
6534830d754dSRandall Stewart 					 * find_(rtx_)pseudo_cumack will
6535830d754dSRandall Stewart 					 * trigger search for the next
6536830d754dSRandall Stewart 					 * expected (rtx-)pseudo-cumack.
6537830d754dSRandall Stewart 					 */
6538830d754dSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
6539830d754dSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
6540830d754dSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
6541830d754dSRandall Stewart 
6542830d754dSRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
6543830d754dSRandall Stewart 						/* sa_ignore NO_NULL_CHK */
6544830d754dSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
6545830d754dSRandall Stewart 					}
6546830d754dSRandall Stewart 				}
6547830d754dSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
6548830d754dSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
6549830d754dSRandall Stewart 				}
6550830d754dSRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
6551830d754dSRandall Stewart 					/* deflate the cwnd */
6552830d754dSRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
6553830d754dSRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
6554830d754dSRandall Stewart 				}
6555830d754dSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
6556830d754dSRandall Stewart 				TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
6557830d754dSRandall Stewart 				if (tp1->data) {
6558830d754dSRandall Stewart 					/* sa_ignore NO_NULL_CHK */
6559830d754dSRandall Stewart 					sctp_free_bufspace(stcb, asoc, tp1, 1);
6560830d754dSRandall Stewart 					sctp_m_freem(tp1->data);
6561830d754dSRandall Stewart 				}
6562830d754dSRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
6563830d754dSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
6564830d754dSRandall Stewart 					    cumack,
6565830d754dSRandall Stewart 					    tp1->rec.data.TSN_seq,
6566830d754dSRandall Stewart 					    0,
6567830d754dSRandall Stewart 					    0,
6568830d754dSRandall Stewart 					    SCTP_LOG_FREE_SENT);
6569830d754dSRandall Stewart 				}
6570830d754dSRandall Stewart 				tp1->data = NULL;
6571830d754dSRandall Stewart 				asoc->sent_queue_cnt--;
6572830d754dSRandall Stewart 				sctp_free_a_chunk(stcb, tp1);
6573830d754dSRandall Stewart 				tp1 = tp2;
6574830d754dSRandall Stewart 			} else {
6575830d754dSRandall Stewart 				break;
6576830d754dSRandall Stewart 			}
6577830d754dSRandall Stewart 		}
6578830d754dSRandall Stewart 
6579830d754dSRandall Stewart 	}
6580830d754dSRandall Stewart 	/* sa_ignore NO_NULL_CHK */
6581830d754dSRandall Stewart 	if (stcb->sctp_socket) {
6582830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
6583830d754dSRandall Stewart 		struct socket *so;
6584830d754dSRandall Stewart 
6585830d754dSRandall Stewart #endif
6586830d754dSRandall Stewart 
6587830d754dSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
6588830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
6589830d754dSRandall Stewart 			/* sa_ignore NO_NULL_CHK */
6590830d754dSRandall Stewart 			sctp_wakeup_log(stcb, cumack, 1, SCTP_WAKESND_FROM_SACK);
6591830d754dSRandall Stewart 		}
6592830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
6593830d754dSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
6594830d754dSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
6595830d754dSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
6596830d754dSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
6597830d754dSRandall Stewart 		SCTP_TCB_LOCK(stcb);
6598830d754dSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
6599830d754dSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
6600830d754dSRandall Stewart 			/* assoc was freed while we were unlocked */
6601830d754dSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
6602830d754dSRandall Stewart 			return;
6603830d754dSRandall Stewart 		}
6604830d754dSRandall Stewart #endif
6605830d754dSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
6606830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
6607830d754dSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
6608830d754dSRandall Stewart #endif
6609830d754dSRandall Stewart 	} else {
6610830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
6611830d754dSRandall Stewart 			sctp_wakeup_log(stcb, cumack, 1, SCTP_NOWAKE_FROM_SACK);
6612830d754dSRandall Stewart 		}
6613830d754dSRandall Stewart 	}
6614830d754dSRandall Stewart 
6615830d754dSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
6616830d754dSRandall Stewart 	if (asoc->last_acked_seq != cumack)
6617830d754dSRandall Stewart 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
6618830d754dSRandall Stewart 
6619830d754dSRandall Stewart 	asoc->last_acked_seq = cumack;
6620830d754dSRandall Stewart 
6621830d754dSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
6622830d754dSRandall Stewart 		/* nothing left in-flight */
6623830d754dSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6624830d754dSRandall Stewart 			net->flight_size = 0;
6625830d754dSRandall Stewart 			net->partial_bytes_acked = 0;
6626830d754dSRandall Stewart 		}
6627830d754dSRandall Stewart 		asoc->total_flight = 0;
6628830d754dSRandall Stewart 		asoc->total_flight_count = 0;
6629830d754dSRandall Stewart 	}
6630830d754dSRandall Stewart 	/* Fix up the a-p-a-p for future PR-SCTP sends */
6631830d754dSRandall Stewart 	if (compare_with_wrap(cumack, asoc->advanced_peer_ack_point, MAX_TSN)) {
6632830d754dSRandall Stewart 		asoc->advanced_peer_ack_point = cumack;
6633830d754dSRandall Stewart 	}
6634830d754dSRandall Stewart 	/* ECN Nonce updates */
6635830d754dSRandall Stewart 	if (asoc->ecn_nonce_allowed) {
6636830d754dSRandall Stewart 		if (asoc->nonce_sum_check) {
6637830d754dSRandall Stewart 			if (nonce_sum_flag != ((asoc->nonce_sum_expect_base) & SCTP_SACK_NONCE_SUM)) {
6638830d754dSRandall Stewart 				if (asoc->nonce_wait_for_ecne == 0) {
6639830d754dSRandall Stewart 					struct sctp_tmit_chunk *lchk;
6640830d754dSRandall Stewart 
6641830d754dSRandall Stewart 					lchk = TAILQ_FIRST(&asoc->send_queue);
6642830d754dSRandall Stewart 					asoc->nonce_wait_for_ecne = 1;
6643830d754dSRandall Stewart 					if (lchk) {
6644830d754dSRandall Stewart 						asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
6645830d754dSRandall Stewart 					} else {
6646830d754dSRandall Stewart 						asoc->nonce_wait_tsn = asoc->sending_seq;
6647830d754dSRandall Stewart 					}
6648830d754dSRandall Stewart 				} else {
6649830d754dSRandall Stewart 					if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
6650830d754dSRandall Stewart 					    (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
6651830d754dSRandall Stewart 						/*
6652830d754dSRandall Stewart 						 * Misbehaving peer. We need
6653830d754dSRandall Stewart 						 * to react to this guy
6654830d754dSRandall Stewart 						 */
6655830d754dSRandall Stewart 						asoc->ecn_allowed = 0;
6656830d754dSRandall Stewart 						asoc->ecn_nonce_allowed = 0;
6657830d754dSRandall Stewart 					}
6658830d754dSRandall Stewart 				}
6659830d754dSRandall Stewart 			}
6660830d754dSRandall Stewart 		} else {
6661830d754dSRandall Stewart 			/* See if Resynchronization Possible */
6662830d754dSRandall Stewart 			if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
6663830d754dSRandall Stewart 				asoc->nonce_sum_check = 1;
6664830d754dSRandall Stewart 				/*
6665830d754dSRandall Stewart 				 * now we must calculate what the base is.
6666830d754dSRandall Stewart 				 * We do this based on two things, we know
6667830d754dSRandall Stewart 				 * the total's for all the segments
6668830d754dSRandall Stewart 				 * gap-acked in the SACK (none), We also
6669830d754dSRandall Stewart 				 * know the SACK's nonce sum, its in
6670830d754dSRandall Stewart 				 * nonce_sum_flag. So we can build a truth
6671830d754dSRandall Stewart 				 * table to back-calculate the new value of
6672830d754dSRandall Stewart 				 * asoc->nonce_sum_expect_base:
6673830d754dSRandall Stewart 				 *
6674830d754dSRandall Stewart 				 * SACK-flag-Value         Seg-Sums Base 0 0 0
6675830d754dSRandall Stewart 				 * 1                    0 1 0 1 1 1 1 0
6676830d754dSRandall Stewart 				 */
6677830d754dSRandall Stewart 				asoc->nonce_sum_expect_base = (0 ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
6678830d754dSRandall Stewart 			}
6679830d754dSRandall Stewart 		}
6680830d754dSRandall Stewart 	}
6681830d754dSRandall Stewart 	/* RWND update */
6682830d754dSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
6683830d754dSRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
6684830d754dSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6685830d754dSRandall Stewart 		/* SWS sender side engages */
6686830d754dSRandall Stewart 		asoc->peers_rwnd = 0;
6687830d754dSRandall Stewart 	}
6688830d754dSRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
6689830d754dSRandall Stewart 		win_probe_recovery = 1;
6690830d754dSRandall Stewart 	}
6691830d754dSRandall Stewart 	/* Now assure a timer where data is queued at */
6692830d754dSRandall Stewart again:
6693830d754dSRandall Stewart 	j = 0;
6694830d754dSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
66955171328bSRandall Stewart 		int to_ticks;
66965171328bSRandall Stewart 
6697830d754dSRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
6698830d754dSRandall Stewart 			win_probe_recovered = 1;
6699830d754dSRandall Stewart 			/*
6700830d754dSRandall Stewart 			 * Find first chunk that was used with window probe
6701830d754dSRandall Stewart 			 * and clear the sent
6702830d754dSRandall Stewart 			 */
6703830d754dSRandall Stewart 			/* sa_ignore FREED_MEMORY */
6704830d754dSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
6705830d754dSRandall Stewart 				if (tp1->window_probe) {
6706830d754dSRandall Stewart 					/* move back to data send queue */
6707830d754dSRandall Stewart 					sctp_window_probe_recovery(stcb, asoc, net, tp1);
6708830d754dSRandall Stewart 					break;
6709830d754dSRandall Stewart 				}
6710830d754dSRandall Stewart 			}
6711830d754dSRandall Stewart 		}
6712830d754dSRandall Stewart 		if (net->RTO == 0) {
6713830d754dSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
6714830d754dSRandall Stewart 		} else {
6715830d754dSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
6716830d754dSRandall Stewart 		}
67175171328bSRandall Stewart 		if (net->flight_size) {
67185171328bSRandall Stewart 
6719830d754dSRandall Stewart 			j++;
6720830d754dSRandall Stewart 			(void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
6721830d754dSRandall Stewart 			    sctp_timeout_handler, &net->rxt_timer);
67225171328bSRandall Stewart 			if (net->window_probe) {
67235171328bSRandall Stewart 				net->window_probe = 0;
67245171328bSRandall Stewart 			}
6725830d754dSRandall Stewart 		} else {
67265171328bSRandall Stewart 			if (net->window_probe) {
67275171328bSRandall Stewart 				/*
67285171328bSRandall Stewart 				 * In window probes we must assure a timer
67295171328bSRandall Stewart 				 * is still running there
67305171328bSRandall Stewart 				 */
67315171328bSRandall Stewart 				net->window_probe = 0;
67325171328bSRandall Stewart 				(void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
67335171328bSRandall Stewart 				    sctp_timeout_handler, &net->rxt_timer);
67345171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
6735830d754dSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
6736830d754dSRandall Stewart 				    stcb, net,
6737830d754dSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
6738830d754dSRandall Stewart 			}
6739830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
6740830d754dSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
6741830d754dSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
6742830d754dSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
6743830d754dSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
6744830d754dSRandall Stewart 				}
6745830d754dSRandall Stewart 			}
6746830d754dSRandall Stewart 		}
6747830d754dSRandall Stewart 	}
6748830d754dSRandall Stewart 	if ((j == 0) &&
6749830d754dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
6750830d754dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
6751830d754dSRandall Stewart 	    (win_probe_recovered == 0) &&
6752830d754dSRandall Stewart 	    (done_once == 0)) {
67530c0982b8SRandall Stewart 		/*
67540c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
67550c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
67560c0982b8SRandall Stewart 		 */
67570c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
6758830d754dSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6759830d754dSRandall Stewart 				net->flight_size = 0;
6760830d754dSRandall Stewart 			}
6761830d754dSRandall Stewart 			asoc->total_flight = 0;
6762830d754dSRandall Stewart 			asoc->total_flight_count = 0;
6763830d754dSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
6764830d754dSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
6765830d754dSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
6766830d754dSRandall Stewart 					sctp_flight_size_increase(tp1);
6767830d754dSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
6768830d754dSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
6769830d754dSRandall Stewart 					asoc->sent_queue_retran_cnt++;
6770830d754dSRandall Stewart 				}
6771830d754dSRandall Stewart 			}
67720c0982b8SRandall Stewart 		}
6773830d754dSRandall Stewart 		done_once = 1;
6774830d754dSRandall Stewart 		goto again;
6775830d754dSRandall Stewart 	}
6776830d754dSRandall Stewart 	/**********************************/
6777830d754dSRandall Stewart 	/* Now what about shutdown issues */
6778830d754dSRandall Stewart 	/**********************************/
6779830d754dSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
6780830d754dSRandall Stewart 		/* nothing left on sendqueue.. consider done */
6781830d754dSRandall Stewart 		/* clean up */
6782830d754dSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
6783830d754dSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
6784830d754dSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
6785830d754dSRandall Stewart 		    (asoc->locked_on_sending)
6786830d754dSRandall Stewart 		    ) {
6787830d754dSRandall Stewart 			struct sctp_stream_queue_pending *sp;
6788830d754dSRandall Stewart 
6789830d754dSRandall Stewart 			/*
6790830d754dSRandall Stewart 			 * I may be in a state where we got all across.. but
6791830d754dSRandall Stewart 			 * cannot write more due to a shutdown... we abort
6792830d754dSRandall Stewart 			 * since the user did not indicate EOR in this case.
6793830d754dSRandall Stewart 			 * The sp will be cleaned during free of the asoc.
6794830d754dSRandall Stewart 			 */
6795830d754dSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
6796830d754dSRandall Stewart 			    sctp_streamhead);
6797830d754dSRandall Stewart 			if ((sp) && (sp->length == 0)) {
6798830d754dSRandall Stewart 				/* Let cleanup code purge it */
6799830d754dSRandall Stewart 				if (sp->msg_is_complete) {
6800830d754dSRandall Stewart 					asoc->stream_queue_cnt--;
6801830d754dSRandall Stewart 				} else {
6802830d754dSRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
6803830d754dSRandall Stewart 					asoc->locked_on_sending = NULL;
6804830d754dSRandall Stewart 					asoc->stream_queue_cnt--;
6805830d754dSRandall Stewart 				}
6806830d754dSRandall Stewart 			}
6807830d754dSRandall Stewart 		}
6808830d754dSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
6809830d754dSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
6810830d754dSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
6811830d754dSRandall Stewart 				/* Need to abort here */
6812830d754dSRandall Stewart 				struct mbuf *oper;
6813830d754dSRandall Stewart 
6814830d754dSRandall Stewart 		abort_out_now:
6815830d754dSRandall Stewart 				*abort_now = 1;
6816830d754dSRandall Stewart 				/* XXX */
6817830d754dSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
6818830d754dSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
6819830d754dSRandall Stewart 				if (oper) {
6820830d754dSRandall Stewart 					struct sctp_paramhdr *ph;
6821830d754dSRandall Stewart 					uint32_t *ippp;
6822830d754dSRandall Stewart 
6823830d754dSRandall Stewart 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
6824830d754dSRandall Stewart 					    sizeof(uint32_t);
6825830d754dSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
6826830d754dSRandall Stewart 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6827830d754dSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
6828830d754dSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
6829830d754dSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_24);
6830830d754dSRandall Stewart 				}
6831830d754dSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
6832830d754dSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
6833830d754dSRandall Stewart 			} else {
6834830d754dSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
6835830d754dSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
6836830d754dSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6837830d754dSRandall Stewart 				}
6838830d754dSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
6839830d754dSRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6840830d754dSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
6841830d754dSRandall Stewart 				sctp_send_shutdown(stcb,
6842830d754dSRandall Stewart 				    stcb->asoc.primary_destination);
6843830d754dSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
6844830d754dSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
6845830d754dSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
6846830d754dSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
6847830d754dSRandall Stewart 			}
6848830d754dSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
6849830d754dSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
6850830d754dSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
6851830d754dSRandall Stewart 				goto abort_out_now;
6852830d754dSRandall Stewart 			}
6853830d754dSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6854830d754dSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
6855830d754dSRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6856830d754dSRandall Stewart 			sctp_send_shutdown_ack(stcb,
6857830d754dSRandall Stewart 			    stcb->asoc.primary_destination);
6858830d754dSRandall Stewart 
6859830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
6860830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, asoc->primary_destination);
6861830d754dSRandall Stewart 		}
6862830d754dSRandall Stewart 	}
6863830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
6864830d754dSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
6865830d754dSRandall Stewart 		    rwnd,
6866830d754dSRandall Stewart 		    stcb->asoc.peers_rwnd,
6867830d754dSRandall Stewart 		    stcb->asoc.total_flight,
6868830d754dSRandall Stewart 		    stcb->asoc.total_output_queue_size);
6869830d754dSRandall Stewart 	}
6870830d754dSRandall Stewart }
6871830d754dSRandall Stewart 
6872830d754dSRandall Stewart /* EY! nr_sack version of sctp_handle_segments, nr-gapped TSNs get removed from RtxQ in this method*/
6873830d754dSRandall Stewart static void
6874830d754dSRandall Stewart sctp_handle_nr_sack_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
6875830d754dSRandall Stewart     struct sctp_nr_sack_chunk *ch, uint32_t last_tsn, uint32_t * biggest_tsn_acked,
6876830d754dSRandall Stewart     uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
6877830d754dSRandall Stewart     uint32_t num_seg, uint32_t num_nr_seg, int *ecn_seg_sums)
6878830d754dSRandall Stewart {
6879830d754dSRandall Stewart 	/************************************************/
6880830d754dSRandall Stewart 	/* process fragments and update sendqueue        */
6881830d754dSRandall Stewart 	/************************************************/
6882830d754dSRandall Stewart 	struct sctp_nr_sack *nr_sack;
6883830d754dSRandall Stewart 	struct sctp_gap_ack_block *frag, block;
6884830d754dSRandall Stewart 	struct sctp_nr_gap_ack_block *nr_frag, nr_block;
6885830d754dSRandall Stewart 	struct sctp_tmit_chunk *tp1;
68860fa753b3SRandall Stewart 	uint32_t i;
6887830d754dSRandall Stewart 	int wake_him = 0;
6888830d754dSRandall Stewart 	int num_frs = 0;
6889830d754dSRandall Stewart 
6890830d754dSRandall Stewart 	uint16_t frag_strt, frag_end, primary_flag_set;
6891830d754dSRandall Stewart 	uint16_t nr_frag_strt, nr_frag_end;
6892830d754dSRandall Stewart 
6893830d754dSRandall Stewart 	uint32_t last_frag_high;
6894830d754dSRandall Stewart 	uint32_t last_nr_frag_high;
6895830d754dSRandall Stewart 
6896830d754dSRandall Stewart 	/*
6897830d754dSRandall Stewart 	 * @@@ JRI : TODO: This flag is not used anywhere .. remove?
6898830d754dSRandall Stewart 	 */
6899830d754dSRandall Stewart 	if (asoc->primary_destination->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
6900830d754dSRandall Stewart 		primary_flag_set = 1;
6901830d754dSRandall Stewart 	} else {
6902830d754dSRandall Stewart 		primary_flag_set = 0;
6903830d754dSRandall Stewart 	}
6904830d754dSRandall Stewart 	nr_sack = &ch->nr_sack;
6905830d754dSRandall Stewart 
6906830d754dSRandall Stewart 	/*
6907830d754dSRandall Stewart 	 * EY! - I will process nr_gaps similarly,by going to this position
6908830d754dSRandall Stewart 	 * again if All bit is set
6909830d754dSRandall Stewart 	 */
6910830d754dSRandall Stewart 	frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
6911830d754dSRandall Stewart 	    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
6912830d754dSRandall Stewart 	*offset += sizeof(block);
6913830d754dSRandall Stewart 	if (frag == NULL) {
6914830d754dSRandall Stewart 		return;
6915830d754dSRandall Stewart 	}
6916830d754dSRandall Stewart 	tp1 = NULL;
6917830d754dSRandall Stewart 	last_frag_high = 0;
6918830d754dSRandall Stewart 	for (i = 0; i < num_seg; i++) {
6919830d754dSRandall Stewart 		frag_strt = ntohs(frag->start);
6920830d754dSRandall Stewart 		frag_end = ntohs(frag->end);
6921830d754dSRandall Stewart 		/* some sanity checks on the fargment offsets */
6922830d754dSRandall Stewart 		if (frag_strt > frag_end) {
6923830d754dSRandall Stewart 			/* this one is malformed, skip */
6924830d754dSRandall Stewart 			frag++;
6925830d754dSRandall Stewart 			continue;
6926830d754dSRandall Stewart 		}
6927830d754dSRandall Stewart 		if (compare_with_wrap((frag_end + last_tsn), *biggest_tsn_acked,
6928830d754dSRandall Stewart 		    MAX_TSN))
6929830d754dSRandall Stewart 			*biggest_tsn_acked = frag_end + last_tsn;
6930830d754dSRandall Stewart 
6931830d754dSRandall Stewart 		/* mark acked dgs and find out the highestTSN being acked */
6932830d754dSRandall Stewart 		if (tp1 == NULL) {
6933830d754dSRandall Stewart 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
6934830d754dSRandall Stewart 
6935830d754dSRandall Stewart 			/* save the locations of the last frags */
6936830d754dSRandall Stewart 			last_frag_high = frag_end + last_tsn;
6937830d754dSRandall Stewart 		} else {
6938830d754dSRandall Stewart 			/*
6939830d754dSRandall Stewart 			 * now lets see if we need to reset the queue due to
6940830d754dSRandall Stewart 			 * a out-of-order SACK fragment
6941830d754dSRandall Stewart 			 */
6942830d754dSRandall Stewart 			if (compare_with_wrap(frag_strt + last_tsn,
6943830d754dSRandall Stewart 			    last_frag_high, MAX_TSN)) {
6944830d754dSRandall Stewart 				/*
6945830d754dSRandall Stewart 				 * if the new frag starts after the last TSN
6946830d754dSRandall Stewart 				 * frag covered, we are ok and this one is
6947830d754dSRandall Stewart 				 * beyond the last one
6948830d754dSRandall Stewart 				 */
6949830d754dSRandall Stewart 				;
6950830d754dSRandall Stewart 			} else {
6951830d754dSRandall Stewart 				/*
6952830d754dSRandall Stewart 				 * ok, they have reset us, so we need to
6953830d754dSRandall Stewart 				 * reset the queue this will cause extra
6954830d754dSRandall Stewart 				 * hunting but hey, they chose the
6955830d754dSRandall Stewart 				 * performance hit when they failed to order
6956830d754dSRandall Stewart 				 * there gaps..
6957830d754dSRandall Stewart 				 */
6958830d754dSRandall Stewart 				tp1 = TAILQ_FIRST(&asoc->sent_queue);
6959830d754dSRandall Stewart 			}
6960830d754dSRandall Stewart 			last_frag_high = frag_end + last_tsn;
6961830d754dSRandall Stewart 		}
69620fa753b3SRandall Stewart 		sctp_process_segment_range(stcb, &tp1, last_tsn, frag_strt, frag_end,
69630fa753b3SRandall Stewart 		    0, &num_frs, biggest_newly_acked_tsn,
69640fa753b3SRandall Stewart 		    this_sack_lowest_newack, ecn_seg_sums);
6965830d754dSRandall Stewart 		frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
6966830d754dSRandall Stewart 		    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
6967830d754dSRandall Stewart 		*offset += sizeof(block);
6968830d754dSRandall Stewart 		if (frag == NULL) {
6969830d754dSRandall Stewart 			break;
6970830d754dSRandall Stewart 		}
6971830d754dSRandall Stewart 	}
6972830d754dSRandall Stewart 
6973830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
6974830d754dSRandall Stewart 		if (num_frs)
6975830d754dSRandall Stewart 			sctp_log_fr(*biggest_tsn_acked,
6976830d754dSRandall Stewart 			    *biggest_newly_acked_tsn,
6977830d754dSRandall Stewart 			    last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
6978830d754dSRandall Stewart 	}
6979830d754dSRandall Stewart 	nr_frag = (struct sctp_nr_gap_ack_block *)sctp_m_getptr(m, *offset,
6980830d754dSRandall Stewart 	    sizeof(struct sctp_nr_gap_ack_block), (uint8_t *) & nr_block);
6981830d754dSRandall Stewart 	*offset += sizeof(nr_block);
6982830d754dSRandall Stewart 
6983830d754dSRandall Stewart 
6984830d754dSRandall Stewart 
6985830d754dSRandall Stewart 	if (nr_frag == NULL) {
6986830d754dSRandall Stewart 		return;
6987830d754dSRandall Stewart 	}
6988830d754dSRandall Stewart 	tp1 = NULL;
6989830d754dSRandall Stewart 	last_nr_frag_high = 0;
69900fa753b3SRandall Stewart 	/* Reset to beginning for the nr_sack section */
69910fa753b3SRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
6992830d754dSRandall Stewart 
6993830d754dSRandall Stewart 	for (i = 0; i < num_nr_seg; i++) {
6994830d754dSRandall Stewart 
6995830d754dSRandall Stewart 		nr_frag_strt = ntohs(nr_frag->start);
6996830d754dSRandall Stewart 		nr_frag_end = ntohs(nr_frag->end);
6997830d754dSRandall Stewart 
6998830d754dSRandall Stewart 		/* some sanity checks on the nr fargment offsets */
6999830d754dSRandall Stewart 		if (nr_frag_strt > nr_frag_end) {
7000830d754dSRandall Stewart 			/* this one is malformed, skip */
7001830d754dSRandall Stewart 			nr_frag++;
7002830d754dSRandall Stewart 			continue;
7003830d754dSRandall Stewart 		}
7004d50c1d79SRandall Stewart 		/* mark acked dgs and find out the highestTSN being acked */
7005830d754dSRandall Stewart 		if (tp1 == NULL) {
7006830d754dSRandall Stewart 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
7007830d754dSRandall Stewart 
7008830d754dSRandall Stewart 			/* save the locations of the last frags */
7009830d754dSRandall Stewart 			last_nr_frag_high = nr_frag_end + last_tsn;
7010830d754dSRandall Stewart 		} else {
7011830d754dSRandall Stewart 			/*
7012d50c1d79SRandall Stewart 			 * now lets see if we need to reset the queue due to
7013d50c1d79SRandall Stewart 			 * a out-of-order SACK fragment
7014830d754dSRandall Stewart 			 */
7015830d754dSRandall Stewart 			if (compare_with_wrap(nr_frag_strt + last_tsn,
7016830d754dSRandall Stewart 			    last_nr_frag_high, MAX_TSN)) {
7017830d754dSRandall Stewart 				/*
7018d50c1d79SRandall Stewart 				 * if the new frag starts after the last TSN
7019d50c1d79SRandall Stewart 				 * frag covered, we are ok and this one is
7020d50c1d79SRandall Stewart 				 * beyond the last one
7021830d754dSRandall Stewart 				 */
7022830d754dSRandall Stewart 				;
7023830d754dSRandall Stewart 			} else {
7024830d754dSRandall Stewart 				/*
7025d50c1d79SRandall Stewart 				 * ok, they have reset us, so we need to
7026d50c1d79SRandall Stewart 				 * reset the queue this will cause extra
7027d50c1d79SRandall Stewart 				 * hunting but hey, they chose the
7028d50c1d79SRandall Stewart 				 * performance hit when they failed to order
7029d50c1d79SRandall Stewart 				 * there gaps..
7030830d754dSRandall Stewart 				 */
7031830d754dSRandall Stewart 				tp1 = TAILQ_FIRST(&asoc->sent_queue);
7032830d754dSRandall Stewart 			}
7033830d754dSRandall Stewart 			last_nr_frag_high = nr_frag_end + last_tsn;
7034830d754dSRandall Stewart 		}
70350fa753b3SRandall Stewart 		num_frs = 0;
70360fa753b3SRandall Stewart 		wake_him = sctp_process_segment_range(stcb, &tp1, last_tsn,
70370fa753b3SRandall Stewart 		    nr_frag_strt, nr_frag_end, 1,
70380fa753b3SRandall Stewart 		    &num_frs, biggest_newly_acked_tsn,
70390fa753b3SRandall Stewart 		    this_sack_lowest_newack, ecn_seg_sums);
7040830d754dSRandall Stewart 
7041830d754dSRandall Stewart 		nr_frag = (struct sctp_nr_gap_ack_block *)sctp_m_getptr(m, *offset,
70420fa753b3SRandall Stewart 		    sizeof(struct sctp_nr_gap_ack_block),
70430fa753b3SRandall Stewart 		    (uint8_t *) & nr_block);
7044830d754dSRandall Stewart 		*offset += sizeof(nr_block);
7045830d754dSRandall Stewart 		if (nr_frag == NULL) {
7046830d754dSRandall Stewart 			break;
7047830d754dSRandall Stewart 		}
7048830d754dSRandall Stewart 	}
7049d50c1d79SRandall Stewart 
7050830d754dSRandall Stewart 	/*
7051830d754dSRandall Stewart 	 * EY- wake up the socket if things have been removed from the sent
7052830d754dSRandall Stewart 	 * queue
7053830d754dSRandall Stewart 	 */
7054830d754dSRandall Stewart 	if ((wake_him) && (stcb->sctp_socket)) {
7055830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7056830d754dSRandall Stewart 		struct socket *so;
7057830d754dSRandall Stewart 
7058830d754dSRandall Stewart #endif
7059830d754dSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
7060830d754dSRandall Stewart 		/*
7061830d754dSRandall Stewart 		 * if (SCTP_BASE_SYSCTL(sctp_logging_level) &
7062830d754dSRandall Stewart 		 * SCTP_WAKE_LOGGING_ENABLE) { sctp_wakeup_log(stcb,
7063830d754dSRandall Stewart 		 * cum_ack, wake_him, SCTP_WAKESND_FROM_SACK);}
7064830d754dSRandall Stewart 		 */
7065830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7066830d754dSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
7067830d754dSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
7068830d754dSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
7069830d754dSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
7070830d754dSRandall Stewart 		SCTP_TCB_LOCK(stcb);
7071830d754dSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
7072830d754dSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
7073830d754dSRandall Stewart 			/* assoc was freed while we were unlocked */
7074830d754dSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
7075830d754dSRandall Stewart 			return;
7076830d754dSRandall Stewart 		}
7077830d754dSRandall Stewart #endif
7078830d754dSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
7079830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7080830d754dSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
7081830d754dSRandall Stewart #endif
7082830d754dSRandall Stewart 	}			/* else { if
7083830d754dSRandall Stewart 				 * (SCTP_BASE_SYSCTL(sctp_logging_level) &
7084830d754dSRandall Stewart 				 * SCTP_WAKE_LOGGING_ENABLE) {
7085830d754dSRandall Stewart 				 * sctp_wakeup_log(stcb, cum_ack, wake_him,
7086830d754dSRandall Stewart 				 * SCTP_NOWAKE_FROM_SACK); } } */
7087830d754dSRandall Stewart }
7088830d754dSRandall Stewart 
7089830d754dSRandall Stewart /* EY- nr_sack */
7090830d754dSRandall Stewart /* Identifies the non-renegable tsns that are revoked*/
7091830d754dSRandall Stewart static void
7092830d754dSRandall Stewart sctp_check_for_nr_revoked(struct sctp_tcb *stcb,
7093830d754dSRandall Stewart     struct sctp_association *asoc, uint32_t cumack,
7094830d754dSRandall Stewart     u_long biggest_tsn_acked)
7095830d754dSRandall Stewart {
7096830d754dSRandall Stewart 	struct sctp_tmit_chunk *tp1;
7097830d754dSRandall Stewart 
7098830d754dSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
7099830d754dSRandall Stewart 	while (tp1) {
7100830d754dSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq, cumack,
7101830d754dSRandall Stewart 		    MAX_TSN)) {
7102830d754dSRandall Stewart 			/*
7103830d754dSRandall Stewart 			 * ok this guy is either ACK or MARKED. If it is
7104830d754dSRandall Stewart 			 * ACKED it has been previously acked but not this
7105830d754dSRandall Stewart 			 * time i.e. revoked.  If it is MARKED it was ACK'ed
7106830d754dSRandall Stewart 			 * again.
7107830d754dSRandall Stewart 			 */
7108830d754dSRandall Stewart 			if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
7109830d754dSRandall Stewart 			    MAX_TSN))
7110830d754dSRandall Stewart 				break;
7111830d754dSRandall Stewart 
7112830d754dSRandall Stewart 
7113830d754dSRandall Stewart 			if (tp1->sent == SCTP_DATAGRAM_NR_ACKED) {
7114830d754dSRandall Stewart 				/*
7115830d754dSRandall Stewart 				 * EY! a non-renegable TSN is revoked, need
7116830d754dSRandall Stewart 				 * to abort the association
7117830d754dSRandall Stewart 				 */
7118830d754dSRandall Stewart 				/*
7119830d754dSRandall Stewart 				 * EY TODO: put in the code to abort the
7120830d754dSRandall Stewart 				 * assoc.
7121830d754dSRandall Stewart 				 */
7122830d754dSRandall Stewart 				return;
7123830d754dSRandall Stewart 			} else if (tp1->sent == SCTP_DATAGRAM_NR_MARKED) {
7124830d754dSRandall Stewart 				/* it has been re-acked in this SACK */
7125830d754dSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_NR_ACKED;
7126830d754dSRandall Stewart 			}
7127830d754dSRandall Stewart 		}
7128830d754dSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT)
7129830d754dSRandall Stewart 			break;
7130830d754dSRandall Stewart 		tp1 = TAILQ_NEXT(tp1, sctp_next);
7131830d754dSRandall Stewart 	}
7132830d754dSRandall Stewart }
7133830d754dSRandall Stewart 
7134830d754dSRandall Stewart /* EY! nr_sack version of sctp_handle_sack, nr_gap_ack processing should be added to this method*/
7135830d754dSRandall Stewart void
7136830d754dSRandall Stewart sctp_handle_nr_sack(struct mbuf *m, int offset,
7137830d754dSRandall Stewart     struct sctp_nr_sack_chunk *ch, struct sctp_tcb *stcb,
7138830d754dSRandall Stewart     struct sctp_nets *net_from, int *abort_now, int nr_sack_len, uint32_t rwnd)
7139830d754dSRandall Stewart {
7140830d754dSRandall Stewart 	struct sctp_association *asoc;
7141830d754dSRandall Stewart 
7142830d754dSRandall Stewart 	/* EY sack */
7143830d754dSRandall Stewart 	struct sctp_nr_sack *nr_sack;
7144830d754dSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
7145830d754dSRandall Stewart 	uint32_t cum_ack, last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked,
7146830d754dSRandall Stewart 	         this_sack_lowest_newack;
7147830d754dSRandall Stewart 	uint32_t sav_cum_ack;
7148830d754dSRandall Stewart 
7149830d754dSRandall Stewart 	/* EY num_seg */
7150830d754dSRandall Stewart 	uint16_t num_seg, num_nr_seg, num_dup;
7151830d754dSRandall Stewart 	uint16_t wake_him = 0;
7152830d754dSRandall Stewart 	unsigned int nr_sack_length;
7153830d754dSRandall Stewart 	uint32_t send_s = 0;
7154830d754dSRandall Stewart 	long j;
7155830d754dSRandall Stewart 	int accum_moved = 0;
7156830d754dSRandall Stewart 	int will_exit_fast_recovery = 0;
7157830d754dSRandall Stewart 	uint32_t a_rwnd, old_rwnd;
7158830d754dSRandall Stewart 	int win_probe_recovery = 0;
7159830d754dSRandall Stewart 	int win_probe_recovered = 0;
7160830d754dSRandall Stewart 	struct sctp_nets *net = NULL;
7161d50c1d79SRandall Stewart 	int nonce_sum_flag, ecn_seg_sums = 0;
7162830d754dSRandall Stewart 	int done_once;
7163830d754dSRandall Stewart 	uint8_t reneged_all = 0;
7164830d754dSRandall Stewart 	uint8_t cmt_dac_flag;
7165830d754dSRandall Stewart 
7166830d754dSRandall Stewart 	/*
7167830d754dSRandall Stewart 	 * we take any chance we can to service our queues since we cannot
7168830d754dSRandall Stewart 	 * get awoken when the socket is read from :<
7169830d754dSRandall Stewart 	 */
7170830d754dSRandall Stewart 	/*
7171830d754dSRandall Stewart 	 * Now perform the actual SACK handling: 1) Verify that it is not an
7172830d754dSRandall Stewart 	 * old sack, if so discard. 2) If there is nothing left in the send
7173830d754dSRandall Stewart 	 * queue (cum-ack is equal to last acked) then you have a duplicate
7174830d754dSRandall Stewart 	 * too, update any rwnd change and verify no timers are running.
7175830d754dSRandall Stewart 	 * then return. 3) Process any new consequtive data i.e. cum-ack
7176830d754dSRandall Stewart 	 * moved process these first and note that it moved. 4) Process any
7177830d754dSRandall Stewart 	 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
7178830d754dSRandall Stewart 	 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
7179830d754dSRandall Stewart 	 * sync up flightsizes and things, stop all timers and also check
7180830d754dSRandall Stewart 	 * for shutdown_pending state. If so then go ahead and send off the
7181830d754dSRandall Stewart 	 * shutdown. If in shutdown recv, send off the shutdown-ack and
7182830d754dSRandall Stewart 	 * start that timer, Ret. 9) Strike any non-acked things and do FR
7183830d754dSRandall Stewart 	 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
7184830d754dSRandall Stewart 	 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
7185830d754dSRandall Stewart 	 * if in shutdown_recv state.
7186830d754dSRandall Stewart 	 */
7187830d754dSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
7188830d754dSRandall Stewart 	nr_sack = &ch->nr_sack;
7189830d754dSRandall Stewart 	/* CMT DAC algo */
7190830d754dSRandall Stewart 	this_sack_lowest_newack = 0;
7191830d754dSRandall Stewart 	j = 0;
7192830d754dSRandall Stewart 	nr_sack_length = (unsigned int)nr_sack_len;
7193830d754dSRandall Stewart 	/* ECN Nonce */
7194830d754dSRandall Stewart 	SCTP_STAT_INCR(sctps_slowpath_sack);
7195830d754dSRandall Stewart 	nonce_sum_flag = ch->ch.chunk_flags & SCTP_SACK_NONCE_SUM;
7196830d754dSRandall Stewart 	cum_ack = last_tsn = ntohl(nr_sack->cum_tsn_ack);
7197830d754dSRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
7198830d754dSRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
7199830d754dSRandall Stewart 	stcb->asoc.cumack_log_at++;
7200830d754dSRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
7201830d754dSRandall Stewart 		stcb->asoc.cumack_log_at = 0;
7202830d754dSRandall Stewart 	}
7203830d754dSRandall Stewart #endif
7204830d754dSRandall Stewart 	num_seg = ntohs(nr_sack->num_gap_ack_blks);
7205830d754dSRandall Stewart 	num_nr_seg = ntohs(nr_sack->num_nr_gap_ack_blks);
7206830d754dSRandall Stewart 	a_rwnd = rwnd;
7207830d754dSRandall Stewart 
7208830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
7209830d754dSRandall Stewart 		sctp_misc_ints(SCTP_SACK_LOG_NORMAL, cum_ack,
7210830d754dSRandall Stewart 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
7211830d754dSRandall Stewart 	}
7212830d754dSRandall Stewart 	/* CMT DAC algo */
7213830d754dSRandall Stewart 	cmt_dac_flag = ch->ch.chunk_flags & SCTP_SACK_CMT_DAC;
7214830d754dSRandall Stewart 	num_dup = ntohs(nr_sack->num_dup_tsns);
7215830d754dSRandall Stewart 
7216830d754dSRandall Stewart 	old_rwnd = stcb->asoc.peers_rwnd;
7217830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
7218830d754dSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
7219830d754dSRandall Stewart 		    stcb->asoc.overall_error_count,
7220830d754dSRandall Stewart 		    0,
7221830d754dSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
7222830d754dSRandall Stewart 		    __LINE__);
7223830d754dSRandall Stewart 	}
7224830d754dSRandall Stewart 	stcb->asoc.overall_error_count = 0;
7225830d754dSRandall Stewart 	asoc = &stcb->asoc;
7226830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
7227830d754dSRandall Stewart 		sctp_log_sack(asoc->last_acked_seq,
7228830d754dSRandall Stewart 		    cum_ack,
7229830d754dSRandall Stewart 		    0,
7230830d754dSRandall Stewart 		    num_seg,
7231830d754dSRandall Stewart 		    num_dup,
7232830d754dSRandall Stewart 		    SCTP_LOG_NEW_SACK);
7233830d754dSRandall Stewart 	}
7234830d754dSRandall Stewart 	if ((num_dup) && (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_FR_LOGGING_ENABLE | SCTP_EARLYFR_LOGGING_ENABLE))) {
7235830d754dSRandall Stewart 		int off_to_dup, iii;
7236830d754dSRandall Stewart 		uint32_t *dupdata, dblock;
7237830d754dSRandall Stewart 
7238830d754dSRandall Stewart 		off_to_dup = (num_seg * sizeof(struct sctp_gap_ack_block)) +
7239830d754dSRandall Stewart 		    (num_nr_seg * sizeof(struct sctp_nr_gap_ack_block)) + sizeof(struct sctp_nr_sack_chunk);
7240830d754dSRandall Stewart 		if ((off_to_dup + (num_dup * sizeof(uint32_t))) <= nr_sack_length) {
7241830d754dSRandall Stewart 			dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
7242830d754dSRandall Stewart 			    sizeof(uint32_t), (uint8_t *) & dblock);
7243830d754dSRandall Stewart 			off_to_dup += sizeof(uint32_t);
7244830d754dSRandall Stewart 			if (dupdata) {
7245830d754dSRandall Stewart 				for (iii = 0; iii < num_dup; iii++) {
7246830d754dSRandall Stewart 					sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
7247830d754dSRandall Stewart 					dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
7248830d754dSRandall Stewart 					    sizeof(uint32_t), (uint8_t *) & dblock);
7249830d754dSRandall Stewart 					if (dupdata == NULL)
7250830d754dSRandall Stewart 						break;
7251830d754dSRandall Stewart 					off_to_dup += sizeof(uint32_t);
7252830d754dSRandall Stewart 				}
7253830d754dSRandall Stewart 			}
7254830d754dSRandall Stewart 		} else {
7255830d754dSRandall Stewart 			SCTP_PRINTF("Size invalid offset to dups:%d number dups:%d nr_sack_len:%d num gaps:%d num nr_gaps:%d\n",
7256830d754dSRandall Stewart 			    off_to_dup, num_dup, nr_sack_length, num_seg, num_nr_seg);
7257830d754dSRandall Stewart 		}
7258830d754dSRandall Stewart 	}
7259830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
7260830d754dSRandall Stewart 		/* reality check */
7261830d754dSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
7262830d754dSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
7263830d754dSRandall Stewart 			    sctpchunk_listhead);
7264830d754dSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
7265830d754dSRandall Stewart 		} else {
7266830d754dSRandall Stewart 			send_s = asoc->sending_seq;
7267830d754dSRandall Stewart 		}
7268830d754dSRandall Stewart 		if (cum_ack == send_s ||
7269830d754dSRandall Stewart 		    compare_with_wrap(cum_ack, send_s, MAX_TSN)) {
7270830d754dSRandall Stewart #ifndef INVARIANTS
7271830d754dSRandall Stewart 			struct mbuf *oper;
7272830d754dSRandall Stewart 
7273830d754dSRandall Stewart #endif
7274830d754dSRandall Stewart #ifdef INVARIANTS
7275830d754dSRandall Stewart 	hopeless_peer:
7276830d754dSRandall Stewart 			panic("Impossible sack 1");
7277830d754dSRandall Stewart #else
7278830d754dSRandall Stewart 
7279830d754dSRandall Stewart 
7280830d754dSRandall Stewart 			/*
7281830d754dSRandall Stewart 			 * no way, we have not even sent this TSN out yet.
7282830d754dSRandall Stewart 			 * Peer is hopelessly messed up with us.
7283830d754dSRandall Stewart 			 */
7284830d754dSRandall Stewart 	hopeless_peer:
7285830d754dSRandall Stewart 			*abort_now = 1;
7286830d754dSRandall Stewart 			/* XXX */
7287830d754dSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
7288830d754dSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
7289830d754dSRandall Stewart 			if (oper) {
7290830d754dSRandall Stewart 				struct sctp_paramhdr *ph;
7291830d754dSRandall Stewart 				uint32_t *ippp;
7292830d754dSRandall Stewart 
7293830d754dSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
7294830d754dSRandall Stewart 				    sizeof(uint32_t);
7295830d754dSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
7296830d754dSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
7297830d754dSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
7298830d754dSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
7299830d754dSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
7300830d754dSRandall Stewart 			}
7301830d754dSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
7302830d754dSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
7303830d754dSRandall Stewart 			return;
7304830d754dSRandall Stewart #endif
7305830d754dSRandall Stewart 		}
7306830d754dSRandall Stewart 	}
7307830d754dSRandall Stewart 	/**********************/
7308830d754dSRandall Stewart 	/* 1) check the range */
7309830d754dSRandall Stewart 	/**********************/
7310830d754dSRandall Stewart 	if (compare_with_wrap(asoc->last_acked_seq, last_tsn, MAX_TSN)) {
7311830d754dSRandall Stewart 		/* acking something behind */
7312830d754dSRandall Stewart 		return;
7313830d754dSRandall Stewart 	}
7314830d754dSRandall Stewart 	sav_cum_ack = asoc->last_acked_seq;
7315830d754dSRandall Stewart 
7316830d754dSRandall Stewart 	/* update the Rwnd of the peer */
7317830d754dSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue) &&
7318830d754dSRandall Stewart 	    TAILQ_EMPTY(&asoc->send_queue) &&
7319830d754dSRandall Stewart 	    (asoc->stream_queue_cnt == 0)
7320830d754dSRandall Stewart 	    ) {
7321830d754dSRandall Stewart 		/* nothing left on send/sent and strmq */
7322830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
7323830d754dSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
7324830d754dSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
7325830d754dSRandall Stewart 		}
7326830d754dSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
7327830d754dSRandall Stewart 		if (asoc->sent_queue_retran_cnt) {
7328830d754dSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
7329830d754dSRandall Stewart 		}
7330830d754dSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
7331830d754dSRandall Stewart 			/* SWS sender side engages */
7332830d754dSRandall Stewart 			asoc->peers_rwnd = 0;
7333830d754dSRandall Stewart 		}
7334830d754dSRandall Stewart 		/* stop any timers */
7335830d754dSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7336830d754dSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
7337830d754dSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
7338830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
7339830d754dSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
7340830d754dSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck1);
7341830d754dSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
7342830d754dSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
7343830d754dSRandall Stewart 				}
7344830d754dSRandall Stewart 			}
7345830d754dSRandall Stewart 			net->partial_bytes_acked = 0;
7346830d754dSRandall Stewart 			net->flight_size = 0;
7347830d754dSRandall Stewart 		}
7348830d754dSRandall Stewart 		asoc->total_flight = 0;
7349830d754dSRandall Stewart 		asoc->total_flight_count = 0;
7350830d754dSRandall Stewart 		return;
7351830d754dSRandall Stewart 	}
7352830d754dSRandall Stewart 	/*
7353830d754dSRandall Stewart 	 * We init netAckSz and netAckSz2 to 0. These are used to track 2
7354830d754dSRandall Stewart 	 * things. The total byte count acked is tracked in netAckSz AND
7355830d754dSRandall Stewart 	 * netAck2 is used to track the total bytes acked that are un-
7356830d754dSRandall Stewart 	 * amibguious and were never retransmitted. We track these on a per
7357830d754dSRandall Stewart 	 * destination address basis.
7358830d754dSRandall Stewart 	 */
7359830d754dSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7360830d754dSRandall Stewart 		net->prev_cwnd = net->cwnd;
7361830d754dSRandall Stewart 		net->net_ack = 0;
7362830d754dSRandall Stewart 		net->net_ack2 = 0;
7363830d754dSRandall Stewart 
7364830d754dSRandall Stewart 		/*
7365830d754dSRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
7366830d754dSRandall Stewart 		 * SACK processing
7367830d754dSRandall Stewart 		 */
7368830d754dSRandall Stewart 		net->new_pseudo_cumack = 0;
7369830d754dSRandall Stewart 		net->will_exit_fast_recovery = 0;
7370830d754dSRandall Stewart 	}
7371830d754dSRandall Stewart 	/* process the new consecutive TSN first */
7372830d754dSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
7373830d754dSRandall Stewart 	while (tp1) {
7374830d754dSRandall Stewart 		if (compare_with_wrap(last_tsn, tp1->rec.data.TSN_seq,
7375830d754dSRandall Stewart 		    MAX_TSN) ||
7376830d754dSRandall Stewart 		    last_tsn == tp1->rec.data.TSN_seq) {
7377830d754dSRandall Stewart 			if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
7378830d754dSRandall Stewart 				/*
7379830d754dSRandall Stewart 				 * ECN Nonce: Add the nonce to the sender's
7380830d754dSRandall Stewart 				 * nonce sum
7381830d754dSRandall Stewart 				 */
7382830d754dSRandall Stewart 				asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
7383830d754dSRandall Stewart 				accum_moved = 1;
7384830d754dSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
7385830d754dSRandall Stewart 					/*
7386830d754dSRandall Stewart 					 * If it is less than ACKED, it is
7387830d754dSRandall Stewart 					 * now no-longer in flight. Higher
7388830d754dSRandall Stewart 					 * values may occur during marking
7389830d754dSRandall Stewart 					 */
7390830d754dSRandall Stewart 					if ((tp1->whoTo->dest_state &
7391830d754dSRandall Stewart 					    SCTP_ADDR_UNCONFIRMED) &&
7392830d754dSRandall Stewart 					    (tp1->snd_count < 2)) {
7393830d754dSRandall Stewart 						/*
7394830d754dSRandall Stewart 						 * If there was no retran
7395830d754dSRandall Stewart 						 * and the address is
7396830d754dSRandall Stewart 						 * un-confirmed and we sent
7397830d754dSRandall Stewart 						 * there and are now
7398830d754dSRandall Stewart 						 * sacked.. its confirmed,
7399830d754dSRandall Stewart 						 * mark it so.
7400830d754dSRandall Stewart 						 */
7401830d754dSRandall Stewart 						tp1->whoTo->dest_state &=
7402830d754dSRandall Stewart 						    ~SCTP_ADDR_UNCONFIRMED;
7403830d754dSRandall Stewart 					}
7404830d754dSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
7405830d754dSRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
7406830d754dSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
7407830d754dSRandall Stewart 							    tp1->whoTo->flight_size,
7408830d754dSRandall Stewart 							    tp1->book_size,
7409830d754dSRandall Stewart 							    (uintptr_t) tp1->whoTo,
7410830d754dSRandall Stewart 							    tp1->rec.data.TSN_seq);
7411830d754dSRandall Stewart 						}
7412830d754dSRandall Stewart 						sctp_flight_size_decrease(tp1);
7413830d754dSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
7414830d754dSRandall Stewart 					}
7415830d754dSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
7416830d754dSRandall Stewart 
7417830d754dSRandall Stewart 					/* CMT SFR and DAC algos */
7418830d754dSRandall Stewart 					this_sack_lowest_newack = tp1->rec.data.TSN_seq;
7419830d754dSRandall Stewart 					tp1->whoTo->saw_newack = 1;
7420830d754dSRandall Stewart 
7421830d754dSRandall Stewart 					if (tp1->snd_count < 2) {
7422830d754dSRandall Stewart 						/*
7423830d754dSRandall Stewart 						 * True non-retransmited
7424830d754dSRandall Stewart 						 * chunk
7425830d754dSRandall Stewart 						 */
7426830d754dSRandall Stewart 						tp1->whoTo->net_ack2 +=
7427830d754dSRandall Stewart 						    tp1->send_size;
7428830d754dSRandall Stewart 
7429830d754dSRandall Stewart 						/* update RTO too? */
7430830d754dSRandall Stewart 						if (tp1->do_rtt) {
7431830d754dSRandall Stewart 							tp1->whoTo->RTO =
7432830d754dSRandall Stewart 							    sctp_calculate_rto(stcb,
7433830d754dSRandall Stewart 							    asoc, tp1->whoTo,
7434830d754dSRandall Stewart 							    &tp1->sent_rcv_time,
7435830d754dSRandall Stewart 							    sctp_align_safe_nocopy);
7436830d754dSRandall Stewart 							tp1->do_rtt = 0;
7437830d754dSRandall Stewart 						}
7438830d754dSRandall Stewart 					}
7439830d754dSRandall Stewart 					/*
7440830d754dSRandall Stewart 					 * CMT: CUCv2 algorithm. From the
7441830d754dSRandall Stewart 					 * cumack'd TSNs, for each TSN being
7442830d754dSRandall Stewart 					 * acked for the first time, set the
7443830d754dSRandall Stewart 					 * following variables for the
7444830d754dSRandall Stewart 					 * corresp destination.
7445830d754dSRandall Stewart 					 * new_pseudo_cumack will trigger a
7446830d754dSRandall Stewart 					 * cwnd update.
7447830d754dSRandall Stewart 					 * find_(rtx_)pseudo_cumack will
7448830d754dSRandall Stewart 					 * trigger search for the next
7449830d754dSRandall Stewart 					 * expected (rtx-)pseudo-cumack.
7450830d754dSRandall Stewart 					 */
7451830d754dSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
7452830d754dSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
7453830d754dSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
7454830d754dSRandall Stewart 
7455830d754dSRandall Stewart 
7456830d754dSRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
7457830d754dSRandall Stewart 						sctp_log_sack(asoc->last_acked_seq,
7458830d754dSRandall Stewart 						    cum_ack,
7459830d754dSRandall Stewart 						    tp1->rec.data.TSN_seq,
7460830d754dSRandall Stewart 						    0,
7461830d754dSRandall Stewart 						    0,
7462830d754dSRandall Stewart 						    SCTP_LOG_TSN_ACKED);
7463830d754dSRandall Stewart 					}
7464830d754dSRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7465830d754dSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
7466830d754dSRandall Stewart 					}
7467830d754dSRandall Stewart 				}
7468830d754dSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
7469830d754dSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
7470830d754dSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
7471830d754dSRandall Stewart 					sctp_audit_log(0xB3,
7472830d754dSRandall Stewart 					    (asoc->sent_queue_retran_cnt & 0x000000ff));
7473830d754dSRandall Stewart #endif
7474830d754dSRandall Stewart 				}
7475830d754dSRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
7476830d754dSRandall Stewart 					/* deflate the cwnd */
7477830d754dSRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
7478830d754dSRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
7479830d754dSRandall Stewart 				}
7480830d754dSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
7481830d754dSRandall Stewart 			}
7482830d754dSRandall Stewart 		} else {
7483830d754dSRandall Stewart 			break;
7484830d754dSRandall Stewart 		}
7485830d754dSRandall Stewart 		tp1 = TAILQ_NEXT(tp1, sctp_next);
7486830d754dSRandall Stewart 	}
7487830d754dSRandall Stewart 	biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
7488830d754dSRandall Stewart 	/* always set this up to cum-ack */
7489830d754dSRandall Stewart 	asoc->this_sack_highest_gap = last_tsn;
7490830d754dSRandall Stewart 
7491830d754dSRandall Stewart 	/* Move offset up to point to gaps/dups */
7492830d754dSRandall Stewart 	offset += sizeof(struct sctp_nr_sack_chunk);
7493830d754dSRandall Stewart 	if (((num_seg * (sizeof(struct sctp_gap_ack_block))) + sizeof(struct sctp_nr_sack_chunk)) > nr_sack_length) {
7494830d754dSRandall Stewart 
7495830d754dSRandall Stewart 		/* skip corrupt segments */
7496830d754dSRandall Stewart 		goto skip_segments;
7497830d754dSRandall Stewart 	}
7498830d754dSRandall Stewart 	if (num_seg > 0) {
7499830d754dSRandall Stewart 
7500830d754dSRandall Stewart 		/*
7501830d754dSRandall Stewart 		 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
7502830d754dSRandall Stewart 		 * to be greater than the cumack. Also reset saw_newack to 0
7503830d754dSRandall Stewart 		 * for all dests.
7504830d754dSRandall Stewart 		 */
7505830d754dSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7506830d754dSRandall Stewart 			net->saw_newack = 0;
7507830d754dSRandall Stewart 			net->this_sack_highest_newack = last_tsn;
7508830d754dSRandall Stewart 		}
7509830d754dSRandall Stewart 
7510830d754dSRandall Stewart 		/*
7511830d754dSRandall Stewart 		 * thisSackHighestGap will increase while handling NEW
7512830d754dSRandall Stewart 		 * segments this_sack_highest_newack will increase while
7513830d754dSRandall Stewart 		 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
7514830d754dSRandall Stewart 		 * used for CMT DAC algo. saw_newack will also change.
7515830d754dSRandall Stewart 		 */
7516830d754dSRandall Stewart 
7517830d754dSRandall Stewart 		sctp_handle_nr_sack_segments(m, &offset, stcb, asoc, ch, last_tsn,
7518830d754dSRandall Stewart 		    &biggest_tsn_acked, &biggest_tsn_newly_acked, &this_sack_lowest_newack,
7519830d754dSRandall Stewart 		    num_seg, num_nr_seg, &ecn_seg_sums);
7520830d754dSRandall Stewart 
7521830d754dSRandall Stewart 
7522830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
7523830d754dSRandall Stewart 			/*
7524830d754dSRandall Stewart 			 * validate the biggest_tsn_acked in the gap acks if
7525830d754dSRandall Stewart 			 * strict adherence is wanted.
7526830d754dSRandall Stewart 			 */
7527830d754dSRandall Stewart 			if ((biggest_tsn_acked == send_s) ||
7528830d754dSRandall Stewart 			    (compare_with_wrap(biggest_tsn_acked, send_s, MAX_TSN))) {
7529830d754dSRandall Stewart 				/*
7530830d754dSRandall Stewart 				 * peer is either confused or we are under
7531830d754dSRandall Stewart 				 * attack. We must abort.
7532830d754dSRandall Stewart 				 */
7533830d754dSRandall Stewart 				goto hopeless_peer;
7534830d754dSRandall Stewart 			}
7535830d754dSRandall Stewart 		}
7536830d754dSRandall Stewart 	}
7537830d754dSRandall Stewart skip_segments:
7538830d754dSRandall Stewart 	/*******************************************/
7539830d754dSRandall Stewart 	/* cancel ALL T3-send timer if accum moved */
7540830d754dSRandall Stewart 	/*******************************************/
7541830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
7542830d754dSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7543830d754dSRandall Stewart 			if (net->new_pseudo_cumack)
7544830d754dSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
7545830d754dSRandall Stewart 				    stcb, net,
7546830d754dSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
7547830d754dSRandall Stewart 
7548830d754dSRandall Stewart 		}
7549830d754dSRandall Stewart 	} else {
7550830d754dSRandall Stewart 		if (accum_moved) {
7551830d754dSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7552830d754dSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
7553830d754dSRandall Stewart 				    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
7554830d754dSRandall Stewart 			}
7555830d754dSRandall Stewart 		}
7556830d754dSRandall Stewart 	}
7557830d754dSRandall Stewart 	/********************************************/
7558830d754dSRandall Stewart 	/* drop the acked chunks from the sendqueue */
7559830d754dSRandall Stewart 	/********************************************/
7560830d754dSRandall Stewart 	asoc->last_acked_seq = cum_ack;
7561830d754dSRandall Stewart 
7562830d754dSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
7563830d754dSRandall Stewart 	if (tp1 == NULL)
7564830d754dSRandall Stewart 		goto done_with_it;
7565830d754dSRandall Stewart 	do {
7566830d754dSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq, cum_ack,
7567830d754dSRandall Stewart 		    MAX_TSN)) {
7568830d754dSRandall Stewart 			break;
7569830d754dSRandall Stewart 		}
7570830d754dSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
7571830d754dSRandall Stewart 			/* no more sent on list */
7572830d754dSRandall Stewart 			printf("Warning, tp1->sent == %d and its now acked?\n",
7573830d754dSRandall Stewart 			    tp1->sent);
7574830d754dSRandall Stewart 		}
7575830d754dSRandall Stewart 		tp2 = TAILQ_NEXT(tp1, sctp_next);
7576830d754dSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
7577830d754dSRandall Stewart 		if (tp1->pr_sctp_on) {
7578830d754dSRandall Stewart 			if (asoc->pr_sctp_cnt != 0)
7579830d754dSRandall Stewart 				asoc->pr_sctp_cnt--;
7580830d754dSRandall Stewart 		}
7581830d754dSRandall Stewart 		if ((TAILQ_FIRST(&asoc->sent_queue) == NULL) &&
7582830d754dSRandall Stewart 		    (asoc->total_flight > 0)) {
7583830d754dSRandall Stewart #ifdef INVARIANTS
7584830d754dSRandall Stewart 			panic("Warning flight size is postive and should be 0");
7585830d754dSRandall Stewart #else
7586830d754dSRandall Stewart 			SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
7587830d754dSRandall Stewart 			    asoc->total_flight);
7588830d754dSRandall Stewart #endif
7589830d754dSRandall Stewart 			asoc->total_flight = 0;
7590830d754dSRandall Stewart 		}
7591830d754dSRandall Stewart 		if (tp1->data) {
7592830d754dSRandall Stewart 			/* sa_ignore NO_NULL_CHK */
7593830d754dSRandall Stewart 			sctp_free_bufspace(stcb, asoc, tp1, 1);
7594830d754dSRandall Stewart 			sctp_m_freem(tp1->data);
7595810ec536SMichael Tuexen 			if (asoc->peer_supports_prsctp && PR_SCTP_BUF_ENABLED(tp1->flags)) {
7596830d754dSRandall Stewart 				asoc->sent_queue_cnt_removeable--;
7597830d754dSRandall Stewart 			}
7598830d754dSRandall Stewart 		}
7599830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
7600830d754dSRandall Stewart 			sctp_log_sack(asoc->last_acked_seq,
7601830d754dSRandall Stewart 			    cum_ack,
7602830d754dSRandall Stewart 			    tp1->rec.data.TSN_seq,
7603830d754dSRandall Stewart 			    0,
7604830d754dSRandall Stewart 			    0,
7605830d754dSRandall Stewart 			    SCTP_LOG_FREE_SENT);
7606830d754dSRandall Stewart 		}
7607830d754dSRandall Stewart 		tp1->data = NULL;
7608830d754dSRandall Stewart 		asoc->sent_queue_cnt--;
7609830d754dSRandall Stewart 		sctp_free_a_chunk(stcb, tp1);
7610830d754dSRandall Stewart 		wake_him++;
7611830d754dSRandall Stewart 		tp1 = tp2;
7612830d754dSRandall Stewart 	} while (tp1 != NULL);
7613830d754dSRandall Stewart 
7614830d754dSRandall Stewart done_with_it:
7615830d754dSRandall Stewart 	/* sa_ignore NO_NULL_CHK */
7616830d754dSRandall Stewart 	if ((wake_him) && (stcb->sctp_socket)) {
7617830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7618830d754dSRandall Stewart 		struct socket *so;
7619830d754dSRandall Stewart 
7620830d754dSRandall Stewart #endif
7621830d754dSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
7622830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
7623830d754dSRandall Stewart 			sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_WAKESND_FROM_SACK);
7624830d754dSRandall Stewart 		}
7625830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7626830d754dSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
7627830d754dSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
7628830d754dSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
7629830d754dSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
7630830d754dSRandall Stewart 		SCTP_TCB_LOCK(stcb);
7631830d754dSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
7632830d754dSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
7633830d754dSRandall Stewart 			/* assoc was freed while we were unlocked */
7634830d754dSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
7635830d754dSRandall Stewart 			return;
7636830d754dSRandall Stewart 		}
7637830d754dSRandall Stewart #endif
7638830d754dSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
7639830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7640830d754dSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
7641830d754dSRandall Stewart #endif
7642830d754dSRandall Stewart 	} else {
7643830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
7644830d754dSRandall Stewart 			sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_NOWAKE_FROM_SACK);
7645830d754dSRandall Stewart 		}
7646830d754dSRandall Stewart 	}
7647830d754dSRandall Stewart 
7648830d754dSRandall Stewart 	if (asoc->fast_retran_loss_recovery && accum_moved) {
7649830d754dSRandall Stewart 		if (compare_with_wrap(asoc->last_acked_seq,
7650830d754dSRandall Stewart 		    asoc->fast_recovery_tsn, MAX_TSN) ||
7651830d754dSRandall Stewart 		    asoc->last_acked_seq == asoc->fast_recovery_tsn) {
7652830d754dSRandall Stewart 			/* Setup so we will exit RFC2582 fast recovery */
7653830d754dSRandall Stewart 			will_exit_fast_recovery = 1;
7654830d754dSRandall Stewart 		}
7655830d754dSRandall Stewart 	}
7656830d754dSRandall Stewart 	/*
7657830d754dSRandall Stewart 	 * Check for revoked fragments:
7658830d754dSRandall Stewart 	 *
7659830d754dSRandall Stewart 	 * if Previous sack - Had no frags then we can't have any revoked if
7660830d754dSRandall Stewart 	 * Previous sack - Had frag's then - If we now have frags aka
7661830d754dSRandall Stewart 	 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
7662830d754dSRandall Stewart 	 * some of them. else - The peer revoked all ACKED fragments, since
7663830d754dSRandall Stewart 	 * we had some before and now we have NONE.
7664830d754dSRandall Stewart 	 */
7665830d754dSRandall Stewart 
7666830d754dSRandall Stewart 	if (num_seg)
7667830d754dSRandall Stewart 		sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
7668830d754dSRandall Stewart 
7669830d754dSRandall Stewart 	else if (asoc->saw_sack_with_frags) {
7670830d754dSRandall Stewart 		int cnt_revoked = 0;
7671830d754dSRandall Stewart 
7672830d754dSRandall Stewart 		tp1 = TAILQ_FIRST(&asoc->sent_queue);
7673830d754dSRandall Stewart 		if (tp1 != NULL) {
7674830d754dSRandall Stewart 			/* Peer revoked all dg's marked or acked */
7675830d754dSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
7676830d754dSRandall Stewart 				/*
7677830d754dSRandall Stewart 				 * EY- maybe check only if it is nr_acked
7678830d754dSRandall Stewart 				 * nr_marked may not be possible
7679830d754dSRandall Stewart 				 */
7680830d754dSRandall Stewart 				if ((tp1->sent == SCTP_DATAGRAM_NR_ACKED) ||
7681830d754dSRandall Stewart 				    (tp1->sent == SCTP_DATAGRAM_NR_MARKED)) {
7682830d754dSRandall Stewart 					/*
7683830d754dSRandall Stewart 					 * EY! - TODO: Something previously
7684830d754dSRandall Stewart 					 * nr_gapped is reneged, abort the
7685830d754dSRandall Stewart 					 * association
7686830d754dSRandall Stewart 					 */
7687830d754dSRandall Stewart 					return;
7688830d754dSRandall Stewart 				}
7689830d754dSRandall Stewart 				if ((tp1->sent > SCTP_DATAGRAM_RESEND) &&
7690830d754dSRandall Stewart 				    (tp1->sent < SCTP_FORWARD_TSN_SKIP)) {
7691830d754dSRandall Stewart 					tp1->sent = SCTP_DATAGRAM_SENT;
7692830d754dSRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
7693830d754dSRandall Stewart 						sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
7694830d754dSRandall Stewart 						    tp1->whoTo->flight_size,
7695830d754dSRandall Stewart 						    tp1->book_size,
7696830d754dSRandall Stewart 						    (uintptr_t) tp1->whoTo,
7697830d754dSRandall Stewart 						    tp1->rec.data.TSN_seq);
7698830d754dSRandall Stewart 					}
7699830d754dSRandall Stewart 					sctp_flight_size_increase(tp1);
7700830d754dSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
7701830d754dSRandall Stewart 					tp1->rec.data.chunk_was_revoked = 1;
7702830d754dSRandall Stewart 					/*
7703830d754dSRandall Stewart 					 * To ensure that this increase in
7704830d754dSRandall Stewart 					 * flightsize, which is artificial,
7705830d754dSRandall Stewart 					 * does not throttle the sender, we
7706830d754dSRandall Stewart 					 * also increase the cwnd
7707830d754dSRandall Stewart 					 * artificially.
7708830d754dSRandall Stewart 					 */
7709830d754dSRandall Stewart 					tp1->whoTo->cwnd += tp1->book_size;
7710830d754dSRandall Stewart 					cnt_revoked++;
7711830d754dSRandall Stewart 				}
7712830d754dSRandall Stewart 			}
7713830d754dSRandall Stewart 			if (cnt_revoked) {
7714830d754dSRandall Stewart 				reneged_all = 1;
7715830d754dSRandall Stewart 			}
7716830d754dSRandall Stewart 		}
7717830d754dSRandall Stewart 		asoc->saw_sack_with_frags = 0;
7718830d754dSRandall Stewart 	}
7719830d754dSRandall Stewart 	if (num_seg)
7720830d754dSRandall Stewart 		asoc->saw_sack_with_frags = 1;
7721830d754dSRandall Stewart 	else
7722830d754dSRandall Stewart 		asoc->saw_sack_with_frags = 0;
7723830d754dSRandall Stewart 
7724830d754dSRandall Stewart 	/* EY! - not sure about if there should be an IF */
7725830d754dSRandall Stewart 	if (num_nr_seg)
7726830d754dSRandall Stewart 		sctp_check_for_nr_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
7727830d754dSRandall Stewart 	else if (asoc->saw_sack_with_nr_frags) {
7728830d754dSRandall Stewart 		/*
7729830d754dSRandall Stewart 		 * EY!- TODO: all previously nr_gapped chunks have been
7730830d754dSRandall Stewart 		 * reneged abort the association
7731830d754dSRandall Stewart 		 */
7732830d754dSRandall Stewart 		asoc->saw_sack_with_nr_frags = 0;
7733830d754dSRandall Stewart 	}
7734830d754dSRandall Stewart 	if (num_nr_seg)
7735830d754dSRandall Stewart 		asoc->saw_sack_with_nr_frags = 1;
7736830d754dSRandall Stewart 	else
7737830d754dSRandall Stewart 		asoc->saw_sack_with_nr_frags = 0;
7738830d754dSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
7739830d754dSRandall Stewart 	asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
7740830d754dSRandall Stewart 
7741830d754dSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
7742830d754dSRandall Stewart 		/* nothing left in-flight */
7743830d754dSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7744830d754dSRandall Stewart 			/* stop all timers */
7745830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
7746830d754dSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
7747830d754dSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
7748830d754dSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
7749830d754dSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_29);
7750830d754dSRandall Stewart 				}
7751830d754dSRandall Stewart 			}
7752830d754dSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
7753830d754dSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
7754830d754dSRandall Stewart 			net->flight_size = 0;
7755830d754dSRandall Stewart 			net->partial_bytes_acked = 0;
7756830d754dSRandall Stewart 		}
7757830d754dSRandall Stewart 		asoc->total_flight = 0;
7758830d754dSRandall Stewart 		asoc->total_flight_count = 0;
7759830d754dSRandall Stewart 	}
7760830d754dSRandall Stewart 	/**********************************/
7761830d754dSRandall Stewart 	/* Now what about shutdown issues */
7762830d754dSRandall Stewart 	/**********************************/
7763830d754dSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
7764830d754dSRandall Stewart 		/* nothing left on sendqueue.. consider done */
7765830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
7766830d754dSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
7767830d754dSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
7768830d754dSRandall Stewart 		}
7769830d754dSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
7770830d754dSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
7771830d754dSRandall Stewart 			/* SWS sender side engages */
7772830d754dSRandall Stewart 			asoc->peers_rwnd = 0;
7773830d754dSRandall Stewart 		}
7774830d754dSRandall Stewart 		/* clean up */
7775830d754dSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
7776830d754dSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7777830d754dSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
7778830d754dSRandall Stewart 		    (asoc->locked_on_sending)
7779830d754dSRandall Stewart 		    ) {
7780830d754dSRandall Stewart 			struct sctp_stream_queue_pending *sp;
7781830d754dSRandall Stewart 
7782830d754dSRandall Stewart 			/*
7783830d754dSRandall Stewart 			 * I may be in a state where we got all across.. but
7784830d754dSRandall Stewart 			 * cannot write more due to a shutdown... we abort
7785830d754dSRandall Stewart 			 * since the user did not indicate EOR in this case.
7786830d754dSRandall Stewart 			 */
7787830d754dSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
7788830d754dSRandall Stewart 			    sctp_streamhead);
7789830d754dSRandall Stewart 			if ((sp) && (sp->length == 0)) {
7790830d754dSRandall Stewart 				asoc->locked_on_sending = NULL;
7791830d754dSRandall Stewart 				if (sp->msg_is_complete) {
7792830d754dSRandall Stewart 					asoc->stream_queue_cnt--;
7793830d754dSRandall Stewart 				} else {
7794830d754dSRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
7795830d754dSRandall Stewart 					asoc->stream_queue_cnt--;
7796830d754dSRandall Stewart 				}
7797830d754dSRandall Stewart 			}
7798830d754dSRandall Stewart 		}
7799830d754dSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
7800830d754dSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
7801830d754dSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
7802830d754dSRandall Stewart 				/* Need to abort here */
7803830d754dSRandall Stewart 				struct mbuf *oper;
7804830d754dSRandall Stewart 
7805830d754dSRandall Stewart 		abort_out_now:
7806830d754dSRandall Stewart 				*abort_now = 1;
7807830d754dSRandall Stewart 				/* XXX */
7808830d754dSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
7809830d754dSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
7810830d754dSRandall Stewart 				if (oper) {
7811830d754dSRandall Stewart 					struct sctp_paramhdr *ph;
7812830d754dSRandall Stewart 					uint32_t *ippp;
7813830d754dSRandall Stewart 
7814830d754dSRandall Stewart 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
7815830d754dSRandall Stewart 					    sizeof(uint32_t);
7816830d754dSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
7817830d754dSRandall Stewart 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
7818830d754dSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
7819830d754dSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
7820830d754dSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_31);
7821830d754dSRandall Stewart 				}
7822830d754dSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
7823830d754dSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
7824830d754dSRandall Stewart 				return;
7825830d754dSRandall Stewart 			} else {
7826830d754dSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
7827830d754dSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
7828830d754dSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
7829830d754dSRandall Stewart 				}
7830830d754dSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
7831830d754dSRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
7832830d754dSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
7833830d754dSRandall Stewart 				sctp_send_shutdown(stcb,
7834830d754dSRandall Stewart 				    stcb->asoc.primary_destination);
7835830d754dSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
7836830d754dSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
7837830d754dSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
7838830d754dSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
7839830d754dSRandall Stewart 			}
7840830d754dSRandall Stewart 			return;
7841830d754dSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
7842830d754dSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
7843830d754dSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
7844830d754dSRandall Stewart 				goto abort_out_now;
7845830d754dSRandall Stewart 			}
7846830d754dSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
7847830d754dSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
7848830d754dSRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
7849830d754dSRandall Stewart 			sctp_send_shutdown_ack(stcb,
7850830d754dSRandall Stewart 			    stcb->asoc.primary_destination);
7851830d754dSRandall Stewart 
7852830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
7853830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, asoc->primary_destination);
7854830d754dSRandall Stewart 			return;
7855830d754dSRandall Stewart 		}
7856830d754dSRandall Stewart 	}
7857830d754dSRandall Stewart 	/*
7858830d754dSRandall Stewart 	 * Now here we are going to recycle net_ack for a different use...
7859830d754dSRandall Stewart 	 * HEADS UP.
7860830d754dSRandall Stewart 	 */
7861830d754dSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7862830d754dSRandall Stewart 		net->net_ack = 0;
7863830d754dSRandall Stewart 	}
7864830d754dSRandall Stewart 
7865830d754dSRandall Stewart 	/*
7866830d754dSRandall Stewart 	 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
7867830d754dSRandall Stewart 	 * to be done. Setting this_sack_lowest_newack to the cum_ack will
7868830d754dSRandall Stewart 	 * automatically ensure that.
7869830d754dSRandall Stewart 	 */
7870830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac) && (cmt_dac_flag == 0)) {
7871830d754dSRandall Stewart 		this_sack_lowest_newack = cum_ack;
7872830d754dSRandall Stewart 	}
7873830d754dSRandall Stewart 	if (num_seg > 0) {
7874830d754dSRandall Stewart 		sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
7875830d754dSRandall Stewart 		    biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
7876830d754dSRandall Stewart 	}
7877830d754dSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
7878830d754dSRandall Stewart 	asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
7879830d754dSRandall Stewart 
7880830d754dSRandall Stewart 	/******************************************************************
7881830d754dSRandall Stewart 	 *  Here we do the stuff with ECN Nonce checking.
7882830d754dSRandall Stewart 	 *  We basically check to see if the nonce sum flag was incorrect
7883830d754dSRandall Stewart 	 *  or if resynchronization needs to be done. Also if we catch a
7884830d754dSRandall Stewart 	 *  misbehaving receiver we give him the kick.
7885830d754dSRandall Stewart 	 ******************************************************************/
7886830d754dSRandall Stewart 
7887830d754dSRandall Stewart 	if (asoc->ecn_nonce_allowed) {
7888830d754dSRandall Stewart 		if (asoc->nonce_sum_check) {
7889830d754dSRandall Stewart 			if (nonce_sum_flag != ((asoc->nonce_sum_expect_base + ecn_seg_sums) & SCTP_SACK_NONCE_SUM)) {
7890830d754dSRandall Stewart 				if (asoc->nonce_wait_for_ecne == 0) {
7891830d754dSRandall Stewart 					struct sctp_tmit_chunk *lchk;
7892830d754dSRandall Stewart 
7893830d754dSRandall Stewart 					lchk = TAILQ_FIRST(&asoc->send_queue);
7894830d754dSRandall Stewart 					asoc->nonce_wait_for_ecne = 1;
7895830d754dSRandall Stewart 					if (lchk) {
7896830d754dSRandall Stewart 						asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
7897830d754dSRandall Stewart 					} else {
7898830d754dSRandall Stewart 						asoc->nonce_wait_tsn = asoc->sending_seq;
7899830d754dSRandall Stewart 					}
7900830d754dSRandall Stewart 				} else {
7901830d754dSRandall Stewart 					if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
7902830d754dSRandall Stewart 					    (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
7903830d754dSRandall Stewart 						/*
7904830d754dSRandall Stewart 						 * Misbehaving peer. We need
7905830d754dSRandall Stewart 						 * to react to this guy
7906830d754dSRandall Stewart 						 */
7907830d754dSRandall Stewart 						asoc->ecn_allowed = 0;
7908830d754dSRandall Stewart 						asoc->ecn_nonce_allowed = 0;
7909830d754dSRandall Stewart 					}
7910830d754dSRandall Stewart 				}
7911830d754dSRandall Stewart 			}
7912830d754dSRandall Stewart 		} else {
7913830d754dSRandall Stewart 			/* See if Resynchronization Possible */
7914830d754dSRandall Stewart 			if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
7915830d754dSRandall Stewart 				asoc->nonce_sum_check = 1;
7916830d754dSRandall Stewart 				/*
7917830d754dSRandall Stewart 				 * now we must calculate what the base is.
7918830d754dSRandall Stewart 				 * We do this based on two things, we know
7919830d754dSRandall Stewart 				 * the total's for all the segments
7920830d754dSRandall Stewart 				 * gap-acked in the SACK, its stored in
7921830d754dSRandall Stewart 				 * ecn_seg_sums. We also know the SACK's
7922830d754dSRandall Stewart 				 * nonce sum, its in nonce_sum_flag. So we
7923830d754dSRandall Stewart 				 * can build a truth table to back-calculate
7924830d754dSRandall Stewart 				 * the new value of
7925830d754dSRandall Stewart 				 * asoc->nonce_sum_expect_base:
7926830d754dSRandall Stewart 				 *
7927830d754dSRandall Stewart 				 * SACK-flag-Value         Seg-Sums Base 0 0 0
7928830d754dSRandall Stewart 				 * 1                    0 1 0 1 1 1 1 0
7929830d754dSRandall Stewart 				 */
7930830d754dSRandall Stewart 				asoc->nonce_sum_expect_base = (ecn_seg_sums ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
7931830d754dSRandall Stewart 			}
7932830d754dSRandall Stewart 		}
7933830d754dSRandall Stewart 	}
7934830d754dSRandall Stewart 	/* Now are we exiting loss recovery ? */
7935830d754dSRandall Stewart 	if (will_exit_fast_recovery) {
7936830d754dSRandall Stewart 		/* Ok, we must exit fast recovery */
7937830d754dSRandall Stewart 		asoc->fast_retran_loss_recovery = 0;
7938830d754dSRandall Stewart 	}
7939830d754dSRandall Stewart 	if ((asoc->sat_t3_loss_recovery) &&
7940830d754dSRandall Stewart 	    ((compare_with_wrap(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn,
7941830d754dSRandall Stewart 	    MAX_TSN) ||
7942830d754dSRandall Stewart 	    (asoc->last_acked_seq == asoc->sat_t3_recovery_tsn)))) {
7943830d754dSRandall Stewart 		/* end satellite t3 loss recovery */
7944830d754dSRandall Stewart 		asoc->sat_t3_loss_recovery = 0;
7945830d754dSRandall Stewart 	}
7946830d754dSRandall Stewart 	/*
7947830d754dSRandall Stewart 	 * CMT Fast recovery
7948830d754dSRandall Stewart 	 */
7949830d754dSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7950830d754dSRandall Stewart 		if (net->will_exit_fast_recovery) {
7951830d754dSRandall Stewart 			/* Ok, we must exit fast recovery */
7952830d754dSRandall Stewart 			net->fast_retran_loss_recovery = 0;
7953830d754dSRandall Stewart 		}
7954830d754dSRandall Stewart 	}
7955830d754dSRandall Stewart 
7956830d754dSRandall Stewart 	/* Adjust and set the new rwnd value */
7957830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
7958830d754dSRandall Stewart 		sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
7959830d754dSRandall Stewart 		    asoc->peers_rwnd, asoc->total_flight, (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)), a_rwnd);
7960830d754dSRandall Stewart 	}
7961830d754dSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
7962830d754dSRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
7963830d754dSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
7964830d754dSRandall Stewart 		/* SWS sender side engages */
7965830d754dSRandall Stewart 		asoc->peers_rwnd = 0;
7966830d754dSRandall Stewart 	}
7967830d754dSRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
7968830d754dSRandall Stewart 		win_probe_recovery = 1;
7969830d754dSRandall Stewart 	}
7970830d754dSRandall Stewart 	/*
7971830d754dSRandall Stewart 	 * Now we must setup so we have a timer up for anyone with
7972830d754dSRandall Stewart 	 * outstanding data.
7973830d754dSRandall Stewart 	 */
7974830d754dSRandall Stewart 	done_once = 0;
7975830d754dSRandall Stewart again:
7976830d754dSRandall Stewart 	j = 0;
7977830d754dSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7978830d754dSRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
7979830d754dSRandall Stewart 			win_probe_recovered = 1;
7980830d754dSRandall Stewart 			/*-
7981830d754dSRandall Stewart 			 * Find first chunk that was used with
7982830d754dSRandall Stewart 			 * window probe and clear the event. Put
7983830d754dSRandall Stewart 			 * it back into the send queue as if has
7984830d754dSRandall Stewart 			 * not been sent.
7985830d754dSRandall Stewart 			 */
7986830d754dSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
7987830d754dSRandall Stewart 				if (tp1->window_probe) {
7988830d754dSRandall Stewart 					sctp_window_probe_recovery(stcb, asoc, net, tp1);
7989830d754dSRandall Stewart 					break;
7990830d754dSRandall Stewart 				}
7991830d754dSRandall Stewart 			}
7992830d754dSRandall Stewart 		}
7993830d754dSRandall Stewart 		if (net->flight_size) {
7994830d754dSRandall Stewart 			j++;
7995830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
7996830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, net);
79975171328bSRandall Stewart 			if (net->window_probe) {
79985171328bSRandall Stewart 				net->window_probe = 0;
79995171328bSRandall Stewart 			}
8000830d754dSRandall Stewart 		} else {
80015171328bSRandall Stewart 			if (net->window_probe) {
80025171328bSRandall Stewart 				net->window_probe = 0;
80035171328bSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SEND,
80045171328bSRandall Stewart 				    stcb->sctp_ep, stcb, net);
80055171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8006830d754dSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
8007830d754dSRandall Stewart 				    stcb, net,
8008830d754dSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
8009830d754dSRandall Stewart 			}
8010830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
8011830d754dSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8012830d754dSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
8013830d754dSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
8014830d754dSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
8015830d754dSRandall Stewart 				}
8016830d754dSRandall Stewart 			}
8017830d754dSRandall Stewart 		}
8018830d754dSRandall Stewart 	}
8019830d754dSRandall Stewart 	if ((j == 0) &&
8020830d754dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
8021830d754dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
8022830d754dSRandall Stewart 	    (win_probe_recovered == 0) &&
8023830d754dSRandall Stewart 	    (done_once == 0)) {
80240c0982b8SRandall Stewart 		/*
80250c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
80260c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
80270c0982b8SRandall Stewart 		 */
80280c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
8029830d754dSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8030830d754dSRandall Stewart 				net->flight_size = 0;
8031830d754dSRandall Stewart 			}
8032830d754dSRandall Stewart 			asoc->total_flight = 0;
8033830d754dSRandall Stewart 			asoc->total_flight_count = 0;
8034830d754dSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
8035830d754dSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
8036830d754dSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
8037830d754dSRandall Stewart 					sctp_flight_size_increase(tp1);
8038830d754dSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
8039830d754dSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
8040830d754dSRandall Stewart 					asoc->sent_queue_retran_cnt++;
8041830d754dSRandall Stewart 				}
8042830d754dSRandall Stewart 			}
80430c0982b8SRandall Stewart 		}
8044830d754dSRandall Stewart 		done_once = 1;
8045830d754dSRandall Stewart 		goto again;
8046830d754dSRandall Stewart 	}
8047830d754dSRandall Stewart 	/*********************************************/
8048830d754dSRandall Stewart 	/* Here we perform PR-SCTP procedures        */
8049830d754dSRandall Stewart 	/* (section 4.2)                             */
8050830d754dSRandall Stewart 	/*********************************************/
8051830d754dSRandall Stewart 	/* C1. update advancedPeerAckPoint */
8052830d754dSRandall Stewart 	if (compare_with_wrap(cum_ack, asoc->advanced_peer_ack_point, MAX_TSN)) {
8053830d754dSRandall Stewart 		asoc->advanced_peer_ack_point = cum_ack;
8054830d754dSRandall Stewart 	}
8055830d754dSRandall Stewart 	/* C2. try to further move advancedPeerAckPoint ahead */
8056830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
8057830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
8058830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
8059830d754dSRandall Stewart 
8060830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
8061830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
8062830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
8063830d754dSRandall Stewart 		if (compare_with_wrap(asoc->advanced_peer_ack_point, cum_ack,
8064830d754dSRandall Stewart 		    MAX_TSN)) {
8065830d754dSRandall Stewart 			/*
8066830d754dSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing for notes
8067830d754dSRandall Stewart 			 * on issues that will occur when the ECN NONCE
8068830d754dSRandall Stewart 			 * stuff is put into SCTP for cross checking.
8069830d754dSRandall Stewart 			 */
8070830d754dSRandall Stewart 			if (compare_with_wrap(asoc->advanced_peer_ack_point, old_adv_peer_ack_point,
8071830d754dSRandall Stewart 			    MAX_TSN)) {
8072830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
8073830d754dSRandall Stewart 				/*
8074830d754dSRandall Stewart 				 * ECN Nonce: Disable Nonce Sum check when
8075830d754dSRandall Stewart 				 * FWD TSN is sent and store resync tsn
8076830d754dSRandall Stewart 				 */
8077830d754dSRandall Stewart 				asoc->nonce_sum_check = 0;
8078830d754dSRandall Stewart 				asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
80790c0982b8SRandall Stewart 			} else if (lchk) {
80800c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
80810c0982b8SRandall Stewart 				lchk->rec.data.fwd_tsn_cnt++;
80820c0982b8SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt > 3) {
80830c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
80840c0982b8SRandall Stewart 					lchk->rec.data.fwd_tsn_cnt = 0;
80850c0982b8SRandall Stewart 				}
8086830d754dSRandall Stewart 			}
8087830d754dSRandall Stewart 		}
8088830d754dSRandall Stewart 		if (lchk) {
8089830d754dSRandall Stewart 			/* Assure a timer is up */
8090830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
8091830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
8092830d754dSRandall Stewart 		}
8093830d754dSRandall Stewart 	}
8094830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
8095830d754dSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
8096830d754dSRandall Stewart 		    a_rwnd,
8097830d754dSRandall Stewart 		    stcb->asoc.peers_rwnd,
8098830d754dSRandall Stewart 		    stcb->asoc.total_flight,
8099830d754dSRandall Stewart 		    stcb->asoc.total_output_queue_size);
8100830d754dSRandall Stewart 	}
8101830d754dSRandall Stewart }
8102