xref: /freebsd/sys/netinet/sctp_indata.c (revision 689e6a5fa300a03cb5dc7e9e6d32a45414504043)
1f8829a4aSRandall Stewart /*-
2b1006367SRandall Stewart  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
35d40cf5dSRandall Stewart  * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved.
45d40cf5dSRandall Stewart  * Copyright (c) 2008-2011, by Michael Tuexen. All rights reserved.
5f8829a4aSRandall Stewart  *
6f8829a4aSRandall Stewart  * Redistribution and use in source and binary forms, with or without
7f8829a4aSRandall Stewart  * modification, are permitted provided that the following conditions are met:
8f8829a4aSRandall Stewart  *
9f8829a4aSRandall Stewart  * a) Redistributions of source code must retain the above copyright notice,
10f8829a4aSRandall Stewart  *   this list of conditions and the following disclaimer.
11f8829a4aSRandall Stewart  *
12f8829a4aSRandall Stewart  * b) Redistributions in binary form must reproduce the above copyright
13f8829a4aSRandall Stewart  *    notice, this list of conditions and the following disclaimer in
14f8829a4aSRandall Stewart  *   the documentation and/or other materials provided with the distribution.
15f8829a4aSRandall Stewart  *
16f8829a4aSRandall Stewart  * c) Neither the name of Cisco Systems, Inc. nor the names of its
17f8829a4aSRandall Stewart  *    contributors may be used to endorse or promote products derived
18f8829a4aSRandall Stewart  *    from this software without specific prior written permission.
19f8829a4aSRandall Stewart  *
20f8829a4aSRandall Stewart  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21f8829a4aSRandall Stewart  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22f8829a4aSRandall Stewart  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23f8829a4aSRandall Stewart  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24f8829a4aSRandall Stewart  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25f8829a4aSRandall Stewart  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26f8829a4aSRandall Stewart  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27f8829a4aSRandall Stewart  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28f8829a4aSRandall Stewart  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29f8829a4aSRandall Stewart  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30f8829a4aSRandall Stewart  * THE POSSIBILITY OF SUCH DAMAGE.
31f8829a4aSRandall Stewart  */
32f8829a4aSRandall Stewart 
33a5d547adSRandall Stewart /* $KAME: sctp_indata.c,v 1.36 2005/03/06 16:04:17 itojun Exp $	 */
34f8829a4aSRandall Stewart 
35f8829a4aSRandall Stewart #include <sys/cdefs.h>
36f8829a4aSRandall Stewart __FBSDID("$FreeBSD$");
37f8829a4aSRandall Stewart 
38f8829a4aSRandall Stewart #include <netinet/sctp_os.h>
39f8829a4aSRandall Stewart #include <netinet/sctp_var.h>
4042551e99SRandall Stewart #include <netinet/sctp_sysctl.h>
41f8829a4aSRandall Stewart #include <netinet/sctp_pcb.h>
42f8829a4aSRandall Stewart #include <netinet/sctp_header.h>
43f8829a4aSRandall Stewart #include <netinet/sctputil.h>
44f8829a4aSRandall Stewart #include <netinet/sctp_output.h>
45f8829a4aSRandall Stewart #include <netinet/sctp_input.h>
46f8829a4aSRandall Stewart #include <netinet/sctp_indata.h>
47f8829a4aSRandall Stewart #include <netinet/sctp_uio.h>
48f8829a4aSRandall Stewart #include <netinet/sctp_timer.h>
49f8829a4aSRandall Stewart 
50d50c1d79SRandall Stewart 
51f8829a4aSRandall Stewart /*
52f8829a4aSRandall Stewart  * NOTES: On the outbound side of things I need to check the sack timer to
53f8829a4aSRandall Stewart  * see if I should generate a sack into the chunk queue (if I have data to
54f8829a4aSRandall Stewart  * send that is and will be sending it .. for bundling.
55f8829a4aSRandall Stewart  *
56f8829a4aSRandall Stewart  * The callback in sctp_usrreq.c will get called when the socket is read from.
57f8829a4aSRandall Stewart  * This will cause sctp_service_queues() to get called on the top entry in
58f8829a4aSRandall Stewart  * the list.
59f8829a4aSRandall Stewart  */
60f8829a4aSRandall Stewart 
6172fb6fdbSRandall Stewart void
62f8829a4aSRandall Stewart sctp_set_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
63f8829a4aSRandall Stewart {
64b3f1ea41SRandall Stewart 	asoc->my_rwnd = sctp_calc_rwnd(stcb, asoc);
65f8829a4aSRandall Stewart }
66f8829a4aSRandall Stewart 
67f8829a4aSRandall Stewart /* Calculate what the rwnd would be */
6872fb6fdbSRandall Stewart uint32_t
69f8829a4aSRandall Stewart sctp_calc_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
70f8829a4aSRandall Stewart {
71b3f1ea41SRandall Stewart 	uint32_t calc = 0;
72f8829a4aSRandall Stewart 
73f8829a4aSRandall Stewart 	/*
74f8829a4aSRandall Stewart 	 * This is really set wrong with respect to a 1-2-m socket. Since
75f8829a4aSRandall Stewart 	 * the sb_cc is the count that everyone as put up. When we re-write
76f8829a4aSRandall Stewart 	 * sctp_soreceive then we will fix this so that ONLY this
77f8829a4aSRandall Stewart 	 * associations data is taken into account.
78f8829a4aSRandall Stewart 	 */
79f8829a4aSRandall Stewart 	if (stcb->sctp_socket == NULL)
80f8829a4aSRandall Stewart 		return (calc);
81f8829a4aSRandall Stewart 
82f8829a4aSRandall Stewart 	if (stcb->asoc.sb_cc == 0 &&
83f8829a4aSRandall Stewart 	    asoc->size_on_reasm_queue == 0 &&
84f8829a4aSRandall Stewart 	    asoc->size_on_all_streams == 0) {
85f8829a4aSRandall Stewart 		/* Full rwnd granted */
86b3f1ea41SRandall Stewart 		calc = max(SCTP_SB_LIMIT_RCV(stcb->sctp_socket), SCTP_MINIMAL_RWND);
87f8829a4aSRandall Stewart 		return (calc);
88f8829a4aSRandall Stewart 	}
89f8829a4aSRandall Stewart 	/* get actual space */
90f8829a4aSRandall Stewart 	calc = (uint32_t) sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv);
91f8829a4aSRandall Stewart 
92f8829a4aSRandall Stewart 	/*
93f8829a4aSRandall Stewart 	 * take out what has NOT been put on socket queue and we yet hold
94f8829a4aSRandall Stewart 	 * for putting up.
95f8829a4aSRandall Stewart 	 */
9644fbe462SRandall Stewart 	calc = sctp_sbspace_sub(calc, (uint32_t) (asoc->size_on_reasm_queue +
9744fbe462SRandall Stewart 	    asoc->cnt_on_reasm_queue * MSIZE));
9844fbe462SRandall Stewart 	calc = sctp_sbspace_sub(calc, (uint32_t) (asoc->size_on_all_streams +
9944fbe462SRandall Stewart 	    asoc->cnt_on_all_streams * MSIZE));
100f8829a4aSRandall Stewart 
101f8829a4aSRandall Stewart 	if (calc == 0) {
102f8829a4aSRandall Stewart 		/* out of space */
103f8829a4aSRandall Stewart 		return (calc);
104f8829a4aSRandall Stewart 	}
105f8829a4aSRandall Stewart 	/* what is the overhead of all these rwnd's */
1062afb3e84SRandall Stewart 	calc = sctp_sbspace_sub(calc, stcb->asoc.my_rwnd_control_len);
107b3f1ea41SRandall Stewart 	/*
108b3f1ea41SRandall Stewart 	 * If the window gets too small due to ctrl-stuff, reduce it to 1,
109b3f1ea41SRandall Stewart 	 * even it is 0. SWS engaged
110f8829a4aSRandall Stewart 	 */
111b3f1ea41SRandall Stewart 	if (calc < stcb->asoc.my_rwnd_control_len) {
112b3f1ea41SRandall Stewart 		calc = 1;
1132afb3e84SRandall Stewart 	}
114b3f1ea41SRandall Stewart 	return (calc);
115f8829a4aSRandall Stewart }
116f8829a4aSRandall Stewart 
117f8829a4aSRandall Stewart 
118f8829a4aSRandall Stewart 
119f8829a4aSRandall Stewart /*
120f8829a4aSRandall Stewart  * Build out our readq entry based on the incoming packet.
121f8829a4aSRandall Stewart  */
122f8829a4aSRandall Stewart struct sctp_queued_to_read *
123f8829a4aSRandall Stewart sctp_build_readq_entry(struct sctp_tcb *stcb,
124f8829a4aSRandall Stewart     struct sctp_nets *net,
125f8829a4aSRandall Stewart     uint32_t tsn, uint32_t ppid,
126f8829a4aSRandall Stewart     uint32_t context, uint16_t stream_no,
127f8829a4aSRandall Stewart     uint16_t stream_seq, uint8_t flags,
128f8829a4aSRandall Stewart     struct mbuf *dm)
129f8829a4aSRandall Stewart {
130f8829a4aSRandall Stewart 	struct sctp_queued_to_read *read_queue_e = NULL;
131f8829a4aSRandall Stewart 
132f8829a4aSRandall Stewart 	sctp_alloc_a_readq(stcb, read_queue_e);
133f8829a4aSRandall Stewart 	if (read_queue_e == NULL) {
134f8829a4aSRandall Stewart 		goto failed_build;
135f8829a4aSRandall Stewart 	}
136f8829a4aSRandall Stewart 	read_queue_e->sinfo_stream = stream_no;
137f8829a4aSRandall Stewart 	read_queue_e->sinfo_ssn = stream_seq;
138f8829a4aSRandall Stewart 	read_queue_e->sinfo_flags = (flags << 8);
139f8829a4aSRandall Stewart 	read_queue_e->sinfo_ppid = ppid;
140f8829a4aSRandall Stewart 	read_queue_e->sinfo_context = stcb->asoc.context;
141f8829a4aSRandall Stewart 	read_queue_e->sinfo_timetolive = 0;
142f8829a4aSRandall Stewart 	read_queue_e->sinfo_tsn = tsn;
143f8829a4aSRandall Stewart 	read_queue_e->sinfo_cumtsn = tsn;
144f8829a4aSRandall Stewart 	read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
145f8829a4aSRandall Stewart 	read_queue_e->whoFrom = net;
146f8829a4aSRandall Stewart 	read_queue_e->length = 0;
147f8829a4aSRandall Stewart 	atomic_add_int(&net->ref_count, 1);
148f8829a4aSRandall Stewart 	read_queue_e->data = dm;
149139bc87fSRandall Stewart 	read_queue_e->spec_flags = 0;
150f8829a4aSRandall Stewart 	read_queue_e->tail_mbuf = NULL;
15117205eccSRandall Stewart 	read_queue_e->aux_data = NULL;
152f8829a4aSRandall Stewart 	read_queue_e->stcb = stcb;
153f8829a4aSRandall Stewart 	read_queue_e->port_from = stcb->rport;
154f8829a4aSRandall Stewart 	read_queue_e->do_not_ref_stcb = 0;
155f8829a4aSRandall Stewart 	read_queue_e->end_added = 0;
1569a6142d8SRandall Stewart 	read_queue_e->some_taken = 0;
15703b0b021SRandall Stewart 	read_queue_e->pdapi_aborted = 0;
158f8829a4aSRandall Stewart failed_build:
159f8829a4aSRandall Stewart 	return (read_queue_e);
160f8829a4aSRandall Stewart }
161f8829a4aSRandall Stewart 
162f8829a4aSRandall Stewart 
163f8829a4aSRandall Stewart /*
164f8829a4aSRandall Stewart  * Build out our readq entry based on the incoming packet.
165f8829a4aSRandall Stewart  */
166f8829a4aSRandall Stewart static struct sctp_queued_to_read *
167f8829a4aSRandall Stewart sctp_build_readq_entry_chk(struct sctp_tcb *stcb,
168f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk)
169f8829a4aSRandall Stewart {
170f8829a4aSRandall Stewart 	struct sctp_queued_to_read *read_queue_e = NULL;
171f8829a4aSRandall Stewart 
172f8829a4aSRandall Stewart 	sctp_alloc_a_readq(stcb, read_queue_e);
173f8829a4aSRandall Stewart 	if (read_queue_e == NULL) {
174f8829a4aSRandall Stewart 		goto failed_build;
175f8829a4aSRandall Stewart 	}
176f8829a4aSRandall Stewart 	read_queue_e->sinfo_stream = chk->rec.data.stream_number;
177f8829a4aSRandall Stewart 	read_queue_e->sinfo_ssn = chk->rec.data.stream_seq;
178f8829a4aSRandall Stewart 	read_queue_e->sinfo_flags = (chk->rec.data.rcv_flags << 8);
179f8829a4aSRandall Stewart 	read_queue_e->sinfo_ppid = chk->rec.data.payloadtype;
180f8829a4aSRandall Stewart 	read_queue_e->sinfo_context = stcb->asoc.context;
181f8829a4aSRandall Stewart 	read_queue_e->sinfo_timetolive = 0;
182f8829a4aSRandall Stewart 	read_queue_e->sinfo_tsn = chk->rec.data.TSN_seq;
183f8829a4aSRandall Stewart 	read_queue_e->sinfo_cumtsn = chk->rec.data.TSN_seq;
184f8829a4aSRandall Stewart 	read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
185f8829a4aSRandall Stewart 	read_queue_e->whoFrom = chk->whoTo;
18617205eccSRandall Stewart 	read_queue_e->aux_data = NULL;
187f8829a4aSRandall Stewart 	read_queue_e->length = 0;
188f8829a4aSRandall Stewart 	atomic_add_int(&chk->whoTo->ref_count, 1);
189f8829a4aSRandall Stewart 	read_queue_e->data = chk->data;
190f8829a4aSRandall Stewart 	read_queue_e->tail_mbuf = NULL;
191f8829a4aSRandall Stewart 	read_queue_e->stcb = stcb;
192f8829a4aSRandall Stewart 	read_queue_e->port_from = stcb->rport;
193139bc87fSRandall Stewart 	read_queue_e->spec_flags = 0;
194f8829a4aSRandall Stewart 	read_queue_e->do_not_ref_stcb = 0;
195f8829a4aSRandall Stewart 	read_queue_e->end_added = 0;
1969a6142d8SRandall Stewart 	read_queue_e->some_taken = 0;
19703b0b021SRandall Stewart 	read_queue_e->pdapi_aborted = 0;
198f8829a4aSRandall Stewart failed_build:
199f8829a4aSRandall Stewart 	return (read_queue_e);
200f8829a4aSRandall Stewart }
201f8829a4aSRandall Stewart 
202f8829a4aSRandall Stewart 
203f8829a4aSRandall Stewart struct mbuf *
204f8829a4aSRandall Stewart sctp_build_ctl_nchunk(struct sctp_inpcb *inp,
205f8829a4aSRandall Stewart     struct sctp_sndrcvinfo *sinfo)
206f8829a4aSRandall Stewart {
207f8829a4aSRandall Stewart 	struct sctp_sndrcvinfo *outinfo;
208f8829a4aSRandall Stewart 	struct cmsghdr *cmh;
209f8829a4aSRandall Stewart 	struct mbuf *ret;
210f8829a4aSRandall Stewart 	int len;
211f8829a4aSRandall Stewart 	int use_extended = 0;
212f8829a4aSRandall Stewart 
213f8829a4aSRandall Stewart 	if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
214f8829a4aSRandall Stewart 		/* user does not want the sndrcv ctl */
215f8829a4aSRandall Stewart 		return (NULL);
216f8829a4aSRandall Stewart 	}
217f8829a4aSRandall Stewart 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
218f8829a4aSRandall Stewart 		use_extended = 1;
219f8829a4aSRandall Stewart 		len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
220f8829a4aSRandall Stewart 	} else {
221f8829a4aSRandall Stewart 		len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
222f8829a4aSRandall Stewart 	}
223f8829a4aSRandall Stewart 
224f8829a4aSRandall Stewart 
225f8829a4aSRandall Stewart 	ret = sctp_get_mbuf_for_msg(len,
226139bc87fSRandall Stewart 	    0, M_DONTWAIT, 1, MT_DATA);
227f8829a4aSRandall Stewart 
228f8829a4aSRandall Stewart 	if (ret == NULL) {
229f8829a4aSRandall Stewart 		/* No space */
230f8829a4aSRandall Stewart 		return (ret);
231f8829a4aSRandall Stewart 	}
232f8829a4aSRandall Stewart 	/* We need a CMSG header followed by the struct  */
233f8829a4aSRandall Stewart 	cmh = mtod(ret, struct cmsghdr *);
234f8829a4aSRandall Stewart 	outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
235f8829a4aSRandall Stewart 	cmh->cmsg_level = IPPROTO_SCTP;
236f8829a4aSRandall Stewart 	if (use_extended) {
237f8829a4aSRandall Stewart 		cmh->cmsg_type = SCTP_EXTRCV;
238f8829a4aSRandall Stewart 		cmh->cmsg_len = len;
239f8829a4aSRandall Stewart 		memcpy(outinfo, sinfo, len);
240f8829a4aSRandall Stewart 	} else {
241f8829a4aSRandall Stewart 		cmh->cmsg_type = SCTP_SNDRCV;
242f8829a4aSRandall Stewart 		cmh->cmsg_len = len;
243f8829a4aSRandall Stewart 		*outinfo = *sinfo;
244f8829a4aSRandall Stewart 	}
245139bc87fSRandall Stewart 	SCTP_BUF_LEN(ret) = cmh->cmsg_len;
246f8829a4aSRandall Stewart 	return (ret);
247f8829a4aSRandall Stewart }
248f8829a4aSRandall Stewart 
249139bc87fSRandall Stewart 
25017205eccSRandall Stewart char *
25117205eccSRandall Stewart sctp_build_ctl_cchunk(struct sctp_inpcb *inp,
25217205eccSRandall Stewart     int *control_len,
25317205eccSRandall Stewart     struct sctp_sndrcvinfo *sinfo)
25417205eccSRandall Stewart {
25517205eccSRandall Stewart 	struct sctp_sndrcvinfo *outinfo;
25617205eccSRandall Stewart 	struct cmsghdr *cmh;
25717205eccSRandall Stewart 	char *buf;
25817205eccSRandall Stewart 	int len;
25917205eccSRandall Stewart 	int use_extended = 0;
26017205eccSRandall Stewart 
26117205eccSRandall Stewart 	if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
26217205eccSRandall Stewart 		/* user does not want the sndrcv ctl */
26317205eccSRandall Stewart 		return (NULL);
26417205eccSRandall Stewart 	}
26517205eccSRandall Stewart 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
26617205eccSRandall Stewart 		use_extended = 1;
26717205eccSRandall Stewart 		len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
26817205eccSRandall Stewart 	} else {
26917205eccSRandall Stewart 		len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
27017205eccSRandall Stewart 	}
271207304d4SRandall Stewart 	SCTP_MALLOC(buf, char *, len, SCTP_M_CMSG);
27217205eccSRandall Stewart 	if (buf == NULL) {
27317205eccSRandall Stewart 		/* No space */
27417205eccSRandall Stewart 		return (buf);
27517205eccSRandall Stewart 	}
27617205eccSRandall Stewart 	/* We need a CMSG header followed by the struct  */
27717205eccSRandall Stewart 	cmh = (struct cmsghdr *)buf;
27817205eccSRandall Stewart 	outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
27917205eccSRandall Stewart 	cmh->cmsg_level = IPPROTO_SCTP;
28017205eccSRandall Stewart 	if (use_extended) {
28117205eccSRandall Stewart 		cmh->cmsg_type = SCTP_EXTRCV;
28217205eccSRandall Stewart 		cmh->cmsg_len = len;
28317205eccSRandall Stewart 		memcpy(outinfo, sinfo, len);
28417205eccSRandall Stewart 	} else {
28517205eccSRandall Stewart 		cmh->cmsg_type = SCTP_SNDRCV;
28617205eccSRandall Stewart 		cmh->cmsg_len = len;
28717205eccSRandall Stewart 		*outinfo = *sinfo;
28817205eccSRandall Stewart 	}
28917205eccSRandall Stewart 	*control_len = len;
29017205eccSRandall Stewart 	return (buf);
29117205eccSRandall Stewart }
29217205eccSRandall Stewart 
29377acdc25SRandall Stewart static void
29477acdc25SRandall Stewart sctp_mark_non_revokable(struct sctp_association *asoc, uint32_t tsn)
29577acdc25SRandall Stewart {
2969b2e0767SRandall Stewart 	uint32_t gap, i, cumackp1;
29777acdc25SRandall Stewart 	int fnd = 0;
29877acdc25SRandall Stewart 
29977acdc25SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
30077acdc25SRandall Stewart 		return;
30177acdc25SRandall Stewart 	}
3029b2e0767SRandall Stewart 	cumackp1 = asoc->cumulative_tsn + 1;
30320b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cumackp1, tsn)) {
3049b2e0767SRandall Stewart 		/*
3059b2e0767SRandall Stewart 		 * this tsn is behind the cum ack and thus we don't need to
3069b2e0767SRandall Stewart 		 * worry about it being moved from one to the other.
3079b2e0767SRandall Stewart 		 */
3089b2e0767SRandall Stewart 		return;
3099b2e0767SRandall Stewart 	}
31077acdc25SRandall Stewart 	SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
31177acdc25SRandall Stewart 	if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
31277acdc25SRandall Stewart 		printf("gap:%x tsn:%x\n", gap, tsn);
31377acdc25SRandall Stewart 		sctp_print_mapping_array(asoc);
314b5c16493SMichael Tuexen #ifdef INVARIANTS
31577acdc25SRandall Stewart 		panic("Things are really messed up now!!");
31677acdc25SRandall Stewart #endif
317b5c16493SMichael Tuexen 	}
31877acdc25SRandall Stewart 	SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
31977acdc25SRandall Stewart 	SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
32020b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
32177acdc25SRandall Stewart 		asoc->highest_tsn_inside_nr_map = tsn;
32277acdc25SRandall Stewart 	}
32377acdc25SRandall Stewart 	if (tsn == asoc->highest_tsn_inside_map) {
32477acdc25SRandall Stewart 		/* We must back down to see what the new highest is */
32520b07a4dSMichael Tuexen 		for (i = tsn - 1; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) {
32677acdc25SRandall Stewart 			SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn);
32777acdc25SRandall Stewart 			if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
32877acdc25SRandall Stewart 				asoc->highest_tsn_inside_map = i;
32977acdc25SRandall Stewart 				fnd = 1;
33077acdc25SRandall Stewart 				break;
33177acdc25SRandall Stewart 			}
33277acdc25SRandall Stewart 		}
33377acdc25SRandall Stewart 		if (!fnd) {
33477acdc25SRandall Stewart 			asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1;
33577acdc25SRandall Stewart 		}
33677acdc25SRandall Stewart 	}
33777acdc25SRandall Stewart }
33877acdc25SRandall Stewart 
33917205eccSRandall Stewart 
340f8829a4aSRandall Stewart /*
341f8829a4aSRandall Stewart  * We are delivering currently from the reassembly queue. We must continue to
342f8829a4aSRandall Stewart  * deliver until we either: 1) run out of space. 2) run out of sequential
343f8829a4aSRandall Stewart  * TSN's 3) hit the SCTP_DATA_LAST_FRAG flag.
344f8829a4aSRandall Stewart  */
345f8829a4aSRandall Stewart static void
346f8829a4aSRandall Stewart sctp_service_reassembly(struct sctp_tcb *stcb, struct sctp_association *asoc)
347f8829a4aSRandall Stewart {
3484a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
349f8829a4aSRandall Stewart 	uint16_t nxt_todel;
350f8829a4aSRandall Stewart 	uint16_t stream_no;
351f8829a4aSRandall Stewart 	int end = 0;
352f8829a4aSRandall Stewart 	int cntDel;
3534a9ef3f8SMichael Tuexen 	struct sctp_queued_to_read *control, *ctl, *nctl;
354f8829a4aSRandall Stewart 
355ad81507eSRandall Stewart 	if (stcb == NULL)
356ad81507eSRandall Stewart 		return;
357ad81507eSRandall Stewart 
358f42a358aSRandall Stewart 	cntDel = stream_no = 0;
359ad81507eSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
360851b7298SRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
361ad81507eSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
362851b7298SRandall Stewart 		/* socket above is long gone or going.. */
363851b7298SRandall Stewart abandon:
364f8829a4aSRandall Stewart 		asoc->fragmented_delivery_inprogress = 0;
3654a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
366f8829a4aSRandall Stewart 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
367f8829a4aSRandall Stewart 			asoc->size_on_reasm_queue -= chk->send_size;
368f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_reasm_queue);
369f8829a4aSRandall Stewart 			/*
370f8829a4aSRandall Stewart 			 * Lose the data pointer, since its in the socket
371f8829a4aSRandall Stewart 			 * buffer
372f8829a4aSRandall Stewart 			 */
373f8829a4aSRandall Stewart 			if (chk->data) {
374f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
375f8829a4aSRandall Stewart 				chk->data = NULL;
376f8829a4aSRandall Stewart 			}
377f8829a4aSRandall Stewart 			/* Now free the address and data */
378*689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3793c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
380f8829a4aSRandall Stewart 		}
381f8829a4aSRandall Stewart 		return;
382f8829a4aSRandall Stewart 	}
383f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
3844a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
385f8829a4aSRandall Stewart 		if (chk->rec.data.TSN_seq != (asoc->tsn_last_delivered + 1)) {
386f8829a4aSRandall Stewart 			/* Can't deliver more :< */
387f8829a4aSRandall Stewart 			return;
388f8829a4aSRandall Stewart 		}
389f8829a4aSRandall Stewart 		stream_no = chk->rec.data.stream_number;
390f8829a4aSRandall Stewart 		nxt_todel = asoc->strmin[stream_no].last_sequence_delivered + 1;
391f8829a4aSRandall Stewart 		if (nxt_todel != chk->rec.data.stream_seq &&
392f8829a4aSRandall Stewart 		    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
393f8829a4aSRandall Stewart 			/*
394f8829a4aSRandall Stewart 			 * Not the next sequence to deliver in its stream OR
395f8829a4aSRandall Stewart 			 * unordered
396f8829a4aSRandall Stewart 			 */
397f8829a4aSRandall Stewart 			return;
398f8829a4aSRandall Stewart 		}
399f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
400f8829a4aSRandall Stewart 
401f8829a4aSRandall Stewart 			control = sctp_build_readq_entry_chk(stcb, chk);
402f8829a4aSRandall Stewart 			if (control == NULL) {
403f8829a4aSRandall Stewart 				/* out of memory? */
404f8829a4aSRandall Stewart 				return;
405f8829a4aSRandall Stewart 			}
406f8829a4aSRandall Stewart 			/* save it off for our future deliveries */
407f8829a4aSRandall Stewart 			stcb->asoc.control_pdapi = control;
408f8829a4aSRandall Stewart 			if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
409f8829a4aSRandall Stewart 				end = 1;
410f8829a4aSRandall Stewart 			else
411f8829a4aSRandall Stewart 				end = 0;
412b5c16493SMichael Tuexen 			sctp_mark_non_revokable(asoc, chk->rec.data.TSN_seq);
413f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep,
414cfde3ff7SRandall Stewart 			    stcb, control, &stcb->sctp_socket->so_rcv, end,
415cfde3ff7SRandall Stewart 			    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
416f8829a4aSRandall Stewart 			cntDel++;
417f8829a4aSRandall Stewart 		} else {
418f8829a4aSRandall Stewart 			if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
419f8829a4aSRandall Stewart 				end = 1;
420f8829a4aSRandall Stewart 			else
421f8829a4aSRandall Stewart 				end = 0;
422b5c16493SMichael Tuexen 			sctp_mark_non_revokable(asoc, chk->rec.data.TSN_seq);
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);
457f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
458f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 0;
459f8829a4aSRandall Stewart 			if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
460f8829a4aSRandall Stewart 				asoc->strmin[stream_no].last_sequence_delivered++;
461f8829a4aSRandall Stewart 			}
462f8829a4aSRandall Stewart 			if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
463f8829a4aSRandall Stewart 				SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
464f8829a4aSRandall Stewart 			}
465f8829a4aSRandall Stewart 		} else if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
466f8829a4aSRandall Stewart 			/*
467f8829a4aSRandall Stewart 			 * turn the flag back on since we just  delivered
468f8829a4aSRandall Stewart 			 * yet another one.
469f8829a4aSRandall Stewart 			 */
470f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 1;
471f8829a4aSRandall Stewart 		}
472f8829a4aSRandall Stewart 		asoc->tsn_of_pdapi_last_delivered = chk->rec.data.TSN_seq;
473f8829a4aSRandall Stewart 		asoc->last_flags_delivered = chk->rec.data.rcv_flags;
474f8829a4aSRandall Stewart 		asoc->last_strm_seq_delivered = chk->rec.data.stream_seq;
475f8829a4aSRandall Stewart 		asoc->last_strm_no_delivered = chk->rec.data.stream_number;
476f8829a4aSRandall Stewart 
477f8829a4aSRandall Stewart 		asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
478f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue -= chk->send_size;
479f8829a4aSRandall Stewart 		sctp_ucount_decr(asoc->cnt_on_reasm_queue);
480f8829a4aSRandall Stewart 		/* free up the chk */
481f8829a4aSRandall Stewart 		chk->data = NULL;
482*689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
483f8829a4aSRandall Stewart 
484f8829a4aSRandall Stewart 		if (asoc->fragmented_delivery_inprogress == 0) {
485f8829a4aSRandall Stewart 			/*
486f8829a4aSRandall Stewart 			 * Now lets see if we can deliver the next one on
487f8829a4aSRandall Stewart 			 * the stream
488f8829a4aSRandall Stewart 			 */
489f8829a4aSRandall Stewart 			struct sctp_stream_in *strm;
490f8829a4aSRandall Stewart 
491f8829a4aSRandall Stewart 			strm = &asoc->strmin[stream_no];
492f8829a4aSRandall Stewart 			nxt_todel = strm->last_sequence_delivered + 1;
4934a9ef3f8SMichael Tuexen 			TAILQ_FOREACH_SAFE(ctl, &strm->inqueue, next, nctl) {
494f8829a4aSRandall Stewart 				/* Deliver more if we can. */
495f8829a4aSRandall Stewart 				if (nxt_todel == ctl->sinfo_ssn) {
496f8829a4aSRandall Stewart 					TAILQ_REMOVE(&strm->inqueue, ctl, next);
497f8829a4aSRandall Stewart 					asoc->size_on_all_streams -= ctl->length;
498f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->cnt_on_all_streams);
499f8829a4aSRandall Stewart 					strm->last_sequence_delivered++;
500b5c16493SMichael Tuexen 					sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
501f8829a4aSRandall Stewart 					sctp_add_to_readq(stcb->sctp_ep, stcb,
502f8829a4aSRandall Stewart 					    ctl,
503cfde3ff7SRandall Stewart 					    &stcb->sctp_socket->so_rcv, 1,
504cfde3ff7SRandall Stewart 					    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
505f8829a4aSRandall Stewart 				} else {
506f8829a4aSRandall Stewart 					break;
507f8829a4aSRandall Stewart 				}
508f8829a4aSRandall Stewart 				nxt_todel = strm->last_sequence_delivered + 1;
509f8829a4aSRandall Stewart 			}
510139bc87fSRandall Stewart 			break;
511f8829a4aSRandall Stewart 		}
5124a9ef3f8SMichael Tuexen 	}
513f8829a4aSRandall Stewart }
514f8829a4aSRandall Stewart 
515f8829a4aSRandall Stewart /*
516f8829a4aSRandall Stewart  * Queue the chunk either right into the socket buffer if it is the next one
517f8829a4aSRandall Stewart  * to go OR put it in the correct place in the delivery queue.  If we do
518f8829a4aSRandall Stewart  * append to the so_buf, keep doing so until we are out of order. One big
519f8829a4aSRandall Stewart  * question still remains, what to do when the socket buffer is FULL??
520f8829a4aSRandall Stewart  */
521f8829a4aSRandall Stewart static void
522f8829a4aSRandall Stewart sctp_queue_data_to_stream(struct sctp_tcb *stcb, struct sctp_association *asoc,
523f8829a4aSRandall Stewart     struct sctp_queued_to_read *control, int *abort_flag)
524f8829a4aSRandall Stewart {
525f8829a4aSRandall Stewart 	/*
526f8829a4aSRandall Stewart 	 * FIX-ME maybe? What happens when the ssn wraps? If we are getting
527f8829a4aSRandall Stewart 	 * all the data in one stream this could happen quite rapidly. One
528f8829a4aSRandall Stewart 	 * could use the TSN to keep track of things, but this scheme breaks
529f8829a4aSRandall Stewart 	 * down in the other type of stream useage that could occur. Send a
530f8829a4aSRandall Stewart 	 * single msg to stream 0, send 4Billion messages to stream 1, now
531f8829a4aSRandall Stewart 	 * send a message to stream 0. You have a situation where the TSN
532f8829a4aSRandall Stewart 	 * has wrapped but not in the stream. Is this worth worrying about
533f8829a4aSRandall Stewart 	 * or should we just change our queue sort at the bottom to be by
534f8829a4aSRandall Stewart 	 * TSN.
535f8829a4aSRandall Stewart 	 *
536f8829a4aSRandall Stewart 	 * Could it also be legal for a peer to send ssn 1 with TSN 2 and ssn 2
537f8829a4aSRandall Stewart 	 * with TSN 1? If the peer is doing some sort of funky TSN/SSN
538f8829a4aSRandall Stewart 	 * assignment this could happen... and I don't see how this would be
539f8829a4aSRandall Stewart 	 * a violation. So for now I am undecided an will leave the sort by
540f8829a4aSRandall Stewart 	 * SSN alone. Maybe a hybred approach is the answer
541f8829a4aSRandall Stewart 	 *
542f8829a4aSRandall Stewart 	 */
543f8829a4aSRandall Stewart 	struct sctp_stream_in *strm;
544f8829a4aSRandall Stewart 	struct sctp_queued_to_read *at;
545f8829a4aSRandall Stewart 	int queue_needed;
546f8829a4aSRandall Stewart 	uint16_t nxt_todel;
547f8829a4aSRandall Stewart 	struct mbuf *oper;
548f8829a4aSRandall Stewart 
549f8829a4aSRandall Stewart 	queue_needed = 1;
550f8829a4aSRandall Stewart 	asoc->size_on_all_streams += control->length;
551f8829a4aSRandall Stewart 	sctp_ucount_incr(asoc->cnt_on_all_streams);
552f8829a4aSRandall Stewart 	strm = &asoc->strmin[control->sinfo_stream];
553f8829a4aSRandall Stewart 	nxt_todel = strm->last_sequence_delivered + 1;
554b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
555f8829a4aSRandall Stewart 		sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INTO_STRD);
55680fefe0aSRandall Stewart 	}
557ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INDATA1,
558ad81507eSRandall Stewart 	    "queue to stream called for ssn:%u lastdel:%u nxt:%u\n",
559f8829a4aSRandall Stewart 	    (uint32_t) control->sinfo_stream,
560ad81507eSRandall Stewart 	    (uint32_t) strm->last_sequence_delivered,
561ad81507eSRandall Stewart 	    (uint32_t) nxt_todel);
56220b07a4dSMichael Tuexen 	if (SCTP_SSN_GE(strm->last_sequence_delivered, control->sinfo_ssn)) {
563f8829a4aSRandall Stewart 		/* The incoming sseq is behind where we last delivered? */
564ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1, "Duplicate S-SEQ:%d delivered:%d from peer, Abort  association\n",
565ad81507eSRandall Stewart 		    control->sinfo_ssn, strm->last_sequence_delivered);
566c40e9cf2SRandall Stewart protocol_error:
567f8829a4aSRandall Stewart 		/*
568f8829a4aSRandall Stewart 		 * throw it in the stream so it gets cleaned up in
569f8829a4aSRandall Stewart 		 * association destruction
570f8829a4aSRandall Stewart 		 */
571f8829a4aSRandall Stewart 		TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
572139bc87fSRandall Stewart 		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
573f8829a4aSRandall Stewart 		    0, M_DONTWAIT, 1, MT_DATA);
574f8829a4aSRandall Stewart 		if (oper) {
575f8829a4aSRandall Stewart 			struct sctp_paramhdr *ph;
576f8829a4aSRandall Stewart 			uint32_t *ippp;
577f8829a4aSRandall Stewart 
578139bc87fSRandall Stewart 			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
579f8829a4aSRandall Stewart 			    (sizeof(uint32_t) * 3);
580f8829a4aSRandall Stewart 			ph = mtod(oper, struct sctp_paramhdr *);
581f8829a4aSRandall Stewart 			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
582139bc87fSRandall Stewart 			ph->param_length = htons(SCTP_BUF_LEN(oper));
583f8829a4aSRandall Stewart 			ippp = (uint32_t *) (ph + 1);
584a5d547adSRandall Stewart 			*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_1);
585f8829a4aSRandall Stewart 			ippp++;
586f8829a4aSRandall Stewart 			*ippp = control->sinfo_tsn;
587f8829a4aSRandall Stewart 			ippp++;
588f8829a4aSRandall Stewart 			*ippp = ((control->sinfo_stream << 16) | control->sinfo_ssn);
589f8829a4aSRandall Stewart 		}
590a5d547adSRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_1;
591f8829a4aSRandall Stewart 		sctp_abort_an_association(stcb->sctp_ep, stcb,
592ceaad40aSRandall Stewart 		    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
593f8829a4aSRandall Stewart 
594f8829a4aSRandall Stewart 		*abort_flag = 1;
595f8829a4aSRandall Stewart 		return;
596f8829a4aSRandall Stewart 
597f8829a4aSRandall Stewart 	}
598f8829a4aSRandall Stewart 	if (nxt_todel == control->sinfo_ssn) {
599f8829a4aSRandall Stewart 		/* can be delivered right away? */
600b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
601f8829a4aSRandall Stewart 			sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_IMMED_DEL);
60280fefe0aSRandall Stewart 		}
603830d754dSRandall Stewart 		/* EY it wont be queued if it could be delivered directly */
604f8829a4aSRandall Stewart 		queue_needed = 0;
605f8829a4aSRandall Stewart 		asoc->size_on_all_streams -= control->length;
606f8829a4aSRandall Stewart 		sctp_ucount_decr(asoc->cnt_on_all_streams);
607f8829a4aSRandall Stewart 		strm->last_sequence_delivered++;
60877acdc25SRandall Stewart 
609b5c16493SMichael Tuexen 		sctp_mark_non_revokable(asoc, control->sinfo_tsn);
610f8829a4aSRandall Stewart 		sctp_add_to_readq(stcb->sctp_ep, stcb,
611f8829a4aSRandall Stewart 		    control,
612cfde3ff7SRandall Stewart 		    &stcb->sctp_socket->so_rcv, 1,
613cfde3ff7SRandall Stewart 		    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
6144a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(control, &strm->inqueue, next, at) {
615f8829a4aSRandall Stewart 			/* all delivered */
616f8829a4aSRandall Stewart 			nxt_todel = strm->last_sequence_delivered + 1;
617f8829a4aSRandall Stewart 			if (nxt_todel == control->sinfo_ssn) {
618f8829a4aSRandall Stewart 				TAILQ_REMOVE(&strm->inqueue, control, next);
619f8829a4aSRandall Stewart 				asoc->size_on_all_streams -= control->length;
620f8829a4aSRandall Stewart 				sctp_ucount_decr(asoc->cnt_on_all_streams);
621f8829a4aSRandall Stewart 				strm->last_sequence_delivered++;
622f8829a4aSRandall Stewart 				/*
623f8829a4aSRandall Stewart 				 * We ignore the return of deliver_data here
624f8829a4aSRandall Stewart 				 * since we always can hold the chunk on the
625f8829a4aSRandall Stewart 				 * d-queue. And we have a finite number that
626f8829a4aSRandall Stewart 				 * can be delivered from the strq.
627f8829a4aSRandall Stewart 				 */
628b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
629f8829a4aSRandall Stewart 					sctp_log_strm_del(control, NULL,
630f8829a4aSRandall Stewart 					    SCTP_STR_LOG_FROM_IMMED_DEL);
63180fefe0aSRandall Stewart 				}
632b5c16493SMichael Tuexen 				sctp_mark_non_revokable(asoc, control->sinfo_tsn);
633f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
634f8829a4aSRandall Stewart 				    control,
635cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1,
636cfde3ff7SRandall Stewart 				    SCTP_READ_LOCK_NOT_HELD,
637cfde3ff7SRandall Stewart 				    SCTP_SO_NOT_LOCKED);
638f8829a4aSRandall Stewart 				continue;
639f8829a4aSRandall Stewart 			}
640f8829a4aSRandall Stewart 			break;
641f8829a4aSRandall Stewart 		}
642f8829a4aSRandall Stewart 	}
643f8829a4aSRandall Stewart 	if (queue_needed) {
644f8829a4aSRandall Stewart 		/*
645f8829a4aSRandall Stewart 		 * Ok, we did not deliver this guy, find the correct place
646f8829a4aSRandall Stewart 		 * to put it on the queue.
647f8829a4aSRandall Stewart 		 */
64820b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(asoc->cumulative_tsn, control->sinfo_tsn)) {
649c40e9cf2SRandall Stewart 			goto protocol_error;
650c40e9cf2SRandall Stewart 		}
651f8829a4aSRandall Stewart 		if (TAILQ_EMPTY(&strm->inqueue)) {
652f8829a4aSRandall Stewart 			/* Empty queue */
653b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
654f8829a4aSRandall Stewart 				sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INSERT_HD);
65580fefe0aSRandall Stewart 			}
656f8829a4aSRandall Stewart 			TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
657f8829a4aSRandall Stewart 		} else {
658f8829a4aSRandall Stewart 			TAILQ_FOREACH(at, &strm->inqueue, next) {
65920b07a4dSMichael Tuexen 				if (SCTP_SSN_GT(at->sinfo_ssn, control->sinfo_ssn)) {
660f8829a4aSRandall Stewart 					/*
661f8829a4aSRandall Stewart 					 * one in queue is bigger than the
662f8829a4aSRandall Stewart 					 * new one, insert before this one
663f8829a4aSRandall Stewart 					 */
664b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
665f8829a4aSRandall Stewart 						sctp_log_strm_del(control, at,
666f8829a4aSRandall Stewart 						    SCTP_STR_LOG_FROM_INSERT_MD);
66780fefe0aSRandall Stewart 					}
668f8829a4aSRandall Stewart 					TAILQ_INSERT_BEFORE(at, control, next);
669f8829a4aSRandall Stewart 					break;
670f8829a4aSRandall Stewart 				} else if (at->sinfo_ssn == control->sinfo_ssn) {
671f8829a4aSRandall Stewart 					/*
672f8829a4aSRandall Stewart 					 * Gak, He sent me a duplicate str
673f8829a4aSRandall Stewart 					 * seq number
674f8829a4aSRandall Stewart 					 */
675f8829a4aSRandall Stewart 					/*
676f8829a4aSRandall Stewart 					 * foo bar, I guess I will just free
677f8829a4aSRandall Stewart 					 * this new guy, should we abort
678f8829a4aSRandall Stewart 					 * too? FIX ME MAYBE? Or it COULD be
679f8829a4aSRandall Stewart 					 * that the SSN's have wrapped.
680f8829a4aSRandall Stewart 					 * Maybe I should compare to TSN
681f8829a4aSRandall Stewart 					 * somehow... sigh for now just blow
682f8829a4aSRandall Stewart 					 * away the chunk!
683f8829a4aSRandall Stewart 					 */
684f8829a4aSRandall Stewart 
685f8829a4aSRandall Stewart 					if (control->data)
686f8829a4aSRandall Stewart 						sctp_m_freem(control->data);
687f8829a4aSRandall Stewart 					control->data = NULL;
688f8829a4aSRandall Stewart 					asoc->size_on_all_streams -= control->length;
689f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->cnt_on_all_streams);
69024f52bbdSMichael Tuexen 					if (control->whoFrom) {
691f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
6925bead436SRandall Stewart 						control->whoFrom = NULL;
69324f52bbdSMichael Tuexen 					}
694f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
695f8829a4aSRandall Stewart 					return;
696f8829a4aSRandall Stewart 				} else {
697f8829a4aSRandall Stewart 					if (TAILQ_NEXT(at, next) == NULL) {
698f8829a4aSRandall Stewart 						/*
699f8829a4aSRandall Stewart 						 * We are at the end, insert
700f8829a4aSRandall Stewart 						 * it after this one
701f8829a4aSRandall Stewart 						 */
702b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
703f8829a4aSRandall Stewart 							sctp_log_strm_del(control, at,
704f8829a4aSRandall Stewart 							    SCTP_STR_LOG_FROM_INSERT_TL);
70580fefe0aSRandall Stewart 						}
706f8829a4aSRandall Stewart 						TAILQ_INSERT_AFTER(&strm->inqueue,
707f8829a4aSRandall Stewart 						    at, control, next);
708f8829a4aSRandall Stewart 						break;
709f8829a4aSRandall Stewart 					}
710f8829a4aSRandall Stewart 				}
711f8829a4aSRandall Stewart 			}
712f8829a4aSRandall Stewart 		}
713f8829a4aSRandall Stewart 	}
714f8829a4aSRandall Stewart }
715f8829a4aSRandall Stewart 
716f8829a4aSRandall Stewart /*
717f8829a4aSRandall Stewart  * Returns two things: You get the total size of the deliverable parts of the
718f8829a4aSRandall Stewart  * first fragmented message on the reassembly queue. And you get a 1 back if
719f8829a4aSRandall Stewart  * all of the message is ready or a 0 back if the message is still incomplete
720f8829a4aSRandall Stewart  */
721f8829a4aSRandall Stewart static int
722f8829a4aSRandall Stewart sctp_is_all_msg_on_reasm(struct sctp_association *asoc, uint32_t * t_size)
723f8829a4aSRandall Stewart {
724f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
725f8829a4aSRandall Stewart 	uint32_t tsn;
726f8829a4aSRandall Stewart 
727f8829a4aSRandall Stewart 	*t_size = 0;
728f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
729f8829a4aSRandall Stewart 	if (chk == NULL) {
730f8829a4aSRandall Stewart 		/* nothing on the queue */
731f8829a4aSRandall Stewart 		return (0);
732f8829a4aSRandall Stewart 	}
733f8829a4aSRandall Stewart 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
734f8829a4aSRandall Stewart 		/* Not a first on the queue */
735f8829a4aSRandall Stewart 		return (0);
736f8829a4aSRandall Stewart 	}
737f8829a4aSRandall Stewart 	tsn = chk->rec.data.TSN_seq;
7384a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(chk, &asoc->reasmqueue, sctp_next) {
739f8829a4aSRandall Stewart 		if (tsn != chk->rec.data.TSN_seq) {
740f8829a4aSRandall Stewart 			return (0);
741f8829a4aSRandall Stewart 		}
742f8829a4aSRandall Stewart 		*t_size += chk->send_size;
743f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
744f8829a4aSRandall Stewart 			return (1);
745f8829a4aSRandall Stewart 		}
746f8829a4aSRandall Stewart 		tsn++;
747f8829a4aSRandall Stewart 	}
748f8829a4aSRandall Stewart 	return (0);
749f8829a4aSRandall Stewart }
750f8829a4aSRandall Stewart 
751f8829a4aSRandall Stewart static void
752f8829a4aSRandall Stewart sctp_deliver_reasm_check(struct sctp_tcb *stcb, struct sctp_association *asoc)
753f8829a4aSRandall Stewart {
754f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
755f8829a4aSRandall Stewart 	uint16_t nxt_todel;
756810ec536SMichael Tuexen 	uint32_t tsize, pd_point;
757f8829a4aSRandall Stewart 
758139bc87fSRandall Stewart doit_again:
759f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
760f8829a4aSRandall Stewart 	if (chk == NULL) {
761f8829a4aSRandall Stewart 		/* Huh? */
762f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = 0;
763f8829a4aSRandall Stewart 		asoc->cnt_on_reasm_queue = 0;
764f8829a4aSRandall Stewart 		return;
765f8829a4aSRandall Stewart 	}
766f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress == 0) {
767f8829a4aSRandall Stewart 		nxt_todel =
768f8829a4aSRandall Stewart 		    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
769f8829a4aSRandall Stewart 		if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
770f8829a4aSRandall Stewart 		    (nxt_todel == chk->rec.data.stream_seq ||
771f8829a4aSRandall Stewart 		    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
772f8829a4aSRandall Stewart 			/*
773f8829a4aSRandall Stewart 			 * Yep the first one is here and its ok to deliver
774f8829a4aSRandall Stewart 			 * but should we?
775f8829a4aSRandall Stewart 			 */
776810ec536SMichael Tuexen 			if (stcb->sctp_socket) {
77724ae5c4aSMichael Tuexen 				pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket),
778810ec536SMichael Tuexen 				    stcb->sctp_ep->partial_delivery_point);
779810ec536SMichael Tuexen 			} else {
780810ec536SMichael Tuexen 				pd_point = stcb->sctp_ep->partial_delivery_point;
781810ec536SMichael Tuexen 			}
782810ec536SMichael Tuexen 			if (sctp_is_all_msg_on_reasm(asoc, &tsize) || (tsize >= pd_point)) {
783f8829a4aSRandall Stewart 
784f8829a4aSRandall Stewart 				/*
785f8829a4aSRandall Stewart 				 * Yes, we setup to start reception, by
786f8829a4aSRandall Stewart 				 * backing down the TSN just in case we
787f8829a4aSRandall Stewart 				 * can't deliver. If we
788f8829a4aSRandall Stewart 				 */
789f8829a4aSRandall Stewart 				asoc->fragmented_delivery_inprogress = 1;
790f8829a4aSRandall Stewart 				asoc->tsn_last_delivered =
791f8829a4aSRandall Stewart 				    chk->rec.data.TSN_seq - 1;
792f8829a4aSRandall Stewart 				asoc->str_of_pdapi =
793f8829a4aSRandall Stewart 				    chk->rec.data.stream_number;
794f8829a4aSRandall Stewart 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
795f8829a4aSRandall Stewart 				asoc->pdapi_ppid = chk->rec.data.payloadtype;
796f8829a4aSRandall Stewart 				asoc->fragment_flags = chk->rec.data.rcv_flags;
797f8829a4aSRandall Stewart 				sctp_service_reassembly(stcb, asoc);
798f8829a4aSRandall Stewart 			}
799f8829a4aSRandall Stewart 		}
800f8829a4aSRandall Stewart 	} else {
801139bc87fSRandall Stewart 		/*
802139bc87fSRandall Stewart 		 * Service re-assembly will deliver stream data queued at
803139bc87fSRandall Stewart 		 * the end of fragmented delivery.. but it wont know to go
804139bc87fSRandall Stewart 		 * back and call itself again... we do that here with the
805139bc87fSRandall Stewart 		 * got doit_again
806139bc87fSRandall Stewart 		 */
807f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
808139bc87fSRandall Stewart 		if (asoc->fragmented_delivery_inprogress == 0) {
809139bc87fSRandall Stewart 			/*
810139bc87fSRandall Stewart 			 * finished our Fragmented delivery, could be more
811139bc87fSRandall Stewart 			 * waiting?
812139bc87fSRandall Stewart 			 */
813139bc87fSRandall Stewart 			goto doit_again;
814139bc87fSRandall Stewart 		}
815f8829a4aSRandall Stewart 	}
816f8829a4aSRandall Stewart }
817f8829a4aSRandall Stewart 
818f8829a4aSRandall Stewart /*
819f8829a4aSRandall Stewart  * Dump onto the re-assembly queue, in its proper place. After dumping on the
820f8829a4aSRandall Stewart  * queue, see if anthing can be delivered. If so pull it off (or as much as
821f8829a4aSRandall Stewart  * we can. If we run out of space then we must dump what we can and set the
822f8829a4aSRandall Stewart  * appropriate flag to say we queued what we could.
823f8829a4aSRandall Stewart  */
824f8829a4aSRandall Stewart static void
825f8829a4aSRandall Stewart sctp_queue_data_for_reasm(struct sctp_tcb *stcb, struct sctp_association *asoc,
826f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk, int *abort_flag)
827f8829a4aSRandall Stewart {
828f8829a4aSRandall Stewart 	struct mbuf *oper;
829f8829a4aSRandall Stewart 	uint32_t cum_ackp1, last_tsn, prev_tsn, post_tsn;
830f8829a4aSRandall Stewart 	u_char last_flags;
831f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *at, *prev, *next;
832f8829a4aSRandall Stewart 
833f8829a4aSRandall Stewart 	prev = next = NULL;
834f8829a4aSRandall Stewart 	cum_ackp1 = asoc->tsn_last_delivered + 1;
835f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->reasmqueue)) {
836f8829a4aSRandall Stewart 		/* This is the first one on the queue */
837f8829a4aSRandall Stewart 		TAILQ_INSERT_HEAD(&asoc->reasmqueue, chk, sctp_next);
838f8829a4aSRandall Stewart 		/*
839f8829a4aSRandall Stewart 		 * we do not check for delivery of anything when only one
840f8829a4aSRandall Stewart 		 * fragment is here
841f8829a4aSRandall Stewart 		 */
842f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = chk->send_size;
843f8829a4aSRandall Stewart 		sctp_ucount_incr(asoc->cnt_on_reasm_queue);
844f8829a4aSRandall Stewart 		if (chk->rec.data.TSN_seq == cum_ackp1) {
845f8829a4aSRandall Stewart 			if (asoc->fragmented_delivery_inprogress == 0 &&
846f8829a4aSRandall Stewart 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) !=
847f8829a4aSRandall Stewart 			    SCTP_DATA_FIRST_FRAG) {
848f8829a4aSRandall Stewart 				/*
849f8829a4aSRandall Stewart 				 * An empty queue, no delivery inprogress,
850f8829a4aSRandall Stewart 				 * we hit the next one and it does NOT have
851f8829a4aSRandall Stewart 				 * a FIRST fragment mark.
852f8829a4aSRandall Stewart 				 */
853ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not first, no fragmented delivery in progress\n");
854139bc87fSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
855f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
856f8829a4aSRandall Stewart 
857f8829a4aSRandall Stewart 				if (oper) {
858f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
859f8829a4aSRandall Stewart 					uint32_t *ippp;
860f8829a4aSRandall Stewart 
861139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) =
862f8829a4aSRandall Stewart 					    sizeof(struct sctp_paramhdr) +
863f8829a4aSRandall Stewart 					    (sizeof(uint32_t) * 3);
864f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
865f8829a4aSRandall Stewart 					ph->param_type =
866f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
867139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
868f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
869a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_2);
870f8829a4aSRandall Stewart 					ippp++;
871f8829a4aSRandall Stewart 					*ippp = chk->rec.data.TSN_seq;
872f8829a4aSRandall Stewart 					ippp++;
873f8829a4aSRandall Stewart 					*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
874f8829a4aSRandall Stewart 
875f8829a4aSRandall Stewart 				}
876a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_2;
877f8829a4aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb,
878ceaad40aSRandall Stewart 				    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
879f8829a4aSRandall Stewart 				*abort_flag = 1;
880f8829a4aSRandall Stewart 			} else if (asoc->fragmented_delivery_inprogress &&
881f8829a4aSRandall Stewart 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
882f8829a4aSRandall Stewart 				/*
883f8829a4aSRandall Stewart 				 * We are doing a partial delivery and the
884f8829a4aSRandall Stewart 				 * NEXT chunk MUST be either the LAST or
885f8829a4aSRandall Stewart 				 * MIDDLE fragment NOT a FIRST
886f8829a4aSRandall Stewart 				 */
887ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS a first and fragmented delivery in progress\n");
888139bc87fSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
889f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
890f8829a4aSRandall Stewart 				if (oper) {
891f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
892f8829a4aSRandall Stewart 					uint32_t *ippp;
893f8829a4aSRandall Stewart 
894139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) =
895f8829a4aSRandall Stewart 					    sizeof(struct sctp_paramhdr) +
896f8829a4aSRandall Stewart 					    (3 * sizeof(uint32_t));
897f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
898f8829a4aSRandall Stewart 					ph->param_type =
899f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
900139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
901f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
902a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_3);
903f8829a4aSRandall Stewart 					ippp++;
904f8829a4aSRandall Stewart 					*ippp = chk->rec.data.TSN_seq;
905f8829a4aSRandall Stewart 					ippp++;
906f8829a4aSRandall Stewart 					*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
907f8829a4aSRandall Stewart 				}
908a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_3;
909f8829a4aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb,
910ceaad40aSRandall Stewart 				    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
911f8829a4aSRandall Stewart 				*abort_flag = 1;
912f8829a4aSRandall Stewart 			} else if (asoc->fragmented_delivery_inprogress) {
913f8829a4aSRandall Stewart 				/*
914f8829a4aSRandall Stewart 				 * Here we are ok with a MIDDLE or LAST
915f8829a4aSRandall Stewart 				 * piece
916f8829a4aSRandall Stewart 				 */
917f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
918f8829a4aSRandall Stewart 				    asoc->str_of_pdapi) {
919f8829a4aSRandall Stewart 					/* Got to be the right STR No */
920ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream number %d vs %d\n",
921f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
922f8829a4aSRandall Stewart 					    asoc->str_of_pdapi);
923139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
924f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
925f8829a4aSRandall Stewart 					if (oper) {
926f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
927f8829a4aSRandall Stewart 						uint32_t *ippp;
928f8829a4aSRandall Stewart 
929139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
930f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
931f8829a4aSRandall Stewart 						    (sizeof(uint32_t) * 3);
932f8829a4aSRandall Stewart 						ph = mtod(oper,
933f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
934f8829a4aSRandall Stewart 						ph->param_type =
935f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
936f8829a4aSRandall Stewart 						ph->param_length =
937139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
938f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
939a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_4);
940f8829a4aSRandall Stewart 						ippp++;
941f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
942f8829a4aSRandall Stewart 						ippp++;
943f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
944f8829a4aSRandall Stewart 					}
945a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_4;
946f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
947ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
948f8829a4aSRandall Stewart 					*abort_flag = 1;
949f8829a4aSRandall Stewart 				} else if ((asoc->fragment_flags & SCTP_DATA_UNORDERED) !=
950f8829a4aSRandall Stewart 					    SCTP_DATA_UNORDERED &&
951b5c16493SMichael Tuexen 				    chk->rec.data.stream_seq != asoc->ssn_of_pdapi) {
952f8829a4aSRandall Stewart 					/* Got to be the right STR Seq */
953ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream seq %d vs %d\n",
954f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
955f8829a4aSRandall Stewart 					    asoc->ssn_of_pdapi);
956139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
957f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
958f8829a4aSRandall Stewart 					if (oper) {
959f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
960f8829a4aSRandall Stewart 						uint32_t *ippp;
961f8829a4aSRandall Stewart 
962139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
963f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
964f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
965f8829a4aSRandall Stewart 						ph = mtod(oper,
966f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
967f8829a4aSRandall Stewart 						ph->param_type =
968f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
969f8829a4aSRandall Stewart 						ph->param_length =
970139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
971f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
972a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_5);
973f8829a4aSRandall Stewart 						ippp++;
974f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
975f8829a4aSRandall Stewart 						ippp++;
976f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
977f8829a4aSRandall Stewart 
978f8829a4aSRandall Stewart 					}
979a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_5;
980f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
981ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
982f8829a4aSRandall Stewart 					*abort_flag = 1;
983f8829a4aSRandall Stewart 				}
984f8829a4aSRandall Stewart 			}
985f8829a4aSRandall Stewart 		}
986f8829a4aSRandall Stewart 		return;
987f8829a4aSRandall Stewart 	}
988f8829a4aSRandall Stewart 	/* Find its place */
989f8829a4aSRandall Stewart 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
99020b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(at->rec.data.TSN_seq, chk->rec.data.TSN_seq)) {
991f8829a4aSRandall Stewart 			/*
992f8829a4aSRandall Stewart 			 * one in queue is bigger than the new one, insert
993f8829a4aSRandall Stewart 			 * before this one
994f8829a4aSRandall Stewart 			 */
995f8829a4aSRandall Stewart 			/* A check */
996f8829a4aSRandall Stewart 			asoc->size_on_reasm_queue += chk->send_size;
997f8829a4aSRandall Stewart 			sctp_ucount_incr(asoc->cnt_on_reasm_queue);
998f8829a4aSRandall Stewart 			next = at;
999f8829a4aSRandall Stewart 			TAILQ_INSERT_BEFORE(at, chk, sctp_next);
1000f8829a4aSRandall Stewart 			break;
1001f8829a4aSRandall Stewart 		} else if (at->rec.data.TSN_seq == chk->rec.data.TSN_seq) {
1002f8829a4aSRandall Stewart 			/* Gak, He sent me a duplicate str seq number */
1003f8829a4aSRandall Stewart 			/*
1004f8829a4aSRandall Stewart 			 * foo bar, I guess I will just free this new guy,
1005f8829a4aSRandall Stewart 			 * should we abort too? FIX ME MAYBE? Or it COULD be
1006f8829a4aSRandall Stewart 			 * that the SSN's have wrapped. Maybe I should
1007f8829a4aSRandall Stewart 			 * compare to TSN somehow... sigh for now just blow
1008f8829a4aSRandall Stewart 			 * away the chunk!
1009f8829a4aSRandall Stewart 			 */
1010f8829a4aSRandall Stewart 			if (chk->data) {
1011f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
1012f8829a4aSRandall Stewart 				chk->data = NULL;
1013f8829a4aSRandall Stewart 			}
1014*689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
1015f8829a4aSRandall Stewart 			return;
1016f8829a4aSRandall Stewart 		} else {
1017f8829a4aSRandall Stewart 			last_flags = at->rec.data.rcv_flags;
1018f8829a4aSRandall Stewart 			last_tsn = at->rec.data.TSN_seq;
1019f8829a4aSRandall Stewart 			prev = at;
1020f8829a4aSRandall Stewart 			if (TAILQ_NEXT(at, sctp_next) == NULL) {
1021f8829a4aSRandall Stewart 				/*
1022f8829a4aSRandall Stewart 				 * We are at the end, insert it after this
1023f8829a4aSRandall Stewart 				 * one
1024f8829a4aSRandall Stewart 				 */
1025f8829a4aSRandall Stewart 				/* check it first */
1026f8829a4aSRandall Stewart 				asoc->size_on_reasm_queue += chk->send_size;
1027f8829a4aSRandall Stewart 				sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1028f8829a4aSRandall Stewart 				TAILQ_INSERT_AFTER(&asoc->reasmqueue, at, chk, sctp_next);
1029f8829a4aSRandall Stewart 				break;
1030f8829a4aSRandall Stewart 			}
1031f8829a4aSRandall Stewart 		}
1032f8829a4aSRandall Stewart 	}
1033f8829a4aSRandall Stewart 	/* Now the audits */
1034f8829a4aSRandall Stewart 	if (prev) {
1035f8829a4aSRandall Stewart 		prev_tsn = chk->rec.data.TSN_seq - 1;
1036f8829a4aSRandall Stewart 		if (prev_tsn == prev->rec.data.TSN_seq) {
1037f8829a4aSRandall Stewart 			/*
1038f8829a4aSRandall Stewart 			 * Ok the one I am dropping onto the end is the
1039f8829a4aSRandall Stewart 			 * NEXT. A bit of valdiation here.
1040f8829a4aSRandall Stewart 			 */
1041f8829a4aSRandall Stewart 			if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1042f8829a4aSRandall Stewart 			    SCTP_DATA_FIRST_FRAG ||
1043f8829a4aSRandall Stewart 			    (prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1044f8829a4aSRandall Stewart 			    SCTP_DATA_MIDDLE_FRAG) {
1045f8829a4aSRandall Stewart 				/*
1046f8829a4aSRandall Stewart 				 * Insert chk MUST be a MIDDLE or LAST
1047f8829a4aSRandall Stewart 				 * fragment
1048f8829a4aSRandall Stewart 				 */
1049f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1050f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1051ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - It can be a midlle or last but not a first\n");
1052ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it's a FIRST!\n");
1053139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1054f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1055f8829a4aSRandall Stewart 					if (oper) {
1056f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1057f8829a4aSRandall Stewart 						uint32_t *ippp;
1058f8829a4aSRandall Stewart 
1059139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1060f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1061f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1062f8829a4aSRandall Stewart 						ph = mtod(oper,
1063f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1064f8829a4aSRandall Stewart 						ph->param_type =
1065f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1066f8829a4aSRandall Stewart 						ph->param_length =
1067139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1068f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1069a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_6);
1070f8829a4aSRandall Stewart 						ippp++;
1071f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1072f8829a4aSRandall Stewart 						ippp++;
1073f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1074f8829a4aSRandall Stewart 
1075f8829a4aSRandall Stewart 					}
1076a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_6;
1077f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1078ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1079f8829a4aSRandall Stewart 					*abort_flag = 1;
1080f8829a4aSRandall Stewart 					return;
1081f8829a4aSRandall Stewart 				}
1082f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
1083f8829a4aSRandall Stewart 				    prev->rec.data.stream_number) {
1084f8829a4aSRandall Stewart 					/*
1085f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1086f8829a4aSRandall Stewart 					 * they must be the same.
1087f8829a4aSRandall Stewart 					 */
1088ad81507eSRandall Stewart 					SCTP_PRINTF("Prev check - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1089f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
1090f8829a4aSRandall Stewart 					    prev->rec.data.stream_number);
1091139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1092f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1093f8829a4aSRandall Stewart 					if (oper) {
1094f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1095f8829a4aSRandall Stewart 						uint32_t *ippp;
1096f8829a4aSRandall Stewart 
1097139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1098f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1099f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1100f8829a4aSRandall Stewart 						ph = mtod(oper,
1101f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1102f8829a4aSRandall Stewart 						ph->param_type =
1103f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1104f8829a4aSRandall Stewart 						ph->param_length =
1105139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1106f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1107a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_7);
1108f8829a4aSRandall Stewart 						ippp++;
1109f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1110f8829a4aSRandall Stewart 						ippp++;
1111f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1112f8829a4aSRandall Stewart 					}
1113a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_7;
1114f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1115ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1116f8829a4aSRandall Stewart 
1117f8829a4aSRandall Stewart 					*abort_flag = 1;
1118f8829a4aSRandall Stewart 					return;
1119f8829a4aSRandall Stewart 				}
1120f8829a4aSRandall Stewart 				if ((prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1121f8829a4aSRandall Stewart 				    chk->rec.data.stream_seq !=
1122f8829a4aSRandall Stewart 				    prev->rec.data.stream_seq) {
1123f8829a4aSRandall Stewart 					/*
1124f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1125f8829a4aSRandall Stewart 					 * they must be the same.
1126f8829a4aSRandall Stewart 					 */
1127ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1128f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
1129f8829a4aSRandall Stewart 					    prev->rec.data.stream_seq);
1130139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1131f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1132f8829a4aSRandall Stewart 					if (oper) {
1133f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1134f8829a4aSRandall Stewart 						uint32_t *ippp;
1135f8829a4aSRandall Stewart 
1136139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1137f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1138f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1139f8829a4aSRandall Stewart 						ph = mtod(oper,
1140f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1141f8829a4aSRandall Stewart 						ph->param_type =
1142f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1143f8829a4aSRandall Stewart 						ph->param_length =
1144139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1145f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1146a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_8);
1147f8829a4aSRandall Stewart 						ippp++;
1148f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1149f8829a4aSRandall Stewart 						ippp++;
1150f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1151f8829a4aSRandall Stewart 					}
1152a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_8;
1153f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1154ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1155f8829a4aSRandall Stewart 
1156f8829a4aSRandall Stewart 					*abort_flag = 1;
1157f8829a4aSRandall Stewart 					return;
1158f8829a4aSRandall Stewart 				}
1159f8829a4aSRandall Stewart 			} else if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1160f8829a4aSRandall Stewart 			    SCTP_DATA_LAST_FRAG) {
1161f8829a4aSRandall Stewart 				/* Insert chk MUST be a FIRST */
1162f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1163f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1164ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, evil plot, its not FIRST and it must be!\n");
1165139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1166f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1167f8829a4aSRandall Stewart 					if (oper) {
1168f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1169f8829a4aSRandall Stewart 						uint32_t *ippp;
1170f8829a4aSRandall Stewart 
1171139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1172f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1173f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1174f8829a4aSRandall Stewart 						ph = mtod(oper,
1175f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1176f8829a4aSRandall Stewart 						ph->param_type =
1177f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1178f8829a4aSRandall Stewart 						ph->param_length =
1179139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1180f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1181a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_9);
1182f8829a4aSRandall Stewart 						ippp++;
1183f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1184f8829a4aSRandall Stewart 						ippp++;
1185f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1186f8829a4aSRandall Stewart 
1187f8829a4aSRandall Stewart 					}
1188a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_9;
1189f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1190ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1191f8829a4aSRandall Stewart 
1192f8829a4aSRandall Stewart 					*abort_flag = 1;
1193f8829a4aSRandall Stewart 					return;
1194f8829a4aSRandall Stewart 				}
1195f8829a4aSRandall Stewart 			}
1196f8829a4aSRandall Stewart 		}
1197f8829a4aSRandall Stewart 	}
1198f8829a4aSRandall Stewart 	if (next) {
1199f8829a4aSRandall Stewart 		post_tsn = chk->rec.data.TSN_seq + 1;
1200f8829a4aSRandall Stewart 		if (post_tsn == next->rec.data.TSN_seq) {
1201f8829a4aSRandall Stewart 			/*
1202f8829a4aSRandall Stewart 			 * Ok the one I am inserting ahead of is my NEXT
1203f8829a4aSRandall Stewart 			 * one. A bit of valdiation here.
1204f8829a4aSRandall Stewart 			 */
1205f8829a4aSRandall Stewart 			if (next->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
1206f8829a4aSRandall Stewart 				/* Insert chk MUST be a last fragment */
1207f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK)
1208f8829a4aSRandall Stewart 				    != SCTP_DATA_LAST_FRAG) {
1209ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is FIRST, we must be LAST\n");
1210ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not a last!\n");
1211139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1212f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1213f8829a4aSRandall Stewart 					if (oper) {
1214f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1215f8829a4aSRandall Stewart 						uint32_t *ippp;
1216f8829a4aSRandall Stewart 
1217139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1218f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1219f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1220f8829a4aSRandall Stewart 						ph = mtod(oper,
1221f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1222f8829a4aSRandall Stewart 						ph->param_type =
1223f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1224f8829a4aSRandall Stewart 						ph->param_length =
1225139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1226f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1227a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_10);
1228f8829a4aSRandall Stewart 						ippp++;
1229f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1230f8829a4aSRandall Stewart 						ippp++;
1231f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1232f8829a4aSRandall Stewart 					}
1233a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_10;
1234f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1235ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1236f8829a4aSRandall Stewart 
1237f8829a4aSRandall Stewart 					*abort_flag = 1;
1238f8829a4aSRandall Stewart 					return;
1239f8829a4aSRandall Stewart 				}
1240f8829a4aSRandall Stewart 			} else if ((next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1241f8829a4aSRandall Stewart 				    SCTP_DATA_MIDDLE_FRAG ||
1242f8829a4aSRandall Stewart 				    (next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1243f8829a4aSRandall Stewart 			    SCTP_DATA_LAST_FRAG) {
1244f8829a4aSRandall Stewart 				/*
1245f8829a4aSRandall Stewart 				 * Insert chk CAN be MIDDLE or FIRST NOT
1246f8829a4aSRandall Stewart 				 * LAST
1247f8829a4aSRandall Stewart 				 */
1248f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1249f8829a4aSRandall Stewart 				    SCTP_DATA_LAST_FRAG) {
1250ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is a MIDDLE/LAST\n");
1251ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, new prev chunk is a LAST\n");
1252139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1253f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1254f8829a4aSRandall Stewart 					if (oper) {
1255f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1256f8829a4aSRandall Stewart 						uint32_t *ippp;
1257f8829a4aSRandall Stewart 
1258139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1259f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1260f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1261f8829a4aSRandall Stewart 						ph = mtod(oper,
1262f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1263f8829a4aSRandall Stewart 						ph->param_type =
1264f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1265f8829a4aSRandall Stewart 						ph->param_length =
1266139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1267f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1268a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_11);
1269f8829a4aSRandall Stewart 						ippp++;
1270f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1271f8829a4aSRandall Stewart 						ippp++;
1272f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1273f8829a4aSRandall Stewart 
1274f8829a4aSRandall Stewart 					}
1275a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_11;
1276f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1277ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1278f8829a4aSRandall Stewart 
1279f8829a4aSRandall Stewart 					*abort_flag = 1;
1280f8829a4aSRandall Stewart 					return;
1281f8829a4aSRandall Stewart 				}
1282f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
1283f8829a4aSRandall Stewart 				    next->rec.data.stream_number) {
1284f8829a4aSRandall Stewart 					/*
1285f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1286f8829a4aSRandall Stewart 					 * they must be the same.
1287f8829a4aSRandall Stewart 					 */
1288ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1289f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
1290f8829a4aSRandall Stewart 					    next->rec.data.stream_number);
1291139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1292f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1293f8829a4aSRandall Stewart 					if (oper) {
1294f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1295f8829a4aSRandall Stewart 						uint32_t *ippp;
1296f8829a4aSRandall Stewart 
1297139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1298f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1299f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1300f8829a4aSRandall Stewart 						ph = mtod(oper,
1301f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1302f8829a4aSRandall Stewart 						ph->param_type =
1303f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1304f8829a4aSRandall Stewart 						ph->param_length =
1305139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1306f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1307a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_12);
1308f8829a4aSRandall Stewart 						ippp++;
1309f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1310f8829a4aSRandall Stewart 						ippp++;
1311f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1312f8829a4aSRandall Stewart 
1313f8829a4aSRandall Stewart 					}
1314a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_12;
1315f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1316ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1317f8829a4aSRandall Stewart 
1318f8829a4aSRandall Stewart 					*abort_flag = 1;
1319f8829a4aSRandall Stewart 					return;
1320f8829a4aSRandall Stewart 				}
1321f8829a4aSRandall Stewart 				if ((next->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1322f8829a4aSRandall Stewart 				    chk->rec.data.stream_seq !=
1323f8829a4aSRandall Stewart 				    next->rec.data.stream_seq) {
1324f8829a4aSRandall Stewart 					/*
1325f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1326f8829a4aSRandall Stewart 					 * they must be the same.
1327f8829a4aSRandall Stewart 					 */
1328ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1329f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
1330f8829a4aSRandall Stewart 					    next->rec.data.stream_seq);
1331139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1332f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1333f8829a4aSRandall Stewart 					if (oper) {
1334f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1335f8829a4aSRandall Stewart 						uint32_t *ippp;
1336f8829a4aSRandall Stewart 
1337139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1338f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1339f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1340f8829a4aSRandall Stewart 						ph = mtod(oper,
1341f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1342f8829a4aSRandall Stewart 						ph->param_type =
1343f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1344f8829a4aSRandall Stewart 						ph->param_length =
1345139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1346f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1347a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_13);
1348f8829a4aSRandall Stewart 						ippp++;
1349f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1350f8829a4aSRandall Stewart 						ippp++;
1351f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1352f8829a4aSRandall Stewart 					}
1353a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_13;
1354f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1355ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1356f8829a4aSRandall Stewart 
1357f8829a4aSRandall Stewart 					*abort_flag = 1;
1358f8829a4aSRandall Stewart 					return;
1359f8829a4aSRandall Stewart 				}
1360f8829a4aSRandall Stewart 			}
1361f8829a4aSRandall Stewart 		}
1362f8829a4aSRandall Stewart 	}
1363f8829a4aSRandall Stewart 	/* Do we need to do some delivery? check */
1364f8829a4aSRandall Stewart 	sctp_deliver_reasm_check(stcb, asoc);
1365f8829a4aSRandall Stewart }
1366f8829a4aSRandall Stewart 
1367f8829a4aSRandall Stewart /*
1368f8829a4aSRandall Stewart  * This is an unfortunate routine. It checks to make sure a evil guy is not
1369f8829a4aSRandall Stewart  * stuffing us full of bad packet fragments. A broken peer could also do this
1370f8829a4aSRandall Stewart  * but this is doubtful. It is to bad I must worry about evil crackers sigh
1371f8829a4aSRandall Stewart  * :< more cycles.
1372f8829a4aSRandall Stewart  */
1373f8829a4aSRandall Stewart static int
1374f8829a4aSRandall Stewart sctp_does_tsn_belong_to_reasm(struct sctp_association *asoc,
1375f8829a4aSRandall Stewart     uint32_t TSN_seq)
1376f8829a4aSRandall Stewart {
1377f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *at;
1378f8829a4aSRandall Stewart 	uint32_t tsn_est;
1379f8829a4aSRandall Stewart 
1380f8829a4aSRandall Stewart 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
138120b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(TSN_seq, at->rec.data.TSN_seq)) {
1382f8829a4aSRandall Stewart 			/* is it one bigger? */
1383f8829a4aSRandall Stewart 			tsn_est = at->rec.data.TSN_seq + 1;
1384f8829a4aSRandall Stewart 			if (tsn_est == TSN_seq) {
1385f8829a4aSRandall Stewart 				/* yep. It better be a last then */
1386f8829a4aSRandall Stewart 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1387f8829a4aSRandall Stewart 				    SCTP_DATA_LAST_FRAG) {
1388f8829a4aSRandall Stewart 					/*
1389f8829a4aSRandall Stewart 					 * Ok this guy belongs next to a guy
1390f8829a4aSRandall Stewart 					 * that is NOT last, it should be a
1391f8829a4aSRandall Stewart 					 * middle/last, not a complete
1392f8829a4aSRandall Stewart 					 * chunk.
1393f8829a4aSRandall Stewart 					 */
1394f8829a4aSRandall Stewart 					return (1);
1395f8829a4aSRandall Stewart 				} else {
1396f8829a4aSRandall Stewart 					/*
1397f8829a4aSRandall Stewart 					 * This guy is ok since its a LAST
1398f8829a4aSRandall Stewart 					 * and the new chunk is a fully
1399f8829a4aSRandall Stewart 					 * self- contained one.
1400f8829a4aSRandall Stewart 					 */
1401f8829a4aSRandall Stewart 					return (0);
1402f8829a4aSRandall Stewart 				}
1403f8829a4aSRandall Stewart 			}
1404f8829a4aSRandall Stewart 		} else if (TSN_seq == at->rec.data.TSN_seq) {
1405f8829a4aSRandall Stewart 			/* Software error since I have a dup? */
1406f8829a4aSRandall Stewart 			return (1);
1407f8829a4aSRandall Stewart 		} else {
1408f8829a4aSRandall Stewart 			/*
1409f8829a4aSRandall Stewart 			 * Ok, 'at' is larger than new chunk but does it
1410f8829a4aSRandall Stewart 			 * need to be right before it.
1411f8829a4aSRandall Stewart 			 */
1412f8829a4aSRandall Stewart 			tsn_est = TSN_seq + 1;
1413f8829a4aSRandall Stewart 			if (tsn_est == at->rec.data.TSN_seq) {
1414f8829a4aSRandall Stewart 				/* Yep, It better be a first */
1415f8829a4aSRandall Stewart 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1416f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1417f8829a4aSRandall Stewart 					return (1);
1418f8829a4aSRandall Stewart 				} else {
1419f8829a4aSRandall Stewart 					return (0);
1420f8829a4aSRandall Stewart 				}
1421f8829a4aSRandall Stewart 			}
1422f8829a4aSRandall Stewart 		}
1423f8829a4aSRandall Stewart 	}
1424f8829a4aSRandall Stewart 	return (0);
1425f8829a4aSRandall Stewart }
1426f8829a4aSRandall Stewart 
1427f8829a4aSRandall Stewart 
1428f8829a4aSRandall Stewart static int
1429f8829a4aSRandall Stewart sctp_process_a_data_chunk(struct sctp_tcb *stcb, struct sctp_association *asoc,
1430f8829a4aSRandall Stewart     struct mbuf **m, int offset, struct sctp_data_chunk *ch, int chk_length,
1431f8829a4aSRandall Stewart     struct sctp_nets *net, uint32_t * high_tsn, int *abort_flag,
1432f8829a4aSRandall Stewart     int *break_flag, int last_chunk)
1433f8829a4aSRandall Stewart {
1434f8829a4aSRandall Stewart 	/* Process a data chunk */
1435f8829a4aSRandall Stewart 	/* struct sctp_tmit_chunk *chk; */
1436f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
1437f8829a4aSRandall Stewart 	uint32_t tsn, gap;
1438f8829a4aSRandall Stewart 	struct mbuf *dmbuf;
1439f8829a4aSRandall Stewart 	int indx, the_len;
1440139bc87fSRandall Stewart 	int need_reasm_check = 0;
1441f8829a4aSRandall Stewart 	uint16_t strmno, strmseq;
1442f8829a4aSRandall Stewart 	struct mbuf *oper;
1443f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
1444f42a358aSRandall Stewart 	int ordered;
1445f42a358aSRandall Stewart 	uint32_t protocol_id;
1446f42a358aSRandall Stewart 	uint8_t chunk_flags;
144717205eccSRandall Stewart 	struct sctp_stream_reset_list *liste;
1448f8829a4aSRandall Stewart 
1449f8829a4aSRandall Stewart 	chk = NULL;
1450f8829a4aSRandall Stewart 	tsn = ntohl(ch->dp.tsn);
1451f42a358aSRandall Stewart 	chunk_flags = ch->ch.chunk_flags;
1452b3f1ea41SRandall Stewart 	if ((chunk_flags & SCTP_DATA_SACK_IMMEDIATELY) == SCTP_DATA_SACK_IMMEDIATELY) {
1453b3f1ea41SRandall Stewart 		asoc->send_sack = 1;
1454b3f1ea41SRandall Stewart 	}
1455f42a358aSRandall Stewart 	protocol_id = ch->dp.protocol_id;
145637f144ebSMichael Tuexen 	ordered = ((chunk_flags & SCTP_DATA_UNORDERED) == 0);
1457b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1458c4739e2fSRandall Stewart 		sctp_log_map(tsn, asoc->cumulative_tsn, asoc->highest_tsn_inside_map, SCTP_MAP_TSN_ENTERS);
145980fefe0aSRandall Stewart 	}
1460ad81507eSRandall Stewart 	if (stcb == NULL) {
1461ad81507eSRandall Stewart 		return (0);
1462ad81507eSRandall Stewart 	}
146380fefe0aSRandall Stewart 	SCTP_LTRACE_CHK(stcb->sctp_ep, stcb, ch->ch.chunk_type, tsn);
146420b07a4dSMichael Tuexen 	if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
1465f8829a4aSRandall Stewart 		/* It is a duplicate */
1466f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvdupdata);
1467f8829a4aSRandall Stewart 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1468f8829a4aSRandall Stewart 			/* Record a dup for the next outbound sack */
1469f8829a4aSRandall Stewart 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1470f8829a4aSRandall Stewart 			asoc->numduptsns++;
1471f8829a4aSRandall Stewart 		}
1472b201f536SRandall Stewart 		asoc->send_sack = 1;
1473f8829a4aSRandall Stewart 		return (0);
1474f8829a4aSRandall Stewart 	}
1475f8829a4aSRandall Stewart 	/* Calculate the number of TSN's between the base and this TSN */
1476d50c1d79SRandall Stewart 	SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
1477f8829a4aSRandall Stewart 	if (gap >= (SCTP_MAPPING_ARRAY << 3)) {
1478f8829a4aSRandall Stewart 		/* Can't hold the bit in the mapping at max array, toss it */
1479f8829a4aSRandall Stewart 		return (0);
1480f8829a4aSRandall Stewart 	}
1481f8829a4aSRandall Stewart 	if (gap >= (uint32_t) (asoc->mapping_array_size << 3)) {
1482207304d4SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
14830696e120SRandall Stewart 		if (sctp_expand_mapping_array(asoc, gap)) {
1484f8829a4aSRandall Stewart 			/* Can't expand, drop it */
1485f8829a4aSRandall Stewart 			return (0);
1486f8829a4aSRandall Stewart 		}
1487f8829a4aSRandall Stewart 	}
148820b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(tsn, *high_tsn)) {
1489f8829a4aSRandall Stewart 		*high_tsn = tsn;
1490f8829a4aSRandall Stewart 	}
1491f8829a4aSRandall Stewart 	/* See if we have received this one already */
149277acdc25SRandall Stewart 	if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap) ||
149377acdc25SRandall Stewart 	    SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, gap)) {
1494f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvdupdata);
1495f8829a4aSRandall Stewart 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1496f8829a4aSRandall Stewart 			/* Record a dup for the next outbound sack */
1497f8829a4aSRandall Stewart 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1498f8829a4aSRandall Stewart 			asoc->numduptsns++;
1499f8829a4aSRandall Stewart 		}
150042551e99SRandall Stewart 		asoc->send_sack = 1;
1501f8829a4aSRandall Stewart 		return (0);
1502f8829a4aSRandall Stewart 	}
1503f8829a4aSRandall Stewart 	/*
1504f8829a4aSRandall Stewart 	 * Check to see about the GONE flag, duplicates would cause a sack
1505f8829a4aSRandall Stewart 	 * to be sent up above
1506f8829a4aSRandall Stewart 	 */
1507ad81507eSRandall Stewart 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1508f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1509f8829a4aSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET))
1510f8829a4aSRandall Stewart 	    ) {
1511f8829a4aSRandall Stewart 		/*
1512f8829a4aSRandall Stewart 		 * wait a minute, this guy is gone, there is no longer a
1513f8829a4aSRandall Stewart 		 * receiver. Send peer an ABORT!
1514f8829a4aSRandall Stewart 		 */
1515f8829a4aSRandall Stewart 		struct mbuf *op_err;
1516f8829a4aSRandall Stewart 
1517f8829a4aSRandall Stewart 		op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1518ceaad40aSRandall Stewart 		sctp_abort_an_association(stcb->sctp_ep, stcb, 0, op_err, SCTP_SO_NOT_LOCKED);
1519f8829a4aSRandall Stewart 		*abort_flag = 1;
1520f8829a4aSRandall Stewart 		return (0);
1521f8829a4aSRandall Stewart 	}
1522f8829a4aSRandall Stewart 	/*
1523f8829a4aSRandall Stewart 	 * Now before going further we see if there is room. If NOT then we
1524f8829a4aSRandall Stewart 	 * MAY let one through only IF this TSN is the one we are waiting
1525f8829a4aSRandall Stewart 	 * for on a partial delivery API.
1526f8829a4aSRandall Stewart 	 */
1527f8829a4aSRandall Stewart 
1528f8829a4aSRandall Stewart 	/* now do the tests */
1529f8829a4aSRandall Stewart 	if (((asoc->cnt_on_all_streams +
1530f8829a4aSRandall Stewart 	    asoc->cnt_on_reasm_queue +
1531b3f1ea41SRandall Stewart 	    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) ||
1532f8829a4aSRandall Stewart 	    (((int)asoc->my_rwnd) <= 0)) {
1533f8829a4aSRandall Stewart 		/*
1534f8829a4aSRandall Stewart 		 * When we have NO room in the rwnd we check to make sure
1535f8829a4aSRandall Stewart 		 * the reader is doing its job...
1536f8829a4aSRandall Stewart 		 */
1537f8829a4aSRandall Stewart 		if (stcb->sctp_socket->so_rcv.sb_cc) {
1538f8829a4aSRandall Stewart 			/* some to read, wake-up */
1539ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1540ceaad40aSRandall Stewart 			struct socket *so;
1541ceaad40aSRandall Stewart 
1542ceaad40aSRandall Stewart 			so = SCTP_INP_SO(stcb->sctp_ep);
1543ceaad40aSRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, 1);
1544ceaad40aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
1545ceaad40aSRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
1546ceaad40aSRandall Stewart 			SCTP_TCB_LOCK(stcb);
1547ceaad40aSRandall Stewart 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
1548ceaad40aSRandall Stewart 			if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1549ceaad40aSRandall Stewart 				/* assoc was freed while we were unlocked */
1550ceaad40aSRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
1551ceaad40aSRandall Stewart 				return (0);
1552ceaad40aSRandall Stewart 			}
1553ceaad40aSRandall Stewart #endif
1554f8829a4aSRandall Stewart 			sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1555ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1556ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
1557ceaad40aSRandall Stewart #endif
1558f8829a4aSRandall Stewart 		}
1559f8829a4aSRandall Stewart 		/* now is it in the mapping array of what we have accepted? */
156020b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_map) &&
156120b07a4dSMichael Tuexen 		    SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1562f8829a4aSRandall Stewart 			/* Nope not in the valid range dump it */
1563f8829a4aSRandall Stewart 			sctp_set_rwnd(stcb, asoc);
1564f8829a4aSRandall Stewart 			if ((asoc->cnt_on_all_streams +
1565f8829a4aSRandall Stewart 			    asoc->cnt_on_reasm_queue +
1566b3f1ea41SRandall Stewart 			    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) {
1567f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_datadropchklmt);
1568f8829a4aSRandall Stewart 			} else {
1569f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_datadroprwnd);
1570f8829a4aSRandall Stewart 			}
1571f8829a4aSRandall Stewart 			indx = *break_flag;
1572f8829a4aSRandall Stewart 			*break_flag = 1;
1573f8829a4aSRandall Stewart 			return (0);
1574f8829a4aSRandall Stewart 		}
1575f8829a4aSRandall Stewart 	}
1576f8829a4aSRandall Stewart 	strmno = ntohs(ch->dp.stream_id);
1577f8829a4aSRandall Stewart 	if (strmno >= asoc->streamincnt) {
1578f8829a4aSRandall Stewart 		struct sctp_paramhdr *phdr;
1579f8829a4aSRandall Stewart 		struct mbuf *mb;
1580f8829a4aSRandall Stewart 
1581f8829a4aSRandall Stewart 		mb = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) * 2),
1582139bc87fSRandall Stewart 		    0, M_DONTWAIT, 1, MT_DATA);
1583f8829a4aSRandall Stewart 		if (mb != NULL) {
1584f8829a4aSRandall Stewart 			/* add some space up front so prepend will work well */
1585139bc87fSRandall Stewart 			SCTP_BUF_RESV_UF(mb, sizeof(struct sctp_chunkhdr));
1586f8829a4aSRandall Stewart 			phdr = mtod(mb, struct sctp_paramhdr *);
1587f8829a4aSRandall Stewart 			/*
1588f8829a4aSRandall Stewart 			 * Error causes are just param's and this one has
1589f8829a4aSRandall Stewart 			 * two back to back phdr, one with the error type
1590f8829a4aSRandall Stewart 			 * and size, the other with the streamid and a rsvd
1591f8829a4aSRandall Stewart 			 */
1592139bc87fSRandall Stewart 			SCTP_BUF_LEN(mb) = (sizeof(struct sctp_paramhdr) * 2);
1593f8829a4aSRandall Stewart 			phdr->param_type = htons(SCTP_CAUSE_INVALID_STREAM);
1594f8829a4aSRandall Stewart 			phdr->param_length =
1595f8829a4aSRandall Stewart 			    htons(sizeof(struct sctp_paramhdr) * 2);
1596f8829a4aSRandall Stewart 			phdr++;
1597f8829a4aSRandall Stewart 			/* We insert the stream in the type field */
1598f8829a4aSRandall Stewart 			phdr->param_type = ch->dp.stream_id;
1599f8829a4aSRandall Stewart 			/* And set the length to 0 for the rsvd field */
1600f8829a4aSRandall Stewart 			phdr->param_length = 0;
1601f8829a4aSRandall Stewart 			sctp_queue_op_err(stcb, mb);
1602f8829a4aSRandall Stewart 		}
1603f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_badsid);
1604207304d4SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
1605830d754dSRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
160620b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1607830d754dSRandall Stewart 			asoc->highest_tsn_inside_nr_map = tsn;
1608d06c82f1SRandall Stewart 		}
1609d06c82f1SRandall Stewart 		if (tsn == (asoc->cumulative_tsn + 1)) {
1610d06c82f1SRandall Stewart 			/* Update cum-ack */
1611d06c82f1SRandall Stewart 			asoc->cumulative_tsn = tsn;
1612d06c82f1SRandall Stewart 		}
1613f8829a4aSRandall Stewart 		return (0);
1614f8829a4aSRandall Stewart 	}
1615f8829a4aSRandall Stewart 	/*
1616f8829a4aSRandall Stewart 	 * Before we continue lets validate that we are not being fooled by
1617f8829a4aSRandall Stewart 	 * an evil attacker. We can only have 4k chunks based on our TSN
1618f8829a4aSRandall Stewart 	 * spread allowed by the mapping array 512 * 8 bits, so there is no
1619f8829a4aSRandall Stewart 	 * way our stream sequence numbers could have wrapped. We of course
1620f8829a4aSRandall Stewart 	 * only validate the FIRST fragment so the bit must be set.
1621f8829a4aSRandall Stewart 	 */
1622f8829a4aSRandall Stewart 	strmseq = ntohs(ch->dp.stream_sequence);
1623f42a358aSRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
162418e198d3SRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
162518e198d3SRandall Stewart 	if (asoc->tsn_in_at >= SCTP_TSN_LOG_SIZE) {
162618e198d3SRandall Stewart 		asoc->tsn_in_at = 0;
162718e198d3SRandall Stewart 		asoc->tsn_in_wrapped = 1;
162818e198d3SRandall Stewart 	}
1629f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].tsn = tsn;
1630f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].strm = strmno;
1631f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].seq = strmseq;
1632f1f73e57SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].sz = chk_length;
1633f1f73e57SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].flgs = chunk_flags;
163418e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].stcb = (void *)stcb;
163518e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].in_pos = asoc->tsn_in_at;
163618e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].in_out = 1;
1637f42a358aSRandall Stewart 	asoc->tsn_in_at++;
1638f42a358aSRandall Stewart #endif
1639f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_FIRST_FRAG) &&
1640d61a0ae0SRandall Stewart 	    (TAILQ_EMPTY(&asoc->resetHead)) &&
1641f42a358aSRandall Stewart 	    (chunk_flags & SCTP_DATA_UNORDERED) == 0 &&
164220b07a4dSMichael Tuexen 	    SCTP_SSN_GE(asoc->strmin[strmno].last_sequence_delivered, strmseq)) {
1643f8829a4aSRandall Stewart 		/* The incoming sseq is behind where we last delivered? */
1644ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1, "EVIL/Broken-Dup S-SEQ:%d delivered:%d from peer, Abort!\n",
1645ad81507eSRandall Stewart 		    strmseq, asoc->strmin[strmno].last_sequence_delivered);
1646139bc87fSRandall Stewart 		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1647f8829a4aSRandall Stewart 		    0, M_DONTWAIT, 1, MT_DATA);
1648f8829a4aSRandall Stewart 		if (oper) {
1649f8829a4aSRandall Stewart 			struct sctp_paramhdr *ph;
1650f8829a4aSRandall Stewart 			uint32_t *ippp;
1651f8829a4aSRandall Stewart 
1652139bc87fSRandall Stewart 			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
1653f8829a4aSRandall Stewart 			    (3 * sizeof(uint32_t));
1654f8829a4aSRandall Stewart 			ph = mtod(oper, struct sctp_paramhdr *);
1655f8829a4aSRandall Stewart 			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1656139bc87fSRandall Stewart 			ph->param_length = htons(SCTP_BUF_LEN(oper));
1657f8829a4aSRandall Stewart 			ippp = (uint32_t *) (ph + 1);
1658a5d547adSRandall Stewart 			*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_14);
1659f8829a4aSRandall Stewart 			ippp++;
1660f8829a4aSRandall Stewart 			*ippp = tsn;
1661f8829a4aSRandall Stewart 			ippp++;
1662f8829a4aSRandall Stewart 			*ippp = ((strmno << 16) | strmseq);
1663f8829a4aSRandall Stewart 
1664f8829a4aSRandall Stewart 		}
1665a5d547adSRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_14;
1666f8829a4aSRandall Stewart 		sctp_abort_an_association(stcb->sctp_ep, stcb,
1667ceaad40aSRandall Stewart 		    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1668f8829a4aSRandall Stewart 		*abort_flag = 1;
1669f8829a4aSRandall Stewart 		return (0);
1670f8829a4aSRandall Stewart 	}
1671f42a358aSRandall Stewart 	/************************************
1672f42a358aSRandall Stewart 	 * From here down we may find ch-> invalid
1673f42a358aSRandall Stewart 	 * so its a good idea NOT to use it.
1674f42a358aSRandall Stewart 	 *************************************/
1675f42a358aSRandall Stewart 
1676f8829a4aSRandall Stewart 	the_len = (chk_length - sizeof(struct sctp_data_chunk));
1677f8829a4aSRandall Stewart 	if (last_chunk == 0) {
167844b7479bSRandall Stewart 		dmbuf = SCTP_M_COPYM(*m,
1679f8829a4aSRandall Stewart 		    (offset + sizeof(struct sctp_data_chunk)),
1680f8829a4aSRandall Stewart 		    the_len, M_DONTWAIT);
1681f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
1682b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
1683f8829a4aSRandall Stewart 			struct mbuf *mat;
1684f8829a4aSRandall Stewart 
1685f8829a4aSRandall Stewart 			mat = dmbuf;
1686f8829a4aSRandall Stewart 			while (mat) {
1687139bc87fSRandall Stewart 				if (SCTP_BUF_IS_EXTENDED(mat)) {
1688f8829a4aSRandall Stewart 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
1689f8829a4aSRandall Stewart 				}
1690139bc87fSRandall Stewart 				mat = SCTP_BUF_NEXT(mat);
1691f8829a4aSRandall Stewart 			}
1692f8829a4aSRandall Stewart 		}
1693f8829a4aSRandall Stewart #endif
1694f8829a4aSRandall Stewart 	} else {
1695f8829a4aSRandall Stewart 		/* We can steal the last chunk */
1696139bc87fSRandall Stewart 		int l_len;
1697139bc87fSRandall Stewart 
1698f8829a4aSRandall Stewart 		dmbuf = *m;
1699f8829a4aSRandall Stewart 		/* lop off the top part */
1700f8829a4aSRandall Stewart 		m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk)));
1701139bc87fSRandall Stewart 		if (SCTP_BUF_NEXT(dmbuf) == NULL) {
1702139bc87fSRandall Stewart 			l_len = SCTP_BUF_LEN(dmbuf);
1703139bc87fSRandall Stewart 		} else {
1704139bc87fSRandall Stewart 			/*
1705139bc87fSRandall Stewart 			 * need to count up the size hopefully does not hit
1706139bc87fSRandall Stewart 			 * this to often :-0
1707139bc87fSRandall Stewart 			 */
1708139bc87fSRandall Stewart 			struct mbuf *lat;
1709139bc87fSRandall Stewart 
1710139bc87fSRandall Stewart 			l_len = 0;
1711139bc87fSRandall Stewart 			lat = dmbuf;
1712139bc87fSRandall Stewart 			while (lat) {
1713139bc87fSRandall Stewart 				l_len += SCTP_BUF_LEN(lat);
1714139bc87fSRandall Stewart 				lat = SCTP_BUF_NEXT(lat);
1715139bc87fSRandall Stewart 			}
1716139bc87fSRandall Stewart 		}
1717139bc87fSRandall Stewart 		if (l_len > the_len) {
1718f8829a4aSRandall Stewart 			/* Trim the end round bytes off  too */
1719139bc87fSRandall Stewart 			m_adj(dmbuf, -(l_len - the_len));
1720f8829a4aSRandall Stewart 		}
1721f8829a4aSRandall Stewart 	}
1722f8829a4aSRandall Stewart 	if (dmbuf == NULL) {
1723f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_nomem);
1724f8829a4aSRandall Stewart 		return (0);
1725f8829a4aSRandall Stewart 	}
1726f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG &&
1727f8829a4aSRandall Stewart 	    asoc->fragmented_delivery_inprogress == 0 &&
1728f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->resetHead) &&
1729f42a358aSRandall Stewart 	    ((ordered == 0) ||
173036ec9f81SMichael Tuexen 	    ((uint16_t) (asoc->strmin[strmno].last_sequence_delivered + 1) == strmseq &&
1731f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->strmin[strmno].inqueue)))) {
1732f8829a4aSRandall Stewart 		/* Candidate for express delivery */
1733f8829a4aSRandall Stewart 		/*
1734f8829a4aSRandall Stewart 		 * Its not fragmented, No PD-API is up, Nothing in the
1735f8829a4aSRandall Stewart 		 * delivery queue, Its un-ordered OR ordered and the next to
1736f8829a4aSRandall Stewart 		 * deliver AND nothing else is stuck on the stream queue,
1737f8829a4aSRandall Stewart 		 * And there is room for it in the socket buffer. Lets just
1738f8829a4aSRandall Stewart 		 * stuff it up the buffer....
1739f8829a4aSRandall Stewart 		 */
1740f8829a4aSRandall Stewart 
1741f8829a4aSRandall Stewart 		/* It would be nice to avoid this copy if we could :< */
1742f8829a4aSRandall Stewart 		sctp_alloc_a_readq(stcb, control);
1743f8829a4aSRandall Stewart 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1744f42a358aSRandall Stewart 		    protocol_id,
1745f8829a4aSRandall Stewart 		    stcb->asoc.context,
1746f8829a4aSRandall Stewart 		    strmno, strmseq,
1747f42a358aSRandall Stewart 		    chunk_flags,
1748f8829a4aSRandall Stewart 		    dmbuf);
1749f8829a4aSRandall Stewart 		if (control == NULL) {
1750f8829a4aSRandall Stewart 			goto failed_express_del;
1751f8829a4aSRandall Stewart 		}
17521ea735c8SMichael Tuexen 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
175320b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
17541ea735c8SMichael Tuexen 			asoc->highest_tsn_inside_nr_map = tsn;
17551ea735c8SMichael Tuexen 		}
1756cfde3ff7SRandall Stewart 		sctp_add_to_readq(stcb->sctp_ep, stcb,
1757cfde3ff7SRandall Stewart 		    control, &stcb->sctp_socket->so_rcv,
1758cfde3ff7SRandall Stewart 		    1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
1759830d754dSRandall Stewart 
1760f42a358aSRandall Stewart 		if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1761f8829a4aSRandall Stewart 			/* for ordered, bump what we delivered */
1762f8829a4aSRandall Stewart 			asoc->strmin[strmno].last_sequence_delivered++;
1763f8829a4aSRandall Stewart 		}
1764f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvexpress);
1765b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
17666a91f103SRandall Stewart 			sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno,
1767f8829a4aSRandall Stewart 			    SCTP_STR_LOG_FROM_EXPRS_DEL);
176880fefe0aSRandall Stewart 		}
1769f8829a4aSRandall Stewart 		control = NULL;
1770b5c16493SMichael Tuexen 
1771f8829a4aSRandall Stewart 		goto finish_express_del;
1772f8829a4aSRandall Stewart 	}
1773f8829a4aSRandall Stewart failed_express_del:
1774f8829a4aSRandall Stewart 	/* If we reach here this is a new chunk */
1775f8829a4aSRandall Stewart 	chk = NULL;
1776f8829a4aSRandall Stewart 	control = NULL;
1777f8829a4aSRandall Stewart 	/* Express for fragmented delivery? */
1778f8829a4aSRandall Stewart 	if ((asoc->fragmented_delivery_inprogress) &&
1779f8829a4aSRandall Stewart 	    (stcb->asoc.control_pdapi) &&
1780f8829a4aSRandall Stewart 	    (asoc->str_of_pdapi == strmno) &&
1781f8829a4aSRandall Stewart 	    (asoc->ssn_of_pdapi == strmseq)
1782f8829a4aSRandall Stewart 	    ) {
1783f8829a4aSRandall Stewart 		control = stcb->asoc.control_pdapi;
1784f42a358aSRandall Stewart 		if ((chunk_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
1785f8829a4aSRandall Stewart 			/* Can't be another first? */
1786f8829a4aSRandall Stewart 			goto failed_pdapi_express_del;
1787f8829a4aSRandall Stewart 		}
1788f8829a4aSRandall Stewart 		if (tsn == (control->sinfo_tsn + 1)) {
1789f8829a4aSRandall Stewart 			/* Yep, we can add it on */
1790f8829a4aSRandall Stewart 			int end = 0;
1791f8829a4aSRandall Stewart 			uint32_t cumack;
1792f8829a4aSRandall Stewart 
1793f42a358aSRandall Stewart 			if (chunk_flags & SCTP_DATA_LAST_FRAG) {
1794f8829a4aSRandall Stewart 				end = 1;
1795f8829a4aSRandall Stewart 			}
1796f8829a4aSRandall Stewart 			cumack = asoc->cumulative_tsn;
1797f8829a4aSRandall Stewart 			if ((cumack + 1) == tsn)
1798f8829a4aSRandall Stewart 				cumack = tsn;
1799f8829a4aSRandall Stewart 
1800f8829a4aSRandall Stewart 			if (sctp_append_to_readq(stcb->sctp_ep, stcb, control, dmbuf, end,
1801f8829a4aSRandall Stewart 			    tsn,
1802f8829a4aSRandall Stewart 			    &stcb->sctp_socket->so_rcv)) {
1803ad81507eSRandall Stewart 				SCTP_PRINTF("Append fails end:%d\n", end);
1804f8829a4aSRandall Stewart 				goto failed_pdapi_express_del;
1805f8829a4aSRandall Stewart 			}
180677acdc25SRandall Stewart 			SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
180720b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1808830d754dSRandall Stewart 				asoc->highest_tsn_inside_nr_map = tsn;
1809830d754dSRandall Stewart 			}
1810f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvexpressm);
1811f8829a4aSRandall Stewart 			control->sinfo_tsn = tsn;
1812f8829a4aSRandall Stewart 			asoc->tsn_last_delivered = tsn;
1813f42a358aSRandall Stewart 			asoc->fragment_flags = chunk_flags;
1814f8829a4aSRandall Stewart 			asoc->tsn_of_pdapi_last_delivered = tsn;
1815f42a358aSRandall Stewart 			asoc->last_flags_delivered = chunk_flags;
1816f8829a4aSRandall Stewart 			asoc->last_strm_seq_delivered = strmseq;
1817f8829a4aSRandall Stewart 			asoc->last_strm_no_delivered = strmno;
1818f8829a4aSRandall Stewart 			if (end) {
1819f8829a4aSRandall Stewart 				/* clean up the flags and such */
1820f8829a4aSRandall Stewart 				asoc->fragmented_delivery_inprogress = 0;
1821f42a358aSRandall Stewart 				if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1822f8829a4aSRandall Stewart 					asoc->strmin[strmno].last_sequence_delivered++;
1823a5d547adSRandall Stewart 				}
1824f8829a4aSRandall Stewart 				stcb->asoc.control_pdapi = NULL;
1825139bc87fSRandall Stewart 				if (TAILQ_EMPTY(&asoc->reasmqueue) == 0) {
1826139bc87fSRandall Stewart 					/*
1827139bc87fSRandall Stewart 					 * There could be another message
1828139bc87fSRandall Stewart 					 * ready
1829139bc87fSRandall Stewart 					 */
1830139bc87fSRandall Stewart 					need_reasm_check = 1;
1831139bc87fSRandall Stewart 				}
1832f8829a4aSRandall Stewart 			}
1833f8829a4aSRandall Stewart 			control = NULL;
1834f8829a4aSRandall Stewart 			goto finish_express_del;
1835f8829a4aSRandall Stewart 		}
1836f8829a4aSRandall Stewart 	}
1837f8829a4aSRandall Stewart failed_pdapi_express_del:
1838f8829a4aSRandall Stewart 	control = NULL;
183977acdc25SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
184077acdc25SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
184120b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
184277acdc25SRandall Stewart 			asoc->highest_tsn_inside_nr_map = tsn;
184377acdc25SRandall Stewart 		}
184477acdc25SRandall Stewart 	} else {
184577acdc25SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
184620b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_map)) {
184777acdc25SRandall Stewart 			asoc->highest_tsn_inside_map = tsn;
184877acdc25SRandall Stewart 		}
184977acdc25SRandall Stewart 	}
1850f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
1851f8829a4aSRandall Stewart 		sctp_alloc_a_chunk(stcb, chk);
1852f8829a4aSRandall Stewart 		if (chk == NULL) {
1853f8829a4aSRandall Stewart 			/* No memory so we drop the chunk */
1854f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_nomem);
1855f8829a4aSRandall Stewart 			if (last_chunk == 0) {
1856f8829a4aSRandall Stewart 				/* we copied it, free the copy */
1857f8829a4aSRandall Stewart 				sctp_m_freem(dmbuf);
1858f8829a4aSRandall Stewart 			}
1859f8829a4aSRandall Stewart 			return (0);
1860f8829a4aSRandall Stewart 		}
1861f8829a4aSRandall Stewart 		chk->rec.data.TSN_seq = tsn;
1862f8829a4aSRandall Stewart 		chk->no_fr_allowed = 0;
1863f8829a4aSRandall Stewart 		chk->rec.data.stream_seq = strmseq;
1864f8829a4aSRandall Stewart 		chk->rec.data.stream_number = strmno;
1865f42a358aSRandall Stewart 		chk->rec.data.payloadtype = protocol_id;
1866f8829a4aSRandall Stewart 		chk->rec.data.context = stcb->asoc.context;
1867f8829a4aSRandall Stewart 		chk->rec.data.doing_fast_retransmit = 0;
1868f42a358aSRandall Stewart 		chk->rec.data.rcv_flags = chunk_flags;
1869f8829a4aSRandall Stewart 		chk->asoc = asoc;
1870f8829a4aSRandall Stewart 		chk->send_size = the_len;
1871f8829a4aSRandall Stewart 		chk->whoTo = net;
1872f8829a4aSRandall Stewart 		atomic_add_int(&net->ref_count, 1);
1873f8829a4aSRandall Stewart 		chk->data = dmbuf;
1874f8829a4aSRandall Stewart 	} else {
1875f8829a4aSRandall Stewart 		sctp_alloc_a_readq(stcb, control);
1876f8829a4aSRandall Stewart 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1877f42a358aSRandall Stewart 		    protocol_id,
1878f8829a4aSRandall Stewart 		    stcb->asoc.context,
1879f8829a4aSRandall Stewart 		    strmno, strmseq,
1880f42a358aSRandall Stewart 		    chunk_flags,
1881f8829a4aSRandall Stewart 		    dmbuf);
1882f8829a4aSRandall Stewart 		if (control == NULL) {
1883f8829a4aSRandall Stewart 			/* No memory so we drop the chunk */
1884f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_nomem);
1885f8829a4aSRandall Stewart 			if (last_chunk == 0) {
1886f8829a4aSRandall Stewart 				/* we copied it, free the copy */
1887f8829a4aSRandall Stewart 				sctp_m_freem(dmbuf);
1888f8829a4aSRandall Stewart 			}
1889f8829a4aSRandall Stewart 			return (0);
1890f8829a4aSRandall Stewart 		}
1891f8829a4aSRandall Stewart 		control->length = the_len;
1892f8829a4aSRandall Stewart 	}
1893f8829a4aSRandall Stewart 
1894f8829a4aSRandall Stewart 	/* Mark it as received */
1895f8829a4aSRandall Stewart 	/* Now queue it where it belongs */
1896f8829a4aSRandall Stewart 	if (control != NULL) {
1897f8829a4aSRandall Stewart 		/* First a sanity check */
1898f8829a4aSRandall Stewart 		if (asoc->fragmented_delivery_inprogress) {
1899f8829a4aSRandall Stewart 			/*
1900f8829a4aSRandall Stewart 			 * Ok, we have a fragmented delivery in progress if
1901f8829a4aSRandall Stewart 			 * this chunk is next to deliver OR belongs in our
1902f8829a4aSRandall Stewart 			 * view to the reassembly, the peer is evil or
1903f8829a4aSRandall Stewart 			 * broken.
1904f8829a4aSRandall Stewart 			 */
1905f8829a4aSRandall Stewart 			uint32_t estimate_tsn;
1906f8829a4aSRandall Stewart 
1907f8829a4aSRandall Stewart 			estimate_tsn = asoc->tsn_last_delivered + 1;
1908f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->reasmqueue) &&
1909f8829a4aSRandall Stewart 			    (estimate_tsn == control->sinfo_tsn)) {
1910f8829a4aSRandall Stewart 				/* Evil/Broke peer */
1911f8829a4aSRandall Stewart 				sctp_m_freem(control->data);
1912f8829a4aSRandall Stewart 				control->data = NULL;
19135bead436SRandall Stewart 				if (control->whoFrom) {
1914f8829a4aSRandall Stewart 					sctp_free_remote_addr(control->whoFrom);
19155bead436SRandall Stewart 					control->whoFrom = NULL;
19165bead436SRandall Stewart 				}
1917f8829a4aSRandall Stewart 				sctp_free_a_readq(stcb, control);
1918139bc87fSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1919f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
1920f8829a4aSRandall Stewart 				if (oper) {
1921f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
1922f8829a4aSRandall Stewart 					uint32_t *ippp;
1923f8829a4aSRandall Stewart 
1924139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) =
1925f8829a4aSRandall Stewart 					    sizeof(struct sctp_paramhdr) +
1926f8829a4aSRandall Stewart 					    (3 * sizeof(uint32_t));
1927f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
1928f8829a4aSRandall Stewart 					ph->param_type =
1929f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1930139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
1931f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
1932a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_15);
1933f8829a4aSRandall Stewart 					ippp++;
1934f8829a4aSRandall Stewart 					*ippp = tsn;
1935f8829a4aSRandall Stewart 					ippp++;
1936f8829a4aSRandall Stewart 					*ippp = ((strmno << 16) | strmseq);
1937f8829a4aSRandall Stewart 				}
1938a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_15;
1939f8829a4aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb,
1940ceaad40aSRandall Stewart 				    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1941f8829a4aSRandall Stewart 
1942f8829a4aSRandall Stewart 				*abort_flag = 1;
1943f8829a4aSRandall Stewart 				return (0);
1944f8829a4aSRandall Stewart 			} else {
1945f8829a4aSRandall Stewart 				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
1946f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
1947f8829a4aSRandall Stewart 					control->data = NULL;
19485bead436SRandall Stewart 					if (control->whoFrom) {
1949f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
19505bead436SRandall Stewart 						control->whoFrom = NULL;
19515bead436SRandall Stewart 					}
1952f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
1953f8829a4aSRandall Stewart 
1954139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1955f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1956f8829a4aSRandall Stewart 					if (oper) {
1957f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1958f8829a4aSRandall Stewart 						uint32_t *ippp;
1959f8829a4aSRandall Stewart 
1960139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1961f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1962f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1963f8829a4aSRandall Stewart 						ph = mtod(oper,
1964f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1965f8829a4aSRandall Stewart 						ph->param_type =
1966f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1967f8829a4aSRandall Stewart 						ph->param_length =
1968139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1969f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1970a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_16);
1971f8829a4aSRandall Stewart 						ippp++;
1972f8829a4aSRandall Stewart 						*ippp = tsn;
1973f8829a4aSRandall Stewart 						ippp++;
1974f8829a4aSRandall Stewart 						*ippp = ((strmno << 16) | strmseq);
1975f8829a4aSRandall Stewart 					}
1976a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_16;
1977f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
1978ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1979f8829a4aSRandall Stewart 
1980f8829a4aSRandall Stewart 					*abort_flag = 1;
1981f8829a4aSRandall Stewart 					return (0);
1982f8829a4aSRandall Stewart 				}
1983f8829a4aSRandall Stewart 			}
1984f8829a4aSRandall Stewart 		} else {
1985f8829a4aSRandall Stewart 			/* No PDAPI running */
1986f8829a4aSRandall Stewart 			if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
1987f8829a4aSRandall Stewart 				/*
1988f8829a4aSRandall Stewart 				 * Reassembly queue is NOT empty validate
1989f8829a4aSRandall Stewart 				 * that this tsn does not need to be in
1990f8829a4aSRandall Stewart 				 * reasembly queue. If it does then our peer
1991f8829a4aSRandall Stewart 				 * is broken or evil.
1992f8829a4aSRandall Stewart 				 */
1993f8829a4aSRandall Stewart 				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
1994f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
1995f8829a4aSRandall Stewart 					control->data = NULL;
19965bead436SRandall Stewart 					if (control->whoFrom) {
1997f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
19985bead436SRandall Stewart 						control->whoFrom = NULL;
19995bead436SRandall Stewart 					}
2000f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
2001139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
2002f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
2003f8829a4aSRandall Stewart 					if (oper) {
2004f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
2005f8829a4aSRandall Stewart 						uint32_t *ippp;
2006f8829a4aSRandall Stewart 
2007139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
2008f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
2009f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
2010f8829a4aSRandall Stewart 						ph = mtod(oper,
2011f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
2012f8829a4aSRandall Stewart 						ph->param_type =
2013f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2014f8829a4aSRandall Stewart 						ph->param_length =
2015139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
2016f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
2017a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_17);
2018f8829a4aSRandall Stewart 						ippp++;
2019f8829a4aSRandall Stewart 						*ippp = tsn;
2020f8829a4aSRandall Stewart 						ippp++;
2021f8829a4aSRandall Stewart 						*ippp = ((strmno << 16) | strmseq);
2022f8829a4aSRandall Stewart 					}
2023a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_17;
2024f8829a4aSRandall Stewart 					sctp_abort_an_association(stcb->sctp_ep,
2025ceaad40aSRandall Stewart 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
2026f8829a4aSRandall Stewart 
2027f8829a4aSRandall Stewart 					*abort_flag = 1;
2028f8829a4aSRandall Stewart 					return (0);
2029f8829a4aSRandall Stewart 				}
2030f8829a4aSRandall Stewart 			}
2031f8829a4aSRandall Stewart 		}
2032f8829a4aSRandall Stewart 		/* ok, if we reach here we have passed the sanity checks */
2033f42a358aSRandall Stewart 		if (chunk_flags & SCTP_DATA_UNORDERED) {
2034f8829a4aSRandall Stewart 			/* queue directly into socket buffer */
2035b5c16493SMichael Tuexen 			sctp_mark_non_revokable(asoc, control->sinfo_tsn);
2036f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep, stcb,
2037f8829a4aSRandall Stewart 			    control,
2038cfde3ff7SRandall Stewart 			    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
2039f8829a4aSRandall Stewart 		} else {
2040f8829a4aSRandall Stewart 			/*
2041f8829a4aSRandall Stewart 			 * Special check for when streams are resetting. We
2042f8829a4aSRandall Stewart 			 * could be more smart about this and check the
2043f8829a4aSRandall Stewart 			 * actual stream to see if it is not being reset..
2044f8829a4aSRandall Stewart 			 * that way we would not create a HOLB when amongst
2045f8829a4aSRandall Stewart 			 * streams being reset and those not being reset.
2046f8829a4aSRandall Stewart 			 *
2047f8829a4aSRandall Stewart 			 * We take complete messages that have a stream reset
2048f8829a4aSRandall Stewart 			 * intervening (aka the TSN is after where our
2049f8829a4aSRandall Stewart 			 * cum-ack needs to be) off and put them on a
2050f8829a4aSRandall Stewart 			 * pending_reply_queue. The reassembly ones we do
2051f8829a4aSRandall Stewart 			 * not have to worry about since they are all sorted
2052f8829a4aSRandall Stewart 			 * and proceessed by TSN order. It is only the
2053f8829a4aSRandall Stewart 			 * singletons I must worry about.
2054f8829a4aSRandall Stewart 			 */
2055f8829a4aSRandall Stewart 			if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
205620b07a4dSMichael Tuexen 			    SCTP_TSN_GT(tsn, liste->tsn)) {
2057f8829a4aSRandall Stewart 				/*
2058f8829a4aSRandall Stewart 				 * yep its past where we need to reset... go
2059f8829a4aSRandall Stewart 				 * ahead and queue it.
2060f8829a4aSRandall Stewart 				 */
2061f8829a4aSRandall Stewart 				if (TAILQ_EMPTY(&asoc->pending_reply_queue)) {
2062f8829a4aSRandall Stewart 					/* first one on */
2063f8829a4aSRandall Stewart 					TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2064f8829a4aSRandall Stewart 				} else {
20654a9ef3f8SMichael Tuexen 					struct sctp_queued_to_read *ctlOn,
20664a9ef3f8SMichael Tuexen 					                   *nctlOn;
2067f8829a4aSRandall Stewart 					unsigned char inserted = 0;
2068f8829a4aSRandall Stewart 
20694a9ef3f8SMichael Tuexen 					TAILQ_FOREACH_SAFE(ctlOn, &asoc->pending_reply_queue, next, nctlOn) {
207020b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(control->sinfo_tsn, ctlOn->sinfo_tsn)) {
20714a9ef3f8SMichael Tuexen 							continue;
2072f8829a4aSRandall Stewart 						} else {
2073f8829a4aSRandall Stewart 							/* found it */
2074f8829a4aSRandall Stewart 							TAILQ_INSERT_BEFORE(ctlOn, control, next);
2075f8829a4aSRandall Stewart 							inserted = 1;
2076f8829a4aSRandall Stewart 							break;
2077f8829a4aSRandall Stewart 						}
2078f8829a4aSRandall Stewart 					}
2079f8829a4aSRandall Stewart 					if (inserted == 0) {
2080f8829a4aSRandall Stewart 						/*
2081f8829a4aSRandall Stewart 						 * must be put at end, use
2082f8829a4aSRandall Stewart 						 * prevP (all setup from
2083f8829a4aSRandall Stewart 						 * loop) to setup nextP.
2084f8829a4aSRandall Stewart 						 */
2085f8829a4aSRandall Stewart 						TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2086f8829a4aSRandall Stewart 					}
2087f8829a4aSRandall Stewart 				}
2088f8829a4aSRandall Stewart 			} else {
2089f8829a4aSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, control, abort_flag);
2090f8829a4aSRandall Stewart 				if (*abort_flag) {
2091f8829a4aSRandall Stewart 					return (0);
2092f8829a4aSRandall Stewart 				}
2093f8829a4aSRandall Stewart 			}
2094f8829a4aSRandall Stewart 		}
2095f8829a4aSRandall Stewart 	} else {
2096f8829a4aSRandall Stewart 		/* Into the re-assembly queue */
2097f8829a4aSRandall Stewart 		sctp_queue_data_for_reasm(stcb, asoc, chk, abort_flag);
2098f8829a4aSRandall Stewart 		if (*abort_flag) {
2099a5d547adSRandall Stewart 			/*
2100a5d547adSRandall Stewart 			 * the assoc is now gone and chk was put onto the
2101a5d547adSRandall Stewart 			 * reasm queue, which has all been freed.
2102a5d547adSRandall Stewart 			 */
2103a5d547adSRandall Stewart 			*m = NULL;
2104f8829a4aSRandall Stewart 			return (0);
2105f8829a4aSRandall Stewart 		}
2106f8829a4aSRandall Stewart 	}
2107f8829a4aSRandall Stewart finish_express_del:
2108307b49efSMichael Tuexen 	if (tsn == (asoc->cumulative_tsn + 1)) {
2109307b49efSMichael Tuexen 		/* Update cum-ack */
2110307b49efSMichael Tuexen 		asoc->cumulative_tsn = tsn;
2111307b49efSMichael Tuexen 	}
2112f8829a4aSRandall Stewart 	if (last_chunk) {
2113f8829a4aSRandall Stewart 		*m = NULL;
2114f8829a4aSRandall Stewart 	}
2115f42a358aSRandall Stewart 	if (ordered) {
2116f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_inorderchunks);
2117f8829a4aSRandall Stewart 	} else {
2118f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_inunorderchunks);
2119f8829a4aSRandall Stewart 	}
2120f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvdata);
2121f8829a4aSRandall Stewart 	/* Set it present please */
2122b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
21236a91f103SRandall Stewart 		sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno, SCTP_STR_LOG_FROM_MARK_TSN);
212480fefe0aSRandall Stewart 	}
2125b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2126f8829a4aSRandall Stewart 		sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2127f8829a4aSRandall Stewart 		    asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE);
212880fefe0aSRandall Stewart 	}
212917205eccSRandall Stewart 	/* check the special flag for stream resets */
213017205eccSRandall Stewart 	if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
213120b07a4dSMichael Tuexen 	    SCTP_TSN_GE(asoc->cumulative_tsn, liste->tsn)) {
213217205eccSRandall Stewart 		/*
213317205eccSRandall Stewart 		 * we have finished working through the backlogged TSN's now
213417205eccSRandall Stewart 		 * time to reset streams. 1: call reset function. 2: free
213517205eccSRandall Stewart 		 * pending_reply space 3: distribute any chunks in
213617205eccSRandall Stewart 		 * pending_reply_queue.
213717205eccSRandall Stewart 		 */
21384a9ef3f8SMichael Tuexen 		struct sctp_queued_to_read *ctl, *nctl;
213917205eccSRandall Stewart 
214017205eccSRandall Stewart 		sctp_reset_in_stream(stcb, liste->number_entries, liste->req.list_of_streams);
214117205eccSRandall Stewart 		TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
2142207304d4SRandall Stewart 		SCTP_FREE(liste, SCTP_M_STRESET);
21433c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
214417205eccSRandall Stewart 		liste = TAILQ_FIRST(&asoc->resetHead);
21454a9ef3f8SMichael Tuexen 		if (TAILQ_EMPTY(&asoc->resetHead)) {
214617205eccSRandall Stewart 			/* All can be removed */
21474a9ef3f8SMichael Tuexen 			TAILQ_FOREACH_SAFE(ctl, &asoc->pending_reply_queue, next, nctl) {
214817205eccSRandall Stewart 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
214917205eccSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
215017205eccSRandall Stewart 				if (*abort_flag) {
215117205eccSRandall Stewart 					return (0);
215217205eccSRandall Stewart 				}
215317205eccSRandall Stewart 			}
21544a9ef3f8SMichael Tuexen 		} else {
21554a9ef3f8SMichael Tuexen 			TAILQ_FOREACH_SAFE(ctl, &asoc->pending_reply_queue, next, nctl) {
215620b07a4dSMichael Tuexen 				if (SCTP_TSN_GT(ctl->sinfo_tsn, liste->tsn)) {
21574a9ef3f8SMichael Tuexen 					break;
21584a9ef3f8SMichael Tuexen 				}
215917205eccSRandall Stewart 				/*
216017205eccSRandall Stewart 				 * if ctl->sinfo_tsn is <= liste->tsn we can
216117205eccSRandall Stewart 				 * process it which is the NOT of
216217205eccSRandall Stewart 				 * ctl->sinfo_tsn > liste->tsn
216317205eccSRandall Stewart 				 */
216417205eccSRandall Stewart 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
216517205eccSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
216617205eccSRandall Stewart 				if (*abort_flag) {
216717205eccSRandall Stewart 					return (0);
216817205eccSRandall Stewart 				}
216917205eccSRandall Stewart 			}
217017205eccSRandall Stewart 		}
217117205eccSRandall Stewart 		/*
217217205eccSRandall Stewart 		 * Now service re-assembly to pick up anything that has been
217317205eccSRandall Stewart 		 * held on reassembly queue?
217417205eccSRandall Stewart 		 */
217517205eccSRandall Stewart 		sctp_deliver_reasm_check(stcb, asoc);
217617205eccSRandall Stewart 		need_reasm_check = 0;
217717205eccSRandall Stewart 	}
2178139bc87fSRandall Stewart 	if (need_reasm_check) {
2179139bc87fSRandall Stewart 		/* Another one waits ? */
2180139bc87fSRandall Stewart 		sctp_deliver_reasm_check(stcb, asoc);
2181139bc87fSRandall Stewart 	}
2182f8829a4aSRandall Stewart 	return (1);
2183f8829a4aSRandall Stewart }
2184f8829a4aSRandall Stewart 
2185f8829a4aSRandall Stewart int8_t sctp_map_lookup_tab[256] = {
2186b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2187b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2188b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2189b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
2190b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2191b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2192b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2193b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 6,
2194b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2195b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2196b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2197b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
2198b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2199b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2200b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2201b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 7,
2202b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2203b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2204b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2205b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
2206b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2207b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2208b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2209b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 6,
2210b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2211b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2212b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2213b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
2214b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2215b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2216b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2217b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 8
2218f8829a4aSRandall Stewart };
2219f8829a4aSRandall Stewart 
2220f8829a4aSRandall Stewart 
2221f8829a4aSRandall Stewart void
2222b5c16493SMichael Tuexen sctp_slide_mapping_arrays(struct sctp_tcb *stcb)
2223f8829a4aSRandall Stewart {
2224f8829a4aSRandall Stewart 	/*
2225f8829a4aSRandall Stewart 	 * Now we also need to check the mapping array in a couple of ways.
2226f8829a4aSRandall Stewart 	 * 1) Did we move the cum-ack point?
222766bd30bdSRandall Stewart 	 *
222866bd30bdSRandall Stewart 	 * When you first glance at this you might think that all entries that
222966bd30bdSRandall Stewart 	 * make up the postion of the cum-ack would be in the nr-mapping
223066bd30bdSRandall Stewart 	 * array only.. i.e. things up to the cum-ack are always
223166bd30bdSRandall Stewart 	 * deliverable. Thats true with one exception, when its a fragmented
223266bd30bdSRandall Stewart 	 * message we may not deliver the data until some threshold (or all
223366bd30bdSRandall Stewart 	 * of it) is in place. So we must OR the nr_mapping_array and
223466bd30bdSRandall Stewart 	 * mapping_array to get a true picture of the cum-ack.
2235f8829a4aSRandall Stewart 	 */
2236f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2237b3f1ea41SRandall Stewart 	int at;
223866bd30bdSRandall Stewart 	uint8_t val;
2239f8829a4aSRandall Stewart 	int slide_from, slide_end, lgap, distance;
224077acdc25SRandall Stewart 	uint32_t old_cumack, old_base, old_highest, highest_tsn;
2241f8829a4aSRandall Stewart 
2242f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2243f8829a4aSRandall Stewart 	at = 0;
2244f8829a4aSRandall Stewart 
2245f8829a4aSRandall Stewart 	old_cumack = asoc->cumulative_tsn;
2246f8829a4aSRandall Stewart 	old_base = asoc->mapping_array_base_tsn;
2247f8829a4aSRandall Stewart 	old_highest = asoc->highest_tsn_inside_map;
2248f8829a4aSRandall Stewart 	/*
2249f8829a4aSRandall Stewart 	 * We could probably improve this a small bit by calculating the
2250f8829a4aSRandall Stewart 	 * offset of the current cum-ack as the starting point.
2251f8829a4aSRandall Stewart 	 */
2252f8829a4aSRandall Stewart 	at = 0;
2253b5c16493SMichael Tuexen 	for (slide_from = 0; slide_from < stcb->asoc.mapping_array_size; slide_from++) {
225466bd30bdSRandall Stewart 		val = asoc->nr_mapping_array[slide_from] | asoc->mapping_array[slide_from];
225566bd30bdSRandall Stewart 		if (val == 0xff) {
2256f8829a4aSRandall Stewart 			at += 8;
2257f8829a4aSRandall Stewart 		} else {
2258f8829a4aSRandall Stewart 			/* there is a 0 bit */
225966bd30bdSRandall Stewart 			at += sctp_map_lookup_tab[val];
2260f8829a4aSRandall Stewart 			break;
2261f8829a4aSRandall Stewart 		}
2262f8829a4aSRandall Stewart 	}
2263b5c16493SMichael Tuexen 	asoc->cumulative_tsn = asoc->mapping_array_base_tsn + (at - 1);
2264f8829a4aSRandall Stewart 
226520b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->cumulative_tsn, asoc->highest_tsn_inside_map) &&
226620b07a4dSMichael Tuexen 	    SCTP_TSN_GT(asoc->cumulative_tsn, asoc->highest_tsn_inside_nr_map)) {
2267a5d547adSRandall Stewart #ifdef INVARIANTS
2268ceaad40aSRandall Stewart 		panic("huh, cumack 0x%x greater than high-tsn 0x%x in map",
2269ceaad40aSRandall Stewart 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2270f8829a4aSRandall Stewart #else
2271ceaad40aSRandall Stewart 		SCTP_PRINTF("huh, cumack 0x%x greater than high-tsn 0x%x in map - should panic?\n",
2272ceaad40aSRandall Stewart 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
22730e13104dSRandall Stewart 		sctp_print_mapping_array(asoc);
2274b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2275b3f1ea41SRandall Stewart 			sctp_log_map(0, 6, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
2276b3f1ea41SRandall Stewart 		}
2277f8829a4aSRandall Stewart 		asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2278830d754dSRandall Stewart 		asoc->highest_tsn_inside_nr_map = asoc->cumulative_tsn;
2279f8829a4aSRandall Stewart #endif
2280f8829a4aSRandall Stewart 	}
228120b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
228277acdc25SRandall Stewart 		highest_tsn = asoc->highest_tsn_inside_nr_map;
228377acdc25SRandall Stewart 	} else {
228477acdc25SRandall Stewart 		highest_tsn = asoc->highest_tsn_inside_map;
228577acdc25SRandall Stewart 	}
228677acdc25SRandall Stewart 	if ((asoc->cumulative_tsn == highest_tsn) && (at >= 8)) {
2287f8829a4aSRandall Stewart 		/* The complete array was completed by a single FR */
228877acdc25SRandall Stewart 		/* highest becomes the cum-ack */
228937f144ebSMichael Tuexen 		int clr;
229037f144ebSMichael Tuexen 
229137f144ebSMichael Tuexen #ifdef INVARIANTS
229237f144ebSMichael Tuexen 		unsigned int i;
229337f144ebSMichael Tuexen 
229437f144ebSMichael Tuexen #endif
2295f8829a4aSRandall Stewart 
2296f8829a4aSRandall Stewart 		/* clear the array */
2297b5c16493SMichael Tuexen 		clr = ((at + 7) >> 3);
2298c4739e2fSRandall Stewart 		if (clr > asoc->mapping_array_size) {
2299f8829a4aSRandall Stewart 			clr = asoc->mapping_array_size;
2300f8829a4aSRandall Stewart 		}
2301f8829a4aSRandall Stewart 		memset(asoc->mapping_array, 0, clr);
2302830d754dSRandall Stewart 		memset(asoc->nr_mapping_array, 0, clr);
230337f144ebSMichael Tuexen #ifdef INVARIANTS
2304b5c16493SMichael Tuexen 		for (i = 0; i < asoc->mapping_array_size; i++) {
2305b5c16493SMichael Tuexen 			if ((asoc->mapping_array[i]) || (asoc->nr_mapping_array[i])) {
2306b5c16493SMichael Tuexen 				printf("Error Mapping array's not clean at clear\n");
2307b5c16493SMichael Tuexen 				sctp_print_mapping_array(asoc);
2308b5c16493SMichael Tuexen 			}
2309b5c16493SMichael Tuexen 		}
231037f144ebSMichael Tuexen #endif
231177acdc25SRandall Stewart 		asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
231277acdc25SRandall Stewart 		asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2313f8829a4aSRandall Stewart 	} else if (at >= 8) {
2314f8829a4aSRandall Stewart 		/* we can slide the mapping array down */
2315b3f1ea41SRandall Stewart 		/* slide_from holds where we hit the first NON 0xff byte */
2316b3f1ea41SRandall Stewart 
2317f8829a4aSRandall Stewart 		/*
2318f8829a4aSRandall Stewart 		 * now calculate the ceiling of the move using our highest
2319f8829a4aSRandall Stewart 		 * TSN value
2320f8829a4aSRandall Stewart 		 */
232177acdc25SRandall Stewart 		SCTP_CALC_TSN_TO_GAP(lgap, highest_tsn, asoc->mapping_array_base_tsn);
232277acdc25SRandall Stewart 		slide_end = (lgap >> 3);
2323f8829a4aSRandall Stewart 		if (slide_end < slide_from) {
232477acdc25SRandall Stewart 			sctp_print_mapping_array(asoc);
2325d55b0b1bSRandall Stewart #ifdef INVARIANTS
2326f8829a4aSRandall Stewart 			panic("impossible slide");
2327d55b0b1bSRandall Stewart #else
232877acdc25SRandall Stewart 			printf("impossible slide lgap:%x slide_end:%x slide_from:%x? at:%d\n",
232977acdc25SRandall Stewart 			    lgap, slide_end, slide_from, at);
2330d55b0b1bSRandall Stewart 			return;
2331d55b0b1bSRandall Stewart #endif
2332f8829a4aSRandall Stewart 		}
2333b3f1ea41SRandall Stewart 		if (slide_end > asoc->mapping_array_size) {
2334b3f1ea41SRandall Stewart #ifdef INVARIANTS
2335b3f1ea41SRandall Stewart 			panic("would overrun buffer");
2336b3f1ea41SRandall Stewart #else
2337b3f1ea41SRandall Stewart 			printf("Gak, would have overrun map end:%d slide_end:%d\n",
2338b3f1ea41SRandall Stewart 			    asoc->mapping_array_size, slide_end);
2339b3f1ea41SRandall Stewart 			slide_end = asoc->mapping_array_size;
2340b3f1ea41SRandall Stewart #endif
2341b3f1ea41SRandall Stewart 		}
2342f8829a4aSRandall Stewart 		distance = (slide_end - slide_from) + 1;
2343b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2344f8829a4aSRandall Stewart 			sctp_log_map(old_base, old_cumack, old_highest,
2345f8829a4aSRandall Stewart 			    SCTP_MAP_PREPARE_SLIDE);
2346f8829a4aSRandall Stewart 			sctp_log_map((uint32_t) slide_from, (uint32_t) slide_end,
2347f8829a4aSRandall Stewart 			    (uint32_t) lgap, SCTP_MAP_SLIDE_FROM);
234880fefe0aSRandall Stewart 		}
2349f8829a4aSRandall Stewart 		if (distance + slide_from > asoc->mapping_array_size ||
2350f8829a4aSRandall Stewart 		    distance < 0) {
2351f8829a4aSRandall Stewart 			/*
2352f8829a4aSRandall Stewart 			 * Here we do NOT slide forward the array so that
2353f8829a4aSRandall Stewart 			 * hopefully when more data comes in to fill it up
2354f8829a4aSRandall Stewart 			 * we will be able to slide it forward. Really I
2355f8829a4aSRandall Stewart 			 * don't think this should happen :-0
2356f8829a4aSRandall Stewart 			 */
2357f8829a4aSRandall Stewart 
2358b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2359f8829a4aSRandall Stewart 				sctp_log_map((uint32_t) distance, (uint32_t) slide_from,
2360f8829a4aSRandall Stewart 				    (uint32_t) asoc->mapping_array_size,
2361f8829a4aSRandall Stewart 				    SCTP_MAP_SLIDE_NONE);
236280fefe0aSRandall Stewart 			}
2363f8829a4aSRandall Stewart 		} else {
2364f8829a4aSRandall Stewart 			int ii;
2365f8829a4aSRandall Stewart 
2366f8829a4aSRandall Stewart 			for (ii = 0; ii < distance; ii++) {
236737f144ebSMichael Tuexen 				asoc->mapping_array[ii] = asoc->mapping_array[slide_from + ii];
236837f144ebSMichael Tuexen 				asoc->nr_mapping_array[ii] = asoc->nr_mapping_array[slide_from + ii];
236977acdc25SRandall Stewart 
2370f8829a4aSRandall Stewart 			}
2371aed5947cSMichael Tuexen 			for (ii = distance; ii < asoc->mapping_array_size; ii++) {
2372f8829a4aSRandall Stewart 				asoc->mapping_array[ii] = 0;
237377acdc25SRandall Stewart 				asoc->nr_mapping_array[ii] = 0;
2374f8829a4aSRandall Stewart 			}
2375ee94f0a2SMichael Tuexen 			if (asoc->highest_tsn_inside_map + 1 == asoc->mapping_array_base_tsn) {
2376ee94f0a2SMichael Tuexen 				asoc->highest_tsn_inside_map += (slide_from << 3);
2377ee94f0a2SMichael Tuexen 			}
2378ee94f0a2SMichael Tuexen 			if (asoc->highest_tsn_inside_nr_map + 1 == asoc->mapping_array_base_tsn) {
2379ee94f0a2SMichael Tuexen 				asoc->highest_tsn_inside_nr_map += (slide_from << 3);
2380ee94f0a2SMichael Tuexen 			}
2381f8829a4aSRandall Stewart 			asoc->mapping_array_base_tsn += (slide_from << 3);
2382b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2383f8829a4aSRandall Stewart 				sctp_log_map(asoc->mapping_array_base_tsn,
2384f8829a4aSRandall Stewart 				    asoc->cumulative_tsn, asoc->highest_tsn_inside_map,
2385f8829a4aSRandall Stewart 				    SCTP_MAP_SLIDE_RESULT);
238680fefe0aSRandall Stewart 			}
2387830d754dSRandall Stewart 		}
2388830d754dSRandall Stewart 	}
2389b5c16493SMichael Tuexen }
2390b5c16493SMichael Tuexen 
2391b5c16493SMichael Tuexen void
2392b5c16493SMichael Tuexen sctp_sack_check(struct sctp_tcb *stcb, int was_a_gap, int *abort_flag)
2393b5c16493SMichael Tuexen {
2394b5c16493SMichael Tuexen 	struct sctp_association *asoc;
2395b5c16493SMichael Tuexen 	uint32_t highest_tsn;
2396b5c16493SMichael Tuexen 
2397b5c16493SMichael Tuexen 	asoc = &stcb->asoc;
239820b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
2399b5c16493SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_nr_map;
2400b5c16493SMichael Tuexen 	} else {
2401b5c16493SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_map;
2402b5c16493SMichael Tuexen 	}
2403b5c16493SMichael Tuexen 
2404830d754dSRandall Stewart 	/*
2405f8829a4aSRandall Stewart 	 * Now we need to see if we need to queue a sack or just start the
2406f8829a4aSRandall Stewart 	 * timer (if allowed).
2407f8829a4aSRandall Stewart 	 */
2408f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2409f8829a4aSRandall Stewart 		/*
2410b5c16493SMichael Tuexen 		 * Ok special case, in SHUTDOWN-SENT case. here we maker
2411b5c16493SMichael Tuexen 		 * sure SACK timer is off and instead send a SHUTDOWN and a
2412b5c16493SMichael Tuexen 		 * SACK
2413f8829a4aSRandall Stewart 		 */
2414139bc87fSRandall Stewart 		if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2415f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
2416a5d547adSRandall Stewart 			    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_INDATA + SCTP_LOC_18);
2417f8829a4aSRandall Stewart 		}
2418f8829a4aSRandall Stewart 		sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
2419*689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
2420f8829a4aSRandall Stewart 	} else {
2421f8829a4aSRandall Stewart 		int is_a_gap;
2422f8829a4aSRandall Stewart 
2423f8829a4aSRandall Stewart 		/* is there a gap now ? */
242420b07a4dSMichael Tuexen 		is_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
2425f8829a4aSRandall Stewart 
2426f8829a4aSRandall Stewart 		/*
2427b5c16493SMichael Tuexen 		 * CMT DAC algorithm: increase number of packets received
2428b5c16493SMichael Tuexen 		 * since last ack
2429f8829a4aSRandall Stewart 		 */
2430f8829a4aSRandall Stewart 		stcb->asoc.cmt_dac_pkts_rcvd++;
2431f8829a4aSRandall Stewart 
243242551e99SRandall Stewart 		if ((stcb->asoc.send_sack == 1) ||	/* We need to send a
243342551e99SRandall Stewart 							 * SACK */
2434f8829a4aSRandall Stewart 		    ((was_a_gap) && (is_a_gap == 0)) ||	/* was a gap, but no
2435f8829a4aSRandall Stewart 							 * longer is one */
2436f8829a4aSRandall Stewart 		    (stcb->asoc.numduptsns) ||	/* we have dup's */
2437f8829a4aSRandall Stewart 		    (is_a_gap) ||	/* is still a gap */
243842551e99SRandall Stewart 		    (stcb->asoc.delayed_ack == 0) ||	/* Delayed sack disabled */
243942551e99SRandall Stewart 		    (stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq)	/* hit limit of pkts */
2440f8829a4aSRandall Stewart 		    ) {
2441f8829a4aSRandall Stewart 
24427c99d56fSMichael Tuexen 			if ((stcb->asoc.sctp_cmt_on_off > 0) &&
2443b3f1ea41SRandall Stewart 			    (SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) &&
244442551e99SRandall Stewart 			    (stcb->asoc.send_sack == 0) &&
2445f8829a4aSRandall Stewart 			    (stcb->asoc.numduptsns == 0) &&
2446f8829a4aSRandall Stewart 			    (stcb->asoc.delayed_ack) &&
2447139bc87fSRandall Stewart 			    (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer))) {
2448f8829a4aSRandall Stewart 
2449f8829a4aSRandall Stewart 				/*
2450b5c16493SMichael Tuexen 				 * CMT DAC algorithm: With CMT, delay acks
2451b5c16493SMichael Tuexen 				 * even in the face of
2452f8829a4aSRandall Stewart 				 *
2453b5c16493SMichael Tuexen 				 * reordering. Therefore, if acks that do not
2454b5c16493SMichael Tuexen 				 * have to be sent because of the above
2455b5c16493SMichael Tuexen 				 * reasons, will be delayed. That is, acks
2456b5c16493SMichael Tuexen 				 * that would have been sent due to gap
2457b5c16493SMichael Tuexen 				 * reports will be delayed with DAC. Start
2458f8829a4aSRandall Stewart 				 * the delayed ack timer.
2459f8829a4aSRandall Stewart 				 */
2460f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2461f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, NULL);
2462f8829a4aSRandall Stewart 			} else {
2463f8829a4aSRandall Stewart 				/*
2464b5c16493SMichael Tuexen 				 * Ok we must build a SACK since the timer
2465b5c16493SMichael Tuexen 				 * is pending, we got our first packet OR
2466b5c16493SMichael Tuexen 				 * there are gaps or duplicates.
2467f8829a4aSRandall Stewart 				 */
2468ad81507eSRandall Stewart 				(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2469*689e6a5fSMichael Tuexen 				sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
2470f8829a4aSRandall Stewart 			}
2471f8829a4aSRandall Stewart 		} else {
247242551e99SRandall Stewart 			if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2473f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2474f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, NULL);
2475f8829a4aSRandall Stewart 			}
2476f8829a4aSRandall Stewart 		}
2477f8829a4aSRandall Stewart 	}
2478f8829a4aSRandall Stewart }
2479f8829a4aSRandall Stewart 
2480f8829a4aSRandall Stewart void
2481f8829a4aSRandall Stewart sctp_service_queues(struct sctp_tcb *stcb, struct sctp_association *asoc)
2482f8829a4aSRandall Stewart {
2483f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
2484810ec536SMichael Tuexen 	uint32_t tsize, pd_point;
2485f8829a4aSRandall Stewart 	uint16_t nxt_todel;
2486f8829a4aSRandall Stewart 
2487f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
2488f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
2489f8829a4aSRandall Stewart 	}
2490f8829a4aSRandall Stewart 	/* Can we proceed further, i.e. the PD-API is complete */
2491f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
2492f8829a4aSRandall Stewart 		/* no */
2493f8829a4aSRandall Stewart 		return;
2494f8829a4aSRandall Stewart 	}
2495f8829a4aSRandall Stewart 	/*
2496f8829a4aSRandall Stewart 	 * Now is there some other chunk I can deliver from the reassembly
2497f8829a4aSRandall Stewart 	 * queue.
2498f8829a4aSRandall Stewart 	 */
2499139bc87fSRandall Stewart doit_again:
2500f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
2501f8829a4aSRandall Stewart 	if (chk == NULL) {
2502f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = 0;
2503f8829a4aSRandall Stewart 		asoc->cnt_on_reasm_queue = 0;
2504f8829a4aSRandall Stewart 		return;
2505f8829a4aSRandall Stewart 	}
2506f8829a4aSRandall Stewart 	nxt_todel = asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
2507f8829a4aSRandall Stewart 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
2508f8829a4aSRandall Stewart 	    ((nxt_todel == chk->rec.data.stream_seq) ||
2509f8829a4aSRandall Stewart 	    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
2510f8829a4aSRandall Stewart 		/*
2511f8829a4aSRandall Stewart 		 * Yep the first one is here. We setup to start reception,
2512f8829a4aSRandall Stewart 		 * by backing down the TSN just in case we can't deliver.
2513f8829a4aSRandall Stewart 		 */
2514f8829a4aSRandall Stewart 
2515f8829a4aSRandall Stewart 		/*
2516f8829a4aSRandall Stewart 		 * Before we start though either all of the message should
251724ae5c4aSMichael Tuexen 		 * be here or the socket buffer max or nothing on the
2518f8829a4aSRandall Stewart 		 * delivery queue and something can be delivered.
2519f8829a4aSRandall Stewart 		 */
2520810ec536SMichael Tuexen 		if (stcb->sctp_socket) {
252124ae5c4aSMichael Tuexen 			pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket),
2522810ec536SMichael Tuexen 			    stcb->sctp_ep->partial_delivery_point);
2523810ec536SMichael Tuexen 		} else {
2524810ec536SMichael Tuexen 			pd_point = stcb->sctp_ep->partial_delivery_point;
2525810ec536SMichael Tuexen 		}
2526810ec536SMichael Tuexen 		if (sctp_is_all_msg_on_reasm(asoc, &tsize) || (tsize >= pd_point)) {
2527f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 1;
2528f8829a4aSRandall Stewart 			asoc->tsn_last_delivered = chk->rec.data.TSN_seq - 1;
2529f8829a4aSRandall Stewart 			asoc->str_of_pdapi = chk->rec.data.stream_number;
2530f8829a4aSRandall Stewart 			asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
2531f8829a4aSRandall Stewart 			asoc->pdapi_ppid = chk->rec.data.payloadtype;
2532f8829a4aSRandall Stewart 			asoc->fragment_flags = chk->rec.data.rcv_flags;
2533f8829a4aSRandall Stewart 			sctp_service_reassembly(stcb, asoc);
2534139bc87fSRandall Stewart 			if (asoc->fragmented_delivery_inprogress == 0) {
2535139bc87fSRandall Stewart 				goto doit_again;
2536139bc87fSRandall Stewart 			}
2537f8829a4aSRandall Stewart 		}
2538f8829a4aSRandall Stewart 	}
2539f8829a4aSRandall Stewart }
2540f8829a4aSRandall Stewart 
2541f8829a4aSRandall Stewart int
2542f8829a4aSRandall Stewart sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length,
2543f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2544f8829a4aSRandall Stewart     struct sctp_nets *net, uint32_t * high_tsn)
2545f8829a4aSRandall Stewart {
2546f8829a4aSRandall Stewart 	struct sctp_data_chunk *ch, chunk_buf;
2547f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2548f8829a4aSRandall Stewart 	int num_chunks = 0;	/* number of control chunks processed */
2549f8829a4aSRandall Stewart 	int stop_proc = 0;
2550f8829a4aSRandall Stewart 	int chk_length, break_flag, last_chunk;
25518f777478SMichael Tuexen 	int abort_flag = 0, was_a_gap;
2552f8829a4aSRandall Stewart 	struct mbuf *m;
25538f777478SMichael Tuexen 	uint32_t highest_tsn;
2554f8829a4aSRandall Stewart 
2555f8829a4aSRandall Stewart 	/* set the rwnd */
2556f8829a4aSRandall Stewart 	sctp_set_rwnd(stcb, &stcb->asoc);
2557f8829a4aSRandall Stewart 
2558f8829a4aSRandall Stewart 	m = *mm;
2559f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
2560f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
256120b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
25628f777478SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_nr_map;
25638f777478SMichael Tuexen 	} else {
25648f777478SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_map;
2565f8829a4aSRandall Stewart 	}
256620b07a4dSMichael Tuexen 	was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
2567f8829a4aSRandall Stewart 	/*
2568f8829a4aSRandall Stewart 	 * setup where we got the last DATA packet from for any SACK that
2569f8829a4aSRandall Stewart 	 * may need to go out. Don't bump the net. This is done ONLY when a
2570f8829a4aSRandall Stewart 	 * chunk is assigned.
2571f8829a4aSRandall Stewart 	 */
2572f8829a4aSRandall Stewart 	asoc->last_data_chunk_from = net;
2573f8829a4aSRandall Stewart 
2574d06c82f1SRandall Stewart 	/*-
2575f8829a4aSRandall Stewart 	 * Now before we proceed we must figure out if this is a wasted
2576f8829a4aSRandall Stewart 	 * cluster... i.e. it is a small packet sent in and yet the driver
2577f8829a4aSRandall Stewart 	 * underneath allocated a full cluster for it. If so we must copy it
2578f8829a4aSRandall Stewart 	 * to a smaller mbuf and free up the cluster mbuf. This will help
2579d06c82f1SRandall Stewart 	 * with cluster starvation. Note for __Panda__ we don't do this
2580d06c82f1SRandall Stewart 	 * since it has clusters all the way down to 64 bytes.
2581f8829a4aSRandall Stewart 	 */
258244b7479bSRandall Stewart 	if (SCTP_BUF_LEN(m) < (long)MLEN && SCTP_BUF_NEXT(m) == NULL) {
2583f8829a4aSRandall Stewart 		/* we only handle mbufs that are singletons.. not chains */
2584139bc87fSRandall Stewart 		m = sctp_get_mbuf_for_msg(SCTP_BUF_LEN(m), 0, M_DONTWAIT, 1, MT_DATA);
2585f8829a4aSRandall Stewart 		if (m) {
2586f8829a4aSRandall Stewart 			/* ok lets see if we can copy the data up */
2587f8829a4aSRandall Stewart 			caddr_t *from, *to;
2588f8829a4aSRandall Stewart 
2589f8829a4aSRandall Stewart 			/* get the pointers and copy */
2590f8829a4aSRandall Stewart 			to = mtod(m, caddr_t *);
2591f8829a4aSRandall Stewart 			from = mtod((*mm), caddr_t *);
2592139bc87fSRandall Stewart 			memcpy(to, from, SCTP_BUF_LEN((*mm)));
2593f8829a4aSRandall Stewart 			/* copy the length and free up the old */
2594139bc87fSRandall Stewart 			SCTP_BUF_LEN(m) = SCTP_BUF_LEN((*mm));
2595f8829a4aSRandall Stewart 			sctp_m_freem(*mm);
2596f8829a4aSRandall Stewart 			/* sucess, back copy */
2597f8829a4aSRandall Stewart 			*mm = m;
2598f8829a4aSRandall Stewart 		} else {
2599f8829a4aSRandall Stewart 			/* We are in trouble in the mbuf world .. yikes */
2600f8829a4aSRandall Stewart 			m = *mm;
2601f8829a4aSRandall Stewart 		}
2602f8829a4aSRandall Stewart 	}
2603f8829a4aSRandall Stewart 	/* get pointer to the first chunk header */
2604f8829a4aSRandall Stewart 	ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2605f8829a4aSRandall Stewart 	    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2606f8829a4aSRandall Stewart 	if (ch == NULL) {
2607f8829a4aSRandall Stewart 		return (1);
2608f8829a4aSRandall Stewart 	}
2609f8829a4aSRandall Stewart 	/*
2610f8829a4aSRandall Stewart 	 * process all DATA chunks...
2611f8829a4aSRandall Stewart 	 */
2612f8829a4aSRandall Stewart 	*high_tsn = asoc->cumulative_tsn;
2613f8829a4aSRandall Stewart 	break_flag = 0;
261442551e99SRandall Stewart 	asoc->data_pkts_seen++;
2615f8829a4aSRandall Stewart 	while (stop_proc == 0) {
2616f8829a4aSRandall Stewart 		/* validate chunk length */
2617f8829a4aSRandall Stewart 		chk_length = ntohs(ch->ch.chunk_length);
2618f8829a4aSRandall Stewart 		if (length - *offset < chk_length) {
2619f8829a4aSRandall Stewart 			/* all done, mutulated chunk */
2620f8829a4aSRandall Stewart 			stop_proc = 1;
2621f8829a4aSRandall Stewart 			break;
2622f8829a4aSRandall Stewart 		}
2623f8829a4aSRandall Stewart 		if (ch->ch.chunk_type == SCTP_DATA) {
2624f8829a4aSRandall Stewart 			if ((size_t)chk_length < sizeof(struct sctp_data_chunk) + 1) {
2625f8829a4aSRandall Stewart 				/*
2626f8829a4aSRandall Stewart 				 * Need to send an abort since we had a
2627f8829a4aSRandall Stewart 				 * invalid data chunk.
2628f8829a4aSRandall Stewart 				 */
2629f8829a4aSRandall Stewart 				struct mbuf *op_err;
2630f8829a4aSRandall Stewart 
2631139bc87fSRandall Stewart 				op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 2 * sizeof(uint32_t)),
2632f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
2633f8829a4aSRandall Stewart 
2634f8829a4aSRandall Stewart 				if (op_err) {
2635f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
2636f8829a4aSRandall Stewart 					uint32_t *ippp;
2637f8829a4aSRandall Stewart 
2638139bc87fSRandall Stewart 					SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr) +
2639f8829a4aSRandall Stewart 					    (2 * sizeof(uint32_t));
2640f8829a4aSRandall Stewart 					ph = mtod(op_err, struct sctp_paramhdr *);
2641f8829a4aSRandall Stewart 					ph->param_type =
2642f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2643139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(op_err));
2644f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
2645a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_19);
2646f8829a4aSRandall Stewart 					ippp++;
2647f8829a4aSRandall Stewart 					*ippp = asoc->cumulative_tsn;
2648f8829a4aSRandall Stewart 
2649f8829a4aSRandall Stewart 				}
2650a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_19;
2651f8829a4aSRandall Stewart 				sctp_abort_association(inp, stcb, m, iphlen, sh,
2652c54a18d2SRandall Stewart 				    op_err, 0, net->port);
2653f8829a4aSRandall Stewart 				return (2);
2654f8829a4aSRandall Stewart 			}
2655f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
2656f8829a4aSRandall Stewart 			sctp_audit_log(0xB1, 0);
2657f8829a4aSRandall Stewart #endif
2658f8829a4aSRandall Stewart 			if (SCTP_SIZE32(chk_length) == (length - *offset)) {
2659f8829a4aSRandall Stewart 				last_chunk = 1;
2660f8829a4aSRandall Stewart 			} else {
2661f8829a4aSRandall Stewart 				last_chunk = 0;
2662f8829a4aSRandall Stewart 			}
2663f8829a4aSRandall Stewart 			if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset, ch,
2664f8829a4aSRandall Stewart 			    chk_length, net, high_tsn, &abort_flag, &break_flag,
2665f8829a4aSRandall Stewart 			    last_chunk)) {
2666f8829a4aSRandall Stewart 				num_chunks++;
2667f8829a4aSRandall Stewart 			}
2668f8829a4aSRandall Stewart 			if (abort_flag)
2669f8829a4aSRandall Stewart 				return (2);
2670f8829a4aSRandall Stewart 
2671f8829a4aSRandall Stewart 			if (break_flag) {
2672f8829a4aSRandall Stewart 				/*
2673f8829a4aSRandall Stewart 				 * Set because of out of rwnd space and no
2674f8829a4aSRandall Stewart 				 * drop rep space left.
2675f8829a4aSRandall Stewart 				 */
2676f8829a4aSRandall Stewart 				stop_proc = 1;
2677f8829a4aSRandall Stewart 				break;
2678f8829a4aSRandall Stewart 			}
2679f8829a4aSRandall Stewart 		} else {
2680f8829a4aSRandall Stewart 			/* not a data chunk in the data region */
2681f8829a4aSRandall Stewart 			switch (ch->ch.chunk_type) {
2682f8829a4aSRandall Stewart 			case SCTP_INITIATION:
2683f8829a4aSRandall Stewart 			case SCTP_INITIATION_ACK:
2684f8829a4aSRandall Stewart 			case SCTP_SELECTIVE_ACK:
2685830d754dSRandall Stewart 			case SCTP_NR_SELECTIVE_ACK:	/* EY */
2686f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_REQUEST:
2687f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_ACK:
2688f8829a4aSRandall Stewart 			case SCTP_ABORT_ASSOCIATION:
2689f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN:
2690f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_ACK:
2691f8829a4aSRandall Stewart 			case SCTP_OPERATION_ERROR:
2692f8829a4aSRandall Stewart 			case SCTP_COOKIE_ECHO:
2693f8829a4aSRandall Stewart 			case SCTP_COOKIE_ACK:
2694f8829a4aSRandall Stewart 			case SCTP_ECN_ECHO:
2695f8829a4aSRandall Stewart 			case SCTP_ECN_CWR:
2696f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_COMPLETE:
2697f8829a4aSRandall Stewart 			case SCTP_AUTHENTICATION:
2698f8829a4aSRandall Stewart 			case SCTP_ASCONF_ACK:
2699f8829a4aSRandall Stewart 			case SCTP_PACKET_DROPPED:
2700f8829a4aSRandall Stewart 			case SCTP_STREAM_RESET:
2701f8829a4aSRandall Stewart 			case SCTP_FORWARD_CUM_TSN:
2702f8829a4aSRandall Stewart 			case SCTP_ASCONF:
2703f8829a4aSRandall Stewart 				/*
2704f8829a4aSRandall Stewart 				 * Now, what do we do with KNOWN chunks that
2705f8829a4aSRandall Stewart 				 * are NOT in the right place?
2706f8829a4aSRandall Stewart 				 *
2707f8829a4aSRandall Stewart 				 * For now, I do nothing but ignore them. We
2708f8829a4aSRandall Stewart 				 * may later want to add sysctl stuff to
2709f8829a4aSRandall Stewart 				 * switch out and do either an ABORT() or
2710f8829a4aSRandall Stewart 				 * possibly process them.
2711f8829a4aSRandall Stewart 				 */
2712b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_strict_data_order)) {
2713f8829a4aSRandall Stewart 					struct mbuf *op_err;
2714f8829a4aSRandall Stewart 
2715f8829a4aSRandall Stewart 					op_err = sctp_generate_invmanparam(SCTP_CAUSE_PROTOCOL_VIOLATION);
2716c54a18d2SRandall Stewart 					sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 0, net->port);
2717f8829a4aSRandall Stewart 					return (2);
2718f8829a4aSRandall Stewart 				}
2719f8829a4aSRandall Stewart 				break;
2720f8829a4aSRandall Stewart 			default:
2721f8829a4aSRandall Stewart 				/* unknown chunk type, use bit rules */
2722f8829a4aSRandall Stewart 				if (ch->ch.chunk_type & 0x40) {
2723f8829a4aSRandall Stewart 					/* Add a error report to the queue */
2724d61a0ae0SRandall Stewart 					struct mbuf *merr;
2725f8829a4aSRandall Stewart 					struct sctp_paramhdr *phd;
2726f8829a4aSRandall Stewart 
2727d61a0ae0SRandall Stewart 					merr = sctp_get_mbuf_for_msg(sizeof(*phd), 0, M_DONTWAIT, 1, MT_DATA);
2728d61a0ae0SRandall Stewart 					if (merr) {
2729d61a0ae0SRandall Stewart 						phd = mtod(merr, struct sctp_paramhdr *);
2730f8829a4aSRandall Stewart 						/*
2731f8829a4aSRandall Stewart 						 * We cheat and use param
2732f8829a4aSRandall Stewart 						 * type since we did not
2733f8829a4aSRandall Stewart 						 * bother to define a error
2734f8829a4aSRandall Stewart 						 * cause struct. They are
2735f8829a4aSRandall Stewart 						 * the same basic format
2736f8829a4aSRandall Stewart 						 * with different names.
2737f8829a4aSRandall Stewart 						 */
2738f8829a4aSRandall Stewart 						phd->param_type =
2739f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_UNRECOG_CHUNK);
2740f8829a4aSRandall Stewart 						phd->param_length =
2741f8829a4aSRandall Stewart 						    htons(chk_length + sizeof(*phd));
2742d61a0ae0SRandall Stewart 						SCTP_BUF_LEN(merr) = sizeof(*phd);
2743d61a0ae0SRandall Stewart 						SCTP_BUF_NEXT(merr) = SCTP_M_COPYM(m, *offset,
2744f8829a4aSRandall Stewart 						    SCTP_SIZE32(chk_length),
2745f8829a4aSRandall Stewart 						    M_DONTWAIT);
2746d61a0ae0SRandall Stewart 						if (SCTP_BUF_NEXT(merr)) {
2747d61a0ae0SRandall Stewart 							sctp_queue_op_err(stcb, merr);
2748f8829a4aSRandall Stewart 						} else {
2749d61a0ae0SRandall Stewart 							sctp_m_freem(merr);
2750f8829a4aSRandall Stewart 						}
2751f8829a4aSRandall Stewart 					}
2752f8829a4aSRandall Stewart 				}
2753f8829a4aSRandall Stewart 				if ((ch->ch.chunk_type & 0x80) == 0) {
2754f8829a4aSRandall Stewart 					/* discard the rest of this packet */
2755f8829a4aSRandall Stewart 					stop_proc = 1;
2756f8829a4aSRandall Stewart 				}	/* else skip this bad chunk and
2757f8829a4aSRandall Stewart 					 * continue... */
2758f8829a4aSRandall Stewart 				break;
2759f8829a4aSRandall Stewart 			};	/* switch of chunk type */
2760f8829a4aSRandall Stewart 		}
2761f8829a4aSRandall Stewart 		*offset += SCTP_SIZE32(chk_length);
2762f8829a4aSRandall Stewart 		if ((*offset >= length) || stop_proc) {
2763f8829a4aSRandall Stewart 			/* no more data left in the mbuf chain */
2764f8829a4aSRandall Stewart 			stop_proc = 1;
2765f8829a4aSRandall Stewart 			continue;
2766f8829a4aSRandall Stewart 		}
2767f8829a4aSRandall Stewart 		ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2768f8829a4aSRandall Stewart 		    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2769f8829a4aSRandall Stewart 		if (ch == NULL) {
2770f8829a4aSRandall Stewart 			*offset = length;
2771f8829a4aSRandall Stewart 			stop_proc = 1;
2772f8829a4aSRandall Stewart 			break;
2773f8829a4aSRandall Stewart 
2774f8829a4aSRandall Stewart 		}
2775f8829a4aSRandall Stewart 	}			/* while */
2776f8829a4aSRandall Stewart 	if (break_flag) {
2777f8829a4aSRandall Stewart 		/*
2778f8829a4aSRandall Stewart 		 * we need to report rwnd overrun drops.
2779f8829a4aSRandall Stewart 		 */
2780f8829a4aSRandall Stewart 		sctp_send_packet_dropped(stcb, net, *mm, iphlen, 0);
2781f8829a4aSRandall Stewart 	}
2782f8829a4aSRandall Stewart 	if (num_chunks) {
2783f8829a4aSRandall Stewart 		/*
2784ceaad40aSRandall Stewart 		 * Did we get data, if so update the time for auto-close and
2785f8829a4aSRandall Stewart 		 * give peer credit for being alive.
2786f8829a4aSRandall Stewart 		 */
2787f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvpktwithdata);
2788b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
2789c4739e2fSRandall Stewart 			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2790c4739e2fSRandall Stewart 			    stcb->asoc.overall_error_count,
2791c4739e2fSRandall Stewart 			    0,
2792c4739e2fSRandall Stewart 			    SCTP_FROM_SCTP_INDATA,
2793c4739e2fSRandall Stewart 			    __LINE__);
2794c4739e2fSRandall Stewart 		}
2795f8829a4aSRandall Stewart 		stcb->asoc.overall_error_count = 0;
27966e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
2797f8829a4aSRandall Stewart 	}
2798f8829a4aSRandall Stewart 	/* now service all of the reassm queue if needed */
2799f8829a4aSRandall Stewart 	if (!(TAILQ_EMPTY(&asoc->reasmqueue)))
2800f8829a4aSRandall Stewart 		sctp_service_queues(stcb, asoc);
2801f8829a4aSRandall Stewart 
2802f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
280342551e99SRandall Stewart 		/* Assure that we ack right away */
280442551e99SRandall Stewart 		stcb->asoc.send_sack = 1;
2805f8829a4aSRandall Stewart 	}
2806f8829a4aSRandall Stewart 	/* Start a sack timer or QUEUE a SACK for sending */
2807b5c16493SMichael Tuexen 	sctp_sack_check(stcb, was_a_gap, &abort_flag);
2808f8829a4aSRandall Stewart 	if (abort_flag)
2809f8829a4aSRandall Stewart 		return (2);
2810f8829a4aSRandall Stewart 
2811f8829a4aSRandall Stewart 	return (0);
2812f8829a4aSRandall Stewart }
2813f8829a4aSRandall Stewart 
28140fa753b3SRandall Stewart static int
28150fa753b3SRandall Stewart sctp_process_segment_range(struct sctp_tcb *stcb, struct sctp_tmit_chunk **p_tp1, uint32_t last_tsn,
28160fa753b3SRandall Stewart     uint16_t frag_strt, uint16_t frag_end, int nr_sacking,
28170fa753b3SRandall Stewart     int *num_frs,
28180fa753b3SRandall Stewart     uint32_t * biggest_newly_acked_tsn,
28190fa753b3SRandall Stewart     uint32_t * this_sack_lowest_newack,
2820f79aab18SRandall Stewart     int *ecn_seg_sums, int *rto_ok)
28210fa753b3SRandall Stewart {
28220fa753b3SRandall Stewart 	struct sctp_tmit_chunk *tp1;
28230fa753b3SRandall Stewart 	unsigned int theTSN;
2824b5c16493SMichael Tuexen 	int j, wake_him = 0, circled = 0;
28250fa753b3SRandall Stewart 
28260fa753b3SRandall Stewart 	/* Recover the tp1 we last saw */
28270fa753b3SRandall Stewart 	tp1 = *p_tp1;
28280fa753b3SRandall Stewart 	if (tp1 == NULL) {
28290fa753b3SRandall Stewart 		tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
28300fa753b3SRandall Stewart 	}
28310fa753b3SRandall Stewart 	for (j = frag_strt; j <= frag_end; j++) {
28320fa753b3SRandall Stewart 		theTSN = j + last_tsn;
28330fa753b3SRandall Stewart 		while (tp1) {
28340fa753b3SRandall Stewart 			if (tp1->rec.data.doing_fast_retransmit)
28350fa753b3SRandall Stewart 				(*num_frs) += 1;
28360fa753b3SRandall Stewart 
28370fa753b3SRandall Stewart 			/*-
28380fa753b3SRandall Stewart 			 * CMT: CUCv2 algorithm. For each TSN being
28390fa753b3SRandall Stewart 			 * processed from the sent queue, track the
28400fa753b3SRandall Stewart 			 * next expected pseudo-cumack, or
28410fa753b3SRandall Stewart 			 * rtx_pseudo_cumack, if required. Separate
28420fa753b3SRandall Stewart 			 * cumack trackers for first transmissions,
28430fa753b3SRandall Stewart 			 * and retransmissions.
28440fa753b3SRandall Stewart 			 */
28450fa753b3SRandall Stewart 			if ((tp1->whoTo->find_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
28460fa753b3SRandall Stewart 			    (tp1->snd_count == 1)) {
28470fa753b3SRandall Stewart 				tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
28480fa753b3SRandall Stewart 				tp1->whoTo->find_pseudo_cumack = 0;
28490fa753b3SRandall Stewart 			}
28500fa753b3SRandall Stewart 			if ((tp1->whoTo->find_rtx_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
28510fa753b3SRandall Stewart 			    (tp1->snd_count > 1)) {
28520fa753b3SRandall Stewart 				tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
28530fa753b3SRandall Stewart 				tp1->whoTo->find_rtx_pseudo_cumack = 0;
28540fa753b3SRandall Stewart 			}
28550fa753b3SRandall Stewart 			if (tp1->rec.data.TSN_seq == theTSN) {
28560fa753b3SRandall Stewart 				if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
28570fa753b3SRandall Stewart 					/*-
28580fa753b3SRandall Stewart 					 * must be held until
28590fa753b3SRandall Stewart 					 * cum-ack passes
28600fa753b3SRandall Stewart 					 */
28610fa753b3SRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
28620fa753b3SRandall Stewart 						/*-
28630fa753b3SRandall Stewart 						 * If it is less than RESEND, it is
28640fa753b3SRandall Stewart 						 * now no-longer in flight.
28650fa753b3SRandall Stewart 						 * Higher values may already be set
28660fa753b3SRandall Stewart 						 * via previous Gap Ack Blocks...
28670fa753b3SRandall Stewart 						 * i.e. ACKED or RESEND.
28680fa753b3SRandall Stewart 						 */
286920b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
287020b07a4dSMichael Tuexen 						    *biggest_newly_acked_tsn)) {
28710fa753b3SRandall Stewart 							*biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
28720fa753b3SRandall Stewart 						}
28730fa753b3SRandall Stewart 						/*-
28740fa753b3SRandall Stewart 						 * CMT: SFR algo (and HTNA) - set
28750fa753b3SRandall Stewart 						 * saw_newack to 1 for dest being
28760fa753b3SRandall Stewart 						 * newly acked. update
28770fa753b3SRandall Stewart 						 * this_sack_highest_newack if
28780fa753b3SRandall Stewart 						 * appropriate.
28790fa753b3SRandall Stewart 						 */
28800fa753b3SRandall Stewart 						if (tp1->rec.data.chunk_was_revoked == 0)
28810fa753b3SRandall Stewart 							tp1->whoTo->saw_newack = 1;
28820fa753b3SRandall Stewart 
288320b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
288420b07a4dSMichael Tuexen 						    tp1->whoTo->this_sack_highest_newack)) {
28850fa753b3SRandall Stewart 							tp1->whoTo->this_sack_highest_newack =
28860fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq;
28870fa753b3SRandall Stewart 						}
28880fa753b3SRandall Stewart 						/*-
28890fa753b3SRandall Stewart 						 * CMT DAC algo: also update
28900fa753b3SRandall Stewart 						 * this_sack_lowest_newack
28910fa753b3SRandall Stewart 						 */
28920fa753b3SRandall Stewart 						if (*this_sack_lowest_newack == 0) {
28930fa753b3SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
28940fa753b3SRandall Stewart 								sctp_log_sack(*this_sack_lowest_newack,
28950fa753b3SRandall Stewart 								    last_tsn,
28960fa753b3SRandall Stewart 								    tp1->rec.data.TSN_seq,
28970fa753b3SRandall Stewart 								    0,
28980fa753b3SRandall Stewart 								    0,
28990fa753b3SRandall Stewart 								    SCTP_LOG_TSN_ACKED);
29000fa753b3SRandall Stewart 							}
29010fa753b3SRandall Stewart 							*this_sack_lowest_newack = tp1->rec.data.TSN_seq;
29020fa753b3SRandall Stewart 						}
29030fa753b3SRandall Stewart 						/*-
29040fa753b3SRandall Stewart 						 * CMT: CUCv2 algorithm. If (rtx-)pseudo-cumack for corresp
29050fa753b3SRandall Stewart 						 * dest is being acked, then we have a new (rtx-)pseudo-cumack. Set
29060fa753b3SRandall Stewart 						 * new_(rtx_)pseudo_cumack to TRUE so that the cwnd for this dest can be
29070fa753b3SRandall Stewart 						 * updated. Also trigger search for the next expected (rtx-)pseudo-cumack.
29080fa753b3SRandall Stewart 						 * Separate pseudo_cumack trackers for first transmissions and
29090fa753b3SRandall Stewart 						 * retransmissions.
29100fa753b3SRandall Stewart 						 */
29110fa753b3SRandall Stewart 						if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
29120fa753b3SRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0) {
29130fa753b3SRandall Stewart 								tp1->whoTo->new_pseudo_cumack = 1;
29140fa753b3SRandall Stewart 							}
29150fa753b3SRandall Stewart 							tp1->whoTo->find_pseudo_cumack = 1;
29160fa753b3SRandall Stewart 						}
29170fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
29180fa753b3SRandall Stewart 							sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
29190fa753b3SRandall Stewart 						}
29200fa753b3SRandall Stewart 						if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
29210fa753b3SRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0) {
29220fa753b3SRandall Stewart 								tp1->whoTo->new_pseudo_cumack = 1;
29230fa753b3SRandall Stewart 							}
29240fa753b3SRandall Stewart 							tp1->whoTo->find_rtx_pseudo_cumack = 1;
29250fa753b3SRandall Stewart 						}
29260fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
29270fa753b3SRandall Stewart 							sctp_log_sack(*biggest_newly_acked_tsn,
29280fa753b3SRandall Stewart 							    last_tsn,
29290fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq,
29300fa753b3SRandall Stewart 							    frag_strt,
29310fa753b3SRandall Stewart 							    frag_end,
29320fa753b3SRandall Stewart 							    SCTP_LOG_TSN_ACKED);
29330fa753b3SRandall Stewart 						}
29340fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
29350fa753b3SRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
29360fa753b3SRandall Stewart 							    tp1->whoTo->flight_size,
29370fa753b3SRandall Stewart 							    tp1->book_size,
29380fa753b3SRandall Stewart 							    (uintptr_t) tp1->whoTo,
29390fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq);
29400fa753b3SRandall Stewart 						}
29410fa753b3SRandall Stewart 						sctp_flight_size_decrease(tp1);
2942299108c5SRandall Stewart 						if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
2943299108c5SRandall Stewart 							(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
2944299108c5SRandall Stewart 							    tp1);
2945299108c5SRandall Stewart 						}
29460fa753b3SRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
29470fa753b3SRandall Stewart 
29480fa753b3SRandall Stewart 						tp1->whoTo->net_ack += tp1->send_size;
29490fa753b3SRandall Stewart 						if (tp1->snd_count < 2) {
29500fa753b3SRandall Stewart 							/*-
29510fa753b3SRandall Stewart 							 * True non-retransmited chunk
29520fa753b3SRandall Stewart 							 */
29530fa753b3SRandall Stewart 							tp1->whoTo->net_ack2 += tp1->send_size;
29540fa753b3SRandall Stewart 
29550fa753b3SRandall Stewart 							/*-
29560fa753b3SRandall Stewart 							 * update RTO too ?
29570fa753b3SRandall Stewart 							 */
29580fa753b3SRandall Stewart 							if (tp1->do_rtt) {
2959f79aab18SRandall Stewart 								if (*rto_ok) {
29600fa753b3SRandall Stewart 									tp1->whoTo->RTO =
29610fa753b3SRandall Stewart 									    sctp_calculate_rto(stcb,
29620fa753b3SRandall Stewart 									    &stcb->asoc,
29630fa753b3SRandall Stewart 									    tp1->whoTo,
29640fa753b3SRandall Stewart 									    &tp1->sent_rcv_time,
2965899288aeSRandall Stewart 									    sctp_align_safe_nocopy,
2966f79aab18SRandall Stewart 									    SCTP_RTT_FROM_DATA);
2967f79aab18SRandall Stewart 									*rto_ok = 0;
2968f79aab18SRandall Stewart 								}
2969f79aab18SRandall Stewart 								if (tp1->whoTo->rto_needed == 0) {
2970f79aab18SRandall Stewart 									tp1->whoTo->rto_needed = 1;
2971f79aab18SRandall Stewart 								}
29720fa753b3SRandall Stewart 								tp1->do_rtt = 0;
29730fa753b3SRandall Stewart 							}
29740fa753b3SRandall Stewart 						}
29750fa753b3SRandall Stewart 					}
29760fa753b3SRandall Stewart 					if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
297720b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
297820b07a4dSMichael Tuexen 						    stcb->asoc.this_sack_highest_gap)) {
29790fa753b3SRandall Stewart 							stcb->asoc.this_sack_highest_gap =
29800fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq;
29810fa753b3SRandall Stewart 						}
29820fa753b3SRandall Stewart 						if (tp1->sent == SCTP_DATAGRAM_RESEND) {
29830fa753b3SRandall Stewart 							sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
29840fa753b3SRandall Stewart #ifdef SCTP_AUDITING_ENABLED
29850fa753b3SRandall Stewart 							sctp_audit_log(0xB2,
29860fa753b3SRandall Stewart 							    (stcb->asoc.sent_queue_retran_cnt & 0x000000ff));
29870fa753b3SRandall Stewart #endif
29880fa753b3SRandall Stewart 						}
29890fa753b3SRandall Stewart 					}
29900fa753b3SRandall Stewart 					/*-
29910fa753b3SRandall Stewart 					 * All chunks NOT UNSENT fall through here and are marked
29920fa753b3SRandall Stewart 					 * (leave PR-SCTP ones that are to skip alone though)
29930fa753b3SRandall Stewart 					 */
29940fa753b3SRandall Stewart 					if (tp1->sent != SCTP_FORWARD_TSN_SKIP)
29950fa753b3SRandall Stewart 						tp1->sent = SCTP_DATAGRAM_MARKED;
29960fa753b3SRandall Stewart 
29970fa753b3SRandall Stewart 					if (tp1->rec.data.chunk_was_revoked) {
29980fa753b3SRandall Stewart 						/* deflate the cwnd */
29990fa753b3SRandall Stewart 						tp1->whoTo->cwnd -= tp1->book_size;
30000fa753b3SRandall Stewart 						tp1->rec.data.chunk_was_revoked = 0;
30010fa753b3SRandall Stewart 					}
30020fa753b3SRandall Stewart 					/* NR Sack code here */
30030fa753b3SRandall Stewart 					if (nr_sacking) {
30040fa753b3SRandall Stewart 						if (tp1->data) {
30050fa753b3SRandall Stewart 							/*
30060fa753b3SRandall Stewart 							 * sa_ignore
30070fa753b3SRandall Stewart 							 * NO_NULL_CHK
30080fa753b3SRandall Stewart 							 */
30090fa753b3SRandall Stewart 							sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
30100fa753b3SRandall Stewart 							sctp_m_freem(tp1->data);
30110fa753b3SRandall Stewart 							tp1->data = NULL;
3012b5c16493SMichael Tuexen 						}
30130fa753b3SRandall Stewart 						wake_him++;
30140fa753b3SRandall Stewart 					}
30150fa753b3SRandall Stewart 				}
30160fa753b3SRandall Stewart 				break;
30170fa753b3SRandall Stewart 			}	/* if (tp1->TSN_seq == theTSN) */
301820b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, theTSN)) {
30190fa753b3SRandall Stewart 				break;
302020b07a4dSMichael Tuexen 			}
30210fa753b3SRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3022b5c16493SMichael Tuexen 			if ((tp1 == NULL) && (circled == 0)) {
3023b5c16493SMichael Tuexen 				circled++;
30240fa753b3SRandall Stewart 				tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
30250fa753b3SRandall Stewart 			}
3026b5c16493SMichael Tuexen 		}		/* end while (tp1) */
3027b5c16493SMichael Tuexen 		if (tp1 == NULL) {
3028b5c16493SMichael Tuexen 			circled = 0;
3029b5c16493SMichael Tuexen 			tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
3030b5c16493SMichael Tuexen 		}
3031b5c16493SMichael Tuexen 		/* In case the fragments were not in order we must reset */
30320fa753b3SRandall Stewart 	}			/* end for (j = fragStart */
30330fa753b3SRandall Stewart 	*p_tp1 = tp1;
30340fa753b3SRandall Stewart 	return (wake_him);	/* Return value only used for nr-sack */
30350fa753b3SRandall Stewart }
30360fa753b3SRandall Stewart 
30370fa753b3SRandall Stewart 
3038cd554309SMichael Tuexen static int
3039458303daSRandall Stewart sctp_handle_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
3040cd554309SMichael Tuexen     uint32_t last_tsn, uint32_t * biggest_tsn_acked,
3041139bc87fSRandall Stewart     uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
3042f79aab18SRandall Stewart     int num_seg, int num_nr_seg, int *ecn_seg_sums,
3043f79aab18SRandall Stewart     int *rto_ok)
3044f8829a4aSRandall Stewart {
3045458303daSRandall Stewart 	struct sctp_gap_ack_block *frag, block;
3046f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
30470fa753b3SRandall Stewart 	int i;
3048f8829a4aSRandall Stewart 	int num_frs = 0;
3049cd554309SMichael Tuexen 	int chunk_freed;
3050cd554309SMichael Tuexen 	int non_revocable;
3051d9c5cfeaSMichael Tuexen 	uint16_t frag_strt, frag_end, prev_frag_end;
3052f8829a4aSRandall Stewart 
3053d9c5cfeaSMichael Tuexen 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
3054d9c5cfeaSMichael Tuexen 	prev_frag_end = 0;
3055cd554309SMichael Tuexen 	chunk_freed = 0;
3056f8829a4aSRandall Stewart 
3057cd554309SMichael Tuexen 	for (i = 0; i < (num_seg + num_nr_seg); i++) {
3058d9c5cfeaSMichael Tuexen 		if (i == num_seg) {
3059d9c5cfeaSMichael Tuexen 			prev_frag_end = 0;
3060d9c5cfeaSMichael Tuexen 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
3061d9c5cfeaSMichael Tuexen 		}
3062458303daSRandall Stewart 		frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
3063458303daSRandall Stewart 		    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
3064458303daSRandall Stewart 		*offset += sizeof(block);
3065458303daSRandall Stewart 		if (frag == NULL) {
3066cd554309SMichael Tuexen 			return (chunk_freed);
3067458303daSRandall Stewart 		}
3068f8829a4aSRandall Stewart 		frag_strt = ntohs(frag->start);
3069f8829a4aSRandall Stewart 		frag_end = ntohs(frag->end);
3070d9c5cfeaSMichael Tuexen 
3071f8829a4aSRandall Stewart 		if (frag_strt > frag_end) {
3072d9c5cfeaSMichael Tuexen 			/* This gap report is malformed, skip it. */
3073f8829a4aSRandall Stewart 			continue;
3074f8829a4aSRandall Stewart 		}
3075d9c5cfeaSMichael Tuexen 		if (frag_strt <= prev_frag_end) {
3076d9c5cfeaSMichael Tuexen 			/* This gap report is not in order, so restart. */
3077f8829a4aSRandall Stewart 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
3078f8829a4aSRandall Stewart 		}
307920b07a4dSMichael Tuexen 		if (SCTP_TSN_GT((last_tsn + frag_end), *biggest_tsn_acked)) {
3080d9c5cfeaSMichael Tuexen 			*biggest_tsn_acked = last_tsn + frag_end;
3081f8829a4aSRandall Stewart 		}
3082cd554309SMichael Tuexen 		if (i < num_seg) {
3083cd554309SMichael Tuexen 			non_revocable = 0;
3084cd554309SMichael Tuexen 		} else {
3085cd554309SMichael Tuexen 			non_revocable = 1;
3086cd554309SMichael Tuexen 		}
3087cd554309SMichael Tuexen 		if (sctp_process_segment_range(stcb, &tp1, last_tsn, frag_strt, frag_end,
3088cd554309SMichael Tuexen 		    non_revocable, &num_frs, biggest_newly_acked_tsn,
3089f79aab18SRandall Stewart 		    this_sack_lowest_newack, ecn_seg_sums, rto_ok)) {
3090cd554309SMichael Tuexen 			chunk_freed = 1;
3091458303daSRandall Stewart 		}
3092d9c5cfeaSMichael Tuexen 		prev_frag_end = frag_end;
3093f8829a4aSRandall Stewart 	}
3094b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
309580fefe0aSRandall Stewart 		if (num_frs)
309680fefe0aSRandall Stewart 			sctp_log_fr(*biggest_tsn_acked,
309780fefe0aSRandall Stewart 			    *biggest_newly_acked_tsn,
309880fefe0aSRandall Stewart 			    last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
309980fefe0aSRandall Stewart 	}
3100cd554309SMichael Tuexen 	return (chunk_freed);
3101f8829a4aSRandall Stewart }
3102f8829a4aSRandall Stewart 
3103f8829a4aSRandall Stewart static void
3104c105859eSRandall Stewart sctp_check_for_revoked(struct sctp_tcb *stcb,
3105c105859eSRandall Stewart     struct sctp_association *asoc, uint32_t cumack,
310663eda93dSMichael Tuexen     uint32_t biggest_tsn_acked)
3107f8829a4aSRandall Stewart {
3108f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
3109f8829a4aSRandall Stewart 	int tot_revoked = 0;
3110f8829a4aSRandall Stewart 
31114a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
311220b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, cumack)) {
3113f8829a4aSRandall Stewart 			/*
3114f8829a4aSRandall Stewart 			 * ok this guy is either ACK or MARKED. If it is
3115f8829a4aSRandall Stewart 			 * ACKED it has been previously acked but not this
3116f8829a4aSRandall Stewart 			 * time i.e. revoked.  If it is MARKED it was ACK'ed
3117f8829a4aSRandall Stewart 			 * again.
3118f8829a4aSRandall Stewart 			 */
311920b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, biggest_tsn_acked)) {
3120d06c82f1SRandall Stewart 				break;
312120b07a4dSMichael Tuexen 			}
3122f8829a4aSRandall Stewart 			if (tp1->sent == SCTP_DATAGRAM_ACKED) {
3123f8829a4aSRandall Stewart 				/* it has been revoked */
3124f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_SENT;
3125f8829a4aSRandall Stewart 				tp1->rec.data.chunk_was_revoked = 1;
3126a5d547adSRandall Stewart 				/*
312742551e99SRandall Stewart 				 * We must add this stuff back in to assure
312842551e99SRandall Stewart 				 * timers and such get started.
3129a5d547adSRandall Stewart 				 */
3130b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3131c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
3132c105859eSRandall Stewart 					    tp1->whoTo->flight_size,
3133c105859eSRandall Stewart 					    tp1->book_size,
3134c105859eSRandall Stewart 					    (uintptr_t) tp1->whoTo,
3135c105859eSRandall Stewart 					    tp1->rec.data.TSN_seq);
313680fefe0aSRandall Stewart 				}
3137c105859eSRandall Stewart 				sctp_flight_size_increase(tp1);
3138c105859eSRandall Stewart 				sctp_total_flight_increase(stcb, tp1);
313942551e99SRandall Stewart 				/*
314042551e99SRandall Stewart 				 * We inflate the cwnd to compensate for our
314142551e99SRandall Stewart 				 * artificial inflation of the flight_size.
314242551e99SRandall Stewart 				 */
314342551e99SRandall Stewart 				tp1->whoTo->cwnd += tp1->book_size;
3144f8829a4aSRandall Stewart 				tot_revoked++;
3145b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3146f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
3147f8829a4aSRandall Stewart 					    cumack,
3148f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
3149f8829a4aSRandall Stewart 					    0,
3150f8829a4aSRandall Stewart 					    0,
3151f8829a4aSRandall Stewart 					    SCTP_LOG_TSN_REVOKED);
315280fefe0aSRandall Stewart 				}
3153f8829a4aSRandall Stewart 			} else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
3154f8829a4aSRandall Stewart 				/* it has been re-acked in this SACK */
3155f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
3156f8829a4aSRandall Stewart 			}
3157f8829a4aSRandall Stewart 		}
3158f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT)
3159f8829a4aSRandall Stewart 			break;
3160f8829a4aSRandall Stewart 	}
3161f8829a4aSRandall Stewart }
3162f8829a4aSRandall Stewart 
3163830d754dSRandall Stewart 
3164f8829a4aSRandall Stewart static void
3165f8829a4aSRandall Stewart sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
316663eda93dSMichael Tuexen     uint32_t biggest_tsn_acked, uint32_t biggest_tsn_newly_acked, uint32_t this_sack_lowest_newack, int accum_moved)
3167f8829a4aSRandall Stewart {
3168f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
3169f8829a4aSRandall Stewart 	int strike_flag = 0;
3170f8829a4aSRandall Stewart 	struct timeval now;
3171f8829a4aSRandall Stewart 	int tot_retrans = 0;
3172f8829a4aSRandall Stewart 	uint32_t sending_seq;
3173f8829a4aSRandall Stewart 	struct sctp_nets *net;
3174f8829a4aSRandall Stewart 	int num_dests_sacked = 0;
3175f8829a4aSRandall Stewart 
3176f8829a4aSRandall Stewart 	/*
3177f8829a4aSRandall Stewart 	 * select the sending_seq, this is either the next thing ready to be
3178f8829a4aSRandall Stewart 	 * sent but not transmitted, OR, the next seq we assign.
3179f8829a4aSRandall Stewart 	 */
3180f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
3181f8829a4aSRandall Stewart 	if (tp1 == NULL) {
3182f8829a4aSRandall Stewart 		sending_seq = asoc->sending_seq;
3183f8829a4aSRandall Stewart 	} else {
3184f8829a4aSRandall Stewart 		sending_seq = tp1->rec.data.TSN_seq;
3185f8829a4aSRandall Stewart 	}
3186f8829a4aSRandall Stewart 
3187f8829a4aSRandall Stewart 	/* CMT DAC algo: finding out if SACK is a mixed SACK */
31887c99d56fSMichael Tuexen 	if ((asoc->sctp_cmt_on_off > 0) &&
318920083c2eSMichael Tuexen 	    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3190f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3191f8829a4aSRandall Stewart 			if (net->saw_newack)
3192f8829a4aSRandall Stewart 				num_dests_sacked++;
3193f8829a4aSRandall Stewart 		}
3194f8829a4aSRandall Stewart 	}
3195f8829a4aSRandall Stewart 	if (stcb->asoc.peer_supports_prsctp) {
31966e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
3197f8829a4aSRandall Stewart 	}
31984a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
3199f8829a4aSRandall Stewart 		strike_flag = 0;
3200f8829a4aSRandall Stewart 		if (tp1->no_fr_allowed) {
3201f8829a4aSRandall Stewart 			/* this one had a timeout or something */
3202f8829a4aSRandall Stewart 			continue;
3203f8829a4aSRandall Stewart 		}
3204b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3205f8829a4aSRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND)
3206f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3207f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3208f8829a4aSRandall Stewart 				    tp1->sent,
3209f8829a4aSRandall Stewart 				    SCTP_FR_LOG_CHECK_STRIKE);
321080fefe0aSRandall Stewart 		}
321120b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, biggest_tsn_acked) ||
3212f8829a4aSRandall Stewart 		    tp1->sent == SCTP_DATAGRAM_UNSENT) {
3213f8829a4aSRandall Stewart 			/* done */
3214f8829a4aSRandall Stewart 			break;
3215f8829a4aSRandall Stewart 		}
3216f8829a4aSRandall Stewart 		if (stcb->asoc.peer_supports_prsctp) {
3217f8829a4aSRandall Stewart 			if ((PR_SCTP_TTL_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3218f8829a4aSRandall Stewart 				/* Is it expired? */
321999ddc825SMichael Tuexen 				if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3220f8829a4aSRandall Stewart 					/* Yes so drop it */
3221f8829a4aSRandall Stewart 					if (tp1->data != NULL) {
3222ad81507eSRandall Stewart 						(void)sctp_release_pr_sctp_chunk(stcb, tp1,
3223f8829a4aSRandall Stewart 						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
32240c0982b8SRandall Stewart 						    SCTP_SO_NOT_LOCKED);
3225f8829a4aSRandall Stewart 					}
3226f8829a4aSRandall Stewart 					continue;
3227f8829a4aSRandall Stewart 				}
3228f8829a4aSRandall Stewart 			}
3229f8829a4aSRandall Stewart 		}
323020b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, asoc->this_sack_highest_gap)) {
3231f8829a4aSRandall Stewart 			/* we are beyond the tsn in the sack  */
3232f8829a4aSRandall Stewart 			break;
3233f8829a4aSRandall Stewart 		}
3234f8829a4aSRandall Stewart 		if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
3235f8829a4aSRandall Stewart 			/* either a RESEND, ACKED, or MARKED */
3236f8829a4aSRandall Stewart 			/* skip */
323744fbe462SRandall Stewart 			if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
323844fbe462SRandall Stewart 				/* Continue strikin FWD-TSN chunks */
323944fbe462SRandall Stewart 				tp1->rec.data.fwd_tsn_cnt++;
324044fbe462SRandall Stewart 			}
3241f8829a4aSRandall Stewart 			continue;
3242f8829a4aSRandall Stewart 		}
3243f8829a4aSRandall Stewart 		/*
3244f8829a4aSRandall Stewart 		 * CMT : SFR algo (covers part of DAC and HTNA as well)
3245f8829a4aSRandall Stewart 		 */
3246ad81507eSRandall Stewart 		if (tp1->whoTo && tp1->whoTo->saw_newack == 0) {
3247f8829a4aSRandall Stewart 			/*
3248f8829a4aSRandall Stewart 			 * No new acks were receieved for data sent to this
3249f8829a4aSRandall Stewart 			 * dest. Therefore, according to the SFR algo for
3250f8829a4aSRandall Stewart 			 * CMT, no data sent to this dest can be marked for
3251c105859eSRandall Stewart 			 * FR using this SACK.
3252f8829a4aSRandall Stewart 			 */
3253f8829a4aSRandall Stewart 			continue;
325420b07a4dSMichael Tuexen 		} else if (tp1->whoTo && SCTP_TSN_GT(tp1->rec.data.TSN_seq,
325520b07a4dSMichael Tuexen 		    tp1->whoTo->this_sack_highest_newack)) {
3256f8829a4aSRandall Stewart 			/*
3257f8829a4aSRandall Stewart 			 * CMT: New acks were receieved for data sent to
3258f8829a4aSRandall Stewart 			 * this dest. But no new acks were seen for data
3259f8829a4aSRandall Stewart 			 * sent after tp1. Therefore, according to the SFR
3260f8829a4aSRandall Stewart 			 * algo for CMT, tp1 cannot be marked for FR using
3261f8829a4aSRandall Stewart 			 * this SACK. This step covers part of the DAC algo
3262f8829a4aSRandall Stewart 			 * and the HTNA algo as well.
3263f8829a4aSRandall Stewart 			 */
3264f8829a4aSRandall Stewart 			continue;
3265f8829a4aSRandall Stewart 		}
3266f8829a4aSRandall Stewart 		/*
3267f8829a4aSRandall Stewart 		 * Here we check to see if we were have already done a FR
3268f8829a4aSRandall Stewart 		 * and if so we see if the biggest TSN we saw in the sack is
3269f8829a4aSRandall Stewart 		 * smaller than the recovery point. If so we don't strike
3270f8829a4aSRandall Stewart 		 * the tsn... otherwise we CAN strike the TSN.
3271f8829a4aSRandall Stewart 		 */
3272f8829a4aSRandall Stewart 		/*
327342551e99SRandall Stewart 		 * @@@ JRI: Check for CMT if (accum_moved &&
327442551e99SRandall Stewart 		 * asoc->fast_retran_loss_recovery && (sctp_cmt_on_off ==
327542551e99SRandall Stewart 		 * 0)) {
3276f8829a4aSRandall Stewart 		 */
327742551e99SRandall Stewart 		if (accum_moved && asoc->fast_retran_loss_recovery) {
3278f8829a4aSRandall Stewart 			/*
3279f8829a4aSRandall Stewart 			 * Strike the TSN if in fast-recovery and cum-ack
3280f8829a4aSRandall Stewart 			 * moved.
3281f8829a4aSRandall Stewart 			 */
3282b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3283f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3284f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3285f8829a4aSRandall Stewart 				    tp1->sent,
3286f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
328780fefe0aSRandall Stewart 			}
32885e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3289f8829a4aSRandall Stewart 				tp1->sent++;
32905e54f665SRandall Stewart 			}
32917c99d56fSMichael Tuexen 			if ((asoc->sctp_cmt_on_off > 0) &&
329220083c2eSMichael Tuexen 			    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3293f8829a4aSRandall Stewart 				/*
3294f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
3295f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
3296f8829a4aSRandall Stewart 				 * because it would have been set to the
3297f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
3298f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
3299f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
3300c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
3301c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3302c105859eSRandall Stewart 				 * indicates that two packets have been
3303c105859eSRandall Stewart 				 * received after this missing TSN.
33045e54f665SRandall Stewart 				 */
33055e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
330620b07a4dSMichael Tuexen 				    SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.TSN_seq)) {
3307b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3308f8829a4aSRandall Stewart 						sctp_log_fr(16 + num_dests_sacked,
3309f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3310f8829a4aSRandall Stewart 						    tp1->sent,
3311f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
331280fefe0aSRandall Stewart 					}
3313f8829a4aSRandall Stewart 					tp1->sent++;
3314f8829a4aSRandall Stewart 				}
3315f8829a4aSRandall Stewart 			}
331620083c2eSMichael Tuexen 		} else if ((tp1->rec.data.doing_fast_retransmit) &&
331720083c2eSMichael Tuexen 		    (asoc->sctp_cmt_on_off == 0)) {
3318f8829a4aSRandall Stewart 			/*
3319f8829a4aSRandall Stewart 			 * For those that have done a FR we must take
3320f8829a4aSRandall Stewart 			 * special consideration if we strike. I.e the
3321f8829a4aSRandall Stewart 			 * biggest_newly_acked must be higher than the
3322f8829a4aSRandall Stewart 			 * sending_seq at the time we did the FR.
3323f8829a4aSRandall Stewart 			 */
33245e54f665SRandall Stewart 			if (
3325f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3326f8829a4aSRandall Stewart 			/*
3327f8829a4aSRandall Stewart 			 * If FR's go to new networks, then we must only do
3328f8829a4aSRandall Stewart 			 * this for singly homed asoc's. However if the FR's
3329f8829a4aSRandall Stewart 			 * go to the same network (Armando's work) then its
3330f8829a4aSRandall Stewart 			 * ok to FR multiple times.
3331f8829a4aSRandall Stewart 			 */
33325e54f665SRandall Stewart 			    (asoc->numnets < 2)
3333f8829a4aSRandall Stewart #else
33345e54f665SRandall Stewart 			    (1)
3335f8829a4aSRandall Stewart #endif
33365e54f665SRandall Stewart 			    ) {
33375e54f665SRandall Stewart 
333820b07a4dSMichael Tuexen 				if (SCTP_TSN_GE(biggest_tsn_newly_acked,
3339f8829a4aSRandall Stewart 				    tp1->rec.data.fast_retran_tsn)) {
3340f8829a4aSRandall Stewart 					/*
3341f8829a4aSRandall Stewart 					 * Strike the TSN, since this ack is
3342f8829a4aSRandall Stewart 					 * beyond where things were when we
3343f8829a4aSRandall Stewart 					 * did a FR.
3344f8829a4aSRandall Stewart 					 */
3345b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3346f8829a4aSRandall Stewart 						sctp_log_fr(biggest_tsn_newly_acked,
3347f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3348f8829a4aSRandall Stewart 						    tp1->sent,
3349f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
335080fefe0aSRandall Stewart 					}
33515e54f665SRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3352f8829a4aSRandall Stewart 						tp1->sent++;
33535e54f665SRandall Stewart 					}
3354f8829a4aSRandall Stewart 					strike_flag = 1;
33557c99d56fSMichael Tuexen 					if ((asoc->sctp_cmt_on_off > 0) &&
335620083c2eSMichael Tuexen 					    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3357f8829a4aSRandall Stewart 						/*
3358f8829a4aSRandall Stewart 						 * CMT DAC algorithm: If
3359f8829a4aSRandall Stewart 						 * SACK flag is set to 0,
3360f8829a4aSRandall Stewart 						 * then lowest_newack test
3361f8829a4aSRandall Stewart 						 * will not pass because it
3362f8829a4aSRandall Stewart 						 * would have been set to
3363f8829a4aSRandall Stewart 						 * the cumack earlier. If
3364f8829a4aSRandall Stewart 						 * not already to be rtx'd,
3365f8829a4aSRandall Stewart 						 * If not a mixed sack and
3366f8829a4aSRandall Stewart 						 * if tp1 is not between two
3367f8829a4aSRandall Stewart 						 * sacked TSNs, then mark by
3368c105859eSRandall Stewart 						 * one more. NOTE that we
3369c105859eSRandall Stewart 						 * are marking by one
3370c105859eSRandall Stewart 						 * additional time since the
3371c105859eSRandall Stewart 						 * SACK DAC flag indicates
3372c105859eSRandall Stewart 						 * that two packets have
3373c105859eSRandall Stewart 						 * been received after this
3374c105859eSRandall Stewart 						 * missing TSN.
3375f8829a4aSRandall Stewart 						 */
33765e54f665SRandall Stewart 						if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
33775e54f665SRandall Stewart 						    (num_dests_sacked == 1) &&
337820b07a4dSMichael Tuexen 						    SCTP_TSN_GT(this_sack_lowest_newack,
337920b07a4dSMichael Tuexen 						    tp1->rec.data.TSN_seq)) {
3380b3f1ea41SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3381f8829a4aSRandall Stewart 								sctp_log_fr(32 + num_dests_sacked,
3382f8829a4aSRandall Stewart 								    tp1->rec.data.TSN_seq,
3383f8829a4aSRandall Stewart 								    tp1->sent,
3384f8829a4aSRandall Stewart 								    SCTP_FR_LOG_STRIKE_CHUNK);
338580fefe0aSRandall Stewart 							}
33865e54f665SRandall Stewart 							if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3387f8829a4aSRandall Stewart 								tp1->sent++;
33885e54f665SRandall Stewart 							}
3389f8829a4aSRandall Stewart 						}
3390f8829a4aSRandall Stewart 					}
3391f8829a4aSRandall Stewart 				}
3392f8829a4aSRandall Stewart 			}
3393f8829a4aSRandall Stewart 			/*
339442551e99SRandall Stewart 			 * JRI: TODO: remove code for HTNA algo. CMT's SFR
339542551e99SRandall Stewart 			 * algo covers HTNA.
3396f8829a4aSRandall Stewart 			 */
339720b07a4dSMichael Tuexen 		} else if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
339820b07a4dSMichael Tuexen 		    biggest_tsn_newly_acked)) {
3399f8829a4aSRandall Stewart 			/*
3400f8829a4aSRandall Stewart 			 * We don't strike these: This is the  HTNA
3401f8829a4aSRandall Stewart 			 * algorithm i.e. we don't strike If our TSN is
3402f8829a4aSRandall Stewart 			 * larger than the Highest TSN Newly Acked.
3403f8829a4aSRandall Stewart 			 */
3404f8829a4aSRandall Stewart 			;
3405f8829a4aSRandall Stewart 		} else {
3406f8829a4aSRandall Stewart 			/* Strike the TSN */
3407b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3408f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3409f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3410f8829a4aSRandall Stewart 				    tp1->sent,
3411f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
341280fefe0aSRandall Stewart 			}
34135e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3414f8829a4aSRandall Stewart 				tp1->sent++;
34155e54f665SRandall Stewart 			}
34167c99d56fSMichael Tuexen 			if ((asoc->sctp_cmt_on_off > 0) &&
341720083c2eSMichael Tuexen 			    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3418f8829a4aSRandall Stewart 				/*
3419f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
3420f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
3421f8829a4aSRandall Stewart 				 * because it would have been set to the
3422f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
3423f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
3424f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
3425c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
3426c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3427c105859eSRandall Stewart 				 * indicates that two packets have been
3428c105859eSRandall Stewart 				 * received after this missing TSN.
3429f8829a4aSRandall Stewart 				 */
34305e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
343120b07a4dSMichael Tuexen 				    SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.TSN_seq)) {
3432b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3433f8829a4aSRandall Stewart 						sctp_log_fr(48 + num_dests_sacked,
3434f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3435f8829a4aSRandall Stewart 						    tp1->sent,
3436f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
343780fefe0aSRandall Stewart 					}
3438f8829a4aSRandall Stewart 					tp1->sent++;
3439f8829a4aSRandall Stewart 				}
3440f8829a4aSRandall Stewart 			}
3441f8829a4aSRandall Stewart 		}
3442f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3443f8829a4aSRandall Stewart 			struct sctp_nets *alt;
3444f8829a4aSRandall Stewart 
3445544e35bdSRandall Stewart 			/* fix counts and things */
3446544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3447544e35bdSRandall Stewart 				sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND,
3448544e35bdSRandall Stewart 				    (tp1->whoTo ? (tp1->whoTo->flight_size) : 0),
3449544e35bdSRandall Stewart 				    tp1->book_size,
3450544e35bdSRandall Stewart 				    (uintptr_t) tp1->whoTo,
3451544e35bdSRandall Stewart 				    tp1->rec.data.TSN_seq);
3452544e35bdSRandall Stewart 			}
3453544e35bdSRandall Stewart 			if (tp1->whoTo) {
3454544e35bdSRandall Stewart 				tp1->whoTo->net_ack++;
3455544e35bdSRandall Stewart 				sctp_flight_size_decrease(tp1);
3456299108c5SRandall Stewart 				if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3457299108c5SRandall Stewart 					(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3458299108c5SRandall Stewart 					    tp1);
3459299108c5SRandall Stewart 				}
3460544e35bdSRandall Stewart 			}
3461544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
3462544e35bdSRandall Stewart 				sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
3463544e35bdSRandall Stewart 				    asoc->peers_rwnd, tp1->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3464544e35bdSRandall Stewart 			}
3465544e35bdSRandall Stewart 			/* add back to the rwnd */
3466544e35bdSRandall Stewart 			asoc->peers_rwnd += (tp1->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3467544e35bdSRandall Stewart 
3468544e35bdSRandall Stewart 			/* remove from the total flight */
3469544e35bdSRandall Stewart 			sctp_total_flight_decrease(stcb, tp1);
3470544e35bdSRandall Stewart 
3471475d0674SMichael Tuexen 			if ((stcb->asoc.peer_supports_prsctp) &&
3472475d0674SMichael Tuexen 			    (PR_SCTP_RTX_ENABLED(tp1->flags))) {
3473475d0674SMichael Tuexen 				/*
3474475d0674SMichael Tuexen 				 * Has it been retransmitted tv_sec times? -
3475475d0674SMichael Tuexen 				 * we store the retran count there.
3476475d0674SMichael Tuexen 				 */
3477475d0674SMichael Tuexen 				if (tp1->snd_count > tp1->rec.data.timetodrop.tv_sec) {
3478475d0674SMichael Tuexen 					/* Yes, so drop it */
3479475d0674SMichael Tuexen 					if (tp1->data != NULL) {
3480475d0674SMichael Tuexen 						(void)sctp_release_pr_sctp_chunk(stcb, tp1,
3481475d0674SMichael Tuexen 						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3482475d0674SMichael Tuexen 						    SCTP_SO_NOT_LOCKED);
3483475d0674SMichael Tuexen 					}
3484475d0674SMichael Tuexen 					/* Make sure to flag we had a FR */
3485475d0674SMichael Tuexen 					tp1->whoTo->net_ack++;
3486475d0674SMichael Tuexen 					continue;
3487475d0674SMichael Tuexen 				}
3488475d0674SMichael Tuexen 			}
3489f8829a4aSRandall Stewart 			/* printf("OK, we are now ready to FR this guy\n"); */
3490b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3491f8829a4aSRandall Stewart 				sctp_log_fr(tp1->rec.data.TSN_seq, tp1->snd_count,
3492f8829a4aSRandall Stewart 				    0, SCTP_FR_MARKED);
349380fefe0aSRandall Stewart 			}
3494f8829a4aSRandall Stewart 			if (strike_flag) {
3495f8829a4aSRandall Stewart 				/* This is a subsequent FR */
3496f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_sendmultfastretrans);
3497f8829a4aSRandall Stewart 			}
34985e54f665SRandall Stewart 			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
34997c99d56fSMichael Tuexen 			if (asoc->sctp_cmt_on_off > 0) {
3500f8829a4aSRandall Stewart 				/*
3501f8829a4aSRandall Stewart 				 * CMT: Using RTX_SSTHRESH policy for CMT.
3502f8829a4aSRandall Stewart 				 * If CMT is being used, then pick dest with
3503f8829a4aSRandall Stewart 				 * largest ssthresh for any retransmission.
3504f8829a4aSRandall Stewart 				 */
3505f8829a4aSRandall Stewart 				tp1->no_fr_allowed = 1;
3506f8829a4aSRandall Stewart 				alt = tp1->whoTo;
35073c503c28SRandall Stewart 				/* sa_ignore NO_NULL_CHK */
350820083c2eSMichael Tuexen 				if (asoc->sctp_cmt_pf > 0) {
3509b54d3a6cSRandall Stewart 					/*
3510b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If CMT PF is on,
3511b54d3a6cSRandall Stewart 					 * use the PF version of
3512b54d3a6cSRandall Stewart 					 * find_alt_net()
3513b54d3a6cSRandall Stewart 					 */
3514b54d3a6cSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 2);
3515b54d3a6cSRandall Stewart 				} else {
3516b54d3a6cSRandall Stewart 					/*
3517b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If only CMT is on,
3518b54d3a6cSRandall Stewart 					 * use the CMT version of
3519b54d3a6cSRandall Stewart 					 * find_alt_net()
3520b54d3a6cSRandall Stewart 					 */
352152be287eSRandall Stewart 					/* sa_ignore NO_NULL_CHK */
3522f8829a4aSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 1);
3523b54d3a6cSRandall Stewart 				}
3524ad81507eSRandall Stewart 				if (alt == NULL) {
3525ad81507eSRandall Stewart 					alt = tp1->whoTo;
3526ad81507eSRandall Stewart 				}
3527f8829a4aSRandall Stewart 				/*
3528f8829a4aSRandall Stewart 				 * CUCv2: If a different dest is picked for
3529f8829a4aSRandall Stewart 				 * the retransmission, then new
3530f8829a4aSRandall Stewart 				 * (rtx-)pseudo_cumack needs to be tracked
3531f8829a4aSRandall Stewart 				 * for orig dest. Let CUCv2 track new (rtx-)
3532f8829a4aSRandall Stewart 				 * pseudo-cumack always.
3533f8829a4aSRandall Stewart 				 */
3534ad81507eSRandall Stewart 				if (tp1->whoTo) {
3535f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
3536f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
3537ad81507eSRandall Stewart 				}
3538f8829a4aSRandall Stewart 			} else {/* CMT is OFF */
3539f8829a4aSRandall Stewart 
3540f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3541f8829a4aSRandall Stewart 				/* Can we find an alternate? */
3542f8829a4aSRandall Stewart 				alt = sctp_find_alternate_net(stcb, tp1->whoTo, 0);
3543f8829a4aSRandall Stewart #else
3544f8829a4aSRandall Stewart 				/*
3545f8829a4aSRandall Stewart 				 * default behavior is to NOT retransmit
3546f8829a4aSRandall Stewart 				 * FR's to an alternate. Armando Caro's
3547f8829a4aSRandall Stewart 				 * paper details why.
3548f8829a4aSRandall Stewart 				 */
3549f8829a4aSRandall Stewart 				alt = tp1->whoTo;
3550f8829a4aSRandall Stewart #endif
3551f8829a4aSRandall Stewart 			}
3552f8829a4aSRandall Stewart 
3553f8829a4aSRandall Stewart 			tp1->rec.data.doing_fast_retransmit = 1;
3554f8829a4aSRandall Stewart 			tot_retrans++;
3555f8829a4aSRandall Stewart 			/* mark the sending seq for possible subsequent FR's */
3556f8829a4aSRandall Stewart 			/*
3557f8829a4aSRandall Stewart 			 * printf("Marking TSN for FR new value %x\n",
3558f8829a4aSRandall Stewart 			 * (uint32_t)tpi->rec.data.TSN_seq);
3559f8829a4aSRandall Stewart 			 */
3560f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->send_queue)) {
3561f8829a4aSRandall Stewart 				/*
3562f8829a4aSRandall Stewart 				 * If the queue of send is empty then its
3563f8829a4aSRandall Stewart 				 * the next sequence number that will be
3564f8829a4aSRandall Stewart 				 * assigned so we subtract one from this to
3565f8829a4aSRandall Stewart 				 * get the one we last sent.
3566f8829a4aSRandall Stewart 				 */
3567f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn = sending_seq;
3568f8829a4aSRandall Stewart 			} else {
3569f8829a4aSRandall Stewart 				/*
3570f8829a4aSRandall Stewart 				 * If there are chunks on the send queue
3571f8829a4aSRandall Stewart 				 * (unsent data that has made it from the
3572f8829a4aSRandall Stewart 				 * stream queues but not out the door, we
3573f8829a4aSRandall Stewart 				 * take the first one (which will have the
3574f8829a4aSRandall Stewart 				 * lowest TSN) and subtract one to get the
3575f8829a4aSRandall Stewart 				 * one we last sent.
3576f8829a4aSRandall Stewart 				 */
3577f8829a4aSRandall Stewart 				struct sctp_tmit_chunk *ttt;
3578f8829a4aSRandall Stewart 
3579f8829a4aSRandall Stewart 				ttt = TAILQ_FIRST(&asoc->send_queue);
3580f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn =
3581f8829a4aSRandall Stewart 				    ttt->rec.data.TSN_seq;
3582f8829a4aSRandall Stewart 			}
3583f8829a4aSRandall Stewart 
3584f8829a4aSRandall Stewart 			if (tp1->do_rtt) {
3585f8829a4aSRandall Stewart 				/*
3586f8829a4aSRandall Stewart 				 * this guy had a RTO calculation pending on
3587f8829a4aSRandall Stewart 				 * it, cancel it
3588f8829a4aSRandall Stewart 				 */
3589f79aab18SRandall Stewart 				if (tp1->whoTo->rto_needed == 0) {
3590f79aab18SRandall Stewart 					tp1->whoTo->rto_needed = 1;
3591f79aab18SRandall Stewart 				}
3592f8829a4aSRandall Stewart 				tp1->do_rtt = 0;
3593f8829a4aSRandall Stewart 			}
3594f8829a4aSRandall Stewart 			if (alt != tp1->whoTo) {
3595f8829a4aSRandall Stewart 				/* yes, there is an alternate. */
3596f8829a4aSRandall Stewart 				sctp_free_remote_addr(tp1->whoTo);
35973c503c28SRandall Stewart 				/* sa_ignore FREED_MEMORY */
3598f8829a4aSRandall Stewart 				tp1->whoTo = alt;
3599f8829a4aSRandall Stewart 				atomic_add_int(&alt->ref_count, 1);
3600f8829a4aSRandall Stewart 			}
3601f8829a4aSRandall Stewart 		}
36024a9ef3f8SMichael Tuexen 	}
3603f8829a4aSRandall Stewart }
3604f8829a4aSRandall Stewart 
3605f8829a4aSRandall Stewart struct sctp_tmit_chunk *
3606f8829a4aSRandall Stewart sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
3607f8829a4aSRandall Stewart     struct sctp_association *asoc)
3608f8829a4aSRandall Stewart {
3609f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2, *a_adv = NULL;
3610f8829a4aSRandall Stewart 	struct timeval now;
3611f8829a4aSRandall Stewart 	int now_filled = 0;
3612f8829a4aSRandall Stewart 
3613f8829a4aSRandall Stewart 	if (asoc->peer_supports_prsctp == 0) {
3614f8829a4aSRandall Stewart 		return (NULL);
3615f8829a4aSRandall Stewart 	}
36164a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
3617f8829a4aSRandall Stewart 		if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
3618f8829a4aSRandall Stewart 		    tp1->sent != SCTP_DATAGRAM_RESEND) {
3619f8829a4aSRandall Stewart 			/* no chance to advance, out of here */
3620f8829a4aSRandall Stewart 			break;
3621f8829a4aSRandall Stewart 		}
36220c0982b8SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
36230c0982b8SRandall Stewart 			if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
36240c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
36250c0982b8SRandall Stewart 				    asoc->advanced_peer_ack_point,
36260c0982b8SRandall Stewart 				    tp1->rec.data.TSN_seq, 0, 0);
36270c0982b8SRandall Stewart 			}
36280c0982b8SRandall Stewart 		}
3629f8829a4aSRandall Stewart 		if (!PR_SCTP_ENABLED(tp1->flags)) {
3630f8829a4aSRandall Stewart 			/*
3631f8829a4aSRandall Stewart 			 * We can't fwd-tsn past any that are reliable aka
3632f8829a4aSRandall Stewart 			 * retransmitted until the asoc fails.
3633f8829a4aSRandall Stewart 			 */
3634f8829a4aSRandall Stewart 			break;
3635f8829a4aSRandall Stewart 		}
3636f8829a4aSRandall Stewart 		if (!now_filled) {
36376e55db54SRandall Stewart 			(void)SCTP_GETTIME_TIMEVAL(&now);
3638f8829a4aSRandall Stewart 			now_filled = 1;
3639f8829a4aSRandall Stewart 		}
3640f8829a4aSRandall Stewart 		/*
3641f8829a4aSRandall Stewart 		 * now we got a chunk which is marked for another
3642f8829a4aSRandall Stewart 		 * retransmission to a PR-stream but has run out its chances
3643f8829a4aSRandall Stewart 		 * already maybe OR has been marked to skip now. Can we skip
3644f8829a4aSRandall Stewart 		 * it if its a resend?
3645f8829a4aSRandall Stewart 		 */
3646f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND &&
3647f8829a4aSRandall Stewart 		    (PR_SCTP_TTL_ENABLED(tp1->flags))) {
3648f8829a4aSRandall Stewart 			/*
3649f8829a4aSRandall Stewart 			 * Now is this one marked for resend and its time is
3650f8829a4aSRandall Stewart 			 * now up?
3651f8829a4aSRandall Stewart 			 */
3652f8829a4aSRandall Stewart 			if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3653f8829a4aSRandall Stewart 				/* Yes so drop it */
3654f8829a4aSRandall Stewart 				if (tp1->data) {
3655ad81507eSRandall Stewart 					(void)sctp_release_pr_sctp_chunk(stcb, tp1,
3656f8829a4aSRandall Stewart 					    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
36570c0982b8SRandall Stewart 					    SCTP_SO_NOT_LOCKED);
3658f8829a4aSRandall Stewart 				}
3659f8829a4aSRandall Stewart 			} else {
3660f8829a4aSRandall Stewart 				/*
3661f8829a4aSRandall Stewart 				 * No, we are done when hit one for resend
3662f8829a4aSRandall Stewart 				 * whos time as not expired.
3663f8829a4aSRandall Stewart 				 */
3664f8829a4aSRandall Stewart 				break;
3665f8829a4aSRandall Stewart 			}
3666f8829a4aSRandall Stewart 		}
3667f8829a4aSRandall Stewart 		/*
3668f8829a4aSRandall Stewart 		 * Ok now if this chunk is marked to drop it we can clean up
3669f8829a4aSRandall Stewart 		 * the chunk, advance our peer ack point and we can check
3670f8829a4aSRandall Stewart 		 * the next chunk.
3671f8829a4aSRandall Stewart 		 */
367244fbe462SRandall Stewart 		if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
3673f8829a4aSRandall Stewart 			/* advance PeerAckPoint goes forward */
367420b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, asoc->advanced_peer_ack_point)) {
3675f8829a4aSRandall Stewart 				asoc->advanced_peer_ack_point = tp1->rec.data.TSN_seq;
3676f8829a4aSRandall Stewart 				a_adv = tp1;
36770c0982b8SRandall Stewart 			} else if (tp1->rec.data.TSN_seq == asoc->advanced_peer_ack_point) {
36780c0982b8SRandall Stewart 				/* No update but we do save the chk */
36790c0982b8SRandall Stewart 				a_adv = tp1;
36800c0982b8SRandall Stewart 			}
3681f8829a4aSRandall Stewart 		} else {
3682f8829a4aSRandall Stewart 			/*
3683f8829a4aSRandall Stewart 			 * If it is still in RESEND we can advance no
3684f8829a4aSRandall Stewart 			 * further
3685f8829a4aSRandall Stewart 			 */
3686f8829a4aSRandall Stewart 			break;
3687f8829a4aSRandall Stewart 		}
3688f8829a4aSRandall Stewart 	}
3689f8829a4aSRandall Stewart 	return (a_adv);
3690f8829a4aSRandall Stewart }
3691f8829a4aSRandall Stewart 
36920c0982b8SRandall Stewart static int
3693c105859eSRandall Stewart sctp_fs_audit(struct sctp_association *asoc)
3694bff64a4dSRandall Stewart {
3695bff64a4dSRandall Stewart 	struct sctp_tmit_chunk *chk;
3696bff64a4dSRandall Stewart 	int inflight = 0, resend = 0, inbetween = 0, acked = 0, above = 0;
36970c0982b8SRandall Stewart 	int entry_flight, entry_cnt, ret;
36980c0982b8SRandall Stewart 
36990c0982b8SRandall Stewart 	entry_flight = asoc->total_flight;
37000c0982b8SRandall Stewart 	entry_cnt = asoc->total_flight_count;
37010c0982b8SRandall Stewart 	ret = 0;
37020c0982b8SRandall Stewart 
37030c0982b8SRandall Stewart 	if (asoc->pr_sctp_cnt >= asoc->sent_queue_cnt)
37040c0982b8SRandall Stewart 		return (0);
3705bff64a4dSRandall Stewart 
3706bff64a4dSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
3707bff64a4dSRandall Stewart 		if (chk->sent < SCTP_DATAGRAM_RESEND) {
37080c0982b8SRandall Stewart 			printf("Chk TSN:%u size:%d inflight cnt:%d\n",
37090c0982b8SRandall Stewart 			    chk->rec.data.TSN_seq,
37100c0982b8SRandall Stewart 			    chk->send_size,
37110c0982b8SRandall Stewart 			    chk->snd_count
37120c0982b8SRandall Stewart 			    );
3713bff64a4dSRandall Stewart 			inflight++;
3714bff64a4dSRandall Stewart 		} else if (chk->sent == SCTP_DATAGRAM_RESEND) {
3715bff64a4dSRandall Stewart 			resend++;
3716bff64a4dSRandall Stewart 		} else if (chk->sent < SCTP_DATAGRAM_ACKED) {
3717bff64a4dSRandall Stewart 			inbetween++;
3718bff64a4dSRandall Stewart 		} else if (chk->sent > SCTP_DATAGRAM_ACKED) {
3719bff64a4dSRandall Stewart 			above++;
3720bff64a4dSRandall Stewart 		} else {
3721bff64a4dSRandall Stewart 			acked++;
3722bff64a4dSRandall Stewart 		}
3723bff64a4dSRandall Stewart 	}
3724f1f73e57SRandall Stewart 
3725c105859eSRandall Stewart 	if ((inflight > 0) || (inbetween > 0)) {
3726f1f73e57SRandall Stewart #ifdef INVARIANTS
3727c105859eSRandall Stewart 		panic("Flight size-express incorrect? \n");
3728f1f73e57SRandall Stewart #else
37290c0982b8SRandall Stewart 		printf("asoc->total_flight:%d cnt:%d\n",
37300c0982b8SRandall Stewart 		    entry_flight, entry_cnt);
37310c0982b8SRandall Stewart 
37320c0982b8SRandall Stewart 		SCTP_PRINTF("Flight size-express incorrect F:%d I:%d R:%d Ab:%d ACK:%d\n",
37330c0982b8SRandall Stewart 		    inflight, inbetween, resend, above, acked);
37340c0982b8SRandall Stewart 		ret = 1;
3735f1f73e57SRandall Stewart #endif
3736bff64a4dSRandall Stewart 	}
37370c0982b8SRandall Stewart 	return (ret);
3738c105859eSRandall Stewart }
3739c105859eSRandall Stewart 
3740c105859eSRandall Stewart 
3741c105859eSRandall Stewart static void
3742c105859eSRandall Stewart sctp_window_probe_recovery(struct sctp_tcb *stcb,
3743c105859eSRandall Stewart     struct sctp_association *asoc,
3744c105859eSRandall Stewart     struct sctp_nets *net,
3745c105859eSRandall Stewart     struct sctp_tmit_chunk *tp1)
3746c105859eSRandall Stewart {
3747dfb11ef8SRandall Stewart 	tp1->window_probe = 0;
37485171328bSRandall Stewart 	if ((tp1->sent >= SCTP_DATAGRAM_ACKED) || (tp1->data == NULL)) {
3749dfb11ef8SRandall Stewart 		/* TSN's skipped we do NOT move back. */
3750dfb11ef8SRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DWN_WP_FWD,
3751dfb11ef8SRandall Stewart 		    tp1->whoTo->flight_size,
3752dfb11ef8SRandall Stewart 		    tp1->book_size,
3753dfb11ef8SRandall Stewart 		    (uintptr_t) tp1->whoTo,
3754dfb11ef8SRandall Stewart 		    tp1->rec.data.TSN_seq);
3755dfb11ef8SRandall Stewart 		return;
3756dfb11ef8SRandall Stewart 	}
37575171328bSRandall Stewart 	/* First setup this by shrinking flight */
3758299108c5SRandall Stewart 	if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3759299108c5SRandall Stewart 		(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3760299108c5SRandall Stewart 		    tp1);
3761299108c5SRandall Stewart 	}
37625171328bSRandall Stewart 	sctp_flight_size_decrease(tp1);
37635171328bSRandall Stewart 	sctp_total_flight_decrease(stcb, tp1);
37645171328bSRandall Stewart 	/* Now mark for resend */
37655171328bSRandall Stewart 	tp1->sent = SCTP_DATAGRAM_RESEND;
3766791437b5SRandall Stewart 	sctp_ucount_incr(asoc->sent_queue_retran_cnt);
3767791437b5SRandall Stewart 
3768b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3769c105859eSRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_WP,
3770c105859eSRandall Stewart 		    tp1->whoTo->flight_size,
3771c105859eSRandall Stewart 		    tp1->book_size,
3772c105859eSRandall Stewart 		    (uintptr_t) tp1->whoTo,
3773c105859eSRandall Stewart 		    tp1->rec.data.TSN_seq);
377480fefe0aSRandall Stewart 	}
3775c105859eSRandall Stewart }
3776c105859eSRandall Stewart 
3777f8829a4aSRandall Stewart void
3778f8829a4aSRandall Stewart sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack,
3779899288aeSRandall Stewart     uint32_t rwnd, int *abort_now, int ecne_seen)
3780f8829a4aSRandall Stewart {
3781f8829a4aSRandall Stewart 	struct sctp_nets *net;
3782f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3783f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
37845e54f665SRandall Stewart 	uint32_t old_rwnd;
37855e54f665SRandall Stewart 	int win_probe_recovery = 0;
3786c105859eSRandall Stewart 	int win_probe_recovered = 0;
3787d06c82f1SRandall Stewart 	int j, done_once = 0;
3788f79aab18SRandall Stewart 	int rto_ok = 1;
3789f8829a4aSRandall Stewart 
3790b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
3791d06c82f1SRandall Stewart 		sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
3792d06c82f1SRandall Stewart 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
379380fefe0aSRandall Stewart 	}
3794f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
379518e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
379618e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
379718e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
379818e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
379918e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
380018e198d3SRandall Stewart 	}
380118e198d3SRandall Stewart #endif
3802f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
3803d06c82f1SRandall Stewart 	old_rwnd = asoc->peers_rwnd;
380420b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->last_acked_seq, cumack)) {
38055e54f665SRandall Stewart 		/* old ack */
38065e54f665SRandall Stewart 		return;
3807d06c82f1SRandall Stewart 	} else if (asoc->last_acked_seq == cumack) {
3808d06c82f1SRandall Stewart 		/* Window update sack */
3809d06c82f1SRandall Stewart 		asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
381044fbe462SRandall Stewart 		    (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
3811d06c82f1SRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
3812d06c82f1SRandall Stewart 			/* SWS sender side engages */
3813d06c82f1SRandall Stewart 			asoc->peers_rwnd = 0;
3814d06c82f1SRandall Stewart 		}
3815d06c82f1SRandall Stewart 		if (asoc->peers_rwnd > old_rwnd) {
3816d06c82f1SRandall Stewart 			goto again;
3817d06c82f1SRandall Stewart 		}
3818d06c82f1SRandall Stewart 		return;
38195e54f665SRandall Stewart 	}
3820f8829a4aSRandall Stewart 	/* First setup for CC stuff */
3821f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3822a21779f0SRandall Stewart 		if (SCTP_TSN_GT(cumack, net->cwr_window_tsn)) {
3823a21779f0SRandall Stewart 			/* Drag along the window_tsn for cwr's */
3824a21779f0SRandall Stewart 			net->cwr_window_tsn = cumack;
3825a21779f0SRandall Stewart 		}
3826f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
3827f8829a4aSRandall Stewart 		net->net_ack = 0;
3828f8829a4aSRandall Stewart 		net->net_ack2 = 0;
3829132dea7dSRandall Stewart 
3830132dea7dSRandall Stewart 		/*
3831132dea7dSRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
3832132dea7dSRandall Stewart 		 * SACK processing
3833132dea7dSRandall Stewart 		 */
3834132dea7dSRandall Stewart 		net->new_pseudo_cumack = 0;
3835132dea7dSRandall Stewart 		net->will_exit_fast_recovery = 0;
3836299108c5SRandall Stewart 		if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) {
3837299108c5SRandall Stewart 			(*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) (stcb, net);
3838299108c5SRandall Stewart 		}
3839f8829a4aSRandall Stewart 	}
3840b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
3841139bc87fSRandall Stewart 		uint32_t send_s;
3842139bc87fSRandall Stewart 
3843c105859eSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3844c105859eSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
3845c105859eSRandall Stewart 			    sctpchunk_listhead);
3846c105859eSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
3847139bc87fSRandall Stewart 		} else {
3848c105859eSRandall Stewart 			send_s = asoc->sending_seq;
3849139bc87fSRandall Stewart 		}
385020b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(cumack, send_s)) {
3851c105859eSRandall Stewart #ifndef INVARIANTS
3852139bc87fSRandall Stewart 			struct mbuf *oper;
3853139bc87fSRandall Stewart 
3854c105859eSRandall Stewart #endif
3855c105859eSRandall Stewart #ifdef INVARIANTS
3856c105859eSRandall Stewart 			panic("Impossible sack 1");
3857c105859eSRandall Stewart #else
3858b5c16493SMichael Tuexen 
3859139bc87fSRandall Stewart 			*abort_now = 1;
3860139bc87fSRandall Stewart 			/* XXX */
3861139bc87fSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3862139bc87fSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
3863139bc87fSRandall Stewart 			if (oper) {
3864139bc87fSRandall Stewart 				struct sctp_paramhdr *ph;
3865139bc87fSRandall Stewart 				uint32_t *ippp;
3866139bc87fSRandall Stewart 
3867139bc87fSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
3868139bc87fSRandall Stewart 				    sizeof(uint32_t);
3869139bc87fSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
3870139bc87fSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
3871139bc87fSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
3872139bc87fSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
3873139bc87fSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
3874139bc87fSRandall Stewart 			}
3875139bc87fSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
3876ceaad40aSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
3877139bc87fSRandall Stewart 			return;
3878139bc87fSRandall Stewart #endif
3879139bc87fSRandall Stewart 		}
3880139bc87fSRandall Stewart 	}
3881f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = cumack;
3882b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
3883c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
3884c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
3885c4739e2fSRandall Stewart 		    0,
3886c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
3887c4739e2fSRandall Stewart 		    __LINE__);
3888c4739e2fSRandall Stewart 	}
3889f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
389020b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cumack, asoc->last_acked_seq)) {
3891f8829a4aSRandall Stewart 		/* process the new consecutive TSN first */
38924a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
389320b07a4dSMichael Tuexen 			if (SCTP_TSN_GE(cumack, tp1->rec.data.TSN_seq)) {
389418e198d3SRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
389518e198d3SRandall Stewart 					printf("Warning, an unsent is now acked?\n");
389618e198d3SRandall Stewart 				}
3897f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
3898f8829a4aSRandall Stewart 					/*
389918e198d3SRandall Stewart 					 * If it is less than ACKED, it is
390018e198d3SRandall Stewart 					 * now no-longer in flight. Higher
390118e198d3SRandall Stewart 					 * values may occur during marking
3902f8829a4aSRandall Stewart 					 */
3903c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3904b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3905c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
3906a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
3907a5d547adSRandall Stewart 							    tp1->book_size,
3908c105859eSRandall Stewart 							    (uintptr_t) tp1->whoTo,
3909a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
391080fefe0aSRandall Stewart 						}
3911c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
3912299108c5SRandall Stewart 						if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3913299108c5SRandall Stewart 							(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3914299108c5SRandall Stewart 							    tp1);
3915299108c5SRandall Stewart 						}
391604ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
3917c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
3918f8829a4aSRandall Stewart 					}
3919f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
3920f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
3921f8829a4aSRandall Stewart 						/*
392218e198d3SRandall Stewart 						 * True non-retransmited
3923f8829a4aSRandall Stewart 						 * chunk
3924f8829a4aSRandall Stewart 						 */
3925f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
3926f8829a4aSRandall Stewart 						    tp1->send_size;
3927f8829a4aSRandall Stewart 
3928f8829a4aSRandall Stewart 						/* update RTO too? */
392962c1ff9cSRandall Stewart 						if (tp1->do_rtt) {
3930f79aab18SRandall Stewart 							if (rto_ok) {
3931f8829a4aSRandall Stewart 								tp1->whoTo->RTO =
393204ee05e8SRandall Stewart 								/*
393304ee05e8SRandall Stewart 								 * sa_ignore
3934f79aab18SRandall Stewart 								 * NO_NULL_CH
3935f79aab18SRandall Stewart 								 * K
393604ee05e8SRandall Stewart 								 */
3937f8829a4aSRandall Stewart 								    sctp_calculate_rto(stcb,
3938f8829a4aSRandall Stewart 								    asoc, tp1->whoTo,
393918e198d3SRandall Stewart 								    &tp1->sent_rcv_time,
3940899288aeSRandall Stewart 								    sctp_align_safe_nocopy,
3941f79aab18SRandall Stewart 								    SCTP_RTT_FROM_DATA);
3942f79aab18SRandall Stewart 								rto_ok = 0;
3943f79aab18SRandall Stewart 							}
3944f79aab18SRandall Stewart 							if (tp1->whoTo->rto_needed == 0) {
3945f79aab18SRandall Stewart 								tp1->whoTo->rto_needed = 1;
3946f79aab18SRandall Stewart 							}
3947f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
3948f8829a4aSRandall Stewart 						}
3949f8829a4aSRandall Stewart 					}
3950132dea7dSRandall Stewart 					/*
395118e198d3SRandall Stewart 					 * CMT: CUCv2 algorithm. From the
395218e198d3SRandall Stewart 					 * cumack'd TSNs, for each TSN being
395318e198d3SRandall Stewart 					 * acked for the first time, set the
395418e198d3SRandall Stewart 					 * following variables for the
395518e198d3SRandall Stewart 					 * corresp destination.
395618e198d3SRandall Stewart 					 * new_pseudo_cumack will trigger a
395718e198d3SRandall Stewart 					 * cwnd update.
395818e198d3SRandall Stewart 					 * find_(rtx_)pseudo_cumack will
395918e198d3SRandall Stewart 					 * trigger search for the next
396018e198d3SRandall Stewart 					 * expected (rtx-)pseudo-cumack.
3961132dea7dSRandall Stewart 					 */
3962132dea7dSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
3963132dea7dSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
3964132dea7dSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
3965132dea7dSRandall Stewart 
3966b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
396704ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
3968f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
396980fefe0aSRandall Stewart 					}
3970f8829a4aSRandall Stewart 				}
3971f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3972f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
3973f8829a4aSRandall Stewart 				}
397442551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
397542551e99SRandall Stewart 					/* deflate the cwnd */
397642551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
397742551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
397842551e99SRandall Stewart 				}
3979f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
3980f8829a4aSRandall Stewart 				TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
3981f8829a4aSRandall Stewart 				if (tp1->data) {
398204ee05e8SRandall Stewart 					/* sa_ignore NO_NULL_CHK */
3983f8829a4aSRandall Stewart 					sctp_free_bufspace(stcb, asoc, tp1, 1);
3984f8829a4aSRandall Stewart 					sctp_m_freem(tp1->data);
39854a9ef3f8SMichael Tuexen 					tp1->data = NULL;
3986f8829a4aSRandall Stewart 				}
3987b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3988f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
3989f8829a4aSRandall Stewart 					    cumack,
3990f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
3991f8829a4aSRandall Stewart 					    0,
3992f8829a4aSRandall Stewart 					    0,
3993f8829a4aSRandall Stewart 					    SCTP_LOG_FREE_SENT);
399480fefe0aSRandall Stewart 				}
3995f8829a4aSRandall Stewart 				asoc->sent_queue_cnt--;
3996*689e6a5fSMichael Tuexen 				sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED);
399718e198d3SRandall Stewart 			} else {
399818e198d3SRandall Stewart 				break;
3999f8829a4aSRandall Stewart 			}
40005e54f665SRandall Stewart 		}
400118e198d3SRandall Stewart 
400218e198d3SRandall Stewart 	}
400304ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
4004f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
4005ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4006ceaad40aSRandall Stewart 		struct socket *so;
4007ceaad40aSRandall Stewart 
4008ceaad40aSRandall Stewart #endif
4009f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4010b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
401104ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
4012f8829a4aSRandall Stewart 			sctp_wakeup_log(stcb, cumack, 1, SCTP_WAKESND_FROM_SACK);
401380fefe0aSRandall Stewart 		}
4014ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4015ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
4016ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
4017ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
4018ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
4019ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
4020ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4021ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4022ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
4023ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
4024ceaad40aSRandall Stewart 			return;
4025ceaad40aSRandall Stewart 		}
4026ceaad40aSRandall Stewart #endif
4027f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4028ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4029ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4030ceaad40aSRandall Stewart #endif
4031f8829a4aSRandall Stewart 	} else {
4032b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4033f8829a4aSRandall Stewart 			sctp_wakeup_log(stcb, cumack, 1, SCTP_NOWAKE_FROM_SACK);
403480fefe0aSRandall Stewart 		}
4035f8829a4aSRandall Stewart 	}
4036f8829a4aSRandall Stewart 
4037b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
4038899288aeSRandall Stewart 	if ((asoc->last_acked_seq != cumack) && (ecne_seen == 0))
4039b54d3a6cSRandall Stewart 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
40405e54f665SRandall Stewart 
4041f8829a4aSRandall Stewart 	asoc->last_acked_seq = cumack;
40425e54f665SRandall Stewart 
4043f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
4044f8829a4aSRandall Stewart 		/* nothing left in-flight */
4045f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4046f8829a4aSRandall Stewart 			net->flight_size = 0;
4047f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4048f8829a4aSRandall Stewart 		}
4049f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4050f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4051f8829a4aSRandall Stewart 	}
4052f8829a4aSRandall Stewart 	/* RWND update */
4053f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
405444fbe462SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4055f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4056f8829a4aSRandall Stewart 		/* SWS sender side engages */
4057f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
4058f8829a4aSRandall Stewart 	}
40595e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
40605e54f665SRandall Stewart 		win_probe_recovery = 1;
40615e54f665SRandall Stewart 	}
4062f8829a4aSRandall Stewart 	/* Now assure a timer where data is queued at */
4063a5d547adSRandall Stewart again:
4064a5d547adSRandall Stewart 	j = 0;
4065f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
40665171328bSRandall Stewart 		int to_ticks;
40675171328bSRandall Stewart 
40685e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
4069c105859eSRandall Stewart 			win_probe_recovered = 1;
40705e54f665SRandall Stewart 			/*
40715e54f665SRandall Stewart 			 * Find first chunk that was used with window probe
40725e54f665SRandall Stewart 			 * and clear the sent
40735e54f665SRandall Stewart 			 */
40743c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
40755e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
40765e54f665SRandall Stewart 				if (tp1->window_probe) {
4077cd554309SMichael Tuexen 					/* move back to data send queue */
4078c105859eSRandall Stewart 					sctp_window_probe_recovery(stcb, asoc, net, tp1);
40795e54f665SRandall Stewart 					break;
40805e54f665SRandall Stewart 				}
40815e54f665SRandall Stewart 			}
40825e54f665SRandall Stewart 		}
4083f8829a4aSRandall Stewart 		if (net->RTO == 0) {
4084f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
4085f8829a4aSRandall Stewart 		} else {
4086f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
4087f8829a4aSRandall Stewart 		}
40885171328bSRandall Stewart 		if (net->flight_size) {
4089a5d547adSRandall Stewart 			j++;
4090ad81507eSRandall Stewart 			(void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
4091f8829a4aSRandall Stewart 			    sctp_timeout_handler, &net->rxt_timer);
40925171328bSRandall Stewart 			if (net->window_probe) {
40935171328bSRandall Stewart 				net->window_probe = 0;
40945171328bSRandall Stewart 			}
4095f8829a4aSRandall Stewart 		} else {
40965171328bSRandall Stewart 			if (net->window_probe) {
40975171328bSRandall Stewart 				/*
40985171328bSRandall Stewart 				 * In window probes we must assure a timer
40995171328bSRandall Stewart 				 * is still running there
41005171328bSRandall Stewart 				 */
41015171328bSRandall Stewart 				net->window_probe = 0;
41025171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
41035171328bSRandall Stewart 					SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
41045171328bSRandall Stewart 					    sctp_timeout_handler, &net->rxt_timer);
41055171328bSRandall Stewart 				}
41065171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4107f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4108a5d547adSRandall Stewart 				    stcb, net,
4109a5d547adSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
4110f8829a4aSRandall Stewart 			}
4111b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
4112139bc87fSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4113f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
4114a5d547adSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4115a5d547adSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
4116f8829a4aSRandall Stewart 				}
4117f8829a4aSRandall Stewart 			}
4118f8829a4aSRandall Stewart 		}
4119f8829a4aSRandall Stewart 	}
4120bff64a4dSRandall Stewart 	if ((j == 0) &&
4121bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
4122bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
4123c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
4124bff64a4dSRandall Stewart 	    (done_once == 0)) {
41250c0982b8SRandall Stewart 		/*
41260c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
41270c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
41280c0982b8SRandall Stewart 		 */
41290c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
4130a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4131a5d547adSRandall Stewart 				net->flight_size = 0;
4132a5d547adSRandall Stewart 			}
4133a5d547adSRandall Stewart 			asoc->total_flight = 0;
4134a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
4135a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4136a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4137a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4138c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
4139c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
4140a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4141791437b5SRandall Stewart 					sctp_ucount_incr(asoc->sent_queue_retran_cnt);
4142a5d547adSRandall Stewart 				}
4143a5d547adSRandall Stewart 			}
41440c0982b8SRandall Stewart 		}
4145bff64a4dSRandall Stewart 		done_once = 1;
4146a5d547adSRandall Stewart 		goto again;
4147a5d547adSRandall Stewart 	}
4148f8829a4aSRandall Stewart 	/**********************************/
4149f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
4150f8829a4aSRandall Stewart 	/**********************************/
4151f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4152f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
4153f8829a4aSRandall Stewart 		/* clean up */
4154f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
4155f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4156f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4157f8829a4aSRandall Stewart 		    (asoc->locked_on_sending)
4158f8829a4aSRandall Stewart 		    ) {
4159f8829a4aSRandall Stewart 			struct sctp_stream_queue_pending *sp;
4160f8829a4aSRandall Stewart 
4161f8829a4aSRandall Stewart 			/*
4162f8829a4aSRandall Stewart 			 * I may be in a state where we got all across.. but
4163f8829a4aSRandall Stewart 			 * cannot write more due to a shutdown... we abort
4164f8829a4aSRandall Stewart 			 * since the user did not indicate EOR in this case.
4165f8829a4aSRandall Stewart 			 * The sp will be cleaned during free of the asoc.
4166f8829a4aSRandall Stewart 			 */
4167f8829a4aSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4168f8829a4aSRandall Stewart 			    sctp_streamhead);
41692afb3e84SRandall Stewart 			if ((sp) && (sp->length == 0)) {
41702afb3e84SRandall Stewart 				/* Let cleanup code purge it */
41712afb3e84SRandall Stewart 				if (sp->msg_is_complete) {
41722afb3e84SRandall Stewart 					asoc->stream_queue_cnt--;
41732afb3e84SRandall Stewart 				} else {
4174f8829a4aSRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
4175f8829a4aSRandall Stewart 					asoc->locked_on_sending = NULL;
4176f8829a4aSRandall Stewart 					asoc->stream_queue_cnt--;
4177f8829a4aSRandall Stewart 				}
4178f8829a4aSRandall Stewart 			}
41792afb3e84SRandall Stewart 		}
4180f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4181f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4182f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4183f8829a4aSRandall Stewart 				/* Need to abort here */
4184f8829a4aSRandall Stewart 				struct mbuf *oper;
4185f8829a4aSRandall Stewart 
4186f8829a4aSRandall Stewart 		abort_out_now:
4187f8829a4aSRandall Stewart 				*abort_now = 1;
4188f8829a4aSRandall Stewart 				/* XXX */
4189f8829a4aSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4190f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
4191f8829a4aSRandall Stewart 				if (oper) {
4192f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
4193f8829a4aSRandall Stewart 					uint32_t *ippp;
4194f8829a4aSRandall Stewart 
4195139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4196f8829a4aSRandall Stewart 					    sizeof(uint32_t);
4197f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
4198f8829a4aSRandall Stewart 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4199139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
4200f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
4201a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_24);
4202f8829a4aSRandall Stewart 				}
4203a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
4204ceaad40aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
4205f8829a4aSRandall Stewart 			} else {
4206f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4207f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4208f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4209f42a358aSRandall Stewart 				}
4210c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4211b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4212f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
4213f8829a4aSRandall Stewart 				sctp_send_shutdown(stcb,
4214f8829a4aSRandall Stewart 				    stcb->asoc.primary_destination);
4215f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4216f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
4217f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4218f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
4219f8829a4aSRandall Stewart 			}
4220f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4221f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4222f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4223f8829a4aSRandall Stewart 				goto abort_out_now;
4224f8829a4aSRandall Stewart 			}
4225f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4226c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4227b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4228f8829a4aSRandall Stewart 			sctp_send_shutdown_ack(stcb,
4229f8829a4aSRandall Stewart 			    stcb->asoc.primary_destination);
423012af6654SMichael Tuexen 			sctp_stop_timers_for_shutdown(stcb);
4231f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4232f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb, asoc->primary_destination);
4233f8829a4aSRandall Stewart 		}
4234f8829a4aSRandall Stewart 	}
4235dfb11ef8SRandall Stewart 	/*********************************************/
4236dfb11ef8SRandall Stewart 	/* Here we perform PR-SCTP procedures        */
4237dfb11ef8SRandall Stewart 	/* (section 4.2)                             */
4238dfb11ef8SRandall Stewart 	/*********************************************/
4239dfb11ef8SRandall Stewart 	/* C1. update advancedPeerAckPoint */
424020b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cumack, asoc->advanced_peer_ack_point)) {
4241dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cumack;
4242dfb11ef8SRandall Stewart 	}
4243830d754dSRandall Stewart 	/* PR-Sctp issues need to be addressed too */
4244830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
4245830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
4246830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
4247830d754dSRandall Stewart 
4248830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
4249830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
4250830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
425120b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cumack)) {
4252830d754dSRandall Stewart 			/*
4253493d8e5aSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing.
4254830d754dSRandall Stewart 			 */
425520b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) {
4256830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
42570c0982b8SRandall Stewart 			} else if (lchk) {
42580c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
425944fbe462SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt >= 3) {
42600c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
42610c0982b8SRandall Stewart 				}
4262830d754dSRandall Stewart 			}
4263830d754dSRandall Stewart 		}
4264830d754dSRandall Stewart 		if (lchk) {
4265830d754dSRandall Stewart 			/* Assure a timer is up */
4266830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4267830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
4268830d754dSRandall Stewart 		}
4269830d754dSRandall Stewart 	}
4270b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
4271f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4272f8829a4aSRandall Stewart 		    rwnd,
4273f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
4274f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
4275f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
427680fefe0aSRandall Stewart 	}
4277f8829a4aSRandall Stewart }
4278f8829a4aSRandall Stewart 
4279f8829a4aSRandall Stewart void
4280cd554309SMichael Tuexen sctp_handle_sack(struct mbuf *m, int offset_seg, int offset_dup,
4281cd554309SMichael Tuexen     struct sctp_tcb *stcb, struct sctp_nets *net_from,
4282cd554309SMichael Tuexen     uint16_t num_seg, uint16_t num_nr_seg, uint16_t num_dup,
4283cd554309SMichael Tuexen     int *abort_now, uint8_t flags,
4284899288aeSRandall Stewart     uint32_t cum_ack, uint32_t rwnd, int ecne_seen)
4285f8829a4aSRandall Stewart {
4286f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4287f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
4288cd554309SMichael Tuexen 	uint32_t last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked, this_sack_lowest_newack;
4289bff64a4dSRandall Stewart 	uint32_t sav_cum_ack;
4290f8829a4aSRandall Stewart 	uint16_t wake_him = 0;
4291c105859eSRandall Stewart 	uint32_t send_s = 0;
4292f8829a4aSRandall Stewart 	long j;
4293f8829a4aSRandall Stewart 	int accum_moved = 0;
4294f8829a4aSRandall Stewart 	int will_exit_fast_recovery = 0;
42955e54f665SRandall Stewart 	uint32_t a_rwnd, old_rwnd;
42965e54f665SRandall Stewart 	int win_probe_recovery = 0;
4297c105859eSRandall Stewart 	int win_probe_recovered = 0;
4298f8829a4aSRandall Stewart 	struct sctp_nets *net = NULL;
4299493d8e5aSRandall Stewart 	int ecn_seg_sums = 0;
4300bff64a4dSRandall Stewart 	int done_once;
4301f79aab18SRandall Stewart 	int rto_ok = 1;
4302f8829a4aSRandall Stewart 	uint8_t reneged_all = 0;
4303f8829a4aSRandall Stewart 	uint8_t cmt_dac_flag;
4304f8829a4aSRandall Stewart 
4305f8829a4aSRandall Stewart 	/*
4306f8829a4aSRandall Stewart 	 * we take any chance we can to service our queues since we cannot
4307f8829a4aSRandall Stewart 	 * get awoken when the socket is read from :<
4308f8829a4aSRandall Stewart 	 */
4309f8829a4aSRandall Stewart 	/*
4310f8829a4aSRandall Stewart 	 * Now perform the actual SACK handling: 1) Verify that it is not an
4311f8829a4aSRandall Stewart 	 * old sack, if so discard. 2) If there is nothing left in the send
4312f8829a4aSRandall Stewart 	 * queue (cum-ack is equal to last acked) then you have a duplicate
4313f8829a4aSRandall Stewart 	 * too, update any rwnd change and verify no timers are running.
4314f8829a4aSRandall Stewart 	 * then return. 3) Process any new consequtive data i.e. cum-ack
4315f8829a4aSRandall Stewart 	 * moved process these first and note that it moved. 4) Process any
4316f8829a4aSRandall Stewart 	 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
4317f8829a4aSRandall Stewart 	 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
4318f8829a4aSRandall Stewart 	 * sync up flightsizes and things, stop all timers and also check
4319f8829a4aSRandall Stewart 	 * for shutdown_pending state. If so then go ahead and send off the
4320f8829a4aSRandall Stewart 	 * shutdown. If in shutdown recv, send off the shutdown-ack and
4321f8829a4aSRandall Stewart 	 * start that timer, Ret. 9) Strike any non-acked things and do FR
4322f8829a4aSRandall Stewart 	 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
4323f8829a4aSRandall Stewart 	 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
4324f8829a4aSRandall Stewart 	 * if in shutdown_recv state.
4325f8829a4aSRandall Stewart 	 */
4326f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
4327f8829a4aSRandall Stewart 	/* CMT DAC algo */
4328f8829a4aSRandall Stewart 	this_sack_lowest_newack = 0;
4329f8829a4aSRandall Stewart 	j = 0;
4330f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_slowpath_sack);
4331cd554309SMichael Tuexen 	last_tsn = cum_ack;
4332cd554309SMichael Tuexen 	cmt_dac_flag = flags & SCTP_SACK_CMT_DAC;
433318e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
433418e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
433518e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
433618e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
433718e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
433818e198d3SRandall Stewart 	}
433918e198d3SRandall Stewart #endif
4340d06c82f1SRandall Stewart 	a_rwnd = rwnd;
4341f8829a4aSRandall Stewart 
4342cd554309SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
4343cd554309SMichael Tuexen 		sctp_misc_ints(SCTP_SACK_LOG_NORMAL, cum_ack,
4344cd554309SMichael Tuexen 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
4345cd554309SMichael Tuexen 	}
43465e54f665SRandall Stewart 	old_rwnd = stcb->asoc.peers_rwnd;
4347b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4348c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4349c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
4350c4739e2fSRandall Stewart 		    0,
4351c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
4352c4739e2fSRandall Stewart 		    __LINE__);
4353c4739e2fSRandall Stewart 	}
4354f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
4355f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
4356b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4357f8829a4aSRandall Stewart 		sctp_log_sack(asoc->last_acked_seq,
4358f8829a4aSRandall Stewart 		    cum_ack,
4359f8829a4aSRandall Stewart 		    0,
4360f8829a4aSRandall Stewart 		    num_seg,
4361f8829a4aSRandall Stewart 		    num_dup,
4362f8829a4aSRandall Stewart 		    SCTP_LOG_NEW_SACK);
436380fefe0aSRandall Stewart 	}
4364b3f1ea41SRandall Stewart 	if ((num_dup) && (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_FR_LOGGING_ENABLE | SCTP_EARLYFR_LOGGING_ENABLE))) {
4365cd554309SMichael Tuexen 		uint16_t i;
4366458303daSRandall Stewart 		uint32_t *dupdata, dblock;
4367f8829a4aSRandall Stewart 
4368cd554309SMichael Tuexen 		for (i = 0; i < num_dup; i++) {
4369cd554309SMichael Tuexen 			dupdata = (uint32_t *) sctp_m_getptr(m, offset_dup + i * sizeof(uint32_t),
4370458303daSRandall Stewart 			    sizeof(uint32_t), (uint8_t *) & dblock);
4371cd554309SMichael Tuexen 			if (dupdata == NULL) {
4372458303daSRandall Stewart 				break;
4373458303daSRandall Stewart 			}
4374cd554309SMichael Tuexen 			sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
4375f8829a4aSRandall Stewart 		}
4376f8829a4aSRandall Stewart 	}
4377b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4378c105859eSRandall Stewart 		/* reality check */
4379c105859eSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4380c105859eSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
4381c105859eSRandall Stewart 			    sctpchunk_listhead);
4382c105859eSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
4383c105859eSRandall Stewart 		} else {
4384b5c16493SMichael Tuexen 			tp1 = NULL;
4385c105859eSRandall Stewart 			send_s = asoc->sending_seq;
4386c105859eSRandall Stewart 		}
438720b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(cum_ack, send_s)) {
4388c105859eSRandall Stewart 			struct mbuf *oper;
4389c105859eSRandall Stewart 
4390f8829a4aSRandall Stewart 			/*
4391f8829a4aSRandall Stewart 			 * no way, we have not even sent this TSN out yet.
4392f8829a4aSRandall Stewart 			 * Peer is hopelessly messed up with us.
4393f8829a4aSRandall Stewart 			 */
4394b5c16493SMichael Tuexen 			printf("NEW cum_ack:%x send_s:%x is smaller or equal\n",
4395b5c16493SMichael Tuexen 			    cum_ack, send_s);
4396b5c16493SMichael Tuexen 			if (tp1) {
4397b5c16493SMichael Tuexen 				printf("Got send_s from tsn:%x + 1 of tp1:%p\n",
4398b5c16493SMichael Tuexen 				    tp1->rec.data.TSN_seq, tp1);
4399b5c16493SMichael Tuexen 			}
4400f8829a4aSRandall Stewart 	hopeless_peer:
4401f8829a4aSRandall Stewart 			*abort_now = 1;
4402f8829a4aSRandall Stewart 			/* XXX */
4403f8829a4aSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4404f8829a4aSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
4405f8829a4aSRandall Stewart 			if (oper) {
4406f8829a4aSRandall Stewart 				struct sctp_paramhdr *ph;
4407f8829a4aSRandall Stewart 				uint32_t *ippp;
4408f8829a4aSRandall Stewart 
4409139bc87fSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4410f8829a4aSRandall Stewart 				    sizeof(uint32_t);
4411f8829a4aSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
4412f8829a4aSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4413139bc87fSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
4414f8829a4aSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
4415a5d547adSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
4416f8829a4aSRandall Stewart 			}
4417a5d547adSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4418ceaad40aSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
4419f8829a4aSRandall Stewart 			return;
4420f8829a4aSRandall Stewart 		}
4421f8829a4aSRandall Stewart 	}
4422f8829a4aSRandall Stewart 	/**********************/
4423f8829a4aSRandall Stewart 	/* 1) check the range */
4424f8829a4aSRandall Stewart 	/**********************/
442520b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->last_acked_seq, last_tsn)) {
4426f8829a4aSRandall Stewart 		/* acking something behind */
4427f8829a4aSRandall Stewart 		return;
4428f8829a4aSRandall Stewart 	}
4429bff64a4dSRandall Stewart 	sav_cum_ack = asoc->last_acked_seq;
4430bff64a4dSRandall Stewart 
4431f8829a4aSRandall Stewart 	/* update the Rwnd of the peer */
4432f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue) &&
4433f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->send_queue) &&
4434cd554309SMichael Tuexen 	    (asoc->stream_queue_cnt == 0)) {
4435f8829a4aSRandall Stewart 		/* nothing left on send/sent and strmq */
4436b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4437f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4438f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
443980fefe0aSRandall Stewart 		}
4440f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
4441f8829a4aSRandall Stewart 		if (asoc->sent_queue_retran_cnt) {
4442f8829a4aSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4443f8829a4aSRandall Stewart 		}
4444f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4445f8829a4aSRandall Stewart 			/* SWS sender side engages */
4446f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
4447f8829a4aSRandall Stewart 		}
4448f8829a4aSRandall Stewart 		/* stop any timers */
4449f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4450f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4451a5d547adSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4452b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
4453139bc87fSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4454f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck1);
4455a5d547adSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4456a5d547adSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4457f8829a4aSRandall Stewart 				}
4458f8829a4aSRandall Stewart 			}
4459f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4460f8829a4aSRandall Stewart 			net->flight_size = 0;
4461f8829a4aSRandall Stewart 		}
4462f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4463f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4464f8829a4aSRandall Stewart 		return;
4465f8829a4aSRandall Stewart 	}
4466f8829a4aSRandall Stewart 	/*
4467f8829a4aSRandall Stewart 	 * We init netAckSz and netAckSz2 to 0. These are used to track 2
4468f8829a4aSRandall Stewart 	 * things. The total byte count acked is tracked in netAckSz AND
4469f8829a4aSRandall Stewart 	 * netAck2 is used to track the total bytes acked that are un-
4470f8829a4aSRandall Stewart 	 * amibguious and were never retransmitted. We track these on a per
4471f8829a4aSRandall Stewart 	 * destination address basis.
4472f8829a4aSRandall Stewart 	 */
4473f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4474a21779f0SRandall Stewart 		if (SCTP_TSN_GT(cum_ack, net->cwr_window_tsn)) {
4475a21779f0SRandall Stewart 			/* Drag along the window_tsn for cwr's */
4476a21779f0SRandall Stewart 			net->cwr_window_tsn = cum_ack;
4477a21779f0SRandall Stewart 		}
4478f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
4479f8829a4aSRandall Stewart 		net->net_ack = 0;
4480f8829a4aSRandall Stewart 		net->net_ack2 = 0;
4481f8829a4aSRandall Stewart 
4482f8829a4aSRandall Stewart 		/*
448342551e99SRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
448442551e99SRandall Stewart 		 * SACK processing
4485f8829a4aSRandall Stewart 		 */
4486f8829a4aSRandall Stewart 		net->new_pseudo_cumack = 0;
4487f8829a4aSRandall Stewart 		net->will_exit_fast_recovery = 0;
4488299108c5SRandall Stewart 		if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) {
4489299108c5SRandall Stewart 			(*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) (stcb, net);
4490299108c5SRandall Stewart 		}
4491f8829a4aSRandall Stewart 	}
4492f8829a4aSRandall Stewart 	/* process the new consecutive TSN first */
44934a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
449420b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(last_tsn, tp1->rec.data.TSN_seq)) {
4495f8829a4aSRandall Stewart 			if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4496f8829a4aSRandall Stewart 				accum_moved = 1;
4497f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4498f8829a4aSRandall Stewart 					/*
4499f8829a4aSRandall Stewart 					 * If it is less than ACKED, it is
4500f8829a4aSRandall Stewart 					 * now no-longer in flight. Higher
4501f8829a4aSRandall Stewart 					 * values may occur during marking
4502f8829a4aSRandall Stewart 					 */
4503f8829a4aSRandall Stewart 					if ((tp1->whoTo->dest_state &
4504f8829a4aSRandall Stewart 					    SCTP_ADDR_UNCONFIRMED) &&
4505f8829a4aSRandall Stewart 					    (tp1->snd_count < 2)) {
4506f8829a4aSRandall Stewart 						/*
4507f8829a4aSRandall Stewart 						 * If there was no retran
4508f8829a4aSRandall Stewart 						 * and the address is
4509f8829a4aSRandall Stewart 						 * un-confirmed and we sent
4510f8829a4aSRandall Stewart 						 * there and are now
4511f8829a4aSRandall Stewart 						 * sacked.. its confirmed,
4512f8829a4aSRandall Stewart 						 * mark it so.
4513f8829a4aSRandall Stewart 						 */
4514f8829a4aSRandall Stewart 						tp1->whoTo->dest_state &=
4515f8829a4aSRandall Stewart 						    ~SCTP_ADDR_UNCONFIRMED;
4516f8829a4aSRandall Stewart 					}
4517c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4518b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4519c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4520a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
4521a5d547adSRandall Stewart 							    tp1->book_size,
4522c105859eSRandall Stewart 							    (uintptr_t) tp1->whoTo,
4523a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
452480fefe0aSRandall Stewart 						}
4525c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
4526c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
4527299108c5SRandall Stewart 						if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
4528299108c5SRandall Stewart 							(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
4529299108c5SRandall Stewart 							    tp1);
4530299108c5SRandall Stewart 						}
4531f8829a4aSRandall Stewart 					}
4532f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
4533f8829a4aSRandall Stewart 
4534f8829a4aSRandall Stewart 					/* CMT SFR and DAC algos */
4535f8829a4aSRandall Stewart 					this_sack_lowest_newack = tp1->rec.data.TSN_seq;
4536f8829a4aSRandall Stewart 					tp1->whoTo->saw_newack = 1;
4537f8829a4aSRandall Stewart 
4538f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
4539f8829a4aSRandall Stewart 						/*
4540f8829a4aSRandall Stewart 						 * True non-retransmited
4541f8829a4aSRandall Stewart 						 * chunk
4542f8829a4aSRandall Stewart 						 */
4543f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
4544f8829a4aSRandall Stewart 						    tp1->send_size;
4545f8829a4aSRandall Stewart 
4546f8829a4aSRandall Stewart 						/* update RTO too? */
4547f8829a4aSRandall Stewart 						if (tp1->do_rtt) {
4548f79aab18SRandall Stewart 							if (rto_ok) {
4549f8829a4aSRandall Stewart 								tp1->whoTo->RTO =
4550f8829a4aSRandall Stewart 								    sctp_calculate_rto(stcb,
4551f8829a4aSRandall Stewart 								    asoc, tp1->whoTo,
455218e198d3SRandall Stewart 								    &tp1->sent_rcv_time,
4553899288aeSRandall Stewart 								    sctp_align_safe_nocopy,
4554f79aab18SRandall Stewart 								    SCTP_RTT_FROM_DATA);
4555f79aab18SRandall Stewart 								rto_ok = 0;
4556f79aab18SRandall Stewart 							}
4557f79aab18SRandall Stewart 							if (tp1->whoTo->rto_needed == 0) {
4558f79aab18SRandall Stewart 								tp1->whoTo->rto_needed = 1;
4559f79aab18SRandall Stewart 							}
4560f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
4561f8829a4aSRandall Stewart 						}
4562f8829a4aSRandall Stewart 					}
4563f8829a4aSRandall Stewart 					/*
4564f8829a4aSRandall Stewart 					 * CMT: CUCv2 algorithm. From the
4565f8829a4aSRandall Stewart 					 * cumack'd TSNs, for each TSN being
4566f8829a4aSRandall Stewart 					 * acked for the first time, set the
4567f8829a4aSRandall Stewart 					 * following variables for the
4568f8829a4aSRandall Stewart 					 * corresp destination.
4569f8829a4aSRandall Stewart 					 * new_pseudo_cumack will trigger a
4570f8829a4aSRandall Stewart 					 * cwnd update.
4571f8829a4aSRandall Stewart 					 * find_(rtx_)pseudo_cumack will
4572f8829a4aSRandall Stewart 					 * trigger search for the next
4573f8829a4aSRandall Stewart 					 * expected (rtx-)pseudo-cumack.
4574f8829a4aSRandall Stewart 					 */
4575f8829a4aSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
4576f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
4577f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
4578f8829a4aSRandall Stewart 
4579f8829a4aSRandall Stewart 
4580b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4581f8829a4aSRandall Stewart 						sctp_log_sack(asoc->last_acked_seq,
4582f8829a4aSRandall Stewart 						    cum_ack,
4583f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
4584f8829a4aSRandall Stewart 						    0,
4585f8829a4aSRandall Stewart 						    0,
4586f8829a4aSRandall Stewart 						    SCTP_LOG_TSN_ACKED);
458780fefe0aSRandall Stewart 					}
4588b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
4589f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
459080fefe0aSRandall Stewart 					}
4591f8829a4aSRandall Stewart 				}
4592f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4593f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4594f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
4595f8829a4aSRandall Stewart 					sctp_audit_log(0xB3,
4596f8829a4aSRandall Stewart 					    (asoc->sent_queue_retran_cnt & 0x000000ff));
4597f8829a4aSRandall Stewart #endif
4598f8829a4aSRandall Stewart 				}
459942551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
460042551e99SRandall Stewart 					/* deflate the cwnd */
460142551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
460242551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
460342551e99SRandall Stewart 				}
4604f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
4605f8829a4aSRandall Stewart 			}
4606f8829a4aSRandall Stewart 		} else {
4607f8829a4aSRandall Stewart 			break;
4608f8829a4aSRandall Stewart 		}
4609f8829a4aSRandall Stewart 	}
4610f8829a4aSRandall Stewart 	biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
4611f8829a4aSRandall Stewart 	/* always set this up to cum-ack */
4612f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = last_tsn;
4613f8829a4aSRandall Stewart 
4614cd554309SMichael Tuexen 	if ((num_seg > 0) || (num_nr_seg > 0)) {
4615f8829a4aSRandall Stewart 
4616f8829a4aSRandall Stewart 		/*
4617f8829a4aSRandall Stewart 		 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
4618f8829a4aSRandall Stewart 		 * to be greater than the cumack. Also reset saw_newack to 0
4619f8829a4aSRandall Stewart 		 * for all dests.
4620f8829a4aSRandall Stewart 		 */
4621f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4622f8829a4aSRandall Stewart 			net->saw_newack = 0;
4623f8829a4aSRandall Stewart 			net->this_sack_highest_newack = last_tsn;
4624f8829a4aSRandall Stewart 		}
4625f8829a4aSRandall Stewart 
4626f8829a4aSRandall Stewart 		/*
4627f8829a4aSRandall Stewart 		 * thisSackHighestGap will increase while handling NEW
4628f8829a4aSRandall Stewart 		 * segments this_sack_highest_newack will increase while
4629f8829a4aSRandall Stewart 		 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
4630f8829a4aSRandall Stewart 		 * used for CMT DAC algo. saw_newack will also change.
4631f8829a4aSRandall Stewart 		 */
4632cd554309SMichael Tuexen 		if (sctp_handle_segments(m, &offset_seg, stcb, asoc, last_tsn, &biggest_tsn_acked,
4633cd554309SMichael Tuexen 		    &biggest_tsn_newly_acked, &this_sack_lowest_newack,
4634f79aab18SRandall Stewart 		    num_seg, num_nr_seg, &ecn_seg_sums,
4635f79aab18SRandall Stewart 		    &rto_ok)) {
4636cd554309SMichael Tuexen 			wake_him++;
4637cd554309SMichael Tuexen 		}
4638b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4639f8829a4aSRandall Stewart 			/*
4640f8829a4aSRandall Stewart 			 * validate the biggest_tsn_acked in the gap acks if
4641f8829a4aSRandall Stewart 			 * strict adherence is wanted.
4642f8829a4aSRandall Stewart 			 */
464320b07a4dSMichael Tuexen 			if (SCTP_TSN_GE(biggest_tsn_acked, send_s)) {
4644f8829a4aSRandall Stewart 				/*
4645f8829a4aSRandall Stewart 				 * peer is either confused or we are under
4646f8829a4aSRandall Stewart 				 * attack. We must abort.
4647f8829a4aSRandall Stewart 				 */
4648b5c16493SMichael Tuexen 				printf("Hopeless peer! biggest_tsn_acked:%x largest seq:%x\n",
4649b5c16493SMichael Tuexen 				    biggest_tsn_acked,
4650b5c16493SMichael Tuexen 				    send_s);
4651b5c16493SMichael Tuexen 
4652f8829a4aSRandall Stewart 				goto hopeless_peer;
4653f8829a4aSRandall Stewart 			}
4654f8829a4aSRandall Stewart 		}
4655f8829a4aSRandall Stewart 	}
4656f8829a4aSRandall Stewart 	/*******************************************/
4657f8829a4aSRandall Stewart 	/* cancel ALL T3-send timer if accum moved */
4658f8829a4aSRandall Stewart 	/*******************************************/
46597c99d56fSMichael Tuexen 	if (asoc->sctp_cmt_on_off > 0) {
4660f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4661f8829a4aSRandall Stewart 			if (net->new_pseudo_cumack)
4662f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4663a5d547adSRandall Stewart 				    stcb, net,
4664a5d547adSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
4665f8829a4aSRandall Stewart 
4666f8829a4aSRandall Stewart 		}
4667f8829a4aSRandall Stewart 	} else {
4668f8829a4aSRandall Stewart 		if (accum_moved) {
4669f8829a4aSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4670f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4671a5d547adSRandall Stewart 				    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
4672f8829a4aSRandall Stewart 			}
4673f8829a4aSRandall Stewart 		}
4674f8829a4aSRandall Stewart 	}
4675f8829a4aSRandall Stewart 	/********************************************/
4676d9c5cfeaSMichael Tuexen 	/* drop the acked chunks from the sentqueue */
4677f8829a4aSRandall Stewart 	/********************************************/
4678f8829a4aSRandall Stewart 	asoc->last_acked_seq = cum_ack;
4679f8829a4aSRandall Stewart 
46807c99d56fSMichael Tuexen 	TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
468120b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, cum_ack)) {
4682f8829a4aSRandall Stewart 			break;
4683f8829a4aSRandall Stewart 		}
4684f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
4685f8829a4aSRandall Stewart 			/* no more sent on list */
468618e198d3SRandall Stewart 			printf("Warning, tp1->sent == %d and its now acked?\n",
468718e198d3SRandall Stewart 			    tp1->sent);
4688f8829a4aSRandall Stewart 		}
4689f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4690f8829a4aSRandall Stewart 		if (tp1->pr_sctp_on) {
4691f8829a4aSRandall Stewart 			if (asoc->pr_sctp_cnt != 0)
4692f8829a4aSRandall Stewart 				asoc->pr_sctp_cnt--;
4693f8829a4aSRandall Stewart 		}
46947c99d56fSMichael Tuexen 		asoc->sent_queue_cnt--;
4695f8829a4aSRandall Stewart 		if (tp1->data) {
469604ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
4697f8829a4aSRandall Stewart 			sctp_free_bufspace(stcb, asoc, tp1, 1);
4698f8829a4aSRandall Stewart 			sctp_m_freem(tp1->data);
46997c99d56fSMichael Tuexen 			tp1->data = NULL;
4700810ec536SMichael Tuexen 			if (asoc->peer_supports_prsctp && PR_SCTP_BUF_ENABLED(tp1->flags)) {
4701f8829a4aSRandall Stewart 				asoc->sent_queue_cnt_removeable--;
4702f8829a4aSRandall Stewart 			}
4703f8829a4aSRandall Stewart 		}
4704b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4705f8829a4aSRandall Stewart 			sctp_log_sack(asoc->last_acked_seq,
4706f8829a4aSRandall Stewart 			    cum_ack,
4707f8829a4aSRandall Stewart 			    tp1->rec.data.TSN_seq,
4708f8829a4aSRandall Stewart 			    0,
4709f8829a4aSRandall Stewart 			    0,
4710f8829a4aSRandall Stewart 			    SCTP_LOG_FREE_SENT);
471180fefe0aSRandall Stewart 		}
4712*689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED);
4713f8829a4aSRandall Stewart 		wake_him++;
47147c99d56fSMichael Tuexen 	}
47157c99d56fSMichael Tuexen 	if (TAILQ_EMPTY(&asoc->sent_queue) && (asoc->total_flight > 0)) {
47167c99d56fSMichael Tuexen #ifdef INVARIANTS
47177c99d56fSMichael Tuexen 		panic("Warning flight size is postive and should be 0");
47187c99d56fSMichael Tuexen #else
47197c99d56fSMichael Tuexen 		SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
47207c99d56fSMichael Tuexen 		    asoc->total_flight);
47217c99d56fSMichael Tuexen #endif
47227c99d56fSMichael Tuexen 		asoc->total_flight = 0;
47237c99d56fSMichael Tuexen 	}
472404ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
4725f8829a4aSRandall Stewart 	if ((wake_him) && (stcb->sctp_socket)) {
4726ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4727ceaad40aSRandall Stewart 		struct socket *so;
4728ceaad40aSRandall Stewart 
4729ceaad40aSRandall Stewart #endif
4730f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4731b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4732f8829a4aSRandall Stewart 			sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_WAKESND_FROM_SACK);
473380fefe0aSRandall Stewart 		}
4734ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4735ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
4736ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
4737ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
4738ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
4739ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
4740ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4741ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4742ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
4743ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
4744ceaad40aSRandall Stewart 			return;
4745ceaad40aSRandall Stewart 		}
4746ceaad40aSRandall Stewart #endif
4747f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4748ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4749ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4750ceaad40aSRandall Stewart #endif
4751f8829a4aSRandall Stewart 	} else {
4752b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4753f8829a4aSRandall Stewart 			sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_NOWAKE_FROM_SACK);
475480fefe0aSRandall Stewart 		}
4755f8829a4aSRandall Stewart 	}
4756f8829a4aSRandall Stewart 
475742551e99SRandall Stewart 	if (asoc->fast_retran_loss_recovery && accum_moved) {
475820b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(asoc->last_acked_seq, asoc->fast_recovery_tsn)) {
4759f8829a4aSRandall Stewart 			/* Setup so we will exit RFC2582 fast recovery */
4760f8829a4aSRandall Stewart 			will_exit_fast_recovery = 1;
4761f8829a4aSRandall Stewart 		}
4762f8829a4aSRandall Stewart 	}
4763f8829a4aSRandall Stewart 	/*
4764f8829a4aSRandall Stewart 	 * Check for revoked fragments:
4765f8829a4aSRandall Stewart 	 *
4766f8829a4aSRandall Stewart 	 * if Previous sack - Had no frags then we can't have any revoked if
4767f8829a4aSRandall Stewart 	 * Previous sack - Had frag's then - If we now have frags aka
4768f8829a4aSRandall Stewart 	 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
4769f8829a4aSRandall Stewart 	 * some of them. else - The peer revoked all ACKED fragments, since
4770f8829a4aSRandall Stewart 	 * we had some before and now we have NONE.
4771f8829a4aSRandall Stewart 	 */
4772f8829a4aSRandall Stewart 
4773d9c5cfeaSMichael Tuexen 	if (num_seg) {
4774c105859eSRandall Stewart 		sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
4775d9c5cfeaSMichael Tuexen 		asoc->saw_sack_with_frags = 1;
4776d9c5cfeaSMichael Tuexen 	} else if (asoc->saw_sack_with_frags) {
4777f8829a4aSRandall Stewart 		int cnt_revoked = 0;
4778f8829a4aSRandall Stewart 
4779f8829a4aSRandall Stewart 		/* Peer revoked all dg's marked or acked */
4780f8829a4aSRandall Stewart 		TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4781b5c16493SMichael Tuexen 			if (tp1->sent == SCTP_DATAGRAM_ACKED) {
4782f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_SENT;
4783b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4784c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
4785c105859eSRandall Stewart 					    tp1->whoTo->flight_size,
4786c105859eSRandall Stewart 					    tp1->book_size,
4787c105859eSRandall Stewart 					    (uintptr_t) tp1->whoTo,
4788c105859eSRandall Stewart 					    tp1->rec.data.TSN_seq);
478980fefe0aSRandall Stewart 				}
4790c105859eSRandall Stewart 				sctp_flight_size_increase(tp1);
4791c105859eSRandall Stewart 				sctp_total_flight_increase(stcb, tp1);
4792a5d547adSRandall Stewart 				tp1->rec.data.chunk_was_revoked = 1;
479342551e99SRandall Stewart 				/*
479442551e99SRandall Stewart 				 * To ensure that this increase in
47954a9ef3f8SMichael Tuexen 				 * flightsize, which is artificial, does not
47964a9ef3f8SMichael Tuexen 				 * throttle the sender, we also increase the
47974a9ef3f8SMichael Tuexen 				 * cwnd artificially.
479842551e99SRandall Stewart 				 */
479942551e99SRandall Stewart 				tp1->whoTo->cwnd += tp1->book_size;
4800f8829a4aSRandall Stewart 				cnt_revoked++;
4801f8829a4aSRandall Stewart 			}
4802f8829a4aSRandall Stewart 		}
4803f8829a4aSRandall Stewart 		if (cnt_revoked) {
4804f8829a4aSRandall Stewart 			reneged_all = 1;
4805f8829a4aSRandall Stewart 		}
4806f8829a4aSRandall Stewart 		asoc->saw_sack_with_frags = 0;
4807f8829a4aSRandall Stewart 	}
4808d9c5cfeaSMichael Tuexen 	if (num_nr_seg > 0)
4809d9c5cfeaSMichael Tuexen 		asoc->saw_sack_with_nr_frags = 1;
4810f8829a4aSRandall Stewart 	else
4811d9c5cfeaSMichael Tuexen 		asoc->saw_sack_with_nr_frags = 0;
4812f8829a4aSRandall Stewart 
4813b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
4814899288aeSRandall Stewart 	if (ecne_seen == 0)
4815b54d3a6cSRandall Stewart 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
4816f8829a4aSRandall Stewart 
4817f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
4818f8829a4aSRandall Stewart 		/* nothing left in-flight */
4819f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4820f8829a4aSRandall Stewart 			/* stop all timers */
4821b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
4822139bc87fSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4823f8829a4aSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
4824a5d547adSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4825a5d547adSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_29);
4826f8829a4aSRandall Stewart 				}
4827f8829a4aSRandall Stewart 			}
4828f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4829a5d547adSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
4830f8829a4aSRandall Stewart 			net->flight_size = 0;
4831f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4832f8829a4aSRandall Stewart 		}
4833f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4834f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4835f8829a4aSRandall Stewart 	}
4836f8829a4aSRandall Stewart 	/**********************************/
4837f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
4838f8829a4aSRandall Stewart 	/**********************************/
4839f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4840f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
4841b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4842f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4843f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
484480fefe0aSRandall Stewart 		}
4845f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
4846f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4847f8829a4aSRandall Stewart 			/* SWS sender side engages */
4848f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
4849f8829a4aSRandall Stewart 		}
4850f8829a4aSRandall Stewart 		/* clean up */
4851f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
4852f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4853f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4854f8829a4aSRandall Stewart 		    (asoc->locked_on_sending)
4855f8829a4aSRandall Stewart 		    ) {
4856f8829a4aSRandall Stewart 			struct sctp_stream_queue_pending *sp;
4857f8829a4aSRandall Stewart 
4858f8829a4aSRandall Stewart 			/*
4859f8829a4aSRandall Stewart 			 * I may be in a state where we got all across.. but
4860f8829a4aSRandall Stewart 			 * cannot write more due to a shutdown... we abort
4861f8829a4aSRandall Stewart 			 * since the user did not indicate EOR in this case.
4862f8829a4aSRandall Stewart 			 */
4863f8829a4aSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4864f8829a4aSRandall Stewart 			    sctp_streamhead);
48652afb3e84SRandall Stewart 			if ((sp) && (sp->length == 0)) {
4866f8829a4aSRandall Stewart 				asoc->locked_on_sending = NULL;
48672afb3e84SRandall Stewart 				if (sp->msg_is_complete) {
4868f8829a4aSRandall Stewart 					asoc->stream_queue_cnt--;
48692afb3e84SRandall Stewart 				} else {
48702afb3e84SRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
48712afb3e84SRandall Stewart 					asoc->stream_queue_cnt--;
48722afb3e84SRandall Stewart 				}
4873f8829a4aSRandall Stewart 			}
4874f8829a4aSRandall Stewart 		}
4875f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4876f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4877f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4878f8829a4aSRandall Stewart 				/* Need to abort here */
4879f8829a4aSRandall Stewart 				struct mbuf *oper;
4880f8829a4aSRandall Stewart 
4881f8829a4aSRandall Stewart 		abort_out_now:
4882f8829a4aSRandall Stewart 				*abort_now = 1;
4883f8829a4aSRandall Stewart 				/* XXX */
4884f8829a4aSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4885f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
4886f8829a4aSRandall Stewart 				if (oper) {
4887f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
4888f8829a4aSRandall Stewart 					uint32_t *ippp;
4889f8829a4aSRandall Stewart 
4890139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4891f8829a4aSRandall Stewart 					    sizeof(uint32_t);
4892f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
4893f8829a4aSRandall Stewart 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4894139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
4895f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
4896a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_31);
4897f8829a4aSRandall Stewart 				}
4898a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
4899ceaad40aSRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
4900f8829a4aSRandall Stewart 				return;
4901f8829a4aSRandall Stewart 			} else {
4902f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4903f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4904f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4905f42a358aSRandall Stewart 				}
4906c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4907b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4908f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
4909f8829a4aSRandall Stewart 				sctp_send_shutdown(stcb,
4910f8829a4aSRandall Stewart 				    stcb->asoc.primary_destination);
4911f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4912f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
4913f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4914f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, asoc->primary_destination);
4915f8829a4aSRandall Stewart 			}
4916f8829a4aSRandall Stewart 			return;
4917f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4918f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4919f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4920f8829a4aSRandall Stewart 				goto abort_out_now;
4921f8829a4aSRandall Stewart 			}
4922f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4923c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4924b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4925f8829a4aSRandall Stewart 			sctp_send_shutdown_ack(stcb,
4926f8829a4aSRandall Stewart 			    stcb->asoc.primary_destination);
492712af6654SMichael Tuexen 			sctp_stop_timers_for_shutdown(stcb);
4928f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4929f8829a4aSRandall Stewart 			    stcb->sctp_ep, stcb, asoc->primary_destination);
4930f8829a4aSRandall Stewart 			return;
4931f8829a4aSRandall Stewart 		}
4932f8829a4aSRandall Stewart 	}
4933f8829a4aSRandall Stewart 	/*
4934f8829a4aSRandall Stewart 	 * Now here we are going to recycle net_ack for a different use...
4935f8829a4aSRandall Stewart 	 * HEADS UP.
4936f8829a4aSRandall Stewart 	 */
4937f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4938f8829a4aSRandall Stewart 		net->net_ack = 0;
4939f8829a4aSRandall Stewart 	}
4940f8829a4aSRandall Stewart 
4941f8829a4aSRandall Stewart 	/*
4942f8829a4aSRandall Stewart 	 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
4943f8829a4aSRandall Stewart 	 * to be done. Setting this_sack_lowest_newack to the cum_ack will
4944f8829a4aSRandall Stewart 	 * automatically ensure that.
4945f8829a4aSRandall Stewart 	 */
49467c99d56fSMichael Tuexen 	if ((asoc->sctp_cmt_on_off > 0) &&
494720083c2eSMichael Tuexen 	    SCTP_BASE_SYSCTL(sctp_cmt_use_dac) &&
494820083c2eSMichael Tuexen 	    (cmt_dac_flag == 0)) {
4949f8829a4aSRandall Stewart 		this_sack_lowest_newack = cum_ack;
4950f8829a4aSRandall Stewart 	}
4951cd554309SMichael Tuexen 	if ((num_seg > 0) || (num_nr_seg > 0)) {
4952f8829a4aSRandall Stewart 		sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
4953f8829a4aSRandall Stewart 		    biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
4954f8829a4aSRandall Stewart 	}
4955b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
4956b54d3a6cSRandall Stewart 	asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
4957f8829a4aSRandall Stewart 
4958f8829a4aSRandall Stewart 	/* Now are we exiting loss recovery ? */
4959f8829a4aSRandall Stewart 	if (will_exit_fast_recovery) {
4960f8829a4aSRandall Stewart 		/* Ok, we must exit fast recovery */
4961f8829a4aSRandall Stewart 		asoc->fast_retran_loss_recovery = 0;
4962f8829a4aSRandall Stewart 	}
4963f8829a4aSRandall Stewart 	if ((asoc->sat_t3_loss_recovery) &&
496420b07a4dSMichael Tuexen 	    SCTP_TSN_GE(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn)) {
4965f8829a4aSRandall Stewart 		/* end satellite t3 loss recovery */
4966f8829a4aSRandall Stewart 		asoc->sat_t3_loss_recovery = 0;
4967f8829a4aSRandall Stewart 	}
496842551e99SRandall Stewart 	/*
496942551e99SRandall Stewart 	 * CMT Fast recovery
497042551e99SRandall Stewart 	 */
4971f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4972f8829a4aSRandall Stewart 		if (net->will_exit_fast_recovery) {
4973f8829a4aSRandall Stewart 			/* Ok, we must exit fast recovery */
4974f8829a4aSRandall Stewart 			net->fast_retran_loss_recovery = 0;
4975f8829a4aSRandall Stewart 		}
4976f8829a4aSRandall Stewart 	}
4977f8829a4aSRandall Stewart 
4978f8829a4aSRandall Stewart 	/* Adjust and set the new rwnd value */
4979b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4980f8829a4aSRandall Stewart 		sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
498144fbe462SRandall Stewart 		    asoc->peers_rwnd, asoc->total_flight, (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)), a_rwnd);
498280fefe0aSRandall Stewart 	}
4983f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
498444fbe462SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4985f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4986f8829a4aSRandall Stewart 		/* SWS sender side engages */
4987f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
4988f8829a4aSRandall Stewart 	}
49895e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
49905e54f665SRandall Stewart 		win_probe_recovery = 1;
49915e54f665SRandall Stewart 	}
4992f8829a4aSRandall Stewart 	/*
4993f8829a4aSRandall Stewart 	 * Now we must setup so we have a timer up for anyone with
4994f8829a4aSRandall Stewart 	 * outstanding data.
4995f8829a4aSRandall Stewart 	 */
4996bff64a4dSRandall Stewart 	done_once = 0;
4997a5d547adSRandall Stewart again:
4998a5d547adSRandall Stewart 	j = 0;
4999f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
50005e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
5001c105859eSRandall Stewart 			win_probe_recovered = 1;
50025e54f665SRandall Stewart 			/*-
50035e54f665SRandall Stewart 			 * Find first chunk that was used with
50045e54f665SRandall Stewart 			 * window probe and clear the event. Put
50055e54f665SRandall Stewart 			 * it back into the send queue as if has
50065e54f665SRandall Stewart 			 * not been sent.
50075e54f665SRandall Stewart 			 */
50085e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
50095e54f665SRandall Stewart 				if (tp1->window_probe) {
5010c105859eSRandall Stewart 					sctp_window_probe_recovery(stcb, asoc, net, tp1);
50115e54f665SRandall Stewart 					break;
50125e54f665SRandall Stewart 				}
50135e54f665SRandall Stewart 			}
50145e54f665SRandall Stewart 		}
5015f8829a4aSRandall Stewart 		if (net->flight_size) {
5016a5d547adSRandall Stewart 			j++;
5017cd554309SMichael Tuexen 			if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5018f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5019f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, net);
5020cd554309SMichael Tuexen 			}
50215171328bSRandall Stewart 			if (net->window_probe) {
5022cd554309SMichael Tuexen 				net->window_probe = 0;
50235171328bSRandall Stewart 			}
5024c105859eSRandall Stewart 		} else {
50255171328bSRandall Stewart 			if (net->window_probe) {
50265171328bSRandall Stewart 				/*
50275171328bSRandall Stewart 				 * In window probes we must assure a timer
50285171328bSRandall Stewart 				 * is still running there
50295171328bSRandall Stewart 				 */
50305171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
50315171328bSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_SEND,
50325171328bSRandall Stewart 					    stcb->sctp_ep, stcb, net);
50335171328bSRandall Stewart 
50345171328bSRandall Stewart 				}
50355171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5036c105859eSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5037c105859eSRandall Stewart 				    stcb, net,
5038c105859eSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
5039c105859eSRandall Stewart 			}
5040b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
5041c105859eSRandall Stewart 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
5042c105859eSRandall Stewart 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
5043c105859eSRandall Stewart 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
5044c105859eSRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
5045c105859eSRandall Stewart 				}
5046c105859eSRandall Stewart 			}
5047f8829a4aSRandall Stewart 		}
5048f8829a4aSRandall Stewart 	}
5049bff64a4dSRandall Stewart 	if ((j == 0) &&
5050bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
5051bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
5052c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
5053bff64a4dSRandall Stewart 	    (done_once == 0)) {
50540c0982b8SRandall Stewart 		/*
50550c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
50560c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
50570c0982b8SRandall Stewart 		 */
50580c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
5059a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5060a5d547adSRandall Stewart 				net->flight_size = 0;
5061a5d547adSRandall Stewart 			}
5062a5d547adSRandall Stewart 			asoc->total_flight = 0;
5063a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
5064a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
5065a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5066a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5067c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
5068c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
5069a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5070791437b5SRandall Stewart 					sctp_ucount_incr(asoc->sent_queue_retran_cnt);
5071a5d547adSRandall Stewart 				}
5072a5d547adSRandall Stewart 			}
50730c0982b8SRandall Stewart 		}
5074bff64a4dSRandall Stewart 		done_once = 1;
5075a5d547adSRandall Stewart 		goto again;
5076a5d547adSRandall Stewart 	}
5077cd554309SMichael Tuexen 	/*********************************************/
5078cd554309SMichael Tuexen 	/* Here we perform PR-SCTP procedures        */
5079cd554309SMichael Tuexen 	/* (section 4.2)                             */
5080cd554309SMichael Tuexen 	/*********************************************/
5081cd554309SMichael Tuexen 	/* C1. update advancedPeerAckPoint */
508220b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cum_ack, asoc->advanced_peer_ack_point)) {
5083dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cum_ack;
5084dfb11ef8SRandall Stewart 	}
5085830d754dSRandall Stewart 	/* C2. try to further move advancedPeerAckPoint ahead */
5086830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
5087830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
5088830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
5089830d754dSRandall Stewart 
5090830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
5091830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
5092830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
509320b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cum_ack)) {
5094830d754dSRandall Stewart 			/*
5095493d8e5aSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing.
5096830d754dSRandall Stewart 			 */
50970c0982b8SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
50980c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
50990c0982b8SRandall Stewart 				    0xee, cum_ack, asoc->advanced_peer_ack_point,
51000c0982b8SRandall Stewart 				    old_adv_peer_ack_point);
51010c0982b8SRandall Stewart 			}
510220b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) {
5103830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
51040c0982b8SRandall Stewart 			} else if (lchk) {
51050c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
510644fbe462SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt >= 3) {
51070c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
51080c0982b8SRandall Stewart 				}
5109830d754dSRandall Stewart 			}
5110830d754dSRandall Stewart 		}
5111830d754dSRandall Stewart 		if (lchk) {
5112830d754dSRandall Stewart 			/* Assure a timer is up */
5113830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5114830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
5115830d754dSRandall Stewart 		}
5116830d754dSRandall Stewart 	}
5117b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
5118f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
5119f8829a4aSRandall Stewart 		    a_rwnd,
5120f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
5121f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
5122f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
512380fefe0aSRandall Stewart 	}
5124f8829a4aSRandall Stewart }
5125f8829a4aSRandall Stewart 
5126f8829a4aSRandall Stewart void
5127f8829a4aSRandall Stewart sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp,
5128f8829a4aSRandall Stewart     struct sctp_nets *netp, int *abort_flag)
5129f8829a4aSRandall Stewart {
5130f8829a4aSRandall Stewart 	/* Copy cum-ack */
5131f8829a4aSRandall Stewart 	uint32_t cum_ack, a_rwnd;
5132f8829a4aSRandall Stewart 
5133f8829a4aSRandall Stewart 	cum_ack = ntohl(cp->cumulative_tsn_ack);
5134f8829a4aSRandall Stewart 	/* Arrange so a_rwnd does NOT change */
5135f8829a4aSRandall Stewart 	a_rwnd = stcb->asoc.peers_rwnd + stcb->asoc.total_flight;
5136f8829a4aSRandall Stewart 
5137f8829a4aSRandall Stewart 	/* Now call the express sack handling */
5138899288aeSRandall Stewart 	sctp_express_handle_sack(stcb, cum_ack, a_rwnd, abort_flag, 0);
5139f8829a4aSRandall Stewart }
5140f8829a4aSRandall Stewart 
5141f8829a4aSRandall Stewart static void
5142f8829a4aSRandall Stewart sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
5143f8829a4aSRandall Stewart     struct sctp_stream_in *strmin)
5144f8829a4aSRandall Stewart {
5145f8829a4aSRandall Stewart 	struct sctp_queued_to_read *ctl, *nctl;
5146f8829a4aSRandall Stewart 	struct sctp_association *asoc;
514783128708SRandall Stewart 	uint16_t tt;
5148f8829a4aSRandall Stewart 
5149f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
5150f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered;
5151f8829a4aSRandall Stewart 	/*
5152f8829a4aSRandall Stewart 	 * First deliver anything prior to and including the stream no that
5153f8829a4aSRandall Stewart 	 * came in
5154f8829a4aSRandall Stewart 	 */
51554a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(ctl, &strmin->inqueue, next, nctl) {
515620b07a4dSMichael Tuexen 		if (SCTP_SSN_GE(tt, ctl->sinfo_ssn)) {
5157f8829a4aSRandall Stewart 			/* this is deliverable now */
5158f8829a4aSRandall Stewart 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5159f8829a4aSRandall Stewart 			/* subtract pending on streams */
5160f8829a4aSRandall Stewart 			asoc->size_on_all_streams -= ctl->length;
5161f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_all_streams);
5162f8829a4aSRandall Stewart 			/* deliver it to at least the delivery-q */
5163f8829a4aSRandall Stewart 			if (stcb->sctp_socket) {
5164b5c16493SMichael Tuexen 				sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
5165f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
5166f8829a4aSRandall Stewart 				    ctl,
5167cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
5168f8829a4aSRandall Stewart 			}
5169f8829a4aSRandall Stewart 		} else {
5170f8829a4aSRandall Stewart 			/* no more delivery now. */
5171f8829a4aSRandall Stewart 			break;
5172f8829a4aSRandall Stewart 		}
5173f8829a4aSRandall Stewart 	}
5174f8829a4aSRandall Stewart 	/*
5175f8829a4aSRandall Stewart 	 * now we must deliver things in queue the normal way  if any are
5176f8829a4aSRandall Stewart 	 * now ready.
5177f8829a4aSRandall Stewart 	 */
5178f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered + 1;
51794a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(ctl, &strmin->inqueue, next, nctl) {
5180f8829a4aSRandall Stewart 		if (tt == ctl->sinfo_ssn) {
5181f8829a4aSRandall Stewart 			/* this is deliverable now */
5182f8829a4aSRandall Stewart 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5183f8829a4aSRandall Stewart 			/* subtract pending on streams */
5184f8829a4aSRandall Stewart 			asoc->size_on_all_streams -= ctl->length;
5185f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_all_streams);
5186f8829a4aSRandall Stewart 			/* deliver it to at least the delivery-q */
5187f8829a4aSRandall Stewart 			strmin->last_sequence_delivered = ctl->sinfo_ssn;
5188f8829a4aSRandall Stewart 			if (stcb->sctp_socket) {
5189b5c16493SMichael Tuexen 				sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
5190f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
5191f8829a4aSRandall Stewart 				    ctl,
5192cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
519377acdc25SRandall Stewart 
5194f8829a4aSRandall Stewart 			}
5195f8829a4aSRandall Stewart 			tt = strmin->last_sequence_delivered + 1;
5196f8829a4aSRandall Stewart 		} else {
5197f8829a4aSRandall Stewart 			break;
5198f8829a4aSRandall Stewart 		}
5199f8829a4aSRandall Stewart 	}
5200f8829a4aSRandall Stewart }
5201f8829a4aSRandall Stewart 
52028933fa13SRandall Stewart static void
52038933fa13SRandall Stewart sctp_flush_reassm_for_str_seq(struct sctp_tcb *stcb,
52048933fa13SRandall Stewart     struct sctp_association *asoc,
52058933fa13SRandall Stewart     uint16_t stream, uint16_t seq)
52068933fa13SRandall Stewart {
52074a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
52088933fa13SRandall Stewart 
52098933fa13SRandall Stewart 	/* For each one on here see if we need to toss it */
52108933fa13SRandall Stewart 	/*
52114a9ef3f8SMichael Tuexen 	 * For now large messages held on the reasmqueue that are complete
52124a9ef3f8SMichael Tuexen 	 * will be tossed too. We could in theory do more work to spin
52134a9ef3f8SMichael Tuexen 	 * through and stop after dumping one msg aka seeing the start of a
52144a9ef3f8SMichael Tuexen 	 * new msg at the head, and call the delivery function... to see if
52154a9ef3f8SMichael Tuexen 	 * it can be delivered... But for now we just dump everything on the
52164a9ef3f8SMichael Tuexen 	 * queue.
52178933fa13SRandall Stewart 	 */
52184a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
52198e71b694SMichael Tuexen 		/*
52204a9ef3f8SMichael Tuexen 		 * Do not toss it if on a different stream or marked for
52214a9ef3f8SMichael Tuexen 		 * unordered delivery in which case the stream sequence
52224a9ef3f8SMichael Tuexen 		 * number has no meaning.
52238e71b694SMichael Tuexen 		 */
52248e71b694SMichael Tuexen 		if ((chk->rec.data.stream_number != stream) ||
52258e71b694SMichael Tuexen 		    ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == SCTP_DATA_UNORDERED)) {
52268933fa13SRandall Stewart 			continue;
52278933fa13SRandall Stewart 		}
52288933fa13SRandall Stewart 		if (chk->rec.data.stream_seq == seq) {
52298933fa13SRandall Stewart 			/* It needs to be tossed */
52308933fa13SRandall Stewart 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
523120b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(chk->rec.data.TSN_seq, asoc->tsn_last_delivered)) {
52324a9ef3f8SMichael Tuexen 				asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
52334a9ef3f8SMichael Tuexen 				asoc->str_of_pdapi = chk->rec.data.stream_number;
52344a9ef3f8SMichael Tuexen 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
52354a9ef3f8SMichael Tuexen 				asoc->fragment_flags = chk->rec.data.rcv_flags;
52368933fa13SRandall Stewart 			}
52378933fa13SRandall Stewart 			asoc->size_on_reasm_queue -= chk->send_size;
52388933fa13SRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_reasm_queue);
52398933fa13SRandall Stewart 
52408933fa13SRandall Stewart 			/* Clear up any stream problem */
524120b07a4dSMichael Tuexen 			if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) != SCTP_DATA_UNORDERED &&
524220b07a4dSMichael Tuexen 			    SCTP_SSN_GT(chk->rec.data.stream_seq, asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered)) {
52438933fa13SRandall Stewart 				/*
52448933fa13SRandall Stewart 				 * We must dump forward this streams
52454a9ef3f8SMichael Tuexen 				 * sequence number if the chunk is not
52464a9ef3f8SMichael Tuexen 				 * unordered that is being skipped. There is
52474a9ef3f8SMichael Tuexen 				 * a chance that if the peer does not
52484a9ef3f8SMichael Tuexen 				 * include the last fragment in its FWD-TSN
52494a9ef3f8SMichael Tuexen 				 * we WILL have a problem here since you
52504a9ef3f8SMichael Tuexen 				 * would have a partial chunk in queue that
52514a9ef3f8SMichael Tuexen 				 * may not be deliverable. Also if a Partial
52524a9ef3f8SMichael Tuexen 				 * delivery API as started the user may get
52534a9ef3f8SMichael Tuexen 				 * a partial chunk. The next read returning
52544a9ef3f8SMichael Tuexen 				 * a new chunk... really ugly but I see no
52554a9ef3f8SMichael Tuexen 				 * way around it! Maybe a notify??
52568933fa13SRandall Stewart 				 */
52574a9ef3f8SMichael Tuexen 				asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered = chk->rec.data.stream_seq;
52588933fa13SRandall Stewart 			}
52598933fa13SRandall Stewart 			if (chk->data) {
52608933fa13SRandall Stewart 				sctp_m_freem(chk->data);
52618933fa13SRandall Stewart 				chk->data = NULL;
52628933fa13SRandall Stewart 			}
5263*689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
526420b07a4dSMichael Tuexen 		} else if (SCTP_SSN_GT(chk->rec.data.stream_seq, seq)) {
52658933fa13SRandall Stewart 			/*
52664a9ef3f8SMichael Tuexen 			 * If the stream_seq is > than the purging one, we
52674a9ef3f8SMichael Tuexen 			 * are done
52688933fa13SRandall Stewart 			 */
52698933fa13SRandall Stewart 			break;
52708933fa13SRandall Stewart 		}
52718933fa13SRandall Stewart 	}
52728933fa13SRandall Stewart }
52738933fa13SRandall Stewart 
52748933fa13SRandall Stewart 
5275f8829a4aSRandall Stewart void
5276f8829a4aSRandall Stewart sctp_handle_forward_tsn(struct sctp_tcb *stcb,
5277b5c16493SMichael Tuexen     struct sctp_forward_tsn_chunk *fwd,
5278b5c16493SMichael Tuexen     int *abort_flag, struct mbuf *m, int offset)
5279f8829a4aSRandall Stewart {
5280f8829a4aSRandall Stewart 	/* The pr-sctp fwd tsn */
5281f8829a4aSRandall Stewart 	/*
5282f8829a4aSRandall Stewart 	 * here we will perform all the data receiver side steps for
5283f8829a4aSRandall Stewart 	 * processing FwdTSN, as required in by pr-sctp draft:
5284f8829a4aSRandall Stewart 	 *
5285f8829a4aSRandall Stewart 	 * Assume we get FwdTSN(x):
5286f8829a4aSRandall Stewart 	 *
5287f8829a4aSRandall Stewart 	 * 1) update local cumTSN to x 2) try to further advance cumTSN to x +
5288f8829a4aSRandall Stewart 	 * others we have 3) examine and update re-ordering queue on
5289f8829a4aSRandall Stewart 	 * pr-in-streams 4) clean up re-assembly queue 5) Send a sack to
5290f8829a4aSRandall Stewart 	 * report where we are.
5291f8829a4aSRandall Stewart 	 */
5292f8829a4aSRandall Stewart 	struct sctp_association *asoc;
52937898f408SRandall Stewart 	uint32_t new_cum_tsn, gap;
52947898f408SRandall Stewart 	unsigned int i, fwd_sz, cumack_set_flag, m_size;
52958933fa13SRandall Stewart 	uint32_t str_seq;
5296f8829a4aSRandall Stewart 	struct sctp_stream_in *strm;
52974a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
52988933fa13SRandall Stewart 	struct sctp_queued_to_read *ctl, *sv;
5299f8829a4aSRandall Stewart 
5300f8829a4aSRandall Stewart 	cumack_set_flag = 0;
5301f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
5302f8829a4aSRandall Stewart 	if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
5303ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1,
5304ad81507eSRandall Stewart 		    "Bad size too small/big fwd-tsn\n");
5305f8829a4aSRandall Stewart 		return;
5306f8829a4aSRandall Stewart 	}
5307f8829a4aSRandall Stewart 	m_size = (stcb->asoc.mapping_array_size << 3);
5308f8829a4aSRandall Stewart 	/*************************************************************/
5309f8829a4aSRandall Stewart 	/* 1. Here we update local cumTSN and shift the bitmap array */
5310f8829a4aSRandall Stewart 	/*************************************************************/
5311f8829a4aSRandall Stewart 	new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
5312f8829a4aSRandall Stewart 
531320b07a4dSMichael Tuexen 	if (SCTP_TSN_GE(asoc->cumulative_tsn, new_cum_tsn)) {
5314f8829a4aSRandall Stewart 		/* Already got there ... */
5315f8829a4aSRandall Stewart 		return;
5316f8829a4aSRandall Stewart 	}
5317f8829a4aSRandall Stewart 	/*
5318f8829a4aSRandall Stewart 	 * now we know the new TSN is more advanced, let's find the actual
5319f8829a4aSRandall Stewart 	 * gap
5320f8829a4aSRandall Stewart 	 */
5321b5c16493SMichael Tuexen 	SCTP_CALC_TSN_TO_GAP(gap, new_cum_tsn, asoc->mapping_array_base_tsn);
532277acdc25SRandall Stewart 	asoc->cumulative_tsn = new_cum_tsn;
53232afb3e84SRandall Stewart 	if (gap >= m_size) {
5324f8829a4aSRandall Stewart 		if ((long)gap > sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv)) {
532517205eccSRandall Stewart 			struct mbuf *oper;
532617205eccSRandall Stewart 
5327f8829a4aSRandall Stewart 			/*
5328f8829a4aSRandall Stewart 			 * out of range (of single byte chunks in the rwnd I
532917205eccSRandall Stewart 			 * give out). This must be an attacker.
5330f8829a4aSRandall Stewart 			 */
533117205eccSRandall Stewart 			*abort_flag = 1;
533217205eccSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
533317205eccSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
533417205eccSRandall Stewart 			if (oper) {
533517205eccSRandall Stewart 				struct sctp_paramhdr *ph;
533617205eccSRandall Stewart 				uint32_t *ippp;
533717205eccSRandall Stewart 
533817205eccSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
533917205eccSRandall Stewart 				    (sizeof(uint32_t) * 3);
534017205eccSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
534117205eccSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
534217205eccSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
534317205eccSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
534417205eccSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_33);
534517205eccSRandall Stewart 				ippp++;
534617205eccSRandall Stewart 				*ippp = asoc->highest_tsn_inside_map;
534717205eccSRandall Stewart 				ippp++;
534817205eccSRandall Stewart 				*ippp = new_cum_tsn;
534917205eccSRandall Stewart 			}
535017205eccSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_33;
535117205eccSRandall Stewart 			sctp_abort_an_association(stcb->sctp_ep, stcb,
5352ceaad40aSRandall Stewart 			    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
5353f8829a4aSRandall Stewart 			return;
5354f8829a4aSRandall Stewart 		}
5355207304d4SRandall Stewart 		SCTP_STAT_INCR(sctps_fwdtsn_map_over);
535677acdc25SRandall Stewart 
53572afb3e84SRandall Stewart 		memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
53582afb3e84SRandall Stewart 		asoc->mapping_array_base_tsn = new_cum_tsn + 1;
535977acdc25SRandall Stewart 		asoc->highest_tsn_inside_map = new_cum_tsn;
536077acdc25SRandall Stewart 
5361b5c16493SMichael Tuexen 		memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
5362830d754dSRandall Stewart 		asoc->highest_tsn_inside_nr_map = new_cum_tsn;
536377acdc25SRandall Stewart 
5364b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
5365f8829a4aSRandall Stewart 			sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
536680fefe0aSRandall Stewart 		}
53672afb3e84SRandall Stewart 	} else {
53682afb3e84SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
53692afb3e84SRandall Stewart 		for (i = 0; i <= gap; i++) {
53707898f408SRandall Stewart 			if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) &&
53717898f408SRandall Stewart 			    !SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, i)) {
53728933fa13SRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, i);
537320b07a4dSMichael Tuexen 				if (SCTP_TSN_GT(asoc->mapping_array_base_tsn + i, asoc->highest_tsn_inside_nr_map)) {
53747898f408SRandall Stewart 					asoc->highest_tsn_inside_nr_map = asoc->mapping_array_base_tsn + i;
5375b5c16493SMichael Tuexen 				}
5376b5c16493SMichael Tuexen 			}
5377b5c16493SMichael Tuexen 		}
5378f8829a4aSRandall Stewart 	}
5379f8829a4aSRandall Stewart 	/*************************************************************/
5380f8829a4aSRandall Stewart 	/* 2. Clear up re-assembly queue                             */
5381f8829a4aSRandall Stewart 	/*************************************************************/
5382f8829a4aSRandall Stewart 	/*
5383f8829a4aSRandall Stewart 	 * First service it if pd-api is up, just in case we can progress it
5384f8829a4aSRandall Stewart 	 * forward
5385f8829a4aSRandall Stewart 	 */
5386f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
5387f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
5388f8829a4aSRandall Stewart 	}
5389f8829a4aSRandall Stewart 	/* For each one on here see if we need to toss it */
5390f8829a4aSRandall Stewart 	/*
53914a9ef3f8SMichael Tuexen 	 * For now large messages held on the reasmqueue that are complete
53924a9ef3f8SMichael Tuexen 	 * will be tossed too. We could in theory do more work to spin
53934a9ef3f8SMichael Tuexen 	 * through and stop after dumping one msg aka seeing the start of a
53944a9ef3f8SMichael Tuexen 	 * new msg at the head, and call the delivery function... to see if
53954a9ef3f8SMichael Tuexen 	 * it can be delivered... But for now we just dump everything on the
53964a9ef3f8SMichael Tuexen 	 * queue.
5397f8829a4aSRandall Stewart 	 */
53984a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
539920b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(new_cum_tsn, chk->rec.data.TSN_seq)) {
5400f8829a4aSRandall Stewart 			/* It needs to be tossed */
5401f8829a4aSRandall Stewart 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
540220b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(chk->rec.data.TSN_seq, asoc->tsn_last_delivered)) {
54034a9ef3f8SMichael Tuexen 				asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
54044a9ef3f8SMichael Tuexen 				asoc->str_of_pdapi = chk->rec.data.stream_number;
54054a9ef3f8SMichael Tuexen 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
54064a9ef3f8SMichael Tuexen 				asoc->fragment_flags = chk->rec.data.rcv_flags;
5407f8829a4aSRandall Stewart 			}
5408f8829a4aSRandall Stewart 			asoc->size_on_reasm_queue -= chk->send_size;
5409f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5410f8829a4aSRandall Stewart 
5411f8829a4aSRandall Stewart 			/* Clear up any stream problem */
541220b07a4dSMichael Tuexen 			if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) != SCTP_DATA_UNORDERED &&
541320b07a4dSMichael Tuexen 			    SCTP_SSN_GT(chk->rec.data.stream_seq, asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered)) {
5414f8829a4aSRandall Stewart 				/*
5415f8829a4aSRandall Stewart 				 * We must dump forward this streams
54164a9ef3f8SMichael Tuexen 				 * sequence number if the chunk is not
54174a9ef3f8SMichael Tuexen 				 * unordered that is being skipped. There is
54184a9ef3f8SMichael Tuexen 				 * a chance that if the peer does not
54194a9ef3f8SMichael Tuexen 				 * include the last fragment in its FWD-TSN
54204a9ef3f8SMichael Tuexen 				 * we WILL have a problem here since you
54214a9ef3f8SMichael Tuexen 				 * would have a partial chunk in queue that
54224a9ef3f8SMichael Tuexen 				 * may not be deliverable. Also if a Partial
54234a9ef3f8SMichael Tuexen 				 * delivery API as started the user may get
54244a9ef3f8SMichael Tuexen 				 * a partial chunk. The next read returning
54254a9ef3f8SMichael Tuexen 				 * a new chunk... really ugly but I see no
54264a9ef3f8SMichael Tuexen 				 * way around it! Maybe a notify??
5427f8829a4aSRandall Stewart 				 */
54284a9ef3f8SMichael Tuexen 				asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered = chk->rec.data.stream_seq;
5429f8829a4aSRandall Stewart 			}
5430f8829a4aSRandall Stewart 			if (chk->data) {
5431f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
5432f8829a4aSRandall Stewart 				chk->data = NULL;
5433f8829a4aSRandall Stewart 			}
5434*689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
5435f8829a4aSRandall Stewart 		} else {
5436f8829a4aSRandall Stewart 			/*
54374a9ef3f8SMichael Tuexen 			 * Ok we have gone beyond the end of the fwd-tsn's
54384a9ef3f8SMichael Tuexen 			 * mark.
5439f8829a4aSRandall Stewart 			 */
5440f8829a4aSRandall Stewart 			break;
5441f8829a4aSRandall Stewart 		}
5442f8829a4aSRandall Stewart 	}
54438933fa13SRandall Stewart 	/*******************************************************/
54448933fa13SRandall Stewart 	/* 3. Update the PR-stream re-ordering queues and fix  */
54458933fa13SRandall Stewart 	/* delivery issues as needed.                       */
54468933fa13SRandall Stewart 	/*******************************************************/
5447f8829a4aSRandall Stewart 	fwd_sz -= sizeof(*fwd);
5448671d309cSRandall Stewart 	if (m && fwd_sz) {
5449f8829a4aSRandall Stewart 		/* New method. */
5450d61a0ae0SRandall Stewart 		unsigned int num_str;
5451671d309cSRandall Stewart 		struct sctp_strseq *stseq, strseqbuf;
5452671d309cSRandall Stewart 
5453671d309cSRandall Stewart 		offset += sizeof(*fwd);
5454f8829a4aSRandall Stewart 
54558933fa13SRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
5456f8829a4aSRandall Stewart 		num_str = fwd_sz / sizeof(struct sctp_strseq);
5457f8829a4aSRandall Stewart 		for (i = 0; i < num_str; i++) {
5458f8829a4aSRandall Stewart 			uint16_t st;
5459f8829a4aSRandall Stewart 
5460671d309cSRandall Stewart 			stseq = (struct sctp_strseq *)sctp_m_getptr(m, offset,
5461671d309cSRandall Stewart 			    sizeof(struct sctp_strseq),
5462671d309cSRandall Stewart 			    (uint8_t *) & strseqbuf);
5463671d309cSRandall Stewart 			offset += sizeof(struct sctp_strseq);
54642afb3e84SRandall Stewart 			if (stseq == NULL) {
5465671d309cSRandall Stewart 				break;
54662afb3e84SRandall Stewart 			}
5467f8829a4aSRandall Stewart 			/* Convert */
5468851b7298SRandall Stewart 			st = ntohs(stseq->stream);
5469851b7298SRandall Stewart 			stseq->stream = st;
5470851b7298SRandall Stewart 			st = ntohs(stseq->sequence);
5471851b7298SRandall Stewart 			stseq->sequence = st;
54728933fa13SRandall Stewart 
5473f8829a4aSRandall Stewart 			/* now process */
54748933fa13SRandall Stewart 
54758933fa13SRandall Stewart 			/*
54768933fa13SRandall Stewart 			 * Ok we now look for the stream/seq on the read
54778933fa13SRandall Stewart 			 * queue where its not all delivered. If we find it
54788933fa13SRandall Stewart 			 * we transmute the read entry into a PDI_ABORTED.
54798933fa13SRandall Stewart 			 */
5480851b7298SRandall Stewart 			if (stseq->stream >= asoc->streamincnt) {
54812afb3e84SRandall Stewart 				/* screwed up streams, stop!  */
54822afb3e84SRandall Stewart 				break;
5483f8829a4aSRandall Stewart 			}
54848933fa13SRandall Stewart 			if ((asoc->str_of_pdapi == stseq->stream) &&
54858933fa13SRandall Stewart 			    (asoc->ssn_of_pdapi == stseq->sequence)) {
54868933fa13SRandall Stewart 				/*
54878933fa13SRandall Stewart 				 * If this is the one we were partially
54888933fa13SRandall Stewart 				 * delivering now then we no longer are.
54898933fa13SRandall Stewart 				 * Note this will change with the reassembly
54908933fa13SRandall Stewart 				 * re-write.
54918933fa13SRandall Stewart 				 */
54928933fa13SRandall Stewart 				asoc->fragmented_delivery_inprogress = 0;
54938933fa13SRandall Stewart 			}
54948933fa13SRandall Stewart 			sctp_flush_reassm_for_str_seq(stcb, asoc, stseq->stream, stseq->sequence);
54958933fa13SRandall Stewart 			TAILQ_FOREACH(ctl, &stcb->sctp_ep->read_queue, next) {
54968933fa13SRandall Stewart 				if ((ctl->sinfo_stream == stseq->stream) &&
54978933fa13SRandall Stewart 				    (ctl->sinfo_ssn == stseq->sequence)) {
54988933fa13SRandall Stewart 					str_seq = (stseq->stream << 16) | stseq->sequence;
54998933fa13SRandall Stewart 					ctl->end_added = 1;
55008933fa13SRandall Stewart 					ctl->pdapi_aborted = 1;
55018933fa13SRandall Stewart 					sv = stcb->asoc.control_pdapi;
55028933fa13SRandall Stewart 					stcb->asoc.control_pdapi = ctl;
5503810ec536SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5504810ec536SMichael Tuexen 					    stcb,
55058933fa13SRandall Stewart 					    SCTP_PARTIAL_DELIVERY_ABORTED,
5506810ec536SMichael Tuexen 					    (void *)&str_seq,
5507810ec536SMichael Tuexen 					    SCTP_SO_NOT_LOCKED);
55088933fa13SRandall Stewart 					stcb->asoc.control_pdapi = sv;
55098933fa13SRandall Stewart 					break;
55108933fa13SRandall Stewart 				} else if ((ctl->sinfo_stream == stseq->stream) &&
551120b07a4dSMichael Tuexen 				    SCTP_SSN_GT(ctl->sinfo_ssn, stseq->sequence)) {
55128933fa13SRandall Stewart 					/* We are past our victim SSN */
55138933fa13SRandall Stewart 					break;
55148933fa13SRandall Stewart 				}
55158933fa13SRandall Stewart 			}
5516851b7298SRandall Stewart 			strm = &asoc->strmin[stseq->stream];
551720b07a4dSMichael Tuexen 			if (SCTP_SSN_GT(stseq->sequence, strm->last_sequence_delivered)) {
5518f8829a4aSRandall Stewart 				/* Update the sequence number */
551920b07a4dSMichael Tuexen 				strm->last_sequence_delivered = stseq->sequence;
5520f8829a4aSRandall Stewart 			}
5521f8829a4aSRandall Stewart 			/* now kick the stream the new way */
552204ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
5523f8829a4aSRandall Stewart 			sctp_kick_prsctp_reorder_queue(stcb, strm);
5524f8829a4aSRandall Stewart 		}
55258933fa13SRandall Stewart 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5526f8829a4aSRandall Stewart 	}
55277898f408SRandall Stewart 	/*
55287898f408SRandall Stewart 	 * Now slide thing forward.
55297898f408SRandall Stewart 	 */
55307898f408SRandall Stewart 	sctp_slide_mapping_arrays(stcb);
55317898f408SRandall Stewart 
553224f52bbdSMichael Tuexen 	if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
5533139bc87fSRandall Stewart 		/* now lets kick out and check for more fragmented delivery */
553404ee05e8SRandall Stewart 		/* sa_ignore NO_NULL_CHK */
5535139bc87fSRandall Stewart 		sctp_deliver_reasm_check(stcb, &stcb->asoc);
5536139bc87fSRandall Stewart 	}
5537f8829a4aSRandall Stewart }
5538