xref: /freebsd/sys/netinet/sctp_indata.c (revision d50c1d79d005ccb513fea8549d8560d533dca128)
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,
391ceaad40aSRandall Stewart 			    stcb, control, &stcb->sctp_socket->so_rcv, end, SCTP_SO_NOT_LOCKED);
392f8829a4aSRandall Stewart 			cntDel++;
393f8829a4aSRandall Stewart 		} else {
394f8829a4aSRandall Stewart 			if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
395f8829a4aSRandall Stewart 				end = 1;
396f8829a4aSRandall Stewart 			else
397f8829a4aSRandall Stewart 				end = 0;
398f8829a4aSRandall Stewart 			if (sctp_append_to_readq(stcb->sctp_ep, stcb,
399f8829a4aSRandall Stewart 			    stcb->asoc.control_pdapi,
400f8829a4aSRandall Stewart 			    chk->data, end, chk->rec.data.TSN_seq,
401f8829a4aSRandall Stewart 			    &stcb->sctp_socket->so_rcv)) {
402f8829a4aSRandall Stewart 				/*
403f8829a4aSRandall Stewart 				 * something is very wrong, either
404f8829a4aSRandall Stewart 				 * control_pdapi is NULL, or the tail_mbuf
405f8829a4aSRandall Stewart 				 * is corrupt, or there is a EOM already on
406f8829a4aSRandall Stewart 				 * the mbuf chain.
407f8829a4aSRandall Stewart 				 */
408851b7298SRandall Stewart 				if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
409851b7298SRandall Stewart 					goto abandon;
410851b7298SRandall Stewart 				} else {
411df6e0cc3SRandall Stewart #ifdef INVARIANTS
412ad81507eSRandall Stewart 					if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
413f8829a4aSRandall Stewart 						panic("This should not happen control_pdapi NULL?");
414f8829a4aSRandall Stewart 					}
415f8829a4aSRandall Stewart 					/* if we did not panic, it was a EOM */
416f8829a4aSRandall Stewart 					panic("Bad chunking ??");
417df6e0cc3SRandall Stewart #else
418df6e0cc3SRandall Stewart 					if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
419df6e0cc3SRandall Stewart 						SCTP_PRINTF("This should not happen control_pdapi NULL?\n");
420df6e0cc3SRandall Stewart 					}
421df6e0cc3SRandall Stewart 					SCTP_PRINTF("Bad chunking ??\n");
422df6e0cc3SRandall Stewart 					SCTP_PRINTF("Dumping re-assembly queue this will probably hose the association\n");
423df6e0cc3SRandall Stewart 
424df6e0cc3SRandall Stewart #endif
425df6e0cc3SRandall Stewart 					goto abandon;
426f8829a4aSRandall Stewart 				}
427851b7298SRandall Stewart 			}
428f8829a4aSRandall Stewart 			cntDel++;
429f8829a4aSRandall Stewart 		}
430f8829a4aSRandall Stewart 		/* pull it we did it */
431f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
432830d754dSRandall Stewart 		/*
433830d754dSRandall Stewart 		 * EY this is the chunk that should be tagged nr gapped
434830d754dSRandall Stewart 		 * calculate the gap and such then tag this TSN nr
435830d754dSRandall Stewart 		 * chk->rec.data.TSN_seq
436830d754dSRandall Stewart 		 */
437830d754dSRandall Stewart 		/*
438830d754dSRandall Stewart 		 * EY!-TODO- this tsn should be tagged nr only if it is
439830d754dSRandall Stewart 		 * out-of-order, the if statement should be modified
440830d754dSRandall Stewart 		 */
441830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
442830d754dSRandall Stewart 
443830d754dSRandall Stewart 			nr_tsn = chk->rec.data.TSN_seq;
444d50c1d79SRandall Stewart 			SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
4458933fa13SRandall Stewart 			if ((nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3)) ||
446830d754dSRandall Stewart 			    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
447830d754dSRandall Stewart 				/*
448830d754dSRandall Stewart 				 * EY The 1st should never happen, as in
449830d754dSRandall Stewart 				 * process_a_data_chunk method this check
450830d754dSRandall Stewart 				 * should be done
451830d754dSRandall Stewart 				 */
452830d754dSRandall Stewart 				/*
453830d754dSRandall Stewart 				 * EY The 2nd should never happen, because
454830d754dSRandall Stewart 				 * nr_mapping_array is always expanded when
455830d754dSRandall Stewart 				 * mapping_array is expanded
456830d754dSRandall Stewart 				 */
4578933fa13SRandall Stewart 				printf("Impossible nr_gap ack range failed\n");
458830d754dSRandall Stewart 			} else {
459830d754dSRandall Stewart 				SCTP_TCB_LOCK_ASSERT(stcb);
460830d754dSRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
461d50c1d79SRandall Stewart 				SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
4628933fa13SRandall Stewart 				if (compare_with_wrap(nr_tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
463830d754dSRandall Stewart 					asoc->highest_tsn_inside_nr_map = nr_tsn;
464830d754dSRandall Stewart 			}
465830d754dSRandall Stewart 		}
466f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
467f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 0;
468f8829a4aSRandall Stewart 			if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
469f8829a4aSRandall Stewart 				asoc->strmin[stream_no].last_sequence_delivered++;
470f8829a4aSRandall Stewart 			}
471f8829a4aSRandall Stewart 			if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
472f8829a4aSRandall Stewart 				SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
473f8829a4aSRandall Stewart 			}
474f8829a4aSRandall Stewart 		} else if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
475f8829a4aSRandall Stewart 			/*
476f8829a4aSRandall Stewart 			 * turn the flag back on since we just  delivered
477f8829a4aSRandall Stewart 			 * yet another one.
478f8829a4aSRandall Stewart 			 */
479f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 1;
480f8829a4aSRandall Stewart 		}
481f8829a4aSRandall Stewart 		asoc->tsn_of_pdapi_last_delivered = chk->rec.data.TSN_seq;
482f8829a4aSRandall Stewart 		asoc->last_flags_delivered = chk->rec.data.rcv_flags;
483f8829a4aSRandall Stewart 		asoc->last_strm_seq_delivered = chk->rec.data.stream_seq;
484f8829a4aSRandall Stewart 		asoc->last_strm_no_delivered = chk->rec.data.stream_number;
485f8829a4aSRandall Stewart 
486f8829a4aSRandall Stewart 		asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
487f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue -= chk->send_size;
488f8829a4aSRandall Stewart 		sctp_ucount_decr(asoc->cnt_on_reasm_queue);
489f8829a4aSRandall Stewart 		/* free up the chk */
490f8829a4aSRandall Stewart 		chk->data = NULL;
491f8829a4aSRandall Stewart 		sctp_free_a_chunk(stcb, chk);
492f8829a4aSRandall Stewart 
493f8829a4aSRandall Stewart 		if (asoc->fragmented_delivery_inprogress == 0) {
494f8829a4aSRandall Stewart 			/*
495f8829a4aSRandall Stewart 			 * Now lets see if we can deliver the next one on
496f8829a4aSRandall Stewart 			 * the stream
497f8829a4aSRandall Stewart 			 */
498f8829a4aSRandall Stewart 			struct sctp_stream_in *strm;
499f8829a4aSRandall Stewart 
500f8829a4aSRandall Stewart 			strm = &asoc->strmin[stream_no];
501f8829a4aSRandall Stewart 			nxt_todel = strm->last_sequence_delivered + 1;
502f8829a4aSRandall Stewart 			ctl = TAILQ_FIRST(&strm->inqueue);
503f8829a4aSRandall Stewart 			if (ctl && (nxt_todel == ctl->sinfo_ssn)) {
504f8829a4aSRandall Stewart 				while (ctl != NULL) {
505f8829a4aSRandall Stewart 					/* Deliver more if we can. */
506f8829a4aSRandall Stewart 					if (nxt_todel == ctl->sinfo_ssn) {
507f8829a4aSRandall Stewart 						ctlat = TAILQ_NEXT(ctl, next);
508f8829a4aSRandall Stewart 						TAILQ_REMOVE(&strm->inqueue, ctl, next);
509f8829a4aSRandall Stewart 						asoc->size_on_all_streams -= ctl->length;
510f8829a4aSRandall Stewart 						sctp_ucount_decr(asoc->cnt_on_all_streams);
511f8829a4aSRandall Stewart 						strm->last_sequence_delivered++;
512830d754dSRandall Stewart 						/*
513830d754dSRandall Stewart 						 * EY will be used to
514830d754dSRandall Stewart 						 * calculate nr-gap
515830d754dSRandall Stewart 						 */
516830d754dSRandall Stewart 						nr_tsn = ctl->sinfo_tsn;
517f8829a4aSRandall Stewart 						sctp_add_to_readq(stcb->sctp_ep, stcb,
518f8829a4aSRandall Stewart 						    ctl,
519ceaad40aSRandall Stewart 						    &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
520830d754dSRandall Stewart 						/*
521830d754dSRandall Stewart 						 * EY -now something is
522830d754dSRandall Stewart 						 * delivered, calculate
523830d754dSRandall Stewart 						 * nr_gap and tag this tsn
524830d754dSRandall Stewart 						 * NR
525830d754dSRandall Stewart 						 */
526830d754dSRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
527d50c1d79SRandall Stewart 							SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
528830d754dSRandall Stewart 							if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
529830d754dSRandall Stewart 							    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
530d50c1d79SRandall Stewart 								printf("Impossible NR gap calculation?\n");
531830d754dSRandall Stewart 								/*
532830d754dSRandall Stewart 								 * EY The
533830d754dSRandall Stewart 								 * 1st
534830d754dSRandall Stewart 								 * should
535830d754dSRandall Stewart 								 * never
536830d754dSRandall Stewart 								 * happen,
537830d754dSRandall Stewart 								 * as in
538830d754dSRandall Stewart 								 * process_a_
539830d754dSRandall Stewart 								 * data_chunk
540830d754dSRandall Stewart 								 *  method
541830d754dSRandall Stewart 								 * this
542830d754dSRandall Stewart 								 * check
543830d754dSRandall Stewart 								 * should be
544830d754dSRandall Stewart 								 * done
545830d754dSRandall Stewart 								 */
546830d754dSRandall Stewart 								/*
547830d754dSRandall Stewart 								 * EY The
548830d754dSRandall Stewart 								 * 2nd
549830d754dSRandall Stewart 								 * should
550830d754dSRandall Stewart 								 * never
551830d754dSRandall Stewart 								 * happen,
552830d754dSRandall Stewart 								 * because
553830d754dSRandall Stewart 								 * nr_mapping
554830d754dSRandall Stewart 								 * _array is
555830d754dSRandall Stewart 								 * always
556830d754dSRandall Stewart 								 * expanded
557830d754dSRandall Stewart 								 * when
558830d754dSRandall Stewart 								 * mapping_ar
559830d754dSRandall Stewart 								 * ray is
560830d754dSRandall Stewart 								 * expanded
561830d754dSRandall Stewart 								 */
562830d754dSRandall Stewart 							} else {
563830d754dSRandall Stewart 								SCTP_TCB_LOCK_ASSERT(stcb);
564830d754dSRandall Stewart 								SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
565d50c1d79SRandall Stewart 								SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
5668933fa13SRandall Stewart 								if (compare_with_wrap(nr_tsn,
5678933fa13SRandall Stewart 								    asoc->highest_tsn_inside_nr_map,
5688933fa13SRandall Stewart 								    MAX_TSN))
569830d754dSRandall Stewart 									asoc->highest_tsn_inside_nr_map = nr_tsn;
570830d754dSRandall Stewart 							}
571830d754dSRandall Stewart 						}
572f8829a4aSRandall Stewart 						ctl = ctlat;
573f8829a4aSRandall Stewart 					} else {
574f8829a4aSRandall Stewart 						break;
575f8829a4aSRandall Stewart 					}
576f8829a4aSRandall Stewart 					nxt_todel = strm->last_sequence_delivered + 1;
577f8829a4aSRandall Stewart 				}
578f8829a4aSRandall Stewart 			}
579139bc87fSRandall Stewart 			break;
580f8829a4aSRandall Stewart 		}
5813c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
582f8829a4aSRandall Stewart 		chk = TAILQ_FIRST(&asoc->reasmqueue);
583f8829a4aSRandall Stewart 	} while (chk);
584f8829a4aSRandall Stewart }
585f8829a4aSRandall Stewart 
586f8829a4aSRandall Stewart /*
587f8829a4aSRandall Stewart  * Queue the chunk either right into the socket buffer if it is the next one
588f8829a4aSRandall Stewart  * to go OR put it in the correct place in the delivery queue.  If we do
589f8829a4aSRandall Stewart  * append to the so_buf, keep doing so until we are out of order. One big
590f8829a4aSRandall Stewart  * question still remains, what to do when the socket buffer is FULL??
591f8829a4aSRandall Stewart  */
592f8829a4aSRandall Stewart static void
593f8829a4aSRandall Stewart sctp_queue_data_to_stream(struct sctp_tcb *stcb, struct sctp_association *asoc,
594f8829a4aSRandall Stewart     struct sctp_queued_to_read *control, int *abort_flag)
595f8829a4aSRandall Stewart {
596f8829a4aSRandall Stewart 	/*
597f8829a4aSRandall Stewart 	 * FIX-ME maybe? What happens when the ssn wraps? If we are getting
598f8829a4aSRandall Stewart 	 * all the data in one stream this could happen quite rapidly. One
599f8829a4aSRandall Stewart 	 * could use the TSN to keep track of things, but this scheme breaks
600f8829a4aSRandall Stewart 	 * down in the other type of stream useage that could occur. Send a
601f8829a4aSRandall Stewart 	 * single msg to stream 0, send 4Billion messages to stream 1, now
602f8829a4aSRandall Stewart 	 * send a message to stream 0. You have a situation where the TSN
603f8829a4aSRandall Stewart 	 * has wrapped but not in the stream. Is this worth worrying about
604f8829a4aSRandall Stewart 	 * or should we just change our queue sort at the bottom to be by
605f8829a4aSRandall Stewart 	 * TSN.
606f8829a4aSRandall Stewart 	 *
607f8829a4aSRandall Stewart 	 * Could it also be legal for a peer to send ssn 1 with TSN 2 and ssn 2
608f8829a4aSRandall Stewart 	 * with TSN 1? If the peer is doing some sort of funky TSN/SSN
609f8829a4aSRandall Stewart 	 * assignment this could happen... and I don't see how this would be
610f8829a4aSRandall Stewart 	 * a violation. So for now I am undecided an will leave the sort by
611f8829a4aSRandall Stewart 	 * SSN alone. Maybe a hybred approach is the answer
612f8829a4aSRandall Stewart 	 *
613f8829a4aSRandall Stewart 	 */
614f8829a4aSRandall Stewart 	struct sctp_stream_in *strm;
615f8829a4aSRandall Stewart 	struct sctp_queued_to_read *at;
616f8829a4aSRandall Stewart 	int queue_needed;
617f8829a4aSRandall Stewart 	uint16_t nxt_todel;
618f8829a4aSRandall Stewart 	struct mbuf *oper;
619f8829a4aSRandall Stewart 
620830d754dSRandall Stewart 	/* EY- will be used to calculate nr-gap for a tsn */
621830d754dSRandall Stewart 	uint32_t nr_tsn, nr_gap;
622830d754dSRandall Stewart 
623f8829a4aSRandall Stewart 	queue_needed = 1;
624f8829a4aSRandall Stewart 	asoc->size_on_all_streams += control->length;
625f8829a4aSRandall Stewart 	sctp_ucount_incr(asoc->cnt_on_all_streams);
626f8829a4aSRandall Stewart 	strm = &asoc->strmin[control->sinfo_stream];
627f8829a4aSRandall Stewart 	nxt_todel = strm->last_sequence_delivered + 1;
628b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
629f8829a4aSRandall Stewart 		sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INTO_STRD);
63080fefe0aSRandall Stewart 	}
631ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INDATA1,
632ad81507eSRandall Stewart 	    "queue to stream called for ssn:%u lastdel:%u nxt:%u\n",
633f8829a4aSRandall Stewart 	    (uint32_t) control->sinfo_stream,
634ad81507eSRandall Stewart 	    (uint32_t) strm->last_sequence_delivered,
635ad81507eSRandall Stewart 	    (uint32_t) nxt_todel);
636f8829a4aSRandall Stewart 	if (compare_with_wrap(strm->last_sequence_delivered,
637f8829a4aSRandall Stewart 	    control->sinfo_ssn, MAX_SEQ) ||
638f8829a4aSRandall Stewart 	    (strm->last_sequence_delivered == control->sinfo_ssn)) {
639f8829a4aSRandall Stewart 		/* The incoming sseq is behind where we last delivered? */
640ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1, "Duplicate S-SEQ:%d delivered:%d from peer, Abort  association\n",
641ad81507eSRandall Stewart 		    control->sinfo_ssn, strm->last_sequence_delivered);
642c40e9cf2SRandall Stewart protocol_error:
643f8829a4aSRandall Stewart 		/*
644f8829a4aSRandall Stewart 		 * throw it in the stream so it gets cleaned up in
645f8829a4aSRandall Stewart 		 * association destruction
646f8829a4aSRandall Stewart 		 */
647f8829a4aSRandall Stewart 		TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
648139bc87fSRandall Stewart 		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
649f8829a4aSRandall Stewart 		    0, M_DONTWAIT, 1, MT_DATA);
650f8829a4aSRandall Stewart 		if (oper) {
651f8829a4aSRandall Stewart 			struct sctp_paramhdr *ph;
652f8829a4aSRandall Stewart 			uint32_t *ippp;
653f8829a4aSRandall Stewart 
654139bc87fSRandall Stewart 			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
655f8829a4aSRandall Stewart 			    (sizeof(uint32_t) * 3);
656f8829a4aSRandall Stewart 			ph = mtod(oper, struct sctp_paramhdr *);
657f8829a4aSRandall Stewart 			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
658139bc87fSRandall Stewart 			ph->param_length = htons(SCTP_BUF_LEN(oper));
659f8829a4aSRandall Stewart 			ippp = (uint32_t *) (ph + 1);
660a5d547adSRandall Stewart 			*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_1);
661f8829a4aSRandall Stewart 			ippp++;
662f8829a4aSRandall Stewart 			*ippp = control->sinfo_tsn;
663f8829a4aSRandall Stewart 			ippp++;
664f8829a4aSRandall Stewart 			*ippp = ((control->sinfo_stream << 16) | control->sinfo_ssn);
665f8829a4aSRandall Stewart 		}
666a5d547adSRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_1;
667f8829a4aSRandall Stewart 		sctp_abort_an_association(stcb->sctp_ep, stcb,
668ceaad40aSRandall Stewart 		    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
669f8829a4aSRandall Stewart 
670f8829a4aSRandall Stewart 		*abort_flag = 1;
671f8829a4aSRandall Stewart 		return;
672f8829a4aSRandall Stewart 
673f8829a4aSRandall Stewart 	}
674f8829a4aSRandall Stewart 	if (nxt_todel == control->sinfo_ssn) {
675f8829a4aSRandall Stewart 		/* can be delivered right away? */
676b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
677f8829a4aSRandall Stewart 			sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_IMMED_DEL);
67880fefe0aSRandall Stewart 		}
679830d754dSRandall Stewart 		/* EY it wont be queued if it could be delivered directly */
680f8829a4aSRandall Stewart 		queue_needed = 0;
681f8829a4aSRandall Stewart 		asoc->size_on_all_streams -= control->length;
682f8829a4aSRandall Stewart 		sctp_ucount_decr(asoc->cnt_on_all_streams);
683f8829a4aSRandall Stewart 		strm->last_sequence_delivered++;
684830d754dSRandall Stewart 		/* EY will be used to calculate nr-gap */
685830d754dSRandall Stewart 		nr_tsn = control->sinfo_tsn;
686f8829a4aSRandall Stewart 		sctp_add_to_readq(stcb->sctp_ep, stcb,
687f8829a4aSRandall Stewart 		    control,
688ceaad40aSRandall Stewart 		    &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
689830d754dSRandall Stewart 
690830d754dSRandall Stewart 		/*
691830d754dSRandall Stewart 		 * EY this is the chunk that should be tagged nr gapped
692830d754dSRandall Stewart 		 * calculate the gap and such then tag this TSN nr
693830d754dSRandall Stewart 		 * chk->rec.data.TSN_seq
694830d754dSRandall Stewart 		 */
695830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
696d50c1d79SRandall Stewart 			SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
697830d754dSRandall Stewart 			if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
698830d754dSRandall Stewart 			    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
699d50c1d79SRandall Stewart 				printf("Impossible nr_tsn set 2?\n");
700830d754dSRandall Stewart 				/*
701830d754dSRandall Stewart 				 * EY The 1st should never happen, as in
702830d754dSRandall Stewart 				 * process_a_data_chunk method this check
703830d754dSRandall Stewart 				 * should be done
704830d754dSRandall Stewart 				 */
705830d754dSRandall Stewart 				/*
706830d754dSRandall Stewart 				 * EY The 2nd should never happen, because
707830d754dSRandall Stewart 				 * nr_mapping_array is always expanded when
708830d754dSRandall Stewart 				 * mapping_array is expanded
709830d754dSRandall Stewart 				 */
710830d754dSRandall Stewart 			} else {
711830d754dSRandall Stewart 				SCTP_TCB_LOCK_ASSERT(stcb);
712830d754dSRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
713d50c1d79SRandall Stewart 				SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
7148933fa13SRandall Stewart 				if (compare_with_wrap(nr_tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
715830d754dSRandall Stewart 					asoc->highest_tsn_inside_nr_map = nr_tsn;
716830d754dSRandall Stewart 			}
717830d754dSRandall Stewart 		}
718f8829a4aSRandall Stewart 		control = TAILQ_FIRST(&strm->inqueue);
719f8829a4aSRandall Stewart 		while (control != NULL) {
720f8829a4aSRandall Stewart 			/* all delivered */
721f8829a4aSRandall Stewart 			nxt_todel = strm->last_sequence_delivered + 1;
722f8829a4aSRandall Stewart 			if (nxt_todel == control->sinfo_ssn) {
723f8829a4aSRandall Stewart 				at = TAILQ_NEXT(control, next);
724f8829a4aSRandall Stewart 				TAILQ_REMOVE(&strm->inqueue, control, next);
725f8829a4aSRandall Stewart 				asoc->size_on_all_streams -= control->length;
726f8829a4aSRandall Stewart 				sctp_ucount_decr(asoc->cnt_on_all_streams);
727f8829a4aSRandall Stewart 				strm->last_sequence_delivered++;
728f8829a4aSRandall Stewart 				/*
729f8829a4aSRandall Stewart 				 * We ignore the return of deliver_data here
730f8829a4aSRandall Stewart 				 * since we always can hold the chunk on the
731f8829a4aSRandall Stewart 				 * d-queue. And we have a finite number that
732f8829a4aSRandall Stewart 				 * can be delivered from the strq.
733f8829a4aSRandall Stewart 				 */
734b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
735f8829a4aSRandall Stewart 					sctp_log_strm_del(control, NULL,
736f8829a4aSRandall Stewart 					    SCTP_STR_LOG_FROM_IMMED_DEL);
73780fefe0aSRandall Stewart 				}
738830d754dSRandall Stewart 				/* EY will be used to calculate nr-gap */
739830d754dSRandall Stewart 				nr_tsn = control->sinfo_tsn;
740f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
741f8829a4aSRandall Stewart 				    control,
742ceaad40aSRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
743830d754dSRandall Stewart 				/*
744830d754dSRandall Stewart 				 * EY this is the chunk that should be
745830d754dSRandall Stewart 				 * tagged nr gapped calculate the gap and
746830d754dSRandall Stewart 				 * such then tag this TSN nr
747830d754dSRandall Stewart 				 * chk->rec.data.TSN_seq
748830d754dSRandall Stewart 				 */
749830d754dSRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
750d50c1d79SRandall Stewart 					SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
751830d754dSRandall Stewart 					if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
752830d754dSRandall Stewart 					    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
753d50c1d79SRandall Stewart 						printf("Impossible nr TSN set 3?\n");
754830d754dSRandall Stewart 						/*
755830d754dSRandall Stewart 						 * EY The 1st should never
756830d754dSRandall Stewart 						 * happen, as in
757830d754dSRandall Stewart 						 * process_a_data_chunk
758830d754dSRandall Stewart 						 * method this check should
759830d754dSRandall Stewart 						 * be done
760830d754dSRandall Stewart 						 */
761830d754dSRandall Stewart 						/*
762830d754dSRandall Stewart 						 * EY The 2nd should never
763830d754dSRandall Stewart 						 * happen, because
764830d754dSRandall Stewart 						 * nr_mapping_array is
765830d754dSRandall Stewart 						 * always expanded when
766830d754dSRandall Stewart 						 * mapping_array is expanded
767830d754dSRandall Stewart 						 */
768830d754dSRandall Stewart 					} else {
769830d754dSRandall Stewart 						SCTP_TCB_LOCK_ASSERT(stcb);
770d50c1d79SRandall Stewart 						SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
771830d754dSRandall Stewart 						SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
7728933fa13SRandall Stewart 						if (compare_with_wrap(nr_tsn, asoc->highest_tsn_inside_nr_map,
7738933fa13SRandall Stewart 						    MAX_TSN))
774830d754dSRandall Stewart 							asoc->highest_tsn_inside_nr_map = nr_tsn;
775830d754dSRandall Stewart 					}
776830d754dSRandall Stewart 				}
777f8829a4aSRandall Stewart 				control = at;
778f8829a4aSRandall Stewart 				continue;
779f8829a4aSRandall Stewart 			}
780f8829a4aSRandall Stewart 			break;
781f8829a4aSRandall Stewart 		}
782f8829a4aSRandall Stewart 	}
783f8829a4aSRandall Stewart 	if (queue_needed) {
784f8829a4aSRandall Stewart 		/*
785f8829a4aSRandall Stewart 		 * Ok, we did not deliver this guy, find the correct place
786f8829a4aSRandall Stewart 		 * to put it on the queue.
787f8829a4aSRandall Stewart 		 */
788c40e9cf2SRandall Stewart 		if ((compare_with_wrap(asoc->cumulative_tsn,
789c40e9cf2SRandall Stewart 		    control->sinfo_tsn, MAX_TSN)) ||
790c40e9cf2SRandall Stewart 		    (control->sinfo_tsn == asoc->cumulative_tsn)) {
791c40e9cf2SRandall Stewart 			goto protocol_error;
792c40e9cf2SRandall Stewart 		}
793f8829a4aSRandall Stewart 		if (TAILQ_EMPTY(&strm->inqueue)) {
794f8829a4aSRandall Stewart 			/* Empty queue */
795b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
796f8829a4aSRandall Stewart 				sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INSERT_HD);
79780fefe0aSRandall Stewart 			}
798f8829a4aSRandall Stewart 			TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
799f8829a4aSRandall Stewart 		} else {
800f8829a4aSRandall Stewart 			TAILQ_FOREACH(at, &strm->inqueue, next) {
801f8829a4aSRandall Stewart 				if (compare_with_wrap(at->sinfo_ssn,
802f8829a4aSRandall Stewart 				    control->sinfo_ssn, MAX_SEQ)) {
803f8829a4aSRandall Stewart 					/*
804f8829a4aSRandall Stewart 					 * one in queue is bigger than the
805f8829a4aSRandall Stewart 					 * new one, insert before this one
806f8829a4aSRandall Stewart 					 */
807b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
808f8829a4aSRandall Stewart 						sctp_log_strm_del(control, at,
809f8829a4aSRandall Stewart 						    SCTP_STR_LOG_FROM_INSERT_MD);
81080fefe0aSRandall Stewart 					}
811f8829a4aSRandall Stewart 					TAILQ_INSERT_BEFORE(at, control, next);
812f8829a4aSRandall Stewart 					break;
813f8829a4aSRandall Stewart 				} else if (at->sinfo_ssn == control->sinfo_ssn) {
814f8829a4aSRandall Stewart 					/*
815f8829a4aSRandall Stewart 					 * Gak, He sent me a duplicate str
816f8829a4aSRandall Stewart 					 * seq number
817f8829a4aSRandall Stewart 					 */
818f8829a4aSRandall Stewart 					/*
819f8829a4aSRandall Stewart 					 * foo bar, I guess I will just free
820f8829a4aSRandall Stewart 					 * this new guy, should we abort
821f8829a4aSRandall Stewart 					 * too? FIX ME MAYBE? Or it COULD be
822f8829a4aSRandall Stewart 					 * that the SSN's have wrapped.
823f8829a4aSRandall Stewart 					 * Maybe I should compare to TSN
824f8829a4aSRandall Stewart 					 * somehow... sigh for now just blow
825f8829a4aSRandall Stewart 					 * away the chunk!
826f8829a4aSRandall Stewart 					 */
827f8829a4aSRandall Stewart 
828f8829a4aSRandall Stewart 					if (control->data)
829f8829a4aSRandall Stewart 						sctp_m_freem(control->data);
830f8829a4aSRandall Stewart 					control->data = NULL;
831f8829a4aSRandall Stewart 					asoc->size_on_all_streams -= control->length;
832f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->cnt_on_all_streams);
8335bead436SRandall Stewart 					if (control->whoFrom)
834f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
8355bead436SRandall Stewart 					control->whoFrom = NULL;
836f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
837f8829a4aSRandall Stewart 					return;
838f8829a4aSRandall Stewart 				} else {
839f8829a4aSRandall Stewart 					if (TAILQ_NEXT(at, next) == NULL) {
840f8829a4aSRandall Stewart 						/*
841f8829a4aSRandall Stewart 						 * We are at the end, insert
842f8829a4aSRandall Stewart 						 * it after this one
843f8829a4aSRandall Stewart 						 */
844b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
845f8829a4aSRandall Stewart 							sctp_log_strm_del(control, at,
846f8829a4aSRandall Stewart 							    SCTP_STR_LOG_FROM_INSERT_TL);
84780fefe0aSRandall Stewart 						}
848f8829a4aSRandall Stewart 						TAILQ_INSERT_AFTER(&strm->inqueue,
849f8829a4aSRandall Stewart 						    at, control, next);
850f8829a4aSRandall Stewart 						break;
851f8829a4aSRandall Stewart 					}
852f8829a4aSRandall Stewart 				}
853f8829a4aSRandall Stewart 			}
854f8829a4aSRandall Stewart 		}
855f8829a4aSRandall Stewart 	}
856f8829a4aSRandall Stewart }
857f8829a4aSRandall Stewart 
858f8829a4aSRandall Stewart /*
859f8829a4aSRandall Stewart  * Returns two things: You get the total size of the deliverable parts of the
860f8829a4aSRandall Stewart  * first fragmented message on the reassembly queue. And you get a 1 back if
861f8829a4aSRandall Stewart  * all of the message is ready or a 0 back if the message is still incomplete
862f8829a4aSRandall Stewart  */
863f8829a4aSRandall Stewart static int
864f8829a4aSRandall Stewart sctp_is_all_msg_on_reasm(struct sctp_association *asoc, uint32_t * t_size)
865f8829a4aSRandall Stewart {
866f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
867f8829a4aSRandall Stewart 	uint32_t tsn;
868f8829a4aSRandall Stewart 
869f8829a4aSRandall Stewart 	*t_size = 0;
870f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
871f8829a4aSRandall Stewart 	if (chk == NULL) {
872f8829a4aSRandall Stewart 		/* nothing on the queue */
873f8829a4aSRandall Stewart 		return (0);
874f8829a4aSRandall Stewart 	}
875f8829a4aSRandall Stewart 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
876f8829a4aSRandall Stewart 		/* Not a first on the queue */
877f8829a4aSRandall Stewart 		return (0);
878f8829a4aSRandall Stewart 	}
879f8829a4aSRandall Stewart 	tsn = chk->rec.data.TSN_seq;
880f8829a4aSRandall Stewart 	while (chk) {
881f8829a4aSRandall Stewart 		if (tsn != chk->rec.data.TSN_seq) {
882f8829a4aSRandall Stewart 			return (0);
883f8829a4aSRandall Stewart 		}
884f8829a4aSRandall Stewart 		*t_size += chk->send_size;
885f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
886f8829a4aSRandall Stewart 			return (1);
887f8829a4aSRandall Stewart 		}
888f8829a4aSRandall Stewart 		tsn++;
889f8829a4aSRandall Stewart 		chk = TAILQ_NEXT(chk, sctp_next);
890f8829a4aSRandall Stewart 	}
891f8829a4aSRandall Stewart 	return (0);
892f8829a4aSRandall Stewart }
893f8829a4aSRandall Stewart 
894f8829a4aSRandall Stewart static void
895f8829a4aSRandall Stewart sctp_deliver_reasm_check(struct sctp_tcb *stcb, struct sctp_association *asoc)
896f8829a4aSRandall Stewart {
897f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
898f8829a4aSRandall Stewart 	uint16_t nxt_todel;
899f8829a4aSRandall Stewart 	uint32_t tsize;
900f8829a4aSRandall Stewart 
901139bc87fSRandall Stewart doit_again:
902f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
903f8829a4aSRandall Stewart 	if (chk == NULL) {
904f8829a4aSRandall Stewart 		/* Huh? */
905f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = 0;
906f8829a4aSRandall Stewart 		asoc->cnt_on_reasm_queue = 0;
907f8829a4aSRandall Stewart 		return;
908f8829a4aSRandall Stewart 	}
909f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress == 0) {
910f8829a4aSRandall Stewart 		nxt_todel =
911f8829a4aSRandall Stewart 		    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
912f8829a4aSRandall Stewart 		if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
913f8829a4aSRandall Stewart 		    (nxt_todel == chk->rec.data.stream_seq ||
914f8829a4aSRandall Stewart 		    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
915f8829a4aSRandall Stewart 			/*
916f8829a4aSRandall Stewart 			 * Yep the first one is here and its ok to deliver
917f8829a4aSRandall Stewart 			 * but should we?
918f8829a4aSRandall Stewart 			 */
919f8829a4aSRandall Stewart 			if ((sctp_is_all_msg_on_reasm(asoc, &tsize) ||
920c4739e2fSRandall Stewart 			    (tsize >= stcb->sctp_ep->partial_delivery_point))) {
921f8829a4aSRandall Stewart 
922f8829a4aSRandall Stewart 				/*
923f8829a4aSRandall Stewart 				 * Yes, we setup to start reception, by
924f8829a4aSRandall Stewart 				 * backing down the TSN just in case we
925f8829a4aSRandall Stewart 				 * can't deliver. If we
926f8829a4aSRandall Stewart 				 */
927f8829a4aSRandall Stewart 				asoc->fragmented_delivery_inprogress = 1;
928f8829a4aSRandall Stewart 				asoc->tsn_last_delivered =
929f8829a4aSRandall Stewart 				    chk->rec.data.TSN_seq - 1;
930f8829a4aSRandall Stewart 				asoc->str_of_pdapi =
931f8829a4aSRandall Stewart 				    chk->rec.data.stream_number;
932f8829a4aSRandall Stewart 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
933f8829a4aSRandall Stewart 				asoc->pdapi_ppid = chk->rec.data.payloadtype;
934f8829a4aSRandall Stewart 				asoc->fragment_flags = chk->rec.data.rcv_flags;
935f8829a4aSRandall Stewart 				sctp_service_reassembly(stcb, asoc);
936f8829a4aSRandall Stewart 			}
937f8829a4aSRandall Stewart 		}
938f8829a4aSRandall Stewart 	} else {
939139bc87fSRandall Stewart 		/*
940139bc87fSRandall Stewart 		 * Service re-assembly will deliver stream data queued at
941139bc87fSRandall Stewart 		 * the end of fragmented delivery.. but it wont know to go
942139bc87fSRandall Stewart 		 * back and call itself again... we do that here with the
943139bc87fSRandall Stewart 		 * got doit_again
944139bc87fSRandall Stewart 		 */
945f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
946139bc87fSRandall Stewart 		if (asoc->fragmented_delivery_inprogress == 0) {
947139bc87fSRandall Stewart 			/*
948139bc87fSRandall Stewart 			 * finished our Fragmented delivery, could be more
949139bc87fSRandall Stewart 			 * waiting?
950139bc87fSRandall Stewart 			 */
951139bc87fSRandall Stewart 			goto doit_again;
952139bc87fSRandall Stewart 		}
953f8829a4aSRandall Stewart 	}
954f8829a4aSRandall Stewart }
955f8829a4aSRandall Stewart 
956f8829a4aSRandall Stewart /*
957f8829a4aSRandall Stewart  * Dump onto the re-assembly queue, in its proper place. After dumping on the
958f8829a4aSRandall Stewart  * queue, see if anthing can be delivered. If so pull it off (or as much as
959f8829a4aSRandall Stewart  * we can. If we run out of space then we must dump what we can and set the
960f8829a4aSRandall Stewart  * appropriate flag to say we queued what we could.
961f8829a4aSRandall Stewart  */
962f8829a4aSRandall Stewart static void
963f8829a4aSRandall Stewart sctp_queue_data_for_reasm(struct sctp_tcb *stcb, struct sctp_association *asoc,
964f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk, int *abort_flag)
965f8829a4aSRandall Stewart {
966f8829a4aSRandall Stewart 	struct mbuf *oper;
967f8829a4aSRandall Stewart 	uint32_t cum_ackp1, last_tsn, prev_tsn, post_tsn;
968f8829a4aSRandall Stewart 	u_char last_flags;
969f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *at, *prev, *next;
970f8829a4aSRandall Stewart 
971f8829a4aSRandall Stewart 	prev = next = NULL;
972f8829a4aSRandall Stewart 	cum_ackp1 = asoc->tsn_last_delivered + 1;
973f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->reasmqueue)) {
974f8829a4aSRandall Stewart 		/* This is the first one on the queue */
975f8829a4aSRandall Stewart 		TAILQ_INSERT_HEAD(&asoc->reasmqueue, chk, sctp_next);
976f8829a4aSRandall Stewart 		/*
977f8829a4aSRandall Stewart 		 * we do not check for delivery of anything when only one
978f8829a4aSRandall Stewart 		 * fragment is here
979f8829a4aSRandall Stewart 		 */
980f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = chk->send_size;
981f8829a4aSRandall Stewart 		sctp_ucount_incr(asoc->cnt_on_reasm_queue);
982f8829a4aSRandall Stewart 		if (chk->rec.data.TSN_seq == cum_ackp1) {
983f8829a4aSRandall Stewart 			if (asoc->fragmented_delivery_inprogress == 0 &&
984f8829a4aSRandall Stewart 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) !=
985f8829a4aSRandall Stewart 			    SCTP_DATA_FIRST_FRAG) {
986f8829a4aSRandall Stewart 				/*
987f8829a4aSRandall Stewart 				 * An empty queue, no delivery inprogress,
988f8829a4aSRandall Stewart 				 * we hit the next one and it does NOT have
989f8829a4aSRandall Stewart 				 * a FIRST fragment mark.
990f8829a4aSRandall Stewart 				 */
991ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not first, no fragmented delivery in progress\n");
992139bc87fSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
993f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
994f8829a4aSRandall Stewart 
995f8829a4aSRandall Stewart 				if (oper) {
996f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
997f8829a4aSRandall Stewart 					uint32_t *ippp;
998f8829a4aSRandall Stewart 
999139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) =
1000f8829a4aSRandall Stewart 					    sizeof(struct sctp_paramhdr) +
1001f8829a4aSRandall Stewart 					    (sizeof(uint32_t) * 3);
1002f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
1003f8829a4aSRandall Stewart 					ph->param_type =
1004f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1005139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
1006f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
1007a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_2);
1008f8829a4aSRandall Stewart 					ippp++;
1009f8829a4aSRandall Stewart 					*ippp = chk->rec.data.TSN_seq;
1010f8829a4aSRandall Stewart 					ippp++;
1011f8829a4aSRandall Stewart 					*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1012f8829a4aSRandall Stewart 
1013f8829a4aSRandall Stewart 				}
1014a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_2;
1015f8829a4aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb,
1016ceaad40aSRandall Stewart 				    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1017f8829a4aSRandall Stewart 				*abort_flag = 1;
1018f8829a4aSRandall Stewart 			} else if (asoc->fragmented_delivery_inprogress &&
1019f8829a4aSRandall Stewart 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
1020f8829a4aSRandall Stewart 				/*
1021f8829a4aSRandall Stewart 				 * We are doing a partial delivery and the
1022f8829a4aSRandall Stewart 				 * NEXT chunk MUST be either the LAST or
1023f8829a4aSRandall Stewart 				 * MIDDLE fragment NOT a FIRST
1024f8829a4aSRandall Stewart 				 */
1025ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS a first and fragmented delivery in progress\n");
1026139bc87fSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1027f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
1028f8829a4aSRandall Stewart 				if (oper) {
1029f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
1030f8829a4aSRandall Stewart 					uint32_t *ippp;
1031f8829a4aSRandall Stewart 
1032139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) =
1033f8829a4aSRandall Stewart 					    sizeof(struct sctp_paramhdr) +
1034f8829a4aSRandall Stewart 					    (3 * sizeof(uint32_t));
1035f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
1036f8829a4aSRandall Stewart 					ph->param_type =
1037f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1038139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
1039f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
1040a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_3);
1041f8829a4aSRandall Stewart 					ippp++;
1042f8829a4aSRandall Stewart 					*ippp = chk->rec.data.TSN_seq;
1043f8829a4aSRandall Stewart 					ippp++;
1044f8829a4aSRandall Stewart 					*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1045f8829a4aSRandall Stewart 				}
1046a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_3;
1047f8829a4aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb,
1048ceaad40aSRandall Stewart 				    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1049f8829a4aSRandall Stewart 				*abort_flag = 1;
1050f8829a4aSRandall Stewart 			} else if (asoc->fragmented_delivery_inprogress) {
1051f8829a4aSRandall Stewart 				/*
1052f8829a4aSRandall Stewart 				 * Here we are ok with a MIDDLE or LAST
1053f8829a4aSRandall Stewart 				 * piece
1054f8829a4aSRandall Stewart 				 */
1055f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
1056f8829a4aSRandall Stewart 				    asoc->str_of_pdapi) {
1057f8829a4aSRandall Stewart 					/* Got to be the right STR No */
1058ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream number %d vs %d\n",
1059f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
1060f8829a4aSRandall Stewart 					    asoc->str_of_pdapi);
1061139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1062f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1063f8829a4aSRandall Stewart 					if (oper) {
1064f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1065f8829a4aSRandall Stewart 						uint32_t *ippp;
1066f8829a4aSRandall Stewart 
1067139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1068f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1069f8829a4aSRandall Stewart 						    (sizeof(uint32_t) * 3);
1070f8829a4aSRandall Stewart 						ph = mtod(oper,
1071f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1072f8829a4aSRandall Stewart 						ph->param_type =
1073f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1074f8829a4aSRandall Stewart 						ph->param_length =
1075139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1076f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1077a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_4);
1078f8829a4aSRandall Stewart 						ippp++;
1079f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1080f8829a4aSRandall Stewart 						ippp++;
1081f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1082f8829a4aSRandall Stewart 					}
1083a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_4;
1084f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1085ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1086f8829a4aSRandall Stewart 					*abort_flag = 1;
1087f8829a4aSRandall Stewart 				} else if ((asoc->fragment_flags & SCTP_DATA_UNORDERED) !=
1088f8829a4aSRandall Stewart 					    SCTP_DATA_UNORDERED &&
1089f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq !=
1090f8829a4aSRandall Stewart 				    asoc->ssn_of_pdapi) {
1091f8829a4aSRandall Stewart 					/* Got to be the right STR Seq */
1092ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream seq %d vs %d\n",
1093f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
1094f8829a4aSRandall Stewart 					    asoc->ssn_of_pdapi);
1095139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1096f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1097f8829a4aSRandall Stewart 					if (oper) {
1098f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1099f8829a4aSRandall Stewart 						uint32_t *ippp;
1100f8829a4aSRandall Stewart 
1101139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1102f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1103f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1104f8829a4aSRandall Stewart 						ph = mtod(oper,
1105f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1106f8829a4aSRandall Stewart 						ph->param_type =
1107f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1108f8829a4aSRandall Stewart 						ph->param_length =
1109139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1110f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1111a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_5);
1112f8829a4aSRandall Stewart 						ippp++;
1113f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1114f8829a4aSRandall Stewart 						ippp++;
1115f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1116f8829a4aSRandall Stewart 
1117f8829a4aSRandall Stewart 					}
1118a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_5;
1119f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1120ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1121f8829a4aSRandall Stewart 					*abort_flag = 1;
1122f8829a4aSRandall Stewart 				}
1123f8829a4aSRandall Stewart 			}
1124f8829a4aSRandall Stewart 		}
1125f8829a4aSRandall Stewart 		return;
1126f8829a4aSRandall Stewart 	}
1127f8829a4aSRandall Stewart 	/* Find its place */
1128f8829a4aSRandall Stewart 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
1129f8829a4aSRandall Stewart 		if (compare_with_wrap(at->rec.data.TSN_seq,
1130f8829a4aSRandall Stewart 		    chk->rec.data.TSN_seq, MAX_TSN)) {
1131f8829a4aSRandall Stewart 			/*
1132f8829a4aSRandall Stewart 			 * one in queue is bigger than the new one, insert
1133f8829a4aSRandall Stewart 			 * before this one
1134f8829a4aSRandall Stewart 			 */
1135f8829a4aSRandall Stewart 			/* A check */
1136f8829a4aSRandall Stewart 			asoc->size_on_reasm_queue += chk->send_size;
1137f8829a4aSRandall Stewart 			sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1138f8829a4aSRandall Stewart 			next = at;
1139f8829a4aSRandall Stewart 			TAILQ_INSERT_BEFORE(at, chk, sctp_next);
1140f8829a4aSRandall Stewart 			break;
1141f8829a4aSRandall Stewart 		} else if (at->rec.data.TSN_seq == chk->rec.data.TSN_seq) {
1142f8829a4aSRandall Stewart 			/* Gak, He sent me a duplicate str seq number */
1143f8829a4aSRandall Stewart 			/*
1144f8829a4aSRandall Stewart 			 * foo bar, I guess I will just free this new guy,
1145f8829a4aSRandall Stewart 			 * should we abort too? FIX ME MAYBE? Or it COULD be
1146f8829a4aSRandall Stewart 			 * that the SSN's have wrapped. Maybe I should
1147f8829a4aSRandall Stewart 			 * compare to TSN somehow... sigh for now just blow
1148f8829a4aSRandall Stewart 			 * away the chunk!
1149f8829a4aSRandall Stewart 			 */
1150f8829a4aSRandall Stewart 			if (chk->data) {
1151f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
1152f8829a4aSRandall Stewart 				chk->data = NULL;
1153f8829a4aSRandall Stewart 			}
1154f8829a4aSRandall Stewart 			sctp_free_a_chunk(stcb, chk);
1155f8829a4aSRandall Stewart 			return;
1156f8829a4aSRandall Stewart 		} else {
1157f8829a4aSRandall Stewart 			last_flags = at->rec.data.rcv_flags;
1158f8829a4aSRandall Stewart 			last_tsn = at->rec.data.TSN_seq;
1159f8829a4aSRandall Stewart 			prev = at;
1160f8829a4aSRandall Stewart 			if (TAILQ_NEXT(at, sctp_next) == NULL) {
1161f8829a4aSRandall Stewart 				/*
1162f8829a4aSRandall Stewart 				 * We are at the end, insert it after this
1163f8829a4aSRandall Stewart 				 * one
1164f8829a4aSRandall Stewart 				 */
1165f8829a4aSRandall Stewart 				/* check it first */
1166f8829a4aSRandall Stewart 				asoc->size_on_reasm_queue += chk->send_size;
1167f8829a4aSRandall Stewart 				sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1168f8829a4aSRandall Stewart 				TAILQ_INSERT_AFTER(&asoc->reasmqueue, at, chk, sctp_next);
1169f8829a4aSRandall Stewart 				break;
1170f8829a4aSRandall Stewart 			}
1171f8829a4aSRandall Stewart 		}
1172f8829a4aSRandall Stewart 	}
1173f8829a4aSRandall Stewart 	/* Now the audits */
1174f8829a4aSRandall Stewart 	if (prev) {
1175f8829a4aSRandall Stewart 		prev_tsn = chk->rec.data.TSN_seq - 1;
1176f8829a4aSRandall Stewart 		if (prev_tsn == prev->rec.data.TSN_seq) {
1177f8829a4aSRandall Stewart 			/*
1178f8829a4aSRandall Stewart 			 * Ok the one I am dropping onto the end is the
1179f8829a4aSRandall Stewart 			 * NEXT. A bit of valdiation here.
1180f8829a4aSRandall Stewart 			 */
1181f8829a4aSRandall Stewart 			if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1182f8829a4aSRandall Stewart 			    SCTP_DATA_FIRST_FRAG ||
1183f8829a4aSRandall Stewart 			    (prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1184f8829a4aSRandall Stewart 			    SCTP_DATA_MIDDLE_FRAG) {
1185f8829a4aSRandall Stewart 				/*
1186f8829a4aSRandall Stewart 				 * Insert chk MUST be a MIDDLE or LAST
1187f8829a4aSRandall Stewart 				 * fragment
1188f8829a4aSRandall Stewart 				 */
1189f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1190f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1191ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - It can be a midlle or last but not a first\n");
1192ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it's a FIRST!\n");
1193139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1194f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1195f8829a4aSRandall Stewart 					if (oper) {
1196f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1197f8829a4aSRandall Stewart 						uint32_t *ippp;
1198f8829a4aSRandall Stewart 
1199139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1200f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1201f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1202f8829a4aSRandall Stewart 						ph = mtod(oper,
1203f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1204f8829a4aSRandall Stewart 						ph->param_type =
1205f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1206f8829a4aSRandall Stewart 						ph->param_length =
1207139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1208f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1209a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_6);
1210f8829a4aSRandall Stewart 						ippp++;
1211f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1212f8829a4aSRandall Stewart 						ippp++;
1213f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1214f8829a4aSRandall Stewart 
1215f8829a4aSRandall Stewart 					}
1216a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_6;
1217f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1218ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1219f8829a4aSRandall Stewart 					*abort_flag = 1;
1220f8829a4aSRandall Stewart 					return;
1221f8829a4aSRandall Stewart 				}
1222f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
1223f8829a4aSRandall Stewart 				    prev->rec.data.stream_number) {
1224f8829a4aSRandall Stewart 					/*
1225f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1226f8829a4aSRandall Stewart 					 * they must be the same.
1227f8829a4aSRandall Stewart 					 */
1228ad81507eSRandall Stewart 					SCTP_PRINTF("Prev check - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1229f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
1230f8829a4aSRandall Stewart 					    prev->rec.data.stream_number);
1231139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1232f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1233f8829a4aSRandall Stewart 					if (oper) {
1234f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1235f8829a4aSRandall Stewart 						uint32_t *ippp;
1236f8829a4aSRandall Stewart 
1237139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1238f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1239f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1240f8829a4aSRandall Stewart 						ph = mtod(oper,
1241f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1242f8829a4aSRandall Stewart 						ph->param_type =
1243f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1244f8829a4aSRandall Stewart 						ph->param_length =
1245139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1246f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1247a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_7);
1248f8829a4aSRandall Stewart 						ippp++;
1249f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1250f8829a4aSRandall Stewart 						ippp++;
1251f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1252f8829a4aSRandall Stewart 					}
1253a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_7;
1254f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1255ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1256f8829a4aSRandall Stewart 
1257f8829a4aSRandall Stewart 					*abort_flag = 1;
1258f8829a4aSRandall Stewart 					return;
1259f8829a4aSRandall Stewart 				}
1260f8829a4aSRandall Stewart 				if ((prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1261f8829a4aSRandall Stewart 				    chk->rec.data.stream_seq !=
1262f8829a4aSRandall Stewart 				    prev->rec.data.stream_seq) {
1263f8829a4aSRandall Stewart 					/*
1264f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1265f8829a4aSRandall Stewart 					 * they must be the same.
1266f8829a4aSRandall Stewart 					 */
1267ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1268f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
1269f8829a4aSRandall Stewart 					    prev->rec.data.stream_seq);
1270139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1271f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1272f8829a4aSRandall Stewart 					if (oper) {
1273f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1274f8829a4aSRandall Stewart 						uint32_t *ippp;
1275f8829a4aSRandall Stewart 
1276139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1277f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1278f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1279f8829a4aSRandall Stewart 						ph = mtod(oper,
1280f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1281f8829a4aSRandall Stewart 						ph->param_type =
1282f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1283f8829a4aSRandall Stewart 						ph->param_length =
1284139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1285f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1286a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_8);
1287f8829a4aSRandall Stewart 						ippp++;
1288f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1289f8829a4aSRandall Stewart 						ippp++;
1290f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1291f8829a4aSRandall Stewart 					}
1292a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_8;
1293f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1294ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1295f8829a4aSRandall Stewart 
1296f8829a4aSRandall Stewart 					*abort_flag = 1;
1297f8829a4aSRandall Stewart 					return;
1298f8829a4aSRandall Stewart 				}
1299f8829a4aSRandall Stewart 			} else if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1300f8829a4aSRandall Stewart 			    SCTP_DATA_LAST_FRAG) {
1301f8829a4aSRandall Stewart 				/* Insert chk MUST be a FIRST */
1302f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1303f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1304ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, evil plot, its not FIRST and it must be!\n");
1305139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1306f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1307f8829a4aSRandall Stewart 					if (oper) {
1308f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1309f8829a4aSRandall Stewart 						uint32_t *ippp;
1310f8829a4aSRandall Stewart 
1311139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1312f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1313f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1314f8829a4aSRandall Stewart 						ph = mtod(oper,
1315f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1316f8829a4aSRandall Stewart 						ph->param_type =
1317f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1318f8829a4aSRandall Stewart 						ph->param_length =
1319139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1320f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1321a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_9);
1322f8829a4aSRandall Stewart 						ippp++;
1323f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1324f8829a4aSRandall Stewart 						ippp++;
1325f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1326f8829a4aSRandall Stewart 
1327f8829a4aSRandall Stewart 					}
1328a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_9;
1329f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1330ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1331f8829a4aSRandall Stewart 
1332f8829a4aSRandall Stewart 					*abort_flag = 1;
1333f8829a4aSRandall Stewart 					return;
1334f8829a4aSRandall Stewart 				}
1335f8829a4aSRandall Stewart 			}
1336f8829a4aSRandall Stewart 		}
1337f8829a4aSRandall Stewart 	}
1338f8829a4aSRandall Stewart 	if (next) {
1339f8829a4aSRandall Stewart 		post_tsn = chk->rec.data.TSN_seq + 1;
1340f8829a4aSRandall Stewart 		if (post_tsn == next->rec.data.TSN_seq) {
1341f8829a4aSRandall Stewart 			/*
1342f8829a4aSRandall Stewart 			 * Ok the one I am inserting ahead of is my NEXT
1343f8829a4aSRandall Stewart 			 * one. A bit of valdiation here.
1344f8829a4aSRandall Stewart 			 */
1345f8829a4aSRandall Stewart 			if (next->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
1346f8829a4aSRandall Stewart 				/* Insert chk MUST be a last fragment */
1347f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK)
1348f8829a4aSRandall Stewart 				    != SCTP_DATA_LAST_FRAG) {
1349ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is FIRST, we must be LAST\n");
1350ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not a last!\n");
1351139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1352f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1353f8829a4aSRandall Stewart 					if (oper) {
1354f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1355f8829a4aSRandall Stewart 						uint32_t *ippp;
1356f8829a4aSRandall Stewart 
1357139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1358f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1359f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1360f8829a4aSRandall Stewart 						ph = mtod(oper,
1361f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1362f8829a4aSRandall Stewart 						ph->param_type =
1363f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1364f8829a4aSRandall Stewart 						ph->param_length =
1365139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1366f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1367a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_10);
1368f8829a4aSRandall Stewart 						ippp++;
1369f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1370f8829a4aSRandall Stewart 						ippp++;
1371f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1372f8829a4aSRandall Stewart 					}
1373a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_10;
1374f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1375ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1376f8829a4aSRandall Stewart 
1377f8829a4aSRandall Stewart 					*abort_flag = 1;
1378f8829a4aSRandall Stewart 					return;
1379f8829a4aSRandall Stewart 				}
1380f8829a4aSRandall Stewart 			} else if ((next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1381f8829a4aSRandall Stewart 				    SCTP_DATA_MIDDLE_FRAG ||
1382f8829a4aSRandall Stewart 				    (next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1383f8829a4aSRandall Stewart 			    SCTP_DATA_LAST_FRAG) {
1384f8829a4aSRandall Stewart 				/*
1385f8829a4aSRandall Stewart 				 * Insert chk CAN be MIDDLE or FIRST NOT
1386f8829a4aSRandall Stewart 				 * LAST
1387f8829a4aSRandall Stewart 				 */
1388f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1389f8829a4aSRandall Stewart 				    SCTP_DATA_LAST_FRAG) {
1390ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is a MIDDLE/LAST\n");
1391ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, new prev chunk is a LAST\n");
1392139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1393f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1394f8829a4aSRandall Stewart 					if (oper) {
1395f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1396f8829a4aSRandall Stewart 						uint32_t *ippp;
1397f8829a4aSRandall Stewart 
1398139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1399f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1400f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1401f8829a4aSRandall Stewart 						ph = mtod(oper,
1402f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1403f8829a4aSRandall Stewart 						ph->param_type =
1404f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1405f8829a4aSRandall Stewart 						ph->param_length =
1406139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1407f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1408a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_11);
1409f8829a4aSRandall Stewart 						ippp++;
1410f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1411f8829a4aSRandall Stewart 						ippp++;
1412f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1413f8829a4aSRandall Stewart 
1414f8829a4aSRandall Stewart 					}
1415a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_11;
1416f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1417ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1418f8829a4aSRandall Stewart 
1419f8829a4aSRandall Stewart 					*abort_flag = 1;
1420f8829a4aSRandall Stewart 					return;
1421f8829a4aSRandall Stewart 				}
1422f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
1423f8829a4aSRandall Stewart 				    next->rec.data.stream_number) {
1424f8829a4aSRandall Stewart 					/*
1425f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1426f8829a4aSRandall Stewart 					 * they must be the same.
1427f8829a4aSRandall Stewart 					 */
1428ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1429f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
1430f8829a4aSRandall Stewart 					    next->rec.data.stream_number);
1431139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1432f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1433f8829a4aSRandall Stewart 					if (oper) {
1434f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1435f8829a4aSRandall Stewart 						uint32_t *ippp;
1436f8829a4aSRandall Stewart 
1437139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1438f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1439f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1440f8829a4aSRandall Stewart 						ph = mtod(oper,
1441f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1442f8829a4aSRandall Stewart 						ph->param_type =
1443f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1444f8829a4aSRandall Stewart 						ph->param_length =
1445139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1446f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1447a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_12);
1448f8829a4aSRandall Stewart 						ippp++;
1449f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1450f8829a4aSRandall Stewart 						ippp++;
1451f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1452f8829a4aSRandall Stewart 
1453f8829a4aSRandall Stewart 					}
1454a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_12;
1455f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1456ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1457f8829a4aSRandall Stewart 
1458f8829a4aSRandall Stewart 					*abort_flag = 1;
1459f8829a4aSRandall Stewart 					return;
1460f8829a4aSRandall Stewart 				}
1461f8829a4aSRandall Stewart 				if ((next->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1462f8829a4aSRandall Stewart 				    chk->rec.data.stream_seq !=
1463f8829a4aSRandall Stewart 				    next->rec.data.stream_seq) {
1464f8829a4aSRandall Stewart 					/*
1465f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1466f8829a4aSRandall Stewart 					 * they must be the same.
1467f8829a4aSRandall Stewart 					 */
1468ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1469f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
1470f8829a4aSRandall Stewart 					    next->rec.data.stream_seq);
1471139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1472f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1473f8829a4aSRandall Stewart 					if (oper) {
1474f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1475f8829a4aSRandall Stewart 						uint32_t *ippp;
1476f8829a4aSRandall Stewart 
1477139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1478f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1479f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1480f8829a4aSRandall Stewart 						ph = mtod(oper,
1481f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1482f8829a4aSRandall Stewart 						ph->param_type =
1483f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1484f8829a4aSRandall Stewart 						ph->param_length =
1485139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1486f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1487a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_13);
1488f8829a4aSRandall Stewart 						ippp++;
1489f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1490f8829a4aSRandall Stewart 						ippp++;
1491f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1492f8829a4aSRandall Stewart 					}
1493a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_13;
1494f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1495ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1496f8829a4aSRandall Stewart 
1497f8829a4aSRandall Stewart 					*abort_flag = 1;
1498f8829a4aSRandall Stewart 					return;
1499f8829a4aSRandall Stewart 				}
1500f8829a4aSRandall Stewart 			}
1501f8829a4aSRandall Stewart 		}
1502f8829a4aSRandall Stewart 	}
1503f8829a4aSRandall Stewart 	/* Do we need to do some delivery? check */
1504f8829a4aSRandall Stewart 	sctp_deliver_reasm_check(stcb, asoc);
1505f8829a4aSRandall Stewart }
1506f8829a4aSRandall Stewart 
1507f8829a4aSRandall Stewart /*
1508f8829a4aSRandall Stewart  * This is an unfortunate routine. It checks to make sure a evil guy is not
1509f8829a4aSRandall Stewart  * stuffing us full of bad packet fragments. A broken peer could also do this
1510f8829a4aSRandall Stewart  * but this is doubtful. It is to bad I must worry about evil crackers sigh
1511f8829a4aSRandall Stewart  * :< more cycles.
1512f8829a4aSRandall Stewart  */
1513f8829a4aSRandall Stewart static int
1514f8829a4aSRandall Stewart sctp_does_tsn_belong_to_reasm(struct sctp_association *asoc,
1515f8829a4aSRandall Stewart     uint32_t TSN_seq)
1516f8829a4aSRandall Stewart {
1517f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *at;
1518f8829a4aSRandall Stewart 	uint32_t tsn_est;
1519f8829a4aSRandall Stewart 
1520f8829a4aSRandall Stewart 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
1521f8829a4aSRandall Stewart 		if (compare_with_wrap(TSN_seq,
1522f8829a4aSRandall Stewart 		    at->rec.data.TSN_seq, MAX_TSN)) {
1523f8829a4aSRandall Stewart 			/* is it one bigger? */
1524f8829a4aSRandall Stewart 			tsn_est = at->rec.data.TSN_seq + 1;
1525f8829a4aSRandall Stewart 			if (tsn_est == TSN_seq) {
1526f8829a4aSRandall Stewart 				/* yep. It better be a last then */
1527f8829a4aSRandall Stewart 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1528f8829a4aSRandall Stewart 				    SCTP_DATA_LAST_FRAG) {
1529f8829a4aSRandall Stewart 					/*
1530f8829a4aSRandall Stewart 					 * Ok this guy belongs next to a guy
1531f8829a4aSRandall Stewart 					 * that is NOT last, it should be a
1532f8829a4aSRandall Stewart 					 * middle/last, not a complete
1533f8829a4aSRandall Stewart 					 * chunk.
1534f8829a4aSRandall Stewart 					 */
1535f8829a4aSRandall Stewart 					return (1);
1536f8829a4aSRandall Stewart 				} else {
1537f8829a4aSRandall Stewart 					/*
1538f8829a4aSRandall Stewart 					 * This guy is ok since its a LAST
1539f8829a4aSRandall Stewart 					 * and the new chunk is a fully
1540f8829a4aSRandall Stewart 					 * self- contained one.
1541f8829a4aSRandall Stewart 					 */
1542f8829a4aSRandall Stewart 					return (0);
1543f8829a4aSRandall Stewart 				}
1544f8829a4aSRandall Stewart 			}
1545f8829a4aSRandall Stewart 		} else if (TSN_seq == at->rec.data.TSN_seq) {
1546f8829a4aSRandall Stewart 			/* Software error since I have a dup? */
1547f8829a4aSRandall Stewart 			return (1);
1548f8829a4aSRandall Stewart 		} else {
1549f8829a4aSRandall Stewart 			/*
1550f8829a4aSRandall Stewart 			 * Ok, 'at' is larger than new chunk but does it
1551f8829a4aSRandall Stewart 			 * need to be right before it.
1552f8829a4aSRandall Stewart 			 */
1553f8829a4aSRandall Stewart 			tsn_est = TSN_seq + 1;
1554f8829a4aSRandall Stewart 			if (tsn_est == at->rec.data.TSN_seq) {
1555f8829a4aSRandall Stewart 				/* Yep, It better be a first */
1556f8829a4aSRandall Stewart 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1557f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1558f8829a4aSRandall Stewart 					return (1);
1559f8829a4aSRandall Stewart 				} else {
1560f8829a4aSRandall Stewart 					return (0);
1561f8829a4aSRandall Stewart 				}
1562f8829a4aSRandall Stewart 			}
1563f8829a4aSRandall Stewart 		}
1564f8829a4aSRandall Stewart 	}
1565f8829a4aSRandall Stewart 	return (0);
1566f8829a4aSRandall Stewart }
1567f8829a4aSRandall Stewart 
1568f8829a4aSRandall Stewart 
1569f8829a4aSRandall Stewart static int
1570f8829a4aSRandall Stewart sctp_process_a_data_chunk(struct sctp_tcb *stcb, struct sctp_association *asoc,
1571f8829a4aSRandall Stewart     struct mbuf **m, int offset, struct sctp_data_chunk *ch, int chk_length,
1572f8829a4aSRandall Stewart     struct sctp_nets *net, uint32_t * high_tsn, int *abort_flag,
1573f8829a4aSRandall Stewart     int *break_flag, int last_chunk)
1574f8829a4aSRandall Stewart {
1575f8829a4aSRandall Stewart 	/* Process a data chunk */
1576f8829a4aSRandall Stewart 	/* struct sctp_tmit_chunk *chk; */
1577f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
1578f8829a4aSRandall Stewart 	uint32_t tsn, gap;
1579830d754dSRandall Stewart 
1580830d754dSRandall Stewart 	/* EY - for nr_sack */
1581830d754dSRandall Stewart 	uint32_t nr_gap;
1582f8829a4aSRandall Stewart 	struct mbuf *dmbuf;
1583f8829a4aSRandall Stewart 	int indx, the_len;
1584139bc87fSRandall Stewart 	int need_reasm_check = 0;
1585f8829a4aSRandall Stewart 	uint16_t strmno, strmseq;
1586f8829a4aSRandall Stewart 	struct mbuf *oper;
1587f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
1588f42a358aSRandall Stewart 	int ordered;
1589f42a358aSRandall Stewart 	uint32_t protocol_id;
1590f42a358aSRandall Stewart 	uint8_t chunk_flags;
159117205eccSRandall Stewart 	struct sctp_stream_reset_list *liste;
1592f8829a4aSRandall Stewart 
1593f8829a4aSRandall Stewart 	chk = NULL;
1594f8829a4aSRandall Stewart 	tsn = ntohl(ch->dp.tsn);
1595f42a358aSRandall Stewart 	chunk_flags = ch->ch.chunk_flags;
1596b3f1ea41SRandall Stewart 	if ((chunk_flags & SCTP_DATA_SACK_IMMEDIATELY) == SCTP_DATA_SACK_IMMEDIATELY) {
1597b3f1ea41SRandall Stewart 		asoc->send_sack = 1;
1598b3f1ea41SRandall Stewart 	}
1599f42a358aSRandall Stewart 	protocol_id = ch->dp.protocol_id;
1600f42a358aSRandall Stewart 	ordered = ((ch->ch.chunk_flags & SCTP_DATA_UNORDERED) == 0);
1601b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1602c4739e2fSRandall Stewart 		sctp_log_map(tsn, asoc->cumulative_tsn, asoc->highest_tsn_inside_map, SCTP_MAP_TSN_ENTERS);
160380fefe0aSRandall Stewart 	}
1604ad81507eSRandall Stewart 	if (stcb == NULL) {
1605ad81507eSRandall Stewart 		return (0);
1606ad81507eSRandall Stewart 	}
160780fefe0aSRandall Stewart 	SCTP_LTRACE_CHK(stcb->sctp_ep, stcb, ch->ch.chunk_type, tsn);
1608f8829a4aSRandall Stewart 	if (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN) ||
1609f8829a4aSRandall Stewart 	    asoc->cumulative_tsn == tsn) {
1610f8829a4aSRandall Stewart 		/* It is a duplicate */
1611f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvdupdata);
1612f8829a4aSRandall Stewart 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1613f8829a4aSRandall Stewart 			/* Record a dup for the next outbound sack */
1614f8829a4aSRandall Stewart 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1615f8829a4aSRandall Stewart 			asoc->numduptsns++;
1616f8829a4aSRandall Stewart 		}
1617b201f536SRandall Stewart 		asoc->send_sack = 1;
1618f8829a4aSRandall Stewart 		return (0);
1619f8829a4aSRandall Stewart 	}
1620f8829a4aSRandall Stewart 	/* Calculate the number of TSN's between the base and this TSN */
1621d50c1d79SRandall Stewart 	SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
1622f8829a4aSRandall Stewart 	if (gap >= (SCTP_MAPPING_ARRAY << 3)) {
1623f8829a4aSRandall Stewart 		/* Can't hold the bit in the mapping at max array, toss it */
1624f8829a4aSRandall Stewart 		return (0);
1625f8829a4aSRandall Stewart 	}
1626f8829a4aSRandall Stewart 	if (gap >= (uint32_t) (asoc->mapping_array_size << 3)) {
1627207304d4SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
16280696e120SRandall Stewart 		if (sctp_expand_mapping_array(asoc, gap)) {
1629f8829a4aSRandall Stewart 			/* Can't expand, drop it */
1630f8829a4aSRandall Stewart 			return (0);
1631f8829a4aSRandall Stewart 		}
1632f8829a4aSRandall Stewart 	}
1633830d754dSRandall Stewart 	/* EY - for nr_sack */
1634830d754dSRandall Stewart 	nr_gap = gap;
1635830d754dSRandall Stewart 
1636f8829a4aSRandall Stewart 	if (compare_with_wrap(tsn, *high_tsn, MAX_TSN)) {
1637f8829a4aSRandall Stewart 		*high_tsn = tsn;
1638f8829a4aSRandall Stewart 	}
1639f8829a4aSRandall Stewart 	/* See if we have received this one already */
1640f8829a4aSRandall Stewart 	if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
1641f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvdupdata);
1642f8829a4aSRandall Stewart 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1643f8829a4aSRandall Stewart 			/* Record a dup for the next outbound sack */
1644f8829a4aSRandall Stewart 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1645f8829a4aSRandall Stewart 			asoc->numduptsns++;
1646f8829a4aSRandall Stewart 		}
164742551e99SRandall Stewart 		asoc->send_sack = 1;
1648f8829a4aSRandall Stewart 		return (0);
1649f8829a4aSRandall Stewart 	}
1650f8829a4aSRandall Stewart 	/*
1651f8829a4aSRandall Stewart 	 * Check to see about the GONE flag, duplicates would cause a sack
1652f8829a4aSRandall Stewart 	 * to be sent up above
1653f8829a4aSRandall Stewart 	 */
1654ad81507eSRandall Stewart 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1655f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1656f8829a4aSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET))
1657f8829a4aSRandall Stewart 	    ) {
1658f8829a4aSRandall Stewart 		/*
1659f8829a4aSRandall Stewart 		 * wait a minute, this guy is gone, there is no longer a
1660f8829a4aSRandall Stewart 		 * receiver. Send peer an ABORT!
1661f8829a4aSRandall Stewart 		 */
1662f8829a4aSRandall Stewart 		struct mbuf *op_err;
1663f8829a4aSRandall Stewart 
1664f8829a4aSRandall Stewart 		op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1665ceaad40aSRandall Stewart 		sctp_abort_an_association(stcb->sctp_ep, stcb, 0, op_err, SCTP_SO_NOT_LOCKED);
1666f8829a4aSRandall Stewart 		*abort_flag = 1;
1667f8829a4aSRandall Stewart 		return (0);
1668f8829a4aSRandall Stewart 	}
1669f8829a4aSRandall Stewart 	/*
1670f8829a4aSRandall Stewart 	 * Now before going further we see if there is room. If NOT then we
1671f8829a4aSRandall Stewart 	 * MAY let one through only IF this TSN is the one we are waiting
1672f8829a4aSRandall Stewart 	 * for on a partial delivery API.
1673f8829a4aSRandall Stewart 	 */
1674f8829a4aSRandall Stewart 
1675f8829a4aSRandall Stewart 	/* now do the tests */
1676f8829a4aSRandall Stewart 	if (((asoc->cnt_on_all_streams +
1677f8829a4aSRandall Stewart 	    asoc->cnt_on_reasm_queue +
1678b3f1ea41SRandall Stewart 	    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) ||
1679f8829a4aSRandall Stewart 	    (((int)asoc->my_rwnd) <= 0)) {
1680f8829a4aSRandall Stewart 		/*
1681f8829a4aSRandall Stewart 		 * When we have NO room in the rwnd we check to make sure
1682f8829a4aSRandall Stewart 		 * the reader is doing its job...
1683f8829a4aSRandall Stewart 		 */
1684f8829a4aSRandall Stewart 		if (stcb->sctp_socket->so_rcv.sb_cc) {
1685f8829a4aSRandall Stewart 			/* some to read, wake-up */
1686ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1687ceaad40aSRandall Stewart 			struct socket *so;
1688ceaad40aSRandall Stewart 
1689ceaad40aSRandall Stewart 			so = SCTP_INP_SO(stcb->sctp_ep);
1690ceaad40aSRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, 1);
1691ceaad40aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
1692ceaad40aSRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
1693ceaad40aSRandall Stewart 			SCTP_TCB_LOCK(stcb);
1694ceaad40aSRandall Stewart 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
1695ceaad40aSRandall Stewart 			if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1696ceaad40aSRandall Stewart 				/* assoc was freed while we were unlocked */
1697ceaad40aSRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
1698ceaad40aSRandall Stewart 				return (0);
1699ceaad40aSRandall Stewart 			}
1700ceaad40aSRandall Stewart #endif
1701f8829a4aSRandall Stewart 			sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1702ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1703ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
1704ceaad40aSRandall Stewart #endif
1705f8829a4aSRandall Stewart 		}
1706f8829a4aSRandall Stewart 		/* now is it in the mapping array of what we have accepted? */
1707b3f1ea41SRandall Stewart 		if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
1708f8829a4aSRandall Stewart 			/* Nope not in the valid range dump it */
1709f8829a4aSRandall Stewart 			sctp_set_rwnd(stcb, asoc);
1710f8829a4aSRandall Stewart 			if ((asoc->cnt_on_all_streams +
1711f8829a4aSRandall Stewart 			    asoc->cnt_on_reasm_queue +
1712b3f1ea41SRandall Stewart 			    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) {
1713f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_datadropchklmt);
1714f8829a4aSRandall Stewart 			} else {
1715f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_datadroprwnd);
1716f8829a4aSRandall Stewart 			}
1717f8829a4aSRandall Stewart 			indx = *break_flag;
1718f8829a4aSRandall Stewart 			*break_flag = 1;
1719f8829a4aSRandall Stewart 			return (0);
1720f8829a4aSRandall Stewart 		}
1721f8829a4aSRandall Stewart 	}
1722f8829a4aSRandall Stewart 	strmno = ntohs(ch->dp.stream_id);
1723f8829a4aSRandall Stewart 	if (strmno >= asoc->streamincnt) {
1724f8829a4aSRandall Stewart 		struct sctp_paramhdr *phdr;
1725f8829a4aSRandall Stewart 		struct mbuf *mb;
1726f8829a4aSRandall Stewart 
1727f8829a4aSRandall Stewart 		mb = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) * 2),
1728139bc87fSRandall Stewart 		    0, M_DONTWAIT, 1, MT_DATA);
1729f8829a4aSRandall Stewart 		if (mb != NULL) {
1730f8829a4aSRandall Stewart 			/* add some space up front so prepend will work well */
1731139bc87fSRandall Stewart 			SCTP_BUF_RESV_UF(mb, sizeof(struct sctp_chunkhdr));
1732f8829a4aSRandall Stewart 			phdr = mtod(mb, struct sctp_paramhdr *);
1733f8829a4aSRandall Stewart 			/*
1734f8829a4aSRandall Stewart 			 * Error causes are just param's and this one has
1735f8829a4aSRandall Stewart 			 * two back to back phdr, one with the error type
1736f8829a4aSRandall Stewart 			 * and size, the other with the streamid and a rsvd
1737f8829a4aSRandall Stewart 			 */
1738139bc87fSRandall Stewart 			SCTP_BUF_LEN(mb) = (sizeof(struct sctp_paramhdr) * 2);
1739f8829a4aSRandall Stewart 			phdr->param_type = htons(SCTP_CAUSE_INVALID_STREAM);
1740f8829a4aSRandall Stewart 			phdr->param_length =
1741f8829a4aSRandall Stewart 			    htons(sizeof(struct sctp_paramhdr) * 2);
1742f8829a4aSRandall Stewart 			phdr++;
1743f8829a4aSRandall Stewart 			/* We insert the stream in the type field */
1744f8829a4aSRandall Stewart 			phdr->param_type = ch->dp.stream_id;
1745f8829a4aSRandall Stewart 			/* And set the length to 0 for the rsvd field */
1746f8829a4aSRandall Stewart 			phdr->param_length = 0;
1747f8829a4aSRandall Stewart 			sctp_queue_op_err(stcb, mb);
1748f8829a4aSRandall Stewart 		}
1749f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_badsid);
1750207304d4SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
1751d06c82f1SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
1752830d754dSRandall Stewart 		/* EY set this tsn present in  nr_sack's nr_mapping_array */
1753830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
1754830d754dSRandall Stewart 			SCTP_TCB_LOCK_ASSERT(stcb);
1755830d754dSRandall Stewart 			SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
1756d50c1d79SRandall Stewart 			SCTP_REVERSE_OUT_TSN_PRES(gap, tsn, asoc);
1757830d754dSRandall Stewart 		}
1758d06c82f1SRandall Stewart 		if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
1759d06c82f1SRandall Stewart 			/* we have a new high score */
1760d06c82f1SRandall Stewart 			asoc->highest_tsn_inside_map = tsn;
1761830d754dSRandall Stewart 			/* EY nr_sack version of the above */
1762830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack)
1763830d754dSRandall Stewart 				asoc->highest_tsn_inside_nr_map = tsn;
1764b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1765d06c82f1SRandall Stewart 				sctp_log_map(0, 2, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
176680fefe0aSRandall Stewart 			}
1767d06c82f1SRandall Stewart 		}
1768d06c82f1SRandall Stewart 		if (tsn == (asoc->cumulative_tsn + 1)) {
1769d06c82f1SRandall Stewart 			/* Update cum-ack */
1770d06c82f1SRandall Stewart 			asoc->cumulative_tsn = tsn;
1771d06c82f1SRandall Stewart 		}
1772f8829a4aSRandall Stewart 		return (0);
1773f8829a4aSRandall Stewart 	}
1774f8829a4aSRandall Stewart 	/*
1775f8829a4aSRandall Stewart 	 * Before we continue lets validate that we are not being fooled by
1776f8829a4aSRandall Stewart 	 * an evil attacker. We can only have 4k chunks based on our TSN
1777f8829a4aSRandall Stewart 	 * spread allowed by the mapping array 512 * 8 bits, so there is no
1778f8829a4aSRandall Stewart 	 * way our stream sequence numbers could have wrapped. We of course
1779f8829a4aSRandall Stewart 	 * only validate the FIRST fragment so the bit must be set.
1780f8829a4aSRandall Stewart 	 */
1781f8829a4aSRandall Stewart 	strmseq = ntohs(ch->dp.stream_sequence);
1782f42a358aSRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
178318e198d3SRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
178418e198d3SRandall Stewart 	if (asoc->tsn_in_at >= SCTP_TSN_LOG_SIZE) {
178518e198d3SRandall Stewart 		asoc->tsn_in_at = 0;
178618e198d3SRandall Stewart 		asoc->tsn_in_wrapped = 1;
178718e198d3SRandall Stewart 	}
1788f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].tsn = tsn;
1789f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].strm = strmno;
1790f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].seq = strmseq;
1791f1f73e57SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].sz = chk_length;
1792f1f73e57SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].flgs = chunk_flags;
179318e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].stcb = (void *)stcb;
179418e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].in_pos = asoc->tsn_in_at;
179518e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].in_out = 1;
1796f42a358aSRandall Stewart 	asoc->tsn_in_at++;
1797f42a358aSRandall Stewart #endif
1798f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_FIRST_FRAG) &&
1799d61a0ae0SRandall Stewart 	    (TAILQ_EMPTY(&asoc->resetHead)) &&
1800f42a358aSRandall Stewart 	    (chunk_flags & SCTP_DATA_UNORDERED) == 0 &&
1801f8829a4aSRandall Stewart 	    (compare_with_wrap(asoc->strmin[strmno].last_sequence_delivered,
1802f8829a4aSRandall Stewart 	    strmseq, MAX_SEQ) ||
1803f8829a4aSRandall Stewart 	    asoc->strmin[strmno].last_sequence_delivered == strmseq)) {
1804f8829a4aSRandall Stewart 		/* The incoming sseq is behind where we last delivered? */
1805ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1, "EVIL/Broken-Dup S-SEQ:%d delivered:%d from peer, Abort!\n",
1806ad81507eSRandall Stewart 		    strmseq, asoc->strmin[strmno].last_sequence_delivered);
1807139bc87fSRandall Stewart 		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1808f8829a4aSRandall Stewart 		    0, M_DONTWAIT, 1, MT_DATA);
1809f8829a4aSRandall Stewart 		if (oper) {
1810f8829a4aSRandall Stewart 			struct sctp_paramhdr *ph;
1811f8829a4aSRandall Stewart 			uint32_t *ippp;
1812f8829a4aSRandall Stewart 
1813139bc87fSRandall Stewart 			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
1814f8829a4aSRandall Stewart 			    (3 * sizeof(uint32_t));
1815f8829a4aSRandall Stewart 			ph = mtod(oper, struct sctp_paramhdr *);
1816f8829a4aSRandall Stewart 			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1817139bc87fSRandall Stewart 			ph->param_length = htons(SCTP_BUF_LEN(oper));
1818f8829a4aSRandall Stewart 			ippp = (uint32_t *) (ph + 1);
1819a5d547adSRandall Stewart 			*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_14);
1820f8829a4aSRandall Stewart 			ippp++;
1821f8829a4aSRandall Stewart 			*ippp = tsn;
1822f8829a4aSRandall Stewart 			ippp++;
1823f8829a4aSRandall Stewart 			*ippp = ((strmno << 16) | strmseq);
1824f8829a4aSRandall Stewart 
1825f8829a4aSRandall Stewart 		}
1826a5d547adSRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_14;
1827f8829a4aSRandall Stewart 		sctp_abort_an_association(stcb->sctp_ep, stcb,
1828ceaad40aSRandall Stewart 		    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1829f8829a4aSRandall Stewart 		*abort_flag = 1;
1830f8829a4aSRandall Stewart 		return (0);
1831f8829a4aSRandall Stewart 	}
1832f42a358aSRandall Stewart 	/************************************
1833f42a358aSRandall Stewart 	 * From here down we may find ch-> invalid
1834f42a358aSRandall Stewart 	 * so its a good idea NOT to use it.
1835f42a358aSRandall Stewart 	 *************************************/
1836f42a358aSRandall Stewart 
1837f8829a4aSRandall Stewart 	the_len = (chk_length - sizeof(struct sctp_data_chunk));
1838f8829a4aSRandall Stewart 	if (last_chunk == 0) {
183944b7479bSRandall Stewart 		dmbuf = SCTP_M_COPYM(*m,
1840f8829a4aSRandall Stewart 		    (offset + sizeof(struct sctp_data_chunk)),
1841f8829a4aSRandall Stewart 		    the_len, M_DONTWAIT);
1842f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
1843b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
1844f8829a4aSRandall Stewart 			struct mbuf *mat;
1845f8829a4aSRandall Stewart 
1846f8829a4aSRandall Stewart 			mat = dmbuf;
1847f8829a4aSRandall Stewart 			while (mat) {
1848139bc87fSRandall Stewart 				if (SCTP_BUF_IS_EXTENDED(mat)) {
1849f8829a4aSRandall Stewart 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
1850f8829a4aSRandall Stewart 				}
1851139bc87fSRandall Stewart 				mat = SCTP_BUF_NEXT(mat);
1852f8829a4aSRandall Stewart 			}
1853f8829a4aSRandall Stewart 		}
1854f8829a4aSRandall Stewart #endif
1855f8829a4aSRandall Stewart 	} else {
1856f8829a4aSRandall Stewart 		/* We can steal the last chunk */
1857139bc87fSRandall Stewart 		int l_len;
1858139bc87fSRandall Stewart 
1859f8829a4aSRandall Stewart 		dmbuf = *m;
1860f8829a4aSRandall Stewart 		/* lop off the top part */
1861f8829a4aSRandall Stewart 		m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk)));
1862139bc87fSRandall Stewart 		if (SCTP_BUF_NEXT(dmbuf) == NULL) {
1863139bc87fSRandall Stewart 			l_len = SCTP_BUF_LEN(dmbuf);
1864139bc87fSRandall Stewart 		} else {
1865139bc87fSRandall Stewart 			/*
1866139bc87fSRandall Stewart 			 * need to count up the size hopefully does not hit
1867139bc87fSRandall Stewart 			 * this to often :-0
1868139bc87fSRandall Stewart 			 */
1869139bc87fSRandall Stewart 			struct mbuf *lat;
1870139bc87fSRandall Stewart 
1871139bc87fSRandall Stewart 			l_len = 0;
1872139bc87fSRandall Stewart 			lat = dmbuf;
1873139bc87fSRandall Stewart 			while (lat) {
1874139bc87fSRandall Stewart 				l_len += SCTP_BUF_LEN(lat);
1875139bc87fSRandall Stewart 				lat = SCTP_BUF_NEXT(lat);
1876139bc87fSRandall Stewart 			}
1877139bc87fSRandall Stewart 		}
1878139bc87fSRandall Stewart 		if (l_len > the_len) {
1879f8829a4aSRandall Stewart 			/* Trim the end round bytes off  too */
1880139bc87fSRandall Stewart 			m_adj(dmbuf, -(l_len - the_len));
1881f8829a4aSRandall Stewart 		}
1882f8829a4aSRandall Stewart 	}
1883f8829a4aSRandall Stewart 	if (dmbuf == NULL) {
1884f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_nomem);
1885f8829a4aSRandall Stewart 		return (0);
1886f8829a4aSRandall Stewart 	}
1887f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG &&
1888f8829a4aSRandall Stewart 	    asoc->fragmented_delivery_inprogress == 0 &&
1889f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->resetHead) &&
1890f42a358aSRandall Stewart 	    ((ordered == 0) ||
1891f8829a4aSRandall Stewart 	    ((asoc->strmin[strmno].last_sequence_delivered + 1) == strmseq &&
1892f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->strmin[strmno].inqueue)))) {
1893f8829a4aSRandall Stewart 		/* Candidate for express delivery */
1894f8829a4aSRandall Stewart 		/*
1895f8829a4aSRandall Stewart 		 * Its not fragmented, No PD-API is up, Nothing in the
1896f8829a4aSRandall Stewart 		 * delivery queue, Its un-ordered OR ordered and the next to
1897f8829a4aSRandall Stewart 		 * deliver AND nothing else is stuck on the stream queue,
1898f8829a4aSRandall Stewart 		 * And there is room for it in the socket buffer. Lets just
1899f8829a4aSRandall Stewart 		 * stuff it up the buffer....
1900f8829a4aSRandall Stewart 		 */
1901f8829a4aSRandall Stewart 
1902f8829a4aSRandall Stewart 		/* It would be nice to avoid this copy if we could :< */
1903f8829a4aSRandall Stewart 		sctp_alloc_a_readq(stcb, control);
1904f8829a4aSRandall Stewart 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1905f42a358aSRandall Stewart 		    protocol_id,
1906f8829a4aSRandall Stewart 		    stcb->asoc.context,
1907f8829a4aSRandall Stewart 		    strmno, strmseq,
1908f42a358aSRandall Stewart 		    chunk_flags,
1909f8829a4aSRandall Stewart 		    dmbuf);
1910f8829a4aSRandall Stewart 		if (control == NULL) {
1911f8829a4aSRandall Stewart 			goto failed_express_del;
1912f8829a4aSRandall Stewart 		}
1913ceaad40aSRandall Stewart 		sctp_add_to_readq(stcb->sctp_ep, stcb, control, &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
1914830d754dSRandall Stewart 
1915830d754dSRandall Stewart 		/*
1916830d754dSRandall Stewart 		 * EY here I should check if this delivered tsn is
1917830d754dSRandall Stewart 		 * out_of_order, if yes then update the nr_map
1918830d754dSRandall Stewart 		 */
1919830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
1920830d754dSRandall Stewart 			/*
1921830d754dSRandall Stewart 			 * EY check if the mapping_array and nr_mapping
1922830d754dSRandall Stewart 			 * array are consistent
1923830d754dSRandall Stewart 			 */
1924830d754dSRandall Stewart 			if (asoc->mapping_array_base_tsn != asoc->nr_mapping_array_base_tsn)
1925830d754dSRandall Stewart 				/*
1926830d754dSRandall Stewart 				 * printf("EY-IN
1927830d754dSRandall Stewart 				 * sctp_process_a_data_chunk(5): Something
1928830d754dSRandall Stewart 				 * is wrong the map base tsn" "\nEY-and
1929830d754dSRandall Stewart 				 * nr_map base tsn should be equal.");
1930830d754dSRandall Stewart 				 */
1931830d754dSRandall Stewart 				/* EY debugging block */
1932830d754dSRandall Stewart 			{
1933830d754dSRandall Stewart 				/*
1934830d754dSRandall Stewart 				 * printf("\nEY-Calculating an
1935830d754dSRandall Stewart 				 * nr_gap!!\nmapping_array_size = %d
1936830d754dSRandall Stewart 				 * nr_mapping_array_size = %d"
1937830d754dSRandall Stewart 				 * "\nEY-mapping_array_base = %d
1938830d754dSRandall Stewart 				 * nr_mapping_array_base =
1939830d754dSRandall Stewart 				 * %d\nEY-highest_tsn_inside_map = %d"
1940830d754dSRandall Stewart 				 * "highest_tsn_inside_nr_map = %d\nEY-TSN =
1941830d754dSRandall Stewart 				 * %d nr_gap = %d",asoc->mapping_array_size,
1942830d754dSRandall Stewart 				 * asoc->nr_mapping_array_size,
1943830d754dSRandall Stewart 				 * asoc->mapping_array_base_tsn,
1944830d754dSRandall Stewart 				 * asoc->nr_mapping_array_base_tsn,
1945830d754dSRandall Stewart 				 * asoc->highest_tsn_inside_map,
1946830d754dSRandall Stewart 				 * asoc->highest_tsn_inside_nr_map,tsn,nr_gap
1947830d754dSRandall Stewart 				 * );
1948830d754dSRandall Stewart 				 */
1949830d754dSRandall Stewart 			}
1950830d754dSRandall Stewart 			/* EY - not %100 sure about the lock thing */
1951830d754dSRandall Stewart 			SCTP_TCB_LOCK_ASSERT(stcb);
1952830d754dSRandall Stewart 			SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
1953d50c1d79SRandall Stewart 			SCTP_REVERSE_OUT_TSN_PRES(nr_gap, tsn, asoc);
1954830d754dSRandall Stewart 			if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
1955830d754dSRandall Stewart 				asoc->highest_tsn_inside_nr_map = tsn;
1956830d754dSRandall Stewart 		}
1957f42a358aSRandall Stewart 		if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1958f8829a4aSRandall Stewart 			/* for ordered, bump what we delivered */
1959f8829a4aSRandall Stewart 			asoc->strmin[strmno].last_sequence_delivered++;
1960f8829a4aSRandall Stewart 		}
1961f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvexpress);
1962b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
19636a91f103SRandall Stewart 			sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno,
1964f8829a4aSRandall Stewart 			    SCTP_STR_LOG_FROM_EXPRS_DEL);
196580fefe0aSRandall Stewart 		}
1966f8829a4aSRandall Stewart 		control = NULL;
1967f8829a4aSRandall Stewart 		goto finish_express_del;
1968f8829a4aSRandall Stewart 	}
1969f8829a4aSRandall Stewart failed_express_del:
1970f8829a4aSRandall Stewart 	/* If we reach here this is a new chunk */
1971f8829a4aSRandall Stewart 	chk = NULL;
1972f8829a4aSRandall Stewart 	control = NULL;
1973f8829a4aSRandall Stewart 	/* Express for fragmented delivery? */
1974f8829a4aSRandall Stewart 	if ((asoc->fragmented_delivery_inprogress) &&
1975f8829a4aSRandall Stewart 	    (stcb->asoc.control_pdapi) &&
1976f8829a4aSRandall Stewart 	    (asoc->str_of_pdapi == strmno) &&
1977f8829a4aSRandall Stewart 	    (asoc->ssn_of_pdapi == strmseq)
1978f8829a4aSRandall Stewart 	    ) {
1979f8829a4aSRandall Stewart 		control = stcb->asoc.control_pdapi;
1980f42a358aSRandall Stewart 		if ((chunk_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
1981f8829a4aSRandall Stewart 			/* Can't be another first? */
1982f8829a4aSRandall Stewart 			goto failed_pdapi_express_del;
1983f8829a4aSRandall Stewart 		}
1984f8829a4aSRandall Stewart 		if (tsn == (control->sinfo_tsn + 1)) {
1985f8829a4aSRandall Stewart 			/* Yep, we can add it on */
1986f8829a4aSRandall Stewart 			int end = 0;
1987f8829a4aSRandall Stewart 			uint32_t cumack;
1988f8829a4aSRandall Stewart 
1989f42a358aSRandall Stewart 			if (chunk_flags & SCTP_DATA_LAST_FRAG) {
1990f8829a4aSRandall Stewart 				end = 1;
1991f8829a4aSRandall Stewart 			}
1992f8829a4aSRandall Stewart 			cumack = asoc->cumulative_tsn;
1993f8829a4aSRandall Stewart 			if ((cumack + 1) == tsn)
1994f8829a4aSRandall Stewart 				cumack = tsn;
1995f8829a4aSRandall Stewart 
1996f8829a4aSRandall Stewart 			if (sctp_append_to_readq(stcb->sctp_ep, stcb, control, dmbuf, end,
1997f8829a4aSRandall Stewart 			    tsn,
1998f8829a4aSRandall Stewart 			    &stcb->sctp_socket->so_rcv)) {
1999ad81507eSRandall Stewart 				SCTP_PRINTF("Append fails end:%d\n", end);
2000f8829a4aSRandall Stewart 				goto failed_pdapi_express_del;
2001f8829a4aSRandall Stewart 			}
2002830d754dSRandall Stewart 			/*
2003830d754dSRandall Stewart 			 * EY It is appended to the read queue in prev if
2004830d754dSRandall Stewart 			 * block here I should check if this delivered tsn
2005830d754dSRandall Stewart 			 * is out_of_order, if yes then update the nr_map
2006830d754dSRandall Stewart 			 */
2007830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
2008830d754dSRandall Stewart 				/* EY debugging block */
2009830d754dSRandall Stewart 				{
2010830d754dSRandall Stewart 					/*
2011830d754dSRandall Stewart 					 * printf("\nEY-Calculating an
2012830d754dSRandall Stewart 					 * nr_gap!!\nEY-mapping_array_size =
2013830d754dSRandall Stewart 					 * %d nr_mapping_array_size = %d"
2014830d754dSRandall Stewart 					 * "\nEY-mapping_array_base = %d
2015830d754dSRandall Stewart 					 * nr_mapping_array_base =
2016830d754dSRandall Stewart 					 * %d\nEY-highest_tsn_inside_map =
2017830d754dSRandall Stewart 					 * %d" "highest_tsn_inside_nr_map =
2018830d754dSRandall Stewart 					 * %d\nEY-TSN = %d nr_gap =
2019830d754dSRandall Stewart 					 * %d",asoc->mapping_array_size,
2020830d754dSRandall Stewart 					 * asoc->nr_mapping_array_size,
2021830d754dSRandall Stewart 					 * asoc->mapping_array_base_tsn,
2022830d754dSRandall Stewart 					 * asoc->nr_mapping_array_base_tsn,
2023830d754dSRandall Stewart 					 * asoc->highest_tsn_inside_map,
2024830d754dSRandall Stewart 					 * asoc->highest_tsn_inside_nr_map,ts
2025830d754dSRandall Stewart 					 * n,nr_gap);
2026830d754dSRandall Stewart 					 */
2027830d754dSRandall Stewart 				}
2028830d754dSRandall Stewart 				/* EY - not %100 sure about the lock thing */
2029830d754dSRandall Stewart 				SCTP_TCB_LOCK_ASSERT(stcb);
2030830d754dSRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
2031d50c1d79SRandall Stewart 				SCTP_REVERSE_OUT_TSN_PRES(nr_gap, tsn, asoc);
2032830d754dSRandall Stewart 				if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
2033830d754dSRandall Stewart 					asoc->highest_tsn_inside_nr_map = tsn;
2034830d754dSRandall Stewart 			}
2035f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvexpressm);
2036f8829a4aSRandall Stewart 			control->sinfo_tsn = tsn;
2037f8829a4aSRandall Stewart 			asoc->tsn_last_delivered = tsn;
2038f42a358aSRandall Stewart 			asoc->fragment_flags = chunk_flags;
2039f8829a4aSRandall Stewart 			asoc->tsn_of_pdapi_last_delivered = tsn;
2040f42a358aSRandall Stewart 			asoc->last_flags_delivered = chunk_flags;
2041f8829a4aSRandall Stewart 			asoc->last_strm_seq_delivered = strmseq;
2042f8829a4aSRandall Stewart 			asoc->last_strm_no_delivered = strmno;
2043f8829a4aSRandall Stewart 			if (end) {
2044f8829a4aSRandall Stewart 				/* clean up the flags and such */
2045f8829a4aSRandall Stewart 				asoc->fragmented_delivery_inprogress = 0;
2046f42a358aSRandall Stewart 				if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
2047f8829a4aSRandall Stewart 					asoc->strmin[strmno].last_sequence_delivered++;
2048a5d547adSRandall Stewart 				}
2049f8829a4aSRandall Stewart 				stcb->asoc.control_pdapi = NULL;
2050139bc87fSRandall Stewart 				if (TAILQ_EMPTY(&asoc->reasmqueue) == 0) {
2051139bc87fSRandall Stewart 					/*
2052139bc87fSRandall Stewart 					 * There could be another message
2053139bc87fSRandall Stewart 					 * ready
2054139bc87fSRandall Stewart 					 */
2055139bc87fSRandall Stewart 					need_reasm_check = 1;
2056139bc87fSRandall Stewart 				}
2057f8829a4aSRandall Stewart 			}
2058f8829a4aSRandall Stewart 			control = NULL;
2059f8829a4aSRandall Stewart 			goto finish_express_del;
2060f8829a4aSRandall Stewart 		}
2061f8829a4aSRandall Stewart 	}
2062f8829a4aSRandall Stewart failed_pdapi_express_del:
2063f8829a4aSRandall Stewart 	control = NULL;
2064f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
2065f8829a4aSRandall Stewart 		sctp_alloc_a_chunk(stcb, chk);
2066f8829a4aSRandall Stewart 		if (chk == NULL) {
2067f8829a4aSRandall Stewart 			/* No memory so we drop the chunk */
2068f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_nomem);
2069f8829a4aSRandall Stewart 			if (last_chunk == 0) {
2070f8829a4aSRandall Stewart 				/* we copied it, free the copy */
2071f8829a4aSRandall Stewart 				sctp_m_freem(dmbuf);
2072f8829a4aSRandall Stewart 			}
2073f8829a4aSRandall Stewart 			return (0);
2074f8829a4aSRandall Stewart 		}
2075f8829a4aSRandall Stewart 		chk->rec.data.TSN_seq = tsn;
2076f8829a4aSRandall Stewart 		chk->no_fr_allowed = 0;
2077f8829a4aSRandall Stewart 		chk->rec.data.stream_seq = strmseq;
2078f8829a4aSRandall Stewart 		chk->rec.data.stream_number = strmno;
2079f42a358aSRandall Stewart 		chk->rec.data.payloadtype = protocol_id;
2080f8829a4aSRandall Stewart 		chk->rec.data.context = stcb->asoc.context;
2081f8829a4aSRandall Stewart 		chk->rec.data.doing_fast_retransmit = 0;
2082f42a358aSRandall Stewart 		chk->rec.data.rcv_flags = chunk_flags;
2083f8829a4aSRandall Stewart 		chk->asoc = asoc;
2084f8829a4aSRandall Stewart 		chk->send_size = the_len;
2085f8829a4aSRandall Stewart 		chk->whoTo = net;
2086f8829a4aSRandall Stewart 		atomic_add_int(&net->ref_count, 1);
2087f8829a4aSRandall Stewart 		chk->data = dmbuf;
2088f8829a4aSRandall Stewart 	} else {
2089f8829a4aSRandall Stewart 		sctp_alloc_a_readq(stcb, control);
2090f8829a4aSRandall Stewart 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
2091f42a358aSRandall Stewart 		    protocol_id,
2092f8829a4aSRandall Stewart 		    stcb->asoc.context,
2093f8829a4aSRandall Stewart 		    strmno, strmseq,
2094f42a358aSRandall Stewart 		    chunk_flags,
2095f8829a4aSRandall Stewart 		    dmbuf);
2096f8829a4aSRandall Stewart 		if (control == NULL) {
2097f8829a4aSRandall Stewart 			/* No memory so we drop the chunk */
2098f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_nomem);
2099f8829a4aSRandall Stewart 			if (last_chunk == 0) {
2100f8829a4aSRandall Stewart 				/* we copied it, free the copy */
2101f8829a4aSRandall Stewart 				sctp_m_freem(dmbuf);
2102f8829a4aSRandall Stewart 			}
2103f8829a4aSRandall Stewart 			return (0);
2104f8829a4aSRandall Stewart 		}
2105f8829a4aSRandall Stewart 		control->length = the_len;
2106f8829a4aSRandall Stewart 	}
2107f8829a4aSRandall Stewart 
2108f8829a4aSRandall Stewart 	/* Mark it as received */
2109f8829a4aSRandall Stewart 	/* Now queue it where it belongs */
2110f8829a4aSRandall Stewart 	if (control != NULL) {
2111f8829a4aSRandall Stewart 		/* First a sanity check */
2112f8829a4aSRandall Stewart 		if (asoc->fragmented_delivery_inprogress) {
2113f8829a4aSRandall Stewart 			/*
2114f8829a4aSRandall Stewart 			 * Ok, we have a fragmented delivery in progress if
2115f8829a4aSRandall Stewart 			 * this chunk is next to deliver OR belongs in our
2116f8829a4aSRandall Stewart 			 * view to the reassembly, the peer is evil or
2117f8829a4aSRandall Stewart 			 * broken.
2118f8829a4aSRandall Stewart 			 */
2119f8829a4aSRandall Stewart 			uint32_t estimate_tsn;
2120f8829a4aSRandall Stewart 
2121f8829a4aSRandall Stewart 			estimate_tsn = asoc->tsn_last_delivered + 1;
2122f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->reasmqueue) &&
2123f8829a4aSRandall Stewart 			    (estimate_tsn == control->sinfo_tsn)) {
2124f8829a4aSRandall Stewart 				/* Evil/Broke peer */
2125f8829a4aSRandall Stewart 				sctp_m_freem(control->data);
2126f8829a4aSRandall Stewart 				control->data = NULL;
21275bead436SRandall Stewart 				if (control->whoFrom) {
2128f8829a4aSRandall Stewart 					sctp_free_remote_addr(control->whoFrom);
21295bead436SRandall Stewart 					control->whoFrom = NULL;
21305bead436SRandall Stewart 				}
2131f8829a4aSRandall Stewart 				sctp_free_a_readq(stcb, control);
2132139bc87fSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
2133f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
2134f8829a4aSRandall Stewart 				if (oper) {
2135f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
2136f8829a4aSRandall Stewart 					uint32_t *ippp;
2137f8829a4aSRandall Stewart 
2138139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) =
2139f8829a4aSRandall Stewart 					    sizeof(struct sctp_paramhdr) +
2140f8829a4aSRandall Stewart 					    (3 * sizeof(uint32_t));
2141f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
2142f8829a4aSRandall Stewart 					ph->param_type =
2143f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2144139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
2145f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
2146a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_15);
2147f8829a4aSRandall Stewart 					ippp++;
2148f8829a4aSRandall Stewart 					*ippp = tsn;
2149f8829a4aSRandall Stewart 					ippp++;
2150f8829a4aSRandall Stewart 					*ippp = ((strmno << 16) | strmseq);
2151f8829a4aSRandall Stewart 				}
2152a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_15;
2153f8829a4aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb,
2154ceaad40aSRandall Stewart 				    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
2155f8829a4aSRandall Stewart 
2156f8829a4aSRandall Stewart 				*abort_flag = 1;
2157f8829a4aSRandall Stewart 				return (0);
2158f8829a4aSRandall Stewart 			} else {
2159f8829a4aSRandall Stewart 				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
2160f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
2161f8829a4aSRandall Stewart 					control->data = NULL;
21625bead436SRandall Stewart 					if (control->whoFrom) {
2163f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
21645bead436SRandall Stewart 						control->whoFrom = NULL;
21655bead436SRandall Stewart 					}
2166f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
2167f8829a4aSRandall Stewart 
2168139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
2169f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
2170f8829a4aSRandall Stewart 					if (oper) {
2171f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
2172f8829a4aSRandall Stewart 						uint32_t *ippp;
2173f8829a4aSRandall Stewart 
2174139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
2175f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
2176f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
2177f8829a4aSRandall Stewart 						ph = mtod(oper,
2178f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
2179f8829a4aSRandall Stewart 						ph->param_type =
2180f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2181f8829a4aSRandall Stewart 						ph->param_length =
2182139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
2183f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
2184a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_16);
2185f8829a4aSRandall Stewart 						ippp++;
2186f8829a4aSRandall Stewart 						*ippp = tsn;
2187f8829a4aSRandall Stewart 						ippp++;
2188f8829a4aSRandall Stewart 						*ippp = ((strmno << 16) | strmseq);
2189f8829a4aSRandall Stewart 					}
2190a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_16;
2191f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
2192ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
2193f8829a4aSRandall Stewart 
2194f8829a4aSRandall Stewart 					*abort_flag = 1;
2195f8829a4aSRandall Stewart 					return (0);
2196f8829a4aSRandall Stewart 				}
2197f8829a4aSRandall Stewart 			}
2198f8829a4aSRandall Stewart 		} else {
2199f8829a4aSRandall Stewart 			/* No PDAPI running */
2200f8829a4aSRandall Stewart 			if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
2201f8829a4aSRandall Stewart 				/*
2202f8829a4aSRandall Stewart 				 * Reassembly queue is NOT empty validate
2203f8829a4aSRandall Stewart 				 * that this tsn does not need to be in
2204f8829a4aSRandall Stewart 				 * reasembly queue. If it does then our peer
2205f8829a4aSRandall Stewart 				 * is broken or evil.
2206f8829a4aSRandall Stewart 				 */
2207f8829a4aSRandall Stewart 				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
2208f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
2209f8829a4aSRandall Stewart 					control->data = NULL;
22105bead436SRandall Stewart 					if (control->whoFrom) {
2211f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
22125bead436SRandall Stewart 						control->whoFrom = NULL;
22135bead436SRandall Stewart 					}
2214f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
2215139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
2216f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
2217f8829a4aSRandall Stewart 					if (oper) {
2218f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
2219f8829a4aSRandall Stewart 						uint32_t *ippp;
2220f8829a4aSRandall Stewart 
2221139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
2222f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
2223f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
2224f8829a4aSRandall Stewart 						ph = mtod(oper,
2225f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
2226f8829a4aSRandall Stewart 						ph->param_type =
2227f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2228f8829a4aSRandall Stewart 						ph->param_length =
2229139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
2230f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
2231a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_17);
2232f8829a4aSRandall Stewart 						ippp++;
2233f8829a4aSRandall Stewart 						*ippp = tsn;
2234f8829a4aSRandall Stewart 						ippp++;
2235f8829a4aSRandall Stewart 						*ippp = ((strmno << 16) | strmseq);
2236f8829a4aSRandall Stewart 					}
2237a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_17;
2238f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
2239ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
2240f8829a4aSRandall Stewart 
2241f8829a4aSRandall Stewart 					*abort_flag = 1;
2242f8829a4aSRandall Stewart 					return (0);
2243f8829a4aSRandall Stewart 				}
2244f8829a4aSRandall Stewart 			}
2245f8829a4aSRandall Stewart 		}
2246f8829a4aSRandall Stewart 		/* ok, if we reach here we have passed the sanity checks */
2247f42a358aSRandall Stewart 		if (chunk_flags & SCTP_DATA_UNORDERED) {
2248f8829a4aSRandall Stewart 			/* queue directly into socket buffer */
2249f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep, stcb,
2250f8829a4aSRandall Stewart 			    control,
2251ceaad40aSRandall Stewart 			    &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
2252830d754dSRandall Stewart 
2253830d754dSRandall Stewart 			/*
2254830d754dSRandall Stewart 			 * EY It is added to the read queue in prev if block
2255830d754dSRandall Stewart 			 * here I should check if this delivered tsn is
2256830d754dSRandall Stewart 			 * out_of_order, if yes then update the nr_map
2257830d754dSRandall Stewart 			 */
2258830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
2259830d754dSRandall Stewart 				/*
2260830d754dSRandall Stewart 				 * EY check if the mapping_array and
2261830d754dSRandall Stewart 				 * nr_mapping array are consistent
2262830d754dSRandall Stewart 				 */
2263830d754dSRandall Stewart 				if (asoc->mapping_array_base_tsn != asoc->nr_mapping_array_base_tsn)
2264830d754dSRandall Stewart 					/*
2265830d754dSRandall Stewart 					 * printf("EY-IN
2266830d754dSRandall Stewart 					 * sctp_process_a_data_chunk(6):
2267830d754dSRandall Stewart 					 * Something is wrong the map base
2268830d754dSRandall Stewart 					 * tsn" "\nEY-and nr_map base tsn
2269830d754dSRandall Stewart 					 * should be equal.");
2270830d754dSRandall Stewart 					 */
2271830d754dSRandall Stewart 					/*
2272830d754dSRandall Stewart 					 * EY - not %100 sure about the lock
2273830d754dSRandall Stewart 					 * thing, i think we don't need the
2274830d754dSRandall Stewart 					 * below,
2275830d754dSRandall Stewart 					 */
2276830d754dSRandall Stewart 					/* SCTP_TCB_LOCK_ASSERT(stcb); */
2277830d754dSRandall Stewart 				{
2278830d754dSRandall Stewart 					/*
2279830d754dSRandall Stewart 					 * printf("\nEY-Calculating an
2280830d754dSRandall Stewart 					 * nr_gap!!\nEY-mapping_array_size =
2281830d754dSRandall Stewart 					 * %d nr_mapping_array_size = %d"
2282830d754dSRandall Stewart 					 * "\nEY-mapping_array_base = %d
2283830d754dSRandall Stewart 					 * nr_mapping_array_base =
2284830d754dSRandall Stewart 					 * %d\nEY-highest_tsn_inside_map =
2285830d754dSRandall Stewart 					 * %d" "highest_tsn_inside_nr_map =
2286830d754dSRandall Stewart 					 * %d\nEY-TSN = %d nr_gap =
2287830d754dSRandall Stewart 					 * %d",asoc->mapping_array_size,
2288830d754dSRandall Stewart 					 * asoc->nr_mapping_array_size,
2289830d754dSRandall Stewart 					 * asoc->mapping_array_base_tsn,
2290830d754dSRandall Stewart 					 * asoc->nr_mapping_array_base_tsn,
2291830d754dSRandall Stewart 					 * asoc->highest_tsn_inside_map,
2292830d754dSRandall Stewart 					 * asoc->highest_tsn_inside_nr_map,ts
2293830d754dSRandall Stewart 					 * n,nr_gap);
2294830d754dSRandall Stewart 					 */
2295830d754dSRandall Stewart 				}
2296830d754dSRandall Stewart 				SCTP_TCB_LOCK_ASSERT(stcb);
2297830d754dSRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
2298d50c1d79SRandall Stewart 				SCTP_REVERSE_OUT_TSN_PRES(nr_gap, tsn, asoc);
2299830d754dSRandall Stewart 				if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
2300830d754dSRandall Stewart 					asoc->highest_tsn_inside_nr_map = tsn;
2301830d754dSRandall Stewart 			}
2302f8829a4aSRandall Stewart 		} else {
2303f8829a4aSRandall Stewart 			/*
2304f8829a4aSRandall Stewart 			 * Special check for when streams are resetting. We
2305f8829a4aSRandall Stewart 			 * could be more smart about this and check the
2306f8829a4aSRandall Stewart 			 * actual stream to see if it is not being reset..
2307f8829a4aSRandall Stewart 			 * that way we would not create a HOLB when amongst
2308f8829a4aSRandall Stewart 			 * streams being reset and those not being reset.
2309f8829a4aSRandall Stewart 			 *
2310f8829a4aSRandall Stewart 			 * We take complete messages that have a stream reset
2311f8829a4aSRandall Stewart 			 * intervening (aka the TSN is after where our
2312f8829a4aSRandall Stewart 			 * cum-ack needs to be) off and put them on a
2313f8829a4aSRandall Stewart 			 * pending_reply_queue. The reassembly ones we do
2314f8829a4aSRandall Stewart 			 * not have to worry about since they are all sorted
2315f8829a4aSRandall Stewart 			 * and proceessed by TSN order. It is only the
2316f8829a4aSRandall Stewart 			 * singletons I must worry about.
2317f8829a4aSRandall Stewart 			 */
2318f8829a4aSRandall Stewart 			if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2319d61a0ae0SRandall Stewart 			    ((compare_with_wrap(tsn, liste->tsn, MAX_TSN)))
2320f8829a4aSRandall Stewart 			    ) {
2321f8829a4aSRandall Stewart 				/*
2322f8829a4aSRandall Stewart 				 * yep its past where we need to reset... go
2323f8829a4aSRandall Stewart 				 * ahead and queue it.
2324f8829a4aSRandall Stewart 				 */
2325f8829a4aSRandall Stewart 				if (TAILQ_EMPTY(&asoc->pending_reply_queue)) {
2326f8829a4aSRandall Stewart 					/* first one on */
2327f8829a4aSRandall Stewart 					TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2328f8829a4aSRandall Stewart 				} else {
2329f8829a4aSRandall Stewart 					struct sctp_queued_to_read *ctlOn;
2330f8829a4aSRandall Stewart 					unsigned char inserted = 0;
2331f8829a4aSRandall Stewart 
2332f8829a4aSRandall Stewart 					ctlOn = TAILQ_FIRST(&asoc->pending_reply_queue);
2333f8829a4aSRandall Stewart 					while (ctlOn) {
2334f8829a4aSRandall Stewart 						if (compare_with_wrap(control->sinfo_tsn,
2335f8829a4aSRandall Stewart 						    ctlOn->sinfo_tsn, MAX_TSN)) {
2336f8829a4aSRandall Stewart 							ctlOn = TAILQ_NEXT(ctlOn, next);
2337f8829a4aSRandall Stewart 						} else {
2338f8829a4aSRandall Stewart 							/* found it */
2339f8829a4aSRandall Stewart 							TAILQ_INSERT_BEFORE(ctlOn, control, next);
2340f8829a4aSRandall Stewart 							inserted = 1;
2341f8829a4aSRandall Stewart 							break;
2342f8829a4aSRandall Stewart 						}
2343f8829a4aSRandall Stewart 					}
2344f8829a4aSRandall Stewart 					if (inserted == 0) {
2345f8829a4aSRandall Stewart 						/*
2346f8829a4aSRandall Stewart 						 * must be put at end, use
2347f8829a4aSRandall Stewart 						 * prevP (all setup from
2348f8829a4aSRandall Stewart 						 * loop) to setup nextP.
2349f8829a4aSRandall Stewart 						 */
2350f8829a4aSRandall Stewart 						TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2351f8829a4aSRandall Stewart 					}
2352f8829a4aSRandall Stewart 				}
2353f8829a4aSRandall Stewart 			} else {
2354f8829a4aSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, control, abort_flag);
2355f8829a4aSRandall Stewart 				if (*abort_flag) {
2356f8829a4aSRandall Stewart 					return (0);
2357f8829a4aSRandall Stewart 				}
2358f8829a4aSRandall Stewart 			}
2359f8829a4aSRandall Stewart 		}
2360f8829a4aSRandall Stewart 	} else {
2361f8829a4aSRandall Stewart 		/* Into the re-assembly queue */
2362f8829a4aSRandall Stewart 		sctp_queue_data_for_reasm(stcb, asoc, chk, abort_flag);
2363f8829a4aSRandall Stewart 		if (*abort_flag) {
2364a5d547adSRandall Stewart 			/*
2365a5d547adSRandall Stewart 			 * the assoc is now gone and chk was put onto the
2366a5d547adSRandall Stewart 			 * reasm queue, which has all been freed.
2367a5d547adSRandall Stewart 			 */
2368a5d547adSRandall Stewart 			*m = NULL;
2369f8829a4aSRandall Stewart 			return (0);
2370f8829a4aSRandall Stewart 		}
2371f8829a4aSRandall Stewart 	}
2372f8829a4aSRandall Stewart finish_express_del:
2373f8829a4aSRandall Stewart 	if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
2374f8829a4aSRandall Stewart 		/* we have a new high score */
2375f8829a4aSRandall Stewart 		asoc->highest_tsn_inside_map = tsn;
2376b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2377f8829a4aSRandall Stewart 			sctp_log_map(0, 2, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
237880fefe0aSRandall Stewart 		}
2379f8829a4aSRandall Stewart 	}
2380f8829a4aSRandall Stewart 	if (tsn == (asoc->cumulative_tsn + 1)) {
2381f8829a4aSRandall Stewart 		/* Update cum-ack */
2382f8829a4aSRandall Stewart 		asoc->cumulative_tsn = tsn;
2383f8829a4aSRandall Stewart 	}
2384f8829a4aSRandall Stewart 	if (last_chunk) {
2385f8829a4aSRandall Stewart 		*m = NULL;
2386f8829a4aSRandall Stewart 	}
2387f42a358aSRandall Stewart 	if (ordered) {
2388f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_inorderchunks);
2389f8829a4aSRandall Stewart 	} else {
2390f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_inunorderchunks);
2391f8829a4aSRandall Stewart 	}
2392f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvdata);
2393f8829a4aSRandall Stewart 	/* Set it present please */
2394b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
23956a91f103SRandall Stewart 		sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno, SCTP_STR_LOG_FROM_MARK_TSN);
239680fefe0aSRandall Stewart 	}
2397b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2398f8829a4aSRandall Stewart 		sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2399f8829a4aSRandall Stewart 		    asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE);
240080fefe0aSRandall Stewart 	}
2401207304d4SRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
2402f8829a4aSRandall Stewart 	SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
24038933fa13SRandall Stewart 
24048933fa13SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) &&
24058933fa13SRandall Stewart 	    asoc->peer_supports_nr_sack &&
24068933fa13SRandall Stewart 	    (SCTP_BASE_SYSCTL(sctp_do_drain) == 0)) {
24078933fa13SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
2408d50c1d79SRandall Stewart 		SCTP_REVERSE_OUT_TSN_PRES(nr_gap, tsn, asoc);
24098933fa13SRandall Stewart 		if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN)) {
24108933fa13SRandall Stewart 			asoc->highest_tsn_inside_nr_map = tsn;
24118933fa13SRandall Stewart 		}
24128933fa13SRandall Stewart 	}
241317205eccSRandall Stewart 	/* check the special flag for stream resets */
241417205eccSRandall Stewart 	if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2415d61a0ae0SRandall Stewart 	    ((compare_with_wrap(asoc->cumulative_tsn, liste->tsn, MAX_TSN)) ||
2416d61a0ae0SRandall Stewart 	    (asoc->cumulative_tsn == liste->tsn))
241717205eccSRandall Stewart 	    ) {
241817205eccSRandall Stewart 		/*
241917205eccSRandall Stewart 		 * we have finished working through the backlogged TSN's now
242017205eccSRandall Stewart 		 * time to reset streams. 1: call reset function. 2: free
242117205eccSRandall Stewart 		 * pending_reply space 3: distribute any chunks in
242217205eccSRandall Stewart 		 * pending_reply_queue.
242317205eccSRandall Stewart 		 */
242417205eccSRandall Stewart 		struct sctp_queued_to_read *ctl;
242517205eccSRandall Stewart 
242617205eccSRandall Stewart 		sctp_reset_in_stream(stcb, liste->number_entries, liste->req.list_of_streams);
242717205eccSRandall Stewart 		TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
2428207304d4SRandall Stewart 		SCTP_FREE(liste, SCTP_M_STRESET);
24293c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
243017205eccSRandall Stewart 		liste = TAILQ_FIRST(&asoc->resetHead);
243117205eccSRandall Stewart 		ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
243217205eccSRandall Stewart 		if (ctl && (liste == NULL)) {
243317205eccSRandall Stewart 			/* All can be removed */
243417205eccSRandall Stewart 			while (ctl) {
243517205eccSRandall Stewart 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
243617205eccSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
243717205eccSRandall Stewart 				if (*abort_flag) {
243817205eccSRandall Stewart 					return (0);
243917205eccSRandall Stewart 				}
244017205eccSRandall Stewart 				ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
244117205eccSRandall Stewart 			}
244217205eccSRandall Stewart 		} else if (ctl) {
244317205eccSRandall Stewart 			/* more than one in queue */
2444d61a0ae0SRandall Stewart 			while (!compare_with_wrap(ctl->sinfo_tsn, liste->tsn, MAX_TSN)) {
244517205eccSRandall Stewart 				/*
244617205eccSRandall Stewart 				 * if ctl->sinfo_tsn is <= liste->tsn we can
244717205eccSRandall Stewart 				 * process it which is the NOT of
244817205eccSRandall Stewart 				 * ctl->sinfo_tsn > liste->tsn
244917205eccSRandall Stewart 				 */
245017205eccSRandall Stewart 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
245117205eccSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
245217205eccSRandall Stewart 				if (*abort_flag) {
245317205eccSRandall Stewart 					return (0);
245417205eccSRandall Stewart 				}
245517205eccSRandall Stewart 				ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
245617205eccSRandall Stewart 			}
245717205eccSRandall Stewart 		}
245817205eccSRandall Stewart 		/*
245917205eccSRandall Stewart 		 * Now service re-assembly to pick up anything that has been
246017205eccSRandall Stewart 		 * held on reassembly queue?
246117205eccSRandall Stewart 		 */
246217205eccSRandall Stewart 		sctp_deliver_reasm_check(stcb, asoc);
246317205eccSRandall Stewart 		need_reasm_check = 0;
246417205eccSRandall Stewart 	}
2465139bc87fSRandall Stewart 	if (need_reasm_check) {
2466139bc87fSRandall Stewart 		/* Another one waits ? */
2467139bc87fSRandall Stewart 		sctp_deliver_reasm_check(stcb, asoc);
2468139bc87fSRandall Stewart 	}
2469f8829a4aSRandall Stewart 	return (1);
2470f8829a4aSRandall Stewart }
2471f8829a4aSRandall Stewart 
2472f8829a4aSRandall Stewart int8_t sctp_map_lookup_tab[256] = {
2473f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2474f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2475f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2476f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 4,
2477f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2478f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2479f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2480f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 5,
2481f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2482f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2483f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2484f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 4,
2485f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2486f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2487f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2488f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 6,
2489f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2490f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2491f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2492f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 4,
2493f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2494f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2495f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2496f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 5,
2497f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2498f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2499f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2500f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 4,
2501f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2502f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2503f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2504f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 7,
2505f8829a4aSRandall Stewart };
2506f8829a4aSRandall Stewart 
2507f8829a4aSRandall Stewart 
2508f8829a4aSRandall Stewart void
2509f8829a4aSRandall Stewart sctp_sack_check(struct sctp_tcb *stcb, int ok_to_sack, int was_a_gap, int *abort_flag)
2510f8829a4aSRandall Stewart {
2511f8829a4aSRandall Stewart 	/*
2512f8829a4aSRandall Stewart 	 * Now we also need to check the mapping array in a couple of ways.
2513f8829a4aSRandall Stewart 	 * 1) Did we move the cum-ack point?
2514f8829a4aSRandall Stewart 	 */
2515f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2516b3f1ea41SRandall Stewart 	int at;
2517c4739e2fSRandall Stewart 	int last_all_ones = 0;
2518f8829a4aSRandall Stewart 	int slide_from, slide_end, lgap, distance;
2519830d754dSRandall Stewart 
2520830d754dSRandall Stewart 	/* EY nr_mapping array variables */
25218933fa13SRandall Stewart 	/* int nr_at; */
25228933fa13SRandall Stewart 	/* int nr_last_all_ones = 0; */
25238933fa13SRandall Stewart 	/* int nr_slide_from, nr_slide_end, nr_lgap, nr_distance; */
2524830d754dSRandall Stewart 
2525f8829a4aSRandall Stewart 	uint32_t old_cumack, old_base, old_highest;
2526f8829a4aSRandall Stewart 	unsigned char aux_array[64];
2527f8829a4aSRandall Stewart 
2528830d754dSRandall Stewart 	/*
2529830d754dSRandall Stewart 	 * EY! Don't think this is required but I am immitating the code for
2530830d754dSRandall Stewart 	 * map just to make sure
2531830d754dSRandall Stewart 	 */
2532830d754dSRandall Stewart 	unsigned char nr_aux_array[64];
2533f8829a4aSRandall Stewart 
2534f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2535f8829a4aSRandall Stewart 	at = 0;
2536f8829a4aSRandall Stewart 
2537f8829a4aSRandall Stewart 	old_cumack = asoc->cumulative_tsn;
2538f8829a4aSRandall Stewart 	old_base = asoc->mapping_array_base_tsn;
2539f8829a4aSRandall Stewart 	old_highest = asoc->highest_tsn_inside_map;
2540f8829a4aSRandall Stewart 	if (asoc->mapping_array_size < 64)
2541f8829a4aSRandall Stewart 		memcpy(aux_array, asoc->mapping_array,
2542f8829a4aSRandall Stewart 		    asoc->mapping_array_size);
2543f8829a4aSRandall Stewart 	else
2544f8829a4aSRandall Stewart 		memcpy(aux_array, asoc->mapping_array, 64);
2545830d754dSRandall Stewart 	/* EY do the same for nr_mapping_array */
2546830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
2547f8829a4aSRandall Stewart 
2548830d754dSRandall Stewart 		if (asoc->nr_mapping_array_size != asoc->mapping_array_size) {
2549830d754dSRandall Stewart 			/*
2550830d754dSRandall Stewart 			 * printf("\nEY-IN sack_check method: \nEY-" "The
2551830d754dSRandall Stewart 			 * size of map and nr_map are inconsitent")
2552830d754dSRandall Stewart 			 */ ;
2553830d754dSRandall Stewart 		}
2554830d754dSRandall Stewart 		if (asoc->nr_mapping_array_base_tsn != asoc->mapping_array_base_tsn) {
2555830d754dSRandall Stewart 			/*
2556830d754dSRandall Stewart 			 * printf("\nEY-IN sack_check method VERY CRUCIAL
2557830d754dSRandall Stewart 			 * error: \nEY-" "The base tsns of map and nr_map
2558830d754dSRandall Stewart 			 * are inconsitent")
2559830d754dSRandall Stewart 			 */ ;
2560830d754dSRandall Stewart 		}
2561830d754dSRandall Stewart 		/* EY! just immitating the above code */
2562830d754dSRandall Stewart 		if (asoc->nr_mapping_array_size < 64)
2563830d754dSRandall Stewart 			memcpy(nr_aux_array, asoc->nr_mapping_array,
2564830d754dSRandall Stewart 			    asoc->nr_mapping_array_size);
2565830d754dSRandall Stewart 		else
2566830d754dSRandall Stewart 			memcpy(aux_array, asoc->nr_mapping_array, 64);
2567830d754dSRandall Stewart 	}
2568f8829a4aSRandall Stewart 	/*
2569f8829a4aSRandall Stewart 	 * We could probably improve this a small bit by calculating the
2570f8829a4aSRandall Stewart 	 * offset of the current cum-ack as the starting point.
2571f8829a4aSRandall Stewart 	 */
2572f8829a4aSRandall Stewart 	at = 0;
2573b3f1ea41SRandall Stewart 	for (slide_from = 0; slide_from < stcb->asoc.mapping_array_size; slide_from++) {
2574d06c82f1SRandall Stewart 
2575b3f1ea41SRandall Stewart 		if (asoc->mapping_array[slide_from] == 0xff) {
2576f8829a4aSRandall Stewart 			at += 8;
257793164cf9SRandall Stewart 			last_all_ones = 1;
2578f8829a4aSRandall Stewart 		} else {
2579f8829a4aSRandall Stewart 			/* there is a 0 bit */
2580b3f1ea41SRandall Stewart 			at += sctp_map_lookup_tab[asoc->mapping_array[slide_from]];
258193164cf9SRandall Stewart 			last_all_ones = 0;
2582f8829a4aSRandall Stewart 			break;
2583f8829a4aSRandall Stewart 		}
2584f8829a4aSRandall Stewart 	}
258593164cf9SRandall Stewart 	asoc->cumulative_tsn = asoc->mapping_array_base_tsn + (at - last_all_ones);
2586f8829a4aSRandall Stewart 	/* at is one off, since in the table a embedded -1 is present */
2587f8829a4aSRandall Stewart 	at++;
2588f8829a4aSRandall Stewart 
2589f8829a4aSRandall Stewart 	if (compare_with_wrap(asoc->cumulative_tsn,
2590f8829a4aSRandall Stewart 	    asoc->highest_tsn_inside_map,
2591f8829a4aSRandall Stewart 	    MAX_TSN)) {
2592a5d547adSRandall Stewart #ifdef INVARIANTS
2593ceaad40aSRandall Stewart 		panic("huh, cumack 0x%x greater than high-tsn 0x%x in map",
2594ceaad40aSRandall Stewart 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2595f8829a4aSRandall Stewart #else
2596ceaad40aSRandall Stewart 		SCTP_PRINTF("huh, cumack 0x%x greater than high-tsn 0x%x in map - should panic?\n",
2597ceaad40aSRandall Stewart 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2598b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2599b3f1ea41SRandall Stewart 			sctp_log_map(0, 6, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
2600b3f1ea41SRandall Stewart 		}
2601f8829a4aSRandall Stewart 		asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2602830d754dSRandall Stewart 		asoc->highest_tsn_inside_nr_map = asoc->cumulative_tsn;
2603f8829a4aSRandall Stewart #endif
2604f8829a4aSRandall Stewart 	}
2605c4739e2fSRandall Stewart 	if ((asoc->cumulative_tsn == asoc->highest_tsn_inside_map) && (at >= 8)) {
2606f8829a4aSRandall Stewart 		/* The complete array was completed by a single FR */
2607f8829a4aSRandall Stewart 		/* higest becomes the cum-ack */
2608f8829a4aSRandall Stewart 		int clr;
2609f8829a4aSRandall Stewart 
2610f8829a4aSRandall Stewart 		asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
2611f8829a4aSRandall Stewart 		/* clear the array */
2612f8829a4aSRandall Stewart 		clr = (at >> 3) + 1;
2613c4739e2fSRandall Stewart 		if (clr > asoc->mapping_array_size) {
2614f8829a4aSRandall Stewart 			clr = asoc->mapping_array_size;
2615f8829a4aSRandall Stewart 		}
2616f8829a4aSRandall Stewart 		memset(asoc->mapping_array, 0, clr);
2617f8829a4aSRandall Stewart 		/* base becomes one ahead of the cum-ack */
2618f8829a4aSRandall Stewart 		asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
2619830d754dSRandall Stewart 
2620830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
2621830d754dSRandall Stewart 
2622830d754dSRandall Stewart 			if (clr > asoc->nr_mapping_array_size)
2623830d754dSRandall Stewart 				clr = asoc->nr_mapping_array_size;
2624830d754dSRandall Stewart 
2625830d754dSRandall Stewart 			memset(asoc->nr_mapping_array, 0, clr);
2626830d754dSRandall Stewart 			/* base becomes one ahead of the cum-ack */
2627830d754dSRandall Stewart 			asoc->nr_mapping_array_base_tsn = asoc->cumulative_tsn + 1;
2628830d754dSRandall Stewart 			asoc->highest_tsn_inside_nr_map = asoc->cumulative_tsn;
2629830d754dSRandall Stewart 		}
2630b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2631f8829a4aSRandall Stewart 			sctp_log_map(old_base, old_cumack, old_highest,
2632f8829a4aSRandall Stewart 			    SCTP_MAP_PREPARE_SLIDE);
2633f8829a4aSRandall Stewart 			sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2634f8829a4aSRandall Stewart 			    asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_CLEARED);
263580fefe0aSRandall Stewart 		}
2636f8829a4aSRandall Stewart 	} else if (at >= 8) {
2637f8829a4aSRandall Stewart 		/* we can slide the mapping array down */
2638b3f1ea41SRandall Stewart 		/* slide_from holds where we hit the first NON 0xff byte */
2639b3f1ea41SRandall Stewart 
2640f8829a4aSRandall Stewart 		/*
2641f8829a4aSRandall Stewart 		 * now calculate the ceiling of the move using our highest
2642f8829a4aSRandall Stewart 		 * TSN value
2643f8829a4aSRandall Stewart 		 */
2644f8829a4aSRandall Stewart 		if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
2645f8829a4aSRandall Stewart 			lgap = asoc->highest_tsn_inside_map -
2646f8829a4aSRandall Stewart 			    asoc->mapping_array_base_tsn;
2647f8829a4aSRandall Stewart 		} else {
2648f8829a4aSRandall Stewart 			lgap = (MAX_TSN - asoc->mapping_array_base_tsn) +
2649f8829a4aSRandall Stewart 			    asoc->highest_tsn_inside_map + 1;
2650f8829a4aSRandall Stewart 		}
2651f8829a4aSRandall Stewart 		slide_end = lgap >> 3;
2652f8829a4aSRandall Stewart 		if (slide_end < slide_from) {
2653d55b0b1bSRandall Stewart #ifdef INVARIANTS
2654f8829a4aSRandall Stewart 			panic("impossible slide");
2655d55b0b1bSRandall Stewart #else
2656d55b0b1bSRandall Stewart 			printf("impossible slide?\n");
2657d55b0b1bSRandall Stewart 			return;
2658d55b0b1bSRandall Stewart #endif
2659f8829a4aSRandall Stewart 		}
2660b3f1ea41SRandall Stewart 		if (slide_end > asoc->mapping_array_size) {
2661b3f1ea41SRandall Stewart #ifdef INVARIANTS
2662b3f1ea41SRandall Stewart 			panic("would overrun buffer");
2663b3f1ea41SRandall Stewart #else
2664b3f1ea41SRandall Stewart 			printf("Gak, would have overrun map end:%d slide_end:%d\n",
2665b3f1ea41SRandall Stewart 			    asoc->mapping_array_size, slide_end);
2666b3f1ea41SRandall Stewart 			slide_end = asoc->mapping_array_size;
2667b3f1ea41SRandall Stewart #endif
2668b3f1ea41SRandall Stewart 		}
2669f8829a4aSRandall Stewart 		distance = (slide_end - slide_from) + 1;
2670b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2671f8829a4aSRandall Stewart 			sctp_log_map(old_base, old_cumack, old_highest,
2672f8829a4aSRandall Stewart 			    SCTP_MAP_PREPARE_SLIDE);
2673f8829a4aSRandall Stewart 			sctp_log_map((uint32_t) slide_from, (uint32_t) slide_end,
2674f8829a4aSRandall Stewart 			    (uint32_t) lgap, SCTP_MAP_SLIDE_FROM);
267580fefe0aSRandall Stewart 		}
2676f8829a4aSRandall Stewart 		if (distance + slide_from > asoc->mapping_array_size ||
2677f8829a4aSRandall Stewart 		    distance < 0) {
2678f8829a4aSRandall Stewart 			/*
2679f8829a4aSRandall Stewart 			 * Here we do NOT slide forward the array so that
2680f8829a4aSRandall Stewart 			 * hopefully when more data comes in to fill it up
2681f8829a4aSRandall Stewart 			 * we will be able to slide it forward. Really I
2682f8829a4aSRandall Stewart 			 * don't think this should happen :-0
2683f8829a4aSRandall Stewart 			 */
2684f8829a4aSRandall Stewart 
2685b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2686f8829a4aSRandall Stewart 				sctp_log_map((uint32_t) distance, (uint32_t) slide_from,
2687f8829a4aSRandall Stewart 				    (uint32_t) asoc->mapping_array_size,
2688f8829a4aSRandall Stewart 				    SCTP_MAP_SLIDE_NONE);
268980fefe0aSRandall Stewart 			}
2690f8829a4aSRandall Stewart 		} else {
2691f8829a4aSRandall Stewart 			int ii;
2692f8829a4aSRandall Stewart 
2693f8829a4aSRandall Stewart 			for (ii = 0; ii < distance; ii++) {
2694f8829a4aSRandall Stewart 				asoc->mapping_array[ii] =
2695f8829a4aSRandall Stewart 				    asoc->mapping_array[slide_from + ii];
2696f8829a4aSRandall Stewart 			}
2697f8829a4aSRandall Stewart 			for (ii = distance; ii <= slide_end; ii++) {
2698f8829a4aSRandall Stewart 				asoc->mapping_array[ii] = 0;
2699f8829a4aSRandall Stewart 			}
2700f8829a4aSRandall Stewart 			asoc->mapping_array_base_tsn += (slide_from << 3);
2701b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2702f8829a4aSRandall Stewart 				sctp_log_map(asoc->mapping_array_base_tsn,
2703f8829a4aSRandall Stewart 				    asoc->cumulative_tsn, asoc->highest_tsn_inside_map,
2704f8829a4aSRandall Stewart 				    SCTP_MAP_SLIDE_RESULT);
270580fefe0aSRandall Stewart 			}
2706f8829a4aSRandall Stewart 			/*
27078933fa13SRandall Stewart 			 * EY if doing nr_sacks then slide the
27088933fa13SRandall Stewart 			 * nr_mapping_array accordingly please
2709830d754dSRandall Stewart 			 */
2710830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
27118933fa13SRandall Stewart 				for (ii = 0; ii < distance; ii++) {
2712830d754dSRandall Stewart 					asoc->nr_mapping_array[ii] =
27138933fa13SRandall Stewart 					    asoc->nr_mapping_array[slide_from + ii];
2714830d754dSRandall Stewart 				}
27158933fa13SRandall Stewart 				for (ii = distance; ii <= slide_end; ii++) {
2716830d754dSRandall Stewart 					asoc->nr_mapping_array[ii] = 0;
2717830d754dSRandall Stewart 				}
27188933fa13SRandall Stewart 				asoc->nr_mapping_array_base_tsn += (slide_from << 3);
2719830d754dSRandall Stewart 			}
2720830d754dSRandall Stewart 		}
2721830d754dSRandall Stewart 	}
2722830d754dSRandall Stewart 	/*
2723f8829a4aSRandall Stewart 	 * Now we need to see if we need to queue a sack or just start the
2724f8829a4aSRandall Stewart 	 * timer (if allowed).
2725f8829a4aSRandall Stewart 	 */
2726f8829a4aSRandall Stewart 	if (ok_to_sack) {
2727f8829a4aSRandall Stewart 		if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2728f8829a4aSRandall Stewart 			/*
2729f8829a4aSRandall Stewart 			 * Ok special case, in SHUTDOWN-SENT case. here we
2730f8829a4aSRandall Stewart 			 * maker sure SACK timer is off and instead send a
2731f8829a4aSRandall Stewart 			 * SHUTDOWN and a SACK
2732f8829a4aSRandall Stewart 			 */
2733139bc87fSRandall Stewart 			if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2734f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
2735a5d547adSRandall Stewart 				    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_INDATA + SCTP_LOC_18);
2736f8829a4aSRandall Stewart 			}
2737f8829a4aSRandall Stewart 			sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
2738830d754dSRandall Stewart 			/*
2739830d754dSRandall Stewart 			 * EY if nr_sacks used then send an nr-sack , a sack
2740830d754dSRandall Stewart 			 * otherwise
2741830d754dSRandall Stewart 			 */
27428933fa13SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack)
2743830d754dSRandall Stewart 				sctp_send_nr_sack(stcb);
2744830d754dSRandall Stewart 			else
2745f8829a4aSRandall Stewart 				sctp_send_sack(stcb);
2746f8829a4aSRandall Stewart 		} else {
2747f8829a4aSRandall Stewart 			int is_a_gap;
2748f8829a4aSRandall Stewart 
2749f8829a4aSRandall Stewart 			/* is there a gap now ? */
2750f8829a4aSRandall Stewart 			is_a_gap = compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
2751f8829a4aSRandall Stewart 			    stcb->asoc.cumulative_tsn, MAX_TSN);
2752f8829a4aSRandall Stewart 
2753f8829a4aSRandall Stewart 			/*
2754f8829a4aSRandall Stewart 			 * CMT DAC algorithm: increase number of packets
2755f8829a4aSRandall Stewart 			 * received since last ack
2756f8829a4aSRandall Stewart 			 */
2757f8829a4aSRandall Stewart 			stcb->asoc.cmt_dac_pkts_rcvd++;
2758f8829a4aSRandall Stewart 
275942551e99SRandall Stewart 			if ((stcb->asoc.send_sack == 1) ||	/* We need to send a
276042551e99SRandall Stewart 								 * SACK */
2761f8829a4aSRandall Stewart 			    ((was_a_gap) && (is_a_gap == 0)) ||	/* was a gap, but no
2762f8829a4aSRandall Stewart 								 * longer is one */
2763f8829a4aSRandall Stewart 			    (stcb->asoc.numduptsns) ||	/* we have dup's */
2764f8829a4aSRandall Stewart 			    (is_a_gap) ||	/* is still a gap */
276542551e99SRandall Stewart 			    (stcb->asoc.delayed_ack == 0) ||	/* Delayed sack disabled */
276642551e99SRandall Stewart 			    (stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq)	/* hit limit of pkts */
2767f8829a4aSRandall Stewart 			    ) {
2768f8829a4aSRandall Stewart 
2769b3f1ea41SRandall Stewart 				if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off)) &&
2770b3f1ea41SRandall Stewart 				    (SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) &&
277142551e99SRandall Stewart 				    (stcb->asoc.send_sack == 0) &&
2772f8829a4aSRandall Stewart 				    (stcb->asoc.numduptsns == 0) &&
2773f8829a4aSRandall Stewart 				    (stcb->asoc.delayed_ack) &&
2774139bc87fSRandall Stewart 				    (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer))) {
2775f8829a4aSRandall Stewart 
2776f8829a4aSRandall Stewart 					/*
2777f8829a4aSRandall Stewart 					 * CMT DAC algorithm: With CMT,
2778f8829a4aSRandall Stewart 					 * delay acks even in the face of
2779f8829a4aSRandall Stewart 					 *
2780f8829a4aSRandall Stewart 					 * reordering. Therefore, if acks that
2781f8829a4aSRandall Stewart 					 * do not have to be sent because of
2782f8829a4aSRandall Stewart 					 * the above reasons, will be
2783f8829a4aSRandall Stewart 					 * delayed. That is, acks that would
2784f8829a4aSRandall Stewart 					 * have been sent due to gap reports
2785f8829a4aSRandall Stewart 					 * will be delayed with DAC. Start
2786f8829a4aSRandall Stewart 					 * the delayed ack timer.
2787f8829a4aSRandall Stewart 					 */
2788f8829a4aSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2789f8829a4aSRandall Stewart 					    stcb->sctp_ep, stcb, NULL);
2790f8829a4aSRandall Stewart 				} else {
2791f8829a4aSRandall Stewart 					/*
2792f8829a4aSRandall Stewart 					 * Ok we must build a SACK since the
2793f8829a4aSRandall Stewart 					 * timer is pending, we got our
2794f8829a4aSRandall Stewart 					 * first packet OR there are gaps or
2795f8829a4aSRandall Stewart 					 * duplicates.
2796f8829a4aSRandall Stewart 					 */
2797ad81507eSRandall Stewart 					(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2798830d754dSRandall Stewart 					/*
2799830d754dSRandall Stewart 					 * EY if nr_sacks used then send an
2800830d754dSRandall Stewart 					 * nr-sack , a sack otherwise
2801830d754dSRandall Stewart 					 */
2802830d754dSRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack)
2803830d754dSRandall Stewart 						sctp_send_nr_sack(stcb);
2804830d754dSRandall Stewart 					else
2805f8829a4aSRandall Stewart 						sctp_send_sack(stcb);
2806f8829a4aSRandall Stewart 				}
2807f8829a4aSRandall Stewart 			} else {
280842551e99SRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2809f8829a4aSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2810f8829a4aSRandall Stewart 					    stcb->sctp_ep, stcb, NULL);
2811f8829a4aSRandall Stewart 				}
2812f8829a4aSRandall Stewart 			}
2813f8829a4aSRandall Stewart 		}
2814f8829a4aSRandall Stewart 	}
281542551e99SRandall Stewart }
2816f8829a4aSRandall Stewart 
2817f8829a4aSRandall Stewart void
2818f8829a4aSRandall Stewart sctp_service_queues(struct sctp_tcb *stcb, struct sctp_association *asoc)
2819f8829a4aSRandall Stewart {
2820f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
2821f8829a4aSRandall Stewart 	uint32_t tsize;
2822f8829a4aSRandall Stewart 	uint16_t nxt_todel;
2823f8829a4aSRandall Stewart 
2824f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
2825f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
2826f8829a4aSRandall Stewart 	}
2827f8829a4aSRandall Stewart 	/* Can we proceed further, i.e. the PD-API is complete */
2828f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
2829f8829a4aSRandall Stewart 		/* no */
2830f8829a4aSRandall Stewart 		return;
2831f8829a4aSRandall Stewart 	}
2832f8829a4aSRandall Stewart 	/*
2833f8829a4aSRandall Stewart 	 * Now is there some other chunk I can deliver from the reassembly
2834f8829a4aSRandall Stewart 	 * queue.
2835f8829a4aSRandall Stewart 	 */
2836139bc87fSRandall Stewart doit_again:
2837f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
2838f8829a4aSRandall Stewart 	if (chk == NULL) {
2839f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = 0;
2840f8829a4aSRandall Stewart 		asoc->cnt_on_reasm_queue = 0;
2841f8829a4aSRandall Stewart 		return;
2842f8829a4aSRandall Stewart 	}
2843f8829a4aSRandall Stewart 	nxt_todel = asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
2844f8829a4aSRandall Stewart 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
2845f8829a4aSRandall Stewart 	    ((nxt_todel == chk->rec.data.stream_seq) ||
2846f8829a4aSRandall Stewart 	    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
2847f8829a4aSRandall Stewart 		/*
2848f8829a4aSRandall Stewart 		 * Yep the first one is here. We setup to start reception,
2849f8829a4aSRandall Stewart 		 * by backing down the TSN just in case we can't deliver.
2850f8829a4aSRandall Stewart 		 */
2851f8829a4aSRandall Stewart 
2852f8829a4aSRandall Stewart 		/*
2853f8829a4aSRandall Stewart 		 * Before we start though either all of the message should
2854f8829a4aSRandall Stewart 		 * be here or 1/4 the socket buffer max or nothing on the
2855f8829a4aSRandall Stewart 		 * delivery queue and something can be delivered.
2856f8829a4aSRandall Stewart 		 */
2857f8829a4aSRandall Stewart 		if ((sctp_is_all_msg_on_reasm(asoc, &tsize) ||
2858c4739e2fSRandall Stewart 		    (tsize >= stcb->sctp_ep->partial_delivery_point))) {
2859f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 1;
2860f8829a4aSRandall Stewart 			asoc->tsn_last_delivered = chk->rec.data.TSN_seq - 1;
2861f8829a4aSRandall Stewart 			asoc->str_of_pdapi = chk->rec.data.stream_number;
2862f8829a4aSRandall Stewart 			asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
2863f8829a4aSRandall Stewart 			asoc->pdapi_ppid = chk->rec.data.payloadtype;
2864f8829a4aSRandall Stewart 			asoc->fragment_flags = chk->rec.data.rcv_flags;
2865f8829a4aSRandall Stewart 			sctp_service_reassembly(stcb, asoc);
2866139bc87fSRandall Stewart 			if (asoc->fragmented_delivery_inprogress == 0) {
2867139bc87fSRandall Stewart 				goto doit_again;
2868139bc87fSRandall Stewart 			}
2869f8829a4aSRandall Stewart 		}
2870f8829a4aSRandall Stewart 	}
2871f8829a4aSRandall Stewart }
2872f8829a4aSRandall Stewart 
2873f8829a4aSRandall Stewart int
2874f8829a4aSRandall Stewart sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length,
2875f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2876f8829a4aSRandall Stewart     struct sctp_nets *net, uint32_t * high_tsn)
2877f8829a4aSRandall Stewart {
2878f8829a4aSRandall Stewart 	struct sctp_data_chunk *ch, chunk_buf;
2879f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2880f8829a4aSRandall Stewart 	int num_chunks = 0;	/* number of control chunks processed */
2881f8829a4aSRandall Stewart 	int stop_proc = 0;
2882f8829a4aSRandall Stewart 	int chk_length, break_flag, last_chunk;
2883f8829a4aSRandall Stewart 	int abort_flag = 0, was_a_gap = 0;
2884f8829a4aSRandall Stewart 	struct mbuf *m;
2885f8829a4aSRandall Stewart 
2886f8829a4aSRandall Stewart 	/* set the rwnd */
2887f8829a4aSRandall Stewart 	sctp_set_rwnd(stcb, &stcb->asoc);
2888f8829a4aSRandall Stewart 
2889f8829a4aSRandall Stewart 	m = *mm;
2890f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
2891f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2892f8829a4aSRandall Stewart 	if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
2893f8829a4aSRandall Stewart 	    stcb->asoc.cumulative_tsn, MAX_TSN)) {
2894f8829a4aSRandall Stewart 		/* there was a gap before this data was processed */
2895f8829a4aSRandall Stewart 		was_a_gap = 1;
2896f8829a4aSRandall Stewart 	}
2897f8829a4aSRandall Stewart 	/*
2898f8829a4aSRandall Stewart 	 * setup where we got the last DATA packet from for any SACK that
2899f8829a4aSRandall Stewart 	 * may need to go out. Don't bump the net. This is done ONLY when a
2900f8829a4aSRandall Stewart 	 * chunk is assigned.
2901f8829a4aSRandall Stewart 	 */
2902f8829a4aSRandall Stewart 	asoc->last_data_chunk_from = net;
2903f8829a4aSRandall Stewart 
2904d06c82f1SRandall Stewart 	/*-
2905f8829a4aSRandall Stewart 	 * Now before we proceed we must figure out if this is a wasted
2906f8829a4aSRandall Stewart 	 * cluster... i.e. it is a small packet sent in and yet the driver
2907f8829a4aSRandall Stewart 	 * underneath allocated a full cluster for it. If so we must copy it
2908f8829a4aSRandall Stewart 	 * to a smaller mbuf and free up the cluster mbuf. This will help
2909d06c82f1SRandall Stewart 	 * with cluster starvation. Note for __Panda__ we don't do this
2910d06c82f1SRandall Stewart 	 * since it has clusters all the way down to 64 bytes.
2911f8829a4aSRandall Stewart 	 */
291244b7479bSRandall Stewart 	if (SCTP_BUF_LEN(m) < (long)MLEN && SCTP_BUF_NEXT(m) == NULL) {
2913f8829a4aSRandall Stewart 		/* we only handle mbufs that are singletons.. not chains */
2914139bc87fSRandall Stewart 		m = sctp_get_mbuf_for_msg(SCTP_BUF_LEN(m), 0, M_DONTWAIT, 1, MT_DATA);
2915f8829a4aSRandall Stewart 		if (m) {
2916f8829a4aSRandall Stewart 			/* ok lets see if we can copy the data up */
2917f8829a4aSRandall Stewart 			caddr_t *from, *to;
2918f8829a4aSRandall Stewart 
2919f8829a4aSRandall Stewart 			/* get the pointers and copy */
2920f8829a4aSRandall Stewart 			to = mtod(m, caddr_t *);
2921f8829a4aSRandall Stewart 			from = mtod((*mm), caddr_t *);
2922139bc87fSRandall Stewart 			memcpy(to, from, SCTP_BUF_LEN((*mm)));
2923f8829a4aSRandall Stewart 			/* copy the length and free up the old */
2924139bc87fSRandall Stewart 			SCTP_BUF_LEN(m) = SCTP_BUF_LEN((*mm));
2925f8829a4aSRandall Stewart 			sctp_m_freem(*mm);
2926f8829a4aSRandall Stewart 			/* sucess, back copy */
2927f8829a4aSRandall Stewart 			*mm = m;
2928f8829a4aSRandall Stewart 		} else {
2929f8829a4aSRandall Stewart 			/* We are in trouble in the mbuf world .. yikes */
2930f8829a4aSRandall Stewart 			m = *mm;
2931f8829a4aSRandall Stewart 		}
2932f8829a4aSRandall Stewart 	}
2933f8829a4aSRandall Stewart 	/* get pointer to the first chunk header */
2934f8829a4aSRandall Stewart 	ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2935f8829a4aSRandall Stewart 	    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2936f8829a4aSRandall Stewart 	if (ch == NULL) {
2937f8829a4aSRandall Stewart 		return (1);
2938f8829a4aSRandall Stewart 	}
2939f8829a4aSRandall Stewart 	/*
2940f8829a4aSRandall Stewart 	 * process all DATA chunks...
2941f8829a4aSRandall Stewart 	 */
2942f8829a4aSRandall Stewart 	*high_tsn = asoc->cumulative_tsn;
2943f8829a4aSRandall Stewart 	break_flag = 0;
294442551e99SRandall Stewart 	asoc->data_pkts_seen++;
2945f8829a4aSRandall Stewart 	while (stop_proc == 0) {
2946f8829a4aSRandall Stewart 		/* validate chunk length */
2947f8829a4aSRandall Stewart 		chk_length = ntohs(ch->ch.chunk_length);
2948f8829a4aSRandall Stewart 		if (length - *offset < chk_length) {
2949f8829a4aSRandall Stewart 			/* all done, mutulated chunk */
2950f8829a4aSRandall Stewart 			stop_proc = 1;
2951f8829a4aSRandall Stewart 			break;
2952f8829a4aSRandall Stewart 		}
2953f8829a4aSRandall Stewart 		if (ch->ch.chunk_type == SCTP_DATA) {
2954f8829a4aSRandall Stewart 			if ((size_t)chk_length < sizeof(struct sctp_data_chunk) + 1) {
2955f8829a4aSRandall Stewart 				/*
2956f8829a4aSRandall Stewart 				 * Need to send an abort since we had a
2957f8829a4aSRandall Stewart 				 * invalid data chunk.
2958f8829a4aSRandall Stewart 				 */
2959f8829a4aSRandall Stewart 				struct mbuf *op_err;
2960f8829a4aSRandall Stewart 
2961139bc87fSRandall Stewart 				op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 2 * sizeof(uint32_t)),
2962f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
2963f8829a4aSRandall Stewart 
2964f8829a4aSRandall Stewart 				if (op_err) {
2965f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
2966f8829a4aSRandall Stewart 					uint32_t *ippp;
2967f8829a4aSRandall Stewart 
2968139bc87fSRandall Stewart 					SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr) +
2969f8829a4aSRandall Stewart 					    (2 * sizeof(uint32_t));
2970f8829a4aSRandall Stewart 					ph = mtod(op_err, struct sctp_paramhdr *);
2971f8829a4aSRandall Stewart 					ph->param_type =
2972f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2973139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(op_err));
2974f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
2975a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_19);
2976f8829a4aSRandall Stewart 					ippp++;
2977f8829a4aSRandall Stewart 					*ippp = asoc->cumulative_tsn;
2978f8829a4aSRandall Stewart 
2979f8829a4aSRandall Stewart 				}
2980a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_19;
2981f8829a4aSRandall Stewart 				sctp_abort_association(inp, stcb, m, iphlen, sh,
2982c54a18d2SRandall Stewart 				    op_err, 0, net->port);
2983f8829a4aSRandall Stewart 				return (2);
2984f8829a4aSRandall Stewart 			}
2985f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
2986f8829a4aSRandall Stewart 			sctp_audit_log(0xB1, 0);
2987f8829a4aSRandall Stewart #endif
2988f8829a4aSRandall Stewart 			if (SCTP_SIZE32(chk_length) == (length - *offset)) {
2989f8829a4aSRandall Stewart 				last_chunk = 1;
2990f8829a4aSRandall Stewart 			} else {
2991f8829a4aSRandall Stewart 				last_chunk = 0;
2992f8829a4aSRandall Stewart 			}
2993f8829a4aSRandall Stewart 			if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset, ch,
2994f8829a4aSRandall Stewart 			    chk_length, net, high_tsn, &abort_flag, &break_flag,
2995f8829a4aSRandall Stewart 			    last_chunk)) {
2996f8829a4aSRandall Stewart 				num_chunks++;
2997f8829a4aSRandall Stewart 			}
2998f8829a4aSRandall Stewart 			if (abort_flag)
2999f8829a4aSRandall Stewart 				return (2);
3000f8829a4aSRandall Stewart 
3001f8829a4aSRandall Stewart 			if (break_flag) {
3002f8829a4aSRandall Stewart 				/*
3003f8829a4aSRandall Stewart 				 * Set because of out of rwnd space and no
3004f8829a4aSRandall Stewart 				 * drop rep space left.
3005f8829a4aSRandall Stewart 				 */
3006f8829a4aSRandall Stewart 				stop_proc = 1;
3007f8829a4aSRandall Stewart 				break;
3008f8829a4aSRandall Stewart 			}
3009f8829a4aSRandall Stewart 		} else {
3010f8829a4aSRandall Stewart 			/* not a data chunk in the data region */
3011f8829a4aSRandall Stewart 			switch (ch->ch.chunk_type) {
3012f8829a4aSRandall Stewart 			case SCTP_INITIATION:
3013f8829a4aSRandall Stewart 			case SCTP_INITIATION_ACK:
3014f8829a4aSRandall Stewart 			case SCTP_SELECTIVE_ACK:
3015830d754dSRandall Stewart 			case SCTP_NR_SELECTIVE_ACK:	/* EY */
3016f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_REQUEST:
3017f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_ACK:
3018f8829a4aSRandall Stewart 			case SCTP_ABORT_ASSOCIATION:
3019f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN:
3020f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_ACK:
3021f8829a4aSRandall Stewart 			case SCTP_OPERATION_ERROR:
3022f8829a4aSRandall Stewart 			case SCTP_COOKIE_ECHO:
3023f8829a4aSRandall Stewart 			case SCTP_COOKIE_ACK:
3024f8829a4aSRandall Stewart 			case SCTP_ECN_ECHO:
3025f8829a4aSRandall Stewart 			case SCTP_ECN_CWR:
3026f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_COMPLETE:
3027f8829a4aSRandall Stewart 			case SCTP_AUTHENTICATION:
3028f8829a4aSRandall Stewart 			case SCTP_ASCONF_ACK:
3029f8829a4aSRandall Stewart 			case SCTP_PACKET_DROPPED:
3030f8829a4aSRandall Stewart 			case SCTP_STREAM_RESET:
3031f8829a4aSRandall Stewart 			case SCTP_FORWARD_CUM_TSN:
3032f8829a4aSRandall Stewart 			case SCTP_ASCONF:
3033f8829a4aSRandall Stewart 				/*
3034f8829a4aSRandall Stewart 				 * Now, what do we do with KNOWN chunks that
3035f8829a4aSRandall Stewart 				 * are NOT in the right place?
3036f8829a4aSRandall Stewart 				 *
3037f8829a4aSRandall Stewart 				 * For now, I do nothing but ignore them. We
3038f8829a4aSRandall Stewart 				 * may later want to add sysctl stuff to
3039f8829a4aSRandall Stewart 				 * switch out and do either an ABORT() or
3040f8829a4aSRandall Stewart 				 * possibly process them.
3041f8829a4aSRandall Stewart 				 */
3042b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_strict_data_order)) {
3043f8829a4aSRandall Stewart 					struct mbuf *op_err;
3044f8829a4aSRandall Stewart 
3045f8829a4aSRandall Stewart 					op_err = sctp_generate_invmanparam(SCTP_CAUSE_PROTOCOL_VIOLATION);
3046c54a18d2SRandall Stewart 					sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 0, net->port);
3047f8829a4aSRandall Stewart 					return (2);
3048f8829a4aSRandall Stewart 				}
3049f8829a4aSRandall Stewart 				break;
3050f8829a4aSRandall Stewart 			default:
3051f8829a4aSRandall Stewart 				/* unknown chunk type, use bit rules */
3052f8829a4aSRandall Stewart 				if (ch->ch.chunk_type & 0x40) {
3053f8829a4aSRandall Stewart 					/* Add a error report to the queue */
3054d61a0ae0SRandall Stewart 					struct mbuf *merr;
3055f8829a4aSRandall Stewart 					struct sctp_paramhdr *phd;
3056f8829a4aSRandall Stewart 
3057d61a0ae0SRandall Stewart 					merr = sctp_get_mbuf_for_msg(sizeof(*phd), 0, M_DONTWAIT, 1, MT_DATA);
3058d61a0ae0SRandall Stewart 					if (merr) {
3059d61a0ae0SRandall Stewart 						phd = mtod(merr, struct sctp_paramhdr *);
3060f8829a4aSRandall Stewart 						/*
3061f8829a4aSRandall Stewart 						 * We cheat and use param
3062f8829a4aSRandall Stewart 						 * type since we did not
3063f8829a4aSRandall Stewart 						 * bother to define a error
3064f8829a4aSRandall Stewart 						 * cause struct. They are
3065f8829a4aSRandall Stewart 						 * the same basic format
3066f8829a4aSRandall Stewart 						 * with different names.
3067f8829a4aSRandall Stewart 						 */
3068f8829a4aSRandall Stewart 						phd->param_type =
3069f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_UNRECOG_CHUNK);
3070f8829a4aSRandall Stewart 						phd->param_length =
3071f8829a4aSRandall Stewart 						    htons(chk_length + sizeof(*phd));
3072d61a0ae0SRandall Stewart 						SCTP_BUF_LEN(merr) = sizeof(*phd);
3073d61a0ae0SRandall Stewart 						SCTP_BUF_NEXT(merr) = SCTP_M_COPYM(m, *offset,
3074f8829a4aSRandall Stewart 						    SCTP_SIZE32(chk_length),
3075f8829a4aSRandall Stewart 						    M_DONTWAIT);
3076d61a0ae0SRandall Stewart 						if (SCTP_BUF_NEXT(merr)) {
3077d61a0ae0SRandall Stewart 							sctp_queue_op_err(stcb, merr);
3078f8829a4aSRandall Stewart 						} else {
3079d61a0ae0SRandall Stewart 							sctp_m_freem(merr);
3080f8829a4aSRandall Stewart 						}
3081f8829a4aSRandall Stewart 					}
3082f8829a4aSRandall Stewart 				}
3083f8829a4aSRandall Stewart 				if ((ch->ch.chunk_type & 0x80) == 0) {
3084f8829a4aSRandall Stewart 					/* discard the rest of this packet */
3085f8829a4aSRandall Stewart 					stop_proc = 1;
3086f8829a4aSRandall Stewart 				}	/* else skip this bad chunk and
3087f8829a4aSRandall Stewart 					 * continue... */
3088f8829a4aSRandall Stewart 				break;
3089f8829a4aSRandall Stewart 			};	/* switch of chunk type */
3090f8829a4aSRandall Stewart 		}
3091f8829a4aSRandall Stewart 		*offset += SCTP_SIZE32(chk_length);
3092f8829a4aSRandall Stewart 		if ((*offset >= length) || stop_proc) {
3093f8829a4aSRandall Stewart 			/* no more data left in the mbuf chain */
3094f8829a4aSRandall Stewart 			stop_proc = 1;
3095f8829a4aSRandall Stewart 			continue;
3096f8829a4aSRandall Stewart 		}
3097f8829a4aSRandall Stewart 		ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
3098f8829a4aSRandall Stewart 		    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
3099f8829a4aSRandall Stewart 		if (ch == NULL) {
3100f8829a4aSRandall Stewart 			*offset = length;
3101f8829a4aSRandall Stewart 			stop_proc = 1;
3102f8829a4aSRandall Stewart 			break;
3103f8829a4aSRandall Stewart 
3104f8829a4aSRandall Stewart 		}
3105f8829a4aSRandall Stewart 	}			/* while */
3106f8829a4aSRandall Stewart 	if (break_flag) {
3107f8829a4aSRandall Stewart 		/*
3108f8829a4aSRandall Stewart 		 * we need to report rwnd overrun drops.
3109f8829a4aSRandall Stewart 		 */
3110f8829a4aSRandall Stewart 		sctp_send_packet_dropped(stcb, net, *mm, iphlen, 0);
3111f8829a4aSRandall Stewart 	}
3112f8829a4aSRandall Stewart 	if (num_chunks) {
3113f8829a4aSRandall Stewart 		/*
3114ceaad40aSRandall Stewart 		 * Did we get data, if so update the time for auto-close and
3115f8829a4aSRandall Stewart 		 * give peer credit for being alive.
3116f8829a4aSRandall Stewart 		 */
3117f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvpktwithdata);
3118b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
3119c4739e2fSRandall Stewart 			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
3120c4739e2fSRandall Stewart 			    stcb->asoc.overall_error_count,
3121c4739e2fSRandall Stewart 			    0,
3122c4739e2fSRandall Stewart 			    SCTP_FROM_SCTP_INDATA,
3123c4739e2fSRandall Stewart 			    __LINE__);
3124c4739e2fSRandall Stewart 		}
3125f8829a4aSRandall Stewart 		stcb->asoc.overall_error_count = 0;
31266e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
3127f8829a4aSRandall Stewart 	}
3128f8829a4aSRandall Stewart 	/* now service all of the reassm queue if needed */
3129f8829a4aSRandall Stewart 	if (!(TAILQ_EMPTY(&asoc->reasmqueue)))
3130f8829a4aSRandall Stewart 		sctp_service_queues(stcb, asoc);
3131f8829a4aSRandall Stewart 
3132f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
313342551e99SRandall Stewart 		/* Assure that we ack right away */
313442551e99SRandall Stewart 		stcb->asoc.send_sack = 1;
3135f8829a4aSRandall Stewart 	}
3136f8829a4aSRandall Stewart 	/* Start a sack timer or QUEUE a SACK for sending */
3137f8829a4aSRandall Stewart 	if ((stcb->asoc.cumulative_tsn == stcb->asoc.highest_tsn_inside_map) &&
313842551e99SRandall Stewart 	    (stcb->asoc.mapping_array[0] != 0xff)) {
313942551e99SRandall Stewart 		if ((stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq) ||
314042551e99SRandall Stewart 		    (stcb->asoc.delayed_ack == 0) ||
3141b201f536SRandall Stewart 		    (stcb->asoc.numduptsns) ||
314242551e99SRandall Stewart 		    (stcb->asoc.send_sack == 1)) {
3143139bc87fSRandall Stewart 			if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
3144ad81507eSRandall Stewart 				(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
314542551e99SRandall Stewart 			}
3146830d754dSRandall Stewart 			/*
3147830d754dSRandall Stewart 			 * EY if nr_sacks used then send an nr-sack , a sack
3148830d754dSRandall Stewart 			 * otherwise
3149830d754dSRandall Stewart 			 */
3150830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack)
3151830d754dSRandall Stewart 				sctp_send_nr_sack(stcb);
3152830d754dSRandall Stewart 			else
3153f8829a4aSRandall Stewart 				sctp_send_sack(stcb);
3154f8829a4aSRandall Stewart 		} else {
315542551e99SRandall Stewart 			if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
3156f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
3157f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, NULL);
3158f8829a4aSRandall Stewart 			}
3159f8829a4aSRandall Stewart 		}
3160f8829a4aSRandall Stewart 	} else {
3161f8829a4aSRandall Stewart 		sctp_sack_check(stcb, 1, was_a_gap, &abort_flag);
3162f8829a4aSRandall Stewart 	}
3163f8829a4aSRandall Stewart 	if (abort_flag)
3164f8829a4aSRandall Stewart 		return (2);
3165f8829a4aSRandall Stewart 
3166f8829a4aSRandall Stewart 	return (0);
3167f8829a4aSRandall Stewart }
3168f8829a4aSRandall Stewart 
3169f8829a4aSRandall Stewart static void
3170458303daSRandall Stewart sctp_handle_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
3171f8829a4aSRandall Stewart     struct sctp_sack_chunk *ch, uint32_t last_tsn, uint32_t * biggest_tsn_acked,
3172139bc87fSRandall Stewart     uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
3173139bc87fSRandall Stewart     int num_seg, int *ecn_seg_sums)
3174f8829a4aSRandall Stewart {
3175f8829a4aSRandall Stewart 	/************************************************/
3176f8829a4aSRandall Stewart 	/* process fragments and update sendqueue        */
3177f8829a4aSRandall Stewart 	/************************************************/
3178f8829a4aSRandall Stewart 	struct sctp_sack *sack;
3179458303daSRandall Stewart 	struct sctp_gap_ack_block *frag, block;
3180f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
3181a1e13272SRandall Stewart 	int i, j;
3182a1e13272SRandall Stewart 	unsigned int theTSN;
3183f8829a4aSRandall Stewart 	int num_frs = 0;
3184f8829a4aSRandall Stewart 
3185f8829a4aSRandall Stewart 	uint16_t frag_strt, frag_end, primary_flag_set;
3186f8829a4aSRandall Stewart 	u_long last_frag_high;
3187f8829a4aSRandall Stewart 
3188f8829a4aSRandall Stewart 	/*
3189f8829a4aSRandall Stewart 	 * @@@ JRI : TODO: This flag is not used anywhere .. remove?
3190f8829a4aSRandall Stewart 	 */
3191f8829a4aSRandall Stewart 	if (asoc->primary_destination->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
3192f8829a4aSRandall Stewart 		primary_flag_set = 1;
3193f8829a4aSRandall Stewart 	} else {
3194f8829a4aSRandall Stewart 		primary_flag_set = 0;
3195f8829a4aSRandall Stewart 	}
3196f8829a4aSRandall Stewart 	sack = &ch->sack;
3197458303daSRandall Stewart 
3198458303daSRandall Stewart 	frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
3199458303daSRandall Stewart 	    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
3200458303daSRandall Stewart 	*offset += sizeof(block);
3201458303daSRandall Stewart 	if (frag == NULL) {
3202458303daSRandall Stewart 		return;
3203458303daSRandall Stewart 	}
3204f8829a4aSRandall Stewart 	tp1 = NULL;
3205f8829a4aSRandall Stewart 	last_frag_high = 0;
3206f8829a4aSRandall Stewart 	for (i = 0; i < num_seg; i++) {
3207f8829a4aSRandall Stewart 		frag_strt = ntohs(frag->start);
3208f8829a4aSRandall Stewart 		frag_end = ntohs(frag->end);
3209830d754dSRandall Stewart 		/* some sanity checks on the fragment offsets */
3210f8829a4aSRandall Stewart 		if (frag_strt > frag_end) {
3211f8829a4aSRandall Stewart 			/* this one is malformed, skip */
3212f8829a4aSRandall Stewart 			frag++;
3213f8829a4aSRandall Stewart 			continue;
3214f8829a4aSRandall Stewart 		}
3215f8829a4aSRandall Stewart 		if (compare_with_wrap((frag_end + last_tsn), *biggest_tsn_acked,
3216f8829a4aSRandall Stewart 		    MAX_TSN))
3217f8829a4aSRandall Stewart 			*biggest_tsn_acked = frag_end + last_tsn;
3218f8829a4aSRandall Stewart 
3219f8829a4aSRandall Stewart 		/* mark acked dgs and find out the highestTSN being acked */
3220f8829a4aSRandall Stewart 		if (tp1 == NULL) {
3221f8829a4aSRandall Stewart 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
3222f8829a4aSRandall Stewart 
3223f8829a4aSRandall Stewart 			/* save the locations of the last frags */
3224f8829a4aSRandall Stewart 			last_frag_high = frag_end + last_tsn;
3225f8829a4aSRandall Stewart 		} else {
3226f8829a4aSRandall Stewart 			/*
3227f8829a4aSRandall Stewart 			 * now lets see if we need to reset the queue due to
3228f8829a4aSRandall Stewart 			 * a out-of-order SACK fragment
3229f8829a4aSRandall Stewart 			 */
3230f8829a4aSRandall Stewart 			if (compare_with_wrap(frag_strt + last_tsn,
3231f8829a4aSRandall Stewart 			    last_frag_high, MAX_TSN)) {
3232f8829a4aSRandall Stewart 				/*
3233f8829a4aSRandall Stewart 				 * if the new frag starts after the last TSN
3234f8829a4aSRandall Stewart 				 * frag covered, we are ok and this one is
3235f8829a4aSRandall Stewart 				 * beyond the last one
3236f8829a4aSRandall Stewart 				 */
3237f8829a4aSRandall Stewart 				;
3238f8829a4aSRandall Stewart 			} else {
3239f8829a4aSRandall Stewart 				/*
3240f8829a4aSRandall Stewart 				 * ok, they have reset us, so we need to
3241f8829a4aSRandall Stewart 				 * reset the queue this will cause extra
3242f8829a4aSRandall Stewart 				 * hunting but hey, they chose the
3243f8829a4aSRandall Stewart 				 * performance hit when they failed to order
3244830d754dSRandall Stewart 				 * their gaps
3245f8829a4aSRandall Stewart 				 */
3246f8829a4aSRandall Stewart 				tp1 = TAILQ_FIRST(&asoc->sent_queue);
3247f8829a4aSRandall Stewart 			}
3248f8829a4aSRandall Stewart 			last_frag_high = frag_end + last_tsn;
3249f8829a4aSRandall Stewart 		}
3250a1e13272SRandall Stewart 		for (j = frag_strt; j <= frag_end; j++) {
3251a1e13272SRandall Stewart 			theTSN = j + last_tsn;
3252f8829a4aSRandall Stewart 			while (tp1) {
3253f8829a4aSRandall Stewart 				if (tp1->rec.data.doing_fast_retransmit)
3254f8829a4aSRandall Stewart 					num_frs++;
3255f8829a4aSRandall Stewart 
3256f8829a4aSRandall Stewart 				/*
3257f8829a4aSRandall Stewart 				 * CMT: CUCv2 algorithm. For each TSN being
3258f8829a4aSRandall Stewart 				 * processed from the sent queue, track the
3259f8829a4aSRandall Stewart 				 * next expected pseudo-cumack, or
3260f8829a4aSRandall Stewart 				 * rtx_pseudo_cumack, if required. Separate
3261f8829a4aSRandall Stewart 				 * cumack trackers for first transmissions,
3262f8829a4aSRandall Stewart 				 * and retransmissions.
3263f8829a4aSRandall Stewart 				 */
3264f8829a4aSRandall Stewart 				if ((tp1->whoTo->find_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
3265f8829a4aSRandall Stewart 				    (tp1->snd_count == 1)) {
3266f8829a4aSRandall Stewart 					tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
3267f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 0;
3268f8829a4aSRandall Stewart 				}
3269f8829a4aSRandall Stewart 				if ((tp1->whoTo->find_rtx_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
3270f8829a4aSRandall Stewart 				    (tp1->snd_count > 1)) {
3271f8829a4aSRandall Stewart 					tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
3272f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 0;
3273f8829a4aSRandall Stewart 				}
3274a1e13272SRandall Stewart 				if (tp1->rec.data.TSN_seq == theTSN) {
3275f8829a4aSRandall Stewart 					if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
3276f8829a4aSRandall Stewart 						/*
3277f8829a4aSRandall Stewart 						 * must be held until
3278f8829a4aSRandall Stewart 						 * cum-ack passes
3279f8829a4aSRandall Stewart 						 */
3280f8829a4aSRandall Stewart 						/*
3281f8829a4aSRandall Stewart 						 * ECN Nonce: Add the nonce
3282f8829a4aSRandall Stewart 						 * value to the sender's
3283f8829a4aSRandall Stewart 						 * nonce sum
3284f8829a4aSRandall Stewart 						 */
3285c105859eSRandall Stewart 						if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3286c105859eSRandall Stewart 							/*-
3287c105859eSRandall Stewart 							 * If it is less than RESEND, it is
3288c105859eSRandall Stewart 							 * now no-longer in flight.
3289c105859eSRandall Stewart 							 * Higher values may already be set
3290c105859eSRandall Stewart 							 * via previous Gap Ack Blocks...
3291c105859eSRandall Stewart 							 * i.e. ACKED or RESEND.
3292f8829a4aSRandall Stewart 							 */
3293f8829a4aSRandall Stewart 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
3294f8829a4aSRandall Stewart 							    *biggest_newly_acked_tsn, MAX_TSN)) {
3295f8829a4aSRandall Stewart 								*biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
3296f8829a4aSRandall Stewart 							}
3297f8829a4aSRandall Stewart 							/*
3298f8829a4aSRandall Stewart 							 * CMT: SFR algo
3299f8829a4aSRandall Stewart 							 * (and HTNA) - set
3300f8829a4aSRandall Stewart 							 * saw_newack to 1
3301f8829a4aSRandall Stewart 							 * for dest being
3302f8829a4aSRandall Stewart 							 * newly acked.
3303f8829a4aSRandall Stewart 							 * update
3304f8829a4aSRandall Stewart 							 * this_sack_highest_
3305f8829a4aSRandall Stewart 							 * newack if
3306f8829a4aSRandall Stewart 							 * appropriate.
3307f8829a4aSRandall Stewart 							 */
3308f8829a4aSRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0)
3309f8829a4aSRandall Stewart 								tp1->whoTo->saw_newack = 1;
3310f8829a4aSRandall Stewart 
3311f8829a4aSRandall Stewart 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
3312f8829a4aSRandall Stewart 							    tp1->whoTo->this_sack_highest_newack,
3313f8829a4aSRandall Stewart 							    MAX_TSN)) {
3314f8829a4aSRandall Stewart 								tp1->whoTo->this_sack_highest_newack =
3315f8829a4aSRandall Stewart 								    tp1->rec.data.TSN_seq;
3316f8829a4aSRandall Stewart 							}
3317f8829a4aSRandall Stewart 							/*
3318f8829a4aSRandall Stewart 							 * CMT DAC algo:
3319f8829a4aSRandall Stewart 							 * also update
3320f8829a4aSRandall Stewart 							 * this_sack_lowest_n
3321f8829a4aSRandall Stewart 							 * ewack
3322f8829a4aSRandall Stewart 							 */
3323f8829a4aSRandall Stewart 							if (*this_sack_lowest_newack == 0) {
3324b3f1ea41SRandall Stewart 								if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3325f8829a4aSRandall Stewart 									sctp_log_sack(*this_sack_lowest_newack,
3326d0cf96b4SMax Laier 									    last_tsn,
3327f8829a4aSRandall Stewart 									    tp1->rec.data.TSN_seq,
3328f8829a4aSRandall Stewart 									    0,
3329f8829a4aSRandall Stewart 									    0,
3330f8829a4aSRandall Stewart 									    SCTP_LOG_TSN_ACKED);
333180fefe0aSRandall Stewart 								}
3332f8829a4aSRandall Stewart 								*this_sack_lowest_newack = tp1->rec.data.TSN_seq;
3333f8829a4aSRandall Stewart 							}
3334f8829a4aSRandall Stewart 							/*
3335f8829a4aSRandall Stewart 							 * CMT: CUCv2
3336f8829a4aSRandall Stewart 							 * algorithm. If
3337f8829a4aSRandall Stewart 							 * (rtx-)pseudo-cumac
3338f8829a4aSRandall Stewart 							 * k for corresp
3339f8829a4aSRandall Stewart 							 * dest is being
3340f8829a4aSRandall Stewart 							 * acked, then we
3341f8829a4aSRandall Stewart 							 * have a new
3342f8829a4aSRandall Stewart 							 * (rtx-)pseudo-cumac
3343f8829a4aSRandall Stewart 							 * k. Set
3344f8829a4aSRandall Stewart 							 * new_(rtx_)pseudo_c
3345f8829a4aSRandall Stewart 							 * umack to TRUE so
3346f8829a4aSRandall Stewart 							 * that the cwnd for
3347f8829a4aSRandall Stewart 							 * this dest can be
3348f8829a4aSRandall Stewart 							 * updated. Also
3349f8829a4aSRandall Stewart 							 * trigger search
3350f8829a4aSRandall Stewart 							 * for the next
3351f8829a4aSRandall Stewart 							 * expected
3352f8829a4aSRandall Stewart 							 * (rtx-)pseudo-cumac
3353f8829a4aSRandall Stewart 							 * k. Separate
3354f8829a4aSRandall Stewart 							 * pseudo_cumack
3355f8829a4aSRandall Stewart 							 * trackers for
3356f8829a4aSRandall Stewart 							 * first
3357f8829a4aSRandall Stewart 							 * transmissions and
3358f8829a4aSRandall Stewart 							 * retransmissions.
3359f8829a4aSRandall Stewart 							 */
3360f8829a4aSRandall Stewart 							if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
3361f8829a4aSRandall Stewart 								if (tp1->rec.data.chunk_was_revoked == 0) {
3362f8829a4aSRandall Stewart 									tp1->whoTo->new_pseudo_cumack = 1;
3363f8829a4aSRandall Stewart 								}
3364f8829a4aSRandall Stewart 								tp1->whoTo->find_pseudo_cumack = 1;
3365f8829a4aSRandall Stewart 							}
3366b3f1ea41SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
3367f8829a4aSRandall Stewart 								sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
336880fefe0aSRandall Stewart 							}
3369f8829a4aSRandall Stewart 							if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
3370f8829a4aSRandall Stewart 								if (tp1->rec.data.chunk_was_revoked == 0) {
3371f8829a4aSRandall Stewart 									tp1->whoTo->new_pseudo_cumack = 1;
3372f8829a4aSRandall Stewart 								}
3373f8829a4aSRandall Stewart 								tp1->whoTo->find_rtx_pseudo_cumack = 1;
3374f8829a4aSRandall Stewart 							}
3375b3f1ea41SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3376f8829a4aSRandall Stewart 								sctp_log_sack(*biggest_newly_acked_tsn,
3377f8829a4aSRandall Stewart 								    last_tsn,
3378f8829a4aSRandall Stewart 								    tp1->rec.data.TSN_seq,
3379f8829a4aSRandall Stewart 								    frag_strt,
3380f8829a4aSRandall Stewart 								    frag_end,
3381f8829a4aSRandall Stewart 								    SCTP_LOG_TSN_ACKED);
338280fefe0aSRandall Stewart 							}
3383b3f1ea41SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3384c105859eSRandall Stewart 								sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
3385a5d547adSRandall Stewart 								    tp1->whoTo->flight_size,
3386a5d547adSRandall Stewart 								    tp1->book_size,
3387c105859eSRandall Stewart 								    (uintptr_t) tp1->whoTo,
3388a5d547adSRandall Stewart 								    tp1->rec.data.TSN_seq);
338980fefe0aSRandall Stewart 							}
3390c105859eSRandall Stewart 							sctp_flight_size_decrease(tp1);
3391c105859eSRandall Stewart 							sctp_total_flight_decrease(stcb, tp1);
3392f8829a4aSRandall Stewart 
3393f8829a4aSRandall Stewart 							tp1->whoTo->net_ack += tp1->send_size;
3394f8829a4aSRandall Stewart 							if (tp1->snd_count < 2) {
3395f8829a4aSRandall Stewart 								/*
3396a5d547adSRandall Stewart 								 * True
3397a5d547adSRandall Stewart 								 * non-retran
3398a5d547adSRandall Stewart 								 * smited
3399a5d547adSRandall Stewart 								 * chunk */
3400f8829a4aSRandall Stewart 								tp1->whoTo->net_ack2 += tp1->send_size;
3401f8829a4aSRandall Stewart 
3402f8829a4aSRandall Stewart 								/*
3403a5d547adSRandall Stewart 								 * update RTO
3404a5d547adSRandall Stewart 								 * too ? */
3405f8829a4aSRandall Stewart 								if (tp1->do_rtt) {
3406f8829a4aSRandall Stewart 									tp1->whoTo->RTO =
3407f8829a4aSRandall Stewart 									    sctp_calculate_rto(stcb,
3408f8829a4aSRandall Stewart 									    asoc,
3409f8829a4aSRandall Stewart 									    tp1->whoTo,
341018e198d3SRandall Stewart 									    &tp1->sent_rcv_time,
341118e198d3SRandall Stewart 									    sctp_align_safe_nocopy);
3412f8829a4aSRandall Stewart 									tp1->do_rtt = 0;
3413f8829a4aSRandall Stewart 								}
3414f8829a4aSRandall Stewart 							}
3415f8829a4aSRandall Stewart 						}
3416c105859eSRandall Stewart 						if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
3417c105859eSRandall Stewart 							(*ecn_seg_sums) += tp1->rec.data.ect_nonce;
3418c105859eSRandall Stewart 							(*ecn_seg_sums) &= SCTP_SACK_NONCE_SUM;
3419c105859eSRandall Stewart 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
3420f8829a4aSRandall Stewart 							    asoc->this_sack_highest_gap,
3421f8829a4aSRandall Stewart 							    MAX_TSN)) {
3422f8829a4aSRandall Stewart 								asoc->this_sack_highest_gap =
3423f8829a4aSRandall Stewart 								    tp1->rec.data.TSN_seq;
3424f8829a4aSRandall Stewart 							}
3425f8829a4aSRandall Stewart 							if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3426f8829a4aSRandall Stewart 								sctp_ucount_decr(asoc->sent_queue_retran_cnt);
3427f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
3428f8829a4aSRandall Stewart 								sctp_audit_log(0xB2,
3429f8829a4aSRandall Stewart 								    (asoc->sent_queue_retran_cnt & 0x000000ff));
3430f8829a4aSRandall Stewart #endif
3431f8829a4aSRandall Stewart 							}
3432c105859eSRandall Stewart 						}
3433c105859eSRandall Stewart 						/*
3434c105859eSRandall Stewart 						 * All chunks NOT UNSENT
3435c105859eSRandall Stewart 						 * fall through here and are
34368933fa13SRandall Stewart 						 * marked (leave PR-SCTP
34378933fa13SRandall Stewart 						 * ones that are to skip
34388933fa13SRandall Stewart 						 * alone though)
3439c105859eSRandall Stewart 						 */
34408933fa13SRandall Stewart 						if (tp1->sent != SCTP_FORWARD_TSN_SKIP)
3441f8829a4aSRandall Stewart 							tp1->sent = SCTP_DATAGRAM_MARKED;
34428933fa13SRandall Stewart 
344342551e99SRandall Stewart 						if (tp1->rec.data.chunk_was_revoked) {
344442551e99SRandall Stewart 							/* deflate the cwnd */
344542551e99SRandall Stewart 							tp1->whoTo->cwnd -= tp1->book_size;
344642551e99SRandall Stewart 							tp1->rec.data.chunk_was_revoked = 0;
344742551e99SRandall Stewart 						}
3448f8829a4aSRandall Stewart 					}
3449f8829a4aSRandall Stewart 					break;
3450a1e13272SRandall Stewart 				}	/* if (tp1->TSN_seq == theTSN) */
3451a1e13272SRandall Stewart 				if (compare_with_wrap(tp1->rec.data.TSN_seq, theTSN,
3452f8829a4aSRandall Stewart 				    MAX_TSN))
3453f8829a4aSRandall Stewart 					break;
3454f8829a4aSRandall Stewart 
3455f8829a4aSRandall Stewart 				tp1 = TAILQ_NEXT(tp1, sctp_next);
3456f8829a4aSRandall Stewart 			}	/* end while (tp1) */
3457f8829a4aSRandall Stewart 		}		/* end for (j = fragStart */
3458458303daSRandall Stewart 		frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
3459458303daSRandall Stewart 		    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
3460458303daSRandall Stewart 		*offset += sizeof(block);
3461458303daSRandall Stewart 		if (frag == NULL) {
3462458303daSRandall Stewart 			break;
3463458303daSRandall Stewart 		}
3464f8829a4aSRandall Stewart 	}
3465b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
346680fefe0aSRandall Stewart 		if (num_frs)
346780fefe0aSRandall Stewart 			sctp_log_fr(*biggest_tsn_acked,
346880fefe0aSRandall Stewart 			    *biggest_newly_acked_tsn,
346980fefe0aSRandall Stewart 			    last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
347080fefe0aSRandall Stewart 	}
3471f8829a4aSRandall Stewart }
3472f8829a4aSRandall Stewart 
3473f8829a4aSRandall Stewart static void
3474c105859eSRandall Stewart sctp_check_for_revoked(struct sctp_tcb *stcb,
3475c105859eSRandall Stewart     struct sctp_association *asoc, uint32_t cumack,
3476f8829a4aSRandall Stewart     u_long biggest_tsn_acked)
3477f8829a4aSRandall Stewart {
3478f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
3479f8829a4aSRandall Stewart 	int tot_revoked = 0;
3480f8829a4aSRandall Stewart 
3481f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
3482f8829a4aSRandall Stewart 	while (tp1) {
3483f8829a4aSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq, cumack,
3484f8829a4aSRandall Stewart 		    MAX_TSN)) {
3485f8829a4aSRandall Stewart 			/*
3486f8829a4aSRandall Stewart 			 * ok this guy is either ACK or MARKED. If it is
3487f8829a4aSRandall Stewart 			 * ACKED it has been previously acked but not this
3488f8829a4aSRandall Stewart 			 * time i.e. revoked.  If it is MARKED it was ACK'ed
3489f8829a4aSRandall Stewart 			 * again.
3490f8829a4aSRandall Stewart 			 */
3491d06c82f1SRandall Stewart 			if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
3492d06c82f1SRandall Stewart 			    MAX_TSN))
3493d06c82f1SRandall Stewart 				break;
3494d06c82f1SRandall Stewart 
3495d06c82f1SRandall Stewart 
3496f8829a4aSRandall Stewart 			if (tp1->sent == SCTP_DATAGRAM_ACKED) {
3497f8829a4aSRandall Stewart 				/* it has been revoked */
3498f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_SENT;
3499f8829a4aSRandall Stewart 				tp1->rec.data.chunk_was_revoked = 1;
3500a5d547adSRandall Stewart 				/*
350142551e99SRandall Stewart 				 * We must add this stuff back in to assure
350242551e99SRandall Stewart 				 * timers and such get started.
3503a5d547adSRandall Stewart 				 */
3504b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3505c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
3506c105859eSRandall Stewart 					    tp1->whoTo->flight_size,
3507c105859eSRandall Stewart 					    tp1->book_size,
3508c105859eSRandall Stewart 					    (uintptr_t) tp1->whoTo,
3509c105859eSRandall Stewart 					    tp1->rec.data.TSN_seq);
351080fefe0aSRandall Stewart 				}
3511c105859eSRandall Stewart 				sctp_flight_size_increase(tp1);
3512c105859eSRandall Stewart 				sctp_total_flight_increase(stcb, tp1);
351342551e99SRandall Stewart 				/*
351442551e99SRandall Stewart 				 * We inflate the cwnd to compensate for our
351542551e99SRandall Stewart 				 * artificial inflation of the flight_size.
351642551e99SRandall Stewart 				 */
351742551e99SRandall Stewart 				tp1->whoTo->cwnd += tp1->book_size;
3518f8829a4aSRandall Stewart 				tot_revoked++;
3519b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3520f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
3521f8829a4aSRandall Stewart 					    cumack,
3522f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
3523f8829a4aSRandall Stewart 					    0,
3524f8829a4aSRandall Stewart 					    0,
3525f8829a4aSRandall Stewart 					    SCTP_LOG_TSN_REVOKED);
352680fefe0aSRandall Stewart 				}
3527f8829a4aSRandall Stewart 			} else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
3528f8829a4aSRandall Stewart 				/* it has been re-acked in this SACK */
3529f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
3530f8829a4aSRandall Stewart 			}
3531f8829a4aSRandall Stewart 		}
3532f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT)
3533f8829a4aSRandall Stewart 			break;
3534f8829a4aSRandall Stewart 		tp1 = TAILQ_NEXT(tp1, sctp_next);
3535f8829a4aSRandall Stewart 	}
3536f8829a4aSRandall Stewart 	if (tot_revoked > 0) {
3537f8829a4aSRandall Stewart 		/*
3538f8829a4aSRandall Stewart 		 * Setup the ecn nonce re-sync point. We do this since once
3539f8829a4aSRandall Stewart 		 * data is revoked we begin to retransmit things, which do
3540f8829a4aSRandall Stewart 		 * NOT have the ECN bits set. This means we are now out of
3541f8829a4aSRandall Stewart 		 * sync and must wait until we get back in sync with the
3542f8829a4aSRandall Stewart 		 * peer to check ECN bits.
3543f8829a4aSRandall Stewart 		 */
3544f8829a4aSRandall Stewart 		tp1 = TAILQ_FIRST(&asoc->send_queue);
3545f8829a4aSRandall Stewart 		if (tp1 == NULL) {
3546f8829a4aSRandall Stewart 			asoc->nonce_resync_tsn = asoc->sending_seq;
3547f8829a4aSRandall Stewart 		} else {
3548f8829a4aSRandall Stewart 			asoc->nonce_resync_tsn = tp1->rec.data.TSN_seq;
3549f8829a4aSRandall Stewart 		}
3550f8829a4aSRandall Stewart 		asoc->nonce_wait_for_ecne = 0;
3551f8829a4aSRandall Stewart 		asoc->nonce_sum_check = 0;
3552f8829a4aSRandall Stewart 	}
3553f8829a4aSRandall Stewart }
3554f8829a4aSRandall Stewart 
3555830d754dSRandall Stewart 
3556f8829a4aSRandall Stewart static void
3557f8829a4aSRandall Stewart sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
3558f8829a4aSRandall Stewart     u_long biggest_tsn_acked, u_long biggest_tsn_newly_acked, u_long this_sack_lowest_newack, int accum_moved)
3559f8829a4aSRandall Stewart {
3560f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
3561f8829a4aSRandall Stewart 	int strike_flag = 0;
3562f8829a4aSRandall Stewart 	struct timeval now;
3563f8829a4aSRandall Stewart 	int tot_retrans = 0;
3564f8829a4aSRandall Stewart 	uint32_t sending_seq;
3565f8829a4aSRandall Stewart 	struct sctp_nets *net;
3566f8829a4aSRandall Stewart 	int num_dests_sacked = 0;
3567f8829a4aSRandall Stewart 
3568f8829a4aSRandall Stewart 	/*
3569f8829a4aSRandall Stewart 	 * select the sending_seq, this is either the next thing ready to be
3570f8829a4aSRandall Stewart 	 * sent but not transmitted, OR, the next seq we assign.
3571f8829a4aSRandall Stewart 	 */
3572f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
3573f8829a4aSRandall Stewart 	if (tp1 == NULL) {
3574f8829a4aSRandall Stewart 		sending_seq = asoc->sending_seq;
3575f8829a4aSRandall Stewart 	} else {
3576f8829a4aSRandall Stewart 		sending_seq = tp1->rec.data.TSN_seq;
3577f8829a4aSRandall Stewart 	}
3578f8829a4aSRandall Stewart 
3579f8829a4aSRandall Stewart 	/* CMT DAC algo: finding out if SACK is a mixed SACK */
3580b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3581f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3582f8829a4aSRandall Stewart 			if (net->saw_newack)
3583f8829a4aSRandall Stewart 				num_dests_sacked++;
3584f8829a4aSRandall Stewart 		}
3585f8829a4aSRandall Stewart 	}
3586f8829a4aSRandall Stewart 	if (stcb->asoc.peer_supports_prsctp) {
35876e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
3588f8829a4aSRandall Stewart 	}
3589f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
3590f8829a4aSRandall Stewart 	while (tp1) {
3591f8829a4aSRandall Stewart 		strike_flag = 0;
3592f8829a4aSRandall Stewart 		if (tp1->no_fr_allowed) {
3593f8829a4aSRandall Stewart 			/* this one had a timeout or something */
3594f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3595f8829a4aSRandall Stewart 			continue;
3596f8829a4aSRandall Stewart 		}
3597b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3598f8829a4aSRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND)
3599f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3600f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3601f8829a4aSRandall Stewart 				    tp1->sent,
3602f8829a4aSRandall Stewart 				    SCTP_FR_LOG_CHECK_STRIKE);
360380fefe0aSRandall Stewart 		}
3604f8829a4aSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
3605f8829a4aSRandall Stewart 		    MAX_TSN) ||
3606f8829a4aSRandall Stewart 		    tp1->sent == SCTP_DATAGRAM_UNSENT) {
3607f8829a4aSRandall Stewart 			/* done */
3608f8829a4aSRandall Stewart 			break;
3609f8829a4aSRandall Stewart 		}
3610f8829a4aSRandall Stewart 		if (stcb->asoc.peer_supports_prsctp) {
3611f8829a4aSRandall Stewart 			if ((PR_SCTP_TTL_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3612f8829a4aSRandall Stewart 				/* Is it expired? */
36135e54f665SRandall Stewart 				if (
3614fc14de76SRandall Stewart 				/*
3615fc14de76SRandall Stewart 				 * TODO sctp_constants.h needs alternative
3616fc14de76SRandall Stewart 				 * time macros when _KERNEL is undefined.
3617fc14de76SRandall Stewart 				 */
36185e54f665SRandall Stewart 				    (timevalcmp(&now, &tp1->rec.data.timetodrop, >))
36195e54f665SRandall Stewart 				    ) {
3620f8829a4aSRandall Stewart 					/* Yes so drop it */
3621f8829a4aSRandall Stewart 					if (tp1->data != NULL) {
3622ad81507eSRandall Stewart 						(void)sctp_release_pr_sctp_chunk(stcb, tp1,
3623f8829a4aSRandall Stewart 						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
36240c0982b8SRandall Stewart 						    SCTP_SO_NOT_LOCKED);
3625f8829a4aSRandall Stewart 					}
3626f8829a4aSRandall Stewart 					tp1 = TAILQ_NEXT(tp1, sctp_next);
3627f8829a4aSRandall Stewart 					continue;
3628f8829a4aSRandall Stewart 				}
3629f8829a4aSRandall Stewart 			}
3630f8829a4aSRandall Stewart 		}
3631f8829a4aSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq,
3632f8829a4aSRandall Stewart 		    asoc->this_sack_highest_gap, MAX_TSN)) {
3633f8829a4aSRandall Stewart 			/* we are beyond the tsn in the sack  */
3634f8829a4aSRandall Stewart 			break;
3635f8829a4aSRandall Stewart 		}
3636f8829a4aSRandall Stewart 		if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
3637f8829a4aSRandall Stewart 			/* either a RESEND, ACKED, or MARKED */
3638f8829a4aSRandall Stewart 			/* skip */
3639f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3640f8829a4aSRandall Stewart 			continue;
3641f8829a4aSRandall Stewart 		}
3642f8829a4aSRandall Stewart 		/*
3643f8829a4aSRandall Stewart 		 * CMT : SFR algo (covers part of DAC and HTNA as well)
3644f8829a4aSRandall Stewart 		 */
3645ad81507eSRandall Stewart 		if (tp1->whoTo && tp1->whoTo->saw_newack == 0) {
3646f8829a4aSRandall Stewart 			/*
3647f8829a4aSRandall Stewart 			 * No new acks were receieved for data sent to this
3648f8829a4aSRandall Stewart 			 * dest. Therefore, according to the SFR algo for
3649f8829a4aSRandall Stewart 			 * CMT, no data sent to this dest can be marked for
3650c105859eSRandall Stewart 			 * FR using this SACK.
3651f8829a4aSRandall Stewart 			 */
3652f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3653f8829a4aSRandall Stewart 			continue;
3654ad81507eSRandall Stewart 		} else if (tp1->whoTo && compare_with_wrap(tp1->rec.data.TSN_seq,
3655f8829a4aSRandall Stewart 		    tp1->whoTo->this_sack_highest_newack, MAX_TSN)) {
3656f8829a4aSRandall Stewart 			/*
3657f8829a4aSRandall Stewart 			 * CMT: New acks were receieved for data sent to
3658f8829a4aSRandall Stewart 			 * this dest. But no new acks were seen for data
3659f8829a4aSRandall Stewart 			 * sent after tp1. Therefore, according to the SFR
3660f8829a4aSRandall Stewart 			 * algo for CMT, tp1 cannot be marked for FR using
3661f8829a4aSRandall Stewart 			 * this SACK. This step covers part of the DAC algo
3662f8829a4aSRandall Stewart 			 * and the HTNA algo as well.
3663f8829a4aSRandall Stewart 			 */
3664f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3665f8829a4aSRandall Stewart 			continue;
3666f8829a4aSRandall Stewart 		}
3667f8829a4aSRandall Stewart 		/*
3668f8829a4aSRandall Stewart 		 * Here we check to see if we were have already done a FR
3669f8829a4aSRandall Stewart 		 * and if so we see if the biggest TSN we saw in the sack is
3670f8829a4aSRandall Stewart 		 * smaller than the recovery point. If so we don't strike
3671f8829a4aSRandall Stewart 		 * the tsn... otherwise we CAN strike the TSN.
3672f8829a4aSRandall Stewart 		 */
3673f8829a4aSRandall Stewart 		/*
367442551e99SRandall Stewart 		 * @@@ JRI: Check for CMT if (accum_moved &&
367542551e99SRandall Stewart 		 * asoc->fast_retran_loss_recovery && (sctp_cmt_on_off ==
367642551e99SRandall Stewart 		 * 0)) {
3677f8829a4aSRandall Stewart 		 */
367842551e99SRandall Stewart 		if (accum_moved && asoc->fast_retran_loss_recovery) {
3679f8829a4aSRandall Stewart 			/*
3680f8829a4aSRandall Stewart 			 * Strike the TSN if in fast-recovery and cum-ack
3681f8829a4aSRandall Stewart 			 * moved.
3682f8829a4aSRandall Stewart 			 */
3683b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3684f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3685f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3686f8829a4aSRandall Stewart 				    tp1->sent,
3687f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
368880fefe0aSRandall Stewart 			}
36895e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3690f8829a4aSRandall Stewart 				tp1->sent++;
36915e54f665SRandall Stewart 			}
3692b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3693f8829a4aSRandall Stewart 				/*
3694f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
3695f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
3696f8829a4aSRandall Stewart 				 * because it would have been set to the
3697f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
3698f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
3699f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
3700c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
3701c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3702c105859eSRandall Stewart 				 * indicates that two packets have been
3703c105859eSRandall Stewart 				 * received after this missing TSN.
37045e54f665SRandall Stewart 				 */
37055e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3706f8829a4aSRandall Stewart 				    compare_with_wrap(this_sack_lowest_newack, tp1->rec.data.TSN_seq, MAX_TSN)) {
3707b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3708f8829a4aSRandall Stewart 						sctp_log_fr(16 + num_dests_sacked,
3709f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3710f8829a4aSRandall Stewart 						    tp1->sent,
3711f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
371280fefe0aSRandall Stewart 					}
3713f8829a4aSRandall Stewart 					tp1->sent++;
3714f8829a4aSRandall Stewart 				}
3715f8829a4aSRandall Stewart 			}
3716b3f1ea41SRandall Stewart 		} else if ((tp1->rec.data.doing_fast_retransmit) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
3717f8829a4aSRandall Stewart 			/*
3718f8829a4aSRandall Stewart 			 * For those that have done a FR we must take
3719f8829a4aSRandall Stewart 			 * special consideration if we strike. I.e the
3720f8829a4aSRandall Stewart 			 * biggest_newly_acked must be higher than the
3721f8829a4aSRandall Stewart 			 * sending_seq at the time we did the FR.
3722f8829a4aSRandall Stewart 			 */
37235e54f665SRandall Stewart 			if (
3724f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3725f8829a4aSRandall Stewart 			/*
3726f8829a4aSRandall Stewart 			 * If FR's go to new networks, then we must only do
3727f8829a4aSRandall Stewart 			 * this for singly homed asoc's. However if the FR's
3728f8829a4aSRandall Stewart 			 * go to the same network (Armando's work) then its
3729f8829a4aSRandall Stewart 			 * ok to FR multiple times.
3730f8829a4aSRandall Stewart 			 */
37315e54f665SRandall Stewart 			    (asoc->numnets < 2)
3732f8829a4aSRandall Stewart #else
37335e54f665SRandall Stewart 			    (1)
3734f8829a4aSRandall Stewart #endif
37355e54f665SRandall Stewart 			    ) {
37365e54f665SRandall Stewart 
3737f8829a4aSRandall Stewart 				if ((compare_with_wrap(biggest_tsn_newly_acked,
3738f8829a4aSRandall Stewart 				    tp1->rec.data.fast_retran_tsn, MAX_TSN)) ||
3739f8829a4aSRandall Stewart 				    (biggest_tsn_newly_acked ==
3740f8829a4aSRandall Stewart 				    tp1->rec.data.fast_retran_tsn)) {
3741f8829a4aSRandall Stewart 					/*
3742f8829a4aSRandall Stewart 					 * Strike the TSN, since this ack is
3743f8829a4aSRandall Stewart 					 * beyond where things were when we
3744f8829a4aSRandall Stewart 					 * did a FR.
3745f8829a4aSRandall Stewart 					 */
3746b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3747f8829a4aSRandall Stewart 						sctp_log_fr(biggest_tsn_newly_acked,
3748f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3749f8829a4aSRandall Stewart 						    tp1->sent,
3750f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
375180fefe0aSRandall Stewart 					}
37525e54f665SRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3753f8829a4aSRandall Stewart 						tp1->sent++;
37545e54f665SRandall Stewart 					}
3755f8829a4aSRandall Stewart 					strike_flag = 1;
3756b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3757f8829a4aSRandall Stewart 						/*
3758f8829a4aSRandall Stewart 						 * CMT DAC algorithm: If
3759f8829a4aSRandall Stewart 						 * SACK flag is set to 0,
3760f8829a4aSRandall Stewart 						 * then lowest_newack test
3761f8829a4aSRandall Stewart 						 * will not pass because it
3762f8829a4aSRandall Stewart 						 * would have been set to
3763f8829a4aSRandall Stewart 						 * the cumack earlier. If
3764f8829a4aSRandall Stewart 						 * not already to be rtx'd,
3765f8829a4aSRandall Stewart 						 * If not a mixed sack and
3766f8829a4aSRandall Stewart 						 * if tp1 is not between two
3767f8829a4aSRandall Stewart 						 * sacked TSNs, then mark by
3768c105859eSRandall Stewart 						 * one more. NOTE that we
3769c105859eSRandall Stewart 						 * are marking by one
3770c105859eSRandall Stewart 						 * additional time since the
3771c105859eSRandall Stewart 						 * SACK DAC flag indicates
3772c105859eSRandall Stewart 						 * that two packets have
3773c105859eSRandall Stewart 						 * been received after this
3774c105859eSRandall Stewart 						 * missing TSN.
3775f8829a4aSRandall Stewart 						 */
37765e54f665SRandall Stewart 						if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
37775e54f665SRandall Stewart 						    (num_dests_sacked == 1) &&
37785e54f665SRandall Stewart 						    compare_with_wrap(this_sack_lowest_newack,
37795e54f665SRandall Stewart 						    tp1->rec.data.TSN_seq, MAX_TSN)) {
3780b3f1ea41SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3781f8829a4aSRandall Stewart 								sctp_log_fr(32 + num_dests_sacked,
3782f8829a4aSRandall Stewart 								    tp1->rec.data.TSN_seq,
3783f8829a4aSRandall Stewart 								    tp1->sent,
3784f8829a4aSRandall Stewart 								    SCTP_FR_LOG_STRIKE_CHUNK);
378580fefe0aSRandall Stewart 							}
37865e54f665SRandall Stewart 							if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3787f8829a4aSRandall Stewart 								tp1->sent++;
37885e54f665SRandall Stewart 							}
3789f8829a4aSRandall Stewart 						}
3790f8829a4aSRandall Stewart 					}
3791f8829a4aSRandall Stewart 				}
3792f8829a4aSRandall Stewart 			}
3793f8829a4aSRandall Stewart 			/*
379442551e99SRandall Stewart 			 * JRI: TODO: remove code for HTNA algo. CMT's SFR
379542551e99SRandall Stewart 			 * algo covers HTNA.
3796f8829a4aSRandall Stewart 			 */
3797f8829a4aSRandall Stewart 		} else if (compare_with_wrap(tp1->rec.data.TSN_seq,
3798f8829a4aSRandall Stewart 		    biggest_tsn_newly_acked, MAX_TSN)) {
3799f8829a4aSRandall Stewart 			/*
3800f8829a4aSRandall Stewart 			 * We don't strike these: This is the  HTNA
3801f8829a4aSRandall Stewart 			 * algorithm i.e. we don't strike If our TSN is
3802f8829a4aSRandall Stewart 			 * larger than the Highest TSN Newly Acked.
3803f8829a4aSRandall Stewart 			 */
3804f8829a4aSRandall Stewart 			;
3805f8829a4aSRandall Stewart 		} else {
3806f8829a4aSRandall Stewart 			/* Strike the TSN */
3807b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3808f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3809f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3810f8829a4aSRandall Stewart 				    tp1->sent,
3811f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
381280fefe0aSRandall Stewart 			}
38135e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3814f8829a4aSRandall Stewart 				tp1->sent++;
38155e54f665SRandall Stewart 			}
3816b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3817f8829a4aSRandall Stewart 				/*
3818f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
3819f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
3820f8829a4aSRandall Stewart 				 * because it would have been set to the
3821f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
3822f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
3823f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
3824c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
3825c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3826c105859eSRandall Stewart 				 * indicates that two packets have been
3827c105859eSRandall Stewart 				 * received after this missing TSN.
3828f8829a4aSRandall Stewart 				 */
38295e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3830f8829a4aSRandall Stewart 				    compare_with_wrap(this_sack_lowest_newack, tp1->rec.data.TSN_seq, MAX_TSN)) {
3831b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3832f8829a4aSRandall Stewart 						sctp_log_fr(48 + num_dests_sacked,
3833f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3834f8829a4aSRandall Stewart 						    tp1->sent,
3835f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
383680fefe0aSRandall Stewart 					}
3837f8829a4aSRandall Stewart 					tp1->sent++;
3838f8829a4aSRandall Stewart 				}
3839f8829a4aSRandall Stewart 			}
3840f8829a4aSRandall Stewart 		}
3841f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3842f8829a4aSRandall Stewart 			struct sctp_nets *alt;
3843f8829a4aSRandall Stewart 
3844544e35bdSRandall Stewart 			/* fix counts and things */
3845544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3846544e35bdSRandall Stewart 				sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND,
3847544e35bdSRandall Stewart 				    (tp1->whoTo ? (tp1->whoTo->flight_size) : 0),
3848544e35bdSRandall Stewart 				    tp1->book_size,
3849544e35bdSRandall Stewart 				    (uintptr_t) tp1->whoTo,
3850544e35bdSRandall Stewart 				    tp1->rec.data.TSN_seq);
3851544e35bdSRandall Stewart 			}
3852544e35bdSRandall Stewart 			if (tp1->whoTo) {
3853544e35bdSRandall Stewart 				tp1->whoTo->net_ack++;
3854544e35bdSRandall Stewart 				sctp_flight_size_decrease(tp1);
3855544e35bdSRandall Stewart 			}
3856544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
3857544e35bdSRandall Stewart 				sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
3858544e35bdSRandall Stewart 				    asoc->peers_rwnd, tp1->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3859544e35bdSRandall Stewart 			}
3860544e35bdSRandall Stewart 			/* add back to the rwnd */
3861544e35bdSRandall Stewart 			asoc->peers_rwnd += (tp1->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3862544e35bdSRandall Stewart 
3863544e35bdSRandall Stewart 			/* remove from the total flight */
3864544e35bdSRandall Stewart 			sctp_total_flight_decrease(stcb, tp1);
3865544e35bdSRandall Stewart 
3866abe15ad6SRandall Stewart 			if ((stcb->asoc.peer_supports_prsctp) &&
3867abe15ad6SRandall Stewart 			    (PR_SCTP_RTX_ENABLED(tp1->flags))) {
3868abe15ad6SRandall Stewart 				/*
3869abe15ad6SRandall Stewart 				 * Has it been retransmitted tv_sec times? -
3870abe15ad6SRandall Stewart 				 * we store the retran count there.
3871abe15ad6SRandall Stewart 				 */
3872abe15ad6SRandall Stewart 				if (tp1->snd_count > tp1->rec.data.timetodrop.tv_sec) {
3873abe15ad6SRandall Stewart 					/* Yes, so drop it */
3874abe15ad6SRandall Stewart 					if (tp1->data != NULL) {
3875abe15ad6SRandall Stewart 						(void)sctp_release_pr_sctp_chunk(stcb, tp1,
3876abe15ad6SRandall Stewart 						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3877abe15ad6SRandall Stewart 						    SCTP_SO_NOT_LOCKED);
3878abe15ad6SRandall Stewart 					}
3879abe15ad6SRandall Stewart 					/* Make sure to flag we had a FR */
3880abe15ad6SRandall Stewart 					tp1->whoTo->net_ack++;
3881abe15ad6SRandall Stewart 					tp1 = TAILQ_NEXT(tp1, sctp_next);
3882abe15ad6SRandall Stewart 					continue;
3883abe15ad6SRandall Stewart 				}
3884abe15ad6SRandall Stewart 			}
3885f8829a4aSRandall Stewart 			/* printf("OK, we are now ready to FR this guy\n"); */
3886b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3887f8829a4aSRandall Stewart 				sctp_log_fr(tp1->rec.data.TSN_seq, tp1->snd_count,
3888f8829a4aSRandall Stewart 				    0, SCTP_FR_MARKED);
388980fefe0aSRandall Stewart 			}
3890f8829a4aSRandall Stewart 			if (strike_flag) {
3891f8829a4aSRandall Stewart 				/* This is a subsequent FR */
3892f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_sendmultfastretrans);
3893f8829a4aSRandall Stewart 			}
38945e54f665SRandall Stewart 			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3895b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
3896f8829a4aSRandall Stewart 				/*
3897f8829a4aSRandall Stewart 				 * CMT: Using RTX_SSTHRESH policy for CMT.
3898f8829a4aSRandall Stewart 				 * If CMT is being used, then pick dest with
3899f8829a4aSRandall Stewart 				 * largest ssthresh for any retransmission.
3900f8829a4aSRandall Stewart 				 */
3901f8829a4aSRandall Stewart 				tp1->no_fr_allowed = 1;
3902f8829a4aSRandall Stewart 				alt = tp1->whoTo;
39033c503c28SRandall Stewart 				/* sa_ignore NO_NULL_CHK */
3904b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
3905b54d3a6cSRandall Stewart 					/*
3906b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If CMT PF is on,
3907b54d3a6cSRandall Stewart 					 * use the PF version of
3908b54d3a6cSRandall Stewart 					 * find_alt_net()
3909b54d3a6cSRandall Stewart 					 */
3910b54d3a6cSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 2);
3911b54d3a6cSRandall Stewart 				} else {
3912b54d3a6cSRandall Stewart 					/*
3913b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If only CMT is on,
3914b54d3a6cSRandall Stewart 					 * use the CMT version of
3915b54d3a6cSRandall Stewart 					 * find_alt_net()
3916b54d3a6cSRandall Stewart 					 */
391752be287eSRandall Stewart 					/* sa_ignore NO_NULL_CHK */
3918f8829a4aSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 1);
3919b54d3a6cSRandall Stewart 				}
3920ad81507eSRandall Stewart 				if (alt == NULL) {
3921ad81507eSRandall Stewart 					alt = tp1->whoTo;
3922ad81507eSRandall Stewart 				}
3923f8829a4aSRandall Stewart 				/*
3924f8829a4aSRandall Stewart 				 * CUCv2: If a different dest is picked for
3925f8829a4aSRandall Stewart 				 * the retransmission, then new
3926f8829a4aSRandall Stewart 				 * (rtx-)pseudo_cumack needs to be tracked
3927f8829a4aSRandall Stewart 				 * for orig dest. Let CUCv2 track new (rtx-)
3928f8829a4aSRandall Stewart 				 * pseudo-cumack always.
3929f8829a4aSRandall Stewart 				 */
3930ad81507eSRandall Stewart 				if (tp1->whoTo) {
3931f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
3932f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
3933ad81507eSRandall Stewart 				}
3934f8829a4aSRandall Stewart 			} else {/* CMT is OFF */
3935f8829a4aSRandall Stewart 
3936f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3937f8829a4aSRandall Stewart 				/* Can we find an alternate? */
3938f8829a4aSRandall Stewart 				alt = sctp_find_alternate_net(stcb, tp1->whoTo, 0);
3939f8829a4aSRandall Stewart #else
3940f8829a4aSRandall Stewart 				/*
3941f8829a4aSRandall Stewart 				 * default behavior is to NOT retransmit
3942f8829a4aSRandall Stewart 				 * FR's to an alternate. Armando Caro's
3943f8829a4aSRandall Stewart 				 * paper details why.
3944f8829a4aSRandall Stewart 				 */
3945f8829a4aSRandall Stewart 				alt = tp1->whoTo;
3946f8829a4aSRandall Stewart #endif
3947f8829a4aSRandall Stewart 			}
3948f8829a4aSRandall Stewart 
3949f8829a4aSRandall Stewart 			tp1->rec.data.doing_fast_retransmit = 1;
3950f8829a4aSRandall Stewart 			tot_retrans++;
3951f8829a4aSRandall Stewart 			/* mark the sending seq for possible subsequent FR's */
3952f8829a4aSRandall Stewart 			/*
3953f8829a4aSRandall Stewart 			 * printf("Marking TSN for FR new value %x\n",
3954f8829a4aSRandall Stewart 			 * (uint32_t)tpi->rec.data.TSN_seq);
3955f8829a4aSRandall Stewart 			 */
3956f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->send_queue)) {
3957f8829a4aSRandall Stewart 				/*
3958f8829a4aSRandall Stewart 				 * If the queue of send is empty then its
3959f8829a4aSRandall Stewart 				 * the next sequence number that will be
3960f8829a4aSRandall Stewart 				 * assigned so we subtract one from this to
3961f8829a4aSRandall Stewart 				 * get the one we last sent.
3962f8829a4aSRandall Stewart 				 */
3963f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn = sending_seq;
3964f8829a4aSRandall Stewart 			} else {
3965f8829a4aSRandall Stewart 				/*
3966f8829a4aSRandall Stewart 				 * If there are chunks on the send queue
3967f8829a4aSRandall Stewart 				 * (unsent data that has made it from the
3968f8829a4aSRandall Stewart 				 * stream queues but not out the door, we
3969f8829a4aSRandall Stewart 				 * take the first one (which will have the
3970f8829a4aSRandall Stewart 				 * lowest TSN) and subtract one to get the
3971f8829a4aSRandall Stewart 				 * one we last sent.
3972f8829a4aSRandall Stewart 				 */
3973f8829a4aSRandall Stewart 				struct sctp_tmit_chunk *ttt;
3974f8829a4aSRandall Stewart 
3975f8829a4aSRandall Stewart 				ttt = TAILQ_FIRST(&asoc->send_queue);
3976f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn =
3977f8829a4aSRandall Stewart 				    ttt->rec.data.TSN_seq;
3978f8829a4aSRandall Stewart 			}
3979f8829a4aSRandall Stewart 
3980f8829a4aSRandall Stewart 			if (tp1->do_rtt) {
3981f8829a4aSRandall Stewart 				/*
3982f8829a4aSRandall Stewart 				 * this guy had a RTO calculation pending on
3983f8829a4aSRandall Stewart 				 * it, cancel it
3984f8829a4aSRandall Stewart 				 */
3985f8829a4aSRandall Stewart 				tp1->do_rtt = 0;
3986f8829a4aSRandall Stewart 			}
3987f8829a4aSRandall Stewart 			if (alt != tp1->whoTo) {
3988f8829a4aSRandall Stewart 				/* yes, there is an alternate. */
3989f8829a4aSRandall Stewart 				sctp_free_remote_addr(tp1->whoTo);
39903c503c28SRandall Stewart 				/* sa_ignore FREED_MEMORY */
3991f8829a4aSRandall Stewart 				tp1->whoTo = alt;
3992f8829a4aSRandall Stewart 				atomic_add_int(&alt->ref_count, 1);
3993f8829a4aSRandall Stewart 			}
3994f8829a4aSRandall Stewart 		}
3995f8829a4aSRandall Stewart 		tp1 = TAILQ_NEXT(tp1, sctp_next);
3996f8829a4aSRandall Stewart 	}			/* while (tp1) */
3997f8829a4aSRandall Stewart 
3998f8829a4aSRandall Stewart 	if (tot_retrans > 0) {
3999f8829a4aSRandall Stewart 		/*
4000f8829a4aSRandall Stewart 		 * Setup the ecn nonce re-sync point. We do this since once
4001f8829a4aSRandall Stewart 		 * we go to FR something we introduce a Karn's rule scenario
4002f8829a4aSRandall Stewart 		 * and won't know the totals for the ECN bits.
4003f8829a4aSRandall Stewart 		 */
4004f8829a4aSRandall Stewart 		asoc->nonce_resync_tsn = sending_seq;
4005f8829a4aSRandall Stewart 		asoc->nonce_wait_for_ecne = 0;
4006f8829a4aSRandall Stewart 		asoc->nonce_sum_check = 0;
4007f8829a4aSRandall Stewart 	}
4008f8829a4aSRandall Stewart }
4009f8829a4aSRandall Stewart 
4010f8829a4aSRandall Stewart struct sctp_tmit_chunk *
4011f8829a4aSRandall Stewart sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
4012f8829a4aSRandall Stewart     struct sctp_association *asoc)
4013f8829a4aSRandall Stewart {
4014f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2, *a_adv = NULL;
4015f8829a4aSRandall Stewart 	struct timeval now;
4016f8829a4aSRandall Stewart 	int now_filled = 0;
4017f8829a4aSRandall Stewart 
4018f8829a4aSRandall Stewart 	if (asoc->peer_supports_prsctp == 0) {
4019f8829a4aSRandall Stewart 		return (NULL);
4020f8829a4aSRandall Stewart 	}
4021f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
4022f8829a4aSRandall Stewart 	while (tp1) {
4023f8829a4aSRandall Stewart 		if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
4024f8829a4aSRandall Stewart 		    tp1->sent != SCTP_DATAGRAM_RESEND) {
4025f8829a4aSRandall Stewart 			/* no chance to advance, out of here */
4026f8829a4aSRandall Stewart 			break;
4027f8829a4aSRandall Stewart 		}
40280c0982b8SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
40290c0982b8SRandall Stewart 			if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
40300c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
40310c0982b8SRandall Stewart 				    asoc->advanced_peer_ack_point,
40320c0982b8SRandall Stewart 				    tp1->rec.data.TSN_seq, 0, 0);
40330c0982b8SRandall Stewart 			}
40340c0982b8SRandall Stewart 		}
4035f8829a4aSRandall Stewart 		if (!PR_SCTP_ENABLED(tp1->flags)) {
4036f8829a4aSRandall Stewart 			/*
4037f8829a4aSRandall Stewart 			 * We can't fwd-tsn past any that are reliable aka
4038f8829a4aSRandall Stewart 			 * retransmitted until the asoc fails.
4039f8829a4aSRandall Stewart 			 */
4040f8829a4aSRandall Stewart 			break;
4041f8829a4aSRandall Stewart 		}
4042f8829a4aSRandall Stewart 		if (!now_filled) {
40436e55db54SRandall Stewart 			(void)SCTP_GETTIME_TIMEVAL(&now);
4044f8829a4aSRandall Stewart 			now_filled = 1;
4045f8829a4aSRandall Stewart 		}
4046f8829a4aSRandall Stewart 		tp2 = TAILQ_NEXT(tp1, sctp_next);
4047f8829a4aSRandall Stewart 		/*
4048f8829a4aSRandall Stewart 		 * now we got a chunk which is marked for another
4049f8829a4aSRandall Stewart 		 * retransmission to a PR-stream but has run out its chances
4050f8829a4aSRandall Stewart 		 * already maybe OR has been marked to skip now. Can we skip
4051f8829a4aSRandall Stewart 		 * it if its a resend?
4052f8829a4aSRandall Stewart 		 */
4053f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND &&
4054f8829a4aSRandall Stewart 		    (PR_SCTP_TTL_ENABLED(tp1->flags))) {
4055f8829a4aSRandall Stewart 			/*
4056f8829a4aSRandall Stewart 			 * Now is this one marked for resend and its time is
4057f8829a4aSRandall Stewart 			 * now up?
4058f8829a4aSRandall Stewart 			 */
4059f8829a4aSRandall Stewart 			if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
4060f8829a4aSRandall Stewart 				/* Yes so drop it */
4061f8829a4aSRandall Stewart 				if (tp1->data) {
4062ad81507eSRandall Stewart 					(void)sctp_release_pr_sctp_chunk(stcb, tp1,
4063f8829a4aSRandall Stewart 					    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
40640c0982b8SRandall Stewart 					    SCTP_SO_NOT_LOCKED);
4065f8829a4aSRandall Stewart 				}
4066f8829a4aSRandall Stewart 			} else {
4067f8829a4aSRandall Stewart 				/*
4068f8829a4aSRandall Stewart 				 * No, we are done when hit one for resend
4069f8829a4aSRandall Stewart 				 * whos time as not expired.
4070f8829a4aSRandall Stewart 				 */
4071f8829a4aSRandall Stewart 				break;
4072f8829a4aSRandall Stewart 			}
4073f8829a4aSRandall Stewart 		}
4074f8829a4aSRandall Stewart 		/*
4075f8829a4aSRandall Stewart 		 * Ok now if this chunk is marked to drop it we can clean up
4076f8829a4aSRandall Stewart 		 * the chunk, advance our peer ack point and we can check
4077f8829a4aSRandall Stewart 		 * the next chunk.
4078f8829a4aSRandall Stewart 		 */
4079f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
4080f8829a4aSRandall Stewart 			/* advance PeerAckPoint goes forward */
40810c0982b8SRandall Stewart 			if (compare_with_wrap(tp1->rec.data.TSN_seq,
40820c0982b8SRandall Stewart 			    asoc->advanced_peer_ack_point,
40830c0982b8SRandall Stewart 			    MAX_TSN)) {
40840c0982b8SRandall Stewart 
4085f8829a4aSRandall Stewart 				asoc->advanced_peer_ack_point = tp1->rec.data.TSN_seq;
4086f8829a4aSRandall Stewart 				a_adv = tp1;
40870c0982b8SRandall Stewart 			} else if (tp1->rec.data.TSN_seq == asoc->advanced_peer_ack_point) {
40880c0982b8SRandall Stewart 				/* No update but we do save the chk */
40890c0982b8SRandall Stewart 				a_adv = tp1;
40900c0982b8SRandall Stewart 			}
4091f8829a4aSRandall Stewart 		} else {
4092f8829a4aSRandall Stewart 			/*
4093f8829a4aSRandall Stewart 			 * If it is still in RESEND we can advance no
4094f8829a4aSRandall Stewart 			 * further
4095f8829a4aSRandall Stewart 			 */
4096f8829a4aSRandall Stewart 			break;
4097f8829a4aSRandall Stewart 		}
4098f8829a4aSRandall Stewart 		/*
4099f8829a4aSRandall Stewart 		 * If we hit here we just dumped tp1, move to next tsn on
4100f8829a4aSRandall Stewart 		 * sent queue.
4101f8829a4aSRandall Stewart 		 */
4102f8829a4aSRandall Stewart 		tp1 = tp2;
4103f8829a4aSRandall Stewart 	}
4104f8829a4aSRandall Stewart 	return (a_adv);
4105f8829a4aSRandall Stewart }
4106f8829a4aSRandall Stewart 
41070c0982b8SRandall Stewart static int
4108c105859eSRandall Stewart sctp_fs_audit(struct sctp_association *asoc)
4109bff64a4dSRandall Stewart {
4110bff64a4dSRandall Stewart 	struct sctp_tmit_chunk *chk;
4111bff64a4dSRandall Stewart 	int inflight = 0, resend = 0, inbetween = 0, acked = 0, above = 0;
41120c0982b8SRandall Stewart 	int entry_flight, entry_cnt, ret;
41130c0982b8SRandall Stewart 
41140c0982b8SRandall Stewart 	entry_flight = asoc->total_flight;
41150c0982b8SRandall Stewart 	entry_cnt = asoc->total_flight_count;
41160c0982b8SRandall Stewart 	ret = 0;
41170c0982b8SRandall Stewart 
41180c0982b8SRandall Stewart 	if (asoc->pr_sctp_cnt >= asoc->sent_queue_cnt)
41190c0982b8SRandall Stewart 		return (0);
4120bff64a4dSRandall Stewart 
4121bff64a4dSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
4122bff64a4dSRandall Stewart 		if (chk->sent < SCTP_DATAGRAM_RESEND) {
41230c0982b8SRandall Stewart 			printf("Chk TSN:%u size:%d inflight cnt:%d\n",
41240c0982b8SRandall Stewart 			    chk->rec.data.TSN_seq,
41250c0982b8SRandall Stewart 			    chk->send_size,
41260c0982b8SRandall Stewart 			    chk->snd_count
41270c0982b8SRandall Stewart 			    );
4128bff64a4dSRandall Stewart 			inflight++;
4129bff64a4dSRandall Stewart 		} else if (chk->sent == SCTP_DATAGRAM_RESEND) {
4130bff64a4dSRandall Stewart 			resend++;
4131bff64a4dSRandall Stewart 		} else if (chk->sent < SCTP_DATAGRAM_ACKED) {
4132bff64a4dSRandall Stewart 			inbetween++;
4133bff64a4dSRandall Stewart 		} else if (chk->sent > SCTP_DATAGRAM_ACKED) {
4134bff64a4dSRandall Stewart 			above++;
4135bff64a4dSRandall Stewart 		} else {
4136bff64a4dSRandall Stewart 			acked++;
4137bff64a4dSRandall Stewart 		}
4138bff64a4dSRandall Stewart 	}
4139f1f73e57SRandall Stewart 
4140c105859eSRandall Stewart 	if ((inflight > 0) || (inbetween > 0)) {
4141f1f73e57SRandall Stewart #ifdef INVARIANTS
4142c105859eSRandall Stewart 		panic("Flight size-express incorrect? \n");
4143f1f73e57SRandall Stewart #else
41440c0982b8SRandall Stewart 		printf("asoc->total_flight:%d cnt:%d\n",
41450c0982b8SRandall Stewart 		    entry_flight, entry_cnt);
41460c0982b8SRandall Stewart 
41470c0982b8SRandall Stewart 		SCTP_PRINTF("Flight size-express incorrect F:%d I:%d R:%d Ab:%d ACK:%d\n",
41480c0982b8SRandall Stewart 		    inflight, inbetween, resend, above, acked);
41490c0982b8SRandall Stewart 		ret = 1;
4150f1f73e57SRandall Stewart #endif
4151bff64a4dSRandall Stewart 	}
41520c0982b8SRandall Stewart 	return (ret);
4153c105859eSRandall Stewart }
4154c105859eSRandall Stewart 
4155c105859eSRandall Stewart 
4156c105859eSRandall Stewart static void
4157c105859eSRandall Stewart sctp_window_probe_recovery(struct sctp_tcb *stcb,
4158c105859eSRandall Stewart     struct sctp_association *asoc,
4159c105859eSRandall Stewart     struct sctp_nets *net,
4160c105859eSRandall Stewart     struct sctp_tmit_chunk *tp1)
4161c105859eSRandall Stewart {
4162dfb11ef8SRandall Stewart 	tp1->window_probe = 0;
41635171328bSRandall Stewart 	if ((tp1->sent >= SCTP_DATAGRAM_ACKED) || (tp1->data == NULL)) {
4164dfb11ef8SRandall Stewart 		/* TSN's skipped we do NOT move back. */
4165dfb11ef8SRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DWN_WP_FWD,
4166dfb11ef8SRandall Stewart 		    tp1->whoTo->flight_size,
4167dfb11ef8SRandall Stewart 		    tp1->book_size,
4168dfb11ef8SRandall Stewart 		    (uintptr_t) tp1->whoTo,
4169dfb11ef8SRandall Stewart 		    tp1->rec.data.TSN_seq);
4170dfb11ef8SRandall Stewart 		return;
4171dfb11ef8SRandall Stewart 	}
41725171328bSRandall Stewart 	/* First setup this by shrinking flight */
41735171328bSRandall Stewart 	sctp_flight_size_decrease(tp1);
41745171328bSRandall Stewart 	sctp_total_flight_decrease(stcb, tp1);
41755171328bSRandall Stewart 	/* Now mark for resend */
41765171328bSRandall Stewart 	tp1->sent = SCTP_DATAGRAM_RESEND;
41775171328bSRandall Stewart 	asoc->sent_queue_retran_cnt++;
4178b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4179c105859eSRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_WP,
4180c105859eSRandall Stewart 		    tp1->whoTo->flight_size,
4181c105859eSRandall Stewart 		    tp1->book_size,
4182c105859eSRandall Stewart 		    (uintptr_t) tp1->whoTo,
4183c105859eSRandall Stewart 		    tp1->rec.data.TSN_seq);
418480fefe0aSRandall Stewart 	}
4185c105859eSRandall Stewart }
4186c105859eSRandall Stewart 
4187f8829a4aSRandall Stewart void
4188f8829a4aSRandall Stewart sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack,
4189f8829a4aSRandall Stewart     uint32_t rwnd, int nonce_sum_flag, int *abort_now)
4190f8829a4aSRandall Stewart {
4191f8829a4aSRandall Stewart 	struct sctp_nets *net;
4192f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4193f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
41945e54f665SRandall Stewart 	uint32_t old_rwnd;
41955e54f665SRandall Stewart 	int win_probe_recovery = 0;
4196c105859eSRandall Stewart 	int win_probe_recovered = 0;
4197d06c82f1SRandall Stewart 	int j, done_once = 0;
4198f8829a4aSRandall Stewart 
4199b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
4200d06c82f1SRandall Stewart 		sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
4201d06c82f1SRandall Stewart 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
420280fefe0aSRandall Stewart 	}
4203f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
420418e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
420518e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
420618e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
420718e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
420818e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
420918e198d3SRandall Stewart 	}
421018e198d3SRandall Stewart #endif
4211f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
4212d06c82f1SRandall Stewart 	old_rwnd = asoc->peers_rwnd;
42135e54f665SRandall Stewart 	if (compare_with_wrap(asoc->last_acked_seq, cumack, MAX_TSN)) {
42145e54f665SRandall Stewart 		/* old ack */
42155e54f665SRandall Stewart 		return;
4216d06c82f1SRandall Stewart 	} else if (asoc->last_acked_seq == cumack) {
4217d06c82f1SRandall Stewart 		/* Window update sack */
4218d06c82f1SRandall Stewart 		asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
4219b3f1ea41SRandall Stewart 		    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4220d06c82f1SRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4221d06c82f1SRandall Stewart 			/* SWS sender side engages */
4222d06c82f1SRandall Stewart 			asoc->peers_rwnd = 0;
4223d06c82f1SRandall Stewart 		}
4224d06c82f1SRandall Stewart 		if (asoc->peers_rwnd > old_rwnd) {
4225d06c82f1SRandall Stewart 			goto again;
4226d06c82f1SRandall Stewart 		}
4227d06c82f1SRandall Stewart 		return;
42285e54f665SRandall Stewart 	}
4229f8829a4aSRandall Stewart 	/* First setup for CC stuff */
4230f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4231f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
4232f8829a4aSRandall Stewart 		net->net_ack = 0;
4233f8829a4aSRandall Stewart 		net->net_ack2 = 0;
4234132dea7dSRandall Stewart 
4235132dea7dSRandall Stewart 		/*
4236132dea7dSRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
4237132dea7dSRandall Stewart 		 * SACK processing
4238132dea7dSRandall Stewart 		 */
4239132dea7dSRandall Stewart 		net->new_pseudo_cumack = 0;
4240132dea7dSRandall Stewart 		net->will_exit_fast_recovery = 0;
4241f8829a4aSRandall Stewart 	}
4242b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4243139bc87fSRandall Stewart 		uint32_t send_s;
4244139bc87fSRandall Stewart 
4245c105859eSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4246c105859eSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
4247c105859eSRandall Stewart 			    sctpchunk_listhead);
4248c105859eSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
4249139bc87fSRandall Stewart 		} else {
4250c105859eSRandall Stewart 			send_s = asoc->sending_seq;
4251139bc87fSRandall Stewart 		}
4252139bc87fSRandall Stewart 		if ((cumack == send_s) ||
4253139bc87fSRandall Stewart 		    compare_with_wrap(cumack, send_s, MAX_TSN)) {
4254c105859eSRandall Stewart #ifndef INVARIANTS
4255139bc87fSRandall Stewart 			struct mbuf *oper;
4256139bc87fSRandall Stewart 
4257c105859eSRandall Stewart #endif
4258c105859eSRandall Stewart #ifdef INVARIANTS
4259c105859eSRandall Stewart 			panic("Impossible sack 1");
4260c105859eSRandall Stewart #else
4261139bc87fSRandall Stewart 			*abort_now = 1;
4262139bc87fSRandall Stewart 			/* XXX */
4263139bc87fSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4264139bc87fSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
4265139bc87fSRandall Stewart 			if (oper) {
4266139bc87fSRandall Stewart 				struct sctp_paramhdr *ph;
4267139bc87fSRandall Stewart 				uint32_t *ippp;
4268139bc87fSRandall Stewart 
4269139bc87fSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4270139bc87fSRandall Stewart 				    sizeof(uint32_t);
4271139bc87fSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
4272139bc87fSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4273139bc87fSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
4274139bc87fSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
4275139bc87fSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
4276139bc87fSRandall Stewart 			}
4277139bc87fSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4278ceaad40aSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
4279139bc87fSRandall Stewart 			return;
4280139bc87fSRandall Stewart #endif
4281139bc87fSRandall Stewart 		}
4282139bc87fSRandall Stewart 	}
4283f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = cumack;
4284b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4285c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4286c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
4287c4739e2fSRandall Stewart 		    0,
4288c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
4289c4739e2fSRandall Stewart 		    __LINE__);
4290c4739e2fSRandall Stewart 	}
4291f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
42925e54f665SRandall Stewart 	if (compare_with_wrap(cumack, asoc->last_acked_seq, MAX_TSN)) {
4293f8829a4aSRandall Stewart 		/* process the new consecutive TSN first */
4294f8829a4aSRandall Stewart 		tp1 = TAILQ_FIRST(&asoc->sent_queue);
4295f8829a4aSRandall Stewart 		while (tp1) {
4296f8829a4aSRandall Stewart 			tp2 = TAILQ_NEXT(tp1, sctp_next);
4297f8829a4aSRandall Stewart 			if (compare_with_wrap(cumack, tp1->rec.data.TSN_seq,
4298f8829a4aSRandall Stewart 			    MAX_TSN) ||
4299f8829a4aSRandall Stewart 			    cumack == tp1->rec.data.TSN_seq) {
430018e198d3SRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
430118e198d3SRandall Stewart 					printf("Warning, an unsent is now acked?\n");
430218e198d3SRandall Stewart 				}
4303f8829a4aSRandall Stewart 				/*
430418e198d3SRandall Stewart 				 * ECN Nonce: Add the nonce to the sender's
430518e198d3SRandall Stewart 				 * nonce sum
4306f8829a4aSRandall Stewart 				 */
4307f8829a4aSRandall Stewart 				asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
4308f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4309f8829a4aSRandall Stewart 					/*
431018e198d3SRandall Stewart 					 * If it is less than ACKED, it is
431118e198d3SRandall Stewart 					 * now no-longer in flight. Higher
431218e198d3SRandall Stewart 					 * values may occur during marking
4313f8829a4aSRandall Stewart 					 */
4314c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4315b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4316c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4317a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
4318a5d547adSRandall Stewart 							    tp1->book_size,
4319c105859eSRandall Stewart 							    (uintptr_t) tp1->whoTo,
4320a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
432180fefe0aSRandall Stewart 						}
4322c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
432304ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
4324c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
4325f8829a4aSRandall Stewart 					}
4326f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
4327f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
4328f8829a4aSRandall Stewart 						/*
432918e198d3SRandall Stewart 						 * True non-retransmited
4330f8829a4aSRandall Stewart 						 * chunk
4331f8829a4aSRandall Stewart 						 */
4332f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
4333f8829a4aSRandall Stewart 						    tp1->send_size;
4334f8829a4aSRandall Stewart 
4335f8829a4aSRandall Stewart 						/* update RTO too? */
433662c1ff9cSRandall Stewart 						if (tp1->do_rtt) {
4337f8829a4aSRandall Stewart 							tp1->whoTo->RTO =
433804ee05e8SRandall Stewart 							/*
433904ee05e8SRandall Stewart 							 * sa_ignore
434004ee05e8SRandall Stewart 							 * NO_NULL_CHK
434104ee05e8SRandall Stewart 							 */
4342f8829a4aSRandall Stewart 							    sctp_calculate_rto(stcb,
4343f8829a4aSRandall Stewart 							    asoc, tp1->whoTo,
434418e198d3SRandall Stewart 							    &tp1->sent_rcv_time,
434518e198d3SRandall Stewart 							    sctp_align_safe_nocopy);
4346f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
4347f8829a4aSRandall Stewart 						}
4348f8829a4aSRandall Stewart 					}
4349132dea7dSRandall Stewart 					/*
435018e198d3SRandall Stewart 					 * CMT: CUCv2 algorithm. From the
435118e198d3SRandall Stewart 					 * cumack'd TSNs, for each TSN being
435218e198d3SRandall Stewart 					 * acked for the first time, set the
435318e198d3SRandall Stewart 					 * following variables for the
435418e198d3SRandall Stewart 					 * corresp destination.
435518e198d3SRandall Stewart 					 * new_pseudo_cumack will trigger a
435618e198d3SRandall Stewart 					 * cwnd update.
435718e198d3SRandall Stewart 					 * find_(rtx_)pseudo_cumack will
435818e198d3SRandall Stewart 					 * trigger search for the next
435918e198d3SRandall Stewart 					 * expected (rtx-)pseudo-cumack.
4360132dea7dSRandall Stewart 					 */
4361132dea7dSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
4362132dea7dSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
4363132dea7dSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
4364132dea7dSRandall Stewart 
4365b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
436604ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
4367f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
436880fefe0aSRandall Stewart 					}
4369f8829a4aSRandall Stewart 				}
4370f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4371f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4372f8829a4aSRandall Stewart 				}
437342551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
437442551e99SRandall Stewart 					/* deflate the cwnd */
437542551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
437642551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
437742551e99SRandall Stewart 				}
4378f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
4379f8829a4aSRandall Stewart 				TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4380f8829a4aSRandall Stewart 				if (tp1->data) {
438104ee05e8SRandall Stewart 					/* sa_ignore NO_NULL_CHK */
4382f8829a4aSRandall Stewart 					sctp_free_bufspace(stcb, asoc, tp1, 1);
4383f8829a4aSRandall Stewart 					sctp_m_freem(tp1->data);
4384f8829a4aSRandall Stewart 				}
4385b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4386f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
4387f8829a4aSRandall Stewart 					    cumack,
4388f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
4389f8829a4aSRandall Stewart 					    0,
4390f8829a4aSRandall Stewart 					    0,
4391f8829a4aSRandall Stewart 					    SCTP_LOG_FREE_SENT);
439280fefe0aSRandall Stewart 				}
4393f8829a4aSRandall Stewart 				tp1->data = NULL;
4394f8829a4aSRandall Stewart 				asoc->sent_queue_cnt--;
4395f8829a4aSRandall Stewart 				sctp_free_a_chunk(stcb, tp1);
4396f8829a4aSRandall Stewart 				tp1 = tp2;
439718e198d3SRandall Stewart 			} else {
439818e198d3SRandall Stewart 				break;
4399f8829a4aSRandall Stewart 			}
44005e54f665SRandall Stewart 		}
440118e198d3SRandall Stewart 
440218e198d3SRandall Stewart 	}
440304ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
4404f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
4405ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4406ceaad40aSRandall Stewart 		struct socket *so;
4407ceaad40aSRandall Stewart 
4408ceaad40aSRandall Stewart #endif
4409ceaad40aSRandall Stewart 
4410f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4411b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
441204ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
4413f8829a4aSRandall Stewart 			sctp_wakeup_log(stcb, cumack, 1, SCTP_WAKESND_FROM_SACK);
441480fefe0aSRandall Stewart 		}
4415ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4416ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
4417ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
4418ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
4419ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
4420ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
4421ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4422ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4423ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
4424ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
4425ceaad40aSRandall Stewart 			return;
4426ceaad40aSRandall Stewart 		}
4427ceaad40aSRandall Stewart #endif
4428f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4429ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4430ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4431ceaad40aSRandall Stewart #endif
4432f8829a4aSRandall Stewart 	} else {
4433b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4434f8829a4aSRandall Stewart 			sctp_wakeup_log(stcb, cumack, 1, SCTP_NOWAKE_FROM_SACK);
443580fefe0aSRandall Stewart 		}
4436f8829a4aSRandall Stewart 	}
4437f8829a4aSRandall Stewart 
4438b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
4439f8829a4aSRandall Stewart 	if (asoc->last_acked_seq != cumack)
4440b54d3a6cSRandall Stewart 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
44415e54f665SRandall Stewart 
4442f8829a4aSRandall Stewart 	asoc->last_acked_seq = cumack;
44435e54f665SRandall Stewart 
4444f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
4445f8829a4aSRandall Stewart 		/* nothing left in-flight */
4446f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4447f8829a4aSRandall Stewart 			net->flight_size = 0;
4448f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4449f8829a4aSRandall Stewart 		}
4450f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4451f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4452f8829a4aSRandall Stewart 	}
4453f8829a4aSRandall Stewart 	/* ECN Nonce updates */
4454f8829a4aSRandall Stewart 	if (asoc->ecn_nonce_allowed) {
4455f8829a4aSRandall Stewart 		if (asoc->nonce_sum_check) {
4456f8829a4aSRandall Stewart 			if (nonce_sum_flag != ((asoc->nonce_sum_expect_base) & SCTP_SACK_NONCE_SUM)) {
4457f8829a4aSRandall Stewart 				if (asoc->nonce_wait_for_ecne == 0) {
4458f8829a4aSRandall Stewart 					struct sctp_tmit_chunk *lchk;
4459f8829a4aSRandall Stewart 
4460f8829a4aSRandall Stewart 					lchk = TAILQ_FIRST(&asoc->send_queue);
4461f8829a4aSRandall Stewart 					asoc->nonce_wait_for_ecne = 1;
4462f8829a4aSRandall Stewart 					if (lchk) {
4463f8829a4aSRandall Stewart 						asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
4464f8829a4aSRandall Stewart 					} else {
4465f8829a4aSRandall Stewart 						asoc->nonce_wait_tsn = asoc->sending_seq;
4466f8829a4aSRandall Stewart 					}
4467f8829a4aSRandall Stewart 				} else {
4468f8829a4aSRandall Stewart 					if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
4469f8829a4aSRandall Stewart 					    (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
4470f8829a4aSRandall Stewart 						/*
4471f8829a4aSRandall Stewart 						 * Misbehaving peer. We need
4472f8829a4aSRandall Stewart 						 * to react to this guy
4473f8829a4aSRandall Stewart 						 */
4474f8829a4aSRandall Stewart 						asoc->ecn_allowed = 0;
4475f8829a4aSRandall Stewart 						asoc->ecn_nonce_allowed = 0;
4476f8829a4aSRandall Stewart 					}
4477f8829a4aSRandall Stewart 				}
4478f8829a4aSRandall Stewart 			}
4479f8829a4aSRandall Stewart 		} else {
4480f8829a4aSRandall Stewart 			/* See if Resynchronization Possible */
4481f8829a4aSRandall Stewart 			if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
4482f8829a4aSRandall Stewart 				asoc->nonce_sum_check = 1;
4483f8829a4aSRandall Stewart 				/*
4484f8829a4aSRandall Stewart 				 * now we must calculate what the base is.
4485f8829a4aSRandall Stewart 				 * We do this based on two things, we know
4486f8829a4aSRandall Stewart 				 * the total's for all the segments
4487f8829a4aSRandall Stewart 				 * gap-acked in the SACK (none), We also
4488f8829a4aSRandall Stewart 				 * know the SACK's nonce sum, its in
4489f8829a4aSRandall Stewart 				 * nonce_sum_flag. So we can build a truth
4490f8829a4aSRandall Stewart 				 * table to back-calculate the new value of
4491f8829a4aSRandall Stewart 				 * asoc->nonce_sum_expect_base:
4492f8829a4aSRandall Stewart 				 *
4493f8829a4aSRandall Stewart 				 * SACK-flag-Value         Seg-Sums Base 0 0 0
4494f8829a4aSRandall Stewart 				 * 1                    0 1 0 1 1 1
4495f8829a4aSRandall Stewart 				 * 1 0
4496f8829a4aSRandall Stewart 				 */
4497f8829a4aSRandall Stewart 				asoc->nonce_sum_expect_base = (0 ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
4498f8829a4aSRandall Stewart 			}
4499f8829a4aSRandall Stewart 		}
4500f8829a4aSRandall Stewart 	}
4501f8829a4aSRandall Stewart 	/* RWND update */
4502f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
4503b3f1ea41SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4504f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4505f8829a4aSRandall Stewart 		/* SWS sender side engages */
4506f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
4507f8829a4aSRandall Stewart 	}
45085e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
45095e54f665SRandall Stewart 		win_probe_recovery = 1;
45105e54f665SRandall Stewart 	}
4511f8829a4aSRandall Stewart 	/* Now assure a timer where data is queued at */
4512a5d547adSRandall Stewart again:
4513a5d547adSRandall Stewart 	j = 0;
4514f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
45155171328bSRandall Stewart 		int to_ticks;
45165171328bSRandall Stewart 
45175e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
4518c105859eSRandall Stewart 			win_probe_recovered = 1;
45195e54f665SRandall Stewart 			/*
45205e54f665SRandall Stewart 			 * Find first chunk that was used with window probe
45215e54f665SRandall Stewart 			 * and clear the sent
45225e54f665SRandall Stewart 			 */
45233c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
45245e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
45255e54f665SRandall Stewart 				if (tp1->window_probe) {
4526c105859eSRandall Stewart 					sctp_window_probe_recovery(stcb, asoc, net, tp1);
45275e54f665SRandall Stewart 					break;
45285e54f665SRandall Stewart 				}
45295e54f665SRandall Stewart 			}
45305e54f665SRandall Stewart 		}
4531f8829a4aSRandall Stewart 		if (net->RTO == 0) {
4532f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
4533f8829a4aSRandall Stewart 		} else {
4534f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
4535f8829a4aSRandall Stewart 		}
45365171328bSRandall Stewart 		if (net->flight_size) {
4537a5d547adSRandall Stewart 			j++;
4538ad81507eSRandall Stewart 			(void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
4539f8829a4aSRandall Stewart 			    sctp_timeout_handler, &net->rxt_timer);
45405171328bSRandall Stewart 			if (net->window_probe) {
45415171328bSRandall Stewart 				net->window_probe = 0;
45425171328bSRandall Stewart 			}
4543f8829a4aSRandall Stewart 		} else {
45445171328bSRandall Stewart 			if (net->window_probe) {
45455171328bSRandall Stewart 				/*
45465171328bSRandall Stewart 				 * In window probes we must assure a timer
45475171328bSRandall Stewart 				 * is still running there
45485171328bSRandall Stewart 				 */
45495171328bSRandall Stewart 				net->window_probe = 0;
45505171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
45515171328bSRandall Stewart 					SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
45525171328bSRandall Stewart 					    sctp_timeout_handler, &net->rxt_timer);
45535171328bSRandall Stewart 				}
45545171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4555f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4556a5d547adSRandall Stewart 				    stcb, net,
4557a5d547adSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
4558f8829a4aSRandall Stewart 			}
4559b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
4560139bc87fSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4561f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
4562a5d547adSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4563a5d547adSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
4564f8829a4aSRandall Stewart 				}
4565f8829a4aSRandall Stewart 			}
4566f8829a4aSRandall Stewart 		}
4567f8829a4aSRandall Stewart 	}
4568bff64a4dSRandall Stewart 	if ((j == 0) &&
4569bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
4570bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
4571c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
4572bff64a4dSRandall Stewart 	    (done_once == 0)) {
45730c0982b8SRandall Stewart 		/*
45740c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
45750c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
45760c0982b8SRandall Stewart 		 */
45770c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
4578a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
45790c0982b8SRandall Stewart 				if (net->flight_size) {
4580a5d547adSRandall Stewart 					net->flight_size = 0;
4581a5d547adSRandall Stewart 				}
45820c0982b8SRandall Stewart 			}
4583a5d547adSRandall Stewart 			asoc->total_flight = 0;
4584a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
4585a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4586a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4587a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4588c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
4589c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
4590a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4591a5d547adSRandall Stewart 					asoc->sent_queue_retran_cnt++;
4592a5d547adSRandall Stewart 				}
4593a5d547adSRandall Stewart 			}
45940c0982b8SRandall Stewart 		}
4595bff64a4dSRandall Stewart 		done_once = 1;
4596a5d547adSRandall Stewart 		goto again;
4597a5d547adSRandall Stewart 	}
4598f8829a4aSRandall Stewart 	/**********************************/
4599f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
4600f8829a4aSRandall Stewart 	/**********************************/
4601f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4602f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
4603f8829a4aSRandall Stewart 		/* clean up */
4604f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
4605f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4606f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4607f8829a4aSRandall Stewart 		    (asoc->locked_on_sending)
4608f8829a4aSRandall Stewart 		    ) {
4609f8829a4aSRandall Stewart 			struct sctp_stream_queue_pending *sp;
4610f8829a4aSRandall Stewart 
4611f8829a4aSRandall Stewart 			/*
4612f8829a4aSRandall Stewart 			 * I may be in a state where we got all across.. but
4613f8829a4aSRandall Stewart 			 * cannot write more due to a shutdown... we abort
4614f8829a4aSRandall Stewart 			 * since the user did not indicate EOR in this case.
4615f8829a4aSRandall Stewart 			 * The sp will be cleaned during free of the asoc.
4616f8829a4aSRandall Stewart 			 */
4617f8829a4aSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4618f8829a4aSRandall Stewart 			    sctp_streamhead);
46192afb3e84SRandall Stewart 			if ((sp) && (sp->length == 0)) {
46202afb3e84SRandall Stewart 				/* Let cleanup code purge it */
46212afb3e84SRandall Stewart 				if (sp->msg_is_complete) {
46222afb3e84SRandall Stewart 					asoc->stream_queue_cnt--;
46232afb3e84SRandall Stewart 				} else {
4624f8829a4aSRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
4625f8829a4aSRandall Stewart 					asoc->locked_on_sending = NULL;
4626f8829a4aSRandall Stewart 					asoc->stream_queue_cnt--;
4627f8829a4aSRandall Stewart 				}
4628f8829a4aSRandall Stewart 			}
46292afb3e84SRandall Stewart 		}
4630f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4631f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4632f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4633f8829a4aSRandall Stewart 				/* Need to abort here */
4634f8829a4aSRandall Stewart 				struct mbuf *oper;
4635f8829a4aSRandall Stewart 
4636f8829a4aSRandall Stewart 		abort_out_now:
4637f8829a4aSRandall Stewart 				*abort_now = 1;
4638f8829a4aSRandall Stewart 				/* XXX */
4639f8829a4aSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4640f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
4641f8829a4aSRandall Stewart 				if (oper) {
4642f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
4643f8829a4aSRandall Stewart 					uint32_t *ippp;
4644f8829a4aSRandall Stewart 
4645139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4646f8829a4aSRandall Stewart 					    sizeof(uint32_t);
4647f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
4648f8829a4aSRandall Stewart 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4649139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
4650f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
4651a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_24);
4652f8829a4aSRandall Stewart 				}
4653a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
4654ceaad40aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
4655f8829a4aSRandall Stewart 			} else {
4656f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4657f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4658f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4659f42a358aSRandall Stewart 				}
4660c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4661b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4662f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
4663f8829a4aSRandall Stewart 				sctp_send_shutdown(stcb,
4664f8829a4aSRandall Stewart 				    stcb->asoc.primary_destination);
4665f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4666f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
4667f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4668f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
4669f8829a4aSRandall Stewart 			}
4670f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4671f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4672f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4673f8829a4aSRandall Stewart 				goto abort_out_now;
4674f8829a4aSRandall Stewart 			}
4675f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4676c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4677b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4678f8829a4aSRandall Stewart 			sctp_send_shutdown_ack(stcb,
4679f8829a4aSRandall Stewart 			    stcb->asoc.primary_destination);
4680f8829a4aSRandall Stewart 
4681f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4682f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb, asoc->primary_destination);
4683f8829a4aSRandall Stewart 		}
4684f8829a4aSRandall Stewart 	}
4685dfb11ef8SRandall Stewart 	/*********************************************/
4686dfb11ef8SRandall Stewart 	/* Here we perform PR-SCTP procedures        */
4687dfb11ef8SRandall Stewart 	/* (section 4.2)                             */
4688dfb11ef8SRandall Stewart 	/*********************************************/
4689dfb11ef8SRandall Stewart 	/* C1. update advancedPeerAckPoint */
4690dfb11ef8SRandall Stewart 	if (compare_with_wrap(cumack, asoc->advanced_peer_ack_point, MAX_TSN)) {
4691dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cumack;
4692dfb11ef8SRandall Stewart 	}
4693830d754dSRandall Stewart 	/* PR-Sctp issues need to be addressed too */
4694830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
4695830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
4696830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
4697830d754dSRandall Stewart 
4698830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
4699830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
4700830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
4701830d754dSRandall Stewart 		if (compare_with_wrap(asoc->advanced_peer_ack_point, cumack,
4702830d754dSRandall Stewart 		    MAX_TSN)) {
4703830d754dSRandall Stewart 			/*
4704830d754dSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing for notes
4705830d754dSRandall Stewart 			 * on issues that will occur when the ECN NONCE
4706830d754dSRandall Stewart 			 * stuff is put into SCTP for cross checking.
4707830d754dSRandall Stewart 			 */
4708830d754dSRandall Stewart 			if (compare_with_wrap(asoc->advanced_peer_ack_point, old_adv_peer_ack_point,
4709830d754dSRandall Stewart 			    MAX_TSN)) {
4710830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
4711830d754dSRandall Stewart 				/*
4712830d754dSRandall Stewart 				 * ECN Nonce: Disable Nonce Sum check when
4713830d754dSRandall Stewart 				 * FWD TSN is sent and store resync tsn
4714830d754dSRandall Stewart 				 */
4715830d754dSRandall Stewart 				asoc->nonce_sum_check = 0;
4716830d754dSRandall Stewart 				asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
47170c0982b8SRandall Stewart 			} else if (lchk) {
47180c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
47190c0982b8SRandall Stewart 				lchk->rec.data.fwd_tsn_cnt++;
47200c0982b8SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt > 3) {
47210c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
47220c0982b8SRandall Stewart 					lchk->rec.data.fwd_tsn_cnt = 0;
47230c0982b8SRandall Stewart 				}
4724830d754dSRandall Stewart 			}
4725830d754dSRandall Stewart 		}
4726830d754dSRandall Stewart 		if (lchk) {
4727830d754dSRandall Stewart 			/* Assure a timer is up */
4728830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4729830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
4730830d754dSRandall Stewart 		}
4731830d754dSRandall Stewart 	}
4732b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
4733f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4734f8829a4aSRandall Stewart 		    rwnd,
4735f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
4736f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
4737f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
473880fefe0aSRandall Stewart 	}
4739f8829a4aSRandall Stewart }
4740f8829a4aSRandall Stewart 
4741f8829a4aSRandall Stewart void
4742458303daSRandall Stewart sctp_handle_sack(struct mbuf *m, int offset,
4743458303daSRandall Stewart     struct sctp_sack_chunk *ch, struct sctp_tcb *stcb,
4744d06c82f1SRandall Stewart     struct sctp_nets *net_from, int *abort_now, int sack_len, uint32_t rwnd)
4745f8829a4aSRandall Stewart {
4746f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4747f8829a4aSRandall Stewart 	struct sctp_sack *sack;
4748f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
4749f8829a4aSRandall Stewart 	uint32_t cum_ack, last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked,
4750f8829a4aSRandall Stewart 	         this_sack_lowest_newack;
4751bff64a4dSRandall Stewart 	uint32_t sav_cum_ack;
4752f8829a4aSRandall Stewart 	uint16_t num_seg, num_dup;
4753f8829a4aSRandall Stewart 	uint16_t wake_him = 0;
4754f8829a4aSRandall Stewart 	unsigned int sack_length;
4755c105859eSRandall Stewart 	uint32_t send_s = 0;
4756f8829a4aSRandall Stewart 	long j;
4757f8829a4aSRandall Stewart 	int accum_moved = 0;
4758f8829a4aSRandall Stewart 	int will_exit_fast_recovery = 0;
47595e54f665SRandall Stewart 	uint32_t a_rwnd, old_rwnd;
47605e54f665SRandall Stewart 	int win_probe_recovery = 0;
4761c105859eSRandall Stewart 	int win_probe_recovered = 0;
4762f8829a4aSRandall Stewart 	struct sctp_nets *net = NULL;
4763f8829a4aSRandall Stewart 	int nonce_sum_flag, ecn_seg_sums = 0;
4764bff64a4dSRandall Stewart 	int done_once;
4765f8829a4aSRandall Stewart 	uint8_t reneged_all = 0;
4766f8829a4aSRandall Stewart 	uint8_t cmt_dac_flag;
4767f8829a4aSRandall Stewart 
4768f8829a4aSRandall Stewart 	/*
4769f8829a4aSRandall Stewart 	 * we take any chance we can to service our queues since we cannot
4770f8829a4aSRandall Stewart 	 * get awoken when the socket is read from :<
4771f8829a4aSRandall Stewart 	 */
4772f8829a4aSRandall Stewart 	/*
4773f8829a4aSRandall Stewart 	 * Now perform the actual SACK handling: 1) Verify that it is not an
4774f8829a4aSRandall Stewart 	 * old sack, if so discard. 2) If there is nothing left in the send
4775f8829a4aSRandall Stewart 	 * queue (cum-ack is equal to last acked) then you have a duplicate
4776f8829a4aSRandall Stewart 	 * too, update any rwnd change and verify no timers are running.
4777f8829a4aSRandall Stewart 	 * then return. 3) Process any new consequtive data i.e. cum-ack
4778f8829a4aSRandall Stewart 	 * moved process these first and note that it moved. 4) Process any
4779f8829a4aSRandall Stewart 	 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
4780f8829a4aSRandall Stewart 	 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
4781f8829a4aSRandall Stewart 	 * sync up flightsizes and things, stop all timers and also check
4782f8829a4aSRandall Stewart 	 * for shutdown_pending state. If so then go ahead and send off the
4783f8829a4aSRandall Stewart 	 * shutdown. If in shutdown recv, send off the shutdown-ack and
4784f8829a4aSRandall Stewart 	 * start that timer, Ret. 9) Strike any non-acked things and do FR
4785f8829a4aSRandall Stewart 	 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
4786f8829a4aSRandall Stewart 	 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
4787f8829a4aSRandall Stewart 	 * if in shutdown_recv state.
4788f8829a4aSRandall Stewart 	 */
4789f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
4790f8829a4aSRandall Stewart 	sack = &ch->sack;
4791f8829a4aSRandall Stewart 	/* CMT DAC algo */
4792f8829a4aSRandall Stewart 	this_sack_lowest_newack = 0;
4793f8829a4aSRandall Stewart 	j = 0;
4794d06c82f1SRandall Stewart 	sack_length = (unsigned int)sack_len;
4795f8829a4aSRandall Stewart 	/* ECN Nonce */
4796f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_slowpath_sack);
4797f8829a4aSRandall Stewart 	nonce_sum_flag = ch->ch.chunk_flags & SCTP_SACK_NONCE_SUM;
4798f8829a4aSRandall Stewart 	cum_ack = last_tsn = ntohl(sack->cum_tsn_ack);
479918e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
480018e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
480118e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
480218e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
480318e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
480418e198d3SRandall Stewart 	}
480518e198d3SRandall Stewart #endif
4806f8829a4aSRandall Stewart 	num_seg = ntohs(sack->num_gap_ack_blks);
4807d06c82f1SRandall Stewart 	a_rwnd = rwnd;
4808f8829a4aSRandall Stewart 
4809f8829a4aSRandall Stewart 	/* CMT DAC algo */
4810f8829a4aSRandall Stewart 	cmt_dac_flag = ch->ch.chunk_flags & SCTP_SACK_CMT_DAC;
4811f8829a4aSRandall Stewart 	num_dup = ntohs(sack->num_dup_tsns);
4812f8829a4aSRandall Stewart 
48135e54f665SRandall Stewart 	old_rwnd = stcb->asoc.peers_rwnd;
4814b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4815c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4816c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
4817c4739e2fSRandall Stewart 		    0,
4818c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
4819c4739e2fSRandall Stewart 		    __LINE__);
4820c4739e2fSRandall Stewart 	}
4821f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
4822f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
4823b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4824f8829a4aSRandall Stewart 		sctp_log_sack(asoc->last_acked_seq,
4825f8829a4aSRandall Stewart 		    cum_ack,
4826f8829a4aSRandall Stewart 		    0,
4827f8829a4aSRandall Stewart 		    num_seg,
4828f8829a4aSRandall Stewart 		    num_dup,
4829f8829a4aSRandall Stewart 		    SCTP_LOG_NEW_SACK);
483080fefe0aSRandall Stewart 	}
4831b3f1ea41SRandall Stewart 	if ((num_dup) && (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_FR_LOGGING_ENABLE | SCTP_EARLYFR_LOGGING_ENABLE))) {
4832f8829a4aSRandall Stewart 		int off_to_dup, iii;
4833458303daSRandall Stewart 		uint32_t *dupdata, dblock;
4834f8829a4aSRandall Stewart 
4835f8829a4aSRandall Stewart 		off_to_dup = (num_seg * sizeof(struct sctp_gap_ack_block)) + sizeof(struct sctp_sack_chunk);
4836f8829a4aSRandall Stewart 		if ((off_to_dup + (num_dup * sizeof(uint32_t))) <= sack_length) {
4837458303daSRandall Stewart 			dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
4838458303daSRandall Stewart 			    sizeof(uint32_t), (uint8_t *) & dblock);
4839458303daSRandall Stewart 			off_to_dup += sizeof(uint32_t);
4840458303daSRandall Stewart 			if (dupdata) {
4841f8829a4aSRandall Stewart 				for (iii = 0; iii < num_dup; iii++) {
4842f8829a4aSRandall Stewart 					sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
4843458303daSRandall Stewart 					dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
4844458303daSRandall Stewart 					    sizeof(uint32_t), (uint8_t *) & dblock);
4845458303daSRandall Stewart 					if (dupdata == NULL)
4846458303daSRandall Stewart 						break;
4847458303daSRandall Stewart 					off_to_dup += sizeof(uint32_t);
4848458303daSRandall Stewart 				}
4849f8829a4aSRandall Stewart 			}
4850f8829a4aSRandall Stewart 		} else {
4851ad81507eSRandall Stewart 			SCTP_PRINTF("Size invalid offset to dups:%d number dups:%d sack_len:%d num gaps:%d\n",
4852f8829a4aSRandall Stewart 			    off_to_dup, num_dup, sack_length, num_seg);
4853f8829a4aSRandall Stewart 		}
4854f8829a4aSRandall Stewart 	}
4855b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4856c105859eSRandall Stewart 		/* reality check */
4857c105859eSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4858c105859eSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
4859c105859eSRandall Stewart 			    sctpchunk_listhead);
4860c105859eSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
4861c105859eSRandall Stewart 		} else {
4862c105859eSRandall Stewart 			send_s = asoc->sending_seq;
4863c105859eSRandall Stewart 		}
4864f8829a4aSRandall Stewart 		if (cum_ack == send_s ||
4865f8829a4aSRandall Stewart 		    compare_with_wrap(cum_ack, send_s, MAX_TSN)) {
4866c105859eSRandall Stewart #ifndef INVARIANTS
4867c105859eSRandall Stewart 			struct mbuf *oper;
4868c105859eSRandall Stewart 
4869c105859eSRandall Stewart #endif
4870c105859eSRandall Stewart #ifdef INVARIANTS
4871139bc87fSRandall Stewart 	hopeless_peer:
4872139bc87fSRandall Stewart 			panic("Impossible sack 1");
4873139bc87fSRandall Stewart #else
4874c105859eSRandall Stewart 
4875f8829a4aSRandall Stewart 
4876f8829a4aSRandall Stewart 			/*
4877f8829a4aSRandall Stewart 			 * no way, we have not even sent this TSN out yet.
4878f8829a4aSRandall Stewart 			 * Peer is hopelessly messed up with us.
4879f8829a4aSRandall Stewart 			 */
4880f8829a4aSRandall Stewart 	hopeless_peer:
4881f8829a4aSRandall Stewart 			*abort_now = 1;
4882f8829a4aSRandall Stewart 			/* XXX */
4883f8829a4aSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4884f8829a4aSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
4885f8829a4aSRandall Stewart 			if (oper) {
4886f8829a4aSRandall Stewart 				struct sctp_paramhdr *ph;
4887f8829a4aSRandall Stewart 				uint32_t *ippp;
4888f8829a4aSRandall Stewart 
4889139bc87fSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4890f8829a4aSRandall Stewart 				    sizeof(uint32_t);
4891f8829a4aSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
4892f8829a4aSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4893139bc87fSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
4894f8829a4aSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
4895a5d547adSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
4896f8829a4aSRandall Stewart 			}
4897a5d547adSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4898ceaad40aSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
4899f8829a4aSRandall Stewart 			return;
4900139bc87fSRandall Stewart #endif
4901f8829a4aSRandall Stewart 		}
4902f8829a4aSRandall Stewart 	}
4903f8829a4aSRandall Stewart 	/**********************/
4904f8829a4aSRandall Stewart 	/* 1) check the range */
4905f8829a4aSRandall Stewart 	/**********************/
4906f8829a4aSRandall Stewart 	if (compare_with_wrap(asoc->last_acked_seq, last_tsn, MAX_TSN)) {
4907f8829a4aSRandall Stewart 		/* acking something behind */
4908f8829a4aSRandall Stewart 		return;
4909f8829a4aSRandall Stewart 	}
4910bff64a4dSRandall Stewart 	sav_cum_ack = asoc->last_acked_seq;
4911bff64a4dSRandall Stewart 
4912f8829a4aSRandall Stewart 	/* update the Rwnd of the peer */
4913f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue) &&
4914f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->send_queue) &&
4915f8829a4aSRandall Stewart 	    (asoc->stream_queue_cnt == 0)
4916f8829a4aSRandall Stewart 	    ) {
4917f8829a4aSRandall Stewart 		/* nothing left on send/sent and strmq */
4918b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4919f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4920f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
492180fefe0aSRandall Stewart 		}
4922f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
4923f8829a4aSRandall Stewart 		if (asoc->sent_queue_retran_cnt) {
4924f8829a4aSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4925f8829a4aSRandall Stewart 		}
4926f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4927f8829a4aSRandall Stewart 			/* SWS sender side engages */
4928f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
4929f8829a4aSRandall Stewart 		}
4930f8829a4aSRandall Stewart 		/* stop any timers */
4931f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4932f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4933a5d547adSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4934b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
4935139bc87fSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4936f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck1);
4937a5d547adSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4938a5d547adSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4939f8829a4aSRandall Stewart 				}
4940f8829a4aSRandall Stewart 			}
4941f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4942f8829a4aSRandall Stewart 			net->flight_size = 0;
4943f8829a4aSRandall Stewart 		}
4944f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4945f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4946f8829a4aSRandall Stewart 		return;
4947f8829a4aSRandall Stewart 	}
4948f8829a4aSRandall Stewart 	/*
4949f8829a4aSRandall Stewart 	 * We init netAckSz and netAckSz2 to 0. These are used to track 2
4950f8829a4aSRandall Stewart 	 * things. The total byte count acked is tracked in netAckSz AND
4951f8829a4aSRandall Stewart 	 * netAck2 is used to track the total bytes acked that are un-
4952f8829a4aSRandall Stewart 	 * amibguious and were never retransmitted. We track these on a per
4953f8829a4aSRandall Stewart 	 * destination address basis.
4954f8829a4aSRandall Stewart 	 */
4955f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4956f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
4957f8829a4aSRandall Stewart 		net->net_ack = 0;
4958f8829a4aSRandall Stewart 		net->net_ack2 = 0;
4959f8829a4aSRandall Stewart 
4960f8829a4aSRandall Stewart 		/*
496142551e99SRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
496242551e99SRandall Stewart 		 * SACK processing
4963f8829a4aSRandall Stewart 		 */
4964f8829a4aSRandall Stewart 		net->new_pseudo_cumack = 0;
4965f8829a4aSRandall Stewart 		net->will_exit_fast_recovery = 0;
4966f8829a4aSRandall Stewart 	}
4967f8829a4aSRandall Stewart 	/* process the new consecutive TSN first */
4968f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
4969f8829a4aSRandall Stewart 	while (tp1) {
4970f8829a4aSRandall Stewart 		if (compare_with_wrap(last_tsn, tp1->rec.data.TSN_seq,
4971f8829a4aSRandall Stewart 		    MAX_TSN) ||
4972f8829a4aSRandall Stewart 		    last_tsn == tp1->rec.data.TSN_seq) {
4973f8829a4aSRandall Stewart 			if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4974f8829a4aSRandall Stewart 				/*
4975f8829a4aSRandall Stewart 				 * ECN Nonce: Add the nonce to the sender's
4976f8829a4aSRandall Stewart 				 * nonce sum
4977f8829a4aSRandall Stewart 				 */
4978f8829a4aSRandall Stewart 				asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
4979f8829a4aSRandall Stewart 				accum_moved = 1;
4980f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4981f8829a4aSRandall Stewart 					/*
4982f8829a4aSRandall Stewart 					 * If it is less than ACKED, it is
4983f8829a4aSRandall Stewart 					 * now no-longer in flight. Higher
4984f8829a4aSRandall Stewart 					 * values may occur during marking
4985f8829a4aSRandall Stewart 					 */
4986f8829a4aSRandall Stewart 					if ((tp1->whoTo->dest_state &
4987f8829a4aSRandall Stewart 					    SCTP_ADDR_UNCONFIRMED) &&
4988f8829a4aSRandall Stewart 					    (tp1->snd_count < 2)) {
4989f8829a4aSRandall Stewart 						/*
4990f8829a4aSRandall Stewart 						 * If there was no retran
4991f8829a4aSRandall Stewart 						 * and the address is
4992f8829a4aSRandall Stewart 						 * un-confirmed and we sent
4993f8829a4aSRandall Stewart 						 * there and are now
4994f8829a4aSRandall Stewart 						 * sacked.. its confirmed,
4995f8829a4aSRandall Stewart 						 * mark it so.
4996f8829a4aSRandall Stewart 						 */
4997f8829a4aSRandall Stewart 						tp1->whoTo->dest_state &=
4998f8829a4aSRandall Stewart 						    ~SCTP_ADDR_UNCONFIRMED;
4999f8829a4aSRandall Stewart 					}
5000c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5001b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
5002c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
5003a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
5004a5d547adSRandall Stewart 							    tp1->book_size,
5005c105859eSRandall Stewart 							    (uintptr_t) tp1->whoTo,
5006a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
500780fefe0aSRandall Stewart 						}
5008c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
5009c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
5010f8829a4aSRandall Stewart 					}
5011f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
5012f8829a4aSRandall Stewart 
5013f8829a4aSRandall Stewart 					/* CMT SFR and DAC algos */
5014f8829a4aSRandall Stewart 					this_sack_lowest_newack = tp1->rec.data.TSN_seq;
5015f8829a4aSRandall Stewart 					tp1->whoTo->saw_newack = 1;
5016f8829a4aSRandall Stewart 
5017f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
5018f8829a4aSRandall Stewart 						/*
5019f8829a4aSRandall Stewart 						 * True non-retransmited
5020f8829a4aSRandall Stewart 						 * chunk
5021f8829a4aSRandall Stewart 						 */
5022f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
5023f8829a4aSRandall Stewart 						    tp1->send_size;
5024f8829a4aSRandall Stewart 
5025f8829a4aSRandall Stewart 						/* update RTO too? */
5026f8829a4aSRandall Stewart 						if (tp1->do_rtt) {
5027f8829a4aSRandall Stewart 							tp1->whoTo->RTO =
5028f8829a4aSRandall Stewart 							    sctp_calculate_rto(stcb,
5029f8829a4aSRandall Stewart 							    asoc, tp1->whoTo,
503018e198d3SRandall Stewart 							    &tp1->sent_rcv_time,
503118e198d3SRandall Stewart 							    sctp_align_safe_nocopy);
5032f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
5033f8829a4aSRandall Stewart 						}
5034f8829a4aSRandall Stewart 					}
5035f8829a4aSRandall Stewart 					/*
5036f8829a4aSRandall Stewart 					 * CMT: CUCv2 algorithm. From the
5037f8829a4aSRandall Stewart 					 * cumack'd TSNs, for each TSN being
5038f8829a4aSRandall Stewart 					 * acked for the first time, set the
5039f8829a4aSRandall Stewart 					 * following variables for the
5040f8829a4aSRandall Stewart 					 * corresp destination.
5041f8829a4aSRandall Stewart 					 * new_pseudo_cumack will trigger a
5042f8829a4aSRandall Stewart 					 * cwnd update.
5043f8829a4aSRandall Stewart 					 * find_(rtx_)pseudo_cumack will
5044f8829a4aSRandall Stewart 					 * trigger search for the next
5045f8829a4aSRandall Stewart 					 * expected (rtx-)pseudo-cumack.
5046f8829a4aSRandall Stewart 					 */
5047f8829a4aSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
5048f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
5049f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
5050f8829a4aSRandall Stewart 
5051f8829a4aSRandall Stewart 
5052b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
5053f8829a4aSRandall Stewart 						sctp_log_sack(asoc->last_acked_seq,
5054f8829a4aSRandall Stewart 						    cum_ack,
5055f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
5056f8829a4aSRandall Stewart 						    0,
5057f8829a4aSRandall Stewart 						    0,
5058f8829a4aSRandall Stewart 						    SCTP_LOG_TSN_ACKED);
505980fefe0aSRandall Stewart 					}
5060b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
5061f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
506280fefe0aSRandall Stewart 					}
5063f8829a4aSRandall Stewart 				}
5064f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5065f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
5066f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
5067f8829a4aSRandall Stewart 					sctp_audit_log(0xB3,
5068f8829a4aSRandall Stewart 					    (asoc->sent_queue_retran_cnt & 0x000000ff));
5069f8829a4aSRandall Stewart #endif
5070f8829a4aSRandall Stewart 				}
507142551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
507242551e99SRandall Stewart 					/* deflate the cwnd */
507342551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
507442551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
507542551e99SRandall Stewart 				}
5076f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
5077f8829a4aSRandall Stewart 			}
5078f8829a4aSRandall Stewart 		} else {
5079f8829a4aSRandall Stewart 			break;
5080f8829a4aSRandall Stewart 		}
5081f8829a4aSRandall Stewart 		tp1 = TAILQ_NEXT(tp1, sctp_next);
5082f8829a4aSRandall Stewart 	}
5083f8829a4aSRandall Stewart 	biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
5084f8829a4aSRandall Stewart 	/* always set this up to cum-ack */
5085f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = last_tsn;
5086f8829a4aSRandall Stewart 
5087458303daSRandall Stewart 	/* Move offset up to point to gaps/dups */
5088458303daSRandall Stewart 	offset += sizeof(struct sctp_sack_chunk);
5089f8829a4aSRandall Stewart 	if (((num_seg * (sizeof(struct sctp_gap_ack_block))) + sizeof(struct sctp_sack_chunk)) > sack_length) {
5090f8829a4aSRandall Stewart 
5091f8829a4aSRandall Stewart 		/* skip corrupt segments */
5092f8829a4aSRandall Stewart 		goto skip_segments;
5093f8829a4aSRandall Stewart 	}
5094f8829a4aSRandall Stewart 	if (num_seg > 0) {
5095f8829a4aSRandall Stewart 
5096f8829a4aSRandall Stewart 		/*
5097f8829a4aSRandall Stewart 		 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
5098f8829a4aSRandall Stewart 		 * to be greater than the cumack. Also reset saw_newack to 0
5099f8829a4aSRandall Stewart 		 * for all dests.
5100f8829a4aSRandall Stewart 		 */
5101f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5102f8829a4aSRandall Stewart 			net->saw_newack = 0;
5103f8829a4aSRandall Stewart 			net->this_sack_highest_newack = last_tsn;
5104f8829a4aSRandall Stewart 		}
5105f8829a4aSRandall Stewart 
5106f8829a4aSRandall Stewart 		/*
5107f8829a4aSRandall Stewart 		 * thisSackHighestGap will increase while handling NEW
5108f8829a4aSRandall Stewart 		 * segments this_sack_highest_newack will increase while
5109f8829a4aSRandall Stewart 		 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
5110f8829a4aSRandall Stewart 		 * used for CMT DAC algo. saw_newack will also change.
5111f8829a4aSRandall Stewart 		 */
5112458303daSRandall Stewart 		sctp_handle_segments(m, &offset, stcb, asoc, ch, last_tsn,
5113f8829a4aSRandall Stewart 		    &biggest_tsn_acked, &biggest_tsn_newly_acked, &this_sack_lowest_newack,
5114f8829a4aSRandall Stewart 		    num_seg, &ecn_seg_sums);
5115f8829a4aSRandall Stewart 
5116b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
5117f8829a4aSRandall Stewart 			/*
5118f8829a4aSRandall Stewart 			 * validate the biggest_tsn_acked in the gap acks if
5119f8829a4aSRandall Stewart 			 * strict adherence is wanted.
5120f8829a4aSRandall Stewart 			 */
5121f8829a4aSRandall Stewart 			if ((biggest_tsn_acked == send_s) ||
5122f8829a4aSRandall Stewart 			    (compare_with_wrap(biggest_tsn_acked, send_s, MAX_TSN))) {
5123f8829a4aSRandall Stewart 				/*
5124f8829a4aSRandall Stewart 				 * peer is either confused or we are under
5125f8829a4aSRandall Stewart 				 * attack. We must abort.
5126f8829a4aSRandall Stewart 				 */
5127f8829a4aSRandall Stewart 				goto hopeless_peer;
5128f8829a4aSRandall Stewart 			}
5129f8829a4aSRandall Stewart 		}
5130f8829a4aSRandall Stewart 	}
5131f8829a4aSRandall Stewart skip_segments:
5132f8829a4aSRandall Stewart 	/*******************************************/
5133f8829a4aSRandall Stewart 	/* cancel ALL T3-send timer if accum moved */
5134f8829a4aSRandall Stewart 	/*******************************************/
5135b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
5136f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5137f8829a4aSRandall Stewart 			if (net->new_pseudo_cumack)
5138f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5139a5d547adSRandall Stewart 				    stcb, net,
5140a5d547adSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
5141f8829a4aSRandall Stewart 
5142f8829a4aSRandall Stewart 		}
5143f8829a4aSRandall Stewart 	} else {
5144f8829a4aSRandall Stewart 		if (accum_moved) {
5145f8829a4aSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5146f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5147a5d547adSRandall Stewart 				    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
5148f8829a4aSRandall Stewart 			}
5149f8829a4aSRandall Stewart 		}
5150f8829a4aSRandall Stewart 	}
5151f8829a4aSRandall Stewart 	/********************************************/
5152f8829a4aSRandall Stewart 	/* drop the acked chunks from the sendqueue */
5153f8829a4aSRandall Stewart 	/********************************************/
5154f8829a4aSRandall Stewart 	asoc->last_acked_seq = cum_ack;
5155f8829a4aSRandall Stewart 
5156f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
5157f8829a4aSRandall Stewart 	if (tp1 == NULL)
5158f8829a4aSRandall Stewart 		goto done_with_it;
5159f8829a4aSRandall Stewart 	do {
5160f8829a4aSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq, cum_ack,
5161f8829a4aSRandall Stewart 		    MAX_TSN)) {
5162f8829a4aSRandall Stewart 			break;
5163f8829a4aSRandall Stewart 		}
5164f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
5165f8829a4aSRandall Stewart 			/* no more sent on list */
516618e198d3SRandall Stewart 			printf("Warning, tp1->sent == %d and its now acked?\n",
516718e198d3SRandall Stewart 			    tp1->sent);
5168f8829a4aSRandall Stewart 		}
5169f8829a4aSRandall Stewart 		tp2 = TAILQ_NEXT(tp1, sctp_next);
5170f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
5171f8829a4aSRandall Stewart 		if (tp1->pr_sctp_on) {
5172f8829a4aSRandall Stewart 			if (asoc->pr_sctp_cnt != 0)
5173f8829a4aSRandall Stewart 				asoc->pr_sctp_cnt--;
5174f8829a4aSRandall Stewart 		}
5175f8829a4aSRandall Stewart 		if ((TAILQ_FIRST(&asoc->sent_queue) == NULL) &&
5176f8829a4aSRandall Stewart 		    (asoc->total_flight > 0)) {
5177f1f73e57SRandall Stewart #ifdef INVARIANTS
5178c105859eSRandall Stewart 			panic("Warning flight size is postive and should be 0");
5179f1f73e57SRandall Stewart #else
5180ad81507eSRandall Stewart 			SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
5181f8829a4aSRandall Stewart 			    asoc->total_flight);
5182f1f73e57SRandall Stewart #endif
5183f8829a4aSRandall Stewart 			asoc->total_flight = 0;
5184f8829a4aSRandall Stewart 		}
5185f8829a4aSRandall Stewart 		if (tp1->data) {
518604ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
5187f8829a4aSRandall Stewart 			sctp_free_bufspace(stcb, asoc, tp1, 1);
5188f8829a4aSRandall Stewart 			sctp_m_freem(tp1->data);
5189f8829a4aSRandall Stewart 			if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
5190f8829a4aSRandall Stewart 				asoc->sent_queue_cnt_removeable--;
5191f8829a4aSRandall Stewart 			}
5192f8829a4aSRandall Stewart 		}
5193b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
5194f8829a4aSRandall Stewart 			sctp_log_sack(asoc->last_acked_seq,
5195f8829a4aSRandall Stewart 			    cum_ack,
5196f8829a4aSRandall Stewart 			    tp1->rec.data.TSN_seq,
5197f8829a4aSRandall Stewart 			    0,
5198f8829a4aSRandall Stewart 			    0,
5199f8829a4aSRandall Stewart 			    SCTP_LOG_FREE_SENT);
520080fefe0aSRandall Stewart 		}
5201f8829a4aSRandall Stewart 		tp1->data = NULL;
5202f8829a4aSRandall Stewart 		asoc->sent_queue_cnt--;
5203f8829a4aSRandall Stewart 		sctp_free_a_chunk(stcb, tp1);
5204f8829a4aSRandall Stewart 		wake_him++;
5205f8829a4aSRandall Stewart 		tp1 = tp2;
5206f8829a4aSRandall Stewart 	} while (tp1 != NULL);
5207f8829a4aSRandall Stewart 
5208f8829a4aSRandall Stewart done_with_it:
520904ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
5210f8829a4aSRandall Stewart 	if ((wake_him) && (stcb->sctp_socket)) {
5211ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5212ceaad40aSRandall Stewart 		struct socket *so;
5213ceaad40aSRandall Stewart 
5214ceaad40aSRandall Stewart #endif
5215f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
5216b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
5217f8829a4aSRandall Stewart 			sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_WAKESND_FROM_SACK);
521880fefe0aSRandall Stewart 		}
5219ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5220ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
5221ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
5222ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
5223ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
5224ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
5225ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
5226ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
5227ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
5228ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
5229ceaad40aSRandall Stewart 			return;
5230ceaad40aSRandall Stewart 		}
5231ceaad40aSRandall Stewart #endif
5232f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
5233ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5234ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
5235ceaad40aSRandall Stewart #endif
5236f8829a4aSRandall Stewart 	} else {
5237b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
5238f8829a4aSRandall Stewart 			sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_NOWAKE_FROM_SACK);
523980fefe0aSRandall Stewart 		}
5240f8829a4aSRandall Stewart 	}
5241f8829a4aSRandall Stewart 
524242551e99SRandall Stewart 	if (asoc->fast_retran_loss_recovery && accum_moved) {
5243f8829a4aSRandall Stewart 		if (compare_with_wrap(asoc->last_acked_seq,
5244f8829a4aSRandall Stewart 		    asoc->fast_recovery_tsn, MAX_TSN) ||
5245f8829a4aSRandall Stewart 		    asoc->last_acked_seq == asoc->fast_recovery_tsn) {
5246f8829a4aSRandall Stewart 			/* Setup so we will exit RFC2582 fast recovery */
5247f8829a4aSRandall Stewart 			will_exit_fast_recovery = 1;
5248f8829a4aSRandall Stewart 		}
5249f8829a4aSRandall Stewart 	}
5250f8829a4aSRandall Stewart 	/*
5251f8829a4aSRandall Stewart 	 * Check for revoked fragments:
5252f8829a4aSRandall Stewart 	 *
5253f8829a4aSRandall Stewart 	 * if Previous sack - Had no frags then we can't have any revoked if
5254f8829a4aSRandall Stewart 	 * Previous sack - Had frag's then - If we now have frags aka
5255f8829a4aSRandall Stewart 	 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
5256f8829a4aSRandall Stewart 	 * some of them. else - The peer revoked all ACKED fragments, since
5257f8829a4aSRandall Stewart 	 * we had some before and now we have NONE.
5258f8829a4aSRandall Stewart 	 */
5259f8829a4aSRandall Stewart 
5260f42a358aSRandall Stewart 	if (num_seg)
5261c105859eSRandall Stewart 		sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
5262f8829a4aSRandall Stewart 	else if (asoc->saw_sack_with_frags) {
5263f8829a4aSRandall Stewart 		int cnt_revoked = 0;
5264f8829a4aSRandall Stewart 
5265f8829a4aSRandall Stewart 		tp1 = TAILQ_FIRST(&asoc->sent_queue);
5266f8829a4aSRandall Stewart 		if (tp1 != NULL) {
5267f8829a4aSRandall Stewart 			/* Peer revoked all dg's marked or acked */
5268f8829a4aSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5269f8829a4aSRandall Stewart 				if ((tp1->sent > SCTP_DATAGRAM_RESEND) &&
5270f8829a4aSRandall Stewart 				    (tp1->sent < SCTP_FORWARD_TSN_SKIP)) {
5271f8829a4aSRandall Stewart 					tp1->sent = SCTP_DATAGRAM_SENT;
5272b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
5273c105859eSRandall Stewart 						sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
5274c105859eSRandall Stewart 						    tp1->whoTo->flight_size,
5275c105859eSRandall Stewart 						    tp1->book_size,
5276c105859eSRandall Stewart 						    (uintptr_t) tp1->whoTo,
5277c105859eSRandall Stewart 						    tp1->rec.data.TSN_seq);
527880fefe0aSRandall Stewart 					}
5279c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
5280c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
5281a5d547adSRandall Stewart 					tp1->rec.data.chunk_was_revoked = 1;
528242551e99SRandall Stewart 					/*
528342551e99SRandall Stewart 					 * To ensure that this increase in
528442551e99SRandall Stewart 					 * flightsize, which is artificial,
528542551e99SRandall Stewart 					 * does not throttle the sender, we
528642551e99SRandall Stewart 					 * also increase the cwnd
528742551e99SRandall Stewart 					 * artificially.
528842551e99SRandall Stewart 					 */
528942551e99SRandall Stewart 					tp1->whoTo->cwnd += tp1->book_size;
5290f8829a4aSRandall Stewart 					cnt_revoked++;
5291f8829a4aSRandall Stewart 				}
5292f8829a4aSRandall Stewart 			}
5293f8829a4aSRandall Stewart 			if (cnt_revoked) {
5294f8829a4aSRandall Stewart 				reneged_all = 1;
5295f8829a4aSRandall Stewart 			}
5296f8829a4aSRandall Stewart 		}
5297f8829a4aSRandall Stewart 		asoc->saw_sack_with_frags = 0;
5298f8829a4aSRandall Stewart 	}
5299f8829a4aSRandall Stewart 	if (num_seg)
5300f8829a4aSRandall Stewart 		asoc->saw_sack_with_frags = 1;
5301f8829a4aSRandall Stewart 	else
5302f8829a4aSRandall Stewart 		asoc->saw_sack_with_frags = 0;
5303f8829a4aSRandall Stewart 
5304b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
5305b54d3a6cSRandall Stewart 	asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
5306f8829a4aSRandall Stewart 
5307f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
5308f8829a4aSRandall Stewart 		/* nothing left in-flight */
5309f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5310f8829a4aSRandall Stewart 			/* stop all timers */
5311b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
5312139bc87fSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
5313f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
5314a5d547adSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
5315a5d547adSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_29);
5316f8829a4aSRandall Stewart 				}
5317f8829a4aSRandall Stewart 			}
5318f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5319a5d547adSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
5320f8829a4aSRandall Stewart 			net->flight_size = 0;
5321f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
5322f8829a4aSRandall Stewart 		}
5323f8829a4aSRandall Stewart 		asoc->total_flight = 0;
5324f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
5325f8829a4aSRandall Stewart 	}
5326f8829a4aSRandall Stewart 	/**********************************/
5327f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
5328f8829a4aSRandall Stewart 	/**********************************/
5329f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
5330f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
5331b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
5332f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
5333f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
533480fefe0aSRandall Stewart 		}
5335f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
5336f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5337f8829a4aSRandall Stewart 			/* SWS sender side engages */
5338f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
5339f8829a4aSRandall Stewart 		}
5340f8829a4aSRandall Stewart 		/* clean up */
5341f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
5342f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
5343f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
5344f8829a4aSRandall Stewart 		    (asoc->locked_on_sending)
5345f8829a4aSRandall Stewart 		    ) {
5346f8829a4aSRandall Stewart 			struct sctp_stream_queue_pending *sp;
5347f8829a4aSRandall Stewart 
5348f8829a4aSRandall Stewart 			/*
5349f8829a4aSRandall Stewart 			 * I may be in a state where we got all across.. but
5350f8829a4aSRandall Stewart 			 * cannot write more due to a shutdown... we abort
5351f8829a4aSRandall Stewart 			 * since the user did not indicate EOR in this case.
5352f8829a4aSRandall Stewart 			 */
5353f8829a4aSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
5354f8829a4aSRandall Stewart 			    sctp_streamhead);
53552afb3e84SRandall Stewart 			if ((sp) && (sp->length == 0)) {
5356f8829a4aSRandall Stewart 				asoc->locked_on_sending = NULL;
53572afb3e84SRandall Stewart 				if (sp->msg_is_complete) {
5358f8829a4aSRandall Stewart 					asoc->stream_queue_cnt--;
53592afb3e84SRandall Stewart 				} else {
53602afb3e84SRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
53612afb3e84SRandall Stewart 					asoc->stream_queue_cnt--;
53622afb3e84SRandall Stewart 				}
5363f8829a4aSRandall Stewart 			}
5364f8829a4aSRandall Stewart 		}
5365f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
5366f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
5367f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
5368f8829a4aSRandall Stewart 				/* Need to abort here */
5369f8829a4aSRandall Stewart 				struct mbuf *oper;
5370f8829a4aSRandall Stewart 
5371f8829a4aSRandall Stewart 		abort_out_now:
5372f8829a4aSRandall Stewart 				*abort_now = 1;
5373f8829a4aSRandall Stewart 				/* XXX */
5374f8829a4aSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
5375f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
5376f8829a4aSRandall Stewart 				if (oper) {
5377f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
5378f8829a4aSRandall Stewart 					uint32_t *ippp;
5379f8829a4aSRandall Stewart 
5380139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
5381f8829a4aSRandall Stewart 					    sizeof(uint32_t);
5382f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
5383f8829a4aSRandall Stewart 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
5384139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
5385f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
5386a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_31);
5387f8829a4aSRandall Stewart 				}
5388a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
5389ceaad40aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
5390f8829a4aSRandall Stewart 				return;
5391f8829a4aSRandall Stewart 			} else {
5392f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
5393f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
5394f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5395f42a358aSRandall Stewart 				}
5396c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
5397b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
5398f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
5399f8829a4aSRandall Stewart 				sctp_send_shutdown(stcb,
5400f8829a4aSRandall Stewart 				    stcb->asoc.primary_destination);
5401f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
5402f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
5403f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
5404f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
5405f8829a4aSRandall Stewart 			}
5406f8829a4aSRandall Stewart 			return;
5407f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
5408f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
5409f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
5410f8829a4aSRandall Stewart 				goto abort_out_now;
5411f8829a4aSRandall Stewart 			}
5412f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5413c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
5414b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
5415f8829a4aSRandall Stewart 			sctp_send_shutdown_ack(stcb,
5416f8829a4aSRandall Stewart 			    stcb->asoc.primary_destination);
5417f8829a4aSRandall Stewart 
5418f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
5419f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb, asoc->primary_destination);
5420f8829a4aSRandall Stewart 			return;
5421f8829a4aSRandall Stewart 		}
5422f8829a4aSRandall Stewart 	}
5423f8829a4aSRandall Stewart 	/*
5424f8829a4aSRandall Stewart 	 * Now here we are going to recycle net_ack for a different use...
5425f8829a4aSRandall Stewart 	 * HEADS UP.
5426f8829a4aSRandall Stewart 	 */
5427f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5428f8829a4aSRandall Stewart 		net->net_ack = 0;
5429f8829a4aSRandall Stewart 	}
5430f8829a4aSRandall Stewart 
5431f8829a4aSRandall Stewart 	/*
5432f8829a4aSRandall Stewart 	 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
5433f8829a4aSRandall Stewart 	 * to be done. Setting this_sack_lowest_newack to the cum_ack will
5434f8829a4aSRandall Stewart 	 * automatically ensure that.
5435f8829a4aSRandall Stewart 	 */
5436b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac) && (cmt_dac_flag == 0)) {
5437f8829a4aSRandall Stewart 		this_sack_lowest_newack = cum_ack;
5438f8829a4aSRandall Stewart 	}
5439f8829a4aSRandall Stewart 	if (num_seg > 0) {
5440f8829a4aSRandall Stewart 		sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
5441f8829a4aSRandall Stewart 		    biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
5442f8829a4aSRandall Stewart 	}
5443b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
5444b54d3a6cSRandall Stewart 	asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
5445f8829a4aSRandall Stewart 
5446f8829a4aSRandall Stewart 	/******************************************************************
5447f8829a4aSRandall Stewart 	 *  Here we do the stuff with ECN Nonce checking.
5448f8829a4aSRandall Stewart 	 *  We basically check to see if the nonce sum flag was incorrect
5449f8829a4aSRandall Stewart 	 *  or if resynchronization needs to be done. Also if we catch a
5450f8829a4aSRandall Stewart 	 *  misbehaving receiver we give him the kick.
5451f8829a4aSRandall Stewart 	 ******************************************************************/
5452f8829a4aSRandall Stewart 
5453f8829a4aSRandall Stewart 	if (asoc->ecn_nonce_allowed) {
5454f8829a4aSRandall Stewart 		if (asoc->nonce_sum_check) {
5455f8829a4aSRandall Stewart 			if (nonce_sum_flag != ((asoc->nonce_sum_expect_base + ecn_seg_sums) & SCTP_SACK_NONCE_SUM)) {
5456f8829a4aSRandall Stewart 				if (asoc->nonce_wait_for_ecne == 0) {
5457f8829a4aSRandall Stewart 					struct sctp_tmit_chunk *lchk;
5458f8829a4aSRandall Stewart 
5459f8829a4aSRandall Stewart 					lchk = TAILQ_FIRST(&asoc->send_queue);
5460f8829a4aSRandall Stewart 					asoc->nonce_wait_for_ecne = 1;
5461f8829a4aSRandall Stewart 					if (lchk) {
5462f8829a4aSRandall Stewart 						asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
5463f8829a4aSRandall Stewart 					} else {
5464f8829a4aSRandall Stewart 						asoc->nonce_wait_tsn = asoc->sending_seq;
5465f8829a4aSRandall Stewart 					}
5466f8829a4aSRandall Stewart 				} else {
5467f8829a4aSRandall Stewart 					if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
5468f8829a4aSRandall Stewart 					    (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
5469f8829a4aSRandall Stewart 						/*
5470f8829a4aSRandall Stewart 						 * Misbehaving peer. We need
5471f8829a4aSRandall Stewart 						 * to react to this guy
5472f8829a4aSRandall Stewart 						 */
5473f8829a4aSRandall Stewart 						asoc->ecn_allowed = 0;
5474f8829a4aSRandall Stewart 						asoc->ecn_nonce_allowed = 0;
5475f8829a4aSRandall Stewart 					}
5476f8829a4aSRandall Stewart 				}
5477f8829a4aSRandall Stewart 			}
5478f8829a4aSRandall Stewart 		} else {
5479f8829a4aSRandall Stewart 			/* See if Resynchronization Possible */
5480f8829a4aSRandall Stewart 			if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
5481f8829a4aSRandall Stewart 				asoc->nonce_sum_check = 1;
5482f8829a4aSRandall Stewart 				/*
5483f8829a4aSRandall Stewart 				 * now we must calculate what the base is.
5484f8829a4aSRandall Stewart 				 * We do this based on two things, we know
5485f8829a4aSRandall Stewart 				 * the total's for all the segments
5486f8829a4aSRandall Stewart 				 * gap-acked in the SACK, its stored in
5487f8829a4aSRandall Stewart 				 * ecn_seg_sums. We also know the SACK's
5488f8829a4aSRandall Stewart 				 * nonce sum, its in nonce_sum_flag. So we
5489f8829a4aSRandall Stewart 				 * can build a truth table to back-calculate
5490f8829a4aSRandall Stewart 				 * the new value of
5491f8829a4aSRandall Stewart 				 * asoc->nonce_sum_expect_base:
5492f8829a4aSRandall Stewart 				 *
5493f8829a4aSRandall Stewart 				 * SACK-flag-Value         Seg-Sums Base 0 0 0
5494f8829a4aSRandall Stewart 				 * 1                    0 1 0 1 1 1
5495f8829a4aSRandall Stewart 				 * 1 0
5496f8829a4aSRandall Stewart 				 */
5497f8829a4aSRandall Stewart 				asoc->nonce_sum_expect_base = (ecn_seg_sums ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
5498f8829a4aSRandall Stewart 			}
5499f8829a4aSRandall Stewart 		}
5500f8829a4aSRandall Stewart 	}
5501f8829a4aSRandall Stewart 	/* Now are we exiting loss recovery ? */
5502f8829a4aSRandall Stewart 	if (will_exit_fast_recovery) {
5503f8829a4aSRandall Stewart 		/* Ok, we must exit fast recovery */
5504f8829a4aSRandall Stewart 		asoc->fast_retran_loss_recovery = 0;
5505f8829a4aSRandall Stewart 	}
5506f8829a4aSRandall Stewart 	if ((asoc->sat_t3_loss_recovery) &&
5507f8829a4aSRandall Stewart 	    ((compare_with_wrap(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn,
5508f8829a4aSRandall Stewart 	    MAX_TSN) ||
5509f8829a4aSRandall Stewart 	    (asoc->last_acked_seq == asoc->sat_t3_recovery_tsn)))) {
5510f8829a4aSRandall Stewart 		/* end satellite t3 loss recovery */
5511f8829a4aSRandall Stewart 		asoc->sat_t3_loss_recovery = 0;
5512f8829a4aSRandall Stewart 	}
551342551e99SRandall Stewart 	/*
551442551e99SRandall Stewart 	 * CMT Fast recovery
551542551e99SRandall Stewart 	 */
5516f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5517f8829a4aSRandall Stewart 		if (net->will_exit_fast_recovery) {
5518f8829a4aSRandall Stewart 			/* Ok, we must exit fast recovery */
5519f8829a4aSRandall Stewart 			net->fast_retran_loss_recovery = 0;
5520f8829a4aSRandall Stewart 		}
5521f8829a4aSRandall Stewart 	}
5522f8829a4aSRandall Stewart 
5523f8829a4aSRandall Stewart 	/* Adjust and set the new rwnd value */
5524b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
5525f8829a4aSRandall Stewart 		sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
5526b3f1ea41SRandall Stewart 		    asoc->peers_rwnd, asoc->total_flight, (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)), a_rwnd);
552780fefe0aSRandall Stewart 	}
5528f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
5529b3f1ea41SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
5530f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5531f8829a4aSRandall Stewart 		/* SWS sender side engages */
5532f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
5533f8829a4aSRandall Stewart 	}
55345e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
55355e54f665SRandall Stewart 		win_probe_recovery = 1;
55365e54f665SRandall Stewart 	}
5537f8829a4aSRandall Stewart 	/*
5538f8829a4aSRandall Stewart 	 * Now we must setup so we have a timer up for anyone with
5539f8829a4aSRandall Stewart 	 * outstanding data.
5540f8829a4aSRandall Stewart 	 */
5541bff64a4dSRandall Stewart 	done_once = 0;
5542a5d547adSRandall Stewart again:
5543a5d547adSRandall Stewart 	j = 0;
5544f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
55455e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
5546c105859eSRandall Stewart 			win_probe_recovered = 1;
55475e54f665SRandall Stewart 			/*-
55485e54f665SRandall Stewart 			 * Find first chunk that was used with
55495e54f665SRandall Stewart 			 * window probe and clear the event. Put
55505e54f665SRandall Stewart 			 * it back into the send queue as if has
55515e54f665SRandall Stewart 			 * not been sent.
55525e54f665SRandall Stewart 			 */
55535e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
55545e54f665SRandall Stewart 				if (tp1->window_probe) {
5555c105859eSRandall Stewart 					sctp_window_probe_recovery(stcb, asoc, net, tp1);
55565e54f665SRandall Stewart 					break;
55575e54f665SRandall Stewart 				}
55585e54f665SRandall Stewart 			}
55595e54f665SRandall Stewart 		}
5560f8829a4aSRandall Stewart 		if (net->flight_size) {
5561a5d547adSRandall Stewart 			j++;
5562f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5563f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb, net);
55645171328bSRandall Stewart 			if (net->window_probe) {
55655171328bSRandall Stewart 			}
5566c105859eSRandall Stewart 		} else {
55675171328bSRandall Stewart 			if (net->window_probe) {
55685171328bSRandall Stewart 				/*
55695171328bSRandall Stewart 				 * In window probes we must assure a timer
55705171328bSRandall Stewart 				 * is still running there
55715171328bSRandall Stewart 				 */
55725171328bSRandall Stewart 
55735171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
55745171328bSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_SEND,
55755171328bSRandall Stewart 					    stcb->sctp_ep, stcb, net);
55765171328bSRandall Stewart 
55775171328bSRandall Stewart 				}
55785171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5579c105859eSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5580c105859eSRandall Stewart 				    stcb, net,
5581c105859eSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
5582c105859eSRandall Stewart 			}
5583b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
5584c105859eSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
5585c105859eSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
5586c105859eSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
5587c105859eSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
5588c105859eSRandall Stewart 				}
5589c105859eSRandall Stewart 			}
5590f8829a4aSRandall Stewart 		}
5591f8829a4aSRandall Stewart 	}
5592bff64a4dSRandall Stewart 	if ((j == 0) &&
5593bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
5594bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
5595c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
5596bff64a4dSRandall Stewart 	    (done_once == 0)) {
55970c0982b8SRandall Stewart 		/*
55980c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
55990c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
56000c0982b8SRandall Stewart 		 */
56010c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
5602a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5603a5d547adSRandall Stewart 				net->flight_size = 0;
5604a5d547adSRandall Stewart 			}
5605a5d547adSRandall Stewart 			asoc->total_flight = 0;
5606a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
5607a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
5608a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5609a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5610c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
5611c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
5612a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5613a5d547adSRandall Stewart 					asoc->sent_queue_retran_cnt++;
5614a5d547adSRandall Stewart 				}
5615a5d547adSRandall Stewart 			}
56160c0982b8SRandall Stewart 		}
5617bff64a4dSRandall Stewart 		done_once = 1;
5618a5d547adSRandall Stewart 		goto again;
5619a5d547adSRandall Stewart 	}
5620dfb11ef8SRandall Stewart 	/* Fix up the a-p-a-p for future PR-SCTP sends */
5621dfb11ef8SRandall Stewart 	if (compare_with_wrap(cum_ack, asoc->advanced_peer_ack_point, MAX_TSN)) {
5622dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cum_ack;
5623dfb11ef8SRandall Stewart 	}
5624830d754dSRandall Stewart 	/* C2. try to further move advancedPeerAckPoint ahead */
5625830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
5626830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
5627830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
5628830d754dSRandall Stewart 
5629830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
5630830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
5631830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
5632830d754dSRandall Stewart 		if (compare_with_wrap(asoc->advanced_peer_ack_point, cum_ack,
5633830d754dSRandall Stewart 		    MAX_TSN)) {
5634830d754dSRandall Stewart 			/*
5635830d754dSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing for notes
5636830d754dSRandall Stewart 			 * on issues that will occur when the ECN NONCE
5637830d754dSRandall Stewart 			 * stuff is put into SCTP for cross checking.
5638830d754dSRandall Stewart 			 */
56390c0982b8SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
56400c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
56410c0982b8SRandall Stewart 				    0xee, cum_ack, asoc->advanced_peer_ack_point,
56420c0982b8SRandall Stewart 				    old_adv_peer_ack_point);
56430c0982b8SRandall Stewart 			}
5644830d754dSRandall Stewart 			if (compare_with_wrap(asoc->advanced_peer_ack_point, old_adv_peer_ack_point,
5645830d754dSRandall Stewart 			    MAX_TSN)) {
5646830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
5647830d754dSRandall Stewart 				/*
5648830d754dSRandall Stewart 				 * ECN Nonce: Disable Nonce Sum check when
5649830d754dSRandall Stewart 				 * FWD TSN is sent and store resync tsn
5650830d754dSRandall Stewart 				 */
5651830d754dSRandall Stewart 				asoc->nonce_sum_check = 0;
5652830d754dSRandall Stewart 				asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
56530c0982b8SRandall Stewart 			} else if (lchk) {
56540c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
56550c0982b8SRandall Stewart 				lchk->rec.data.fwd_tsn_cnt++;
56560c0982b8SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt > 3) {
56570c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
56580c0982b8SRandall Stewart 					lchk->rec.data.fwd_tsn_cnt = 0;
56590c0982b8SRandall Stewart 				}
5660830d754dSRandall Stewart 			}
5661830d754dSRandall Stewart 		}
5662830d754dSRandall Stewart 		if (lchk) {
5663830d754dSRandall Stewart 			/* Assure a timer is up */
5664830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5665830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
5666830d754dSRandall Stewart 		}
5667830d754dSRandall Stewart 	}
5668b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
5669f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
5670f8829a4aSRandall Stewart 		    a_rwnd,
5671f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
5672f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
5673f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
567480fefe0aSRandall Stewart 	}
5675f8829a4aSRandall Stewart }
5676f8829a4aSRandall Stewart 
5677f8829a4aSRandall Stewart void
5678f8829a4aSRandall Stewart sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp,
5679f8829a4aSRandall Stewart     struct sctp_nets *netp, int *abort_flag)
5680f8829a4aSRandall Stewart {
5681f8829a4aSRandall Stewart 	/* Copy cum-ack */
5682f8829a4aSRandall Stewart 	uint32_t cum_ack, a_rwnd;
5683f8829a4aSRandall Stewart 
5684f8829a4aSRandall Stewart 	cum_ack = ntohl(cp->cumulative_tsn_ack);
5685f8829a4aSRandall Stewart 	/* Arrange so a_rwnd does NOT change */
5686f8829a4aSRandall Stewart 	a_rwnd = stcb->asoc.peers_rwnd + stcb->asoc.total_flight;
5687f8829a4aSRandall Stewart 
5688f8829a4aSRandall Stewart 	/* Now call the express sack handling */
5689f8829a4aSRandall Stewart 	sctp_express_handle_sack(stcb, cum_ack, a_rwnd, 0, abort_flag);
5690f8829a4aSRandall Stewart }
5691f8829a4aSRandall Stewart 
5692f8829a4aSRandall Stewart static void
5693f8829a4aSRandall Stewart sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
5694f8829a4aSRandall Stewart     struct sctp_stream_in *strmin)
5695f8829a4aSRandall Stewart {
5696f8829a4aSRandall Stewart 	struct sctp_queued_to_read *ctl, *nctl;
5697f8829a4aSRandall Stewart 	struct sctp_association *asoc;
5698f8829a4aSRandall Stewart 	int tt;
5699f8829a4aSRandall Stewart 
5700830d754dSRandall Stewart 	/* EY -used to calculate nr_gap information */
5701830d754dSRandall Stewart 	uint32_t nr_tsn, nr_gap;
5702830d754dSRandall Stewart 
5703f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
5704f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered;
5705f8829a4aSRandall Stewart 	/*
5706f8829a4aSRandall Stewart 	 * First deliver anything prior to and including the stream no that
5707f8829a4aSRandall Stewart 	 * came in
5708f8829a4aSRandall Stewart 	 */
5709f8829a4aSRandall Stewart 	ctl = TAILQ_FIRST(&strmin->inqueue);
5710f8829a4aSRandall Stewart 	while (ctl) {
5711f8829a4aSRandall Stewart 		nctl = TAILQ_NEXT(ctl, next);
5712f8829a4aSRandall Stewart 		if (compare_with_wrap(tt, ctl->sinfo_ssn, MAX_SEQ) ||
5713f8829a4aSRandall Stewart 		    (tt == ctl->sinfo_ssn)) {
5714f8829a4aSRandall Stewart 			/* this is deliverable now */
5715f8829a4aSRandall Stewart 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5716f8829a4aSRandall Stewart 			/* subtract pending on streams */
5717f8829a4aSRandall Stewart 			asoc->size_on_all_streams -= ctl->length;
5718f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_all_streams);
5719f8829a4aSRandall Stewart 			/* deliver it to at least the delivery-q */
5720f8829a4aSRandall Stewart 			if (stcb->sctp_socket) {
5721830d754dSRandall Stewart 				/* EY need the tsn info for calculating nr */
5722830d754dSRandall Stewart 				nr_tsn = ctl->sinfo_tsn;
5723f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
5724f8829a4aSRandall Stewart 				    ctl,
5725ceaad40aSRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
5726830d754dSRandall Stewart 				/*
5727830d754dSRandall Stewart 				 * EY this is the chunk that should be
5728830d754dSRandall Stewart 				 * tagged nr gapped calculate the gap and
5729830d754dSRandall Stewart 				 * such then tag this TSN nr
5730830d754dSRandall Stewart 				 * chk->rec.data.TSN_seq
5731830d754dSRandall Stewart 				 */
5732830d754dSRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
5733830d754dSRandall Stewart 
5734d50c1d79SRandall Stewart 					SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
5735830d754dSRandall Stewart 					if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
5736830d754dSRandall Stewart 					    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
5737830d754dSRandall Stewart 						/*
5738830d754dSRandall Stewart 						 * EY These should never
5739830d754dSRandall Stewart 						 * happen- explained before
5740830d754dSRandall Stewart 						 */
5741830d754dSRandall Stewart 					} else {
5742830d754dSRandall Stewart 						SCTP_TCB_LOCK_ASSERT(stcb);
5743830d754dSRandall Stewart 						SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
5744d50c1d79SRandall Stewart 						SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
57458933fa13SRandall Stewart 						if (compare_with_wrap(nr_tsn,
57468933fa13SRandall Stewart 						    asoc->highest_tsn_inside_nr_map,
57478933fa13SRandall Stewart 						    MAX_TSN))
5748830d754dSRandall Stewart 							asoc->highest_tsn_inside_nr_map = nr_tsn;
5749830d754dSRandall Stewart 					}
5750830d754dSRandall Stewart 					if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, nr_gap))
5751830d754dSRandall Stewart 						/*
5752830d754dSRandall Stewart 						 * printf("In
5753830d754dSRandall Stewart 						 * sctp_kick_prsctp_reorder_q
5754830d754dSRandall Stewart 						 * ueue(7): Something wrong,
5755830d754dSRandall Stewart 						 * the TSN to be tagged"
5756830d754dSRandall Stewart 						 * "\nas NR is not even in
5757830d754dSRandall Stewart 						 * the mapping_array, or map
5758830d754dSRandall Stewart 						 * and nr_map are
5759830d754dSRandall Stewart 						 * inconsistent");
5760830d754dSRandall Stewart 						 */
5761830d754dSRandall Stewart 						/*
5762830d754dSRandall Stewart 						 * EY - not %100 sure about
5763830d754dSRandall Stewart 						 * the lock thing, don't
5764830d754dSRandall Stewart 						 * think its required
5765830d754dSRandall Stewart 						 */
5766830d754dSRandall Stewart 						/*
5767830d754dSRandall Stewart 						 * SCTP_TCB_LOCK_ASSERT(stcb)
5768830d754dSRandall Stewart 						 * ;
5769830d754dSRandall Stewart 						 */
5770830d754dSRandall Stewart 					{
5771830d754dSRandall Stewart 						/*
5772830d754dSRandall Stewart 						 * printf("\nCalculating an
5773830d754dSRandall Stewart 						 * nr_gap!!\nmapping_array_si
5774830d754dSRandall Stewart 						 * ze = %d
5775830d754dSRandall Stewart 						 * nr_mapping_array_size =
5776830d754dSRandall Stewart 						 * %d" "\nmapping_array_base
5777830d754dSRandall Stewart 						 * = %d
5778830d754dSRandall Stewart 						 * nr_mapping_array_base =
5779830d754dSRandall Stewart 						 * %d\nhighest_tsn_inside_map
5780830d754dSRandall Stewart 						 *  = %d"
5781830d754dSRandall Stewart 						 * "highest_tsn_inside_nr_map
5782830d754dSRandall Stewart 						 *  = %d\nTSN = %d nr_gap =
5783830d754dSRandall Stewart 						 * %d",asoc->mapping_array_si
5784830d754dSRandall Stewart 						 * ze,
5785830d754dSRandall Stewart 						 * asoc->nr_mapping_array_siz
5786830d754dSRandall Stewart 						 * e,
5787830d754dSRandall Stewart 						 * asoc->mapping_array_base_t
5788830d754dSRandall Stewart 						 * sn,
5789830d754dSRandall Stewart 						 * asoc->nr_mapping_array_bas
5790830d754dSRandall Stewart 						 * e_tsn,
5791830d754dSRandall Stewart 						 * asoc->highest_tsn_inside_m
5792830d754dSRandall Stewart 						 * ap,
5793830d754dSRandall Stewart 						 * asoc->highest_tsn_inside_n
5794830d754dSRandall Stewart 						 * r_map,tsn,nr_gap);
5795830d754dSRandall Stewart 						 */
5796830d754dSRandall Stewart 					}
5797830d754dSRandall Stewart 				}
5798f8829a4aSRandall Stewart 			}
5799f8829a4aSRandall Stewart 		} else {
5800f8829a4aSRandall Stewart 			/* no more delivery now. */
5801f8829a4aSRandall Stewart 			break;
5802f8829a4aSRandall Stewart 		}
5803f8829a4aSRandall Stewart 		ctl = nctl;
5804f8829a4aSRandall Stewart 	}
5805f8829a4aSRandall Stewart 	/*
5806f8829a4aSRandall Stewart 	 * now we must deliver things in queue the normal way  if any are
5807f8829a4aSRandall Stewart 	 * now ready.
5808f8829a4aSRandall Stewart 	 */
5809f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered + 1;
5810f8829a4aSRandall Stewart 	ctl = TAILQ_FIRST(&strmin->inqueue);
5811f8829a4aSRandall Stewart 	while (ctl) {
5812f8829a4aSRandall Stewart 		nctl = TAILQ_NEXT(ctl, next);
5813f8829a4aSRandall Stewart 		if (tt == ctl->sinfo_ssn) {
5814f8829a4aSRandall Stewart 			/* this is deliverable now */
5815f8829a4aSRandall Stewart 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5816f8829a4aSRandall Stewart 			/* subtract pending on streams */
5817f8829a4aSRandall Stewart 			asoc->size_on_all_streams -= ctl->length;
5818f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_all_streams);
5819f8829a4aSRandall Stewart 			/* deliver it to at least the delivery-q */
5820f8829a4aSRandall Stewart 			strmin->last_sequence_delivered = ctl->sinfo_ssn;
5821f8829a4aSRandall Stewart 			if (stcb->sctp_socket) {
5822830d754dSRandall Stewart 				/* EY */
5823830d754dSRandall Stewart 				nr_tsn = ctl->sinfo_tsn;
5824f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
5825f8829a4aSRandall Stewart 				    ctl,
5826ceaad40aSRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
5827830d754dSRandall Stewart 				/*
5828830d754dSRandall Stewart 				 * EY this is the chunk that should be
5829830d754dSRandall Stewart 				 * tagged nr gapped calculate the gap and
5830830d754dSRandall Stewart 				 * such then tag this TSN nr
5831830d754dSRandall Stewart 				 * chk->rec.data.TSN_seq
5832830d754dSRandall Stewart 				 */
5833830d754dSRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
5834d50c1d79SRandall Stewart 					SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
5835830d754dSRandall Stewart 					if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
5836830d754dSRandall Stewart 					    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
5837830d754dSRandall Stewart 						/*
5838830d754dSRandall Stewart 						 * EY These should never
5839830d754dSRandall Stewart 						 * happen, explained before
5840830d754dSRandall Stewart 						 */
5841830d754dSRandall Stewart 					} else {
5842830d754dSRandall Stewart 						SCTP_TCB_LOCK_ASSERT(stcb);
5843830d754dSRandall Stewart 						SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
5844d50c1d79SRandall Stewart 						SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
58458933fa13SRandall Stewart 						if (compare_with_wrap(nr_tsn, asoc->highest_tsn_inside_nr_map,
58468933fa13SRandall Stewart 						    MAX_TSN))
5847830d754dSRandall Stewart 							asoc->highest_tsn_inside_nr_map = nr_tsn;
5848830d754dSRandall Stewart 					}
5849830d754dSRandall Stewart 					if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, nr_gap))
5850830d754dSRandall Stewart 						/*
5851830d754dSRandall Stewart 						 * printf("In
5852830d754dSRandall Stewart 						 * sctp_kick_prsctp_reorder_q
5853830d754dSRandall Stewart 						 * ueue(8): Something wrong,
5854830d754dSRandall Stewart 						 * the TSN to be tagged"
5855830d754dSRandall Stewart 						 * "\nas NR is not even in
5856830d754dSRandall Stewart 						 * the mapping_array, or map
5857830d754dSRandall Stewart 						 * and nr_map are
5858830d754dSRandall Stewart 						 * inconsistent");
5859830d754dSRandall Stewart 						 */
5860830d754dSRandall Stewart 						/*
5861830d754dSRandall Stewart 						 * EY - not %100 sure about
5862830d754dSRandall Stewart 						 * the lock thing, don't
5863830d754dSRandall Stewart 						 * think its required
5864830d754dSRandall Stewart 						 */
5865830d754dSRandall Stewart 						/*
5866830d754dSRandall Stewart 						 * SCTP_TCB_LOCK_ASSERT(stcb)
5867830d754dSRandall Stewart 						 * ;
5868830d754dSRandall Stewart 						 */
5869830d754dSRandall Stewart 					{
5870830d754dSRandall Stewart 						/*
5871830d754dSRandall Stewart 						 * printf("\nCalculating an
5872830d754dSRandall Stewart 						 * nr_gap!!\nmapping_array_si
5873830d754dSRandall Stewart 						 * ze = %d
5874830d754dSRandall Stewart 						 * nr_mapping_array_size =
5875830d754dSRandall Stewart 						 * %d" "\nmapping_array_base
5876830d754dSRandall Stewart 						 * = %d
5877830d754dSRandall Stewart 						 * nr_mapping_array_base =
5878830d754dSRandall Stewart 						 * %d\nhighest_tsn_inside_map
5879830d754dSRandall Stewart 						 *  = %d"
5880830d754dSRandall Stewart 						 * "highest_tsn_inside_nr_map
5881830d754dSRandall Stewart 						 *  = %d\nTSN = %d nr_gap =
5882830d754dSRandall Stewart 						 * %d",asoc->mapping_array_si
5883830d754dSRandall Stewart 						 * ze,
5884830d754dSRandall Stewart 						 * asoc->nr_mapping_array_siz
5885830d754dSRandall Stewart 						 * e,
5886830d754dSRandall Stewart 						 * asoc->mapping_array_base_t
5887830d754dSRandall Stewart 						 * sn,
5888830d754dSRandall Stewart 						 * asoc->nr_mapping_array_bas
5889830d754dSRandall Stewart 						 * e_tsn,
5890830d754dSRandall Stewart 						 * asoc->highest_tsn_inside_m
5891830d754dSRandall Stewart 						 * ap,
5892830d754dSRandall Stewart 						 * asoc->highest_tsn_inside_n
5893830d754dSRandall Stewart 						 * r_map,tsn,nr_gap);
5894830d754dSRandall Stewart 						 */
5895830d754dSRandall Stewart 					}
5896830d754dSRandall Stewart 				}
5897f8829a4aSRandall Stewart 			}
5898f8829a4aSRandall Stewart 			tt = strmin->last_sequence_delivered + 1;
5899f8829a4aSRandall Stewart 		} else {
5900f8829a4aSRandall Stewart 			break;
5901f8829a4aSRandall Stewart 		}
5902f8829a4aSRandall Stewart 		ctl = nctl;
5903f8829a4aSRandall Stewart 	}
5904f8829a4aSRandall Stewart }
5905f8829a4aSRandall Stewart 
59068933fa13SRandall Stewart static void
59078933fa13SRandall Stewart sctp_flush_reassm_for_str_seq(struct sctp_tcb *stcb,
59088933fa13SRandall Stewart     struct sctp_association *asoc,
59098933fa13SRandall Stewart     uint16_t stream, uint16_t seq)
59108933fa13SRandall Stewart {
59118933fa13SRandall Stewart 	struct sctp_tmit_chunk *chk, *at;
59128933fa13SRandall Stewart 
59138933fa13SRandall Stewart 	if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
59148933fa13SRandall Stewart 		/* For each one on here see if we need to toss it */
59158933fa13SRandall Stewart 		/*
59168933fa13SRandall Stewart 		 * For now large messages held on the reasmqueue that are
59178933fa13SRandall Stewart 		 * complete will be tossed too. We could in theory do more
59188933fa13SRandall Stewart 		 * work to spin through and stop after dumping one msg aka
59198933fa13SRandall Stewart 		 * seeing the start of a new msg at the head, and call the
59208933fa13SRandall Stewart 		 * delivery function... to see if it can be delivered... But
59218933fa13SRandall Stewart 		 * for now we just dump everything on the queue.
59228933fa13SRandall Stewart 		 */
59238933fa13SRandall Stewart 		chk = TAILQ_FIRST(&asoc->reasmqueue);
59248933fa13SRandall Stewart 		while (chk) {
59258933fa13SRandall Stewart 			at = TAILQ_NEXT(chk, sctp_next);
59268933fa13SRandall Stewart 			if (chk->rec.data.stream_number != stream) {
59278933fa13SRandall Stewart 				chk = at;
59288933fa13SRandall Stewart 				continue;
59298933fa13SRandall Stewart 			}
59308933fa13SRandall Stewart 			if (chk->rec.data.stream_seq == seq) {
59318933fa13SRandall Stewart 				/* It needs to be tossed */
59328933fa13SRandall Stewart 				TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
59338933fa13SRandall Stewart 				if (compare_with_wrap(chk->rec.data.TSN_seq,
59348933fa13SRandall Stewart 				    asoc->tsn_last_delivered, MAX_TSN)) {
59358933fa13SRandall Stewart 					asoc->tsn_last_delivered =
59368933fa13SRandall Stewart 					    chk->rec.data.TSN_seq;
59378933fa13SRandall Stewart 					asoc->str_of_pdapi =
59388933fa13SRandall Stewart 					    chk->rec.data.stream_number;
59398933fa13SRandall Stewart 					asoc->ssn_of_pdapi =
59408933fa13SRandall Stewart 					    chk->rec.data.stream_seq;
59418933fa13SRandall Stewart 					asoc->fragment_flags =
59428933fa13SRandall Stewart 					    chk->rec.data.rcv_flags;
59438933fa13SRandall Stewart 				}
59448933fa13SRandall Stewart 				asoc->size_on_reasm_queue -= chk->send_size;
59458933fa13SRandall Stewart 				sctp_ucount_decr(asoc->cnt_on_reasm_queue);
59468933fa13SRandall Stewart 
59478933fa13SRandall Stewart 				/* Clear up any stream problem */
59488933fa13SRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) !=
59498933fa13SRandall Stewart 				    SCTP_DATA_UNORDERED &&
59508933fa13SRandall Stewart 				    (compare_with_wrap(chk->rec.data.stream_seq,
59518933fa13SRandall Stewart 				    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered,
59528933fa13SRandall Stewart 				    MAX_SEQ))) {
59538933fa13SRandall Stewart 					/*
59548933fa13SRandall Stewart 					 * We must dump forward this streams
59558933fa13SRandall Stewart 					 * sequence number if the chunk is
59568933fa13SRandall Stewart 					 * not unordered that is being
59578933fa13SRandall Stewart 					 * skipped. There is a chance that
59588933fa13SRandall Stewart 					 * if the peer does not include the
59598933fa13SRandall Stewart 					 * last fragment in its FWD-TSN we
59608933fa13SRandall Stewart 					 * WILL have a problem here since
59618933fa13SRandall Stewart 					 * you would have a partial chunk in
59628933fa13SRandall Stewart 					 * queue that may not be
59638933fa13SRandall Stewart 					 * deliverable. Also if a Partial
59648933fa13SRandall Stewart 					 * delivery API as started the user
59658933fa13SRandall Stewart 					 * may get a partial chunk. The next
59668933fa13SRandall Stewart 					 * read returning a new chunk...
59678933fa13SRandall Stewart 					 * really ugly but I see no way
59688933fa13SRandall Stewart 					 * around it! Maybe a notify??
59698933fa13SRandall Stewart 					 */
59708933fa13SRandall Stewart 					asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered =
59718933fa13SRandall Stewart 					    chk->rec.data.stream_seq;
59728933fa13SRandall Stewart 				}
59738933fa13SRandall Stewart 				if (chk->data) {
59748933fa13SRandall Stewart 					sctp_m_freem(chk->data);
59758933fa13SRandall Stewart 					chk->data = NULL;
59768933fa13SRandall Stewart 				}
59778933fa13SRandall Stewart 				sctp_free_a_chunk(stcb, chk);
59788933fa13SRandall Stewart 			} else if (compare_with_wrap(chk->rec.data.stream_seq, seq, MAX_SEQ)) {
59798933fa13SRandall Stewart 				/*
59808933fa13SRandall Stewart 				 * If the stream_seq is > than the purging
59818933fa13SRandall Stewart 				 * one, we are done
59828933fa13SRandall Stewart 				 */
59838933fa13SRandall Stewart 				break;
59848933fa13SRandall Stewart 			}
59858933fa13SRandall Stewart 			chk = at;
59868933fa13SRandall Stewart 		}
59878933fa13SRandall Stewart 	}
59888933fa13SRandall Stewart }
59898933fa13SRandall Stewart 
59908933fa13SRandall Stewart 
5991f8829a4aSRandall Stewart void
5992f8829a4aSRandall Stewart sctp_handle_forward_tsn(struct sctp_tcb *stcb,
5993671d309cSRandall Stewart     struct sctp_forward_tsn_chunk *fwd, int *abort_flag, struct mbuf *m, int offset)
5994f8829a4aSRandall Stewart {
5995f8829a4aSRandall Stewart 	/*
5996f8829a4aSRandall Stewart 	 * ISSUES that MUST be fixed for ECN! When we are the sender of the
5997f8829a4aSRandall Stewart 	 * forward TSN, when the SACK comes back that acknowledges the
5998f8829a4aSRandall Stewart 	 * FWD-TSN we must reset the NONCE sum to match correctly. This will
5999f8829a4aSRandall Stewart 	 * get quite tricky since we may have sent more data interveneing
6000f8829a4aSRandall Stewart 	 * and must carefully account for what the SACK says on the nonce
6001f8829a4aSRandall Stewart 	 * and any gaps that are reported. This work will NOT be done here,
6002f8829a4aSRandall Stewart 	 * but I note it here since it is really related to PR-SCTP and
6003f8829a4aSRandall Stewart 	 * FWD-TSN's
6004f8829a4aSRandall Stewart 	 */
6005f8829a4aSRandall Stewart 
6006f8829a4aSRandall Stewart 	/* The pr-sctp fwd tsn */
6007f8829a4aSRandall Stewart 	/*
6008f8829a4aSRandall Stewart 	 * here we will perform all the data receiver side steps for
6009f8829a4aSRandall Stewart 	 * processing FwdTSN, as required in by pr-sctp draft:
6010f8829a4aSRandall Stewart 	 *
6011f8829a4aSRandall Stewart 	 * Assume we get FwdTSN(x):
6012f8829a4aSRandall Stewart 	 *
6013f8829a4aSRandall Stewart 	 * 1) update local cumTSN to x 2) try to further advance cumTSN to x +
6014f8829a4aSRandall Stewart 	 * others we have 3) examine and update re-ordering queue on
6015f8829a4aSRandall Stewart 	 * pr-in-streams 4) clean up re-assembly queue 5) Send a sack to
6016f8829a4aSRandall Stewart 	 * report where we are.
6017f8829a4aSRandall Stewart 	 */
6018f8829a4aSRandall Stewart 	struct sctp_association *asoc;
60192afb3e84SRandall Stewart 	uint32_t new_cum_tsn, gap;
60208933fa13SRandall Stewart 	unsigned int i, fwd_sz, cumack_set_flag, m_size;
60218933fa13SRandall Stewart 	uint32_t str_seq;
6022f8829a4aSRandall Stewart 	struct sctp_stream_in *strm;
6023f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk, *at;
60248933fa13SRandall Stewart 	struct sctp_queued_to_read *ctl, *sv;
6025f8829a4aSRandall Stewart 
6026f8829a4aSRandall Stewart 	cumack_set_flag = 0;
6027f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
6028f8829a4aSRandall Stewart 	if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
6029ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1,
6030ad81507eSRandall Stewart 		    "Bad size too small/big fwd-tsn\n");
6031f8829a4aSRandall Stewart 		return;
6032f8829a4aSRandall Stewart 	}
6033f8829a4aSRandall Stewart 	m_size = (stcb->asoc.mapping_array_size << 3);
6034f8829a4aSRandall Stewart 	/*************************************************************/
6035f8829a4aSRandall Stewart 	/* 1. Here we update local cumTSN and shift the bitmap array */
6036f8829a4aSRandall Stewart 	/*************************************************************/
6037f8829a4aSRandall Stewart 	new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
6038f8829a4aSRandall Stewart 
6039f8829a4aSRandall Stewart 	if (compare_with_wrap(asoc->cumulative_tsn, new_cum_tsn, MAX_TSN) ||
6040f8829a4aSRandall Stewart 	    asoc->cumulative_tsn == new_cum_tsn) {
6041f8829a4aSRandall Stewart 		/* Already got there ... */
6042f8829a4aSRandall Stewart 		return;
6043f8829a4aSRandall Stewart 	}
6044f8829a4aSRandall Stewart 	if (compare_with_wrap(new_cum_tsn, asoc->highest_tsn_inside_map,
6045f8829a4aSRandall Stewart 	    MAX_TSN)) {
6046f8829a4aSRandall Stewart 		asoc->highest_tsn_inside_map = new_cum_tsn;
6047830d754dSRandall Stewart 		/* EY nr_mapping_array version of the above */
6048830d754dSRandall Stewart 		/*
6049830d754dSRandall Stewart 		 * if(SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) &&
6050830d754dSRandall Stewart 		 * asoc->peer_supports_nr_sack)
6051830d754dSRandall Stewart 		 */
6052830d754dSRandall Stewart 		asoc->highest_tsn_inside_nr_map = new_cum_tsn;
6053b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
6054f8829a4aSRandall Stewart 			sctp_log_map(0, 0, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
605580fefe0aSRandall Stewart 		}
6056f8829a4aSRandall Stewart 	}
6057f8829a4aSRandall Stewart 	/*
6058f8829a4aSRandall Stewart 	 * now we know the new TSN is more advanced, let's find the actual
6059f8829a4aSRandall Stewart 	 * gap
6060f8829a4aSRandall Stewart 	 */
6061d50c1d79SRandall Stewart 	SCTP_CALC_TSN_TO_GAP(gap, new_cum_tsn, asoc->mapping_array_base_tsn);
60622afb3e84SRandall Stewart 	if (gap >= m_size) {
6063b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
6064c4739e2fSRandall Stewart 			sctp_log_map(0, 0, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
6065c4739e2fSRandall Stewart 		}
6066f8829a4aSRandall Stewart 		if ((long)gap > sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv)) {
606717205eccSRandall Stewart 			struct mbuf *oper;
606817205eccSRandall Stewart 
6069f8829a4aSRandall Stewart 			/*
6070f8829a4aSRandall Stewart 			 * out of range (of single byte chunks in the rwnd I
607117205eccSRandall Stewart 			 * give out). This must be an attacker.
6072f8829a4aSRandall Stewart 			 */
607317205eccSRandall Stewart 			*abort_flag = 1;
607417205eccSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
607517205eccSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
607617205eccSRandall Stewart 			if (oper) {
607717205eccSRandall Stewart 				struct sctp_paramhdr *ph;
607817205eccSRandall Stewart 				uint32_t *ippp;
607917205eccSRandall Stewart 
608017205eccSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
608117205eccSRandall Stewart 				    (sizeof(uint32_t) * 3);
608217205eccSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
608317205eccSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
608417205eccSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
608517205eccSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
608617205eccSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_33);
608717205eccSRandall Stewart 				ippp++;
608817205eccSRandall Stewart 				*ippp = asoc->highest_tsn_inside_map;
608917205eccSRandall Stewart 				ippp++;
609017205eccSRandall Stewart 				*ippp = new_cum_tsn;
609117205eccSRandall Stewart 			}
609217205eccSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_33;
609317205eccSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb,
6094ceaad40aSRandall Stewart 			    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
6095f8829a4aSRandall Stewart 			return;
6096f8829a4aSRandall Stewart 		}
6097207304d4SRandall Stewart 		SCTP_STAT_INCR(sctps_fwdtsn_map_over);
60982afb3e84SRandall Stewart 		memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
6099f8829a4aSRandall Stewart 		cumack_set_flag = 1;
61002afb3e84SRandall Stewart 		asoc->mapping_array_base_tsn = new_cum_tsn + 1;
61012afb3e84SRandall Stewart 		asoc->cumulative_tsn = asoc->highest_tsn_inside_map = new_cum_tsn;
6102830d754dSRandall Stewart 		/* EY - nr_sack: nr_mapping_array version of the above */
6103830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
6104830d754dSRandall Stewart 			memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.nr_mapping_array_size);
6105830d754dSRandall Stewart 			asoc->nr_mapping_array_base_tsn = new_cum_tsn + 1;
6106830d754dSRandall Stewart 			asoc->highest_tsn_inside_nr_map = new_cum_tsn;
6107830d754dSRandall Stewart 			if (asoc->nr_mapping_array_size != asoc->mapping_array_size) {
6108830d754dSRandall Stewart 				/*
6109830d754dSRandall Stewart 				 * printf("IN sctp_handle_forward_tsn:
6110830d754dSRandall Stewart 				 * Something is wrong the size of" "map and
6111830d754dSRandall Stewart 				 * nr_map should be equal!")
6112830d754dSRandall Stewart 				 */ ;
6113830d754dSRandall Stewart 			}
6114830d754dSRandall Stewart 		}
6115b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
6116f8829a4aSRandall Stewart 			sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
611780fefe0aSRandall Stewart 		}
6118f8829a4aSRandall Stewart 		asoc->last_echo_tsn = asoc->highest_tsn_inside_map;
61192afb3e84SRandall Stewart 	} else {
61202afb3e84SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
61212afb3e84SRandall Stewart 		for (i = 0; i <= gap; i++) {
61228933fa13SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack
61238933fa13SRandall Stewart 			    && SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
61248933fa13SRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, i);
6125d50c1d79SRandall Stewart 			} else {
6126d50c1d79SRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->mapping_array, i);
61278933fa13SRandall Stewart 			}
6128f8829a4aSRandall Stewart 		}
61292afb3e84SRandall Stewart 		/*
61302afb3e84SRandall Stewart 		 * Now after marking all, slide thing forward but no sack
61312afb3e84SRandall Stewart 		 * please.
61322afb3e84SRandall Stewart 		 */
61332afb3e84SRandall Stewart 		sctp_sack_check(stcb, 0, 0, abort_flag);
61342afb3e84SRandall Stewart 		if (*abort_flag)
61352afb3e84SRandall Stewart 			return;
61362afb3e84SRandall Stewart 	}
6137f8829a4aSRandall Stewart 	/*************************************************************/
6138f8829a4aSRandall Stewart 	/* 2. Clear up re-assembly queue                             */
6139f8829a4aSRandall Stewart 	/*************************************************************/
6140f8829a4aSRandall Stewart 	/*
6141f8829a4aSRandall Stewart 	 * First service it if pd-api is up, just in case we can progress it
6142f8829a4aSRandall Stewart 	 * forward
6143f8829a4aSRandall Stewart 	 */
6144f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
6145f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
6146f8829a4aSRandall Stewart 	}
6147f8829a4aSRandall Stewart 	if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
6148f8829a4aSRandall Stewart 		/* For each one on here see if we need to toss it */
6149f8829a4aSRandall Stewart 		/*
6150f8829a4aSRandall Stewart 		 * For now large messages held on the reasmqueue that are
6151f8829a4aSRandall Stewart 		 * complete will be tossed too. We could in theory do more
6152f8829a4aSRandall Stewart 		 * work to spin through and stop after dumping one msg aka
6153f8829a4aSRandall Stewart 		 * seeing the start of a new msg at the head, and call the
6154f8829a4aSRandall Stewart 		 * delivery function... to see if it can be delivered... But
6155f8829a4aSRandall Stewart 		 * for now we just dump everything on the queue.
6156f8829a4aSRandall Stewart 		 */
6157f8829a4aSRandall Stewart 		chk = TAILQ_FIRST(&asoc->reasmqueue);
6158f8829a4aSRandall Stewart 		while (chk) {
6159f8829a4aSRandall Stewart 			at = TAILQ_NEXT(chk, sctp_next);
61600c0982b8SRandall Stewart 			if ((compare_with_wrap(new_cum_tsn,
61610c0982b8SRandall Stewart 			    chk->rec.data.TSN_seq, MAX_TSN)) ||
61620c0982b8SRandall Stewart 			    (new_cum_tsn == chk->rec.data.TSN_seq)) {
6163f8829a4aSRandall Stewart 				/* It needs to be tossed */
6164f8829a4aSRandall Stewart 				TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
6165f8829a4aSRandall Stewart 				if (compare_with_wrap(chk->rec.data.TSN_seq,
6166f8829a4aSRandall Stewart 				    asoc->tsn_last_delivered, MAX_TSN)) {
6167f8829a4aSRandall Stewart 					asoc->tsn_last_delivered =
6168f8829a4aSRandall Stewart 					    chk->rec.data.TSN_seq;
6169f8829a4aSRandall Stewart 					asoc->str_of_pdapi =
6170f8829a4aSRandall Stewart 					    chk->rec.data.stream_number;
6171f8829a4aSRandall Stewart 					asoc->ssn_of_pdapi =
6172f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq;
6173f8829a4aSRandall Stewart 					asoc->fragment_flags =
6174f8829a4aSRandall Stewart 					    chk->rec.data.rcv_flags;
6175f8829a4aSRandall Stewart 				}
6176f8829a4aSRandall Stewart 				asoc->size_on_reasm_queue -= chk->send_size;
6177f8829a4aSRandall Stewart 				sctp_ucount_decr(asoc->cnt_on_reasm_queue);
6178f8829a4aSRandall Stewart 
6179f8829a4aSRandall Stewart 				/* Clear up any stream problem */
6180f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) !=
6181f8829a4aSRandall Stewart 				    SCTP_DATA_UNORDERED &&
6182f8829a4aSRandall Stewart 				    (compare_with_wrap(chk->rec.data.stream_seq,
6183f8829a4aSRandall Stewart 				    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered,
6184f8829a4aSRandall Stewart 				    MAX_SEQ))) {
6185f8829a4aSRandall Stewart 					/*
6186f8829a4aSRandall Stewart 					 * We must dump forward this streams
6187f8829a4aSRandall Stewart 					 * sequence number if the chunk is
6188f8829a4aSRandall Stewart 					 * not unordered that is being
6189f8829a4aSRandall Stewart 					 * skipped. There is a chance that
6190f8829a4aSRandall Stewart 					 * if the peer does not include the
6191f8829a4aSRandall Stewart 					 * last fragment in its FWD-TSN we
6192f8829a4aSRandall Stewart 					 * WILL have a problem here since
6193f8829a4aSRandall Stewart 					 * you would have a partial chunk in
6194f8829a4aSRandall Stewart 					 * queue that may not be
6195f8829a4aSRandall Stewart 					 * deliverable. Also if a Partial
6196f8829a4aSRandall Stewart 					 * delivery API as started the user
6197f8829a4aSRandall Stewart 					 * may get a partial chunk. The next
6198f8829a4aSRandall Stewart 					 * read returning a new chunk...
6199f8829a4aSRandall Stewart 					 * really ugly but I see no way
6200f8829a4aSRandall Stewart 					 * around it! Maybe a notify??
6201f8829a4aSRandall Stewart 					 */
6202f8829a4aSRandall Stewart 					asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered =
6203f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq;
6204f8829a4aSRandall Stewart 				}
6205f8829a4aSRandall Stewart 				if (chk->data) {
6206f8829a4aSRandall Stewart 					sctp_m_freem(chk->data);
6207f8829a4aSRandall Stewart 					chk->data = NULL;
6208f8829a4aSRandall Stewart 				}
6209f8829a4aSRandall Stewart 				sctp_free_a_chunk(stcb, chk);
6210f8829a4aSRandall Stewart 			} else {
6211f8829a4aSRandall Stewart 				/*
6212f8829a4aSRandall Stewart 				 * Ok we have gone beyond the end of the
62138933fa13SRandall Stewart 				 * fwd-tsn's mark.
6214f8829a4aSRandall Stewart 				 */
6215f8829a4aSRandall Stewart 				break;
6216f8829a4aSRandall Stewart 			}
6217f8829a4aSRandall Stewart 			chk = at;
6218f8829a4aSRandall Stewart 		}
6219f8829a4aSRandall Stewart 	}
62208933fa13SRandall Stewart 	/*******************************************************/
62218933fa13SRandall Stewart 	/* 3. Update the PR-stream re-ordering queues and fix  */
62228933fa13SRandall Stewart 	/* delivery issues as needed.                       */
62238933fa13SRandall Stewart 	/*******************************************************/
6224f8829a4aSRandall Stewart 	fwd_sz -= sizeof(*fwd);
6225671d309cSRandall Stewart 	if (m && fwd_sz) {
6226f8829a4aSRandall Stewart 		/* New method. */
6227d61a0ae0SRandall Stewart 		unsigned int num_str;
6228671d309cSRandall Stewart 		struct sctp_strseq *stseq, strseqbuf;
6229671d309cSRandall Stewart 
6230671d309cSRandall Stewart 		offset += sizeof(*fwd);
6231f8829a4aSRandall Stewart 
62328933fa13SRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
6233f8829a4aSRandall Stewart 		num_str = fwd_sz / sizeof(struct sctp_strseq);
6234f8829a4aSRandall Stewart 		for (i = 0; i < num_str; i++) {
6235f8829a4aSRandall Stewart 			uint16_t st;
6236f8829a4aSRandall Stewart 
6237671d309cSRandall Stewart 			stseq = (struct sctp_strseq *)sctp_m_getptr(m, offset,
6238671d309cSRandall Stewart 			    sizeof(struct sctp_strseq),
6239671d309cSRandall Stewart 			    (uint8_t *) & strseqbuf);
6240671d309cSRandall Stewart 			offset += sizeof(struct sctp_strseq);
62412afb3e84SRandall Stewart 			if (stseq == NULL) {
6242671d309cSRandall Stewart 				break;
62432afb3e84SRandall Stewart 			}
6244f8829a4aSRandall Stewart 			/* Convert */
6245851b7298SRandall Stewart 			st = ntohs(stseq->stream);
6246851b7298SRandall Stewart 			stseq->stream = st;
6247851b7298SRandall Stewart 			st = ntohs(stseq->sequence);
6248851b7298SRandall Stewart 			stseq->sequence = st;
62498933fa13SRandall Stewart 
6250f8829a4aSRandall Stewart 			/* now process */
62518933fa13SRandall Stewart 
62528933fa13SRandall Stewart 			/*
62538933fa13SRandall Stewart 			 * Ok we now look for the stream/seq on the read
62548933fa13SRandall Stewart 			 * queue where its not all delivered. If we find it
62558933fa13SRandall Stewart 			 * we transmute the read entry into a PDI_ABORTED.
62568933fa13SRandall Stewart 			 */
6257851b7298SRandall Stewart 			if (stseq->stream >= asoc->streamincnt) {
62582afb3e84SRandall Stewart 				/* screwed up streams, stop!  */
62592afb3e84SRandall Stewart 				break;
6260f8829a4aSRandall Stewart 			}
62618933fa13SRandall Stewart 			if ((asoc->str_of_pdapi == stseq->stream) &&
62628933fa13SRandall Stewart 			    (asoc->ssn_of_pdapi == stseq->sequence)) {
62638933fa13SRandall Stewart 				/*
62648933fa13SRandall Stewart 				 * If this is the one we were partially
62658933fa13SRandall Stewart 				 * delivering now then we no longer are.
62668933fa13SRandall Stewart 				 * Note this will change with the reassembly
62678933fa13SRandall Stewart 				 * re-write.
62688933fa13SRandall Stewart 				 */
62698933fa13SRandall Stewart 				asoc->fragmented_delivery_inprogress = 0;
62708933fa13SRandall Stewart 			}
62718933fa13SRandall Stewart 			sctp_flush_reassm_for_str_seq(stcb, asoc, stseq->stream, stseq->sequence);
62728933fa13SRandall Stewart 			TAILQ_FOREACH(ctl, &stcb->sctp_ep->read_queue, next) {
62738933fa13SRandall Stewart 				if ((ctl->sinfo_stream == stseq->stream) &&
62748933fa13SRandall Stewart 				    (ctl->sinfo_ssn == stseq->sequence)) {
62758933fa13SRandall Stewart 					str_seq = (stseq->stream << 16) | stseq->sequence;
62768933fa13SRandall Stewart 					ctl->end_added = 1;
62778933fa13SRandall Stewart 					ctl->pdapi_aborted = 1;
62788933fa13SRandall Stewart 					sv = stcb->asoc.control_pdapi;
62798933fa13SRandall Stewart 					stcb->asoc.control_pdapi = ctl;
62808933fa13SRandall Stewart 					sctp_notify_partial_delivery_indication(stcb,
62818933fa13SRandall Stewart 					    SCTP_PARTIAL_DELIVERY_ABORTED,
62828933fa13SRandall Stewart 					    SCTP_HOLDS_LOCK,
62838933fa13SRandall Stewart 					    str_seq);
62848933fa13SRandall Stewart 					stcb->asoc.control_pdapi = sv;
62858933fa13SRandall Stewart 					break;
62868933fa13SRandall Stewart 				} else if ((ctl->sinfo_stream == stseq->stream) &&
62878933fa13SRandall Stewart 				    (compare_with_wrap(ctl->sinfo_ssn, stseq->sequence, MAX_SEQ))) {
62888933fa13SRandall Stewart 					/* We are past our victim SSN */
62898933fa13SRandall Stewart 					break;
62908933fa13SRandall Stewart 				}
62918933fa13SRandall Stewart 			}
6292851b7298SRandall Stewart 			strm = &asoc->strmin[stseq->stream];
6293851b7298SRandall Stewart 			if (compare_with_wrap(stseq->sequence,
6294f8829a4aSRandall Stewart 			    strm->last_sequence_delivered, MAX_SEQ)) {
6295f8829a4aSRandall Stewart 				/* Update the sequence number */
6296f8829a4aSRandall Stewart 				strm->last_sequence_delivered =
6297851b7298SRandall Stewart 				    stseq->sequence;
6298f8829a4aSRandall Stewart 			}
6299f8829a4aSRandall Stewart 			/* now kick the stream the new way */
630004ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
6301f8829a4aSRandall Stewart 			sctp_kick_prsctp_reorder_queue(stcb, strm);
6302f8829a4aSRandall Stewart 		}
63038933fa13SRandall Stewart 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
6304f8829a4aSRandall Stewart 	}
6305139bc87fSRandall Stewart 	if (TAILQ_FIRST(&asoc->reasmqueue)) {
6306139bc87fSRandall Stewart 		/* now lets kick out and check for more fragmented delivery */
630704ee05e8SRandall Stewart 		/* sa_ignore NO_NULL_CHK */
6308139bc87fSRandall Stewart 		sctp_deliver_reasm_check(stcb, &stcb->asoc);
6309139bc87fSRandall Stewart 	}
6310f8829a4aSRandall Stewart }
6311830d754dSRandall Stewart 
6312830d754dSRandall Stewart /* EY fully identical to sctp_express_handle_sack, duplicated for only naming convention */
6313830d754dSRandall Stewart void
6314830d754dSRandall Stewart sctp_express_handle_nr_sack(struct sctp_tcb *stcb, uint32_t cumack,
6315830d754dSRandall Stewart     uint32_t rwnd, int nonce_sum_flag, int *abort_now)
6316830d754dSRandall Stewart {
6317830d754dSRandall Stewart 	struct sctp_nets *net;
6318830d754dSRandall Stewart 	struct sctp_association *asoc;
6319830d754dSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
6320830d754dSRandall Stewart 	uint32_t old_rwnd;
6321830d754dSRandall Stewart 	int win_probe_recovery = 0;
6322830d754dSRandall Stewart 	int win_probe_recovered = 0;
6323830d754dSRandall Stewart 	int j, done_once = 0;
6324830d754dSRandall Stewart 
6325830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
6326830d754dSRandall Stewart 		sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
6327830d754dSRandall Stewart 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
6328830d754dSRandall Stewart 	}
6329830d754dSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
6330830d754dSRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
6331830d754dSRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
6332830d754dSRandall Stewart 	stcb->asoc.cumack_log_at++;
6333830d754dSRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
6334830d754dSRandall Stewart 		stcb->asoc.cumack_log_at = 0;
6335830d754dSRandall Stewart 	}
6336830d754dSRandall Stewart #endif
6337830d754dSRandall Stewart 	asoc = &stcb->asoc;
6338830d754dSRandall Stewart 	old_rwnd = asoc->peers_rwnd;
6339830d754dSRandall Stewart 	if (compare_with_wrap(asoc->last_acked_seq, cumack, MAX_TSN)) {
6340830d754dSRandall Stewart 		/* old ack */
6341830d754dSRandall Stewart 		return;
6342830d754dSRandall Stewart 	} else if (asoc->last_acked_seq == cumack) {
6343830d754dSRandall Stewart 		/* Window update sack */
6344830d754dSRandall Stewart 		asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
6345830d754dSRandall Stewart 		    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
6346830d754dSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6347830d754dSRandall Stewart 			/* SWS sender side engages */
6348830d754dSRandall Stewart 			asoc->peers_rwnd = 0;
6349830d754dSRandall Stewart 		}
6350830d754dSRandall Stewart 		if (asoc->peers_rwnd > old_rwnd) {
6351830d754dSRandall Stewart 			goto again;
6352830d754dSRandall Stewart 		}
6353830d754dSRandall Stewart 		return;
6354830d754dSRandall Stewart 	}
6355830d754dSRandall Stewart 	/* First setup for CC stuff */
6356830d754dSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6357830d754dSRandall Stewart 		net->prev_cwnd = net->cwnd;
6358830d754dSRandall Stewart 		net->net_ack = 0;
6359830d754dSRandall Stewart 		net->net_ack2 = 0;
6360830d754dSRandall Stewart 
6361830d754dSRandall Stewart 		/*
6362830d754dSRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
6363830d754dSRandall Stewart 		 * SACK processing
6364830d754dSRandall Stewart 		 */
6365830d754dSRandall Stewart 		net->new_pseudo_cumack = 0;
6366830d754dSRandall Stewart 		net->will_exit_fast_recovery = 0;
6367830d754dSRandall Stewart 	}
6368830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
6369830d754dSRandall Stewart 		uint32_t send_s;
6370830d754dSRandall Stewart 
6371830d754dSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
6372830d754dSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
6373830d754dSRandall Stewart 			    sctpchunk_listhead);
6374830d754dSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
6375830d754dSRandall Stewart 		} else {
6376830d754dSRandall Stewart 			send_s = asoc->sending_seq;
6377830d754dSRandall Stewart 		}
6378830d754dSRandall Stewart 		if ((cumack == send_s) ||
6379830d754dSRandall Stewart 		    compare_with_wrap(cumack, send_s, MAX_TSN)) {
6380830d754dSRandall Stewart #ifndef INVARIANTS
6381830d754dSRandall Stewart 			struct mbuf *oper;
6382830d754dSRandall Stewart 
6383830d754dSRandall Stewart #endif
6384830d754dSRandall Stewart #ifdef INVARIANTS
6385830d754dSRandall Stewart 			panic("Impossible sack 1");
6386830d754dSRandall Stewart #else
6387830d754dSRandall Stewart 			*abort_now = 1;
6388830d754dSRandall Stewart 			/* XXX */
6389830d754dSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
6390830d754dSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
6391830d754dSRandall Stewart 			if (oper) {
6392830d754dSRandall Stewart 				struct sctp_paramhdr *ph;
6393830d754dSRandall Stewart 				uint32_t *ippp;
6394830d754dSRandall Stewart 
6395830d754dSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
6396830d754dSRandall Stewart 				    sizeof(uint32_t);
6397830d754dSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
6398830d754dSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
6399830d754dSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
6400830d754dSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
6401830d754dSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
6402830d754dSRandall Stewart 			}
6403830d754dSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
6404830d754dSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
6405830d754dSRandall Stewart 			return;
6406830d754dSRandall Stewart #endif
6407830d754dSRandall Stewart 		}
6408830d754dSRandall Stewart 	}
6409830d754dSRandall Stewart 	asoc->this_sack_highest_gap = cumack;
6410830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
6411830d754dSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
6412830d754dSRandall Stewart 		    stcb->asoc.overall_error_count,
6413830d754dSRandall Stewart 		    0,
6414830d754dSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
6415830d754dSRandall Stewart 		    __LINE__);
6416830d754dSRandall Stewart 	}
6417830d754dSRandall Stewart 	stcb->asoc.overall_error_count = 0;
6418830d754dSRandall Stewart 	if (compare_with_wrap(cumack, asoc->last_acked_seq, MAX_TSN)) {
6419830d754dSRandall Stewart 		/* process the new consecutive TSN first */
6420830d754dSRandall Stewart 		tp1 = TAILQ_FIRST(&asoc->sent_queue);
6421830d754dSRandall Stewart 		while (tp1) {
6422830d754dSRandall Stewart 			tp2 = TAILQ_NEXT(tp1, sctp_next);
6423830d754dSRandall Stewart 			if (compare_with_wrap(cumack, tp1->rec.data.TSN_seq,
6424830d754dSRandall Stewart 			    MAX_TSN) ||
6425830d754dSRandall Stewart 			    cumack == tp1->rec.data.TSN_seq) {
6426830d754dSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
6427830d754dSRandall Stewart 					printf("Warning, an unsent is now acked?\n");
6428830d754dSRandall Stewart 				}
6429830d754dSRandall Stewart 				/*
6430830d754dSRandall Stewart 				 * ECN Nonce: Add the nonce to the sender's
6431830d754dSRandall Stewart 				 * nonce sum
6432830d754dSRandall Stewart 				 */
6433830d754dSRandall Stewart 				asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
6434830d754dSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
6435830d754dSRandall Stewart 					/*
6436830d754dSRandall Stewart 					 * If it is less than ACKED, it is
6437830d754dSRandall Stewart 					 * now no-longer in flight. Higher
6438830d754dSRandall Stewart 					 * values may occur during marking
6439830d754dSRandall Stewart 					 */
6440830d754dSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
6441830d754dSRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
6442830d754dSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
6443830d754dSRandall Stewart 							    tp1->whoTo->flight_size,
6444830d754dSRandall Stewart 							    tp1->book_size,
6445830d754dSRandall Stewart 							    (uintptr_t) tp1->whoTo,
6446830d754dSRandall Stewart 							    tp1->rec.data.TSN_seq);
6447830d754dSRandall Stewart 						}
6448830d754dSRandall Stewart 						sctp_flight_size_decrease(tp1);
6449830d754dSRandall Stewart 						/* sa_ignore NO_NULL_CHK */
6450830d754dSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
6451830d754dSRandall Stewart 					}
6452830d754dSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
6453830d754dSRandall Stewart 					if (tp1->snd_count < 2) {
6454830d754dSRandall Stewart 						/*
6455830d754dSRandall Stewart 						 * True non-retransmited
6456830d754dSRandall Stewart 						 * chunk
6457830d754dSRandall Stewart 						 */
6458830d754dSRandall Stewart 						tp1->whoTo->net_ack2 +=
6459830d754dSRandall Stewart 						    tp1->send_size;
6460830d754dSRandall Stewart 
6461830d754dSRandall Stewart 						/* update RTO too? */
6462830d754dSRandall Stewart 						if (tp1->do_rtt) {
6463830d754dSRandall Stewart 							tp1->whoTo->RTO =
6464830d754dSRandall Stewart 							/*
6465830d754dSRandall Stewart 							 * sa_ignore
6466830d754dSRandall Stewart 							 * NO_NULL_CHK
6467830d754dSRandall Stewart 							 */
6468830d754dSRandall Stewart 							    sctp_calculate_rto(stcb,
6469830d754dSRandall Stewart 							    asoc, tp1->whoTo,
6470830d754dSRandall Stewart 							    &tp1->sent_rcv_time,
6471830d754dSRandall Stewart 							    sctp_align_safe_nocopy);
6472830d754dSRandall Stewart 							tp1->do_rtt = 0;
6473830d754dSRandall Stewart 						}
6474830d754dSRandall Stewart 					}
6475830d754dSRandall Stewart 					/*
6476830d754dSRandall Stewart 					 * CMT: CUCv2 algorithm. From the
6477830d754dSRandall Stewart 					 * cumack'd TSNs, for each TSN being
6478830d754dSRandall Stewart 					 * acked for the first time, set the
6479830d754dSRandall Stewart 					 * following variables for the
6480830d754dSRandall Stewart 					 * corresp destination.
6481830d754dSRandall Stewart 					 * new_pseudo_cumack will trigger a
6482830d754dSRandall Stewart 					 * cwnd update.
6483830d754dSRandall Stewart 					 * find_(rtx_)pseudo_cumack will
6484830d754dSRandall Stewart 					 * trigger search for the next
6485830d754dSRandall Stewart 					 * expected (rtx-)pseudo-cumack.
6486830d754dSRandall Stewart 					 */
6487830d754dSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
6488830d754dSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
6489830d754dSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
6490830d754dSRandall Stewart 
6491830d754dSRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
6492830d754dSRandall Stewart 						/* sa_ignore NO_NULL_CHK */
6493830d754dSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
6494830d754dSRandall Stewart 					}
6495830d754dSRandall Stewart 				}
6496830d754dSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
6497830d754dSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
6498830d754dSRandall Stewart 				}
6499830d754dSRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
6500830d754dSRandall Stewart 					/* deflate the cwnd */
6501830d754dSRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
6502830d754dSRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
6503830d754dSRandall Stewart 				}
6504830d754dSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
6505830d754dSRandall Stewart 				TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
6506830d754dSRandall Stewart 				if (tp1->data) {
6507830d754dSRandall Stewart 					/* sa_ignore NO_NULL_CHK */
6508830d754dSRandall Stewart 					sctp_free_bufspace(stcb, asoc, tp1, 1);
6509830d754dSRandall Stewart 					sctp_m_freem(tp1->data);
6510830d754dSRandall Stewart 				}
6511830d754dSRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
6512830d754dSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
6513830d754dSRandall Stewart 					    cumack,
6514830d754dSRandall Stewart 					    tp1->rec.data.TSN_seq,
6515830d754dSRandall Stewart 					    0,
6516830d754dSRandall Stewart 					    0,
6517830d754dSRandall Stewart 					    SCTP_LOG_FREE_SENT);
6518830d754dSRandall Stewart 				}
6519830d754dSRandall Stewart 				tp1->data = NULL;
6520830d754dSRandall Stewart 				asoc->sent_queue_cnt--;
6521830d754dSRandall Stewart 				sctp_free_a_chunk(stcb, tp1);
6522830d754dSRandall Stewart 				tp1 = tp2;
6523830d754dSRandall Stewart 			} else {
6524830d754dSRandall Stewart 				break;
6525830d754dSRandall Stewart 			}
6526830d754dSRandall Stewart 		}
6527830d754dSRandall Stewart 
6528830d754dSRandall Stewart 	}
6529830d754dSRandall Stewart 	/* sa_ignore NO_NULL_CHK */
6530830d754dSRandall Stewart 	if (stcb->sctp_socket) {
6531830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
6532830d754dSRandall Stewart 		struct socket *so;
6533830d754dSRandall Stewart 
6534830d754dSRandall Stewart #endif
6535830d754dSRandall Stewart 
6536830d754dSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
6537830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
6538830d754dSRandall Stewart 			/* sa_ignore NO_NULL_CHK */
6539830d754dSRandall Stewart 			sctp_wakeup_log(stcb, cumack, 1, SCTP_WAKESND_FROM_SACK);
6540830d754dSRandall Stewart 		}
6541830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
6542830d754dSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
6543830d754dSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
6544830d754dSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
6545830d754dSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
6546830d754dSRandall Stewart 		SCTP_TCB_LOCK(stcb);
6547830d754dSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
6548830d754dSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
6549830d754dSRandall Stewart 			/* assoc was freed while we were unlocked */
6550830d754dSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
6551830d754dSRandall Stewart 			return;
6552830d754dSRandall Stewart 		}
6553830d754dSRandall Stewart #endif
6554830d754dSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
6555830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
6556830d754dSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
6557830d754dSRandall Stewart #endif
6558830d754dSRandall Stewart 	} else {
6559830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
6560830d754dSRandall Stewart 			sctp_wakeup_log(stcb, cumack, 1, SCTP_NOWAKE_FROM_SACK);
6561830d754dSRandall Stewart 		}
6562830d754dSRandall Stewart 	}
6563830d754dSRandall Stewart 
6564830d754dSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
6565830d754dSRandall Stewart 	if (asoc->last_acked_seq != cumack)
6566830d754dSRandall Stewart 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
6567830d754dSRandall Stewart 
6568830d754dSRandall Stewart 	asoc->last_acked_seq = cumack;
6569830d754dSRandall Stewart 
6570830d754dSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
6571830d754dSRandall Stewart 		/* nothing left in-flight */
6572830d754dSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6573830d754dSRandall Stewart 			net->flight_size = 0;
6574830d754dSRandall Stewart 			net->partial_bytes_acked = 0;
6575830d754dSRandall Stewart 		}
6576830d754dSRandall Stewart 		asoc->total_flight = 0;
6577830d754dSRandall Stewart 		asoc->total_flight_count = 0;
6578830d754dSRandall Stewart 	}
6579830d754dSRandall Stewart 	/* Fix up the a-p-a-p for future PR-SCTP sends */
6580830d754dSRandall Stewart 	if (compare_with_wrap(cumack, asoc->advanced_peer_ack_point, MAX_TSN)) {
6581830d754dSRandall Stewart 		asoc->advanced_peer_ack_point = cumack;
6582830d754dSRandall Stewart 	}
6583830d754dSRandall Stewart 	/* ECN Nonce updates */
6584830d754dSRandall Stewart 	if (asoc->ecn_nonce_allowed) {
6585830d754dSRandall Stewart 		if (asoc->nonce_sum_check) {
6586830d754dSRandall Stewart 			if (nonce_sum_flag != ((asoc->nonce_sum_expect_base) & SCTP_SACK_NONCE_SUM)) {
6587830d754dSRandall Stewart 				if (asoc->nonce_wait_for_ecne == 0) {
6588830d754dSRandall Stewart 					struct sctp_tmit_chunk *lchk;
6589830d754dSRandall Stewart 
6590830d754dSRandall Stewart 					lchk = TAILQ_FIRST(&asoc->send_queue);
6591830d754dSRandall Stewart 					asoc->nonce_wait_for_ecne = 1;
6592830d754dSRandall Stewart 					if (lchk) {
6593830d754dSRandall Stewart 						asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
6594830d754dSRandall Stewart 					} else {
6595830d754dSRandall Stewart 						asoc->nonce_wait_tsn = asoc->sending_seq;
6596830d754dSRandall Stewart 					}
6597830d754dSRandall Stewart 				} else {
6598830d754dSRandall Stewart 					if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
6599830d754dSRandall Stewart 					    (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
6600830d754dSRandall Stewart 						/*
6601830d754dSRandall Stewart 						 * Misbehaving peer. We need
6602830d754dSRandall Stewart 						 * to react to this guy
6603830d754dSRandall Stewart 						 */
6604830d754dSRandall Stewart 						asoc->ecn_allowed = 0;
6605830d754dSRandall Stewart 						asoc->ecn_nonce_allowed = 0;
6606830d754dSRandall Stewart 					}
6607830d754dSRandall Stewart 				}
6608830d754dSRandall Stewart 			}
6609830d754dSRandall Stewart 		} else {
6610830d754dSRandall Stewart 			/* See if Resynchronization Possible */
6611830d754dSRandall Stewart 			if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
6612830d754dSRandall Stewart 				asoc->nonce_sum_check = 1;
6613830d754dSRandall Stewart 				/*
6614830d754dSRandall Stewart 				 * now we must calculate what the base is.
6615830d754dSRandall Stewart 				 * We do this based on two things, we know
6616830d754dSRandall Stewart 				 * the total's for all the segments
6617830d754dSRandall Stewart 				 * gap-acked in the SACK (none), We also
6618830d754dSRandall Stewart 				 * know the SACK's nonce sum, its in
6619830d754dSRandall Stewart 				 * nonce_sum_flag. So we can build a truth
6620830d754dSRandall Stewart 				 * table to back-calculate the new value of
6621830d754dSRandall Stewart 				 * asoc->nonce_sum_expect_base:
6622830d754dSRandall Stewart 				 *
6623830d754dSRandall Stewart 				 * SACK-flag-Value         Seg-Sums Base 0 0 0
6624830d754dSRandall Stewart 				 * 1                    0 1 0 1 1 1 1 0
6625830d754dSRandall Stewart 				 */
6626830d754dSRandall Stewart 				asoc->nonce_sum_expect_base = (0 ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
6627830d754dSRandall Stewart 			}
6628830d754dSRandall Stewart 		}
6629830d754dSRandall Stewart 	}
6630830d754dSRandall Stewart 	/* RWND update */
6631830d754dSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
6632830d754dSRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
6633830d754dSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6634830d754dSRandall Stewart 		/* SWS sender side engages */
6635830d754dSRandall Stewart 		asoc->peers_rwnd = 0;
6636830d754dSRandall Stewart 	}
6637830d754dSRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
6638830d754dSRandall Stewart 		win_probe_recovery = 1;
6639830d754dSRandall Stewart 	}
6640830d754dSRandall Stewart 	/* Now assure a timer where data is queued at */
6641830d754dSRandall Stewart again:
6642830d754dSRandall Stewart 	j = 0;
6643830d754dSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
66445171328bSRandall Stewart 		int to_ticks;
66455171328bSRandall Stewart 
6646830d754dSRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
6647830d754dSRandall Stewart 			win_probe_recovered = 1;
6648830d754dSRandall Stewart 			/*
6649830d754dSRandall Stewart 			 * Find first chunk that was used with window probe
6650830d754dSRandall Stewart 			 * and clear the sent
6651830d754dSRandall Stewart 			 */
6652830d754dSRandall Stewart 			/* sa_ignore FREED_MEMORY */
6653830d754dSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
6654830d754dSRandall Stewart 				if (tp1->window_probe) {
6655830d754dSRandall Stewart 					/* move back to data send queue */
6656830d754dSRandall Stewart 					sctp_window_probe_recovery(stcb, asoc, net, tp1);
6657830d754dSRandall Stewart 					break;
6658830d754dSRandall Stewart 				}
6659830d754dSRandall Stewart 			}
6660830d754dSRandall Stewart 		}
6661830d754dSRandall Stewart 		if (net->RTO == 0) {
6662830d754dSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
6663830d754dSRandall Stewart 		} else {
6664830d754dSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
6665830d754dSRandall Stewart 		}
66665171328bSRandall Stewart 		if (net->flight_size) {
66675171328bSRandall Stewart 
6668830d754dSRandall Stewart 			j++;
6669830d754dSRandall Stewart 			(void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
6670830d754dSRandall Stewart 			    sctp_timeout_handler, &net->rxt_timer);
66715171328bSRandall Stewart 			if (net->window_probe) {
66725171328bSRandall Stewart 				net->window_probe = 0;
66735171328bSRandall Stewart 			}
6674830d754dSRandall Stewart 		} else {
66755171328bSRandall Stewart 			if (net->window_probe) {
66765171328bSRandall Stewart 				/*
66775171328bSRandall Stewart 				 * In window probes we must assure a timer
66785171328bSRandall Stewart 				 * is still running there
66795171328bSRandall Stewart 				 */
66805171328bSRandall Stewart 				net->window_probe = 0;
66815171328bSRandall Stewart 				(void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
66825171328bSRandall Stewart 				    sctp_timeout_handler, &net->rxt_timer);
66835171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
6684830d754dSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
6685830d754dSRandall Stewart 				    stcb, net,
6686830d754dSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
6687830d754dSRandall Stewart 			}
6688830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
6689830d754dSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
6690830d754dSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
6691830d754dSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
6692830d754dSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
6693830d754dSRandall Stewart 				}
6694830d754dSRandall Stewart 			}
6695830d754dSRandall Stewart 		}
6696830d754dSRandall Stewart 	}
6697830d754dSRandall Stewart 	if ((j == 0) &&
6698830d754dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
6699830d754dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
6700830d754dSRandall Stewart 	    (win_probe_recovered == 0) &&
6701830d754dSRandall Stewart 	    (done_once == 0)) {
67020c0982b8SRandall Stewart 		/*
67030c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
67040c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
67050c0982b8SRandall Stewart 		 */
67060c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
6707830d754dSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6708830d754dSRandall Stewart 				net->flight_size = 0;
6709830d754dSRandall Stewart 			}
6710830d754dSRandall Stewart 			asoc->total_flight = 0;
6711830d754dSRandall Stewart 			asoc->total_flight_count = 0;
6712830d754dSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
6713830d754dSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
6714830d754dSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
6715830d754dSRandall Stewart 					sctp_flight_size_increase(tp1);
6716830d754dSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
6717830d754dSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
6718830d754dSRandall Stewart 					asoc->sent_queue_retran_cnt++;
6719830d754dSRandall Stewart 				}
6720830d754dSRandall Stewart 			}
67210c0982b8SRandall Stewart 		}
6722830d754dSRandall Stewart 		done_once = 1;
6723830d754dSRandall Stewart 		goto again;
6724830d754dSRandall Stewart 	}
6725830d754dSRandall Stewart 	/**********************************/
6726830d754dSRandall Stewart 	/* Now what about shutdown issues */
6727830d754dSRandall Stewart 	/**********************************/
6728830d754dSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
6729830d754dSRandall Stewart 		/* nothing left on sendqueue.. consider done */
6730830d754dSRandall Stewart 		/* clean up */
6731830d754dSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
6732830d754dSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
6733830d754dSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
6734830d754dSRandall Stewart 		    (asoc->locked_on_sending)
6735830d754dSRandall Stewart 		    ) {
6736830d754dSRandall Stewart 			struct sctp_stream_queue_pending *sp;
6737830d754dSRandall Stewart 
6738830d754dSRandall Stewart 			/*
6739830d754dSRandall Stewart 			 * I may be in a state where we got all across.. but
6740830d754dSRandall Stewart 			 * cannot write more due to a shutdown... we abort
6741830d754dSRandall Stewart 			 * since the user did not indicate EOR in this case.
6742830d754dSRandall Stewart 			 * The sp will be cleaned during free of the asoc.
6743830d754dSRandall Stewart 			 */
6744830d754dSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
6745830d754dSRandall Stewart 			    sctp_streamhead);
6746830d754dSRandall Stewart 			if ((sp) && (sp->length == 0)) {
6747830d754dSRandall Stewart 				/* Let cleanup code purge it */
6748830d754dSRandall Stewart 				if (sp->msg_is_complete) {
6749830d754dSRandall Stewart 					asoc->stream_queue_cnt--;
6750830d754dSRandall Stewart 				} else {
6751830d754dSRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
6752830d754dSRandall Stewart 					asoc->locked_on_sending = NULL;
6753830d754dSRandall Stewart 					asoc->stream_queue_cnt--;
6754830d754dSRandall Stewart 				}
6755830d754dSRandall Stewart 			}
6756830d754dSRandall Stewart 		}
6757830d754dSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
6758830d754dSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
6759830d754dSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
6760830d754dSRandall Stewart 				/* Need to abort here */
6761830d754dSRandall Stewart 				struct mbuf *oper;
6762830d754dSRandall Stewart 
6763830d754dSRandall Stewart 		abort_out_now:
6764830d754dSRandall Stewart 				*abort_now = 1;
6765830d754dSRandall Stewart 				/* XXX */
6766830d754dSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
6767830d754dSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
6768830d754dSRandall Stewart 				if (oper) {
6769830d754dSRandall Stewart 					struct sctp_paramhdr *ph;
6770830d754dSRandall Stewart 					uint32_t *ippp;
6771830d754dSRandall Stewart 
6772830d754dSRandall Stewart 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
6773830d754dSRandall Stewart 					    sizeof(uint32_t);
6774830d754dSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
6775830d754dSRandall Stewart 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6776830d754dSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
6777830d754dSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
6778830d754dSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_24);
6779830d754dSRandall Stewart 				}
6780830d754dSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
6781830d754dSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
6782830d754dSRandall Stewart 			} else {
6783830d754dSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
6784830d754dSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
6785830d754dSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6786830d754dSRandall Stewart 				}
6787830d754dSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
6788830d754dSRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6789830d754dSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
6790830d754dSRandall Stewart 				sctp_send_shutdown(stcb,
6791830d754dSRandall Stewart 				    stcb->asoc.primary_destination);
6792830d754dSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
6793830d754dSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
6794830d754dSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
6795830d754dSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
6796830d754dSRandall Stewart 			}
6797830d754dSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
6798830d754dSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
6799830d754dSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
6800830d754dSRandall Stewart 				goto abort_out_now;
6801830d754dSRandall Stewart 			}
6802830d754dSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6803830d754dSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
6804830d754dSRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6805830d754dSRandall Stewart 			sctp_send_shutdown_ack(stcb,
6806830d754dSRandall Stewart 			    stcb->asoc.primary_destination);
6807830d754dSRandall Stewart 
6808830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
6809830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, asoc->primary_destination);
6810830d754dSRandall Stewart 		}
6811830d754dSRandall Stewart 	}
6812830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
6813830d754dSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
6814830d754dSRandall Stewart 		    rwnd,
6815830d754dSRandall Stewart 		    stcb->asoc.peers_rwnd,
6816830d754dSRandall Stewart 		    stcb->asoc.total_flight,
6817830d754dSRandall Stewart 		    stcb->asoc.total_output_queue_size);
6818830d754dSRandall Stewart 	}
6819830d754dSRandall Stewart }
6820830d754dSRandall Stewart 
6821830d754dSRandall Stewart /* EY! nr_sack version of sctp_handle_segments, nr-gapped TSNs get removed from RtxQ in this method*/
6822830d754dSRandall Stewart static void
6823830d754dSRandall Stewart sctp_handle_nr_sack_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
6824830d754dSRandall Stewart     struct sctp_nr_sack_chunk *ch, uint32_t last_tsn, uint32_t * biggest_tsn_acked,
6825830d754dSRandall Stewart     uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
6826830d754dSRandall Stewart     uint32_t num_seg, uint32_t num_nr_seg, int *ecn_seg_sums)
6827830d754dSRandall Stewart {
6828830d754dSRandall Stewart 	/************************************************/
6829830d754dSRandall Stewart 	/* process fragments and update sendqueue        */
6830830d754dSRandall Stewart 	/************************************************/
6831830d754dSRandall Stewart 	struct sctp_nr_sack *nr_sack;
6832830d754dSRandall Stewart 	struct sctp_gap_ack_block *frag, block;
6833830d754dSRandall Stewart 	struct sctp_nr_gap_ack_block *nr_frag, nr_block;
6834830d754dSRandall Stewart 	struct sctp_tmit_chunk *tp1;
6835d50c1d79SRandall Stewart 	uint32_t i, j;
6836830d754dSRandall Stewart 	int wake_him = 0;
6837830d754dSRandall Stewart 	uint32_t theTSN;
6838830d754dSRandall Stewart 	int num_frs = 0;
6839830d754dSRandall Stewart 
6840830d754dSRandall Stewart 	uint16_t frag_strt, frag_end, primary_flag_set;
6841830d754dSRandall Stewart 	uint16_t nr_frag_strt, nr_frag_end;
6842830d754dSRandall Stewart 
6843830d754dSRandall Stewart 	uint32_t last_frag_high;
6844830d754dSRandall Stewart 	uint32_t last_nr_frag_high;
6845830d754dSRandall Stewart 
6846830d754dSRandall Stewart 	/*
6847830d754dSRandall Stewart 	 * @@@ JRI : TODO: This flag is not used anywhere .. remove?
6848830d754dSRandall Stewart 	 */
6849830d754dSRandall Stewart 	if (asoc->primary_destination->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
6850830d754dSRandall Stewart 		primary_flag_set = 1;
6851830d754dSRandall Stewart 	} else {
6852830d754dSRandall Stewart 		primary_flag_set = 0;
6853830d754dSRandall Stewart 	}
6854830d754dSRandall Stewart 	nr_sack = &ch->nr_sack;
6855830d754dSRandall Stewart 
6856830d754dSRandall Stewart 	/*
6857830d754dSRandall Stewart 	 * EY! - I will process nr_gaps similarly,by going to this position
6858830d754dSRandall Stewart 	 * again if All bit is set
6859830d754dSRandall Stewart 	 */
6860830d754dSRandall Stewart 	frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
6861830d754dSRandall Stewart 	    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
6862830d754dSRandall Stewart 	*offset += sizeof(block);
6863830d754dSRandall Stewart 	if (frag == NULL) {
6864830d754dSRandall Stewart 		return;
6865830d754dSRandall Stewart 	}
6866830d754dSRandall Stewart 	tp1 = NULL;
6867830d754dSRandall Stewart 	last_frag_high = 0;
6868830d754dSRandall Stewart 	for (i = 0; i < num_seg; i++) {
6869830d754dSRandall Stewart 		frag_strt = ntohs(frag->start);
6870830d754dSRandall Stewart 		frag_end = ntohs(frag->end);
6871830d754dSRandall Stewart 		/* some sanity checks on the fargment offsets */
6872830d754dSRandall Stewart 		if (frag_strt > frag_end) {
6873830d754dSRandall Stewart 			/* this one is malformed, skip */
6874830d754dSRandall Stewart 			frag++;
6875830d754dSRandall Stewart 			continue;
6876830d754dSRandall Stewart 		}
6877830d754dSRandall Stewart 		if (compare_with_wrap((frag_end + last_tsn), *biggest_tsn_acked,
6878830d754dSRandall Stewart 		    MAX_TSN))
6879830d754dSRandall Stewart 			*biggest_tsn_acked = frag_end + last_tsn;
6880830d754dSRandall Stewart 
6881830d754dSRandall Stewart 		/* mark acked dgs and find out the highestTSN being acked */
6882830d754dSRandall Stewart 		if (tp1 == NULL) {
6883830d754dSRandall Stewart 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
6884830d754dSRandall Stewart 
6885830d754dSRandall Stewart 			/* save the locations of the last frags */
6886830d754dSRandall Stewart 			last_frag_high = frag_end + last_tsn;
6887830d754dSRandall Stewart 		} else {
6888830d754dSRandall Stewart 			/*
6889830d754dSRandall Stewart 			 * now lets see if we need to reset the queue due to
6890830d754dSRandall Stewart 			 * a out-of-order SACK fragment
6891830d754dSRandall Stewart 			 */
6892830d754dSRandall Stewart 			if (compare_with_wrap(frag_strt + last_tsn,
6893830d754dSRandall Stewart 			    last_frag_high, MAX_TSN)) {
6894830d754dSRandall Stewart 				/*
6895830d754dSRandall Stewart 				 * if the new frag starts after the last TSN
6896830d754dSRandall Stewart 				 * frag covered, we are ok and this one is
6897830d754dSRandall Stewart 				 * beyond the last one
6898830d754dSRandall Stewart 				 */
6899830d754dSRandall Stewart 				;
6900830d754dSRandall Stewart 			} else {
6901830d754dSRandall Stewart 				/*
6902830d754dSRandall Stewart 				 * ok, they have reset us, so we need to
6903830d754dSRandall Stewart 				 * reset the queue this will cause extra
6904830d754dSRandall Stewart 				 * hunting but hey, they chose the
6905830d754dSRandall Stewart 				 * performance hit when they failed to order
6906830d754dSRandall Stewart 				 * there gaps..
6907830d754dSRandall Stewart 				 */
6908830d754dSRandall Stewart 				tp1 = TAILQ_FIRST(&asoc->sent_queue);
6909830d754dSRandall Stewart 			}
6910830d754dSRandall Stewart 			last_frag_high = frag_end + last_tsn;
6911830d754dSRandall Stewart 		}
6912830d754dSRandall Stewart 		for (j = frag_strt; j <= frag_end; j++) {
6913830d754dSRandall Stewart 			theTSN = j + last_tsn;
6914830d754dSRandall Stewart 			while (tp1) {
6915830d754dSRandall Stewart 				if (tp1->rec.data.doing_fast_retransmit)
6916830d754dSRandall Stewart 					num_frs++;
6917830d754dSRandall Stewart 
6918830d754dSRandall Stewart 				/*
6919830d754dSRandall Stewart 				 * CMT: CUCv2 algorithm. For each TSN being
6920830d754dSRandall Stewart 				 * processed from the sent queue, track the
6921830d754dSRandall Stewart 				 * next expected pseudo-cumack, or
6922830d754dSRandall Stewart 				 * rtx_pseudo_cumack, if required. Separate
6923830d754dSRandall Stewart 				 * cumack trackers for first transmissions,
6924830d754dSRandall Stewart 				 * and retransmissions.
6925830d754dSRandall Stewart 				 */
6926830d754dSRandall Stewart 				if ((tp1->whoTo->find_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
6927830d754dSRandall Stewart 				    (tp1->snd_count == 1)) {
6928830d754dSRandall Stewart 					tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
6929830d754dSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 0;
6930830d754dSRandall Stewart 				}
6931830d754dSRandall Stewart 				if ((tp1->whoTo->find_rtx_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
6932830d754dSRandall Stewart 				    (tp1->snd_count > 1)) {
6933830d754dSRandall Stewart 					tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
6934830d754dSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 0;
6935830d754dSRandall Stewart 				}
6936830d754dSRandall Stewart 				if (tp1->rec.data.TSN_seq == theTSN) {
6937830d754dSRandall Stewart 					if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
6938830d754dSRandall Stewart 						/*
6939830d754dSRandall Stewart 						 * must be held until
6940830d754dSRandall Stewart 						 * cum-ack passes
6941830d754dSRandall Stewart 						 */
6942830d754dSRandall Stewart 						/*
6943830d754dSRandall Stewart 						 * ECN Nonce: Add the nonce
6944830d754dSRandall Stewart 						 * value to the sender's
6945830d754dSRandall Stewart 						 * nonce sum
6946830d754dSRandall Stewart 						 */
6947830d754dSRandall Stewart 						if (tp1->sent < SCTP_DATAGRAM_RESEND) {
6948830d754dSRandall Stewart 							/*-
6949830d754dSRandall Stewart 						         * If it is less than RESEND, it is
6950830d754dSRandall Stewart 						         * now no-longer in flight.
6951830d754dSRandall Stewart 						         * Higher values may already be set
6952830d754dSRandall Stewart 						         * via previous Gap Ack Blocks...
6953830d754dSRandall Stewart 						         * i.e. ACKED or RESEND.
6954830d754dSRandall Stewart 						         */
6955830d754dSRandall Stewart 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
6956830d754dSRandall Stewart 							    *biggest_newly_acked_tsn, MAX_TSN)) {
6957830d754dSRandall Stewart 								*biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
6958830d754dSRandall Stewart 							}
6959830d754dSRandall Stewart 							/*
6960830d754dSRandall Stewart 							 * CMT: SFR algo
6961830d754dSRandall Stewart 							 * (and HTNA) - set
6962830d754dSRandall Stewart 							 * saw_newack to 1
6963830d754dSRandall Stewart 							 * for dest being
6964830d754dSRandall Stewart 							 * newly acked.
6965830d754dSRandall Stewart 							 * update
6966830d754dSRandall Stewart 							 * this_sack_highest_
6967830d754dSRandall Stewart 							 * newack if
6968830d754dSRandall Stewart 							 * appropriate.
6969830d754dSRandall Stewart 							 */
6970830d754dSRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0)
6971830d754dSRandall Stewart 								tp1->whoTo->saw_newack = 1;
6972830d754dSRandall Stewart 
6973830d754dSRandall Stewart 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
6974830d754dSRandall Stewart 							    tp1->whoTo->this_sack_highest_newack,
6975830d754dSRandall Stewart 							    MAX_TSN)) {
6976830d754dSRandall Stewart 								tp1->whoTo->this_sack_highest_newack =
6977830d754dSRandall Stewart 								    tp1->rec.data.TSN_seq;
6978830d754dSRandall Stewart 							}
6979830d754dSRandall Stewart 							/*
6980830d754dSRandall Stewart 							 * CMT DAC algo:
6981830d754dSRandall Stewart 							 * also update
6982830d754dSRandall Stewart 							 * this_sack_lowest_n
6983830d754dSRandall Stewart 							 * ewack
6984830d754dSRandall Stewart 							 */
6985830d754dSRandall Stewart 							if (*this_sack_lowest_newack == 0) {
6986830d754dSRandall Stewart 								if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
6987830d754dSRandall Stewart 									sctp_log_sack(*this_sack_lowest_newack,
6988830d754dSRandall Stewart 									    last_tsn,
6989830d754dSRandall Stewart 									    tp1->rec.data.TSN_seq,
6990830d754dSRandall Stewart 									    0,
6991830d754dSRandall Stewart 									    0,
6992830d754dSRandall Stewart 									    SCTP_LOG_TSN_ACKED);
6993830d754dSRandall Stewart 								}
6994830d754dSRandall Stewart 								*this_sack_lowest_newack = tp1->rec.data.TSN_seq;
6995830d754dSRandall Stewart 							}
6996830d754dSRandall Stewart 							/*
6997830d754dSRandall Stewart 							 * CMT: CUCv2
6998830d754dSRandall Stewart 							 * algorithm. If
6999830d754dSRandall Stewart 							 * (rtx-)pseudo-cumac
7000830d754dSRandall Stewart 							 * k for corresp
7001830d754dSRandall Stewart 							 * dest is being
7002830d754dSRandall Stewart 							 * acked, then we
7003830d754dSRandall Stewart 							 * have a new
7004830d754dSRandall Stewart 							 * (rtx-)pseudo-cumac
7005830d754dSRandall Stewart 							 * k. Set
7006830d754dSRandall Stewart 							 * new_(rtx_)pseudo_c
7007830d754dSRandall Stewart 							 * umack to TRUE so
7008830d754dSRandall Stewart 							 * that the cwnd for
7009830d754dSRandall Stewart 							 * this dest can be
7010830d754dSRandall Stewart 							 * updated. Also
7011830d754dSRandall Stewart 							 * trigger search
7012830d754dSRandall Stewart 							 * for the next
7013830d754dSRandall Stewart 							 * expected
7014830d754dSRandall Stewart 							 * (rtx-)pseudo-cumac
7015830d754dSRandall Stewart 							 * k. Separate
7016830d754dSRandall Stewart 							 * pseudo_cumack
7017830d754dSRandall Stewart 							 * trackers for
7018830d754dSRandall Stewart 							 * first
7019830d754dSRandall Stewart 							 * transmissions and
7020830d754dSRandall Stewart 							 * retransmissions.
7021830d754dSRandall Stewart 							 */
7022830d754dSRandall Stewart 							if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
7023830d754dSRandall Stewart 								if (tp1->rec.data.chunk_was_revoked == 0) {
7024830d754dSRandall Stewart 									tp1->whoTo->new_pseudo_cumack = 1;
7025830d754dSRandall Stewart 								}
7026830d754dSRandall Stewart 								tp1->whoTo->find_pseudo_cumack = 1;
7027830d754dSRandall Stewart 							}
7028830d754dSRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7029830d754dSRandall Stewart 								sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
7030830d754dSRandall Stewart 							}
7031830d754dSRandall Stewart 							if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
7032830d754dSRandall Stewart 								if (tp1->rec.data.chunk_was_revoked == 0) {
7033830d754dSRandall Stewart 									tp1->whoTo->new_pseudo_cumack = 1;
7034830d754dSRandall Stewart 								}
7035830d754dSRandall Stewart 								tp1->whoTo->find_rtx_pseudo_cumack = 1;
7036830d754dSRandall Stewart 							}
7037830d754dSRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
7038830d754dSRandall Stewart 								sctp_log_sack(*biggest_newly_acked_tsn,
7039830d754dSRandall Stewart 								    last_tsn,
7040830d754dSRandall Stewart 								    tp1->rec.data.TSN_seq,
7041830d754dSRandall Stewart 								    frag_strt,
7042830d754dSRandall Stewart 								    frag_end,
7043830d754dSRandall Stewart 								    SCTP_LOG_TSN_ACKED);
7044830d754dSRandall Stewart 							}
7045830d754dSRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
7046830d754dSRandall Stewart 								sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
7047830d754dSRandall Stewart 								    tp1->whoTo->flight_size,
7048830d754dSRandall Stewart 								    tp1->book_size,
7049830d754dSRandall Stewart 								    (uintptr_t) tp1->whoTo,
7050830d754dSRandall Stewart 								    tp1->rec.data.TSN_seq);
7051830d754dSRandall Stewart 							}
7052830d754dSRandall Stewart 							sctp_flight_size_decrease(tp1);
7053830d754dSRandall Stewart 							sctp_total_flight_decrease(stcb, tp1);
7054830d754dSRandall Stewart 
7055830d754dSRandall Stewart 							tp1->whoTo->net_ack += tp1->send_size;
7056830d754dSRandall Stewart 							if (tp1->snd_count < 2) {
7057830d754dSRandall Stewart 								/*
7058830d754dSRandall Stewart 								 * True
7059830d754dSRandall Stewart 								 * non-retran
7060830d754dSRandall Stewart 								 * smited
7061830d754dSRandall Stewart 								 * chunk
7062830d754dSRandall Stewart 								 */
7063830d754dSRandall Stewart 								tp1->whoTo->net_ack2 += tp1->send_size;
7064830d754dSRandall Stewart 
7065830d754dSRandall Stewart 								/*
7066830d754dSRandall Stewart 								 * update
7067830d754dSRandall Stewart 								 * RTO too ?
7068830d754dSRandall Stewart 								 */
7069830d754dSRandall Stewart 								if (tp1->do_rtt) {
7070830d754dSRandall Stewart 									tp1->whoTo->RTO =
7071830d754dSRandall Stewart 									    sctp_calculate_rto(stcb,
7072830d754dSRandall Stewart 									    asoc,
7073830d754dSRandall Stewart 									    tp1->whoTo,
7074830d754dSRandall Stewart 									    &tp1->sent_rcv_time,
7075830d754dSRandall Stewart 									    sctp_align_safe_nocopy);
7076830d754dSRandall Stewart 									tp1->do_rtt = 0;
7077830d754dSRandall Stewart 								}
7078830d754dSRandall Stewart 							}
7079830d754dSRandall Stewart 						}
7080830d754dSRandall Stewart 						if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
7081830d754dSRandall Stewart 							(*ecn_seg_sums) += tp1->rec.data.ect_nonce;
7082830d754dSRandall Stewart 							(*ecn_seg_sums) &= SCTP_SACK_NONCE_SUM;
7083830d754dSRandall Stewart 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
7084830d754dSRandall Stewart 							    asoc->this_sack_highest_gap,
7085830d754dSRandall Stewart 							    MAX_TSN)) {
7086830d754dSRandall Stewart 								asoc->this_sack_highest_gap =
7087830d754dSRandall Stewart 								    tp1->rec.data.TSN_seq;
7088830d754dSRandall Stewart 							}
7089830d754dSRandall Stewart 							if (tp1->sent == SCTP_DATAGRAM_RESEND) {
7090830d754dSRandall Stewart 								sctp_ucount_decr(asoc->sent_queue_retran_cnt);
7091830d754dSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
7092830d754dSRandall Stewart 								sctp_audit_log(0xB2,
7093830d754dSRandall Stewart 								    (asoc->sent_queue_retran_cnt & 0x000000ff));
7094830d754dSRandall Stewart #endif
7095830d754dSRandall Stewart 							}
7096830d754dSRandall Stewart 						}
7097830d754dSRandall Stewart 						/*
7098830d754dSRandall Stewart 						 * All chunks NOT UNSENT
7099830d754dSRandall Stewart 						 * fall through here and are
7100830d754dSRandall Stewart 						 * marked
7101830d754dSRandall Stewart 						 */
71028933fa13SRandall Stewart 						if (tp1->sent != SCTP_FORWARD_TSN_SKIP)
71038933fa13SRandall Stewart 							tp1->sent = SCTP_DATAGRAM_NR_MARKED;
7104830d754dSRandall Stewart 						if (tp1->rec.data.chunk_was_revoked) {
7105830d754dSRandall Stewart 							/* deflate the cwnd */
7106830d754dSRandall Stewart 							tp1->whoTo->cwnd -= tp1->book_size;
7107830d754dSRandall Stewart 							tp1->rec.data.chunk_was_revoked = 0;
7108830d754dSRandall Stewart 						}
7109830d754dSRandall Stewart 					}
7110830d754dSRandall Stewart 					break;
7111830d754dSRandall Stewart 				}	/* if (tp1->TSN_seq == theTSN) */
7112830d754dSRandall Stewart 				if (compare_with_wrap(tp1->rec.data.TSN_seq, theTSN,
7113830d754dSRandall Stewart 				    MAX_TSN))
7114830d754dSRandall Stewart 					break;
7115830d754dSRandall Stewart 
7116830d754dSRandall Stewart 				tp1 = TAILQ_NEXT(tp1, sctp_next);
7117830d754dSRandall Stewart 			}	/* end while (tp1) */
7118830d754dSRandall Stewart 		}		/* end for (j = fragStart */
7119830d754dSRandall Stewart 		frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
7120830d754dSRandall Stewart 		    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
7121830d754dSRandall Stewart 		*offset += sizeof(block);
7122830d754dSRandall Stewart 		if (frag == NULL) {
7123830d754dSRandall Stewart 			break;
7124830d754dSRandall Stewart 		}
7125830d754dSRandall Stewart 	}
7126830d754dSRandall Stewart 
7127830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
7128830d754dSRandall Stewart 		if (num_frs)
7129830d754dSRandall Stewart 			sctp_log_fr(*biggest_tsn_acked,
7130830d754dSRandall Stewart 			    *biggest_newly_acked_tsn,
7131830d754dSRandall Stewart 			    last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
7132830d754dSRandall Stewart 	}
7133830d754dSRandall Stewart 	nr_frag = (struct sctp_nr_gap_ack_block *)sctp_m_getptr(m, *offset,
7134830d754dSRandall Stewart 	    sizeof(struct sctp_nr_gap_ack_block), (uint8_t *) & nr_block);
7135830d754dSRandall Stewart 	*offset += sizeof(nr_block);
7136830d754dSRandall Stewart 
7137830d754dSRandall Stewart 
7138830d754dSRandall Stewart 
7139830d754dSRandall Stewart 	if (nr_frag == NULL) {
7140830d754dSRandall Stewart 		return;
7141830d754dSRandall Stewart 	}
7142830d754dSRandall Stewart 	tp1 = NULL;
7143830d754dSRandall Stewart 	last_nr_frag_high = 0;
7144830d754dSRandall Stewart 
7145830d754dSRandall Stewart 	for (i = 0; i < num_nr_seg; i++) {
7146830d754dSRandall Stewart 
7147830d754dSRandall Stewart 		nr_frag_strt = ntohs(nr_frag->start);
7148830d754dSRandall Stewart 		nr_frag_end = ntohs(nr_frag->end);
7149830d754dSRandall Stewart 
7150830d754dSRandall Stewart 		/* some sanity checks on the nr fargment offsets */
7151830d754dSRandall Stewart 		if (nr_frag_strt > nr_frag_end) {
7152830d754dSRandall Stewart 			/* this one is malformed, skip */
7153830d754dSRandall Stewart 			nr_frag++;
7154830d754dSRandall Stewart 			continue;
7155830d754dSRandall Stewart 		}
7156d50c1d79SRandall Stewart 		/* mark acked dgs and find out the highestTSN being acked */
7157830d754dSRandall Stewart 		if (tp1 == NULL) {
7158830d754dSRandall Stewart 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
7159830d754dSRandall Stewart 
7160830d754dSRandall Stewart 			/* save the locations of the last frags */
7161830d754dSRandall Stewart 			last_nr_frag_high = nr_frag_end + last_tsn;
7162830d754dSRandall Stewart 		} else {
7163830d754dSRandall Stewart 			/*
7164d50c1d79SRandall Stewart 			 * now lets see if we need to reset the queue due to
7165d50c1d79SRandall Stewart 			 * a out-of-order SACK fragment
7166830d754dSRandall Stewart 			 */
7167830d754dSRandall Stewart 			if (compare_with_wrap(nr_frag_strt + last_tsn,
7168830d754dSRandall Stewart 			    last_nr_frag_high, MAX_TSN)) {
7169830d754dSRandall Stewart 				/*
7170d50c1d79SRandall Stewart 				 * if the new frag starts after the last TSN
7171d50c1d79SRandall Stewart 				 * frag covered, we are ok and this one is
7172d50c1d79SRandall Stewart 				 * beyond the last one
7173830d754dSRandall Stewart 				 */
7174830d754dSRandall Stewart 				;
7175830d754dSRandall Stewart 			} else {
7176830d754dSRandall Stewart 				/*
7177d50c1d79SRandall Stewart 				 * ok, they have reset us, so we need to
7178d50c1d79SRandall Stewart 				 * reset the queue this will cause extra
7179d50c1d79SRandall Stewart 				 * hunting but hey, they chose the
7180d50c1d79SRandall Stewart 				 * performance hit when they failed to order
7181d50c1d79SRandall Stewart 				 * there gaps..
7182830d754dSRandall Stewart 				 */
7183830d754dSRandall Stewart 				tp1 = TAILQ_FIRST(&asoc->sent_queue);
7184830d754dSRandall Stewart 			}
7185830d754dSRandall Stewart 			last_nr_frag_high = nr_frag_end + last_tsn;
7186830d754dSRandall Stewart 		}
7187830d754dSRandall Stewart 
7188830d754dSRandall Stewart 		for (j = nr_frag_strt + last_tsn; (compare_with_wrap((nr_frag_end + last_tsn), j, MAX_TSN)); j++) {
7189830d754dSRandall Stewart 			while (tp1) {
7190830d754dSRandall Stewart 				if (tp1->rec.data.TSN_seq == j) {
7191830d754dSRandall Stewart 					if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
71928933fa13SRandall Stewart 						if (tp1->sent != SCTP_FORWARD_TSN_SKIP)
7193830d754dSRandall Stewart 							tp1->sent = SCTP_DATAGRAM_NR_MARKED;
7194830d754dSRandall Stewart 						/*
7195d50c1d79SRandall Stewart 						 * TAILQ_REMOVE(&asoc->sent_q
7196d50c1d79SRandall Stewart 						 * ueue, tp1, sctp_next);
7197830d754dSRandall Stewart 						 */
7198830d754dSRandall Stewart 						if (tp1->data) {
7199830d754dSRandall Stewart 							/*
7200830d754dSRandall Stewart 							 * sa_ignore
7201d50c1d79SRandall Stewart 							 * NO_NULL_CHK
7202830d754dSRandall Stewart 							 */
7203830d754dSRandall Stewart 							sctp_free_bufspace(stcb, asoc, tp1, 1);
7204830d754dSRandall Stewart 							sctp_m_freem(tp1->data);
7205830d754dSRandall Stewart 						}
7206830d754dSRandall Stewart 						tp1->data = NULL;
7207d50c1d79SRandall Stewart 						/* asoc->sent_queue_cnt--; */
7208830d754dSRandall Stewart 						/*
7209d50c1d79SRandall Stewart 						 * sctp_free_a_chunk(stcb,
7210d50c1d79SRandall Stewart 						 * tp1);
7211830d754dSRandall Stewart 						 */
7212830d754dSRandall Stewart 						wake_him++;
7213830d754dSRandall Stewart 					}
7214830d754dSRandall Stewart 					break;
7215830d754dSRandall Stewart 				}	/* if (tp1->TSN_seq == j) */
7216830d754dSRandall Stewart 				if (compare_with_wrap(tp1->rec.data.TSN_seq, j,
7217830d754dSRandall Stewart 				    MAX_TSN))
7218830d754dSRandall Stewart 					break;
7219830d754dSRandall Stewart 				tp1 = TAILQ_NEXT(tp1, sctp_next);
7220830d754dSRandall Stewart 			}	/* end while (tp1) */
7221830d754dSRandall Stewart 
7222830d754dSRandall Stewart 		}		/* end for (j = nrFragStart */
7223830d754dSRandall Stewart 
7224830d754dSRandall Stewart 		nr_frag = (struct sctp_nr_gap_ack_block *)sctp_m_getptr(m, *offset,
7225830d754dSRandall Stewart 		    sizeof(struct sctp_nr_gap_ack_block), (uint8_t *) & nr_block);
7226830d754dSRandall Stewart 		*offset += sizeof(nr_block);
7227830d754dSRandall Stewart 		if (nr_frag == NULL) {
7228830d754dSRandall Stewart 			break;
7229830d754dSRandall Stewart 		}
7230830d754dSRandall Stewart 	}
7231d50c1d79SRandall Stewart 
7232830d754dSRandall Stewart 	/*
7233830d754dSRandall Stewart 	 * EY- wake up the socket if things have been removed from the sent
7234830d754dSRandall Stewart 	 * queue
7235830d754dSRandall Stewart 	 */
7236830d754dSRandall Stewart 	if ((wake_him) && (stcb->sctp_socket)) {
7237830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7238830d754dSRandall Stewart 		struct socket *so;
7239830d754dSRandall Stewart 
7240830d754dSRandall Stewart #endif
7241830d754dSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
7242830d754dSRandall Stewart 		/*
7243830d754dSRandall Stewart 		 * if (SCTP_BASE_SYSCTL(sctp_logging_level) &
7244830d754dSRandall Stewart 		 * SCTP_WAKE_LOGGING_ENABLE) { sctp_wakeup_log(stcb,
7245830d754dSRandall Stewart 		 * cum_ack, wake_him, SCTP_WAKESND_FROM_SACK);}
7246830d754dSRandall Stewart 		 */
7247830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7248830d754dSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
7249830d754dSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
7250830d754dSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
7251830d754dSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
7252830d754dSRandall Stewart 		SCTP_TCB_LOCK(stcb);
7253830d754dSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
7254830d754dSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
7255830d754dSRandall Stewart 			/* assoc was freed while we were unlocked */
7256830d754dSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
7257830d754dSRandall Stewart 			return;
7258830d754dSRandall Stewart 		}
7259830d754dSRandall Stewart #endif
7260830d754dSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
7261830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7262830d754dSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
7263830d754dSRandall Stewart #endif
7264830d754dSRandall Stewart 	}			/* else { if
7265830d754dSRandall Stewart 				 * (SCTP_BASE_SYSCTL(sctp_logging_level) &
7266830d754dSRandall Stewart 				 * SCTP_WAKE_LOGGING_ENABLE) {
7267830d754dSRandall Stewart 				 * sctp_wakeup_log(stcb, cum_ack, wake_him,
7268830d754dSRandall Stewart 				 * SCTP_NOWAKE_FROM_SACK); } } */
7269830d754dSRandall Stewart }
7270830d754dSRandall Stewart 
7271830d754dSRandall Stewart /* EY- nr_sack */
7272830d754dSRandall Stewart /* Identifies the non-renegable tsns that are revoked*/
7273830d754dSRandall Stewart static void
7274830d754dSRandall Stewart sctp_check_for_nr_revoked(struct sctp_tcb *stcb,
7275830d754dSRandall Stewart     struct sctp_association *asoc, uint32_t cumack,
7276830d754dSRandall Stewart     u_long biggest_tsn_acked)
7277830d754dSRandall Stewart {
7278830d754dSRandall Stewart 	struct sctp_tmit_chunk *tp1;
7279830d754dSRandall Stewart 
7280830d754dSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
7281830d754dSRandall Stewart 	while (tp1) {
7282830d754dSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq, cumack,
7283830d754dSRandall Stewart 		    MAX_TSN)) {
7284830d754dSRandall Stewart 			/*
7285830d754dSRandall Stewart 			 * ok this guy is either ACK or MARKED. If it is
7286830d754dSRandall Stewart 			 * ACKED it has been previously acked but not this
7287830d754dSRandall Stewart 			 * time i.e. revoked.  If it is MARKED it was ACK'ed
7288830d754dSRandall Stewart 			 * again.
7289830d754dSRandall Stewart 			 */
7290830d754dSRandall Stewart 			if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
7291830d754dSRandall Stewart 			    MAX_TSN))
7292830d754dSRandall Stewart 				break;
7293830d754dSRandall Stewart 
7294830d754dSRandall Stewart 
7295830d754dSRandall Stewart 			if (tp1->sent == SCTP_DATAGRAM_NR_ACKED) {
7296830d754dSRandall Stewart 				/*
7297830d754dSRandall Stewart 				 * EY! a non-renegable TSN is revoked, need
7298830d754dSRandall Stewart 				 * to abort the association
7299830d754dSRandall Stewart 				 */
7300830d754dSRandall Stewart 				/*
7301830d754dSRandall Stewart 				 * EY TODO: put in the code to abort the
7302830d754dSRandall Stewart 				 * assoc.
7303830d754dSRandall Stewart 				 */
7304830d754dSRandall Stewart 				return;
7305830d754dSRandall Stewart 			} else if (tp1->sent == SCTP_DATAGRAM_NR_MARKED) {
7306830d754dSRandall Stewart 				/* it has been re-acked in this SACK */
7307830d754dSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_NR_ACKED;
7308830d754dSRandall Stewart 			}
7309830d754dSRandall Stewart 		}
7310830d754dSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT)
7311830d754dSRandall Stewart 			break;
7312830d754dSRandall Stewart 		tp1 = TAILQ_NEXT(tp1, sctp_next);
7313830d754dSRandall Stewart 	}
7314830d754dSRandall Stewart }
7315830d754dSRandall Stewart 
7316830d754dSRandall Stewart /* EY! nr_sack version of sctp_handle_sack, nr_gap_ack processing should be added to this method*/
7317830d754dSRandall Stewart void
7318830d754dSRandall Stewart sctp_handle_nr_sack(struct mbuf *m, int offset,
7319830d754dSRandall Stewart     struct sctp_nr_sack_chunk *ch, struct sctp_tcb *stcb,
7320830d754dSRandall Stewart     struct sctp_nets *net_from, int *abort_now, int nr_sack_len, uint32_t rwnd)
7321830d754dSRandall Stewart {
7322830d754dSRandall Stewart 	struct sctp_association *asoc;
7323830d754dSRandall Stewart 
7324830d754dSRandall Stewart 	/* EY sack */
7325830d754dSRandall Stewart 	struct sctp_nr_sack *nr_sack;
7326830d754dSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
7327830d754dSRandall Stewart 	uint32_t cum_ack, last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked,
7328830d754dSRandall Stewart 	         this_sack_lowest_newack;
7329830d754dSRandall Stewart 	uint32_t sav_cum_ack;
7330830d754dSRandall Stewart 
7331830d754dSRandall Stewart 	/* EY num_seg */
7332830d754dSRandall Stewart 	uint16_t num_seg, num_nr_seg, num_dup;
7333830d754dSRandall Stewart 	uint16_t wake_him = 0;
7334830d754dSRandall Stewart 	unsigned int nr_sack_length;
7335830d754dSRandall Stewart 	uint32_t send_s = 0;
7336830d754dSRandall Stewart 	long j;
7337830d754dSRandall Stewart 	int accum_moved = 0;
7338830d754dSRandall Stewart 	int will_exit_fast_recovery = 0;
7339830d754dSRandall Stewart 	uint32_t a_rwnd, old_rwnd;
7340830d754dSRandall Stewart 	int win_probe_recovery = 0;
7341830d754dSRandall Stewart 	int win_probe_recovered = 0;
7342830d754dSRandall Stewart 	struct sctp_nets *net = NULL;
7343d50c1d79SRandall Stewart 	int nonce_sum_flag, ecn_seg_sums = 0;
7344830d754dSRandall Stewart 	int done_once;
7345830d754dSRandall Stewart 	uint8_t reneged_all = 0;
7346830d754dSRandall Stewart 	uint8_t cmt_dac_flag;
7347830d754dSRandall Stewart 
7348830d754dSRandall Stewart 	/*
7349830d754dSRandall Stewart 	 * we take any chance we can to service our queues since we cannot
7350830d754dSRandall Stewart 	 * get awoken when the socket is read from :<
7351830d754dSRandall Stewart 	 */
7352830d754dSRandall Stewart 	/*
7353830d754dSRandall Stewart 	 * Now perform the actual SACK handling: 1) Verify that it is not an
7354830d754dSRandall Stewart 	 * old sack, if so discard. 2) If there is nothing left in the send
7355830d754dSRandall Stewart 	 * queue (cum-ack is equal to last acked) then you have a duplicate
7356830d754dSRandall Stewart 	 * too, update any rwnd change and verify no timers are running.
7357830d754dSRandall Stewart 	 * then return. 3) Process any new consequtive data i.e. cum-ack
7358830d754dSRandall Stewart 	 * moved process these first and note that it moved. 4) Process any
7359830d754dSRandall Stewart 	 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
7360830d754dSRandall Stewart 	 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
7361830d754dSRandall Stewart 	 * sync up flightsizes and things, stop all timers and also check
7362830d754dSRandall Stewart 	 * for shutdown_pending state. If so then go ahead and send off the
7363830d754dSRandall Stewart 	 * shutdown. If in shutdown recv, send off the shutdown-ack and
7364830d754dSRandall Stewart 	 * start that timer, Ret. 9) Strike any non-acked things and do FR
7365830d754dSRandall Stewart 	 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
7366830d754dSRandall Stewart 	 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
7367830d754dSRandall Stewart 	 * if in shutdown_recv state.
7368830d754dSRandall Stewart 	 */
7369830d754dSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
7370830d754dSRandall Stewart 	nr_sack = &ch->nr_sack;
7371830d754dSRandall Stewart 	/* CMT DAC algo */
7372830d754dSRandall Stewart 	this_sack_lowest_newack = 0;
7373830d754dSRandall Stewart 	j = 0;
7374830d754dSRandall Stewart 	nr_sack_length = (unsigned int)nr_sack_len;
7375830d754dSRandall Stewart 	/* ECN Nonce */
7376830d754dSRandall Stewart 	SCTP_STAT_INCR(sctps_slowpath_sack);
7377830d754dSRandall Stewart 	nonce_sum_flag = ch->ch.chunk_flags & SCTP_SACK_NONCE_SUM;
7378830d754dSRandall Stewart 	cum_ack = last_tsn = ntohl(nr_sack->cum_tsn_ack);
7379830d754dSRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
7380830d754dSRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
7381830d754dSRandall Stewart 	stcb->asoc.cumack_log_at++;
7382830d754dSRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
7383830d754dSRandall Stewart 		stcb->asoc.cumack_log_at = 0;
7384830d754dSRandall Stewart 	}
7385830d754dSRandall Stewart #endif
7386830d754dSRandall Stewart 	num_seg = ntohs(nr_sack->num_gap_ack_blks);
7387830d754dSRandall Stewart 	num_nr_seg = ntohs(nr_sack->num_nr_gap_ack_blks);
7388830d754dSRandall Stewart 	a_rwnd = rwnd;
7389830d754dSRandall Stewart 
7390830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
7391830d754dSRandall Stewart 		sctp_misc_ints(SCTP_SACK_LOG_NORMAL, cum_ack,
7392830d754dSRandall Stewart 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
7393830d754dSRandall Stewart 	}
7394830d754dSRandall Stewart 	/* CMT DAC algo */
7395830d754dSRandall Stewart 	cmt_dac_flag = ch->ch.chunk_flags & SCTP_SACK_CMT_DAC;
7396830d754dSRandall Stewart 	num_dup = ntohs(nr_sack->num_dup_tsns);
7397830d754dSRandall Stewart 
7398830d754dSRandall Stewart 	old_rwnd = stcb->asoc.peers_rwnd;
7399830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
7400830d754dSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
7401830d754dSRandall Stewart 		    stcb->asoc.overall_error_count,
7402830d754dSRandall Stewart 		    0,
7403830d754dSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
7404830d754dSRandall Stewart 		    __LINE__);
7405830d754dSRandall Stewart 	}
7406830d754dSRandall Stewart 	stcb->asoc.overall_error_count = 0;
7407830d754dSRandall Stewart 	asoc = &stcb->asoc;
7408830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
7409830d754dSRandall Stewart 		sctp_log_sack(asoc->last_acked_seq,
7410830d754dSRandall Stewart 		    cum_ack,
7411830d754dSRandall Stewart 		    0,
7412830d754dSRandall Stewart 		    num_seg,
7413830d754dSRandall Stewart 		    num_dup,
7414830d754dSRandall Stewart 		    SCTP_LOG_NEW_SACK);
7415830d754dSRandall Stewart 	}
7416830d754dSRandall Stewart 	if ((num_dup) && (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_FR_LOGGING_ENABLE | SCTP_EARLYFR_LOGGING_ENABLE))) {
7417830d754dSRandall Stewart 		int off_to_dup, iii;
7418830d754dSRandall Stewart 		uint32_t *dupdata, dblock;
7419830d754dSRandall Stewart 
7420830d754dSRandall Stewart 		off_to_dup = (num_seg * sizeof(struct sctp_gap_ack_block)) +
7421830d754dSRandall Stewart 		    (num_nr_seg * sizeof(struct sctp_nr_gap_ack_block)) + sizeof(struct sctp_nr_sack_chunk);
7422830d754dSRandall Stewart 		if ((off_to_dup + (num_dup * sizeof(uint32_t))) <= nr_sack_length) {
7423830d754dSRandall Stewart 			dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
7424830d754dSRandall Stewart 			    sizeof(uint32_t), (uint8_t *) & dblock);
7425830d754dSRandall Stewart 			off_to_dup += sizeof(uint32_t);
7426830d754dSRandall Stewart 			if (dupdata) {
7427830d754dSRandall Stewart 				for (iii = 0; iii < num_dup; iii++) {
7428830d754dSRandall Stewart 					sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
7429830d754dSRandall Stewart 					dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
7430830d754dSRandall Stewart 					    sizeof(uint32_t), (uint8_t *) & dblock);
7431830d754dSRandall Stewart 					if (dupdata == NULL)
7432830d754dSRandall Stewart 						break;
7433830d754dSRandall Stewart 					off_to_dup += sizeof(uint32_t);
7434830d754dSRandall Stewart 				}
7435830d754dSRandall Stewart 			}
7436830d754dSRandall Stewart 		} else {
7437830d754dSRandall Stewart 			SCTP_PRINTF("Size invalid offset to dups:%d number dups:%d nr_sack_len:%d num gaps:%d num nr_gaps:%d\n",
7438830d754dSRandall Stewart 			    off_to_dup, num_dup, nr_sack_length, num_seg, num_nr_seg);
7439830d754dSRandall Stewart 		}
7440830d754dSRandall Stewart 	}
7441830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
7442830d754dSRandall Stewart 		/* reality check */
7443830d754dSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
7444830d754dSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
7445830d754dSRandall Stewart 			    sctpchunk_listhead);
7446830d754dSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
7447830d754dSRandall Stewart 		} else {
7448830d754dSRandall Stewart 			send_s = asoc->sending_seq;
7449830d754dSRandall Stewart 		}
7450830d754dSRandall Stewart 		if (cum_ack == send_s ||
7451830d754dSRandall Stewart 		    compare_with_wrap(cum_ack, send_s, MAX_TSN)) {
7452830d754dSRandall Stewart #ifndef INVARIANTS
7453830d754dSRandall Stewart 			struct mbuf *oper;
7454830d754dSRandall Stewart 
7455830d754dSRandall Stewart #endif
7456830d754dSRandall Stewart #ifdef INVARIANTS
7457830d754dSRandall Stewart 	hopeless_peer:
7458830d754dSRandall Stewart 			panic("Impossible sack 1");
7459830d754dSRandall Stewart #else
7460830d754dSRandall Stewart 
7461830d754dSRandall Stewart 
7462830d754dSRandall Stewart 			/*
7463830d754dSRandall Stewart 			 * no way, we have not even sent this TSN out yet.
7464830d754dSRandall Stewart 			 * Peer is hopelessly messed up with us.
7465830d754dSRandall Stewart 			 */
7466830d754dSRandall Stewart 	hopeless_peer:
7467830d754dSRandall Stewart 			*abort_now = 1;
7468830d754dSRandall Stewart 			/* XXX */
7469830d754dSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
7470830d754dSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
7471830d754dSRandall Stewart 			if (oper) {
7472830d754dSRandall Stewart 				struct sctp_paramhdr *ph;
7473830d754dSRandall Stewart 				uint32_t *ippp;
7474830d754dSRandall Stewart 
7475830d754dSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
7476830d754dSRandall Stewart 				    sizeof(uint32_t);
7477830d754dSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
7478830d754dSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
7479830d754dSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
7480830d754dSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
7481830d754dSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
7482830d754dSRandall Stewart 			}
7483830d754dSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
7484830d754dSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
7485830d754dSRandall Stewart 			return;
7486830d754dSRandall Stewart #endif
7487830d754dSRandall Stewart 		}
7488830d754dSRandall Stewart 	}
7489830d754dSRandall Stewart 	/**********************/
7490830d754dSRandall Stewart 	/* 1) check the range */
7491830d754dSRandall Stewart 	/**********************/
7492830d754dSRandall Stewart 	if (compare_with_wrap(asoc->last_acked_seq, last_tsn, MAX_TSN)) {
7493830d754dSRandall Stewart 		/* acking something behind */
7494830d754dSRandall Stewart 		return;
7495830d754dSRandall Stewart 	}
7496830d754dSRandall Stewart 	sav_cum_ack = asoc->last_acked_seq;
7497830d754dSRandall Stewart 
7498830d754dSRandall Stewart 	/* update the Rwnd of the peer */
7499830d754dSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue) &&
7500830d754dSRandall Stewart 	    TAILQ_EMPTY(&asoc->send_queue) &&
7501830d754dSRandall Stewart 	    (asoc->stream_queue_cnt == 0)
7502830d754dSRandall Stewart 	    ) {
7503830d754dSRandall Stewart 		/* nothing left on send/sent and strmq */
7504830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
7505830d754dSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
7506830d754dSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
7507830d754dSRandall Stewart 		}
7508830d754dSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
7509830d754dSRandall Stewart 		if (asoc->sent_queue_retran_cnt) {
7510830d754dSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
7511830d754dSRandall Stewart 		}
7512830d754dSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
7513830d754dSRandall Stewart 			/* SWS sender side engages */
7514830d754dSRandall Stewart 			asoc->peers_rwnd = 0;
7515830d754dSRandall Stewart 		}
7516830d754dSRandall Stewart 		/* stop any timers */
7517830d754dSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7518830d754dSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
7519830d754dSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
7520830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
7521830d754dSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
7522830d754dSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck1);
7523830d754dSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
7524830d754dSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
7525830d754dSRandall Stewart 				}
7526830d754dSRandall Stewart 			}
7527830d754dSRandall Stewart 			net->partial_bytes_acked = 0;
7528830d754dSRandall Stewart 			net->flight_size = 0;
7529830d754dSRandall Stewart 		}
7530830d754dSRandall Stewart 		asoc->total_flight = 0;
7531830d754dSRandall Stewart 		asoc->total_flight_count = 0;
7532830d754dSRandall Stewart 		return;
7533830d754dSRandall Stewart 	}
7534830d754dSRandall Stewart 	/*
7535830d754dSRandall Stewart 	 * We init netAckSz and netAckSz2 to 0. These are used to track 2
7536830d754dSRandall Stewart 	 * things. The total byte count acked is tracked in netAckSz AND
7537830d754dSRandall Stewart 	 * netAck2 is used to track the total bytes acked that are un-
7538830d754dSRandall Stewart 	 * amibguious and were never retransmitted. We track these on a per
7539830d754dSRandall Stewart 	 * destination address basis.
7540830d754dSRandall Stewart 	 */
7541830d754dSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7542830d754dSRandall Stewart 		net->prev_cwnd = net->cwnd;
7543830d754dSRandall Stewart 		net->net_ack = 0;
7544830d754dSRandall Stewart 		net->net_ack2 = 0;
7545830d754dSRandall Stewart 
7546830d754dSRandall Stewart 		/*
7547830d754dSRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
7548830d754dSRandall Stewart 		 * SACK processing
7549830d754dSRandall Stewart 		 */
7550830d754dSRandall Stewart 		net->new_pseudo_cumack = 0;
7551830d754dSRandall Stewart 		net->will_exit_fast_recovery = 0;
7552830d754dSRandall Stewart 	}
7553830d754dSRandall Stewart 	/* process the new consecutive TSN first */
7554830d754dSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
7555830d754dSRandall Stewart 	while (tp1) {
7556830d754dSRandall Stewart 		if (compare_with_wrap(last_tsn, tp1->rec.data.TSN_seq,
7557830d754dSRandall Stewart 		    MAX_TSN) ||
7558830d754dSRandall Stewart 		    last_tsn == tp1->rec.data.TSN_seq) {
7559830d754dSRandall Stewart 			if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
7560830d754dSRandall Stewart 				/*
7561830d754dSRandall Stewart 				 * ECN Nonce: Add the nonce to the sender's
7562830d754dSRandall Stewart 				 * nonce sum
7563830d754dSRandall Stewart 				 */
7564830d754dSRandall Stewart 				asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
7565830d754dSRandall Stewart 				accum_moved = 1;
7566830d754dSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
7567830d754dSRandall Stewart 					/*
7568830d754dSRandall Stewart 					 * If it is less than ACKED, it is
7569830d754dSRandall Stewart 					 * now no-longer in flight. Higher
7570830d754dSRandall Stewart 					 * values may occur during marking
7571830d754dSRandall Stewart 					 */
7572830d754dSRandall Stewart 					if ((tp1->whoTo->dest_state &
7573830d754dSRandall Stewart 					    SCTP_ADDR_UNCONFIRMED) &&
7574830d754dSRandall Stewart 					    (tp1->snd_count < 2)) {
7575830d754dSRandall Stewart 						/*
7576830d754dSRandall Stewart 						 * If there was no retran
7577830d754dSRandall Stewart 						 * and the address is
7578830d754dSRandall Stewart 						 * un-confirmed and we sent
7579830d754dSRandall Stewart 						 * there and are now
7580830d754dSRandall Stewart 						 * sacked.. its confirmed,
7581830d754dSRandall Stewart 						 * mark it so.
7582830d754dSRandall Stewart 						 */
7583830d754dSRandall Stewart 						tp1->whoTo->dest_state &=
7584830d754dSRandall Stewart 						    ~SCTP_ADDR_UNCONFIRMED;
7585830d754dSRandall Stewart 					}
7586830d754dSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
7587830d754dSRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
7588830d754dSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
7589830d754dSRandall Stewart 							    tp1->whoTo->flight_size,
7590830d754dSRandall Stewart 							    tp1->book_size,
7591830d754dSRandall Stewart 							    (uintptr_t) tp1->whoTo,
7592830d754dSRandall Stewart 							    tp1->rec.data.TSN_seq);
7593830d754dSRandall Stewart 						}
7594830d754dSRandall Stewart 						sctp_flight_size_decrease(tp1);
7595830d754dSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
7596830d754dSRandall Stewart 					}
7597830d754dSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
7598830d754dSRandall Stewart 
7599830d754dSRandall Stewart 					/* CMT SFR and DAC algos */
7600830d754dSRandall Stewart 					this_sack_lowest_newack = tp1->rec.data.TSN_seq;
7601830d754dSRandall Stewart 					tp1->whoTo->saw_newack = 1;
7602830d754dSRandall Stewart 
7603830d754dSRandall Stewart 					if (tp1->snd_count < 2) {
7604830d754dSRandall Stewart 						/*
7605830d754dSRandall Stewart 						 * True non-retransmited
7606830d754dSRandall Stewart 						 * chunk
7607830d754dSRandall Stewart 						 */
7608830d754dSRandall Stewart 						tp1->whoTo->net_ack2 +=
7609830d754dSRandall Stewart 						    tp1->send_size;
7610830d754dSRandall Stewart 
7611830d754dSRandall Stewart 						/* update RTO too? */
7612830d754dSRandall Stewart 						if (tp1->do_rtt) {
7613830d754dSRandall Stewart 							tp1->whoTo->RTO =
7614830d754dSRandall Stewart 							    sctp_calculate_rto(stcb,
7615830d754dSRandall Stewart 							    asoc, tp1->whoTo,
7616830d754dSRandall Stewart 							    &tp1->sent_rcv_time,
7617830d754dSRandall Stewart 							    sctp_align_safe_nocopy);
7618830d754dSRandall Stewart 							tp1->do_rtt = 0;
7619830d754dSRandall Stewart 						}
7620830d754dSRandall Stewart 					}
7621830d754dSRandall Stewart 					/*
7622830d754dSRandall Stewart 					 * CMT: CUCv2 algorithm. From the
7623830d754dSRandall Stewart 					 * cumack'd TSNs, for each TSN being
7624830d754dSRandall Stewart 					 * acked for the first time, set the
7625830d754dSRandall Stewart 					 * following variables for the
7626830d754dSRandall Stewart 					 * corresp destination.
7627830d754dSRandall Stewart 					 * new_pseudo_cumack will trigger a
7628830d754dSRandall Stewart 					 * cwnd update.
7629830d754dSRandall Stewart 					 * find_(rtx_)pseudo_cumack will
7630830d754dSRandall Stewart 					 * trigger search for the next
7631830d754dSRandall Stewart 					 * expected (rtx-)pseudo-cumack.
7632830d754dSRandall Stewart 					 */
7633830d754dSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
7634830d754dSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
7635830d754dSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
7636830d754dSRandall Stewart 
7637830d754dSRandall Stewart 
7638830d754dSRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
7639830d754dSRandall Stewart 						sctp_log_sack(asoc->last_acked_seq,
7640830d754dSRandall Stewart 						    cum_ack,
7641830d754dSRandall Stewart 						    tp1->rec.data.TSN_seq,
7642830d754dSRandall Stewart 						    0,
7643830d754dSRandall Stewart 						    0,
7644830d754dSRandall Stewart 						    SCTP_LOG_TSN_ACKED);
7645830d754dSRandall Stewart 					}
7646830d754dSRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7647830d754dSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
7648830d754dSRandall Stewart 					}
7649830d754dSRandall Stewart 				}
7650830d754dSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
7651830d754dSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
7652830d754dSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
7653830d754dSRandall Stewart 					sctp_audit_log(0xB3,
7654830d754dSRandall Stewart 					    (asoc->sent_queue_retran_cnt & 0x000000ff));
7655830d754dSRandall Stewart #endif
7656830d754dSRandall Stewart 				}
7657830d754dSRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
7658830d754dSRandall Stewart 					/* deflate the cwnd */
7659830d754dSRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
7660830d754dSRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
7661830d754dSRandall Stewart 				}
7662830d754dSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
7663830d754dSRandall Stewart 			}
7664830d754dSRandall Stewart 		} else {
7665830d754dSRandall Stewart 			break;
7666830d754dSRandall Stewart 		}
7667830d754dSRandall Stewart 		tp1 = TAILQ_NEXT(tp1, sctp_next);
7668830d754dSRandall Stewart 	}
7669830d754dSRandall Stewart 	biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
7670830d754dSRandall Stewart 	/* always set this up to cum-ack */
7671830d754dSRandall Stewart 	asoc->this_sack_highest_gap = last_tsn;
7672830d754dSRandall Stewart 
7673830d754dSRandall Stewart 	/* Move offset up to point to gaps/dups */
7674830d754dSRandall Stewart 	offset += sizeof(struct sctp_nr_sack_chunk);
7675830d754dSRandall Stewart 	if (((num_seg * (sizeof(struct sctp_gap_ack_block))) + sizeof(struct sctp_nr_sack_chunk)) > nr_sack_length) {
7676830d754dSRandall Stewart 
7677830d754dSRandall Stewart 		/* skip corrupt segments */
7678830d754dSRandall Stewart 		goto skip_segments;
7679830d754dSRandall Stewart 	}
7680830d754dSRandall Stewart 	if (num_seg > 0) {
7681830d754dSRandall Stewart 
7682830d754dSRandall Stewart 		/*
7683830d754dSRandall Stewart 		 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
7684830d754dSRandall Stewart 		 * to be greater than the cumack. Also reset saw_newack to 0
7685830d754dSRandall Stewart 		 * for all dests.
7686830d754dSRandall Stewart 		 */
7687830d754dSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7688830d754dSRandall Stewart 			net->saw_newack = 0;
7689830d754dSRandall Stewart 			net->this_sack_highest_newack = last_tsn;
7690830d754dSRandall Stewart 		}
7691830d754dSRandall Stewart 
7692830d754dSRandall Stewart 		/*
7693830d754dSRandall Stewart 		 * thisSackHighestGap will increase while handling NEW
7694830d754dSRandall Stewart 		 * segments this_sack_highest_newack will increase while
7695830d754dSRandall Stewart 		 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
7696830d754dSRandall Stewart 		 * used for CMT DAC algo. saw_newack will also change.
7697830d754dSRandall Stewart 		 */
7698830d754dSRandall Stewart 
7699830d754dSRandall Stewart 		sctp_handle_nr_sack_segments(m, &offset, stcb, asoc, ch, last_tsn,
7700830d754dSRandall Stewart 		    &biggest_tsn_acked, &biggest_tsn_newly_acked, &this_sack_lowest_newack,
7701830d754dSRandall Stewart 		    num_seg, num_nr_seg, &ecn_seg_sums);
7702830d754dSRandall Stewart 
7703830d754dSRandall Stewart 
7704830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
7705830d754dSRandall Stewart 			/*
7706830d754dSRandall Stewart 			 * validate the biggest_tsn_acked in the gap acks if
7707830d754dSRandall Stewart 			 * strict adherence is wanted.
7708830d754dSRandall Stewart 			 */
7709830d754dSRandall Stewart 			if ((biggest_tsn_acked == send_s) ||
7710830d754dSRandall Stewart 			    (compare_with_wrap(biggest_tsn_acked, send_s, MAX_TSN))) {
7711830d754dSRandall Stewart 				/*
7712830d754dSRandall Stewart 				 * peer is either confused or we are under
7713830d754dSRandall Stewart 				 * attack. We must abort.
7714830d754dSRandall Stewart 				 */
7715830d754dSRandall Stewart 				goto hopeless_peer;
7716830d754dSRandall Stewart 			}
7717830d754dSRandall Stewart 		}
7718830d754dSRandall Stewart 	}
7719830d754dSRandall Stewart skip_segments:
7720830d754dSRandall Stewart 	/*******************************************/
7721830d754dSRandall Stewart 	/* cancel ALL T3-send timer if accum moved */
7722830d754dSRandall Stewart 	/*******************************************/
7723830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
7724830d754dSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7725830d754dSRandall Stewart 			if (net->new_pseudo_cumack)
7726830d754dSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
7727830d754dSRandall Stewart 				    stcb, net,
7728830d754dSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
7729830d754dSRandall Stewart 
7730830d754dSRandall Stewart 		}
7731830d754dSRandall Stewart 	} else {
7732830d754dSRandall Stewart 		if (accum_moved) {
7733830d754dSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7734830d754dSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
7735830d754dSRandall Stewart 				    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
7736830d754dSRandall Stewart 			}
7737830d754dSRandall Stewart 		}
7738830d754dSRandall Stewart 	}
7739830d754dSRandall Stewart 	/********************************************/
7740830d754dSRandall Stewart 	/* drop the acked chunks from the sendqueue */
7741830d754dSRandall Stewart 	/********************************************/
7742830d754dSRandall Stewart 	asoc->last_acked_seq = cum_ack;
7743830d754dSRandall Stewart 
7744830d754dSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
7745830d754dSRandall Stewart 	if (tp1 == NULL)
7746830d754dSRandall Stewart 		goto done_with_it;
7747830d754dSRandall Stewart 	do {
7748830d754dSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq, cum_ack,
7749830d754dSRandall Stewart 		    MAX_TSN)) {
7750830d754dSRandall Stewart 			break;
7751830d754dSRandall Stewart 		}
7752830d754dSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
7753830d754dSRandall Stewart 			/* no more sent on list */
7754830d754dSRandall Stewart 			printf("Warning, tp1->sent == %d and its now acked?\n",
7755830d754dSRandall Stewart 			    tp1->sent);
7756830d754dSRandall Stewart 		}
7757830d754dSRandall Stewart 		tp2 = TAILQ_NEXT(tp1, sctp_next);
7758830d754dSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
7759830d754dSRandall Stewart 		if (tp1->pr_sctp_on) {
7760830d754dSRandall Stewart 			if (asoc->pr_sctp_cnt != 0)
7761830d754dSRandall Stewart 				asoc->pr_sctp_cnt--;
7762830d754dSRandall Stewart 		}
7763830d754dSRandall Stewart 		if ((TAILQ_FIRST(&asoc->sent_queue) == NULL) &&
7764830d754dSRandall Stewart 		    (asoc->total_flight > 0)) {
7765830d754dSRandall Stewart #ifdef INVARIANTS
7766830d754dSRandall Stewart 			panic("Warning flight size is postive and should be 0");
7767830d754dSRandall Stewart #else
7768830d754dSRandall Stewart 			SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
7769830d754dSRandall Stewart 			    asoc->total_flight);
7770830d754dSRandall Stewart #endif
7771830d754dSRandall Stewart 			asoc->total_flight = 0;
7772830d754dSRandall Stewart 		}
7773830d754dSRandall Stewart 		if (tp1->data) {
7774830d754dSRandall Stewart 			/* sa_ignore NO_NULL_CHK */
7775830d754dSRandall Stewart 			sctp_free_bufspace(stcb, asoc, tp1, 1);
7776830d754dSRandall Stewart 			sctp_m_freem(tp1->data);
7777830d754dSRandall Stewart 			if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
7778830d754dSRandall Stewart 				asoc->sent_queue_cnt_removeable--;
7779830d754dSRandall Stewart 			}
7780830d754dSRandall Stewart 		}
7781830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
7782830d754dSRandall Stewart 			sctp_log_sack(asoc->last_acked_seq,
7783830d754dSRandall Stewart 			    cum_ack,
7784830d754dSRandall Stewart 			    tp1->rec.data.TSN_seq,
7785830d754dSRandall Stewart 			    0,
7786830d754dSRandall Stewart 			    0,
7787830d754dSRandall Stewart 			    SCTP_LOG_FREE_SENT);
7788830d754dSRandall Stewart 		}
7789830d754dSRandall Stewart 		tp1->data = NULL;
7790830d754dSRandall Stewart 		asoc->sent_queue_cnt--;
7791830d754dSRandall Stewart 		sctp_free_a_chunk(stcb, tp1);
7792830d754dSRandall Stewart 		wake_him++;
7793830d754dSRandall Stewart 		tp1 = tp2;
7794830d754dSRandall Stewart 	} while (tp1 != NULL);
7795830d754dSRandall Stewart 
7796830d754dSRandall Stewart done_with_it:
7797830d754dSRandall Stewart 	/* sa_ignore NO_NULL_CHK */
7798830d754dSRandall Stewart 	if ((wake_him) && (stcb->sctp_socket)) {
7799830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7800830d754dSRandall Stewart 		struct socket *so;
7801830d754dSRandall Stewart 
7802830d754dSRandall Stewart #endif
7803830d754dSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
7804830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
7805830d754dSRandall Stewart 			sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_WAKESND_FROM_SACK);
7806830d754dSRandall Stewart 		}
7807830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7808830d754dSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
7809830d754dSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
7810830d754dSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
7811830d754dSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
7812830d754dSRandall Stewart 		SCTP_TCB_LOCK(stcb);
7813830d754dSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
7814830d754dSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
7815830d754dSRandall Stewart 			/* assoc was freed while we were unlocked */
7816830d754dSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
7817830d754dSRandall Stewart 			return;
7818830d754dSRandall Stewart 		}
7819830d754dSRandall Stewart #endif
7820830d754dSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
7821830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7822830d754dSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
7823830d754dSRandall Stewart #endif
7824830d754dSRandall Stewart 	} else {
7825830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
7826830d754dSRandall Stewart 			sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_NOWAKE_FROM_SACK);
7827830d754dSRandall Stewart 		}
7828830d754dSRandall Stewart 	}
7829830d754dSRandall Stewart 
7830830d754dSRandall Stewart 	if (asoc->fast_retran_loss_recovery && accum_moved) {
7831830d754dSRandall Stewart 		if (compare_with_wrap(asoc->last_acked_seq,
7832830d754dSRandall Stewart 		    asoc->fast_recovery_tsn, MAX_TSN) ||
7833830d754dSRandall Stewart 		    asoc->last_acked_seq == asoc->fast_recovery_tsn) {
7834830d754dSRandall Stewart 			/* Setup so we will exit RFC2582 fast recovery */
7835830d754dSRandall Stewart 			will_exit_fast_recovery = 1;
7836830d754dSRandall Stewart 		}
7837830d754dSRandall Stewart 	}
7838830d754dSRandall Stewart 	/*
7839830d754dSRandall Stewart 	 * Check for revoked fragments:
7840830d754dSRandall Stewart 	 *
7841830d754dSRandall Stewart 	 * if Previous sack - Had no frags then we can't have any revoked if
7842830d754dSRandall Stewart 	 * Previous sack - Had frag's then - If we now have frags aka
7843830d754dSRandall Stewart 	 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
7844830d754dSRandall Stewart 	 * some of them. else - The peer revoked all ACKED fragments, since
7845830d754dSRandall Stewart 	 * we had some before and now we have NONE.
7846830d754dSRandall Stewart 	 */
7847830d754dSRandall Stewart 
7848830d754dSRandall Stewart 	if (num_seg)
7849830d754dSRandall Stewart 		sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
7850830d754dSRandall Stewart 
7851830d754dSRandall Stewart 	else if (asoc->saw_sack_with_frags) {
7852830d754dSRandall Stewart 		int cnt_revoked = 0;
7853830d754dSRandall Stewart 
7854830d754dSRandall Stewart 		tp1 = TAILQ_FIRST(&asoc->sent_queue);
7855830d754dSRandall Stewart 		if (tp1 != NULL) {
7856830d754dSRandall Stewart 			/* Peer revoked all dg's marked or acked */
7857830d754dSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
7858830d754dSRandall Stewart 				/*
7859830d754dSRandall Stewart 				 * EY- maybe check only if it is nr_acked
7860830d754dSRandall Stewart 				 * nr_marked may not be possible
7861830d754dSRandall Stewart 				 */
7862830d754dSRandall Stewart 				if ((tp1->sent == SCTP_DATAGRAM_NR_ACKED) ||
7863830d754dSRandall Stewart 				    (tp1->sent == SCTP_DATAGRAM_NR_MARKED)) {
7864830d754dSRandall Stewart 					/*
7865830d754dSRandall Stewart 					 * EY! - TODO: Something previously
7866830d754dSRandall Stewart 					 * nr_gapped is reneged, abort the
7867830d754dSRandall Stewart 					 * association
7868830d754dSRandall Stewart 					 */
7869830d754dSRandall Stewart 					return;
7870830d754dSRandall Stewart 				}
7871830d754dSRandall Stewart 				if ((tp1->sent > SCTP_DATAGRAM_RESEND) &&
7872830d754dSRandall Stewart 				    (tp1->sent < SCTP_FORWARD_TSN_SKIP)) {
7873830d754dSRandall Stewart 					tp1->sent = SCTP_DATAGRAM_SENT;
7874830d754dSRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
7875830d754dSRandall Stewart 						sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
7876830d754dSRandall Stewart 						    tp1->whoTo->flight_size,
7877830d754dSRandall Stewart 						    tp1->book_size,
7878830d754dSRandall Stewart 						    (uintptr_t) tp1->whoTo,
7879830d754dSRandall Stewart 						    tp1->rec.data.TSN_seq);
7880830d754dSRandall Stewart 					}
7881830d754dSRandall Stewart 					sctp_flight_size_increase(tp1);
7882830d754dSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
7883830d754dSRandall Stewart 					tp1->rec.data.chunk_was_revoked = 1;
7884830d754dSRandall Stewart 					/*
7885830d754dSRandall Stewart 					 * To ensure that this increase in
7886830d754dSRandall Stewart 					 * flightsize, which is artificial,
7887830d754dSRandall Stewart 					 * does not throttle the sender, we
7888830d754dSRandall Stewart 					 * also increase the cwnd
7889830d754dSRandall Stewart 					 * artificially.
7890830d754dSRandall Stewart 					 */
7891830d754dSRandall Stewart 					tp1->whoTo->cwnd += tp1->book_size;
7892830d754dSRandall Stewart 					cnt_revoked++;
7893830d754dSRandall Stewart 				}
7894830d754dSRandall Stewart 			}
7895830d754dSRandall Stewart 			if (cnt_revoked) {
7896830d754dSRandall Stewart 				reneged_all = 1;
7897830d754dSRandall Stewart 			}
7898830d754dSRandall Stewart 		}
7899830d754dSRandall Stewart 		asoc->saw_sack_with_frags = 0;
7900830d754dSRandall Stewart 	}
7901830d754dSRandall Stewart 	if (num_seg)
7902830d754dSRandall Stewart 		asoc->saw_sack_with_frags = 1;
7903830d754dSRandall Stewart 	else
7904830d754dSRandall Stewart 		asoc->saw_sack_with_frags = 0;
7905830d754dSRandall Stewart 
7906830d754dSRandall Stewart 	/* EY! - not sure about if there should be an IF */
7907830d754dSRandall Stewart 	if (num_nr_seg)
7908830d754dSRandall Stewart 		sctp_check_for_nr_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
7909830d754dSRandall Stewart 	else if (asoc->saw_sack_with_nr_frags) {
7910830d754dSRandall Stewart 		/*
7911830d754dSRandall Stewart 		 * EY!- TODO: all previously nr_gapped chunks have been
7912830d754dSRandall Stewart 		 * reneged abort the association
7913830d754dSRandall Stewart 		 */
7914830d754dSRandall Stewart 		asoc->saw_sack_with_nr_frags = 0;
7915830d754dSRandall Stewart 	}
7916830d754dSRandall Stewart 	if (num_nr_seg)
7917830d754dSRandall Stewart 		asoc->saw_sack_with_nr_frags = 1;
7918830d754dSRandall Stewart 	else
7919830d754dSRandall Stewart 		asoc->saw_sack_with_nr_frags = 0;
7920830d754dSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
7921830d754dSRandall Stewart 	asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
7922830d754dSRandall Stewart 
7923830d754dSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
7924830d754dSRandall Stewart 		/* nothing left in-flight */
7925830d754dSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7926830d754dSRandall Stewart 			/* stop all timers */
7927830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
7928830d754dSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
7929830d754dSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
7930830d754dSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
7931830d754dSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_29);
7932830d754dSRandall Stewart 				}
7933830d754dSRandall Stewart 			}
7934830d754dSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
7935830d754dSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
7936830d754dSRandall Stewart 			net->flight_size = 0;
7937830d754dSRandall Stewart 			net->partial_bytes_acked = 0;
7938830d754dSRandall Stewart 		}
7939830d754dSRandall Stewart 		asoc->total_flight = 0;
7940830d754dSRandall Stewart 		asoc->total_flight_count = 0;
7941830d754dSRandall Stewart 	}
7942830d754dSRandall Stewart 	/**********************************/
7943830d754dSRandall Stewart 	/* Now what about shutdown issues */
7944830d754dSRandall Stewart 	/**********************************/
7945830d754dSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
7946830d754dSRandall Stewart 		/* nothing left on sendqueue.. consider done */
7947830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
7948830d754dSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
7949830d754dSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
7950830d754dSRandall Stewart 		}
7951830d754dSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
7952830d754dSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
7953830d754dSRandall Stewart 			/* SWS sender side engages */
7954830d754dSRandall Stewart 			asoc->peers_rwnd = 0;
7955830d754dSRandall Stewart 		}
7956830d754dSRandall Stewart 		/* clean up */
7957830d754dSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
7958830d754dSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7959830d754dSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
7960830d754dSRandall Stewart 		    (asoc->locked_on_sending)
7961830d754dSRandall Stewart 		    ) {
7962830d754dSRandall Stewart 			struct sctp_stream_queue_pending *sp;
7963830d754dSRandall Stewart 
7964830d754dSRandall Stewart 			/*
7965830d754dSRandall Stewart 			 * I may be in a state where we got all across.. but
7966830d754dSRandall Stewart 			 * cannot write more due to a shutdown... we abort
7967830d754dSRandall Stewart 			 * since the user did not indicate EOR in this case.
7968830d754dSRandall Stewart 			 */
7969830d754dSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
7970830d754dSRandall Stewart 			    sctp_streamhead);
7971830d754dSRandall Stewart 			if ((sp) && (sp->length == 0)) {
7972830d754dSRandall Stewart 				asoc->locked_on_sending = NULL;
7973830d754dSRandall Stewart 				if (sp->msg_is_complete) {
7974830d754dSRandall Stewart 					asoc->stream_queue_cnt--;
7975830d754dSRandall Stewart 				} else {
7976830d754dSRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
7977830d754dSRandall Stewart 					asoc->stream_queue_cnt--;
7978830d754dSRandall Stewart 				}
7979830d754dSRandall Stewart 			}
7980830d754dSRandall Stewart 		}
7981830d754dSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
7982830d754dSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
7983830d754dSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
7984830d754dSRandall Stewart 				/* Need to abort here */
7985830d754dSRandall Stewart 				struct mbuf *oper;
7986830d754dSRandall Stewart 
7987830d754dSRandall Stewart 		abort_out_now:
7988830d754dSRandall Stewart 				*abort_now = 1;
7989830d754dSRandall Stewart 				/* XXX */
7990830d754dSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
7991830d754dSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
7992830d754dSRandall Stewart 				if (oper) {
7993830d754dSRandall Stewart 					struct sctp_paramhdr *ph;
7994830d754dSRandall Stewart 					uint32_t *ippp;
7995830d754dSRandall Stewart 
7996830d754dSRandall Stewart 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
7997830d754dSRandall Stewart 					    sizeof(uint32_t);
7998830d754dSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
7999830d754dSRandall Stewart 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
8000830d754dSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
8001830d754dSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
8002830d754dSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_31);
8003830d754dSRandall Stewart 				}
8004830d754dSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
8005830d754dSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
8006830d754dSRandall Stewart 				return;
8007830d754dSRandall Stewart 			} else {
8008830d754dSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
8009830d754dSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
8010830d754dSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
8011830d754dSRandall Stewart 				}
8012830d754dSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
8013830d754dSRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
8014830d754dSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
8015830d754dSRandall Stewart 				sctp_send_shutdown(stcb,
8016830d754dSRandall Stewart 				    stcb->asoc.primary_destination);
8017830d754dSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
8018830d754dSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
8019830d754dSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
8020830d754dSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
8021830d754dSRandall Stewart 			}
8022830d754dSRandall Stewart 			return;
8023830d754dSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
8024830d754dSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
8025830d754dSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
8026830d754dSRandall Stewart 				goto abort_out_now;
8027830d754dSRandall Stewart 			}
8028830d754dSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
8029830d754dSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
8030830d754dSRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
8031830d754dSRandall Stewart 			sctp_send_shutdown_ack(stcb,
8032830d754dSRandall Stewart 			    stcb->asoc.primary_destination);
8033830d754dSRandall Stewart 
8034830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
8035830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, asoc->primary_destination);
8036830d754dSRandall Stewart 			return;
8037830d754dSRandall Stewart 		}
8038830d754dSRandall Stewart 	}
8039830d754dSRandall Stewart 	/*
8040830d754dSRandall Stewart 	 * Now here we are going to recycle net_ack for a different use...
8041830d754dSRandall Stewart 	 * HEADS UP.
8042830d754dSRandall Stewart 	 */
8043830d754dSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8044830d754dSRandall Stewart 		net->net_ack = 0;
8045830d754dSRandall Stewart 	}
8046830d754dSRandall Stewart 
8047830d754dSRandall Stewart 	/*
8048830d754dSRandall Stewart 	 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
8049830d754dSRandall Stewart 	 * to be done. Setting this_sack_lowest_newack to the cum_ack will
8050830d754dSRandall Stewart 	 * automatically ensure that.
8051830d754dSRandall Stewart 	 */
8052830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac) && (cmt_dac_flag == 0)) {
8053830d754dSRandall Stewart 		this_sack_lowest_newack = cum_ack;
8054830d754dSRandall Stewart 	}
8055830d754dSRandall Stewart 	if (num_seg > 0) {
8056830d754dSRandall Stewart 		sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
8057830d754dSRandall Stewart 		    biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
8058830d754dSRandall Stewart 	}
8059830d754dSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
8060830d754dSRandall Stewart 	asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
8061830d754dSRandall Stewart 
8062830d754dSRandall Stewart 	/******************************************************************
8063830d754dSRandall Stewart 	 *  Here we do the stuff with ECN Nonce checking.
8064830d754dSRandall Stewart 	 *  We basically check to see if the nonce sum flag was incorrect
8065830d754dSRandall Stewart 	 *  or if resynchronization needs to be done. Also if we catch a
8066830d754dSRandall Stewart 	 *  misbehaving receiver we give him the kick.
8067830d754dSRandall Stewart 	 ******************************************************************/
8068830d754dSRandall Stewart 
8069830d754dSRandall Stewart 	if (asoc->ecn_nonce_allowed) {
8070830d754dSRandall Stewart 		if (asoc->nonce_sum_check) {
8071830d754dSRandall Stewart 			if (nonce_sum_flag != ((asoc->nonce_sum_expect_base + ecn_seg_sums) & SCTP_SACK_NONCE_SUM)) {
8072830d754dSRandall Stewart 				if (asoc->nonce_wait_for_ecne == 0) {
8073830d754dSRandall Stewart 					struct sctp_tmit_chunk *lchk;
8074830d754dSRandall Stewart 
8075830d754dSRandall Stewart 					lchk = TAILQ_FIRST(&asoc->send_queue);
8076830d754dSRandall Stewart 					asoc->nonce_wait_for_ecne = 1;
8077830d754dSRandall Stewart 					if (lchk) {
8078830d754dSRandall Stewart 						asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
8079830d754dSRandall Stewart 					} else {
8080830d754dSRandall Stewart 						asoc->nonce_wait_tsn = asoc->sending_seq;
8081830d754dSRandall Stewart 					}
8082830d754dSRandall Stewart 				} else {
8083830d754dSRandall Stewart 					if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
8084830d754dSRandall Stewart 					    (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
8085830d754dSRandall Stewart 						/*
8086830d754dSRandall Stewart 						 * Misbehaving peer. We need
8087830d754dSRandall Stewart 						 * to react to this guy
8088830d754dSRandall Stewart 						 */
8089830d754dSRandall Stewart 						asoc->ecn_allowed = 0;
8090830d754dSRandall Stewart 						asoc->ecn_nonce_allowed = 0;
8091830d754dSRandall Stewart 					}
8092830d754dSRandall Stewart 				}
8093830d754dSRandall Stewart 			}
8094830d754dSRandall Stewart 		} else {
8095830d754dSRandall Stewart 			/* See if Resynchronization Possible */
8096830d754dSRandall Stewart 			if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
8097830d754dSRandall Stewart 				asoc->nonce_sum_check = 1;
8098830d754dSRandall Stewart 				/*
8099830d754dSRandall Stewart 				 * now we must calculate what the base is.
8100830d754dSRandall Stewart 				 * We do this based on two things, we know
8101830d754dSRandall Stewart 				 * the total's for all the segments
8102830d754dSRandall Stewart 				 * gap-acked in the SACK, its stored in
8103830d754dSRandall Stewart 				 * ecn_seg_sums. We also know the SACK's
8104830d754dSRandall Stewart 				 * nonce sum, its in nonce_sum_flag. So we
8105830d754dSRandall Stewart 				 * can build a truth table to back-calculate
8106830d754dSRandall Stewart 				 * the new value of
8107830d754dSRandall Stewart 				 * asoc->nonce_sum_expect_base:
8108830d754dSRandall Stewart 				 *
8109830d754dSRandall Stewart 				 * SACK-flag-Value         Seg-Sums Base 0 0 0
8110830d754dSRandall Stewart 				 * 1                    0 1 0 1 1 1 1 0
8111830d754dSRandall Stewart 				 */
8112830d754dSRandall Stewart 				asoc->nonce_sum_expect_base = (ecn_seg_sums ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
8113830d754dSRandall Stewart 			}
8114830d754dSRandall Stewart 		}
8115830d754dSRandall Stewart 	}
8116830d754dSRandall Stewart 	/* Now are we exiting loss recovery ? */
8117830d754dSRandall Stewart 	if (will_exit_fast_recovery) {
8118830d754dSRandall Stewart 		/* Ok, we must exit fast recovery */
8119830d754dSRandall Stewart 		asoc->fast_retran_loss_recovery = 0;
8120830d754dSRandall Stewart 	}
8121830d754dSRandall Stewart 	if ((asoc->sat_t3_loss_recovery) &&
8122830d754dSRandall Stewart 	    ((compare_with_wrap(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn,
8123830d754dSRandall Stewart 	    MAX_TSN) ||
8124830d754dSRandall Stewart 	    (asoc->last_acked_seq == asoc->sat_t3_recovery_tsn)))) {
8125830d754dSRandall Stewart 		/* end satellite t3 loss recovery */
8126830d754dSRandall Stewart 		asoc->sat_t3_loss_recovery = 0;
8127830d754dSRandall Stewart 	}
8128830d754dSRandall Stewart 	/*
8129830d754dSRandall Stewart 	 * CMT Fast recovery
8130830d754dSRandall Stewart 	 */
8131830d754dSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8132830d754dSRandall Stewart 		if (net->will_exit_fast_recovery) {
8133830d754dSRandall Stewart 			/* Ok, we must exit fast recovery */
8134830d754dSRandall Stewart 			net->fast_retran_loss_recovery = 0;
8135830d754dSRandall Stewart 		}
8136830d754dSRandall Stewart 	}
8137830d754dSRandall Stewart 
8138830d754dSRandall Stewart 	/* Adjust and set the new rwnd value */
8139830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
8140830d754dSRandall Stewart 		sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
8141830d754dSRandall Stewart 		    asoc->peers_rwnd, asoc->total_flight, (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)), a_rwnd);
8142830d754dSRandall Stewart 	}
8143830d754dSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
8144830d754dSRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
8145830d754dSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
8146830d754dSRandall Stewart 		/* SWS sender side engages */
8147830d754dSRandall Stewart 		asoc->peers_rwnd = 0;
8148830d754dSRandall Stewart 	}
8149830d754dSRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
8150830d754dSRandall Stewart 		win_probe_recovery = 1;
8151830d754dSRandall Stewart 	}
8152830d754dSRandall Stewart 	/*
8153830d754dSRandall Stewart 	 * Now we must setup so we have a timer up for anyone with
8154830d754dSRandall Stewart 	 * outstanding data.
8155830d754dSRandall Stewart 	 */
8156830d754dSRandall Stewart 	done_once = 0;
8157830d754dSRandall Stewart again:
8158830d754dSRandall Stewart 	j = 0;
8159830d754dSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8160830d754dSRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
8161830d754dSRandall Stewart 			win_probe_recovered = 1;
8162830d754dSRandall Stewart 			/*-
8163830d754dSRandall Stewart 			 * Find first chunk that was used with
8164830d754dSRandall Stewart 			 * window probe and clear the event. Put
8165830d754dSRandall Stewart 			 * it back into the send queue as if has
8166830d754dSRandall Stewart 			 * not been sent.
8167830d754dSRandall Stewart 			 */
8168830d754dSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
8169830d754dSRandall Stewart 				if (tp1->window_probe) {
8170830d754dSRandall Stewart 					sctp_window_probe_recovery(stcb, asoc, net, tp1);
8171830d754dSRandall Stewart 					break;
8172830d754dSRandall Stewart 				}
8173830d754dSRandall Stewart 			}
8174830d754dSRandall Stewart 		}
8175830d754dSRandall Stewart 		if (net->flight_size) {
8176830d754dSRandall Stewart 			j++;
8177830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
8178830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, net);
81795171328bSRandall Stewart 			if (net->window_probe) {
81805171328bSRandall Stewart 				net->window_probe = 0;
81815171328bSRandall Stewart 			}
8182830d754dSRandall Stewart 		} else {
81835171328bSRandall Stewart 			if (net->window_probe) {
81845171328bSRandall Stewart 				net->window_probe = 0;
81855171328bSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SEND,
81865171328bSRandall Stewart 				    stcb->sctp_ep, stcb, net);
81875171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8188830d754dSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
8189830d754dSRandall Stewart 				    stcb, net,
8190830d754dSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
8191830d754dSRandall Stewart 			}
8192830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
8193830d754dSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8194830d754dSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
8195830d754dSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
8196830d754dSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
8197830d754dSRandall Stewart 				}
8198830d754dSRandall Stewart 			}
8199830d754dSRandall Stewart 		}
8200830d754dSRandall Stewart 	}
8201830d754dSRandall Stewart 	if ((j == 0) &&
8202830d754dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
8203830d754dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
8204830d754dSRandall Stewart 	    (win_probe_recovered == 0) &&
8205830d754dSRandall Stewart 	    (done_once == 0)) {
82060c0982b8SRandall Stewart 		/*
82070c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
82080c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
82090c0982b8SRandall Stewart 		 */
82100c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
8211830d754dSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8212830d754dSRandall Stewart 				net->flight_size = 0;
8213830d754dSRandall Stewart 			}
8214830d754dSRandall Stewart 			asoc->total_flight = 0;
8215830d754dSRandall Stewart 			asoc->total_flight_count = 0;
8216830d754dSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
8217830d754dSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
8218830d754dSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
8219830d754dSRandall Stewart 					sctp_flight_size_increase(tp1);
8220830d754dSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
8221830d754dSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
8222830d754dSRandall Stewart 					asoc->sent_queue_retran_cnt++;
8223830d754dSRandall Stewart 				}
8224830d754dSRandall Stewart 			}
82250c0982b8SRandall Stewart 		}
8226830d754dSRandall Stewart 		done_once = 1;
8227830d754dSRandall Stewart 		goto again;
8228830d754dSRandall Stewart 	}
8229830d754dSRandall Stewart 	/*********************************************/
8230830d754dSRandall Stewart 	/* Here we perform PR-SCTP procedures        */
8231830d754dSRandall Stewart 	/* (section 4.2)                             */
8232830d754dSRandall Stewart 	/*********************************************/
8233830d754dSRandall Stewart 	/* C1. update advancedPeerAckPoint */
8234830d754dSRandall Stewart 	if (compare_with_wrap(cum_ack, asoc->advanced_peer_ack_point, MAX_TSN)) {
8235830d754dSRandall Stewart 		asoc->advanced_peer_ack_point = cum_ack;
8236830d754dSRandall Stewart 	}
8237830d754dSRandall Stewart 	/* C2. try to further move advancedPeerAckPoint ahead */
8238830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
8239830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
8240830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
8241830d754dSRandall Stewart 
8242830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
8243830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
8244830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
8245830d754dSRandall Stewart 		if (compare_with_wrap(asoc->advanced_peer_ack_point, cum_ack,
8246830d754dSRandall Stewart 		    MAX_TSN)) {
8247830d754dSRandall Stewart 			/*
8248830d754dSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing for notes
8249830d754dSRandall Stewart 			 * on issues that will occur when the ECN NONCE
8250830d754dSRandall Stewart 			 * stuff is put into SCTP for cross checking.
8251830d754dSRandall Stewart 			 */
8252830d754dSRandall Stewart 			if (compare_with_wrap(asoc->advanced_peer_ack_point, old_adv_peer_ack_point,
8253830d754dSRandall Stewart 			    MAX_TSN)) {
8254830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
8255830d754dSRandall Stewart 				/*
8256830d754dSRandall Stewart 				 * ECN Nonce: Disable Nonce Sum check when
8257830d754dSRandall Stewart 				 * FWD TSN is sent and store resync tsn
8258830d754dSRandall Stewart 				 */
8259830d754dSRandall Stewart 				asoc->nonce_sum_check = 0;
8260830d754dSRandall Stewart 				asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
82610c0982b8SRandall Stewart 			} else if (lchk) {
82620c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
82630c0982b8SRandall Stewart 				lchk->rec.data.fwd_tsn_cnt++;
82640c0982b8SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt > 3) {
82650c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
82660c0982b8SRandall Stewart 					lchk->rec.data.fwd_tsn_cnt = 0;
82670c0982b8SRandall Stewart 				}
8268830d754dSRandall Stewart 			}
8269830d754dSRandall Stewart 		}
8270830d754dSRandall Stewart 		if (lchk) {
8271830d754dSRandall Stewart 			/* Assure a timer is up */
8272830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
8273830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
8274830d754dSRandall Stewart 		}
8275830d754dSRandall Stewart 	}
8276830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
8277830d754dSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
8278830d754dSRandall Stewart 		    a_rwnd,
8279830d754dSRandall Stewart 		    stcb->asoc.peers_rwnd,
8280830d754dSRandall Stewart 		    stcb->asoc.total_flight,
8281830d754dSRandall Stewart 		    stcb->asoc.total_output_queue_size);
8282830d754dSRandall Stewart 	}
8283830d754dSRandall Stewart }
8284