xref: /freebsd/sys/netinet/sctp_indata.c (revision 77acdc25650eec0e709bfe7bb6581763923e4e7b)
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 { \
4977acdc25SRandall Stewart 	                if (tsn >= mapping_tsn) { \
50d50c1d79SRandall Stewart 						gap = tsn - mapping_tsn; \
51d50c1d79SRandall Stewart 					} else { \
52d50c1d79SRandall Stewart 						gap = (MAX_TSN - mapping_tsn) + tsn + 1; \
53d50c1d79SRandall Stewart 					} \
54d50c1d79SRandall Stewart                   } while(0)
55d50c1d79SRandall Stewart 
56f8829a4aSRandall Stewart /*
57f8829a4aSRandall Stewart  * NOTES: On the outbound side of things I need to check the sack timer to
58f8829a4aSRandall Stewart  * see if I should generate a sack into the chunk queue (if I have data to
59f8829a4aSRandall Stewart  * send that is and will be sending it .. for bundling.
60f8829a4aSRandall Stewart  *
61f8829a4aSRandall Stewart  * The callback in sctp_usrreq.c will get called when the socket is read from.
62f8829a4aSRandall Stewart  * This will cause sctp_service_queues() to get called on the top entry in
63f8829a4aSRandall Stewart  * the list.
64f8829a4aSRandall Stewart  */
65f8829a4aSRandall Stewart 
6672fb6fdbSRandall Stewart void
67f8829a4aSRandall Stewart sctp_set_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
68f8829a4aSRandall Stewart {
69b3f1ea41SRandall Stewart 	asoc->my_rwnd = sctp_calc_rwnd(stcb, asoc);
70f8829a4aSRandall Stewart }
71f8829a4aSRandall Stewart 
72f8829a4aSRandall Stewart /* Calculate what the rwnd would be */
7372fb6fdbSRandall Stewart uint32_t
74f8829a4aSRandall Stewart sctp_calc_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
75f8829a4aSRandall Stewart {
76b3f1ea41SRandall Stewart 	uint32_t calc = 0;
77f8829a4aSRandall Stewart 
78f8829a4aSRandall Stewart 	/*
79f8829a4aSRandall Stewart 	 * This is really set wrong with respect to a 1-2-m socket. Since
80f8829a4aSRandall Stewart 	 * the sb_cc is the count that everyone as put up. When we re-write
81f8829a4aSRandall Stewart 	 * sctp_soreceive then we will fix this so that ONLY this
82f8829a4aSRandall Stewart 	 * associations data is taken into account.
83f8829a4aSRandall Stewart 	 */
84f8829a4aSRandall Stewart 	if (stcb->sctp_socket == NULL)
85f8829a4aSRandall Stewart 		return (calc);
86f8829a4aSRandall Stewart 
87f8829a4aSRandall Stewart 	if (stcb->asoc.sb_cc == 0 &&
88f8829a4aSRandall Stewart 	    asoc->size_on_reasm_queue == 0 &&
89f8829a4aSRandall Stewart 	    asoc->size_on_all_streams == 0) {
90f8829a4aSRandall Stewart 		/* Full rwnd granted */
91b3f1ea41SRandall Stewart 		calc = max(SCTP_SB_LIMIT_RCV(stcb->sctp_socket), SCTP_MINIMAL_RWND);
92f8829a4aSRandall Stewart 		return (calc);
93f8829a4aSRandall Stewart 	}
94f8829a4aSRandall Stewart 	/* get actual space */
95f8829a4aSRandall Stewart 	calc = (uint32_t) sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv);
96f8829a4aSRandall Stewart 
97f8829a4aSRandall Stewart 	/*
98f8829a4aSRandall Stewart 	 * take out what has NOT been put on socket queue and we yet hold
99f8829a4aSRandall Stewart 	 * for putting up.
100f8829a4aSRandall Stewart 	 */
101f8829a4aSRandall Stewart 	calc = sctp_sbspace_sub(calc, (uint32_t) asoc->size_on_reasm_queue);
102f8829a4aSRandall Stewart 	calc = sctp_sbspace_sub(calc, (uint32_t) asoc->size_on_all_streams);
103f8829a4aSRandall Stewart 
104f8829a4aSRandall Stewart 	if (calc == 0) {
105f8829a4aSRandall Stewart 		/* out of space */
106f8829a4aSRandall Stewart 		return (calc);
107f8829a4aSRandall Stewart 	}
108f8829a4aSRandall Stewart 	/* what is the overhead of all these rwnd's */
1092afb3e84SRandall Stewart 	calc = sctp_sbspace_sub(calc, stcb->asoc.my_rwnd_control_len);
110b3f1ea41SRandall Stewart 	/*
111b3f1ea41SRandall Stewart 	 * If the window gets too small due to ctrl-stuff, reduce it to 1,
112b3f1ea41SRandall Stewart 	 * even it is 0. SWS engaged
113f8829a4aSRandall Stewart 	 */
114b3f1ea41SRandall Stewart 	if (calc < stcb->asoc.my_rwnd_control_len) {
115b3f1ea41SRandall Stewart 		calc = 1;
1162afb3e84SRandall Stewart 	}
117b3f1ea41SRandall Stewart 	return (calc);
118f8829a4aSRandall Stewart }
119f8829a4aSRandall Stewart 
120f8829a4aSRandall Stewart 
121f8829a4aSRandall Stewart 
122f8829a4aSRandall Stewart /*
123f8829a4aSRandall Stewart  * Build out our readq entry based on the incoming packet.
124f8829a4aSRandall Stewart  */
125f8829a4aSRandall Stewart struct sctp_queued_to_read *
126f8829a4aSRandall Stewart sctp_build_readq_entry(struct sctp_tcb *stcb,
127f8829a4aSRandall Stewart     struct sctp_nets *net,
128f8829a4aSRandall Stewart     uint32_t tsn, uint32_t ppid,
129f8829a4aSRandall Stewart     uint32_t context, uint16_t stream_no,
130f8829a4aSRandall Stewart     uint16_t stream_seq, uint8_t flags,
131f8829a4aSRandall Stewart     struct mbuf *dm)
132f8829a4aSRandall Stewart {
133f8829a4aSRandall Stewart 	struct sctp_queued_to_read *read_queue_e = NULL;
134f8829a4aSRandall Stewart 
135f8829a4aSRandall Stewart 	sctp_alloc_a_readq(stcb, read_queue_e);
136f8829a4aSRandall Stewart 	if (read_queue_e == NULL) {
137f8829a4aSRandall Stewart 		goto failed_build;
138f8829a4aSRandall Stewart 	}
139f8829a4aSRandall Stewart 	read_queue_e->sinfo_stream = stream_no;
140f8829a4aSRandall Stewart 	read_queue_e->sinfo_ssn = stream_seq;
141f8829a4aSRandall Stewart 	read_queue_e->sinfo_flags = (flags << 8);
142f8829a4aSRandall Stewart 	read_queue_e->sinfo_ppid = ppid;
143f8829a4aSRandall Stewart 	read_queue_e->sinfo_context = stcb->asoc.context;
144f8829a4aSRandall Stewart 	read_queue_e->sinfo_timetolive = 0;
145f8829a4aSRandall Stewart 	read_queue_e->sinfo_tsn = tsn;
146f8829a4aSRandall Stewart 	read_queue_e->sinfo_cumtsn = tsn;
147f8829a4aSRandall Stewart 	read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
148f8829a4aSRandall Stewart 	read_queue_e->whoFrom = net;
149f8829a4aSRandall Stewart 	read_queue_e->length = 0;
150f8829a4aSRandall Stewart 	atomic_add_int(&net->ref_count, 1);
151f8829a4aSRandall Stewart 	read_queue_e->data = dm;
152139bc87fSRandall Stewart 	read_queue_e->spec_flags = 0;
153f8829a4aSRandall Stewart 	read_queue_e->tail_mbuf = NULL;
15417205eccSRandall Stewart 	read_queue_e->aux_data = NULL;
155f8829a4aSRandall Stewart 	read_queue_e->stcb = stcb;
156f8829a4aSRandall Stewart 	read_queue_e->port_from = stcb->rport;
157f8829a4aSRandall Stewart 	read_queue_e->do_not_ref_stcb = 0;
158f8829a4aSRandall Stewart 	read_queue_e->end_added = 0;
1599a6142d8SRandall Stewart 	read_queue_e->some_taken = 0;
16003b0b021SRandall Stewart 	read_queue_e->pdapi_aborted = 0;
161f8829a4aSRandall Stewart failed_build:
162f8829a4aSRandall Stewart 	return (read_queue_e);
163f8829a4aSRandall Stewart }
164f8829a4aSRandall Stewart 
165f8829a4aSRandall Stewart 
166f8829a4aSRandall Stewart /*
167f8829a4aSRandall Stewart  * Build out our readq entry based on the incoming packet.
168f8829a4aSRandall Stewart  */
169f8829a4aSRandall Stewart static struct sctp_queued_to_read *
170f8829a4aSRandall Stewart sctp_build_readq_entry_chk(struct sctp_tcb *stcb,
171f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk)
172f8829a4aSRandall Stewart {
173f8829a4aSRandall Stewart 	struct sctp_queued_to_read *read_queue_e = NULL;
174f8829a4aSRandall Stewart 
175f8829a4aSRandall Stewart 	sctp_alloc_a_readq(stcb, read_queue_e);
176f8829a4aSRandall Stewart 	if (read_queue_e == NULL) {
177f8829a4aSRandall Stewart 		goto failed_build;
178f8829a4aSRandall Stewart 	}
179f8829a4aSRandall Stewart 	read_queue_e->sinfo_stream = chk->rec.data.stream_number;
180f8829a4aSRandall Stewart 	read_queue_e->sinfo_ssn = chk->rec.data.stream_seq;
181f8829a4aSRandall Stewart 	read_queue_e->sinfo_flags = (chk->rec.data.rcv_flags << 8);
182f8829a4aSRandall Stewart 	read_queue_e->sinfo_ppid = chk->rec.data.payloadtype;
183f8829a4aSRandall Stewart 	read_queue_e->sinfo_context = stcb->asoc.context;
184f8829a4aSRandall Stewart 	read_queue_e->sinfo_timetolive = 0;
185f8829a4aSRandall Stewart 	read_queue_e->sinfo_tsn = chk->rec.data.TSN_seq;
186f8829a4aSRandall Stewart 	read_queue_e->sinfo_cumtsn = chk->rec.data.TSN_seq;
187f8829a4aSRandall Stewart 	read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
188f8829a4aSRandall Stewart 	read_queue_e->whoFrom = chk->whoTo;
18917205eccSRandall Stewart 	read_queue_e->aux_data = NULL;
190f8829a4aSRandall Stewart 	read_queue_e->length = 0;
191f8829a4aSRandall Stewart 	atomic_add_int(&chk->whoTo->ref_count, 1);
192f8829a4aSRandall Stewart 	read_queue_e->data = chk->data;
193f8829a4aSRandall Stewart 	read_queue_e->tail_mbuf = NULL;
194f8829a4aSRandall Stewart 	read_queue_e->stcb = stcb;
195f8829a4aSRandall Stewart 	read_queue_e->port_from = stcb->rport;
196139bc87fSRandall Stewart 	read_queue_e->spec_flags = 0;
197f8829a4aSRandall Stewart 	read_queue_e->do_not_ref_stcb = 0;
198f8829a4aSRandall Stewart 	read_queue_e->end_added = 0;
1999a6142d8SRandall Stewart 	read_queue_e->some_taken = 0;
20003b0b021SRandall Stewart 	read_queue_e->pdapi_aborted = 0;
201f8829a4aSRandall Stewart failed_build:
202f8829a4aSRandall Stewart 	return (read_queue_e);
203f8829a4aSRandall Stewart }
204f8829a4aSRandall Stewart 
205f8829a4aSRandall Stewart 
206f8829a4aSRandall Stewart struct mbuf *
207f8829a4aSRandall Stewart sctp_build_ctl_nchunk(struct sctp_inpcb *inp,
208f8829a4aSRandall Stewart     struct sctp_sndrcvinfo *sinfo)
209f8829a4aSRandall Stewart {
210f8829a4aSRandall Stewart 	struct sctp_sndrcvinfo *outinfo;
211f8829a4aSRandall Stewart 	struct cmsghdr *cmh;
212f8829a4aSRandall Stewart 	struct mbuf *ret;
213f8829a4aSRandall Stewart 	int len;
214f8829a4aSRandall Stewart 	int use_extended = 0;
215f8829a4aSRandall Stewart 
216f8829a4aSRandall Stewart 	if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
217f8829a4aSRandall Stewart 		/* user does not want the sndrcv ctl */
218f8829a4aSRandall Stewart 		return (NULL);
219f8829a4aSRandall Stewart 	}
220f8829a4aSRandall Stewart 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
221f8829a4aSRandall Stewart 		use_extended = 1;
222f8829a4aSRandall Stewart 		len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
223f8829a4aSRandall Stewart 	} else {
224f8829a4aSRandall Stewart 		len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
225f8829a4aSRandall Stewart 	}
226f8829a4aSRandall Stewart 
227f8829a4aSRandall Stewart 
228f8829a4aSRandall Stewart 	ret = sctp_get_mbuf_for_msg(len,
229139bc87fSRandall Stewart 	    0, M_DONTWAIT, 1, MT_DATA);
230f8829a4aSRandall Stewart 
231f8829a4aSRandall Stewart 	if (ret == NULL) {
232f8829a4aSRandall Stewart 		/* No space */
233f8829a4aSRandall Stewart 		return (ret);
234f8829a4aSRandall Stewart 	}
235f8829a4aSRandall Stewart 	/* We need a CMSG header followed by the struct  */
236f8829a4aSRandall Stewart 	cmh = mtod(ret, struct cmsghdr *);
237f8829a4aSRandall Stewart 	outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
238f8829a4aSRandall Stewart 	cmh->cmsg_level = IPPROTO_SCTP;
239f8829a4aSRandall Stewart 	if (use_extended) {
240f8829a4aSRandall Stewart 		cmh->cmsg_type = SCTP_EXTRCV;
241f8829a4aSRandall Stewart 		cmh->cmsg_len = len;
242f8829a4aSRandall Stewart 		memcpy(outinfo, sinfo, len);
243f8829a4aSRandall Stewart 	} else {
244f8829a4aSRandall Stewart 		cmh->cmsg_type = SCTP_SNDRCV;
245f8829a4aSRandall Stewart 		cmh->cmsg_len = len;
246f8829a4aSRandall Stewart 		*outinfo = *sinfo;
247f8829a4aSRandall Stewart 	}
248139bc87fSRandall Stewart 	SCTP_BUF_LEN(ret) = cmh->cmsg_len;
249f8829a4aSRandall Stewart 	return (ret);
250f8829a4aSRandall Stewart }
251f8829a4aSRandall Stewart 
252139bc87fSRandall Stewart 
25317205eccSRandall Stewart char *
25417205eccSRandall Stewart sctp_build_ctl_cchunk(struct sctp_inpcb *inp,
25517205eccSRandall Stewart     int *control_len,
25617205eccSRandall Stewart     struct sctp_sndrcvinfo *sinfo)
25717205eccSRandall Stewart {
25817205eccSRandall Stewart 	struct sctp_sndrcvinfo *outinfo;
25917205eccSRandall Stewart 	struct cmsghdr *cmh;
26017205eccSRandall Stewart 	char *buf;
26117205eccSRandall Stewart 	int len;
26217205eccSRandall Stewart 	int use_extended = 0;
26317205eccSRandall Stewart 
26417205eccSRandall Stewart 	if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
26517205eccSRandall Stewart 		/* user does not want the sndrcv ctl */
26617205eccSRandall Stewart 		return (NULL);
26717205eccSRandall Stewart 	}
26817205eccSRandall Stewart 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
26917205eccSRandall Stewart 		use_extended = 1;
27017205eccSRandall Stewart 		len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
27117205eccSRandall Stewart 	} else {
27217205eccSRandall Stewart 		len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
27317205eccSRandall Stewart 	}
274207304d4SRandall Stewart 	SCTP_MALLOC(buf, char *, len, SCTP_M_CMSG);
27517205eccSRandall Stewart 	if (buf == NULL) {
27617205eccSRandall Stewart 		/* No space */
27717205eccSRandall Stewart 		return (buf);
27817205eccSRandall Stewart 	}
27917205eccSRandall Stewart 	/* We need a CMSG header followed by the struct  */
28017205eccSRandall Stewart 	cmh = (struct cmsghdr *)buf;
28117205eccSRandall Stewart 	outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
28217205eccSRandall Stewart 	cmh->cmsg_level = IPPROTO_SCTP;
28317205eccSRandall Stewart 	if (use_extended) {
28417205eccSRandall Stewart 		cmh->cmsg_type = SCTP_EXTRCV;
28517205eccSRandall Stewart 		cmh->cmsg_len = len;
28617205eccSRandall Stewart 		memcpy(outinfo, sinfo, len);
28717205eccSRandall Stewart 	} else {
28817205eccSRandall Stewart 		cmh->cmsg_type = SCTP_SNDRCV;
28917205eccSRandall Stewart 		cmh->cmsg_len = len;
29017205eccSRandall Stewart 		*outinfo = *sinfo;
29117205eccSRandall Stewart 	}
29217205eccSRandall Stewart 	*control_len = len;
29317205eccSRandall Stewart 	return (buf);
29417205eccSRandall Stewart }
29517205eccSRandall Stewart 
29677acdc25SRandall Stewart static void
29777acdc25SRandall Stewart sctp_mark_non_revokable(struct sctp_association *asoc, uint32_t tsn)
29877acdc25SRandall Stewart {
29977acdc25SRandall Stewart 	uint32_t gap, i;
30077acdc25SRandall Stewart 	int fnd = 0;
30177acdc25SRandall Stewart 
30277acdc25SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
30377acdc25SRandall Stewart 		return;
30477acdc25SRandall Stewart 	}
30577acdc25SRandall Stewart 	SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
30677acdc25SRandall Stewart #ifdef INVARIANTS
30777acdc25SRandall Stewart 	if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
30877acdc25SRandall Stewart 		printf("gap:%x tsn:%x\n", gap, tsn);
30977acdc25SRandall Stewart 		sctp_print_mapping_array(asoc);
31077acdc25SRandall Stewart 		panic("Things are really messed up now!!");
31177acdc25SRandall Stewart 	}
31277acdc25SRandall Stewart #endif
31377acdc25SRandall Stewart 	SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
31477acdc25SRandall Stewart 	SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
31577acdc25SRandall Stewart 	if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN)) {
31677acdc25SRandall Stewart 		asoc->highest_tsn_inside_nr_map = tsn;
31777acdc25SRandall Stewart 	}
31877acdc25SRandall Stewart 	if (tsn == asoc->highest_tsn_inside_map) {
31977acdc25SRandall Stewart 		/* We must back down to see what the new highest is */
32077acdc25SRandall Stewart 		for (i = tsn - 1; compare_with_wrap(i, asoc->mapping_array_base_tsn, MAX_TSN); i--) {
32177acdc25SRandall Stewart 			SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn);
32277acdc25SRandall Stewart 			if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
32377acdc25SRandall Stewart 				asoc->highest_tsn_inside_map = i;
32477acdc25SRandall Stewart 				fnd = 1;
32577acdc25SRandall Stewart 				break;
32677acdc25SRandall Stewart 			}
32777acdc25SRandall Stewart 		}
32877acdc25SRandall Stewart 		if (!fnd) {
32977acdc25SRandall Stewart 			asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1;
33077acdc25SRandall Stewart 		}
33177acdc25SRandall Stewart 	}
33277acdc25SRandall Stewart }
33377acdc25SRandall Stewart 
33417205eccSRandall Stewart 
335f8829a4aSRandall Stewart /*
336f8829a4aSRandall Stewart  * We are delivering currently from the reassembly queue. We must continue to
337f8829a4aSRandall Stewart  * deliver until we either: 1) run out of space. 2) run out of sequential
338f8829a4aSRandall Stewart  * TSN's 3) hit the SCTP_DATA_LAST_FRAG flag.
339f8829a4aSRandall Stewart  */
340f8829a4aSRandall Stewart static void
341f8829a4aSRandall Stewart sctp_service_reassembly(struct sctp_tcb *stcb, struct sctp_association *asoc)
342f8829a4aSRandall Stewart {
343f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
344f8829a4aSRandall Stewart 	uint16_t nxt_todel;
345f8829a4aSRandall Stewart 	uint16_t stream_no;
346f8829a4aSRandall Stewart 	int end = 0;
347f8829a4aSRandall Stewart 	int cntDel;
348830d754dSRandall Stewart 
349f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control, *ctl, *ctlat;
350f8829a4aSRandall Stewart 
351ad81507eSRandall Stewart 	if (stcb == NULL)
352ad81507eSRandall Stewart 		return;
353ad81507eSRandall Stewart 
354f42a358aSRandall Stewart 	cntDel = stream_no = 0;
355ad81507eSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
356851b7298SRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
357ad81507eSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
358851b7298SRandall Stewart 		/* socket above is long gone or going.. */
359851b7298SRandall Stewart abandon:
360f8829a4aSRandall Stewart 		asoc->fragmented_delivery_inprogress = 0;
361f8829a4aSRandall Stewart 		chk = TAILQ_FIRST(&asoc->reasmqueue);
362f8829a4aSRandall Stewart 		while (chk) {
363f8829a4aSRandall Stewart 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
364f8829a4aSRandall Stewart 			asoc->size_on_reasm_queue -= chk->send_size;
365f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_reasm_queue);
366f8829a4aSRandall Stewart 			/*
367f8829a4aSRandall Stewart 			 * Lose the data pointer, since its in the socket
368f8829a4aSRandall Stewart 			 * buffer
369f8829a4aSRandall Stewart 			 */
370f8829a4aSRandall Stewart 			if (chk->data) {
371f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
372f8829a4aSRandall Stewart 				chk->data = NULL;
373f8829a4aSRandall Stewart 			}
374f8829a4aSRandall Stewart 			/* Now free the address and data */
375f8829a4aSRandall Stewart 			sctp_free_a_chunk(stcb, chk);
3763c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
377f8829a4aSRandall Stewart 			chk = TAILQ_FIRST(&asoc->reasmqueue);
378f8829a4aSRandall Stewart 		}
379f8829a4aSRandall Stewart 		return;
380f8829a4aSRandall Stewart 	}
381f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
382f8829a4aSRandall Stewart 	do {
383f8829a4aSRandall Stewart 		chk = TAILQ_FIRST(&asoc->reasmqueue);
384f8829a4aSRandall Stewart 		if (chk == NULL) {
385f8829a4aSRandall Stewart 			return;
386f8829a4aSRandall Stewart 		}
387f8829a4aSRandall Stewart 		if (chk->rec.data.TSN_seq != (asoc->tsn_last_delivered + 1)) {
388f8829a4aSRandall Stewart 			/* Can't deliver more :< */
389f8829a4aSRandall Stewart 			return;
390f8829a4aSRandall Stewart 		}
391f8829a4aSRandall Stewart 		stream_no = chk->rec.data.stream_number;
392f8829a4aSRandall Stewart 		nxt_todel = asoc->strmin[stream_no].last_sequence_delivered + 1;
393f8829a4aSRandall Stewart 		if (nxt_todel != chk->rec.data.stream_seq &&
394f8829a4aSRandall Stewart 		    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
395f8829a4aSRandall Stewart 			/*
396f8829a4aSRandall Stewart 			 * Not the next sequence to deliver in its stream OR
397f8829a4aSRandall Stewart 			 * unordered
398f8829a4aSRandall Stewart 			 */
399f8829a4aSRandall Stewart 			return;
400f8829a4aSRandall Stewart 		}
401f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
402f8829a4aSRandall Stewart 
403f8829a4aSRandall Stewart 			control = sctp_build_readq_entry_chk(stcb, chk);
404f8829a4aSRandall Stewart 			if (control == NULL) {
405f8829a4aSRandall Stewart 				/* out of memory? */
406f8829a4aSRandall Stewart 				return;
407f8829a4aSRandall Stewart 			}
408f8829a4aSRandall Stewart 			/* save it off for our future deliveries */
409f8829a4aSRandall Stewart 			stcb->asoc.control_pdapi = control;
410f8829a4aSRandall Stewart 			if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
411f8829a4aSRandall Stewart 				end = 1;
412f8829a4aSRandall Stewart 			else
413f8829a4aSRandall Stewart 				end = 0;
414f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep,
415cfde3ff7SRandall Stewart 			    stcb, control, &stcb->sctp_socket->so_rcv, end,
416cfde3ff7SRandall Stewart 			    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
417f8829a4aSRandall Stewart 			cntDel++;
418f8829a4aSRandall Stewart 		} else {
419f8829a4aSRandall Stewart 			if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
420f8829a4aSRandall Stewart 				end = 1;
421f8829a4aSRandall Stewart 			else
422f8829a4aSRandall Stewart 				end = 0;
423f8829a4aSRandall Stewart 			if (sctp_append_to_readq(stcb->sctp_ep, stcb,
424f8829a4aSRandall Stewart 			    stcb->asoc.control_pdapi,
425f8829a4aSRandall Stewart 			    chk->data, end, chk->rec.data.TSN_seq,
426f8829a4aSRandall Stewart 			    &stcb->sctp_socket->so_rcv)) {
427f8829a4aSRandall Stewart 				/*
428f8829a4aSRandall Stewart 				 * something is very wrong, either
429f8829a4aSRandall Stewart 				 * control_pdapi is NULL, or the tail_mbuf
430f8829a4aSRandall Stewart 				 * is corrupt, or there is a EOM already on
431f8829a4aSRandall Stewart 				 * the mbuf chain.
432f8829a4aSRandall Stewart 				 */
433851b7298SRandall Stewart 				if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
434851b7298SRandall Stewart 					goto abandon;
435851b7298SRandall Stewart 				} else {
436df6e0cc3SRandall Stewart #ifdef INVARIANTS
437ad81507eSRandall Stewart 					if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
438f8829a4aSRandall Stewart 						panic("This should not happen control_pdapi NULL?");
439f8829a4aSRandall Stewart 					}
440f8829a4aSRandall Stewart 					/* if we did not panic, it was a EOM */
441f8829a4aSRandall Stewart 					panic("Bad chunking ??");
442df6e0cc3SRandall Stewart #else
443df6e0cc3SRandall Stewart 					if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
444df6e0cc3SRandall Stewart 						SCTP_PRINTF("This should not happen control_pdapi NULL?\n");
445df6e0cc3SRandall Stewart 					}
446df6e0cc3SRandall Stewart 					SCTP_PRINTF("Bad chunking ??\n");
447df6e0cc3SRandall Stewart 					SCTP_PRINTF("Dumping re-assembly queue this will probably hose the association\n");
448df6e0cc3SRandall Stewart 
449df6e0cc3SRandall Stewart #endif
450df6e0cc3SRandall Stewart 					goto abandon;
451f8829a4aSRandall Stewart 				}
452851b7298SRandall Stewart 			}
453f8829a4aSRandall Stewart 			cntDel++;
454f8829a4aSRandall Stewart 		}
455f8829a4aSRandall Stewart 		/* pull it we did it */
456f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
45777acdc25SRandall Stewart 		sctp_mark_non_revokable(asoc, chk->rec.data.TSN_seq);
458f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
459f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 0;
460f8829a4aSRandall Stewart 			if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
461f8829a4aSRandall Stewart 				asoc->strmin[stream_no].last_sequence_delivered++;
462f8829a4aSRandall Stewart 			}
463f8829a4aSRandall Stewart 			if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
464f8829a4aSRandall Stewart 				SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
465f8829a4aSRandall Stewart 			}
466f8829a4aSRandall Stewart 		} else if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
467f8829a4aSRandall Stewart 			/*
468f8829a4aSRandall Stewart 			 * turn the flag back on since we just  delivered
469f8829a4aSRandall Stewart 			 * yet another one.
470f8829a4aSRandall Stewart 			 */
471f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 1;
472f8829a4aSRandall Stewart 		}
473f8829a4aSRandall Stewart 		asoc->tsn_of_pdapi_last_delivered = chk->rec.data.TSN_seq;
474f8829a4aSRandall Stewart 		asoc->last_flags_delivered = chk->rec.data.rcv_flags;
475f8829a4aSRandall Stewart 		asoc->last_strm_seq_delivered = chk->rec.data.stream_seq;
476f8829a4aSRandall Stewart 		asoc->last_strm_no_delivered = chk->rec.data.stream_number;
477f8829a4aSRandall Stewart 
478f8829a4aSRandall Stewart 		asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
479f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue -= chk->send_size;
480f8829a4aSRandall Stewart 		sctp_ucount_decr(asoc->cnt_on_reasm_queue);
481f8829a4aSRandall Stewart 		/* free up the chk */
482f8829a4aSRandall Stewart 		chk->data = NULL;
483f8829a4aSRandall Stewart 		sctp_free_a_chunk(stcb, chk);
484f8829a4aSRandall Stewart 
485f8829a4aSRandall Stewart 		if (asoc->fragmented_delivery_inprogress == 0) {
486f8829a4aSRandall Stewart 			/*
487f8829a4aSRandall Stewart 			 * Now lets see if we can deliver the next one on
488f8829a4aSRandall Stewart 			 * the stream
489f8829a4aSRandall Stewart 			 */
490f8829a4aSRandall Stewart 			struct sctp_stream_in *strm;
491f8829a4aSRandall Stewart 
492f8829a4aSRandall Stewart 			strm = &asoc->strmin[stream_no];
493f8829a4aSRandall Stewart 			nxt_todel = strm->last_sequence_delivered + 1;
494f8829a4aSRandall Stewart 			ctl = TAILQ_FIRST(&strm->inqueue);
495f8829a4aSRandall Stewart 			if (ctl && (nxt_todel == ctl->sinfo_ssn)) {
496f8829a4aSRandall Stewart 				while (ctl != NULL) {
497f8829a4aSRandall Stewart 					/* Deliver more if we can. */
498f8829a4aSRandall Stewart 					if (nxt_todel == ctl->sinfo_ssn) {
499f8829a4aSRandall Stewart 						ctlat = TAILQ_NEXT(ctl, next);
500f8829a4aSRandall Stewart 						TAILQ_REMOVE(&strm->inqueue, ctl, next);
501f8829a4aSRandall Stewart 						asoc->size_on_all_streams -= ctl->length;
502f8829a4aSRandall Stewart 						sctp_ucount_decr(asoc->cnt_on_all_streams);
503f8829a4aSRandall Stewart 						strm->last_sequence_delivered++;
504f8829a4aSRandall Stewart 						sctp_add_to_readq(stcb->sctp_ep, stcb,
505f8829a4aSRandall Stewart 						    ctl,
506cfde3ff7SRandall Stewart 						    &stcb->sctp_socket->so_rcv, 1,
507cfde3ff7SRandall Stewart 						    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
50877acdc25SRandall Stewart 						sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
509f8829a4aSRandall Stewart 						ctl = ctlat;
510f8829a4aSRandall Stewart 					} else {
511f8829a4aSRandall Stewart 						break;
512f8829a4aSRandall Stewart 					}
513f8829a4aSRandall Stewart 					nxt_todel = strm->last_sequence_delivered + 1;
514f8829a4aSRandall Stewart 				}
515f8829a4aSRandall Stewart 			}
516139bc87fSRandall Stewart 			break;
517f8829a4aSRandall Stewart 		}
5183c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
519f8829a4aSRandall Stewart 		chk = TAILQ_FIRST(&asoc->reasmqueue);
520f8829a4aSRandall Stewart 	} while (chk);
521f8829a4aSRandall Stewart }
522f8829a4aSRandall Stewart 
523f8829a4aSRandall Stewart /*
524f8829a4aSRandall Stewart  * Queue the chunk either right into the socket buffer if it is the next one
525f8829a4aSRandall Stewart  * to go OR put it in the correct place in the delivery queue.  If we do
526f8829a4aSRandall Stewart  * append to the so_buf, keep doing so until we are out of order. One big
527f8829a4aSRandall Stewart  * question still remains, what to do when the socket buffer is FULL??
528f8829a4aSRandall Stewart  */
529f8829a4aSRandall Stewart static void
530f8829a4aSRandall Stewart sctp_queue_data_to_stream(struct sctp_tcb *stcb, struct sctp_association *asoc,
531f8829a4aSRandall Stewart     struct sctp_queued_to_read *control, int *abort_flag)
532f8829a4aSRandall Stewart {
533f8829a4aSRandall Stewart 	/*
534f8829a4aSRandall Stewart 	 * FIX-ME maybe? What happens when the ssn wraps? If we are getting
535f8829a4aSRandall Stewart 	 * all the data in one stream this could happen quite rapidly. One
536f8829a4aSRandall Stewart 	 * could use the TSN to keep track of things, but this scheme breaks
537f8829a4aSRandall Stewart 	 * down in the other type of stream useage that could occur. Send a
538f8829a4aSRandall Stewart 	 * single msg to stream 0, send 4Billion messages to stream 1, now
539f8829a4aSRandall Stewart 	 * send a message to stream 0. You have a situation where the TSN
540f8829a4aSRandall Stewart 	 * has wrapped but not in the stream. Is this worth worrying about
541f8829a4aSRandall Stewart 	 * or should we just change our queue sort at the bottom to be by
542f8829a4aSRandall Stewart 	 * TSN.
543f8829a4aSRandall Stewart 	 *
544f8829a4aSRandall Stewart 	 * Could it also be legal for a peer to send ssn 1 with TSN 2 and ssn 2
545f8829a4aSRandall Stewart 	 * with TSN 1? If the peer is doing some sort of funky TSN/SSN
546f8829a4aSRandall Stewart 	 * assignment this could happen... and I don't see how this would be
547f8829a4aSRandall Stewart 	 * a violation. So for now I am undecided an will leave the sort by
548f8829a4aSRandall Stewart 	 * SSN alone. Maybe a hybred approach is the answer
549f8829a4aSRandall Stewart 	 *
550f8829a4aSRandall Stewart 	 */
551f8829a4aSRandall Stewart 	struct sctp_stream_in *strm;
552f8829a4aSRandall Stewart 	struct sctp_queued_to_read *at;
553f8829a4aSRandall Stewart 	int queue_needed;
554f8829a4aSRandall Stewart 	uint16_t nxt_todel;
555f8829a4aSRandall Stewart 	struct mbuf *oper;
556f8829a4aSRandall Stewart 
557f8829a4aSRandall Stewart 	queue_needed = 1;
558f8829a4aSRandall Stewart 	asoc->size_on_all_streams += control->length;
559f8829a4aSRandall Stewart 	sctp_ucount_incr(asoc->cnt_on_all_streams);
560f8829a4aSRandall Stewart 	strm = &asoc->strmin[control->sinfo_stream];
561f8829a4aSRandall Stewart 	nxt_todel = strm->last_sequence_delivered + 1;
562b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
563f8829a4aSRandall Stewart 		sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INTO_STRD);
56480fefe0aSRandall Stewart 	}
565ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INDATA1,
566ad81507eSRandall Stewart 	    "queue to stream called for ssn:%u lastdel:%u nxt:%u\n",
567f8829a4aSRandall Stewart 	    (uint32_t) control->sinfo_stream,
568ad81507eSRandall Stewart 	    (uint32_t) strm->last_sequence_delivered,
569ad81507eSRandall Stewart 	    (uint32_t) nxt_todel);
570f8829a4aSRandall Stewart 	if (compare_with_wrap(strm->last_sequence_delivered,
571f8829a4aSRandall Stewart 	    control->sinfo_ssn, MAX_SEQ) ||
572f8829a4aSRandall Stewart 	    (strm->last_sequence_delivered == control->sinfo_ssn)) {
573f8829a4aSRandall Stewart 		/* The incoming sseq is behind where we last delivered? */
574ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1, "Duplicate S-SEQ:%d delivered:%d from peer, Abort  association\n",
575ad81507eSRandall Stewart 		    control->sinfo_ssn, strm->last_sequence_delivered);
576c40e9cf2SRandall Stewart protocol_error:
577f8829a4aSRandall Stewart 		/*
578f8829a4aSRandall Stewart 		 * throw it in the stream so it gets cleaned up in
579f8829a4aSRandall Stewart 		 * association destruction
580f8829a4aSRandall Stewart 		 */
581f8829a4aSRandall Stewart 		TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
582139bc87fSRandall Stewart 		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
583f8829a4aSRandall Stewart 		    0, M_DONTWAIT, 1, MT_DATA);
584f8829a4aSRandall Stewart 		if (oper) {
585f8829a4aSRandall Stewart 			struct sctp_paramhdr *ph;
586f8829a4aSRandall Stewart 			uint32_t *ippp;
587f8829a4aSRandall Stewart 
588139bc87fSRandall Stewart 			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
589f8829a4aSRandall Stewart 			    (sizeof(uint32_t) * 3);
590f8829a4aSRandall Stewart 			ph = mtod(oper, struct sctp_paramhdr *);
591f8829a4aSRandall Stewart 			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
592139bc87fSRandall Stewart 			ph->param_length = htons(SCTP_BUF_LEN(oper));
593f8829a4aSRandall Stewart 			ippp = (uint32_t *) (ph + 1);
594a5d547adSRandall Stewart 			*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_1);
595f8829a4aSRandall Stewart 			ippp++;
596f8829a4aSRandall Stewart 			*ippp = control->sinfo_tsn;
597f8829a4aSRandall Stewart 			ippp++;
598f8829a4aSRandall Stewart 			*ippp = ((control->sinfo_stream << 16) | control->sinfo_ssn);
599f8829a4aSRandall Stewart 		}
600a5d547adSRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_1;
601f8829a4aSRandall Stewart 		sctp_abort_an_association(stcb->sctp_ep, stcb,
602ceaad40aSRandall Stewart 		    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
603f8829a4aSRandall Stewart 
604f8829a4aSRandall Stewart 		*abort_flag = 1;
605f8829a4aSRandall Stewart 		return;
606f8829a4aSRandall Stewart 
607f8829a4aSRandall Stewart 	}
608f8829a4aSRandall Stewart 	if (nxt_todel == control->sinfo_ssn) {
609f8829a4aSRandall Stewart 		/* can be delivered right away? */
610b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
611f8829a4aSRandall Stewart 			sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_IMMED_DEL);
61280fefe0aSRandall Stewart 		}
613830d754dSRandall Stewart 		/* EY it wont be queued if it could be delivered directly */
614f8829a4aSRandall Stewart 		queue_needed = 0;
615f8829a4aSRandall Stewart 		asoc->size_on_all_streams -= control->length;
616f8829a4aSRandall Stewart 		sctp_ucount_decr(asoc->cnt_on_all_streams);
617f8829a4aSRandall Stewart 		strm->last_sequence_delivered++;
61877acdc25SRandall Stewart 
619f8829a4aSRandall Stewart 		sctp_add_to_readq(stcb->sctp_ep, stcb,
620f8829a4aSRandall Stewart 		    control,
621cfde3ff7SRandall Stewart 		    &stcb->sctp_socket->so_rcv, 1,
622cfde3ff7SRandall Stewart 		    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
62377acdc25SRandall Stewart 		sctp_mark_non_revokable(asoc, control->sinfo_tsn);
624f8829a4aSRandall Stewart 		control = TAILQ_FIRST(&strm->inqueue);
625f8829a4aSRandall Stewart 		while (control != NULL) {
626f8829a4aSRandall Stewart 			/* all delivered */
627f8829a4aSRandall Stewart 			nxt_todel = strm->last_sequence_delivered + 1;
628f8829a4aSRandall Stewart 			if (nxt_todel == control->sinfo_ssn) {
629f8829a4aSRandall Stewart 				at = TAILQ_NEXT(control, next);
630f8829a4aSRandall Stewart 				TAILQ_REMOVE(&strm->inqueue, control, next);
631f8829a4aSRandall Stewart 				asoc->size_on_all_streams -= control->length;
632f8829a4aSRandall Stewart 				sctp_ucount_decr(asoc->cnt_on_all_streams);
633f8829a4aSRandall Stewart 				strm->last_sequence_delivered++;
634f8829a4aSRandall Stewart 				/*
635f8829a4aSRandall Stewart 				 * We ignore the return of deliver_data here
636f8829a4aSRandall Stewart 				 * since we always can hold the chunk on the
637f8829a4aSRandall Stewart 				 * d-queue. And we have a finite number that
638f8829a4aSRandall Stewart 				 * can be delivered from the strq.
639f8829a4aSRandall Stewart 				 */
640b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
641f8829a4aSRandall Stewart 					sctp_log_strm_del(control, NULL,
642f8829a4aSRandall Stewart 					    SCTP_STR_LOG_FROM_IMMED_DEL);
64380fefe0aSRandall Stewart 				}
644830d754dSRandall Stewart 				/* EY will be used to calculate nr-gap */
645f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
646f8829a4aSRandall Stewart 				    control,
647cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1,
648cfde3ff7SRandall Stewart 				    SCTP_READ_LOCK_NOT_HELD,
649cfde3ff7SRandall Stewart 				    SCTP_SO_NOT_LOCKED);
65077acdc25SRandall Stewart 				sctp_mark_non_revokable(asoc, control->sinfo_tsn);
651f8829a4aSRandall Stewart 				control = at;
652f8829a4aSRandall Stewart 				continue;
653f8829a4aSRandall Stewart 			}
654f8829a4aSRandall Stewart 			break;
655f8829a4aSRandall Stewart 		}
656f8829a4aSRandall Stewart 	}
657f8829a4aSRandall Stewart 	if (queue_needed) {
658f8829a4aSRandall Stewart 		/*
659f8829a4aSRandall Stewart 		 * Ok, we did not deliver this guy, find the correct place
660f8829a4aSRandall Stewart 		 * to put it on the queue.
661f8829a4aSRandall Stewart 		 */
662c40e9cf2SRandall Stewart 		if ((compare_with_wrap(asoc->cumulative_tsn,
663c40e9cf2SRandall Stewart 		    control->sinfo_tsn, MAX_TSN)) ||
664c40e9cf2SRandall Stewart 		    (control->sinfo_tsn == asoc->cumulative_tsn)) {
665c40e9cf2SRandall Stewart 			goto protocol_error;
666c40e9cf2SRandall Stewart 		}
667f8829a4aSRandall Stewart 		if (TAILQ_EMPTY(&strm->inqueue)) {
668f8829a4aSRandall Stewart 			/* Empty queue */
669b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
670f8829a4aSRandall Stewart 				sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INSERT_HD);
67180fefe0aSRandall Stewart 			}
672f8829a4aSRandall Stewart 			TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
673f8829a4aSRandall Stewart 		} else {
674f8829a4aSRandall Stewart 			TAILQ_FOREACH(at, &strm->inqueue, next) {
675f8829a4aSRandall Stewart 				if (compare_with_wrap(at->sinfo_ssn,
676f8829a4aSRandall Stewart 				    control->sinfo_ssn, MAX_SEQ)) {
677f8829a4aSRandall Stewart 					/*
678f8829a4aSRandall Stewart 					 * one in queue is bigger than the
679f8829a4aSRandall Stewart 					 * new one, insert before this one
680f8829a4aSRandall Stewart 					 */
681b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
682f8829a4aSRandall Stewart 						sctp_log_strm_del(control, at,
683f8829a4aSRandall Stewart 						    SCTP_STR_LOG_FROM_INSERT_MD);
68480fefe0aSRandall Stewart 					}
685f8829a4aSRandall Stewart 					TAILQ_INSERT_BEFORE(at, control, next);
686f8829a4aSRandall Stewart 					break;
687f8829a4aSRandall Stewart 				} else if (at->sinfo_ssn == control->sinfo_ssn) {
688f8829a4aSRandall Stewart 					/*
689f8829a4aSRandall Stewart 					 * Gak, He sent me a duplicate str
690f8829a4aSRandall Stewart 					 * seq number
691f8829a4aSRandall Stewart 					 */
692f8829a4aSRandall Stewart 					/*
693f8829a4aSRandall Stewart 					 * foo bar, I guess I will just free
694f8829a4aSRandall Stewart 					 * this new guy, should we abort
695f8829a4aSRandall Stewart 					 * too? FIX ME MAYBE? Or it COULD be
696f8829a4aSRandall Stewart 					 * that the SSN's have wrapped.
697f8829a4aSRandall Stewart 					 * Maybe I should compare to TSN
698f8829a4aSRandall Stewart 					 * somehow... sigh for now just blow
699f8829a4aSRandall Stewart 					 * away the chunk!
700f8829a4aSRandall Stewart 					 */
701f8829a4aSRandall Stewart 
702f8829a4aSRandall Stewart 					if (control->data)
703f8829a4aSRandall Stewart 						sctp_m_freem(control->data);
704f8829a4aSRandall Stewart 					control->data = NULL;
705f8829a4aSRandall Stewart 					asoc->size_on_all_streams -= control->length;
706f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->cnt_on_all_streams);
7075bead436SRandall Stewart 					if (control->whoFrom)
708f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
7095bead436SRandall Stewart 					control->whoFrom = NULL;
710f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
711f8829a4aSRandall Stewart 					return;
712f8829a4aSRandall Stewart 				} else {
713f8829a4aSRandall Stewart 					if (TAILQ_NEXT(at, next) == NULL) {
714f8829a4aSRandall Stewart 						/*
715f8829a4aSRandall Stewart 						 * We are at the end, insert
716f8829a4aSRandall Stewart 						 * it after this one
717f8829a4aSRandall Stewart 						 */
718b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
719f8829a4aSRandall Stewart 							sctp_log_strm_del(control, at,
720f8829a4aSRandall Stewart 							    SCTP_STR_LOG_FROM_INSERT_TL);
72180fefe0aSRandall Stewart 						}
722f8829a4aSRandall Stewart 						TAILQ_INSERT_AFTER(&strm->inqueue,
723f8829a4aSRandall Stewart 						    at, control, next);
724f8829a4aSRandall Stewart 						break;
725f8829a4aSRandall Stewart 					}
726f8829a4aSRandall Stewart 				}
727f8829a4aSRandall Stewart 			}
728f8829a4aSRandall Stewart 		}
729f8829a4aSRandall Stewart 	}
730f8829a4aSRandall Stewart }
731f8829a4aSRandall Stewart 
732f8829a4aSRandall Stewart /*
733f8829a4aSRandall Stewart  * Returns two things: You get the total size of the deliverable parts of the
734f8829a4aSRandall Stewart  * first fragmented message on the reassembly queue. And you get a 1 back if
735f8829a4aSRandall Stewart  * all of the message is ready or a 0 back if the message is still incomplete
736f8829a4aSRandall Stewart  */
737f8829a4aSRandall Stewart static int
738f8829a4aSRandall Stewart sctp_is_all_msg_on_reasm(struct sctp_association *asoc, uint32_t * t_size)
739f8829a4aSRandall Stewart {
740f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
741f8829a4aSRandall Stewart 	uint32_t tsn;
742f8829a4aSRandall Stewart 
743f8829a4aSRandall Stewart 	*t_size = 0;
744f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
745f8829a4aSRandall Stewart 	if (chk == NULL) {
746f8829a4aSRandall Stewart 		/* nothing on the queue */
747f8829a4aSRandall Stewart 		return (0);
748f8829a4aSRandall Stewart 	}
749f8829a4aSRandall Stewart 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
750f8829a4aSRandall Stewart 		/* Not a first on the queue */
751f8829a4aSRandall Stewart 		return (0);
752f8829a4aSRandall Stewart 	}
753f8829a4aSRandall Stewart 	tsn = chk->rec.data.TSN_seq;
754f8829a4aSRandall Stewart 	while (chk) {
755f8829a4aSRandall Stewart 		if (tsn != chk->rec.data.TSN_seq) {
756f8829a4aSRandall Stewart 			return (0);
757f8829a4aSRandall Stewart 		}
758f8829a4aSRandall Stewart 		*t_size += chk->send_size;
759f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
760f8829a4aSRandall Stewart 			return (1);
761f8829a4aSRandall Stewart 		}
762f8829a4aSRandall Stewart 		tsn++;
763f8829a4aSRandall Stewart 		chk = TAILQ_NEXT(chk, sctp_next);
764f8829a4aSRandall Stewart 	}
765f8829a4aSRandall Stewart 	return (0);
766f8829a4aSRandall Stewart }
767f8829a4aSRandall Stewart 
768f8829a4aSRandall Stewart static void
769f8829a4aSRandall Stewart sctp_deliver_reasm_check(struct sctp_tcb *stcb, struct sctp_association *asoc)
770f8829a4aSRandall Stewart {
771f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
772f8829a4aSRandall Stewart 	uint16_t nxt_todel;
773810ec536SMichael Tuexen 	uint32_t tsize, pd_point;
774f8829a4aSRandall Stewart 
775139bc87fSRandall Stewart doit_again:
776f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
777f8829a4aSRandall Stewart 	if (chk == NULL) {
778f8829a4aSRandall Stewart 		/* Huh? */
779f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = 0;
780f8829a4aSRandall Stewart 		asoc->cnt_on_reasm_queue = 0;
781f8829a4aSRandall Stewart 		return;
782f8829a4aSRandall Stewart 	}
783f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress == 0) {
784f8829a4aSRandall Stewart 		nxt_todel =
785f8829a4aSRandall Stewart 		    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
786f8829a4aSRandall Stewart 		if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
787f8829a4aSRandall Stewart 		    (nxt_todel == chk->rec.data.stream_seq ||
788f8829a4aSRandall Stewart 		    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
789f8829a4aSRandall Stewart 			/*
790f8829a4aSRandall Stewart 			 * Yep the first one is here and its ok to deliver
791f8829a4aSRandall Stewart 			 * but should we?
792f8829a4aSRandall Stewart 			 */
793810ec536SMichael Tuexen 			if (stcb->sctp_socket) {
79424ae5c4aSMichael Tuexen 				pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket),
795810ec536SMichael Tuexen 				    stcb->sctp_ep->partial_delivery_point);
796810ec536SMichael Tuexen 			} else {
797810ec536SMichael Tuexen 				pd_point = stcb->sctp_ep->partial_delivery_point;
798810ec536SMichael Tuexen 			}
799810ec536SMichael Tuexen 			if (sctp_is_all_msg_on_reasm(asoc, &tsize) || (tsize >= pd_point)) {
800f8829a4aSRandall Stewart 
801f8829a4aSRandall Stewart 				/*
802f8829a4aSRandall Stewart 				 * Yes, we setup to start reception, by
803f8829a4aSRandall Stewart 				 * backing down the TSN just in case we
804f8829a4aSRandall Stewart 				 * can't deliver. If we
805f8829a4aSRandall Stewart 				 */
806f8829a4aSRandall Stewart 				asoc->fragmented_delivery_inprogress = 1;
807f8829a4aSRandall Stewart 				asoc->tsn_last_delivered =
808f8829a4aSRandall Stewart 				    chk->rec.data.TSN_seq - 1;
809f8829a4aSRandall Stewart 				asoc->str_of_pdapi =
810f8829a4aSRandall Stewart 				    chk->rec.data.stream_number;
811f8829a4aSRandall Stewart 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
812f8829a4aSRandall Stewart 				asoc->pdapi_ppid = chk->rec.data.payloadtype;
813f8829a4aSRandall Stewart 				asoc->fragment_flags = chk->rec.data.rcv_flags;
814f8829a4aSRandall Stewart 				sctp_service_reassembly(stcb, asoc);
815f8829a4aSRandall Stewart 			}
816f8829a4aSRandall Stewart 		}
817f8829a4aSRandall Stewart 	} else {
818139bc87fSRandall Stewart 		/*
819139bc87fSRandall Stewart 		 * Service re-assembly will deliver stream data queued at
820139bc87fSRandall Stewart 		 * the end of fragmented delivery.. but it wont know to go
821139bc87fSRandall Stewart 		 * back and call itself again... we do that here with the
822139bc87fSRandall Stewart 		 * got doit_again
823139bc87fSRandall Stewart 		 */
824f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
825139bc87fSRandall Stewart 		if (asoc->fragmented_delivery_inprogress == 0) {
826139bc87fSRandall Stewart 			/*
827139bc87fSRandall Stewart 			 * finished our Fragmented delivery, could be more
828139bc87fSRandall Stewart 			 * waiting?
829139bc87fSRandall Stewart 			 */
830139bc87fSRandall Stewart 			goto doit_again;
831139bc87fSRandall Stewart 		}
832f8829a4aSRandall Stewart 	}
833f8829a4aSRandall Stewart }
834f8829a4aSRandall Stewart 
835f8829a4aSRandall Stewart /*
836f8829a4aSRandall Stewart  * Dump onto the re-assembly queue, in its proper place. After dumping on the
837f8829a4aSRandall Stewart  * queue, see if anthing can be delivered. If so pull it off (or as much as
838f8829a4aSRandall Stewart  * we can. If we run out of space then we must dump what we can and set the
839f8829a4aSRandall Stewart  * appropriate flag to say we queued what we could.
840f8829a4aSRandall Stewart  */
841f8829a4aSRandall Stewart static void
842f8829a4aSRandall Stewart sctp_queue_data_for_reasm(struct sctp_tcb *stcb, struct sctp_association *asoc,
843f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk, int *abort_flag)
844f8829a4aSRandall Stewart {
845f8829a4aSRandall Stewart 	struct mbuf *oper;
846f8829a4aSRandall Stewart 	uint32_t cum_ackp1, last_tsn, prev_tsn, post_tsn;
847f8829a4aSRandall Stewart 	u_char last_flags;
848f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *at, *prev, *next;
849f8829a4aSRandall Stewart 
850f8829a4aSRandall Stewart 	prev = next = NULL;
851f8829a4aSRandall Stewart 	cum_ackp1 = asoc->tsn_last_delivered + 1;
852f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->reasmqueue)) {
853f8829a4aSRandall Stewart 		/* This is the first one on the queue */
854f8829a4aSRandall Stewart 		TAILQ_INSERT_HEAD(&asoc->reasmqueue, chk, sctp_next);
855f8829a4aSRandall Stewart 		/*
856f8829a4aSRandall Stewart 		 * we do not check for delivery of anything when only one
857f8829a4aSRandall Stewart 		 * fragment is here
858f8829a4aSRandall Stewart 		 */
859f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = chk->send_size;
860f8829a4aSRandall Stewart 		sctp_ucount_incr(asoc->cnt_on_reasm_queue);
861f8829a4aSRandall Stewart 		if (chk->rec.data.TSN_seq == cum_ackp1) {
862f8829a4aSRandall Stewart 			if (asoc->fragmented_delivery_inprogress == 0 &&
863f8829a4aSRandall Stewart 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) !=
864f8829a4aSRandall Stewart 			    SCTP_DATA_FIRST_FRAG) {
865f8829a4aSRandall Stewart 				/*
866f8829a4aSRandall Stewart 				 * An empty queue, no delivery inprogress,
867f8829a4aSRandall Stewart 				 * we hit the next one and it does NOT have
868f8829a4aSRandall Stewart 				 * a FIRST fragment mark.
869f8829a4aSRandall Stewart 				 */
870ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not first, no fragmented delivery in progress\n");
871139bc87fSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
872f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
873f8829a4aSRandall Stewart 
874f8829a4aSRandall Stewart 				if (oper) {
875f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
876f8829a4aSRandall Stewart 					uint32_t *ippp;
877f8829a4aSRandall Stewart 
878139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) =
879f8829a4aSRandall Stewart 					    sizeof(struct sctp_paramhdr) +
880f8829a4aSRandall Stewart 					    (sizeof(uint32_t) * 3);
881f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
882f8829a4aSRandall Stewart 					ph->param_type =
883f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
884139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
885f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
886a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_2);
887f8829a4aSRandall Stewart 					ippp++;
888f8829a4aSRandall Stewart 					*ippp = chk->rec.data.TSN_seq;
889f8829a4aSRandall Stewart 					ippp++;
890f8829a4aSRandall Stewart 					*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
891f8829a4aSRandall Stewart 
892f8829a4aSRandall Stewart 				}
893a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_2;
894f8829a4aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb,
895ceaad40aSRandall Stewart 				    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
896f8829a4aSRandall Stewart 				*abort_flag = 1;
897f8829a4aSRandall Stewart 			} else if (asoc->fragmented_delivery_inprogress &&
898f8829a4aSRandall Stewart 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
899f8829a4aSRandall Stewart 				/*
900f8829a4aSRandall Stewart 				 * We are doing a partial delivery and the
901f8829a4aSRandall Stewart 				 * NEXT chunk MUST be either the LAST or
902f8829a4aSRandall Stewart 				 * MIDDLE fragment NOT a FIRST
903f8829a4aSRandall Stewart 				 */
904ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS a first and fragmented delivery in progress\n");
905139bc87fSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
906f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
907f8829a4aSRandall Stewart 				if (oper) {
908f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
909f8829a4aSRandall Stewart 					uint32_t *ippp;
910f8829a4aSRandall Stewart 
911139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) =
912f8829a4aSRandall Stewart 					    sizeof(struct sctp_paramhdr) +
913f8829a4aSRandall Stewart 					    (3 * sizeof(uint32_t));
914f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
915f8829a4aSRandall Stewart 					ph->param_type =
916f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
917139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
918f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
919a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_3);
920f8829a4aSRandall Stewart 					ippp++;
921f8829a4aSRandall Stewart 					*ippp = chk->rec.data.TSN_seq;
922f8829a4aSRandall Stewart 					ippp++;
923f8829a4aSRandall Stewart 					*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
924f8829a4aSRandall Stewart 				}
925a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_3;
926f8829a4aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb,
927ceaad40aSRandall Stewart 				    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
928f8829a4aSRandall Stewart 				*abort_flag = 1;
929f8829a4aSRandall Stewart 			} else if (asoc->fragmented_delivery_inprogress) {
930f8829a4aSRandall Stewart 				/*
931f8829a4aSRandall Stewart 				 * Here we are ok with a MIDDLE or LAST
932f8829a4aSRandall Stewart 				 * piece
933f8829a4aSRandall Stewart 				 */
934f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
935f8829a4aSRandall Stewart 				    asoc->str_of_pdapi) {
936f8829a4aSRandall Stewart 					/* Got to be the right STR No */
937ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream number %d vs %d\n",
938f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
939f8829a4aSRandall Stewart 					    asoc->str_of_pdapi);
940139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
941f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
942f8829a4aSRandall Stewart 					if (oper) {
943f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
944f8829a4aSRandall Stewart 						uint32_t *ippp;
945f8829a4aSRandall Stewart 
946139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
947f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
948f8829a4aSRandall Stewart 						    (sizeof(uint32_t) * 3);
949f8829a4aSRandall Stewart 						ph = mtod(oper,
950f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
951f8829a4aSRandall Stewart 						ph->param_type =
952f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
953f8829a4aSRandall Stewart 						ph->param_length =
954139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
955f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
956a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_4);
957f8829a4aSRandall Stewart 						ippp++;
958f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
959f8829a4aSRandall Stewart 						ippp++;
960f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
961f8829a4aSRandall Stewart 					}
962a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_4;
963f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
964ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
965f8829a4aSRandall Stewart 					*abort_flag = 1;
966f8829a4aSRandall Stewart 				} else if ((asoc->fragment_flags & SCTP_DATA_UNORDERED) !=
967f8829a4aSRandall Stewart 					    SCTP_DATA_UNORDERED &&
968f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq !=
969f8829a4aSRandall Stewart 				    asoc->ssn_of_pdapi) {
970f8829a4aSRandall Stewart 					/* Got to be the right STR Seq */
971ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream seq %d vs %d\n",
972f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
973f8829a4aSRandall Stewart 					    asoc->ssn_of_pdapi);
974139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
975f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
976f8829a4aSRandall Stewart 					if (oper) {
977f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
978f8829a4aSRandall Stewart 						uint32_t *ippp;
979f8829a4aSRandall Stewart 
980139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
981f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
982f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
983f8829a4aSRandall Stewart 						ph = mtod(oper,
984f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
985f8829a4aSRandall Stewart 						ph->param_type =
986f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
987f8829a4aSRandall Stewart 						ph->param_length =
988139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
989f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
990a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_5);
991f8829a4aSRandall Stewart 						ippp++;
992f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
993f8829a4aSRandall Stewart 						ippp++;
994f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
995f8829a4aSRandall Stewart 
996f8829a4aSRandall Stewart 					}
997a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_5;
998f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
999ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1000f8829a4aSRandall Stewart 					*abort_flag = 1;
1001f8829a4aSRandall Stewart 				}
1002f8829a4aSRandall Stewart 			}
1003f8829a4aSRandall Stewart 		}
1004f8829a4aSRandall Stewart 		return;
1005f8829a4aSRandall Stewart 	}
1006f8829a4aSRandall Stewart 	/* Find its place */
1007f8829a4aSRandall Stewart 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
1008f8829a4aSRandall Stewart 		if (compare_with_wrap(at->rec.data.TSN_seq,
1009f8829a4aSRandall Stewart 		    chk->rec.data.TSN_seq, MAX_TSN)) {
1010f8829a4aSRandall Stewart 			/*
1011f8829a4aSRandall Stewart 			 * one in queue is bigger than the new one, insert
1012f8829a4aSRandall Stewart 			 * before this one
1013f8829a4aSRandall Stewart 			 */
1014f8829a4aSRandall Stewart 			/* A check */
1015f8829a4aSRandall Stewart 			asoc->size_on_reasm_queue += chk->send_size;
1016f8829a4aSRandall Stewart 			sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1017f8829a4aSRandall Stewart 			next = at;
1018f8829a4aSRandall Stewart 			TAILQ_INSERT_BEFORE(at, chk, sctp_next);
1019f8829a4aSRandall Stewart 			break;
1020f8829a4aSRandall Stewart 		} else if (at->rec.data.TSN_seq == chk->rec.data.TSN_seq) {
1021f8829a4aSRandall Stewart 			/* Gak, He sent me a duplicate str seq number */
1022f8829a4aSRandall Stewart 			/*
1023f8829a4aSRandall Stewart 			 * foo bar, I guess I will just free this new guy,
1024f8829a4aSRandall Stewart 			 * should we abort too? FIX ME MAYBE? Or it COULD be
1025f8829a4aSRandall Stewart 			 * that the SSN's have wrapped. Maybe I should
1026f8829a4aSRandall Stewart 			 * compare to TSN somehow... sigh for now just blow
1027f8829a4aSRandall Stewart 			 * away the chunk!
1028f8829a4aSRandall Stewart 			 */
1029f8829a4aSRandall Stewart 			if (chk->data) {
1030f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
1031f8829a4aSRandall Stewart 				chk->data = NULL;
1032f8829a4aSRandall Stewart 			}
1033f8829a4aSRandall Stewart 			sctp_free_a_chunk(stcb, chk);
1034f8829a4aSRandall Stewart 			return;
1035f8829a4aSRandall Stewart 		} else {
1036f8829a4aSRandall Stewart 			last_flags = at->rec.data.rcv_flags;
1037f8829a4aSRandall Stewart 			last_tsn = at->rec.data.TSN_seq;
1038f8829a4aSRandall Stewart 			prev = at;
1039f8829a4aSRandall Stewart 			if (TAILQ_NEXT(at, sctp_next) == NULL) {
1040f8829a4aSRandall Stewart 				/*
1041f8829a4aSRandall Stewart 				 * We are at the end, insert it after this
1042f8829a4aSRandall Stewart 				 * one
1043f8829a4aSRandall Stewart 				 */
1044f8829a4aSRandall Stewart 				/* check it first */
1045f8829a4aSRandall Stewart 				asoc->size_on_reasm_queue += chk->send_size;
1046f8829a4aSRandall Stewart 				sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1047f8829a4aSRandall Stewart 				TAILQ_INSERT_AFTER(&asoc->reasmqueue, at, chk, sctp_next);
1048f8829a4aSRandall Stewart 				break;
1049f8829a4aSRandall Stewart 			}
1050f8829a4aSRandall Stewart 		}
1051f8829a4aSRandall Stewart 	}
1052f8829a4aSRandall Stewart 	/* Now the audits */
1053f8829a4aSRandall Stewart 	if (prev) {
1054f8829a4aSRandall Stewart 		prev_tsn = chk->rec.data.TSN_seq - 1;
1055f8829a4aSRandall Stewart 		if (prev_tsn == prev->rec.data.TSN_seq) {
1056f8829a4aSRandall Stewart 			/*
1057f8829a4aSRandall Stewart 			 * Ok the one I am dropping onto the end is the
1058f8829a4aSRandall Stewart 			 * NEXT. A bit of valdiation here.
1059f8829a4aSRandall Stewart 			 */
1060f8829a4aSRandall Stewart 			if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1061f8829a4aSRandall Stewart 			    SCTP_DATA_FIRST_FRAG ||
1062f8829a4aSRandall Stewart 			    (prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1063f8829a4aSRandall Stewart 			    SCTP_DATA_MIDDLE_FRAG) {
1064f8829a4aSRandall Stewart 				/*
1065f8829a4aSRandall Stewart 				 * Insert chk MUST be a MIDDLE or LAST
1066f8829a4aSRandall Stewart 				 * fragment
1067f8829a4aSRandall Stewart 				 */
1068f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1069f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1070ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - It can be a midlle or last but not a first\n");
1071ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it's a FIRST!\n");
1072139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1073f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1074f8829a4aSRandall Stewart 					if (oper) {
1075f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1076f8829a4aSRandall Stewart 						uint32_t *ippp;
1077f8829a4aSRandall Stewart 
1078139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1079f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1080f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1081f8829a4aSRandall Stewart 						ph = mtod(oper,
1082f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1083f8829a4aSRandall Stewart 						ph->param_type =
1084f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1085f8829a4aSRandall Stewart 						ph->param_length =
1086139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1087f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1088a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_6);
1089f8829a4aSRandall Stewart 						ippp++;
1090f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1091f8829a4aSRandall Stewart 						ippp++;
1092f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1093f8829a4aSRandall Stewart 
1094f8829a4aSRandall Stewart 					}
1095a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_6;
1096f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1097ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1098f8829a4aSRandall Stewart 					*abort_flag = 1;
1099f8829a4aSRandall Stewart 					return;
1100f8829a4aSRandall Stewart 				}
1101f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
1102f8829a4aSRandall Stewart 				    prev->rec.data.stream_number) {
1103f8829a4aSRandall Stewart 					/*
1104f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1105f8829a4aSRandall Stewart 					 * they must be the same.
1106f8829a4aSRandall Stewart 					 */
1107ad81507eSRandall Stewart 					SCTP_PRINTF("Prev check - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1108f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
1109f8829a4aSRandall Stewart 					    prev->rec.data.stream_number);
1110139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1111f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1112f8829a4aSRandall Stewart 					if (oper) {
1113f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1114f8829a4aSRandall Stewart 						uint32_t *ippp;
1115f8829a4aSRandall Stewart 
1116139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1117f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1118f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1119f8829a4aSRandall Stewart 						ph = mtod(oper,
1120f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1121f8829a4aSRandall Stewart 						ph->param_type =
1122f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1123f8829a4aSRandall Stewart 						ph->param_length =
1124139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1125f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1126a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_7);
1127f8829a4aSRandall Stewart 						ippp++;
1128f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1129f8829a4aSRandall Stewart 						ippp++;
1130f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1131f8829a4aSRandall Stewart 					}
1132a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_7;
1133f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1134ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1135f8829a4aSRandall Stewart 
1136f8829a4aSRandall Stewart 					*abort_flag = 1;
1137f8829a4aSRandall Stewart 					return;
1138f8829a4aSRandall Stewart 				}
1139f8829a4aSRandall Stewart 				if ((prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1140f8829a4aSRandall Stewart 				    chk->rec.data.stream_seq !=
1141f8829a4aSRandall Stewart 				    prev->rec.data.stream_seq) {
1142f8829a4aSRandall Stewart 					/*
1143f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1144f8829a4aSRandall Stewart 					 * they must be the same.
1145f8829a4aSRandall Stewart 					 */
1146ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1147f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
1148f8829a4aSRandall Stewart 					    prev->rec.data.stream_seq);
1149139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1150f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1151f8829a4aSRandall Stewart 					if (oper) {
1152f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1153f8829a4aSRandall Stewart 						uint32_t *ippp;
1154f8829a4aSRandall Stewart 
1155139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1156f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1157f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1158f8829a4aSRandall Stewart 						ph = mtod(oper,
1159f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1160f8829a4aSRandall Stewart 						ph->param_type =
1161f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1162f8829a4aSRandall Stewart 						ph->param_length =
1163139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1164f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1165a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_8);
1166f8829a4aSRandall Stewart 						ippp++;
1167f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1168f8829a4aSRandall Stewart 						ippp++;
1169f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1170f8829a4aSRandall Stewart 					}
1171a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_8;
1172f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1173ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1174f8829a4aSRandall Stewart 
1175f8829a4aSRandall Stewart 					*abort_flag = 1;
1176f8829a4aSRandall Stewart 					return;
1177f8829a4aSRandall Stewart 				}
1178f8829a4aSRandall Stewart 			} else if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1179f8829a4aSRandall Stewart 			    SCTP_DATA_LAST_FRAG) {
1180f8829a4aSRandall Stewart 				/* Insert chk MUST be a FIRST */
1181f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1182f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1183ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, evil plot, its not FIRST and it must be!\n");
1184139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1185f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1186f8829a4aSRandall Stewart 					if (oper) {
1187f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1188f8829a4aSRandall Stewart 						uint32_t *ippp;
1189f8829a4aSRandall Stewart 
1190139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1191f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1192f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1193f8829a4aSRandall Stewart 						ph = mtod(oper,
1194f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1195f8829a4aSRandall Stewart 						ph->param_type =
1196f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1197f8829a4aSRandall Stewart 						ph->param_length =
1198139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1199f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1200a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_9);
1201f8829a4aSRandall Stewart 						ippp++;
1202f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1203f8829a4aSRandall Stewart 						ippp++;
1204f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1205f8829a4aSRandall Stewart 
1206f8829a4aSRandall Stewart 					}
1207a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_9;
1208f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1209ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1210f8829a4aSRandall Stewart 
1211f8829a4aSRandall Stewart 					*abort_flag = 1;
1212f8829a4aSRandall Stewart 					return;
1213f8829a4aSRandall Stewart 				}
1214f8829a4aSRandall Stewart 			}
1215f8829a4aSRandall Stewart 		}
1216f8829a4aSRandall Stewart 	}
1217f8829a4aSRandall Stewart 	if (next) {
1218f8829a4aSRandall Stewart 		post_tsn = chk->rec.data.TSN_seq + 1;
1219f8829a4aSRandall Stewart 		if (post_tsn == next->rec.data.TSN_seq) {
1220f8829a4aSRandall Stewart 			/*
1221f8829a4aSRandall Stewart 			 * Ok the one I am inserting ahead of is my NEXT
1222f8829a4aSRandall Stewart 			 * one. A bit of valdiation here.
1223f8829a4aSRandall Stewart 			 */
1224f8829a4aSRandall Stewart 			if (next->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
1225f8829a4aSRandall Stewart 				/* Insert chk MUST be a last fragment */
1226f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK)
1227f8829a4aSRandall Stewart 				    != SCTP_DATA_LAST_FRAG) {
1228ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is FIRST, we must be LAST\n");
1229ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not a last!\n");
1230139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1231f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1232f8829a4aSRandall Stewart 					if (oper) {
1233f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1234f8829a4aSRandall Stewart 						uint32_t *ippp;
1235f8829a4aSRandall Stewart 
1236139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1237f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1238f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1239f8829a4aSRandall Stewart 						ph = mtod(oper,
1240f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1241f8829a4aSRandall Stewart 						ph->param_type =
1242f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1243f8829a4aSRandall Stewart 						ph->param_length =
1244139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1245f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1246a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_10);
1247f8829a4aSRandall Stewart 						ippp++;
1248f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1249f8829a4aSRandall Stewart 						ippp++;
1250f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1251f8829a4aSRandall Stewart 					}
1252a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_10;
1253f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1254ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1255f8829a4aSRandall Stewart 
1256f8829a4aSRandall Stewart 					*abort_flag = 1;
1257f8829a4aSRandall Stewart 					return;
1258f8829a4aSRandall Stewart 				}
1259f8829a4aSRandall Stewart 			} else if ((next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1260f8829a4aSRandall Stewart 				    SCTP_DATA_MIDDLE_FRAG ||
1261f8829a4aSRandall Stewart 				    (next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1262f8829a4aSRandall Stewart 			    SCTP_DATA_LAST_FRAG) {
1263f8829a4aSRandall Stewart 				/*
1264f8829a4aSRandall Stewart 				 * Insert chk CAN be MIDDLE or FIRST NOT
1265f8829a4aSRandall Stewart 				 * LAST
1266f8829a4aSRandall Stewart 				 */
1267f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1268f8829a4aSRandall Stewart 				    SCTP_DATA_LAST_FRAG) {
1269ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is a MIDDLE/LAST\n");
1270ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, new prev chunk is a LAST\n");
1271139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1272f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1273f8829a4aSRandall Stewart 					if (oper) {
1274f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1275f8829a4aSRandall Stewart 						uint32_t *ippp;
1276f8829a4aSRandall Stewart 
1277139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1278f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1279f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1280f8829a4aSRandall Stewart 						ph = mtod(oper,
1281f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1282f8829a4aSRandall Stewart 						ph->param_type =
1283f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1284f8829a4aSRandall Stewart 						ph->param_length =
1285139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1286f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1287a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_11);
1288f8829a4aSRandall Stewart 						ippp++;
1289f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1290f8829a4aSRandall Stewart 						ippp++;
1291f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1292f8829a4aSRandall Stewart 
1293f8829a4aSRandall Stewart 					}
1294a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_11;
1295f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1296ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1297f8829a4aSRandall Stewart 
1298f8829a4aSRandall Stewart 					*abort_flag = 1;
1299f8829a4aSRandall Stewart 					return;
1300f8829a4aSRandall Stewart 				}
1301f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
1302f8829a4aSRandall Stewart 				    next->rec.data.stream_number) {
1303f8829a4aSRandall Stewart 					/*
1304f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1305f8829a4aSRandall Stewart 					 * they must be the same.
1306f8829a4aSRandall Stewart 					 */
1307ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1308f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
1309f8829a4aSRandall Stewart 					    next->rec.data.stream_number);
1310139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1311f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1312f8829a4aSRandall Stewart 					if (oper) {
1313f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1314f8829a4aSRandall Stewart 						uint32_t *ippp;
1315f8829a4aSRandall Stewart 
1316139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1317f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1318f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1319f8829a4aSRandall Stewart 						ph = mtod(oper,
1320f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1321f8829a4aSRandall Stewart 						ph->param_type =
1322f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1323f8829a4aSRandall Stewart 						ph->param_length =
1324139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1325f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1326a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_12);
1327f8829a4aSRandall Stewart 						ippp++;
1328f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1329f8829a4aSRandall Stewart 						ippp++;
1330f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1331f8829a4aSRandall Stewart 
1332f8829a4aSRandall Stewart 					}
1333a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_12;
1334f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1335ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1336f8829a4aSRandall Stewart 
1337f8829a4aSRandall Stewart 					*abort_flag = 1;
1338f8829a4aSRandall Stewart 					return;
1339f8829a4aSRandall Stewart 				}
1340f8829a4aSRandall Stewart 				if ((next->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1341f8829a4aSRandall Stewart 				    chk->rec.data.stream_seq !=
1342f8829a4aSRandall Stewart 				    next->rec.data.stream_seq) {
1343f8829a4aSRandall Stewart 					/*
1344f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1345f8829a4aSRandall Stewart 					 * they must be the same.
1346f8829a4aSRandall Stewart 					 */
1347ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1348f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
1349f8829a4aSRandall Stewart 					    next->rec.data.stream_seq);
1350139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1351f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1352f8829a4aSRandall Stewart 					if (oper) {
1353f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1354f8829a4aSRandall Stewart 						uint32_t *ippp;
1355f8829a4aSRandall Stewart 
1356139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1357f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1358f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1359f8829a4aSRandall Stewart 						ph = mtod(oper,
1360f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1361f8829a4aSRandall Stewart 						ph->param_type =
1362f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1363f8829a4aSRandall Stewart 						ph->param_length =
1364139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1365f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1366a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_13);
1367f8829a4aSRandall Stewart 						ippp++;
1368f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1369f8829a4aSRandall Stewart 						ippp++;
1370f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1371f8829a4aSRandall Stewart 					}
1372a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_13;
1373f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1374ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1375f8829a4aSRandall Stewart 
1376f8829a4aSRandall Stewart 					*abort_flag = 1;
1377f8829a4aSRandall Stewart 					return;
1378f8829a4aSRandall Stewart 				}
1379f8829a4aSRandall Stewart 			}
1380f8829a4aSRandall Stewart 		}
1381f8829a4aSRandall Stewart 	}
1382f8829a4aSRandall Stewart 	/* Do we need to do some delivery? check */
1383f8829a4aSRandall Stewart 	sctp_deliver_reasm_check(stcb, asoc);
1384f8829a4aSRandall Stewart }
1385f8829a4aSRandall Stewart 
1386f8829a4aSRandall Stewart /*
1387f8829a4aSRandall Stewart  * This is an unfortunate routine. It checks to make sure a evil guy is not
1388f8829a4aSRandall Stewart  * stuffing us full of bad packet fragments. A broken peer could also do this
1389f8829a4aSRandall Stewart  * but this is doubtful. It is to bad I must worry about evil crackers sigh
1390f8829a4aSRandall Stewart  * :< more cycles.
1391f8829a4aSRandall Stewart  */
1392f8829a4aSRandall Stewart static int
1393f8829a4aSRandall Stewart sctp_does_tsn_belong_to_reasm(struct sctp_association *asoc,
1394f8829a4aSRandall Stewart     uint32_t TSN_seq)
1395f8829a4aSRandall Stewart {
1396f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *at;
1397f8829a4aSRandall Stewart 	uint32_t tsn_est;
1398f8829a4aSRandall Stewart 
1399f8829a4aSRandall Stewart 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
1400f8829a4aSRandall Stewart 		if (compare_with_wrap(TSN_seq,
1401f8829a4aSRandall Stewart 		    at->rec.data.TSN_seq, MAX_TSN)) {
1402f8829a4aSRandall Stewart 			/* is it one bigger? */
1403f8829a4aSRandall Stewart 			tsn_est = at->rec.data.TSN_seq + 1;
1404f8829a4aSRandall Stewart 			if (tsn_est == TSN_seq) {
1405f8829a4aSRandall Stewart 				/* yep. It better be a last then */
1406f8829a4aSRandall Stewart 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1407f8829a4aSRandall Stewart 				    SCTP_DATA_LAST_FRAG) {
1408f8829a4aSRandall Stewart 					/*
1409f8829a4aSRandall Stewart 					 * Ok this guy belongs next to a guy
1410f8829a4aSRandall Stewart 					 * that is NOT last, it should be a
1411f8829a4aSRandall Stewart 					 * middle/last, not a complete
1412f8829a4aSRandall Stewart 					 * chunk.
1413f8829a4aSRandall Stewart 					 */
1414f8829a4aSRandall Stewart 					return (1);
1415f8829a4aSRandall Stewart 				} else {
1416f8829a4aSRandall Stewart 					/*
1417f8829a4aSRandall Stewart 					 * This guy is ok since its a LAST
1418f8829a4aSRandall Stewart 					 * and the new chunk is a fully
1419f8829a4aSRandall Stewart 					 * self- contained one.
1420f8829a4aSRandall Stewart 					 */
1421f8829a4aSRandall Stewart 					return (0);
1422f8829a4aSRandall Stewart 				}
1423f8829a4aSRandall Stewart 			}
1424f8829a4aSRandall Stewart 		} else if (TSN_seq == at->rec.data.TSN_seq) {
1425f8829a4aSRandall Stewart 			/* Software error since I have a dup? */
1426f8829a4aSRandall Stewart 			return (1);
1427f8829a4aSRandall Stewart 		} else {
1428f8829a4aSRandall Stewart 			/*
1429f8829a4aSRandall Stewart 			 * Ok, 'at' is larger than new chunk but does it
1430f8829a4aSRandall Stewart 			 * need to be right before it.
1431f8829a4aSRandall Stewart 			 */
1432f8829a4aSRandall Stewart 			tsn_est = TSN_seq + 1;
1433f8829a4aSRandall Stewart 			if (tsn_est == at->rec.data.TSN_seq) {
1434f8829a4aSRandall Stewart 				/* Yep, It better be a first */
1435f8829a4aSRandall Stewart 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1436f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1437f8829a4aSRandall Stewart 					return (1);
1438f8829a4aSRandall Stewart 				} else {
1439f8829a4aSRandall Stewart 					return (0);
1440f8829a4aSRandall Stewart 				}
1441f8829a4aSRandall Stewart 			}
1442f8829a4aSRandall Stewart 		}
1443f8829a4aSRandall Stewart 	}
1444f8829a4aSRandall Stewart 	return (0);
1445f8829a4aSRandall Stewart }
1446f8829a4aSRandall Stewart 
1447f8829a4aSRandall Stewart 
1448f8829a4aSRandall Stewart static int
1449f8829a4aSRandall Stewart sctp_process_a_data_chunk(struct sctp_tcb *stcb, struct sctp_association *asoc,
1450f8829a4aSRandall Stewart     struct mbuf **m, int offset, struct sctp_data_chunk *ch, int chk_length,
1451f8829a4aSRandall Stewart     struct sctp_nets *net, uint32_t * high_tsn, int *abort_flag,
1452f8829a4aSRandall Stewart     int *break_flag, int last_chunk)
1453f8829a4aSRandall Stewart {
1454f8829a4aSRandall Stewart 	/* Process a data chunk */
1455f8829a4aSRandall Stewart 	/* struct sctp_tmit_chunk *chk; */
1456f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
1457f8829a4aSRandall Stewart 	uint32_t tsn, gap;
1458f8829a4aSRandall Stewart 	struct mbuf *dmbuf;
1459f8829a4aSRandall Stewart 	int indx, the_len;
1460139bc87fSRandall Stewart 	int need_reasm_check = 0;
1461f8829a4aSRandall Stewart 	uint16_t strmno, strmseq;
1462f8829a4aSRandall Stewart 	struct mbuf *oper;
1463f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
1464f42a358aSRandall Stewart 	int ordered;
1465f42a358aSRandall Stewart 	uint32_t protocol_id;
1466f42a358aSRandall Stewart 	uint8_t chunk_flags;
146717205eccSRandall Stewart 	struct sctp_stream_reset_list *liste;
1468f8829a4aSRandall Stewart 
1469f8829a4aSRandall Stewart 	chk = NULL;
1470f8829a4aSRandall Stewart 	tsn = ntohl(ch->dp.tsn);
1471f42a358aSRandall Stewart 	chunk_flags = ch->ch.chunk_flags;
1472b3f1ea41SRandall Stewart 	if ((chunk_flags & SCTP_DATA_SACK_IMMEDIATELY) == SCTP_DATA_SACK_IMMEDIATELY) {
1473b3f1ea41SRandall Stewart 		asoc->send_sack = 1;
1474b3f1ea41SRandall Stewart 	}
1475f42a358aSRandall Stewart 	protocol_id = ch->dp.protocol_id;
1476f42a358aSRandall Stewart 	ordered = ((ch->ch.chunk_flags & SCTP_DATA_UNORDERED) == 0);
1477b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1478c4739e2fSRandall Stewart 		sctp_log_map(tsn, asoc->cumulative_tsn, asoc->highest_tsn_inside_map, SCTP_MAP_TSN_ENTERS);
147980fefe0aSRandall Stewart 	}
1480ad81507eSRandall Stewart 	if (stcb == NULL) {
1481ad81507eSRandall Stewart 		return (0);
1482ad81507eSRandall Stewart 	}
148380fefe0aSRandall Stewart 	SCTP_LTRACE_CHK(stcb->sctp_ep, stcb, ch->ch.chunk_type, tsn);
1484f8829a4aSRandall Stewart 	if (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN) ||
1485f8829a4aSRandall Stewart 	    asoc->cumulative_tsn == tsn) {
1486f8829a4aSRandall Stewart 		/* It is a duplicate */
1487f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvdupdata);
1488f8829a4aSRandall Stewart 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1489f8829a4aSRandall Stewart 			/* Record a dup for the next outbound sack */
1490f8829a4aSRandall Stewart 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1491f8829a4aSRandall Stewart 			asoc->numduptsns++;
1492f8829a4aSRandall Stewart 		}
1493b201f536SRandall Stewart 		asoc->send_sack = 1;
1494f8829a4aSRandall Stewart 		return (0);
1495f8829a4aSRandall Stewart 	}
1496f8829a4aSRandall Stewart 	/* Calculate the number of TSN's between the base and this TSN */
1497d50c1d79SRandall Stewart 	SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
1498f8829a4aSRandall Stewart 	if (gap >= (SCTP_MAPPING_ARRAY << 3)) {
1499f8829a4aSRandall Stewart 		/* Can't hold the bit in the mapping at max array, toss it */
1500f8829a4aSRandall Stewart 		return (0);
1501f8829a4aSRandall Stewart 	}
1502f8829a4aSRandall Stewart 	if (gap >= (uint32_t) (asoc->mapping_array_size << 3)) {
1503207304d4SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
15040696e120SRandall Stewart 		if (sctp_expand_mapping_array(asoc, gap)) {
1505f8829a4aSRandall Stewart 			/* Can't expand, drop it */
1506f8829a4aSRandall Stewart 			return (0);
1507f8829a4aSRandall Stewart 		}
1508f8829a4aSRandall Stewart 	}
1509f8829a4aSRandall Stewart 	if (compare_with_wrap(tsn, *high_tsn, MAX_TSN)) {
1510f8829a4aSRandall Stewart 		*high_tsn = tsn;
1511f8829a4aSRandall Stewart 	}
1512f8829a4aSRandall Stewart 	/* See if we have received this one already */
151377acdc25SRandall Stewart 	if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap) ||
151477acdc25SRandall Stewart 	    SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, gap)) {
1515f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvdupdata);
1516f8829a4aSRandall Stewart 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1517f8829a4aSRandall Stewart 			/* Record a dup for the next outbound sack */
1518f8829a4aSRandall Stewart 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1519f8829a4aSRandall Stewart 			asoc->numduptsns++;
1520f8829a4aSRandall Stewart 		}
152142551e99SRandall Stewart 		asoc->send_sack = 1;
1522f8829a4aSRandall Stewart 		return (0);
1523f8829a4aSRandall Stewart 	}
1524f8829a4aSRandall Stewart 	/*
1525f8829a4aSRandall Stewart 	 * Check to see about the GONE flag, duplicates would cause a sack
1526f8829a4aSRandall Stewart 	 * to be sent up above
1527f8829a4aSRandall Stewart 	 */
1528ad81507eSRandall Stewart 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1529f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1530f8829a4aSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET))
1531f8829a4aSRandall Stewart 	    ) {
1532f8829a4aSRandall Stewart 		/*
1533f8829a4aSRandall Stewart 		 * wait a minute, this guy is gone, there is no longer a
1534f8829a4aSRandall Stewart 		 * receiver. Send peer an ABORT!
1535f8829a4aSRandall Stewart 		 */
1536f8829a4aSRandall Stewart 		struct mbuf *op_err;
1537f8829a4aSRandall Stewart 
1538f8829a4aSRandall Stewart 		op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1539ceaad40aSRandall Stewart 		sctp_abort_an_association(stcb->sctp_ep, stcb, 0, op_err, SCTP_SO_NOT_LOCKED);
1540f8829a4aSRandall Stewart 		*abort_flag = 1;
1541f8829a4aSRandall Stewart 		return (0);
1542f8829a4aSRandall Stewart 	}
1543f8829a4aSRandall Stewart 	/*
1544f8829a4aSRandall Stewart 	 * Now before going further we see if there is room. If NOT then we
1545f8829a4aSRandall Stewart 	 * MAY let one through only IF this TSN is the one we are waiting
1546f8829a4aSRandall Stewart 	 * for on a partial delivery API.
1547f8829a4aSRandall Stewart 	 */
1548f8829a4aSRandall Stewart 
1549f8829a4aSRandall Stewart 	/* now do the tests */
1550f8829a4aSRandall Stewart 	if (((asoc->cnt_on_all_streams +
1551f8829a4aSRandall Stewart 	    asoc->cnt_on_reasm_queue +
1552b3f1ea41SRandall Stewart 	    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) ||
1553f8829a4aSRandall Stewart 	    (((int)asoc->my_rwnd) <= 0)) {
1554f8829a4aSRandall Stewart 		/*
1555f8829a4aSRandall Stewart 		 * When we have NO room in the rwnd we check to make sure
1556f8829a4aSRandall Stewart 		 * the reader is doing its job...
1557f8829a4aSRandall Stewart 		 */
1558f8829a4aSRandall Stewart 		if (stcb->sctp_socket->so_rcv.sb_cc) {
1559f8829a4aSRandall Stewart 			/* some to read, wake-up */
1560ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1561ceaad40aSRandall Stewart 			struct socket *so;
1562ceaad40aSRandall Stewart 
1563ceaad40aSRandall Stewart 			so = SCTP_INP_SO(stcb->sctp_ep);
1564ceaad40aSRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, 1);
1565ceaad40aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
1566ceaad40aSRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
1567ceaad40aSRandall Stewart 			SCTP_TCB_LOCK(stcb);
1568ceaad40aSRandall Stewart 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
1569ceaad40aSRandall Stewart 			if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1570ceaad40aSRandall Stewart 				/* assoc was freed while we were unlocked */
1571ceaad40aSRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
1572ceaad40aSRandall Stewart 				return (0);
1573ceaad40aSRandall Stewart 			}
1574ceaad40aSRandall Stewart #endif
1575f8829a4aSRandall Stewart 			sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1576ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1577ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
1578ceaad40aSRandall Stewart #endif
1579f8829a4aSRandall Stewart 		}
1580f8829a4aSRandall Stewart 		/* now is it in the mapping array of what we have accepted? */
158177acdc25SRandall Stewart 		if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN) &&
158277acdc25SRandall Stewart 		    compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN)) {
1583f8829a4aSRandall Stewart 			/* Nope not in the valid range dump it */
1584f8829a4aSRandall Stewart 			sctp_set_rwnd(stcb, asoc);
1585f8829a4aSRandall Stewart 			if ((asoc->cnt_on_all_streams +
1586f8829a4aSRandall Stewart 			    asoc->cnt_on_reasm_queue +
1587b3f1ea41SRandall Stewart 			    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) {
1588f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_datadropchklmt);
1589f8829a4aSRandall Stewart 			} else {
1590f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_datadroprwnd);
1591f8829a4aSRandall Stewart 			}
1592f8829a4aSRandall Stewart 			indx = *break_flag;
1593f8829a4aSRandall Stewart 			*break_flag = 1;
1594f8829a4aSRandall Stewart 			return (0);
1595f8829a4aSRandall Stewart 		}
1596f8829a4aSRandall Stewart 	}
1597f8829a4aSRandall Stewart 	strmno = ntohs(ch->dp.stream_id);
1598f8829a4aSRandall Stewart 	if (strmno >= asoc->streamincnt) {
1599f8829a4aSRandall Stewart 		struct sctp_paramhdr *phdr;
1600f8829a4aSRandall Stewart 		struct mbuf *mb;
1601f8829a4aSRandall Stewart 
1602f8829a4aSRandall Stewart 		mb = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) * 2),
1603139bc87fSRandall Stewart 		    0, M_DONTWAIT, 1, MT_DATA);
1604f8829a4aSRandall Stewart 		if (mb != NULL) {
1605f8829a4aSRandall Stewart 			/* add some space up front so prepend will work well */
1606139bc87fSRandall Stewart 			SCTP_BUF_RESV_UF(mb, sizeof(struct sctp_chunkhdr));
1607f8829a4aSRandall Stewart 			phdr = mtod(mb, struct sctp_paramhdr *);
1608f8829a4aSRandall Stewart 			/*
1609f8829a4aSRandall Stewart 			 * Error causes are just param's and this one has
1610f8829a4aSRandall Stewart 			 * two back to back phdr, one with the error type
1611f8829a4aSRandall Stewart 			 * and size, the other with the streamid and a rsvd
1612f8829a4aSRandall Stewart 			 */
1613139bc87fSRandall Stewart 			SCTP_BUF_LEN(mb) = (sizeof(struct sctp_paramhdr) * 2);
1614f8829a4aSRandall Stewart 			phdr->param_type = htons(SCTP_CAUSE_INVALID_STREAM);
1615f8829a4aSRandall Stewart 			phdr->param_length =
1616f8829a4aSRandall Stewart 			    htons(sizeof(struct sctp_paramhdr) * 2);
1617f8829a4aSRandall Stewart 			phdr++;
1618f8829a4aSRandall Stewart 			/* We insert the stream in the type field */
1619f8829a4aSRandall Stewart 			phdr->param_type = ch->dp.stream_id;
1620f8829a4aSRandall Stewart 			/* And set the length to 0 for the rsvd field */
1621f8829a4aSRandall Stewart 			phdr->param_length = 0;
1622f8829a4aSRandall Stewart 			sctp_queue_op_err(stcb, mb);
1623f8829a4aSRandall Stewart 		}
1624f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_badsid);
1625207304d4SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
162677acdc25SRandall Stewart 
1627830d754dSRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
162877acdc25SRandall Stewart 		if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN)) {
1629830d754dSRandall Stewart 			asoc->highest_tsn_inside_nr_map = tsn;
1630d06c82f1SRandall Stewart 		}
1631d06c82f1SRandall Stewart 		if (tsn == (asoc->cumulative_tsn + 1)) {
1632d06c82f1SRandall Stewart 			/* Update cum-ack */
1633d06c82f1SRandall Stewart 			asoc->cumulative_tsn = tsn;
1634d06c82f1SRandall Stewart 		}
1635f8829a4aSRandall Stewart 		return (0);
1636f8829a4aSRandall Stewart 	}
1637f8829a4aSRandall Stewart 	/*
1638f8829a4aSRandall Stewart 	 * Before we continue lets validate that we are not being fooled by
1639f8829a4aSRandall Stewart 	 * an evil attacker. We can only have 4k chunks based on our TSN
1640f8829a4aSRandall Stewart 	 * spread allowed by the mapping array 512 * 8 bits, so there is no
1641f8829a4aSRandall Stewart 	 * way our stream sequence numbers could have wrapped. We of course
1642f8829a4aSRandall Stewart 	 * only validate the FIRST fragment so the bit must be set.
1643f8829a4aSRandall Stewart 	 */
1644f8829a4aSRandall Stewart 	strmseq = ntohs(ch->dp.stream_sequence);
1645f42a358aSRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
164618e198d3SRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
164718e198d3SRandall Stewart 	if (asoc->tsn_in_at >= SCTP_TSN_LOG_SIZE) {
164818e198d3SRandall Stewart 		asoc->tsn_in_at = 0;
164918e198d3SRandall Stewart 		asoc->tsn_in_wrapped = 1;
165018e198d3SRandall Stewart 	}
1651f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].tsn = tsn;
1652f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].strm = strmno;
1653f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].seq = strmseq;
1654f1f73e57SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].sz = chk_length;
1655f1f73e57SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].flgs = chunk_flags;
165618e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].stcb = (void *)stcb;
165718e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].in_pos = asoc->tsn_in_at;
165818e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].in_out = 1;
1659f42a358aSRandall Stewart 	asoc->tsn_in_at++;
1660f42a358aSRandall Stewart #endif
1661f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_FIRST_FRAG) &&
1662d61a0ae0SRandall Stewart 	    (TAILQ_EMPTY(&asoc->resetHead)) &&
1663f42a358aSRandall Stewart 	    (chunk_flags & SCTP_DATA_UNORDERED) == 0 &&
1664f8829a4aSRandall Stewart 	    (compare_with_wrap(asoc->strmin[strmno].last_sequence_delivered,
1665f8829a4aSRandall Stewart 	    strmseq, MAX_SEQ) ||
1666f8829a4aSRandall Stewart 	    asoc->strmin[strmno].last_sequence_delivered == strmseq)) {
1667f8829a4aSRandall Stewart 		/* The incoming sseq is behind where we last delivered? */
1668ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1, "EVIL/Broken-Dup S-SEQ:%d delivered:%d from peer, Abort!\n",
1669ad81507eSRandall Stewart 		    strmseq, asoc->strmin[strmno].last_sequence_delivered);
1670139bc87fSRandall Stewart 		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1671f8829a4aSRandall Stewart 		    0, M_DONTWAIT, 1, MT_DATA);
1672f8829a4aSRandall Stewart 		if (oper) {
1673f8829a4aSRandall Stewart 			struct sctp_paramhdr *ph;
1674f8829a4aSRandall Stewart 			uint32_t *ippp;
1675f8829a4aSRandall Stewart 
1676139bc87fSRandall Stewart 			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
1677f8829a4aSRandall Stewart 			    (3 * sizeof(uint32_t));
1678f8829a4aSRandall Stewart 			ph = mtod(oper, struct sctp_paramhdr *);
1679f8829a4aSRandall Stewart 			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1680139bc87fSRandall Stewart 			ph->param_length = htons(SCTP_BUF_LEN(oper));
1681f8829a4aSRandall Stewart 			ippp = (uint32_t *) (ph + 1);
1682a5d547adSRandall Stewart 			*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_14);
1683f8829a4aSRandall Stewart 			ippp++;
1684f8829a4aSRandall Stewart 			*ippp = tsn;
1685f8829a4aSRandall Stewart 			ippp++;
1686f8829a4aSRandall Stewart 			*ippp = ((strmno << 16) | strmseq);
1687f8829a4aSRandall Stewart 
1688f8829a4aSRandall Stewart 		}
1689a5d547adSRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_14;
1690f8829a4aSRandall Stewart 		sctp_abort_an_association(stcb->sctp_ep, stcb,
1691ceaad40aSRandall Stewart 		    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1692f8829a4aSRandall Stewart 		*abort_flag = 1;
1693f8829a4aSRandall Stewart 		return (0);
1694f8829a4aSRandall Stewart 	}
1695f42a358aSRandall Stewart 	/************************************
1696f42a358aSRandall Stewart 	 * From here down we may find ch-> invalid
1697f42a358aSRandall Stewart 	 * so its a good idea NOT to use it.
1698f42a358aSRandall Stewart 	 *************************************/
1699f42a358aSRandall Stewart 
1700f8829a4aSRandall Stewart 	the_len = (chk_length - sizeof(struct sctp_data_chunk));
1701f8829a4aSRandall Stewart 	if (last_chunk == 0) {
170244b7479bSRandall Stewart 		dmbuf = SCTP_M_COPYM(*m,
1703f8829a4aSRandall Stewart 		    (offset + sizeof(struct sctp_data_chunk)),
1704f8829a4aSRandall Stewart 		    the_len, M_DONTWAIT);
1705f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
1706b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
1707f8829a4aSRandall Stewart 			struct mbuf *mat;
1708f8829a4aSRandall Stewart 
1709f8829a4aSRandall Stewart 			mat = dmbuf;
1710f8829a4aSRandall Stewart 			while (mat) {
1711139bc87fSRandall Stewart 				if (SCTP_BUF_IS_EXTENDED(mat)) {
1712f8829a4aSRandall Stewart 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
1713f8829a4aSRandall Stewart 				}
1714139bc87fSRandall Stewart 				mat = SCTP_BUF_NEXT(mat);
1715f8829a4aSRandall Stewart 			}
1716f8829a4aSRandall Stewart 		}
1717f8829a4aSRandall Stewart #endif
1718f8829a4aSRandall Stewart 	} else {
1719f8829a4aSRandall Stewart 		/* We can steal the last chunk */
1720139bc87fSRandall Stewart 		int l_len;
1721139bc87fSRandall Stewart 
1722f8829a4aSRandall Stewart 		dmbuf = *m;
1723f8829a4aSRandall Stewart 		/* lop off the top part */
1724f8829a4aSRandall Stewart 		m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk)));
1725139bc87fSRandall Stewart 		if (SCTP_BUF_NEXT(dmbuf) == NULL) {
1726139bc87fSRandall Stewart 			l_len = SCTP_BUF_LEN(dmbuf);
1727139bc87fSRandall Stewart 		} else {
1728139bc87fSRandall Stewart 			/*
1729139bc87fSRandall Stewart 			 * need to count up the size hopefully does not hit
1730139bc87fSRandall Stewart 			 * this to often :-0
1731139bc87fSRandall Stewart 			 */
1732139bc87fSRandall Stewart 			struct mbuf *lat;
1733139bc87fSRandall Stewart 
1734139bc87fSRandall Stewart 			l_len = 0;
1735139bc87fSRandall Stewart 			lat = dmbuf;
1736139bc87fSRandall Stewart 			while (lat) {
1737139bc87fSRandall Stewart 				l_len += SCTP_BUF_LEN(lat);
1738139bc87fSRandall Stewart 				lat = SCTP_BUF_NEXT(lat);
1739139bc87fSRandall Stewart 			}
1740139bc87fSRandall Stewart 		}
1741139bc87fSRandall Stewart 		if (l_len > the_len) {
1742f8829a4aSRandall Stewart 			/* Trim the end round bytes off  too */
1743139bc87fSRandall Stewart 			m_adj(dmbuf, -(l_len - the_len));
1744f8829a4aSRandall Stewart 		}
1745f8829a4aSRandall Stewart 	}
1746f8829a4aSRandall Stewart 	if (dmbuf == NULL) {
1747f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_nomem);
1748f8829a4aSRandall Stewart 		return (0);
1749f8829a4aSRandall Stewart 	}
1750f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG &&
1751f8829a4aSRandall Stewart 	    asoc->fragmented_delivery_inprogress == 0 &&
1752f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->resetHead) &&
1753f42a358aSRandall Stewart 	    ((ordered == 0) ||
1754f8829a4aSRandall Stewart 	    ((asoc->strmin[strmno].last_sequence_delivered + 1) == strmseq &&
1755f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->strmin[strmno].inqueue)))) {
1756f8829a4aSRandall Stewart 		/* Candidate for express delivery */
1757f8829a4aSRandall Stewart 		/*
1758f8829a4aSRandall Stewart 		 * Its not fragmented, No PD-API is up, Nothing in the
1759f8829a4aSRandall Stewart 		 * delivery queue, Its un-ordered OR ordered and the next to
1760f8829a4aSRandall Stewart 		 * deliver AND nothing else is stuck on the stream queue,
1761f8829a4aSRandall Stewart 		 * And there is room for it in the socket buffer. Lets just
1762f8829a4aSRandall Stewart 		 * stuff it up the buffer....
1763f8829a4aSRandall Stewart 		 */
1764f8829a4aSRandall Stewart 
1765f8829a4aSRandall Stewart 		/* It would be nice to avoid this copy if we could :< */
1766f8829a4aSRandall Stewart 		sctp_alloc_a_readq(stcb, control);
1767f8829a4aSRandall Stewart 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1768f42a358aSRandall Stewart 		    protocol_id,
1769f8829a4aSRandall Stewart 		    stcb->asoc.context,
1770f8829a4aSRandall Stewart 		    strmno, strmseq,
1771f42a358aSRandall Stewart 		    chunk_flags,
1772f8829a4aSRandall Stewart 		    dmbuf);
1773f8829a4aSRandall Stewart 		if (control == NULL) {
1774f8829a4aSRandall Stewart 			goto failed_express_del;
1775f8829a4aSRandall Stewart 		}
1776cfde3ff7SRandall Stewart 		sctp_add_to_readq(stcb->sctp_ep, stcb,
1777cfde3ff7SRandall Stewart 		    control, &stcb->sctp_socket->so_rcv,
1778cfde3ff7SRandall Stewart 		    1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
1779830d754dSRandall Stewart 
1780f42a358aSRandall Stewart 		if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1781f8829a4aSRandall Stewart 			/* for ordered, bump what we delivered */
1782f8829a4aSRandall Stewart 			asoc->strmin[strmno].last_sequence_delivered++;
1783f8829a4aSRandall Stewart 		}
1784f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvexpress);
1785b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
17866a91f103SRandall Stewart 			sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno,
1787f8829a4aSRandall Stewart 			    SCTP_STR_LOG_FROM_EXPRS_DEL);
178880fefe0aSRandall Stewart 		}
1789f8829a4aSRandall Stewart 		control = NULL;
179077acdc25SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
179177acdc25SRandall Stewart 		if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN)) {
179277acdc25SRandall Stewart 			asoc->highest_tsn_inside_nr_map = tsn;
179377acdc25SRandall Stewart 		}
1794f8829a4aSRandall Stewart 		goto finish_express_del;
1795f8829a4aSRandall Stewart 	}
1796f8829a4aSRandall Stewart failed_express_del:
1797f8829a4aSRandall Stewart 	/* If we reach here this is a new chunk */
1798f8829a4aSRandall Stewart 	chk = NULL;
1799f8829a4aSRandall Stewart 	control = NULL;
1800f8829a4aSRandall Stewart 	/* Express for fragmented delivery? */
1801f8829a4aSRandall Stewart 	if ((asoc->fragmented_delivery_inprogress) &&
1802f8829a4aSRandall Stewart 	    (stcb->asoc.control_pdapi) &&
1803f8829a4aSRandall Stewart 	    (asoc->str_of_pdapi == strmno) &&
1804f8829a4aSRandall Stewart 	    (asoc->ssn_of_pdapi == strmseq)
1805f8829a4aSRandall Stewart 	    ) {
1806f8829a4aSRandall Stewart 		control = stcb->asoc.control_pdapi;
1807f42a358aSRandall Stewart 		if ((chunk_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
1808f8829a4aSRandall Stewart 			/* Can't be another first? */
1809f8829a4aSRandall Stewart 			goto failed_pdapi_express_del;
1810f8829a4aSRandall Stewart 		}
1811f8829a4aSRandall Stewart 		if (tsn == (control->sinfo_tsn + 1)) {
1812f8829a4aSRandall Stewart 			/* Yep, we can add it on */
1813f8829a4aSRandall Stewart 			int end = 0;
1814f8829a4aSRandall Stewart 			uint32_t cumack;
1815f8829a4aSRandall Stewart 
1816f42a358aSRandall Stewart 			if (chunk_flags & SCTP_DATA_LAST_FRAG) {
1817f8829a4aSRandall Stewart 				end = 1;
1818f8829a4aSRandall Stewart 			}
1819f8829a4aSRandall Stewart 			cumack = asoc->cumulative_tsn;
1820f8829a4aSRandall Stewart 			if ((cumack + 1) == tsn)
1821f8829a4aSRandall Stewart 				cumack = tsn;
1822f8829a4aSRandall Stewart 
1823f8829a4aSRandall Stewart 			if (sctp_append_to_readq(stcb->sctp_ep, stcb, control, dmbuf, end,
1824f8829a4aSRandall Stewart 			    tsn,
1825f8829a4aSRandall Stewart 			    &stcb->sctp_socket->so_rcv)) {
1826ad81507eSRandall Stewart 				SCTP_PRINTF("Append fails end:%d\n", end);
1827f8829a4aSRandall Stewart 				goto failed_pdapi_express_del;
1828f8829a4aSRandall Stewart 			}
182977acdc25SRandall Stewart 			SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
183077acdc25SRandall Stewart 			if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN)) {
1831830d754dSRandall Stewart 				asoc->highest_tsn_inside_nr_map = tsn;
1832830d754dSRandall Stewart 			}
1833f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvexpressm);
1834f8829a4aSRandall Stewart 			control->sinfo_tsn = tsn;
1835f8829a4aSRandall Stewart 			asoc->tsn_last_delivered = tsn;
1836f42a358aSRandall Stewart 			asoc->fragment_flags = chunk_flags;
1837f8829a4aSRandall Stewart 			asoc->tsn_of_pdapi_last_delivered = tsn;
1838f42a358aSRandall Stewart 			asoc->last_flags_delivered = chunk_flags;
1839f8829a4aSRandall Stewart 			asoc->last_strm_seq_delivered = strmseq;
1840f8829a4aSRandall Stewart 			asoc->last_strm_no_delivered = strmno;
1841f8829a4aSRandall Stewart 			if (end) {
1842f8829a4aSRandall Stewart 				/* clean up the flags and such */
1843f8829a4aSRandall Stewart 				asoc->fragmented_delivery_inprogress = 0;
1844f42a358aSRandall Stewart 				if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1845f8829a4aSRandall Stewart 					asoc->strmin[strmno].last_sequence_delivered++;
1846a5d547adSRandall Stewart 				}
1847f8829a4aSRandall Stewart 				stcb->asoc.control_pdapi = NULL;
1848139bc87fSRandall Stewart 				if (TAILQ_EMPTY(&asoc->reasmqueue) == 0) {
1849139bc87fSRandall Stewart 					/*
1850139bc87fSRandall Stewart 					 * There could be another message
1851139bc87fSRandall Stewart 					 * ready
1852139bc87fSRandall Stewart 					 */
1853139bc87fSRandall Stewart 					need_reasm_check = 1;
1854139bc87fSRandall Stewart 				}
1855f8829a4aSRandall Stewart 			}
185677acdc25SRandall Stewart 			SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
185777acdc25SRandall Stewart 			if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN)) {
185877acdc25SRandall Stewart 				asoc->highest_tsn_inside_nr_map = tsn;
185977acdc25SRandall Stewart 			}
1860f8829a4aSRandall Stewart 			control = NULL;
1861f8829a4aSRandall Stewart 			goto finish_express_del;
1862f8829a4aSRandall Stewart 		}
1863f8829a4aSRandall Stewart 	}
1864f8829a4aSRandall Stewart failed_pdapi_express_del:
1865f8829a4aSRandall Stewart 	control = NULL;
186677acdc25SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
186777acdc25SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
186877acdc25SRandall Stewart 		if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN)) {
186977acdc25SRandall Stewart 			asoc->highest_tsn_inside_nr_map = tsn;
187077acdc25SRandall Stewart 		}
187177acdc25SRandall Stewart 	} else {
187277acdc25SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
187377acdc25SRandall Stewart 		if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
187477acdc25SRandall Stewart 			asoc->highest_tsn_inside_map = tsn;
187577acdc25SRandall Stewart 		}
187677acdc25SRandall Stewart 	}
1877f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
1878f8829a4aSRandall Stewart 		sctp_alloc_a_chunk(stcb, chk);
1879f8829a4aSRandall Stewart 		if (chk == NULL) {
1880f8829a4aSRandall Stewart 			/* No memory so we drop the chunk */
1881f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_nomem);
1882f8829a4aSRandall Stewart 			if (last_chunk == 0) {
1883f8829a4aSRandall Stewart 				/* we copied it, free the copy */
1884f8829a4aSRandall Stewart 				sctp_m_freem(dmbuf);
1885f8829a4aSRandall Stewart 			}
1886f8829a4aSRandall Stewart 			return (0);
1887f8829a4aSRandall Stewart 		}
1888f8829a4aSRandall Stewart 		chk->rec.data.TSN_seq = tsn;
1889f8829a4aSRandall Stewart 		chk->no_fr_allowed = 0;
1890f8829a4aSRandall Stewart 		chk->rec.data.stream_seq = strmseq;
1891f8829a4aSRandall Stewart 		chk->rec.data.stream_number = strmno;
1892f42a358aSRandall Stewart 		chk->rec.data.payloadtype = protocol_id;
1893f8829a4aSRandall Stewart 		chk->rec.data.context = stcb->asoc.context;
1894f8829a4aSRandall Stewart 		chk->rec.data.doing_fast_retransmit = 0;
1895f42a358aSRandall Stewart 		chk->rec.data.rcv_flags = chunk_flags;
1896f8829a4aSRandall Stewart 		chk->asoc = asoc;
1897f8829a4aSRandall Stewart 		chk->send_size = the_len;
1898f8829a4aSRandall Stewart 		chk->whoTo = net;
1899f8829a4aSRandall Stewart 		atomic_add_int(&net->ref_count, 1);
1900f8829a4aSRandall Stewart 		chk->data = dmbuf;
1901f8829a4aSRandall Stewart 	} else {
1902f8829a4aSRandall Stewart 		sctp_alloc_a_readq(stcb, control);
1903f8829a4aSRandall Stewart 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1904f42a358aSRandall Stewart 		    protocol_id,
1905f8829a4aSRandall Stewart 		    stcb->asoc.context,
1906f8829a4aSRandall Stewart 		    strmno, strmseq,
1907f42a358aSRandall Stewart 		    chunk_flags,
1908f8829a4aSRandall Stewart 		    dmbuf);
1909f8829a4aSRandall Stewart 		if (control == NULL) {
1910f8829a4aSRandall Stewart 			/* No memory so we drop the chunk */
1911f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_nomem);
1912f8829a4aSRandall Stewart 			if (last_chunk == 0) {
1913f8829a4aSRandall Stewart 				/* we copied it, free the copy */
1914f8829a4aSRandall Stewart 				sctp_m_freem(dmbuf);
1915f8829a4aSRandall Stewart 			}
1916f8829a4aSRandall Stewart 			return (0);
1917f8829a4aSRandall Stewart 		}
1918f8829a4aSRandall Stewart 		control->length = the_len;
1919f8829a4aSRandall Stewart 	}
1920f8829a4aSRandall Stewart 
1921f8829a4aSRandall Stewart 	/* Mark it as received */
1922f8829a4aSRandall Stewart 	/* Now queue it where it belongs */
1923f8829a4aSRandall Stewart 	if (control != NULL) {
1924f8829a4aSRandall Stewart 		/* First a sanity check */
1925f8829a4aSRandall Stewart 		if (asoc->fragmented_delivery_inprogress) {
1926f8829a4aSRandall Stewart 			/*
1927f8829a4aSRandall Stewart 			 * Ok, we have a fragmented delivery in progress if
1928f8829a4aSRandall Stewart 			 * this chunk is next to deliver OR belongs in our
1929f8829a4aSRandall Stewart 			 * view to the reassembly, the peer is evil or
1930f8829a4aSRandall Stewart 			 * broken.
1931f8829a4aSRandall Stewart 			 */
1932f8829a4aSRandall Stewart 			uint32_t estimate_tsn;
1933f8829a4aSRandall Stewart 
1934f8829a4aSRandall Stewart 			estimate_tsn = asoc->tsn_last_delivered + 1;
1935f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->reasmqueue) &&
1936f8829a4aSRandall Stewart 			    (estimate_tsn == control->sinfo_tsn)) {
1937f8829a4aSRandall Stewart 				/* Evil/Broke peer */
1938f8829a4aSRandall Stewart 				sctp_m_freem(control->data);
1939f8829a4aSRandall Stewart 				control->data = NULL;
19405bead436SRandall Stewart 				if (control->whoFrom) {
1941f8829a4aSRandall Stewart 					sctp_free_remote_addr(control->whoFrom);
19425bead436SRandall Stewart 					control->whoFrom = NULL;
19435bead436SRandall Stewart 				}
1944f8829a4aSRandall Stewart 				sctp_free_a_readq(stcb, control);
1945139bc87fSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1946f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
1947f8829a4aSRandall Stewart 				if (oper) {
1948f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
1949f8829a4aSRandall Stewart 					uint32_t *ippp;
1950f8829a4aSRandall Stewart 
1951139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) =
1952f8829a4aSRandall Stewart 					    sizeof(struct sctp_paramhdr) +
1953f8829a4aSRandall Stewart 					    (3 * sizeof(uint32_t));
1954f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
1955f8829a4aSRandall Stewart 					ph->param_type =
1956f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1957139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
1958f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
1959a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_15);
1960f8829a4aSRandall Stewart 					ippp++;
1961f8829a4aSRandall Stewart 					*ippp = tsn;
1962f8829a4aSRandall Stewart 					ippp++;
1963f8829a4aSRandall Stewart 					*ippp = ((strmno << 16) | strmseq);
1964f8829a4aSRandall Stewart 				}
1965a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_15;
1966f8829a4aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb,
1967ceaad40aSRandall Stewart 				    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1968f8829a4aSRandall Stewart 
1969f8829a4aSRandall Stewart 				*abort_flag = 1;
1970f8829a4aSRandall Stewart 				return (0);
1971f8829a4aSRandall Stewart 			} else {
1972f8829a4aSRandall Stewart 				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
1973f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
1974f8829a4aSRandall Stewart 					control->data = NULL;
19755bead436SRandall Stewart 					if (control->whoFrom) {
1976f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
19775bead436SRandall Stewart 						control->whoFrom = NULL;
19785bead436SRandall Stewart 					}
1979f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
1980f8829a4aSRandall Stewart 
1981139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1982f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1983f8829a4aSRandall Stewart 					if (oper) {
1984f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1985f8829a4aSRandall Stewart 						uint32_t *ippp;
1986f8829a4aSRandall Stewart 
1987139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1988f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1989f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1990f8829a4aSRandall Stewart 						ph = mtod(oper,
1991f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1992f8829a4aSRandall Stewart 						ph->param_type =
1993f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1994f8829a4aSRandall Stewart 						ph->param_length =
1995139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1996f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1997a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_16);
1998f8829a4aSRandall Stewart 						ippp++;
1999f8829a4aSRandall Stewart 						*ippp = tsn;
2000f8829a4aSRandall Stewart 						ippp++;
2001f8829a4aSRandall Stewart 						*ippp = ((strmno << 16) | strmseq);
2002f8829a4aSRandall Stewart 					}
2003a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_16;
2004f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
2005ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
2006f8829a4aSRandall Stewart 
2007f8829a4aSRandall Stewart 					*abort_flag = 1;
2008f8829a4aSRandall Stewart 					return (0);
2009f8829a4aSRandall Stewart 				}
2010f8829a4aSRandall Stewart 			}
2011f8829a4aSRandall Stewart 		} else {
2012f8829a4aSRandall Stewart 			/* No PDAPI running */
2013f8829a4aSRandall Stewart 			if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
2014f8829a4aSRandall Stewart 				/*
2015f8829a4aSRandall Stewart 				 * Reassembly queue is NOT empty validate
2016f8829a4aSRandall Stewart 				 * that this tsn does not need to be in
2017f8829a4aSRandall Stewart 				 * reasembly queue. If it does then our peer
2018f8829a4aSRandall Stewart 				 * is broken or evil.
2019f8829a4aSRandall Stewart 				 */
2020f8829a4aSRandall Stewart 				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
2021f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
2022f8829a4aSRandall Stewart 					control->data = NULL;
20235bead436SRandall Stewart 					if (control->whoFrom) {
2024f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
20255bead436SRandall Stewart 						control->whoFrom = NULL;
20265bead436SRandall Stewart 					}
2027f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
2028139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
2029f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
2030f8829a4aSRandall Stewart 					if (oper) {
2031f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
2032f8829a4aSRandall Stewart 						uint32_t *ippp;
2033f8829a4aSRandall Stewart 
2034139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
2035f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
2036f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
2037f8829a4aSRandall Stewart 						ph = mtod(oper,
2038f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
2039f8829a4aSRandall Stewart 						ph->param_type =
2040f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2041f8829a4aSRandall Stewart 						ph->param_length =
2042139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
2043f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
2044a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_17);
2045f8829a4aSRandall Stewart 						ippp++;
2046f8829a4aSRandall Stewart 						*ippp = tsn;
2047f8829a4aSRandall Stewart 						ippp++;
2048f8829a4aSRandall Stewart 						*ippp = ((strmno << 16) | strmseq);
2049f8829a4aSRandall Stewart 					}
2050a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_17;
2051f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
2052ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
2053f8829a4aSRandall Stewart 
2054f8829a4aSRandall Stewart 					*abort_flag = 1;
2055f8829a4aSRandall Stewart 					return (0);
2056f8829a4aSRandall Stewart 				}
2057f8829a4aSRandall Stewart 			}
2058f8829a4aSRandall Stewart 		}
2059f8829a4aSRandall Stewart 		/* ok, if we reach here we have passed the sanity checks */
2060f42a358aSRandall Stewart 		if (chunk_flags & SCTP_DATA_UNORDERED) {
2061f8829a4aSRandall Stewart 			/* queue directly into socket buffer */
2062f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep, stcb,
2063f8829a4aSRandall Stewart 			    control,
2064cfde3ff7SRandall Stewart 			    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
206577acdc25SRandall Stewart 
2066f8829a4aSRandall Stewart 		} else {
2067f8829a4aSRandall Stewart 			/*
2068f8829a4aSRandall Stewart 			 * Special check for when streams are resetting. We
2069f8829a4aSRandall Stewart 			 * could be more smart about this and check the
2070f8829a4aSRandall Stewart 			 * actual stream to see if it is not being reset..
2071f8829a4aSRandall Stewart 			 * that way we would not create a HOLB when amongst
2072f8829a4aSRandall Stewart 			 * streams being reset and those not being reset.
2073f8829a4aSRandall Stewart 			 *
2074f8829a4aSRandall Stewart 			 * We take complete messages that have a stream reset
2075f8829a4aSRandall Stewart 			 * intervening (aka the TSN is after where our
2076f8829a4aSRandall Stewart 			 * cum-ack needs to be) off and put them on a
2077f8829a4aSRandall Stewart 			 * pending_reply_queue. The reassembly ones we do
2078f8829a4aSRandall Stewart 			 * not have to worry about since they are all sorted
2079f8829a4aSRandall Stewart 			 * and proceessed by TSN order. It is only the
2080f8829a4aSRandall Stewart 			 * singletons I must worry about.
2081f8829a4aSRandall Stewart 			 */
2082f8829a4aSRandall Stewart 			if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2083d61a0ae0SRandall Stewart 			    ((compare_with_wrap(tsn, liste->tsn, MAX_TSN)))
2084f8829a4aSRandall Stewart 			    ) {
2085f8829a4aSRandall Stewart 				/*
2086f8829a4aSRandall Stewart 				 * yep its past where we need to reset... go
2087f8829a4aSRandall Stewart 				 * ahead and queue it.
2088f8829a4aSRandall Stewart 				 */
2089f8829a4aSRandall Stewart 				if (TAILQ_EMPTY(&asoc->pending_reply_queue)) {
2090f8829a4aSRandall Stewart 					/* first one on */
2091f8829a4aSRandall Stewart 					TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2092f8829a4aSRandall Stewart 				} else {
2093f8829a4aSRandall Stewart 					struct sctp_queued_to_read *ctlOn;
2094f8829a4aSRandall Stewart 					unsigned char inserted = 0;
2095f8829a4aSRandall Stewart 
2096f8829a4aSRandall Stewart 					ctlOn = TAILQ_FIRST(&asoc->pending_reply_queue);
2097f8829a4aSRandall Stewart 					while (ctlOn) {
2098f8829a4aSRandall Stewart 						if (compare_with_wrap(control->sinfo_tsn,
2099f8829a4aSRandall Stewart 						    ctlOn->sinfo_tsn, MAX_TSN)) {
2100f8829a4aSRandall Stewart 							ctlOn = TAILQ_NEXT(ctlOn, next);
2101f8829a4aSRandall Stewart 						} else {
2102f8829a4aSRandall Stewart 							/* found it */
2103f8829a4aSRandall Stewart 							TAILQ_INSERT_BEFORE(ctlOn, control, next);
2104f8829a4aSRandall Stewart 							inserted = 1;
2105f8829a4aSRandall Stewart 							break;
2106f8829a4aSRandall Stewart 						}
2107f8829a4aSRandall Stewart 					}
2108f8829a4aSRandall Stewart 					if (inserted == 0) {
2109f8829a4aSRandall Stewart 						/*
2110f8829a4aSRandall Stewart 						 * must be put at end, use
2111f8829a4aSRandall Stewart 						 * prevP (all setup from
2112f8829a4aSRandall Stewart 						 * loop) to setup nextP.
2113f8829a4aSRandall Stewart 						 */
2114f8829a4aSRandall Stewart 						TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2115f8829a4aSRandall Stewart 					}
2116f8829a4aSRandall Stewart 				}
2117f8829a4aSRandall Stewart 			} else {
2118f8829a4aSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, control, abort_flag);
2119f8829a4aSRandall Stewart 				if (*abort_flag) {
2120f8829a4aSRandall Stewart 					return (0);
2121f8829a4aSRandall Stewart 				}
2122f8829a4aSRandall Stewart 			}
2123f8829a4aSRandall Stewart 		}
2124f8829a4aSRandall Stewart 	} else {
2125f8829a4aSRandall Stewart 		/* Into the re-assembly queue */
2126f8829a4aSRandall Stewart 		sctp_queue_data_for_reasm(stcb, asoc, chk, abort_flag);
2127f8829a4aSRandall Stewart 		if (*abort_flag) {
2128a5d547adSRandall Stewart 			/*
2129a5d547adSRandall Stewart 			 * the assoc is now gone and chk was put onto the
2130a5d547adSRandall Stewart 			 * reasm queue, which has all been freed.
2131a5d547adSRandall Stewart 			 */
2132a5d547adSRandall Stewart 			*m = NULL;
2133f8829a4aSRandall Stewart 			return (0);
2134f8829a4aSRandall Stewart 		}
2135f8829a4aSRandall Stewart 	}
2136f8829a4aSRandall Stewart finish_express_del:
2137f8829a4aSRandall Stewart 	if (tsn == (asoc->cumulative_tsn + 1)) {
2138f8829a4aSRandall Stewart 		/* Update cum-ack */
2139f8829a4aSRandall Stewart 		asoc->cumulative_tsn = tsn;
2140f8829a4aSRandall Stewart 	}
2141f8829a4aSRandall Stewart 	if (last_chunk) {
2142f8829a4aSRandall Stewart 		*m = NULL;
2143f8829a4aSRandall Stewart 	}
2144f42a358aSRandall Stewart 	if (ordered) {
2145f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_inorderchunks);
2146f8829a4aSRandall Stewart 	} else {
2147f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_inunorderchunks);
2148f8829a4aSRandall Stewart 	}
2149f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvdata);
2150f8829a4aSRandall Stewart 	/* Set it present please */
2151b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
21526a91f103SRandall Stewart 		sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno, SCTP_STR_LOG_FROM_MARK_TSN);
215380fefe0aSRandall Stewart 	}
2154b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2155f8829a4aSRandall Stewart 		sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2156f8829a4aSRandall Stewart 		    asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE);
215780fefe0aSRandall Stewart 	}
215817205eccSRandall Stewart 	/* check the special flag for stream resets */
215917205eccSRandall Stewart 	if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2160d61a0ae0SRandall Stewart 	    ((compare_with_wrap(asoc->cumulative_tsn, liste->tsn, MAX_TSN)) ||
2161d61a0ae0SRandall Stewart 	    (asoc->cumulative_tsn == liste->tsn))
216217205eccSRandall Stewart 	    ) {
216317205eccSRandall Stewart 		/*
216417205eccSRandall Stewart 		 * we have finished working through the backlogged TSN's now
216517205eccSRandall Stewart 		 * time to reset streams. 1: call reset function. 2: free
216617205eccSRandall Stewart 		 * pending_reply space 3: distribute any chunks in
216717205eccSRandall Stewart 		 * pending_reply_queue.
216817205eccSRandall Stewart 		 */
216917205eccSRandall Stewart 		struct sctp_queued_to_read *ctl;
217017205eccSRandall Stewart 
217117205eccSRandall Stewart 		sctp_reset_in_stream(stcb, liste->number_entries, liste->req.list_of_streams);
217217205eccSRandall Stewart 		TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
2173207304d4SRandall Stewart 		SCTP_FREE(liste, SCTP_M_STRESET);
21743c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
217517205eccSRandall Stewart 		liste = TAILQ_FIRST(&asoc->resetHead);
217617205eccSRandall Stewart 		ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
217717205eccSRandall Stewart 		if (ctl && (liste == NULL)) {
217817205eccSRandall Stewart 			/* All can be removed */
217917205eccSRandall Stewart 			while (ctl) {
218017205eccSRandall Stewart 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
218117205eccSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
218217205eccSRandall Stewart 				if (*abort_flag) {
218317205eccSRandall Stewart 					return (0);
218417205eccSRandall Stewart 				}
218517205eccSRandall Stewart 				ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
218617205eccSRandall Stewart 			}
218717205eccSRandall Stewart 		} else if (ctl) {
218817205eccSRandall Stewart 			/* more than one in queue */
2189d61a0ae0SRandall Stewart 			while (!compare_with_wrap(ctl->sinfo_tsn, liste->tsn, MAX_TSN)) {
219017205eccSRandall Stewart 				/*
219117205eccSRandall Stewart 				 * if ctl->sinfo_tsn is <= liste->tsn we can
219217205eccSRandall Stewart 				 * process it which is the NOT of
219317205eccSRandall Stewart 				 * ctl->sinfo_tsn > liste->tsn
219417205eccSRandall Stewart 				 */
219517205eccSRandall Stewart 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
219617205eccSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
219717205eccSRandall Stewart 				if (*abort_flag) {
219817205eccSRandall Stewart 					return (0);
219917205eccSRandall Stewart 				}
220017205eccSRandall Stewart 				ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
220117205eccSRandall Stewart 			}
220217205eccSRandall Stewart 		}
220317205eccSRandall Stewart 		/*
220417205eccSRandall Stewart 		 * Now service re-assembly to pick up anything that has been
220517205eccSRandall Stewart 		 * held on reassembly queue?
220617205eccSRandall Stewart 		 */
220717205eccSRandall Stewart 		sctp_deliver_reasm_check(stcb, asoc);
220817205eccSRandall Stewart 		need_reasm_check = 0;
220917205eccSRandall Stewart 	}
2210139bc87fSRandall Stewart 	if (need_reasm_check) {
2211139bc87fSRandall Stewart 		/* Another one waits ? */
2212139bc87fSRandall Stewart 		sctp_deliver_reasm_check(stcb, asoc);
2213139bc87fSRandall Stewart 	}
2214f8829a4aSRandall Stewart 	return (1);
2215f8829a4aSRandall Stewart }
2216f8829a4aSRandall Stewart 
2217f8829a4aSRandall Stewart int8_t sctp_map_lookup_tab[256] = {
2218f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2219f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2220f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2221f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 4,
2222f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2223f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2224f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2225f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 5,
2226f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2227f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2228f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2229f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 4,
2230f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2231f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2232f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2233f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 6,
2234f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2235f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2236f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2237f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 4,
2238f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2239f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2240f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2241f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 5,
2242f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2243f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2244f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2245f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 4,
2246f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2247f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 3,
2248f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 2,
2249f8829a4aSRandall Stewart 	-1, 0, -1, 1, -1, 0, -1, 7,
2250f8829a4aSRandall Stewart };
2251f8829a4aSRandall Stewart 
2252f8829a4aSRandall Stewart 
2253f8829a4aSRandall Stewart void
2254f8829a4aSRandall Stewart sctp_sack_check(struct sctp_tcb *stcb, int ok_to_sack, int was_a_gap, int *abort_flag)
2255f8829a4aSRandall Stewart {
2256f8829a4aSRandall Stewart 	/*
2257f8829a4aSRandall Stewart 	 * Now we also need to check the mapping array in a couple of ways.
2258f8829a4aSRandall Stewart 	 * 1) Did we move the cum-ack point?
2259f8829a4aSRandall Stewart 	 */
2260f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2261b3f1ea41SRandall Stewart 	int at;
2262c4739e2fSRandall Stewart 	int last_all_ones = 0;
2263f8829a4aSRandall Stewart 	int slide_from, slide_end, lgap, distance;
2264830d754dSRandall Stewart 
2265830d754dSRandall Stewart 	/* EY nr_mapping array variables */
22668933fa13SRandall Stewart 	/* int nr_at; */
22678933fa13SRandall Stewart 	/* int nr_last_all_ones = 0; */
22688933fa13SRandall Stewart 	/* int nr_slide_from, nr_slide_end, nr_lgap, nr_distance; */
226977acdc25SRandall Stewart 	uint32_t old_cumack, old_base, old_highest, highest_tsn;
2270f8829a4aSRandall Stewart 
2271f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2272f8829a4aSRandall Stewart 	at = 0;
2273f8829a4aSRandall Stewart 
2274f8829a4aSRandall Stewart 	old_cumack = asoc->cumulative_tsn;
2275f8829a4aSRandall Stewart 	old_base = asoc->mapping_array_base_tsn;
2276f8829a4aSRandall Stewart 	old_highest = asoc->highest_tsn_inside_map;
2277f8829a4aSRandall Stewart 	/*
2278f8829a4aSRandall Stewart 	 * We could probably improve this a small bit by calculating the
2279f8829a4aSRandall Stewart 	 * offset of the current cum-ack as the starting point.
2280f8829a4aSRandall Stewart 	 */
2281f8829a4aSRandall Stewart 	at = 0;
228277acdc25SRandall Stewart 	for (slide_from = 0; slide_from < stcb->asoc.nr_mapping_array_size; slide_from++) {
228377acdc25SRandall Stewart 		if (asoc->nr_mapping_array[slide_from] == 0xff) {
2284f8829a4aSRandall Stewart 			at += 8;
228593164cf9SRandall Stewart 			last_all_ones = 1;
2286f8829a4aSRandall Stewart 		} else {
2287f8829a4aSRandall Stewart 			/* there is a 0 bit */
228877acdc25SRandall Stewart 			at += sctp_map_lookup_tab[asoc->nr_mapping_array[slide_from]];
228993164cf9SRandall Stewart 			last_all_ones = 0;
2290f8829a4aSRandall Stewart 			break;
2291f8829a4aSRandall Stewart 		}
2292f8829a4aSRandall Stewart 	}
229377acdc25SRandall Stewart 	asoc->cumulative_tsn = asoc->nr_mapping_array_base_tsn + (at - last_all_ones);
2294f8829a4aSRandall Stewart 	at++;
2295f8829a4aSRandall Stewart 
229677acdc25SRandall Stewart 	if (compare_with_wrap(asoc->cumulative_tsn, asoc->highest_tsn_inside_map, MAX_TSN) &&
229777acdc25SRandall Stewart 	    compare_with_wrap(asoc->cumulative_tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN)
229877acdc25SRandall Stewart 	    ) {
2299a5d547adSRandall Stewart #ifdef INVARIANTS
2300ceaad40aSRandall Stewart 		panic("huh, cumack 0x%x greater than high-tsn 0x%x in map",
2301ceaad40aSRandall Stewart 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2302f8829a4aSRandall Stewart #else
2303ceaad40aSRandall Stewart 		SCTP_PRINTF("huh, cumack 0x%x greater than high-tsn 0x%x in map - should panic?\n",
2304ceaad40aSRandall Stewart 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
23050e13104dSRandall Stewart 		sctp_print_mapping_array(asoc);
2306b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2307b3f1ea41SRandall Stewart 			sctp_log_map(0, 6, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
2308b3f1ea41SRandall Stewart 		}
2309f8829a4aSRandall Stewart 		asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2310830d754dSRandall Stewart 		asoc->highest_tsn_inside_nr_map = asoc->cumulative_tsn;
2311f8829a4aSRandall Stewart #endif
2312f8829a4aSRandall Stewart 	}
231377acdc25SRandall Stewart 	if (compare_with_wrap(asoc->highest_tsn_inside_nr_map,
231477acdc25SRandall Stewart 	    asoc->highest_tsn_inside_map,
231577acdc25SRandall Stewart 	    MAX_TSN)) {
231677acdc25SRandall Stewart 		highest_tsn = asoc->highest_tsn_inside_nr_map;
231777acdc25SRandall Stewart 	} else {
231877acdc25SRandall Stewart 		highest_tsn = asoc->highest_tsn_inside_map;
231977acdc25SRandall Stewart 	}
232077acdc25SRandall Stewart 	if ((asoc->cumulative_tsn == highest_tsn) && (at >= 8)) {
2321f8829a4aSRandall Stewart 		/* The complete array was completed by a single FR */
232277acdc25SRandall Stewart 		/* highest becomes the cum-ack */
2323f8829a4aSRandall Stewart 		int clr;
2324f8829a4aSRandall Stewart 
2325f8829a4aSRandall Stewart 		/* clear the array */
2326f8829a4aSRandall Stewart 		clr = (at >> 3) + 1;
2327c4739e2fSRandall Stewart 		if (clr > asoc->mapping_array_size) {
2328f8829a4aSRandall Stewart 			clr = asoc->mapping_array_size;
2329f8829a4aSRandall Stewart 		}
2330f8829a4aSRandall Stewart 		memset(asoc->mapping_array, 0, clr);
2331830d754dSRandall Stewart 		memset(asoc->nr_mapping_array, 0, clr);
233277acdc25SRandall Stewart 
233377acdc25SRandall Stewart 		asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
2334830d754dSRandall Stewart 		asoc->nr_mapping_array_base_tsn = asoc->cumulative_tsn + 1;
233577acdc25SRandall Stewart 		asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2336f8829a4aSRandall Stewart 	} else if (at >= 8) {
2337f8829a4aSRandall Stewart 		/* we can slide the mapping array down */
2338b3f1ea41SRandall Stewart 		/* slide_from holds where we hit the first NON 0xff byte */
2339b3f1ea41SRandall Stewart 
2340f8829a4aSRandall Stewart 		/*
2341f8829a4aSRandall Stewart 		 * now calculate the ceiling of the move using our highest
2342f8829a4aSRandall Stewart 		 * TSN value
2343f8829a4aSRandall Stewart 		 */
234477acdc25SRandall Stewart 		SCTP_CALC_TSN_TO_GAP(lgap, highest_tsn, asoc->mapping_array_base_tsn);
234577acdc25SRandall Stewart 		slide_end = (lgap >> 3);
2346f8829a4aSRandall Stewart 		if (slide_end < slide_from) {
234777acdc25SRandall Stewart 			sctp_print_mapping_array(asoc);
2348d55b0b1bSRandall Stewart #ifdef INVARIANTS
2349f8829a4aSRandall Stewart 			panic("impossible slide");
2350d55b0b1bSRandall Stewart #else
235177acdc25SRandall Stewart 			printf("impossible slide lgap:%x slide_end:%x slide_from:%x? at:%d\n",
235277acdc25SRandall Stewart 			    lgap, slide_end, slide_from, at);
2353d55b0b1bSRandall Stewart 			return;
2354d55b0b1bSRandall Stewart #endif
2355f8829a4aSRandall Stewart 		}
2356b3f1ea41SRandall Stewart 		if (slide_end > asoc->mapping_array_size) {
2357b3f1ea41SRandall Stewart #ifdef INVARIANTS
2358b3f1ea41SRandall Stewart 			panic("would overrun buffer");
2359b3f1ea41SRandall Stewart #else
2360b3f1ea41SRandall Stewart 			printf("Gak, would have overrun map end:%d slide_end:%d\n",
2361b3f1ea41SRandall Stewart 			    asoc->mapping_array_size, slide_end);
2362b3f1ea41SRandall Stewart 			slide_end = asoc->mapping_array_size;
2363b3f1ea41SRandall Stewart #endif
2364b3f1ea41SRandall Stewart 		}
2365f8829a4aSRandall Stewart 		distance = (slide_end - slide_from) + 1;
2366b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2367f8829a4aSRandall Stewart 			sctp_log_map(old_base, old_cumack, old_highest,
2368f8829a4aSRandall Stewart 			    SCTP_MAP_PREPARE_SLIDE);
2369f8829a4aSRandall Stewart 			sctp_log_map((uint32_t) slide_from, (uint32_t) slide_end,
2370f8829a4aSRandall Stewart 			    (uint32_t) lgap, SCTP_MAP_SLIDE_FROM);
237180fefe0aSRandall Stewart 		}
2372f8829a4aSRandall Stewart 		if (distance + slide_from > asoc->mapping_array_size ||
2373f8829a4aSRandall Stewart 		    distance < 0) {
2374f8829a4aSRandall Stewart 			/*
2375f8829a4aSRandall Stewart 			 * Here we do NOT slide forward the array so that
2376f8829a4aSRandall Stewart 			 * hopefully when more data comes in to fill it up
2377f8829a4aSRandall Stewart 			 * we will be able to slide it forward. Really I
2378f8829a4aSRandall Stewart 			 * don't think this should happen :-0
2379f8829a4aSRandall Stewart 			 */
2380f8829a4aSRandall Stewart 
2381b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2382f8829a4aSRandall Stewart 				sctp_log_map((uint32_t) distance, (uint32_t) slide_from,
2383f8829a4aSRandall Stewart 				    (uint32_t) asoc->mapping_array_size,
2384f8829a4aSRandall Stewart 				    SCTP_MAP_SLIDE_NONE);
238580fefe0aSRandall Stewart 			}
2386f8829a4aSRandall Stewart 		} else {
2387f8829a4aSRandall Stewart 			int ii;
2388f8829a4aSRandall Stewart 
2389f8829a4aSRandall Stewart 			for (ii = 0; ii < distance; ii++) {
2390f8829a4aSRandall Stewart 				asoc->mapping_array[ii] =
2391f8829a4aSRandall Stewart 				    asoc->mapping_array[slide_from + ii];
239277acdc25SRandall Stewart 				asoc->nr_mapping_array[ii] =
239377acdc25SRandall Stewart 				    asoc->nr_mapping_array[slide_from + ii];
239477acdc25SRandall Stewart 
2395f8829a4aSRandall Stewart 			}
2396f8829a4aSRandall Stewart 			for (ii = distance; ii <= slide_end; ii++) {
2397f8829a4aSRandall Stewart 				asoc->mapping_array[ii] = 0;
239877acdc25SRandall Stewart 				asoc->nr_mapping_array[ii] = 0;
2399f8829a4aSRandall Stewart 			}
2400f8829a4aSRandall Stewart 			asoc->mapping_array_base_tsn += (slide_from << 3);
240177acdc25SRandall Stewart 			asoc->nr_mapping_array_base_tsn += (slide_from << 3);
2402b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2403f8829a4aSRandall Stewart 				sctp_log_map(asoc->mapping_array_base_tsn,
2404f8829a4aSRandall Stewart 				    asoc->cumulative_tsn, asoc->highest_tsn_inside_map,
2405f8829a4aSRandall Stewart 				    SCTP_MAP_SLIDE_RESULT);
240680fefe0aSRandall Stewart 			}
2407830d754dSRandall Stewart 		}
2408830d754dSRandall Stewart 	}
2409830d754dSRandall Stewart 	/*
2410f8829a4aSRandall Stewart 	 * Now we need to see if we need to queue a sack or just start the
2411f8829a4aSRandall Stewart 	 * timer (if allowed).
2412f8829a4aSRandall Stewart 	 */
2413f8829a4aSRandall Stewart 	if (ok_to_sack) {
2414f8829a4aSRandall Stewart 		if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2415f8829a4aSRandall Stewart 			/*
2416f8829a4aSRandall Stewart 			 * Ok special case, in SHUTDOWN-SENT case. here we
2417f8829a4aSRandall Stewart 			 * maker sure SACK timer is off and instead send a
2418f8829a4aSRandall Stewart 			 * SHUTDOWN and a SACK
2419f8829a4aSRandall Stewart 			 */
2420139bc87fSRandall Stewart 			if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2421f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
2422a5d547adSRandall Stewart 				    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_INDATA + SCTP_LOC_18);
2423f8829a4aSRandall Stewart 			}
2424f8829a4aSRandall Stewart 			sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
2425830d754dSRandall Stewart 			/*
2426830d754dSRandall Stewart 			 * EY if nr_sacks used then send an nr-sack , a sack
2427830d754dSRandall Stewart 			 * otherwise
2428830d754dSRandall Stewart 			 */
24298933fa13SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack)
2430830d754dSRandall Stewart 				sctp_send_nr_sack(stcb);
2431830d754dSRandall Stewart 			else
2432f8829a4aSRandall Stewart 				sctp_send_sack(stcb);
2433f8829a4aSRandall Stewart 		} else {
2434f8829a4aSRandall Stewart 			int is_a_gap;
2435f8829a4aSRandall Stewart 
2436f8829a4aSRandall Stewart 			/* is there a gap now ? */
243777acdc25SRandall Stewart 			is_a_gap = compare_with_wrap(highest_tsn, stcb->asoc.cumulative_tsn, MAX_TSN);
2438f8829a4aSRandall Stewart 
2439f8829a4aSRandall Stewart 			/*
2440f8829a4aSRandall Stewart 			 * CMT DAC algorithm: increase number of packets
2441f8829a4aSRandall Stewart 			 * received since last ack
2442f8829a4aSRandall Stewart 			 */
2443f8829a4aSRandall Stewart 			stcb->asoc.cmt_dac_pkts_rcvd++;
2444f8829a4aSRandall Stewart 
244542551e99SRandall Stewart 			if ((stcb->asoc.send_sack == 1) ||	/* We need to send a
244642551e99SRandall Stewart 								 * SACK */
2447f8829a4aSRandall Stewart 			    ((was_a_gap) && (is_a_gap == 0)) ||	/* was a gap, but no
2448f8829a4aSRandall Stewart 								 * longer is one */
2449f8829a4aSRandall Stewart 			    (stcb->asoc.numduptsns) ||	/* we have dup's */
2450f8829a4aSRandall Stewart 			    (is_a_gap) ||	/* is still a gap */
245142551e99SRandall Stewart 			    (stcb->asoc.delayed_ack == 0) ||	/* Delayed sack disabled */
245242551e99SRandall Stewart 			    (stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq)	/* hit limit of pkts */
2453f8829a4aSRandall Stewart 			    ) {
2454f8829a4aSRandall Stewart 
2455b3f1ea41SRandall Stewart 				if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off)) &&
2456b3f1ea41SRandall Stewart 				    (SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) &&
245742551e99SRandall Stewart 				    (stcb->asoc.send_sack == 0) &&
2458f8829a4aSRandall Stewart 				    (stcb->asoc.numduptsns == 0) &&
2459f8829a4aSRandall Stewart 				    (stcb->asoc.delayed_ack) &&
2460139bc87fSRandall Stewart 				    (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer))) {
2461f8829a4aSRandall Stewart 
2462f8829a4aSRandall Stewart 					/*
2463f8829a4aSRandall Stewart 					 * CMT DAC algorithm: With CMT,
2464f8829a4aSRandall Stewart 					 * delay acks even in the face of
2465f8829a4aSRandall Stewart 					 *
2466f8829a4aSRandall Stewart 					 * reordering. Therefore, if acks that
2467f8829a4aSRandall Stewart 					 * do not have to be sent because of
2468f8829a4aSRandall Stewart 					 * the above reasons, will be
2469f8829a4aSRandall Stewart 					 * delayed. That is, acks that would
2470f8829a4aSRandall Stewart 					 * have been sent due to gap reports
2471f8829a4aSRandall Stewart 					 * will be delayed with DAC. Start
2472f8829a4aSRandall Stewart 					 * the delayed ack timer.
2473f8829a4aSRandall Stewart 					 */
2474f8829a4aSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2475f8829a4aSRandall Stewart 					    stcb->sctp_ep, stcb, NULL);
2476f8829a4aSRandall Stewart 				} else {
2477f8829a4aSRandall Stewart 					/*
2478f8829a4aSRandall Stewart 					 * Ok we must build a SACK since the
2479f8829a4aSRandall Stewart 					 * timer is pending, we got our
2480f8829a4aSRandall Stewart 					 * first packet OR there are gaps or
2481f8829a4aSRandall Stewart 					 * duplicates.
2482f8829a4aSRandall Stewart 					 */
2483ad81507eSRandall Stewart 					(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2484830d754dSRandall Stewart 					/*
2485830d754dSRandall Stewart 					 * EY if nr_sacks used then send an
2486830d754dSRandall Stewart 					 * nr-sack , a sack otherwise
2487830d754dSRandall Stewart 					 */
2488830d754dSRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack)
2489830d754dSRandall Stewart 						sctp_send_nr_sack(stcb);
2490830d754dSRandall Stewart 					else
2491f8829a4aSRandall Stewart 						sctp_send_sack(stcb);
2492f8829a4aSRandall Stewart 				}
2493f8829a4aSRandall Stewart 			} else {
249442551e99SRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2495f8829a4aSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2496f8829a4aSRandall Stewart 					    stcb->sctp_ep, stcb, NULL);
2497f8829a4aSRandall Stewart 				}
2498f8829a4aSRandall Stewart 			}
2499f8829a4aSRandall Stewart 		}
2500f8829a4aSRandall Stewart 	}
250142551e99SRandall Stewart }
2502f8829a4aSRandall Stewart 
2503f8829a4aSRandall Stewart void
2504f8829a4aSRandall Stewart sctp_service_queues(struct sctp_tcb *stcb, struct sctp_association *asoc)
2505f8829a4aSRandall Stewart {
2506f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
2507810ec536SMichael Tuexen 	uint32_t tsize, pd_point;
2508f8829a4aSRandall Stewart 	uint16_t nxt_todel;
2509f8829a4aSRandall Stewart 
2510f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
2511f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
2512f8829a4aSRandall Stewart 	}
2513f8829a4aSRandall Stewart 	/* Can we proceed further, i.e. the PD-API is complete */
2514f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
2515f8829a4aSRandall Stewart 		/* no */
2516f8829a4aSRandall Stewart 		return;
2517f8829a4aSRandall Stewart 	}
2518f8829a4aSRandall Stewart 	/*
2519f8829a4aSRandall Stewart 	 * Now is there some other chunk I can deliver from the reassembly
2520f8829a4aSRandall Stewart 	 * queue.
2521f8829a4aSRandall Stewart 	 */
2522139bc87fSRandall Stewart doit_again:
2523f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
2524f8829a4aSRandall Stewart 	if (chk == NULL) {
2525f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = 0;
2526f8829a4aSRandall Stewart 		asoc->cnt_on_reasm_queue = 0;
2527f8829a4aSRandall Stewart 		return;
2528f8829a4aSRandall Stewart 	}
2529f8829a4aSRandall Stewart 	nxt_todel = asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
2530f8829a4aSRandall Stewart 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
2531f8829a4aSRandall Stewart 	    ((nxt_todel == chk->rec.data.stream_seq) ||
2532f8829a4aSRandall Stewart 	    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
2533f8829a4aSRandall Stewart 		/*
2534f8829a4aSRandall Stewart 		 * Yep the first one is here. We setup to start reception,
2535f8829a4aSRandall Stewart 		 * by backing down the TSN just in case we can't deliver.
2536f8829a4aSRandall Stewart 		 */
2537f8829a4aSRandall Stewart 
2538f8829a4aSRandall Stewart 		/*
2539f8829a4aSRandall Stewart 		 * Before we start though either all of the message should
254024ae5c4aSMichael Tuexen 		 * be here or the socket buffer max or nothing on the
2541f8829a4aSRandall Stewart 		 * delivery queue and something can be delivered.
2542f8829a4aSRandall Stewart 		 */
2543810ec536SMichael Tuexen 		if (stcb->sctp_socket) {
254424ae5c4aSMichael Tuexen 			pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket),
2545810ec536SMichael Tuexen 			    stcb->sctp_ep->partial_delivery_point);
2546810ec536SMichael Tuexen 		} else {
2547810ec536SMichael Tuexen 			pd_point = stcb->sctp_ep->partial_delivery_point;
2548810ec536SMichael Tuexen 		}
2549810ec536SMichael Tuexen 		if (sctp_is_all_msg_on_reasm(asoc, &tsize) || (tsize >= pd_point)) {
2550f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 1;
2551f8829a4aSRandall Stewart 			asoc->tsn_last_delivered = chk->rec.data.TSN_seq - 1;
2552f8829a4aSRandall Stewart 			asoc->str_of_pdapi = chk->rec.data.stream_number;
2553f8829a4aSRandall Stewart 			asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
2554f8829a4aSRandall Stewart 			asoc->pdapi_ppid = chk->rec.data.payloadtype;
2555f8829a4aSRandall Stewart 			asoc->fragment_flags = chk->rec.data.rcv_flags;
2556f8829a4aSRandall Stewart 			sctp_service_reassembly(stcb, asoc);
2557139bc87fSRandall Stewart 			if (asoc->fragmented_delivery_inprogress == 0) {
2558139bc87fSRandall Stewart 				goto doit_again;
2559139bc87fSRandall Stewart 			}
2560f8829a4aSRandall Stewart 		}
2561f8829a4aSRandall Stewart 	}
2562f8829a4aSRandall Stewart }
2563f8829a4aSRandall Stewart 
2564f8829a4aSRandall Stewart int
2565f8829a4aSRandall Stewart sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length,
2566f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2567f8829a4aSRandall Stewart     struct sctp_nets *net, uint32_t * high_tsn)
2568f8829a4aSRandall Stewart {
2569f8829a4aSRandall Stewart 	struct sctp_data_chunk *ch, chunk_buf;
2570f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2571f8829a4aSRandall Stewart 	int num_chunks = 0;	/* number of control chunks processed */
2572f8829a4aSRandall Stewart 	int stop_proc = 0;
2573f8829a4aSRandall Stewart 	int chk_length, break_flag, last_chunk;
2574f8829a4aSRandall Stewart 	int abort_flag = 0, was_a_gap = 0;
2575f8829a4aSRandall Stewart 	struct mbuf *m;
2576f8829a4aSRandall Stewart 
2577f8829a4aSRandall Stewart 	/* set the rwnd */
2578f8829a4aSRandall Stewart 	sctp_set_rwnd(stcb, &stcb->asoc);
2579f8829a4aSRandall Stewart 
2580f8829a4aSRandall Stewart 	m = *mm;
2581f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
2582f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2583f8829a4aSRandall Stewart 	if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
2584f8829a4aSRandall Stewart 	    stcb->asoc.cumulative_tsn, MAX_TSN)) {
2585f8829a4aSRandall Stewart 		/* there was a gap before this data was processed */
2586f8829a4aSRandall Stewart 		was_a_gap = 1;
2587f8829a4aSRandall Stewart 	}
2588f8829a4aSRandall Stewart 	/*
2589f8829a4aSRandall Stewart 	 * setup where we got the last DATA packet from for any SACK that
2590f8829a4aSRandall Stewart 	 * may need to go out. Don't bump the net. This is done ONLY when a
2591f8829a4aSRandall Stewart 	 * chunk is assigned.
2592f8829a4aSRandall Stewart 	 */
2593f8829a4aSRandall Stewart 	asoc->last_data_chunk_from = net;
2594f8829a4aSRandall Stewart 
2595d06c82f1SRandall Stewart 	/*-
2596f8829a4aSRandall Stewart 	 * Now before we proceed we must figure out if this is a wasted
2597f8829a4aSRandall Stewart 	 * cluster... i.e. it is a small packet sent in and yet the driver
2598f8829a4aSRandall Stewart 	 * underneath allocated a full cluster for it. If so we must copy it
2599f8829a4aSRandall Stewart 	 * to a smaller mbuf and free up the cluster mbuf. This will help
2600d06c82f1SRandall Stewart 	 * with cluster starvation. Note for __Panda__ we don't do this
2601d06c82f1SRandall Stewart 	 * since it has clusters all the way down to 64 bytes.
2602f8829a4aSRandall Stewart 	 */
260344b7479bSRandall Stewart 	if (SCTP_BUF_LEN(m) < (long)MLEN && SCTP_BUF_NEXT(m) == NULL) {
2604f8829a4aSRandall Stewart 		/* we only handle mbufs that are singletons.. not chains */
2605139bc87fSRandall Stewart 		m = sctp_get_mbuf_for_msg(SCTP_BUF_LEN(m), 0, M_DONTWAIT, 1, MT_DATA);
2606f8829a4aSRandall Stewart 		if (m) {
2607f8829a4aSRandall Stewart 			/* ok lets see if we can copy the data up */
2608f8829a4aSRandall Stewart 			caddr_t *from, *to;
2609f8829a4aSRandall Stewart 
2610f8829a4aSRandall Stewart 			/* get the pointers and copy */
2611f8829a4aSRandall Stewart 			to = mtod(m, caddr_t *);
2612f8829a4aSRandall Stewart 			from = mtod((*mm), caddr_t *);
2613139bc87fSRandall Stewart 			memcpy(to, from, SCTP_BUF_LEN((*mm)));
2614f8829a4aSRandall Stewart 			/* copy the length and free up the old */
2615139bc87fSRandall Stewart 			SCTP_BUF_LEN(m) = SCTP_BUF_LEN((*mm));
2616f8829a4aSRandall Stewart 			sctp_m_freem(*mm);
2617f8829a4aSRandall Stewart 			/* sucess, back copy */
2618f8829a4aSRandall Stewart 			*mm = m;
2619f8829a4aSRandall Stewart 		} else {
2620f8829a4aSRandall Stewart 			/* We are in trouble in the mbuf world .. yikes */
2621f8829a4aSRandall Stewart 			m = *mm;
2622f8829a4aSRandall Stewart 		}
2623f8829a4aSRandall Stewart 	}
2624f8829a4aSRandall Stewart 	/* get pointer to the first chunk header */
2625f8829a4aSRandall Stewart 	ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2626f8829a4aSRandall Stewart 	    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2627f8829a4aSRandall Stewart 	if (ch == NULL) {
2628f8829a4aSRandall Stewart 		return (1);
2629f8829a4aSRandall Stewart 	}
2630f8829a4aSRandall Stewart 	/*
2631f8829a4aSRandall Stewart 	 * process all DATA chunks...
2632f8829a4aSRandall Stewart 	 */
2633f8829a4aSRandall Stewart 	*high_tsn = asoc->cumulative_tsn;
2634f8829a4aSRandall Stewart 	break_flag = 0;
263542551e99SRandall Stewart 	asoc->data_pkts_seen++;
2636f8829a4aSRandall Stewart 	while (stop_proc == 0) {
2637f8829a4aSRandall Stewart 		/* validate chunk length */
2638f8829a4aSRandall Stewart 		chk_length = ntohs(ch->ch.chunk_length);
2639f8829a4aSRandall Stewart 		if (length - *offset < chk_length) {
2640f8829a4aSRandall Stewart 			/* all done, mutulated chunk */
2641f8829a4aSRandall Stewart 			stop_proc = 1;
2642f8829a4aSRandall Stewart 			break;
2643f8829a4aSRandall Stewart 		}
2644f8829a4aSRandall Stewart 		if (ch->ch.chunk_type == SCTP_DATA) {
2645f8829a4aSRandall Stewart 			if ((size_t)chk_length < sizeof(struct sctp_data_chunk) + 1) {
2646f8829a4aSRandall Stewart 				/*
2647f8829a4aSRandall Stewart 				 * Need to send an abort since we had a
2648f8829a4aSRandall Stewart 				 * invalid data chunk.
2649f8829a4aSRandall Stewart 				 */
2650f8829a4aSRandall Stewart 				struct mbuf *op_err;
2651f8829a4aSRandall Stewart 
2652139bc87fSRandall Stewart 				op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 2 * sizeof(uint32_t)),
2653f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
2654f8829a4aSRandall Stewart 
2655f8829a4aSRandall Stewart 				if (op_err) {
2656f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
2657f8829a4aSRandall Stewart 					uint32_t *ippp;
2658f8829a4aSRandall Stewart 
2659139bc87fSRandall Stewart 					SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr) +
2660f8829a4aSRandall Stewart 					    (2 * sizeof(uint32_t));
2661f8829a4aSRandall Stewart 					ph = mtod(op_err, struct sctp_paramhdr *);
2662f8829a4aSRandall Stewart 					ph->param_type =
2663f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2664139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(op_err));
2665f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
2666a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_19);
2667f8829a4aSRandall Stewart 					ippp++;
2668f8829a4aSRandall Stewart 					*ippp = asoc->cumulative_tsn;
2669f8829a4aSRandall Stewart 
2670f8829a4aSRandall Stewart 				}
2671a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_19;
2672f8829a4aSRandall Stewart 				sctp_abort_association(inp, stcb, m, iphlen, sh,
2673c54a18d2SRandall Stewart 				    op_err, 0, net->port);
2674f8829a4aSRandall Stewart 				return (2);
2675f8829a4aSRandall Stewart 			}
2676f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
2677f8829a4aSRandall Stewart 			sctp_audit_log(0xB1, 0);
2678f8829a4aSRandall Stewart #endif
2679f8829a4aSRandall Stewart 			if (SCTP_SIZE32(chk_length) == (length - *offset)) {
2680f8829a4aSRandall Stewart 				last_chunk = 1;
2681f8829a4aSRandall Stewart 			} else {
2682f8829a4aSRandall Stewart 				last_chunk = 0;
2683f8829a4aSRandall Stewart 			}
2684f8829a4aSRandall Stewart 			if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset, ch,
2685f8829a4aSRandall Stewart 			    chk_length, net, high_tsn, &abort_flag, &break_flag,
2686f8829a4aSRandall Stewart 			    last_chunk)) {
2687f8829a4aSRandall Stewart 				num_chunks++;
2688f8829a4aSRandall Stewart 			}
2689f8829a4aSRandall Stewart 			if (abort_flag)
2690f8829a4aSRandall Stewart 				return (2);
2691f8829a4aSRandall Stewart 
2692f8829a4aSRandall Stewart 			if (break_flag) {
2693f8829a4aSRandall Stewart 				/*
2694f8829a4aSRandall Stewart 				 * Set because of out of rwnd space and no
2695f8829a4aSRandall Stewart 				 * drop rep space left.
2696f8829a4aSRandall Stewart 				 */
2697f8829a4aSRandall Stewart 				stop_proc = 1;
2698f8829a4aSRandall Stewart 				break;
2699f8829a4aSRandall Stewart 			}
2700f8829a4aSRandall Stewart 		} else {
2701f8829a4aSRandall Stewart 			/* not a data chunk in the data region */
2702f8829a4aSRandall Stewart 			switch (ch->ch.chunk_type) {
2703f8829a4aSRandall Stewart 			case SCTP_INITIATION:
2704f8829a4aSRandall Stewart 			case SCTP_INITIATION_ACK:
2705f8829a4aSRandall Stewart 			case SCTP_SELECTIVE_ACK:
2706830d754dSRandall Stewart 			case SCTP_NR_SELECTIVE_ACK:	/* EY */
2707f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_REQUEST:
2708f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_ACK:
2709f8829a4aSRandall Stewart 			case SCTP_ABORT_ASSOCIATION:
2710f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN:
2711f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_ACK:
2712f8829a4aSRandall Stewart 			case SCTP_OPERATION_ERROR:
2713f8829a4aSRandall Stewart 			case SCTP_COOKIE_ECHO:
2714f8829a4aSRandall Stewart 			case SCTP_COOKIE_ACK:
2715f8829a4aSRandall Stewart 			case SCTP_ECN_ECHO:
2716f8829a4aSRandall Stewart 			case SCTP_ECN_CWR:
2717f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_COMPLETE:
2718f8829a4aSRandall Stewart 			case SCTP_AUTHENTICATION:
2719f8829a4aSRandall Stewart 			case SCTP_ASCONF_ACK:
2720f8829a4aSRandall Stewart 			case SCTP_PACKET_DROPPED:
2721f8829a4aSRandall Stewart 			case SCTP_STREAM_RESET:
2722f8829a4aSRandall Stewart 			case SCTP_FORWARD_CUM_TSN:
2723f8829a4aSRandall Stewart 			case SCTP_ASCONF:
2724f8829a4aSRandall Stewart 				/*
2725f8829a4aSRandall Stewart 				 * Now, what do we do with KNOWN chunks that
2726f8829a4aSRandall Stewart 				 * are NOT in the right place?
2727f8829a4aSRandall Stewart 				 *
2728f8829a4aSRandall Stewart 				 * For now, I do nothing but ignore them. We
2729f8829a4aSRandall Stewart 				 * may later want to add sysctl stuff to
2730f8829a4aSRandall Stewart 				 * switch out and do either an ABORT() or
2731f8829a4aSRandall Stewart 				 * possibly process them.
2732f8829a4aSRandall Stewart 				 */
2733b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_strict_data_order)) {
2734f8829a4aSRandall Stewart 					struct mbuf *op_err;
2735f8829a4aSRandall Stewart 
2736f8829a4aSRandall Stewart 					op_err = sctp_generate_invmanparam(SCTP_CAUSE_PROTOCOL_VIOLATION);
2737c54a18d2SRandall Stewart 					sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 0, net->port);
2738f8829a4aSRandall Stewart 					return (2);
2739f8829a4aSRandall Stewart 				}
2740f8829a4aSRandall Stewart 				break;
2741f8829a4aSRandall Stewart 			default:
2742f8829a4aSRandall Stewart 				/* unknown chunk type, use bit rules */
2743f8829a4aSRandall Stewart 				if (ch->ch.chunk_type & 0x40) {
2744f8829a4aSRandall Stewart 					/* Add a error report to the queue */
2745d61a0ae0SRandall Stewart 					struct mbuf *merr;
2746f8829a4aSRandall Stewart 					struct sctp_paramhdr *phd;
2747f8829a4aSRandall Stewart 
2748d61a0ae0SRandall Stewart 					merr = sctp_get_mbuf_for_msg(sizeof(*phd), 0, M_DONTWAIT, 1, MT_DATA);
2749d61a0ae0SRandall Stewart 					if (merr) {
2750d61a0ae0SRandall Stewart 						phd = mtod(merr, struct sctp_paramhdr *);
2751f8829a4aSRandall Stewart 						/*
2752f8829a4aSRandall Stewart 						 * We cheat and use param
2753f8829a4aSRandall Stewart 						 * type since we did not
2754f8829a4aSRandall Stewart 						 * bother to define a error
2755f8829a4aSRandall Stewart 						 * cause struct. They are
2756f8829a4aSRandall Stewart 						 * the same basic format
2757f8829a4aSRandall Stewart 						 * with different names.
2758f8829a4aSRandall Stewart 						 */
2759f8829a4aSRandall Stewart 						phd->param_type =
2760f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_UNRECOG_CHUNK);
2761f8829a4aSRandall Stewart 						phd->param_length =
2762f8829a4aSRandall Stewart 						    htons(chk_length + sizeof(*phd));
2763d61a0ae0SRandall Stewart 						SCTP_BUF_LEN(merr) = sizeof(*phd);
2764d61a0ae0SRandall Stewart 						SCTP_BUF_NEXT(merr) = SCTP_M_COPYM(m, *offset,
2765f8829a4aSRandall Stewart 						    SCTP_SIZE32(chk_length),
2766f8829a4aSRandall Stewart 						    M_DONTWAIT);
2767d61a0ae0SRandall Stewart 						if (SCTP_BUF_NEXT(merr)) {
2768d61a0ae0SRandall Stewart 							sctp_queue_op_err(stcb, merr);
2769f8829a4aSRandall Stewart 						} else {
2770d61a0ae0SRandall Stewart 							sctp_m_freem(merr);
2771f8829a4aSRandall Stewart 						}
2772f8829a4aSRandall Stewart 					}
2773f8829a4aSRandall Stewart 				}
2774f8829a4aSRandall Stewart 				if ((ch->ch.chunk_type & 0x80) == 0) {
2775f8829a4aSRandall Stewart 					/* discard the rest of this packet */
2776f8829a4aSRandall Stewart 					stop_proc = 1;
2777f8829a4aSRandall Stewart 				}	/* else skip this bad chunk and
2778f8829a4aSRandall Stewart 					 * continue... */
2779f8829a4aSRandall Stewart 				break;
2780f8829a4aSRandall Stewart 			};	/* switch of chunk type */
2781f8829a4aSRandall Stewart 		}
2782f8829a4aSRandall Stewart 		*offset += SCTP_SIZE32(chk_length);
2783f8829a4aSRandall Stewart 		if ((*offset >= length) || stop_proc) {
2784f8829a4aSRandall Stewart 			/* no more data left in the mbuf chain */
2785f8829a4aSRandall Stewart 			stop_proc = 1;
2786f8829a4aSRandall Stewart 			continue;
2787f8829a4aSRandall Stewart 		}
2788f8829a4aSRandall Stewart 		ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2789f8829a4aSRandall Stewart 		    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2790f8829a4aSRandall Stewart 		if (ch == NULL) {
2791f8829a4aSRandall Stewart 			*offset = length;
2792f8829a4aSRandall Stewart 			stop_proc = 1;
2793f8829a4aSRandall Stewart 			break;
2794f8829a4aSRandall Stewart 
2795f8829a4aSRandall Stewart 		}
2796f8829a4aSRandall Stewart 	}			/* while */
2797f8829a4aSRandall Stewart 	if (break_flag) {
2798f8829a4aSRandall Stewart 		/*
2799f8829a4aSRandall Stewart 		 * we need to report rwnd overrun drops.
2800f8829a4aSRandall Stewart 		 */
2801f8829a4aSRandall Stewart 		sctp_send_packet_dropped(stcb, net, *mm, iphlen, 0);
2802f8829a4aSRandall Stewart 	}
2803f8829a4aSRandall Stewart 	if (num_chunks) {
2804f8829a4aSRandall Stewart 		/*
2805ceaad40aSRandall Stewart 		 * Did we get data, if so update the time for auto-close and
2806f8829a4aSRandall Stewart 		 * give peer credit for being alive.
2807f8829a4aSRandall Stewart 		 */
2808f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvpktwithdata);
2809b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
2810c4739e2fSRandall Stewart 			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2811c4739e2fSRandall Stewart 			    stcb->asoc.overall_error_count,
2812c4739e2fSRandall Stewart 			    0,
2813c4739e2fSRandall Stewart 			    SCTP_FROM_SCTP_INDATA,
2814c4739e2fSRandall Stewart 			    __LINE__);
2815c4739e2fSRandall Stewart 		}
2816f8829a4aSRandall Stewart 		stcb->asoc.overall_error_count = 0;
28176e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
2818f8829a4aSRandall Stewart 	}
2819f8829a4aSRandall Stewart 	/* now service all of the reassm queue if needed */
2820f8829a4aSRandall Stewart 	if (!(TAILQ_EMPTY(&asoc->reasmqueue)))
2821f8829a4aSRandall Stewart 		sctp_service_queues(stcb, asoc);
2822f8829a4aSRandall Stewart 
2823f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
282442551e99SRandall Stewart 		/* Assure that we ack right away */
282542551e99SRandall Stewart 		stcb->asoc.send_sack = 1;
2826f8829a4aSRandall Stewart 	}
2827f8829a4aSRandall Stewart 	/* Start a sack timer or QUEUE a SACK for sending */
2828f8829a4aSRandall Stewart 	if ((stcb->asoc.cumulative_tsn == stcb->asoc.highest_tsn_inside_map) &&
282942551e99SRandall Stewart 	    (stcb->asoc.mapping_array[0] != 0xff)) {
283042551e99SRandall Stewart 		if ((stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq) ||
283142551e99SRandall Stewart 		    (stcb->asoc.delayed_ack == 0) ||
2832b201f536SRandall Stewart 		    (stcb->asoc.numduptsns) ||
283342551e99SRandall Stewart 		    (stcb->asoc.send_sack == 1)) {
2834139bc87fSRandall Stewart 			if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2835ad81507eSRandall Stewart 				(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
283642551e99SRandall Stewart 			}
2837830d754dSRandall Stewart 			/*
2838830d754dSRandall Stewart 			 * EY if nr_sacks used then send an nr-sack , a sack
2839830d754dSRandall Stewart 			 * otherwise
2840830d754dSRandall Stewart 			 */
2841830d754dSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack)
2842830d754dSRandall Stewart 				sctp_send_nr_sack(stcb);
2843830d754dSRandall Stewart 			else
2844f8829a4aSRandall Stewart 				sctp_send_sack(stcb);
2845f8829a4aSRandall Stewart 		} else {
284642551e99SRandall Stewart 			if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2847f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2848f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, NULL);
2849f8829a4aSRandall Stewart 			}
2850f8829a4aSRandall Stewart 		}
2851f8829a4aSRandall Stewart 	} else {
2852f8829a4aSRandall Stewart 		sctp_sack_check(stcb, 1, was_a_gap, &abort_flag);
2853f8829a4aSRandall Stewart 	}
2854f8829a4aSRandall Stewart 	if (abort_flag)
2855f8829a4aSRandall Stewart 		return (2);
2856f8829a4aSRandall Stewart 
2857f8829a4aSRandall Stewart 	return (0);
2858f8829a4aSRandall Stewart }
2859f8829a4aSRandall Stewart 
28600fa753b3SRandall Stewart static int
28610fa753b3SRandall Stewart sctp_process_segment_range(struct sctp_tcb *stcb, struct sctp_tmit_chunk **p_tp1, uint32_t last_tsn,
28620fa753b3SRandall Stewart     uint16_t frag_strt, uint16_t frag_end, int nr_sacking,
28630fa753b3SRandall Stewart     int *num_frs,
28640fa753b3SRandall Stewart     uint32_t * biggest_newly_acked_tsn,
28650fa753b3SRandall Stewart     uint32_t * this_sack_lowest_newack,
28660fa753b3SRandall Stewart     int *ecn_seg_sums)
28670fa753b3SRandall Stewart {
28680fa753b3SRandall Stewart 	struct sctp_tmit_chunk *tp1;
28690fa753b3SRandall Stewart 	unsigned int theTSN;
28700fa753b3SRandall Stewart 	int j, wake_him = 0;
28710fa753b3SRandall Stewart 
28720fa753b3SRandall Stewart 	/* Recover the tp1 we last saw */
28730fa753b3SRandall Stewart 	tp1 = *p_tp1;
28740fa753b3SRandall Stewart 	if (tp1 == NULL) {
28750fa753b3SRandall Stewart 		tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
28760fa753b3SRandall Stewart 	}
28770fa753b3SRandall Stewart 	for (j = frag_strt; j <= frag_end; j++) {
28780fa753b3SRandall Stewart 		theTSN = j + last_tsn;
28790fa753b3SRandall Stewart 		while (tp1) {
28800fa753b3SRandall Stewart 			if (tp1->rec.data.doing_fast_retransmit)
28810fa753b3SRandall Stewart 				(*num_frs) += 1;
28820fa753b3SRandall Stewart 
28830fa753b3SRandall Stewart 			/*-
28840fa753b3SRandall Stewart 			 * CMT: CUCv2 algorithm. For each TSN being
28850fa753b3SRandall Stewart 			 * processed from the sent queue, track the
28860fa753b3SRandall Stewart 			 * next expected pseudo-cumack, or
28870fa753b3SRandall Stewart 			 * rtx_pseudo_cumack, if required. Separate
28880fa753b3SRandall Stewart 			 * cumack trackers for first transmissions,
28890fa753b3SRandall Stewart 			 * and retransmissions.
28900fa753b3SRandall Stewart 			 */
28910fa753b3SRandall Stewart 			if ((tp1->whoTo->find_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
28920fa753b3SRandall Stewart 			    (tp1->snd_count == 1)) {
28930fa753b3SRandall Stewart 				tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
28940fa753b3SRandall Stewart 				tp1->whoTo->find_pseudo_cumack = 0;
28950fa753b3SRandall Stewart 			}
28960fa753b3SRandall Stewart 			if ((tp1->whoTo->find_rtx_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
28970fa753b3SRandall Stewart 			    (tp1->snd_count > 1)) {
28980fa753b3SRandall Stewart 				tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
28990fa753b3SRandall Stewart 				tp1->whoTo->find_rtx_pseudo_cumack = 0;
29000fa753b3SRandall Stewart 			}
29010fa753b3SRandall Stewart 			if (tp1->rec.data.TSN_seq == theTSN) {
29020fa753b3SRandall Stewart 				if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
29030fa753b3SRandall Stewart 					/*-
29040fa753b3SRandall Stewart 					 * must be held until
29050fa753b3SRandall Stewart 					 * cum-ack passes
29060fa753b3SRandall Stewart 					 */
29070fa753b3SRandall Stewart 					/*-
29080fa753b3SRandall Stewart 					 * ECN Nonce: Add the nonce
29090fa753b3SRandall Stewart 					 * value to the sender's
29100fa753b3SRandall Stewart 					 * nonce sum
29110fa753b3SRandall Stewart 					 */
29120fa753b3SRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
29130fa753b3SRandall Stewart 						/*-
29140fa753b3SRandall Stewart 						 * If it is less than RESEND, it is
29150fa753b3SRandall Stewart 						 * now no-longer in flight.
29160fa753b3SRandall Stewart 						 * Higher values may already be set
29170fa753b3SRandall Stewart 						 * via previous Gap Ack Blocks...
29180fa753b3SRandall Stewart 						 * i.e. ACKED or RESEND.
29190fa753b3SRandall Stewart 						 */
29200fa753b3SRandall Stewart 						if (compare_with_wrap(tp1->rec.data.TSN_seq,
29210fa753b3SRandall Stewart 						    *biggest_newly_acked_tsn, MAX_TSN)) {
29220fa753b3SRandall Stewart 							*biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
29230fa753b3SRandall Stewart 						}
29240fa753b3SRandall Stewart 						/*-
29250fa753b3SRandall Stewart 						 * CMT: SFR algo (and HTNA) - set
29260fa753b3SRandall Stewart 						 * saw_newack to 1 for dest being
29270fa753b3SRandall Stewart 						 * newly acked. update
29280fa753b3SRandall Stewart 						 * this_sack_highest_newack if
29290fa753b3SRandall Stewart 						 * appropriate.
29300fa753b3SRandall Stewart 						 */
29310fa753b3SRandall Stewart 						if (tp1->rec.data.chunk_was_revoked == 0)
29320fa753b3SRandall Stewart 							tp1->whoTo->saw_newack = 1;
29330fa753b3SRandall Stewart 
29340fa753b3SRandall Stewart 						if (compare_with_wrap(tp1->rec.data.TSN_seq,
29350fa753b3SRandall Stewart 						    tp1->whoTo->this_sack_highest_newack,
29360fa753b3SRandall Stewart 						    MAX_TSN)) {
29370fa753b3SRandall Stewart 							tp1->whoTo->this_sack_highest_newack =
29380fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq;
29390fa753b3SRandall Stewart 						}
29400fa753b3SRandall Stewart 						/*-
29410fa753b3SRandall Stewart 						 * CMT DAC algo: also update
29420fa753b3SRandall Stewart 						 * this_sack_lowest_newack
29430fa753b3SRandall Stewart 						 */
29440fa753b3SRandall Stewart 						if (*this_sack_lowest_newack == 0) {
29450fa753b3SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
29460fa753b3SRandall Stewart 								sctp_log_sack(*this_sack_lowest_newack,
29470fa753b3SRandall Stewart 								    last_tsn,
29480fa753b3SRandall Stewart 								    tp1->rec.data.TSN_seq,
29490fa753b3SRandall Stewart 								    0,
29500fa753b3SRandall Stewart 								    0,
29510fa753b3SRandall Stewart 								    SCTP_LOG_TSN_ACKED);
29520fa753b3SRandall Stewart 							}
29530fa753b3SRandall Stewart 							*this_sack_lowest_newack = tp1->rec.data.TSN_seq;
29540fa753b3SRandall Stewart 						}
29550fa753b3SRandall Stewart 						/*-
29560fa753b3SRandall Stewart 						 * CMT: CUCv2 algorithm. If (rtx-)pseudo-cumack for corresp
29570fa753b3SRandall Stewart 						 * dest is being acked, then we have a new (rtx-)pseudo-cumack. Set
29580fa753b3SRandall Stewart 						 * new_(rtx_)pseudo_cumack to TRUE so that the cwnd for this dest can be
29590fa753b3SRandall Stewart 						 * updated. Also trigger search for the next expected (rtx-)pseudo-cumack.
29600fa753b3SRandall Stewart 						 * Separate pseudo_cumack trackers for first transmissions and
29610fa753b3SRandall Stewart 						 * retransmissions.
29620fa753b3SRandall Stewart 						 */
29630fa753b3SRandall Stewart 						if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
29640fa753b3SRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0) {
29650fa753b3SRandall Stewart 								tp1->whoTo->new_pseudo_cumack = 1;
29660fa753b3SRandall Stewart 							}
29670fa753b3SRandall Stewart 							tp1->whoTo->find_pseudo_cumack = 1;
29680fa753b3SRandall Stewart 						}
29690fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
29700fa753b3SRandall Stewart 							sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
29710fa753b3SRandall Stewart 						}
29720fa753b3SRandall Stewart 						if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
29730fa753b3SRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0) {
29740fa753b3SRandall Stewart 								tp1->whoTo->new_pseudo_cumack = 1;
29750fa753b3SRandall Stewart 							}
29760fa753b3SRandall Stewart 							tp1->whoTo->find_rtx_pseudo_cumack = 1;
29770fa753b3SRandall Stewart 						}
29780fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
29790fa753b3SRandall Stewart 							sctp_log_sack(*biggest_newly_acked_tsn,
29800fa753b3SRandall Stewart 							    last_tsn,
29810fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq,
29820fa753b3SRandall Stewart 							    frag_strt,
29830fa753b3SRandall Stewart 							    frag_end,
29840fa753b3SRandall Stewart 							    SCTP_LOG_TSN_ACKED);
29850fa753b3SRandall Stewart 						}
29860fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
29870fa753b3SRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
29880fa753b3SRandall Stewart 							    tp1->whoTo->flight_size,
29890fa753b3SRandall Stewart 							    tp1->book_size,
29900fa753b3SRandall Stewart 							    (uintptr_t) tp1->whoTo,
29910fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq);
29920fa753b3SRandall Stewart 						}
29930fa753b3SRandall Stewart 						sctp_flight_size_decrease(tp1);
29940fa753b3SRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
29950fa753b3SRandall Stewart 
29960fa753b3SRandall Stewart 						tp1->whoTo->net_ack += tp1->send_size;
29970fa753b3SRandall Stewart 						if (tp1->snd_count < 2) {
29980fa753b3SRandall Stewart 							/*-
29990fa753b3SRandall Stewart 							 * True non-retransmited chunk
30000fa753b3SRandall Stewart 							 */
30010fa753b3SRandall Stewart 							tp1->whoTo->net_ack2 += tp1->send_size;
30020fa753b3SRandall Stewart 
30030fa753b3SRandall Stewart 							/*-
30040fa753b3SRandall Stewart 							 * update RTO too ?
30050fa753b3SRandall Stewart 							 */
30060fa753b3SRandall Stewart 							if (tp1->do_rtt) {
30070fa753b3SRandall Stewart 								tp1->whoTo->RTO =
30080fa753b3SRandall Stewart 								    sctp_calculate_rto(stcb,
30090fa753b3SRandall Stewart 								    &stcb->asoc,
30100fa753b3SRandall Stewart 								    tp1->whoTo,
30110fa753b3SRandall Stewart 								    &tp1->sent_rcv_time,
30120fa753b3SRandall Stewart 								    sctp_align_safe_nocopy);
30130fa753b3SRandall Stewart 								tp1->do_rtt = 0;
30140fa753b3SRandall Stewart 							}
30150fa753b3SRandall Stewart 						}
30160fa753b3SRandall Stewart 					}
30170fa753b3SRandall Stewart 					if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
30180fa753b3SRandall Stewart 						(*ecn_seg_sums) += tp1->rec.data.ect_nonce;
30190fa753b3SRandall Stewart 						(*ecn_seg_sums) &= SCTP_SACK_NONCE_SUM;
30200fa753b3SRandall Stewart 						if (compare_with_wrap(tp1->rec.data.TSN_seq,
30210fa753b3SRandall Stewart 						    stcb->asoc.this_sack_highest_gap,
30220fa753b3SRandall Stewart 						    MAX_TSN)) {
30230fa753b3SRandall Stewart 							stcb->asoc.this_sack_highest_gap =
30240fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq;
30250fa753b3SRandall Stewart 						}
30260fa753b3SRandall Stewart 						if (tp1->sent == SCTP_DATAGRAM_RESEND) {
30270fa753b3SRandall Stewart 							sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
30280fa753b3SRandall Stewart #ifdef SCTP_AUDITING_ENABLED
30290fa753b3SRandall Stewart 							sctp_audit_log(0xB2,
30300fa753b3SRandall Stewart 							    (stcb->asoc.sent_queue_retran_cnt & 0x000000ff));
30310fa753b3SRandall Stewart #endif
30320fa753b3SRandall Stewart 						}
30330fa753b3SRandall Stewart 					}
30340fa753b3SRandall Stewart 					/*-
30350fa753b3SRandall Stewart 					 * All chunks NOT UNSENT fall through here and are marked
30360fa753b3SRandall Stewart 					 * (leave PR-SCTP ones that are to skip alone though)
30370fa753b3SRandall Stewart 					 */
30380fa753b3SRandall Stewart 					if (tp1->sent != SCTP_FORWARD_TSN_SKIP)
30390fa753b3SRandall Stewart 						tp1->sent = SCTP_DATAGRAM_MARKED;
30400fa753b3SRandall Stewart 
30410fa753b3SRandall Stewart 					if (tp1->rec.data.chunk_was_revoked) {
30420fa753b3SRandall Stewart 						/* deflate the cwnd */
30430fa753b3SRandall Stewart 						tp1->whoTo->cwnd -= tp1->book_size;
30440fa753b3SRandall Stewart 						tp1->rec.data.chunk_was_revoked = 0;
30450fa753b3SRandall Stewart 					}
30460fa753b3SRandall Stewart 					/* NR Sack code here */
30470fa753b3SRandall Stewart 					if (nr_sacking) {
30480fa753b3SRandall Stewart 						if (tp1->sent != SCTP_FORWARD_TSN_SKIP)
30490fa753b3SRandall Stewart 							tp1->sent = SCTP_DATAGRAM_NR_MARKED;
30500fa753b3SRandall Stewart 						/*
30510fa753b3SRandall Stewart 						 * TAILQ_REMOVE(&asoc->sent_q
30520fa753b3SRandall Stewart 						 * ueue, tp1, sctp_next);
30530fa753b3SRandall Stewart 						 */
30540fa753b3SRandall Stewart 						if (tp1->data) {
30550fa753b3SRandall Stewart 							/*
30560fa753b3SRandall Stewart 							 * sa_ignore
30570fa753b3SRandall Stewart 							 * NO_NULL_CHK
30580fa753b3SRandall Stewart 							 */
30590fa753b3SRandall Stewart 							sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
30600fa753b3SRandall Stewart 							sctp_m_freem(tp1->data);
30610fa753b3SRandall Stewart 						}
30620fa753b3SRandall Stewart 						tp1->data = NULL;
30630fa753b3SRandall Stewart 						/* asoc->sent_queue_cnt--; */
30640fa753b3SRandall Stewart 						/*
30650fa753b3SRandall Stewart 						 * sctp_free_a_chunk(stcb,
30660fa753b3SRandall Stewart 						 * tp1);
30670fa753b3SRandall Stewart 						 */
30680fa753b3SRandall Stewart 						wake_him++;
30690fa753b3SRandall Stewart 					}
30700fa753b3SRandall Stewart 				}
30710fa753b3SRandall Stewart 				break;
30720fa753b3SRandall Stewart 			}	/* if (tp1->TSN_seq == theTSN) */
30730fa753b3SRandall Stewart 			if (compare_with_wrap(tp1->rec.data.TSN_seq, theTSN,
30740fa753b3SRandall Stewart 			    MAX_TSN))
30750fa753b3SRandall Stewart 				break;
30760fa753b3SRandall Stewart 
30770fa753b3SRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
30780fa753b3SRandall Stewart 		}		/* end while (tp1) */
30790fa753b3SRandall Stewart 		/* In case the fragments were not in order we must reset */
30800fa753b3SRandall Stewart 		if (tp1 == NULL) {
30810fa753b3SRandall Stewart 			tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
30820fa753b3SRandall Stewart 		}
30830fa753b3SRandall Stewart 	}			/* end for (j = fragStart */
30840fa753b3SRandall Stewart 	*p_tp1 = tp1;
30850fa753b3SRandall Stewart 	return (wake_him);	/* Return value only used for nr-sack */
30860fa753b3SRandall Stewart }
30870fa753b3SRandall Stewart 
30880fa753b3SRandall Stewart 
3089cd554309SMichael Tuexen static int
3090458303daSRandall Stewart sctp_handle_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
3091cd554309SMichael Tuexen     uint32_t last_tsn, uint32_t * biggest_tsn_acked,
3092139bc87fSRandall Stewart     uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
3093cd554309SMichael Tuexen     int num_seg, int num_nr_seg, int *ecn_seg_sums)
3094f8829a4aSRandall Stewart {
3095458303daSRandall Stewart 	struct sctp_gap_ack_block *frag, block;
3096f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
30970fa753b3SRandall Stewart 	int i;
3098f8829a4aSRandall Stewart 	int num_frs = 0;
3099cd554309SMichael Tuexen 	int chunk_freed;
3100cd554309SMichael Tuexen 	int non_revocable;
3101cd554309SMichael Tuexen 	uint16_t frag_strt, frag_end;
3102cd554309SMichael Tuexen 	uint32_t last_frag_high;
3103f8829a4aSRandall Stewart 
3104cd554309SMichael Tuexen 	tp1 = NULL;
3105cd554309SMichael Tuexen 	last_frag_high = 0;
3106cd554309SMichael Tuexen 	chunk_freed = 0;
3107f8829a4aSRandall Stewart 
3108cd554309SMichael Tuexen 	for (i = 0; i < (num_seg + num_nr_seg); i++) {
3109458303daSRandall Stewart 		frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
3110458303daSRandall Stewart 		    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
3111458303daSRandall Stewart 		*offset += sizeof(block);
3112458303daSRandall Stewart 		if (frag == NULL) {
3113cd554309SMichael Tuexen 			return (chunk_freed);
3114458303daSRandall Stewart 		}
3115f8829a4aSRandall Stewart 		frag_strt = ntohs(frag->start);
3116f8829a4aSRandall Stewart 		frag_end = ntohs(frag->end);
3117830d754dSRandall Stewart 		/* some sanity checks on the fragment offsets */
3118f8829a4aSRandall Stewart 		if (frag_strt > frag_end) {
3119f8829a4aSRandall Stewart 			/* this one is malformed, skip */
3120f8829a4aSRandall Stewart 			continue;
3121f8829a4aSRandall Stewart 		}
3122f8829a4aSRandall Stewart 		if (compare_with_wrap((frag_end + last_tsn), *biggest_tsn_acked,
3123f8829a4aSRandall Stewart 		    MAX_TSN))
3124f8829a4aSRandall Stewart 			*biggest_tsn_acked = frag_end + last_tsn;
3125f8829a4aSRandall Stewart 
3126f8829a4aSRandall Stewart 		/* mark acked dgs and find out the highestTSN being acked */
3127f8829a4aSRandall Stewart 		if (tp1 == NULL) {
3128f8829a4aSRandall Stewart 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
3129f8829a4aSRandall Stewart 			/* save the locations of the last frags */
3130f8829a4aSRandall Stewart 			last_frag_high = frag_end + last_tsn;
3131f8829a4aSRandall Stewart 		} else {
3132f8829a4aSRandall Stewart 			/*
3133f8829a4aSRandall Stewart 			 * now lets see if we need to reset the queue due to
3134f8829a4aSRandall Stewart 			 * a out-of-order SACK fragment
3135f8829a4aSRandall Stewart 			 */
3136f8829a4aSRandall Stewart 			if (compare_with_wrap(frag_strt + last_tsn,
3137f8829a4aSRandall Stewart 			    last_frag_high, MAX_TSN)) {
3138f8829a4aSRandall Stewart 				/*
3139f8829a4aSRandall Stewart 				 * if the new frag starts after the last TSN
3140f8829a4aSRandall Stewart 				 * frag covered, we are ok and this one is
3141f8829a4aSRandall Stewart 				 * beyond the last one
3142f8829a4aSRandall Stewart 				 */
3143f8829a4aSRandall Stewart 				;
3144f8829a4aSRandall Stewart 			} else {
3145f8829a4aSRandall Stewart 				/*
3146f8829a4aSRandall Stewart 				 * ok, they have reset us, so we need to
3147f8829a4aSRandall Stewart 				 * reset the queue this will cause extra
3148f8829a4aSRandall Stewart 				 * hunting but hey, they chose the
3149f8829a4aSRandall Stewart 				 * performance hit when they failed to order
3150830d754dSRandall Stewart 				 * their gaps
3151f8829a4aSRandall Stewart 				 */
3152f8829a4aSRandall Stewart 				tp1 = TAILQ_FIRST(&asoc->sent_queue);
3153f8829a4aSRandall Stewart 			}
3154f8829a4aSRandall Stewart 			last_frag_high = frag_end + last_tsn;
3155f8829a4aSRandall Stewart 		}
3156cd554309SMichael Tuexen 		if (i < num_seg) {
3157cd554309SMichael Tuexen 			non_revocable = 0;
3158cd554309SMichael Tuexen 		} else {
3159cd554309SMichael Tuexen 			non_revocable = 1;
3160cd554309SMichael Tuexen 		}
3161cd554309SMichael Tuexen 		if (sctp_process_segment_range(stcb, &tp1, last_tsn, frag_strt, frag_end,
3162cd554309SMichael Tuexen 		    non_revocable, &num_frs, biggest_newly_acked_tsn,
3163cd554309SMichael Tuexen 		    this_sack_lowest_newack, ecn_seg_sums)) {
3164cd554309SMichael Tuexen 			chunk_freed = 1;
3165458303daSRandall Stewart 		}
3166f8829a4aSRandall Stewart 	}
3167b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
316880fefe0aSRandall Stewart 		if (num_frs)
316980fefe0aSRandall Stewart 			sctp_log_fr(*biggest_tsn_acked,
317080fefe0aSRandall Stewart 			    *biggest_newly_acked_tsn,
317180fefe0aSRandall Stewart 			    last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
317280fefe0aSRandall Stewart 	}
3173cd554309SMichael Tuexen 	return (chunk_freed);
3174f8829a4aSRandall Stewart }
3175f8829a4aSRandall Stewart 
3176f8829a4aSRandall Stewart static void
3177c105859eSRandall Stewart sctp_check_for_revoked(struct sctp_tcb *stcb,
3178c105859eSRandall Stewart     struct sctp_association *asoc, uint32_t cumack,
317963eda93dSMichael Tuexen     uint32_t biggest_tsn_acked)
3180f8829a4aSRandall Stewart {
3181f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
3182f8829a4aSRandall Stewart 	int tot_revoked = 0;
3183f8829a4aSRandall Stewart 
3184f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
3185f8829a4aSRandall Stewart 	while (tp1) {
3186f8829a4aSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq, cumack,
3187f8829a4aSRandall Stewart 		    MAX_TSN)) {
3188f8829a4aSRandall Stewart 			/*
3189f8829a4aSRandall Stewart 			 * ok this guy is either ACK or MARKED. If it is
3190f8829a4aSRandall Stewart 			 * ACKED it has been previously acked but not this
3191f8829a4aSRandall Stewart 			 * time i.e. revoked.  If it is MARKED it was ACK'ed
3192f8829a4aSRandall Stewart 			 * again.
3193f8829a4aSRandall Stewart 			 */
3194d06c82f1SRandall Stewart 			if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
3195d06c82f1SRandall Stewart 			    MAX_TSN))
3196d06c82f1SRandall Stewart 				break;
3197d06c82f1SRandall Stewart 
3198d06c82f1SRandall Stewart 
3199f8829a4aSRandall Stewart 			if (tp1->sent == SCTP_DATAGRAM_ACKED) {
3200f8829a4aSRandall Stewart 				/* it has been revoked */
3201f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_SENT;
3202f8829a4aSRandall Stewart 				tp1->rec.data.chunk_was_revoked = 1;
3203a5d547adSRandall Stewart 				/*
320442551e99SRandall Stewart 				 * We must add this stuff back in to assure
320542551e99SRandall Stewart 				 * timers and such get started.
3206a5d547adSRandall Stewart 				 */
3207b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3208c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
3209c105859eSRandall Stewart 					    tp1->whoTo->flight_size,
3210c105859eSRandall Stewart 					    tp1->book_size,
3211c105859eSRandall Stewart 					    (uintptr_t) tp1->whoTo,
3212c105859eSRandall Stewart 					    tp1->rec.data.TSN_seq);
321380fefe0aSRandall Stewart 				}
3214c105859eSRandall Stewart 				sctp_flight_size_increase(tp1);
3215c105859eSRandall Stewart 				sctp_total_flight_increase(stcb, tp1);
321642551e99SRandall Stewart 				/*
321742551e99SRandall Stewart 				 * We inflate the cwnd to compensate for our
321842551e99SRandall Stewart 				 * artificial inflation of the flight_size.
321942551e99SRandall Stewart 				 */
322042551e99SRandall Stewart 				tp1->whoTo->cwnd += tp1->book_size;
3221f8829a4aSRandall Stewart 				tot_revoked++;
3222b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3223f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
3224f8829a4aSRandall Stewart 					    cumack,
3225f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
3226f8829a4aSRandall Stewart 					    0,
3227f8829a4aSRandall Stewart 					    0,
3228f8829a4aSRandall Stewart 					    SCTP_LOG_TSN_REVOKED);
322980fefe0aSRandall Stewart 				}
3230f8829a4aSRandall Stewart 			} else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
3231f8829a4aSRandall Stewart 				/* it has been re-acked in this SACK */
3232f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
3233f8829a4aSRandall Stewart 			}
3234f8829a4aSRandall Stewart 		}
3235f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT)
3236f8829a4aSRandall Stewart 			break;
3237f8829a4aSRandall Stewart 		tp1 = TAILQ_NEXT(tp1, sctp_next);
3238f8829a4aSRandall Stewart 	}
3239f8829a4aSRandall Stewart 	if (tot_revoked > 0) {
3240f8829a4aSRandall Stewart 		/*
3241f8829a4aSRandall Stewart 		 * Setup the ecn nonce re-sync point. We do this since once
3242f8829a4aSRandall Stewart 		 * data is revoked we begin to retransmit things, which do
3243f8829a4aSRandall Stewart 		 * NOT have the ECN bits set. This means we are now out of
3244f8829a4aSRandall Stewart 		 * sync and must wait until we get back in sync with the
3245f8829a4aSRandall Stewart 		 * peer to check ECN bits.
3246f8829a4aSRandall Stewart 		 */
3247f8829a4aSRandall Stewart 		tp1 = TAILQ_FIRST(&asoc->send_queue);
3248f8829a4aSRandall Stewart 		if (tp1 == NULL) {
3249f8829a4aSRandall Stewart 			asoc->nonce_resync_tsn = asoc->sending_seq;
3250f8829a4aSRandall Stewart 		} else {
3251f8829a4aSRandall Stewart 			asoc->nonce_resync_tsn = tp1->rec.data.TSN_seq;
3252f8829a4aSRandall Stewart 		}
3253f8829a4aSRandall Stewart 		asoc->nonce_wait_for_ecne = 0;
3254f8829a4aSRandall Stewart 		asoc->nonce_sum_check = 0;
3255f8829a4aSRandall Stewart 	}
3256f8829a4aSRandall Stewart }
3257f8829a4aSRandall Stewart 
3258830d754dSRandall Stewart 
3259f8829a4aSRandall Stewart static void
3260f8829a4aSRandall Stewart sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
326163eda93dSMichael Tuexen     uint32_t biggest_tsn_acked, uint32_t biggest_tsn_newly_acked, uint32_t this_sack_lowest_newack, int accum_moved)
3262f8829a4aSRandall Stewart {
3263f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
3264f8829a4aSRandall Stewart 	int strike_flag = 0;
3265f8829a4aSRandall Stewart 	struct timeval now;
3266f8829a4aSRandall Stewart 	int tot_retrans = 0;
3267f8829a4aSRandall Stewart 	uint32_t sending_seq;
3268f8829a4aSRandall Stewart 	struct sctp_nets *net;
3269f8829a4aSRandall Stewart 	int num_dests_sacked = 0;
3270f8829a4aSRandall Stewart 
3271f8829a4aSRandall Stewart 	/*
3272f8829a4aSRandall Stewart 	 * select the sending_seq, this is either the next thing ready to be
3273f8829a4aSRandall Stewart 	 * sent but not transmitted, OR, the next seq we assign.
3274f8829a4aSRandall Stewart 	 */
3275f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
3276f8829a4aSRandall Stewart 	if (tp1 == NULL) {
3277f8829a4aSRandall Stewart 		sending_seq = asoc->sending_seq;
3278f8829a4aSRandall Stewart 	} else {
3279f8829a4aSRandall Stewart 		sending_seq = tp1->rec.data.TSN_seq;
3280f8829a4aSRandall Stewart 	}
3281f8829a4aSRandall Stewart 
3282f8829a4aSRandall Stewart 	/* CMT DAC algo: finding out if SACK is a mixed SACK */
3283b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3284f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3285f8829a4aSRandall Stewart 			if (net->saw_newack)
3286f8829a4aSRandall Stewart 				num_dests_sacked++;
3287f8829a4aSRandall Stewart 		}
3288f8829a4aSRandall Stewart 	}
3289f8829a4aSRandall Stewart 	if (stcb->asoc.peer_supports_prsctp) {
32906e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
3291f8829a4aSRandall Stewart 	}
3292f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
3293f8829a4aSRandall Stewart 	while (tp1) {
3294f8829a4aSRandall Stewart 		strike_flag = 0;
3295f8829a4aSRandall Stewart 		if (tp1->no_fr_allowed) {
3296f8829a4aSRandall Stewart 			/* this one had a timeout or something */
3297f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3298f8829a4aSRandall Stewart 			continue;
3299f8829a4aSRandall Stewart 		}
3300b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3301f8829a4aSRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND)
3302f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3303f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3304f8829a4aSRandall Stewart 				    tp1->sent,
3305f8829a4aSRandall Stewart 				    SCTP_FR_LOG_CHECK_STRIKE);
330680fefe0aSRandall Stewart 		}
3307f8829a4aSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
3308f8829a4aSRandall Stewart 		    MAX_TSN) ||
3309f8829a4aSRandall Stewart 		    tp1->sent == SCTP_DATAGRAM_UNSENT) {
3310f8829a4aSRandall Stewart 			/* done */
3311f8829a4aSRandall Stewart 			break;
3312f8829a4aSRandall Stewart 		}
3313f8829a4aSRandall Stewart 		if (stcb->asoc.peer_supports_prsctp) {
3314f8829a4aSRandall Stewart 			if ((PR_SCTP_TTL_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3315f8829a4aSRandall Stewart 				/* Is it expired? */
33165e54f665SRandall Stewart 				if (
3317fc14de76SRandall Stewart 				/*
3318fc14de76SRandall Stewart 				 * TODO sctp_constants.h needs alternative
3319fc14de76SRandall Stewart 				 * time macros when _KERNEL is undefined.
3320fc14de76SRandall Stewart 				 */
33215e54f665SRandall Stewart 				    (timevalcmp(&now, &tp1->rec.data.timetodrop, >))
33225e54f665SRandall Stewart 				    ) {
3323f8829a4aSRandall Stewart 					/* Yes so drop it */
3324f8829a4aSRandall Stewart 					if (tp1->data != NULL) {
3325ad81507eSRandall Stewart 						(void)sctp_release_pr_sctp_chunk(stcb, tp1,
3326f8829a4aSRandall Stewart 						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
33270c0982b8SRandall Stewart 						    SCTP_SO_NOT_LOCKED);
3328f8829a4aSRandall Stewart 					}
3329f8829a4aSRandall Stewart 					tp1 = TAILQ_NEXT(tp1, sctp_next);
3330f8829a4aSRandall Stewart 					continue;
3331f8829a4aSRandall Stewart 				}
3332f8829a4aSRandall Stewart 			}
3333f8829a4aSRandall Stewart 		}
3334f8829a4aSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq,
3335f8829a4aSRandall Stewart 		    asoc->this_sack_highest_gap, MAX_TSN)) {
3336f8829a4aSRandall Stewart 			/* we are beyond the tsn in the sack  */
3337f8829a4aSRandall Stewart 			break;
3338f8829a4aSRandall Stewart 		}
3339f8829a4aSRandall Stewart 		if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
3340f8829a4aSRandall Stewart 			/* either a RESEND, ACKED, or MARKED */
3341f8829a4aSRandall Stewart 			/* skip */
3342f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3343f8829a4aSRandall Stewart 			continue;
3344f8829a4aSRandall Stewart 		}
3345f8829a4aSRandall Stewart 		/*
3346f8829a4aSRandall Stewart 		 * CMT : SFR algo (covers part of DAC and HTNA as well)
3347f8829a4aSRandall Stewart 		 */
3348ad81507eSRandall Stewart 		if (tp1->whoTo && tp1->whoTo->saw_newack == 0) {
3349f8829a4aSRandall Stewart 			/*
3350f8829a4aSRandall Stewart 			 * No new acks were receieved for data sent to this
3351f8829a4aSRandall Stewart 			 * dest. Therefore, according to the SFR algo for
3352f8829a4aSRandall Stewart 			 * CMT, no data sent to this dest can be marked for
3353c105859eSRandall Stewart 			 * FR using this SACK.
3354f8829a4aSRandall Stewart 			 */
3355f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3356f8829a4aSRandall Stewart 			continue;
3357ad81507eSRandall Stewart 		} else if (tp1->whoTo && compare_with_wrap(tp1->rec.data.TSN_seq,
3358f8829a4aSRandall Stewart 		    tp1->whoTo->this_sack_highest_newack, MAX_TSN)) {
3359f8829a4aSRandall Stewart 			/*
3360f8829a4aSRandall Stewart 			 * CMT: New acks were receieved for data sent to
3361f8829a4aSRandall Stewart 			 * this dest. But no new acks were seen for data
3362f8829a4aSRandall Stewart 			 * sent after tp1. Therefore, according to the SFR
3363f8829a4aSRandall Stewart 			 * algo for CMT, tp1 cannot be marked for FR using
3364f8829a4aSRandall Stewart 			 * this SACK. This step covers part of the DAC algo
3365f8829a4aSRandall Stewart 			 * and the HTNA algo as well.
3366f8829a4aSRandall Stewart 			 */
3367f8829a4aSRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3368f8829a4aSRandall Stewart 			continue;
3369f8829a4aSRandall Stewart 		}
3370f8829a4aSRandall Stewart 		/*
3371f8829a4aSRandall Stewart 		 * Here we check to see if we were have already done a FR
3372f8829a4aSRandall Stewart 		 * and if so we see if the biggest TSN we saw in the sack is
3373f8829a4aSRandall Stewart 		 * smaller than the recovery point. If so we don't strike
3374f8829a4aSRandall Stewart 		 * the tsn... otherwise we CAN strike the TSN.
3375f8829a4aSRandall Stewart 		 */
3376f8829a4aSRandall Stewart 		/*
337742551e99SRandall Stewart 		 * @@@ JRI: Check for CMT if (accum_moved &&
337842551e99SRandall Stewart 		 * asoc->fast_retran_loss_recovery && (sctp_cmt_on_off ==
337942551e99SRandall Stewart 		 * 0)) {
3380f8829a4aSRandall Stewart 		 */
338142551e99SRandall Stewart 		if (accum_moved && asoc->fast_retran_loss_recovery) {
3382f8829a4aSRandall Stewart 			/*
3383f8829a4aSRandall Stewart 			 * Strike the TSN if in fast-recovery and cum-ack
3384f8829a4aSRandall Stewart 			 * moved.
3385f8829a4aSRandall Stewart 			 */
3386b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3387f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3388f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3389f8829a4aSRandall Stewart 				    tp1->sent,
3390f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
339180fefe0aSRandall Stewart 			}
33925e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3393f8829a4aSRandall Stewart 				tp1->sent++;
33945e54f665SRandall Stewart 			}
3395b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3396f8829a4aSRandall Stewart 				/*
3397f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
3398f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
3399f8829a4aSRandall Stewart 				 * because it would have been set to the
3400f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
3401f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
3402f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
3403c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
3404c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3405c105859eSRandall Stewart 				 * indicates that two packets have been
3406c105859eSRandall Stewart 				 * received after this missing TSN.
34075e54f665SRandall Stewart 				 */
34085e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3409f8829a4aSRandall Stewart 				    compare_with_wrap(this_sack_lowest_newack, tp1->rec.data.TSN_seq, MAX_TSN)) {
3410b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3411f8829a4aSRandall Stewart 						sctp_log_fr(16 + num_dests_sacked,
3412f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3413f8829a4aSRandall Stewart 						    tp1->sent,
3414f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
341580fefe0aSRandall Stewart 					}
3416f8829a4aSRandall Stewart 					tp1->sent++;
3417f8829a4aSRandall Stewart 				}
3418f8829a4aSRandall Stewart 			}
3419b3f1ea41SRandall Stewart 		} else if ((tp1->rec.data.doing_fast_retransmit) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
3420f8829a4aSRandall Stewart 			/*
3421f8829a4aSRandall Stewart 			 * For those that have done a FR we must take
3422f8829a4aSRandall Stewart 			 * special consideration if we strike. I.e the
3423f8829a4aSRandall Stewart 			 * biggest_newly_acked must be higher than the
3424f8829a4aSRandall Stewart 			 * sending_seq at the time we did the FR.
3425f8829a4aSRandall Stewart 			 */
34265e54f665SRandall Stewart 			if (
3427f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3428f8829a4aSRandall Stewart 			/*
3429f8829a4aSRandall Stewart 			 * If FR's go to new networks, then we must only do
3430f8829a4aSRandall Stewart 			 * this for singly homed asoc's. However if the FR's
3431f8829a4aSRandall Stewart 			 * go to the same network (Armando's work) then its
3432f8829a4aSRandall Stewart 			 * ok to FR multiple times.
3433f8829a4aSRandall Stewart 			 */
34345e54f665SRandall Stewart 			    (asoc->numnets < 2)
3435f8829a4aSRandall Stewart #else
34365e54f665SRandall Stewart 			    (1)
3437f8829a4aSRandall Stewart #endif
34385e54f665SRandall Stewart 			    ) {
34395e54f665SRandall Stewart 
3440f8829a4aSRandall Stewart 				if ((compare_with_wrap(biggest_tsn_newly_acked,
3441f8829a4aSRandall Stewart 				    tp1->rec.data.fast_retran_tsn, MAX_TSN)) ||
3442f8829a4aSRandall Stewart 				    (biggest_tsn_newly_acked ==
3443f8829a4aSRandall Stewart 				    tp1->rec.data.fast_retran_tsn)) {
3444f8829a4aSRandall Stewart 					/*
3445f8829a4aSRandall Stewart 					 * Strike the TSN, since this ack is
3446f8829a4aSRandall Stewart 					 * beyond where things were when we
3447f8829a4aSRandall Stewart 					 * did a FR.
3448f8829a4aSRandall Stewart 					 */
3449b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3450f8829a4aSRandall Stewart 						sctp_log_fr(biggest_tsn_newly_acked,
3451f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3452f8829a4aSRandall Stewart 						    tp1->sent,
3453f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
345480fefe0aSRandall Stewart 					}
34555e54f665SRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3456f8829a4aSRandall Stewart 						tp1->sent++;
34575e54f665SRandall Stewart 					}
3458f8829a4aSRandall Stewart 					strike_flag = 1;
3459b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3460f8829a4aSRandall Stewart 						/*
3461f8829a4aSRandall Stewart 						 * CMT DAC algorithm: If
3462f8829a4aSRandall Stewart 						 * SACK flag is set to 0,
3463f8829a4aSRandall Stewart 						 * then lowest_newack test
3464f8829a4aSRandall Stewart 						 * will not pass because it
3465f8829a4aSRandall Stewart 						 * would have been set to
3466f8829a4aSRandall Stewart 						 * the cumack earlier. If
3467f8829a4aSRandall Stewart 						 * not already to be rtx'd,
3468f8829a4aSRandall Stewart 						 * If not a mixed sack and
3469f8829a4aSRandall Stewart 						 * if tp1 is not between two
3470f8829a4aSRandall Stewart 						 * sacked TSNs, then mark by
3471c105859eSRandall Stewart 						 * one more. NOTE that we
3472c105859eSRandall Stewart 						 * are marking by one
3473c105859eSRandall Stewart 						 * additional time since the
3474c105859eSRandall Stewart 						 * SACK DAC flag indicates
3475c105859eSRandall Stewart 						 * that two packets have
3476c105859eSRandall Stewart 						 * been received after this
3477c105859eSRandall Stewart 						 * missing TSN.
3478f8829a4aSRandall Stewart 						 */
34795e54f665SRandall Stewart 						if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
34805e54f665SRandall Stewart 						    (num_dests_sacked == 1) &&
34815e54f665SRandall Stewart 						    compare_with_wrap(this_sack_lowest_newack,
34825e54f665SRandall Stewart 						    tp1->rec.data.TSN_seq, MAX_TSN)) {
3483b3f1ea41SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3484f8829a4aSRandall Stewart 								sctp_log_fr(32 + num_dests_sacked,
3485f8829a4aSRandall Stewart 								    tp1->rec.data.TSN_seq,
3486f8829a4aSRandall Stewart 								    tp1->sent,
3487f8829a4aSRandall Stewart 								    SCTP_FR_LOG_STRIKE_CHUNK);
348880fefe0aSRandall Stewart 							}
34895e54f665SRandall Stewart 							if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3490f8829a4aSRandall Stewart 								tp1->sent++;
34915e54f665SRandall Stewart 							}
3492f8829a4aSRandall Stewart 						}
3493f8829a4aSRandall Stewart 					}
3494f8829a4aSRandall Stewart 				}
3495f8829a4aSRandall Stewart 			}
3496f8829a4aSRandall Stewart 			/*
349742551e99SRandall Stewart 			 * JRI: TODO: remove code for HTNA algo. CMT's SFR
349842551e99SRandall Stewart 			 * algo covers HTNA.
3499f8829a4aSRandall Stewart 			 */
3500f8829a4aSRandall Stewart 		} else if (compare_with_wrap(tp1->rec.data.TSN_seq,
3501f8829a4aSRandall Stewart 		    biggest_tsn_newly_acked, MAX_TSN)) {
3502f8829a4aSRandall Stewart 			/*
3503f8829a4aSRandall Stewart 			 * We don't strike these: This is the  HTNA
3504f8829a4aSRandall Stewart 			 * algorithm i.e. we don't strike If our TSN is
3505f8829a4aSRandall Stewart 			 * larger than the Highest TSN Newly Acked.
3506f8829a4aSRandall Stewart 			 */
3507f8829a4aSRandall Stewart 			;
3508f8829a4aSRandall Stewart 		} else {
3509f8829a4aSRandall Stewart 			/* Strike the TSN */
3510b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3511f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3512f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3513f8829a4aSRandall Stewart 				    tp1->sent,
3514f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
351580fefe0aSRandall Stewart 			}
35165e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3517f8829a4aSRandall Stewart 				tp1->sent++;
35185e54f665SRandall Stewart 			}
3519b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3520f8829a4aSRandall Stewart 				/*
3521f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
3522f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
3523f8829a4aSRandall Stewart 				 * because it would have been set to the
3524f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
3525f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
3526f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
3527c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
3528c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3529c105859eSRandall Stewart 				 * indicates that two packets have been
3530c105859eSRandall Stewart 				 * received after this missing TSN.
3531f8829a4aSRandall Stewart 				 */
35325e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3533f8829a4aSRandall Stewart 				    compare_with_wrap(this_sack_lowest_newack, tp1->rec.data.TSN_seq, MAX_TSN)) {
3534b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3535f8829a4aSRandall Stewart 						sctp_log_fr(48 + num_dests_sacked,
3536f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3537f8829a4aSRandall Stewart 						    tp1->sent,
3538f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
353980fefe0aSRandall Stewart 					}
3540f8829a4aSRandall Stewart 					tp1->sent++;
3541f8829a4aSRandall Stewart 				}
3542f8829a4aSRandall Stewart 			}
3543f8829a4aSRandall Stewart 		}
3544f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3545f8829a4aSRandall Stewart 			struct sctp_nets *alt;
3546f8829a4aSRandall Stewart 
3547544e35bdSRandall Stewart 			/* fix counts and things */
3548544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3549544e35bdSRandall Stewart 				sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND,
3550544e35bdSRandall Stewart 				    (tp1->whoTo ? (tp1->whoTo->flight_size) : 0),
3551544e35bdSRandall Stewart 				    tp1->book_size,
3552544e35bdSRandall Stewart 				    (uintptr_t) tp1->whoTo,
3553544e35bdSRandall Stewart 				    tp1->rec.data.TSN_seq);
3554544e35bdSRandall Stewart 			}
3555544e35bdSRandall Stewart 			if (tp1->whoTo) {
3556544e35bdSRandall Stewart 				tp1->whoTo->net_ack++;
3557544e35bdSRandall Stewart 				sctp_flight_size_decrease(tp1);
3558544e35bdSRandall Stewart 			}
3559544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
3560544e35bdSRandall Stewart 				sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
3561544e35bdSRandall Stewart 				    asoc->peers_rwnd, tp1->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3562544e35bdSRandall Stewart 			}
3563544e35bdSRandall Stewart 			/* add back to the rwnd */
3564544e35bdSRandall Stewart 			asoc->peers_rwnd += (tp1->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3565544e35bdSRandall Stewart 
3566544e35bdSRandall Stewart 			/* remove from the total flight */
3567544e35bdSRandall Stewart 			sctp_total_flight_decrease(stcb, tp1);
3568544e35bdSRandall Stewart 
3569abe15ad6SRandall Stewart 			if ((stcb->asoc.peer_supports_prsctp) &&
3570abe15ad6SRandall Stewart 			    (PR_SCTP_RTX_ENABLED(tp1->flags))) {
3571abe15ad6SRandall Stewart 				/*
3572abe15ad6SRandall Stewart 				 * Has it been retransmitted tv_sec times? -
3573abe15ad6SRandall Stewart 				 * we store the retran count there.
3574abe15ad6SRandall Stewart 				 */
3575abe15ad6SRandall Stewart 				if (tp1->snd_count > tp1->rec.data.timetodrop.tv_sec) {
3576abe15ad6SRandall Stewart 					/* Yes, so drop it */
3577abe15ad6SRandall Stewart 					if (tp1->data != NULL) {
3578abe15ad6SRandall Stewart 						(void)sctp_release_pr_sctp_chunk(stcb, tp1,
3579abe15ad6SRandall Stewart 						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3580abe15ad6SRandall Stewart 						    SCTP_SO_NOT_LOCKED);
3581abe15ad6SRandall Stewart 					}
3582abe15ad6SRandall Stewart 					/* Make sure to flag we had a FR */
3583abe15ad6SRandall Stewart 					tp1->whoTo->net_ack++;
3584abe15ad6SRandall Stewart 					tp1 = TAILQ_NEXT(tp1, sctp_next);
3585abe15ad6SRandall Stewart 					continue;
3586abe15ad6SRandall Stewart 				}
3587abe15ad6SRandall Stewart 			}
3588f8829a4aSRandall Stewart 			/* printf("OK, we are now ready to FR this guy\n"); */
3589b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3590f8829a4aSRandall Stewart 				sctp_log_fr(tp1->rec.data.TSN_seq, tp1->snd_count,
3591f8829a4aSRandall Stewart 				    0, SCTP_FR_MARKED);
359280fefe0aSRandall Stewart 			}
3593f8829a4aSRandall Stewart 			if (strike_flag) {
3594f8829a4aSRandall Stewart 				/* This is a subsequent FR */
3595f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_sendmultfastretrans);
3596f8829a4aSRandall Stewart 			}
35975e54f665SRandall Stewart 			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3598b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
3599f8829a4aSRandall Stewart 				/*
3600f8829a4aSRandall Stewart 				 * CMT: Using RTX_SSTHRESH policy for CMT.
3601f8829a4aSRandall Stewart 				 * If CMT is being used, then pick dest with
3602f8829a4aSRandall Stewart 				 * largest ssthresh for any retransmission.
3603f8829a4aSRandall Stewart 				 */
3604f8829a4aSRandall Stewart 				tp1->no_fr_allowed = 1;
3605f8829a4aSRandall Stewart 				alt = tp1->whoTo;
36063c503c28SRandall Stewart 				/* sa_ignore NO_NULL_CHK */
3607b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
3608b54d3a6cSRandall Stewart 					/*
3609b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If CMT PF is on,
3610b54d3a6cSRandall Stewart 					 * use the PF version of
3611b54d3a6cSRandall Stewart 					 * find_alt_net()
3612b54d3a6cSRandall Stewart 					 */
3613b54d3a6cSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 2);
3614b54d3a6cSRandall Stewart 				} else {
3615b54d3a6cSRandall Stewart 					/*
3616b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If only CMT is on,
3617b54d3a6cSRandall Stewart 					 * use the CMT version of
3618b54d3a6cSRandall Stewart 					 * find_alt_net()
3619b54d3a6cSRandall Stewart 					 */
362052be287eSRandall Stewart 					/* sa_ignore NO_NULL_CHK */
3621f8829a4aSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 1);
3622b54d3a6cSRandall Stewart 				}
3623ad81507eSRandall Stewart 				if (alt == NULL) {
3624ad81507eSRandall Stewart 					alt = tp1->whoTo;
3625ad81507eSRandall Stewart 				}
3626f8829a4aSRandall Stewart 				/*
3627f8829a4aSRandall Stewart 				 * CUCv2: If a different dest is picked for
3628f8829a4aSRandall Stewart 				 * the retransmission, then new
3629f8829a4aSRandall Stewart 				 * (rtx-)pseudo_cumack needs to be tracked
3630f8829a4aSRandall Stewart 				 * for orig dest. Let CUCv2 track new (rtx-)
3631f8829a4aSRandall Stewart 				 * pseudo-cumack always.
3632f8829a4aSRandall Stewart 				 */
3633ad81507eSRandall Stewart 				if (tp1->whoTo) {
3634f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
3635f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
3636ad81507eSRandall Stewart 				}
3637f8829a4aSRandall Stewart 			} else {/* CMT is OFF */
3638f8829a4aSRandall Stewart 
3639f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3640f8829a4aSRandall Stewart 				/* Can we find an alternate? */
3641f8829a4aSRandall Stewart 				alt = sctp_find_alternate_net(stcb, tp1->whoTo, 0);
3642f8829a4aSRandall Stewart #else
3643f8829a4aSRandall Stewart 				/*
3644f8829a4aSRandall Stewart 				 * default behavior is to NOT retransmit
3645f8829a4aSRandall Stewart 				 * FR's to an alternate. Armando Caro's
3646f8829a4aSRandall Stewart 				 * paper details why.
3647f8829a4aSRandall Stewart 				 */
3648f8829a4aSRandall Stewart 				alt = tp1->whoTo;
3649f8829a4aSRandall Stewart #endif
3650f8829a4aSRandall Stewart 			}
3651f8829a4aSRandall Stewart 
3652f8829a4aSRandall Stewart 			tp1->rec.data.doing_fast_retransmit = 1;
3653f8829a4aSRandall Stewart 			tot_retrans++;
3654f8829a4aSRandall Stewart 			/* mark the sending seq for possible subsequent FR's */
3655f8829a4aSRandall Stewart 			/*
3656f8829a4aSRandall Stewart 			 * printf("Marking TSN for FR new value %x\n",
3657f8829a4aSRandall Stewart 			 * (uint32_t)tpi->rec.data.TSN_seq);
3658f8829a4aSRandall Stewart 			 */
3659f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->send_queue)) {
3660f8829a4aSRandall Stewart 				/*
3661f8829a4aSRandall Stewart 				 * If the queue of send is empty then its
3662f8829a4aSRandall Stewart 				 * the next sequence number that will be
3663f8829a4aSRandall Stewart 				 * assigned so we subtract one from this to
3664f8829a4aSRandall Stewart 				 * get the one we last sent.
3665f8829a4aSRandall Stewart 				 */
3666f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn = sending_seq;
3667f8829a4aSRandall Stewart 			} else {
3668f8829a4aSRandall Stewart 				/*
3669f8829a4aSRandall Stewart 				 * If there are chunks on the send queue
3670f8829a4aSRandall Stewart 				 * (unsent data that has made it from the
3671f8829a4aSRandall Stewart 				 * stream queues but not out the door, we
3672f8829a4aSRandall Stewart 				 * take the first one (which will have the
3673f8829a4aSRandall Stewart 				 * lowest TSN) and subtract one to get the
3674f8829a4aSRandall Stewart 				 * one we last sent.
3675f8829a4aSRandall Stewart 				 */
3676f8829a4aSRandall Stewart 				struct sctp_tmit_chunk *ttt;
3677f8829a4aSRandall Stewart 
3678f8829a4aSRandall Stewart 				ttt = TAILQ_FIRST(&asoc->send_queue);
3679f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn =
3680f8829a4aSRandall Stewart 				    ttt->rec.data.TSN_seq;
3681f8829a4aSRandall Stewart 			}
3682f8829a4aSRandall Stewart 
3683f8829a4aSRandall Stewart 			if (tp1->do_rtt) {
3684f8829a4aSRandall Stewart 				/*
3685f8829a4aSRandall Stewart 				 * this guy had a RTO calculation pending on
3686f8829a4aSRandall Stewart 				 * it, cancel it
3687f8829a4aSRandall Stewart 				 */
3688f8829a4aSRandall Stewart 				tp1->do_rtt = 0;
3689f8829a4aSRandall Stewart 			}
3690f8829a4aSRandall Stewart 			if (alt != tp1->whoTo) {
3691f8829a4aSRandall Stewart 				/* yes, there is an alternate. */
3692f8829a4aSRandall Stewart 				sctp_free_remote_addr(tp1->whoTo);
36933c503c28SRandall Stewart 				/* sa_ignore FREED_MEMORY */
3694f8829a4aSRandall Stewart 				tp1->whoTo = alt;
3695f8829a4aSRandall Stewart 				atomic_add_int(&alt->ref_count, 1);
3696f8829a4aSRandall Stewart 			}
3697f8829a4aSRandall Stewart 		}
3698f8829a4aSRandall Stewart 		tp1 = TAILQ_NEXT(tp1, sctp_next);
3699f8829a4aSRandall Stewart 	}			/* while (tp1) */
3700f8829a4aSRandall Stewart 
3701f8829a4aSRandall Stewart 	if (tot_retrans > 0) {
3702f8829a4aSRandall Stewart 		/*
3703f8829a4aSRandall Stewart 		 * Setup the ecn nonce re-sync point. We do this since once
3704f8829a4aSRandall Stewart 		 * we go to FR something we introduce a Karn's rule scenario
3705f8829a4aSRandall Stewart 		 * and won't know the totals for the ECN bits.
3706f8829a4aSRandall Stewart 		 */
3707f8829a4aSRandall Stewart 		asoc->nonce_resync_tsn = sending_seq;
3708f8829a4aSRandall Stewart 		asoc->nonce_wait_for_ecne = 0;
3709f8829a4aSRandall Stewart 		asoc->nonce_sum_check = 0;
3710f8829a4aSRandall Stewart 	}
3711f8829a4aSRandall Stewart }
3712f8829a4aSRandall Stewart 
3713f8829a4aSRandall Stewart struct sctp_tmit_chunk *
3714f8829a4aSRandall Stewart sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
3715f8829a4aSRandall Stewart     struct sctp_association *asoc)
3716f8829a4aSRandall Stewart {
3717f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2, *a_adv = NULL;
3718f8829a4aSRandall Stewart 	struct timeval now;
3719f8829a4aSRandall Stewart 	int now_filled = 0;
3720f8829a4aSRandall Stewart 
3721f8829a4aSRandall Stewart 	if (asoc->peer_supports_prsctp == 0) {
3722f8829a4aSRandall Stewart 		return (NULL);
3723f8829a4aSRandall Stewart 	}
3724f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
3725f8829a4aSRandall Stewart 	while (tp1) {
3726f8829a4aSRandall Stewart 		if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
3727f8829a4aSRandall Stewart 		    tp1->sent != SCTP_DATAGRAM_RESEND) {
3728f8829a4aSRandall Stewart 			/* no chance to advance, out of here */
3729f8829a4aSRandall Stewart 			break;
3730f8829a4aSRandall Stewart 		}
37310c0982b8SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
37320c0982b8SRandall Stewart 			if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
37330c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
37340c0982b8SRandall Stewart 				    asoc->advanced_peer_ack_point,
37350c0982b8SRandall Stewart 				    tp1->rec.data.TSN_seq, 0, 0);
37360c0982b8SRandall Stewart 			}
37370c0982b8SRandall Stewart 		}
3738f8829a4aSRandall Stewart 		if (!PR_SCTP_ENABLED(tp1->flags)) {
3739f8829a4aSRandall Stewart 			/*
3740f8829a4aSRandall Stewart 			 * We can't fwd-tsn past any that are reliable aka
3741f8829a4aSRandall Stewart 			 * retransmitted until the asoc fails.
3742f8829a4aSRandall Stewart 			 */
3743f8829a4aSRandall Stewart 			break;
3744f8829a4aSRandall Stewart 		}
3745f8829a4aSRandall Stewart 		if (!now_filled) {
37466e55db54SRandall Stewart 			(void)SCTP_GETTIME_TIMEVAL(&now);
3747f8829a4aSRandall Stewart 			now_filled = 1;
3748f8829a4aSRandall Stewart 		}
3749f8829a4aSRandall Stewart 		tp2 = TAILQ_NEXT(tp1, sctp_next);
3750f8829a4aSRandall Stewart 		/*
3751f8829a4aSRandall Stewart 		 * now we got a chunk which is marked for another
3752f8829a4aSRandall Stewart 		 * retransmission to a PR-stream but has run out its chances
3753f8829a4aSRandall Stewart 		 * already maybe OR has been marked to skip now. Can we skip
3754f8829a4aSRandall Stewart 		 * it if its a resend?
3755f8829a4aSRandall Stewart 		 */
3756f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND &&
3757f8829a4aSRandall Stewart 		    (PR_SCTP_TTL_ENABLED(tp1->flags))) {
3758f8829a4aSRandall Stewart 			/*
3759f8829a4aSRandall Stewart 			 * Now is this one marked for resend and its time is
3760f8829a4aSRandall Stewart 			 * now up?
3761f8829a4aSRandall Stewart 			 */
3762f8829a4aSRandall Stewart 			if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3763f8829a4aSRandall Stewart 				/* Yes so drop it */
3764f8829a4aSRandall Stewart 				if (tp1->data) {
3765ad81507eSRandall Stewart 					(void)sctp_release_pr_sctp_chunk(stcb, tp1,
3766f8829a4aSRandall Stewart 					    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
37670c0982b8SRandall Stewart 					    SCTP_SO_NOT_LOCKED);
3768f8829a4aSRandall Stewart 				}
3769f8829a4aSRandall Stewart 			} else {
3770f8829a4aSRandall Stewart 				/*
3771f8829a4aSRandall Stewart 				 * No, we are done when hit one for resend
3772f8829a4aSRandall Stewart 				 * whos time as not expired.
3773f8829a4aSRandall Stewart 				 */
3774f8829a4aSRandall Stewart 				break;
3775f8829a4aSRandall Stewart 			}
3776f8829a4aSRandall Stewart 		}
3777f8829a4aSRandall Stewart 		/*
3778f8829a4aSRandall Stewart 		 * Ok now if this chunk is marked to drop it we can clean up
3779f8829a4aSRandall Stewart 		 * the chunk, advance our peer ack point and we can check
3780f8829a4aSRandall Stewart 		 * the next chunk.
3781f8829a4aSRandall Stewart 		 */
3782f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
3783f8829a4aSRandall Stewart 			/* advance PeerAckPoint goes forward */
37840c0982b8SRandall Stewart 			if (compare_with_wrap(tp1->rec.data.TSN_seq,
37850c0982b8SRandall Stewart 			    asoc->advanced_peer_ack_point,
37860c0982b8SRandall Stewart 			    MAX_TSN)) {
37870c0982b8SRandall Stewart 
3788f8829a4aSRandall Stewart 				asoc->advanced_peer_ack_point = tp1->rec.data.TSN_seq;
3789f8829a4aSRandall Stewart 				a_adv = tp1;
37900c0982b8SRandall Stewart 			} else if (tp1->rec.data.TSN_seq == asoc->advanced_peer_ack_point) {
37910c0982b8SRandall Stewart 				/* No update but we do save the chk */
37920c0982b8SRandall Stewart 				a_adv = tp1;
37930c0982b8SRandall Stewart 			}
3794f8829a4aSRandall Stewart 		} else {
3795f8829a4aSRandall Stewart 			/*
3796f8829a4aSRandall Stewart 			 * If it is still in RESEND we can advance no
3797f8829a4aSRandall Stewart 			 * further
3798f8829a4aSRandall Stewart 			 */
3799f8829a4aSRandall Stewart 			break;
3800f8829a4aSRandall Stewart 		}
3801f8829a4aSRandall Stewart 		/*
3802f8829a4aSRandall Stewart 		 * If we hit here we just dumped tp1, move to next tsn on
3803f8829a4aSRandall Stewart 		 * sent queue.
3804f8829a4aSRandall Stewart 		 */
3805f8829a4aSRandall Stewart 		tp1 = tp2;
3806f8829a4aSRandall Stewart 	}
3807f8829a4aSRandall Stewart 	return (a_adv);
3808f8829a4aSRandall Stewart }
3809f8829a4aSRandall Stewart 
38100c0982b8SRandall Stewart static int
3811c105859eSRandall Stewart sctp_fs_audit(struct sctp_association *asoc)
3812bff64a4dSRandall Stewart {
3813bff64a4dSRandall Stewart 	struct sctp_tmit_chunk *chk;
3814bff64a4dSRandall Stewart 	int inflight = 0, resend = 0, inbetween = 0, acked = 0, above = 0;
38150c0982b8SRandall Stewart 	int entry_flight, entry_cnt, ret;
38160c0982b8SRandall Stewart 
38170c0982b8SRandall Stewart 	entry_flight = asoc->total_flight;
38180c0982b8SRandall Stewart 	entry_cnt = asoc->total_flight_count;
38190c0982b8SRandall Stewart 	ret = 0;
38200c0982b8SRandall Stewart 
38210c0982b8SRandall Stewart 	if (asoc->pr_sctp_cnt >= asoc->sent_queue_cnt)
38220c0982b8SRandall Stewart 		return (0);
3823bff64a4dSRandall Stewart 
3824bff64a4dSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
3825bff64a4dSRandall Stewart 		if (chk->sent < SCTP_DATAGRAM_RESEND) {
38260c0982b8SRandall Stewart 			printf("Chk TSN:%u size:%d inflight cnt:%d\n",
38270c0982b8SRandall Stewart 			    chk->rec.data.TSN_seq,
38280c0982b8SRandall Stewart 			    chk->send_size,
38290c0982b8SRandall Stewart 			    chk->snd_count
38300c0982b8SRandall Stewart 			    );
3831bff64a4dSRandall Stewart 			inflight++;
3832bff64a4dSRandall Stewart 		} else if (chk->sent == SCTP_DATAGRAM_RESEND) {
3833bff64a4dSRandall Stewart 			resend++;
3834bff64a4dSRandall Stewart 		} else if (chk->sent < SCTP_DATAGRAM_ACKED) {
3835bff64a4dSRandall Stewart 			inbetween++;
3836bff64a4dSRandall Stewart 		} else if (chk->sent > SCTP_DATAGRAM_ACKED) {
3837bff64a4dSRandall Stewart 			above++;
3838bff64a4dSRandall Stewart 		} else {
3839bff64a4dSRandall Stewart 			acked++;
3840bff64a4dSRandall Stewart 		}
3841bff64a4dSRandall Stewart 	}
3842f1f73e57SRandall Stewart 
3843c105859eSRandall Stewart 	if ((inflight > 0) || (inbetween > 0)) {
3844f1f73e57SRandall Stewart #ifdef INVARIANTS
3845c105859eSRandall Stewart 		panic("Flight size-express incorrect? \n");
3846f1f73e57SRandall Stewart #else
38470c0982b8SRandall Stewart 		printf("asoc->total_flight:%d cnt:%d\n",
38480c0982b8SRandall Stewart 		    entry_flight, entry_cnt);
38490c0982b8SRandall Stewart 
38500c0982b8SRandall Stewart 		SCTP_PRINTF("Flight size-express incorrect F:%d I:%d R:%d Ab:%d ACK:%d\n",
38510c0982b8SRandall Stewart 		    inflight, inbetween, resend, above, acked);
38520c0982b8SRandall Stewart 		ret = 1;
3853f1f73e57SRandall Stewart #endif
3854bff64a4dSRandall Stewart 	}
38550c0982b8SRandall Stewart 	return (ret);
3856c105859eSRandall Stewart }
3857c105859eSRandall Stewart 
3858c105859eSRandall Stewart 
3859c105859eSRandall Stewart static void
3860c105859eSRandall Stewart sctp_window_probe_recovery(struct sctp_tcb *stcb,
3861c105859eSRandall Stewart     struct sctp_association *asoc,
3862c105859eSRandall Stewart     struct sctp_nets *net,
3863c105859eSRandall Stewart     struct sctp_tmit_chunk *tp1)
3864c105859eSRandall Stewart {
3865dfb11ef8SRandall Stewart 	tp1->window_probe = 0;
38665171328bSRandall Stewart 	if ((tp1->sent >= SCTP_DATAGRAM_ACKED) || (tp1->data == NULL)) {
3867dfb11ef8SRandall Stewart 		/* TSN's skipped we do NOT move back. */
3868dfb11ef8SRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DWN_WP_FWD,
3869dfb11ef8SRandall Stewart 		    tp1->whoTo->flight_size,
3870dfb11ef8SRandall Stewart 		    tp1->book_size,
3871dfb11ef8SRandall Stewart 		    (uintptr_t) tp1->whoTo,
3872dfb11ef8SRandall Stewart 		    tp1->rec.data.TSN_seq);
3873dfb11ef8SRandall Stewart 		return;
3874dfb11ef8SRandall Stewart 	}
38755171328bSRandall Stewart 	/* First setup this by shrinking flight */
38765171328bSRandall Stewart 	sctp_flight_size_decrease(tp1);
38775171328bSRandall Stewart 	sctp_total_flight_decrease(stcb, tp1);
38785171328bSRandall Stewart 	/* Now mark for resend */
38795171328bSRandall Stewart 	tp1->sent = SCTP_DATAGRAM_RESEND;
38805171328bSRandall Stewart 	asoc->sent_queue_retran_cnt++;
3881b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3882c105859eSRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_WP,
3883c105859eSRandall Stewart 		    tp1->whoTo->flight_size,
3884c105859eSRandall Stewart 		    tp1->book_size,
3885c105859eSRandall Stewart 		    (uintptr_t) tp1->whoTo,
3886c105859eSRandall Stewart 		    tp1->rec.data.TSN_seq);
388780fefe0aSRandall Stewart 	}
3888c105859eSRandall Stewart }
3889c105859eSRandall Stewart 
3890f8829a4aSRandall Stewart void
3891f8829a4aSRandall Stewart sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack,
3892f8829a4aSRandall Stewart     uint32_t rwnd, int nonce_sum_flag, int *abort_now)
3893f8829a4aSRandall Stewart {
3894f8829a4aSRandall Stewart 	struct sctp_nets *net;
3895f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3896f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
38975e54f665SRandall Stewart 	uint32_t old_rwnd;
38985e54f665SRandall Stewart 	int win_probe_recovery = 0;
3899c105859eSRandall Stewart 	int win_probe_recovered = 0;
3900d06c82f1SRandall Stewart 	int j, done_once = 0;
3901f8829a4aSRandall Stewart 
3902b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
3903d06c82f1SRandall Stewart 		sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
3904d06c82f1SRandall Stewart 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
390580fefe0aSRandall Stewart 	}
3906f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
390718e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
390818e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
390918e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
391018e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
391118e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
391218e198d3SRandall Stewart 	}
391318e198d3SRandall Stewart #endif
3914f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
3915d06c82f1SRandall Stewart 	old_rwnd = asoc->peers_rwnd;
39165e54f665SRandall Stewart 	if (compare_with_wrap(asoc->last_acked_seq, cumack, MAX_TSN)) {
39175e54f665SRandall Stewart 		/* old ack */
39185e54f665SRandall Stewart 		return;
3919d06c82f1SRandall Stewart 	} else if (asoc->last_acked_seq == cumack) {
3920d06c82f1SRandall Stewart 		/* Window update sack */
3921d06c82f1SRandall Stewart 		asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
3922b3f1ea41SRandall Stewart 		    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
3923d06c82f1SRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
3924d06c82f1SRandall Stewart 			/* SWS sender side engages */
3925d06c82f1SRandall Stewart 			asoc->peers_rwnd = 0;
3926d06c82f1SRandall Stewart 		}
3927d06c82f1SRandall Stewart 		if (asoc->peers_rwnd > old_rwnd) {
3928d06c82f1SRandall Stewart 			goto again;
3929d06c82f1SRandall Stewart 		}
3930d06c82f1SRandall Stewart 		return;
39315e54f665SRandall Stewart 	}
3932f8829a4aSRandall Stewart 	/* First setup for CC stuff */
3933f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3934f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
3935f8829a4aSRandall Stewart 		net->net_ack = 0;
3936f8829a4aSRandall Stewart 		net->net_ack2 = 0;
3937132dea7dSRandall Stewart 
3938132dea7dSRandall Stewart 		/*
3939132dea7dSRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
3940132dea7dSRandall Stewart 		 * SACK processing
3941132dea7dSRandall Stewart 		 */
3942132dea7dSRandall Stewart 		net->new_pseudo_cumack = 0;
3943132dea7dSRandall Stewart 		net->will_exit_fast_recovery = 0;
3944f8829a4aSRandall Stewart 	}
3945b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
3946139bc87fSRandall Stewart 		uint32_t send_s;
3947139bc87fSRandall Stewart 
3948c105859eSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3949c105859eSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
3950c105859eSRandall Stewart 			    sctpchunk_listhead);
3951c105859eSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
3952139bc87fSRandall Stewart 		} else {
3953c105859eSRandall Stewart 			send_s = asoc->sending_seq;
3954139bc87fSRandall Stewart 		}
3955139bc87fSRandall Stewart 		if ((cumack == send_s) ||
3956139bc87fSRandall Stewart 		    compare_with_wrap(cumack, send_s, MAX_TSN)) {
3957c105859eSRandall Stewart #ifndef INVARIANTS
3958139bc87fSRandall Stewart 			struct mbuf *oper;
3959139bc87fSRandall Stewart 
3960c105859eSRandall Stewart #endif
3961c105859eSRandall Stewart #ifdef INVARIANTS
3962c105859eSRandall Stewart 			panic("Impossible sack 1");
3963c105859eSRandall Stewart #else
3964139bc87fSRandall Stewart 			*abort_now = 1;
3965139bc87fSRandall Stewart 			/* XXX */
3966139bc87fSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3967139bc87fSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
3968139bc87fSRandall Stewart 			if (oper) {
3969139bc87fSRandall Stewart 				struct sctp_paramhdr *ph;
3970139bc87fSRandall Stewart 				uint32_t *ippp;
3971139bc87fSRandall Stewart 
3972139bc87fSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
3973139bc87fSRandall Stewart 				    sizeof(uint32_t);
3974139bc87fSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
3975139bc87fSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
3976139bc87fSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
3977139bc87fSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
3978139bc87fSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
3979139bc87fSRandall Stewart 			}
3980139bc87fSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
3981ceaad40aSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
3982139bc87fSRandall Stewart 			return;
3983139bc87fSRandall Stewart #endif
3984139bc87fSRandall Stewart 		}
3985139bc87fSRandall Stewart 	}
3986f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = cumack;
3987b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
3988c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
3989c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
3990c4739e2fSRandall Stewart 		    0,
3991c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
3992c4739e2fSRandall Stewart 		    __LINE__);
3993c4739e2fSRandall Stewart 	}
3994f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
39955e54f665SRandall Stewart 	if (compare_with_wrap(cumack, asoc->last_acked_seq, MAX_TSN)) {
3996f8829a4aSRandall Stewart 		/* process the new consecutive TSN first */
3997f8829a4aSRandall Stewart 		tp1 = TAILQ_FIRST(&asoc->sent_queue);
3998f8829a4aSRandall Stewart 		while (tp1) {
3999f8829a4aSRandall Stewart 			tp2 = TAILQ_NEXT(tp1, sctp_next);
4000f8829a4aSRandall Stewart 			if (compare_with_wrap(cumack, tp1->rec.data.TSN_seq,
4001f8829a4aSRandall Stewart 			    MAX_TSN) ||
4002f8829a4aSRandall Stewart 			    cumack == tp1->rec.data.TSN_seq) {
400318e198d3SRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
400418e198d3SRandall Stewart 					printf("Warning, an unsent is now acked?\n");
400518e198d3SRandall Stewart 				}
4006f8829a4aSRandall Stewart 				/*
400718e198d3SRandall Stewart 				 * ECN Nonce: Add the nonce to the sender's
400818e198d3SRandall Stewart 				 * nonce sum
4009f8829a4aSRandall Stewart 				 */
4010f8829a4aSRandall Stewart 				asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
4011f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4012f8829a4aSRandall Stewart 					/*
401318e198d3SRandall Stewart 					 * If it is less than ACKED, it is
401418e198d3SRandall Stewart 					 * now no-longer in flight. Higher
401518e198d3SRandall Stewart 					 * values may occur during marking
4016f8829a4aSRandall Stewart 					 */
4017c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4018b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4019c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4020a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
4021a5d547adSRandall Stewart 							    tp1->book_size,
4022c105859eSRandall Stewart 							    (uintptr_t) tp1->whoTo,
4023a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
402480fefe0aSRandall Stewart 						}
4025c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
402604ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
4027c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
4028f8829a4aSRandall Stewart 					}
4029f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
4030f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
4031f8829a4aSRandall Stewart 						/*
403218e198d3SRandall Stewart 						 * True non-retransmited
4033f8829a4aSRandall Stewart 						 * chunk
4034f8829a4aSRandall Stewart 						 */
4035f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
4036f8829a4aSRandall Stewart 						    tp1->send_size;
4037f8829a4aSRandall Stewart 
4038f8829a4aSRandall Stewart 						/* update RTO too? */
403962c1ff9cSRandall Stewart 						if (tp1->do_rtt) {
4040f8829a4aSRandall Stewart 							tp1->whoTo->RTO =
404104ee05e8SRandall Stewart 							/*
404204ee05e8SRandall Stewart 							 * sa_ignore
404304ee05e8SRandall Stewart 							 * NO_NULL_CHK
404404ee05e8SRandall Stewart 							 */
4045f8829a4aSRandall Stewart 							    sctp_calculate_rto(stcb,
4046f8829a4aSRandall Stewart 							    asoc, tp1->whoTo,
404718e198d3SRandall Stewart 							    &tp1->sent_rcv_time,
404818e198d3SRandall Stewart 							    sctp_align_safe_nocopy);
4049f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
4050f8829a4aSRandall Stewart 						}
4051f8829a4aSRandall Stewart 					}
4052132dea7dSRandall Stewart 					/*
405318e198d3SRandall Stewart 					 * CMT: CUCv2 algorithm. From the
405418e198d3SRandall Stewart 					 * cumack'd TSNs, for each TSN being
405518e198d3SRandall Stewart 					 * acked for the first time, set the
405618e198d3SRandall Stewart 					 * following variables for the
405718e198d3SRandall Stewart 					 * corresp destination.
405818e198d3SRandall Stewart 					 * new_pseudo_cumack will trigger a
405918e198d3SRandall Stewart 					 * cwnd update.
406018e198d3SRandall Stewart 					 * find_(rtx_)pseudo_cumack will
406118e198d3SRandall Stewart 					 * trigger search for the next
406218e198d3SRandall Stewart 					 * expected (rtx-)pseudo-cumack.
4063132dea7dSRandall Stewart 					 */
4064132dea7dSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
4065132dea7dSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
4066132dea7dSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
4067132dea7dSRandall Stewart 
4068b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
406904ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
4070f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
407180fefe0aSRandall Stewart 					}
4072f8829a4aSRandall Stewart 				}
4073f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4074f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4075f8829a4aSRandall Stewart 				}
407642551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
407742551e99SRandall Stewart 					/* deflate the cwnd */
407842551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
407942551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
408042551e99SRandall Stewart 				}
4081f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
4082f8829a4aSRandall Stewart 				TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4083f8829a4aSRandall Stewart 				if (tp1->data) {
408404ee05e8SRandall Stewart 					/* sa_ignore NO_NULL_CHK */
4085f8829a4aSRandall Stewart 					sctp_free_bufspace(stcb, asoc, tp1, 1);
4086f8829a4aSRandall Stewart 					sctp_m_freem(tp1->data);
4087f8829a4aSRandall Stewart 				}
4088b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4089f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
4090f8829a4aSRandall Stewart 					    cumack,
4091f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
4092f8829a4aSRandall Stewart 					    0,
4093f8829a4aSRandall Stewart 					    0,
4094f8829a4aSRandall Stewart 					    SCTP_LOG_FREE_SENT);
409580fefe0aSRandall Stewart 				}
4096f8829a4aSRandall Stewart 				tp1->data = NULL;
4097f8829a4aSRandall Stewart 				asoc->sent_queue_cnt--;
4098f8829a4aSRandall Stewart 				sctp_free_a_chunk(stcb, tp1);
4099f8829a4aSRandall Stewart 				tp1 = tp2;
410018e198d3SRandall Stewart 			} else {
410118e198d3SRandall Stewart 				break;
4102f8829a4aSRandall Stewart 			}
41035e54f665SRandall Stewart 		}
410418e198d3SRandall Stewart 
410518e198d3SRandall Stewart 	}
410604ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
4107f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
4108ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4109ceaad40aSRandall Stewart 		struct socket *so;
4110ceaad40aSRandall Stewart 
4111ceaad40aSRandall Stewart #endif
4112f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4113b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
411404ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
4115f8829a4aSRandall Stewart 			sctp_wakeup_log(stcb, cumack, 1, SCTP_WAKESND_FROM_SACK);
411680fefe0aSRandall Stewart 		}
4117ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4118ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
4119ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
4120ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
4121ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
4122ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
4123ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4124ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4125ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
4126ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
4127ceaad40aSRandall Stewart 			return;
4128ceaad40aSRandall Stewart 		}
4129ceaad40aSRandall Stewart #endif
4130f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4131ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4132ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4133ceaad40aSRandall Stewart #endif
4134f8829a4aSRandall Stewart 	} else {
4135b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4136f8829a4aSRandall Stewart 			sctp_wakeup_log(stcb, cumack, 1, SCTP_NOWAKE_FROM_SACK);
413780fefe0aSRandall Stewart 		}
4138f8829a4aSRandall Stewart 	}
4139f8829a4aSRandall Stewart 
4140b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
4141f8829a4aSRandall Stewart 	if (asoc->last_acked_seq != cumack)
4142b54d3a6cSRandall Stewart 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
41435e54f665SRandall Stewart 
4144f8829a4aSRandall Stewart 	asoc->last_acked_seq = cumack;
41455e54f665SRandall Stewart 
4146f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
4147f8829a4aSRandall Stewart 		/* nothing left in-flight */
4148f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4149f8829a4aSRandall Stewart 			net->flight_size = 0;
4150f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4151f8829a4aSRandall Stewart 		}
4152f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4153f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4154f8829a4aSRandall Stewart 	}
4155f8829a4aSRandall Stewart 	/* ECN Nonce updates */
4156f8829a4aSRandall Stewart 	if (asoc->ecn_nonce_allowed) {
4157f8829a4aSRandall Stewart 		if (asoc->nonce_sum_check) {
4158f8829a4aSRandall Stewart 			if (nonce_sum_flag != ((asoc->nonce_sum_expect_base) & SCTP_SACK_NONCE_SUM)) {
4159f8829a4aSRandall Stewart 				if (asoc->nonce_wait_for_ecne == 0) {
4160f8829a4aSRandall Stewart 					struct sctp_tmit_chunk *lchk;
4161f8829a4aSRandall Stewart 
4162f8829a4aSRandall Stewart 					lchk = TAILQ_FIRST(&asoc->send_queue);
4163f8829a4aSRandall Stewart 					asoc->nonce_wait_for_ecne = 1;
4164f8829a4aSRandall Stewart 					if (lchk) {
4165f8829a4aSRandall Stewart 						asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
4166f8829a4aSRandall Stewart 					} else {
4167f8829a4aSRandall Stewart 						asoc->nonce_wait_tsn = asoc->sending_seq;
4168f8829a4aSRandall Stewart 					}
4169f8829a4aSRandall Stewart 				} else {
4170f8829a4aSRandall Stewart 					if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
4171f8829a4aSRandall Stewart 					    (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
4172f8829a4aSRandall Stewart 						/*
4173f8829a4aSRandall Stewart 						 * Misbehaving peer. We need
4174f8829a4aSRandall Stewart 						 * to react to this guy
4175f8829a4aSRandall Stewart 						 */
4176f8829a4aSRandall Stewart 						asoc->ecn_allowed = 0;
4177f8829a4aSRandall Stewart 						asoc->ecn_nonce_allowed = 0;
4178f8829a4aSRandall Stewart 					}
4179f8829a4aSRandall Stewart 				}
4180f8829a4aSRandall Stewart 			}
4181f8829a4aSRandall Stewart 		} else {
4182f8829a4aSRandall Stewart 			/* See if Resynchronization Possible */
4183f8829a4aSRandall Stewart 			if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
4184f8829a4aSRandall Stewart 				asoc->nonce_sum_check = 1;
4185f8829a4aSRandall Stewart 				/*
4186cd554309SMichael Tuexen 				 * Now we must calculate what the base is.
4187f8829a4aSRandall Stewart 				 * We do this based on two things, we know
4188f8829a4aSRandall Stewart 				 * the total's for all the segments
4189cd554309SMichael Tuexen 				 * gap-acked in the SACK (none). We also
4190f8829a4aSRandall Stewart 				 * know the SACK's nonce sum, its in
4191f8829a4aSRandall Stewart 				 * nonce_sum_flag. So we can build a truth
4192f8829a4aSRandall Stewart 				 * table to back-calculate the new value of
4193f8829a4aSRandall Stewart 				 * asoc->nonce_sum_expect_base:
4194f8829a4aSRandall Stewart 				 *
4195f8829a4aSRandall Stewart 				 * SACK-flag-Value         Seg-Sums Base 0 0 0
4196f8829a4aSRandall Stewart 				 * 1                    0 1 0 1 1 1
4197f8829a4aSRandall Stewart 				 * 1 0
4198f8829a4aSRandall Stewart 				 */
4199f8829a4aSRandall Stewart 				asoc->nonce_sum_expect_base = (0 ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
4200f8829a4aSRandall Stewart 			}
4201f8829a4aSRandall Stewart 		}
4202f8829a4aSRandall Stewart 	}
4203f8829a4aSRandall Stewart 	/* RWND update */
4204f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
4205b3f1ea41SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4206f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4207f8829a4aSRandall Stewart 		/* SWS sender side engages */
4208f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
4209f8829a4aSRandall Stewart 	}
42105e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
42115e54f665SRandall Stewart 		win_probe_recovery = 1;
42125e54f665SRandall Stewart 	}
4213f8829a4aSRandall Stewart 	/* Now assure a timer where data is queued at */
4214a5d547adSRandall Stewart again:
4215a5d547adSRandall Stewart 	j = 0;
4216f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
42175171328bSRandall Stewart 		int to_ticks;
42185171328bSRandall Stewart 
42195e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
4220c105859eSRandall Stewart 			win_probe_recovered = 1;
42215e54f665SRandall Stewart 			/*
42225e54f665SRandall Stewart 			 * Find first chunk that was used with window probe
42235e54f665SRandall Stewart 			 * and clear the sent
42245e54f665SRandall Stewart 			 */
42253c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
42265e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
42275e54f665SRandall Stewart 				if (tp1->window_probe) {
4228cd554309SMichael Tuexen 					/* move back to data send queue */
4229c105859eSRandall Stewart 					sctp_window_probe_recovery(stcb, asoc, net, tp1);
42305e54f665SRandall Stewart 					break;
42315e54f665SRandall Stewart 				}
42325e54f665SRandall Stewart 			}
42335e54f665SRandall Stewart 		}
4234f8829a4aSRandall Stewart 		if (net->RTO == 0) {
4235f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
4236f8829a4aSRandall Stewart 		} else {
4237f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
4238f8829a4aSRandall Stewart 		}
42395171328bSRandall Stewart 		if (net->flight_size) {
4240a5d547adSRandall Stewart 			j++;
4241ad81507eSRandall Stewart 			(void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
4242f8829a4aSRandall Stewart 			    sctp_timeout_handler, &net->rxt_timer);
42435171328bSRandall Stewart 			if (net->window_probe) {
42445171328bSRandall Stewart 				net->window_probe = 0;
42455171328bSRandall Stewart 			}
4246f8829a4aSRandall Stewart 		} else {
42475171328bSRandall Stewart 			if (net->window_probe) {
42485171328bSRandall Stewart 				/*
42495171328bSRandall Stewart 				 * In window probes we must assure a timer
42505171328bSRandall Stewart 				 * is still running there
42515171328bSRandall Stewart 				 */
42525171328bSRandall Stewart 				net->window_probe = 0;
42535171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
42545171328bSRandall Stewart 					SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
42555171328bSRandall Stewart 					    sctp_timeout_handler, &net->rxt_timer);
42565171328bSRandall Stewart 				}
42575171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4258f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4259a5d547adSRandall Stewart 				    stcb, net,
4260a5d547adSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
4261f8829a4aSRandall Stewart 			}
4262b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
4263139bc87fSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4264f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
4265a5d547adSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4266a5d547adSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
4267f8829a4aSRandall Stewart 				}
4268f8829a4aSRandall Stewart 			}
4269f8829a4aSRandall Stewart 		}
4270f8829a4aSRandall Stewart 	}
4271bff64a4dSRandall Stewart 	if ((j == 0) &&
4272bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
4273bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
4274c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
4275bff64a4dSRandall Stewart 	    (done_once == 0)) {
42760c0982b8SRandall Stewart 		/*
42770c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
42780c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
42790c0982b8SRandall Stewart 		 */
42800c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
4281a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4282a5d547adSRandall Stewart 				net->flight_size = 0;
4283a5d547adSRandall Stewart 			}
4284a5d547adSRandall Stewart 			asoc->total_flight = 0;
4285a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
4286a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4287a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4288a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4289c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
4290c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
4291a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4292a5d547adSRandall Stewart 					asoc->sent_queue_retran_cnt++;
4293a5d547adSRandall Stewart 				}
4294a5d547adSRandall Stewart 			}
42950c0982b8SRandall Stewart 		}
4296bff64a4dSRandall Stewart 		done_once = 1;
4297a5d547adSRandall Stewart 		goto again;
4298a5d547adSRandall Stewart 	}
4299f8829a4aSRandall Stewart 	/**********************************/
4300f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
4301f8829a4aSRandall Stewart 	/**********************************/
4302f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4303f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
4304f8829a4aSRandall Stewart 		/* clean up */
4305f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
4306f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4307f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4308f8829a4aSRandall Stewart 		    (asoc->locked_on_sending)
4309f8829a4aSRandall Stewart 		    ) {
4310f8829a4aSRandall Stewart 			struct sctp_stream_queue_pending *sp;
4311f8829a4aSRandall Stewart 
4312f8829a4aSRandall Stewart 			/*
4313f8829a4aSRandall Stewart 			 * I may be in a state where we got all across.. but
4314f8829a4aSRandall Stewart 			 * cannot write more due to a shutdown... we abort
4315f8829a4aSRandall Stewart 			 * since the user did not indicate EOR in this case.
4316f8829a4aSRandall Stewart 			 * The sp will be cleaned during free of the asoc.
4317f8829a4aSRandall Stewart 			 */
4318f8829a4aSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4319f8829a4aSRandall Stewart 			    sctp_streamhead);
43202afb3e84SRandall Stewart 			if ((sp) && (sp->length == 0)) {
43212afb3e84SRandall Stewart 				/* Let cleanup code purge it */
43222afb3e84SRandall Stewart 				if (sp->msg_is_complete) {
43232afb3e84SRandall Stewart 					asoc->stream_queue_cnt--;
43242afb3e84SRandall Stewart 				} else {
4325f8829a4aSRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
4326f8829a4aSRandall Stewart 					asoc->locked_on_sending = NULL;
4327f8829a4aSRandall Stewart 					asoc->stream_queue_cnt--;
4328f8829a4aSRandall Stewart 				}
4329f8829a4aSRandall Stewart 			}
43302afb3e84SRandall Stewart 		}
4331f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4332f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4333f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4334f8829a4aSRandall Stewart 				/* Need to abort here */
4335f8829a4aSRandall Stewart 				struct mbuf *oper;
4336f8829a4aSRandall Stewart 
4337f8829a4aSRandall Stewart 		abort_out_now:
4338f8829a4aSRandall Stewart 				*abort_now = 1;
4339f8829a4aSRandall Stewart 				/* XXX */
4340f8829a4aSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4341f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
4342f8829a4aSRandall Stewart 				if (oper) {
4343f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
4344f8829a4aSRandall Stewart 					uint32_t *ippp;
4345f8829a4aSRandall Stewart 
4346139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4347f8829a4aSRandall Stewart 					    sizeof(uint32_t);
4348f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
4349f8829a4aSRandall Stewart 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4350139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
4351f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
4352a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_24);
4353f8829a4aSRandall Stewart 				}
4354a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
4355ceaad40aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
4356f8829a4aSRandall Stewart 			} else {
4357f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4358f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4359f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4360f42a358aSRandall Stewart 				}
4361c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4362b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4363f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
4364f8829a4aSRandall Stewart 				sctp_send_shutdown(stcb,
4365f8829a4aSRandall Stewart 				    stcb->asoc.primary_destination);
4366f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4367f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
4368f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4369f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
4370f8829a4aSRandall Stewart 			}
4371f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4372f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4373f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4374f8829a4aSRandall Stewart 				goto abort_out_now;
4375f8829a4aSRandall Stewart 			}
4376f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4377c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4378b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4379f8829a4aSRandall Stewart 			sctp_send_shutdown_ack(stcb,
4380f8829a4aSRandall Stewart 			    stcb->asoc.primary_destination);
4381f8829a4aSRandall Stewart 
4382f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4383f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb, asoc->primary_destination);
4384f8829a4aSRandall Stewart 		}
4385f8829a4aSRandall Stewart 	}
4386dfb11ef8SRandall Stewart 	/*********************************************/
4387dfb11ef8SRandall Stewart 	/* Here we perform PR-SCTP procedures        */
4388dfb11ef8SRandall Stewart 	/* (section 4.2)                             */
4389dfb11ef8SRandall Stewart 	/*********************************************/
4390dfb11ef8SRandall Stewart 	/* C1. update advancedPeerAckPoint */
4391dfb11ef8SRandall Stewart 	if (compare_with_wrap(cumack, asoc->advanced_peer_ack_point, MAX_TSN)) {
4392dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cumack;
4393dfb11ef8SRandall Stewart 	}
4394830d754dSRandall Stewart 	/* PR-Sctp issues need to be addressed too */
4395830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
4396830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
4397830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
4398830d754dSRandall Stewart 
4399830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
4400830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
4401830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
4402830d754dSRandall Stewart 		if (compare_with_wrap(asoc->advanced_peer_ack_point, cumack,
4403830d754dSRandall Stewart 		    MAX_TSN)) {
4404830d754dSRandall Stewart 			/*
4405830d754dSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing for notes
4406830d754dSRandall Stewart 			 * on issues that will occur when the ECN NONCE
4407830d754dSRandall Stewart 			 * stuff is put into SCTP for cross checking.
4408830d754dSRandall Stewart 			 */
4409830d754dSRandall Stewart 			if (compare_with_wrap(asoc->advanced_peer_ack_point, old_adv_peer_ack_point,
4410830d754dSRandall Stewart 			    MAX_TSN)) {
4411830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
4412830d754dSRandall Stewart 				/*
4413830d754dSRandall Stewart 				 * ECN Nonce: Disable Nonce Sum check when
4414830d754dSRandall Stewart 				 * FWD TSN is sent and store resync tsn
4415830d754dSRandall Stewart 				 */
4416830d754dSRandall Stewart 				asoc->nonce_sum_check = 0;
4417830d754dSRandall Stewart 				asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
44180c0982b8SRandall Stewart 			} else if (lchk) {
44190c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
44200c0982b8SRandall Stewart 				lchk->rec.data.fwd_tsn_cnt++;
44210c0982b8SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt > 3) {
44220c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
44230c0982b8SRandall Stewart 					lchk->rec.data.fwd_tsn_cnt = 0;
44240c0982b8SRandall Stewart 				}
4425830d754dSRandall Stewart 			}
4426830d754dSRandall Stewart 		}
4427830d754dSRandall Stewart 		if (lchk) {
4428830d754dSRandall Stewart 			/* Assure a timer is up */
4429830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4430830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
4431830d754dSRandall Stewart 		}
4432830d754dSRandall Stewart 	}
4433b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
4434f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4435f8829a4aSRandall Stewart 		    rwnd,
4436f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
4437f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
4438f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
443980fefe0aSRandall Stewart 	}
4440f8829a4aSRandall Stewart }
4441f8829a4aSRandall Stewart 
4442cd554309SMichael Tuexen /* EY- nr_sack */
4443cd554309SMichael Tuexen /* Identifies the non-renegable tsns that are revoked*/
4444cd554309SMichael Tuexen static void
4445cd554309SMichael Tuexen sctp_check_for_nr_revoked(struct sctp_tcb *stcb,
4446cd554309SMichael Tuexen     struct sctp_association *asoc, uint32_t cumack,
444763eda93dSMichael Tuexen     uint32_t biggest_tsn_acked)
4448cd554309SMichael Tuexen {
4449cd554309SMichael Tuexen 	struct sctp_tmit_chunk *tp1;
4450cd554309SMichael Tuexen 
4451cd554309SMichael Tuexen 	for (tp1 = TAILQ_FIRST(&asoc->sent_queue); tp1; tp1 = TAILQ_NEXT(tp1, sctp_next)) {
4452cd554309SMichael Tuexen 		if (compare_with_wrap(tp1->rec.data.TSN_seq, cumack,
4453cd554309SMichael Tuexen 		    MAX_TSN)) {
4454cd554309SMichael Tuexen 			/*
4455cd554309SMichael Tuexen 			 * ok this guy is either ACK or MARKED. If it is
4456cd554309SMichael Tuexen 			 * ACKED it has been previously acked but not this
4457cd554309SMichael Tuexen 			 * time i.e. revoked.  If it is MARKED it was ACK'ed
4458cd554309SMichael Tuexen 			 * again.
4459cd554309SMichael Tuexen 			 */
4460cd554309SMichael Tuexen 			if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
4461cd554309SMichael Tuexen 			    MAX_TSN))
4462cd554309SMichael Tuexen 				break;
4463cd554309SMichael Tuexen 
4464cd554309SMichael Tuexen 
4465cd554309SMichael Tuexen 			if (tp1->sent == SCTP_DATAGRAM_NR_ACKED) {
4466cd554309SMichael Tuexen 				/*
4467cd554309SMichael Tuexen 				 * EY! a non-renegable TSN is revoked, need
4468cd554309SMichael Tuexen 				 * to abort the association
4469cd554309SMichael Tuexen 				 */
4470cd554309SMichael Tuexen 				/*
4471cd554309SMichael Tuexen 				 * EY TODO: put in the code to abort the
4472cd554309SMichael Tuexen 				 * assoc.
4473cd554309SMichael Tuexen 				 */
4474cd554309SMichael Tuexen 				return;
4475cd554309SMichael Tuexen 			} else if (tp1->sent == SCTP_DATAGRAM_NR_MARKED) {
4476cd554309SMichael Tuexen 				/* it has been re-acked in this SACK */
4477cd554309SMichael Tuexen 				tp1->sent = SCTP_DATAGRAM_NR_ACKED;
4478cd554309SMichael Tuexen 			}
4479cd554309SMichael Tuexen 		}
4480cd554309SMichael Tuexen 		if (tp1->sent == SCTP_DATAGRAM_UNSENT)
4481cd554309SMichael Tuexen 			break;
4482cd554309SMichael Tuexen 	}
4483cd554309SMichael Tuexen 	return;
4484cd554309SMichael Tuexen }
4485cd554309SMichael Tuexen 
4486f8829a4aSRandall Stewart void
4487cd554309SMichael Tuexen sctp_handle_sack(struct mbuf *m, int offset_seg, int offset_dup,
4488cd554309SMichael Tuexen     struct sctp_tcb *stcb, struct sctp_nets *net_from,
4489cd554309SMichael Tuexen     uint16_t num_seg, uint16_t num_nr_seg, uint16_t num_dup,
4490cd554309SMichael Tuexen     int *abort_now, uint8_t flags,
4491cd554309SMichael Tuexen     uint32_t cum_ack, uint32_t rwnd)
4492f8829a4aSRandall Stewart {
4493f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4494f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
4495cd554309SMichael Tuexen 	uint32_t last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked, this_sack_lowest_newack;
4496bff64a4dSRandall Stewart 	uint32_t sav_cum_ack;
4497f8829a4aSRandall Stewart 	uint16_t wake_him = 0;
4498c105859eSRandall Stewart 	uint32_t send_s = 0;
4499f8829a4aSRandall Stewart 	long j;
4500f8829a4aSRandall Stewart 	int accum_moved = 0;
4501f8829a4aSRandall Stewart 	int will_exit_fast_recovery = 0;
45025e54f665SRandall Stewart 	uint32_t a_rwnd, old_rwnd;
45035e54f665SRandall Stewart 	int win_probe_recovery = 0;
4504c105859eSRandall Stewart 	int win_probe_recovered = 0;
4505f8829a4aSRandall Stewart 	struct sctp_nets *net = NULL;
4506f8829a4aSRandall Stewart 	int nonce_sum_flag, ecn_seg_sums = 0;
4507bff64a4dSRandall Stewart 	int done_once;
4508f8829a4aSRandall Stewart 	uint8_t reneged_all = 0;
4509f8829a4aSRandall Stewart 	uint8_t cmt_dac_flag;
4510f8829a4aSRandall Stewart 
4511f8829a4aSRandall Stewart 	/*
4512f8829a4aSRandall Stewart 	 * we take any chance we can to service our queues since we cannot
4513f8829a4aSRandall Stewart 	 * get awoken when the socket is read from :<
4514f8829a4aSRandall Stewart 	 */
4515f8829a4aSRandall Stewart 	/*
4516f8829a4aSRandall Stewart 	 * Now perform the actual SACK handling: 1) Verify that it is not an
4517f8829a4aSRandall Stewart 	 * old sack, if so discard. 2) If there is nothing left in the send
4518f8829a4aSRandall Stewart 	 * queue (cum-ack is equal to last acked) then you have a duplicate
4519f8829a4aSRandall Stewart 	 * too, update any rwnd change and verify no timers are running.
4520f8829a4aSRandall Stewart 	 * then return. 3) Process any new consequtive data i.e. cum-ack
4521f8829a4aSRandall Stewart 	 * moved process these first and note that it moved. 4) Process any
4522f8829a4aSRandall Stewart 	 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
4523f8829a4aSRandall Stewart 	 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
4524f8829a4aSRandall Stewart 	 * sync up flightsizes and things, stop all timers and also check
4525f8829a4aSRandall Stewart 	 * for shutdown_pending state. If so then go ahead and send off the
4526f8829a4aSRandall Stewart 	 * shutdown. If in shutdown recv, send off the shutdown-ack and
4527f8829a4aSRandall Stewart 	 * start that timer, Ret. 9) Strike any non-acked things and do FR
4528f8829a4aSRandall Stewart 	 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
4529f8829a4aSRandall Stewart 	 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
4530f8829a4aSRandall Stewart 	 * if in shutdown_recv state.
4531f8829a4aSRandall Stewart 	 */
4532f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
4533f8829a4aSRandall Stewart 	/* CMT DAC algo */
4534f8829a4aSRandall Stewart 	this_sack_lowest_newack = 0;
4535f8829a4aSRandall Stewart 	j = 0;
4536f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_slowpath_sack);
4537cd554309SMichael Tuexen 	last_tsn = cum_ack;
4538cd554309SMichael Tuexen 	nonce_sum_flag = flags & SCTP_SACK_NONCE_SUM;
4539cd554309SMichael Tuexen 	cmt_dac_flag = flags & SCTP_SACK_CMT_DAC;
454018e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
454118e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
454218e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
454318e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
454418e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
454518e198d3SRandall Stewart 	}
454618e198d3SRandall Stewart #endif
4547d06c82f1SRandall Stewart 	a_rwnd = rwnd;
4548f8829a4aSRandall Stewart 
4549cd554309SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
4550cd554309SMichael Tuexen 		sctp_misc_ints(SCTP_SACK_LOG_NORMAL, cum_ack,
4551cd554309SMichael Tuexen 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
4552cd554309SMichael Tuexen 	}
45535e54f665SRandall Stewart 	old_rwnd = stcb->asoc.peers_rwnd;
4554b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4555c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4556c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
4557c4739e2fSRandall Stewart 		    0,
4558c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
4559c4739e2fSRandall Stewart 		    __LINE__);
4560c4739e2fSRandall Stewart 	}
4561f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
4562f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
4563b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4564f8829a4aSRandall Stewart 		sctp_log_sack(asoc->last_acked_seq,
4565f8829a4aSRandall Stewart 		    cum_ack,
4566f8829a4aSRandall Stewart 		    0,
4567f8829a4aSRandall Stewart 		    num_seg,
4568f8829a4aSRandall Stewart 		    num_dup,
4569f8829a4aSRandall Stewart 		    SCTP_LOG_NEW_SACK);
457080fefe0aSRandall Stewart 	}
4571b3f1ea41SRandall Stewart 	if ((num_dup) && (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_FR_LOGGING_ENABLE | SCTP_EARLYFR_LOGGING_ENABLE))) {
4572cd554309SMichael Tuexen 		uint16_t i;
4573458303daSRandall Stewart 		uint32_t *dupdata, dblock;
4574f8829a4aSRandall Stewart 
4575cd554309SMichael Tuexen 		for (i = 0; i < num_dup; i++) {
4576cd554309SMichael Tuexen 			dupdata = (uint32_t *) sctp_m_getptr(m, offset_dup + i * sizeof(uint32_t),
4577458303daSRandall Stewart 			    sizeof(uint32_t), (uint8_t *) & dblock);
4578cd554309SMichael Tuexen 			if (dupdata == NULL) {
4579458303daSRandall Stewart 				break;
4580458303daSRandall Stewart 			}
4581cd554309SMichael Tuexen 			sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
4582f8829a4aSRandall Stewart 		}
4583f8829a4aSRandall Stewart 	}
4584b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4585c105859eSRandall Stewart 		/* reality check */
4586c105859eSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4587c105859eSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
4588c105859eSRandall Stewart 			    sctpchunk_listhead);
4589c105859eSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
4590c105859eSRandall Stewart 		} else {
4591c105859eSRandall Stewart 			send_s = asoc->sending_seq;
4592c105859eSRandall Stewart 		}
4593f8829a4aSRandall Stewart 		if (cum_ack == send_s ||
4594f8829a4aSRandall Stewart 		    compare_with_wrap(cum_ack, send_s, MAX_TSN)) {
4595c105859eSRandall Stewart #ifndef INVARIANTS
4596c105859eSRandall Stewart 			struct mbuf *oper;
4597c105859eSRandall Stewart 
4598c105859eSRandall Stewart #endif
4599c105859eSRandall Stewart #ifdef INVARIANTS
4600139bc87fSRandall Stewart 	hopeless_peer:
4601139bc87fSRandall Stewart 			panic("Impossible sack 1");
4602139bc87fSRandall Stewart #else
4603f8829a4aSRandall Stewart 			/*
4604f8829a4aSRandall Stewart 			 * no way, we have not even sent this TSN out yet.
4605f8829a4aSRandall Stewart 			 * Peer is hopelessly messed up with us.
4606f8829a4aSRandall Stewart 			 */
4607f8829a4aSRandall Stewart 	hopeless_peer:
4608f8829a4aSRandall Stewart 			*abort_now = 1;
4609f8829a4aSRandall Stewart 			/* XXX */
4610f8829a4aSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4611f8829a4aSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
4612f8829a4aSRandall Stewart 			if (oper) {
4613f8829a4aSRandall Stewart 				struct sctp_paramhdr *ph;
4614f8829a4aSRandall Stewart 				uint32_t *ippp;
4615f8829a4aSRandall Stewart 
4616139bc87fSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4617f8829a4aSRandall Stewart 				    sizeof(uint32_t);
4618f8829a4aSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
4619f8829a4aSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4620139bc87fSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
4621f8829a4aSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
4622a5d547adSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
4623f8829a4aSRandall Stewart 			}
4624a5d547adSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4625ceaad40aSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
4626f8829a4aSRandall Stewart 			return;
4627139bc87fSRandall Stewart #endif
4628f8829a4aSRandall Stewart 		}
4629f8829a4aSRandall Stewart 	}
4630f8829a4aSRandall Stewart 	/**********************/
4631f8829a4aSRandall Stewart 	/* 1) check the range */
4632f8829a4aSRandall Stewart 	/**********************/
4633f8829a4aSRandall Stewart 	if (compare_with_wrap(asoc->last_acked_seq, last_tsn, MAX_TSN)) {
4634f8829a4aSRandall Stewart 		/* acking something behind */
4635f8829a4aSRandall Stewart 		return;
4636f8829a4aSRandall Stewart 	}
4637bff64a4dSRandall Stewart 	sav_cum_ack = asoc->last_acked_seq;
4638bff64a4dSRandall Stewart 
4639f8829a4aSRandall Stewart 	/* update the Rwnd of the peer */
4640f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue) &&
4641f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->send_queue) &&
4642cd554309SMichael Tuexen 	    (asoc->stream_queue_cnt == 0)) {
4643f8829a4aSRandall Stewart 		/* nothing left on send/sent and strmq */
4644b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4645f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4646f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
464780fefe0aSRandall Stewart 		}
4648f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
4649f8829a4aSRandall Stewart 		if (asoc->sent_queue_retran_cnt) {
4650f8829a4aSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4651f8829a4aSRandall Stewart 		}
4652f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4653f8829a4aSRandall Stewart 			/* SWS sender side engages */
4654f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
4655f8829a4aSRandall Stewart 		}
4656f8829a4aSRandall Stewart 		/* stop any timers */
4657f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4658f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4659a5d547adSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4660b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
4661139bc87fSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4662f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck1);
4663a5d547adSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4664a5d547adSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4665f8829a4aSRandall Stewart 				}
4666f8829a4aSRandall Stewart 			}
4667f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4668f8829a4aSRandall Stewart 			net->flight_size = 0;
4669f8829a4aSRandall Stewart 		}
4670f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4671f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4672f8829a4aSRandall Stewart 		return;
4673f8829a4aSRandall Stewart 	}
4674f8829a4aSRandall Stewart 	/*
4675f8829a4aSRandall Stewart 	 * We init netAckSz and netAckSz2 to 0. These are used to track 2
4676f8829a4aSRandall Stewart 	 * things. The total byte count acked is tracked in netAckSz AND
4677f8829a4aSRandall Stewart 	 * netAck2 is used to track the total bytes acked that are un-
4678f8829a4aSRandall Stewart 	 * amibguious and were never retransmitted. We track these on a per
4679f8829a4aSRandall Stewart 	 * destination address basis.
4680f8829a4aSRandall Stewart 	 */
4681f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4682f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
4683f8829a4aSRandall Stewart 		net->net_ack = 0;
4684f8829a4aSRandall Stewart 		net->net_ack2 = 0;
4685f8829a4aSRandall Stewart 
4686f8829a4aSRandall Stewart 		/*
468742551e99SRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
468842551e99SRandall Stewart 		 * SACK processing
4689f8829a4aSRandall Stewart 		 */
4690f8829a4aSRandall Stewart 		net->new_pseudo_cumack = 0;
4691f8829a4aSRandall Stewart 		net->will_exit_fast_recovery = 0;
4692f8829a4aSRandall Stewart 	}
4693f8829a4aSRandall Stewart 	/* process the new consecutive TSN first */
4694f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
4695f8829a4aSRandall Stewart 	while (tp1) {
4696f8829a4aSRandall Stewart 		if (compare_with_wrap(last_tsn, tp1->rec.data.TSN_seq,
4697f8829a4aSRandall Stewart 		    MAX_TSN) ||
4698f8829a4aSRandall Stewart 		    last_tsn == tp1->rec.data.TSN_seq) {
4699f8829a4aSRandall Stewart 			if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4700f8829a4aSRandall Stewart 				/*
4701f8829a4aSRandall Stewart 				 * ECN Nonce: Add the nonce to the sender's
4702f8829a4aSRandall Stewart 				 * nonce sum
4703f8829a4aSRandall Stewart 				 */
4704f8829a4aSRandall Stewart 				asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
4705f8829a4aSRandall Stewart 				accum_moved = 1;
4706f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4707f8829a4aSRandall Stewart 					/*
4708f8829a4aSRandall Stewart 					 * If it is less than ACKED, it is
4709f8829a4aSRandall Stewart 					 * now no-longer in flight. Higher
4710f8829a4aSRandall Stewart 					 * values may occur during marking
4711f8829a4aSRandall Stewart 					 */
4712f8829a4aSRandall Stewart 					if ((tp1->whoTo->dest_state &
4713f8829a4aSRandall Stewart 					    SCTP_ADDR_UNCONFIRMED) &&
4714f8829a4aSRandall Stewart 					    (tp1->snd_count < 2)) {
4715f8829a4aSRandall Stewart 						/*
4716f8829a4aSRandall Stewart 						 * If there was no retran
4717f8829a4aSRandall Stewart 						 * and the address is
4718f8829a4aSRandall Stewart 						 * un-confirmed and we sent
4719f8829a4aSRandall Stewart 						 * there and are now
4720f8829a4aSRandall Stewart 						 * sacked.. its confirmed,
4721f8829a4aSRandall Stewart 						 * mark it so.
4722f8829a4aSRandall Stewart 						 */
4723f8829a4aSRandall Stewart 						tp1->whoTo->dest_state &=
4724f8829a4aSRandall Stewart 						    ~SCTP_ADDR_UNCONFIRMED;
4725f8829a4aSRandall Stewart 					}
4726c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4727b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4728c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4729a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
4730a5d547adSRandall Stewart 							    tp1->book_size,
4731c105859eSRandall Stewart 							    (uintptr_t) tp1->whoTo,
4732a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
473380fefe0aSRandall Stewart 						}
4734c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
4735c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
4736f8829a4aSRandall Stewart 					}
4737f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
4738f8829a4aSRandall Stewart 
4739f8829a4aSRandall Stewart 					/* CMT SFR and DAC algos */
4740f8829a4aSRandall Stewart 					this_sack_lowest_newack = tp1->rec.data.TSN_seq;
4741f8829a4aSRandall Stewart 					tp1->whoTo->saw_newack = 1;
4742f8829a4aSRandall Stewart 
4743f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
4744f8829a4aSRandall Stewart 						/*
4745f8829a4aSRandall Stewart 						 * True non-retransmited
4746f8829a4aSRandall Stewart 						 * chunk
4747f8829a4aSRandall Stewart 						 */
4748f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
4749f8829a4aSRandall Stewart 						    tp1->send_size;
4750f8829a4aSRandall Stewart 
4751f8829a4aSRandall Stewart 						/* update RTO too? */
4752f8829a4aSRandall Stewart 						if (tp1->do_rtt) {
4753f8829a4aSRandall Stewart 							tp1->whoTo->RTO =
4754f8829a4aSRandall Stewart 							    sctp_calculate_rto(stcb,
4755f8829a4aSRandall Stewart 							    asoc, tp1->whoTo,
475618e198d3SRandall Stewart 							    &tp1->sent_rcv_time,
475718e198d3SRandall Stewart 							    sctp_align_safe_nocopy);
4758f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
4759f8829a4aSRandall Stewart 						}
4760f8829a4aSRandall Stewart 					}
4761f8829a4aSRandall Stewart 					/*
4762f8829a4aSRandall Stewart 					 * CMT: CUCv2 algorithm. From the
4763f8829a4aSRandall Stewart 					 * cumack'd TSNs, for each TSN being
4764f8829a4aSRandall Stewart 					 * acked for the first time, set the
4765f8829a4aSRandall Stewart 					 * following variables for the
4766f8829a4aSRandall Stewart 					 * corresp destination.
4767f8829a4aSRandall Stewart 					 * new_pseudo_cumack will trigger a
4768f8829a4aSRandall Stewart 					 * cwnd update.
4769f8829a4aSRandall Stewart 					 * find_(rtx_)pseudo_cumack will
4770f8829a4aSRandall Stewart 					 * trigger search for the next
4771f8829a4aSRandall Stewart 					 * expected (rtx-)pseudo-cumack.
4772f8829a4aSRandall Stewart 					 */
4773f8829a4aSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
4774f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
4775f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
4776f8829a4aSRandall Stewart 
4777f8829a4aSRandall Stewart 
4778b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4779f8829a4aSRandall Stewart 						sctp_log_sack(asoc->last_acked_seq,
4780f8829a4aSRandall Stewart 						    cum_ack,
4781f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
4782f8829a4aSRandall Stewart 						    0,
4783f8829a4aSRandall Stewart 						    0,
4784f8829a4aSRandall Stewart 						    SCTP_LOG_TSN_ACKED);
478580fefe0aSRandall Stewart 					}
4786b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
4787f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
478880fefe0aSRandall Stewart 					}
4789f8829a4aSRandall Stewart 				}
4790f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4791f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4792f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
4793f8829a4aSRandall Stewart 					sctp_audit_log(0xB3,
4794f8829a4aSRandall Stewart 					    (asoc->sent_queue_retran_cnt & 0x000000ff));
4795f8829a4aSRandall Stewart #endif
4796f8829a4aSRandall Stewart 				}
479742551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
479842551e99SRandall Stewart 					/* deflate the cwnd */
479942551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
480042551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
480142551e99SRandall Stewart 				}
4802f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
4803f8829a4aSRandall Stewart 			}
4804f8829a4aSRandall Stewart 		} else {
4805f8829a4aSRandall Stewart 			break;
4806f8829a4aSRandall Stewart 		}
4807f8829a4aSRandall Stewart 		tp1 = TAILQ_NEXT(tp1, sctp_next);
4808f8829a4aSRandall Stewart 	}
4809f8829a4aSRandall Stewart 	biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
4810f8829a4aSRandall Stewart 	/* always set this up to cum-ack */
4811f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = last_tsn;
4812f8829a4aSRandall Stewart 
4813cd554309SMichael Tuexen 	if ((num_seg > 0) || (num_nr_seg > 0)) {
4814f8829a4aSRandall Stewart 
4815f8829a4aSRandall Stewart 		/*
4816f8829a4aSRandall Stewart 		 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
4817f8829a4aSRandall Stewart 		 * to be greater than the cumack. Also reset saw_newack to 0
4818f8829a4aSRandall Stewart 		 * for all dests.
4819f8829a4aSRandall Stewart 		 */
4820f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4821f8829a4aSRandall Stewart 			net->saw_newack = 0;
4822f8829a4aSRandall Stewart 			net->this_sack_highest_newack = last_tsn;
4823f8829a4aSRandall Stewart 		}
4824f8829a4aSRandall Stewart 
4825f8829a4aSRandall Stewart 		/*
4826f8829a4aSRandall Stewart 		 * thisSackHighestGap will increase while handling NEW
4827f8829a4aSRandall Stewart 		 * segments this_sack_highest_newack will increase while
4828f8829a4aSRandall Stewart 		 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
4829f8829a4aSRandall Stewart 		 * used for CMT DAC algo. saw_newack will also change.
4830f8829a4aSRandall Stewart 		 */
4831cd554309SMichael Tuexen 		if (sctp_handle_segments(m, &offset_seg, stcb, asoc, last_tsn, &biggest_tsn_acked,
4832cd554309SMichael Tuexen 		    &biggest_tsn_newly_acked, &this_sack_lowest_newack,
4833cd554309SMichael Tuexen 		    num_seg, num_nr_seg, &ecn_seg_sums)) {
4834cd554309SMichael Tuexen 			wake_him++;
4835cd554309SMichael Tuexen 		}
4836b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4837f8829a4aSRandall Stewart 			/*
4838f8829a4aSRandall Stewart 			 * validate the biggest_tsn_acked in the gap acks if
4839f8829a4aSRandall Stewart 			 * strict adherence is wanted.
4840f8829a4aSRandall Stewart 			 */
4841f8829a4aSRandall Stewart 			if ((biggest_tsn_acked == send_s) ||
4842f8829a4aSRandall Stewart 			    (compare_with_wrap(biggest_tsn_acked, send_s, MAX_TSN))) {
4843f8829a4aSRandall Stewart 				/*
4844f8829a4aSRandall Stewart 				 * peer is either confused or we are under
4845f8829a4aSRandall Stewart 				 * attack. We must abort.
4846f8829a4aSRandall Stewart 				 */
4847f8829a4aSRandall Stewart 				goto hopeless_peer;
4848f8829a4aSRandall Stewart 			}
4849f8829a4aSRandall Stewart 		}
4850f8829a4aSRandall Stewart 	}
4851f8829a4aSRandall Stewart 	/*******************************************/
4852f8829a4aSRandall Stewart 	/* cancel ALL T3-send timer if accum moved */
4853f8829a4aSRandall Stewart 	/*******************************************/
4854b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
4855f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4856f8829a4aSRandall Stewart 			if (net->new_pseudo_cumack)
4857f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4858a5d547adSRandall Stewart 				    stcb, net,
4859a5d547adSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
4860f8829a4aSRandall Stewart 
4861f8829a4aSRandall Stewart 		}
4862f8829a4aSRandall Stewart 	} else {
4863f8829a4aSRandall Stewart 		if (accum_moved) {
4864f8829a4aSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4865f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4866a5d547adSRandall Stewart 				    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
4867f8829a4aSRandall Stewart 			}
4868f8829a4aSRandall Stewart 		}
4869f8829a4aSRandall Stewart 	}
4870f8829a4aSRandall Stewart 	/********************************************/
4871f8829a4aSRandall Stewart 	/* drop the acked chunks from the sendqueue */
4872f8829a4aSRandall Stewart 	/********************************************/
4873f8829a4aSRandall Stewart 	asoc->last_acked_seq = cum_ack;
4874f8829a4aSRandall Stewart 
4875f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
4876f8829a4aSRandall Stewart 	if (tp1 == NULL)
4877f8829a4aSRandall Stewart 		goto done_with_it;
4878f8829a4aSRandall Stewart 	do {
4879f8829a4aSRandall Stewart 		if (compare_with_wrap(tp1->rec.data.TSN_seq, cum_ack,
4880f8829a4aSRandall Stewart 		    MAX_TSN)) {
4881f8829a4aSRandall Stewart 			break;
4882f8829a4aSRandall Stewart 		}
4883f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
4884f8829a4aSRandall Stewart 			/* no more sent on list */
488518e198d3SRandall Stewart 			printf("Warning, tp1->sent == %d and its now acked?\n",
488618e198d3SRandall Stewart 			    tp1->sent);
4887f8829a4aSRandall Stewart 		}
4888f8829a4aSRandall Stewart 		tp2 = TAILQ_NEXT(tp1, sctp_next);
4889f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4890f8829a4aSRandall Stewart 		if (tp1->pr_sctp_on) {
4891f8829a4aSRandall Stewart 			if (asoc->pr_sctp_cnt != 0)
4892f8829a4aSRandall Stewart 				asoc->pr_sctp_cnt--;
4893f8829a4aSRandall Stewart 		}
4894f8829a4aSRandall Stewart 		if ((TAILQ_FIRST(&asoc->sent_queue) == NULL) &&
4895f8829a4aSRandall Stewart 		    (asoc->total_flight > 0)) {
4896f1f73e57SRandall Stewart #ifdef INVARIANTS
4897c105859eSRandall Stewart 			panic("Warning flight size is postive and should be 0");
4898f1f73e57SRandall Stewart #else
4899ad81507eSRandall Stewart 			SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
4900f8829a4aSRandall Stewart 			    asoc->total_flight);
4901f1f73e57SRandall Stewart #endif
4902f8829a4aSRandall Stewart 			asoc->total_flight = 0;
4903f8829a4aSRandall Stewart 		}
4904f8829a4aSRandall Stewart 		if (tp1->data) {
490504ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
4906f8829a4aSRandall Stewart 			sctp_free_bufspace(stcb, asoc, tp1, 1);
4907f8829a4aSRandall Stewart 			sctp_m_freem(tp1->data);
4908810ec536SMichael Tuexen 			if (asoc->peer_supports_prsctp && PR_SCTP_BUF_ENABLED(tp1->flags)) {
4909f8829a4aSRandall Stewart 				asoc->sent_queue_cnt_removeable--;
4910f8829a4aSRandall Stewart 			}
4911f8829a4aSRandall Stewart 		}
4912b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4913f8829a4aSRandall Stewart 			sctp_log_sack(asoc->last_acked_seq,
4914f8829a4aSRandall Stewart 			    cum_ack,
4915f8829a4aSRandall Stewart 			    tp1->rec.data.TSN_seq,
4916f8829a4aSRandall Stewart 			    0,
4917f8829a4aSRandall Stewart 			    0,
4918f8829a4aSRandall Stewart 			    SCTP_LOG_FREE_SENT);
491980fefe0aSRandall Stewart 		}
4920f8829a4aSRandall Stewart 		tp1->data = NULL;
4921f8829a4aSRandall Stewart 		asoc->sent_queue_cnt--;
4922f8829a4aSRandall Stewart 		sctp_free_a_chunk(stcb, tp1);
4923f8829a4aSRandall Stewart 		wake_him++;
4924f8829a4aSRandall Stewart 		tp1 = tp2;
4925f8829a4aSRandall Stewart 	} while (tp1 != NULL);
4926f8829a4aSRandall Stewart 
4927f8829a4aSRandall Stewart done_with_it:
492804ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
4929f8829a4aSRandall Stewart 	if ((wake_him) && (stcb->sctp_socket)) {
4930ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4931ceaad40aSRandall Stewart 		struct socket *so;
4932ceaad40aSRandall Stewart 
4933ceaad40aSRandall Stewart #endif
4934f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4935b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4936f8829a4aSRandall Stewart 			sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_WAKESND_FROM_SACK);
493780fefe0aSRandall Stewart 		}
4938ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4939ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
4940ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
4941ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
4942ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
4943ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
4944ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4945ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4946ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
4947ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
4948ceaad40aSRandall Stewart 			return;
4949ceaad40aSRandall Stewart 		}
4950ceaad40aSRandall Stewart #endif
4951f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4952ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4953ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4954ceaad40aSRandall Stewart #endif
4955f8829a4aSRandall Stewart 	} else {
4956b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4957f8829a4aSRandall Stewart 			sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_NOWAKE_FROM_SACK);
495880fefe0aSRandall Stewart 		}
4959f8829a4aSRandall Stewart 	}
4960f8829a4aSRandall Stewart 
496142551e99SRandall Stewart 	if (asoc->fast_retran_loss_recovery && accum_moved) {
4962f8829a4aSRandall Stewart 		if (compare_with_wrap(asoc->last_acked_seq,
4963f8829a4aSRandall Stewart 		    asoc->fast_recovery_tsn, MAX_TSN) ||
4964f8829a4aSRandall Stewart 		    asoc->last_acked_seq == asoc->fast_recovery_tsn) {
4965f8829a4aSRandall Stewart 			/* Setup so we will exit RFC2582 fast recovery */
4966f8829a4aSRandall Stewart 			will_exit_fast_recovery = 1;
4967f8829a4aSRandall Stewart 		}
4968f8829a4aSRandall Stewart 	}
4969f8829a4aSRandall Stewart 	/*
4970f8829a4aSRandall Stewart 	 * Check for revoked fragments:
4971f8829a4aSRandall Stewart 	 *
4972f8829a4aSRandall Stewart 	 * if Previous sack - Had no frags then we can't have any revoked if
4973f8829a4aSRandall Stewart 	 * Previous sack - Had frag's then - If we now have frags aka
4974f8829a4aSRandall Stewart 	 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
4975f8829a4aSRandall Stewart 	 * some of them. else - The peer revoked all ACKED fragments, since
4976f8829a4aSRandall Stewart 	 * we had some before and now we have NONE.
4977f8829a4aSRandall Stewart 	 */
4978f8829a4aSRandall Stewart 
4979f42a358aSRandall Stewart 	if (num_seg)
4980c105859eSRandall Stewart 		sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
4981f8829a4aSRandall Stewart 	else if (asoc->saw_sack_with_frags) {
4982f8829a4aSRandall Stewart 		int cnt_revoked = 0;
4983f8829a4aSRandall Stewart 
4984f8829a4aSRandall Stewart 		tp1 = TAILQ_FIRST(&asoc->sent_queue);
4985f8829a4aSRandall Stewart 		if (tp1 != NULL) {
4986f8829a4aSRandall Stewart 			/* Peer revoked all dg's marked or acked */
4987f8829a4aSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4988cd554309SMichael Tuexen 				/*
4989cd554309SMichael Tuexen 				 * EY- maybe check only if it is nr_acked
4990cd554309SMichael Tuexen 				 * nr_marked may not be possible
4991cd554309SMichael Tuexen 				 */
4992cd554309SMichael Tuexen 				if ((tp1->sent == SCTP_DATAGRAM_NR_ACKED) ||
4993cd554309SMichael Tuexen 				    (tp1->sent == SCTP_DATAGRAM_NR_MARKED)) {
4994cd554309SMichael Tuexen 					/*
4995cd554309SMichael Tuexen 					 * EY! - TODO: Something previously
4996cd554309SMichael Tuexen 					 * nr_gapped is reneged, abort the
4997cd554309SMichael Tuexen 					 * association
4998cd554309SMichael Tuexen 					 */
4999cd554309SMichael Tuexen 					return;
5000cd554309SMichael Tuexen 				}
5001f8829a4aSRandall Stewart 				if ((tp1->sent > SCTP_DATAGRAM_RESEND) &&
5002f8829a4aSRandall Stewart 				    (tp1->sent < SCTP_FORWARD_TSN_SKIP)) {
5003f8829a4aSRandall Stewart 					tp1->sent = SCTP_DATAGRAM_SENT;
5004b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
5005c105859eSRandall Stewart 						sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
5006c105859eSRandall Stewart 						    tp1->whoTo->flight_size,
5007c105859eSRandall Stewart 						    tp1->book_size,
5008c105859eSRandall Stewart 						    (uintptr_t) tp1->whoTo,
5009c105859eSRandall Stewart 						    tp1->rec.data.TSN_seq);
501080fefe0aSRandall Stewart 					}
5011c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
5012c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
5013a5d547adSRandall Stewart 					tp1->rec.data.chunk_was_revoked = 1;
501442551e99SRandall Stewart 					/*
501542551e99SRandall Stewart 					 * To ensure that this increase in
501642551e99SRandall Stewart 					 * flightsize, which is artificial,
501742551e99SRandall Stewart 					 * does not throttle the sender, we
501842551e99SRandall Stewart 					 * also increase the cwnd
501942551e99SRandall Stewart 					 * artificially.
502042551e99SRandall Stewart 					 */
502142551e99SRandall Stewart 					tp1->whoTo->cwnd += tp1->book_size;
5022f8829a4aSRandall Stewart 					cnt_revoked++;
5023f8829a4aSRandall Stewart 				}
5024f8829a4aSRandall Stewart 			}
5025f8829a4aSRandall Stewart 			if (cnt_revoked) {
5026f8829a4aSRandall Stewart 				reneged_all = 1;
5027f8829a4aSRandall Stewart 			}
5028f8829a4aSRandall Stewart 		}
5029f8829a4aSRandall Stewart 		asoc->saw_sack_with_frags = 0;
5030f8829a4aSRandall Stewart 	}
5031f8829a4aSRandall Stewart 	if (num_seg)
5032f8829a4aSRandall Stewart 		asoc->saw_sack_with_frags = 1;
5033f8829a4aSRandall Stewart 	else
5034f8829a4aSRandall Stewart 		asoc->saw_sack_with_frags = 0;
5035f8829a4aSRandall Stewart 
5036cd554309SMichael Tuexen 	/* EY! - not sure about if there should be an IF */
5037cd554309SMichael Tuexen 	if (num_nr_seg > 0)
5038cd554309SMichael Tuexen 		sctp_check_for_nr_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
5039cd554309SMichael Tuexen 
5040b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
5041b54d3a6cSRandall Stewart 	asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
5042f8829a4aSRandall Stewart 
5043f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
5044f8829a4aSRandall Stewart 		/* nothing left in-flight */
5045f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5046f8829a4aSRandall Stewart 			/* stop all timers */
5047b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
5048139bc87fSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
5049f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
5050a5d547adSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
5051a5d547adSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_29);
5052f8829a4aSRandall Stewart 				}
5053f8829a4aSRandall Stewart 			}
5054f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5055a5d547adSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
5056f8829a4aSRandall Stewart 			net->flight_size = 0;
5057f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
5058f8829a4aSRandall Stewart 		}
5059f8829a4aSRandall Stewart 		asoc->total_flight = 0;
5060f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
5061f8829a4aSRandall Stewart 	}
5062f8829a4aSRandall Stewart 	/**********************************/
5063f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
5064f8829a4aSRandall Stewart 	/**********************************/
5065f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
5066f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
5067b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
5068f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
5069f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
507080fefe0aSRandall Stewart 		}
5071f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
5072f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5073f8829a4aSRandall Stewart 			/* SWS sender side engages */
5074f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
5075f8829a4aSRandall Stewart 		}
5076f8829a4aSRandall Stewart 		/* clean up */
5077f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
5078f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
5079f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
5080f8829a4aSRandall Stewart 		    (asoc->locked_on_sending)
5081f8829a4aSRandall Stewart 		    ) {
5082f8829a4aSRandall Stewart 			struct sctp_stream_queue_pending *sp;
5083f8829a4aSRandall Stewart 
5084f8829a4aSRandall Stewart 			/*
5085f8829a4aSRandall Stewart 			 * I may be in a state where we got all across.. but
5086f8829a4aSRandall Stewart 			 * cannot write more due to a shutdown... we abort
5087f8829a4aSRandall Stewart 			 * since the user did not indicate EOR in this case.
5088f8829a4aSRandall Stewart 			 */
5089f8829a4aSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
5090f8829a4aSRandall Stewart 			    sctp_streamhead);
50912afb3e84SRandall Stewart 			if ((sp) && (sp->length == 0)) {
5092f8829a4aSRandall Stewart 				asoc->locked_on_sending = NULL;
50932afb3e84SRandall Stewart 				if (sp->msg_is_complete) {
5094f8829a4aSRandall Stewart 					asoc->stream_queue_cnt--;
50952afb3e84SRandall Stewart 				} else {
50962afb3e84SRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
50972afb3e84SRandall Stewart 					asoc->stream_queue_cnt--;
50982afb3e84SRandall Stewart 				}
5099f8829a4aSRandall Stewart 			}
5100f8829a4aSRandall Stewart 		}
5101f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
5102f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
5103f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
5104f8829a4aSRandall Stewart 				/* Need to abort here */
5105f8829a4aSRandall Stewart 				struct mbuf *oper;
5106f8829a4aSRandall Stewart 
5107f8829a4aSRandall Stewart 		abort_out_now:
5108f8829a4aSRandall Stewart 				*abort_now = 1;
5109f8829a4aSRandall Stewart 				/* XXX */
5110f8829a4aSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
5111f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
5112f8829a4aSRandall Stewart 				if (oper) {
5113f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
5114f8829a4aSRandall Stewart 					uint32_t *ippp;
5115f8829a4aSRandall Stewart 
5116139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
5117f8829a4aSRandall Stewart 					    sizeof(uint32_t);
5118f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
5119f8829a4aSRandall Stewart 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
5120139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
5121f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
5122a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_31);
5123f8829a4aSRandall Stewart 				}
5124a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
5125ceaad40aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
5126f8829a4aSRandall Stewart 				return;
5127f8829a4aSRandall Stewart 			} else {
5128f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
5129f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
5130f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5131f42a358aSRandall Stewart 				}
5132c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
5133b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
5134f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
5135f8829a4aSRandall Stewart 				sctp_send_shutdown(stcb,
5136f8829a4aSRandall Stewart 				    stcb->asoc.primary_destination);
5137f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
5138f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
5139f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
5140f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
5141f8829a4aSRandall Stewart 			}
5142f8829a4aSRandall Stewart 			return;
5143f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
5144f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
5145f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
5146f8829a4aSRandall Stewart 				goto abort_out_now;
5147f8829a4aSRandall Stewart 			}
5148f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5149c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
5150b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
5151f8829a4aSRandall Stewart 			sctp_send_shutdown_ack(stcb,
5152f8829a4aSRandall Stewart 			    stcb->asoc.primary_destination);
5153f8829a4aSRandall Stewart 
5154f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
5155f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb, asoc->primary_destination);
5156f8829a4aSRandall Stewart 			return;
5157f8829a4aSRandall Stewart 		}
5158f8829a4aSRandall Stewart 	}
5159f8829a4aSRandall Stewart 	/*
5160f8829a4aSRandall Stewart 	 * Now here we are going to recycle net_ack for a different use...
5161f8829a4aSRandall Stewart 	 * HEADS UP.
5162f8829a4aSRandall Stewart 	 */
5163f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5164f8829a4aSRandall Stewart 		net->net_ack = 0;
5165f8829a4aSRandall Stewart 	}
5166f8829a4aSRandall Stewart 
5167f8829a4aSRandall Stewart 	/*
5168f8829a4aSRandall Stewart 	 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
5169f8829a4aSRandall Stewart 	 * to be done. Setting this_sack_lowest_newack to the cum_ack will
5170f8829a4aSRandall Stewart 	 * automatically ensure that.
5171f8829a4aSRandall Stewart 	 */
5172b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac) && (cmt_dac_flag == 0)) {
5173f8829a4aSRandall Stewart 		this_sack_lowest_newack = cum_ack;
5174f8829a4aSRandall Stewart 	}
5175cd554309SMichael Tuexen 	if ((num_seg > 0) || (num_nr_seg > 0)) {
5176f8829a4aSRandall Stewart 		sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
5177f8829a4aSRandall Stewart 		    biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
5178f8829a4aSRandall Stewart 	}
5179b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
5180b54d3a6cSRandall Stewart 	asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
5181f8829a4aSRandall Stewart 
5182f8829a4aSRandall Stewart 	/******************************************************************
5183f8829a4aSRandall Stewart 	 *  Here we do the stuff with ECN Nonce checking.
5184f8829a4aSRandall Stewart 	 *  We basically check to see if the nonce sum flag was incorrect
5185f8829a4aSRandall Stewart 	 *  or if resynchronization needs to be done. Also if we catch a
5186f8829a4aSRandall Stewart 	 *  misbehaving receiver we give him the kick.
5187f8829a4aSRandall Stewart 	 ******************************************************************/
5188f8829a4aSRandall Stewart 
5189f8829a4aSRandall Stewart 	if (asoc->ecn_nonce_allowed) {
5190f8829a4aSRandall Stewart 		if (asoc->nonce_sum_check) {
5191f8829a4aSRandall Stewart 			if (nonce_sum_flag != ((asoc->nonce_sum_expect_base + ecn_seg_sums) & SCTP_SACK_NONCE_SUM)) {
5192f8829a4aSRandall Stewart 				if (asoc->nonce_wait_for_ecne == 0) {
5193f8829a4aSRandall Stewart 					struct sctp_tmit_chunk *lchk;
5194f8829a4aSRandall Stewart 
5195f8829a4aSRandall Stewart 					lchk = TAILQ_FIRST(&asoc->send_queue);
5196f8829a4aSRandall Stewart 					asoc->nonce_wait_for_ecne = 1;
5197f8829a4aSRandall Stewart 					if (lchk) {
5198f8829a4aSRandall Stewart 						asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
5199f8829a4aSRandall Stewart 					} else {
5200f8829a4aSRandall Stewart 						asoc->nonce_wait_tsn = asoc->sending_seq;
5201f8829a4aSRandall Stewart 					}
5202f8829a4aSRandall Stewart 				} else {
5203f8829a4aSRandall Stewart 					if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
5204f8829a4aSRandall Stewart 					    (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
5205f8829a4aSRandall Stewart 						/*
5206f8829a4aSRandall Stewart 						 * Misbehaving peer. We need
5207f8829a4aSRandall Stewart 						 * to react to this guy
5208f8829a4aSRandall Stewart 						 */
5209f8829a4aSRandall Stewart 						asoc->ecn_allowed = 0;
5210f8829a4aSRandall Stewart 						asoc->ecn_nonce_allowed = 0;
5211f8829a4aSRandall Stewart 					}
5212f8829a4aSRandall Stewart 				}
5213f8829a4aSRandall Stewart 			}
5214f8829a4aSRandall Stewart 		} else {
5215f8829a4aSRandall Stewart 			/* See if Resynchronization Possible */
5216f8829a4aSRandall Stewart 			if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
5217f8829a4aSRandall Stewart 				asoc->nonce_sum_check = 1;
5218f8829a4aSRandall Stewart 				/*
5219f8829a4aSRandall Stewart 				 * now we must calculate what the base is.
5220f8829a4aSRandall Stewart 				 * We do this based on two things, we know
5221f8829a4aSRandall Stewart 				 * the total's for all the segments
5222f8829a4aSRandall Stewart 				 * gap-acked in the SACK, its stored in
5223f8829a4aSRandall Stewart 				 * ecn_seg_sums. We also know the SACK's
5224f8829a4aSRandall Stewart 				 * nonce sum, its in nonce_sum_flag. So we
5225f8829a4aSRandall Stewart 				 * can build a truth table to back-calculate
5226f8829a4aSRandall Stewart 				 * the new value of
5227f8829a4aSRandall Stewart 				 * asoc->nonce_sum_expect_base:
5228f8829a4aSRandall Stewart 				 *
5229f8829a4aSRandall Stewart 				 * SACK-flag-Value         Seg-Sums Base 0 0 0
5230f8829a4aSRandall Stewart 				 * 1                    0 1 0 1 1 1
5231f8829a4aSRandall Stewart 				 * 1 0
5232f8829a4aSRandall Stewart 				 */
5233f8829a4aSRandall Stewart 				asoc->nonce_sum_expect_base = (ecn_seg_sums ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
5234f8829a4aSRandall Stewart 			}
5235f8829a4aSRandall Stewart 		}
5236f8829a4aSRandall Stewart 	}
5237f8829a4aSRandall Stewart 	/* Now are we exiting loss recovery ? */
5238f8829a4aSRandall Stewart 	if (will_exit_fast_recovery) {
5239f8829a4aSRandall Stewart 		/* Ok, we must exit fast recovery */
5240f8829a4aSRandall Stewart 		asoc->fast_retran_loss_recovery = 0;
5241f8829a4aSRandall Stewart 	}
5242f8829a4aSRandall Stewart 	if ((asoc->sat_t3_loss_recovery) &&
5243f8829a4aSRandall Stewart 	    ((compare_with_wrap(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn,
5244f8829a4aSRandall Stewart 	    MAX_TSN) ||
5245f8829a4aSRandall Stewart 	    (asoc->last_acked_seq == asoc->sat_t3_recovery_tsn)))) {
5246f8829a4aSRandall Stewart 		/* end satellite t3 loss recovery */
5247f8829a4aSRandall Stewart 		asoc->sat_t3_loss_recovery = 0;
5248f8829a4aSRandall Stewart 	}
524942551e99SRandall Stewart 	/*
525042551e99SRandall Stewart 	 * CMT Fast recovery
525142551e99SRandall Stewart 	 */
5252f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5253f8829a4aSRandall Stewart 		if (net->will_exit_fast_recovery) {
5254f8829a4aSRandall Stewart 			/* Ok, we must exit fast recovery */
5255f8829a4aSRandall Stewart 			net->fast_retran_loss_recovery = 0;
5256f8829a4aSRandall Stewart 		}
5257f8829a4aSRandall Stewart 	}
5258f8829a4aSRandall Stewart 
5259f8829a4aSRandall Stewart 	/* Adjust and set the new rwnd value */
5260b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
5261f8829a4aSRandall Stewart 		sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
5262b3f1ea41SRandall Stewart 		    asoc->peers_rwnd, asoc->total_flight, (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)), a_rwnd);
526380fefe0aSRandall Stewart 	}
5264f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
5265b3f1ea41SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
5266f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5267f8829a4aSRandall Stewart 		/* SWS sender side engages */
5268f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
5269f8829a4aSRandall Stewart 	}
52705e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
52715e54f665SRandall Stewart 		win_probe_recovery = 1;
52725e54f665SRandall Stewart 	}
5273f8829a4aSRandall Stewart 	/*
5274f8829a4aSRandall Stewart 	 * Now we must setup so we have a timer up for anyone with
5275f8829a4aSRandall Stewart 	 * outstanding data.
5276f8829a4aSRandall Stewart 	 */
5277bff64a4dSRandall Stewart 	done_once = 0;
5278a5d547adSRandall Stewart again:
5279a5d547adSRandall Stewart 	j = 0;
5280f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
52815e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
5282c105859eSRandall Stewart 			win_probe_recovered = 1;
52835e54f665SRandall Stewart 			/*-
52845e54f665SRandall Stewart 			 * Find first chunk that was used with
52855e54f665SRandall Stewart 			 * window probe and clear the event. Put
52865e54f665SRandall Stewart 			 * it back into the send queue as if has
52875e54f665SRandall Stewart 			 * not been sent.
52885e54f665SRandall Stewart 			 */
52895e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
52905e54f665SRandall Stewart 				if (tp1->window_probe) {
5291c105859eSRandall Stewart 					sctp_window_probe_recovery(stcb, asoc, net, tp1);
52925e54f665SRandall Stewart 					break;
52935e54f665SRandall Stewart 				}
52945e54f665SRandall Stewart 			}
52955e54f665SRandall Stewart 		}
5296f8829a4aSRandall Stewart 		if (net->flight_size) {
5297a5d547adSRandall Stewart 			j++;
5298cd554309SMichael Tuexen 			if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5299f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5300f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, net);
5301cd554309SMichael Tuexen 			}
53025171328bSRandall Stewart 			if (net->window_probe) {
5303cd554309SMichael Tuexen 				net->window_probe = 0;
53045171328bSRandall Stewart 			}
5305c105859eSRandall Stewart 		} else {
53065171328bSRandall Stewart 			if (net->window_probe) {
53075171328bSRandall Stewart 				/*
53085171328bSRandall Stewart 				 * In window probes we must assure a timer
53095171328bSRandall Stewart 				 * is still running there
53105171328bSRandall Stewart 				 */
53115171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
53125171328bSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_SEND,
53135171328bSRandall Stewart 					    stcb->sctp_ep, stcb, net);
53145171328bSRandall Stewart 
53155171328bSRandall Stewart 				}
53165171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5317c105859eSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5318c105859eSRandall Stewart 				    stcb, net,
5319c105859eSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
5320c105859eSRandall Stewart 			}
5321b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
5322c105859eSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
5323c105859eSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
5324c105859eSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
5325c105859eSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
5326c105859eSRandall Stewart 				}
5327c105859eSRandall Stewart 			}
5328f8829a4aSRandall Stewart 		}
5329f8829a4aSRandall Stewart 	}
5330bff64a4dSRandall Stewart 	if ((j == 0) &&
5331bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
5332bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
5333c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
5334bff64a4dSRandall Stewart 	    (done_once == 0)) {
53350c0982b8SRandall Stewart 		/*
53360c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
53370c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
53380c0982b8SRandall Stewart 		 */
53390c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
5340a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5341a5d547adSRandall Stewart 				net->flight_size = 0;
5342a5d547adSRandall Stewart 			}
5343a5d547adSRandall Stewart 			asoc->total_flight = 0;
5344a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
5345a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
5346a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5347a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5348c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
5349c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
5350a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5351a5d547adSRandall Stewart 					asoc->sent_queue_retran_cnt++;
5352a5d547adSRandall Stewart 				}
5353a5d547adSRandall Stewart 			}
53540c0982b8SRandall Stewart 		}
5355bff64a4dSRandall Stewart 		done_once = 1;
5356a5d547adSRandall Stewart 		goto again;
5357a5d547adSRandall Stewart 	}
5358cd554309SMichael Tuexen 	/*********************************************/
5359cd554309SMichael Tuexen 	/* Here we perform PR-SCTP procedures        */
5360cd554309SMichael Tuexen 	/* (section 4.2)                             */
5361cd554309SMichael Tuexen 	/*********************************************/
5362cd554309SMichael Tuexen 	/* C1. update advancedPeerAckPoint */
5363dfb11ef8SRandall Stewart 	if (compare_with_wrap(cum_ack, asoc->advanced_peer_ack_point, MAX_TSN)) {
5364dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cum_ack;
5365dfb11ef8SRandall Stewart 	}
5366830d754dSRandall Stewart 	/* C2. try to further move advancedPeerAckPoint ahead */
5367830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
5368830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
5369830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
5370830d754dSRandall Stewart 
5371830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
5372830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
5373830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
5374830d754dSRandall Stewart 		if (compare_with_wrap(asoc->advanced_peer_ack_point, cum_ack,
5375830d754dSRandall Stewart 		    MAX_TSN)) {
5376830d754dSRandall Stewart 			/*
5377830d754dSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing for notes
5378830d754dSRandall Stewart 			 * on issues that will occur when the ECN NONCE
5379830d754dSRandall Stewart 			 * stuff is put into SCTP for cross checking.
5380830d754dSRandall Stewart 			 */
53810c0982b8SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
53820c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
53830c0982b8SRandall Stewart 				    0xee, cum_ack, asoc->advanced_peer_ack_point,
53840c0982b8SRandall Stewart 				    old_adv_peer_ack_point);
53850c0982b8SRandall Stewart 			}
5386830d754dSRandall Stewart 			if (compare_with_wrap(asoc->advanced_peer_ack_point, old_adv_peer_ack_point,
5387830d754dSRandall Stewart 			    MAX_TSN)) {
5388830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
5389830d754dSRandall Stewart 				/*
5390830d754dSRandall Stewart 				 * ECN Nonce: Disable Nonce Sum check when
5391830d754dSRandall Stewart 				 * FWD TSN is sent and store resync tsn
5392830d754dSRandall Stewart 				 */
5393830d754dSRandall Stewart 				asoc->nonce_sum_check = 0;
5394830d754dSRandall Stewart 				asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
53950c0982b8SRandall Stewart 			} else if (lchk) {
53960c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
53970c0982b8SRandall Stewart 				lchk->rec.data.fwd_tsn_cnt++;
53980c0982b8SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt > 3) {
53990c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
54000c0982b8SRandall Stewart 					lchk->rec.data.fwd_tsn_cnt = 0;
54010c0982b8SRandall Stewart 				}
5402830d754dSRandall Stewart 			}
5403830d754dSRandall Stewart 		}
5404830d754dSRandall Stewart 		if (lchk) {
5405830d754dSRandall Stewart 			/* Assure a timer is up */
5406830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5407830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
5408830d754dSRandall Stewart 		}
5409830d754dSRandall Stewart 	}
5410b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
5411f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
5412f8829a4aSRandall Stewart 		    a_rwnd,
5413f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
5414f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
5415f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
541680fefe0aSRandall Stewart 	}
5417f8829a4aSRandall Stewart }
5418f8829a4aSRandall Stewart 
5419f8829a4aSRandall Stewart void
5420f8829a4aSRandall Stewart sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp,
5421f8829a4aSRandall Stewart     struct sctp_nets *netp, int *abort_flag)
5422f8829a4aSRandall Stewart {
5423f8829a4aSRandall Stewart 	/* Copy cum-ack */
5424f8829a4aSRandall Stewart 	uint32_t cum_ack, a_rwnd;
5425f8829a4aSRandall Stewart 
5426f8829a4aSRandall Stewart 	cum_ack = ntohl(cp->cumulative_tsn_ack);
5427f8829a4aSRandall Stewart 	/* Arrange so a_rwnd does NOT change */
5428f8829a4aSRandall Stewart 	a_rwnd = stcb->asoc.peers_rwnd + stcb->asoc.total_flight;
5429f8829a4aSRandall Stewart 
5430f8829a4aSRandall Stewart 	/* Now call the express sack handling */
5431f8829a4aSRandall Stewart 	sctp_express_handle_sack(stcb, cum_ack, a_rwnd, 0, abort_flag);
5432f8829a4aSRandall Stewart }
5433f8829a4aSRandall Stewart 
5434f8829a4aSRandall Stewart static void
5435f8829a4aSRandall Stewart sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
5436f8829a4aSRandall Stewart     struct sctp_stream_in *strmin)
5437f8829a4aSRandall Stewart {
5438f8829a4aSRandall Stewart 	struct sctp_queued_to_read *ctl, *nctl;
5439f8829a4aSRandall Stewart 	struct sctp_association *asoc;
5440f8829a4aSRandall Stewart 	int tt;
5441f8829a4aSRandall Stewart 
5442f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
5443f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered;
5444f8829a4aSRandall Stewart 	/*
5445f8829a4aSRandall Stewart 	 * First deliver anything prior to and including the stream no that
5446f8829a4aSRandall Stewart 	 * came in
5447f8829a4aSRandall Stewart 	 */
5448f8829a4aSRandall Stewart 	ctl = TAILQ_FIRST(&strmin->inqueue);
5449f8829a4aSRandall Stewart 	while (ctl) {
5450f8829a4aSRandall Stewart 		nctl = TAILQ_NEXT(ctl, next);
5451f8829a4aSRandall Stewart 		if (compare_with_wrap(tt, ctl->sinfo_ssn, MAX_SEQ) ||
5452f8829a4aSRandall Stewart 		    (tt == ctl->sinfo_ssn)) {
5453f8829a4aSRandall Stewart 			/* this is deliverable now */
5454f8829a4aSRandall Stewart 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5455f8829a4aSRandall Stewart 			/* subtract pending on streams */
5456f8829a4aSRandall Stewart 			asoc->size_on_all_streams -= ctl->length;
5457f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_all_streams);
5458f8829a4aSRandall Stewart 			/* deliver it to at least the delivery-q */
5459f8829a4aSRandall Stewart 			if (stcb->sctp_socket) {
5460830d754dSRandall Stewart 				/* EY need the tsn info for calculating nr */
5461f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
5462f8829a4aSRandall Stewart 				    ctl,
5463cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
546477acdc25SRandall Stewart 				sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
5465f8829a4aSRandall Stewart 			}
5466f8829a4aSRandall Stewart 		} else {
5467f8829a4aSRandall Stewart 			/* no more delivery now. */
5468f8829a4aSRandall Stewart 			break;
5469f8829a4aSRandall Stewart 		}
5470f8829a4aSRandall Stewart 		ctl = nctl;
5471f8829a4aSRandall Stewart 	}
5472f8829a4aSRandall Stewart 	/*
5473f8829a4aSRandall Stewart 	 * now we must deliver things in queue the normal way  if any are
5474f8829a4aSRandall Stewart 	 * now ready.
5475f8829a4aSRandall Stewart 	 */
5476f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered + 1;
5477f8829a4aSRandall Stewart 	ctl = TAILQ_FIRST(&strmin->inqueue);
5478f8829a4aSRandall Stewart 	while (ctl) {
5479f8829a4aSRandall Stewart 		nctl = TAILQ_NEXT(ctl, next);
5480f8829a4aSRandall Stewart 		if (tt == ctl->sinfo_ssn) {
5481f8829a4aSRandall Stewart 			/* this is deliverable now */
5482f8829a4aSRandall Stewart 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5483f8829a4aSRandall Stewart 			/* subtract pending on streams */
5484f8829a4aSRandall Stewart 			asoc->size_on_all_streams -= ctl->length;
5485f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_all_streams);
5486f8829a4aSRandall Stewart 			/* deliver it to at least the delivery-q */
5487f8829a4aSRandall Stewart 			strmin->last_sequence_delivered = ctl->sinfo_ssn;
5488f8829a4aSRandall Stewart 			if (stcb->sctp_socket) {
5489f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
5490f8829a4aSRandall Stewart 				    ctl,
5491cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
549277acdc25SRandall Stewart 				sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
549377acdc25SRandall Stewart 
5494f8829a4aSRandall Stewart 			}
5495f8829a4aSRandall Stewart 			tt = strmin->last_sequence_delivered + 1;
5496f8829a4aSRandall Stewart 		} else {
5497f8829a4aSRandall Stewart 			break;
5498f8829a4aSRandall Stewart 		}
5499f8829a4aSRandall Stewart 		ctl = nctl;
5500f8829a4aSRandall Stewart 	}
5501f8829a4aSRandall Stewart }
5502f8829a4aSRandall Stewart 
55038933fa13SRandall Stewart static void
55048933fa13SRandall Stewart sctp_flush_reassm_for_str_seq(struct sctp_tcb *stcb,
55058933fa13SRandall Stewart     struct sctp_association *asoc,
55068933fa13SRandall Stewart     uint16_t stream, uint16_t seq)
55078933fa13SRandall Stewart {
55088933fa13SRandall Stewart 	struct sctp_tmit_chunk *chk, *at;
55098933fa13SRandall Stewart 
55108933fa13SRandall Stewart 	if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
55118933fa13SRandall Stewart 		/* For each one on here see if we need to toss it */
55128933fa13SRandall Stewart 		/*
55138933fa13SRandall Stewart 		 * For now large messages held on the reasmqueue that are
55148933fa13SRandall Stewart 		 * complete will be tossed too. We could in theory do more
55158933fa13SRandall Stewart 		 * work to spin through and stop after dumping one msg aka
55168933fa13SRandall Stewart 		 * seeing the start of a new msg at the head, and call the
55178933fa13SRandall Stewart 		 * delivery function... to see if it can be delivered... But
55188933fa13SRandall Stewart 		 * for now we just dump everything on the queue.
55198933fa13SRandall Stewart 		 */
55208933fa13SRandall Stewart 		chk = TAILQ_FIRST(&asoc->reasmqueue);
55218933fa13SRandall Stewart 		while (chk) {
55228933fa13SRandall Stewart 			at = TAILQ_NEXT(chk, sctp_next);
55238e71b694SMichael Tuexen 			/*
55248e71b694SMichael Tuexen 			 * Do not toss it if on a different stream or marked
55258e71b694SMichael Tuexen 			 * for unordered delivery in which case the stream
55268e71b694SMichael Tuexen 			 * sequence number has no meaning.
55278e71b694SMichael Tuexen 			 */
55288e71b694SMichael Tuexen 			if ((chk->rec.data.stream_number != stream) ||
55298e71b694SMichael Tuexen 			    ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == SCTP_DATA_UNORDERED)) {
55308933fa13SRandall Stewart 				chk = at;
55318933fa13SRandall Stewart 				continue;
55328933fa13SRandall Stewart 			}
55338933fa13SRandall Stewart 			if (chk->rec.data.stream_seq == seq) {
55348933fa13SRandall Stewart 				/* It needs to be tossed */
55358933fa13SRandall Stewart 				TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
55368933fa13SRandall Stewart 				if (compare_with_wrap(chk->rec.data.TSN_seq,
55378933fa13SRandall Stewart 				    asoc->tsn_last_delivered, MAX_TSN)) {
55388933fa13SRandall Stewart 					asoc->tsn_last_delivered =
55398933fa13SRandall Stewart 					    chk->rec.data.TSN_seq;
55408933fa13SRandall Stewart 					asoc->str_of_pdapi =
55418933fa13SRandall Stewart 					    chk->rec.data.stream_number;
55428933fa13SRandall Stewart 					asoc->ssn_of_pdapi =
55438933fa13SRandall Stewart 					    chk->rec.data.stream_seq;
55448933fa13SRandall Stewart 					asoc->fragment_flags =
55458933fa13SRandall Stewart 					    chk->rec.data.rcv_flags;
55468933fa13SRandall Stewart 				}
55478933fa13SRandall Stewart 				asoc->size_on_reasm_queue -= chk->send_size;
55488933fa13SRandall Stewart 				sctp_ucount_decr(asoc->cnt_on_reasm_queue);
55498933fa13SRandall Stewart 
55508933fa13SRandall Stewart 				/* Clear up any stream problem */
55518933fa13SRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) !=
55528933fa13SRandall Stewart 				    SCTP_DATA_UNORDERED &&
55538933fa13SRandall Stewart 				    (compare_with_wrap(chk->rec.data.stream_seq,
55548933fa13SRandall Stewart 				    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered,
55558933fa13SRandall Stewart 				    MAX_SEQ))) {
55568933fa13SRandall Stewart 					/*
55578933fa13SRandall Stewart 					 * We must dump forward this streams
55588933fa13SRandall Stewart 					 * sequence number if the chunk is
55598933fa13SRandall Stewart 					 * not unordered that is being
55608933fa13SRandall Stewart 					 * skipped. There is a chance that
55618933fa13SRandall Stewart 					 * if the peer does not include the
55628933fa13SRandall Stewart 					 * last fragment in its FWD-TSN we
55638933fa13SRandall Stewart 					 * WILL have a problem here since
55648933fa13SRandall Stewart 					 * you would have a partial chunk in
55658933fa13SRandall Stewart 					 * queue that may not be
55668933fa13SRandall Stewart 					 * deliverable. Also if a Partial
55678933fa13SRandall Stewart 					 * delivery API as started the user
55688933fa13SRandall Stewart 					 * may get a partial chunk. The next
55698933fa13SRandall Stewart 					 * read returning a new chunk...
55708933fa13SRandall Stewart 					 * really ugly but I see no way
55718933fa13SRandall Stewart 					 * around it! Maybe a notify??
55728933fa13SRandall Stewart 					 */
55738933fa13SRandall Stewart 					asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered =
55748933fa13SRandall Stewart 					    chk->rec.data.stream_seq;
55758933fa13SRandall Stewart 				}
55768933fa13SRandall Stewart 				if (chk->data) {
55778933fa13SRandall Stewart 					sctp_m_freem(chk->data);
55788933fa13SRandall Stewart 					chk->data = NULL;
55798933fa13SRandall Stewart 				}
55808933fa13SRandall Stewart 				sctp_free_a_chunk(stcb, chk);
55818933fa13SRandall Stewart 			} else if (compare_with_wrap(chk->rec.data.stream_seq, seq, MAX_SEQ)) {
55828933fa13SRandall Stewart 				/*
55838933fa13SRandall Stewart 				 * If the stream_seq is > than the purging
55848933fa13SRandall Stewart 				 * one, we are done
55858933fa13SRandall Stewart 				 */
55868933fa13SRandall Stewart 				break;
55878933fa13SRandall Stewart 			}
55888933fa13SRandall Stewart 			chk = at;
55898933fa13SRandall Stewart 		}
55908933fa13SRandall Stewart 	}
55918933fa13SRandall Stewart }
55928933fa13SRandall Stewart 
55938933fa13SRandall Stewart 
5594f8829a4aSRandall Stewart void
5595f8829a4aSRandall Stewart sctp_handle_forward_tsn(struct sctp_tcb *stcb,
5596671d309cSRandall Stewart     struct sctp_forward_tsn_chunk *fwd, int *abort_flag, struct mbuf *m, int offset)
5597f8829a4aSRandall Stewart {
5598f8829a4aSRandall Stewart 	/*
5599f8829a4aSRandall Stewart 	 * ISSUES that MUST be fixed for ECN! When we are the sender of the
5600f8829a4aSRandall Stewart 	 * forward TSN, when the SACK comes back that acknowledges the
5601f8829a4aSRandall Stewart 	 * FWD-TSN we must reset the NONCE sum to match correctly. This will
5602f8829a4aSRandall Stewart 	 * get quite tricky since we may have sent more data interveneing
5603f8829a4aSRandall Stewart 	 * and must carefully account for what the SACK says on the nonce
5604f8829a4aSRandall Stewart 	 * and any gaps that are reported. This work will NOT be done here,
5605f8829a4aSRandall Stewart 	 * but I note it here since it is really related to PR-SCTP and
5606f8829a4aSRandall Stewart 	 * FWD-TSN's
5607f8829a4aSRandall Stewart 	 */
5608f8829a4aSRandall Stewart 
5609f8829a4aSRandall Stewart 	/* The pr-sctp fwd tsn */
5610f8829a4aSRandall Stewart 	/*
5611f8829a4aSRandall Stewart 	 * here we will perform all the data receiver side steps for
5612f8829a4aSRandall Stewart 	 * processing FwdTSN, as required in by pr-sctp draft:
5613f8829a4aSRandall Stewart 	 *
5614f8829a4aSRandall Stewart 	 * Assume we get FwdTSN(x):
5615f8829a4aSRandall Stewart 	 *
5616f8829a4aSRandall Stewart 	 * 1) update local cumTSN to x 2) try to further advance cumTSN to x +
5617f8829a4aSRandall Stewart 	 * others we have 3) examine and update re-ordering queue on
5618f8829a4aSRandall Stewart 	 * pr-in-streams 4) clean up re-assembly queue 5) Send a sack to
5619f8829a4aSRandall Stewart 	 * report where we are.
5620f8829a4aSRandall Stewart 	 */
5621f8829a4aSRandall Stewart 	struct sctp_association *asoc;
56222afb3e84SRandall Stewart 	uint32_t new_cum_tsn, gap;
56238933fa13SRandall Stewart 	unsigned int i, fwd_sz, cumack_set_flag, m_size;
56248933fa13SRandall Stewart 	uint32_t str_seq;
5625f8829a4aSRandall Stewart 	struct sctp_stream_in *strm;
5626f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk, *at;
56278933fa13SRandall Stewart 	struct sctp_queued_to_read *ctl, *sv;
5628f8829a4aSRandall Stewart 
5629f8829a4aSRandall Stewart 	cumack_set_flag = 0;
5630f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
5631f8829a4aSRandall Stewart 	if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
5632ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1,
5633ad81507eSRandall Stewart 		    "Bad size too small/big fwd-tsn\n");
5634f8829a4aSRandall Stewart 		return;
5635f8829a4aSRandall Stewart 	}
5636f8829a4aSRandall Stewart 	m_size = (stcb->asoc.mapping_array_size << 3);
5637f8829a4aSRandall Stewart 	/*************************************************************/
5638f8829a4aSRandall Stewart 	/* 1. Here we update local cumTSN and shift the bitmap array */
5639f8829a4aSRandall Stewart 	/*************************************************************/
5640f8829a4aSRandall Stewart 	new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
5641f8829a4aSRandall Stewart 
5642f8829a4aSRandall Stewart 	if (compare_with_wrap(asoc->cumulative_tsn, new_cum_tsn, MAX_TSN) ||
5643f8829a4aSRandall Stewart 	    asoc->cumulative_tsn == new_cum_tsn) {
5644f8829a4aSRandall Stewart 		/* Already got there ... */
5645f8829a4aSRandall Stewart 		return;
5646f8829a4aSRandall Stewart 	}
5647f8829a4aSRandall Stewart 	if (compare_with_wrap(new_cum_tsn, asoc->highest_tsn_inside_map,
5648f8829a4aSRandall Stewart 	    MAX_TSN)) {
5649f8829a4aSRandall Stewart 		asoc->highest_tsn_inside_map = new_cum_tsn;
565077acdc25SRandall Stewart 
565180fefe0aSRandall Stewart 	}
565277acdc25SRandall Stewart 	if (compare_with_wrap(new_cum_tsn, asoc->highest_tsn_inside_nr_map,
565377acdc25SRandall Stewart 	    MAX_TSN)) {
565477acdc25SRandall Stewart 		asoc->highest_tsn_inside_nr_map = new_cum_tsn;
5655f8829a4aSRandall Stewart 	}
5656f8829a4aSRandall Stewart 	/*
5657f8829a4aSRandall Stewart 	 * now we know the new TSN is more advanced, let's find the actual
5658f8829a4aSRandall Stewart 	 * gap
5659f8829a4aSRandall Stewart 	 */
566077acdc25SRandall Stewart 	SCTP_CALC_TSN_TO_GAP(gap, new_cum_tsn, asoc->nr_mapping_array_base_tsn);
566177acdc25SRandall Stewart 	asoc->cumulative_tsn = new_cum_tsn;
56622afb3e84SRandall Stewart 	if (gap >= m_size) {
5663f8829a4aSRandall Stewart 		if ((long)gap > sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv)) {
566417205eccSRandall Stewart 			struct mbuf *oper;
566517205eccSRandall Stewart 
5666f8829a4aSRandall Stewart 			/*
5667f8829a4aSRandall Stewart 			 * out of range (of single byte chunks in the rwnd I
566817205eccSRandall Stewart 			 * give out). This must be an attacker.
5669f8829a4aSRandall Stewart 			 */
567017205eccSRandall Stewart 			*abort_flag = 1;
567117205eccSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
567217205eccSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
567317205eccSRandall Stewart 			if (oper) {
567417205eccSRandall Stewart 				struct sctp_paramhdr *ph;
567517205eccSRandall Stewart 				uint32_t *ippp;
567617205eccSRandall Stewart 
567717205eccSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
567817205eccSRandall Stewart 				    (sizeof(uint32_t) * 3);
567917205eccSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
568017205eccSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
568117205eccSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
568217205eccSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
568317205eccSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_33);
568417205eccSRandall Stewart 				ippp++;
568517205eccSRandall Stewart 				*ippp = asoc->highest_tsn_inside_map;
568617205eccSRandall Stewart 				ippp++;
568717205eccSRandall Stewart 				*ippp = new_cum_tsn;
568817205eccSRandall Stewart 			}
568917205eccSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_33;
569017205eccSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb,
5691ceaad40aSRandall Stewart 			    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
5692f8829a4aSRandall Stewart 			return;
5693f8829a4aSRandall Stewart 		}
5694207304d4SRandall Stewart 		SCTP_STAT_INCR(sctps_fwdtsn_map_over);
569577acdc25SRandall Stewart 
56962afb3e84SRandall Stewart 		memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
56972afb3e84SRandall Stewart 		asoc->mapping_array_base_tsn = new_cum_tsn + 1;
569877acdc25SRandall Stewart 		asoc->highest_tsn_inside_map = new_cum_tsn;
569977acdc25SRandall Stewart 
5700830d754dSRandall Stewart 		memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.nr_mapping_array_size);
5701830d754dSRandall Stewart 		asoc->nr_mapping_array_base_tsn = new_cum_tsn + 1;
5702830d754dSRandall Stewart 		asoc->highest_tsn_inside_nr_map = new_cum_tsn;
570377acdc25SRandall Stewart 
5704b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
5705f8829a4aSRandall Stewart 			sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
570680fefe0aSRandall Stewart 		}
5707f8829a4aSRandall Stewart 		asoc->last_echo_tsn = asoc->highest_tsn_inside_map;
57082afb3e84SRandall Stewart 	} else {
57092afb3e84SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
57102afb3e84SRandall Stewart 		for (i = 0; i <= gap; i++) {
571177acdc25SRandall Stewart 			SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, i);
57128933fa13SRandall Stewart 			SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, i);
5713f8829a4aSRandall Stewart 		}
57142afb3e84SRandall Stewart 		/*
57152afb3e84SRandall Stewart 		 * Now after marking all, slide thing forward but no sack
57162afb3e84SRandall Stewart 		 * please.
57172afb3e84SRandall Stewart 		 */
57182afb3e84SRandall Stewart 		sctp_sack_check(stcb, 0, 0, abort_flag);
57192afb3e84SRandall Stewart 		if (*abort_flag)
57202afb3e84SRandall Stewart 			return;
57212afb3e84SRandall Stewart 	}
5722f8829a4aSRandall Stewart 	/*************************************************************/
5723f8829a4aSRandall Stewart 	/* 2. Clear up re-assembly queue                             */
5724f8829a4aSRandall Stewart 	/*************************************************************/
5725f8829a4aSRandall Stewart 	/*
5726f8829a4aSRandall Stewart 	 * First service it if pd-api is up, just in case we can progress it
5727f8829a4aSRandall Stewart 	 * forward
5728f8829a4aSRandall Stewart 	 */
5729f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
5730f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
5731f8829a4aSRandall Stewart 	}
5732f8829a4aSRandall Stewart 	if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
5733f8829a4aSRandall Stewart 		/* For each one on here see if we need to toss it */
5734f8829a4aSRandall Stewart 		/*
5735f8829a4aSRandall Stewart 		 * For now large messages held on the reasmqueue that are
5736f8829a4aSRandall Stewart 		 * complete will be tossed too. We could in theory do more
5737f8829a4aSRandall Stewart 		 * work to spin through and stop after dumping one msg aka
5738f8829a4aSRandall Stewart 		 * seeing the start of a new msg at the head, and call the
5739f8829a4aSRandall Stewart 		 * delivery function... to see if it can be delivered... But
5740f8829a4aSRandall Stewart 		 * for now we just dump everything on the queue.
5741f8829a4aSRandall Stewart 		 */
5742f8829a4aSRandall Stewart 		chk = TAILQ_FIRST(&asoc->reasmqueue);
5743f8829a4aSRandall Stewart 		while (chk) {
5744f8829a4aSRandall Stewart 			at = TAILQ_NEXT(chk, sctp_next);
57450c0982b8SRandall Stewart 			if ((compare_with_wrap(new_cum_tsn,
57460c0982b8SRandall Stewart 			    chk->rec.data.TSN_seq, MAX_TSN)) ||
57470c0982b8SRandall Stewart 			    (new_cum_tsn == chk->rec.data.TSN_seq)) {
5748f8829a4aSRandall Stewart 				/* It needs to be tossed */
5749f8829a4aSRandall Stewart 				TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
5750f8829a4aSRandall Stewart 				if (compare_with_wrap(chk->rec.data.TSN_seq,
5751f8829a4aSRandall Stewart 				    asoc->tsn_last_delivered, MAX_TSN)) {
5752f8829a4aSRandall Stewart 					asoc->tsn_last_delivered =
5753f8829a4aSRandall Stewart 					    chk->rec.data.TSN_seq;
5754f8829a4aSRandall Stewart 					asoc->str_of_pdapi =
5755f8829a4aSRandall Stewart 					    chk->rec.data.stream_number;
5756f8829a4aSRandall Stewart 					asoc->ssn_of_pdapi =
5757f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq;
5758f8829a4aSRandall Stewart 					asoc->fragment_flags =
5759f8829a4aSRandall Stewart 					    chk->rec.data.rcv_flags;
5760f8829a4aSRandall Stewart 				}
5761f8829a4aSRandall Stewart 				asoc->size_on_reasm_queue -= chk->send_size;
5762f8829a4aSRandall Stewart 				sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5763f8829a4aSRandall Stewart 
5764f8829a4aSRandall Stewart 				/* Clear up any stream problem */
5765f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) !=
5766f8829a4aSRandall Stewart 				    SCTP_DATA_UNORDERED &&
5767f8829a4aSRandall Stewart 				    (compare_with_wrap(chk->rec.data.stream_seq,
5768f8829a4aSRandall Stewart 				    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered,
5769f8829a4aSRandall Stewart 				    MAX_SEQ))) {
5770f8829a4aSRandall Stewart 					/*
5771f8829a4aSRandall Stewart 					 * We must dump forward this streams
5772f8829a4aSRandall Stewart 					 * sequence number if the chunk is
5773f8829a4aSRandall Stewart 					 * not unordered that is being
5774f8829a4aSRandall Stewart 					 * skipped. There is a chance that
5775f8829a4aSRandall Stewart 					 * if the peer does not include the
5776f8829a4aSRandall Stewart 					 * last fragment in its FWD-TSN we
5777f8829a4aSRandall Stewart 					 * WILL have a problem here since
5778f8829a4aSRandall Stewart 					 * you would have a partial chunk in
5779f8829a4aSRandall Stewart 					 * queue that may not be
5780f8829a4aSRandall Stewart 					 * deliverable. Also if a Partial
5781f8829a4aSRandall Stewart 					 * delivery API as started the user
5782f8829a4aSRandall Stewart 					 * may get a partial chunk. The next
5783f8829a4aSRandall Stewart 					 * read returning a new chunk...
5784f8829a4aSRandall Stewart 					 * really ugly but I see no way
5785f8829a4aSRandall Stewart 					 * around it! Maybe a notify??
5786f8829a4aSRandall Stewart 					 */
5787f8829a4aSRandall Stewart 					asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered =
5788f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq;
5789f8829a4aSRandall Stewart 				}
5790f8829a4aSRandall Stewart 				if (chk->data) {
5791f8829a4aSRandall Stewart 					sctp_m_freem(chk->data);
5792f8829a4aSRandall Stewart 					chk->data = NULL;
5793f8829a4aSRandall Stewart 				}
5794f8829a4aSRandall Stewart 				sctp_free_a_chunk(stcb, chk);
5795f8829a4aSRandall Stewart 			} else {
5796f8829a4aSRandall Stewart 				/*
5797f8829a4aSRandall Stewart 				 * Ok we have gone beyond the end of the
57988933fa13SRandall Stewart 				 * fwd-tsn's mark.
5799f8829a4aSRandall Stewart 				 */
5800f8829a4aSRandall Stewart 				break;
5801f8829a4aSRandall Stewart 			}
5802f8829a4aSRandall Stewart 			chk = at;
5803f8829a4aSRandall Stewart 		}
5804f8829a4aSRandall Stewart 	}
58058933fa13SRandall Stewart 	/*******************************************************/
58068933fa13SRandall Stewart 	/* 3. Update the PR-stream re-ordering queues and fix  */
58078933fa13SRandall Stewart 	/* delivery issues as needed.                       */
58088933fa13SRandall Stewart 	/*******************************************************/
5809f8829a4aSRandall Stewart 	fwd_sz -= sizeof(*fwd);
5810671d309cSRandall Stewart 	if (m && fwd_sz) {
5811f8829a4aSRandall Stewart 		/* New method. */
5812d61a0ae0SRandall Stewart 		unsigned int num_str;
5813671d309cSRandall Stewart 		struct sctp_strseq *stseq, strseqbuf;
5814671d309cSRandall Stewart 
5815671d309cSRandall Stewart 		offset += sizeof(*fwd);
5816f8829a4aSRandall Stewart 
58178933fa13SRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
5818f8829a4aSRandall Stewart 		num_str = fwd_sz / sizeof(struct sctp_strseq);
5819f8829a4aSRandall Stewart 		for (i = 0; i < num_str; i++) {
5820f8829a4aSRandall Stewart 			uint16_t st;
5821f8829a4aSRandall Stewart 
5822671d309cSRandall Stewart 			stseq = (struct sctp_strseq *)sctp_m_getptr(m, offset,
5823671d309cSRandall Stewart 			    sizeof(struct sctp_strseq),
5824671d309cSRandall Stewart 			    (uint8_t *) & strseqbuf);
5825671d309cSRandall Stewart 			offset += sizeof(struct sctp_strseq);
58262afb3e84SRandall Stewart 			if (stseq == NULL) {
5827671d309cSRandall Stewart 				break;
58282afb3e84SRandall Stewart 			}
5829f8829a4aSRandall Stewart 			/* Convert */
5830851b7298SRandall Stewart 			st = ntohs(stseq->stream);
5831851b7298SRandall Stewart 			stseq->stream = st;
5832851b7298SRandall Stewart 			st = ntohs(stseq->sequence);
5833851b7298SRandall Stewart 			stseq->sequence = st;
58348933fa13SRandall Stewart 
5835f8829a4aSRandall Stewart 			/* now process */
58368933fa13SRandall Stewart 
58378933fa13SRandall Stewart 			/*
58388933fa13SRandall Stewart 			 * Ok we now look for the stream/seq on the read
58398933fa13SRandall Stewart 			 * queue where its not all delivered. If we find it
58408933fa13SRandall Stewart 			 * we transmute the read entry into a PDI_ABORTED.
58418933fa13SRandall Stewart 			 */
5842851b7298SRandall Stewart 			if (stseq->stream >= asoc->streamincnt) {
58432afb3e84SRandall Stewart 				/* screwed up streams, stop!  */
58442afb3e84SRandall Stewart 				break;
5845f8829a4aSRandall Stewart 			}
58468933fa13SRandall Stewart 			if ((asoc->str_of_pdapi == stseq->stream) &&
58478933fa13SRandall Stewart 			    (asoc->ssn_of_pdapi == stseq->sequence)) {
58488933fa13SRandall Stewart 				/*
58498933fa13SRandall Stewart 				 * If this is the one we were partially
58508933fa13SRandall Stewart 				 * delivering now then we no longer are.
58518933fa13SRandall Stewart 				 * Note this will change with the reassembly
58528933fa13SRandall Stewart 				 * re-write.
58538933fa13SRandall Stewart 				 */
58548933fa13SRandall Stewart 				asoc->fragmented_delivery_inprogress = 0;
58558933fa13SRandall Stewart 			}
58568933fa13SRandall Stewart 			sctp_flush_reassm_for_str_seq(stcb, asoc, stseq->stream, stseq->sequence);
58578933fa13SRandall Stewart 			TAILQ_FOREACH(ctl, &stcb->sctp_ep->read_queue, next) {
58588933fa13SRandall Stewart 				if ((ctl->sinfo_stream == stseq->stream) &&
58598933fa13SRandall Stewart 				    (ctl->sinfo_ssn == stseq->sequence)) {
58608933fa13SRandall Stewart 					str_seq = (stseq->stream << 16) | stseq->sequence;
58618933fa13SRandall Stewart 					ctl->end_added = 1;
58628933fa13SRandall Stewart 					ctl->pdapi_aborted = 1;
58638933fa13SRandall Stewart 					sv = stcb->asoc.control_pdapi;
58648933fa13SRandall Stewart 					stcb->asoc.control_pdapi = ctl;
5865810ec536SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5866810ec536SMichael Tuexen 					    stcb,
58678933fa13SRandall Stewart 					    SCTP_PARTIAL_DELIVERY_ABORTED,
5868810ec536SMichael Tuexen 					    (void *)&str_seq,
5869810ec536SMichael Tuexen 					    SCTP_SO_NOT_LOCKED);
58708933fa13SRandall Stewart 					stcb->asoc.control_pdapi = sv;
58718933fa13SRandall Stewart 					break;
58728933fa13SRandall Stewart 				} else if ((ctl->sinfo_stream == stseq->stream) &&
58738933fa13SRandall Stewart 				    (compare_with_wrap(ctl->sinfo_ssn, stseq->sequence, MAX_SEQ))) {
58748933fa13SRandall Stewart 					/* We are past our victim SSN */
58758933fa13SRandall Stewart 					break;
58768933fa13SRandall Stewart 				}
58778933fa13SRandall Stewart 			}
5878851b7298SRandall Stewart 			strm = &asoc->strmin[stseq->stream];
5879851b7298SRandall Stewart 			if (compare_with_wrap(stseq->sequence,
5880f8829a4aSRandall Stewart 			    strm->last_sequence_delivered, MAX_SEQ)) {
5881f8829a4aSRandall Stewart 				/* Update the sequence number */
5882f8829a4aSRandall Stewart 				strm->last_sequence_delivered =
5883851b7298SRandall Stewart 				    stseq->sequence;
5884f8829a4aSRandall Stewart 			}
5885f8829a4aSRandall Stewart 			/* now kick the stream the new way */
588604ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
5887f8829a4aSRandall Stewart 			sctp_kick_prsctp_reorder_queue(stcb, strm);
5888f8829a4aSRandall Stewart 		}
58898933fa13SRandall Stewart 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5890f8829a4aSRandall Stewart 	}
5891139bc87fSRandall Stewart 	if (TAILQ_FIRST(&asoc->reasmqueue)) {
5892139bc87fSRandall Stewart 		/* now lets kick out and check for more fragmented delivery */
589304ee05e8SRandall Stewart 		/* sa_ignore NO_NULL_CHK */
5894139bc87fSRandall Stewart 		sctp_deliver_reasm_check(stcb, &stcb->asoc);
5895139bc87fSRandall Stewart 	}
5896f8829a4aSRandall Stewart }
5897