xref: /freebsd/sys/netinet/sctp_indata.c (revision cfde3ff70b3fb7df9711811a75cb627da721c0d1)
1f8829a4aSRandall Stewart /*-
2b1006367SRandall Stewart  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3f8829a4aSRandall Stewart  *
4f8829a4aSRandall Stewart  * Redistribution and use in source and binary forms, with or without
5f8829a4aSRandall Stewart  * modification, are permitted provided that the following conditions are met:
6f8829a4aSRandall Stewart  *
7f8829a4aSRandall Stewart  * a) Redistributions of source code must retain the above copyright notice,
8f8829a4aSRandall Stewart  *   this list of conditions and the following disclaimer.
9f8829a4aSRandall Stewart  *
10f8829a4aSRandall Stewart  * b) Redistributions in binary form must reproduce the above copyright
11f8829a4aSRandall Stewart  *    notice, this list of conditions and the following disclaimer in
12f8829a4aSRandall Stewart  *   the documentation and/or other materials provided with the distribution.
13f8829a4aSRandall Stewart  *
14f8829a4aSRandall Stewart  * c) Neither the name of Cisco Systems, Inc. nor the names of its
15f8829a4aSRandall Stewart  *    contributors may be used to endorse or promote products derived
16f8829a4aSRandall Stewart  *    from this software without specific prior written permission.
17f8829a4aSRandall Stewart  *
18f8829a4aSRandall Stewart  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19f8829a4aSRandall Stewart  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20f8829a4aSRandall Stewart  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21f8829a4aSRandall Stewart  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22f8829a4aSRandall Stewart  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23f8829a4aSRandall Stewart  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24f8829a4aSRandall Stewart  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25f8829a4aSRandall Stewart  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26f8829a4aSRandall Stewart  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27f8829a4aSRandall Stewart  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28f8829a4aSRandall Stewart  * THE POSSIBILITY OF SUCH DAMAGE.
29f8829a4aSRandall Stewart  */
30f8829a4aSRandall Stewart 
31a5d547adSRandall Stewart /* $KAME: sctp_indata.c,v 1.36 2005/03/06 16:04:17 itojun Exp $	 */
32f8829a4aSRandall Stewart 
33f8829a4aSRandall Stewart #include <sys/cdefs.h>
34f8829a4aSRandall Stewart __FBSDID("$FreeBSD$");
35f8829a4aSRandall Stewart 
36f8829a4aSRandall Stewart #include <netinet/sctp_os.h>
37f8829a4aSRandall Stewart #include <netinet/sctp_var.h>
3842551e99SRandall Stewart #include <netinet/sctp_sysctl.h>
39f8829a4aSRandall Stewart #include <netinet/sctp_pcb.h>
40f8829a4aSRandall Stewart #include <netinet/sctp_header.h>
41f8829a4aSRandall Stewart #include <netinet/sctputil.h>
42f8829a4aSRandall Stewart #include <netinet/sctp_output.h>
43f8829a4aSRandall Stewart #include <netinet/sctp_input.h>
44f8829a4aSRandall Stewart #include <netinet/sctp_indata.h>
45f8829a4aSRandall Stewart #include <netinet/sctp_uio.h>
46f8829a4aSRandall Stewart #include <netinet/sctp_timer.h>
47f8829a4aSRandall Stewart 
48d50c1d79SRandall Stewart #define SCTP_CALC_TSN_TO_GAP(gap, tsn, mapping_tsn) do { \
49d50c1d79SRandall Stewart 					if ((compare_with_wrap(tsn, mapping_tsn, MAX_TSN)) || \
50d50c1d79SRandall Stewart                         (tsn == mapping_tsn)) { \
51d50c1d79SRandall Stewart 						gap = tsn - mapping_tsn; \
52d50c1d79SRandall Stewart 					} else { \
53d50c1d79SRandall Stewart 						gap = (MAX_TSN - mapping_tsn) + tsn + 1; \
54d50c1d79SRandall Stewart 					} \
55d50c1d79SRandall Stewart                   } while(0)
56d50c1d79SRandall Stewart 
57d50c1d79SRandall Stewart #define SCTP_REVERSE_OUT_TSN_PRES(nr_gap, tsn, asoc) do { \
58d50c1d79SRandall Stewart                     if (asoc->mapping_array_base_tsn == asoc->nr_mapping_array_base_tsn) { \
59d50c1d79SRandall Stewart                        SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, nr_gap); \
60d50c1d79SRandall Stewart                     } else {\
61d50c1d79SRandall Stewart                        int lgap; \
62d50c1d79SRandall Stewart                        SCTP_CALC_TSN_TO_GAP(lgap, tsn, asoc->mapping_array_base_tsn); \
63d50c1d79SRandall Stewart                        SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, lgap); \
64d50c1d79SRandall Stewart                     } \
65d50c1d79SRandall Stewart                   } while(0)
66f8829a4aSRandall Stewart 
67f8829a4aSRandall Stewart /*
68f8829a4aSRandall Stewart  * NOTES: On the outbound side of things I need to check the sack timer to
69f8829a4aSRandall Stewart  * see if I should generate a sack into the chunk queue (if I have data to
70f8829a4aSRandall Stewart  * send that is and will be sending it .. for bundling.
71f8829a4aSRandall Stewart  *
72f8829a4aSRandall Stewart  * The callback in sctp_usrreq.c will get called when the socket is read from.
73f8829a4aSRandall Stewart  * This will cause sctp_service_queues() to get called on the top entry in
74f8829a4aSRandall Stewart  * the list.
75f8829a4aSRandall Stewart  */
76f8829a4aSRandall Stewart 
7772fb6fdbSRandall Stewart void
78f8829a4aSRandall Stewart sctp_set_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
79f8829a4aSRandall Stewart {
80b3f1ea41SRandall Stewart 	asoc->my_rwnd = sctp_calc_rwnd(stcb, asoc);
81f8829a4aSRandall Stewart }
82f8829a4aSRandall Stewart 
83f8829a4aSRandall Stewart /* Calculate what the rwnd would be */
8472fb6fdbSRandall Stewart uint32_t
85f8829a4aSRandall Stewart sctp_calc_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
86f8829a4aSRandall Stewart {
87b3f1ea41SRandall Stewart 	uint32_t calc = 0;
88f8829a4aSRandall Stewart 
89f8829a4aSRandall Stewart 	/*
90f8829a4aSRandall Stewart 	 * This is really set wrong with respect to a 1-2-m socket. Since
91f8829a4aSRandall Stewart 	 * the sb_cc is the count that everyone as put up. When we re-write
92f8829a4aSRandall Stewart 	 * sctp_soreceive then we will fix this so that ONLY this
93f8829a4aSRandall Stewart 	 * associations data is taken into account.
94f8829a4aSRandall Stewart 	 */
95f8829a4aSRandall Stewart 	if (stcb->sctp_socket == NULL)
96f8829a4aSRandall Stewart 		return (calc);
97f8829a4aSRandall Stewart 
98f8829a4aSRandall Stewart 	if (stcb->asoc.sb_cc == 0 &&
99f8829a4aSRandall Stewart 	    asoc->size_on_reasm_queue == 0 &&
100f8829a4aSRandall Stewart 	    asoc->size_on_all_streams == 0) {
101f8829a4aSRandall Stewart 		/* Full rwnd granted */
102b3f1ea41SRandall Stewart 		calc = max(SCTP_SB_LIMIT_RCV(stcb->sctp_socket), SCTP_MINIMAL_RWND);
103f8829a4aSRandall Stewart 		return (calc);
104f8829a4aSRandall Stewart 	}
105f8829a4aSRandall Stewart 	/* get actual space */
106f8829a4aSRandall Stewart 	calc = (uint32_t) sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv);
107f8829a4aSRandall Stewart 
108f8829a4aSRandall Stewart 	/*
109f8829a4aSRandall Stewart 	 * take out what has NOT been put on socket queue and we yet hold
110f8829a4aSRandall Stewart 	 * for putting up.
111f8829a4aSRandall Stewart 	 */
112f8829a4aSRandall Stewart 	calc = sctp_sbspace_sub(calc, (uint32_t) asoc->size_on_reasm_queue);
113f8829a4aSRandall Stewart 	calc = sctp_sbspace_sub(calc, (uint32_t) asoc->size_on_all_streams);
114f8829a4aSRandall Stewart 
115f8829a4aSRandall Stewart 	if (calc == 0) {
116f8829a4aSRandall Stewart 		/* out of space */
117f8829a4aSRandall Stewart 		return (calc);
118f8829a4aSRandall Stewart 	}
119f8829a4aSRandall Stewart 	/* what is the overhead of all these rwnd's */
1202afb3e84SRandall Stewart 	calc = sctp_sbspace_sub(calc, stcb->asoc.my_rwnd_control_len);
121b3f1ea41SRandall Stewart 	/*
122b3f1ea41SRandall Stewart 	 * If the window gets too small due to ctrl-stuff, reduce it to 1,
123b3f1ea41SRandall Stewart 	 * even it is 0. SWS engaged
124f8829a4aSRandall Stewart 	 */
125b3f1ea41SRandall Stewart 	if (calc < stcb->asoc.my_rwnd_control_len) {
126b3f1ea41SRandall Stewart 		calc = 1;
1272afb3e84SRandall Stewart 	}
128b3f1ea41SRandall Stewart 	return (calc);
129f8829a4aSRandall Stewart }
130f8829a4aSRandall Stewart 
131f8829a4aSRandall Stewart 
132f8829a4aSRandall Stewart 
133f8829a4aSRandall Stewart /*
134f8829a4aSRandall Stewart  * Build out our readq entry based on the incoming packet.
135f8829a4aSRandall Stewart  */
136f8829a4aSRandall Stewart struct sctp_queued_to_read *
137f8829a4aSRandall Stewart sctp_build_readq_entry(struct sctp_tcb *stcb,
138f8829a4aSRandall Stewart     struct sctp_nets *net,
139f8829a4aSRandall Stewart     uint32_t tsn, uint32_t ppid,
140f8829a4aSRandall Stewart     uint32_t context, uint16_t stream_no,
141f8829a4aSRandall Stewart     uint16_t stream_seq, uint8_t flags,
142f8829a4aSRandall Stewart     struct mbuf *dm)
143f8829a4aSRandall Stewart {
144f8829a4aSRandall Stewart 	struct sctp_queued_to_read *read_queue_e = NULL;
145f8829a4aSRandall Stewart 
146f8829a4aSRandall Stewart 	sctp_alloc_a_readq(stcb, read_queue_e);
147f8829a4aSRandall Stewart 	if (read_queue_e == NULL) {
148f8829a4aSRandall Stewart 		goto failed_build;
149f8829a4aSRandall Stewart 	}
150f8829a4aSRandall Stewart 	read_queue_e->sinfo_stream = stream_no;
151f8829a4aSRandall Stewart 	read_queue_e->sinfo_ssn = stream_seq;
152f8829a4aSRandall Stewart 	read_queue_e->sinfo_flags = (flags << 8);
153f8829a4aSRandall Stewart 	read_queue_e->sinfo_ppid = ppid;
154f8829a4aSRandall Stewart 	read_queue_e->sinfo_context = stcb->asoc.context;
155f8829a4aSRandall Stewart 	read_queue_e->sinfo_timetolive = 0;
156f8829a4aSRandall Stewart 	read_queue_e->sinfo_tsn = tsn;
157f8829a4aSRandall Stewart 	read_queue_e->sinfo_cumtsn = tsn;
158f8829a4aSRandall Stewart 	read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
159f8829a4aSRandall Stewart 	read_queue_e->whoFrom = net;
160f8829a4aSRandall Stewart 	read_queue_e->length = 0;
161f8829a4aSRandall Stewart 	atomic_add_int(&net->ref_count, 1);
162f8829a4aSRandall Stewart 	read_queue_e->data = dm;
163139bc87fSRandall Stewart 	read_queue_e->spec_flags = 0;
164f8829a4aSRandall Stewart 	read_queue_e->tail_mbuf = NULL;
16517205eccSRandall Stewart 	read_queue_e->aux_data = NULL;
166f8829a4aSRandall Stewart 	read_queue_e->stcb = stcb;
167f8829a4aSRandall Stewart 	read_queue_e->port_from = stcb->rport;
168f8829a4aSRandall Stewart 	read_queue_e->do_not_ref_stcb = 0;
169f8829a4aSRandall Stewart 	read_queue_e->end_added = 0;
1709a6142d8SRandall Stewart 	read_queue_e->some_taken = 0;
17103b0b021SRandall Stewart 	read_queue_e->pdapi_aborted = 0;
172f8829a4aSRandall Stewart failed_build:
173f8829a4aSRandall Stewart 	return (read_queue_e);
174f8829a4aSRandall Stewart }
175f8829a4aSRandall Stewart 
176f8829a4aSRandall Stewart 
177f8829a4aSRandall Stewart /*
178f8829a4aSRandall Stewart  * Build out our readq entry based on the incoming packet.
179f8829a4aSRandall Stewart  */
180f8829a4aSRandall Stewart static struct sctp_queued_to_read *
181f8829a4aSRandall Stewart sctp_build_readq_entry_chk(struct sctp_tcb *stcb,
182f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk)
183f8829a4aSRandall Stewart {
184f8829a4aSRandall Stewart 	struct sctp_queued_to_read *read_queue_e = NULL;
185f8829a4aSRandall Stewart 
186f8829a4aSRandall Stewart 	sctp_alloc_a_readq(stcb, read_queue_e);
187f8829a4aSRandall Stewart 	if (read_queue_e == NULL) {
188f8829a4aSRandall Stewart 		goto failed_build;
189f8829a4aSRandall Stewart 	}
190f8829a4aSRandall Stewart 	read_queue_e->sinfo_stream = chk->rec.data.stream_number;
191f8829a4aSRandall Stewart 	read_queue_e->sinfo_ssn = chk->rec.data.stream_seq;
192f8829a4aSRandall Stewart 	read_queue_e->sinfo_flags = (chk->rec.data.rcv_flags << 8);
193f8829a4aSRandall Stewart 	read_queue_e->sinfo_ppid = chk->rec.data.payloadtype;
194f8829a4aSRandall Stewart 	read_queue_e->sinfo_context = stcb->asoc.context;
195f8829a4aSRandall Stewart 	read_queue_e->sinfo_timetolive = 0;
196f8829a4aSRandall Stewart 	read_queue_e->sinfo_tsn = chk->rec.data.TSN_seq;
197f8829a4aSRandall Stewart 	read_queue_e->sinfo_cumtsn = chk->rec.data.TSN_seq;
198f8829a4aSRandall Stewart 	read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
199f8829a4aSRandall Stewart 	read_queue_e->whoFrom = chk->whoTo;
20017205eccSRandall Stewart 	read_queue_e->aux_data = NULL;
201f8829a4aSRandall Stewart 	read_queue_e->length = 0;
202f8829a4aSRandall Stewart 	atomic_add_int(&chk->whoTo->ref_count, 1);
203f8829a4aSRandall Stewart 	read_queue_e->data = chk->data;
204f8829a4aSRandall Stewart 	read_queue_e->tail_mbuf = NULL;
205f8829a4aSRandall Stewart 	read_queue_e->stcb = stcb;
206f8829a4aSRandall Stewart 	read_queue_e->port_from = stcb->rport;
207139bc87fSRandall Stewart 	read_queue_e->spec_flags = 0;
208f8829a4aSRandall Stewart 	read_queue_e->do_not_ref_stcb = 0;
209f8829a4aSRandall Stewart 	read_queue_e->end_added = 0;
2109a6142d8SRandall Stewart 	read_queue_e->some_taken = 0;
21103b0b021SRandall Stewart 	read_queue_e->pdapi_aborted = 0;
212f8829a4aSRandall Stewart failed_build:
213f8829a4aSRandall Stewart 	return (read_queue_e);
214f8829a4aSRandall Stewart }
215f8829a4aSRandall Stewart 
216f8829a4aSRandall Stewart 
217f8829a4aSRandall Stewart struct mbuf *
218f8829a4aSRandall Stewart sctp_build_ctl_nchunk(struct sctp_inpcb *inp,
219f8829a4aSRandall Stewart     struct sctp_sndrcvinfo *sinfo)
220f8829a4aSRandall Stewart {
221f8829a4aSRandall Stewart 	struct sctp_sndrcvinfo *outinfo;
222f8829a4aSRandall Stewart 	struct cmsghdr *cmh;
223f8829a4aSRandall Stewart 	struct mbuf *ret;
224f8829a4aSRandall Stewart 	int len;
225f8829a4aSRandall Stewart 	int use_extended = 0;
226f8829a4aSRandall Stewart 
227f8829a4aSRandall Stewart 	if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
228f8829a4aSRandall Stewart 		/* user does not want the sndrcv ctl */
229f8829a4aSRandall Stewart 		return (NULL);
230f8829a4aSRandall Stewart 	}
231f8829a4aSRandall Stewart 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
232f8829a4aSRandall Stewart 		use_extended = 1;
233f8829a4aSRandall Stewart 		len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
234f8829a4aSRandall Stewart 	} else {
235f8829a4aSRandall Stewart 		len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
236f8829a4aSRandall Stewart 	}
237f8829a4aSRandall Stewart 
238f8829a4aSRandall Stewart 
239f8829a4aSRandall Stewart 	ret = sctp_get_mbuf_for_msg(len,
240139bc87fSRandall Stewart 	    0, M_DONTWAIT, 1, MT_DATA);
241f8829a4aSRandall Stewart 
242f8829a4aSRandall Stewart 	if (ret == NULL) {
243f8829a4aSRandall Stewart 		/* No space */
244f8829a4aSRandall Stewart 		return (ret);
245f8829a4aSRandall Stewart 	}
246f8829a4aSRandall Stewart 	/* We need a CMSG header followed by the struct  */
247f8829a4aSRandall Stewart 	cmh = mtod(ret, struct cmsghdr *);
248f8829a4aSRandall Stewart 	outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
249f8829a4aSRandall Stewart 	cmh->cmsg_level = IPPROTO_SCTP;
250f8829a4aSRandall Stewart 	if (use_extended) {
251f8829a4aSRandall Stewart 		cmh->cmsg_type = SCTP_EXTRCV;
252f8829a4aSRandall Stewart 		cmh->cmsg_len = len;
253f8829a4aSRandall Stewart 		memcpy(outinfo, sinfo, len);
254f8829a4aSRandall Stewart 	} else {
255f8829a4aSRandall Stewart 		cmh->cmsg_type = SCTP_SNDRCV;
256f8829a4aSRandall Stewart 		cmh->cmsg_len = len;
257f8829a4aSRandall Stewart 		*outinfo = *sinfo;
258f8829a4aSRandall Stewart 	}
259139bc87fSRandall Stewart 	SCTP_BUF_LEN(ret) = cmh->cmsg_len;
260f8829a4aSRandall Stewart 	return (ret);
261f8829a4aSRandall Stewart }
262f8829a4aSRandall Stewart 
263139bc87fSRandall Stewart 
26417205eccSRandall Stewart char *
26517205eccSRandall Stewart sctp_build_ctl_cchunk(struct sctp_inpcb *inp,
26617205eccSRandall Stewart     int *control_len,
26717205eccSRandall Stewart     struct sctp_sndrcvinfo *sinfo)
26817205eccSRandall Stewart {
26917205eccSRandall Stewart 	struct sctp_sndrcvinfo *outinfo;
27017205eccSRandall Stewart 	struct cmsghdr *cmh;
27117205eccSRandall Stewart 	char *buf;
27217205eccSRandall Stewart 	int len;
27317205eccSRandall Stewart 	int use_extended = 0;
27417205eccSRandall Stewart 
27517205eccSRandall Stewart 	if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
27617205eccSRandall Stewart 		/* user does not want the sndrcv ctl */
27717205eccSRandall Stewart 		return (NULL);
27817205eccSRandall Stewart 	}
27917205eccSRandall Stewart 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
28017205eccSRandall Stewart 		use_extended = 1;
28117205eccSRandall Stewart 		len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
28217205eccSRandall Stewart 	} else {
28317205eccSRandall Stewart 		len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
28417205eccSRandall Stewart 	}
285207304d4SRandall Stewart 	SCTP_MALLOC(buf, char *, len, SCTP_M_CMSG);
28617205eccSRandall Stewart 	if (buf == NULL) {
28717205eccSRandall Stewart 		/* No space */
28817205eccSRandall Stewart 		return (buf);
28917205eccSRandall Stewart 	}
29017205eccSRandall Stewart 	/* We need a CMSG header followed by the struct  */
29117205eccSRandall Stewart 	cmh = (struct cmsghdr *)buf;
29217205eccSRandall Stewart 	outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
29317205eccSRandall Stewart 	cmh->cmsg_level = IPPROTO_SCTP;
29417205eccSRandall Stewart 	if (use_extended) {
29517205eccSRandall Stewart 		cmh->cmsg_type = SCTP_EXTRCV;
29617205eccSRandall Stewart 		cmh->cmsg_len = len;
29717205eccSRandall Stewart 		memcpy(outinfo, sinfo, len);
29817205eccSRandall Stewart 	} else {
29917205eccSRandall Stewart 		cmh->cmsg_type = SCTP_SNDRCV;
30017205eccSRandall Stewart 		cmh->cmsg_len = len;
30117205eccSRandall Stewart 		*outinfo = *sinfo;
30217205eccSRandall Stewart 	}
30317205eccSRandall Stewart 	*control_len = len;
30417205eccSRandall Stewart 	return (buf);
30517205eccSRandall Stewart }
30617205eccSRandall Stewart 
30717205eccSRandall Stewart 
308f8829a4aSRandall Stewart /*
309f8829a4aSRandall Stewart  * We are delivering currently from the reassembly queue. We must continue to
310f8829a4aSRandall Stewart  * deliver until we either: 1) run out of space. 2) run out of sequential
311f8829a4aSRandall Stewart  * TSN's 3) hit the SCTP_DATA_LAST_FRAG flag.
312f8829a4aSRandall Stewart  */
313f8829a4aSRandall Stewart static void
314f8829a4aSRandall Stewart sctp_service_reassembly(struct sctp_tcb *stcb, struct sctp_association *asoc)
315f8829a4aSRandall Stewart {
316f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
317f8829a4aSRandall Stewart 	uint16_t nxt_todel;
318f8829a4aSRandall Stewart 	uint16_t stream_no;
319f8829a4aSRandall Stewart 	int end = 0;
320f8829a4aSRandall Stewart 	int cntDel;
321830d754dSRandall Stewart 
322830d754dSRandall Stewart 	/* EY if any out-of-order delivered, then tag it nr on nr_map */
323830d754dSRandall Stewart 	uint32_t nr_tsn, nr_gap;
324830d754dSRandall Stewart 
325f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control, *ctl, *ctlat;
326f8829a4aSRandall Stewart 
327ad81507eSRandall Stewart 	if (stcb == NULL)
328ad81507eSRandall Stewart 		return;
329ad81507eSRandall Stewart 
330f42a358aSRandall Stewart 	cntDel = stream_no = 0;
331ad81507eSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
332851b7298SRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
333ad81507eSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
334851b7298SRandall Stewart 		/* socket above is long gone or going.. */
335851b7298SRandall Stewart abandon:
336f8829a4aSRandall Stewart 		asoc->fragmented_delivery_inprogress = 0;
337f8829a4aSRandall Stewart 		chk = TAILQ_FIRST(&asoc->reasmqueue);
338f8829a4aSRandall Stewart 		while (chk) {
339f8829a4aSRandall Stewart 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
340f8829a4aSRandall Stewart 			asoc->size_on_reasm_queue -= chk->send_size;
341f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_reasm_queue);
342f8829a4aSRandall Stewart 			/*
343f8829a4aSRandall Stewart 			 * Lose the data pointer, since its in the socket
344f8829a4aSRandall Stewart 			 * buffer
345f8829a4aSRandall Stewart 			 */
346f8829a4aSRandall Stewart 			if (chk->data) {
347f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
348f8829a4aSRandall Stewart 				chk->data = NULL;
349f8829a4aSRandall Stewart 			}
350f8829a4aSRandall Stewart 			/* Now free the address and data */
351f8829a4aSRandall Stewart 			sctp_free_a_chunk(stcb, chk);
3523c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
353f8829a4aSRandall Stewart 			chk = TAILQ_FIRST(&asoc->reasmqueue);
354f8829a4aSRandall Stewart 		}
355f8829a4aSRandall Stewart 		return;
356f8829a4aSRandall Stewart 	}
357f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
358f8829a4aSRandall Stewart 	do {
359f8829a4aSRandall Stewart 		chk = TAILQ_FIRST(&asoc->reasmqueue);
360f8829a4aSRandall Stewart 		if (chk == NULL) {
361f8829a4aSRandall Stewart 			return;
362f8829a4aSRandall Stewart 		}
363f8829a4aSRandall Stewart 		if (chk->rec.data.TSN_seq != (asoc->tsn_last_delivered + 1)) {
364f8829a4aSRandall Stewart 			/* Can't deliver more :< */
365f8829a4aSRandall Stewart 			return;
366f8829a4aSRandall Stewart 		}
367f8829a4aSRandall Stewart 		stream_no = chk->rec.data.stream_number;
368f8829a4aSRandall Stewart 		nxt_todel = asoc->strmin[stream_no].last_sequence_delivered + 1;
369f8829a4aSRandall Stewart 		if (nxt_todel != chk->rec.data.stream_seq &&
370f8829a4aSRandall Stewart 		    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
371f8829a4aSRandall Stewart 			/*
372f8829a4aSRandall Stewart 			 * Not the next sequence to deliver in its stream OR
373f8829a4aSRandall Stewart 			 * unordered
374f8829a4aSRandall Stewart 			 */
375f8829a4aSRandall Stewart 			return;
376f8829a4aSRandall Stewart 		}
377f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
378f8829a4aSRandall Stewart 
379f8829a4aSRandall Stewart 			control = sctp_build_readq_entry_chk(stcb, chk);
380f8829a4aSRandall Stewart 			if (control == NULL) {
381f8829a4aSRandall Stewart 				/* out of memory? */
382f8829a4aSRandall Stewart 				return;
383f8829a4aSRandall Stewart 			}
384f8829a4aSRandall Stewart 			/* save it off for our future deliveries */
385f8829a4aSRandall Stewart 			stcb->asoc.control_pdapi = control;
386f8829a4aSRandall Stewart 			if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
387f8829a4aSRandall Stewart 				end = 1;
388f8829a4aSRandall Stewart 			else
389f8829a4aSRandall Stewart 				end = 0;
390f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep,
391cfde3ff7SRandall Stewart 			    stcb, control, &stcb->sctp_socket->so_rcv, end,
392cfde3ff7SRandall Stewart 			    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
393f8829a4aSRandall Stewart 			cntDel++;
394f8829a4aSRandall Stewart 		} else {
395f8829a4aSRandall Stewart 			if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
396f8829a4aSRandall Stewart 				end = 1;
397f8829a4aSRandall Stewart 			else
398f8829a4aSRandall Stewart 				end = 0;
399f8829a4aSRandall Stewart 			if (sctp_append_to_readq(stcb->sctp_ep, stcb,
400f8829a4aSRandall Stewart 			    stcb->asoc.control_pdapi,
401f8829a4aSRandall Stewart 			    chk->data, end, chk->rec.data.TSN_seq,
402f8829a4aSRandall Stewart 			    &stcb->sctp_socket->so_rcv)) {
403f8829a4aSRandall Stewart 				/*
404f8829a4aSRandall Stewart 				 * something is very wrong, either
405f8829a4aSRandall Stewart 				 * control_pdapi is NULL, or the tail_mbuf
406f8829a4aSRandall Stewart 				 * is corrupt, or there is a EOM already on
407f8829a4aSRandall Stewart 				 * the mbuf chain.
408f8829a4aSRandall Stewart 				 */
409851b7298SRandall Stewart 				if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
410851b7298SRandall Stewart 					goto abandon;
411851b7298SRandall Stewart 				} else {
412df6e0cc3SRandall Stewart #ifdef INVARIANTS
413ad81507eSRandall Stewart 					if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
414f8829a4aSRandall Stewart 						panic("This should not happen control_pdapi NULL?");
415f8829a4aSRandall Stewart 					}
416f8829a4aSRandall Stewart 					/* if we did not panic, it was a EOM */
417f8829a4aSRandall Stewart 					panic("Bad chunking ??");
418df6e0cc3SRandall Stewart #else
419df6e0cc3SRandall Stewart 					if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
420df6e0cc3SRandall Stewart 						SCTP_PRINTF("This should not happen control_pdapi NULL?\n");
421df6e0cc3SRandall Stewart 					}
422df6e0cc3SRandall Stewart 					SCTP_PRINTF("Bad chunking ??\n");
423df6e0cc3SRandall Stewart 					SCTP_PRINTF("Dumping re-assembly queue this will probably hose the association\n");
424df6e0cc3SRandall Stewart 
425df6e0cc3SRandall Stewart #endif
426df6e0cc3SRandall Stewart 					goto abandon;
427f8829a4aSRandall Stewart 				}
428851b7298SRandall Stewart 			}
429f8829a4aSRandall Stewart 			cntDel++;
430f8829a4aSRandall Stewart 		}
431f8829a4aSRandall Stewart 		/* pull it we did it */
432f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
433830d754dSRandall Stewart 		/*
434830d754dSRandall Stewart 		 * EY this is the chunk that should be tagged nr gapped
435830d754dSRandall Stewart 		 * calculate the gap and such then tag this TSN nr
436830d754dSRandall Stewart 		 * chk->rec.data.TSN_seq
437830d754dSRandall Stewart 		 */
438830d754dSRandall Stewart 		/*
439830d754dSRandall Stewart 		 * EY!-TODO- this tsn should be tagged nr only if it is
440830d754dSRandall Stewart 		 * out-of-order, the if statement should be modified
441830d754dSRandall Stewart 		 */
442830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
443830d754dSRandall Stewart 
444830d754dSRandall Stewart 			nr_tsn = chk->rec.data.TSN_seq;
445d50c1d79SRandall Stewart 			SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
4468933fa13SRandall Stewart 			if ((nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3)) ||
447830d754dSRandall Stewart 			    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
448830d754dSRandall Stewart 				/*
449830d754dSRandall Stewart 				 * EY The 1st should never happen, as in
450830d754dSRandall Stewart 				 * process_a_data_chunk method this check
451830d754dSRandall Stewart 				 * should be done
452830d754dSRandall Stewart 				 */
453830d754dSRandall Stewart 				/*
454830d754dSRandall Stewart 				 * EY The 2nd should never happen, because
455830d754dSRandall Stewart 				 * nr_mapping_array is always expanded when
456830d754dSRandall Stewart 				 * mapping_array is expanded
457830d754dSRandall Stewart 				 */
4588933fa13SRandall Stewart 				printf("Impossible nr_gap ack range failed\n");
459830d754dSRandall Stewart 			} else {
460830d754dSRandall Stewart 				SCTP_TCB_LOCK_ASSERT(stcb);
461830d754dSRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
462d50c1d79SRandall Stewart 				SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
4638933fa13SRandall Stewart 				if (compare_with_wrap(nr_tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
464830d754dSRandall Stewart 					asoc->highest_tsn_inside_nr_map = nr_tsn;
465830d754dSRandall Stewart 			}
466830d754dSRandall Stewart 		}
467f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
468f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 0;
469f8829a4aSRandall Stewart 			if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
470f8829a4aSRandall Stewart 				asoc->strmin[stream_no].last_sequence_delivered++;
471f8829a4aSRandall Stewart 			}
472f8829a4aSRandall Stewart 			if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
473f8829a4aSRandall Stewart 				SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
474f8829a4aSRandall Stewart 			}
475f8829a4aSRandall Stewart 		} else if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
476f8829a4aSRandall Stewart 			/*
477f8829a4aSRandall Stewart 			 * turn the flag back on since we just  delivered
478f8829a4aSRandall Stewart 			 * yet another one.
479f8829a4aSRandall Stewart 			 */
480f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 1;
481f8829a4aSRandall Stewart 		}
482f8829a4aSRandall Stewart 		asoc->tsn_of_pdapi_last_delivered = chk->rec.data.TSN_seq;
483f8829a4aSRandall Stewart 		asoc->last_flags_delivered = chk->rec.data.rcv_flags;
484f8829a4aSRandall Stewart 		asoc->last_strm_seq_delivered = chk->rec.data.stream_seq;
485f8829a4aSRandall Stewart 		asoc->last_strm_no_delivered = chk->rec.data.stream_number;
486f8829a4aSRandall Stewart 
487f8829a4aSRandall Stewart 		asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
488f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue -= chk->send_size;
489f8829a4aSRandall Stewart 		sctp_ucount_decr(asoc->cnt_on_reasm_queue);
490f8829a4aSRandall Stewart 		/* free up the chk */
491f8829a4aSRandall Stewart 		chk->data = NULL;
492f8829a4aSRandall Stewart 		sctp_free_a_chunk(stcb, chk);
493f8829a4aSRandall Stewart 
494f8829a4aSRandall Stewart 		if (asoc->fragmented_delivery_inprogress == 0) {
495f8829a4aSRandall Stewart 			/*
496f8829a4aSRandall Stewart 			 * Now lets see if we can deliver the next one on
497f8829a4aSRandall Stewart 			 * the stream
498f8829a4aSRandall Stewart 			 */
499f8829a4aSRandall Stewart 			struct sctp_stream_in *strm;
500f8829a4aSRandall Stewart 
501f8829a4aSRandall Stewart 			strm = &asoc->strmin[stream_no];
502f8829a4aSRandall Stewart 			nxt_todel = strm->last_sequence_delivered + 1;
503f8829a4aSRandall Stewart 			ctl = TAILQ_FIRST(&strm->inqueue);
504f8829a4aSRandall Stewart 			if (ctl && (nxt_todel == ctl->sinfo_ssn)) {
505f8829a4aSRandall Stewart 				while (ctl != NULL) {
506f8829a4aSRandall Stewart 					/* Deliver more if we can. */
507f8829a4aSRandall Stewart 					if (nxt_todel == ctl->sinfo_ssn) {
508f8829a4aSRandall Stewart 						ctlat = TAILQ_NEXT(ctl, next);
509f8829a4aSRandall Stewart 						TAILQ_REMOVE(&strm->inqueue, ctl, next);
510f8829a4aSRandall Stewart 						asoc->size_on_all_streams -= ctl->length;
511f8829a4aSRandall Stewart 						sctp_ucount_decr(asoc->cnt_on_all_streams);
512f8829a4aSRandall Stewart 						strm->last_sequence_delivered++;
513830d754dSRandall Stewart 						/*
514830d754dSRandall Stewart 						 * EY will be used to
515830d754dSRandall Stewart 						 * calculate nr-gap
516830d754dSRandall Stewart 						 */
517830d754dSRandall Stewart 						nr_tsn = ctl->sinfo_tsn;
518f8829a4aSRandall Stewart 						sctp_add_to_readq(stcb->sctp_ep, stcb,
519f8829a4aSRandall Stewart 						    ctl,
520cfde3ff7SRandall Stewart 						    &stcb->sctp_socket->so_rcv, 1,
521cfde3ff7SRandall Stewart 						    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
522830d754dSRandall Stewart 						/*
523830d754dSRandall Stewart 						 * EY -now something is
524830d754dSRandall Stewart 						 * delivered, calculate
525830d754dSRandall Stewart 						 * nr_gap and tag this tsn
526830d754dSRandall Stewart 						 * NR
527830d754dSRandall Stewart 						 */
528830d754dSRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
529d50c1d79SRandall Stewart 							SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
530830d754dSRandall Stewart 							if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
531830d754dSRandall Stewart 							    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
532d50c1d79SRandall Stewart 								printf("Impossible NR gap calculation?\n");
533830d754dSRandall Stewart 								/*
534830d754dSRandall Stewart 								 * EY The
535830d754dSRandall Stewart 								 * 1st
536830d754dSRandall Stewart 								 * should
537830d754dSRandall Stewart 								 * never
538830d754dSRandall Stewart 								 * happen,
539830d754dSRandall Stewart 								 * as in
540830d754dSRandall Stewart 								 * process_a_
541830d754dSRandall Stewart 								 * data_chunk
542830d754dSRandall Stewart 								 *  method
543830d754dSRandall Stewart 								 * this
544830d754dSRandall Stewart 								 * check
545830d754dSRandall Stewart 								 * should be
546830d754dSRandall Stewart 								 * done
547830d754dSRandall Stewart 								 */
548830d754dSRandall Stewart 								/*
549830d754dSRandall Stewart 								 * EY The
550830d754dSRandall Stewart 								 * 2nd
551830d754dSRandall Stewart 								 * should
552830d754dSRandall Stewart 								 * never
553830d754dSRandall Stewart 								 * happen,
554830d754dSRandall Stewart 								 * because
555830d754dSRandall Stewart 								 * nr_mapping
556830d754dSRandall Stewart 								 * _array is
557830d754dSRandall Stewart 								 * always
558830d754dSRandall Stewart 								 * expanded
559830d754dSRandall Stewart 								 * when
560830d754dSRandall Stewart 								 * mapping_ar
561830d754dSRandall Stewart 								 * ray is
562830d754dSRandall Stewart 								 * expanded
563830d754dSRandall Stewart 								 */
564830d754dSRandall Stewart 							} else {
565830d754dSRandall Stewart 								SCTP_TCB_LOCK_ASSERT(stcb);
566830d754dSRandall Stewart 								SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
567d50c1d79SRandall Stewart 								SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
5688933fa13SRandall Stewart 								if (compare_with_wrap(nr_tsn,
5698933fa13SRandall Stewart 								    asoc->highest_tsn_inside_nr_map,
5708933fa13SRandall Stewart 								    MAX_TSN))
571830d754dSRandall Stewart 									asoc->highest_tsn_inside_nr_map = nr_tsn;
572830d754dSRandall Stewart 							}
573830d754dSRandall Stewart 						}
574f8829a4aSRandall Stewart 						ctl = ctlat;
575f8829a4aSRandall Stewart 					} else {
576f8829a4aSRandall Stewart 						break;
577f8829a4aSRandall Stewart 					}
578f8829a4aSRandall Stewart 					nxt_todel = strm->last_sequence_delivered + 1;
579f8829a4aSRandall Stewart 				}
580f8829a4aSRandall Stewart 			}
581139bc87fSRandall Stewart 			break;
582f8829a4aSRandall Stewart 		}
5833c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
584f8829a4aSRandall Stewart 		chk = TAILQ_FIRST(&asoc->reasmqueue);
585f8829a4aSRandall Stewart 	} while (chk);
586f8829a4aSRandall Stewart }
587f8829a4aSRandall Stewart 
588f8829a4aSRandall Stewart /*
589f8829a4aSRandall Stewart  * Queue the chunk either right into the socket buffer if it is the next one
590f8829a4aSRandall Stewart  * to go OR put it in the correct place in the delivery queue.  If we do
591f8829a4aSRandall Stewart  * append to the so_buf, keep doing so until we are out of order. One big
592f8829a4aSRandall Stewart  * question still remains, what to do when the socket buffer is FULL??
593f8829a4aSRandall Stewart  */
594f8829a4aSRandall Stewart static void
595f8829a4aSRandall Stewart sctp_queue_data_to_stream(struct sctp_tcb *stcb, struct sctp_association *asoc,
596f8829a4aSRandall Stewart     struct sctp_queued_to_read *control, int *abort_flag)
597f8829a4aSRandall Stewart {
598f8829a4aSRandall Stewart 	/*
599f8829a4aSRandall Stewart 	 * FIX-ME maybe? What happens when the ssn wraps? If we are getting
600f8829a4aSRandall Stewart 	 * all the data in one stream this could happen quite rapidly. One
601f8829a4aSRandall Stewart 	 * could use the TSN to keep track of things, but this scheme breaks
602f8829a4aSRandall Stewart 	 * down in the other type of stream useage that could occur. Send a
603f8829a4aSRandall Stewart 	 * single msg to stream 0, send 4Billion messages to stream 1, now
604f8829a4aSRandall Stewart 	 * send a message to stream 0. You have a situation where the TSN
605f8829a4aSRandall Stewart 	 * has wrapped but not in the stream. Is this worth worrying about
606f8829a4aSRandall Stewart 	 * or should we just change our queue sort at the bottom to be by
607f8829a4aSRandall Stewart 	 * TSN.
608f8829a4aSRandall Stewart 	 *
609f8829a4aSRandall Stewart 	 * Could it also be legal for a peer to send ssn 1 with TSN 2 and ssn 2
610f8829a4aSRandall Stewart 	 * with TSN 1? If the peer is doing some sort of funky TSN/SSN
611f8829a4aSRandall Stewart 	 * assignment this could happen... and I don't see how this would be
612f8829a4aSRandall Stewart 	 * a violation. So for now I am undecided an will leave the sort by
613f8829a4aSRandall Stewart 	 * SSN alone. Maybe a hybred approach is the answer
614f8829a4aSRandall Stewart 	 *
615f8829a4aSRandall Stewart 	 */
616f8829a4aSRandall Stewart 	struct sctp_stream_in *strm;
617f8829a4aSRandall Stewart 	struct sctp_queued_to_read *at;
618f8829a4aSRandall Stewart 	int queue_needed;
619f8829a4aSRandall Stewart 	uint16_t nxt_todel;
620f8829a4aSRandall Stewart 	struct mbuf *oper;
621f8829a4aSRandall Stewart 
622830d754dSRandall Stewart 	/* EY- will be used to calculate nr-gap for a tsn */
623830d754dSRandall Stewart 	uint32_t nr_tsn, nr_gap;
624830d754dSRandall Stewart 
625f8829a4aSRandall Stewart 	queue_needed = 1;
626f8829a4aSRandall Stewart 	asoc->size_on_all_streams += control->length;
627f8829a4aSRandall Stewart 	sctp_ucount_incr(asoc->cnt_on_all_streams);
628f8829a4aSRandall Stewart 	strm = &asoc->strmin[control->sinfo_stream];
629f8829a4aSRandall Stewart 	nxt_todel = strm->last_sequence_delivered + 1;
630b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
631f8829a4aSRandall Stewart 		sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INTO_STRD);
63280fefe0aSRandall Stewart 	}
633ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INDATA1,
634ad81507eSRandall Stewart 	    "queue to stream called for ssn:%u lastdel:%u nxt:%u\n",
635f8829a4aSRandall Stewart 	    (uint32_t) control->sinfo_stream,
636ad81507eSRandall Stewart 	    (uint32_t) strm->last_sequence_delivered,
637ad81507eSRandall Stewart 	    (uint32_t) nxt_todel);
638f8829a4aSRandall Stewart 	if (compare_with_wrap(strm->last_sequence_delivered,
639f8829a4aSRandall Stewart 	    control->sinfo_ssn, MAX_SEQ) ||
640f8829a4aSRandall Stewart 	    (strm->last_sequence_delivered == control->sinfo_ssn)) {
641f8829a4aSRandall Stewart 		/* The incoming sseq is behind where we last delivered? */
642ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1, "Duplicate S-SEQ:%d delivered:%d from peer, Abort  association\n",
643ad81507eSRandall Stewart 		    control->sinfo_ssn, strm->last_sequence_delivered);
644c40e9cf2SRandall Stewart protocol_error:
645f8829a4aSRandall Stewart 		/*
646f8829a4aSRandall Stewart 		 * throw it in the stream so it gets cleaned up in
647f8829a4aSRandall Stewart 		 * association destruction
648f8829a4aSRandall Stewart 		 */
649f8829a4aSRandall Stewart 		TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
650139bc87fSRandall Stewart 		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
651f8829a4aSRandall Stewart 		    0, M_DONTWAIT, 1, MT_DATA);
652f8829a4aSRandall Stewart 		if (oper) {
653f8829a4aSRandall Stewart 			struct sctp_paramhdr *ph;
654f8829a4aSRandall Stewart 			uint32_t *ippp;
655f8829a4aSRandall Stewart 
656139bc87fSRandall Stewart 			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
657f8829a4aSRandall Stewart 			    (sizeof(uint32_t) * 3);
658f8829a4aSRandall Stewart 			ph = mtod(oper, struct sctp_paramhdr *);
659f8829a4aSRandall Stewart 			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
660139bc87fSRandall Stewart 			ph->param_length = htons(SCTP_BUF_LEN(oper));
661f8829a4aSRandall Stewart 			ippp = (uint32_t *) (ph + 1);
662a5d547adSRandall Stewart 			*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_1);
663f8829a4aSRandall Stewart 			ippp++;
664f8829a4aSRandall Stewart 			*ippp = control->sinfo_tsn;
665f8829a4aSRandall Stewart 			ippp++;
666f8829a4aSRandall Stewart 			*ippp = ((control->sinfo_stream << 16) | control->sinfo_ssn);
667f8829a4aSRandall Stewart 		}
668a5d547adSRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_1;
669f8829a4aSRandall Stewart 		sctp_abort_an_association(stcb->sctp_ep, stcb,
670ceaad40aSRandall Stewart 		    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
671f8829a4aSRandall Stewart 
672f8829a4aSRandall Stewart 		*abort_flag = 1;
673f8829a4aSRandall Stewart 		return;
674f8829a4aSRandall Stewart 
675f8829a4aSRandall Stewart 	}
676f8829a4aSRandall Stewart 	if (nxt_todel == control->sinfo_ssn) {
677f8829a4aSRandall Stewart 		/* can be delivered right away? */
678b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
679f8829a4aSRandall Stewart 			sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_IMMED_DEL);
68080fefe0aSRandall Stewart 		}
681830d754dSRandall Stewart 		/* EY it wont be queued if it could be delivered directly */
682f8829a4aSRandall Stewart 		queue_needed = 0;
683f8829a4aSRandall Stewart 		asoc->size_on_all_streams -= control->length;
684f8829a4aSRandall Stewart 		sctp_ucount_decr(asoc->cnt_on_all_streams);
685f8829a4aSRandall Stewart 		strm->last_sequence_delivered++;
686830d754dSRandall Stewart 		/* EY will be used to calculate nr-gap */
687830d754dSRandall Stewart 		nr_tsn = control->sinfo_tsn;
688f8829a4aSRandall Stewart 		sctp_add_to_readq(stcb->sctp_ep, stcb,
689f8829a4aSRandall Stewart 		    control,
690cfde3ff7SRandall Stewart 		    &stcb->sctp_socket->so_rcv, 1,
691cfde3ff7SRandall Stewart 		    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
692830d754dSRandall Stewart 		/*
693830d754dSRandall Stewart 		 * EY this is the chunk that should be tagged nr gapped
694830d754dSRandall Stewart 		 * calculate the gap and such then tag this TSN nr
695830d754dSRandall Stewart 		 * chk->rec.data.TSN_seq
696830d754dSRandall Stewart 		 */
697830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
698d50c1d79SRandall Stewart 			SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
699830d754dSRandall Stewart 			if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
700830d754dSRandall Stewart 			    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
701d50c1d79SRandall Stewart 				printf("Impossible nr_tsn set 2?\n");
702830d754dSRandall Stewart 				/*
703830d754dSRandall Stewart 				 * EY The 1st should never happen, as in
704830d754dSRandall Stewart 				 * process_a_data_chunk method this check
705830d754dSRandall Stewart 				 * should be done
706830d754dSRandall Stewart 				 */
707830d754dSRandall Stewart 				/*
708830d754dSRandall Stewart 				 * EY The 2nd should never happen, because
709830d754dSRandall Stewart 				 * nr_mapping_array is always expanded when
710830d754dSRandall Stewart 				 * mapping_array is expanded
711830d754dSRandall Stewart 				 */
712830d754dSRandall Stewart 			} else {
713830d754dSRandall Stewart 				SCTP_TCB_LOCK_ASSERT(stcb);
714830d754dSRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
715d50c1d79SRandall Stewart 				SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
7168933fa13SRandall Stewart 				if (compare_with_wrap(nr_tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
717830d754dSRandall Stewart 					asoc->highest_tsn_inside_nr_map = nr_tsn;
718830d754dSRandall Stewart 			}
719830d754dSRandall Stewart 		}
720f8829a4aSRandall Stewart 		control = TAILQ_FIRST(&strm->inqueue);
721f8829a4aSRandall Stewart 		while (control != NULL) {
722f8829a4aSRandall Stewart 			/* all delivered */
723f8829a4aSRandall Stewart 			nxt_todel = strm->last_sequence_delivered + 1;
724f8829a4aSRandall Stewart 			if (nxt_todel == control->sinfo_ssn) {
725f8829a4aSRandall Stewart 				at = TAILQ_NEXT(control, next);
726f8829a4aSRandall Stewart 				TAILQ_REMOVE(&strm->inqueue, control, next);
727f8829a4aSRandall Stewart 				asoc->size_on_all_streams -= control->length;
728f8829a4aSRandall Stewart 				sctp_ucount_decr(asoc->cnt_on_all_streams);
729f8829a4aSRandall Stewart 				strm->last_sequence_delivered++;
730f8829a4aSRandall Stewart 				/*
731f8829a4aSRandall Stewart 				 * We ignore the return of deliver_data here
732f8829a4aSRandall Stewart 				 * since we always can hold the chunk on the
733f8829a4aSRandall Stewart 				 * d-queue. And we have a finite number that
734f8829a4aSRandall Stewart 				 * can be delivered from the strq.
735f8829a4aSRandall Stewart 				 */
736b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
737f8829a4aSRandall Stewart 					sctp_log_strm_del(control, NULL,
738f8829a4aSRandall Stewart 					    SCTP_STR_LOG_FROM_IMMED_DEL);
73980fefe0aSRandall Stewart 				}
740830d754dSRandall Stewart 				/* EY will be used to calculate nr-gap */
741830d754dSRandall Stewart 				nr_tsn = control->sinfo_tsn;
742f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
743f8829a4aSRandall Stewart 				    control,
744cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1,
745cfde3ff7SRandall Stewart 				    SCTP_READ_LOCK_NOT_HELD,
746cfde3ff7SRandall Stewart 				    SCTP_SO_NOT_LOCKED);
747830d754dSRandall Stewart 				/*
748830d754dSRandall Stewart 				 * EY this is the chunk that should be
749830d754dSRandall Stewart 				 * tagged nr gapped calculate the gap and
750830d754dSRandall Stewart 				 * such then tag this TSN nr
751830d754dSRandall Stewart 				 * chk->rec.data.TSN_seq
752830d754dSRandall Stewart 				 */
753830d754dSRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
754d50c1d79SRandall Stewart 					SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
755830d754dSRandall Stewart 					if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
756830d754dSRandall Stewart 					    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
757d50c1d79SRandall Stewart 						printf("Impossible nr TSN set 3?\n");
758830d754dSRandall Stewart 						/*
759830d754dSRandall Stewart 						 * EY The 1st should never
760830d754dSRandall Stewart 						 * happen, as in
761830d754dSRandall Stewart 						 * process_a_data_chunk
762830d754dSRandall Stewart 						 * method this check should
763830d754dSRandall Stewart 						 * be done
764830d754dSRandall Stewart 						 */
765830d754dSRandall Stewart 						/*
766830d754dSRandall Stewart 						 * EY The 2nd should never
767830d754dSRandall Stewart 						 * happen, because
768830d754dSRandall Stewart 						 * nr_mapping_array is
769830d754dSRandall Stewart 						 * always expanded when
770830d754dSRandall Stewart 						 * mapping_array is expanded
771830d754dSRandall Stewart 						 */
772830d754dSRandall Stewart 					} else {
773830d754dSRandall Stewart 						SCTP_TCB_LOCK_ASSERT(stcb);
774d50c1d79SRandall Stewart 						SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
775830d754dSRandall Stewart 						SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
7768933fa13SRandall Stewart 						if (compare_with_wrap(nr_tsn, asoc->highest_tsn_inside_nr_map,
7778933fa13SRandall Stewart 						    MAX_TSN))
778830d754dSRandall Stewart 							asoc->highest_tsn_inside_nr_map = nr_tsn;
779830d754dSRandall Stewart 					}
780830d754dSRandall Stewart 				}
781f8829a4aSRandall Stewart 				control = at;
782f8829a4aSRandall Stewart 				continue;
783f8829a4aSRandall Stewart 			}
784f8829a4aSRandall Stewart 			break;
785f8829a4aSRandall Stewart 		}
786f8829a4aSRandall Stewart 	}
787f8829a4aSRandall Stewart 	if (queue_needed) {
788f8829a4aSRandall Stewart 		/*
789f8829a4aSRandall Stewart 		 * Ok, we did not deliver this guy, find the correct place
790f8829a4aSRandall Stewart 		 * to put it on the queue.
791f8829a4aSRandall Stewart 		 */
792c40e9cf2SRandall Stewart 		if ((compare_with_wrap(asoc->cumulative_tsn,
793c40e9cf2SRandall Stewart 		    control->sinfo_tsn, MAX_TSN)) ||
794c40e9cf2SRandall Stewart 		    (control->sinfo_tsn == asoc->cumulative_tsn)) {
795c40e9cf2SRandall Stewart 			goto protocol_error;
796c40e9cf2SRandall Stewart 		}
797f8829a4aSRandall Stewart 		if (TAILQ_EMPTY(&strm->inqueue)) {
798f8829a4aSRandall Stewart 			/* Empty queue */
799b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
800f8829a4aSRandall Stewart 				sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INSERT_HD);
80180fefe0aSRandall Stewart 			}
802f8829a4aSRandall Stewart 			TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
803f8829a4aSRandall Stewart 		} else {
804f8829a4aSRandall Stewart 			TAILQ_FOREACH(at, &strm->inqueue, next) {
805f8829a4aSRandall Stewart 				if (compare_with_wrap(at->sinfo_ssn,
806f8829a4aSRandall Stewart 				    control->sinfo_ssn, MAX_SEQ)) {
807f8829a4aSRandall Stewart 					/*
808f8829a4aSRandall Stewart 					 * one in queue is bigger than the
809f8829a4aSRandall Stewart 					 * new one, insert before this one
810f8829a4aSRandall Stewart 					 */
811b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
812f8829a4aSRandall Stewart 						sctp_log_strm_del(control, at,
813f8829a4aSRandall Stewart 						    SCTP_STR_LOG_FROM_INSERT_MD);
81480fefe0aSRandall Stewart 					}
815f8829a4aSRandall Stewart 					TAILQ_INSERT_BEFORE(at, control, next);
816f8829a4aSRandall Stewart 					break;
817f8829a4aSRandall Stewart 				} else if (at->sinfo_ssn == control->sinfo_ssn) {
818f8829a4aSRandall Stewart 					/*
819f8829a4aSRandall Stewart 					 * Gak, He sent me a duplicate str
820f8829a4aSRandall Stewart 					 * seq number
821f8829a4aSRandall Stewart 					 */
822f8829a4aSRandall Stewart 					/*
823f8829a4aSRandall Stewart 					 * foo bar, I guess I will just free
824f8829a4aSRandall Stewart 					 * this new guy, should we abort
825f8829a4aSRandall Stewart 					 * too? FIX ME MAYBE? Or it COULD be
826f8829a4aSRandall Stewart 					 * that the SSN's have wrapped.
827f8829a4aSRandall Stewart 					 * Maybe I should compare to TSN
828f8829a4aSRandall Stewart 					 * somehow... sigh for now just blow
829f8829a4aSRandall Stewart 					 * away the chunk!
830f8829a4aSRandall Stewart 					 */
831f8829a4aSRandall Stewart 
832f8829a4aSRandall Stewart 					if (control->data)
833f8829a4aSRandall Stewart 						sctp_m_freem(control->data);
834f8829a4aSRandall Stewart 					control->data = NULL;
835f8829a4aSRandall Stewart 					asoc->size_on_all_streams -= control->length;
836f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->cnt_on_all_streams);
8375bead436SRandall Stewart 					if (control->whoFrom)
838f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
8395bead436SRandall Stewart 					control->whoFrom = NULL;
840f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
841f8829a4aSRandall Stewart 					return;
842f8829a4aSRandall Stewart 				} else {
843f8829a4aSRandall Stewart 					if (TAILQ_NEXT(at, next) == NULL) {
844f8829a4aSRandall Stewart 						/*
845f8829a4aSRandall Stewart 						 * We are at the end, insert
846f8829a4aSRandall Stewart 						 * it after this one
847f8829a4aSRandall Stewart 						 */
848b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
849f8829a4aSRandall Stewart 							sctp_log_strm_del(control, at,
850f8829a4aSRandall Stewart 							    SCTP_STR_LOG_FROM_INSERT_TL);
85180fefe0aSRandall Stewart 						}
852f8829a4aSRandall Stewart 						TAILQ_INSERT_AFTER(&strm->inqueue,
853f8829a4aSRandall Stewart 						    at, control, next);
854f8829a4aSRandall Stewart 						break;
855f8829a4aSRandall Stewart 					}
856f8829a4aSRandall Stewart 				}
857f8829a4aSRandall Stewart 			}
858f8829a4aSRandall Stewart 		}
859f8829a4aSRandall Stewart 	}
860f8829a4aSRandall Stewart }
861f8829a4aSRandall Stewart 
862f8829a4aSRandall Stewart /*
863f8829a4aSRandall Stewart  * Returns two things: You get the total size of the deliverable parts of the
864f8829a4aSRandall Stewart  * first fragmented message on the reassembly queue. And you get a 1 back if
865f8829a4aSRandall Stewart  * all of the message is ready or a 0 back if the message is still incomplete
866f8829a4aSRandall Stewart  */
867f8829a4aSRandall Stewart static int
868f8829a4aSRandall Stewart sctp_is_all_msg_on_reasm(struct sctp_association *asoc, uint32_t * t_size)
869f8829a4aSRandall Stewart {
870f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
871f8829a4aSRandall Stewart 	uint32_t tsn;
872f8829a4aSRandall Stewart 
873f8829a4aSRandall Stewart 	*t_size = 0;
874f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
875f8829a4aSRandall Stewart 	if (chk == NULL) {
876f8829a4aSRandall Stewart 		/* nothing on the queue */
877f8829a4aSRandall Stewart 		return (0);
878f8829a4aSRandall Stewart 	}
879f8829a4aSRandall Stewart 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
880f8829a4aSRandall Stewart 		/* Not a first on the queue */
881f8829a4aSRandall Stewart 		return (0);
882f8829a4aSRandall Stewart 	}
883f8829a4aSRandall Stewart 	tsn = chk->rec.data.TSN_seq;
884f8829a4aSRandall Stewart 	while (chk) {
885f8829a4aSRandall Stewart 		if (tsn != chk->rec.data.TSN_seq) {
886f8829a4aSRandall Stewart 			return (0);
887f8829a4aSRandall Stewart 		}
888f8829a4aSRandall Stewart 		*t_size += chk->send_size;
889f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
890f8829a4aSRandall Stewart 			return (1);
891f8829a4aSRandall Stewart 		}
892f8829a4aSRandall Stewart 		tsn++;
893f8829a4aSRandall Stewart 		chk = TAILQ_NEXT(chk, sctp_next);
894f8829a4aSRandall Stewart 	}
895f8829a4aSRandall Stewart 	return (0);
896f8829a4aSRandall Stewart }
897f8829a4aSRandall Stewart 
898f8829a4aSRandall Stewart static void
899f8829a4aSRandall Stewart sctp_deliver_reasm_check(struct sctp_tcb *stcb, struct sctp_association *asoc)
900f8829a4aSRandall Stewart {
901f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
902f8829a4aSRandall Stewart 	uint16_t nxt_todel;
903f8829a4aSRandall Stewart 	uint32_t tsize;
904f8829a4aSRandall Stewart 
905139bc87fSRandall Stewart doit_again:
906f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
907f8829a4aSRandall Stewart 	if (chk == NULL) {
908f8829a4aSRandall Stewart 		/* Huh? */
909f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = 0;
910f8829a4aSRandall Stewart 		asoc->cnt_on_reasm_queue = 0;
911f8829a4aSRandall Stewart 		return;
912f8829a4aSRandall Stewart 	}
913f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress == 0) {
914f8829a4aSRandall Stewart 		nxt_todel =
915f8829a4aSRandall Stewart 		    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
916f8829a4aSRandall Stewart 		if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
917f8829a4aSRandall Stewart 		    (nxt_todel == chk->rec.data.stream_seq ||
918f8829a4aSRandall Stewart 		    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
919f8829a4aSRandall Stewart 			/*
920f8829a4aSRandall Stewart 			 * Yep the first one is here and its ok to deliver
921f8829a4aSRandall Stewart 			 * but should we?
922f8829a4aSRandall Stewart 			 */
923f8829a4aSRandall Stewart 			if ((sctp_is_all_msg_on_reasm(asoc, &tsize) ||
924c4739e2fSRandall Stewart 			    (tsize >= stcb->sctp_ep->partial_delivery_point))) {
925f8829a4aSRandall Stewart 
926f8829a4aSRandall Stewart 				/*
927f8829a4aSRandall Stewart 				 * Yes, we setup to start reception, by
928f8829a4aSRandall Stewart 				 * backing down the TSN just in case we
929f8829a4aSRandall Stewart 				 * can't deliver. If we
930f8829a4aSRandall Stewart 				 */
931f8829a4aSRandall Stewart 				asoc->fragmented_delivery_inprogress = 1;
932f8829a4aSRandall Stewart 				asoc->tsn_last_delivered =
933f8829a4aSRandall Stewart 				    chk->rec.data.TSN_seq - 1;
934f8829a4aSRandall Stewart 				asoc->str_of_pdapi =
935f8829a4aSRandall Stewart 				    chk->rec.data.stream_number;
936f8829a4aSRandall Stewart 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
937f8829a4aSRandall Stewart 				asoc->pdapi_ppid = chk->rec.data.payloadtype;
938f8829a4aSRandall Stewart 				asoc->fragment_flags = chk->rec.data.rcv_flags;
939f8829a4aSRandall Stewart 				sctp_service_reassembly(stcb, asoc);
940f8829a4aSRandall Stewart 			}
941f8829a4aSRandall Stewart 		}
942f8829a4aSRandall Stewart 	} else {
943139bc87fSRandall Stewart 		/*
944139bc87fSRandall Stewart 		 * Service re-assembly will deliver stream data queued at
945139bc87fSRandall Stewart 		 * the end of fragmented delivery.. but it wont know to go
946139bc87fSRandall Stewart 		 * back and call itself again... we do that here with the
947139bc87fSRandall Stewart 		 * got doit_again
948139bc87fSRandall Stewart 		 */
949f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
950139bc87fSRandall Stewart 		if (asoc->fragmented_delivery_inprogress == 0) {
951139bc87fSRandall Stewart 			/*
952139bc87fSRandall Stewart 			 * finished our Fragmented delivery, could be more
953139bc87fSRandall Stewart 			 * waiting?
954139bc87fSRandall Stewart 			 */
955139bc87fSRandall Stewart 			goto doit_again;
956139bc87fSRandall Stewart 		}
957f8829a4aSRandall Stewart 	}
958f8829a4aSRandall Stewart }
959f8829a4aSRandall Stewart 
960f8829a4aSRandall Stewart /*
961f8829a4aSRandall Stewart  * Dump onto the re-assembly queue, in its proper place. After dumping on the
962f8829a4aSRandall Stewart  * queue, see if anthing can be delivered. If so pull it off (or as much as
963f8829a4aSRandall Stewart  * we can. If we run out of space then we must dump what we can and set the
964f8829a4aSRandall Stewart  * appropriate flag to say we queued what we could.
965f8829a4aSRandall Stewart  */
966f8829a4aSRandall Stewart static void
967f8829a4aSRandall Stewart sctp_queue_data_for_reasm(struct sctp_tcb *stcb, struct sctp_association *asoc,
968f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk, int *abort_flag)
969f8829a4aSRandall Stewart {
970f8829a4aSRandall Stewart 	struct mbuf *oper;
971f8829a4aSRandall Stewart 	uint32_t cum_ackp1, last_tsn, prev_tsn, post_tsn;
972f8829a4aSRandall Stewart 	u_char last_flags;
973f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *at, *prev, *next;
974f8829a4aSRandall Stewart 
975f8829a4aSRandall Stewart 	prev = next = NULL;
976f8829a4aSRandall Stewart 	cum_ackp1 = asoc->tsn_last_delivered + 1;
977f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->reasmqueue)) {
978f8829a4aSRandall Stewart 		/* This is the first one on the queue */
979f8829a4aSRandall Stewart 		TAILQ_INSERT_HEAD(&asoc->reasmqueue, chk, sctp_next);
980f8829a4aSRandall Stewart 		/*
981f8829a4aSRandall Stewart 		 * we do not check for delivery of anything when only one
982f8829a4aSRandall Stewart 		 * fragment is here
983f8829a4aSRandall Stewart 		 */
984f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = chk->send_size;
985f8829a4aSRandall Stewart 		sctp_ucount_incr(asoc->cnt_on_reasm_queue);
986f8829a4aSRandall Stewart 		if (chk->rec.data.TSN_seq == cum_ackp1) {
987f8829a4aSRandall Stewart 			if (asoc->fragmented_delivery_inprogress == 0 &&
988f8829a4aSRandall Stewart 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) !=
989f8829a4aSRandall Stewart 			    SCTP_DATA_FIRST_FRAG) {
990f8829a4aSRandall Stewart 				/*
991f8829a4aSRandall Stewart 				 * An empty queue, no delivery inprogress,
992f8829a4aSRandall Stewart 				 * we hit the next one and it does NOT have
993f8829a4aSRandall Stewart 				 * a FIRST fragment mark.
994f8829a4aSRandall Stewart 				 */
995ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not first, no fragmented delivery in progress\n");
996139bc87fSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
997f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
998f8829a4aSRandall Stewart 
999f8829a4aSRandall Stewart 				if (oper) {
1000f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
1001f8829a4aSRandall Stewart 					uint32_t *ippp;
1002f8829a4aSRandall Stewart 
1003139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) =
1004f8829a4aSRandall Stewart 					    sizeof(struct sctp_paramhdr) +
1005f8829a4aSRandall Stewart 					    (sizeof(uint32_t) * 3);
1006f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
1007f8829a4aSRandall Stewart 					ph->param_type =
1008f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1009139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
1010f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
1011a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_2);
1012f8829a4aSRandall Stewart 					ippp++;
1013f8829a4aSRandall Stewart 					*ippp = chk->rec.data.TSN_seq;
1014f8829a4aSRandall Stewart 					ippp++;
1015f8829a4aSRandall Stewart 					*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1016f8829a4aSRandall Stewart 
1017f8829a4aSRandall Stewart 				}
1018a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_2;
1019f8829a4aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb,
1020ceaad40aSRandall Stewart 				    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1021f8829a4aSRandall Stewart 				*abort_flag = 1;
1022f8829a4aSRandall Stewart 			} else if (asoc->fragmented_delivery_inprogress &&
1023f8829a4aSRandall Stewart 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
1024f8829a4aSRandall Stewart 				/*
1025f8829a4aSRandall Stewart 				 * We are doing a partial delivery and the
1026f8829a4aSRandall Stewart 				 * NEXT chunk MUST be either the LAST or
1027f8829a4aSRandall Stewart 				 * MIDDLE fragment NOT a FIRST
1028f8829a4aSRandall Stewart 				 */
1029ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS a first and fragmented delivery in progress\n");
1030139bc87fSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1031f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
1032f8829a4aSRandall Stewart 				if (oper) {
1033f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
1034f8829a4aSRandall Stewart 					uint32_t *ippp;
1035f8829a4aSRandall Stewart 
1036139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) =
1037f8829a4aSRandall Stewart 					    sizeof(struct sctp_paramhdr) +
1038f8829a4aSRandall Stewart 					    (3 * sizeof(uint32_t));
1039f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
1040f8829a4aSRandall Stewart 					ph->param_type =
1041f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1042139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
1043f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
1044a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_3);
1045f8829a4aSRandall Stewart 					ippp++;
1046f8829a4aSRandall Stewart 					*ippp = chk->rec.data.TSN_seq;
1047f8829a4aSRandall Stewart 					ippp++;
1048f8829a4aSRandall Stewart 					*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1049f8829a4aSRandall Stewart 				}
1050a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_3;
1051f8829a4aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb,
1052ceaad40aSRandall Stewart 				    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1053f8829a4aSRandall Stewart 				*abort_flag = 1;
1054f8829a4aSRandall Stewart 			} else if (asoc->fragmented_delivery_inprogress) {
1055f8829a4aSRandall Stewart 				/*
1056f8829a4aSRandall Stewart 				 * Here we are ok with a MIDDLE or LAST
1057f8829a4aSRandall Stewart 				 * piece
1058f8829a4aSRandall Stewart 				 */
1059f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
1060f8829a4aSRandall Stewart 				    asoc->str_of_pdapi) {
1061f8829a4aSRandall Stewart 					/* Got to be the right STR No */
1062ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream number %d vs %d\n",
1063f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
1064f8829a4aSRandall Stewart 					    asoc->str_of_pdapi);
1065139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1066f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1067f8829a4aSRandall Stewart 					if (oper) {
1068f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1069f8829a4aSRandall Stewart 						uint32_t *ippp;
1070f8829a4aSRandall Stewart 
1071139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1072f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1073f8829a4aSRandall Stewart 						    (sizeof(uint32_t) * 3);
1074f8829a4aSRandall Stewart 						ph = mtod(oper,
1075f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1076f8829a4aSRandall Stewart 						ph->param_type =
1077f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1078f8829a4aSRandall Stewart 						ph->param_length =
1079139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1080f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1081a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_4);
1082f8829a4aSRandall Stewart 						ippp++;
1083f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1084f8829a4aSRandall Stewart 						ippp++;
1085f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1086f8829a4aSRandall Stewart 					}
1087a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_4;
1088f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1089ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1090f8829a4aSRandall Stewart 					*abort_flag = 1;
1091f8829a4aSRandall Stewart 				} else if ((asoc->fragment_flags & SCTP_DATA_UNORDERED) !=
1092f8829a4aSRandall Stewart 					    SCTP_DATA_UNORDERED &&
1093f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq !=
1094f8829a4aSRandall Stewart 				    asoc->ssn_of_pdapi) {
1095f8829a4aSRandall Stewart 					/* Got to be the right STR Seq */
1096ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream seq %d vs %d\n",
1097f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
1098f8829a4aSRandall Stewart 					    asoc->ssn_of_pdapi);
1099139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1100f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1101f8829a4aSRandall Stewart 					if (oper) {
1102f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1103f8829a4aSRandall Stewart 						uint32_t *ippp;
1104f8829a4aSRandall Stewart 
1105139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1106f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1107f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1108f8829a4aSRandall Stewart 						ph = mtod(oper,
1109f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1110f8829a4aSRandall Stewart 						ph->param_type =
1111f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1112f8829a4aSRandall Stewart 						ph->param_length =
1113139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1114f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1115a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_5);
1116f8829a4aSRandall Stewart 						ippp++;
1117f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1118f8829a4aSRandall Stewart 						ippp++;
1119f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1120f8829a4aSRandall Stewart 
1121f8829a4aSRandall Stewart 					}
1122a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_5;
1123f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1124ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1125f8829a4aSRandall Stewart 					*abort_flag = 1;
1126f8829a4aSRandall Stewart 				}
1127f8829a4aSRandall Stewart 			}
1128f8829a4aSRandall Stewart 		}
1129f8829a4aSRandall Stewart 		return;
1130f8829a4aSRandall Stewart 	}
1131f8829a4aSRandall Stewart 	/* Find its place */
1132f8829a4aSRandall Stewart 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
1133f8829a4aSRandall Stewart 		if (compare_with_wrap(at->rec.data.TSN_seq,
1134f8829a4aSRandall Stewart 		    chk->rec.data.TSN_seq, MAX_TSN)) {
1135f8829a4aSRandall Stewart 			/*
1136f8829a4aSRandall Stewart 			 * one in queue is bigger than the new one, insert
1137f8829a4aSRandall Stewart 			 * before this one
1138f8829a4aSRandall Stewart 			 */
1139f8829a4aSRandall Stewart 			/* A check */
1140f8829a4aSRandall Stewart 			asoc->size_on_reasm_queue += chk->send_size;
1141f8829a4aSRandall Stewart 			sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1142f8829a4aSRandall Stewart 			next = at;
1143f8829a4aSRandall Stewart 			TAILQ_INSERT_BEFORE(at, chk, sctp_next);
1144f8829a4aSRandall Stewart 			break;
1145f8829a4aSRandall Stewart 		} else if (at->rec.data.TSN_seq == chk->rec.data.TSN_seq) {
1146f8829a4aSRandall Stewart 			/* Gak, He sent me a duplicate str seq number */
1147f8829a4aSRandall Stewart 			/*
1148f8829a4aSRandall Stewart 			 * foo bar, I guess I will just free this new guy,
1149f8829a4aSRandall Stewart 			 * should we abort too? FIX ME MAYBE? Or it COULD be
1150f8829a4aSRandall Stewart 			 * that the SSN's have wrapped. Maybe I should
1151f8829a4aSRandall Stewart 			 * compare to TSN somehow... sigh for now just blow
1152f8829a4aSRandall Stewart 			 * away the chunk!
1153f8829a4aSRandall Stewart 			 */
1154f8829a4aSRandall Stewart 			if (chk->data) {
1155f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
1156f8829a4aSRandall Stewart 				chk->data = NULL;
1157f8829a4aSRandall Stewart 			}
1158f8829a4aSRandall Stewart 			sctp_free_a_chunk(stcb, chk);
1159f8829a4aSRandall Stewart 			return;
1160f8829a4aSRandall Stewart 		} else {
1161f8829a4aSRandall Stewart 			last_flags = at->rec.data.rcv_flags;
1162f8829a4aSRandall Stewart 			last_tsn = at->rec.data.TSN_seq;
1163f8829a4aSRandall Stewart 			prev = at;
1164f8829a4aSRandall Stewart 			if (TAILQ_NEXT(at, sctp_next) == NULL) {
1165f8829a4aSRandall Stewart 				/*
1166f8829a4aSRandall Stewart 				 * We are at the end, insert it after this
1167f8829a4aSRandall Stewart 				 * one
1168f8829a4aSRandall Stewart 				 */
1169f8829a4aSRandall Stewart 				/* check it first */
1170f8829a4aSRandall Stewart 				asoc->size_on_reasm_queue += chk->send_size;
1171f8829a4aSRandall Stewart 				sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1172f8829a4aSRandall Stewart 				TAILQ_INSERT_AFTER(&asoc->reasmqueue, at, chk, sctp_next);
1173f8829a4aSRandall Stewart 				break;
1174f8829a4aSRandall Stewart 			}
1175f8829a4aSRandall Stewart 		}
1176f8829a4aSRandall Stewart 	}
1177f8829a4aSRandall Stewart 	/* Now the audits */
1178f8829a4aSRandall Stewart 	if (prev) {
1179f8829a4aSRandall Stewart 		prev_tsn = chk->rec.data.TSN_seq - 1;
1180f8829a4aSRandall Stewart 		if (prev_tsn == prev->rec.data.TSN_seq) {
1181f8829a4aSRandall Stewart 			/*
1182f8829a4aSRandall Stewart 			 * Ok the one I am dropping onto the end is the
1183f8829a4aSRandall Stewart 			 * NEXT. A bit of valdiation here.
1184f8829a4aSRandall Stewart 			 */
1185f8829a4aSRandall Stewart 			if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1186f8829a4aSRandall Stewart 			    SCTP_DATA_FIRST_FRAG ||
1187f8829a4aSRandall Stewart 			    (prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1188f8829a4aSRandall Stewart 			    SCTP_DATA_MIDDLE_FRAG) {
1189f8829a4aSRandall Stewart 				/*
1190f8829a4aSRandall Stewart 				 * Insert chk MUST be a MIDDLE or LAST
1191f8829a4aSRandall Stewart 				 * fragment
1192f8829a4aSRandall Stewart 				 */
1193f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1194f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1195ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - It can be a midlle or last but not a first\n");
1196ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it's a FIRST!\n");
1197139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1198f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1199f8829a4aSRandall Stewart 					if (oper) {
1200f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1201f8829a4aSRandall Stewart 						uint32_t *ippp;
1202f8829a4aSRandall Stewart 
1203139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1204f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1205f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1206f8829a4aSRandall Stewart 						ph = mtod(oper,
1207f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1208f8829a4aSRandall Stewart 						ph->param_type =
1209f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1210f8829a4aSRandall Stewart 						ph->param_length =
1211139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1212f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1213a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_6);
1214f8829a4aSRandall Stewart 						ippp++;
1215f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1216f8829a4aSRandall Stewart 						ippp++;
1217f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1218f8829a4aSRandall Stewart 
1219f8829a4aSRandall Stewart 					}
1220a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_6;
1221f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1222ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1223f8829a4aSRandall Stewart 					*abort_flag = 1;
1224f8829a4aSRandall Stewart 					return;
1225f8829a4aSRandall Stewart 				}
1226f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
1227f8829a4aSRandall Stewart 				    prev->rec.data.stream_number) {
1228f8829a4aSRandall Stewart 					/*
1229f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1230f8829a4aSRandall Stewart 					 * they must be the same.
1231f8829a4aSRandall Stewart 					 */
1232ad81507eSRandall Stewart 					SCTP_PRINTF("Prev check - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1233f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
1234f8829a4aSRandall Stewart 					    prev->rec.data.stream_number);
1235139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1236f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1237f8829a4aSRandall Stewart 					if (oper) {
1238f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1239f8829a4aSRandall Stewart 						uint32_t *ippp;
1240f8829a4aSRandall Stewart 
1241139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1242f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1243f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1244f8829a4aSRandall Stewart 						ph = mtod(oper,
1245f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1246f8829a4aSRandall Stewart 						ph->param_type =
1247f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1248f8829a4aSRandall Stewart 						ph->param_length =
1249139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1250f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1251a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_7);
1252f8829a4aSRandall Stewart 						ippp++;
1253f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1254f8829a4aSRandall Stewart 						ippp++;
1255f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1256f8829a4aSRandall Stewart 					}
1257a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_7;
1258f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1259ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1260f8829a4aSRandall Stewart 
1261f8829a4aSRandall Stewart 					*abort_flag = 1;
1262f8829a4aSRandall Stewart 					return;
1263f8829a4aSRandall Stewart 				}
1264f8829a4aSRandall Stewart 				if ((prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1265f8829a4aSRandall Stewart 				    chk->rec.data.stream_seq !=
1266f8829a4aSRandall Stewart 				    prev->rec.data.stream_seq) {
1267f8829a4aSRandall Stewart 					/*
1268f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1269f8829a4aSRandall Stewart 					 * they must be the same.
1270f8829a4aSRandall Stewart 					 */
1271ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1272f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
1273f8829a4aSRandall Stewart 					    prev->rec.data.stream_seq);
1274139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1275f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1276f8829a4aSRandall Stewart 					if (oper) {
1277f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1278f8829a4aSRandall Stewart 						uint32_t *ippp;
1279f8829a4aSRandall Stewart 
1280139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1281f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1282f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1283f8829a4aSRandall Stewart 						ph = mtod(oper,
1284f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1285f8829a4aSRandall Stewart 						ph->param_type =
1286f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1287f8829a4aSRandall Stewart 						ph->param_length =
1288139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1289f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1290a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_8);
1291f8829a4aSRandall Stewart 						ippp++;
1292f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1293f8829a4aSRandall Stewart 						ippp++;
1294f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1295f8829a4aSRandall Stewart 					}
1296a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_8;
1297f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1298ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1299f8829a4aSRandall Stewart 
1300f8829a4aSRandall Stewart 					*abort_flag = 1;
1301f8829a4aSRandall Stewart 					return;
1302f8829a4aSRandall Stewart 				}
1303f8829a4aSRandall Stewart 			} else if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1304f8829a4aSRandall Stewart 			    SCTP_DATA_LAST_FRAG) {
1305f8829a4aSRandall Stewart 				/* Insert chk MUST be a FIRST */
1306f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1307f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1308ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, evil plot, its not FIRST and it must be!\n");
1309139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1310f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1311f8829a4aSRandall Stewart 					if (oper) {
1312f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1313f8829a4aSRandall Stewart 						uint32_t *ippp;
1314f8829a4aSRandall Stewart 
1315139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1316f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1317f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1318f8829a4aSRandall Stewart 						ph = mtod(oper,
1319f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1320f8829a4aSRandall Stewart 						ph->param_type =
1321f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1322f8829a4aSRandall Stewart 						ph->param_length =
1323139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1324f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1325a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_9);
1326f8829a4aSRandall Stewart 						ippp++;
1327f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1328f8829a4aSRandall Stewart 						ippp++;
1329f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1330f8829a4aSRandall Stewart 
1331f8829a4aSRandall Stewart 					}
1332a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_9;
1333f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1334ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1335f8829a4aSRandall Stewart 
1336f8829a4aSRandall Stewart 					*abort_flag = 1;
1337f8829a4aSRandall Stewart 					return;
1338f8829a4aSRandall Stewart 				}
1339f8829a4aSRandall Stewart 			}
1340f8829a4aSRandall Stewart 		}
1341f8829a4aSRandall Stewart 	}
1342f8829a4aSRandall Stewart 	if (next) {
1343f8829a4aSRandall Stewart 		post_tsn = chk->rec.data.TSN_seq + 1;
1344f8829a4aSRandall Stewart 		if (post_tsn == next->rec.data.TSN_seq) {
1345f8829a4aSRandall Stewart 			/*
1346f8829a4aSRandall Stewart 			 * Ok the one I am inserting ahead of is my NEXT
1347f8829a4aSRandall Stewart 			 * one. A bit of valdiation here.
1348f8829a4aSRandall Stewart 			 */
1349f8829a4aSRandall Stewart 			if (next->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
1350f8829a4aSRandall Stewart 				/* Insert chk MUST be a last fragment */
1351f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK)
1352f8829a4aSRandall Stewart 				    != SCTP_DATA_LAST_FRAG) {
1353ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is FIRST, we must be LAST\n");
1354ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not a last!\n");
1355139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1356f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1357f8829a4aSRandall Stewart 					if (oper) {
1358f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1359f8829a4aSRandall Stewart 						uint32_t *ippp;
1360f8829a4aSRandall Stewart 
1361139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1362f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1363f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1364f8829a4aSRandall Stewart 						ph = mtod(oper,
1365f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1366f8829a4aSRandall Stewart 						ph->param_type =
1367f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1368f8829a4aSRandall Stewart 						ph->param_length =
1369139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1370f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1371a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_10);
1372f8829a4aSRandall Stewart 						ippp++;
1373f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1374f8829a4aSRandall Stewart 						ippp++;
1375f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1376f8829a4aSRandall Stewart 					}
1377a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_10;
1378f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1379ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1380f8829a4aSRandall Stewart 
1381f8829a4aSRandall Stewart 					*abort_flag = 1;
1382f8829a4aSRandall Stewart 					return;
1383f8829a4aSRandall Stewart 				}
1384f8829a4aSRandall Stewart 			} else if ((next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1385f8829a4aSRandall Stewart 				    SCTP_DATA_MIDDLE_FRAG ||
1386f8829a4aSRandall Stewart 				    (next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1387f8829a4aSRandall Stewart 			    SCTP_DATA_LAST_FRAG) {
1388f8829a4aSRandall Stewart 				/*
1389f8829a4aSRandall Stewart 				 * Insert chk CAN be MIDDLE or FIRST NOT
1390f8829a4aSRandall Stewart 				 * LAST
1391f8829a4aSRandall Stewart 				 */
1392f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1393f8829a4aSRandall Stewart 				    SCTP_DATA_LAST_FRAG) {
1394ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is a MIDDLE/LAST\n");
1395ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, new prev chunk is a LAST\n");
1396139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1397f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1398f8829a4aSRandall Stewart 					if (oper) {
1399f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1400f8829a4aSRandall Stewart 						uint32_t *ippp;
1401f8829a4aSRandall Stewart 
1402139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1403f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1404f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1405f8829a4aSRandall Stewart 						ph = mtod(oper,
1406f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1407f8829a4aSRandall Stewart 						ph->param_type =
1408f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1409f8829a4aSRandall Stewart 						ph->param_length =
1410139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1411f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1412a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_11);
1413f8829a4aSRandall Stewart 						ippp++;
1414f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1415f8829a4aSRandall Stewart 						ippp++;
1416f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1417f8829a4aSRandall Stewart 
1418f8829a4aSRandall Stewart 					}
1419a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_11;
1420f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1421ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1422f8829a4aSRandall Stewart 
1423f8829a4aSRandall Stewart 					*abort_flag = 1;
1424f8829a4aSRandall Stewart 					return;
1425f8829a4aSRandall Stewart 				}
1426f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
1427f8829a4aSRandall Stewart 				    next->rec.data.stream_number) {
1428f8829a4aSRandall Stewart 					/*
1429f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1430f8829a4aSRandall Stewart 					 * they must be the same.
1431f8829a4aSRandall Stewart 					 */
1432ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1433f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
1434f8829a4aSRandall Stewart 					    next->rec.data.stream_number);
1435139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1436f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1437f8829a4aSRandall Stewart 					if (oper) {
1438f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1439f8829a4aSRandall Stewart 						uint32_t *ippp;
1440f8829a4aSRandall Stewart 
1441139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1442f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1443f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1444f8829a4aSRandall Stewart 						ph = mtod(oper,
1445f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1446f8829a4aSRandall Stewart 						ph->param_type =
1447f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1448f8829a4aSRandall Stewart 						ph->param_length =
1449139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1450f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1451a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_12);
1452f8829a4aSRandall Stewart 						ippp++;
1453f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1454f8829a4aSRandall Stewart 						ippp++;
1455f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1456f8829a4aSRandall Stewart 
1457f8829a4aSRandall Stewart 					}
1458a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_12;
1459f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1460ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1461f8829a4aSRandall Stewart 
1462f8829a4aSRandall Stewart 					*abort_flag = 1;
1463f8829a4aSRandall Stewart 					return;
1464f8829a4aSRandall Stewart 				}
1465f8829a4aSRandall Stewart 				if ((next->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1466f8829a4aSRandall Stewart 				    chk->rec.data.stream_seq !=
1467f8829a4aSRandall Stewart 				    next->rec.data.stream_seq) {
1468f8829a4aSRandall Stewart 					/*
1469f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1470f8829a4aSRandall Stewart 					 * they must be the same.
1471f8829a4aSRandall Stewart 					 */
1472ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1473f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
1474f8829a4aSRandall Stewart 					    next->rec.data.stream_seq);
1475139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1476f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1477f8829a4aSRandall Stewart 					if (oper) {
1478f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1479f8829a4aSRandall Stewart 						uint32_t *ippp;
1480f8829a4aSRandall Stewart 
1481139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1482f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1483f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1484f8829a4aSRandall Stewart 						ph = mtod(oper,
1485f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1486f8829a4aSRandall Stewart 						ph->param_type =
1487f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1488f8829a4aSRandall Stewart 						ph->param_length =
1489139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1490f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1491a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_13);
1492f8829a4aSRandall Stewart 						ippp++;
1493f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1494f8829a4aSRandall Stewart 						ippp++;
1495f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1496f8829a4aSRandall Stewart 					}
1497a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_13;
1498f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1499ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1500f8829a4aSRandall Stewart 
1501f8829a4aSRandall Stewart 					*abort_flag = 1;
1502f8829a4aSRandall Stewart 					return;
1503f8829a4aSRandall Stewart 				}
1504f8829a4aSRandall Stewart 			}
1505f8829a4aSRandall Stewart 		}
1506f8829a4aSRandall Stewart 	}
1507f8829a4aSRandall Stewart 	/* Do we need to do some delivery? check */
1508f8829a4aSRandall Stewart 	sctp_deliver_reasm_check(stcb, asoc);
1509f8829a4aSRandall Stewart }
1510f8829a4aSRandall Stewart 
1511f8829a4aSRandall Stewart /*
1512f8829a4aSRandall Stewart  * This is an unfortunate routine. It checks to make sure a evil guy is not
1513f8829a4aSRandall Stewart  * stuffing us full of bad packet fragments. A broken peer could also do this
1514f8829a4aSRandall Stewart  * but this is doubtful. It is to bad I must worry about evil crackers sigh
1515f8829a4aSRandall Stewart  * :< more cycles.
1516f8829a4aSRandall Stewart  */
1517f8829a4aSRandall Stewart static int
1518f8829a4aSRandall Stewart sctp_does_tsn_belong_to_reasm(struct sctp_association *asoc,
1519f8829a4aSRandall Stewart     uint32_t TSN_seq)
1520f8829a4aSRandall Stewart {
1521f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *at;
1522f8829a4aSRandall Stewart 	uint32_t tsn_est;
1523f8829a4aSRandall Stewart 
1524f8829a4aSRandall Stewart 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
1525f8829a4aSRandall Stewart 		if (compare_with_wrap(TSN_seq,
1526f8829a4aSRandall Stewart 		    at->rec.data.TSN_seq, MAX_TSN)) {
1527f8829a4aSRandall Stewart 			/* is it one bigger? */
1528f8829a4aSRandall Stewart 			tsn_est = at->rec.data.TSN_seq + 1;
1529f8829a4aSRandall Stewart 			if (tsn_est == TSN_seq) {
1530f8829a4aSRandall Stewart 				/* yep. It better be a last then */
1531f8829a4aSRandall Stewart 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1532f8829a4aSRandall Stewart 				    SCTP_DATA_LAST_FRAG) {
1533f8829a4aSRandall Stewart 					/*
1534f8829a4aSRandall Stewart 					 * Ok this guy belongs next to a guy
1535f8829a4aSRandall Stewart 					 * that is NOT last, it should be a
1536f8829a4aSRandall Stewart 					 * middle/last, not a complete
1537f8829a4aSRandall Stewart 					 * chunk.
1538f8829a4aSRandall Stewart 					 */
1539f8829a4aSRandall Stewart 					return (1);
1540f8829a4aSRandall Stewart 				} else {
1541f8829a4aSRandall Stewart 					/*
1542f8829a4aSRandall Stewart 					 * This guy is ok since its a LAST
1543f8829a4aSRandall Stewart 					 * and the new chunk is a fully
1544f8829a4aSRandall Stewart 					 * self- contained one.
1545f8829a4aSRandall Stewart 					 */
1546f8829a4aSRandall Stewart 					return (0);
1547f8829a4aSRandall Stewart 				}
1548f8829a4aSRandall Stewart 			}
1549f8829a4aSRandall Stewart 		} else if (TSN_seq == at->rec.data.TSN_seq) {
1550f8829a4aSRandall Stewart 			/* Software error since I have a dup? */
1551f8829a4aSRandall Stewart 			return (1);
1552f8829a4aSRandall Stewart 		} else {
1553f8829a4aSRandall Stewart 			/*
1554f8829a4aSRandall Stewart 			 * Ok, 'at' is larger than new chunk but does it
1555f8829a4aSRandall Stewart 			 * need to be right before it.
1556f8829a4aSRandall Stewart 			 */
1557f8829a4aSRandall Stewart 			tsn_est = TSN_seq + 1;
1558f8829a4aSRandall Stewart 			if (tsn_est == at->rec.data.TSN_seq) {
1559f8829a4aSRandall Stewart 				/* Yep, It better be a first */
1560f8829a4aSRandall Stewart 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1561f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1562f8829a4aSRandall Stewart 					return (1);
1563f8829a4aSRandall Stewart 				} else {
1564f8829a4aSRandall Stewart 					return (0);
1565f8829a4aSRandall Stewart 				}
1566f8829a4aSRandall Stewart 			}
1567f8829a4aSRandall Stewart 		}
1568f8829a4aSRandall Stewart 	}
1569f8829a4aSRandall Stewart 	return (0);
1570f8829a4aSRandall Stewart }
1571f8829a4aSRandall Stewart 
1572f8829a4aSRandall Stewart 
1573f8829a4aSRandall Stewart static int
1574f8829a4aSRandall Stewart sctp_process_a_data_chunk(struct sctp_tcb *stcb, struct sctp_association *asoc,
1575f8829a4aSRandall Stewart     struct mbuf **m, int offset, struct sctp_data_chunk *ch, int chk_length,
1576f8829a4aSRandall Stewart     struct sctp_nets *net, uint32_t * high_tsn, int *abort_flag,
1577f8829a4aSRandall Stewart     int *break_flag, int last_chunk)
1578f8829a4aSRandall Stewart {
1579f8829a4aSRandall Stewart 	/* Process a data chunk */
1580f8829a4aSRandall Stewart 	/* struct sctp_tmit_chunk *chk; */
1581f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
1582f8829a4aSRandall Stewart 	uint32_t tsn, gap;
1583830d754dSRandall Stewart 
1584830d754dSRandall Stewart 	/* EY - for nr_sack */
1585830d754dSRandall Stewart 	uint32_t nr_gap;
1586f8829a4aSRandall Stewart 	struct mbuf *dmbuf;
1587f8829a4aSRandall Stewart 	int indx, the_len;
1588139bc87fSRandall Stewart 	int need_reasm_check = 0;
1589f8829a4aSRandall Stewart 	uint16_t strmno, strmseq;
1590f8829a4aSRandall Stewart 	struct mbuf *oper;
1591f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
1592f42a358aSRandall Stewart 	int ordered;
1593f42a358aSRandall Stewart 	uint32_t protocol_id;
1594f42a358aSRandall Stewart 	uint8_t chunk_flags;
159517205eccSRandall Stewart 	struct sctp_stream_reset_list *liste;
1596f8829a4aSRandall Stewart 
1597f8829a4aSRandall Stewart 	chk = NULL;
1598f8829a4aSRandall Stewart 	tsn = ntohl(ch->dp.tsn);
1599f42a358aSRandall Stewart 	chunk_flags = ch->ch.chunk_flags;
1600b3f1ea41SRandall Stewart 	if ((chunk_flags & SCTP_DATA_SACK_IMMEDIATELY) == SCTP_DATA_SACK_IMMEDIATELY) {
1601b3f1ea41SRandall Stewart 		asoc->send_sack = 1;
1602b3f1ea41SRandall Stewart 	}
1603f42a358aSRandall Stewart 	protocol_id = ch->dp.protocol_id;
1604f42a358aSRandall Stewart 	ordered = ((ch->ch.chunk_flags & SCTP_DATA_UNORDERED) == 0);
1605b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1606c4739e2fSRandall Stewart 		sctp_log_map(tsn, asoc->cumulative_tsn, asoc->highest_tsn_inside_map, SCTP_MAP_TSN_ENTERS);
160780fefe0aSRandall Stewart 	}
1608ad81507eSRandall Stewart 	if (stcb == NULL) {
1609ad81507eSRandall Stewart 		return (0);
1610ad81507eSRandall Stewart 	}
161180fefe0aSRandall Stewart 	SCTP_LTRACE_CHK(stcb->sctp_ep, stcb, ch->ch.chunk_type, tsn);
1612f8829a4aSRandall Stewart 	if (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN) ||
1613f8829a4aSRandall Stewart 	    asoc->cumulative_tsn == tsn) {
1614f8829a4aSRandall Stewart 		/* It is a duplicate */
1615f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvdupdata);
1616f8829a4aSRandall Stewart 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1617f8829a4aSRandall Stewart 			/* Record a dup for the next outbound sack */
1618f8829a4aSRandall Stewart 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1619f8829a4aSRandall Stewart 			asoc->numduptsns++;
1620f8829a4aSRandall Stewart 		}
1621b201f536SRandall Stewart 		asoc->send_sack = 1;
1622f8829a4aSRandall Stewart 		return (0);
1623f8829a4aSRandall Stewart 	}
1624f8829a4aSRandall Stewart 	/* Calculate the number of TSN's between the base and this TSN */
1625d50c1d79SRandall Stewart 	SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
1626f8829a4aSRandall Stewart 	if (gap >= (SCTP_MAPPING_ARRAY << 3)) {
1627f8829a4aSRandall Stewart 		/* Can't hold the bit in the mapping at max array, toss it */
1628f8829a4aSRandall Stewart 		return (0);
1629f8829a4aSRandall Stewart 	}
1630f8829a4aSRandall Stewart 	if (gap >= (uint32_t) (asoc->mapping_array_size << 3)) {
1631207304d4SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
16320696e120SRandall Stewart 		if (sctp_expand_mapping_array(asoc, gap)) {
1633f8829a4aSRandall Stewart 			/* Can't expand, drop it */
1634f8829a4aSRandall Stewart 			return (0);
1635f8829a4aSRandall Stewart 		}
1636f8829a4aSRandall Stewart 	}
1637830d754dSRandall Stewart 	/* EY - for nr_sack */
1638830d754dSRandall Stewart 	nr_gap = gap;
1639830d754dSRandall Stewart 
1640f8829a4aSRandall Stewart 	if (compare_with_wrap(tsn, *high_tsn, MAX_TSN)) {
1641f8829a4aSRandall Stewart 		*high_tsn = tsn;
1642f8829a4aSRandall Stewart 	}
1643f8829a4aSRandall Stewart 	/* See if we have received this one already */
1644f8829a4aSRandall Stewart 	if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
1645f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvdupdata);
1646f8829a4aSRandall Stewart 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1647f8829a4aSRandall Stewart 			/* Record a dup for the next outbound sack */
1648f8829a4aSRandall Stewart 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1649f8829a4aSRandall Stewart 			asoc->numduptsns++;
1650f8829a4aSRandall Stewart 		}
165142551e99SRandall Stewart 		asoc->send_sack = 1;
1652f8829a4aSRandall Stewart 		return (0);
1653f8829a4aSRandall Stewart 	}
1654f8829a4aSRandall Stewart 	/*
1655f8829a4aSRandall Stewart 	 * Check to see about the GONE flag, duplicates would cause a sack
1656f8829a4aSRandall Stewart 	 * to be sent up above
1657f8829a4aSRandall Stewart 	 */
1658ad81507eSRandall Stewart 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1659f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1660f8829a4aSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET))
1661f8829a4aSRandall Stewart 	    ) {
1662f8829a4aSRandall Stewart 		/*
1663f8829a4aSRandall Stewart 		 * wait a minute, this guy is gone, there is no longer a
1664f8829a4aSRandall Stewart 		 * receiver. Send peer an ABORT!
1665f8829a4aSRandall Stewart 		 */
1666f8829a4aSRandall Stewart 		struct mbuf *op_err;
1667f8829a4aSRandall Stewart 
1668f8829a4aSRandall Stewart 		op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1669ceaad40aSRandall Stewart 		sctp_abort_an_association(stcb->sctp_ep, stcb, 0, op_err, SCTP_SO_NOT_LOCKED);
1670f8829a4aSRandall Stewart 		*abort_flag = 1;
1671f8829a4aSRandall Stewart 		return (0);
1672f8829a4aSRandall Stewart 	}
1673f8829a4aSRandall Stewart 	/*
1674f8829a4aSRandall Stewart 	 * Now before going further we see if there is room. If NOT then we
1675f8829a4aSRandall Stewart 	 * MAY let one through only IF this TSN is the one we are waiting
1676f8829a4aSRandall Stewart 	 * for on a partial delivery API.
1677f8829a4aSRandall Stewart 	 */
1678f8829a4aSRandall Stewart 
1679f8829a4aSRandall Stewart 	/* now do the tests */
1680f8829a4aSRandall Stewart 	if (((asoc->cnt_on_all_streams +
1681f8829a4aSRandall Stewart 	    asoc->cnt_on_reasm_queue +
1682b3f1ea41SRandall Stewart 	    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) ||
1683f8829a4aSRandall Stewart 	    (((int)asoc->my_rwnd) <= 0)) {
1684f8829a4aSRandall Stewart 		/*
1685f8829a4aSRandall Stewart 		 * When we have NO room in the rwnd we check to make sure
1686f8829a4aSRandall Stewart 		 * the reader is doing its job...
1687f8829a4aSRandall Stewart 		 */
1688f8829a4aSRandall Stewart 		if (stcb->sctp_socket->so_rcv.sb_cc) {
1689f8829a4aSRandall Stewart 			/* some to read, wake-up */
1690ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1691ceaad40aSRandall Stewart 			struct socket *so;
1692ceaad40aSRandall Stewart 
1693ceaad40aSRandall Stewart 			so = SCTP_INP_SO(stcb->sctp_ep);
1694ceaad40aSRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, 1);
1695ceaad40aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
1696ceaad40aSRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
1697ceaad40aSRandall Stewart 			SCTP_TCB_LOCK(stcb);
1698ceaad40aSRandall Stewart 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
1699ceaad40aSRandall Stewart 			if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1700ceaad40aSRandall Stewart 				/* assoc was freed while we were unlocked */
1701ceaad40aSRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
1702ceaad40aSRandall Stewart 				return (0);
1703ceaad40aSRandall Stewart 			}
1704ceaad40aSRandall Stewart #endif
1705f8829a4aSRandall Stewart 			sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1706ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1707ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
1708ceaad40aSRandall Stewart #endif
1709f8829a4aSRandall Stewart 		}
1710f8829a4aSRandall Stewart 		/* now is it in the mapping array of what we have accepted? */
1711b3f1ea41SRandall Stewart 		if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
1712f8829a4aSRandall Stewart 			/* Nope not in the valid range dump it */
1713f8829a4aSRandall Stewart 			sctp_set_rwnd(stcb, asoc);
1714f8829a4aSRandall Stewart 			if ((asoc->cnt_on_all_streams +
1715f8829a4aSRandall Stewart 			    asoc->cnt_on_reasm_queue +
1716b3f1ea41SRandall Stewart 			    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) {
1717f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_datadropchklmt);
1718f8829a4aSRandall Stewart 			} else {
1719f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_datadroprwnd);
1720f8829a4aSRandall Stewart 			}
1721f8829a4aSRandall Stewart 			indx = *break_flag;
1722f8829a4aSRandall Stewart 			*break_flag = 1;
1723f8829a4aSRandall Stewart 			return (0);
1724f8829a4aSRandall Stewart 		}
1725f8829a4aSRandall Stewart 	}
1726f8829a4aSRandall Stewart 	strmno = ntohs(ch->dp.stream_id);
1727f8829a4aSRandall Stewart 	if (strmno >= asoc->streamincnt) {
1728f8829a4aSRandall Stewart 		struct sctp_paramhdr *phdr;
1729f8829a4aSRandall Stewart 		struct mbuf *mb;
1730f8829a4aSRandall Stewart 
1731f8829a4aSRandall Stewart 		mb = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) * 2),
1732139bc87fSRandall Stewart 		    0, M_DONTWAIT, 1, MT_DATA);
1733f8829a4aSRandall Stewart 		if (mb != NULL) {
1734f8829a4aSRandall Stewart 			/* add some space up front so prepend will work well */
1735139bc87fSRandall Stewart 			SCTP_BUF_RESV_UF(mb, sizeof(struct sctp_chunkhdr));
1736f8829a4aSRandall Stewart 			phdr = mtod(mb, struct sctp_paramhdr *);
1737f8829a4aSRandall Stewart 			/*
1738f8829a4aSRandall Stewart 			 * Error causes are just param's and this one has
1739f8829a4aSRandall Stewart 			 * two back to back phdr, one with the error type
1740f8829a4aSRandall Stewart 			 * and size, the other with the streamid and a rsvd
1741f8829a4aSRandall Stewart 			 */
1742139bc87fSRandall Stewart 			SCTP_BUF_LEN(mb) = (sizeof(struct sctp_paramhdr) * 2);
1743f8829a4aSRandall Stewart 			phdr->param_type = htons(SCTP_CAUSE_INVALID_STREAM);
1744f8829a4aSRandall Stewart 			phdr->param_length =
1745f8829a4aSRandall Stewart 			    htons(sizeof(struct sctp_paramhdr) * 2);
1746f8829a4aSRandall Stewart 			phdr++;
1747f8829a4aSRandall Stewart 			/* We insert the stream in the type field */
1748f8829a4aSRandall Stewart 			phdr->param_type = ch->dp.stream_id;
1749f8829a4aSRandall Stewart 			/* And set the length to 0 for the rsvd field */
1750f8829a4aSRandall Stewart 			phdr->param_length = 0;
1751f8829a4aSRandall Stewart 			sctp_queue_op_err(stcb, mb);
1752f8829a4aSRandall Stewart 		}
1753f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_badsid);
1754207304d4SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
1755d06c82f1SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
1756830d754dSRandall Stewart 		/* EY set this tsn present in  nr_sack's nr_mapping_array */
1757830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
1758830d754dSRandall Stewart 			SCTP_TCB_LOCK_ASSERT(stcb);
1759830d754dSRandall Stewart 			SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
1760d50c1d79SRandall Stewart 			SCTP_REVERSE_OUT_TSN_PRES(gap, tsn, asoc);
1761830d754dSRandall Stewart 		}
1762d06c82f1SRandall Stewart 		if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
1763d06c82f1SRandall Stewart 			/* we have a new high score */
1764d06c82f1SRandall Stewart 			asoc->highest_tsn_inside_map = tsn;
1765830d754dSRandall Stewart 			/* EY nr_sack version of the above */
1766830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack)
1767830d754dSRandall Stewart 				asoc->highest_tsn_inside_nr_map = tsn;
1768b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1769d06c82f1SRandall Stewart 				sctp_log_map(0, 2, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
177080fefe0aSRandall Stewart 			}
1771d06c82f1SRandall Stewart 		}
1772d06c82f1SRandall Stewart 		if (tsn == (asoc->cumulative_tsn + 1)) {
1773d06c82f1SRandall Stewart 			/* Update cum-ack */
1774d06c82f1SRandall Stewart 			asoc->cumulative_tsn = tsn;
1775d06c82f1SRandall Stewart 		}
1776f8829a4aSRandall Stewart 		return (0);
1777f8829a4aSRandall Stewart 	}
1778f8829a4aSRandall Stewart 	/*
1779f8829a4aSRandall Stewart 	 * Before we continue lets validate that we are not being fooled by
1780f8829a4aSRandall Stewart 	 * an evil attacker. We can only have 4k chunks based on our TSN
1781f8829a4aSRandall Stewart 	 * spread allowed by the mapping array 512 * 8 bits, so there is no
1782f8829a4aSRandall Stewart 	 * way our stream sequence numbers could have wrapped. We of course
1783f8829a4aSRandall Stewart 	 * only validate the FIRST fragment so the bit must be set.
1784f8829a4aSRandall Stewart 	 */
1785f8829a4aSRandall Stewart 	strmseq = ntohs(ch->dp.stream_sequence);
1786f42a358aSRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
178718e198d3SRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
178818e198d3SRandall Stewart 	if (asoc->tsn_in_at >= SCTP_TSN_LOG_SIZE) {
178918e198d3SRandall Stewart 		asoc->tsn_in_at = 0;
179018e198d3SRandall Stewart 		asoc->tsn_in_wrapped = 1;
179118e198d3SRandall Stewart 	}
1792f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].tsn = tsn;
1793f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].strm = strmno;
1794f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].seq = strmseq;
1795f1f73e57SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].sz = chk_length;
1796f1f73e57SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].flgs = chunk_flags;
179718e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].stcb = (void *)stcb;
179818e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].in_pos = asoc->tsn_in_at;
179918e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].in_out = 1;
1800f42a358aSRandall Stewart 	asoc->tsn_in_at++;
1801f42a358aSRandall Stewart #endif
1802f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_FIRST_FRAG) &&
1803d61a0ae0SRandall Stewart 	    (TAILQ_EMPTY(&asoc->resetHead)) &&
1804f42a358aSRandall Stewart 	    (chunk_flags & SCTP_DATA_UNORDERED) == 0 &&
1805f8829a4aSRandall Stewart 	    (compare_with_wrap(asoc->strmin[strmno].last_sequence_delivered,
1806f8829a4aSRandall Stewart 	    strmseq, MAX_SEQ) ||
1807f8829a4aSRandall Stewart 	    asoc->strmin[strmno].last_sequence_delivered == strmseq)) {
1808f8829a4aSRandall Stewart 		/* The incoming sseq is behind where we last delivered? */
1809ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1, "EVIL/Broken-Dup S-SEQ:%d delivered:%d from peer, Abort!\n",
1810ad81507eSRandall Stewart 		    strmseq, asoc->strmin[strmno].last_sequence_delivered);
1811139bc87fSRandall Stewart 		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1812f8829a4aSRandall Stewart 		    0, M_DONTWAIT, 1, MT_DATA);
1813f8829a4aSRandall Stewart 		if (oper) {
1814f8829a4aSRandall Stewart 			struct sctp_paramhdr *ph;
1815f8829a4aSRandall Stewart 			uint32_t *ippp;
1816f8829a4aSRandall Stewart 
1817139bc87fSRandall Stewart 			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
1818f8829a4aSRandall Stewart 			    (3 * sizeof(uint32_t));
1819f8829a4aSRandall Stewart 			ph = mtod(oper, struct sctp_paramhdr *);
1820f8829a4aSRandall Stewart 			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1821139bc87fSRandall Stewart 			ph->param_length = htons(SCTP_BUF_LEN(oper));
1822f8829a4aSRandall Stewart 			ippp = (uint32_t *) (ph + 1);
1823a5d547adSRandall Stewart 			*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_14);
1824f8829a4aSRandall Stewart 			ippp++;
1825f8829a4aSRandall Stewart 			*ippp = tsn;
1826f8829a4aSRandall Stewart 			ippp++;
1827f8829a4aSRandall Stewart 			*ippp = ((strmno << 16) | strmseq);
1828f8829a4aSRandall Stewart 
1829f8829a4aSRandall Stewart 		}
1830a5d547adSRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_14;
1831f8829a4aSRandall Stewart 		sctp_abort_an_association(stcb->sctp_ep, stcb,
1832ceaad40aSRandall Stewart 		    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1833f8829a4aSRandall Stewart 		*abort_flag = 1;
1834f8829a4aSRandall Stewart 		return (0);
1835f8829a4aSRandall Stewart 	}
1836f42a358aSRandall Stewart 	/************************************
1837f42a358aSRandall Stewart 	 * From here down we may find ch-> invalid
1838f42a358aSRandall Stewart 	 * so its a good idea NOT to use it.
1839f42a358aSRandall Stewart 	 *************************************/
1840f42a358aSRandall Stewart 
1841f8829a4aSRandall Stewart 	the_len = (chk_length - sizeof(struct sctp_data_chunk));
1842f8829a4aSRandall Stewart 	if (last_chunk == 0) {
184344b7479bSRandall Stewart 		dmbuf = SCTP_M_COPYM(*m,
1844f8829a4aSRandall Stewart 		    (offset + sizeof(struct sctp_data_chunk)),
1845f8829a4aSRandall Stewart 		    the_len, M_DONTWAIT);
1846f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
1847b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
1848f8829a4aSRandall Stewart 			struct mbuf *mat;
1849f8829a4aSRandall Stewart 
1850f8829a4aSRandall Stewart 			mat = dmbuf;
1851f8829a4aSRandall Stewart 			while (mat) {
1852139bc87fSRandall Stewart 				if (SCTP_BUF_IS_EXTENDED(mat)) {
1853f8829a4aSRandall Stewart 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
1854f8829a4aSRandall Stewart 				}
1855139bc87fSRandall Stewart 				mat = SCTP_BUF_NEXT(mat);
1856f8829a4aSRandall Stewart 			}
1857f8829a4aSRandall Stewart 		}
1858f8829a4aSRandall Stewart #endif
1859f8829a4aSRandall Stewart 	} else {
1860f8829a4aSRandall Stewart 		/* We can steal the last chunk */
1861139bc87fSRandall Stewart 		int l_len;
1862139bc87fSRandall Stewart 
1863f8829a4aSRandall Stewart 		dmbuf = *m;
1864f8829a4aSRandall Stewart 		/* lop off the top part */
1865f8829a4aSRandall Stewart 		m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk)));
1866139bc87fSRandall Stewart 		if (SCTP_BUF_NEXT(dmbuf) == NULL) {
1867139bc87fSRandall Stewart 			l_len = SCTP_BUF_LEN(dmbuf);
1868139bc87fSRandall Stewart 		} else {
1869139bc87fSRandall Stewart 			/*
1870139bc87fSRandall Stewart 			 * need to count up the size hopefully does not hit
1871139bc87fSRandall Stewart 			 * this to often :-0
1872139bc87fSRandall Stewart 			 */
1873139bc87fSRandall Stewart 			struct mbuf *lat;
1874139bc87fSRandall Stewart 
1875139bc87fSRandall Stewart 			l_len = 0;
1876139bc87fSRandall Stewart 			lat = dmbuf;
1877139bc87fSRandall Stewart 			while (lat) {
1878139bc87fSRandall Stewart 				l_len += SCTP_BUF_LEN(lat);
1879139bc87fSRandall Stewart 				lat = SCTP_BUF_NEXT(lat);
1880139bc87fSRandall Stewart 			}
1881139bc87fSRandall Stewart 		}
1882139bc87fSRandall Stewart 		if (l_len > the_len) {
1883f8829a4aSRandall Stewart 			/* Trim the end round bytes off  too */
1884139bc87fSRandall Stewart 			m_adj(dmbuf, -(l_len - the_len));
1885f8829a4aSRandall Stewart 		}
1886f8829a4aSRandall Stewart 	}
1887f8829a4aSRandall Stewart 	if (dmbuf == NULL) {
1888f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_nomem);
1889f8829a4aSRandall Stewart 		return (0);
1890f8829a4aSRandall Stewart 	}
1891f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG &&
1892f8829a4aSRandall Stewart 	    asoc->fragmented_delivery_inprogress == 0 &&
1893f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->resetHead) &&
1894f42a358aSRandall Stewart 	    ((ordered == 0) ||
1895f8829a4aSRandall Stewart 	    ((asoc->strmin[strmno].last_sequence_delivered + 1) == strmseq &&
1896f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->strmin[strmno].inqueue)))) {
1897f8829a4aSRandall Stewart 		/* Candidate for express delivery */
1898f8829a4aSRandall Stewart 		/*
1899f8829a4aSRandall Stewart 		 * Its not fragmented, No PD-API is up, Nothing in the
1900f8829a4aSRandall Stewart 		 * delivery queue, Its un-ordered OR ordered and the next to
1901f8829a4aSRandall Stewart 		 * deliver AND nothing else is stuck on the stream queue,
1902f8829a4aSRandall Stewart 		 * And there is room for it in the socket buffer. Lets just
1903f8829a4aSRandall Stewart 		 * stuff it up the buffer....
1904f8829a4aSRandall Stewart 		 */
1905f8829a4aSRandall Stewart 
1906f8829a4aSRandall Stewart 		/* It would be nice to avoid this copy if we could :< */
1907f8829a4aSRandall Stewart 		sctp_alloc_a_readq(stcb, control);
1908f8829a4aSRandall Stewart 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1909f42a358aSRandall Stewart 		    protocol_id,
1910f8829a4aSRandall Stewart 		    stcb->asoc.context,
1911f8829a4aSRandall Stewart 		    strmno, strmseq,
1912f42a358aSRandall Stewart 		    chunk_flags,
1913f8829a4aSRandall Stewart 		    dmbuf);
1914f8829a4aSRandall Stewart 		if (control == NULL) {
1915f8829a4aSRandall Stewart 			goto failed_express_del;
1916f8829a4aSRandall Stewart 		}
1917cfde3ff7SRandall Stewart 		sctp_add_to_readq(stcb->sctp_ep, stcb,
1918cfde3ff7SRandall Stewart 		    control, &stcb->sctp_socket->so_rcv,
1919cfde3ff7SRandall Stewart 		    1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
1920830d754dSRandall Stewart 
1921830d754dSRandall Stewart 		/*
1922830d754dSRandall Stewart 		 * EY here I should check if this delivered tsn is
1923830d754dSRandall Stewart 		 * out_of_order, if yes then update the nr_map
1924830d754dSRandall Stewart 		 */
1925830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
1926830d754dSRandall Stewart 			/*
1927830d754dSRandall Stewart 			 * EY check if the mapping_array and nr_mapping
1928830d754dSRandall Stewart 			 * array are consistent
1929830d754dSRandall Stewart 			 */
1930830d754dSRandall Stewart 			if (asoc->mapping_array_base_tsn != asoc->nr_mapping_array_base_tsn)
1931830d754dSRandall Stewart 				/*
1932830d754dSRandall Stewart 				 * printf("EY-IN
1933830d754dSRandall Stewart 				 * sctp_process_a_data_chunk(5): Something
1934830d754dSRandall Stewart 				 * is wrong the map base tsn" "\nEY-and
1935830d754dSRandall Stewart 				 * nr_map base tsn should be equal.");
1936830d754dSRandall Stewart 				 */
1937830d754dSRandall Stewart 				/* EY debugging block */
1938830d754dSRandall Stewart 			{
1939830d754dSRandall Stewart 				/*
1940830d754dSRandall Stewart 				 * printf("\nEY-Calculating an
1941830d754dSRandall Stewart 				 * nr_gap!!\nmapping_array_size = %d
1942830d754dSRandall Stewart 				 * nr_mapping_array_size = %d"
1943830d754dSRandall Stewart 				 * "\nEY-mapping_array_base = %d
1944830d754dSRandall Stewart 				 * nr_mapping_array_base =
1945830d754dSRandall Stewart 				 * %d\nEY-highest_tsn_inside_map = %d"
1946830d754dSRandall Stewart 				 * "highest_tsn_inside_nr_map = %d\nEY-TSN =
1947830d754dSRandall Stewart 				 * %d nr_gap = %d",asoc->mapping_array_size,
1948830d754dSRandall Stewart 				 * asoc->nr_mapping_array_size,
1949830d754dSRandall Stewart 				 * asoc->mapping_array_base_tsn,
1950830d754dSRandall Stewart 				 * asoc->nr_mapping_array_base_tsn,
1951830d754dSRandall Stewart 				 * asoc->highest_tsn_inside_map,
1952830d754dSRandall Stewart 				 * asoc->highest_tsn_inside_nr_map,tsn,nr_gap
1953830d754dSRandall Stewart 				 * );
1954830d754dSRandall Stewart 				 */
1955830d754dSRandall Stewart 			}
1956830d754dSRandall Stewart 			/* EY - not %100 sure about the lock thing */
1957830d754dSRandall Stewart 			SCTP_TCB_LOCK_ASSERT(stcb);
1958830d754dSRandall Stewart 			SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
1959d50c1d79SRandall Stewart 			SCTP_REVERSE_OUT_TSN_PRES(nr_gap, tsn, asoc);
1960830d754dSRandall Stewart 			if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
1961830d754dSRandall Stewart 				asoc->highest_tsn_inside_nr_map = tsn;
1962830d754dSRandall Stewart 		}
1963f42a358aSRandall Stewart 		if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1964f8829a4aSRandall Stewart 			/* for ordered, bump what we delivered */
1965f8829a4aSRandall Stewart 			asoc->strmin[strmno].last_sequence_delivered++;
1966f8829a4aSRandall Stewart 		}
1967f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvexpress);
1968b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
19696a91f103SRandall Stewart 			sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno,
1970f8829a4aSRandall Stewart 			    SCTP_STR_LOG_FROM_EXPRS_DEL);
197180fefe0aSRandall Stewart 		}
1972f8829a4aSRandall Stewart 		control = NULL;
1973f8829a4aSRandall Stewart 		goto finish_express_del;
1974f8829a4aSRandall Stewart 	}
1975f8829a4aSRandall Stewart failed_express_del:
1976f8829a4aSRandall Stewart 	/* If we reach here this is a new chunk */
1977f8829a4aSRandall Stewart 	chk = NULL;
1978f8829a4aSRandall Stewart 	control = NULL;
1979f8829a4aSRandall Stewart 	/* Express for fragmented delivery? */
1980f8829a4aSRandall Stewart 	if ((asoc->fragmented_delivery_inprogress) &&
1981f8829a4aSRandall Stewart 	    (stcb->asoc.control_pdapi) &&
1982f8829a4aSRandall Stewart 	    (asoc->str_of_pdapi == strmno) &&
1983f8829a4aSRandall Stewart 	    (asoc->ssn_of_pdapi == strmseq)
1984f8829a4aSRandall Stewart 	    ) {
1985f8829a4aSRandall Stewart 		control = stcb->asoc.control_pdapi;
1986f42a358aSRandall Stewart 		if ((chunk_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
1987f8829a4aSRandall Stewart 			/* Can't be another first? */
1988f8829a4aSRandall Stewart 			goto failed_pdapi_express_del;
1989f8829a4aSRandall Stewart 		}
1990f8829a4aSRandall Stewart 		if (tsn == (control->sinfo_tsn + 1)) {
1991f8829a4aSRandall Stewart 			/* Yep, we can add it on */
1992f8829a4aSRandall Stewart 			int end = 0;
1993f8829a4aSRandall Stewart 			uint32_t cumack;
1994f8829a4aSRandall Stewart 
1995f42a358aSRandall Stewart 			if (chunk_flags & SCTP_DATA_LAST_FRAG) {
1996f8829a4aSRandall Stewart 				end = 1;
1997f8829a4aSRandall Stewart 			}
1998f8829a4aSRandall Stewart 			cumack = asoc->cumulative_tsn;
1999f8829a4aSRandall Stewart 			if ((cumack + 1) == tsn)
2000f8829a4aSRandall Stewart 				cumack = tsn;
2001f8829a4aSRandall Stewart 
2002f8829a4aSRandall Stewart 			if (sctp_append_to_readq(stcb->sctp_ep, stcb, control, dmbuf, end,
2003f8829a4aSRandall Stewart 			    tsn,
2004f8829a4aSRandall Stewart 			    &stcb->sctp_socket->so_rcv)) {
2005ad81507eSRandall Stewart 				SCTP_PRINTF("Append fails end:%d\n", end);
2006f8829a4aSRandall Stewart 				goto failed_pdapi_express_del;
2007f8829a4aSRandall Stewart 			}
2008830d754dSRandall Stewart 			/*
2009830d754dSRandall Stewart 			 * EY It is appended to the read queue in prev if
2010830d754dSRandall Stewart 			 * block here I should check if this delivered tsn
2011830d754dSRandall Stewart 			 * is out_of_order, if yes then update the nr_map
2012830d754dSRandall Stewart 			 */
2013830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
2014830d754dSRandall Stewart 				/* EY debugging block */
2015830d754dSRandall Stewart 				{
2016830d754dSRandall Stewart 					/*
2017830d754dSRandall Stewart 					 * printf("\nEY-Calculating an
2018830d754dSRandall Stewart 					 * nr_gap!!\nEY-mapping_array_size =
2019830d754dSRandall Stewart 					 * %d nr_mapping_array_size = %d"
2020830d754dSRandall Stewart 					 * "\nEY-mapping_array_base = %d
2021830d754dSRandall Stewart 					 * nr_mapping_array_base =
2022830d754dSRandall Stewart 					 * %d\nEY-highest_tsn_inside_map =
2023830d754dSRandall Stewart 					 * %d" "highest_tsn_inside_nr_map =
2024830d754dSRandall Stewart 					 * %d\nEY-TSN = %d nr_gap =
2025830d754dSRandall Stewart 					 * %d",asoc->mapping_array_size,
2026830d754dSRandall Stewart 					 * asoc->nr_mapping_array_size,
2027830d754dSRandall Stewart 					 * asoc->mapping_array_base_tsn,
2028830d754dSRandall Stewart 					 * asoc->nr_mapping_array_base_tsn,
2029830d754dSRandall Stewart 					 * asoc->highest_tsn_inside_map,
2030830d754dSRandall Stewart 					 * asoc->highest_tsn_inside_nr_map,ts
2031830d754dSRandall Stewart 					 * n,nr_gap);
2032830d754dSRandall Stewart 					 */
2033830d754dSRandall Stewart 				}
2034830d754dSRandall Stewart 				/* EY - not %100 sure about the lock thing */
2035830d754dSRandall Stewart 				SCTP_TCB_LOCK_ASSERT(stcb);
2036830d754dSRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
2037d50c1d79SRandall Stewart 				SCTP_REVERSE_OUT_TSN_PRES(nr_gap, tsn, asoc);
2038830d754dSRandall Stewart 				if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
2039830d754dSRandall Stewart 					asoc->highest_tsn_inside_nr_map = tsn;
2040830d754dSRandall Stewart 			}
2041f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvexpressm);
2042f8829a4aSRandall Stewart 			control->sinfo_tsn = tsn;
2043f8829a4aSRandall Stewart 			asoc->tsn_last_delivered = tsn;
2044f42a358aSRandall Stewart 			asoc->fragment_flags = chunk_flags;
2045f8829a4aSRandall Stewart 			asoc->tsn_of_pdapi_last_delivered = tsn;
2046f42a358aSRandall Stewart 			asoc->last_flags_delivered = chunk_flags;
2047f8829a4aSRandall Stewart 			asoc->last_strm_seq_delivered = strmseq;
2048f8829a4aSRandall Stewart 			asoc->last_strm_no_delivered = strmno;
2049f8829a4aSRandall Stewart 			if (end) {
2050f8829a4aSRandall Stewart 				/* clean up the flags and such */
2051f8829a4aSRandall Stewart 				asoc->fragmented_delivery_inprogress = 0;
2052f42a358aSRandall Stewart 				if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
2053f8829a4aSRandall Stewart 					asoc->strmin[strmno].last_sequence_delivered++;
2054a5d547adSRandall Stewart 				}
2055f8829a4aSRandall Stewart 				stcb->asoc.control_pdapi = NULL;
2056139bc87fSRandall Stewart 				if (TAILQ_EMPTY(&asoc->reasmqueue) == 0) {
2057139bc87fSRandall Stewart 					/*
2058139bc87fSRandall Stewart 					 * There could be another message
2059139bc87fSRandall Stewart 					 * ready
2060139bc87fSRandall Stewart 					 */
2061139bc87fSRandall Stewart 					need_reasm_check = 1;
2062139bc87fSRandall Stewart 				}
2063f8829a4aSRandall Stewart 			}
2064f8829a4aSRandall Stewart 			control = NULL;
2065f8829a4aSRandall Stewart 			goto finish_express_del;
2066f8829a4aSRandall Stewart 		}
2067f8829a4aSRandall Stewart 	}
2068f8829a4aSRandall Stewart failed_pdapi_express_del:
2069f8829a4aSRandall Stewart 	control = NULL;
2070f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
2071f8829a4aSRandall Stewart 		sctp_alloc_a_chunk(stcb, chk);
2072f8829a4aSRandall Stewart 		if (chk == NULL) {
2073f8829a4aSRandall Stewart 			/* No memory so we drop the chunk */
2074f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_nomem);
2075f8829a4aSRandall Stewart 			if (last_chunk == 0) {
2076f8829a4aSRandall Stewart 				/* we copied it, free the copy */
2077f8829a4aSRandall Stewart 				sctp_m_freem(dmbuf);
2078f8829a4aSRandall Stewart 			}
2079f8829a4aSRandall Stewart 			return (0);
2080f8829a4aSRandall Stewart 		}
2081f8829a4aSRandall Stewart 		chk->rec.data.TSN_seq = tsn;
2082f8829a4aSRandall Stewart 		chk->no_fr_allowed = 0;
2083f8829a4aSRandall Stewart 		chk->rec.data.stream_seq = strmseq;
2084f8829a4aSRandall Stewart 		chk->rec.data.stream_number = strmno;
2085f42a358aSRandall Stewart 		chk->rec.data.payloadtype = protocol_id;
2086f8829a4aSRandall Stewart 		chk->rec.data.context = stcb->asoc.context;
2087f8829a4aSRandall Stewart 		chk->rec.data.doing_fast_retransmit = 0;
2088f42a358aSRandall Stewart 		chk->rec.data.rcv_flags = chunk_flags;
2089f8829a4aSRandall Stewart 		chk->asoc = asoc;
2090f8829a4aSRandall Stewart 		chk->send_size = the_len;
2091f8829a4aSRandall Stewart 		chk->whoTo = net;
2092f8829a4aSRandall Stewart 		atomic_add_int(&net->ref_count, 1);
2093f8829a4aSRandall Stewart 		chk->data = dmbuf;
2094f8829a4aSRandall Stewart 	} else {
2095f8829a4aSRandall Stewart 		sctp_alloc_a_readq(stcb, control);
2096f8829a4aSRandall Stewart 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
2097f42a358aSRandall Stewart 		    protocol_id,
2098f8829a4aSRandall Stewart 		    stcb->asoc.context,
2099f8829a4aSRandall Stewart 		    strmno, strmseq,
2100f42a358aSRandall Stewart 		    chunk_flags,
2101f8829a4aSRandall Stewart 		    dmbuf);
2102f8829a4aSRandall Stewart 		if (control == NULL) {
2103f8829a4aSRandall Stewart 			/* No memory so we drop the chunk */
2104f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_nomem);
2105f8829a4aSRandall Stewart 			if (last_chunk == 0) {
2106f8829a4aSRandall Stewart 				/* we copied it, free the copy */
2107f8829a4aSRandall Stewart 				sctp_m_freem(dmbuf);
2108f8829a4aSRandall Stewart 			}
2109f8829a4aSRandall Stewart 			return (0);
2110f8829a4aSRandall Stewart 		}
2111f8829a4aSRandall Stewart 		control->length = the_len;
2112f8829a4aSRandall Stewart 	}
2113f8829a4aSRandall Stewart 
2114f8829a4aSRandall Stewart 	/* Mark it as received */
2115f8829a4aSRandall Stewart 	/* Now queue it where it belongs */
2116f8829a4aSRandall Stewart 	if (control != NULL) {
2117f8829a4aSRandall Stewart 		/* First a sanity check */
2118f8829a4aSRandall Stewart 		if (asoc->fragmented_delivery_inprogress) {
2119f8829a4aSRandall Stewart 			/*
2120f8829a4aSRandall Stewart 			 * Ok, we have a fragmented delivery in progress if
2121f8829a4aSRandall Stewart 			 * this chunk is next to deliver OR belongs in our
2122f8829a4aSRandall Stewart 			 * view to the reassembly, the peer is evil or
2123f8829a4aSRandall Stewart 			 * broken.
2124f8829a4aSRandall Stewart 			 */
2125f8829a4aSRandall Stewart 			uint32_t estimate_tsn;
2126f8829a4aSRandall Stewart 
2127f8829a4aSRandall Stewart 			estimate_tsn = asoc->tsn_last_delivered + 1;
2128f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->reasmqueue) &&
2129f8829a4aSRandall Stewart 			    (estimate_tsn == control->sinfo_tsn)) {
2130f8829a4aSRandall Stewart 				/* Evil/Broke peer */
2131f8829a4aSRandall Stewart 				sctp_m_freem(control->data);
2132f8829a4aSRandall Stewart 				control->data = NULL;
21335bead436SRandall Stewart 				if (control->whoFrom) {
2134f8829a4aSRandall Stewart 					sctp_free_remote_addr(control->whoFrom);
21355bead436SRandall Stewart 					control->whoFrom = NULL;
21365bead436SRandall Stewart 				}
2137f8829a4aSRandall Stewart 				sctp_free_a_readq(stcb, control);
2138139bc87fSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
2139f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
2140f8829a4aSRandall Stewart 				if (oper) {
2141f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
2142f8829a4aSRandall Stewart 					uint32_t *ippp;
2143f8829a4aSRandall Stewart 
2144139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) =
2145f8829a4aSRandall Stewart 					    sizeof(struct sctp_paramhdr) +
2146f8829a4aSRandall Stewart 					    (3 * sizeof(uint32_t));
2147f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
2148f8829a4aSRandall Stewart 					ph->param_type =
2149f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2150139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
2151f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
2152a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_15);
2153f8829a4aSRandall Stewart 					ippp++;
2154f8829a4aSRandall Stewart 					*ippp = tsn;
2155f8829a4aSRandall Stewart 					ippp++;
2156f8829a4aSRandall Stewart 					*ippp = ((strmno << 16) | strmseq);
2157f8829a4aSRandall Stewart 				}
2158a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_15;
2159f8829a4aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb,
2160ceaad40aSRandall Stewart 				    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
2161f8829a4aSRandall Stewart 
2162f8829a4aSRandall Stewart 				*abort_flag = 1;
2163f8829a4aSRandall Stewart 				return (0);
2164f8829a4aSRandall Stewart 			} else {
2165f8829a4aSRandall Stewart 				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
2166f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
2167f8829a4aSRandall Stewart 					control->data = NULL;
21685bead436SRandall Stewart 					if (control->whoFrom) {
2169f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
21705bead436SRandall Stewart 						control->whoFrom = NULL;
21715bead436SRandall Stewart 					}
2172f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
2173f8829a4aSRandall Stewart 
2174139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
2175f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
2176f8829a4aSRandall Stewart 					if (oper) {
2177f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
2178f8829a4aSRandall Stewart 						uint32_t *ippp;
2179f8829a4aSRandall Stewart 
2180139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
2181f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
2182f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
2183f8829a4aSRandall Stewart 						ph = mtod(oper,
2184f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
2185f8829a4aSRandall Stewart 						ph->param_type =
2186f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2187f8829a4aSRandall Stewart 						ph->param_length =
2188139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
2189f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
2190a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_16);
2191f8829a4aSRandall Stewart 						ippp++;
2192f8829a4aSRandall Stewart 						*ippp = tsn;
2193f8829a4aSRandall Stewart 						ippp++;
2194f8829a4aSRandall Stewart 						*ippp = ((strmno << 16) | strmseq);
2195f8829a4aSRandall Stewart 					}
2196a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_16;
2197f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
2198ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
2199f8829a4aSRandall Stewart 
2200f8829a4aSRandall Stewart 					*abort_flag = 1;
2201f8829a4aSRandall Stewart 					return (0);
2202f8829a4aSRandall Stewart 				}
2203f8829a4aSRandall Stewart 			}
2204f8829a4aSRandall Stewart 		} else {
2205f8829a4aSRandall Stewart 			/* No PDAPI running */
2206f8829a4aSRandall Stewart 			if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
2207f8829a4aSRandall Stewart 				/*
2208f8829a4aSRandall Stewart 				 * Reassembly queue is NOT empty validate
2209f8829a4aSRandall Stewart 				 * that this tsn does not need to be in
2210f8829a4aSRandall Stewart 				 * reasembly queue. If it does then our peer
2211f8829a4aSRandall Stewart 				 * is broken or evil.
2212f8829a4aSRandall Stewart 				 */
2213f8829a4aSRandall Stewart 				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
2214f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
2215f8829a4aSRandall Stewart 					control->data = NULL;
22165bead436SRandall Stewart 					if (control->whoFrom) {
2217f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
22185bead436SRandall Stewart 						control->whoFrom = NULL;
22195bead436SRandall Stewart 					}
2220f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
2221139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
2222f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
2223f8829a4aSRandall Stewart 					if (oper) {
2224f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
2225f8829a4aSRandall Stewart 						uint32_t *ippp;
2226f8829a4aSRandall Stewart 
2227139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
2228f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
2229f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
2230f8829a4aSRandall Stewart 						ph = mtod(oper,
2231f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
2232f8829a4aSRandall Stewart 						ph->param_type =
2233f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2234f8829a4aSRandall Stewart 						ph->param_length =
2235139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
2236f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
2237a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_17);
2238f8829a4aSRandall Stewart 						ippp++;
2239f8829a4aSRandall Stewart 						*ippp = tsn;
2240f8829a4aSRandall Stewart 						ippp++;
2241f8829a4aSRandall Stewart 						*ippp = ((strmno << 16) | strmseq);
2242f8829a4aSRandall Stewart 					}
2243a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_17;
2244f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
2245ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
2246f8829a4aSRandall Stewart 
2247f8829a4aSRandall Stewart 					*abort_flag = 1;
2248f8829a4aSRandall Stewart 					return (0);
2249f8829a4aSRandall Stewart 				}
2250f8829a4aSRandall Stewart 			}
2251f8829a4aSRandall Stewart 		}
2252f8829a4aSRandall Stewart 		/* ok, if we reach here we have passed the sanity checks */
2253f42a358aSRandall Stewart 		if (chunk_flags & SCTP_DATA_UNORDERED) {
2254f8829a4aSRandall Stewart 			/* queue directly into socket buffer */
2255f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep, stcb,
2256f8829a4aSRandall Stewart 			    control,
2257cfde3ff7SRandall Stewart 			    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
2258830d754dSRandall Stewart 
2259830d754dSRandall Stewart 			/*
2260830d754dSRandall Stewart 			 * EY It is added to the read queue in prev if block
2261830d754dSRandall Stewart 			 * here I should check if this delivered tsn is
2262830d754dSRandall Stewart 			 * out_of_order, if yes then update the nr_map
2263830d754dSRandall Stewart 			 */
2264830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
2265830d754dSRandall Stewart 				/*
2266830d754dSRandall Stewart 				 * EY check if the mapping_array and
2267830d754dSRandall Stewart 				 * nr_mapping array are consistent
2268830d754dSRandall Stewart 				 */
2269830d754dSRandall Stewart 				if (asoc->mapping_array_base_tsn != asoc->nr_mapping_array_base_tsn)
2270830d754dSRandall Stewart 					/*
2271830d754dSRandall Stewart 					 * printf("EY-IN
2272830d754dSRandall Stewart 					 * sctp_process_a_data_chunk(6):
2273830d754dSRandall Stewart 					 * Something is wrong the map base
2274830d754dSRandall Stewart 					 * tsn" "\nEY-and nr_map base tsn
2275830d754dSRandall Stewart 					 * should be equal.");
2276830d754dSRandall Stewart 					 */
2277830d754dSRandall Stewart 					/*
2278830d754dSRandall Stewart 					 * EY - not %100 sure about the lock
2279830d754dSRandall Stewart 					 * thing, i think we don't need the
2280830d754dSRandall Stewart 					 * below,
2281830d754dSRandall Stewart 					 */
2282830d754dSRandall Stewart 					/* SCTP_TCB_LOCK_ASSERT(stcb); */
2283830d754dSRandall Stewart 				{
2284830d754dSRandall Stewart 					/*
2285830d754dSRandall Stewart 					 * printf("\nEY-Calculating an
2286830d754dSRandall Stewart 					 * nr_gap!!\nEY-mapping_array_size =
2287830d754dSRandall Stewart 					 * %d nr_mapping_array_size = %d"
2288830d754dSRandall Stewart 					 * "\nEY-mapping_array_base = %d
2289830d754dSRandall Stewart 					 * nr_mapping_array_base =
2290830d754dSRandall Stewart 					 * %d\nEY-highest_tsn_inside_map =
2291830d754dSRandall Stewart 					 * %d" "highest_tsn_inside_nr_map =
2292830d754dSRandall Stewart 					 * %d\nEY-TSN = %d nr_gap =
2293830d754dSRandall Stewart 					 * %d",asoc->mapping_array_size,
2294830d754dSRandall Stewart 					 * asoc->nr_mapping_array_size,
2295830d754dSRandall Stewart 					 * asoc->mapping_array_base_tsn,
2296830d754dSRandall Stewart 					 * asoc->nr_mapping_array_base_tsn,
2297830d754dSRandall Stewart 					 * asoc->highest_tsn_inside_map,
2298830d754dSRandall Stewart 					 * asoc->highest_tsn_inside_nr_map,ts
2299830d754dSRandall Stewart 					 * n,nr_gap);
2300830d754dSRandall Stewart 					 */
2301830d754dSRandall Stewart 				}
2302830d754dSRandall Stewart 				SCTP_TCB_LOCK_ASSERT(stcb);
2303830d754dSRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
2304d50c1d79SRandall Stewart 				SCTP_REVERSE_OUT_TSN_PRES(nr_gap, tsn, asoc);
2305830d754dSRandall Stewart 				if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
2306830d754dSRandall Stewart 					asoc->highest_tsn_inside_nr_map = tsn;
2307830d754dSRandall Stewart 			}
2308f8829a4aSRandall Stewart 		} else {
2309f8829a4aSRandall Stewart 			/*
2310f8829a4aSRandall Stewart 			 * Special check for when streams are resetting. We
2311f8829a4aSRandall Stewart 			 * could be more smart about this and check the
2312f8829a4aSRandall Stewart 			 * actual stream to see if it is not being reset..
2313f8829a4aSRandall Stewart 			 * that way we would not create a HOLB when amongst
2314f8829a4aSRandall Stewart 			 * streams being reset and those not being reset.
2315f8829a4aSRandall Stewart 			 *
2316f8829a4aSRandall Stewart 			 * We take complete messages that have a stream reset
2317f8829a4aSRandall Stewart 			 * intervening (aka the TSN is after where our
2318f8829a4aSRandall Stewart 			 * cum-ack needs to be) off and put them on a
2319f8829a4aSRandall Stewart 			 * pending_reply_queue. The reassembly ones we do
2320f8829a4aSRandall Stewart 			 * not have to worry about since they are all sorted
2321f8829a4aSRandall Stewart 			 * and proceessed by TSN order. It is only the
2322f8829a4aSRandall Stewart 			 * singletons I must worry about.
2323f8829a4aSRandall Stewart 			 */
2324f8829a4aSRandall Stewart 			if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2325d61a0ae0SRandall Stewart 			    ((compare_with_wrap(tsn, liste->tsn, MAX_TSN)))
2326f8829a4aSRandall Stewart 			    ) {
2327f8829a4aSRandall Stewart 				/*
2328f8829a4aSRandall Stewart 				 * yep its past where we need to reset... go
2329f8829a4aSRandall Stewart 				 * ahead and queue it.
2330f8829a4aSRandall Stewart 				 */
2331f8829a4aSRandall Stewart 				if (TAILQ_EMPTY(&asoc->pending_reply_queue)) {
2332f8829a4aSRandall Stewart 					/* first one on */
2333f8829a4aSRandall Stewart 					TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2334f8829a4aSRandall Stewart 				} else {
2335f8829a4aSRandall Stewart 					struct sctp_queued_to_read *ctlOn;
2336f8829a4aSRandall Stewart 					unsigned char inserted = 0;
2337f8829a4aSRandall Stewart 
2338f8829a4aSRandall Stewart 					ctlOn = TAILQ_FIRST(&asoc->pending_reply_queue);
2339f8829a4aSRandall Stewart 					while (ctlOn) {
2340f8829a4aSRandall Stewart 						if (compare_with_wrap(control->sinfo_tsn,
2341f8829a4aSRandall Stewart 						    ctlOn->sinfo_tsn, MAX_TSN)) {
2342f8829a4aSRandall Stewart 							ctlOn = TAILQ_NEXT(ctlOn, next);
2343f8829a4aSRandall Stewart 						} else {
2344f8829a4aSRandall Stewart 							/* found it */
2345f8829a4aSRandall Stewart 							TAILQ_INSERT_BEFORE(ctlOn, control, next);
2346f8829a4aSRandall Stewart 							inserted = 1;
2347f8829a4aSRandall Stewart 							break;
2348f8829a4aSRandall Stewart 						}
2349f8829a4aSRandall Stewart 					}
2350f8829a4aSRandall Stewart 					if (inserted == 0) {
2351f8829a4aSRandall Stewart 						/*
2352f8829a4aSRandall Stewart 						 * must be put at end, use
2353f8829a4aSRandall Stewart 						 * prevP (all setup from
2354f8829a4aSRandall Stewart 						 * loop) to setup nextP.
2355f8829a4aSRandall Stewart 						 */
2356f8829a4aSRandall Stewart 						TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2357f8829a4aSRandall Stewart 					}
2358f8829a4aSRandall Stewart 				}
2359f8829a4aSRandall Stewart 			} else {
2360f8829a4aSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, control, abort_flag);
2361f8829a4aSRandall Stewart 				if (*abort_flag) {
2362f8829a4aSRandall Stewart 					return (0);
2363f8829a4aSRandall Stewart 				}
2364f8829a4aSRandall Stewart 			}
2365f8829a4aSRandall Stewart 		}
2366f8829a4aSRandall Stewart 	} else {
2367f8829a4aSRandall Stewart 		/* Into the re-assembly queue */
2368f8829a4aSRandall Stewart 		sctp_queue_data_for_reasm(stcb, asoc, chk, abort_flag);
2369f8829a4aSRandall Stewart 		if (*abort_flag) {
2370a5d547adSRandall Stewart 			/*
2371a5d547adSRandall Stewart 			 * the assoc is now gone and chk was put onto the
2372a5d547adSRandall Stewart 			 * reasm queue, which has all been freed.
2373a5d547adSRandall Stewart 			 */
2374a5d547adSRandall Stewart 			*m = NULL;
2375f8829a4aSRandall Stewart 			return (0);
2376f8829a4aSRandall Stewart 		}
2377f8829a4aSRandall Stewart 	}
2378f8829a4aSRandall Stewart finish_express_del:
2379f8829a4aSRandall Stewart 	if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
2380f8829a4aSRandall Stewart 		/* we have a new high score */
2381f8829a4aSRandall Stewart 		asoc->highest_tsn_inside_map = tsn;
2382b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2383f8829a4aSRandall Stewart 			sctp_log_map(0, 2, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
238480fefe0aSRandall Stewart 		}
2385f8829a4aSRandall Stewart 	}
2386f8829a4aSRandall Stewart 	if (tsn == (asoc->cumulative_tsn + 1)) {
2387f8829a4aSRandall Stewart 		/* Update cum-ack */
2388f8829a4aSRandall Stewart 		asoc->cumulative_tsn = tsn;
2389f8829a4aSRandall Stewart 	}
2390f8829a4aSRandall Stewart 	if (last_chunk) {
2391f8829a4aSRandall Stewart 		*m = NULL;
2392f8829a4aSRandall Stewart 	}
2393f42a358aSRandall Stewart 	if (ordered) {
2394f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_inorderchunks);
2395f8829a4aSRandall Stewart 	} else {
2396f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_inunorderchunks);
2397f8829a4aSRandall Stewart 	}
2398f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvdata);
2399f8829a4aSRandall Stewart 	/* Set it present please */
2400b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
24016a91f103SRandall Stewart 		sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno, SCTP_STR_LOG_FROM_MARK_TSN);
240280fefe0aSRandall Stewart 	}
2403b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2404f8829a4aSRandall Stewart 		sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2405f8829a4aSRandall Stewart 		    asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE);
240680fefe0aSRandall Stewart 	}
2407207304d4SRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
2408f8829a4aSRandall Stewart 	SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
24098933fa13SRandall Stewart 
24108933fa13SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) &&
24118933fa13SRandall Stewart 	    asoc->peer_supports_nr_sack &&
24128933fa13SRandall Stewart 	    (SCTP_BASE_SYSCTL(sctp_do_drain) == 0)) {
24138933fa13SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
2414d50c1d79SRandall Stewart 		SCTP_REVERSE_OUT_TSN_PRES(nr_gap, tsn, asoc);
24158933fa13SRandall Stewart 		if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN)) {
24168933fa13SRandall Stewart 			asoc->highest_tsn_inside_nr_map = tsn;
24178933fa13SRandall Stewart 		}
24188933fa13SRandall Stewart 	}
241917205eccSRandall Stewart 	/* check the special flag for stream resets */
242017205eccSRandall Stewart 	if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2421d61a0ae0SRandall Stewart 	    ((compare_with_wrap(asoc->cumulative_tsn, liste->tsn, MAX_TSN)) ||
2422d61a0ae0SRandall Stewart 	    (asoc->cumulative_tsn == liste->tsn))
242317205eccSRandall Stewart 	    ) {
242417205eccSRandall Stewart 		/*
242517205eccSRandall Stewart 		 * we have finished working through the backlogged TSN's now
242617205eccSRandall Stewart 		 * time to reset streams. 1: call reset function. 2: free
242717205eccSRandall Stewart 		 * pending_reply space 3: distribute any chunks in
242817205eccSRandall Stewart 		 * pending_reply_queue.
242917205eccSRandall Stewart 		 */
243017205eccSRandall Stewart 		struct sctp_queued_to_read *ctl;
243117205eccSRandall Stewart 
243217205eccSRandall Stewart 		sctp_reset_in_stream(stcb, liste->number_entries, liste->req.list_of_streams);
243317205eccSRandall Stewart 		TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
2434207304d4SRandall Stewart 		SCTP_FREE(liste, SCTP_M_STRESET);
24353c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
243617205eccSRandall Stewart 		liste = TAILQ_FIRST(&asoc->resetHead);
243717205eccSRandall Stewart 		ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
243817205eccSRandall Stewart 		if (ctl && (liste == NULL)) {
243917205eccSRandall Stewart 			/* All can be removed */
244017205eccSRandall Stewart 			while (ctl) {
244117205eccSRandall Stewart 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
244217205eccSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
244317205eccSRandall Stewart 				if (*abort_flag) {
244417205eccSRandall Stewart 					return (0);
244517205eccSRandall Stewart 				}
244617205eccSRandall Stewart 				ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
244717205eccSRandall Stewart 			}
244817205eccSRandall Stewart 		} else if (ctl) {
244917205eccSRandall Stewart 			/* more than one in queue */
2450d61a0ae0SRandall Stewart 			while (!compare_with_wrap(ctl->sinfo_tsn, liste->tsn, MAX_TSN)) {
245117205eccSRandall Stewart 				/*
245217205eccSRandall Stewart 				 * if ctl->sinfo_tsn is <= liste->tsn we can
245317205eccSRandall Stewart 				 * process it which is the NOT of
245417205eccSRandall Stewart 				 * ctl->sinfo_tsn > liste->tsn
245517205eccSRandall Stewart 				 */
245617205eccSRandall Stewart 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
245717205eccSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
245817205eccSRandall Stewart 				if (*abort_flag) {
245917205eccSRandall Stewart 					return (0);
246017205eccSRandall Stewart 				}
246117205eccSRandall Stewart 				ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
246217205eccSRandall Stewart 			}
246317205eccSRandall Stewart 		}
246417205eccSRandall Stewart 		/*
246517205eccSRandall Stewart 		 * Now service re-assembly to pick up anything that has been
246617205eccSRandall Stewart 		 * held on reassembly queue?
246717205eccSRandall Stewart 		 */
246817205eccSRandall Stewart 		sctp_deliver_reasm_check(stcb, asoc);
246917205eccSRandall Stewart 		need_reasm_check = 0;
247017205eccSRandall Stewart 	}
2471139bc87fSRandall Stewart 	if (need_reasm_check) {
2472139bc87fSRandall Stewart 		/* Another one waits ? */
2473139bc87fSRandall Stewart 		sctp_deliver_reasm_check(stcb, asoc);
2474139bc87fSRandall Stewart 	}
2475f8829a4aSRandall Stewart 	return (1);
2476f8829a4aSRandall Stewart }
2477f8829a4aSRandall Stewart 
2478f8829a4aSRandall Stewart int8_t sctp_map_lookup_tab[256] = {
2479f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2480f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2481f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2482f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 4,
2483f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2484f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2485f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2486f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 5,
2487f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2488f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2489f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2490f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 4,
2491f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2492f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2493f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2494f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 6,
2495f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2496f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2497f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2498f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 4,
2499f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2500f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2501f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2502f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 5,
2503f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2504f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2505f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2506f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 4,
2507f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2508f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2509f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2510f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 7,
2511f8829a4aSRandall Stewart };
2512f8829a4aSRandall Stewart 
2513f8829a4aSRandall Stewart 
2514f8829a4aSRandall Stewart void
2515f8829a4aSRandall Stewart sctp_sack_check(struct sctp_tcb *stcb, int ok_to_sack, int was_a_gap, int *abort_flag)
2516f8829a4aSRandall Stewart {
2517f8829a4aSRandall Stewart 	/*
2518f8829a4aSRandall Stewart 	 * Now we also need to check the mapping array in a couple of ways.
2519f8829a4aSRandall Stewart 	 * 1) Did we move the cum-ack point?
2520f8829a4aSRandall Stewart 	 */
2521f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2522b3f1ea41SRandall Stewart 	int at;
2523c4739e2fSRandall Stewart 	int last_all_ones = 0;
2524f8829a4aSRandall Stewart 	int slide_from, slide_end, lgap, distance;
2525830d754dSRandall Stewart 
2526830d754dSRandall Stewart 	/* EY nr_mapping array variables */
25278933fa13SRandall Stewart 	/* int nr_at; */
25288933fa13SRandall Stewart 	/* int nr_last_all_ones = 0; */
25298933fa13SRandall Stewart 	/* int nr_slide_from, nr_slide_end, nr_lgap, nr_distance; */
2530830d754dSRandall Stewart 
2531f8829a4aSRandall Stewart 	uint32_t old_cumack, old_base, old_highest;
2532f8829a4aSRandall Stewart 	unsigned char aux_array[64];
2533f8829a4aSRandall Stewart 
2534830d754dSRandall Stewart 	/*
2535830d754dSRandall Stewart 	 * EY! Don't think this is required but I am immitating the code for
2536830d754dSRandall Stewart 	 * map just to make sure
2537830d754dSRandall Stewart 	 */
2538830d754dSRandall Stewart 	unsigned char nr_aux_array[64];
2539f8829a4aSRandall Stewart 
2540f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2541f8829a4aSRandall Stewart 	at = 0;
2542f8829a4aSRandall Stewart 
2543f8829a4aSRandall Stewart 	old_cumack = asoc->cumulative_tsn;
2544f8829a4aSRandall Stewart 	old_base = asoc->mapping_array_base_tsn;
2545f8829a4aSRandall Stewart 	old_highest = asoc->highest_tsn_inside_map;
2546f8829a4aSRandall Stewart 	if (asoc->mapping_array_size < 64)
2547f8829a4aSRandall Stewart 		memcpy(aux_array, asoc->mapping_array,
2548f8829a4aSRandall Stewart 		    asoc->mapping_array_size);
2549f8829a4aSRandall Stewart 	else
2550f8829a4aSRandall Stewart 		memcpy(aux_array, asoc->mapping_array, 64);
2551830d754dSRandall Stewart 	/* EY do the same for nr_mapping_array */
2552830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
2553f8829a4aSRandall Stewart 
2554830d754dSRandall Stewart 		if (asoc->nr_mapping_array_size != asoc->mapping_array_size) {
2555830d754dSRandall Stewart 			/*
2556830d754dSRandall Stewart 			 * printf("\nEY-IN sack_check method: \nEY-" "The
2557830d754dSRandall Stewart 			 * size of map and nr_map are inconsitent")
2558830d754dSRandall Stewart 			 */ ;
2559830d754dSRandall Stewart 		}
2560830d754dSRandall Stewart 		if (asoc->nr_mapping_array_base_tsn != asoc->mapping_array_base_tsn) {
2561830d754dSRandall Stewart 			/*
2562830d754dSRandall Stewart 			 * printf("\nEY-IN sack_check method VERY CRUCIAL
2563830d754dSRandall Stewart 			 * error: \nEY-" "The base tsns of map and nr_map
2564830d754dSRandall Stewart 			 * are inconsitent")
2565830d754dSRandall Stewart 			 */ ;
2566830d754dSRandall Stewart 		}
2567830d754dSRandall Stewart 		/* EY! just immitating the above code */
2568830d754dSRandall Stewart 		if (asoc->nr_mapping_array_size < 64)
2569830d754dSRandall Stewart 			memcpy(nr_aux_array, asoc->nr_mapping_array,
2570830d754dSRandall Stewart 			    asoc->nr_mapping_array_size);
2571830d754dSRandall Stewart 		else
2572830d754dSRandall Stewart 			memcpy(aux_array, asoc->nr_mapping_array, 64);
2573830d754dSRandall Stewart 	}
2574f8829a4aSRandall Stewart 	/*
2575f8829a4aSRandall Stewart 	 * We could probably improve this a small bit by calculating the
2576f8829a4aSRandall Stewart 	 * offset of the current cum-ack as the starting point.
2577f8829a4aSRandall Stewart 	 */
2578f8829a4aSRandall Stewart 	at = 0;
2579b3f1ea41SRandall Stewart 	for (slide_from = 0; slide_from < stcb->asoc.mapping_array_size; slide_from++) {
2580d06c82f1SRandall Stewart 
2581b3f1ea41SRandall Stewart 		if (asoc->mapping_array[slide_from] == 0xff) {
2582f8829a4aSRandall Stewart 			at += 8;
258393164cf9SRandall Stewart 			last_all_ones = 1;
2584f8829a4aSRandall Stewart 		} else {
2585f8829a4aSRandall Stewart 			/* there is a 0 bit */
2586b3f1ea41SRandall Stewart 			at += sctp_map_lookup_tab[asoc->mapping_array[slide_from]];
258793164cf9SRandall Stewart 			last_all_ones = 0;
2588f8829a4aSRandall Stewart 			break;
2589f8829a4aSRandall Stewart 		}
2590f8829a4aSRandall Stewart 	}
259193164cf9SRandall Stewart 	asoc->cumulative_tsn = asoc->mapping_array_base_tsn + (at - last_all_ones);
2592f8829a4aSRandall Stewart 	/* at is one off, since in the table a embedded -1 is present */
2593f8829a4aSRandall Stewart 	at++;
2594f8829a4aSRandall Stewart 
2595f8829a4aSRandall Stewart 	if (compare_with_wrap(asoc->cumulative_tsn,
2596f8829a4aSRandall Stewart 	    asoc->highest_tsn_inside_map,
2597f8829a4aSRandall Stewart 	    MAX_TSN)) {
2598a5d547adSRandall Stewart #ifdef INVARIANTS
2599ceaad40aSRandall Stewart 		panic("huh, cumack 0x%x greater than high-tsn 0x%x in map",
2600ceaad40aSRandall Stewart 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2601f8829a4aSRandall Stewart #else
2602ceaad40aSRandall Stewart 		SCTP_PRINTF("huh, cumack 0x%x greater than high-tsn 0x%x in map - should panic?\n",
2603ceaad40aSRandall Stewart 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2604b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2605b3f1ea41SRandall Stewart 			sctp_log_map(0, 6, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
2606b3f1ea41SRandall Stewart 		}
2607f8829a4aSRandall Stewart 		asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2608830d754dSRandall Stewart 		asoc->highest_tsn_inside_nr_map = asoc->cumulative_tsn;
2609f8829a4aSRandall Stewart #endif
2610f8829a4aSRandall Stewart 	}
2611c4739e2fSRandall Stewart 	if ((asoc->cumulative_tsn == asoc->highest_tsn_inside_map) && (at >= 8)) {
2612f8829a4aSRandall Stewart 		/* The complete array was completed by a single FR */
2613f8829a4aSRandall Stewart 		/* higest becomes the cum-ack */
2614f8829a4aSRandall Stewart 		int clr;
2615f8829a4aSRandall Stewart 
2616f8829a4aSRandall Stewart 		asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
2617f8829a4aSRandall Stewart 		/* clear the array */
2618f8829a4aSRandall Stewart 		clr = (at >> 3) + 1;
2619c4739e2fSRandall Stewart 		if (clr > asoc->mapping_array_size) {
2620f8829a4aSRandall Stewart 			clr = asoc->mapping_array_size;
2621f8829a4aSRandall Stewart 		}
2622f8829a4aSRandall Stewart 		memset(asoc->mapping_array, 0, clr);
2623f8829a4aSRandall Stewart 		/* base becomes one ahead of the cum-ack */
2624f8829a4aSRandall Stewart 		asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
2625830d754dSRandall Stewart 
2626830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
2627830d754dSRandall Stewart 
2628830d754dSRandall Stewart 			if (clr > asoc->nr_mapping_array_size)
2629830d754dSRandall Stewart 				clr = asoc->nr_mapping_array_size;
2630830d754dSRandall Stewart 
2631830d754dSRandall Stewart 			memset(asoc->nr_mapping_array, 0, clr);
2632830d754dSRandall Stewart 			/* base becomes one ahead of the cum-ack */
2633830d754dSRandall Stewart 			asoc->nr_mapping_array_base_tsn = asoc->cumulative_tsn + 1;
2634830d754dSRandall Stewart 			asoc->highest_tsn_inside_nr_map = asoc->cumulative_tsn;
2635830d754dSRandall Stewart 		}
2636b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2637f8829a4aSRandall Stewart 			sctp_log_map(old_base, old_cumack, old_highest,
2638f8829a4aSRandall Stewart 			    SCTP_MAP_PREPARE_SLIDE);
2639f8829a4aSRandall Stewart 			sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2640f8829a4aSRandall Stewart 			    asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_CLEARED);
264180fefe0aSRandall Stewart 		}
2642f8829a4aSRandall Stewart 	} else if (at >= 8) {
2643f8829a4aSRandall Stewart 		/* we can slide the mapping array down */
2644b3f1ea41SRandall Stewart 		/* slide_from holds where we hit the first NON 0xff byte */
2645b3f1ea41SRandall Stewart 
2646f8829a4aSRandall Stewart 		/*
2647f8829a4aSRandall Stewart 		 * now calculate the ceiling of the move using our highest
2648f8829a4aSRandall Stewart 		 * TSN value
2649f8829a4aSRandall Stewart 		 */
2650f8829a4aSRandall Stewart 		if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
2651f8829a4aSRandall Stewart 			lgap = asoc->highest_tsn_inside_map -
2652f8829a4aSRandall Stewart 			    asoc->mapping_array_base_tsn;
2653f8829a4aSRandall Stewart 		} else {
2654f8829a4aSRandall Stewart 			lgap = (MAX_TSN - asoc->mapping_array_base_tsn) +
2655f8829a4aSRandall Stewart 			    asoc->highest_tsn_inside_map + 1;
2656f8829a4aSRandall Stewart 		}
2657f8829a4aSRandall Stewart 		slide_end = lgap >> 3;
2658f8829a4aSRandall Stewart 		if (slide_end < slide_from) {
2659d55b0b1bSRandall Stewart #ifdef INVARIANTS
2660f8829a4aSRandall Stewart 			panic("impossible slide");
2661d55b0b1bSRandall Stewart #else
2662d55b0b1bSRandall Stewart 			printf("impossible slide?\n");
2663d55b0b1bSRandall Stewart 			return;
2664d55b0b1bSRandall Stewart #endif
2665f8829a4aSRandall Stewart 		}
2666b3f1ea41SRandall Stewart 		if (slide_end > asoc->mapping_array_size) {
2667b3f1ea41SRandall Stewart #ifdef INVARIANTS
2668b3f1ea41SRandall Stewart 			panic("would overrun buffer");
2669b3f1ea41SRandall Stewart #else
2670b3f1ea41SRandall Stewart 			printf("Gak, would have overrun map end:%d slide_end:%d\n",
2671b3f1ea41SRandall Stewart 			    asoc->mapping_array_size, slide_end);
2672b3f1ea41SRandall Stewart 			slide_end = asoc->mapping_array_size;
2673b3f1ea41SRandall Stewart #endif
2674b3f1ea41SRandall Stewart 		}
2675f8829a4aSRandall Stewart 		distance = (slide_end - slide_from) + 1;
2676b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2677f8829a4aSRandall Stewart 			sctp_log_map(old_base, old_cumack, old_highest,
2678f8829a4aSRandall Stewart 			    SCTP_MAP_PREPARE_SLIDE);
2679f8829a4aSRandall Stewart 			sctp_log_map((uint32_t) slide_from, (uint32_t) slide_end,
2680f8829a4aSRandall Stewart 			    (uint32_t) lgap, SCTP_MAP_SLIDE_FROM);
268180fefe0aSRandall Stewart 		}
2682f8829a4aSRandall Stewart 		if (distance + slide_from > asoc->mapping_array_size ||
2683f8829a4aSRandall Stewart 		    distance < 0) {
2684f8829a4aSRandall Stewart 			/*
2685f8829a4aSRandall Stewart 			 * Here we do NOT slide forward the array so that
2686f8829a4aSRandall Stewart 			 * hopefully when more data comes in to fill it up
2687f8829a4aSRandall Stewart 			 * we will be able to slide it forward. Really I
2688f8829a4aSRandall Stewart 			 * don't think this should happen :-0
2689f8829a4aSRandall Stewart 			 */
2690f8829a4aSRandall Stewart 
2691b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2692f8829a4aSRandall Stewart 				sctp_log_map((uint32_t) distance, (uint32_t) slide_from,
2693f8829a4aSRandall Stewart 				    (uint32_t) asoc->mapping_array_size,
2694f8829a4aSRandall Stewart 				    SCTP_MAP_SLIDE_NONE);
269580fefe0aSRandall Stewart 			}
2696f8829a4aSRandall Stewart 		} else {
2697f8829a4aSRandall Stewart 			int ii;
2698f8829a4aSRandall Stewart 
2699f8829a4aSRandall Stewart 			for (ii = 0; ii < distance; ii++) {
2700f8829a4aSRandall Stewart 				asoc->mapping_array[ii] =
2701f8829a4aSRandall Stewart 				    asoc->mapping_array[slide_from + ii];
2702f8829a4aSRandall Stewart 			}
2703f8829a4aSRandall Stewart 			for (ii = distance; ii <= slide_end; ii++) {
2704f8829a4aSRandall Stewart 				asoc->mapping_array[ii] = 0;
2705f8829a4aSRandall Stewart 			}
2706f8829a4aSRandall Stewart 			asoc->mapping_array_base_tsn += (slide_from << 3);
2707b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2708f8829a4aSRandall Stewart 				sctp_log_map(asoc->mapping_array_base_tsn,
2709f8829a4aSRandall Stewart 				    asoc->cumulative_tsn, asoc->highest_tsn_inside_map,
2710f8829a4aSRandall Stewart 				    SCTP_MAP_SLIDE_RESULT);
271180fefe0aSRandall Stewart 			}
2712f8829a4aSRandall Stewart 			/*
27138933fa13SRandall Stewart 			 * EY if doing nr_sacks then slide the
27148933fa13SRandall Stewart 			 * nr_mapping_array accordingly please
2715830d754dSRandall Stewart 			 */
2716830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
27178933fa13SRandall Stewart 				for (ii = 0; ii < distance; ii++) {
2718830d754dSRandall Stewart 					asoc->nr_mapping_array[ii] =
27198933fa13SRandall Stewart 					    asoc->nr_mapping_array[slide_from + ii];
2720830d754dSRandall Stewart 				}
27218933fa13SRandall Stewart 				for (ii = distance; ii <= slide_end; ii++) {
2722830d754dSRandall Stewart 					asoc->nr_mapping_array[ii] = 0;
2723830d754dSRandall Stewart 				}
27248933fa13SRandall Stewart 				asoc->nr_mapping_array_base_tsn += (slide_from << 3);
2725830d754dSRandall Stewart 			}
2726830d754dSRandall Stewart 		}
2727830d754dSRandall Stewart 	}
2728830d754dSRandall Stewart 	/*
2729f8829a4aSRandall Stewart 	 * Now we need to see if we need to queue a sack or just start the
2730f8829a4aSRandall Stewart 	 * timer (if allowed).
2731f8829a4aSRandall Stewart 	 */
2732f8829a4aSRandall Stewart 	if (ok_to_sack) {
2733f8829a4aSRandall Stewart 		if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2734f8829a4aSRandall Stewart 			/*
2735f8829a4aSRandall Stewart 			 * Ok special case, in SHUTDOWN-SENT case. here we
2736f8829a4aSRandall Stewart 			 * maker sure SACK timer is off and instead send a
2737f8829a4aSRandall Stewart 			 * SHUTDOWN and a SACK
2738f8829a4aSRandall Stewart 			 */
2739139bc87fSRandall Stewart 			if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2740f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
2741a5d547adSRandall Stewart 				    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_INDATA + SCTP_LOC_18);
2742f8829a4aSRandall Stewart 			}
2743f8829a4aSRandall Stewart 			sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
2744830d754dSRandall Stewart 			/*
2745830d754dSRandall Stewart 			 * EY if nr_sacks used then send an nr-sack , a sack
2746830d754dSRandall Stewart 			 * otherwise
2747830d754dSRandall Stewart 			 */
27488933fa13SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack)
2749830d754dSRandall Stewart 				sctp_send_nr_sack(stcb);
2750830d754dSRandall Stewart 			else
2751f8829a4aSRandall Stewart 				sctp_send_sack(stcb);
2752f8829a4aSRandall Stewart 		} else {
2753f8829a4aSRandall Stewart 			int is_a_gap;
2754f8829a4aSRandall Stewart 
2755f8829a4aSRandall Stewart 			/* is there a gap now ? */
2756f8829a4aSRandall Stewart 			is_a_gap = compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
2757f8829a4aSRandall Stewart 			    stcb->asoc.cumulative_tsn, MAX_TSN);
2758f8829a4aSRandall Stewart 
2759f8829a4aSRandall Stewart 			/*
2760f8829a4aSRandall Stewart 			 * CMT DAC algorithm: increase number of packets
2761f8829a4aSRandall Stewart 			 * received since last ack
2762f8829a4aSRandall Stewart 			 */
2763f8829a4aSRandall Stewart 			stcb->asoc.cmt_dac_pkts_rcvd++;
2764f8829a4aSRandall Stewart 
276542551e99SRandall Stewart 			if ((stcb->asoc.send_sack == 1) ||	/* We need to send a
276642551e99SRandall Stewart 								 * SACK */
2767f8829a4aSRandall Stewart 			    ((was_a_gap) && (is_a_gap == 0)) ||	/* was a gap, but no
2768f8829a4aSRandall Stewart 								 * longer is one */
2769f8829a4aSRandall Stewart 			    (stcb->asoc.numduptsns) ||	/* we have dup's */
2770f8829a4aSRandall Stewart 			    (is_a_gap) ||	/* is still a gap */
277142551e99SRandall Stewart 			    (stcb->asoc.delayed_ack == 0) ||	/* Delayed sack disabled */
277242551e99SRandall Stewart 			    (stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq)	/* hit limit of pkts */
2773f8829a4aSRandall Stewart 			    ) {
2774f8829a4aSRandall Stewart 
2775b3f1ea41SRandall Stewart 				if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off)) &&
2776b3f1ea41SRandall Stewart 				    (SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) &&
277742551e99SRandall Stewart 				    (stcb->asoc.send_sack == 0) &&
2778f8829a4aSRandall Stewart 				    (stcb->asoc.numduptsns == 0) &&
2779f8829a4aSRandall Stewart 				    (stcb->asoc.delayed_ack) &&
2780139bc87fSRandall Stewart 				    (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer))) {
2781f8829a4aSRandall Stewart 
2782f8829a4aSRandall Stewart 					/*
2783f8829a4aSRandall Stewart 					 * CMT DAC algorithm: With CMT,
2784f8829a4aSRandall Stewart 					 * delay acks even in the face of
2785f8829a4aSRandall Stewart 					 *
2786f8829a4aSRandall Stewart 					 * reordering. Therefore, if acks that
2787f8829a4aSRandall Stewart 					 * do not have to be sent because of
2788f8829a4aSRandall Stewart 					 * the above reasons, will be
2789f8829a4aSRandall Stewart 					 * delayed. That is, acks that would
2790f8829a4aSRandall Stewart 					 * have been sent due to gap reports
2791f8829a4aSRandall Stewart 					 * will be delayed with DAC. Start
2792f8829a4aSRandall Stewart 					 * the delayed ack timer.
2793f8829a4aSRandall Stewart 					 */
2794f8829a4aSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2795f8829a4aSRandall Stewart 					    stcb->sctp_ep, stcb, NULL);
2796f8829a4aSRandall Stewart 				} else {
2797f8829a4aSRandall Stewart 					/*
2798f8829a4aSRandall Stewart 					 * Ok we must build a SACK since the
2799f8829a4aSRandall Stewart 					 * timer is pending, we got our
2800f8829a4aSRandall Stewart 					 * first packet OR there are gaps or
2801f8829a4aSRandall Stewart 					 * duplicates.
2802f8829a4aSRandall Stewart 					 */
2803ad81507eSRandall Stewart 					(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2804830d754dSRandall Stewart 					/*
2805830d754dSRandall Stewart 					 * EY if nr_sacks used then send an
2806830d754dSRandall Stewart 					 * nr-sack , a sack otherwise
2807830d754dSRandall Stewart 					 */
2808830d754dSRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack)
2809830d754dSRandall Stewart 						sctp_send_nr_sack(stcb);
2810830d754dSRandall Stewart 					else
2811f8829a4aSRandall Stewart 						sctp_send_sack(stcb);
2812f8829a4aSRandall Stewart 				}
2813f8829a4aSRandall Stewart 			} else {
281442551e99SRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2815f8829a4aSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2816f8829a4aSRandall Stewart 					    stcb->sctp_ep, stcb, NULL);
2817f8829a4aSRandall Stewart 				}
2818f8829a4aSRandall Stewart 			}
2819f8829a4aSRandall Stewart 		}
2820f8829a4aSRandall Stewart 	}
282142551e99SRandall Stewart }
2822f8829a4aSRandall Stewart 
2823f8829a4aSRandall Stewart void
2824f8829a4aSRandall Stewart sctp_service_queues(struct sctp_tcb *stcb, struct sctp_association *asoc)
2825f8829a4aSRandall Stewart {
2826f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
2827f8829a4aSRandall Stewart 	uint32_t tsize;
2828f8829a4aSRandall Stewart 	uint16_t nxt_todel;
2829f8829a4aSRandall Stewart 
2830f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
2831f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
2832f8829a4aSRandall Stewart 	}
2833f8829a4aSRandall Stewart 	/* Can we proceed further, i.e. the PD-API is complete */
2834f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
2835f8829a4aSRandall Stewart 		/* no */
2836f8829a4aSRandall Stewart 		return;
2837f8829a4aSRandall Stewart 	}
2838f8829a4aSRandall Stewart 	/*
2839f8829a4aSRandall Stewart 	 * Now is there some other chunk I can deliver from the reassembly
2840f8829a4aSRandall Stewart 	 * queue.
2841f8829a4aSRandall Stewart 	 */
2842139bc87fSRandall Stewart doit_again:
2843f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
2844f8829a4aSRandall Stewart 	if (chk == NULL) {
2845f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = 0;
2846f8829a4aSRandall Stewart 		asoc->cnt_on_reasm_queue = 0;
2847f8829a4aSRandall Stewart 		return;
2848f8829a4aSRandall Stewart 	}
2849f8829a4aSRandall Stewart 	nxt_todel = asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
2850f8829a4aSRandall Stewart 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
2851f8829a4aSRandall Stewart 	    ((nxt_todel == chk->rec.data.stream_seq) ||
2852f8829a4aSRandall Stewart 	    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
2853f8829a4aSRandall Stewart 		/*
2854f8829a4aSRandall Stewart 		 * Yep the first one is here. We setup to start reception,
2855f8829a4aSRandall Stewart 		 * by backing down the TSN just in case we can't deliver.
2856f8829a4aSRandall Stewart 		 */
2857f8829a4aSRandall Stewart 
2858f8829a4aSRandall Stewart 		/*
2859f8829a4aSRandall Stewart 		 * Before we start though either all of the message should
2860f8829a4aSRandall Stewart 		 * be here or 1/4 the socket buffer max or nothing on the
2861f8829a4aSRandall Stewart 		 * delivery queue and something can be delivered.
2862f8829a4aSRandall Stewart 		 */
2863f8829a4aSRandall Stewart 		if ((sctp_is_all_msg_on_reasm(asoc, &tsize) ||
2864c4739e2fSRandall Stewart 		    (tsize >= stcb->sctp_ep->partial_delivery_point))) {
2865f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 1;
2866f8829a4aSRandall Stewart 			asoc->tsn_last_delivered = chk->rec.data.TSN_seq - 1;
2867f8829a4aSRandall Stewart 			asoc->str_of_pdapi = chk->rec.data.stream_number;
2868f8829a4aSRandall Stewart 			asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
2869f8829a4aSRandall Stewart 			asoc->pdapi_ppid = chk->rec.data.payloadtype;
2870f8829a4aSRandall Stewart 			asoc->fragment_flags = chk->rec.data.rcv_flags;
2871f8829a4aSRandall Stewart 			sctp_service_reassembly(stcb, asoc);
2872139bc87fSRandall Stewart 			if (asoc->fragmented_delivery_inprogress == 0) {
2873139bc87fSRandall Stewart 				goto doit_again;
2874139bc87fSRandall Stewart 			}
2875f8829a4aSRandall Stewart 		}
2876f8829a4aSRandall Stewart 	}
2877f8829a4aSRandall Stewart }
2878f8829a4aSRandall Stewart 
2879f8829a4aSRandall Stewart int
2880f8829a4aSRandall Stewart sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length,
2881f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2882f8829a4aSRandall Stewart     struct sctp_nets *net, uint32_t * high_tsn)
2883f8829a4aSRandall Stewart {
2884f8829a4aSRandall Stewart 	struct sctp_data_chunk *ch, chunk_buf;
2885f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2886f8829a4aSRandall Stewart 	int num_chunks = 0;	/* number of control chunks processed */
2887f8829a4aSRandall Stewart 	int stop_proc = 0;
2888f8829a4aSRandall Stewart 	int chk_length, break_flag, last_chunk;
2889f8829a4aSRandall Stewart 	int abort_flag = 0, was_a_gap = 0;
2890f8829a4aSRandall Stewart 	struct mbuf *m;
2891f8829a4aSRandall Stewart 
2892f8829a4aSRandall Stewart 	/* set the rwnd */
2893f8829a4aSRandall Stewart 	sctp_set_rwnd(stcb, &stcb->asoc);
2894f8829a4aSRandall Stewart 
2895f8829a4aSRandall Stewart 	m = *mm;
2896f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
2897f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2898f8829a4aSRandall Stewart 	if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
2899f8829a4aSRandall Stewart 	    stcb->asoc.cumulative_tsn, MAX_TSN)) {
2900f8829a4aSRandall Stewart 		/* there was a gap before this data was processed */
2901f8829a4aSRandall Stewart 		was_a_gap = 1;
2902f8829a4aSRandall Stewart 	}
2903f8829a4aSRandall Stewart 	/*
2904f8829a4aSRandall Stewart 	 * setup where we got the last DATA packet from for any SACK that
2905f8829a4aSRandall Stewart 	 * may need to go out. Don't bump the net. This is done ONLY when a
2906f8829a4aSRandall Stewart 	 * chunk is assigned.
2907f8829a4aSRandall Stewart 	 */
2908f8829a4aSRandall Stewart 	asoc->last_data_chunk_from = net;
2909f8829a4aSRandall Stewart 
2910d06c82f1SRandall Stewart 	/*-
2911f8829a4aSRandall Stewart 	 * Now before we proceed we must figure out if this is a wasted
2912f8829a4aSRandall Stewart 	 * cluster... i.e. it is a small packet sent in and yet the driver
2913f8829a4aSRandall Stewart 	 * underneath allocated a full cluster for it. If so we must copy it
2914f8829a4aSRandall Stewart 	 * to a smaller mbuf and free up the cluster mbuf. This will help
2915d06c82f1SRandall Stewart 	 * with cluster starvation. Note for __Panda__ we don't do this
2916d06c82f1SRandall Stewart 	 * since it has clusters all the way down to 64 bytes.
2917f8829a4aSRandall Stewart 	 */
291844b7479bSRandall Stewart 	if (SCTP_BUF_LEN(m) < (long)MLEN && SCTP_BUF_NEXT(m) == NULL) {
2919f8829a4aSRandall Stewart 		/* we only handle mbufs that are singletons.. not chains */
2920139bc87fSRandall Stewart 		m = sctp_get_mbuf_for_msg(SCTP_BUF_LEN(m), 0, M_DONTWAIT, 1, MT_DATA);
2921f8829a4aSRandall Stewart 		if (m) {
2922f8829a4aSRandall Stewart 			/* ok lets see if we can copy the data up */
2923f8829a4aSRandall Stewart 			caddr_t *from, *to;
2924f8829a4aSRandall Stewart 
2925f8829a4aSRandall Stewart 			/* get the pointers and copy */
2926f8829a4aSRandall Stewart 			to = mtod(m, caddr_t *);
2927f8829a4aSRandall Stewart 			from = mtod((*mm), caddr_t *);
2928139bc87fSRandall Stewart 			memcpy(to, from, SCTP_BUF_LEN((*mm)));
2929f8829a4aSRandall Stewart 			/* copy the length and free up the old */
2930139bc87fSRandall Stewart 			SCTP_BUF_LEN(m) = SCTP_BUF_LEN((*mm));
2931f8829a4aSRandall Stewart 			sctp_m_freem(*mm);
2932f8829a4aSRandall Stewart 			/* sucess, back copy */
2933f8829a4aSRandall Stewart 			*mm = m;
2934f8829a4aSRandall Stewart 		} else {
2935f8829a4aSRandall Stewart 			/* We are in trouble in the mbuf world .. yikes */
2936f8829a4aSRandall Stewart 			m = *mm;
2937f8829a4aSRandall Stewart 		}
2938f8829a4aSRandall Stewart 	}
2939f8829a4aSRandall Stewart 	/* get pointer to the first chunk header */
2940f8829a4aSRandall Stewart 	ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2941f8829a4aSRandall Stewart 	    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2942f8829a4aSRandall Stewart 	if (ch == NULL) {
2943f8829a4aSRandall Stewart 		return (1);
2944f8829a4aSRandall Stewart 	}
2945f8829a4aSRandall Stewart 	/*
2946f8829a4aSRandall Stewart 	 * process all DATA chunks...
2947f8829a4aSRandall Stewart 	 */
2948f8829a4aSRandall Stewart 	*high_tsn = asoc->cumulative_tsn;
2949f8829a4aSRandall Stewart 	break_flag = 0;
295042551e99SRandall Stewart 	asoc->data_pkts_seen++;
2951f8829a4aSRandall Stewart 	while (stop_proc == 0) {
2952f8829a4aSRandall Stewart 		/* validate chunk length */
2953f8829a4aSRandall Stewart 		chk_length = ntohs(ch->ch.chunk_length);
2954f8829a4aSRandall Stewart 		if (length - *offset < chk_length) {
2955f8829a4aSRandall Stewart 			/* all done, mutulated chunk */
2956f8829a4aSRandall Stewart 			stop_proc = 1;
2957f8829a4aSRandall Stewart 			break;
2958f8829a4aSRandall Stewart 		}
2959f8829a4aSRandall Stewart 		if (ch->ch.chunk_type == SCTP_DATA) {
2960f8829a4aSRandall Stewart 			if ((size_t)chk_length < sizeof(struct sctp_data_chunk) + 1) {
2961f8829a4aSRandall Stewart 				/*
2962f8829a4aSRandall Stewart 				 * Need to send an abort since we had a
2963f8829a4aSRandall Stewart 				 * invalid data chunk.
2964f8829a4aSRandall Stewart 				 */
2965f8829a4aSRandall Stewart 				struct mbuf *op_err;
2966f8829a4aSRandall Stewart 
2967139bc87fSRandall Stewart 				op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 2 * sizeof(uint32_t)),
2968f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
2969f8829a4aSRandall Stewart 
2970f8829a4aSRandall Stewart 				if (op_err) {
2971f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
2972f8829a4aSRandall Stewart 					uint32_t *ippp;
2973f8829a4aSRandall Stewart 
2974139bc87fSRandall Stewart 					SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr) +
2975f8829a4aSRandall Stewart 					    (2 * sizeof(uint32_t));
2976f8829a4aSRandall Stewart 					ph = mtod(op_err, struct sctp_paramhdr *);
2977f8829a4aSRandall Stewart 					ph->param_type =
2978f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2979139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(op_err));
2980f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
2981a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_19);
2982f8829a4aSRandall Stewart 					ippp++;
2983f8829a4aSRandall Stewart 					*ippp = asoc->cumulative_tsn;
2984f8829a4aSRandall Stewart 
2985f8829a4aSRandall Stewart 				}
2986a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_19;
2987f8829a4aSRandall Stewart 				sctp_abort_association(inp, stcb, m, iphlen, sh,
2988c54a18d2SRandall Stewart 				    op_err, 0, net->port);
2989f8829a4aSRandall Stewart 				return (2);
2990f8829a4aSRandall Stewart 			}
2991f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
2992f8829a4aSRandall Stewart 			sctp_audit_log(0xB1, 0);
2993f8829a4aSRandall Stewart #endif
2994f8829a4aSRandall Stewart 			if (SCTP_SIZE32(chk_length) == (length - *offset)) {
2995f8829a4aSRandall Stewart 				last_chunk = 1;
2996f8829a4aSRandall Stewart 			} else {
2997f8829a4aSRandall Stewart 				last_chunk = 0;
2998f8829a4aSRandall Stewart 			}
2999f8829a4aSRandall Stewart 			if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset, ch,
3000f8829a4aSRandall Stewart 			    chk_length, net, high_tsn, &abort_flag, &break_flag,
3001f8829a4aSRandall Stewart 			    last_chunk)) {
3002f8829a4aSRandall Stewart 				num_chunks++;
3003f8829a4aSRandall Stewart 			}
3004f8829a4aSRandall Stewart 			if (abort_flag)
3005f8829a4aSRandall Stewart 				return (2);
3006f8829a4aSRandall Stewart 
3007f8829a4aSRandall Stewart 			if (break_flag) {
3008f8829a4aSRandall Stewart 				/*
3009f8829a4aSRandall Stewart 				 * Set because of out of rwnd space and no
3010f8829a4aSRandall Stewart 				 * drop rep space left.
3011f8829a4aSRandall Stewart 				 */
3012f8829a4aSRandall Stewart 				stop_proc = 1;
3013f8829a4aSRandall Stewart 				break;
3014f8829a4aSRandall Stewart 			}
3015f8829a4aSRandall Stewart 		} else {
3016f8829a4aSRandall Stewart 			/* not a data chunk in the data region */
3017f8829a4aSRandall Stewart 			switch (ch->ch.chunk_type) {
3018f8829a4aSRandall Stewart 			case SCTP_INITIATION:
3019f8829a4aSRandall Stewart 			case SCTP_INITIATION_ACK:
3020f8829a4aSRandall Stewart 			case SCTP_SELECTIVE_ACK:
3021830d754dSRandall Stewart 			case SCTP_NR_SELECTIVE_ACK:	/* EY */
3022f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_REQUEST:
3023f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_ACK:
3024f8829a4aSRandall Stewart 			case SCTP_ABORT_ASSOCIATION:
3025f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN:
3026f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_ACK:
3027f8829a4aSRandall Stewart 			case SCTP_OPERATION_ERROR:
3028f8829a4aSRandall Stewart 			case SCTP_COOKIE_ECHO:
3029f8829a4aSRandall Stewart 			case SCTP_COOKIE_ACK:
3030f8829a4aSRandall Stewart 			case SCTP_ECN_ECHO:
3031f8829a4aSRandall Stewart 			case SCTP_ECN_CWR:
3032f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_COMPLETE:
3033f8829a4aSRandall Stewart 			case SCTP_AUTHENTICATION:
3034f8829a4aSRandall Stewart 			case SCTP_ASCONF_ACK:
3035f8829a4aSRandall Stewart 			case SCTP_PACKET_DROPPED:
3036f8829a4aSRandall Stewart 			case SCTP_STREAM_RESET:
3037f8829a4aSRandall Stewart 			case SCTP_FORWARD_CUM_TSN:
3038f8829a4aSRandall Stewart 			case SCTP_ASCONF:
3039f8829a4aSRandall Stewart 				/*
3040f8829a4aSRandall Stewart 				 * Now, what do we do with KNOWN chunks that
3041f8829a4aSRandall Stewart 				 * are NOT in the right place?
3042f8829a4aSRandall Stewart 				 *
3043f8829a4aSRandall Stewart 				 * For now, I do nothing but ignore them. We
3044f8829a4aSRandall Stewart 				 * may later want to add sysctl stuff to
3045f8829a4aSRandall Stewart 				 * switch out and do either an ABORT() or
3046f8829a4aSRandall Stewart 				 * possibly process them.
3047f8829a4aSRandall Stewart 				 */
3048b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_strict_data_order)) {
3049f8829a4aSRandall Stewart 					struct mbuf *op_err;
3050f8829a4aSRandall Stewart 
3051f8829a4aSRandall Stewart 					op_err = sctp_generate_invmanparam(SCTP_CAUSE_PROTOCOL_VIOLATION);
3052c54a18d2SRandall Stewart 					sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 0, net->port);
3053f8829a4aSRandall Stewart 					return (2);
3054f8829a4aSRandall Stewart 				}
3055f8829a4aSRandall Stewart 				break;
3056f8829a4aSRandall Stewart 			default:
3057f8829a4aSRandall Stewart 				/* unknown chunk type, use bit rules */
3058f8829a4aSRandall Stewart 				if (ch->ch.chunk_type & 0x40) {
3059f8829a4aSRandall Stewart 					/* Add a error report to the queue */
3060d61a0ae0SRandall Stewart 					struct mbuf *merr;
3061f8829a4aSRandall Stewart 					struct sctp_paramhdr *phd;
3062f8829a4aSRandall Stewart 
3063d61a0ae0SRandall Stewart 					merr = sctp_get_mbuf_for_msg(sizeof(*phd), 0, M_DONTWAIT, 1, MT_DATA);
3064d61a0ae0SRandall Stewart 					if (merr) {
3065d61a0ae0SRandall Stewart 						phd = mtod(merr, struct sctp_paramhdr *);
3066f8829a4aSRandall Stewart 						/*
3067f8829a4aSRandall Stewart 						 * We cheat and use param
3068f8829a4aSRandall Stewart 						 * type since we did not
3069f8829a4aSRandall Stewart 						 * bother to define a error
3070f8829a4aSRandall Stewart 						 * cause struct. They are
3071f8829a4aSRandall Stewart 						 * the same basic format
3072f8829a4aSRandall Stewart 						 * with different names.
3073f8829a4aSRandall Stewart 						 */
3074f8829a4aSRandall Stewart 						phd->param_type =
3075f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_UNRECOG_CHUNK);
3076f8829a4aSRandall Stewart 						phd->param_length =
3077f8829a4aSRandall Stewart 						    htons(chk_length + sizeof(*phd));
3078d61a0ae0SRandall Stewart 						SCTP_BUF_LEN(merr) = sizeof(*phd);
3079d61a0ae0SRandall Stewart 						SCTP_BUF_NEXT(merr) = SCTP_M_COPYM(m, *offset,
3080f8829a4aSRandall Stewart 						    SCTP_SIZE32(chk_length),
3081f8829a4aSRandall Stewart 						    M_DONTWAIT);
3082d61a0ae0SRandall Stewart 						if (SCTP_BUF_NEXT(merr)) {
3083d61a0ae0SRandall Stewart 							sctp_queue_op_err(stcb, merr);
3084f8829a4aSRandall Stewart 						} else {
3085d61a0ae0SRandall Stewart 							sctp_m_freem(merr);
3086f8829a4aSRandall Stewart 						}
3087f8829a4aSRandall Stewart 					}
3088f8829a4aSRandall Stewart 				}
3089f8829a4aSRandall Stewart 				if ((ch->ch.chunk_type & 0x80) == 0) {
3090f8829a4aSRandall Stewart 					/* discard the rest of this packet */
3091f8829a4aSRandall Stewart 					stop_proc = 1;
3092f8829a4aSRandall Stewart 				}	/* else skip this bad chunk and
3093f8829a4aSRandall Stewart 					 * continue... */
3094f8829a4aSRandall Stewart 				break;
3095f8829a4aSRandall Stewart 			};	/* switch of chunk type */
3096f8829a4aSRandall Stewart 		}
3097f8829a4aSRandall Stewart 		*offset += SCTP_SIZE32(chk_length);
3098f8829a4aSRandall Stewart 		if ((*offset >= length) || stop_proc) {
3099f8829a4aSRandall Stewart 			/* no more data left in the mbuf chain */
3100f8829a4aSRandall Stewart 			stop_proc = 1;
3101f8829a4aSRandall Stewart 			continue;
3102f8829a4aSRandall Stewart 		}
3103f8829a4aSRandall Stewart 		ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
3104f8829a4aSRandall Stewart 		    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
3105f8829a4aSRandall Stewart 		if (ch == NULL) {
3106f8829a4aSRandall Stewart 			*offset = length;
3107f8829a4aSRandall Stewart 			stop_proc = 1;
3108f8829a4aSRandall Stewart 			break;
3109f8829a4aSRandall Stewart 
3110f8829a4aSRandall Stewart 		}
3111f8829a4aSRandall Stewart 	}			/* while */
3112f8829a4aSRandall Stewart 	if (break_flag) {
3113f8829a4aSRandall Stewart 		/*
3114f8829a4aSRandall Stewart 		 * we need to report rwnd overrun drops.
3115f8829a4aSRandall Stewart 		 */
3116f8829a4aSRandall Stewart 		sctp_send_packet_dropped(stcb, net, *mm, iphlen, 0);
3117f8829a4aSRandall Stewart 	}
3118f8829a4aSRandall Stewart 	if (num_chunks) {
3119f8829a4aSRandall Stewart 		/*
3120ceaad40aSRandall Stewart 		 * Did we get data, if so update the time for auto-close and
3121f8829a4aSRandall Stewart 		 * give peer credit for being alive.
3122f8829a4aSRandall Stewart 		 */
3123f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvpktwithdata);
3124b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
3125c4739e2fSRandall Stewart 			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
3126c4739e2fSRandall Stewart 			    stcb->asoc.overall_error_count,
3127c4739e2fSRandall Stewart 			    0,
3128c4739e2fSRandall Stewart 			    SCTP_FROM_SCTP_INDATA,
3129c4739e2fSRandall Stewart 			    __LINE__);
3130c4739e2fSRandall Stewart 		}
3131f8829a4aSRandall Stewart 		stcb->asoc.overall_error_count = 0;
31326e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
3133f8829a4aSRandall Stewart 	}
3134f8829a4aSRandall Stewart 	/* now service all of the reassm queue if needed */
3135f8829a4aSRandall Stewart 	if (!(TAILQ_EMPTY(&asoc->reasmqueue)))
3136f8829a4aSRandall Stewart 		sctp_service_queues(stcb, asoc);
3137f8829a4aSRandall Stewart 
3138f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
313942551e99SRandall Stewart 		/* Assure that we ack right away */
314042551e99SRandall Stewart 		stcb->asoc.send_sack = 1;
3141f8829a4aSRandall Stewart 	}
3142f8829a4aSRandall Stewart 	/* Start a sack timer or QUEUE a SACK for sending */
3143f8829a4aSRandall Stewart 	if ((stcb->asoc.cumulative_tsn == stcb->asoc.highest_tsn_inside_map) &&
314442551e99SRandall Stewart 	    (stcb->asoc.mapping_array[0] != 0xff)) {
314542551e99SRandall Stewart 		if ((stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq) ||
314642551e99SRandall Stewart 		    (stcb->asoc.delayed_ack == 0) ||
3147b201f536SRandall Stewart 		    (stcb->asoc.numduptsns) ||
314842551e99SRandall Stewart 		    (stcb->asoc.send_sack == 1)) {
3149139bc87fSRandall Stewart 			if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
3150ad81507eSRandall Stewart 				(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
315142551e99SRandall Stewart 			}
3152830d754dSRandall Stewart 			/*
3153830d754dSRandall Stewart 			 * EY if nr_sacks used then send an nr-sack , a sack
3154830d754dSRandall Stewart 			 * otherwise
3155830d754dSRandall Stewart 			 */
3156830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack)
3157830d754dSRandall Stewart 				sctp_send_nr_sack(stcb);
3158830d754dSRandall Stewart 			else
3159f8829a4aSRandall Stewart 				sctp_send_sack(stcb);
3160f8829a4aSRandall Stewart 		} else {
316142551e99SRandall Stewart 			if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
3162f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
3163f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, NULL);
3164f8829a4aSRandall Stewart 			}
3165f8829a4aSRandall Stewart 		}
3166f8829a4aSRandall Stewart 	} else {
3167f8829a4aSRandall Stewart 		sctp_sack_check(stcb, 1, was_a_gap, &abort_flag);
3168f8829a4aSRandall Stewart 	}
3169f8829a4aSRandall Stewart 	if (abort_flag)
3170f8829a4aSRandall Stewart 		return (2);
3171f8829a4aSRandall Stewart 
3172f8829a4aSRandall Stewart 	return (0);
3173f8829a4aSRandall Stewart }
3174f8829a4aSRandall Stewart 
3175f8829a4aSRandall Stewart static void
3176458303daSRandall Stewart sctp_handle_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
3177f8829a4aSRandall Stewart     struct sctp_sack_chunk *ch, uint32_t last_tsn, uint32_t * biggest_tsn_acked,
3178139bc87fSRandall Stewart     uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
3179139bc87fSRandall Stewart     int num_seg, int *ecn_seg_sums)
3180f8829a4aSRandall Stewart {
3181f8829a4aSRandall Stewart 	/************************************************/
3182f8829a4aSRandall Stewart 	/* process fragments and update sendqueue        */
3183f8829a4aSRandall Stewart 	/************************************************/
3184f8829a4aSRandall Stewart 	struct sctp_sack *sack;
3185458303daSRandall Stewart 	struct sctp_gap_ack_block *frag, block;
3186f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
3187a1e13272SRandall Stewart 	int i, j;
3188a1e13272SRandall Stewart 	unsigned int theTSN;
3189f8829a4aSRandall Stewart 	int num_frs = 0;
3190f8829a4aSRandall Stewart 
3191f8829a4aSRandall Stewart 	uint16_t frag_strt, frag_end, primary_flag_set;
3192f8829a4aSRandall Stewart 	u_long last_frag_high;
3193f8829a4aSRandall Stewart 
3194f8829a4aSRandall Stewart 	/*
3195f8829a4aSRandall Stewart 	 * @@@ JRI : TODO: This flag is not used anywhere .. remove?
3196f8829a4aSRandall Stewart 	 */
3197f8829a4aSRandall Stewart 	if (asoc->primary_destination->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
3198f8829a4aSRandall Stewart 		primary_flag_set = 1;
3199f8829a4aSRandall Stewart 	} else {
3200f8829a4aSRandall Stewart 		primary_flag_set = 0;
3201f8829a4aSRandall Stewart 	}
3202f8829a4aSRandall Stewart 	sack = &ch->sack;
3203458303daSRandall Stewart 
3204458303daSRandall Stewart 	frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
3205458303daSRandall Stewart 	    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
3206458303daSRandall Stewart 	*offset += sizeof(block);
3207458303daSRandall Stewart 	if (frag == NULL) {
3208458303daSRandall Stewart 		return;
3209458303daSRandall Stewart 	}
3210f8829a4aSRandall Stewart 	tp1 = NULL;
3211f8829a4aSRandall Stewart 	last_frag_high = 0;
3212f8829a4aSRandall Stewart 	for (i = 0; i < num_seg; i++) {
3213f8829a4aSRandall Stewart 		frag_strt = ntohs(frag->start);
3214f8829a4aSRandall Stewart 		frag_end = ntohs(frag->end);
3215830d754dSRandall Stewart 		/* some sanity checks on the fragment offsets */
3216f8829a4aSRandall Stewart 		if (frag_strt > frag_end) {
3217f8829a4aSRandall Stewart 			/* this one is malformed, skip */
3218f8829a4aSRandall Stewart 			frag++;
3219f8829a4aSRandall Stewart 			continue;
3220f8829a4aSRandall Stewart 		}
3221f8829a4aSRandall Stewart 		if (compare_with_wrap((frag_end + last_tsn), *biggest_tsn_acked,
3222f8829a4aSRandall Stewart 		    MAX_TSN))
3223f8829a4aSRandall Stewart 			*biggest_tsn_acked = frag_end + last_tsn;
3224f8829a4aSRandall Stewart 
3225f8829a4aSRandall Stewart 		/* mark acked dgs and find out the highestTSN being acked */
3226f8829a4aSRandall Stewart 		if (tp1 == NULL) {
3227f8829a4aSRandall Stewart 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
3228f8829a4aSRandall Stewart 
3229f8829a4aSRandall Stewart 			/* save the locations of the last frags */
3230f8829a4aSRandall Stewart 			last_frag_high = frag_end + last_tsn;
3231f8829a4aSRandall Stewart 		} else {
3232f8829a4aSRandall Stewart 			/*
3233f8829a4aSRandall Stewart 			 * now lets see if we need to reset the queue due to
3234f8829a4aSRandall Stewart 			 * a out-of-order SACK fragment
3235f8829a4aSRandall Stewart 			 */
3236f8829a4aSRandall Stewart 			if (compare_with_wrap(frag_strt + last_tsn,
3237f8829a4aSRandall Stewart 			    last_frag_high, MAX_TSN)) {
3238f8829a4aSRandall Stewart 				/*
3239f8829a4aSRandall Stewart 				 * if the new frag starts after the last TSN
3240f8829a4aSRandall Stewart 				 * frag covered, we are ok and this one is
3241f8829a4aSRandall Stewart 				 * beyond the last one
3242f8829a4aSRandall Stewart 				 */
3243f8829a4aSRandall Stewart 				;
3244f8829a4aSRandall Stewart 			} else {
3245f8829a4aSRandall Stewart 				/*
3246f8829a4aSRandall Stewart 				 * ok, they have reset us, so we need to
3247f8829a4aSRandall Stewart 				 * reset the queue this will cause extra
3248f8829a4aSRandall Stewart 				 * hunting but hey, they chose the
3249f8829a4aSRandall Stewart 				 * performance hit when they failed to order
3250830d754dSRandall Stewart 				 * their gaps
3251f8829a4aSRandall Stewart 				 */
3252f8829a4aSRandall Stewart 				tp1 = TAILQ_FIRST(&asoc->sent_queue);
3253f8829a4aSRandall Stewart 			}
3254f8829a4aSRandall Stewart 			last_frag_high = frag_end + last_tsn;
3255f8829a4aSRandall Stewart 		}
3256a1e13272SRandall Stewart 		for (j = frag_strt; j <= frag_end; j++) {
3257a1e13272SRandall Stewart 			theTSN = j + last_tsn;
3258f8829a4aSRandall Stewart 			while (tp1) {
3259f8829a4aSRandall Stewart 				if (tp1->rec.data.doing_fast_retransmit)
3260f8829a4aSRandall Stewart 					num_frs++;
3261f8829a4aSRandall Stewart 
3262f8829a4aSRandall Stewart 				/*
3263f8829a4aSRandall Stewart 				 * CMT: CUCv2 algorithm. For each TSN being
3264f8829a4aSRandall Stewart 				 * processed from the sent queue, track the
3265f8829a4aSRandall Stewart 				 * next expected pseudo-cumack, or
3266f8829a4aSRandall Stewart 				 * rtx_pseudo_cumack, if required. Separate
3267f8829a4aSRandall Stewart 				 * cumack trackers for first transmissions,
3268f8829a4aSRandall Stewart 				 * and retransmissions.
3269f8829a4aSRandall Stewart 				 */
3270f8829a4aSRandall Stewart 				if ((tp1->whoTo->find_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
3271f8829a4aSRandall Stewart 				    (tp1->snd_count == 1)) {
3272f8829a4aSRandall Stewart 					tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
3273f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 0;
3274f8829a4aSRandall Stewart 				}
3275f8829a4aSRandall Stewart 				if ((tp1->whoTo->find_rtx_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
3276f8829a4aSRandall Stewart 				    (tp1->snd_count > 1)) {
3277f8829a4aSRandall Stewart 					tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
3278f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 0;
3279f8829a4aSRandall Stewart 				}
3280a1e13272SRandall Stewart 				if (tp1->rec.data.TSN_seq == theTSN) {
3281f8829a4aSRandall Stewart 					if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
3282f8829a4aSRandall Stewart 						/*
3283f8829a4aSRandall Stewart 						 * must be held until
3284f8829a4aSRandall Stewart 						 * cum-ack passes
3285f8829a4aSRandall Stewart 						 */
3286f8829a4aSRandall Stewart 						/*
3287f8829a4aSRandall Stewart 						 * ECN Nonce: Add the nonce
3288f8829a4aSRandall Stewart 						 * value to the sender's
3289f8829a4aSRandall Stewart 						 * nonce sum
3290f8829a4aSRandall Stewart 						 */
3291c105859eSRandall Stewart 						if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3292c105859eSRandall Stewart 							/*-
3293c105859eSRandall Stewart 							 * If it is less than RESEND, it is
3294c105859eSRandall Stewart 							 * now no-longer in flight.
3295c105859eSRandall Stewart 							 * Higher values may already be set
3296c105859eSRandall Stewart 							 * via previous Gap Ack Blocks...
3297c105859eSRandall Stewart 							 * i.e. ACKED or RESEND.
3298f8829a4aSRandall Stewart 							 */
3299f8829a4aSRandall Stewart 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
3300f8829a4aSRandall Stewart 							    *biggest_newly_acked_tsn, MAX_TSN)) {
3301f8829a4aSRandall Stewart 								*biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
3302f8829a4aSRandall Stewart 							}
3303f8829a4aSRandall Stewart 							/*
3304f8829a4aSRandall Stewart 							 * CMT: SFR algo
3305f8829a4aSRandall Stewart 							 * (and HTNA) - set
3306f8829a4aSRandall Stewart 							 * saw_newack to 1
3307f8829a4aSRandall Stewart 							 * for dest being
3308f8829a4aSRandall Stewart 							 * newly acked.
3309f8829a4aSRandall Stewart 							 * update
3310f8829a4aSRandall Stewart 							 * this_sack_highest_
3311f8829a4aSRandall Stewart 							 * newack if
3312f8829a4aSRandall Stewart 							 * appropriate.
3313f8829a4aSRandall Stewart 							 */
3314f8829a4aSRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0)
3315f8829a4aSRandall Stewart 								tp1->whoTo->saw_newack = 1;
3316f8829a4aSRandall Stewart 
3317f8829a4aSRandall Stewart 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
3318f8829a4aSRandall Stewart 							    tp1->whoTo->this_sack_highest_newack,
3319f8829a4aSRandall Stewart 							    MAX_TSN)) {
3320f8829a4aSRandall Stewart 								tp1->whoTo->this_sack_highest_newack =
3321f8829a4aSRandall Stewart 								    tp1->rec.data.TSN_seq;
3322f8829a4aSRandall Stewart 							}
3323f8829a4aSRandall Stewart 							/*
3324f8829a4aSRandall Stewart 							 * CMT DAC algo:
3325f8829a4aSRandall Stewart 							 * also update
3326f8829a4aSRandall Stewart 							 * this_sack_lowest_n
3327f8829a4aSRandall Stewart 							 * ewack
3328f8829a4aSRandall Stewart 							 */
3329f8829a4aSRandall Stewart 							if (*this_sack_lowest_newack == 0) {
3330b3f1ea41SRandall Stewart 								if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3331f8829a4aSRandall Stewart 									sctp_log_sack(*this_sack_lowest_newack,
3332d0cf96b4SMax Laier 									    last_tsn,
3333f8829a4aSRandall Stewart 									    tp1->rec.data.TSN_seq,
3334f8829a4aSRandall Stewart 									    0,
3335f8829a4aSRandall Stewart 									    0,
3336f8829a4aSRandall Stewart 									    SCTP_LOG_TSN_ACKED);
333780fefe0aSRandall Stewart 								}
3338f8829a4aSRandall Stewart 								*this_sack_lowest_newack = tp1->rec.data.TSN_seq;
3339f8829a4aSRandall Stewart 							}
3340f8829a4aSRandall Stewart 							/*
3341f8829a4aSRandall Stewart 							 * CMT: CUCv2
3342f8829a4aSRandall Stewart 							 * algorithm. If
3343f8829a4aSRandall Stewart 							 * (rtx-)pseudo-cumac
3344f8829a4aSRandall Stewart 							 * k for corresp
3345f8829a4aSRandall Stewart 							 * dest is being
3346f8829a4aSRandall Stewart 							 * acked, then we
3347f8829a4aSRandall Stewart 							 * have a new
3348f8829a4aSRandall Stewart 							 * (rtx-)pseudo-cumac
3349f8829a4aSRandall Stewart 							 * k. Set
3350f8829a4aSRandall Stewart 							 * new_(rtx_)pseudo_c
3351f8829a4aSRandall Stewart 							 * umack to TRUE so
3352f8829a4aSRandall Stewart 							 * that the cwnd for
3353f8829a4aSRandall Stewart 							 * this dest can be
3354f8829a4aSRandall Stewart 							 * updated. Also
3355f8829a4aSRandall Stewart 							 * trigger search
3356f8829a4aSRandall Stewart 							 * for the next
3357f8829a4aSRandall Stewart 							 * expected
3358f8829a4aSRandall Stewart 							 * (rtx-)pseudo-cumac
3359f8829a4aSRandall Stewart 							 * k. Separate
3360f8829a4aSRandall Stewart 							 * pseudo_cumack
3361f8829a4aSRandall Stewart 							 * trackers for
3362f8829a4aSRandall Stewart 							 * first
3363f8829a4aSRandall Stewart 							 * transmissions and
3364f8829a4aSRandall Stewart 							 * retransmissions.
3365f8829a4aSRandall Stewart 							 */
3366f8829a4aSRandall Stewart 							if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
3367f8829a4aSRandall Stewart 								if (tp1->rec.data.chunk_was_revoked == 0) {
3368f8829a4aSRandall Stewart 									tp1->whoTo->new_pseudo_cumack = 1;
3369f8829a4aSRandall Stewart 								}
3370f8829a4aSRandall Stewart 								tp1->whoTo->find_pseudo_cumack = 1;
3371f8829a4aSRandall Stewart 							}
3372b3f1ea41SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
3373f8829a4aSRandall Stewart 								sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
337480fefe0aSRandall Stewart 							}
3375f8829a4aSRandall Stewart 							if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
3376f8829a4aSRandall Stewart 								if (tp1->rec.data.chunk_was_revoked == 0) {
3377f8829a4aSRandall Stewart 									tp1->whoTo->new_pseudo_cumack = 1;
3378f8829a4aSRandall Stewart 								}
3379f8829a4aSRandall Stewart 								tp1->whoTo->find_rtx_pseudo_cumack = 1;
3380f8829a4aSRandall Stewart 							}
3381b3f1ea41SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3382f8829a4aSRandall Stewart 								sctp_log_sack(*biggest_newly_acked_tsn,
3383f8829a4aSRandall Stewart 								    last_tsn,
3384f8829a4aSRandall Stewart 								    tp1->rec.data.TSN_seq,
3385f8829a4aSRandall Stewart 								    frag_strt,
3386f8829a4aSRandall Stewart 								    frag_end,
3387f8829a4aSRandall Stewart 								    SCTP_LOG_TSN_ACKED);
338880fefe0aSRandall Stewart 							}
3389b3f1ea41SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3390c105859eSRandall Stewart 								sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
3391a5d547adSRandall Stewart 								    tp1->whoTo->flight_size,
3392a5d547adSRandall Stewart 								    tp1->book_size,
3393c105859eSRandall Stewart 								    (uintptr_t) tp1->whoTo,
3394a5d547adSRandall Stewart 								    tp1->rec.data.TSN_seq);
339580fefe0aSRandall Stewart 							}
3396c105859eSRandall Stewart 							sctp_flight_size_decrease(tp1);
3397c105859eSRandall Stewart 							sctp_total_flight_decrease(stcb, tp1);
3398f8829a4aSRandall Stewart 
3399f8829a4aSRandall Stewart 							tp1->whoTo->net_ack += tp1->send_size;
3400f8829a4aSRandall Stewart 							if (tp1->snd_count < 2) {
3401f8829a4aSRandall Stewart 								/*
3402a5d547adSRandall Stewart 								 * True
3403a5d547adSRandall Stewart 								 * non-retran
3404a5d547adSRandall Stewart 								 * smited
3405a5d547adSRandall Stewart 								 * chunk */
3406f8829a4aSRandall Stewart 								tp1->whoTo->net_ack2 += tp1->send_size;
3407f8829a4aSRandall Stewart 
3408f8829a4aSRandall Stewart 								/*
3409a5d547adSRandall Stewart 								 * update RTO
3410a5d547adSRandall Stewart 								 * too ? */
3411f8829a4aSRandall Stewart 								if (tp1->do_rtt) {
3412f8829a4aSRandall Stewart 									tp1->whoTo->RTO =
3413f8829a4aSRandall Stewart 									    sctp_calculate_rto(stcb,
3414f8829a4aSRandall Stewart 									    asoc,
3415f8829a4aSRandall Stewart 									    tp1->whoTo,
341618e198d3SRandall Stewart 									    &tp1->sent_rcv_time,
341718e198d3SRandall Stewart 									    sctp_align_safe_nocopy);
3418f8829a4aSRandall Stewart 									tp1->do_rtt = 0;
3419f8829a4aSRandall Stewart 								}
3420f8829a4aSRandall Stewart 							}
3421f8829a4aSRandall Stewart 						}
3422c105859eSRandall Stewart 						if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
3423c105859eSRandall Stewart 							(*ecn_seg_sums) += tp1->rec.data.ect_nonce;
3424c105859eSRandall Stewart 							(*ecn_seg_sums) &= SCTP_SACK_NONCE_SUM;
3425c105859eSRandall Stewart 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
3426f8829a4aSRandall Stewart 							    asoc->this_sack_highest_gap,
3427f8829a4aSRandall Stewart 							    MAX_TSN)) {
3428f8829a4aSRandall Stewart 								asoc->this_sack_highest_gap =
3429f8829a4aSRandall Stewart 								    tp1->rec.data.TSN_seq;
3430f8829a4aSRandall Stewart 							}
3431f8829a4aSRandall Stewart 							if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3432f8829a4aSRandall Stewart 								sctp_ucount_decr(asoc->sent_queue_retran_cnt);
3433f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
3434f8829a4aSRandall Stewart 								sctp_audit_log(0xB2,
3435f8829a4aSRandall Stewart 								    (asoc->sent_queue_retran_cnt & 0x000000ff));
3436f8829a4aSRandall Stewart #endif
3437f8829a4aSRandall Stewart 							}
3438c105859eSRandall Stewart 						}
3439c105859eSRandall Stewart 						/*
3440c105859eSRandall Stewart 						 * All chunks NOT UNSENT
3441c105859eSRandall Stewart 						 * fall through here and are
34428933fa13SRandall Stewart 						 * marked (leave PR-SCTP
34438933fa13SRandall Stewart 						 * ones that are to skip
34448933fa13SRandall Stewart 						 * alone though)
3445c105859eSRandall Stewart 						 */
34468933fa13SRandall Stewart 						if (tp1->sent != SCTP_FORWARD_TSN_SKIP)
3447f8829a4aSRandall Stewart 							tp1->sent = SCTP_DATAGRAM_MARKED;
34488933fa13SRandall Stewart 
344942551e99SRandall Stewart 						if (tp1->rec.data.chunk_was_revoked) {
345042551e99SRandall Stewart 							/* deflate the cwnd */
345142551e99SRandall Stewart 							tp1->whoTo->cwnd -= tp1->book_size;
345242551e99SRandall Stewart 							tp1->rec.data.chunk_was_revoked = 0;
345342551e99SRandall Stewart 						}
3454f8829a4aSRandall Stewart 					}
3455f8829a4aSRandall Stewart 					break;
3456a1e13272SRandall Stewart 				}	/* if (tp1->TSN_seq == theTSN) */
3457a1e13272SRandall Stewart 				if (compare_with_wrap(tp1->rec.data.TSN_seq, theTSN,
3458f8829a4aSRandall Stewart 				    MAX_TSN))
3459f8829a4aSRandall Stewart 					break;
3460f8829a4aSRandall Stewart 
3461f8829a4aSRandall Stewart 				tp1 = TAILQ_NEXT(tp1, sctp_next);
3462f8829a4aSRandall Stewart 			}	/* end while (tp1) */
3463f8829a4aSRandall Stewart 		}		/* end for (j = fragStart */
3464458303daSRandall Stewart 		frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
3465458303daSRandall Stewart 		    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
3466458303daSRandall Stewart 		*offset += sizeof(block);
3467458303daSRandall Stewart 		if (frag == NULL) {
3468458303daSRandall Stewart 			break;
3469458303daSRandall Stewart 		}
3470f8829a4aSRandall Stewart 	}
3471b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
347280fefe0aSRandall Stewart 		if (num_frs)
347380fefe0aSRandall Stewart 			sctp_log_fr(*biggest_tsn_acked,
347480fefe0aSRandall Stewart 			    *biggest_newly_acked_tsn,
347580fefe0aSRandall Stewart 			    last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
347680fefe0aSRandall Stewart 	}
3477f8829a4aSRandall Stewart }
3478f8829a4aSRandall Stewart 
3479f8829a4aSRandall Stewart static void
3480c105859eSRandall Stewart sctp_check_for_revoked(struct sctp_tcb *stcb,
3481c105859eSRandall Stewart     struct sctp_association *asoc, uint32_t cumack,
3482f8829a4aSRandall Stewart     u_long biggest_tsn_acked)
3483f8829a4aSRandall Stewart {
3484f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
3485f8829a4aSRandall Stewart 	int tot_revoked = 0;
3486f8829a4aSRandall Stewart 
3487f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
3488f8829a4aSRandall Stewart 	while (tp1) {
3489f8829a4aSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq, cumack,
3490f8829a4aSRandall Stewart 		    MAX_TSN)) {
3491f8829a4aSRandall Stewart 			/*
3492f8829a4aSRandall Stewart 			 * ok this guy is either ACK or MARKED. If it is
3493f8829a4aSRandall Stewart 			 * ACKED it has been previously acked but not this
3494f8829a4aSRandall Stewart 			 * time i.e. revoked.  If it is MARKED it was ACK'ed
3495f8829a4aSRandall Stewart 			 * again.
3496f8829a4aSRandall Stewart 			 */
3497d06c82f1SRandall Stewart 			if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
3498d06c82f1SRandall Stewart 			    MAX_TSN))
3499d06c82f1SRandall Stewart 				break;
3500d06c82f1SRandall Stewart 
3501d06c82f1SRandall Stewart 
3502f8829a4aSRandall Stewart 			if (tp1->sent == SCTP_DATAGRAM_ACKED) {
3503f8829a4aSRandall Stewart 				/* it has been revoked */
3504f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_SENT;
3505f8829a4aSRandall Stewart 				tp1->rec.data.chunk_was_revoked = 1;
3506a5d547adSRandall Stewart 				/*
350742551e99SRandall Stewart 				 * We must add this stuff back in to assure
350842551e99SRandall Stewart 				 * timers and such get started.
3509a5d547adSRandall Stewart 				 */
3510b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3511c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
3512c105859eSRandall Stewart 					    tp1->whoTo->flight_size,
3513c105859eSRandall Stewart 					    tp1->book_size,
3514c105859eSRandall Stewart 					    (uintptr_t) tp1->whoTo,
3515c105859eSRandall Stewart 					    tp1->rec.data.TSN_seq);
351680fefe0aSRandall Stewart 				}
3517c105859eSRandall Stewart 				sctp_flight_size_increase(tp1);
3518c105859eSRandall Stewart 				sctp_total_flight_increase(stcb, tp1);
351942551e99SRandall Stewart 				/*
352042551e99SRandall Stewart 				 * We inflate the cwnd to compensate for our
352142551e99SRandall Stewart 				 * artificial inflation of the flight_size.
352242551e99SRandall Stewart 				 */
352342551e99SRandall Stewart 				tp1->whoTo->cwnd += tp1->book_size;
3524f8829a4aSRandall Stewart 				tot_revoked++;
3525b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3526f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
3527f8829a4aSRandall Stewart 					    cumack,
3528f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
3529f8829a4aSRandall Stewart 					    0,
3530f8829a4aSRandall Stewart 					    0,
3531f8829a4aSRandall Stewart 					    SCTP_LOG_TSN_REVOKED);
353280fefe0aSRandall Stewart 				}
3533f8829a4aSRandall Stewart 			} else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
3534f8829a4aSRandall Stewart 				/* it has been re-acked in this SACK */
3535f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
3536f8829a4aSRandall Stewart 			}
3537f8829a4aSRandall Stewart 		}
3538f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT)
3539f8829a4aSRandall Stewart 			break;
3540f8829a4aSRandall Stewart 		tp1 = TAILQ_NEXT(tp1, sctp_next);
3541f8829a4aSRandall Stewart 	}
3542f8829a4aSRandall Stewart 	if (tot_revoked > 0) {
3543f8829a4aSRandall Stewart 		/*
3544f8829a4aSRandall Stewart 		 * Setup the ecn nonce re-sync point. We do this since once
3545f8829a4aSRandall Stewart 		 * data is revoked we begin to retransmit things, which do
3546f8829a4aSRandall Stewart 		 * NOT have the ECN bits set. This means we are now out of
3547f8829a4aSRandall Stewart 		 * sync and must wait until we get back in sync with the
3548f8829a4aSRandall Stewart 		 * peer to check ECN bits.
3549f8829a4aSRandall Stewart 		 */
3550f8829a4aSRandall Stewart 		tp1 = TAILQ_FIRST(&asoc->send_queue);
3551f8829a4aSRandall Stewart 		if (tp1 == NULL) {
3552f8829a4aSRandall Stewart 			asoc->nonce_resync_tsn = asoc->sending_seq;
3553f8829a4aSRandall Stewart 		} else {
3554f8829a4aSRandall Stewart 			asoc->nonce_resync_tsn = tp1->rec.data.TSN_seq;
3555f8829a4aSRandall Stewart 		}
3556f8829a4aSRandall Stewart 		asoc->nonce_wait_for_ecne = 0;
3557f8829a4aSRandall Stewart 		asoc->nonce_sum_check = 0;
3558f8829a4aSRandall Stewart 	}
3559f8829a4aSRandall Stewart }
3560f8829a4aSRandall Stewart 
3561830d754dSRandall Stewart 
3562f8829a4aSRandall Stewart static void
3563f8829a4aSRandall Stewart sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
3564f8829a4aSRandall Stewart     u_long biggest_tsn_acked, u_long biggest_tsn_newly_acked, u_long this_sack_lowest_newack, int accum_moved)
3565f8829a4aSRandall Stewart {
3566f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
3567f8829a4aSRandall Stewart 	int strike_flag = 0;
3568f8829a4aSRandall Stewart 	struct timeval now;
3569f8829a4aSRandall Stewart 	int tot_retrans = 0;
3570f8829a4aSRandall Stewart 	uint32_t sending_seq;
3571f8829a4aSRandall Stewart 	struct sctp_nets *net;
3572f8829a4aSRandall Stewart 	int num_dests_sacked = 0;
3573f8829a4aSRandall Stewart 
3574f8829a4aSRandall Stewart 	/*
3575f8829a4aSRandall Stewart 	 * select the sending_seq, this is either the next thing ready to be
3576f8829a4aSRandall Stewart 	 * sent but not transmitted, OR, the next seq we assign.
3577f8829a4aSRandall Stewart 	 */
3578f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
3579f8829a4aSRandall Stewart 	if (tp1 == NULL) {
3580f8829a4aSRandall Stewart 		sending_seq = asoc->sending_seq;
3581f8829a4aSRandall Stewart 	} else {
3582f8829a4aSRandall Stewart 		sending_seq = tp1->rec.data.TSN_seq;
3583f8829a4aSRandall Stewart 	}
3584f8829a4aSRandall Stewart 
3585f8829a4aSRandall Stewart 	/* CMT DAC algo: finding out if SACK is a mixed SACK */
3586b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3587f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3588f8829a4aSRandall Stewart 			if (net->saw_newack)
3589f8829a4aSRandall Stewart 				num_dests_sacked++;
3590f8829a4aSRandall Stewart 		}
3591f8829a4aSRandall Stewart 	}
3592f8829a4aSRandall Stewart 	if (stcb->asoc.peer_supports_prsctp) {
35936e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
3594f8829a4aSRandall Stewart 	}
3595f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
3596f8829a4aSRandall Stewart 	while (tp1) {
3597f8829a4aSRandall Stewart 		strike_flag = 0;
3598f8829a4aSRandall Stewart 		if (tp1->no_fr_allowed) {
3599f8829a4aSRandall Stewart 			/* this one had a timeout or something */
3600f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3601f8829a4aSRandall Stewart 			continue;
3602f8829a4aSRandall Stewart 		}
3603b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3604f8829a4aSRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND)
3605f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3606f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3607f8829a4aSRandall Stewart 				    tp1->sent,
3608f8829a4aSRandall Stewart 				    SCTP_FR_LOG_CHECK_STRIKE);
360980fefe0aSRandall Stewart 		}
3610f8829a4aSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
3611f8829a4aSRandall Stewart 		    MAX_TSN) ||
3612f8829a4aSRandall Stewart 		    tp1->sent == SCTP_DATAGRAM_UNSENT) {
3613f8829a4aSRandall Stewart 			/* done */
3614f8829a4aSRandall Stewart 			break;
3615f8829a4aSRandall Stewart 		}
3616f8829a4aSRandall Stewart 		if (stcb->asoc.peer_supports_prsctp) {
3617f8829a4aSRandall Stewart 			if ((PR_SCTP_TTL_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3618f8829a4aSRandall Stewart 				/* Is it expired? */
36195e54f665SRandall Stewart 				if (
3620fc14de76SRandall Stewart 				/*
3621fc14de76SRandall Stewart 				 * TODO sctp_constants.h needs alternative
3622fc14de76SRandall Stewart 				 * time macros when _KERNEL is undefined.
3623fc14de76SRandall Stewart 				 */
36245e54f665SRandall Stewart 				    (timevalcmp(&now, &tp1->rec.data.timetodrop, >))
36255e54f665SRandall Stewart 				    ) {
3626f8829a4aSRandall Stewart 					/* Yes so drop it */
3627f8829a4aSRandall Stewart 					if (tp1->data != NULL) {
3628ad81507eSRandall Stewart 						(void)sctp_release_pr_sctp_chunk(stcb, tp1,
3629f8829a4aSRandall Stewart 						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
36300c0982b8SRandall Stewart 						    SCTP_SO_NOT_LOCKED);
3631f8829a4aSRandall Stewart 					}
3632f8829a4aSRandall Stewart 					tp1 = TAILQ_NEXT(tp1, sctp_next);
3633f8829a4aSRandall Stewart 					continue;
3634f8829a4aSRandall Stewart 				}
3635f8829a4aSRandall Stewart 			}
3636f8829a4aSRandall Stewart 		}
3637f8829a4aSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq,
3638f8829a4aSRandall Stewart 		    asoc->this_sack_highest_gap, MAX_TSN)) {
3639f8829a4aSRandall Stewart 			/* we are beyond the tsn in the sack  */
3640f8829a4aSRandall Stewart 			break;
3641f8829a4aSRandall Stewart 		}
3642f8829a4aSRandall Stewart 		if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
3643f8829a4aSRandall Stewart 			/* either a RESEND, ACKED, or MARKED */
3644f8829a4aSRandall Stewart 			/* skip */
3645f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3646f8829a4aSRandall Stewart 			continue;
3647f8829a4aSRandall Stewart 		}
3648f8829a4aSRandall Stewart 		/*
3649f8829a4aSRandall Stewart 		 * CMT : SFR algo (covers part of DAC and HTNA as well)
3650f8829a4aSRandall Stewart 		 */
3651ad81507eSRandall Stewart 		if (tp1->whoTo && tp1->whoTo->saw_newack == 0) {
3652f8829a4aSRandall Stewart 			/*
3653f8829a4aSRandall Stewart 			 * No new acks were receieved for data sent to this
3654f8829a4aSRandall Stewart 			 * dest. Therefore, according to the SFR algo for
3655f8829a4aSRandall Stewart 			 * CMT, no data sent to this dest can be marked for
3656c105859eSRandall Stewart 			 * FR using this SACK.
3657f8829a4aSRandall Stewart 			 */
3658f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3659f8829a4aSRandall Stewart 			continue;
3660ad81507eSRandall Stewart 		} else if (tp1->whoTo && compare_with_wrap(tp1->rec.data.TSN_seq,
3661f8829a4aSRandall Stewart 		    tp1->whoTo->this_sack_highest_newack, MAX_TSN)) {
3662f8829a4aSRandall Stewart 			/*
3663f8829a4aSRandall Stewart 			 * CMT: New acks were receieved for data sent to
3664f8829a4aSRandall Stewart 			 * this dest. But no new acks were seen for data
3665f8829a4aSRandall Stewart 			 * sent after tp1. Therefore, according to the SFR
3666f8829a4aSRandall Stewart 			 * algo for CMT, tp1 cannot be marked for FR using
3667f8829a4aSRandall Stewart 			 * this SACK. This step covers part of the DAC algo
3668f8829a4aSRandall Stewart 			 * and the HTNA algo as well.
3669f8829a4aSRandall Stewart 			 */
3670f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3671f8829a4aSRandall Stewart 			continue;
3672f8829a4aSRandall Stewart 		}
3673f8829a4aSRandall Stewart 		/*
3674f8829a4aSRandall Stewart 		 * Here we check to see if we were have already done a FR
3675f8829a4aSRandall Stewart 		 * and if so we see if the biggest TSN we saw in the sack is
3676f8829a4aSRandall Stewart 		 * smaller than the recovery point. If so we don't strike
3677f8829a4aSRandall Stewart 		 * the tsn... otherwise we CAN strike the TSN.
3678f8829a4aSRandall Stewart 		 */
3679f8829a4aSRandall Stewart 		/*
368042551e99SRandall Stewart 		 * @@@ JRI: Check for CMT if (accum_moved &&
368142551e99SRandall Stewart 		 * asoc->fast_retran_loss_recovery && (sctp_cmt_on_off ==
368242551e99SRandall Stewart 		 * 0)) {
3683f8829a4aSRandall Stewart 		 */
368442551e99SRandall Stewart 		if (accum_moved && asoc->fast_retran_loss_recovery) {
3685f8829a4aSRandall Stewart 			/*
3686f8829a4aSRandall Stewart 			 * Strike the TSN if in fast-recovery and cum-ack
3687f8829a4aSRandall Stewart 			 * moved.
3688f8829a4aSRandall Stewart 			 */
3689b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3690f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3691f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3692f8829a4aSRandall Stewart 				    tp1->sent,
3693f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
369480fefe0aSRandall Stewart 			}
36955e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3696f8829a4aSRandall Stewart 				tp1->sent++;
36975e54f665SRandall Stewart 			}
3698b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3699f8829a4aSRandall Stewart 				/*
3700f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
3701f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
3702f8829a4aSRandall Stewart 				 * because it would have been set to the
3703f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
3704f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
3705f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
3706c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
3707c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3708c105859eSRandall Stewart 				 * indicates that two packets have been
3709c105859eSRandall Stewart 				 * received after this missing TSN.
37105e54f665SRandall Stewart 				 */
37115e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3712f8829a4aSRandall Stewart 				    compare_with_wrap(this_sack_lowest_newack, tp1->rec.data.TSN_seq, MAX_TSN)) {
3713b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3714f8829a4aSRandall Stewart 						sctp_log_fr(16 + num_dests_sacked,
3715f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3716f8829a4aSRandall Stewart 						    tp1->sent,
3717f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
371880fefe0aSRandall Stewart 					}
3719f8829a4aSRandall Stewart 					tp1->sent++;
3720f8829a4aSRandall Stewart 				}
3721f8829a4aSRandall Stewart 			}
3722b3f1ea41SRandall Stewart 		} else if ((tp1->rec.data.doing_fast_retransmit) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
3723f8829a4aSRandall Stewart 			/*
3724f8829a4aSRandall Stewart 			 * For those that have done a FR we must take
3725f8829a4aSRandall Stewart 			 * special consideration if we strike. I.e the
3726f8829a4aSRandall Stewart 			 * biggest_newly_acked must be higher than the
3727f8829a4aSRandall Stewart 			 * sending_seq at the time we did the FR.
3728f8829a4aSRandall Stewart 			 */
37295e54f665SRandall Stewart 			if (
3730f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3731f8829a4aSRandall Stewart 			/*
3732f8829a4aSRandall Stewart 			 * If FR's go to new networks, then we must only do
3733f8829a4aSRandall Stewart 			 * this for singly homed asoc's. However if the FR's
3734f8829a4aSRandall Stewart 			 * go to the same network (Armando's work) then its
3735f8829a4aSRandall Stewart 			 * ok to FR multiple times.
3736f8829a4aSRandall Stewart 			 */
37375e54f665SRandall Stewart 			    (asoc->numnets < 2)
3738f8829a4aSRandall Stewart #else
37395e54f665SRandall Stewart 			    (1)
3740f8829a4aSRandall Stewart #endif
37415e54f665SRandall Stewart 			    ) {
37425e54f665SRandall Stewart 
3743f8829a4aSRandall Stewart 				if ((compare_with_wrap(biggest_tsn_newly_acked,
3744f8829a4aSRandall Stewart 				    tp1->rec.data.fast_retran_tsn, MAX_TSN)) ||
3745f8829a4aSRandall Stewart 				    (biggest_tsn_newly_acked ==
3746f8829a4aSRandall Stewart 				    tp1->rec.data.fast_retran_tsn)) {
3747f8829a4aSRandall Stewart 					/*
3748f8829a4aSRandall Stewart 					 * Strike the TSN, since this ack is
3749f8829a4aSRandall Stewart 					 * beyond where things were when we
3750f8829a4aSRandall Stewart 					 * did a FR.
3751f8829a4aSRandall Stewart 					 */
3752b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3753f8829a4aSRandall Stewart 						sctp_log_fr(biggest_tsn_newly_acked,
3754f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3755f8829a4aSRandall Stewart 						    tp1->sent,
3756f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
375780fefe0aSRandall Stewart 					}
37585e54f665SRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3759f8829a4aSRandall Stewart 						tp1->sent++;
37605e54f665SRandall Stewart 					}
3761f8829a4aSRandall Stewart 					strike_flag = 1;
3762b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3763f8829a4aSRandall Stewart 						/*
3764f8829a4aSRandall Stewart 						 * CMT DAC algorithm: If
3765f8829a4aSRandall Stewart 						 * SACK flag is set to 0,
3766f8829a4aSRandall Stewart 						 * then lowest_newack test
3767f8829a4aSRandall Stewart 						 * will not pass because it
3768f8829a4aSRandall Stewart 						 * would have been set to
3769f8829a4aSRandall Stewart 						 * the cumack earlier. If
3770f8829a4aSRandall Stewart 						 * not already to be rtx'd,
3771f8829a4aSRandall Stewart 						 * If not a mixed sack and
3772f8829a4aSRandall Stewart 						 * if tp1 is not between two
3773f8829a4aSRandall Stewart 						 * sacked TSNs, then mark by
3774c105859eSRandall Stewart 						 * one more. NOTE that we
3775c105859eSRandall Stewart 						 * are marking by one
3776c105859eSRandall Stewart 						 * additional time since the
3777c105859eSRandall Stewart 						 * SACK DAC flag indicates
3778c105859eSRandall Stewart 						 * that two packets have
3779c105859eSRandall Stewart 						 * been received after this
3780c105859eSRandall Stewart 						 * missing TSN.
3781f8829a4aSRandall Stewart 						 */
37825e54f665SRandall Stewart 						if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
37835e54f665SRandall Stewart 						    (num_dests_sacked == 1) &&
37845e54f665SRandall Stewart 						    compare_with_wrap(this_sack_lowest_newack,
37855e54f665SRandall Stewart 						    tp1->rec.data.TSN_seq, MAX_TSN)) {
3786b3f1ea41SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3787f8829a4aSRandall Stewart 								sctp_log_fr(32 + num_dests_sacked,
3788f8829a4aSRandall Stewart 								    tp1->rec.data.TSN_seq,
3789f8829a4aSRandall Stewart 								    tp1->sent,
3790f8829a4aSRandall Stewart 								    SCTP_FR_LOG_STRIKE_CHUNK);
379180fefe0aSRandall Stewart 							}
37925e54f665SRandall Stewart 							if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3793f8829a4aSRandall Stewart 								tp1->sent++;
37945e54f665SRandall Stewart 							}
3795f8829a4aSRandall Stewart 						}
3796f8829a4aSRandall Stewart 					}
3797f8829a4aSRandall Stewart 				}
3798f8829a4aSRandall Stewart 			}
3799f8829a4aSRandall Stewart 			/*
380042551e99SRandall Stewart 			 * JRI: TODO: remove code for HTNA algo. CMT's SFR
380142551e99SRandall Stewart 			 * algo covers HTNA.
3802f8829a4aSRandall Stewart 			 */
3803f8829a4aSRandall Stewart 		} else if (compare_with_wrap(tp1->rec.data.TSN_seq,
3804f8829a4aSRandall Stewart 		    biggest_tsn_newly_acked, MAX_TSN)) {
3805f8829a4aSRandall Stewart 			/*
3806f8829a4aSRandall Stewart 			 * We don't strike these: This is the  HTNA
3807f8829a4aSRandall Stewart 			 * algorithm i.e. we don't strike If our TSN is
3808f8829a4aSRandall Stewart 			 * larger than the Highest TSN Newly Acked.
3809f8829a4aSRandall Stewart 			 */
3810f8829a4aSRandall Stewart 			;
3811f8829a4aSRandall Stewart 		} else {
3812f8829a4aSRandall Stewart 			/* Strike the TSN */
3813b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3814f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3815f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3816f8829a4aSRandall Stewart 				    tp1->sent,
3817f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
381880fefe0aSRandall Stewart 			}
38195e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3820f8829a4aSRandall Stewart 				tp1->sent++;
38215e54f665SRandall Stewart 			}
3822b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3823f8829a4aSRandall Stewart 				/*
3824f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
3825f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
3826f8829a4aSRandall Stewart 				 * because it would have been set to the
3827f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
3828f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
3829f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
3830c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
3831c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3832c105859eSRandall Stewart 				 * indicates that two packets have been
3833c105859eSRandall Stewart 				 * received after this missing TSN.
3834f8829a4aSRandall Stewart 				 */
38355e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3836f8829a4aSRandall Stewart 				    compare_with_wrap(this_sack_lowest_newack, tp1->rec.data.TSN_seq, MAX_TSN)) {
3837b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3838f8829a4aSRandall Stewart 						sctp_log_fr(48 + num_dests_sacked,
3839f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3840f8829a4aSRandall Stewart 						    tp1->sent,
3841f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
384280fefe0aSRandall Stewart 					}
3843f8829a4aSRandall Stewart 					tp1->sent++;
3844f8829a4aSRandall Stewart 				}
3845f8829a4aSRandall Stewart 			}
3846f8829a4aSRandall Stewart 		}
3847f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3848f8829a4aSRandall Stewart 			struct sctp_nets *alt;
3849f8829a4aSRandall Stewart 
3850544e35bdSRandall Stewart 			/* fix counts and things */
3851544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3852544e35bdSRandall Stewart 				sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND,
3853544e35bdSRandall Stewart 				    (tp1->whoTo ? (tp1->whoTo->flight_size) : 0),
3854544e35bdSRandall Stewart 				    tp1->book_size,
3855544e35bdSRandall Stewart 				    (uintptr_t) tp1->whoTo,
3856544e35bdSRandall Stewart 				    tp1->rec.data.TSN_seq);
3857544e35bdSRandall Stewart 			}
3858544e35bdSRandall Stewart 			if (tp1->whoTo) {
3859544e35bdSRandall Stewart 				tp1->whoTo->net_ack++;
3860544e35bdSRandall Stewart 				sctp_flight_size_decrease(tp1);
3861544e35bdSRandall Stewart 			}
3862544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
3863544e35bdSRandall Stewart 				sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
3864544e35bdSRandall Stewart 				    asoc->peers_rwnd, tp1->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3865544e35bdSRandall Stewart 			}
3866544e35bdSRandall Stewart 			/* add back to the rwnd */
3867544e35bdSRandall Stewart 			asoc->peers_rwnd += (tp1->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3868544e35bdSRandall Stewart 
3869544e35bdSRandall Stewart 			/* remove from the total flight */
3870544e35bdSRandall Stewart 			sctp_total_flight_decrease(stcb, tp1);
3871544e35bdSRandall Stewart 
3872abe15ad6SRandall Stewart 			if ((stcb->asoc.peer_supports_prsctp) &&
3873abe15ad6SRandall Stewart 			    (PR_SCTP_RTX_ENABLED(tp1->flags))) {
3874abe15ad6SRandall Stewart 				/*
3875abe15ad6SRandall Stewart 				 * Has it been retransmitted tv_sec times? -
3876abe15ad6SRandall Stewart 				 * we store the retran count there.
3877abe15ad6SRandall Stewart 				 */
3878abe15ad6SRandall Stewart 				if (tp1->snd_count > tp1->rec.data.timetodrop.tv_sec) {
3879abe15ad6SRandall Stewart 					/* Yes, so drop it */
3880abe15ad6SRandall Stewart 					if (tp1->data != NULL) {
3881abe15ad6SRandall Stewart 						(void)sctp_release_pr_sctp_chunk(stcb, tp1,
3882abe15ad6SRandall Stewart 						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3883abe15ad6SRandall Stewart 						    SCTP_SO_NOT_LOCKED);
3884abe15ad6SRandall Stewart 					}
3885abe15ad6SRandall Stewart 					/* Make sure to flag we had a FR */
3886abe15ad6SRandall Stewart 					tp1->whoTo->net_ack++;
3887abe15ad6SRandall Stewart 					tp1 = TAILQ_NEXT(tp1, sctp_next);
3888abe15ad6SRandall Stewart 					continue;
3889abe15ad6SRandall Stewart 				}
3890abe15ad6SRandall Stewart 			}
3891f8829a4aSRandall Stewart 			/* printf("OK, we are now ready to FR this guy\n"); */
3892b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3893f8829a4aSRandall Stewart 				sctp_log_fr(tp1->rec.data.TSN_seq, tp1->snd_count,
3894f8829a4aSRandall Stewart 				    0, SCTP_FR_MARKED);
389580fefe0aSRandall Stewart 			}
3896f8829a4aSRandall Stewart 			if (strike_flag) {
3897f8829a4aSRandall Stewart 				/* This is a subsequent FR */
3898f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_sendmultfastretrans);
3899f8829a4aSRandall Stewart 			}
39005e54f665SRandall Stewart 			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3901b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
3902f8829a4aSRandall Stewart 				/*
3903f8829a4aSRandall Stewart 				 * CMT: Using RTX_SSTHRESH policy for CMT.
3904f8829a4aSRandall Stewart 				 * If CMT is being used, then pick dest with
3905f8829a4aSRandall Stewart 				 * largest ssthresh for any retransmission.
3906f8829a4aSRandall Stewart 				 */
3907f8829a4aSRandall Stewart 				tp1->no_fr_allowed = 1;
3908f8829a4aSRandall Stewart 				alt = tp1->whoTo;
39093c503c28SRandall Stewart 				/* sa_ignore NO_NULL_CHK */
3910b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
3911b54d3a6cSRandall Stewart 					/*
3912b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If CMT PF is on,
3913b54d3a6cSRandall Stewart 					 * use the PF version of
3914b54d3a6cSRandall Stewart 					 * find_alt_net()
3915b54d3a6cSRandall Stewart 					 */
3916b54d3a6cSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 2);
3917b54d3a6cSRandall Stewart 				} else {
3918b54d3a6cSRandall Stewart 					/*
3919b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If only CMT is on,
3920b54d3a6cSRandall Stewart 					 * use the CMT version of
3921b54d3a6cSRandall Stewart 					 * find_alt_net()
3922b54d3a6cSRandall Stewart 					 */
392352be287eSRandall Stewart 					/* sa_ignore NO_NULL_CHK */
3924f8829a4aSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 1);
3925b54d3a6cSRandall Stewart 				}
3926ad81507eSRandall Stewart 				if (alt == NULL) {
3927ad81507eSRandall Stewart 					alt = tp1->whoTo;
3928ad81507eSRandall Stewart 				}
3929f8829a4aSRandall Stewart 				/*
3930f8829a4aSRandall Stewart 				 * CUCv2: If a different dest is picked for
3931f8829a4aSRandall Stewart 				 * the retransmission, then new
3932f8829a4aSRandall Stewart 				 * (rtx-)pseudo_cumack needs to be tracked
3933f8829a4aSRandall Stewart 				 * for orig dest. Let CUCv2 track new (rtx-)
3934f8829a4aSRandall Stewart 				 * pseudo-cumack always.
3935f8829a4aSRandall Stewart 				 */
3936ad81507eSRandall Stewart 				if (tp1->whoTo) {
3937f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
3938f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
3939ad81507eSRandall Stewart 				}
3940f8829a4aSRandall Stewart 			} else {/* CMT is OFF */
3941f8829a4aSRandall Stewart 
3942f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3943f8829a4aSRandall Stewart 				/* Can we find an alternate? */
3944f8829a4aSRandall Stewart 				alt = sctp_find_alternate_net(stcb, tp1->whoTo, 0);
3945f8829a4aSRandall Stewart #else
3946f8829a4aSRandall Stewart 				/*
3947f8829a4aSRandall Stewart 				 * default behavior is to NOT retransmit
3948f8829a4aSRandall Stewart 				 * FR's to an alternate. Armando Caro's
3949f8829a4aSRandall Stewart 				 * paper details why.
3950f8829a4aSRandall Stewart 				 */
3951f8829a4aSRandall Stewart 				alt = tp1->whoTo;
3952f8829a4aSRandall Stewart #endif
3953f8829a4aSRandall Stewart 			}
3954f8829a4aSRandall Stewart 
3955f8829a4aSRandall Stewart 			tp1->rec.data.doing_fast_retransmit = 1;
3956f8829a4aSRandall Stewart 			tot_retrans++;
3957f8829a4aSRandall Stewart 			/* mark the sending seq for possible subsequent FR's */
3958f8829a4aSRandall Stewart 			/*
3959f8829a4aSRandall Stewart 			 * printf("Marking TSN for FR new value %x\n",
3960f8829a4aSRandall Stewart 			 * (uint32_t)tpi->rec.data.TSN_seq);
3961f8829a4aSRandall Stewart 			 */
3962f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->send_queue)) {
3963f8829a4aSRandall Stewart 				/*
3964f8829a4aSRandall Stewart 				 * If the queue of send is empty then its
3965f8829a4aSRandall Stewart 				 * the next sequence number that will be
3966f8829a4aSRandall Stewart 				 * assigned so we subtract one from this to
3967f8829a4aSRandall Stewart 				 * get the one we last sent.
3968f8829a4aSRandall Stewart 				 */
3969f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn = sending_seq;
3970f8829a4aSRandall Stewart 			} else {
3971f8829a4aSRandall Stewart 				/*
3972f8829a4aSRandall Stewart 				 * If there are chunks on the send queue
3973f8829a4aSRandall Stewart 				 * (unsent data that has made it from the
3974f8829a4aSRandall Stewart 				 * stream queues but not out the door, we
3975f8829a4aSRandall Stewart 				 * take the first one (which will have the
3976f8829a4aSRandall Stewart 				 * lowest TSN) and subtract one to get the
3977f8829a4aSRandall Stewart 				 * one we last sent.
3978f8829a4aSRandall Stewart 				 */
3979f8829a4aSRandall Stewart 				struct sctp_tmit_chunk *ttt;
3980f8829a4aSRandall Stewart 
3981f8829a4aSRandall Stewart 				ttt = TAILQ_FIRST(&asoc->send_queue);
3982f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn =
3983f8829a4aSRandall Stewart 				    ttt->rec.data.TSN_seq;
3984f8829a4aSRandall Stewart 			}
3985f8829a4aSRandall Stewart 
3986f8829a4aSRandall Stewart 			if (tp1->do_rtt) {
3987f8829a4aSRandall Stewart 				/*
3988f8829a4aSRandall Stewart 				 * this guy had a RTO calculation pending on
3989f8829a4aSRandall Stewart 				 * it, cancel it
3990f8829a4aSRandall Stewart 				 */
3991f8829a4aSRandall Stewart 				tp1->do_rtt = 0;
3992f8829a4aSRandall Stewart 			}
3993f8829a4aSRandall Stewart 			if (alt != tp1->whoTo) {
3994f8829a4aSRandall Stewart 				/* yes, there is an alternate. */
3995f8829a4aSRandall Stewart 				sctp_free_remote_addr(tp1->whoTo);
39963c503c28SRandall Stewart 				/* sa_ignore FREED_MEMORY */
3997f8829a4aSRandall Stewart 				tp1->whoTo = alt;
3998f8829a4aSRandall Stewart 				atomic_add_int(&alt->ref_count, 1);
3999f8829a4aSRandall Stewart 			}
4000f8829a4aSRandall Stewart 		}
4001f8829a4aSRandall Stewart 		tp1 = TAILQ_NEXT(tp1, sctp_next);
4002f8829a4aSRandall Stewart 	}			/* while (tp1) */
4003f8829a4aSRandall Stewart 
4004f8829a4aSRandall Stewart 	if (tot_retrans > 0) {
4005f8829a4aSRandall Stewart 		/*
4006f8829a4aSRandall Stewart 		 * Setup the ecn nonce re-sync point. We do this since once
4007f8829a4aSRandall Stewart 		 * we go to FR something we introduce a Karn's rule scenario
4008f8829a4aSRandall Stewart 		 * and won't know the totals for the ECN bits.
4009f8829a4aSRandall Stewart 		 */
4010f8829a4aSRandall Stewart 		asoc->nonce_resync_tsn = sending_seq;
4011f8829a4aSRandall Stewart 		asoc->nonce_wait_for_ecne = 0;
4012f8829a4aSRandall Stewart 		asoc->nonce_sum_check = 0;
4013f8829a4aSRandall Stewart 	}
4014f8829a4aSRandall Stewart }
4015f8829a4aSRandall Stewart 
4016f8829a4aSRandall Stewart struct sctp_tmit_chunk *
4017f8829a4aSRandall Stewart sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
4018f8829a4aSRandall Stewart     struct sctp_association *asoc)
4019f8829a4aSRandall Stewart {
4020f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2, *a_adv = NULL;
4021f8829a4aSRandall Stewart 	struct timeval now;
4022f8829a4aSRandall Stewart 	int now_filled = 0;
4023f8829a4aSRandall Stewart 
4024f8829a4aSRandall Stewart 	if (asoc->peer_supports_prsctp == 0) {
4025f8829a4aSRandall Stewart 		return (NULL);
4026f8829a4aSRandall Stewart 	}
4027f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
4028f8829a4aSRandall Stewart 	while (tp1) {
4029f8829a4aSRandall Stewart 		if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
4030f8829a4aSRandall Stewart 		    tp1->sent != SCTP_DATAGRAM_RESEND) {
4031f8829a4aSRandall Stewart 			/* no chance to advance, out of here */
4032f8829a4aSRandall Stewart 			break;
4033f8829a4aSRandall Stewart 		}
40340c0982b8SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
40350c0982b8SRandall Stewart 			if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
40360c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
40370c0982b8SRandall Stewart 				    asoc->advanced_peer_ack_point,
40380c0982b8SRandall Stewart 				    tp1->rec.data.TSN_seq, 0, 0);
40390c0982b8SRandall Stewart 			}
40400c0982b8SRandall Stewart 		}
4041f8829a4aSRandall Stewart 		if (!PR_SCTP_ENABLED(tp1->flags)) {
4042f8829a4aSRandall Stewart 			/*
4043f8829a4aSRandall Stewart 			 * We can't fwd-tsn past any that are reliable aka
4044f8829a4aSRandall Stewart 			 * retransmitted until the asoc fails.
4045f8829a4aSRandall Stewart 			 */
4046f8829a4aSRandall Stewart 			break;
4047f8829a4aSRandall Stewart 		}
4048f8829a4aSRandall Stewart 		if (!now_filled) {
40496e55db54SRandall Stewart 			(void)SCTP_GETTIME_TIMEVAL(&now);
4050f8829a4aSRandall Stewart 			now_filled = 1;
4051f8829a4aSRandall Stewart 		}
4052f8829a4aSRandall Stewart 		tp2 = TAILQ_NEXT(tp1, sctp_next);
4053f8829a4aSRandall Stewart 		/*
4054f8829a4aSRandall Stewart 		 * now we got a chunk which is marked for another
4055f8829a4aSRandall Stewart 		 * retransmission to a PR-stream but has run out its chances
4056f8829a4aSRandall Stewart 		 * already maybe OR has been marked to skip now. Can we skip
4057f8829a4aSRandall Stewart 		 * it if its a resend?
4058f8829a4aSRandall Stewart 		 */
4059f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND &&
4060f8829a4aSRandall Stewart 		    (PR_SCTP_TTL_ENABLED(tp1->flags))) {
4061f8829a4aSRandall Stewart 			/*
4062f8829a4aSRandall Stewart 			 * Now is this one marked for resend and its time is
4063f8829a4aSRandall Stewart 			 * now up?
4064f8829a4aSRandall Stewart 			 */
4065f8829a4aSRandall Stewart 			if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
4066f8829a4aSRandall Stewart 				/* Yes so drop it */
4067f8829a4aSRandall Stewart 				if (tp1->data) {
4068ad81507eSRandall Stewart 					(void)sctp_release_pr_sctp_chunk(stcb, tp1,
4069f8829a4aSRandall Stewart 					    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
40700c0982b8SRandall Stewart 					    SCTP_SO_NOT_LOCKED);
4071f8829a4aSRandall Stewart 				}
4072f8829a4aSRandall Stewart 			} else {
4073f8829a4aSRandall Stewart 				/*
4074f8829a4aSRandall Stewart 				 * No, we are done when hit one for resend
4075f8829a4aSRandall Stewart 				 * whos time as not expired.
4076f8829a4aSRandall Stewart 				 */
4077f8829a4aSRandall Stewart 				break;
4078f8829a4aSRandall Stewart 			}
4079f8829a4aSRandall Stewart 		}
4080f8829a4aSRandall Stewart 		/*
4081f8829a4aSRandall Stewart 		 * Ok now if this chunk is marked to drop it we can clean up
4082f8829a4aSRandall Stewart 		 * the chunk, advance our peer ack point and we can check
4083f8829a4aSRandall Stewart 		 * the next chunk.
4084f8829a4aSRandall Stewart 		 */
4085f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
4086f8829a4aSRandall Stewart 			/* advance PeerAckPoint goes forward */
40870c0982b8SRandall Stewart 			if (compare_with_wrap(tp1->rec.data.TSN_seq,
40880c0982b8SRandall Stewart 			    asoc->advanced_peer_ack_point,
40890c0982b8SRandall Stewart 			    MAX_TSN)) {
40900c0982b8SRandall Stewart 
4091f8829a4aSRandall Stewart 				asoc->advanced_peer_ack_point = tp1->rec.data.TSN_seq;
4092f8829a4aSRandall Stewart 				a_adv = tp1;
40930c0982b8SRandall Stewart 			} else if (tp1->rec.data.TSN_seq == asoc->advanced_peer_ack_point) {
40940c0982b8SRandall Stewart 				/* No update but we do save the chk */
40950c0982b8SRandall Stewart 				a_adv = tp1;
40960c0982b8SRandall Stewart 			}
4097f8829a4aSRandall Stewart 		} else {
4098f8829a4aSRandall Stewart 			/*
4099f8829a4aSRandall Stewart 			 * If it is still in RESEND we can advance no
4100f8829a4aSRandall Stewart 			 * further
4101f8829a4aSRandall Stewart 			 */
4102f8829a4aSRandall Stewart 			break;
4103f8829a4aSRandall Stewart 		}
4104f8829a4aSRandall Stewart 		/*
4105f8829a4aSRandall Stewart 		 * If we hit here we just dumped tp1, move to next tsn on
4106f8829a4aSRandall Stewart 		 * sent queue.
4107f8829a4aSRandall Stewart 		 */
4108f8829a4aSRandall Stewart 		tp1 = tp2;
4109f8829a4aSRandall Stewart 	}
4110f8829a4aSRandall Stewart 	return (a_adv);
4111f8829a4aSRandall Stewart }
4112f8829a4aSRandall Stewart 
41130c0982b8SRandall Stewart static int
4114c105859eSRandall Stewart sctp_fs_audit(struct sctp_association *asoc)
4115bff64a4dSRandall Stewart {
4116bff64a4dSRandall Stewart 	struct sctp_tmit_chunk *chk;
4117bff64a4dSRandall Stewart 	int inflight = 0, resend = 0, inbetween = 0, acked = 0, above = 0;
41180c0982b8SRandall Stewart 	int entry_flight, entry_cnt, ret;
41190c0982b8SRandall Stewart 
41200c0982b8SRandall Stewart 	entry_flight = asoc->total_flight;
41210c0982b8SRandall Stewart 	entry_cnt = asoc->total_flight_count;
41220c0982b8SRandall Stewart 	ret = 0;
41230c0982b8SRandall Stewart 
41240c0982b8SRandall Stewart 	if (asoc->pr_sctp_cnt >= asoc->sent_queue_cnt)
41250c0982b8SRandall Stewart 		return (0);
4126bff64a4dSRandall Stewart 
4127bff64a4dSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
4128bff64a4dSRandall Stewart 		if (chk->sent < SCTP_DATAGRAM_RESEND) {
41290c0982b8SRandall Stewart 			printf("Chk TSN:%u size:%d inflight cnt:%d\n",
41300c0982b8SRandall Stewart 			    chk->rec.data.TSN_seq,
41310c0982b8SRandall Stewart 			    chk->send_size,
41320c0982b8SRandall Stewart 			    chk->snd_count
41330c0982b8SRandall Stewart 			    );
4134bff64a4dSRandall Stewart 			inflight++;
4135bff64a4dSRandall Stewart 		} else if (chk->sent == SCTP_DATAGRAM_RESEND) {
4136bff64a4dSRandall Stewart 			resend++;
4137bff64a4dSRandall Stewart 		} else if (chk->sent < SCTP_DATAGRAM_ACKED) {
4138bff64a4dSRandall Stewart 			inbetween++;
4139bff64a4dSRandall Stewart 		} else if (chk->sent > SCTP_DATAGRAM_ACKED) {
4140bff64a4dSRandall Stewart 			above++;
4141bff64a4dSRandall Stewart 		} else {
4142bff64a4dSRandall Stewart 			acked++;
4143bff64a4dSRandall Stewart 		}
4144bff64a4dSRandall Stewart 	}
4145f1f73e57SRandall Stewart 
4146c105859eSRandall Stewart 	if ((inflight > 0) || (inbetween > 0)) {
4147f1f73e57SRandall Stewart #ifdef INVARIANTS
4148c105859eSRandall Stewart 		panic("Flight size-express incorrect? \n");
4149f1f73e57SRandall Stewart #else
41500c0982b8SRandall Stewart 		printf("asoc->total_flight:%d cnt:%d\n",
41510c0982b8SRandall Stewart 		    entry_flight, entry_cnt);
41520c0982b8SRandall Stewart 
41530c0982b8SRandall Stewart 		SCTP_PRINTF("Flight size-express incorrect F:%d I:%d R:%d Ab:%d ACK:%d\n",
41540c0982b8SRandall Stewart 		    inflight, inbetween, resend, above, acked);
41550c0982b8SRandall Stewart 		ret = 1;
4156f1f73e57SRandall Stewart #endif
4157bff64a4dSRandall Stewart 	}
41580c0982b8SRandall Stewart 	return (ret);
4159c105859eSRandall Stewart }
4160c105859eSRandall Stewart 
4161c105859eSRandall Stewart 
4162c105859eSRandall Stewart static void
4163c105859eSRandall Stewart sctp_window_probe_recovery(struct sctp_tcb *stcb,
4164c105859eSRandall Stewart     struct sctp_association *asoc,
4165c105859eSRandall Stewart     struct sctp_nets *net,
4166c105859eSRandall Stewart     struct sctp_tmit_chunk *tp1)
4167c105859eSRandall Stewart {
4168dfb11ef8SRandall Stewart 	tp1->window_probe = 0;
41695171328bSRandall Stewart 	if ((tp1->sent >= SCTP_DATAGRAM_ACKED) || (tp1->data == NULL)) {
4170dfb11ef8SRandall Stewart 		/* TSN's skipped we do NOT move back. */
4171dfb11ef8SRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DWN_WP_FWD,
4172dfb11ef8SRandall Stewart 		    tp1->whoTo->flight_size,
4173dfb11ef8SRandall Stewart 		    tp1->book_size,
4174dfb11ef8SRandall Stewart 		    (uintptr_t) tp1->whoTo,
4175dfb11ef8SRandall Stewart 		    tp1->rec.data.TSN_seq);
4176dfb11ef8SRandall Stewart 		return;
4177dfb11ef8SRandall Stewart 	}
41785171328bSRandall Stewart 	/* First setup this by shrinking flight */
41795171328bSRandall Stewart 	sctp_flight_size_decrease(tp1);
41805171328bSRandall Stewart 	sctp_total_flight_decrease(stcb, tp1);
41815171328bSRandall Stewart 	/* Now mark for resend */
41825171328bSRandall Stewart 	tp1->sent = SCTP_DATAGRAM_RESEND;
41835171328bSRandall Stewart 	asoc->sent_queue_retran_cnt++;
4184b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4185c105859eSRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_WP,
4186c105859eSRandall Stewart 		    tp1->whoTo->flight_size,
4187c105859eSRandall Stewart 		    tp1->book_size,
4188c105859eSRandall Stewart 		    (uintptr_t) tp1->whoTo,
4189c105859eSRandall Stewart 		    tp1->rec.data.TSN_seq);
419080fefe0aSRandall Stewart 	}
4191c105859eSRandall Stewart }
4192c105859eSRandall Stewart 
4193f8829a4aSRandall Stewart void
4194f8829a4aSRandall Stewart sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack,
4195f8829a4aSRandall Stewart     uint32_t rwnd, int nonce_sum_flag, int *abort_now)
4196f8829a4aSRandall Stewart {
4197f8829a4aSRandall Stewart 	struct sctp_nets *net;
4198f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4199f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
42005e54f665SRandall Stewart 	uint32_t old_rwnd;
42015e54f665SRandall Stewart 	int win_probe_recovery = 0;
4202c105859eSRandall Stewart 	int win_probe_recovered = 0;
4203d06c82f1SRandall Stewart 	int j, done_once = 0;
4204f8829a4aSRandall Stewart 
4205b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
4206d06c82f1SRandall Stewart 		sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
4207d06c82f1SRandall Stewart 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
420880fefe0aSRandall Stewart 	}
4209f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
421018e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
421118e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
421218e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
421318e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
421418e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
421518e198d3SRandall Stewart 	}
421618e198d3SRandall Stewart #endif
4217f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
4218d06c82f1SRandall Stewart 	old_rwnd = asoc->peers_rwnd;
42195e54f665SRandall Stewart 	if (compare_with_wrap(asoc->last_acked_seq, cumack, MAX_TSN)) {
42205e54f665SRandall Stewart 		/* old ack */
42215e54f665SRandall Stewart 		return;
4222d06c82f1SRandall Stewart 	} else if (asoc->last_acked_seq == cumack) {
4223d06c82f1SRandall Stewart 		/* Window update sack */
4224d06c82f1SRandall Stewart 		asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
4225b3f1ea41SRandall Stewart 		    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4226d06c82f1SRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4227d06c82f1SRandall Stewart 			/* SWS sender side engages */
4228d06c82f1SRandall Stewart 			asoc->peers_rwnd = 0;
4229d06c82f1SRandall Stewart 		}
4230d06c82f1SRandall Stewart 		if (asoc->peers_rwnd > old_rwnd) {
4231d06c82f1SRandall Stewart 			goto again;
4232d06c82f1SRandall Stewart 		}
4233d06c82f1SRandall Stewart 		return;
42345e54f665SRandall Stewart 	}
4235f8829a4aSRandall Stewart 	/* First setup for CC stuff */
4236f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4237f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
4238f8829a4aSRandall Stewart 		net->net_ack = 0;
4239f8829a4aSRandall Stewart 		net->net_ack2 = 0;
4240132dea7dSRandall Stewart 
4241132dea7dSRandall Stewart 		/*
4242132dea7dSRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
4243132dea7dSRandall Stewart 		 * SACK processing
4244132dea7dSRandall Stewart 		 */
4245132dea7dSRandall Stewart 		net->new_pseudo_cumack = 0;
4246132dea7dSRandall Stewart 		net->will_exit_fast_recovery = 0;
4247f8829a4aSRandall Stewart 	}
4248b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4249139bc87fSRandall Stewart 		uint32_t send_s;
4250139bc87fSRandall Stewart 
4251c105859eSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4252c105859eSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
4253c105859eSRandall Stewart 			    sctpchunk_listhead);
4254c105859eSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
4255139bc87fSRandall Stewart 		} else {
4256c105859eSRandall Stewart 			send_s = asoc->sending_seq;
4257139bc87fSRandall Stewart 		}
4258139bc87fSRandall Stewart 		if ((cumack == send_s) ||
4259139bc87fSRandall Stewart 		    compare_with_wrap(cumack, send_s, MAX_TSN)) {
4260c105859eSRandall Stewart #ifndef INVARIANTS
4261139bc87fSRandall Stewart 			struct mbuf *oper;
4262139bc87fSRandall Stewart 
4263c105859eSRandall Stewart #endif
4264c105859eSRandall Stewart #ifdef INVARIANTS
4265c105859eSRandall Stewart 			panic("Impossible sack 1");
4266c105859eSRandall Stewart #else
4267139bc87fSRandall Stewart 			*abort_now = 1;
4268139bc87fSRandall Stewart 			/* XXX */
4269139bc87fSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4270139bc87fSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
4271139bc87fSRandall Stewart 			if (oper) {
4272139bc87fSRandall Stewart 				struct sctp_paramhdr *ph;
4273139bc87fSRandall Stewart 				uint32_t *ippp;
4274139bc87fSRandall Stewart 
4275139bc87fSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4276139bc87fSRandall Stewart 				    sizeof(uint32_t);
4277139bc87fSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
4278139bc87fSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4279139bc87fSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
4280139bc87fSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
4281139bc87fSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
4282139bc87fSRandall Stewart 			}
4283139bc87fSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4284ceaad40aSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
4285139bc87fSRandall Stewart 			return;
4286139bc87fSRandall Stewart #endif
4287139bc87fSRandall Stewart 		}
4288139bc87fSRandall Stewart 	}
4289f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = cumack;
4290b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4291c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4292c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
4293c4739e2fSRandall Stewart 		    0,
4294c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
4295c4739e2fSRandall Stewart 		    __LINE__);
4296c4739e2fSRandall Stewart 	}
4297f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
42985e54f665SRandall Stewart 	if (compare_with_wrap(cumack, asoc->last_acked_seq, MAX_TSN)) {
4299f8829a4aSRandall Stewart 		/* process the new consecutive TSN first */
4300f8829a4aSRandall Stewart 		tp1 = TAILQ_FIRST(&asoc->sent_queue);
4301f8829a4aSRandall Stewart 		while (tp1) {
4302f8829a4aSRandall Stewart 			tp2 = TAILQ_NEXT(tp1, sctp_next);
4303f8829a4aSRandall Stewart 			if (compare_with_wrap(cumack, tp1->rec.data.TSN_seq,
4304f8829a4aSRandall Stewart 			    MAX_TSN) ||
4305f8829a4aSRandall Stewart 			    cumack == tp1->rec.data.TSN_seq) {
430618e198d3SRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
430718e198d3SRandall Stewart 					printf("Warning, an unsent is now acked?\n");
430818e198d3SRandall Stewart 				}
4309f8829a4aSRandall Stewart 				/*
431018e198d3SRandall Stewart 				 * ECN Nonce: Add the nonce to the sender's
431118e198d3SRandall Stewart 				 * nonce sum
4312f8829a4aSRandall Stewart 				 */
4313f8829a4aSRandall Stewart 				asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
4314f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4315f8829a4aSRandall Stewart 					/*
431618e198d3SRandall Stewart 					 * If it is less than ACKED, it is
431718e198d3SRandall Stewart 					 * now no-longer in flight. Higher
431818e198d3SRandall Stewart 					 * values may occur during marking
4319f8829a4aSRandall Stewart 					 */
4320c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4321b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4322c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4323a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
4324a5d547adSRandall Stewart 							    tp1->book_size,
4325c105859eSRandall Stewart 							    (uintptr_t) tp1->whoTo,
4326a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
432780fefe0aSRandall Stewart 						}
4328c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
432904ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
4330c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
4331f8829a4aSRandall Stewart 					}
4332f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
4333f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
4334f8829a4aSRandall Stewart 						/*
433518e198d3SRandall Stewart 						 * True non-retransmited
4336f8829a4aSRandall Stewart 						 * chunk
4337f8829a4aSRandall Stewart 						 */
4338f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
4339f8829a4aSRandall Stewart 						    tp1->send_size;
4340f8829a4aSRandall Stewart 
4341f8829a4aSRandall Stewart 						/* update RTO too? */
434262c1ff9cSRandall Stewart 						if (tp1->do_rtt) {
4343f8829a4aSRandall Stewart 							tp1->whoTo->RTO =
434404ee05e8SRandall Stewart 							/*
434504ee05e8SRandall Stewart 							 * sa_ignore
434604ee05e8SRandall Stewart 							 * NO_NULL_CHK
434704ee05e8SRandall Stewart 							 */
4348f8829a4aSRandall Stewart 							    sctp_calculate_rto(stcb,
4349f8829a4aSRandall Stewart 							    asoc, tp1->whoTo,
435018e198d3SRandall Stewart 							    &tp1->sent_rcv_time,
435118e198d3SRandall Stewart 							    sctp_align_safe_nocopy);
4352f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
4353f8829a4aSRandall Stewart 						}
4354f8829a4aSRandall Stewart 					}
4355132dea7dSRandall Stewart 					/*
435618e198d3SRandall Stewart 					 * CMT: CUCv2 algorithm. From the
435718e198d3SRandall Stewart 					 * cumack'd TSNs, for each TSN being
435818e198d3SRandall Stewart 					 * acked for the first time, set the
435918e198d3SRandall Stewart 					 * following variables for the
436018e198d3SRandall Stewart 					 * corresp destination.
436118e198d3SRandall Stewart 					 * new_pseudo_cumack will trigger a
436218e198d3SRandall Stewart 					 * cwnd update.
436318e198d3SRandall Stewart 					 * find_(rtx_)pseudo_cumack will
436418e198d3SRandall Stewart 					 * trigger search for the next
436518e198d3SRandall Stewart 					 * expected (rtx-)pseudo-cumack.
4366132dea7dSRandall Stewart 					 */
4367132dea7dSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
4368132dea7dSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
4369132dea7dSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
4370132dea7dSRandall Stewart 
4371b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
437204ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
4373f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
437480fefe0aSRandall Stewart 					}
4375f8829a4aSRandall Stewart 				}
4376f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4377f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4378f8829a4aSRandall Stewart 				}
437942551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
438042551e99SRandall Stewart 					/* deflate the cwnd */
438142551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
438242551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
438342551e99SRandall Stewart 				}
4384f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
4385f8829a4aSRandall Stewart 				TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4386f8829a4aSRandall Stewart 				if (tp1->data) {
438704ee05e8SRandall Stewart 					/* sa_ignore NO_NULL_CHK */
4388f8829a4aSRandall Stewart 					sctp_free_bufspace(stcb, asoc, tp1, 1);
4389f8829a4aSRandall Stewart 					sctp_m_freem(tp1->data);
4390f8829a4aSRandall Stewart 				}
4391b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4392f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
4393f8829a4aSRandall Stewart 					    cumack,
4394f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
4395f8829a4aSRandall Stewart 					    0,
4396f8829a4aSRandall Stewart 					    0,
4397f8829a4aSRandall Stewart 					    SCTP_LOG_FREE_SENT);
439880fefe0aSRandall Stewart 				}
4399f8829a4aSRandall Stewart 				tp1->data = NULL;
4400f8829a4aSRandall Stewart 				asoc->sent_queue_cnt--;
4401f8829a4aSRandall Stewart 				sctp_free_a_chunk(stcb, tp1);
4402f8829a4aSRandall Stewart 				tp1 = tp2;
440318e198d3SRandall Stewart 			} else {
440418e198d3SRandall Stewart 				break;
4405f8829a4aSRandall Stewart 			}
44065e54f665SRandall Stewart 		}
440718e198d3SRandall Stewart 
440818e198d3SRandall Stewart 	}
440904ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
4410f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
4411ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4412ceaad40aSRandall Stewart 		struct socket *so;
4413ceaad40aSRandall Stewart 
4414ceaad40aSRandall Stewart #endif
4415ceaad40aSRandall Stewart 
4416f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4417b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
441804ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
4419f8829a4aSRandall Stewart 			sctp_wakeup_log(stcb, cumack, 1, SCTP_WAKESND_FROM_SACK);
442080fefe0aSRandall Stewart 		}
4421ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4422ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
4423ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
4424ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
4425ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
4426ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
4427ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4428ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4429ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
4430ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
4431ceaad40aSRandall Stewart 			return;
4432ceaad40aSRandall Stewart 		}
4433ceaad40aSRandall Stewart #endif
4434f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4435ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4436ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4437ceaad40aSRandall Stewart #endif
4438f8829a4aSRandall Stewart 	} else {
4439b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4440f8829a4aSRandall Stewart 			sctp_wakeup_log(stcb, cumack, 1, SCTP_NOWAKE_FROM_SACK);
444180fefe0aSRandall Stewart 		}
4442f8829a4aSRandall Stewart 	}
4443f8829a4aSRandall Stewart 
4444b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
4445f8829a4aSRandall Stewart 	if (asoc->last_acked_seq != cumack)
4446b54d3a6cSRandall Stewart 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
44475e54f665SRandall Stewart 
4448f8829a4aSRandall Stewart 	asoc->last_acked_seq = cumack;
44495e54f665SRandall Stewart 
4450f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
4451f8829a4aSRandall Stewart 		/* nothing left in-flight */
4452f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4453f8829a4aSRandall Stewart 			net->flight_size = 0;
4454f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4455f8829a4aSRandall Stewart 		}
4456f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4457f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4458f8829a4aSRandall Stewart 	}
4459f8829a4aSRandall Stewart 	/* ECN Nonce updates */
4460f8829a4aSRandall Stewart 	if (asoc->ecn_nonce_allowed) {
4461f8829a4aSRandall Stewart 		if (asoc->nonce_sum_check) {
4462f8829a4aSRandall Stewart 			if (nonce_sum_flag != ((asoc->nonce_sum_expect_base) & SCTP_SACK_NONCE_SUM)) {
4463f8829a4aSRandall Stewart 				if (asoc->nonce_wait_for_ecne == 0) {
4464f8829a4aSRandall Stewart 					struct sctp_tmit_chunk *lchk;
4465f8829a4aSRandall Stewart 
4466f8829a4aSRandall Stewart 					lchk = TAILQ_FIRST(&asoc->send_queue);
4467f8829a4aSRandall Stewart 					asoc->nonce_wait_for_ecne = 1;
4468f8829a4aSRandall Stewart 					if (lchk) {
4469f8829a4aSRandall Stewart 						asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
4470f8829a4aSRandall Stewart 					} else {
4471f8829a4aSRandall Stewart 						asoc->nonce_wait_tsn = asoc->sending_seq;
4472f8829a4aSRandall Stewart 					}
4473f8829a4aSRandall Stewart 				} else {
4474f8829a4aSRandall Stewart 					if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
4475f8829a4aSRandall Stewart 					    (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
4476f8829a4aSRandall Stewart 						/*
4477f8829a4aSRandall Stewart 						 * Misbehaving peer. We need
4478f8829a4aSRandall Stewart 						 * to react to this guy
4479f8829a4aSRandall Stewart 						 */
4480f8829a4aSRandall Stewart 						asoc->ecn_allowed = 0;
4481f8829a4aSRandall Stewart 						asoc->ecn_nonce_allowed = 0;
4482f8829a4aSRandall Stewart 					}
4483f8829a4aSRandall Stewart 				}
4484f8829a4aSRandall Stewart 			}
4485f8829a4aSRandall Stewart 		} else {
4486f8829a4aSRandall Stewart 			/* See if Resynchronization Possible */
4487f8829a4aSRandall Stewart 			if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
4488f8829a4aSRandall Stewart 				asoc->nonce_sum_check = 1;
4489f8829a4aSRandall Stewart 				/*
4490f8829a4aSRandall Stewart 				 * now we must calculate what the base is.
4491f8829a4aSRandall Stewart 				 * We do this based on two things, we know
4492f8829a4aSRandall Stewart 				 * the total's for all the segments
4493f8829a4aSRandall Stewart 				 * gap-acked in the SACK (none), We also
4494f8829a4aSRandall Stewart 				 * know the SACK's nonce sum, its in
4495f8829a4aSRandall Stewart 				 * nonce_sum_flag. So we can build a truth
4496f8829a4aSRandall Stewart 				 * table to back-calculate the new value of
4497f8829a4aSRandall Stewart 				 * asoc->nonce_sum_expect_base:
4498f8829a4aSRandall Stewart 				 *
4499f8829a4aSRandall Stewart 				 * SACK-flag-Value         Seg-Sums Base 0 0 0
4500f8829a4aSRandall Stewart 				 * 1                    0 1 0 1 1 1
4501f8829a4aSRandall Stewart 				 * 1 0
4502f8829a4aSRandall Stewart 				 */
4503f8829a4aSRandall Stewart 				asoc->nonce_sum_expect_base = (0 ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
4504f8829a4aSRandall Stewart 			}
4505f8829a4aSRandall Stewart 		}
4506f8829a4aSRandall Stewart 	}
4507f8829a4aSRandall Stewart 	/* RWND update */
4508f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
4509b3f1ea41SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4510f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4511f8829a4aSRandall Stewart 		/* SWS sender side engages */
4512f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
4513f8829a4aSRandall Stewart 	}
45145e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
45155e54f665SRandall Stewart 		win_probe_recovery = 1;
45165e54f665SRandall Stewart 	}
4517f8829a4aSRandall Stewart 	/* Now assure a timer where data is queued at */
4518a5d547adSRandall Stewart again:
4519a5d547adSRandall Stewart 	j = 0;
4520f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
45215171328bSRandall Stewart 		int to_ticks;
45225171328bSRandall Stewart 
45235e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
4524c105859eSRandall Stewart 			win_probe_recovered = 1;
45255e54f665SRandall Stewart 			/*
45265e54f665SRandall Stewart 			 * Find first chunk that was used with window probe
45275e54f665SRandall Stewart 			 * and clear the sent
45285e54f665SRandall Stewart 			 */
45293c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
45305e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
45315e54f665SRandall Stewart 				if (tp1->window_probe) {
4532c105859eSRandall Stewart 					sctp_window_probe_recovery(stcb, asoc, net, tp1);
45335e54f665SRandall Stewart 					break;
45345e54f665SRandall Stewart 				}
45355e54f665SRandall Stewart 			}
45365e54f665SRandall Stewart 		}
4537f8829a4aSRandall Stewart 		if (net->RTO == 0) {
4538f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
4539f8829a4aSRandall Stewart 		} else {
4540f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
4541f8829a4aSRandall Stewart 		}
45425171328bSRandall Stewart 		if (net->flight_size) {
4543a5d547adSRandall Stewart 			j++;
4544ad81507eSRandall Stewart 			(void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
4545f8829a4aSRandall Stewart 			    sctp_timeout_handler, &net->rxt_timer);
45465171328bSRandall Stewart 			if (net->window_probe) {
45475171328bSRandall Stewart 				net->window_probe = 0;
45485171328bSRandall Stewart 			}
4549f8829a4aSRandall Stewart 		} else {
45505171328bSRandall Stewart 			if (net->window_probe) {
45515171328bSRandall Stewart 				/*
45525171328bSRandall Stewart 				 * In window probes we must assure a timer
45535171328bSRandall Stewart 				 * is still running there
45545171328bSRandall Stewart 				 */
45555171328bSRandall Stewart 				net->window_probe = 0;
45565171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
45575171328bSRandall Stewart 					SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
45585171328bSRandall Stewart 					    sctp_timeout_handler, &net->rxt_timer);
45595171328bSRandall Stewart 				}
45605171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4561f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4562a5d547adSRandall Stewart 				    stcb, net,
4563a5d547adSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
4564f8829a4aSRandall Stewart 			}
4565b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
4566139bc87fSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4567f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
4568a5d547adSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4569a5d547adSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
4570f8829a4aSRandall Stewart 				}
4571f8829a4aSRandall Stewart 			}
4572f8829a4aSRandall Stewart 		}
4573f8829a4aSRandall Stewart 	}
4574bff64a4dSRandall Stewart 	if ((j == 0) &&
4575bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
4576bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
4577c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
4578bff64a4dSRandall Stewart 	    (done_once == 0)) {
45790c0982b8SRandall Stewart 		/*
45800c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
45810c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
45820c0982b8SRandall Stewart 		 */
45830c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
4584a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
45850c0982b8SRandall Stewart 				if (net->flight_size) {
4586a5d547adSRandall Stewart 					net->flight_size = 0;
4587a5d547adSRandall Stewart 				}
45880c0982b8SRandall Stewart 			}
4589a5d547adSRandall Stewart 			asoc->total_flight = 0;
4590a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
4591a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4592a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4593a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4594c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
4595c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
4596a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4597a5d547adSRandall Stewart 					asoc->sent_queue_retran_cnt++;
4598a5d547adSRandall Stewart 				}
4599a5d547adSRandall Stewart 			}
46000c0982b8SRandall Stewart 		}
4601bff64a4dSRandall Stewart 		done_once = 1;
4602a5d547adSRandall Stewart 		goto again;
4603a5d547adSRandall Stewart 	}
4604f8829a4aSRandall Stewart 	/**********************************/
4605f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
4606f8829a4aSRandall Stewart 	/**********************************/
4607f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4608f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
4609f8829a4aSRandall Stewart 		/* clean up */
4610f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
4611f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4612f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4613f8829a4aSRandall Stewart 		    (asoc->locked_on_sending)
4614f8829a4aSRandall Stewart 		    ) {
4615f8829a4aSRandall Stewart 			struct sctp_stream_queue_pending *sp;
4616f8829a4aSRandall Stewart 
4617f8829a4aSRandall Stewart 			/*
4618f8829a4aSRandall Stewart 			 * I may be in a state where we got all across.. but
4619f8829a4aSRandall Stewart 			 * cannot write more due to a shutdown... we abort
4620f8829a4aSRandall Stewart 			 * since the user did not indicate EOR in this case.
4621f8829a4aSRandall Stewart 			 * The sp will be cleaned during free of the asoc.
4622f8829a4aSRandall Stewart 			 */
4623f8829a4aSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4624f8829a4aSRandall Stewart 			    sctp_streamhead);
46252afb3e84SRandall Stewart 			if ((sp) && (sp->length == 0)) {
46262afb3e84SRandall Stewart 				/* Let cleanup code purge it */
46272afb3e84SRandall Stewart 				if (sp->msg_is_complete) {
46282afb3e84SRandall Stewart 					asoc->stream_queue_cnt--;
46292afb3e84SRandall Stewart 				} else {
4630f8829a4aSRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
4631f8829a4aSRandall Stewart 					asoc->locked_on_sending = NULL;
4632f8829a4aSRandall Stewart 					asoc->stream_queue_cnt--;
4633f8829a4aSRandall Stewart 				}
4634f8829a4aSRandall Stewart 			}
46352afb3e84SRandall Stewart 		}
4636f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4637f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4638f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4639f8829a4aSRandall Stewart 				/* Need to abort here */
4640f8829a4aSRandall Stewart 				struct mbuf *oper;
4641f8829a4aSRandall Stewart 
4642f8829a4aSRandall Stewart 		abort_out_now:
4643f8829a4aSRandall Stewart 				*abort_now = 1;
4644f8829a4aSRandall Stewart 				/* XXX */
4645f8829a4aSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4646f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
4647f8829a4aSRandall Stewart 				if (oper) {
4648f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
4649f8829a4aSRandall Stewart 					uint32_t *ippp;
4650f8829a4aSRandall Stewart 
4651139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4652f8829a4aSRandall Stewart 					    sizeof(uint32_t);
4653f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
4654f8829a4aSRandall Stewart 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4655139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
4656f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
4657a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_24);
4658f8829a4aSRandall Stewart 				}
4659a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
4660ceaad40aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
4661f8829a4aSRandall Stewart 			} else {
4662f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4663f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4664f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4665f42a358aSRandall Stewart 				}
4666c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4667b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4668f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
4669f8829a4aSRandall Stewart 				sctp_send_shutdown(stcb,
4670f8829a4aSRandall Stewart 				    stcb->asoc.primary_destination);
4671f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4672f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
4673f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4674f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
4675f8829a4aSRandall Stewart 			}
4676f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4677f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4678f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4679f8829a4aSRandall Stewart 				goto abort_out_now;
4680f8829a4aSRandall Stewart 			}
4681f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4682c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4683b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4684f8829a4aSRandall Stewart 			sctp_send_shutdown_ack(stcb,
4685f8829a4aSRandall Stewart 			    stcb->asoc.primary_destination);
4686f8829a4aSRandall Stewart 
4687f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4688f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb, asoc->primary_destination);
4689f8829a4aSRandall Stewart 		}
4690f8829a4aSRandall Stewart 	}
4691dfb11ef8SRandall Stewart 	/*********************************************/
4692dfb11ef8SRandall Stewart 	/* Here we perform PR-SCTP procedures        */
4693dfb11ef8SRandall Stewart 	/* (section 4.2)                             */
4694dfb11ef8SRandall Stewart 	/*********************************************/
4695dfb11ef8SRandall Stewart 	/* C1. update advancedPeerAckPoint */
4696dfb11ef8SRandall Stewart 	if (compare_with_wrap(cumack, asoc->advanced_peer_ack_point, MAX_TSN)) {
4697dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cumack;
4698dfb11ef8SRandall Stewart 	}
4699830d754dSRandall Stewart 	/* PR-Sctp issues need to be addressed too */
4700830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
4701830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
4702830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
4703830d754dSRandall Stewart 
4704830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
4705830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
4706830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
4707830d754dSRandall Stewart 		if (compare_with_wrap(asoc->advanced_peer_ack_point, cumack,
4708830d754dSRandall Stewart 		    MAX_TSN)) {
4709830d754dSRandall Stewart 			/*
4710830d754dSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing for notes
4711830d754dSRandall Stewart 			 * on issues that will occur when the ECN NONCE
4712830d754dSRandall Stewart 			 * stuff is put into SCTP for cross checking.
4713830d754dSRandall Stewart 			 */
4714830d754dSRandall Stewart 			if (compare_with_wrap(asoc->advanced_peer_ack_point, old_adv_peer_ack_point,
4715830d754dSRandall Stewart 			    MAX_TSN)) {
4716830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
4717830d754dSRandall Stewart 				/*
4718830d754dSRandall Stewart 				 * ECN Nonce: Disable Nonce Sum check when
4719830d754dSRandall Stewart 				 * FWD TSN is sent and store resync tsn
4720830d754dSRandall Stewart 				 */
4721830d754dSRandall Stewart 				asoc->nonce_sum_check = 0;
4722830d754dSRandall Stewart 				asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
47230c0982b8SRandall Stewart 			} else if (lchk) {
47240c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
47250c0982b8SRandall Stewart 				lchk->rec.data.fwd_tsn_cnt++;
47260c0982b8SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt > 3) {
47270c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
47280c0982b8SRandall Stewart 					lchk->rec.data.fwd_tsn_cnt = 0;
47290c0982b8SRandall Stewart 				}
4730830d754dSRandall Stewart 			}
4731830d754dSRandall Stewart 		}
4732830d754dSRandall Stewart 		if (lchk) {
4733830d754dSRandall Stewart 			/* Assure a timer is up */
4734830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4735830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
4736830d754dSRandall Stewart 		}
4737830d754dSRandall Stewart 	}
4738b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
4739f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4740f8829a4aSRandall Stewart 		    rwnd,
4741f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
4742f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
4743f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
474480fefe0aSRandall Stewart 	}
4745f8829a4aSRandall Stewart }
4746f8829a4aSRandall Stewart 
4747f8829a4aSRandall Stewart void
4748458303daSRandall Stewart sctp_handle_sack(struct mbuf *m, int offset,
4749458303daSRandall Stewart     struct sctp_sack_chunk *ch, struct sctp_tcb *stcb,
4750d06c82f1SRandall Stewart     struct sctp_nets *net_from, int *abort_now, int sack_len, uint32_t rwnd)
4751f8829a4aSRandall Stewart {
4752f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4753f8829a4aSRandall Stewart 	struct sctp_sack *sack;
4754f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
4755f8829a4aSRandall Stewart 	uint32_t cum_ack, last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked,
4756f8829a4aSRandall Stewart 	         this_sack_lowest_newack;
4757bff64a4dSRandall Stewart 	uint32_t sav_cum_ack;
4758f8829a4aSRandall Stewart 	uint16_t num_seg, num_dup;
4759f8829a4aSRandall Stewart 	uint16_t wake_him = 0;
4760f8829a4aSRandall Stewart 	unsigned int sack_length;
4761c105859eSRandall Stewart 	uint32_t send_s = 0;
4762f8829a4aSRandall Stewart 	long j;
4763f8829a4aSRandall Stewart 	int accum_moved = 0;
4764f8829a4aSRandall Stewart 	int will_exit_fast_recovery = 0;
47655e54f665SRandall Stewart 	uint32_t a_rwnd, old_rwnd;
47665e54f665SRandall Stewart 	int win_probe_recovery = 0;
4767c105859eSRandall Stewart 	int win_probe_recovered = 0;
4768f8829a4aSRandall Stewart 	struct sctp_nets *net = NULL;
4769f8829a4aSRandall Stewart 	int nonce_sum_flag, ecn_seg_sums = 0;
4770bff64a4dSRandall Stewart 	int done_once;
4771f8829a4aSRandall Stewart 	uint8_t reneged_all = 0;
4772f8829a4aSRandall Stewart 	uint8_t cmt_dac_flag;
4773f8829a4aSRandall Stewart 
4774f8829a4aSRandall Stewart 	/*
4775f8829a4aSRandall Stewart 	 * we take any chance we can to service our queues since we cannot
4776f8829a4aSRandall Stewart 	 * get awoken when the socket is read from :<
4777f8829a4aSRandall Stewart 	 */
4778f8829a4aSRandall Stewart 	/*
4779f8829a4aSRandall Stewart 	 * Now perform the actual SACK handling: 1) Verify that it is not an
4780f8829a4aSRandall Stewart 	 * old sack, if so discard. 2) If there is nothing left in the send
4781f8829a4aSRandall Stewart 	 * queue (cum-ack is equal to last acked) then you have a duplicate
4782f8829a4aSRandall Stewart 	 * too, update any rwnd change and verify no timers are running.
4783f8829a4aSRandall Stewart 	 * then return. 3) Process any new consequtive data i.e. cum-ack
4784f8829a4aSRandall Stewart 	 * moved process these first and note that it moved. 4) Process any
4785f8829a4aSRandall Stewart 	 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
4786f8829a4aSRandall Stewart 	 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
4787f8829a4aSRandall Stewart 	 * sync up flightsizes and things, stop all timers and also check
4788f8829a4aSRandall Stewart 	 * for shutdown_pending state. If so then go ahead and send off the
4789f8829a4aSRandall Stewart 	 * shutdown. If in shutdown recv, send off the shutdown-ack and
4790f8829a4aSRandall Stewart 	 * start that timer, Ret. 9) Strike any non-acked things and do FR
4791f8829a4aSRandall Stewart 	 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
4792f8829a4aSRandall Stewart 	 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
4793f8829a4aSRandall Stewart 	 * if in shutdown_recv state.
4794f8829a4aSRandall Stewart 	 */
4795f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
4796f8829a4aSRandall Stewart 	sack = &ch->sack;
4797f8829a4aSRandall Stewart 	/* CMT DAC algo */
4798f8829a4aSRandall Stewart 	this_sack_lowest_newack = 0;
4799f8829a4aSRandall Stewart 	j = 0;
4800d06c82f1SRandall Stewart 	sack_length = (unsigned int)sack_len;
4801f8829a4aSRandall Stewart 	/* ECN Nonce */
4802f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_slowpath_sack);
4803f8829a4aSRandall Stewart 	nonce_sum_flag = ch->ch.chunk_flags & SCTP_SACK_NONCE_SUM;
4804f8829a4aSRandall Stewart 	cum_ack = last_tsn = ntohl(sack->cum_tsn_ack);
480518e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
480618e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
480718e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
480818e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
480918e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
481018e198d3SRandall Stewart 	}
481118e198d3SRandall Stewart #endif
4812f8829a4aSRandall Stewart 	num_seg = ntohs(sack->num_gap_ack_blks);
4813d06c82f1SRandall Stewart 	a_rwnd = rwnd;
4814f8829a4aSRandall Stewart 
4815f8829a4aSRandall Stewart 	/* CMT DAC algo */
4816f8829a4aSRandall Stewart 	cmt_dac_flag = ch->ch.chunk_flags & SCTP_SACK_CMT_DAC;
4817f8829a4aSRandall Stewart 	num_dup = ntohs(sack->num_dup_tsns);
4818f8829a4aSRandall Stewart 
48195e54f665SRandall Stewart 	old_rwnd = stcb->asoc.peers_rwnd;
4820b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4821c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4822c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
4823c4739e2fSRandall Stewart 		    0,
4824c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
4825c4739e2fSRandall Stewart 		    __LINE__);
4826c4739e2fSRandall Stewart 	}
4827f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
4828f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
4829b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4830f8829a4aSRandall Stewart 		sctp_log_sack(asoc->last_acked_seq,
4831f8829a4aSRandall Stewart 		    cum_ack,
4832f8829a4aSRandall Stewart 		    0,
4833f8829a4aSRandall Stewart 		    num_seg,
4834f8829a4aSRandall Stewart 		    num_dup,
4835f8829a4aSRandall Stewart 		    SCTP_LOG_NEW_SACK);
483680fefe0aSRandall Stewart 	}
4837b3f1ea41SRandall Stewart 	if ((num_dup) && (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_FR_LOGGING_ENABLE | SCTP_EARLYFR_LOGGING_ENABLE))) {
4838f8829a4aSRandall Stewart 		int off_to_dup, iii;
4839458303daSRandall Stewart 		uint32_t *dupdata, dblock;
4840f8829a4aSRandall Stewart 
4841f8829a4aSRandall Stewart 		off_to_dup = (num_seg * sizeof(struct sctp_gap_ack_block)) + sizeof(struct sctp_sack_chunk);
4842f8829a4aSRandall Stewart 		if ((off_to_dup + (num_dup * sizeof(uint32_t))) <= sack_length) {
4843458303daSRandall Stewart 			dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
4844458303daSRandall Stewart 			    sizeof(uint32_t), (uint8_t *) & dblock);
4845458303daSRandall Stewart 			off_to_dup += sizeof(uint32_t);
4846458303daSRandall Stewart 			if (dupdata) {
4847f8829a4aSRandall Stewart 				for (iii = 0; iii < num_dup; iii++) {
4848f8829a4aSRandall Stewart 					sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
4849458303daSRandall Stewart 					dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
4850458303daSRandall Stewart 					    sizeof(uint32_t), (uint8_t *) & dblock);
4851458303daSRandall Stewart 					if (dupdata == NULL)
4852458303daSRandall Stewart 						break;
4853458303daSRandall Stewart 					off_to_dup += sizeof(uint32_t);
4854458303daSRandall Stewart 				}
4855f8829a4aSRandall Stewart 			}
4856f8829a4aSRandall Stewart 		} else {
4857ad81507eSRandall Stewart 			SCTP_PRINTF("Size invalid offset to dups:%d number dups:%d sack_len:%d num gaps:%d\n",
4858f8829a4aSRandall Stewart 			    off_to_dup, num_dup, sack_length, num_seg);
4859f8829a4aSRandall Stewart 		}
4860f8829a4aSRandall Stewart 	}
4861b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4862c105859eSRandall Stewart 		/* reality check */
4863c105859eSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4864c105859eSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
4865c105859eSRandall Stewart 			    sctpchunk_listhead);
4866c105859eSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
4867c105859eSRandall Stewart 		} else {
4868c105859eSRandall Stewart 			send_s = asoc->sending_seq;
4869c105859eSRandall Stewart 		}
4870f8829a4aSRandall Stewart 		if (cum_ack == send_s ||
4871f8829a4aSRandall Stewart 		    compare_with_wrap(cum_ack, send_s, MAX_TSN)) {
4872c105859eSRandall Stewart #ifndef INVARIANTS
4873c105859eSRandall Stewart 			struct mbuf *oper;
4874c105859eSRandall Stewart 
4875c105859eSRandall Stewart #endif
4876c105859eSRandall Stewart #ifdef INVARIANTS
4877139bc87fSRandall Stewart 	hopeless_peer:
4878139bc87fSRandall Stewart 			panic("Impossible sack 1");
4879139bc87fSRandall Stewart #else
4880c105859eSRandall Stewart 
4881f8829a4aSRandall Stewart 
4882f8829a4aSRandall Stewart 			/*
4883f8829a4aSRandall Stewart 			 * no way, we have not even sent this TSN out yet.
4884f8829a4aSRandall Stewart 			 * Peer is hopelessly messed up with us.
4885f8829a4aSRandall Stewart 			 */
4886f8829a4aSRandall Stewart 	hopeless_peer:
4887f8829a4aSRandall Stewart 			*abort_now = 1;
4888f8829a4aSRandall Stewart 			/* XXX */
4889f8829a4aSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4890f8829a4aSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
4891f8829a4aSRandall Stewart 			if (oper) {
4892f8829a4aSRandall Stewart 				struct sctp_paramhdr *ph;
4893f8829a4aSRandall Stewart 				uint32_t *ippp;
4894f8829a4aSRandall Stewart 
4895139bc87fSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4896f8829a4aSRandall Stewart 				    sizeof(uint32_t);
4897f8829a4aSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
4898f8829a4aSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4899139bc87fSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
4900f8829a4aSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
4901a5d547adSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
4902f8829a4aSRandall Stewart 			}
4903a5d547adSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4904ceaad40aSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
4905f8829a4aSRandall Stewart 			return;
4906139bc87fSRandall Stewart #endif
4907f8829a4aSRandall Stewart 		}
4908f8829a4aSRandall Stewart 	}
4909f8829a4aSRandall Stewart 	/**********************/
4910f8829a4aSRandall Stewart 	/* 1) check the range */
4911f8829a4aSRandall Stewart 	/**********************/
4912f8829a4aSRandall Stewart 	if (compare_with_wrap(asoc->last_acked_seq, last_tsn, MAX_TSN)) {
4913f8829a4aSRandall Stewart 		/* acking something behind */
4914f8829a4aSRandall Stewart 		return;
4915f8829a4aSRandall Stewart 	}
4916bff64a4dSRandall Stewart 	sav_cum_ack = asoc->last_acked_seq;
4917bff64a4dSRandall Stewart 
4918f8829a4aSRandall Stewart 	/* update the Rwnd of the peer */
4919f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue) &&
4920f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->send_queue) &&
4921f8829a4aSRandall Stewart 	    (asoc->stream_queue_cnt == 0)
4922f8829a4aSRandall Stewart 	    ) {
4923f8829a4aSRandall Stewart 		/* nothing left on send/sent and strmq */
4924b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4925f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4926f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
492780fefe0aSRandall Stewart 		}
4928f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
4929f8829a4aSRandall Stewart 		if (asoc->sent_queue_retran_cnt) {
4930f8829a4aSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4931f8829a4aSRandall Stewart 		}
4932f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4933f8829a4aSRandall Stewart 			/* SWS sender side engages */
4934f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
4935f8829a4aSRandall Stewart 		}
4936f8829a4aSRandall Stewart 		/* stop any timers */
4937f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4938f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4939a5d547adSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4940b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
4941139bc87fSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4942f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck1);
4943a5d547adSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4944a5d547adSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4945f8829a4aSRandall Stewart 				}
4946f8829a4aSRandall Stewart 			}
4947f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4948f8829a4aSRandall Stewart 			net->flight_size = 0;
4949f8829a4aSRandall Stewart 		}
4950f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4951f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4952f8829a4aSRandall Stewart 		return;
4953f8829a4aSRandall Stewart 	}
4954f8829a4aSRandall Stewart 	/*
4955f8829a4aSRandall Stewart 	 * We init netAckSz and netAckSz2 to 0. These are used to track 2
4956f8829a4aSRandall Stewart 	 * things. The total byte count acked is tracked in netAckSz AND
4957f8829a4aSRandall Stewart 	 * netAck2 is used to track the total bytes acked that are un-
4958f8829a4aSRandall Stewart 	 * amibguious and were never retransmitted. We track these on a per
4959f8829a4aSRandall Stewart 	 * destination address basis.
4960f8829a4aSRandall Stewart 	 */
4961f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4962f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
4963f8829a4aSRandall Stewart 		net->net_ack = 0;
4964f8829a4aSRandall Stewart 		net->net_ack2 = 0;
4965f8829a4aSRandall Stewart 
4966f8829a4aSRandall Stewart 		/*
496742551e99SRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
496842551e99SRandall Stewart 		 * SACK processing
4969f8829a4aSRandall Stewart 		 */
4970f8829a4aSRandall Stewart 		net->new_pseudo_cumack = 0;
4971f8829a4aSRandall Stewart 		net->will_exit_fast_recovery = 0;
4972f8829a4aSRandall Stewart 	}
4973f8829a4aSRandall Stewart 	/* process the new consecutive TSN first */
4974f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
4975f8829a4aSRandall Stewart 	while (tp1) {
4976f8829a4aSRandall Stewart 		if (compare_with_wrap(last_tsn, tp1->rec.data.TSN_seq,
4977f8829a4aSRandall Stewart 		    MAX_TSN) ||
4978f8829a4aSRandall Stewart 		    last_tsn == tp1->rec.data.TSN_seq) {
4979f8829a4aSRandall Stewart 			if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4980f8829a4aSRandall Stewart 				/*
4981f8829a4aSRandall Stewart 				 * ECN Nonce: Add the nonce to the sender's
4982f8829a4aSRandall Stewart 				 * nonce sum
4983f8829a4aSRandall Stewart 				 */
4984f8829a4aSRandall Stewart 				asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
4985f8829a4aSRandall Stewart 				accum_moved = 1;
4986f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4987f8829a4aSRandall Stewart 					/*
4988f8829a4aSRandall Stewart 					 * If it is less than ACKED, it is
4989f8829a4aSRandall Stewart 					 * now no-longer in flight. Higher
4990f8829a4aSRandall Stewart 					 * values may occur during marking
4991f8829a4aSRandall Stewart 					 */
4992f8829a4aSRandall Stewart 					if ((tp1->whoTo->dest_state &
4993f8829a4aSRandall Stewart 					    SCTP_ADDR_UNCONFIRMED) &&
4994f8829a4aSRandall Stewart 					    (tp1->snd_count < 2)) {
4995f8829a4aSRandall Stewart 						/*
4996f8829a4aSRandall Stewart 						 * If there was no retran
4997f8829a4aSRandall Stewart 						 * and the address is
4998f8829a4aSRandall Stewart 						 * un-confirmed and we sent
4999f8829a4aSRandall Stewart 						 * there and are now
5000f8829a4aSRandall Stewart 						 * sacked.. its confirmed,
5001f8829a4aSRandall Stewart 						 * mark it so.
5002f8829a4aSRandall Stewart 						 */
5003f8829a4aSRandall Stewart 						tp1->whoTo->dest_state &=
5004f8829a4aSRandall Stewart 						    ~SCTP_ADDR_UNCONFIRMED;
5005f8829a4aSRandall Stewart 					}
5006c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5007b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
5008c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
5009a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
5010a5d547adSRandall Stewart 							    tp1->book_size,
5011c105859eSRandall Stewart 							    (uintptr_t) tp1->whoTo,
5012a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
501380fefe0aSRandall Stewart 						}
5014c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
5015c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
5016f8829a4aSRandall Stewart 					}
5017f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
5018f8829a4aSRandall Stewart 
5019f8829a4aSRandall Stewart 					/* CMT SFR and DAC algos */
5020f8829a4aSRandall Stewart 					this_sack_lowest_newack = tp1->rec.data.TSN_seq;
5021f8829a4aSRandall Stewart 					tp1->whoTo->saw_newack = 1;
5022f8829a4aSRandall Stewart 
5023f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
5024f8829a4aSRandall Stewart 						/*
5025f8829a4aSRandall Stewart 						 * True non-retransmited
5026f8829a4aSRandall Stewart 						 * chunk
5027f8829a4aSRandall Stewart 						 */
5028f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
5029f8829a4aSRandall Stewart 						    tp1->send_size;
5030f8829a4aSRandall Stewart 
5031f8829a4aSRandall Stewart 						/* update RTO too? */
5032f8829a4aSRandall Stewart 						if (tp1->do_rtt) {
5033f8829a4aSRandall Stewart 							tp1->whoTo->RTO =
5034f8829a4aSRandall Stewart 							    sctp_calculate_rto(stcb,
5035f8829a4aSRandall Stewart 							    asoc, tp1->whoTo,
503618e198d3SRandall Stewart 							    &tp1->sent_rcv_time,
503718e198d3SRandall Stewart 							    sctp_align_safe_nocopy);
5038f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
5039f8829a4aSRandall Stewart 						}
5040f8829a4aSRandall Stewart 					}
5041f8829a4aSRandall Stewart 					/*
5042f8829a4aSRandall Stewart 					 * CMT: CUCv2 algorithm. From the
5043f8829a4aSRandall Stewart 					 * cumack'd TSNs, for each TSN being
5044f8829a4aSRandall Stewart 					 * acked for the first time, set the
5045f8829a4aSRandall Stewart 					 * following variables for the
5046f8829a4aSRandall Stewart 					 * corresp destination.
5047f8829a4aSRandall Stewart 					 * new_pseudo_cumack will trigger a
5048f8829a4aSRandall Stewart 					 * cwnd update.
5049f8829a4aSRandall Stewart 					 * find_(rtx_)pseudo_cumack will
5050f8829a4aSRandall Stewart 					 * trigger search for the next
5051f8829a4aSRandall Stewart 					 * expected (rtx-)pseudo-cumack.
5052f8829a4aSRandall Stewart 					 */
5053f8829a4aSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
5054f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
5055f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
5056f8829a4aSRandall Stewart 
5057f8829a4aSRandall Stewart 
5058b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
5059f8829a4aSRandall Stewart 						sctp_log_sack(asoc->last_acked_seq,
5060f8829a4aSRandall Stewart 						    cum_ack,
5061f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
5062f8829a4aSRandall Stewart 						    0,
5063f8829a4aSRandall Stewart 						    0,
5064f8829a4aSRandall Stewart 						    SCTP_LOG_TSN_ACKED);
506580fefe0aSRandall Stewart 					}
5066b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
5067f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
506880fefe0aSRandall Stewart 					}
5069f8829a4aSRandall Stewart 				}
5070f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5071f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
5072f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
5073f8829a4aSRandall Stewart 					sctp_audit_log(0xB3,
5074f8829a4aSRandall Stewart 					    (asoc->sent_queue_retran_cnt & 0x000000ff));
5075f8829a4aSRandall Stewart #endif
5076f8829a4aSRandall Stewart 				}
507742551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
507842551e99SRandall Stewart 					/* deflate the cwnd */
507942551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
508042551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
508142551e99SRandall Stewart 				}
5082f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
5083f8829a4aSRandall Stewart 			}
5084f8829a4aSRandall Stewart 		} else {
5085f8829a4aSRandall Stewart 			break;
5086f8829a4aSRandall Stewart 		}
5087f8829a4aSRandall Stewart 		tp1 = TAILQ_NEXT(tp1, sctp_next);
5088f8829a4aSRandall Stewart 	}
5089f8829a4aSRandall Stewart 	biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
5090f8829a4aSRandall Stewart 	/* always set this up to cum-ack */
5091f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = last_tsn;
5092f8829a4aSRandall Stewart 
5093458303daSRandall Stewart 	/* Move offset up to point to gaps/dups */
5094458303daSRandall Stewart 	offset += sizeof(struct sctp_sack_chunk);
5095f8829a4aSRandall Stewart 	if (((num_seg * (sizeof(struct sctp_gap_ack_block))) + sizeof(struct sctp_sack_chunk)) > sack_length) {
5096f8829a4aSRandall Stewart 
5097f8829a4aSRandall Stewart 		/* skip corrupt segments */
5098f8829a4aSRandall Stewart 		goto skip_segments;
5099f8829a4aSRandall Stewart 	}
5100f8829a4aSRandall Stewart 	if (num_seg > 0) {
5101f8829a4aSRandall Stewart 
5102f8829a4aSRandall Stewart 		/*
5103f8829a4aSRandall Stewart 		 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
5104f8829a4aSRandall Stewart 		 * to be greater than the cumack. Also reset saw_newack to 0
5105f8829a4aSRandall Stewart 		 * for all dests.
5106f8829a4aSRandall Stewart 		 */
5107f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5108f8829a4aSRandall Stewart 			net->saw_newack = 0;
5109f8829a4aSRandall Stewart 			net->this_sack_highest_newack = last_tsn;
5110f8829a4aSRandall Stewart 		}
5111f8829a4aSRandall Stewart 
5112f8829a4aSRandall Stewart 		/*
5113f8829a4aSRandall Stewart 		 * thisSackHighestGap will increase while handling NEW
5114f8829a4aSRandall Stewart 		 * segments this_sack_highest_newack will increase while
5115f8829a4aSRandall Stewart 		 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
5116f8829a4aSRandall Stewart 		 * used for CMT DAC algo. saw_newack will also change.
5117f8829a4aSRandall Stewart 		 */
5118458303daSRandall Stewart 		sctp_handle_segments(m, &offset, stcb, asoc, ch, last_tsn,
5119f8829a4aSRandall Stewart 		    &biggest_tsn_acked, &biggest_tsn_newly_acked, &this_sack_lowest_newack,
5120f8829a4aSRandall Stewart 		    num_seg, &ecn_seg_sums);
5121f8829a4aSRandall Stewart 
5122b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
5123f8829a4aSRandall Stewart 			/*
5124f8829a4aSRandall Stewart 			 * validate the biggest_tsn_acked in the gap acks if
5125f8829a4aSRandall Stewart 			 * strict adherence is wanted.
5126f8829a4aSRandall Stewart 			 */
5127f8829a4aSRandall Stewart 			if ((biggest_tsn_acked == send_s) ||
5128f8829a4aSRandall Stewart 			    (compare_with_wrap(biggest_tsn_acked, send_s, MAX_TSN))) {
5129f8829a4aSRandall Stewart 				/*
5130f8829a4aSRandall Stewart 				 * peer is either confused or we are under
5131f8829a4aSRandall Stewart 				 * attack. We must abort.
5132f8829a4aSRandall Stewart 				 */
5133f8829a4aSRandall Stewart 				goto hopeless_peer;
5134f8829a4aSRandall Stewart 			}
5135f8829a4aSRandall Stewart 		}
5136f8829a4aSRandall Stewart 	}
5137f8829a4aSRandall Stewart skip_segments:
5138f8829a4aSRandall Stewart 	/*******************************************/
5139f8829a4aSRandall Stewart 	/* cancel ALL T3-send timer if accum moved */
5140f8829a4aSRandall Stewart 	/*******************************************/
5141b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
5142f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5143f8829a4aSRandall Stewart 			if (net->new_pseudo_cumack)
5144f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5145a5d547adSRandall Stewart 				    stcb, net,
5146a5d547adSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
5147f8829a4aSRandall Stewart 
5148f8829a4aSRandall Stewart 		}
5149f8829a4aSRandall Stewart 	} else {
5150f8829a4aSRandall Stewart 		if (accum_moved) {
5151f8829a4aSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5152f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5153a5d547adSRandall Stewart 				    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
5154f8829a4aSRandall Stewart 			}
5155f8829a4aSRandall Stewart 		}
5156f8829a4aSRandall Stewart 	}
5157f8829a4aSRandall Stewart 	/********************************************/
5158f8829a4aSRandall Stewart 	/* drop the acked chunks from the sendqueue */
5159f8829a4aSRandall Stewart 	/********************************************/
5160f8829a4aSRandall Stewart 	asoc->last_acked_seq = cum_ack;
5161f8829a4aSRandall Stewart 
5162f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
5163f8829a4aSRandall Stewart 	if (tp1 == NULL)
5164f8829a4aSRandall Stewart 		goto done_with_it;
5165f8829a4aSRandall Stewart 	do {
5166f8829a4aSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq, cum_ack,
5167f8829a4aSRandall Stewart 		    MAX_TSN)) {
5168f8829a4aSRandall Stewart 			break;
5169f8829a4aSRandall Stewart 		}
5170f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
5171f8829a4aSRandall Stewart 			/* no more sent on list */
517218e198d3SRandall Stewart 			printf("Warning, tp1->sent == %d and its now acked?\n",
517318e198d3SRandall Stewart 			    tp1->sent);
5174f8829a4aSRandall Stewart 		}
5175f8829a4aSRandall Stewart 		tp2 = TAILQ_NEXT(tp1, sctp_next);
5176f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
5177f8829a4aSRandall Stewart 		if (tp1->pr_sctp_on) {
5178f8829a4aSRandall Stewart 			if (asoc->pr_sctp_cnt != 0)
5179f8829a4aSRandall Stewart 				asoc->pr_sctp_cnt--;
5180f8829a4aSRandall Stewart 		}
5181f8829a4aSRandall Stewart 		if ((TAILQ_FIRST(&asoc->sent_queue) == NULL) &&
5182f8829a4aSRandall Stewart 		    (asoc->total_flight > 0)) {
5183f1f73e57SRandall Stewart #ifdef INVARIANTS
5184c105859eSRandall Stewart 			panic("Warning flight size is postive and should be 0");
5185f1f73e57SRandall Stewart #else
5186ad81507eSRandall Stewart 			SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
5187f8829a4aSRandall Stewart 			    asoc->total_flight);
5188f1f73e57SRandall Stewart #endif
5189f8829a4aSRandall Stewart 			asoc->total_flight = 0;
5190f8829a4aSRandall Stewart 		}
5191f8829a4aSRandall Stewart 		if (tp1->data) {
519204ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
5193f8829a4aSRandall Stewart 			sctp_free_bufspace(stcb, asoc, tp1, 1);
5194f8829a4aSRandall Stewart 			sctp_m_freem(tp1->data);
5195f8829a4aSRandall Stewart 			if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
5196f8829a4aSRandall Stewart 				asoc->sent_queue_cnt_removeable--;
5197f8829a4aSRandall Stewart 			}
5198f8829a4aSRandall Stewart 		}
5199b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
5200f8829a4aSRandall Stewart 			sctp_log_sack(asoc->last_acked_seq,
5201f8829a4aSRandall Stewart 			    cum_ack,
5202f8829a4aSRandall Stewart 			    tp1->rec.data.TSN_seq,
5203f8829a4aSRandall Stewart 			    0,
5204f8829a4aSRandall Stewart 			    0,
5205f8829a4aSRandall Stewart 			    SCTP_LOG_FREE_SENT);
520680fefe0aSRandall Stewart 		}
5207f8829a4aSRandall Stewart 		tp1->data = NULL;
5208f8829a4aSRandall Stewart 		asoc->sent_queue_cnt--;
5209f8829a4aSRandall Stewart 		sctp_free_a_chunk(stcb, tp1);
5210f8829a4aSRandall Stewart 		wake_him++;
5211f8829a4aSRandall Stewart 		tp1 = tp2;
5212f8829a4aSRandall Stewart 	} while (tp1 != NULL);
5213f8829a4aSRandall Stewart 
5214f8829a4aSRandall Stewart done_with_it:
521504ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
5216f8829a4aSRandall Stewart 	if ((wake_him) && (stcb->sctp_socket)) {
5217ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5218ceaad40aSRandall Stewart 		struct socket *so;
5219ceaad40aSRandall Stewart 
5220ceaad40aSRandall Stewart #endif
5221f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
5222b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
5223f8829a4aSRandall Stewart 			sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_WAKESND_FROM_SACK);
522480fefe0aSRandall Stewart 		}
5225ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5226ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
5227ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
5228ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
5229ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
5230ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
5231ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
5232ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
5233ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
5234ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
5235ceaad40aSRandall Stewart 			return;
5236ceaad40aSRandall Stewart 		}
5237ceaad40aSRandall Stewart #endif
5238f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
5239ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5240ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
5241ceaad40aSRandall Stewart #endif
5242f8829a4aSRandall Stewart 	} else {
5243b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
5244f8829a4aSRandall Stewart 			sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_NOWAKE_FROM_SACK);
524580fefe0aSRandall Stewart 		}
5246f8829a4aSRandall Stewart 	}
5247f8829a4aSRandall Stewart 
524842551e99SRandall Stewart 	if (asoc->fast_retran_loss_recovery && accum_moved) {
5249f8829a4aSRandall Stewart 		if (compare_with_wrap(asoc->last_acked_seq,
5250f8829a4aSRandall Stewart 		    asoc->fast_recovery_tsn, MAX_TSN) ||
5251f8829a4aSRandall Stewart 		    asoc->last_acked_seq == asoc->fast_recovery_tsn) {
5252f8829a4aSRandall Stewart 			/* Setup so we will exit RFC2582 fast recovery */
5253f8829a4aSRandall Stewart 			will_exit_fast_recovery = 1;
5254f8829a4aSRandall Stewart 		}
5255f8829a4aSRandall Stewart 	}
5256f8829a4aSRandall Stewart 	/*
5257f8829a4aSRandall Stewart 	 * Check for revoked fragments:
5258f8829a4aSRandall Stewart 	 *
5259f8829a4aSRandall Stewart 	 * if Previous sack - Had no frags then we can't have any revoked if
5260f8829a4aSRandall Stewart 	 * Previous sack - Had frag's then - If we now have frags aka
5261f8829a4aSRandall Stewart 	 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
5262f8829a4aSRandall Stewart 	 * some of them. else - The peer revoked all ACKED fragments, since
5263f8829a4aSRandall Stewart 	 * we had some before and now we have NONE.
5264f8829a4aSRandall Stewart 	 */
5265f8829a4aSRandall Stewart 
5266f42a358aSRandall Stewart 	if (num_seg)
5267c105859eSRandall Stewart 		sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
5268f8829a4aSRandall Stewart 	else if (asoc->saw_sack_with_frags) {
5269f8829a4aSRandall Stewart 		int cnt_revoked = 0;
5270f8829a4aSRandall Stewart 
5271f8829a4aSRandall Stewart 		tp1 = TAILQ_FIRST(&asoc->sent_queue);
5272f8829a4aSRandall Stewart 		if (tp1 != NULL) {
5273f8829a4aSRandall Stewart 			/* Peer revoked all dg's marked or acked */
5274f8829a4aSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5275f8829a4aSRandall Stewart 				if ((tp1->sent > SCTP_DATAGRAM_RESEND) &&
5276f8829a4aSRandall Stewart 				    (tp1->sent < SCTP_FORWARD_TSN_SKIP)) {
5277f8829a4aSRandall Stewart 					tp1->sent = SCTP_DATAGRAM_SENT;
5278b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
5279c105859eSRandall Stewart 						sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
5280c105859eSRandall Stewart 						    tp1->whoTo->flight_size,
5281c105859eSRandall Stewart 						    tp1->book_size,
5282c105859eSRandall Stewart 						    (uintptr_t) tp1->whoTo,
5283c105859eSRandall Stewart 						    tp1->rec.data.TSN_seq);
528480fefe0aSRandall Stewart 					}
5285c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
5286c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
5287a5d547adSRandall Stewart 					tp1->rec.data.chunk_was_revoked = 1;
528842551e99SRandall Stewart 					/*
528942551e99SRandall Stewart 					 * To ensure that this increase in
529042551e99SRandall Stewart 					 * flightsize, which is artificial,
529142551e99SRandall Stewart 					 * does not throttle the sender, we
529242551e99SRandall Stewart 					 * also increase the cwnd
529342551e99SRandall Stewart 					 * artificially.
529442551e99SRandall Stewart 					 */
529542551e99SRandall Stewart 					tp1->whoTo->cwnd += tp1->book_size;
5296f8829a4aSRandall Stewart 					cnt_revoked++;
5297f8829a4aSRandall Stewart 				}
5298f8829a4aSRandall Stewart 			}
5299f8829a4aSRandall Stewart 			if (cnt_revoked) {
5300f8829a4aSRandall Stewart 				reneged_all = 1;
5301f8829a4aSRandall Stewart 			}
5302f8829a4aSRandall Stewart 		}
5303f8829a4aSRandall Stewart 		asoc->saw_sack_with_frags = 0;
5304f8829a4aSRandall Stewart 	}
5305f8829a4aSRandall Stewart 	if (num_seg)
5306f8829a4aSRandall Stewart 		asoc->saw_sack_with_frags = 1;
5307f8829a4aSRandall Stewart 	else
5308f8829a4aSRandall Stewart 		asoc->saw_sack_with_frags = 0;
5309f8829a4aSRandall Stewart 
5310b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
5311b54d3a6cSRandall Stewart 	asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
5312f8829a4aSRandall Stewart 
5313f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
5314f8829a4aSRandall Stewart 		/* nothing left in-flight */
5315f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5316f8829a4aSRandall Stewart 			/* stop all timers */
5317b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
5318139bc87fSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
5319f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
5320a5d547adSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
5321a5d547adSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_29);
5322f8829a4aSRandall Stewart 				}
5323f8829a4aSRandall Stewart 			}
5324f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5325a5d547adSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
5326f8829a4aSRandall Stewart 			net->flight_size = 0;
5327f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
5328f8829a4aSRandall Stewart 		}
5329f8829a4aSRandall Stewart 		asoc->total_flight = 0;
5330f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
5331f8829a4aSRandall Stewart 	}
5332f8829a4aSRandall Stewart 	/**********************************/
5333f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
5334f8829a4aSRandall Stewart 	/**********************************/
5335f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
5336f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
5337b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
5338f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
5339f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
534080fefe0aSRandall Stewart 		}
5341f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
5342f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5343f8829a4aSRandall Stewart 			/* SWS sender side engages */
5344f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
5345f8829a4aSRandall Stewart 		}
5346f8829a4aSRandall Stewart 		/* clean up */
5347f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
5348f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
5349f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
5350f8829a4aSRandall Stewart 		    (asoc->locked_on_sending)
5351f8829a4aSRandall Stewart 		    ) {
5352f8829a4aSRandall Stewart 			struct sctp_stream_queue_pending *sp;
5353f8829a4aSRandall Stewart 
5354f8829a4aSRandall Stewart 			/*
5355f8829a4aSRandall Stewart 			 * I may be in a state where we got all across.. but
5356f8829a4aSRandall Stewart 			 * cannot write more due to a shutdown... we abort
5357f8829a4aSRandall Stewart 			 * since the user did not indicate EOR in this case.
5358f8829a4aSRandall Stewart 			 */
5359f8829a4aSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
5360f8829a4aSRandall Stewart 			    sctp_streamhead);
53612afb3e84SRandall Stewart 			if ((sp) && (sp->length == 0)) {
5362f8829a4aSRandall Stewart 				asoc->locked_on_sending = NULL;
53632afb3e84SRandall Stewart 				if (sp->msg_is_complete) {
5364f8829a4aSRandall Stewart 					asoc->stream_queue_cnt--;
53652afb3e84SRandall Stewart 				} else {
53662afb3e84SRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
53672afb3e84SRandall Stewart 					asoc->stream_queue_cnt--;
53682afb3e84SRandall Stewart 				}
5369f8829a4aSRandall Stewart 			}
5370f8829a4aSRandall Stewart 		}
5371f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
5372f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
5373f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
5374f8829a4aSRandall Stewart 				/* Need to abort here */
5375f8829a4aSRandall Stewart 				struct mbuf *oper;
5376f8829a4aSRandall Stewart 
5377f8829a4aSRandall Stewart 		abort_out_now:
5378f8829a4aSRandall Stewart 				*abort_now = 1;
5379f8829a4aSRandall Stewart 				/* XXX */
5380f8829a4aSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
5381f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
5382f8829a4aSRandall Stewart 				if (oper) {
5383f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
5384f8829a4aSRandall Stewart 					uint32_t *ippp;
5385f8829a4aSRandall Stewart 
5386139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
5387f8829a4aSRandall Stewart 					    sizeof(uint32_t);
5388f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
5389f8829a4aSRandall Stewart 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
5390139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
5391f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
5392a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_31);
5393f8829a4aSRandall Stewart 				}
5394a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
5395ceaad40aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
5396f8829a4aSRandall Stewart 				return;
5397f8829a4aSRandall Stewart 			} else {
5398f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
5399f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
5400f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5401f42a358aSRandall Stewart 				}
5402c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
5403b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
5404f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
5405f8829a4aSRandall Stewart 				sctp_send_shutdown(stcb,
5406f8829a4aSRandall Stewart 				    stcb->asoc.primary_destination);
5407f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
5408f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
5409f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
5410f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
5411f8829a4aSRandall Stewart 			}
5412f8829a4aSRandall Stewart 			return;
5413f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
5414f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
5415f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
5416f8829a4aSRandall Stewart 				goto abort_out_now;
5417f8829a4aSRandall Stewart 			}
5418f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5419c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
5420b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
5421f8829a4aSRandall Stewart 			sctp_send_shutdown_ack(stcb,
5422f8829a4aSRandall Stewart 			    stcb->asoc.primary_destination);
5423f8829a4aSRandall Stewart 
5424f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
5425f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb, asoc->primary_destination);
5426f8829a4aSRandall Stewart 			return;
5427f8829a4aSRandall Stewart 		}
5428f8829a4aSRandall Stewart 	}
5429f8829a4aSRandall Stewart 	/*
5430f8829a4aSRandall Stewart 	 * Now here we are going to recycle net_ack for a different use...
5431f8829a4aSRandall Stewart 	 * HEADS UP.
5432f8829a4aSRandall Stewart 	 */
5433f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5434f8829a4aSRandall Stewart 		net->net_ack = 0;
5435f8829a4aSRandall Stewart 	}
5436f8829a4aSRandall Stewart 
5437f8829a4aSRandall Stewart 	/*
5438f8829a4aSRandall Stewart 	 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
5439f8829a4aSRandall Stewart 	 * to be done. Setting this_sack_lowest_newack to the cum_ack will
5440f8829a4aSRandall Stewart 	 * automatically ensure that.
5441f8829a4aSRandall Stewart 	 */
5442b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac) && (cmt_dac_flag == 0)) {
5443f8829a4aSRandall Stewart 		this_sack_lowest_newack = cum_ack;
5444f8829a4aSRandall Stewart 	}
5445f8829a4aSRandall Stewart 	if (num_seg > 0) {
5446f8829a4aSRandall Stewart 		sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
5447f8829a4aSRandall Stewart 		    biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
5448f8829a4aSRandall Stewart 	}
5449b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
5450b54d3a6cSRandall Stewart 	asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
5451f8829a4aSRandall Stewart 
5452f8829a4aSRandall Stewart 	/******************************************************************
5453f8829a4aSRandall Stewart 	 *  Here we do the stuff with ECN Nonce checking.
5454f8829a4aSRandall Stewart 	 *  We basically check to see if the nonce sum flag was incorrect
5455f8829a4aSRandall Stewart 	 *  or if resynchronization needs to be done. Also if we catch a
5456f8829a4aSRandall Stewart 	 *  misbehaving receiver we give him the kick.
5457f8829a4aSRandall Stewart 	 ******************************************************************/
5458f8829a4aSRandall Stewart 
5459f8829a4aSRandall Stewart 	if (asoc->ecn_nonce_allowed) {
5460f8829a4aSRandall Stewart 		if (asoc->nonce_sum_check) {
5461f8829a4aSRandall Stewart 			if (nonce_sum_flag != ((asoc->nonce_sum_expect_base + ecn_seg_sums) & SCTP_SACK_NONCE_SUM)) {
5462f8829a4aSRandall Stewart 				if (asoc->nonce_wait_for_ecne == 0) {
5463f8829a4aSRandall Stewart 					struct sctp_tmit_chunk *lchk;
5464f8829a4aSRandall Stewart 
5465f8829a4aSRandall Stewart 					lchk = TAILQ_FIRST(&asoc->send_queue);
5466f8829a4aSRandall Stewart 					asoc->nonce_wait_for_ecne = 1;
5467f8829a4aSRandall Stewart 					if (lchk) {
5468f8829a4aSRandall Stewart 						asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
5469f8829a4aSRandall Stewart 					} else {
5470f8829a4aSRandall Stewart 						asoc->nonce_wait_tsn = asoc->sending_seq;
5471f8829a4aSRandall Stewart 					}
5472f8829a4aSRandall Stewart 				} else {
5473f8829a4aSRandall Stewart 					if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
5474f8829a4aSRandall Stewart 					    (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
5475f8829a4aSRandall Stewart 						/*
5476f8829a4aSRandall Stewart 						 * Misbehaving peer. We need
5477f8829a4aSRandall Stewart 						 * to react to this guy
5478f8829a4aSRandall Stewart 						 */
5479f8829a4aSRandall Stewart 						asoc->ecn_allowed = 0;
5480f8829a4aSRandall Stewart 						asoc->ecn_nonce_allowed = 0;
5481f8829a4aSRandall Stewart 					}
5482f8829a4aSRandall Stewart 				}
5483f8829a4aSRandall Stewart 			}
5484f8829a4aSRandall Stewart 		} else {
5485f8829a4aSRandall Stewart 			/* See if Resynchronization Possible */
5486f8829a4aSRandall Stewart 			if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
5487f8829a4aSRandall Stewart 				asoc->nonce_sum_check = 1;
5488f8829a4aSRandall Stewart 				/*
5489f8829a4aSRandall Stewart 				 * now we must calculate what the base is.
5490f8829a4aSRandall Stewart 				 * We do this based on two things, we know
5491f8829a4aSRandall Stewart 				 * the total's for all the segments
5492f8829a4aSRandall Stewart 				 * gap-acked in the SACK, its stored in
5493f8829a4aSRandall Stewart 				 * ecn_seg_sums. We also know the SACK's
5494f8829a4aSRandall Stewart 				 * nonce sum, its in nonce_sum_flag. So we
5495f8829a4aSRandall Stewart 				 * can build a truth table to back-calculate
5496f8829a4aSRandall Stewart 				 * the new value of
5497f8829a4aSRandall Stewart 				 * asoc->nonce_sum_expect_base:
5498f8829a4aSRandall Stewart 				 *
5499f8829a4aSRandall Stewart 				 * SACK-flag-Value         Seg-Sums Base 0 0 0
5500f8829a4aSRandall Stewart 				 * 1                    0 1 0 1 1 1
5501f8829a4aSRandall Stewart 				 * 1 0
5502f8829a4aSRandall Stewart 				 */
5503f8829a4aSRandall Stewart 				asoc->nonce_sum_expect_base = (ecn_seg_sums ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
5504f8829a4aSRandall Stewart 			}
5505f8829a4aSRandall Stewart 		}
5506f8829a4aSRandall Stewart 	}
5507f8829a4aSRandall Stewart 	/* Now are we exiting loss recovery ? */
5508f8829a4aSRandall Stewart 	if (will_exit_fast_recovery) {
5509f8829a4aSRandall Stewart 		/* Ok, we must exit fast recovery */
5510f8829a4aSRandall Stewart 		asoc->fast_retran_loss_recovery = 0;
5511f8829a4aSRandall Stewart 	}
5512f8829a4aSRandall Stewart 	if ((asoc->sat_t3_loss_recovery) &&
5513f8829a4aSRandall Stewart 	    ((compare_with_wrap(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn,
5514f8829a4aSRandall Stewart 	    MAX_TSN) ||
5515f8829a4aSRandall Stewart 	    (asoc->last_acked_seq == asoc->sat_t3_recovery_tsn)))) {
5516f8829a4aSRandall Stewart 		/* end satellite t3 loss recovery */
5517f8829a4aSRandall Stewart 		asoc->sat_t3_loss_recovery = 0;
5518f8829a4aSRandall Stewart 	}
551942551e99SRandall Stewart 	/*
552042551e99SRandall Stewart 	 * CMT Fast recovery
552142551e99SRandall Stewart 	 */
5522f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5523f8829a4aSRandall Stewart 		if (net->will_exit_fast_recovery) {
5524f8829a4aSRandall Stewart 			/* Ok, we must exit fast recovery */
5525f8829a4aSRandall Stewart 			net->fast_retran_loss_recovery = 0;
5526f8829a4aSRandall Stewart 		}
5527f8829a4aSRandall Stewart 	}
5528f8829a4aSRandall Stewart 
5529f8829a4aSRandall Stewart 	/* Adjust and set the new rwnd value */
5530b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
5531f8829a4aSRandall Stewart 		sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
5532b3f1ea41SRandall Stewart 		    asoc->peers_rwnd, asoc->total_flight, (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)), a_rwnd);
553380fefe0aSRandall Stewart 	}
5534f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
5535b3f1ea41SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
5536f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5537f8829a4aSRandall Stewart 		/* SWS sender side engages */
5538f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
5539f8829a4aSRandall Stewart 	}
55405e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
55415e54f665SRandall Stewart 		win_probe_recovery = 1;
55425e54f665SRandall Stewart 	}
5543f8829a4aSRandall Stewart 	/*
5544f8829a4aSRandall Stewart 	 * Now we must setup so we have a timer up for anyone with
5545f8829a4aSRandall Stewart 	 * outstanding data.
5546f8829a4aSRandall Stewart 	 */
5547bff64a4dSRandall Stewart 	done_once = 0;
5548a5d547adSRandall Stewart again:
5549a5d547adSRandall Stewart 	j = 0;
5550f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
55515e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
5552c105859eSRandall Stewart 			win_probe_recovered = 1;
55535e54f665SRandall Stewart 			/*-
55545e54f665SRandall Stewart 			 * Find first chunk that was used with
55555e54f665SRandall Stewart 			 * window probe and clear the event. Put
55565e54f665SRandall Stewart 			 * it back into the send queue as if has
55575e54f665SRandall Stewart 			 * not been sent.
55585e54f665SRandall Stewart 			 */
55595e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
55605e54f665SRandall Stewart 				if (tp1->window_probe) {
5561c105859eSRandall Stewart 					sctp_window_probe_recovery(stcb, asoc, net, tp1);
55625e54f665SRandall Stewart 					break;
55635e54f665SRandall Stewart 				}
55645e54f665SRandall Stewart 			}
55655e54f665SRandall Stewart 		}
5566f8829a4aSRandall Stewart 		if (net->flight_size) {
5567a5d547adSRandall Stewart 			j++;
5568f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5569f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb, net);
55705171328bSRandall Stewart 			if (net->window_probe) {
55715171328bSRandall Stewart 			}
5572c105859eSRandall Stewart 		} else {
55735171328bSRandall Stewart 			if (net->window_probe) {
55745171328bSRandall Stewart 				/*
55755171328bSRandall Stewart 				 * In window probes we must assure a timer
55765171328bSRandall Stewart 				 * is still running there
55775171328bSRandall Stewart 				 */
55785171328bSRandall Stewart 
55795171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
55805171328bSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_SEND,
55815171328bSRandall Stewart 					    stcb->sctp_ep, stcb, net);
55825171328bSRandall Stewart 
55835171328bSRandall Stewart 				}
55845171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5585c105859eSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5586c105859eSRandall Stewart 				    stcb, net,
5587c105859eSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
5588c105859eSRandall Stewart 			}
5589b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
5590c105859eSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
5591c105859eSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
5592c105859eSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
5593c105859eSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
5594c105859eSRandall Stewart 				}
5595c105859eSRandall Stewart 			}
5596f8829a4aSRandall Stewart 		}
5597f8829a4aSRandall Stewart 	}
5598bff64a4dSRandall Stewart 	if ((j == 0) &&
5599bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
5600bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
5601c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
5602bff64a4dSRandall Stewart 	    (done_once == 0)) {
56030c0982b8SRandall Stewart 		/*
56040c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
56050c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
56060c0982b8SRandall Stewart 		 */
56070c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
5608a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5609a5d547adSRandall Stewart 				net->flight_size = 0;
5610a5d547adSRandall Stewart 			}
5611a5d547adSRandall Stewart 			asoc->total_flight = 0;
5612a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
5613a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
5614a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5615a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5616c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
5617c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
5618a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5619a5d547adSRandall Stewart 					asoc->sent_queue_retran_cnt++;
5620a5d547adSRandall Stewart 				}
5621a5d547adSRandall Stewart 			}
56220c0982b8SRandall Stewart 		}
5623bff64a4dSRandall Stewart 		done_once = 1;
5624a5d547adSRandall Stewart 		goto again;
5625a5d547adSRandall Stewart 	}
5626dfb11ef8SRandall Stewart 	/* Fix up the a-p-a-p for future PR-SCTP sends */
5627dfb11ef8SRandall Stewart 	if (compare_with_wrap(cum_ack, asoc->advanced_peer_ack_point, MAX_TSN)) {
5628dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cum_ack;
5629dfb11ef8SRandall Stewart 	}
5630830d754dSRandall Stewart 	/* C2. try to further move advancedPeerAckPoint ahead */
5631830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
5632830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
5633830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
5634830d754dSRandall Stewart 
5635830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
5636830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
5637830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
5638830d754dSRandall Stewart 		if (compare_with_wrap(asoc->advanced_peer_ack_point, cum_ack,
5639830d754dSRandall Stewart 		    MAX_TSN)) {
5640830d754dSRandall Stewart 			/*
5641830d754dSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing for notes
5642830d754dSRandall Stewart 			 * on issues that will occur when the ECN NONCE
5643830d754dSRandall Stewart 			 * stuff is put into SCTP for cross checking.
5644830d754dSRandall Stewart 			 */
56450c0982b8SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
56460c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
56470c0982b8SRandall Stewart 				    0xee, cum_ack, asoc->advanced_peer_ack_point,
56480c0982b8SRandall Stewart 				    old_adv_peer_ack_point);
56490c0982b8SRandall Stewart 			}
5650830d754dSRandall Stewart 			if (compare_with_wrap(asoc->advanced_peer_ack_point, old_adv_peer_ack_point,
5651830d754dSRandall Stewart 			    MAX_TSN)) {
5652830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
5653830d754dSRandall Stewart 				/*
5654830d754dSRandall Stewart 				 * ECN Nonce: Disable Nonce Sum check when
5655830d754dSRandall Stewart 				 * FWD TSN is sent and store resync tsn
5656830d754dSRandall Stewart 				 */
5657830d754dSRandall Stewart 				asoc->nonce_sum_check = 0;
5658830d754dSRandall Stewart 				asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
56590c0982b8SRandall Stewart 			} else if (lchk) {
56600c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
56610c0982b8SRandall Stewart 				lchk->rec.data.fwd_tsn_cnt++;
56620c0982b8SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt > 3) {
56630c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
56640c0982b8SRandall Stewart 					lchk->rec.data.fwd_tsn_cnt = 0;
56650c0982b8SRandall Stewart 				}
5666830d754dSRandall Stewart 			}
5667830d754dSRandall Stewart 		}
5668830d754dSRandall Stewart 		if (lchk) {
5669830d754dSRandall Stewart 			/* Assure a timer is up */
5670830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5671830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
5672830d754dSRandall Stewart 		}
5673830d754dSRandall Stewart 	}
5674b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
5675f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
5676f8829a4aSRandall Stewart 		    a_rwnd,
5677f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
5678f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
5679f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
568080fefe0aSRandall Stewart 	}
5681f8829a4aSRandall Stewart }
5682f8829a4aSRandall Stewart 
5683f8829a4aSRandall Stewart void
5684f8829a4aSRandall Stewart sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp,
5685f8829a4aSRandall Stewart     struct sctp_nets *netp, int *abort_flag)
5686f8829a4aSRandall Stewart {
5687f8829a4aSRandall Stewart 	/* Copy cum-ack */
5688f8829a4aSRandall Stewart 	uint32_t cum_ack, a_rwnd;
5689f8829a4aSRandall Stewart 
5690f8829a4aSRandall Stewart 	cum_ack = ntohl(cp->cumulative_tsn_ack);
5691f8829a4aSRandall Stewart 	/* Arrange so a_rwnd does NOT change */
5692f8829a4aSRandall Stewart 	a_rwnd = stcb->asoc.peers_rwnd + stcb->asoc.total_flight;
5693f8829a4aSRandall Stewart 
5694f8829a4aSRandall Stewart 	/* Now call the express sack handling */
5695f8829a4aSRandall Stewart 	sctp_express_handle_sack(stcb, cum_ack, a_rwnd, 0, abort_flag);
5696f8829a4aSRandall Stewart }
5697f8829a4aSRandall Stewart 
5698f8829a4aSRandall Stewart static void
5699f8829a4aSRandall Stewart sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
5700f8829a4aSRandall Stewart     struct sctp_stream_in *strmin)
5701f8829a4aSRandall Stewart {
5702f8829a4aSRandall Stewart 	struct sctp_queued_to_read *ctl, *nctl;
5703f8829a4aSRandall Stewart 	struct sctp_association *asoc;
5704f8829a4aSRandall Stewart 	int tt;
5705f8829a4aSRandall Stewart 
5706830d754dSRandall Stewart 	/* EY -used to calculate nr_gap information */
5707830d754dSRandall Stewart 	uint32_t nr_tsn, nr_gap;
5708830d754dSRandall Stewart 
5709f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
5710f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered;
5711f8829a4aSRandall Stewart 	/*
5712f8829a4aSRandall Stewart 	 * First deliver anything prior to and including the stream no that
5713f8829a4aSRandall Stewart 	 * came in
5714f8829a4aSRandall Stewart 	 */
5715f8829a4aSRandall Stewart 	ctl = TAILQ_FIRST(&strmin->inqueue);
5716f8829a4aSRandall Stewart 	while (ctl) {
5717f8829a4aSRandall Stewart 		nctl = TAILQ_NEXT(ctl, next);
5718f8829a4aSRandall Stewart 		if (compare_with_wrap(tt, ctl->sinfo_ssn, MAX_SEQ) ||
5719f8829a4aSRandall Stewart 		    (tt == ctl->sinfo_ssn)) {
5720f8829a4aSRandall Stewart 			/* this is deliverable now */
5721f8829a4aSRandall Stewart 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5722f8829a4aSRandall Stewart 			/* subtract pending on streams */
5723f8829a4aSRandall Stewart 			asoc->size_on_all_streams -= ctl->length;
5724f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_all_streams);
5725f8829a4aSRandall Stewart 			/* deliver it to at least the delivery-q */
5726f8829a4aSRandall Stewart 			if (stcb->sctp_socket) {
5727830d754dSRandall Stewart 				/* EY need the tsn info for calculating nr */
5728830d754dSRandall Stewart 				nr_tsn = ctl->sinfo_tsn;
5729f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
5730f8829a4aSRandall Stewart 				    ctl,
5731cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
5732830d754dSRandall Stewart 				/*
5733830d754dSRandall Stewart 				 * EY this is the chunk that should be
5734830d754dSRandall Stewart 				 * tagged nr gapped calculate the gap and
5735830d754dSRandall Stewart 				 * such then tag this TSN nr
5736830d754dSRandall Stewart 				 * chk->rec.data.TSN_seq
5737830d754dSRandall Stewart 				 */
5738830d754dSRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
5739830d754dSRandall Stewart 
5740d50c1d79SRandall Stewart 					SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
5741830d754dSRandall Stewart 					if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
5742830d754dSRandall Stewart 					    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
5743830d754dSRandall Stewart 						/*
5744830d754dSRandall Stewart 						 * EY These should never
5745830d754dSRandall Stewart 						 * happen- explained before
5746830d754dSRandall Stewart 						 */
5747830d754dSRandall Stewart 					} else {
5748830d754dSRandall Stewart 						SCTP_TCB_LOCK_ASSERT(stcb);
5749830d754dSRandall Stewart 						SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
5750d50c1d79SRandall Stewart 						SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
57518933fa13SRandall Stewart 						if (compare_with_wrap(nr_tsn,
57528933fa13SRandall Stewart 						    asoc->highest_tsn_inside_nr_map,
57538933fa13SRandall Stewart 						    MAX_TSN))
5754830d754dSRandall Stewart 							asoc->highest_tsn_inside_nr_map = nr_tsn;
5755830d754dSRandall Stewart 					}
5756830d754dSRandall Stewart 					if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, nr_gap))
5757830d754dSRandall Stewart 						/*
5758830d754dSRandall Stewart 						 * printf("In
5759830d754dSRandall Stewart 						 * sctp_kick_prsctp_reorder_q
5760830d754dSRandall Stewart 						 * ueue(7): Something wrong,
5761830d754dSRandall Stewart 						 * the TSN to be tagged"
5762830d754dSRandall Stewart 						 * "\nas NR is not even in
5763830d754dSRandall Stewart 						 * the mapping_array, or map
5764830d754dSRandall Stewart 						 * and nr_map are
5765830d754dSRandall Stewart 						 * inconsistent");
5766830d754dSRandall Stewart 						 */
5767830d754dSRandall Stewart 						/*
5768830d754dSRandall Stewart 						 * EY - not %100 sure about
5769830d754dSRandall Stewart 						 * the lock thing, don't
5770830d754dSRandall Stewart 						 * think its required
5771830d754dSRandall Stewart 						 */
5772830d754dSRandall Stewart 						/*
5773830d754dSRandall Stewart 						 * SCTP_TCB_LOCK_ASSERT(stcb)
5774830d754dSRandall Stewart 						 * ;
5775830d754dSRandall Stewart 						 */
5776830d754dSRandall Stewart 					{
5777830d754dSRandall Stewart 						/*
5778830d754dSRandall Stewart 						 * printf("\nCalculating an
5779830d754dSRandall Stewart 						 * nr_gap!!\nmapping_array_si
5780830d754dSRandall Stewart 						 * ze = %d
5781830d754dSRandall Stewart 						 * nr_mapping_array_size =
5782830d754dSRandall Stewart 						 * %d" "\nmapping_array_base
5783830d754dSRandall Stewart 						 * = %d
5784830d754dSRandall Stewart 						 * nr_mapping_array_base =
5785830d754dSRandall Stewart 						 * %d\nhighest_tsn_inside_map
5786830d754dSRandall Stewart 						 *  = %d"
5787830d754dSRandall Stewart 						 * "highest_tsn_inside_nr_map
5788830d754dSRandall Stewart 						 *  = %d\nTSN = %d nr_gap =
5789830d754dSRandall Stewart 						 * %d",asoc->mapping_array_si
5790830d754dSRandall Stewart 						 * ze,
5791830d754dSRandall Stewart 						 * asoc->nr_mapping_array_siz
5792830d754dSRandall Stewart 						 * e,
5793830d754dSRandall Stewart 						 * asoc->mapping_array_base_t
5794830d754dSRandall Stewart 						 * sn,
5795830d754dSRandall Stewart 						 * asoc->nr_mapping_array_bas
5796830d754dSRandall Stewart 						 * e_tsn,
5797830d754dSRandall Stewart 						 * asoc->highest_tsn_inside_m
5798830d754dSRandall Stewart 						 * ap,
5799830d754dSRandall Stewart 						 * asoc->highest_tsn_inside_n
5800830d754dSRandall Stewart 						 * r_map,tsn,nr_gap);
5801830d754dSRandall Stewart 						 */
5802830d754dSRandall Stewart 					}
5803830d754dSRandall Stewart 				}
5804f8829a4aSRandall Stewart 			}
5805f8829a4aSRandall Stewart 		} else {
5806f8829a4aSRandall Stewart 			/* no more delivery now. */
5807f8829a4aSRandall Stewart 			break;
5808f8829a4aSRandall Stewart 		}
5809f8829a4aSRandall Stewart 		ctl = nctl;
5810f8829a4aSRandall Stewart 	}
5811f8829a4aSRandall Stewart 	/*
5812f8829a4aSRandall Stewart 	 * now we must deliver things in queue the normal way  if any are
5813f8829a4aSRandall Stewart 	 * now ready.
5814f8829a4aSRandall Stewart 	 */
5815f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered + 1;
5816f8829a4aSRandall Stewart 	ctl = TAILQ_FIRST(&strmin->inqueue);
5817f8829a4aSRandall Stewart 	while (ctl) {
5818f8829a4aSRandall Stewart 		nctl = TAILQ_NEXT(ctl, next);
5819f8829a4aSRandall Stewart 		if (tt == ctl->sinfo_ssn) {
5820f8829a4aSRandall Stewart 			/* this is deliverable now */
5821f8829a4aSRandall Stewart 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5822f8829a4aSRandall Stewart 			/* subtract pending on streams */
5823f8829a4aSRandall Stewart 			asoc->size_on_all_streams -= ctl->length;
5824f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_all_streams);
5825f8829a4aSRandall Stewart 			/* deliver it to at least the delivery-q */
5826f8829a4aSRandall Stewart 			strmin->last_sequence_delivered = ctl->sinfo_ssn;
5827f8829a4aSRandall Stewart 			if (stcb->sctp_socket) {
5828830d754dSRandall Stewart 				/* EY */
5829830d754dSRandall Stewart 				nr_tsn = ctl->sinfo_tsn;
5830f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
5831f8829a4aSRandall Stewart 				    ctl,
5832cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
5833830d754dSRandall Stewart 				/*
5834830d754dSRandall Stewart 				 * EY this is the chunk that should be
5835830d754dSRandall Stewart 				 * tagged nr gapped calculate the gap and
5836830d754dSRandall Stewart 				 * such then tag this TSN nr
5837830d754dSRandall Stewart 				 * chk->rec.data.TSN_seq
5838830d754dSRandall Stewart 				 */
5839830d754dSRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
5840d50c1d79SRandall Stewart 					SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
5841830d754dSRandall Stewart 					if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
5842830d754dSRandall Stewart 					    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
5843830d754dSRandall Stewart 						/*
5844830d754dSRandall Stewart 						 * EY These should never
5845830d754dSRandall Stewart 						 * happen, explained before
5846830d754dSRandall Stewart 						 */
5847830d754dSRandall Stewart 					} else {
5848830d754dSRandall Stewart 						SCTP_TCB_LOCK_ASSERT(stcb);
5849830d754dSRandall Stewart 						SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
5850d50c1d79SRandall Stewart 						SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
58518933fa13SRandall Stewart 						if (compare_with_wrap(nr_tsn, asoc->highest_tsn_inside_nr_map,
58528933fa13SRandall Stewart 						    MAX_TSN))
5853830d754dSRandall Stewart 							asoc->highest_tsn_inside_nr_map = nr_tsn;
5854830d754dSRandall Stewart 					}
5855830d754dSRandall Stewart 					if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, nr_gap))
5856830d754dSRandall Stewart 						/*
5857830d754dSRandall Stewart 						 * printf("In
5858830d754dSRandall Stewart 						 * sctp_kick_prsctp_reorder_q
5859830d754dSRandall Stewart 						 * ueue(8): Something wrong,
5860830d754dSRandall Stewart 						 * the TSN to be tagged"
5861830d754dSRandall Stewart 						 * "\nas NR is not even in
5862830d754dSRandall Stewart 						 * the mapping_array, or map
5863830d754dSRandall Stewart 						 * and nr_map are
5864830d754dSRandall Stewart 						 * inconsistent");
5865830d754dSRandall Stewart 						 */
5866830d754dSRandall Stewart 						/*
5867830d754dSRandall Stewart 						 * EY - not %100 sure about
5868830d754dSRandall Stewart 						 * the lock thing, don't
5869830d754dSRandall Stewart 						 * think its required
5870830d754dSRandall Stewart 						 */
5871830d754dSRandall Stewart 						/*
5872830d754dSRandall Stewart 						 * SCTP_TCB_LOCK_ASSERT(stcb)
5873830d754dSRandall Stewart 						 * ;
5874830d754dSRandall Stewart 						 */
5875830d754dSRandall Stewart 					{
5876830d754dSRandall Stewart 						/*
5877830d754dSRandall Stewart 						 * printf("\nCalculating an
5878830d754dSRandall Stewart 						 * nr_gap!!\nmapping_array_si
5879830d754dSRandall Stewart 						 * ze = %d
5880830d754dSRandall Stewart 						 * nr_mapping_array_size =
5881830d754dSRandall Stewart 						 * %d" "\nmapping_array_base
5882830d754dSRandall Stewart 						 * = %d
5883830d754dSRandall Stewart 						 * nr_mapping_array_base =
5884830d754dSRandall Stewart 						 * %d\nhighest_tsn_inside_map
5885830d754dSRandall Stewart 						 *  = %d"
5886830d754dSRandall Stewart 						 * "highest_tsn_inside_nr_map
5887830d754dSRandall Stewart 						 *  = %d\nTSN = %d nr_gap =
5888830d754dSRandall Stewart 						 * %d",asoc->mapping_array_si
5889830d754dSRandall Stewart 						 * ze,
5890830d754dSRandall Stewart 						 * asoc->nr_mapping_array_siz
5891830d754dSRandall Stewart 						 * e,
5892830d754dSRandall Stewart 						 * asoc->mapping_array_base_t
5893830d754dSRandall Stewart 						 * sn,
5894830d754dSRandall Stewart 						 * asoc->nr_mapping_array_bas
5895830d754dSRandall Stewart 						 * e_tsn,
5896830d754dSRandall Stewart 						 * asoc->highest_tsn_inside_m
5897830d754dSRandall Stewart 						 * ap,
5898830d754dSRandall Stewart 						 * asoc->highest_tsn_inside_n
5899830d754dSRandall Stewart 						 * r_map,tsn,nr_gap);
5900830d754dSRandall Stewart 						 */
5901830d754dSRandall Stewart 					}
5902830d754dSRandall Stewart 				}
5903f8829a4aSRandall Stewart 			}
5904f8829a4aSRandall Stewart 			tt = strmin->last_sequence_delivered + 1;
5905f8829a4aSRandall Stewart 		} else {
5906f8829a4aSRandall Stewart 			break;
5907f8829a4aSRandall Stewart 		}
5908f8829a4aSRandall Stewart 		ctl = nctl;
5909f8829a4aSRandall Stewart 	}
5910f8829a4aSRandall Stewart }
5911f8829a4aSRandall Stewart 
59128933fa13SRandall Stewart static void
59138933fa13SRandall Stewart sctp_flush_reassm_for_str_seq(struct sctp_tcb *stcb,
59148933fa13SRandall Stewart     struct sctp_association *asoc,
59158933fa13SRandall Stewart     uint16_t stream, uint16_t seq)
59168933fa13SRandall Stewart {
59178933fa13SRandall Stewart 	struct sctp_tmit_chunk *chk, *at;
59188933fa13SRandall Stewart 
59198933fa13SRandall Stewart 	if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
59208933fa13SRandall Stewart 		/* For each one on here see if we need to toss it */
59218933fa13SRandall Stewart 		/*
59228933fa13SRandall Stewart 		 * For now large messages held on the reasmqueue that are
59238933fa13SRandall Stewart 		 * complete will be tossed too. We could in theory do more
59248933fa13SRandall Stewart 		 * work to spin through and stop after dumping one msg aka
59258933fa13SRandall Stewart 		 * seeing the start of a new msg at the head, and call the
59268933fa13SRandall Stewart 		 * delivery function... to see if it can be delivered... But
59278933fa13SRandall Stewart 		 * for now we just dump everything on the queue.
59288933fa13SRandall Stewart 		 */
59298933fa13SRandall Stewart 		chk = TAILQ_FIRST(&asoc->reasmqueue);
59308933fa13SRandall Stewart 		while (chk) {
59318933fa13SRandall Stewart 			at = TAILQ_NEXT(chk, sctp_next);
59328e71b694SMichael Tuexen 			/*
59338e71b694SMichael Tuexen 			 * Do not toss it if on a different stream or marked
59348e71b694SMichael Tuexen 			 * for unordered delivery in which case the stream
59358e71b694SMichael Tuexen 			 * sequence number has no meaning.
59368e71b694SMichael Tuexen 			 */
59378e71b694SMichael Tuexen 			if ((chk->rec.data.stream_number != stream) ||
59388e71b694SMichael Tuexen 			    ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == SCTP_DATA_UNORDERED)) {
59398933fa13SRandall Stewart 				chk = at;
59408933fa13SRandall Stewart 				continue;
59418933fa13SRandall Stewart 			}
59428933fa13SRandall Stewart 			if (chk->rec.data.stream_seq == seq) {
59438933fa13SRandall Stewart 				/* It needs to be tossed */
59448933fa13SRandall Stewart 				TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
59458933fa13SRandall Stewart 				if (compare_with_wrap(chk->rec.data.TSN_seq,
59468933fa13SRandall Stewart 				    asoc->tsn_last_delivered, MAX_TSN)) {
59478933fa13SRandall Stewart 					asoc->tsn_last_delivered =
59488933fa13SRandall Stewart 					    chk->rec.data.TSN_seq;
59498933fa13SRandall Stewart 					asoc->str_of_pdapi =
59508933fa13SRandall Stewart 					    chk->rec.data.stream_number;
59518933fa13SRandall Stewart 					asoc->ssn_of_pdapi =
59528933fa13SRandall Stewart 					    chk->rec.data.stream_seq;
59538933fa13SRandall Stewart 					asoc->fragment_flags =
59548933fa13SRandall Stewart 					    chk->rec.data.rcv_flags;
59558933fa13SRandall Stewart 				}
59568933fa13SRandall Stewart 				asoc->size_on_reasm_queue -= chk->send_size;
59578933fa13SRandall Stewart 				sctp_ucount_decr(asoc->cnt_on_reasm_queue);
59588933fa13SRandall Stewart 
59598933fa13SRandall Stewart 				/* Clear up any stream problem */
59608933fa13SRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) !=
59618933fa13SRandall Stewart 				    SCTP_DATA_UNORDERED &&
59628933fa13SRandall Stewart 				    (compare_with_wrap(chk->rec.data.stream_seq,
59638933fa13SRandall Stewart 				    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered,
59648933fa13SRandall Stewart 				    MAX_SEQ))) {
59658933fa13SRandall Stewart 					/*
59668933fa13SRandall Stewart 					 * We must dump forward this streams
59678933fa13SRandall Stewart 					 * sequence number if the chunk is
59688933fa13SRandall Stewart 					 * not unordered that is being
59698933fa13SRandall Stewart 					 * skipped. There is a chance that
59708933fa13SRandall Stewart 					 * if the peer does not include the
59718933fa13SRandall Stewart 					 * last fragment in its FWD-TSN we
59728933fa13SRandall Stewart 					 * WILL have a problem here since
59738933fa13SRandall Stewart 					 * you would have a partial chunk in
59748933fa13SRandall Stewart 					 * queue that may not be
59758933fa13SRandall Stewart 					 * deliverable. Also if a Partial
59768933fa13SRandall Stewart 					 * delivery API as started the user
59778933fa13SRandall Stewart 					 * may get a partial chunk. The next
59788933fa13SRandall Stewart 					 * read returning a new chunk...
59798933fa13SRandall Stewart 					 * really ugly but I see no way
59808933fa13SRandall Stewart 					 * around it! Maybe a notify??
59818933fa13SRandall Stewart 					 */
59828933fa13SRandall Stewart 					asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered =
59838933fa13SRandall Stewart 					    chk->rec.data.stream_seq;
59848933fa13SRandall Stewart 				}
59858933fa13SRandall Stewart 				if (chk->data) {
59868933fa13SRandall Stewart 					sctp_m_freem(chk->data);
59878933fa13SRandall Stewart 					chk->data = NULL;
59888933fa13SRandall Stewart 				}
59898933fa13SRandall Stewart 				sctp_free_a_chunk(stcb, chk);
59908933fa13SRandall Stewart 			} else if (compare_with_wrap(chk->rec.data.stream_seq, seq, MAX_SEQ)) {
59918933fa13SRandall Stewart 				/*
59928933fa13SRandall Stewart 				 * If the stream_seq is > than the purging
59938933fa13SRandall Stewart 				 * one, we are done
59948933fa13SRandall Stewart 				 */
59958933fa13SRandall Stewart 				break;
59968933fa13SRandall Stewart 			}
59978933fa13SRandall Stewart 			chk = at;
59988933fa13SRandall Stewart 		}
59998933fa13SRandall Stewart 	}
60008933fa13SRandall Stewart }
60018933fa13SRandall Stewart 
60028933fa13SRandall Stewart 
6003f8829a4aSRandall Stewart void
6004f8829a4aSRandall Stewart sctp_handle_forward_tsn(struct sctp_tcb *stcb,
6005671d309cSRandall Stewart     struct sctp_forward_tsn_chunk *fwd, int *abort_flag, struct mbuf *m, int offset)
6006f8829a4aSRandall Stewart {
6007f8829a4aSRandall Stewart 	/*
6008f8829a4aSRandall Stewart 	 * ISSUES that MUST be fixed for ECN! When we are the sender of the
6009f8829a4aSRandall Stewart 	 * forward TSN, when the SACK comes back that acknowledges the
6010f8829a4aSRandall Stewart 	 * FWD-TSN we must reset the NONCE sum to match correctly. This will
6011f8829a4aSRandall Stewart 	 * get quite tricky since we may have sent more data interveneing
6012f8829a4aSRandall Stewart 	 * and must carefully account for what the SACK says on the nonce
6013f8829a4aSRandall Stewart 	 * and any gaps that are reported. This work will NOT be done here,
6014f8829a4aSRandall Stewart 	 * but I note it here since it is really related to PR-SCTP and
6015f8829a4aSRandall Stewart 	 * FWD-TSN's
6016f8829a4aSRandall Stewart 	 */
6017f8829a4aSRandall Stewart 
6018f8829a4aSRandall Stewart 	/* The pr-sctp fwd tsn */
6019f8829a4aSRandall Stewart 	/*
6020f8829a4aSRandall Stewart 	 * here we will perform all the data receiver side steps for
6021f8829a4aSRandall Stewart 	 * processing FwdTSN, as required in by pr-sctp draft:
6022f8829a4aSRandall Stewart 	 *
6023f8829a4aSRandall Stewart 	 * Assume we get FwdTSN(x):
6024f8829a4aSRandall Stewart 	 *
6025f8829a4aSRandall Stewart 	 * 1) update local cumTSN to x 2) try to further advance cumTSN to x +
6026f8829a4aSRandall Stewart 	 * others we have 3) examine and update re-ordering queue on
6027f8829a4aSRandall Stewart 	 * pr-in-streams 4) clean up re-assembly queue 5) Send a sack to
6028f8829a4aSRandall Stewart 	 * report where we are.
6029f8829a4aSRandall Stewart 	 */
6030f8829a4aSRandall Stewart 	struct sctp_association *asoc;
60312afb3e84SRandall Stewart 	uint32_t new_cum_tsn, gap;
60328933fa13SRandall Stewart 	unsigned int i, fwd_sz, cumack_set_flag, m_size;
60338933fa13SRandall Stewart 	uint32_t str_seq;
6034f8829a4aSRandall Stewart 	struct sctp_stream_in *strm;
6035f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk, *at;
60368933fa13SRandall Stewart 	struct sctp_queued_to_read *ctl, *sv;
6037f8829a4aSRandall Stewart 
6038f8829a4aSRandall Stewart 	cumack_set_flag = 0;
6039f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
6040f8829a4aSRandall Stewart 	if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
6041ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1,
6042ad81507eSRandall Stewart 		    "Bad size too small/big fwd-tsn\n");
6043f8829a4aSRandall Stewart 		return;
6044f8829a4aSRandall Stewart 	}
6045f8829a4aSRandall Stewart 	m_size = (stcb->asoc.mapping_array_size << 3);
6046f8829a4aSRandall Stewart 	/*************************************************************/
6047f8829a4aSRandall Stewart 	/* 1. Here we update local cumTSN and shift the bitmap array */
6048f8829a4aSRandall Stewart 	/*************************************************************/
6049f8829a4aSRandall Stewart 	new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
6050f8829a4aSRandall Stewart 
6051f8829a4aSRandall Stewart 	if (compare_with_wrap(asoc->cumulative_tsn, new_cum_tsn, MAX_TSN) ||
6052f8829a4aSRandall Stewart 	    asoc->cumulative_tsn == new_cum_tsn) {
6053f8829a4aSRandall Stewart 		/* Already got there ... */
6054f8829a4aSRandall Stewart 		return;
6055f8829a4aSRandall Stewart 	}
6056f8829a4aSRandall Stewart 	if (compare_with_wrap(new_cum_tsn, asoc->highest_tsn_inside_map,
6057f8829a4aSRandall Stewart 	    MAX_TSN)) {
6058f8829a4aSRandall Stewart 		asoc->highest_tsn_inside_map = new_cum_tsn;
6059830d754dSRandall Stewart 		/* EY nr_mapping_array version of the above */
6060830d754dSRandall Stewart 		/*
6061830d754dSRandall Stewart 		 * if(SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) &&
6062830d754dSRandall Stewart 		 * asoc->peer_supports_nr_sack)
6063830d754dSRandall Stewart 		 */
6064830d754dSRandall Stewart 		asoc->highest_tsn_inside_nr_map = new_cum_tsn;
6065b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
6066f8829a4aSRandall Stewart 			sctp_log_map(0, 0, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
606780fefe0aSRandall Stewart 		}
6068f8829a4aSRandall Stewart 	}
6069f8829a4aSRandall Stewart 	/*
6070f8829a4aSRandall Stewart 	 * now we know the new TSN is more advanced, let's find the actual
6071f8829a4aSRandall Stewart 	 * gap
6072f8829a4aSRandall Stewart 	 */
6073d50c1d79SRandall Stewart 	SCTP_CALC_TSN_TO_GAP(gap, new_cum_tsn, asoc->mapping_array_base_tsn);
60742afb3e84SRandall Stewart 	if (gap >= m_size) {
6075b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
6076c4739e2fSRandall Stewart 			sctp_log_map(0, 0, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
6077c4739e2fSRandall Stewart 		}
6078f8829a4aSRandall Stewart 		if ((long)gap > sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv)) {
607917205eccSRandall Stewart 			struct mbuf *oper;
608017205eccSRandall Stewart 
6081f8829a4aSRandall Stewart 			/*
6082f8829a4aSRandall Stewart 			 * out of range (of single byte chunks in the rwnd I
608317205eccSRandall Stewart 			 * give out). This must be an attacker.
6084f8829a4aSRandall Stewart 			 */
608517205eccSRandall Stewart 			*abort_flag = 1;
608617205eccSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
608717205eccSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
608817205eccSRandall Stewart 			if (oper) {
608917205eccSRandall Stewart 				struct sctp_paramhdr *ph;
609017205eccSRandall Stewart 				uint32_t *ippp;
609117205eccSRandall Stewart 
609217205eccSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
609317205eccSRandall Stewart 				    (sizeof(uint32_t) * 3);
609417205eccSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
609517205eccSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
609617205eccSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
609717205eccSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
609817205eccSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_33);
609917205eccSRandall Stewart 				ippp++;
610017205eccSRandall Stewart 				*ippp = asoc->highest_tsn_inside_map;
610117205eccSRandall Stewart 				ippp++;
610217205eccSRandall Stewart 				*ippp = new_cum_tsn;
610317205eccSRandall Stewart 			}
610417205eccSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_33;
610517205eccSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb,
6106ceaad40aSRandall Stewart 			    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
6107f8829a4aSRandall Stewart 			return;
6108f8829a4aSRandall Stewart 		}
6109207304d4SRandall Stewart 		SCTP_STAT_INCR(sctps_fwdtsn_map_over);
61102afb3e84SRandall Stewart 		memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
6111f8829a4aSRandall Stewart 		cumack_set_flag = 1;
61122afb3e84SRandall Stewart 		asoc->mapping_array_base_tsn = new_cum_tsn + 1;
61132afb3e84SRandall Stewart 		asoc->cumulative_tsn = asoc->highest_tsn_inside_map = new_cum_tsn;
6114830d754dSRandall Stewart 		/* EY - nr_sack: nr_mapping_array version of the above */
6115830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
6116830d754dSRandall Stewart 			memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.nr_mapping_array_size);
6117830d754dSRandall Stewart 			asoc->nr_mapping_array_base_tsn = new_cum_tsn + 1;
6118830d754dSRandall Stewart 			asoc->highest_tsn_inside_nr_map = new_cum_tsn;
6119830d754dSRandall Stewart 			if (asoc->nr_mapping_array_size != asoc->mapping_array_size) {
6120830d754dSRandall Stewart 				/*
6121830d754dSRandall Stewart 				 * printf("IN sctp_handle_forward_tsn:
6122830d754dSRandall Stewart 				 * Something is wrong the size of" "map and
6123830d754dSRandall Stewart 				 * nr_map should be equal!")
6124830d754dSRandall Stewart 				 */ ;
6125830d754dSRandall Stewart 			}
6126830d754dSRandall Stewart 		}
6127b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
6128f8829a4aSRandall Stewart 			sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
612980fefe0aSRandall Stewart 		}
6130f8829a4aSRandall Stewart 		asoc->last_echo_tsn = asoc->highest_tsn_inside_map;
61312afb3e84SRandall Stewart 	} else {
61322afb3e84SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
61332afb3e84SRandall Stewart 		for (i = 0; i <= gap; i++) {
61348933fa13SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack
61358933fa13SRandall Stewart 			    && SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
61368933fa13SRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, i);
6137d50c1d79SRandall Stewart 			} else {
6138d50c1d79SRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->mapping_array, i);
61398933fa13SRandall Stewart 			}
6140f8829a4aSRandall Stewart 		}
61412afb3e84SRandall Stewart 		/*
61422afb3e84SRandall Stewart 		 * Now after marking all, slide thing forward but no sack
61432afb3e84SRandall Stewart 		 * please.
61442afb3e84SRandall Stewart 		 */
61452afb3e84SRandall Stewart 		sctp_sack_check(stcb, 0, 0, abort_flag);
61462afb3e84SRandall Stewart 		if (*abort_flag)
61472afb3e84SRandall Stewart 			return;
61482afb3e84SRandall Stewart 	}
6149f8829a4aSRandall Stewart 	/*************************************************************/
6150f8829a4aSRandall Stewart 	/* 2. Clear up re-assembly queue                             */
6151f8829a4aSRandall Stewart 	/*************************************************************/
6152f8829a4aSRandall Stewart 	/*
6153f8829a4aSRandall Stewart 	 * First service it if pd-api is up, just in case we can progress it
6154f8829a4aSRandall Stewart 	 * forward
6155f8829a4aSRandall Stewart 	 */
6156f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
6157f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
6158f8829a4aSRandall Stewart 	}
6159f8829a4aSRandall Stewart 	if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
6160f8829a4aSRandall Stewart 		/* For each one on here see if we need to toss it */
6161f8829a4aSRandall Stewart 		/*
6162f8829a4aSRandall Stewart 		 * For now large messages held on the reasmqueue that are
6163f8829a4aSRandall Stewart 		 * complete will be tossed too. We could in theory do more
6164f8829a4aSRandall Stewart 		 * work to spin through and stop after dumping one msg aka
6165f8829a4aSRandall Stewart 		 * seeing the start of a new msg at the head, and call the
6166f8829a4aSRandall Stewart 		 * delivery function... to see if it can be delivered... But
6167f8829a4aSRandall Stewart 		 * for now we just dump everything on the queue.
6168f8829a4aSRandall Stewart 		 */
6169f8829a4aSRandall Stewart 		chk = TAILQ_FIRST(&asoc->reasmqueue);
6170f8829a4aSRandall Stewart 		while (chk) {
6171f8829a4aSRandall Stewart 			at = TAILQ_NEXT(chk, sctp_next);
61720c0982b8SRandall Stewart 			if ((compare_with_wrap(new_cum_tsn,
61730c0982b8SRandall Stewart 			    chk->rec.data.TSN_seq, MAX_TSN)) ||
61740c0982b8SRandall Stewart 			    (new_cum_tsn == chk->rec.data.TSN_seq)) {
6175f8829a4aSRandall Stewart 				/* It needs to be tossed */
6176f8829a4aSRandall Stewart 				TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
6177f8829a4aSRandall Stewart 				if (compare_with_wrap(chk->rec.data.TSN_seq,
6178f8829a4aSRandall Stewart 				    asoc->tsn_last_delivered, MAX_TSN)) {
6179f8829a4aSRandall Stewart 					asoc->tsn_last_delivered =
6180f8829a4aSRandall Stewart 					    chk->rec.data.TSN_seq;
6181f8829a4aSRandall Stewart 					asoc->str_of_pdapi =
6182f8829a4aSRandall Stewart 					    chk->rec.data.stream_number;
6183f8829a4aSRandall Stewart 					asoc->ssn_of_pdapi =
6184f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq;
6185f8829a4aSRandall Stewart 					asoc->fragment_flags =
6186f8829a4aSRandall Stewart 					    chk->rec.data.rcv_flags;
6187f8829a4aSRandall Stewart 				}
6188f8829a4aSRandall Stewart 				asoc->size_on_reasm_queue -= chk->send_size;
6189f8829a4aSRandall Stewart 				sctp_ucount_decr(asoc->cnt_on_reasm_queue);
6190f8829a4aSRandall Stewart 
6191f8829a4aSRandall Stewart 				/* Clear up any stream problem */
6192f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) !=
6193f8829a4aSRandall Stewart 				    SCTP_DATA_UNORDERED &&
6194f8829a4aSRandall Stewart 				    (compare_with_wrap(chk->rec.data.stream_seq,
6195f8829a4aSRandall Stewart 				    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered,
6196f8829a4aSRandall Stewart 				    MAX_SEQ))) {
6197f8829a4aSRandall Stewart 					/*
6198f8829a4aSRandall Stewart 					 * We must dump forward this streams
6199f8829a4aSRandall Stewart 					 * sequence number if the chunk is
6200f8829a4aSRandall Stewart 					 * not unordered that is being
6201f8829a4aSRandall Stewart 					 * skipped. There is a chance that
6202f8829a4aSRandall Stewart 					 * if the peer does not include the
6203f8829a4aSRandall Stewart 					 * last fragment in its FWD-TSN we
6204f8829a4aSRandall Stewart 					 * WILL have a problem here since
6205f8829a4aSRandall Stewart 					 * you would have a partial chunk in
6206f8829a4aSRandall Stewart 					 * queue that may not be
6207f8829a4aSRandall Stewart 					 * deliverable. Also if a Partial
6208f8829a4aSRandall Stewart 					 * delivery API as started the user
6209f8829a4aSRandall Stewart 					 * may get a partial chunk. The next
6210f8829a4aSRandall Stewart 					 * read returning a new chunk...
6211f8829a4aSRandall Stewart 					 * really ugly but I see no way
6212f8829a4aSRandall Stewart 					 * around it! Maybe a notify??
6213f8829a4aSRandall Stewart 					 */
6214f8829a4aSRandall Stewart 					asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered =
6215f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq;
6216f8829a4aSRandall Stewart 				}
6217f8829a4aSRandall Stewart 				if (chk->data) {
6218f8829a4aSRandall Stewart 					sctp_m_freem(chk->data);
6219f8829a4aSRandall Stewart 					chk->data = NULL;
6220f8829a4aSRandall Stewart 				}
6221f8829a4aSRandall Stewart 				sctp_free_a_chunk(stcb, chk);
6222f8829a4aSRandall Stewart 			} else {
6223f8829a4aSRandall Stewart 				/*
6224f8829a4aSRandall Stewart 				 * Ok we have gone beyond the end of the
62258933fa13SRandall Stewart 				 * fwd-tsn's mark.
6226f8829a4aSRandall Stewart 				 */
6227f8829a4aSRandall Stewart 				break;
6228f8829a4aSRandall Stewart 			}
6229f8829a4aSRandall Stewart 			chk = at;
6230f8829a4aSRandall Stewart 		}
6231f8829a4aSRandall Stewart 	}
62328933fa13SRandall Stewart 	/*******************************************************/
62338933fa13SRandall Stewart 	/* 3. Update the PR-stream re-ordering queues and fix  */
62348933fa13SRandall Stewart 	/* delivery issues as needed.                       */
62358933fa13SRandall Stewart 	/*******************************************************/
6236f8829a4aSRandall Stewart 	fwd_sz -= sizeof(*fwd);
6237671d309cSRandall Stewart 	if (m && fwd_sz) {
6238f8829a4aSRandall Stewart 		/* New method. */
6239d61a0ae0SRandall Stewart 		unsigned int num_str;
6240671d309cSRandall Stewart 		struct sctp_strseq *stseq, strseqbuf;
6241671d309cSRandall Stewart 
6242671d309cSRandall Stewart 		offset += sizeof(*fwd);
6243f8829a4aSRandall Stewart 
62448933fa13SRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
6245f8829a4aSRandall Stewart 		num_str = fwd_sz / sizeof(struct sctp_strseq);
6246f8829a4aSRandall Stewart 		for (i = 0; i < num_str; i++) {
6247f8829a4aSRandall Stewart 			uint16_t st;
6248f8829a4aSRandall Stewart 
6249671d309cSRandall Stewart 			stseq = (struct sctp_strseq *)sctp_m_getptr(m, offset,
6250671d309cSRandall Stewart 			    sizeof(struct sctp_strseq),
6251671d309cSRandall Stewart 			    (uint8_t *) & strseqbuf);
6252671d309cSRandall Stewart 			offset += sizeof(struct sctp_strseq);
62532afb3e84SRandall Stewart 			if (stseq == NULL) {
6254671d309cSRandall Stewart 				break;
62552afb3e84SRandall Stewart 			}
6256f8829a4aSRandall Stewart 			/* Convert */
6257851b7298SRandall Stewart 			st = ntohs(stseq->stream);
6258851b7298SRandall Stewart 			stseq->stream = st;
6259851b7298SRandall Stewart 			st = ntohs(stseq->sequence);
6260851b7298SRandall Stewart 			stseq->sequence = st;
62618933fa13SRandall Stewart 
6262f8829a4aSRandall Stewart 			/* now process */
62638933fa13SRandall Stewart 
62648933fa13SRandall Stewart 			/*
62658933fa13SRandall Stewart 			 * Ok we now look for the stream/seq on the read
62668933fa13SRandall Stewart 			 * queue where its not all delivered. If we find it
62678933fa13SRandall Stewart 			 * we transmute the read entry into a PDI_ABORTED.
62688933fa13SRandall Stewart 			 */
6269851b7298SRandall Stewart 			if (stseq->stream >= asoc->streamincnt) {
62702afb3e84SRandall Stewart 				/* screwed up streams, stop!  */
62712afb3e84SRandall Stewart 				break;
6272f8829a4aSRandall Stewart 			}
62738933fa13SRandall Stewart 			if ((asoc->str_of_pdapi == stseq->stream) &&
62748933fa13SRandall Stewart 			    (asoc->ssn_of_pdapi == stseq->sequence)) {
62758933fa13SRandall Stewart 				/*
62768933fa13SRandall Stewart 				 * If this is the one we were partially
62778933fa13SRandall Stewart 				 * delivering now then we no longer are.
62788933fa13SRandall Stewart 				 * Note this will change with the reassembly
62798933fa13SRandall Stewart 				 * re-write.
62808933fa13SRandall Stewart 				 */
62818933fa13SRandall Stewart 				asoc->fragmented_delivery_inprogress = 0;
62828933fa13SRandall Stewart 			}
62838933fa13SRandall Stewart 			sctp_flush_reassm_for_str_seq(stcb, asoc, stseq->stream, stseq->sequence);
62848933fa13SRandall Stewart 			TAILQ_FOREACH(ctl, &stcb->sctp_ep->read_queue, next) {
62858933fa13SRandall Stewart 				if ((ctl->sinfo_stream == stseq->stream) &&
62868933fa13SRandall Stewart 				    (ctl->sinfo_ssn == stseq->sequence)) {
62878933fa13SRandall Stewart 					str_seq = (stseq->stream << 16) | stseq->sequence;
62888933fa13SRandall Stewart 					ctl->end_added = 1;
62898933fa13SRandall Stewart 					ctl->pdapi_aborted = 1;
62908933fa13SRandall Stewart 					sv = stcb->asoc.control_pdapi;
62918933fa13SRandall Stewart 					stcb->asoc.control_pdapi = ctl;
62928933fa13SRandall Stewart 					sctp_notify_partial_delivery_indication(stcb,
62938933fa13SRandall Stewart 					    SCTP_PARTIAL_DELIVERY_ABORTED,
62948933fa13SRandall Stewart 					    SCTP_HOLDS_LOCK,
62958933fa13SRandall Stewart 					    str_seq);
62968933fa13SRandall Stewart 					stcb->asoc.control_pdapi = sv;
62978933fa13SRandall Stewart 					break;
62988933fa13SRandall Stewart 				} else if ((ctl->sinfo_stream == stseq->stream) &&
62998933fa13SRandall Stewart 				    (compare_with_wrap(ctl->sinfo_ssn, stseq->sequence, MAX_SEQ))) {
63008933fa13SRandall Stewart 					/* We are past our victim SSN */
63018933fa13SRandall Stewart 					break;
63028933fa13SRandall Stewart 				}
63038933fa13SRandall Stewart 			}
6304851b7298SRandall Stewart 			strm = &asoc->strmin[stseq->stream];
6305851b7298SRandall Stewart 			if (compare_with_wrap(stseq->sequence,
6306f8829a4aSRandall Stewart 			    strm->last_sequence_delivered, MAX_SEQ)) {
6307f8829a4aSRandall Stewart 				/* Update the sequence number */
6308f8829a4aSRandall Stewart 				strm->last_sequence_delivered =
6309851b7298SRandall Stewart 				    stseq->sequence;
6310f8829a4aSRandall Stewart 			}
6311f8829a4aSRandall Stewart 			/* now kick the stream the new way */
631204ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
6313f8829a4aSRandall Stewart 			sctp_kick_prsctp_reorder_queue(stcb, strm);
6314f8829a4aSRandall Stewart 		}
63158933fa13SRandall Stewart 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
6316f8829a4aSRandall Stewart 	}
6317139bc87fSRandall Stewart 	if (TAILQ_FIRST(&asoc->reasmqueue)) {
6318139bc87fSRandall Stewart 		/* now lets kick out and check for more fragmented delivery */
631904ee05e8SRandall Stewart 		/* sa_ignore NO_NULL_CHK */
6320139bc87fSRandall Stewart 		sctp_deliver_reasm_check(stcb, &stcb->asoc);
6321139bc87fSRandall Stewart 	}
6322f8829a4aSRandall Stewart }
6323830d754dSRandall Stewart 
6324830d754dSRandall Stewart /* EY fully identical to sctp_express_handle_sack, duplicated for only naming convention */
6325830d754dSRandall Stewart void
6326830d754dSRandall Stewart sctp_express_handle_nr_sack(struct sctp_tcb *stcb, uint32_t cumack,
6327830d754dSRandall Stewart     uint32_t rwnd, int nonce_sum_flag, int *abort_now)
6328830d754dSRandall Stewart {
6329830d754dSRandall Stewart 	struct sctp_nets *net;
6330830d754dSRandall Stewart 	struct sctp_association *asoc;
6331830d754dSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
6332830d754dSRandall Stewart 	uint32_t old_rwnd;
6333830d754dSRandall Stewart 	int win_probe_recovery = 0;
6334830d754dSRandall Stewart 	int win_probe_recovered = 0;
6335830d754dSRandall Stewart 	int j, done_once = 0;
6336830d754dSRandall Stewart 
6337830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
6338830d754dSRandall Stewart 		sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
6339830d754dSRandall Stewart 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
6340830d754dSRandall Stewart 	}
6341830d754dSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
6342830d754dSRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
6343830d754dSRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
6344830d754dSRandall Stewart 	stcb->asoc.cumack_log_at++;
6345830d754dSRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
6346830d754dSRandall Stewart 		stcb->asoc.cumack_log_at = 0;
6347830d754dSRandall Stewart 	}
6348830d754dSRandall Stewart #endif
6349830d754dSRandall Stewart 	asoc = &stcb->asoc;
6350830d754dSRandall Stewart 	old_rwnd = asoc->peers_rwnd;
6351830d754dSRandall Stewart 	if (compare_with_wrap(asoc->last_acked_seq, cumack, MAX_TSN)) {
6352830d754dSRandall Stewart 		/* old ack */
6353830d754dSRandall Stewart 		return;
6354830d754dSRandall Stewart 	} else if (asoc->last_acked_seq == cumack) {
6355830d754dSRandall Stewart 		/* Window update sack */
6356830d754dSRandall Stewart 		asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
6357830d754dSRandall Stewart 		    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
6358830d754dSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6359830d754dSRandall Stewart 			/* SWS sender side engages */
6360830d754dSRandall Stewart 			asoc->peers_rwnd = 0;
6361830d754dSRandall Stewart 		}
6362830d754dSRandall Stewart 		if (asoc->peers_rwnd > old_rwnd) {
6363830d754dSRandall Stewart 			goto again;
6364830d754dSRandall Stewart 		}
6365830d754dSRandall Stewart 		return;
6366830d754dSRandall Stewart 	}
6367830d754dSRandall Stewart 	/* First setup for CC stuff */
6368830d754dSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6369830d754dSRandall Stewart 		net->prev_cwnd = net->cwnd;
6370830d754dSRandall Stewart 		net->net_ack = 0;
6371830d754dSRandall Stewart 		net->net_ack2 = 0;
6372830d754dSRandall Stewart 
6373830d754dSRandall Stewart 		/*
6374830d754dSRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
6375830d754dSRandall Stewart 		 * SACK processing
6376830d754dSRandall Stewart 		 */
6377830d754dSRandall Stewart 		net->new_pseudo_cumack = 0;
6378830d754dSRandall Stewart 		net->will_exit_fast_recovery = 0;
6379830d754dSRandall Stewart 	}
6380830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
6381830d754dSRandall Stewart 		uint32_t send_s;
6382830d754dSRandall Stewart 
6383830d754dSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
6384830d754dSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
6385830d754dSRandall Stewart 			    sctpchunk_listhead);
6386830d754dSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
6387830d754dSRandall Stewart 		} else {
6388830d754dSRandall Stewart 			send_s = asoc->sending_seq;
6389830d754dSRandall Stewart 		}
6390830d754dSRandall Stewart 		if ((cumack == send_s) ||
6391830d754dSRandall Stewart 		    compare_with_wrap(cumack, send_s, MAX_TSN)) {
6392830d754dSRandall Stewart #ifndef INVARIANTS
6393830d754dSRandall Stewart 			struct mbuf *oper;
6394830d754dSRandall Stewart 
6395830d754dSRandall Stewart #endif
6396830d754dSRandall Stewart #ifdef INVARIANTS
6397830d754dSRandall Stewart 			panic("Impossible sack 1");
6398830d754dSRandall Stewart #else
6399830d754dSRandall Stewart 			*abort_now = 1;
6400830d754dSRandall Stewart 			/* XXX */
6401830d754dSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
6402830d754dSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
6403830d754dSRandall Stewart 			if (oper) {
6404830d754dSRandall Stewart 				struct sctp_paramhdr *ph;
6405830d754dSRandall Stewart 				uint32_t *ippp;
6406830d754dSRandall Stewart 
6407830d754dSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
6408830d754dSRandall Stewart 				    sizeof(uint32_t);
6409830d754dSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
6410830d754dSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
6411830d754dSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
6412830d754dSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
6413830d754dSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
6414830d754dSRandall Stewart 			}
6415830d754dSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
6416830d754dSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
6417830d754dSRandall Stewart 			return;
6418830d754dSRandall Stewart #endif
6419830d754dSRandall Stewart 		}
6420830d754dSRandall Stewart 	}
6421830d754dSRandall Stewart 	asoc->this_sack_highest_gap = cumack;
6422830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
6423830d754dSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
6424830d754dSRandall Stewart 		    stcb->asoc.overall_error_count,
6425830d754dSRandall Stewart 		    0,
6426830d754dSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
6427830d754dSRandall Stewart 		    __LINE__);
6428830d754dSRandall Stewart 	}
6429830d754dSRandall Stewart 	stcb->asoc.overall_error_count = 0;
6430830d754dSRandall Stewart 	if (compare_with_wrap(cumack, asoc->last_acked_seq, MAX_TSN)) {
6431830d754dSRandall Stewart 		/* process the new consecutive TSN first */
6432830d754dSRandall Stewart 		tp1 = TAILQ_FIRST(&asoc->sent_queue);
6433830d754dSRandall Stewart 		while (tp1) {
6434830d754dSRandall Stewart 			tp2 = TAILQ_NEXT(tp1, sctp_next);
6435830d754dSRandall Stewart 			if (compare_with_wrap(cumack, tp1->rec.data.TSN_seq,
6436830d754dSRandall Stewart 			    MAX_TSN) ||
6437830d754dSRandall Stewart 			    cumack == tp1->rec.data.TSN_seq) {
6438830d754dSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
6439830d754dSRandall Stewart 					printf("Warning, an unsent is now acked?\n");
6440830d754dSRandall Stewart 				}
6441830d754dSRandall Stewart 				/*
6442830d754dSRandall Stewart 				 * ECN Nonce: Add the nonce to the sender's
6443830d754dSRandall Stewart 				 * nonce sum
6444830d754dSRandall Stewart 				 */
6445830d754dSRandall Stewart 				asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
6446830d754dSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
6447830d754dSRandall Stewart 					/*
6448830d754dSRandall Stewart 					 * If it is less than ACKED, it is
6449830d754dSRandall Stewart 					 * now no-longer in flight. Higher
6450830d754dSRandall Stewart 					 * values may occur during marking
6451830d754dSRandall Stewart 					 */
6452830d754dSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
6453830d754dSRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
6454830d754dSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
6455830d754dSRandall Stewart 							    tp1->whoTo->flight_size,
6456830d754dSRandall Stewart 							    tp1->book_size,
6457830d754dSRandall Stewart 							    (uintptr_t) tp1->whoTo,
6458830d754dSRandall Stewart 							    tp1->rec.data.TSN_seq);
6459830d754dSRandall Stewart 						}
6460830d754dSRandall Stewart 						sctp_flight_size_decrease(tp1);
6461830d754dSRandall Stewart 						/* sa_ignore NO_NULL_CHK */
6462830d754dSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
6463830d754dSRandall Stewart 					}
6464830d754dSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
6465830d754dSRandall Stewart 					if (tp1->snd_count < 2) {
6466830d754dSRandall Stewart 						/*
6467830d754dSRandall Stewart 						 * True non-retransmited
6468830d754dSRandall Stewart 						 * chunk
6469830d754dSRandall Stewart 						 */
6470830d754dSRandall Stewart 						tp1->whoTo->net_ack2 +=
6471830d754dSRandall Stewart 						    tp1->send_size;
6472830d754dSRandall Stewart 
6473830d754dSRandall Stewart 						/* update RTO too? */
6474830d754dSRandall Stewart 						if (tp1->do_rtt) {
6475830d754dSRandall Stewart 							tp1->whoTo->RTO =
6476830d754dSRandall Stewart 							/*
6477830d754dSRandall Stewart 							 * sa_ignore
6478830d754dSRandall Stewart 							 * NO_NULL_CHK
6479830d754dSRandall Stewart 							 */
6480830d754dSRandall Stewart 							    sctp_calculate_rto(stcb,
6481830d754dSRandall Stewart 							    asoc, tp1->whoTo,
6482830d754dSRandall Stewart 							    &tp1->sent_rcv_time,
6483830d754dSRandall Stewart 							    sctp_align_safe_nocopy);
6484830d754dSRandall Stewart 							tp1->do_rtt = 0;
6485830d754dSRandall Stewart 						}
6486830d754dSRandall Stewart 					}
6487830d754dSRandall Stewart 					/*
6488830d754dSRandall Stewart 					 * CMT: CUCv2 algorithm. From the
6489830d754dSRandall Stewart 					 * cumack'd TSNs, for each TSN being
6490830d754dSRandall Stewart 					 * acked for the first time, set the
6491830d754dSRandall Stewart 					 * following variables for the
6492830d754dSRandall Stewart 					 * corresp destination.
6493830d754dSRandall Stewart 					 * new_pseudo_cumack will trigger a
6494830d754dSRandall Stewart 					 * cwnd update.
6495830d754dSRandall Stewart 					 * find_(rtx_)pseudo_cumack will
6496830d754dSRandall Stewart 					 * trigger search for the next
6497830d754dSRandall Stewart 					 * expected (rtx-)pseudo-cumack.
6498830d754dSRandall Stewart 					 */
6499830d754dSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
6500830d754dSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
6501830d754dSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
6502830d754dSRandall Stewart 
6503830d754dSRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
6504830d754dSRandall Stewart 						/* sa_ignore NO_NULL_CHK */
6505830d754dSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
6506830d754dSRandall Stewart 					}
6507830d754dSRandall Stewart 				}
6508830d754dSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
6509830d754dSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
6510830d754dSRandall Stewart 				}
6511830d754dSRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
6512830d754dSRandall Stewart 					/* deflate the cwnd */
6513830d754dSRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
6514830d754dSRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
6515830d754dSRandall Stewart 				}
6516830d754dSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
6517830d754dSRandall Stewart 				TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
6518830d754dSRandall Stewart 				if (tp1->data) {
6519830d754dSRandall Stewart 					/* sa_ignore NO_NULL_CHK */
6520830d754dSRandall Stewart 					sctp_free_bufspace(stcb, asoc, tp1, 1);
6521830d754dSRandall Stewart 					sctp_m_freem(tp1->data);
6522830d754dSRandall Stewart 				}
6523830d754dSRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
6524830d754dSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
6525830d754dSRandall Stewart 					    cumack,
6526830d754dSRandall Stewart 					    tp1->rec.data.TSN_seq,
6527830d754dSRandall Stewart 					    0,
6528830d754dSRandall Stewart 					    0,
6529830d754dSRandall Stewart 					    SCTP_LOG_FREE_SENT);
6530830d754dSRandall Stewart 				}
6531830d754dSRandall Stewart 				tp1->data = NULL;
6532830d754dSRandall Stewart 				asoc->sent_queue_cnt--;
6533830d754dSRandall Stewart 				sctp_free_a_chunk(stcb, tp1);
6534830d754dSRandall Stewart 				tp1 = tp2;
6535830d754dSRandall Stewart 			} else {
6536830d754dSRandall Stewart 				break;
6537830d754dSRandall Stewart 			}
6538830d754dSRandall Stewart 		}
6539830d754dSRandall Stewart 
6540830d754dSRandall Stewart 	}
6541830d754dSRandall Stewart 	/* sa_ignore NO_NULL_CHK */
6542830d754dSRandall Stewart 	if (stcb->sctp_socket) {
6543830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
6544830d754dSRandall Stewart 		struct socket *so;
6545830d754dSRandall Stewart 
6546830d754dSRandall Stewart #endif
6547830d754dSRandall Stewart 
6548830d754dSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
6549830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
6550830d754dSRandall Stewart 			/* sa_ignore NO_NULL_CHK */
6551830d754dSRandall Stewart 			sctp_wakeup_log(stcb, cumack, 1, SCTP_WAKESND_FROM_SACK);
6552830d754dSRandall Stewart 		}
6553830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
6554830d754dSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
6555830d754dSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
6556830d754dSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
6557830d754dSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
6558830d754dSRandall Stewart 		SCTP_TCB_LOCK(stcb);
6559830d754dSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
6560830d754dSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
6561830d754dSRandall Stewart 			/* assoc was freed while we were unlocked */
6562830d754dSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
6563830d754dSRandall Stewart 			return;
6564830d754dSRandall Stewart 		}
6565830d754dSRandall Stewart #endif
6566830d754dSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
6567830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
6568830d754dSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
6569830d754dSRandall Stewart #endif
6570830d754dSRandall Stewart 	} else {
6571830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
6572830d754dSRandall Stewart 			sctp_wakeup_log(stcb, cumack, 1, SCTP_NOWAKE_FROM_SACK);
6573830d754dSRandall Stewart 		}
6574830d754dSRandall Stewart 	}
6575830d754dSRandall Stewart 
6576830d754dSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
6577830d754dSRandall Stewart 	if (asoc->last_acked_seq != cumack)
6578830d754dSRandall Stewart 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
6579830d754dSRandall Stewart 
6580830d754dSRandall Stewart 	asoc->last_acked_seq = cumack;
6581830d754dSRandall Stewart 
6582830d754dSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
6583830d754dSRandall Stewart 		/* nothing left in-flight */
6584830d754dSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6585830d754dSRandall Stewart 			net->flight_size = 0;
6586830d754dSRandall Stewart 			net->partial_bytes_acked = 0;
6587830d754dSRandall Stewart 		}
6588830d754dSRandall Stewart 		asoc->total_flight = 0;
6589830d754dSRandall Stewart 		asoc->total_flight_count = 0;
6590830d754dSRandall Stewart 	}
6591830d754dSRandall Stewart 	/* Fix up the a-p-a-p for future PR-SCTP sends */
6592830d754dSRandall Stewart 	if (compare_with_wrap(cumack, asoc->advanced_peer_ack_point, MAX_TSN)) {
6593830d754dSRandall Stewart 		asoc->advanced_peer_ack_point = cumack;
6594830d754dSRandall Stewart 	}
6595830d754dSRandall Stewart 	/* ECN Nonce updates */
6596830d754dSRandall Stewart 	if (asoc->ecn_nonce_allowed) {
6597830d754dSRandall Stewart 		if (asoc->nonce_sum_check) {
6598830d754dSRandall Stewart 			if (nonce_sum_flag != ((asoc->nonce_sum_expect_base) & SCTP_SACK_NONCE_SUM)) {
6599830d754dSRandall Stewart 				if (asoc->nonce_wait_for_ecne == 0) {
6600830d754dSRandall Stewart 					struct sctp_tmit_chunk *lchk;
6601830d754dSRandall Stewart 
6602830d754dSRandall Stewart 					lchk = TAILQ_FIRST(&asoc->send_queue);
6603830d754dSRandall Stewart 					asoc->nonce_wait_for_ecne = 1;
6604830d754dSRandall Stewart 					if (lchk) {
6605830d754dSRandall Stewart 						asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
6606830d754dSRandall Stewart 					} else {
6607830d754dSRandall Stewart 						asoc->nonce_wait_tsn = asoc->sending_seq;
6608830d754dSRandall Stewart 					}
6609830d754dSRandall Stewart 				} else {
6610830d754dSRandall Stewart 					if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
6611830d754dSRandall Stewart 					    (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
6612830d754dSRandall Stewart 						/*
6613830d754dSRandall Stewart 						 * Misbehaving peer. We need
6614830d754dSRandall Stewart 						 * to react to this guy
6615830d754dSRandall Stewart 						 */
6616830d754dSRandall Stewart 						asoc->ecn_allowed = 0;
6617830d754dSRandall Stewart 						asoc->ecn_nonce_allowed = 0;
6618830d754dSRandall Stewart 					}
6619830d754dSRandall Stewart 				}
6620830d754dSRandall Stewart 			}
6621830d754dSRandall Stewart 		} else {
6622830d754dSRandall Stewart 			/* See if Resynchronization Possible */
6623830d754dSRandall Stewart 			if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
6624830d754dSRandall Stewart 				asoc->nonce_sum_check = 1;
6625830d754dSRandall Stewart 				/*
6626830d754dSRandall Stewart 				 * now we must calculate what the base is.
6627830d754dSRandall Stewart 				 * We do this based on two things, we know
6628830d754dSRandall Stewart 				 * the total's for all the segments
6629830d754dSRandall Stewart 				 * gap-acked in the SACK (none), We also
6630830d754dSRandall Stewart 				 * know the SACK's nonce sum, its in
6631830d754dSRandall Stewart 				 * nonce_sum_flag. So we can build a truth
6632830d754dSRandall Stewart 				 * table to back-calculate the new value of
6633830d754dSRandall Stewart 				 * asoc->nonce_sum_expect_base:
6634830d754dSRandall Stewart 				 *
6635830d754dSRandall Stewart 				 * SACK-flag-Value         Seg-Sums Base 0 0 0
6636830d754dSRandall Stewart 				 * 1                    0 1 0 1 1 1 1 0
6637830d754dSRandall Stewart 				 */
6638830d754dSRandall Stewart 				asoc->nonce_sum_expect_base = (0 ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
6639830d754dSRandall Stewart 			}
6640830d754dSRandall Stewart 		}
6641830d754dSRandall Stewart 	}
6642830d754dSRandall Stewart 	/* RWND update */
6643830d754dSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
6644830d754dSRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
6645830d754dSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6646830d754dSRandall Stewart 		/* SWS sender side engages */
6647830d754dSRandall Stewart 		asoc->peers_rwnd = 0;
6648830d754dSRandall Stewart 	}
6649830d754dSRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
6650830d754dSRandall Stewart 		win_probe_recovery = 1;
6651830d754dSRandall Stewart 	}
6652830d754dSRandall Stewart 	/* Now assure a timer where data is queued at */
6653830d754dSRandall Stewart again:
6654830d754dSRandall Stewart 	j = 0;
6655830d754dSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
66565171328bSRandall Stewart 		int to_ticks;
66575171328bSRandall Stewart 
6658830d754dSRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
6659830d754dSRandall Stewart 			win_probe_recovered = 1;
6660830d754dSRandall Stewart 			/*
6661830d754dSRandall Stewart 			 * Find first chunk that was used with window probe
6662830d754dSRandall Stewart 			 * and clear the sent
6663830d754dSRandall Stewart 			 */
6664830d754dSRandall Stewart 			/* sa_ignore FREED_MEMORY */
6665830d754dSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
6666830d754dSRandall Stewart 				if (tp1->window_probe) {
6667830d754dSRandall Stewart 					/* move back to data send queue */
6668830d754dSRandall Stewart 					sctp_window_probe_recovery(stcb, asoc, net, tp1);
6669830d754dSRandall Stewart 					break;
6670830d754dSRandall Stewart 				}
6671830d754dSRandall Stewart 			}
6672830d754dSRandall Stewart 		}
6673830d754dSRandall Stewart 		if (net->RTO == 0) {
6674830d754dSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
6675830d754dSRandall Stewart 		} else {
6676830d754dSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
6677830d754dSRandall Stewart 		}
66785171328bSRandall Stewart 		if (net->flight_size) {
66795171328bSRandall Stewart 
6680830d754dSRandall Stewart 			j++;
6681830d754dSRandall Stewart 			(void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
6682830d754dSRandall Stewart 			    sctp_timeout_handler, &net->rxt_timer);
66835171328bSRandall Stewart 			if (net->window_probe) {
66845171328bSRandall Stewart 				net->window_probe = 0;
66855171328bSRandall Stewart 			}
6686830d754dSRandall Stewart 		} else {
66875171328bSRandall Stewart 			if (net->window_probe) {
66885171328bSRandall Stewart 				/*
66895171328bSRandall Stewart 				 * In window probes we must assure a timer
66905171328bSRandall Stewart 				 * is still running there
66915171328bSRandall Stewart 				 */
66925171328bSRandall Stewart 				net->window_probe = 0;
66935171328bSRandall Stewart 				(void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
66945171328bSRandall Stewart 				    sctp_timeout_handler, &net->rxt_timer);
66955171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
6696830d754dSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
6697830d754dSRandall Stewart 				    stcb, net,
6698830d754dSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
6699830d754dSRandall Stewart 			}
6700830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
6701830d754dSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
6702830d754dSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
6703830d754dSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
6704830d754dSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
6705830d754dSRandall Stewart 				}
6706830d754dSRandall Stewart 			}
6707830d754dSRandall Stewart 		}
6708830d754dSRandall Stewart 	}
6709830d754dSRandall Stewart 	if ((j == 0) &&
6710830d754dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
6711830d754dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
6712830d754dSRandall Stewart 	    (win_probe_recovered == 0) &&
6713830d754dSRandall Stewart 	    (done_once == 0)) {
67140c0982b8SRandall Stewart 		/*
67150c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
67160c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
67170c0982b8SRandall Stewart 		 */
67180c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
6719830d754dSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6720830d754dSRandall Stewart 				net->flight_size = 0;
6721830d754dSRandall Stewart 			}
6722830d754dSRandall Stewart 			asoc->total_flight = 0;
6723830d754dSRandall Stewart 			asoc->total_flight_count = 0;
6724830d754dSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
6725830d754dSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
6726830d754dSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
6727830d754dSRandall Stewart 					sctp_flight_size_increase(tp1);
6728830d754dSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
6729830d754dSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
6730830d754dSRandall Stewart 					asoc->sent_queue_retran_cnt++;
6731830d754dSRandall Stewart 				}
6732830d754dSRandall Stewart 			}
67330c0982b8SRandall Stewart 		}
6734830d754dSRandall Stewart 		done_once = 1;
6735830d754dSRandall Stewart 		goto again;
6736830d754dSRandall Stewart 	}
6737830d754dSRandall Stewart 	/**********************************/
6738830d754dSRandall Stewart 	/* Now what about shutdown issues */
6739830d754dSRandall Stewart 	/**********************************/
6740830d754dSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
6741830d754dSRandall Stewart 		/* nothing left on sendqueue.. consider done */
6742830d754dSRandall Stewart 		/* clean up */
6743830d754dSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
6744830d754dSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
6745830d754dSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
6746830d754dSRandall Stewart 		    (asoc->locked_on_sending)
6747830d754dSRandall Stewart 		    ) {
6748830d754dSRandall Stewart 			struct sctp_stream_queue_pending *sp;
6749830d754dSRandall Stewart 
6750830d754dSRandall Stewart 			/*
6751830d754dSRandall Stewart 			 * I may be in a state where we got all across.. but
6752830d754dSRandall Stewart 			 * cannot write more due to a shutdown... we abort
6753830d754dSRandall Stewart 			 * since the user did not indicate EOR in this case.
6754830d754dSRandall Stewart 			 * The sp will be cleaned during free of the asoc.
6755830d754dSRandall Stewart 			 */
6756830d754dSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
6757830d754dSRandall Stewart 			    sctp_streamhead);
6758830d754dSRandall Stewart 			if ((sp) && (sp->length == 0)) {
6759830d754dSRandall Stewart 				/* Let cleanup code purge it */
6760830d754dSRandall Stewart 				if (sp->msg_is_complete) {
6761830d754dSRandall Stewart 					asoc->stream_queue_cnt--;
6762830d754dSRandall Stewart 				} else {
6763830d754dSRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
6764830d754dSRandall Stewart 					asoc->locked_on_sending = NULL;
6765830d754dSRandall Stewart 					asoc->stream_queue_cnt--;
6766830d754dSRandall Stewart 				}
6767830d754dSRandall Stewart 			}
6768830d754dSRandall Stewart 		}
6769830d754dSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
6770830d754dSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
6771830d754dSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
6772830d754dSRandall Stewart 				/* Need to abort here */
6773830d754dSRandall Stewart 				struct mbuf *oper;
6774830d754dSRandall Stewart 
6775830d754dSRandall Stewart 		abort_out_now:
6776830d754dSRandall Stewart 				*abort_now = 1;
6777830d754dSRandall Stewart 				/* XXX */
6778830d754dSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
6779830d754dSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
6780830d754dSRandall Stewart 				if (oper) {
6781830d754dSRandall Stewart 					struct sctp_paramhdr *ph;
6782830d754dSRandall Stewart 					uint32_t *ippp;
6783830d754dSRandall Stewart 
6784830d754dSRandall Stewart 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
6785830d754dSRandall Stewart 					    sizeof(uint32_t);
6786830d754dSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
6787830d754dSRandall Stewart 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6788830d754dSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
6789830d754dSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
6790830d754dSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_24);
6791830d754dSRandall Stewart 				}
6792830d754dSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
6793830d754dSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
6794830d754dSRandall Stewart 			} else {
6795830d754dSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
6796830d754dSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
6797830d754dSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6798830d754dSRandall Stewart 				}
6799830d754dSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
6800830d754dSRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6801830d754dSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
6802830d754dSRandall Stewart 				sctp_send_shutdown(stcb,
6803830d754dSRandall Stewart 				    stcb->asoc.primary_destination);
6804830d754dSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
6805830d754dSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
6806830d754dSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
6807830d754dSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
6808830d754dSRandall Stewart 			}
6809830d754dSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
6810830d754dSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
6811830d754dSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
6812830d754dSRandall Stewart 				goto abort_out_now;
6813830d754dSRandall Stewart 			}
6814830d754dSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6815830d754dSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
6816830d754dSRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6817830d754dSRandall Stewart 			sctp_send_shutdown_ack(stcb,
6818830d754dSRandall Stewart 			    stcb->asoc.primary_destination);
6819830d754dSRandall Stewart 
6820830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
6821830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, asoc->primary_destination);
6822830d754dSRandall Stewart 		}
6823830d754dSRandall Stewart 	}
6824830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
6825830d754dSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
6826830d754dSRandall Stewart 		    rwnd,
6827830d754dSRandall Stewart 		    stcb->asoc.peers_rwnd,
6828830d754dSRandall Stewart 		    stcb->asoc.total_flight,
6829830d754dSRandall Stewart 		    stcb->asoc.total_output_queue_size);
6830830d754dSRandall Stewart 	}
6831830d754dSRandall Stewart }
6832830d754dSRandall Stewart 
6833830d754dSRandall Stewart /* EY! nr_sack version of sctp_handle_segments, nr-gapped TSNs get removed from RtxQ in this method*/
6834830d754dSRandall Stewart static void
6835830d754dSRandall Stewart sctp_handle_nr_sack_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
6836830d754dSRandall Stewart     struct sctp_nr_sack_chunk *ch, uint32_t last_tsn, uint32_t * biggest_tsn_acked,
6837830d754dSRandall Stewart     uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
6838830d754dSRandall Stewart     uint32_t num_seg, uint32_t num_nr_seg, int *ecn_seg_sums)
6839830d754dSRandall Stewart {
6840830d754dSRandall Stewart 	/************************************************/
6841830d754dSRandall Stewart 	/* process fragments and update sendqueue        */
6842830d754dSRandall Stewart 	/************************************************/
6843830d754dSRandall Stewart 	struct sctp_nr_sack *nr_sack;
6844830d754dSRandall Stewart 	struct sctp_gap_ack_block *frag, block;
6845830d754dSRandall Stewart 	struct sctp_nr_gap_ack_block *nr_frag, nr_block;
6846830d754dSRandall Stewart 	struct sctp_tmit_chunk *tp1;
6847d50c1d79SRandall Stewart 	uint32_t i, j;
6848830d754dSRandall Stewart 	int wake_him = 0;
6849830d754dSRandall Stewart 	uint32_t theTSN;
6850830d754dSRandall Stewart 	int num_frs = 0;
6851830d754dSRandall Stewart 
6852830d754dSRandall Stewart 	uint16_t frag_strt, frag_end, primary_flag_set;
6853830d754dSRandall Stewart 	uint16_t nr_frag_strt, nr_frag_end;
6854830d754dSRandall Stewart 
6855830d754dSRandall Stewart 	uint32_t last_frag_high;
6856830d754dSRandall Stewart 	uint32_t last_nr_frag_high;
6857830d754dSRandall Stewart 
6858830d754dSRandall Stewart 	/*
6859830d754dSRandall Stewart 	 * @@@ JRI : TODO: This flag is not used anywhere .. remove?
6860830d754dSRandall Stewart 	 */
6861830d754dSRandall Stewart 	if (asoc->primary_destination->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
6862830d754dSRandall Stewart 		primary_flag_set = 1;
6863830d754dSRandall Stewart 	} else {
6864830d754dSRandall Stewart 		primary_flag_set = 0;
6865830d754dSRandall Stewart 	}
6866830d754dSRandall Stewart 	nr_sack = &ch->nr_sack;
6867830d754dSRandall Stewart 
6868830d754dSRandall Stewart 	/*
6869830d754dSRandall Stewart 	 * EY! - I will process nr_gaps similarly,by going to this position
6870830d754dSRandall Stewart 	 * again if All bit is set
6871830d754dSRandall Stewart 	 */
6872830d754dSRandall Stewart 	frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
6873830d754dSRandall Stewart 	    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
6874830d754dSRandall Stewart 	*offset += sizeof(block);
6875830d754dSRandall Stewart 	if (frag == NULL) {
6876830d754dSRandall Stewart 		return;
6877830d754dSRandall Stewart 	}
6878830d754dSRandall Stewart 	tp1 = NULL;
6879830d754dSRandall Stewart 	last_frag_high = 0;
6880830d754dSRandall Stewart 	for (i = 0; i < num_seg; i++) {
6881830d754dSRandall Stewart 		frag_strt = ntohs(frag->start);
6882830d754dSRandall Stewart 		frag_end = ntohs(frag->end);
6883830d754dSRandall Stewart 		/* some sanity checks on the fargment offsets */
6884830d754dSRandall Stewart 		if (frag_strt > frag_end) {
6885830d754dSRandall Stewart 			/* this one is malformed, skip */
6886830d754dSRandall Stewart 			frag++;
6887830d754dSRandall Stewart 			continue;
6888830d754dSRandall Stewart 		}
6889830d754dSRandall Stewart 		if (compare_with_wrap((frag_end + last_tsn), *biggest_tsn_acked,
6890830d754dSRandall Stewart 		    MAX_TSN))
6891830d754dSRandall Stewart 			*biggest_tsn_acked = frag_end + last_tsn;
6892830d754dSRandall Stewart 
6893830d754dSRandall Stewart 		/* mark acked dgs and find out the highestTSN being acked */
6894830d754dSRandall Stewart 		if (tp1 == NULL) {
6895830d754dSRandall Stewart 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
6896830d754dSRandall Stewart 
6897830d754dSRandall Stewart 			/* save the locations of the last frags */
6898830d754dSRandall Stewart 			last_frag_high = frag_end + last_tsn;
6899830d754dSRandall Stewart 		} else {
6900830d754dSRandall Stewart 			/*
6901830d754dSRandall Stewart 			 * now lets see if we need to reset the queue due to
6902830d754dSRandall Stewart 			 * a out-of-order SACK fragment
6903830d754dSRandall Stewart 			 */
6904830d754dSRandall Stewart 			if (compare_with_wrap(frag_strt + last_tsn,
6905830d754dSRandall Stewart 			    last_frag_high, MAX_TSN)) {
6906830d754dSRandall Stewart 				/*
6907830d754dSRandall Stewart 				 * if the new frag starts after the last TSN
6908830d754dSRandall Stewart 				 * frag covered, we are ok and this one is
6909830d754dSRandall Stewart 				 * beyond the last one
6910830d754dSRandall Stewart 				 */
6911830d754dSRandall Stewart 				;
6912830d754dSRandall Stewart 			} else {
6913830d754dSRandall Stewart 				/*
6914830d754dSRandall Stewart 				 * ok, they have reset us, so we need to
6915830d754dSRandall Stewart 				 * reset the queue this will cause extra
6916830d754dSRandall Stewart 				 * hunting but hey, they chose the
6917830d754dSRandall Stewart 				 * performance hit when they failed to order
6918830d754dSRandall Stewart 				 * there gaps..
6919830d754dSRandall Stewart 				 */
6920830d754dSRandall Stewart 				tp1 = TAILQ_FIRST(&asoc->sent_queue);
6921830d754dSRandall Stewart 			}
6922830d754dSRandall Stewart 			last_frag_high = frag_end + last_tsn;
6923830d754dSRandall Stewart 		}
6924830d754dSRandall Stewart 		for (j = frag_strt; j <= frag_end; j++) {
6925830d754dSRandall Stewart 			theTSN = j + last_tsn;
6926830d754dSRandall Stewart 			while (tp1) {
6927830d754dSRandall Stewart 				if (tp1->rec.data.doing_fast_retransmit)
6928830d754dSRandall Stewart 					num_frs++;
6929830d754dSRandall Stewart 
6930830d754dSRandall Stewart 				/*
6931830d754dSRandall Stewart 				 * CMT: CUCv2 algorithm. For each TSN being
6932830d754dSRandall Stewart 				 * processed from the sent queue, track the
6933830d754dSRandall Stewart 				 * next expected pseudo-cumack, or
6934830d754dSRandall Stewart 				 * rtx_pseudo_cumack, if required. Separate
6935830d754dSRandall Stewart 				 * cumack trackers for first transmissions,
6936830d754dSRandall Stewart 				 * and retransmissions.
6937830d754dSRandall Stewart 				 */
6938830d754dSRandall Stewart 				if ((tp1->whoTo->find_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
6939830d754dSRandall Stewart 				    (tp1->snd_count == 1)) {
6940830d754dSRandall Stewart 					tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
6941830d754dSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 0;
6942830d754dSRandall Stewart 				}
6943830d754dSRandall Stewart 				if ((tp1->whoTo->find_rtx_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
6944830d754dSRandall Stewart 				    (tp1->snd_count > 1)) {
6945830d754dSRandall Stewart 					tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
6946830d754dSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 0;
6947830d754dSRandall Stewart 				}
6948830d754dSRandall Stewart 				if (tp1->rec.data.TSN_seq == theTSN) {
6949830d754dSRandall Stewart 					if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
6950830d754dSRandall Stewart 						/*
6951830d754dSRandall Stewart 						 * must be held until
6952830d754dSRandall Stewart 						 * cum-ack passes
6953830d754dSRandall Stewart 						 */
6954830d754dSRandall Stewart 						/*
6955830d754dSRandall Stewart 						 * ECN Nonce: Add the nonce
6956830d754dSRandall Stewart 						 * value to the sender's
6957830d754dSRandall Stewart 						 * nonce sum
6958830d754dSRandall Stewart 						 */
6959830d754dSRandall Stewart 						if (tp1->sent < SCTP_DATAGRAM_RESEND) {
6960830d754dSRandall Stewart 							/*-
6961830d754dSRandall Stewart 						         * If it is less than RESEND, it is
6962830d754dSRandall Stewart 						         * now no-longer in flight.
6963830d754dSRandall Stewart 						         * Higher values may already be set
6964830d754dSRandall Stewart 						         * via previous Gap Ack Blocks...
6965830d754dSRandall Stewart 						         * i.e. ACKED or RESEND.
6966830d754dSRandall Stewart 						         */
6967830d754dSRandall Stewart 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
6968830d754dSRandall Stewart 							    *biggest_newly_acked_tsn, MAX_TSN)) {
6969830d754dSRandall Stewart 								*biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
6970830d754dSRandall Stewart 							}
6971830d754dSRandall Stewart 							/*
6972830d754dSRandall Stewart 							 * CMT: SFR algo
6973830d754dSRandall Stewart 							 * (and HTNA) - set
6974830d754dSRandall Stewart 							 * saw_newack to 1
6975830d754dSRandall Stewart 							 * for dest being
6976830d754dSRandall Stewart 							 * newly acked.
6977830d754dSRandall Stewart 							 * update
6978830d754dSRandall Stewart 							 * this_sack_highest_
6979830d754dSRandall Stewart 							 * newack if
6980830d754dSRandall Stewart 							 * appropriate.
6981830d754dSRandall Stewart 							 */
6982830d754dSRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0)
6983830d754dSRandall Stewart 								tp1->whoTo->saw_newack = 1;
6984830d754dSRandall Stewart 
6985830d754dSRandall Stewart 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
6986830d754dSRandall Stewart 							    tp1->whoTo->this_sack_highest_newack,
6987830d754dSRandall Stewart 							    MAX_TSN)) {
6988830d754dSRandall Stewart 								tp1->whoTo->this_sack_highest_newack =
6989830d754dSRandall Stewart 								    tp1->rec.data.TSN_seq;
6990830d754dSRandall Stewart 							}
6991830d754dSRandall Stewart 							/*
6992830d754dSRandall Stewart 							 * CMT DAC algo:
6993830d754dSRandall Stewart 							 * also update
6994830d754dSRandall Stewart 							 * this_sack_lowest_n
6995830d754dSRandall Stewart 							 * ewack
6996830d754dSRandall Stewart 							 */
6997830d754dSRandall Stewart 							if (*this_sack_lowest_newack == 0) {
6998830d754dSRandall Stewart 								if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
6999830d754dSRandall Stewart 									sctp_log_sack(*this_sack_lowest_newack,
7000830d754dSRandall Stewart 									    last_tsn,
7001830d754dSRandall Stewart 									    tp1->rec.data.TSN_seq,
7002830d754dSRandall Stewart 									    0,
7003830d754dSRandall Stewart 									    0,
7004830d754dSRandall Stewart 									    SCTP_LOG_TSN_ACKED);
7005830d754dSRandall Stewart 								}
7006830d754dSRandall Stewart 								*this_sack_lowest_newack = tp1->rec.data.TSN_seq;
7007830d754dSRandall Stewart 							}
7008830d754dSRandall Stewart 							/*
7009830d754dSRandall Stewart 							 * CMT: CUCv2
7010830d754dSRandall Stewart 							 * algorithm. If
7011830d754dSRandall Stewart 							 * (rtx-)pseudo-cumac
7012830d754dSRandall Stewart 							 * k for corresp
7013830d754dSRandall Stewart 							 * dest is being
7014830d754dSRandall Stewart 							 * acked, then we
7015830d754dSRandall Stewart 							 * have a new
7016830d754dSRandall Stewart 							 * (rtx-)pseudo-cumac
7017830d754dSRandall Stewart 							 * k. Set
7018830d754dSRandall Stewart 							 * new_(rtx_)pseudo_c
7019830d754dSRandall Stewart 							 * umack to TRUE so
7020830d754dSRandall Stewart 							 * that the cwnd for
7021830d754dSRandall Stewart 							 * this dest can be
7022830d754dSRandall Stewart 							 * updated. Also
7023830d754dSRandall Stewart 							 * trigger search
7024830d754dSRandall Stewart 							 * for the next
7025830d754dSRandall Stewart 							 * expected
7026830d754dSRandall Stewart 							 * (rtx-)pseudo-cumac
7027830d754dSRandall Stewart 							 * k. Separate
7028830d754dSRandall Stewart 							 * pseudo_cumack
7029830d754dSRandall Stewart 							 * trackers for
7030830d754dSRandall Stewart 							 * first
7031830d754dSRandall Stewart 							 * transmissions and
7032830d754dSRandall Stewart 							 * retransmissions.
7033830d754dSRandall Stewart 							 */
7034830d754dSRandall Stewart 							if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
7035830d754dSRandall Stewart 								if (tp1->rec.data.chunk_was_revoked == 0) {
7036830d754dSRandall Stewart 									tp1->whoTo->new_pseudo_cumack = 1;
7037830d754dSRandall Stewart 								}
7038830d754dSRandall Stewart 								tp1->whoTo->find_pseudo_cumack = 1;
7039830d754dSRandall Stewart 							}
7040830d754dSRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7041830d754dSRandall Stewart 								sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
7042830d754dSRandall Stewart 							}
7043830d754dSRandall Stewart 							if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
7044830d754dSRandall Stewart 								if (tp1->rec.data.chunk_was_revoked == 0) {
7045830d754dSRandall Stewart 									tp1->whoTo->new_pseudo_cumack = 1;
7046830d754dSRandall Stewart 								}
7047830d754dSRandall Stewart 								tp1->whoTo->find_rtx_pseudo_cumack = 1;
7048830d754dSRandall Stewart 							}
7049830d754dSRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
7050830d754dSRandall Stewart 								sctp_log_sack(*biggest_newly_acked_tsn,
7051830d754dSRandall Stewart 								    last_tsn,
7052830d754dSRandall Stewart 								    tp1->rec.data.TSN_seq,
7053830d754dSRandall Stewart 								    frag_strt,
7054830d754dSRandall Stewart 								    frag_end,
7055830d754dSRandall Stewart 								    SCTP_LOG_TSN_ACKED);
7056830d754dSRandall Stewart 							}
7057830d754dSRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
7058830d754dSRandall Stewart 								sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
7059830d754dSRandall Stewart 								    tp1->whoTo->flight_size,
7060830d754dSRandall Stewart 								    tp1->book_size,
7061830d754dSRandall Stewart 								    (uintptr_t) tp1->whoTo,
7062830d754dSRandall Stewart 								    tp1->rec.data.TSN_seq);
7063830d754dSRandall Stewart 							}
7064830d754dSRandall Stewart 							sctp_flight_size_decrease(tp1);
7065830d754dSRandall Stewart 							sctp_total_flight_decrease(stcb, tp1);
7066830d754dSRandall Stewart 
7067830d754dSRandall Stewart 							tp1->whoTo->net_ack += tp1->send_size;
7068830d754dSRandall Stewart 							if (tp1->snd_count < 2) {
7069830d754dSRandall Stewart 								/*
7070830d754dSRandall Stewart 								 * True
7071830d754dSRandall Stewart 								 * non-retran
7072830d754dSRandall Stewart 								 * smited
7073830d754dSRandall Stewart 								 * chunk
7074830d754dSRandall Stewart 								 */
7075830d754dSRandall Stewart 								tp1->whoTo->net_ack2 += tp1->send_size;
7076830d754dSRandall Stewart 
7077830d754dSRandall Stewart 								/*
7078830d754dSRandall Stewart 								 * update
7079830d754dSRandall Stewart 								 * RTO too ?
7080830d754dSRandall Stewart 								 */
7081830d754dSRandall Stewart 								if (tp1->do_rtt) {
7082830d754dSRandall Stewart 									tp1->whoTo->RTO =
7083830d754dSRandall Stewart 									    sctp_calculate_rto(stcb,
7084830d754dSRandall Stewart 									    asoc,
7085830d754dSRandall Stewart 									    tp1->whoTo,
7086830d754dSRandall Stewart 									    &tp1->sent_rcv_time,
7087830d754dSRandall Stewart 									    sctp_align_safe_nocopy);
7088830d754dSRandall Stewart 									tp1->do_rtt = 0;
7089830d754dSRandall Stewart 								}
7090830d754dSRandall Stewart 							}
7091830d754dSRandall Stewart 						}
7092830d754dSRandall Stewart 						if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
7093830d754dSRandall Stewart 							(*ecn_seg_sums) += tp1->rec.data.ect_nonce;
7094830d754dSRandall Stewart 							(*ecn_seg_sums) &= SCTP_SACK_NONCE_SUM;
7095830d754dSRandall Stewart 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
7096830d754dSRandall Stewart 							    asoc->this_sack_highest_gap,
7097830d754dSRandall Stewart 							    MAX_TSN)) {
7098830d754dSRandall Stewart 								asoc->this_sack_highest_gap =
7099830d754dSRandall Stewart 								    tp1->rec.data.TSN_seq;
7100830d754dSRandall Stewart 							}
7101830d754dSRandall Stewart 							if (tp1->sent == SCTP_DATAGRAM_RESEND) {
7102830d754dSRandall Stewart 								sctp_ucount_decr(asoc->sent_queue_retran_cnt);
7103830d754dSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
7104830d754dSRandall Stewart 								sctp_audit_log(0xB2,
7105830d754dSRandall Stewart 								    (asoc->sent_queue_retran_cnt & 0x000000ff));
7106830d754dSRandall Stewart #endif
7107830d754dSRandall Stewart 							}
7108830d754dSRandall Stewart 						}
7109830d754dSRandall Stewart 						/*
7110830d754dSRandall Stewart 						 * All chunks NOT UNSENT
7111830d754dSRandall Stewart 						 * fall through here and are
7112830d754dSRandall Stewart 						 * marked
7113830d754dSRandall Stewart 						 */
71148933fa13SRandall Stewart 						if (tp1->sent != SCTP_FORWARD_TSN_SKIP)
71158933fa13SRandall Stewart 							tp1->sent = SCTP_DATAGRAM_NR_MARKED;
7116830d754dSRandall Stewart 						if (tp1->rec.data.chunk_was_revoked) {
7117830d754dSRandall Stewart 							/* deflate the cwnd */
7118830d754dSRandall Stewart 							tp1->whoTo->cwnd -= tp1->book_size;
7119830d754dSRandall Stewart 							tp1->rec.data.chunk_was_revoked = 0;
7120830d754dSRandall Stewart 						}
7121830d754dSRandall Stewart 					}
7122830d754dSRandall Stewart 					break;
7123830d754dSRandall Stewart 				}	/* if (tp1->TSN_seq == theTSN) */
7124830d754dSRandall Stewart 				if (compare_with_wrap(tp1->rec.data.TSN_seq, theTSN,
7125830d754dSRandall Stewart 				    MAX_TSN))
7126830d754dSRandall Stewart 					break;
7127830d754dSRandall Stewart 
7128830d754dSRandall Stewart 				tp1 = TAILQ_NEXT(tp1, sctp_next);
7129830d754dSRandall Stewart 			}	/* end while (tp1) */
7130830d754dSRandall Stewart 		}		/* end for (j = fragStart */
7131830d754dSRandall Stewart 		frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
7132830d754dSRandall Stewart 		    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
7133830d754dSRandall Stewart 		*offset += sizeof(block);
7134830d754dSRandall Stewart 		if (frag == NULL) {
7135830d754dSRandall Stewart 			break;
7136830d754dSRandall Stewart 		}
7137830d754dSRandall Stewart 	}
7138830d754dSRandall Stewart 
7139830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
7140830d754dSRandall Stewart 		if (num_frs)
7141830d754dSRandall Stewart 			sctp_log_fr(*biggest_tsn_acked,
7142830d754dSRandall Stewart 			    *biggest_newly_acked_tsn,
7143830d754dSRandall Stewart 			    last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
7144830d754dSRandall Stewart 	}
7145830d754dSRandall Stewart 	nr_frag = (struct sctp_nr_gap_ack_block *)sctp_m_getptr(m, *offset,
7146830d754dSRandall Stewart 	    sizeof(struct sctp_nr_gap_ack_block), (uint8_t *) & nr_block);
7147830d754dSRandall Stewart 	*offset += sizeof(nr_block);
7148830d754dSRandall Stewart 
7149830d754dSRandall Stewart 
7150830d754dSRandall Stewart 
7151830d754dSRandall Stewart 	if (nr_frag == NULL) {
7152830d754dSRandall Stewart 		return;
7153830d754dSRandall Stewart 	}
7154830d754dSRandall Stewart 	tp1 = NULL;
7155830d754dSRandall Stewart 	last_nr_frag_high = 0;
7156830d754dSRandall Stewart 
7157830d754dSRandall Stewart 	for (i = 0; i < num_nr_seg; i++) {
7158830d754dSRandall Stewart 
7159830d754dSRandall Stewart 		nr_frag_strt = ntohs(nr_frag->start);
7160830d754dSRandall Stewart 		nr_frag_end = ntohs(nr_frag->end);
7161830d754dSRandall Stewart 
7162830d754dSRandall Stewart 		/* some sanity checks on the nr fargment offsets */
7163830d754dSRandall Stewart 		if (nr_frag_strt > nr_frag_end) {
7164830d754dSRandall Stewart 			/* this one is malformed, skip */
7165830d754dSRandall Stewart 			nr_frag++;
7166830d754dSRandall Stewart 			continue;
7167830d754dSRandall Stewart 		}
7168d50c1d79SRandall Stewart 		/* mark acked dgs and find out the highestTSN being acked */
7169830d754dSRandall Stewart 		if (tp1 == NULL) {
7170830d754dSRandall Stewart 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
7171830d754dSRandall Stewart 
7172830d754dSRandall Stewart 			/* save the locations of the last frags */
7173830d754dSRandall Stewart 			last_nr_frag_high = nr_frag_end + last_tsn;
7174830d754dSRandall Stewart 		} else {
7175830d754dSRandall Stewart 			/*
7176d50c1d79SRandall Stewart 			 * now lets see if we need to reset the queue due to
7177d50c1d79SRandall Stewart 			 * a out-of-order SACK fragment
7178830d754dSRandall Stewart 			 */
7179830d754dSRandall Stewart 			if (compare_with_wrap(nr_frag_strt + last_tsn,
7180830d754dSRandall Stewart 			    last_nr_frag_high, MAX_TSN)) {
7181830d754dSRandall Stewart 				/*
7182d50c1d79SRandall Stewart 				 * if the new frag starts after the last TSN
7183d50c1d79SRandall Stewart 				 * frag covered, we are ok and this one is
7184d50c1d79SRandall Stewart 				 * beyond the last one
7185830d754dSRandall Stewart 				 */
7186830d754dSRandall Stewart 				;
7187830d754dSRandall Stewart 			} else {
7188830d754dSRandall Stewart 				/*
7189d50c1d79SRandall Stewart 				 * ok, they have reset us, so we need to
7190d50c1d79SRandall Stewart 				 * reset the queue this will cause extra
7191d50c1d79SRandall Stewart 				 * hunting but hey, they chose the
7192d50c1d79SRandall Stewart 				 * performance hit when they failed to order
7193d50c1d79SRandall Stewart 				 * there gaps..
7194830d754dSRandall Stewart 				 */
7195830d754dSRandall Stewart 				tp1 = TAILQ_FIRST(&asoc->sent_queue);
7196830d754dSRandall Stewart 			}
7197830d754dSRandall Stewart 			last_nr_frag_high = nr_frag_end + last_tsn;
7198830d754dSRandall Stewart 		}
7199830d754dSRandall Stewart 
7200830d754dSRandall Stewart 		for (j = nr_frag_strt + last_tsn; (compare_with_wrap((nr_frag_end + last_tsn), j, MAX_TSN)); j++) {
7201830d754dSRandall Stewart 			while (tp1) {
7202830d754dSRandall Stewart 				if (tp1->rec.data.TSN_seq == j) {
7203830d754dSRandall Stewart 					if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
72048933fa13SRandall Stewart 						if (tp1->sent != SCTP_FORWARD_TSN_SKIP)
7205830d754dSRandall Stewart 							tp1->sent = SCTP_DATAGRAM_NR_MARKED;
7206830d754dSRandall Stewart 						/*
7207d50c1d79SRandall Stewart 						 * TAILQ_REMOVE(&asoc->sent_q
7208d50c1d79SRandall Stewart 						 * ueue, tp1, sctp_next);
7209830d754dSRandall Stewart 						 */
7210830d754dSRandall Stewart 						if (tp1->data) {
7211830d754dSRandall Stewart 							/*
7212830d754dSRandall Stewart 							 * sa_ignore
7213d50c1d79SRandall Stewart 							 * NO_NULL_CHK
7214830d754dSRandall Stewart 							 */
7215830d754dSRandall Stewart 							sctp_free_bufspace(stcb, asoc, tp1, 1);
7216830d754dSRandall Stewart 							sctp_m_freem(tp1->data);
7217830d754dSRandall Stewart 						}
7218830d754dSRandall Stewart 						tp1->data = NULL;
7219d50c1d79SRandall Stewart 						/* asoc->sent_queue_cnt--; */
7220830d754dSRandall Stewart 						/*
7221d50c1d79SRandall Stewart 						 * sctp_free_a_chunk(stcb,
7222d50c1d79SRandall Stewart 						 * tp1);
7223830d754dSRandall Stewart 						 */
7224830d754dSRandall Stewart 						wake_him++;
7225830d754dSRandall Stewart 					}
7226830d754dSRandall Stewart 					break;
7227830d754dSRandall Stewart 				}	/* if (tp1->TSN_seq == j) */
7228830d754dSRandall Stewart 				if (compare_with_wrap(tp1->rec.data.TSN_seq, j,
7229830d754dSRandall Stewart 				    MAX_TSN))
7230830d754dSRandall Stewart 					break;
7231830d754dSRandall Stewart 				tp1 = TAILQ_NEXT(tp1, sctp_next);
7232830d754dSRandall Stewart 			}	/* end while (tp1) */
7233830d754dSRandall Stewart 
7234830d754dSRandall Stewart 		}		/* end for (j = nrFragStart */
7235830d754dSRandall Stewart 
7236830d754dSRandall Stewart 		nr_frag = (struct sctp_nr_gap_ack_block *)sctp_m_getptr(m, *offset,
7237830d754dSRandall Stewart 		    sizeof(struct sctp_nr_gap_ack_block), (uint8_t *) & nr_block);
7238830d754dSRandall Stewart 		*offset += sizeof(nr_block);
7239830d754dSRandall Stewart 		if (nr_frag == NULL) {
7240830d754dSRandall Stewart 			break;
7241830d754dSRandall Stewart 		}
7242830d754dSRandall Stewart 	}
7243d50c1d79SRandall Stewart 
7244830d754dSRandall Stewart 	/*
7245830d754dSRandall Stewart 	 * EY- wake up the socket if things have been removed from the sent
7246830d754dSRandall Stewart 	 * queue
7247830d754dSRandall Stewart 	 */
7248830d754dSRandall Stewart 	if ((wake_him) && (stcb->sctp_socket)) {
7249830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7250830d754dSRandall Stewart 		struct socket *so;
7251830d754dSRandall Stewart 
7252830d754dSRandall Stewart #endif
7253830d754dSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
7254830d754dSRandall Stewart 		/*
7255830d754dSRandall Stewart 		 * if (SCTP_BASE_SYSCTL(sctp_logging_level) &
7256830d754dSRandall Stewart 		 * SCTP_WAKE_LOGGING_ENABLE) { sctp_wakeup_log(stcb,
7257830d754dSRandall Stewart 		 * cum_ack, wake_him, SCTP_WAKESND_FROM_SACK);}
7258830d754dSRandall Stewart 		 */
7259830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7260830d754dSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
7261830d754dSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
7262830d754dSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
7263830d754dSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
7264830d754dSRandall Stewart 		SCTP_TCB_LOCK(stcb);
7265830d754dSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
7266830d754dSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
7267830d754dSRandall Stewart 			/* assoc was freed while we were unlocked */
7268830d754dSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
7269830d754dSRandall Stewart 			return;
7270830d754dSRandall Stewart 		}
7271830d754dSRandall Stewart #endif
7272830d754dSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
7273830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7274830d754dSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
7275830d754dSRandall Stewart #endif
7276830d754dSRandall Stewart 	}			/* else { if
7277830d754dSRandall Stewart 				 * (SCTP_BASE_SYSCTL(sctp_logging_level) &
7278830d754dSRandall Stewart 				 * SCTP_WAKE_LOGGING_ENABLE) {
7279830d754dSRandall Stewart 				 * sctp_wakeup_log(stcb, cum_ack, wake_him,
7280830d754dSRandall Stewart 				 * SCTP_NOWAKE_FROM_SACK); } } */
7281830d754dSRandall Stewart }
7282830d754dSRandall Stewart 
7283830d754dSRandall Stewart /* EY- nr_sack */
7284830d754dSRandall Stewart /* Identifies the non-renegable tsns that are revoked*/
7285830d754dSRandall Stewart static void
7286830d754dSRandall Stewart sctp_check_for_nr_revoked(struct sctp_tcb *stcb,
7287830d754dSRandall Stewart     struct sctp_association *asoc, uint32_t cumack,
7288830d754dSRandall Stewart     u_long biggest_tsn_acked)
7289830d754dSRandall Stewart {
7290830d754dSRandall Stewart 	struct sctp_tmit_chunk *tp1;
7291830d754dSRandall Stewart 
7292830d754dSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
7293830d754dSRandall Stewart 	while (tp1) {
7294830d754dSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq, cumack,
7295830d754dSRandall Stewart 		    MAX_TSN)) {
7296830d754dSRandall Stewart 			/*
7297830d754dSRandall Stewart 			 * ok this guy is either ACK or MARKED. If it is
7298830d754dSRandall Stewart 			 * ACKED it has been previously acked but not this
7299830d754dSRandall Stewart 			 * time i.e. revoked.  If it is MARKED it was ACK'ed
7300830d754dSRandall Stewart 			 * again.
7301830d754dSRandall Stewart 			 */
7302830d754dSRandall Stewart 			if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
7303830d754dSRandall Stewart 			    MAX_TSN))
7304830d754dSRandall Stewart 				break;
7305830d754dSRandall Stewart 
7306830d754dSRandall Stewart 
7307830d754dSRandall Stewart 			if (tp1->sent == SCTP_DATAGRAM_NR_ACKED) {
7308830d754dSRandall Stewart 				/*
7309830d754dSRandall Stewart 				 * EY! a non-renegable TSN is revoked, need
7310830d754dSRandall Stewart 				 * to abort the association
7311830d754dSRandall Stewart 				 */
7312830d754dSRandall Stewart 				/*
7313830d754dSRandall Stewart 				 * EY TODO: put in the code to abort the
7314830d754dSRandall Stewart 				 * assoc.
7315830d754dSRandall Stewart 				 */
7316830d754dSRandall Stewart 				return;
7317830d754dSRandall Stewart 			} else if (tp1->sent == SCTP_DATAGRAM_NR_MARKED) {
7318830d754dSRandall Stewart 				/* it has been re-acked in this SACK */
7319830d754dSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_NR_ACKED;
7320830d754dSRandall Stewart 			}
7321830d754dSRandall Stewart 		}
7322830d754dSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT)
7323830d754dSRandall Stewart 			break;
7324830d754dSRandall Stewart 		tp1 = TAILQ_NEXT(tp1, sctp_next);
7325830d754dSRandall Stewart 	}
7326830d754dSRandall Stewart }
7327830d754dSRandall Stewart 
7328830d754dSRandall Stewart /* EY! nr_sack version of sctp_handle_sack, nr_gap_ack processing should be added to this method*/
7329830d754dSRandall Stewart void
7330830d754dSRandall Stewart sctp_handle_nr_sack(struct mbuf *m, int offset,
7331830d754dSRandall Stewart     struct sctp_nr_sack_chunk *ch, struct sctp_tcb *stcb,
7332830d754dSRandall Stewart     struct sctp_nets *net_from, int *abort_now, int nr_sack_len, uint32_t rwnd)
7333830d754dSRandall Stewart {
7334830d754dSRandall Stewart 	struct sctp_association *asoc;
7335830d754dSRandall Stewart 
7336830d754dSRandall Stewart 	/* EY sack */
7337830d754dSRandall Stewart 	struct sctp_nr_sack *nr_sack;
7338830d754dSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
7339830d754dSRandall Stewart 	uint32_t cum_ack, last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked,
7340830d754dSRandall Stewart 	         this_sack_lowest_newack;
7341830d754dSRandall Stewart 	uint32_t sav_cum_ack;
7342830d754dSRandall Stewart 
7343830d754dSRandall Stewart 	/* EY num_seg */
7344830d754dSRandall Stewart 	uint16_t num_seg, num_nr_seg, num_dup;
7345830d754dSRandall Stewart 	uint16_t wake_him = 0;
7346830d754dSRandall Stewart 	unsigned int nr_sack_length;
7347830d754dSRandall Stewart 	uint32_t send_s = 0;
7348830d754dSRandall Stewart 	long j;
7349830d754dSRandall Stewart 	int accum_moved = 0;
7350830d754dSRandall Stewart 	int will_exit_fast_recovery = 0;
7351830d754dSRandall Stewart 	uint32_t a_rwnd, old_rwnd;
7352830d754dSRandall Stewart 	int win_probe_recovery = 0;
7353830d754dSRandall Stewart 	int win_probe_recovered = 0;
7354830d754dSRandall Stewart 	struct sctp_nets *net = NULL;
7355d50c1d79SRandall Stewart 	int nonce_sum_flag, ecn_seg_sums = 0;
7356830d754dSRandall Stewart 	int done_once;
7357830d754dSRandall Stewart 	uint8_t reneged_all = 0;
7358830d754dSRandall Stewart 	uint8_t cmt_dac_flag;
7359830d754dSRandall Stewart 
7360830d754dSRandall Stewart 	/*
7361830d754dSRandall Stewart 	 * we take any chance we can to service our queues since we cannot
7362830d754dSRandall Stewart 	 * get awoken when the socket is read from :<
7363830d754dSRandall Stewart 	 */
7364830d754dSRandall Stewart 	/*
7365830d754dSRandall Stewart 	 * Now perform the actual SACK handling: 1) Verify that it is not an
7366830d754dSRandall Stewart 	 * old sack, if so discard. 2) If there is nothing left in the send
7367830d754dSRandall Stewart 	 * queue (cum-ack is equal to last acked) then you have a duplicate
7368830d754dSRandall Stewart 	 * too, update any rwnd change and verify no timers are running.
7369830d754dSRandall Stewart 	 * then return. 3) Process any new consequtive data i.e. cum-ack
7370830d754dSRandall Stewart 	 * moved process these first and note that it moved. 4) Process any
7371830d754dSRandall Stewart 	 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
7372830d754dSRandall Stewart 	 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
7373830d754dSRandall Stewart 	 * sync up flightsizes and things, stop all timers and also check
7374830d754dSRandall Stewart 	 * for shutdown_pending state. If so then go ahead and send off the
7375830d754dSRandall Stewart 	 * shutdown. If in shutdown recv, send off the shutdown-ack and
7376830d754dSRandall Stewart 	 * start that timer, Ret. 9) Strike any non-acked things and do FR
7377830d754dSRandall Stewart 	 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
7378830d754dSRandall Stewart 	 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
7379830d754dSRandall Stewart 	 * if in shutdown_recv state.
7380830d754dSRandall Stewart 	 */
7381830d754dSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
7382830d754dSRandall Stewart 	nr_sack = &ch->nr_sack;
7383830d754dSRandall Stewart 	/* CMT DAC algo */
7384830d754dSRandall Stewart 	this_sack_lowest_newack = 0;
7385830d754dSRandall Stewart 	j = 0;
7386830d754dSRandall Stewart 	nr_sack_length = (unsigned int)nr_sack_len;
7387830d754dSRandall Stewart 	/* ECN Nonce */
7388830d754dSRandall Stewart 	SCTP_STAT_INCR(sctps_slowpath_sack);
7389830d754dSRandall Stewart 	nonce_sum_flag = ch->ch.chunk_flags & SCTP_SACK_NONCE_SUM;
7390830d754dSRandall Stewart 	cum_ack = last_tsn = ntohl(nr_sack->cum_tsn_ack);
7391830d754dSRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
7392830d754dSRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
7393830d754dSRandall Stewart 	stcb->asoc.cumack_log_at++;
7394830d754dSRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
7395830d754dSRandall Stewart 		stcb->asoc.cumack_log_at = 0;
7396830d754dSRandall Stewart 	}
7397830d754dSRandall Stewart #endif
7398830d754dSRandall Stewart 	num_seg = ntohs(nr_sack->num_gap_ack_blks);
7399830d754dSRandall Stewart 	num_nr_seg = ntohs(nr_sack->num_nr_gap_ack_blks);
7400830d754dSRandall Stewart 	a_rwnd = rwnd;
7401830d754dSRandall Stewart 
7402830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
7403830d754dSRandall Stewart 		sctp_misc_ints(SCTP_SACK_LOG_NORMAL, cum_ack,
7404830d754dSRandall Stewart 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
7405830d754dSRandall Stewart 	}
7406830d754dSRandall Stewart 	/* CMT DAC algo */
7407830d754dSRandall Stewart 	cmt_dac_flag = ch->ch.chunk_flags & SCTP_SACK_CMT_DAC;
7408830d754dSRandall Stewart 	num_dup = ntohs(nr_sack->num_dup_tsns);
7409830d754dSRandall Stewart 
7410830d754dSRandall Stewart 	old_rwnd = stcb->asoc.peers_rwnd;
7411830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
7412830d754dSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
7413830d754dSRandall Stewart 		    stcb->asoc.overall_error_count,
7414830d754dSRandall Stewart 		    0,
7415830d754dSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
7416830d754dSRandall Stewart 		    __LINE__);
7417830d754dSRandall Stewart 	}
7418830d754dSRandall Stewart 	stcb->asoc.overall_error_count = 0;
7419830d754dSRandall Stewart 	asoc = &stcb->asoc;
7420830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
7421830d754dSRandall Stewart 		sctp_log_sack(asoc->last_acked_seq,
7422830d754dSRandall Stewart 		    cum_ack,
7423830d754dSRandall Stewart 		    0,
7424830d754dSRandall Stewart 		    num_seg,
7425830d754dSRandall Stewart 		    num_dup,
7426830d754dSRandall Stewart 		    SCTP_LOG_NEW_SACK);
7427830d754dSRandall Stewart 	}
7428830d754dSRandall Stewart 	if ((num_dup) && (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_FR_LOGGING_ENABLE | SCTP_EARLYFR_LOGGING_ENABLE))) {
7429830d754dSRandall Stewart 		int off_to_dup, iii;
7430830d754dSRandall Stewart 		uint32_t *dupdata, dblock;
7431830d754dSRandall Stewart 
7432830d754dSRandall Stewart 		off_to_dup = (num_seg * sizeof(struct sctp_gap_ack_block)) +
7433830d754dSRandall Stewart 		    (num_nr_seg * sizeof(struct sctp_nr_gap_ack_block)) + sizeof(struct sctp_nr_sack_chunk);
7434830d754dSRandall Stewart 		if ((off_to_dup + (num_dup * sizeof(uint32_t))) <= nr_sack_length) {
7435830d754dSRandall Stewart 			dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
7436830d754dSRandall Stewart 			    sizeof(uint32_t), (uint8_t *) & dblock);
7437830d754dSRandall Stewart 			off_to_dup += sizeof(uint32_t);
7438830d754dSRandall Stewart 			if (dupdata) {
7439830d754dSRandall Stewart 				for (iii = 0; iii < num_dup; iii++) {
7440830d754dSRandall Stewart 					sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
7441830d754dSRandall Stewart 					dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
7442830d754dSRandall Stewart 					    sizeof(uint32_t), (uint8_t *) & dblock);
7443830d754dSRandall Stewart 					if (dupdata == NULL)
7444830d754dSRandall Stewart 						break;
7445830d754dSRandall Stewart 					off_to_dup += sizeof(uint32_t);
7446830d754dSRandall Stewart 				}
7447830d754dSRandall Stewart 			}
7448830d754dSRandall Stewart 		} else {
7449830d754dSRandall Stewart 			SCTP_PRINTF("Size invalid offset to dups:%d number dups:%d nr_sack_len:%d num gaps:%d num nr_gaps:%d\n",
7450830d754dSRandall Stewart 			    off_to_dup, num_dup, nr_sack_length, num_seg, num_nr_seg);
7451830d754dSRandall Stewart 		}
7452830d754dSRandall Stewart 	}
7453830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
7454830d754dSRandall Stewart 		/* reality check */
7455830d754dSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
7456830d754dSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
7457830d754dSRandall Stewart 			    sctpchunk_listhead);
7458830d754dSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
7459830d754dSRandall Stewart 		} else {
7460830d754dSRandall Stewart 			send_s = asoc->sending_seq;
7461830d754dSRandall Stewart 		}
7462830d754dSRandall Stewart 		if (cum_ack == send_s ||
7463830d754dSRandall Stewart 		    compare_with_wrap(cum_ack, send_s, MAX_TSN)) {
7464830d754dSRandall Stewart #ifndef INVARIANTS
7465830d754dSRandall Stewart 			struct mbuf *oper;
7466830d754dSRandall Stewart 
7467830d754dSRandall Stewart #endif
7468830d754dSRandall Stewart #ifdef INVARIANTS
7469830d754dSRandall Stewart 	hopeless_peer:
7470830d754dSRandall Stewart 			panic("Impossible sack 1");
7471830d754dSRandall Stewart #else
7472830d754dSRandall Stewart 
7473830d754dSRandall Stewart 
7474830d754dSRandall Stewart 			/*
7475830d754dSRandall Stewart 			 * no way, we have not even sent this TSN out yet.
7476830d754dSRandall Stewart 			 * Peer is hopelessly messed up with us.
7477830d754dSRandall Stewart 			 */
7478830d754dSRandall Stewart 	hopeless_peer:
7479830d754dSRandall Stewart 			*abort_now = 1;
7480830d754dSRandall Stewart 			/* XXX */
7481830d754dSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
7482830d754dSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
7483830d754dSRandall Stewart 			if (oper) {
7484830d754dSRandall Stewart 				struct sctp_paramhdr *ph;
7485830d754dSRandall Stewart 				uint32_t *ippp;
7486830d754dSRandall Stewart 
7487830d754dSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
7488830d754dSRandall Stewart 				    sizeof(uint32_t);
7489830d754dSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
7490830d754dSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
7491830d754dSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
7492830d754dSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
7493830d754dSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
7494830d754dSRandall Stewart 			}
7495830d754dSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
7496830d754dSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
7497830d754dSRandall Stewart 			return;
7498830d754dSRandall Stewart #endif
7499830d754dSRandall Stewart 		}
7500830d754dSRandall Stewart 	}
7501830d754dSRandall Stewart 	/**********************/
7502830d754dSRandall Stewart 	/* 1) check the range */
7503830d754dSRandall Stewart 	/**********************/
7504830d754dSRandall Stewart 	if (compare_with_wrap(asoc->last_acked_seq, last_tsn, MAX_TSN)) {
7505830d754dSRandall Stewart 		/* acking something behind */
7506830d754dSRandall Stewart 		return;
7507830d754dSRandall Stewart 	}
7508830d754dSRandall Stewart 	sav_cum_ack = asoc->last_acked_seq;
7509830d754dSRandall Stewart 
7510830d754dSRandall Stewart 	/* update the Rwnd of the peer */
7511830d754dSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue) &&
7512830d754dSRandall Stewart 	    TAILQ_EMPTY(&asoc->send_queue) &&
7513830d754dSRandall Stewart 	    (asoc->stream_queue_cnt == 0)
7514830d754dSRandall Stewart 	    ) {
7515830d754dSRandall Stewart 		/* nothing left on send/sent and strmq */
7516830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
7517830d754dSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
7518830d754dSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
7519830d754dSRandall Stewart 		}
7520830d754dSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
7521830d754dSRandall Stewart 		if (asoc->sent_queue_retran_cnt) {
7522830d754dSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
7523830d754dSRandall Stewart 		}
7524830d754dSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
7525830d754dSRandall Stewart 			/* SWS sender side engages */
7526830d754dSRandall Stewart 			asoc->peers_rwnd = 0;
7527830d754dSRandall Stewart 		}
7528830d754dSRandall Stewart 		/* stop any timers */
7529830d754dSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7530830d754dSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
7531830d754dSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
7532830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
7533830d754dSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
7534830d754dSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck1);
7535830d754dSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
7536830d754dSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
7537830d754dSRandall Stewart 				}
7538830d754dSRandall Stewart 			}
7539830d754dSRandall Stewart 			net->partial_bytes_acked = 0;
7540830d754dSRandall Stewart 			net->flight_size = 0;
7541830d754dSRandall Stewart 		}
7542830d754dSRandall Stewart 		asoc->total_flight = 0;
7543830d754dSRandall Stewart 		asoc->total_flight_count = 0;
7544830d754dSRandall Stewart 		return;
7545830d754dSRandall Stewart 	}
7546830d754dSRandall Stewart 	/*
7547830d754dSRandall Stewart 	 * We init netAckSz and netAckSz2 to 0. These are used to track 2
7548830d754dSRandall Stewart 	 * things. The total byte count acked is tracked in netAckSz AND
7549830d754dSRandall Stewart 	 * netAck2 is used to track the total bytes acked that are un-
7550830d754dSRandall Stewart 	 * amibguious and were never retransmitted. We track these on a per
7551830d754dSRandall Stewart 	 * destination address basis.
7552830d754dSRandall Stewart 	 */
7553830d754dSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7554830d754dSRandall Stewart 		net->prev_cwnd = net->cwnd;
7555830d754dSRandall Stewart 		net->net_ack = 0;
7556830d754dSRandall Stewart 		net->net_ack2 = 0;
7557830d754dSRandall Stewart 
7558830d754dSRandall Stewart 		/*
7559830d754dSRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
7560830d754dSRandall Stewart 		 * SACK processing
7561830d754dSRandall Stewart 		 */
7562830d754dSRandall Stewart 		net->new_pseudo_cumack = 0;
7563830d754dSRandall Stewart 		net->will_exit_fast_recovery = 0;
7564830d754dSRandall Stewart 	}
7565830d754dSRandall Stewart 	/* process the new consecutive TSN first */
7566830d754dSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
7567830d754dSRandall Stewart 	while (tp1) {
7568830d754dSRandall Stewart 		if (compare_with_wrap(last_tsn, tp1->rec.data.TSN_seq,
7569830d754dSRandall Stewart 		    MAX_TSN) ||
7570830d754dSRandall Stewart 		    last_tsn == tp1->rec.data.TSN_seq) {
7571830d754dSRandall Stewart 			if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
7572830d754dSRandall Stewart 				/*
7573830d754dSRandall Stewart 				 * ECN Nonce: Add the nonce to the sender's
7574830d754dSRandall Stewart 				 * nonce sum
7575830d754dSRandall Stewart 				 */
7576830d754dSRandall Stewart 				asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
7577830d754dSRandall Stewart 				accum_moved = 1;
7578830d754dSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
7579830d754dSRandall Stewart 					/*
7580830d754dSRandall Stewart 					 * If it is less than ACKED, it is
7581830d754dSRandall Stewart 					 * now no-longer in flight. Higher
7582830d754dSRandall Stewart 					 * values may occur during marking
7583830d754dSRandall Stewart 					 */
7584830d754dSRandall Stewart 					if ((tp1->whoTo->dest_state &
7585830d754dSRandall Stewart 					    SCTP_ADDR_UNCONFIRMED) &&
7586830d754dSRandall Stewart 					    (tp1->snd_count < 2)) {
7587830d754dSRandall Stewart 						/*
7588830d754dSRandall Stewart 						 * If there was no retran
7589830d754dSRandall Stewart 						 * and the address is
7590830d754dSRandall Stewart 						 * un-confirmed and we sent
7591830d754dSRandall Stewart 						 * there and are now
7592830d754dSRandall Stewart 						 * sacked.. its confirmed,
7593830d754dSRandall Stewart 						 * mark it so.
7594830d754dSRandall Stewart 						 */
7595830d754dSRandall Stewart 						tp1->whoTo->dest_state &=
7596830d754dSRandall Stewart 						    ~SCTP_ADDR_UNCONFIRMED;
7597830d754dSRandall Stewart 					}
7598830d754dSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
7599830d754dSRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
7600830d754dSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
7601830d754dSRandall Stewart 							    tp1->whoTo->flight_size,
7602830d754dSRandall Stewart 							    tp1->book_size,
7603830d754dSRandall Stewart 							    (uintptr_t) tp1->whoTo,
7604830d754dSRandall Stewart 							    tp1->rec.data.TSN_seq);
7605830d754dSRandall Stewart 						}
7606830d754dSRandall Stewart 						sctp_flight_size_decrease(tp1);
7607830d754dSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
7608830d754dSRandall Stewart 					}
7609830d754dSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
7610830d754dSRandall Stewart 
7611830d754dSRandall Stewart 					/* CMT SFR and DAC algos */
7612830d754dSRandall Stewart 					this_sack_lowest_newack = tp1->rec.data.TSN_seq;
7613830d754dSRandall Stewart 					tp1->whoTo->saw_newack = 1;
7614830d754dSRandall Stewart 
7615830d754dSRandall Stewart 					if (tp1->snd_count < 2) {
7616830d754dSRandall Stewart 						/*
7617830d754dSRandall Stewart 						 * True non-retransmited
7618830d754dSRandall Stewart 						 * chunk
7619830d754dSRandall Stewart 						 */
7620830d754dSRandall Stewart 						tp1->whoTo->net_ack2 +=
7621830d754dSRandall Stewart 						    tp1->send_size;
7622830d754dSRandall Stewart 
7623830d754dSRandall Stewart 						/* update RTO too? */
7624830d754dSRandall Stewart 						if (tp1->do_rtt) {
7625830d754dSRandall Stewart 							tp1->whoTo->RTO =
7626830d754dSRandall Stewart 							    sctp_calculate_rto(stcb,
7627830d754dSRandall Stewart 							    asoc, tp1->whoTo,
7628830d754dSRandall Stewart 							    &tp1->sent_rcv_time,
7629830d754dSRandall Stewart 							    sctp_align_safe_nocopy);
7630830d754dSRandall Stewart 							tp1->do_rtt = 0;
7631830d754dSRandall Stewart 						}
7632830d754dSRandall Stewart 					}
7633830d754dSRandall Stewart 					/*
7634830d754dSRandall Stewart 					 * CMT: CUCv2 algorithm. From the
7635830d754dSRandall Stewart 					 * cumack'd TSNs, for each TSN being
7636830d754dSRandall Stewart 					 * acked for the first time, set the
7637830d754dSRandall Stewart 					 * following variables for the
7638830d754dSRandall Stewart 					 * corresp destination.
7639830d754dSRandall Stewart 					 * new_pseudo_cumack will trigger a
7640830d754dSRandall Stewart 					 * cwnd update.
7641830d754dSRandall Stewart 					 * find_(rtx_)pseudo_cumack will
7642830d754dSRandall Stewart 					 * trigger search for the next
7643830d754dSRandall Stewart 					 * expected (rtx-)pseudo-cumack.
7644830d754dSRandall Stewart 					 */
7645830d754dSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
7646830d754dSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
7647830d754dSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
7648830d754dSRandall Stewart 
7649830d754dSRandall Stewart 
7650830d754dSRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
7651830d754dSRandall Stewart 						sctp_log_sack(asoc->last_acked_seq,
7652830d754dSRandall Stewart 						    cum_ack,
7653830d754dSRandall Stewart 						    tp1->rec.data.TSN_seq,
7654830d754dSRandall Stewart 						    0,
7655830d754dSRandall Stewart 						    0,
7656830d754dSRandall Stewart 						    SCTP_LOG_TSN_ACKED);
7657830d754dSRandall Stewart 					}
7658830d754dSRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7659830d754dSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
7660830d754dSRandall Stewart 					}
7661830d754dSRandall Stewart 				}
7662830d754dSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
7663830d754dSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
7664830d754dSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
7665830d754dSRandall Stewart 					sctp_audit_log(0xB3,
7666830d754dSRandall Stewart 					    (asoc->sent_queue_retran_cnt & 0x000000ff));
7667830d754dSRandall Stewart #endif
7668830d754dSRandall Stewart 				}
7669830d754dSRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
7670830d754dSRandall Stewart 					/* deflate the cwnd */
7671830d754dSRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
7672830d754dSRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
7673830d754dSRandall Stewart 				}
7674830d754dSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
7675830d754dSRandall Stewart 			}
7676830d754dSRandall Stewart 		} else {
7677830d754dSRandall Stewart 			break;
7678830d754dSRandall Stewart 		}
7679830d754dSRandall Stewart 		tp1 = TAILQ_NEXT(tp1, sctp_next);
7680830d754dSRandall Stewart 	}
7681830d754dSRandall Stewart 	biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
7682830d754dSRandall Stewart 	/* always set this up to cum-ack */
7683830d754dSRandall Stewart 	asoc->this_sack_highest_gap = last_tsn;
7684830d754dSRandall Stewart 
7685830d754dSRandall Stewart 	/* Move offset up to point to gaps/dups */
7686830d754dSRandall Stewart 	offset += sizeof(struct sctp_nr_sack_chunk);
7687830d754dSRandall Stewart 	if (((num_seg * (sizeof(struct sctp_gap_ack_block))) + sizeof(struct sctp_nr_sack_chunk)) > nr_sack_length) {
7688830d754dSRandall Stewart 
7689830d754dSRandall Stewart 		/* skip corrupt segments */
7690830d754dSRandall Stewart 		goto skip_segments;
7691830d754dSRandall Stewart 	}
7692830d754dSRandall Stewart 	if (num_seg > 0) {
7693830d754dSRandall Stewart 
7694830d754dSRandall Stewart 		/*
7695830d754dSRandall Stewart 		 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
7696830d754dSRandall Stewart 		 * to be greater than the cumack. Also reset saw_newack to 0
7697830d754dSRandall Stewart 		 * for all dests.
7698830d754dSRandall Stewart 		 */
7699830d754dSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7700830d754dSRandall Stewart 			net->saw_newack = 0;
7701830d754dSRandall Stewart 			net->this_sack_highest_newack = last_tsn;
7702830d754dSRandall Stewart 		}
7703830d754dSRandall Stewart 
7704830d754dSRandall Stewart 		/*
7705830d754dSRandall Stewart 		 * thisSackHighestGap will increase while handling NEW
7706830d754dSRandall Stewart 		 * segments this_sack_highest_newack will increase while
7707830d754dSRandall Stewart 		 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
7708830d754dSRandall Stewart 		 * used for CMT DAC algo. saw_newack will also change.
7709830d754dSRandall Stewart 		 */
7710830d754dSRandall Stewart 
7711830d754dSRandall Stewart 		sctp_handle_nr_sack_segments(m, &offset, stcb, asoc, ch, last_tsn,
7712830d754dSRandall Stewart 		    &biggest_tsn_acked, &biggest_tsn_newly_acked, &this_sack_lowest_newack,
7713830d754dSRandall Stewart 		    num_seg, num_nr_seg, &ecn_seg_sums);
7714830d754dSRandall Stewart 
7715830d754dSRandall Stewart 
7716830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
7717830d754dSRandall Stewart 			/*
7718830d754dSRandall Stewart 			 * validate the biggest_tsn_acked in the gap acks if
7719830d754dSRandall Stewart 			 * strict adherence is wanted.
7720830d754dSRandall Stewart 			 */
7721830d754dSRandall Stewart 			if ((biggest_tsn_acked == send_s) ||
7722830d754dSRandall Stewart 			    (compare_with_wrap(biggest_tsn_acked, send_s, MAX_TSN))) {
7723830d754dSRandall Stewart 				/*
7724830d754dSRandall Stewart 				 * peer is either confused or we are under
7725830d754dSRandall Stewart 				 * attack. We must abort.
7726830d754dSRandall Stewart 				 */
7727830d754dSRandall Stewart 				goto hopeless_peer;
7728830d754dSRandall Stewart 			}
7729830d754dSRandall Stewart 		}
7730830d754dSRandall Stewart 	}
7731830d754dSRandall Stewart skip_segments:
7732830d754dSRandall Stewart 	/*******************************************/
7733830d754dSRandall Stewart 	/* cancel ALL T3-send timer if accum moved */
7734830d754dSRandall Stewart 	/*******************************************/
7735830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
7736830d754dSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7737830d754dSRandall Stewart 			if (net->new_pseudo_cumack)
7738830d754dSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
7739830d754dSRandall Stewart 				    stcb, net,
7740830d754dSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
7741830d754dSRandall Stewart 
7742830d754dSRandall Stewart 		}
7743830d754dSRandall Stewart 	} else {
7744830d754dSRandall Stewart 		if (accum_moved) {
7745830d754dSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7746830d754dSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
7747830d754dSRandall Stewart 				    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
7748830d754dSRandall Stewart 			}
7749830d754dSRandall Stewart 		}
7750830d754dSRandall Stewart 	}
7751830d754dSRandall Stewart 	/********************************************/
7752830d754dSRandall Stewart 	/* drop the acked chunks from the sendqueue */
7753830d754dSRandall Stewart 	/********************************************/
7754830d754dSRandall Stewart 	asoc->last_acked_seq = cum_ack;
7755830d754dSRandall Stewart 
7756830d754dSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
7757830d754dSRandall Stewart 	if (tp1 == NULL)
7758830d754dSRandall Stewart 		goto done_with_it;
7759830d754dSRandall Stewart 	do {
7760830d754dSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq, cum_ack,
7761830d754dSRandall Stewart 		    MAX_TSN)) {
7762830d754dSRandall Stewart 			break;
7763830d754dSRandall Stewart 		}
7764830d754dSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
7765830d754dSRandall Stewart 			/* no more sent on list */
7766830d754dSRandall Stewart 			printf("Warning, tp1->sent == %d and its now acked?\n",
7767830d754dSRandall Stewart 			    tp1->sent);
7768830d754dSRandall Stewart 		}
7769830d754dSRandall Stewart 		tp2 = TAILQ_NEXT(tp1, sctp_next);
7770830d754dSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
7771830d754dSRandall Stewart 		if (tp1->pr_sctp_on) {
7772830d754dSRandall Stewart 			if (asoc->pr_sctp_cnt != 0)
7773830d754dSRandall Stewart 				asoc->pr_sctp_cnt--;
7774830d754dSRandall Stewart 		}
7775830d754dSRandall Stewart 		if ((TAILQ_FIRST(&asoc->sent_queue) == NULL) &&
7776830d754dSRandall Stewart 		    (asoc->total_flight > 0)) {
7777830d754dSRandall Stewart #ifdef INVARIANTS
7778830d754dSRandall Stewart 			panic("Warning flight size is postive and should be 0");
7779830d754dSRandall Stewart #else
7780830d754dSRandall Stewart 			SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
7781830d754dSRandall Stewart 			    asoc->total_flight);
7782830d754dSRandall Stewart #endif
7783830d754dSRandall Stewart 			asoc->total_flight = 0;
7784830d754dSRandall Stewart 		}
7785830d754dSRandall Stewart 		if (tp1->data) {
7786830d754dSRandall Stewart 			/* sa_ignore NO_NULL_CHK */
7787830d754dSRandall Stewart 			sctp_free_bufspace(stcb, asoc, tp1, 1);
7788830d754dSRandall Stewart 			sctp_m_freem(tp1->data);
7789830d754dSRandall Stewart 			if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
7790830d754dSRandall Stewart 				asoc->sent_queue_cnt_removeable--;
7791830d754dSRandall Stewart 			}
7792830d754dSRandall Stewart 		}
7793830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
7794830d754dSRandall Stewart 			sctp_log_sack(asoc->last_acked_seq,
7795830d754dSRandall Stewart 			    cum_ack,
7796830d754dSRandall Stewart 			    tp1->rec.data.TSN_seq,
7797830d754dSRandall Stewart 			    0,
7798830d754dSRandall Stewart 			    0,
7799830d754dSRandall Stewart 			    SCTP_LOG_FREE_SENT);
7800830d754dSRandall Stewart 		}
7801830d754dSRandall Stewart 		tp1->data = NULL;
7802830d754dSRandall Stewart 		asoc->sent_queue_cnt--;
7803830d754dSRandall Stewart 		sctp_free_a_chunk(stcb, tp1);
7804830d754dSRandall Stewart 		wake_him++;
7805830d754dSRandall Stewart 		tp1 = tp2;
7806830d754dSRandall Stewart 	} while (tp1 != NULL);
7807830d754dSRandall Stewart 
7808830d754dSRandall Stewart done_with_it:
7809830d754dSRandall Stewart 	/* sa_ignore NO_NULL_CHK */
7810830d754dSRandall Stewart 	if ((wake_him) && (stcb->sctp_socket)) {
7811830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7812830d754dSRandall Stewart 		struct socket *so;
7813830d754dSRandall Stewart 
7814830d754dSRandall Stewart #endif
7815830d754dSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
7816830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
7817830d754dSRandall Stewart 			sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_WAKESND_FROM_SACK);
7818830d754dSRandall Stewart 		}
7819830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7820830d754dSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
7821830d754dSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
7822830d754dSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
7823830d754dSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
7824830d754dSRandall Stewart 		SCTP_TCB_LOCK(stcb);
7825830d754dSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
7826830d754dSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
7827830d754dSRandall Stewart 			/* assoc was freed while we were unlocked */
7828830d754dSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
7829830d754dSRandall Stewart 			return;
7830830d754dSRandall Stewart 		}
7831830d754dSRandall Stewart #endif
7832830d754dSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
7833830d754dSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7834830d754dSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
7835830d754dSRandall Stewart #endif
7836830d754dSRandall Stewart 	} else {
7837830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
7838830d754dSRandall Stewart 			sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_NOWAKE_FROM_SACK);
7839830d754dSRandall Stewart 		}
7840830d754dSRandall Stewart 	}
7841830d754dSRandall Stewart 
7842830d754dSRandall Stewart 	if (asoc->fast_retran_loss_recovery && accum_moved) {
7843830d754dSRandall Stewart 		if (compare_with_wrap(asoc->last_acked_seq,
7844830d754dSRandall Stewart 		    asoc->fast_recovery_tsn, MAX_TSN) ||
7845830d754dSRandall Stewart 		    asoc->last_acked_seq == asoc->fast_recovery_tsn) {
7846830d754dSRandall Stewart 			/* Setup so we will exit RFC2582 fast recovery */
7847830d754dSRandall Stewart 			will_exit_fast_recovery = 1;
7848830d754dSRandall Stewart 		}
7849830d754dSRandall Stewart 	}
7850830d754dSRandall Stewart 	/*
7851830d754dSRandall Stewart 	 * Check for revoked fragments:
7852830d754dSRandall Stewart 	 *
7853830d754dSRandall Stewart 	 * if Previous sack - Had no frags then we can't have any revoked if
7854830d754dSRandall Stewart 	 * Previous sack - Had frag's then - If we now have frags aka
7855830d754dSRandall Stewart 	 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
7856830d754dSRandall Stewart 	 * some of them. else - The peer revoked all ACKED fragments, since
7857830d754dSRandall Stewart 	 * we had some before and now we have NONE.
7858830d754dSRandall Stewart 	 */
7859830d754dSRandall Stewart 
7860830d754dSRandall Stewart 	if (num_seg)
7861830d754dSRandall Stewart 		sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
7862830d754dSRandall Stewart 
7863830d754dSRandall Stewart 	else if (asoc->saw_sack_with_frags) {
7864830d754dSRandall Stewart 		int cnt_revoked = 0;
7865830d754dSRandall Stewart 
7866830d754dSRandall Stewart 		tp1 = TAILQ_FIRST(&asoc->sent_queue);
7867830d754dSRandall Stewart 		if (tp1 != NULL) {
7868830d754dSRandall Stewart 			/* Peer revoked all dg's marked or acked */
7869830d754dSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
7870830d754dSRandall Stewart 				/*
7871830d754dSRandall Stewart 				 * EY- maybe check only if it is nr_acked
7872830d754dSRandall Stewart 				 * nr_marked may not be possible
7873830d754dSRandall Stewart 				 */
7874830d754dSRandall Stewart 				if ((tp1->sent == SCTP_DATAGRAM_NR_ACKED) ||
7875830d754dSRandall Stewart 				    (tp1->sent == SCTP_DATAGRAM_NR_MARKED)) {
7876830d754dSRandall Stewart 					/*
7877830d754dSRandall Stewart 					 * EY! - TODO: Something previously
7878830d754dSRandall Stewart 					 * nr_gapped is reneged, abort the
7879830d754dSRandall Stewart 					 * association
7880830d754dSRandall Stewart 					 */
7881830d754dSRandall Stewart 					return;
7882830d754dSRandall Stewart 				}
7883830d754dSRandall Stewart 				if ((tp1->sent > SCTP_DATAGRAM_RESEND) &&
7884830d754dSRandall Stewart 				    (tp1->sent < SCTP_FORWARD_TSN_SKIP)) {
7885830d754dSRandall Stewart 					tp1->sent = SCTP_DATAGRAM_SENT;
7886830d754dSRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
7887830d754dSRandall Stewart 						sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
7888830d754dSRandall Stewart 						    tp1->whoTo->flight_size,
7889830d754dSRandall Stewart 						    tp1->book_size,
7890830d754dSRandall Stewart 						    (uintptr_t) tp1->whoTo,
7891830d754dSRandall Stewart 						    tp1->rec.data.TSN_seq);
7892830d754dSRandall Stewart 					}
7893830d754dSRandall Stewart 					sctp_flight_size_increase(tp1);
7894830d754dSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
7895830d754dSRandall Stewart 					tp1->rec.data.chunk_was_revoked = 1;
7896830d754dSRandall Stewart 					/*
7897830d754dSRandall Stewart 					 * To ensure that this increase in
7898830d754dSRandall Stewart 					 * flightsize, which is artificial,
7899830d754dSRandall Stewart 					 * does not throttle the sender, we
7900830d754dSRandall Stewart 					 * also increase the cwnd
7901830d754dSRandall Stewart 					 * artificially.
7902830d754dSRandall Stewart 					 */
7903830d754dSRandall Stewart 					tp1->whoTo->cwnd += tp1->book_size;
7904830d754dSRandall Stewart 					cnt_revoked++;
7905830d754dSRandall Stewart 				}
7906830d754dSRandall Stewart 			}
7907830d754dSRandall Stewart 			if (cnt_revoked) {
7908830d754dSRandall Stewart 				reneged_all = 1;
7909830d754dSRandall Stewart 			}
7910830d754dSRandall Stewart 		}
7911830d754dSRandall Stewart 		asoc->saw_sack_with_frags = 0;
7912830d754dSRandall Stewart 	}
7913830d754dSRandall Stewart 	if (num_seg)
7914830d754dSRandall Stewart 		asoc->saw_sack_with_frags = 1;
7915830d754dSRandall Stewart 	else
7916830d754dSRandall Stewart 		asoc->saw_sack_with_frags = 0;
7917830d754dSRandall Stewart 
7918830d754dSRandall Stewart 	/* EY! - not sure about if there should be an IF */
7919830d754dSRandall Stewart 	if (num_nr_seg)
7920830d754dSRandall Stewart 		sctp_check_for_nr_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
7921830d754dSRandall Stewart 	else if (asoc->saw_sack_with_nr_frags) {
7922830d754dSRandall Stewart 		/*
7923830d754dSRandall Stewart 		 * EY!- TODO: all previously nr_gapped chunks have been
7924830d754dSRandall Stewart 		 * reneged abort the association
7925830d754dSRandall Stewart 		 */
7926830d754dSRandall Stewart 		asoc->saw_sack_with_nr_frags = 0;
7927830d754dSRandall Stewart 	}
7928830d754dSRandall Stewart 	if (num_nr_seg)
7929830d754dSRandall Stewart 		asoc->saw_sack_with_nr_frags = 1;
7930830d754dSRandall Stewart 	else
7931830d754dSRandall Stewart 		asoc->saw_sack_with_nr_frags = 0;
7932830d754dSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
7933830d754dSRandall Stewart 	asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
7934830d754dSRandall Stewart 
7935830d754dSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
7936830d754dSRandall Stewart 		/* nothing left in-flight */
7937830d754dSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7938830d754dSRandall Stewart 			/* stop all timers */
7939830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
7940830d754dSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
7941830d754dSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
7942830d754dSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
7943830d754dSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_29);
7944830d754dSRandall Stewart 				}
7945830d754dSRandall Stewart 			}
7946830d754dSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
7947830d754dSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
7948830d754dSRandall Stewart 			net->flight_size = 0;
7949830d754dSRandall Stewart 			net->partial_bytes_acked = 0;
7950830d754dSRandall Stewart 		}
7951830d754dSRandall Stewart 		asoc->total_flight = 0;
7952830d754dSRandall Stewart 		asoc->total_flight_count = 0;
7953830d754dSRandall Stewart 	}
7954830d754dSRandall Stewart 	/**********************************/
7955830d754dSRandall Stewart 	/* Now what about shutdown issues */
7956830d754dSRandall Stewart 	/**********************************/
7957830d754dSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
7958830d754dSRandall Stewart 		/* nothing left on sendqueue.. consider done */
7959830d754dSRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
7960830d754dSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
7961830d754dSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
7962830d754dSRandall Stewart 		}
7963830d754dSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
7964830d754dSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
7965830d754dSRandall Stewart 			/* SWS sender side engages */
7966830d754dSRandall Stewart 			asoc->peers_rwnd = 0;
7967830d754dSRandall Stewart 		}
7968830d754dSRandall Stewart 		/* clean up */
7969830d754dSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
7970830d754dSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7971830d754dSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
7972830d754dSRandall Stewart 		    (asoc->locked_on_sending)
7973830d754dSRandall Stewart 		    ) {
7974830d754dSRandall Stewart 			struct sctp_stream_queue_pending *sp;
7975830d754dSRandall Stewart 
7976830d754dSRandall Stewart 			/*
7977830d754dSRandall Stewart 			 * I may be in a state where we got all across.. but
7978830d754dSRandall Stewart 			 * cannot write more due to a shutdown... we abort
7979830d754dSRandall Stewart 			 * since the user did not indicate EOR in this case.
7980830d754dSRandall Stewart 			 */
7981830d754dSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
7982830d754dSRandall Stewart 			    sctp_streamhead);
7983830d754dSRandall Stewart 			if ((sp) && (sp->length == 0)) {
7984830d754dSRandall Stewart 				asoc->locked_on_sending = NULL;
7985830d754dSRandall Stewart 				if (sp->msg_is_complete) {
7986830d754dSRandall Stewart 					asoc->stream_queue_cnt--;
7987830d754dSRandall Stewart 				} else {
7988830d754dSRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
7989830d754dSRandall Stewart 					asoc->stream_queue_cnt--;
7990830d754dSRandall Stewart 				}
7991830d754dSRandall Stewart 			}
7992830d754dSRandall Stewart 		}
7993830d754dSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
7994830d754dSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
7995830d754dSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
7996830d754dSRandall Stewart 				/* Need to abort here */
7997830d754dSRandall Stewart 				struct mbuf *oper;
7998830d754dSRandall Stewart 
7999830d754dSRandall Stewart 		abort_out_now:
8000830d754dSRandall Stewart 				*abort_now = 1;
8001830d754dSRandall Stewart 				/* XXX */
8002830d754dSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
8003830d754dSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
8004830d754dSRandall Stewart 				if (oper) {
8005830d754dSRandall Stewart 					struct sctp_paramhdr *ph;
8006830d754dSRandall Stewart 					uint32_t *ippp;
8007830d754dSRandall Stewart 
8008830d754dSRandall Stewart 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
8009830d754dSRandall Stewart 					    sizeof(uint32_t);
8010830d754dSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
8011830d754dSRandall Stewart 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
8012830d754dSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
8013830d754dSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
8014830d754dSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_31);
8015830d754dSRandall Stewart 				}
8016830d754dSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
8017830d754dSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
8018830d754dSRandall Stewart 				return;
8019830d754dSRandall Stewart 			} else {
8020830d754dSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
8021830d754dSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
8022830d754dSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
8023830d754dSRandall Stewart 				}
8024830d754dSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
8025830d754dSRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
8026830d754dSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
8027830d754dSRandall Stewart 				sctp_send_shutdown(stcb,
8028830d754dSRandall Stewart 				    stcb->asoc.primary_destination);
8029830d754dSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
8030830d754dSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
8031830d754dSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
8032830d754dSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
8033830d754dSRandall Stewart 			}
8034830d754dSRandall Stewart 			return;
8035830d754dSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
8036830d754dSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
8037830d754dSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
8038830d754dSRandall Stewart 				goto abort_out_now;
8039830d754dSRandall Stewart 			}
8040830d754dSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
8041830d754dSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
8042830d754dSRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
8043830d754dSRandall Stewart 			sctp_send_shutdown_ack(stcb,
8044830d754dSRandall Stewart 			    stcb->asoc.primary_destination);
8045830d754dSRandall Stewart 
8046830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
8047830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, asoc->primary_destination);
8048830d754dSRandall Stewart 			return;
8049830d754dSRandall Stewart 		}
8050830d754dSRandall Stewart 	}
8051830d754dSRandall Stewart 	/*
8052830d754dSRandall Stewart 	 * Now here we are going to recycle net_ack for a different use...
8053830d754dSRandall Stewart 	 * HEADS UP.
8054830d754dSRandall Stewart 	 */
8055830d754dSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8056830d754dSRandall Stewart 		net->net_ack = 0;
8057830d754dSRandall Stewart 	}
8058830d754dSRandall Stewart 
8059830d754dSRandall Stewart 	/*
8060830d754dSRandall Stewart 	 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
8061830d754dSRandall Stewart 	 * to be done. Setting this_sack_lowest_newack to the cum_ack will
8062830d754dSRandall Stewart 	 * automatically ensure that.
8063830d754dSRandall Stewart 	 */
8064830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac) && (cmt_dac_flag == 0)) {
8065830d754dSRandall Stewart 		this_sack_lowest_newack = cum_ack;
8066830d754dSRandall Stewart 	}
8067830d754dSRandall Stewart 	if (num_seg > 0) {
8068830d754dSRandall Stewart 		sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
8069830d754dSRandall Stewart 		    biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
8070830d754dSRandall Stewart 	}
8071830d754dSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
8072830d754dSRandall Stewart 	asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
8073830d754dSRandall Stewart 
8074830d754dSRandall Stewart 	/******************************************************************
8075830d754dSRandall Stewart 	 *  Here we do the stuff with ECN Nonce checking.
8076830d754dSRandall Stewart 	 *  We basically check to see if the nonce sum flag was incorrect
8077830d754dSRandall Stewart 	 *  or if resynchronization needs to be done. Also if we catch a
8078830d754dSRandall Stewart 	 *  misbehaving receiver we give him the kick.
8079830d754dSRandall Stewart 	 ******************************************************************/
8080830d754dSRandall Stewart 
8081830d754dSRandall Stewart 	if (asoc->ecn_nonce_allowed) {
8082830d754dSRandall Stewart 		if (asoc->nonce_sum_check) {
8083830d754dSRandall Stewart 			if (nonce_sum_flag != ((asoc->nonce_sum_expect_base + ecn_seg_sums) & SCTP_SACK_NONCE_SUM)) {
8084830d754dSRandall Stewart 				if (asoc->nonce_wait_for_ecne == 0) {
8085830d754dSRandall Stewart 					struct sctp_tmit_chunk *lchk;
8086830d754dSRandall Stewart 
8087830d754dSRandall Stewart 					lchk = TAILQ_FIRST(&asoc->send_queue);
8088830d754dSRandall Stewart 					asoc->nonce_wait_for_ecne = 1;
8089830d754dSRandall Stewart 					if (lchk) {
8090830d754dSRandall Stewart 						asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
8091830d754dSRandall Stewart 					} else {
8092830d754dSRandall Stewart 						asoc->nonce_wait_tsn = asoc->sending_seq;
8093830d754dSRandall Stewart 					}
8094830d754dSRandall Stewart 				} else {
8095830d754dSRandall Stewart 					if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
8096830d754dSRandall Stewart 					    (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
8097830d754dSRandall Stewart 						/*
8098830d754dSRandall Stewart 						 * Misbehaving peer. We need
8099830d754dSRandall Stewart 						 * to react to this guy
8100830d754dSRandall Stewart 						 */
8101830d754dSRandall Stewart 						asoc->ecn_allowed = 0;
8102830d754dSRandall Stewart 						asoc->ecn_nonce_allowed = 0;
8103830d754dSRandall Stewart 					}
8104830d754dSRandall Stewart 				}
8105830d754dSRandall Stewart 			}
8106830d754dSRandall Stewart 		} else {
8107830d754dSRandall Stewart 			/* See if Resynchronization Possible */
8108830d754dSRandall Stewart 			if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
8109830d754dSRandall Stewart 				asoc->nonce_sum_check = 1;
8110830d754dSRandall Stewart 				/*
8111830d754dSRandall Stewart 				 * now we must calculate what the base is.
8112830d754dSRandall Stewart 				 * We do this based on two things, we know
8113830d754dSRandall Stewart 				 * the total's for all the segments
8114830d754dSRandall Stewart 				 * gap-acked in the SACK, its stored in
8115830d754dSRandall Stewart 				 * ecn_seg_sums. We also know the SACK's
8116830d754dSRandall Stewart 				 * nonce sum, its in nonce_sum_flag. So we
8117830d754dSRandall Stewart 				 * can build a truth table to back-calculate
8118830d754dSRandall Stewart 				 * the new value of
8119830d754dSRandall Stewart 				 * asoc->nonce_sum_expect_base:
8120830d754dSRandall Stewart 				 *
8121830d754dSRandall Stewart 				 * SACK-flag-Value         Seg-Sums Base 0 0 0
8122830d754dSRandall Stewart 				 * 1                    0 1 0 1 1 1 1 0
8123830d754dSRandall Stewart 				 */
8124830d754dSRandall Stewart 				asoc->nonce_sum_expect_base = (ecn_seg_sums ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
8125830d754dSRandall Stewart 			}
8126830d754dSRandall Stewart 		}
8127830d754dSRandall Stewart 	}
8128830d754dSRandall Stewart 	/* Now are we exiting loss recovery ? */
8129830d754dSRandall Stewart 	if (will_exit_fast_recovery) {
8130830d754dSRandall Stewart 		/* Ok, we must exit fast recovery */
8131830d754dSRandall Stewart 		asoc->fast_retran_loss_recovery = 0;
8132830d754dSRandall Stewart 	}
8133830d754dSRandall Stewart 	if ((asoc->sat_t3_loss_recovery) &&
8134830d754dSRandall Stewart 	    ((compare_with_wrap(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn,
8135830d754dSRandall Stewart 	    MAX_TSN) ||
8136830d754dSRandall Stewart 	    (asoc->last_acked_seq == asoc->sat_t3_recovery_tsn)))) {
8137830d754dSRandall Stewart 		/* end satellite t3 loss recovery */
8138830d754dSRandall Stewart 		asoc->sat_t3_loss_recovery = 0;
8139830d754dSRandall Stewart 	}
8140830d754dSRandall Stewart 	/*
8141830d754dSRandall Stewart 	 * CMT Fast recovery
8142830d754dSRandall Stewart 	 */
8143830d754dSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8144830d754dSRandall Stewart 		if (net->will_exit_fast_recovery) {
8145830d754dSRandall Stewart 			/* Ok, we must exit fast recovery */
8146830d754dSRandall Stewart 			net->fast_retran_loss_recovery = 0;
8147830d754dSRandall Stewart 		}
8148830d754dSRandall Stewart 	}
8149830d754dSRandall Stewart 
8150830d754dSRandall Stewart 	/* Adjust and set the new rwnd value */
8151830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
8152830d754dSRandall Stewart 		sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
8153830d754dSRandall Stewart 		    asoc->peers_rwnd, asoc->total_flight, (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)), a_rwnd);
8154830d754dSRandall Stewart 	}
8155830d754dSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
8156830d754dSRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
8157830d754dSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
8158830d754dSRandall Stewart 		/* SWS sender side engages */
8159830d754dSRandall Stewart 		asoc->peers_rwnd = 0;
8160830d754dSRandall Stewart 	}
8161830d754dSRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
8162830d754dSRandall Stewart 		win_probe_recovery = 1;
8163830d754dSRandall Stewart 	}
8164830d754dSRandall Stewart 	/*
8165830d754dSRandall Stewart 	 * Now we must setup so we have a timer up for anyone with
8166830d754dSRandall Stewart 	 * outstanding data.
8167830d754dSRandall Stewart 	 */
8168830d754dSRandall Stewart 	done_once = 0;
8169830d754dSRandall Stewart again:
8170830d754dSRandall Stewart 	j = 0;
8171830d754dSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8172830d754dSRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
8173830d754dSRandall Stewart 			win_probe_recovered = 1;
8174830d754dSRandall Stewart 			/*-
8175830d754dSRandall Stewart 			 * Find first chunk that was used with
8176830d754dSRandall Stewart 			 * window probe and clear the event. Put
8177830d754dSRandall Stewart 			 * it back into the send queue as if has
8178830d754dSRandall Stewart 			 * not been sent.
8179830d754dSRandall Stewart 			 */
8180830d754dSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
8181830d754dSRandall Stewart 				if (tp1->window_probe) {
8182830d754dSRandall Stewart 					sctp_window_probe_recovery(stcb, asoc, net, tp1);
8183830d754dSRandall Stewart 					break;
8184830d754dSRandall Stewart 				}
8185830d754dSRandall Stewart 			}
8186830d754dSRandall Stewart 		}
8187830d754dSRandall Stewart 		if (net->flight_size) {
8188830d754dSRandall Stewart 			j++;
8189830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
8190830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, net);
81915171328bSRandall Stewart 			if (net->window_probe) {
81925171328bSRandall Stewart 				net->window_probe = 0;
81935171328bSRandall Stewart 			}
8194830d754dSRandall Stewart 		} else {
81955171328bSRandall Stewart 			if (net->window_probe) {
81965171328bSRandall Stewart 				net->window_probe = 0;
81975171328bSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SEND,
81985171328bSRandall Stewart 				    stcb->sctp_ep, stcb, net);
81995171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8200830d754dSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
8201830d754dSRandall Stewart 				    stcb, net,
8202830d754dSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
8203830d754dSRandall Stewart 			}
8204830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
8205830d754dSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8206830d754dSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
8207830d754dSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
8208830d754dSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
8209830d754dSRandall Stewart 				}
8210830d754dSRandall Stewart 			}
8211830d754dSRandall Stewart 		}
8212830d754dSRandall Stewart 	}
8213830d754dSRandall Stewart 	if ((j == 0) &&
8214830d754dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
8215830d754dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
8216830d754dSRandall Stewart 	    (win_probe_recovered == 0) &&
8217830d754dSRandall Stewart 	    (done_once == 0)) {
82180c0982b8SRandall Stewart 		/*
82190c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
82200c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
82210c0982b8SRandall Stewart 		 */
82220c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
8223830d754dSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8224830d754dSRandall Stewart 				net->flight_size = 0;
8225830d754dSRandall Stewart 			}
8226830d754dSRandall Stewart 			asoc->total_flight = 0;
8227830d754dSRandall Stewart 			asoc->total_flight_count = 0;
8228830d754dSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
8229830d754dSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
8230830d754dSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
8231830d754dSRandall Stewart 					sctp_flight_size_increase(tp1);
8232830d754dSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
8233830d754dSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
8234830d754dSRandall Stewart 					asoc->sent_queue_retran_cnt++;
8235830d754dSRandall Stewart 				}
8236830d754dSRandall Stewart 			}
82370c0982b8SRandall Stewart 		}
8238830d754dSRandall Stewart 		done_once = 1;
8239830d754dSRandall Stewart 		goto again;
8240830d754dSRandall Stewart 	}
8241830d754dSRandall Stewart 	/*********************************************/
8242830d754dSRandall Stewart 	/* Here we perform PR-SCTP procedures        */
8243830d754dSRandall Stewart 	/* (section 4.2)                             */
8244830d754dSRandall Stewart 	/*********************************************/
8245830d754dSRandall Stewart 	/* C1. update advancedPeerAckPoint */
8246830d754dSRandall Stewart 	if (compare_with_wrap(cum_ack, asoc->advanced_peer_ack_point, MAX_TSN)) {
8247830d754dSRandall Stewart 		asoc->advanced_peer_ack_point = cum_ack;
8248830d754dSRandall Stewart 	}
8249830d754dSRandall Stewart 	/* C2. try to further move advancedPeerAckPoint ahead */
8250830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
8251830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
8252830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
8253830d754dSRandall Stewart 
8254830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
8255830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
8256830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
8257830d754dSRandall Stewart 		if (compare_with_wrap(asoc->advanced_peer_ack_point, cum_ack,
8258830d754dSRandall Stewart 		    MAX_TSN)) {
8259830d754dSRandall Stewart 			/*
8260830d754dSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing for notes
8261830d754dSRandall Stewart 			 * on issues that will occur when the ECN NONCE
8262830d754dSRandall Stewart 			 * stuff is put into SCTP for cross checking.
8263830d754dSRandall Stewart 			 */
8264830d754dSRandall Stewart 			if (compare_with_wrap(asoc->advanced_peer_ack_point, old_adv_peer_ack_point,
8265830d754dSRandall Stewart 			    MAX_TSN)) {
8266830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
8267830d754dSRandall Stewart 				/*
8268830d754dSRandall Stewart 				 * ECN Nonce: Disable Nonce Sum check when
8269830d754dSRandall Stewart 				 * FWD TSN is sent and store resync tsn
8270830d754dSRandall Stewart 				 */
8271830d754dSRandall Stewart 				asoc->nonce_sum_check = 0;
8272830d754dSRandall Stewart 				asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
82730c0982b8SRandall Stewart 			} else if (lchk) {
82740c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
82750c0982b8SRandall Stewart 				lchk->rec.data.fwd_tsn_cnt++;
82760c0982b8SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt > 3) {
82770c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
82780c0982b8SRandall Stewart 					lchk->rec.data.fwd_tsn_cnt = 0;
82790c0982b8SRandall Stewart 				}
8280830d754dSRandall Stewart 			}
8281830d754dSRandall Stewart 		}
8282830d754dSRandall Stewart 		if (lchk) {
8283830d754dSRandall Stewart 			/* Assure a timer is up */
8284830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
8285830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
8286830d754dSRandall Stewart 		}
8287830d754dSRandall Stewart 	}
8288830d754dSRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
8289830d754dSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
8290830d754dSRandall Stewart 		    a_rwnd,
8291830d754dSRandall Stewart 		    stcb->asoc.peers_rwnd,
8292830d754dSRandall Stewart 		    stcb->asoc.total_flight,
8293830d754dSRandall Stewart 		    stcb->asoc.total_output_queue_size);
8294830d754dSRandall Stewart 	}
8295830d754dSRandall Stewart }
8296