xref: /openbmc/linux/net/sctp/stream.c (revision d570a59c)
1a8386317SXin Long /* SCTP kernel implementation
2a8386317SXin Long  * (C) Copyright IBM Corp. 2001, 2004
3a8386317SXin Long  * Copyright (c) 1999-2000 Cisco, Inc.
4a8386317SXin Long  * Copyright (c) 1999-2001 Motorola, Inc.
5a8386317SXin Long  * Copyright (c) 2001 Intel Corp.
6a8386317SXin Long  *
7a8386317SXin Long  * This file is part of the SCTP kernel implementation
8a8386317SXin Long  *
9a8386317SXin Long  * These functions manipulate sctp tsn mapping array.
10a8386317SXin Long  *
11a8386317SXin Long  * This SCTP implementation is free software;
12a8386317SXin Long  * you can redistribute it and/or modify it under the terms of
13a8386317SXin Long  * the GNU General Public License as published by
14a8386317SXin Long  * the Free Software Foundation; either version 2, or (at your option)
15a8386317SXin Long  * any later version.
16a8386317SXin Long  *
17a8386317SXin Long  * This SCTP implementation is distributed in the hope that it
18a8386317SXin Long  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
19a8386317SXin Long  *                 ************************
20a8386317SXin Long  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21a8386317SXin Long  * See the GNU General Public License for more details.
22a8386317SXin Long  *
23a8386317SXin Long  * You should have received a copy of the GNU General Public License
24a8386317SXin Long  * along with GNU CC; see the file COPYING.  If not, see
25a8386317SXin Long  * <http://www.gnu.org/licenses/>.
26a8386317SXin Long  *
27a8386317SXin Long  * Please send any bug reports or fixes you make to the
28a8386317SXin Long  * email address(es):
29a8386317SXin Long  *    lksctp developers <linux-sctp@vger.kernel.org>
30a8386317SXin Long  *
31a8386317SXin Long  * Written or modified by:
32a8386317SXin Long  *    Xin Long <lucien.xin@gmail.com>
33a8386317SXin Long  */
34a8386317SXin Long 
355bbbbe32SMarcelo Ricardo Leitner #include <linux/list.h>
36a8386317SXin Long #include <net/sctp/sctp.h>
377f9d68acSXin Long #include <net/sctp/sm.h>
385bbbbe32SMarcelo Ricardo Leitner #include <net/sctp/stream_sched.h>
395bbbbe32SMarcelo Ricardo Leitner 
405bbbbe32SMarcelo Ricardo Leitner /* Migrates chunks from stream queues to new stream queues if needed,
415bbbbe32SMarcelo Ricardo Leitner  * but not across associations. Also, removes those chunks to streams
425bbbbe32SMarcelo Ricardo Leitner  * higher than the new max.
435bbbbe32SMarcelo Ricardo Leitner  */
445bbbbe32SMarcelo Ricardo Leitner static void sctp_stream_outq_migrate(struct sctp_stream *stream,
455bbbbe32SMarcelo Ricardo Leitner 				     struct sctp_stream *new, __u16 outcnt)
465bbbbe32SMarcelo Ricardo Leitner {
475bbbbe32SMarcelo Ricardo Leitner 	struct sctp_association *asoc;
485bbbbe32SMarcelo Ricardo Leitner 	struct sctp_chunk *ch, *temp;
495bbbbe32SMarcelo Ricardo Leitner 	struct sctp_outq *outq;
505bbbbe32SMarcelo Ricardo Leitner 	int i;
515bbbbe32SMarcelo Ricardo Leitner 
525bbbbe32SMarcelo Ricardo Leitner 	asoc = container_of(stream, struct sctp_association, stream);
535bbbbe32SMarcelo Ricardo Leitner 	outq = &asoc->outqueue;
545bbbbe32SMarcelo Ricardo Leitner 
555bbbbe32SMarcelo Ricardo Leitner 	list_for_each_entry_safe(ch, temp, &outq->out_chunk_list, list) {
565bbbbe32SMarcelo Ricardo Leitner 		__u16 sid = sctp_chunk_stream_no(ch);
575bbbbe32SMarcelo Ricardo Leitner 
585bbbbe32SMarcelo Ricardo Leitner 		if (sid < outcnt)
595bbbbe32SMarcelo Ricardo Leitner 			continue;
605bbbbe32SMarcelo Ricardo Leitner 
615bbbbe32SMarcelo Ricardo Leitner 		sctp_sched_dequeue_common(outq, ch);
625bbbbe32SMarcelo Ricardo Leitner 		/* No need to call dequeue_done here because
635bbbbe32SMarcelo Ricardo Leitner 		 * the chunks are not scheduled by now.
645bbbbe32SMarcelo Ricardo Leitner 		 */
655bbbbe32SMarcelo Ricardo Leitner 
665bbbbe32SMarcelo Ricardo Leitner 		/* Mark as failed send. */
675bbbbe32SMarcelo Ricardo Leitner 		sctp_chunk_fail(ch, SCTP_ERROR_INV_STRM);
685bbbbe32SMarcelo Ricardo Leitner 		if (asoc->peer.prsctp_capable &&
695bbbbe32SMarcelo Ricardo Leitner 		    SCTP_PR_PRIO_ENABLED(ch->sinfo.sinfo_flags))
705bbbbe32SMarcelo Ricardo Leitner 			asoc->sent_cnt_removable--;
715bbbbe32SMarcelo Ricardo Leitner 
725bbbbe32SMarcelo Ricardo Leitner 		sctp_chunk_free(ch);
735bbbbe32SMarcelo Ricardo Leitner 	}
745bbbbe32SMarcelo Ricardo Leitner 
755bbbbe32SMarcelo Ricardo Leitner 	if (new) {
765bbbbe32SMarcelo Ricardo Leitner 		/* Here we actually move the old ext stuff into the new
775bbbbe32SMarcelo Ricardo Leitner 		 * buffer, because we want to keep it. Then
785bbbbe32SMarcelo Ricardo Leitner 		 * sctp_stream_update will swap ->out pointers.
795bbbbe32SMarcelo Ricardo Leitner 		 */
805bbbbe32SMarcelo Ricardo Leitner 		for (i = 0; i < outcnt; i++) {
815bbbbe32SMarcelo Ricardo Leitner 			kfree(new->out[i].ext);
825bbbbe32SMarcelo Ricardo Leitner 			new->out[i].ext = stream->out[i].ext;
835bbbbe32SMarcelo Ricardo Leitner 			stream->out[i].ext = NULL;
845bbbbe32SMarcelo Ricardo Leitner 		}
855bbbbe32SMarcelo Ricardo Leitner 	}
865bbbbe32SMarcelo Ricardo Leitner 
875bbbbe32SMarcelo Ricardo Leitner 	for (i = outcnt; i < stream->outcnt; i++)
885bbbbe32SMarcelo Ricardo Leitner 		kfree(stream->out[i].ext);
895bbbbe32SMarcelo Ricardo Leitner }
90a8386317SXin Long 
91e090abd0SMarcelo Ricardo Leitner static int sctp_stream_alloc_out(struct sctp_stream *stream, __u16 outcnt,
92e090abd0SMarcelo Ricardo Leitner 				 gfp_t gfp)
93e090abd0SMarcelo Ricardo Leitner {
94e090abd0SMarcelo Ricardo Leitner 	struct sctp_stream_out *out;
95e090abd0SMarcelo Ricardo Leitner 
96e090abd0SMarcelo Ricardo Leitner 	out = kmalloc_array(outcnt, sizeof(*out), gfp);
97e090abd0SMarcelo Ricardo Leitner 	if (!out)
98e090abd0SMarcelo Ricardo Leitner 		return -ENOMEM;
99e090abd0SMarcelo Ricardo Leitner 
100e090abd0SMarcelo Ricardo Leitner 	if (stream->out) {
101e090abd0SMarcelo Ricardo Leitner 		memcpy(out, stream->out, min(outcnt, stream->outcnt) *
102e090abd0SMarcelo Ricardo Leitner 					 sizeof(*out));
103e090abd0SMarcelo Ricardo Leitner 		kfree(stream->out);
104e090abd0SMarcelo Ricardo Leitner 	}
105e090abd0SMarcelo Ricardo Leitner 
106e090abd0SMarcelo Ricardo Leitner 	if (outcnt > stream->outcnt)
107e090abd0SMarcelo Ricardo Leitner 		memset(out + stream->outcnt, 0,
108e090abd0SMarcelo Ricardo Leitner 		       (outcnt - stream->outcnt) * sizeof(*out));
109e090abd0SMarcelo Ricardo Leitner 
110e090abd0SMarcelo Ricardo Leitner 	stream->out = out;
111e090abd0SMarcelo Ricardo Leitner 
112e090abd0SMarcelo Ricardo Leitner 	return 0;
113e090abd0SMarcelo Ricardo Leitner }
114e090abd0SMarcelo Ricardo Leitner 
1151fdb8d8fSMarcelo Ricardo Leitner static int sctp_stream_alloc_in(struct sctp_stream *stream, __u16 incnt,
1161fdb8d8fSMarcelo Ricardo Leitner 				gfp_t gfp)
1171fdb8d8fSMarcelo Ricardo Leitner {
1181fdb8d8fSMarcelo Ricardo Leitner 	struct sctp_stream_in *in;
1191fdb8d8fSMarcelo Ricardo Leitner 
1201fdb8d8fSMarcelo Ricardo Leitner 	in = kmalloc_array(incnt, sizeof(*stream->in), gfp);
1211fdb8d8fSMarcelo Ricardo Leitner 
1221fdb8d8fSMarcelo Ricardo Leitner 	if (!in)
1231fdb8d8fSMarcelo Ricardo Leitner 		return -ENOMEM;
1241fdb8d8fSMarcelo Ricardo Leitner 
1251fdb8d8fSMarcelo Ricardo Leitner 	if (stream->in) {
1261fdb8d8fSMarcelo Ricardo Leitner 		memcpy(in, stream->in, min(incnt, stream->incnt) *
1271fdb8d8fSMarcelo Ricardo Leitner 				       sizeof(*in));
1281fdb8d8fSMarcelo Ricardo Leitner 		kfree(stream->in);
1291fdb8d8fSMarcelo Ricardo Leitner 	}
1301fdb8d8fSMarcelo Ricardo Leitner 
1311fdb8d8fSMarcelo Ricardo Leitner 	if (incnt > stream->incnt)
1321fdb8d8fSMarcelo Ricardo Leitner 		memset(in + stream->incnt, 0,
1331fdb8d8fSMarcelo Ricardo Leitner 		       (incnt - stream->incnt) * sizeof(*in));
1341fdb8d8fSMarcelo Ricardo Leitner 
1351fdb8d8fSMarcelo Ricardo Leitner 	stream->in = in;
1361fdb8d8fSMarcelo Ricardo Leitner 
1371fdb8d8fSMarcelo Ricardo Leitner 	return 0;
1381fdb8d8fSMarcelo Ricardo Leitner }
1391fdb8d8fSMarcelo Ricardo Leitner 
140ff356414SXin Long int sctp_stream_init(struct sctp_stream *stream, __u16 outcnt, __u16 incnt,
141ff356414SXin Long 		     gfp_t gfp)
142a8386317SXin Long {
1435bbbbe32SMarcelo Ricardo Leitner 	struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
1445bbbbe32SMarcelo Ricardo Leitner 	int i, ret = 0;
1453dbcc105SXin Long 
1461ae2eaaaSMarcelo Ricardo Leitner 	gfp |= __GFP_NOWARN;
1471ae2eaaaSMarcelo Ricardo Leitner 
1483dbcc105SXin Long 	/* Initial stream->out size may be very big, so free it and alloc
1491ae2eaaaSMarcelo Ricardo Leitner 	 * a new one with new outcnt to save memory if needed.
1503dbcc105SXin Long 	 */
1511ae2eaaaSMarcelo Ricardo Leitner 	if (outcnt == stream->outcnt)
1521ae2eaaaSMarcelo Ricardo Leitner 		goto in;
1531ae2eaaaSMarcelo Ricardo Leitner 
1545bbbbe32SMarcelo Ricardo Leitner 	/* Filter out chunks queued on streams that won't exist anymore */
1555bbbbe32SMarcelo Ricardo Leitner 	sched->unsched_all(stream);
1565bbbbe32SMarcelo Ricardo Leitner 	sctp_stream_outq_migrate(stream, NULL, outcnt);
1575bbbbe32SMarcelo Ricardo Leitner 	sched->sched_all(stream);
1585bbbbe32SMarcelo Ricardo Leitner 
159e090abd0SMarcelo Ricardo Leitner 	i = sctp_stream_alloc_out(stream, outcnt, gfp);
160e090abd0SMarcelo Ricardo Leitner 	if (i)
161e090abd0SMarcelo Ricardo Leitner 		return i;
1623dbcc105SXin Long 
163ff356414SXin Long 	stream->outcnt = outcnt;
1643dbcc105SXin Long 	for (i = 0; i < stream->outcnt; i++)
1653dbcc105SXin Long 		stream->out[i].state = SCTP_STREAM_OPEN;
1663dbcc105SXin Long 
1675bbbbe32SMarcelo Ricardo Leitner 	sched->init(stream);
1685bbbbe32SMarcelo Ricardo Leitner 
1691ae2eaaaSMarcelo Ricardo Leitner in:
170ff356414SXin Long 	if (!incnt)
1715bbbbe32SMarcelo Ricardo Leitner 		goto out;
172ff356414SXin Long 
1731fdb8d8fSMarcelo Ricardo Leitner 	i = sctp_stream_alloc_in(stream, incnt, gfp);
1741fdb8d8fSMarcelo Ricardo Leitner 	if (i) {
1755bbbbe32SMarcelo Ricardo Leitner 		ret = -ENOMEM;
1765bbbbe32SMarcelo Ricardo Leitner 		goto free;
177a8386317SXin Long 	}
178a8386317SXin Long 
179ff356414SXin Long 	stream->incnt = incnt;
1805bbbbe32SMarcelo Ricardo Leitner 	goto out;
181ff356414SXin Long 
1825bbbbe32SMarcelo Ricardo Leitner free:
1835bbbbe32SMarcelo Ricardo Leitner 	sched->free(stream);
1845bbbbe32SMarcelo Ricardo Leitner 	kfree(stream->out);
1855bbbbe32SMarcelo Ricardo Leitner 	stream->out = NULL;
1865bbbbe32SMarcelo Ricardo Leitner out:
1875bbbbe32SMarcelo Ricardo Leitner 	return ret;
188a8386317SXin Long }
189a8386317SXin Long 
190f952be79SMarcelo Ricardo Leitner int sctp_stream_init_ext(struct sctp_stream *stream, __u16 sid)
191f952be79SMarcelo Ricardo Leitner {
192f952be79SMarcelo Ricardo Leitner 	struct sctp_stream_out_ext *soute;
193f952be79SMarcelo Ricardo Leitner 
194f952be79SMarcelo Ricardo Leitner 	soute = kzalloc(sizeof(*soute), GFP_KERNEL);
195f952be79SMarcelo Ricardo Leitner 	if (!soute)
196f952be79SMarcelo Ricardo Leitner 		return -ENOMEM;
197f952be79SMarcelo Ricardo Leitner 	stream->out[sid].ext = soute;
198f952be79SMarcelo Ricardo Leitner 
1995bbbbe32SMarcelo Ricardo Leitner 	return sctp_sched_init_sid(stream, sid, GFP_KERNEL);
200f952be79SMarcelo Ricardo Leitner }
201f952be79SMarcelo Ricardo Leitner 
202a8386317SXin Long void sctp_stream_free(struct sctp_stream *stream)
203a8386317SXin Long {
2045bbbbe32SMarcelo Ricardo Leitner 	struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
205f952be79SMarcelo Ricardo Leitner 	int i;
206f952be79SMarcelo Ricardo Leitner 
2075bbbbe32SMarcelo Ricardo Leitner 	sched->free(stream);
208f952be79SMarcelo Ricardo Leitner 	for (i = 0; i < stream->outcnt; i++)
209f952be79SMarcelo Ricardo Leitner 		kfree(stream->out[i].ext);
210a8386317SXin Long 	kfree(stream->out);
211a8386317SXin Long 	kfree(stream->in);
212a8386317SXin Long }
213a8386317SXin Long 
214a8386317SXin Long void sctp_stream_clear(struct sctp_stream *stream)
215a8386317SXin Long {
216a8386317SXin Long 	int i;
217a8386317SXin Long 
218a8386317SXin Long 	for (i = 0; i < stream->outcnt; i++)
219a8386317SXin Long 		stream->out[i].ssn = 0;
220a8386317SXin Long 
221a8386317SXin Long 	for (i = 0; i < stream->incnt; i++)
222a8386317SXin Long 		stream->in[i].ssn = 0;
223a8386317SXin Long }
2247f9d68acSXin Long 
225cee360abSXin Long void sctp_stream_update(struct sctp_stream *stream, struct sctp_stream *new)
226cee360abSXin Long {
2275bbbbe32SMarcelo Ricardo Leitner 	struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
2285bbbbe32SMarcelo Ricardo Leitner 
2295bbbbe32SMarcelo Ricardo Leitner 	sched->unsched_all(stream);
2305bbbbe32SMarcelo Ricardo Leitner 	sctp_stream_outq_migrate(stream, new, new->outcnt);
231cee360abSXin Long 	sctp_stream_free(stream);
232cee360abSXin Long 
233cee360abSXin Long 	stream->out = new->out;
234cee360abSXin Long 	stream->in  = new->in;
235cee360abSXin Long 	stream->outcnt = new->outcnt;
236cee360abSXin Long 	stream->incnt  = new->incnt;
237cee360abSXin Long 
2385bbbbe32SMarcelo Ricardo Leitner 	sched->sched_all(stream);
2395bbbbe32SMarcelo Ricardo Leitner 
240cee360abSXin Long 	new->out = NULL;
241cee360abSXin Long 	new->in  = NULL;
242cee360abSXin Long }
243cee360abSXin Long 
2447f9d68acSXin Long static int sctp_send_reconf(struct sctp_association *asoc,
2457f9d68acSXin Long 			    struct sctp_chunk *chunk)
2467f9d68acSXin Long {
2477f9d68acSXin Long 	struct net *net = sock_net(asoc->base.sk);
2487f9d68acSXin Long 	int retval = 0;
2497f9d68acSXin Long 
2507f9d68acSXin Long 	retval = sctp_primitive_RECONF(net, asoc, chunk);
2517f9d68acSXin Long 	if (retval)
2527f9d68acSXin Long 		sctp_chunk_free(chunk);
2537f9d68acSXin Long 
2547f9d68acSXin Long 	return retval;
2557f9d68acSXin Long }
2567f9d68acSXin Long 
257d570a59cSXin Long static bool sctp_stream_outq_is_empty(struct sctp_stream *stream,
258d570a59cSXin Long 				      __u16 str_nums, __be16 *str_list)
259d570a59cSXin Long {
260d570a59cSXin Long 	struct sctp_association *asoc;
261d570a59cSXin Long 	__u16 i;
262d570a59cSXin Long 
263d570a59cSXin Long 	asoc = container_of(stream, struct sctp_association, stream);
264d570a59cSXin Long 	if (!asoc->outqueue.out_qlen)
265d570a59cSXin Long 		return true;
266d570a59cSXin Long 
267d570a59cSXin Long 	if (!str_nums)
268d570a59cSXin Long 		return false;
269d570a59cSXin Long 
270d570a59cSXin Long 	for (i = 0; i < str_nums; i++) {
271d570a59cSXin Long 		__u16 sid = ntohs(str_list[i]);
272d570a59cSXin Long 
273d570a59cSXin Long 		if (stream->out[sid].ext &&
274d570a59cSXin Long 		    !list_empty(&stream->out[sid].ext->outq))
275d570a59cSXin Long 			return false;
276d570a59cSXin Long 	}
277d570a59cSXin Long 
278d570a59cSXin Long 	return true;
279d570a59cSXin Long }
280d570a59cSXin Long 
2817f9d68acSXin Long int sctp_send_reset_streams(struct sctp_association *asoc,
2827f9d68acSXin Long 			    struct sctp_reset_streams *params)
2837f9d68acSXin Long {
284cee360abSXin Long 	struct sctp_stream *stream = &asoc->stream;
2857f9d68acSXin Long 	__u16 i, str_nums, *str_list;
2867f9d68acSXin Long 	struct sctp_chunk *chunk;
2877f9d68acSXin Long 	int retval = -EINVAL;
2881da4fc97SXin Long 	__be16 *nstr_list;
2897f9d68acSXin Long 	bool out, in;
2907f9d68acSXin Long 
2917f9d68acSXin Long 	if (!asoc->peer.reconf_capable ||
2927f9d68acSXin Long 	    !(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ)) {
2937f9d68acSXin Long 		retval = -ENOPROTOOPT;
2947f9d68acSXin Long 		goto out;
2957f9d68acSXin Long 	}
2967f9d68acSXin Long 
2977f9d68acSXin Long 	if (asoc->strreset_outstanding) {
2987f9d68acSXin Long 		retval = -EINPROGRESS;
2997f9d68acSXin Long 		goto out;
3007f9d68acSXin Long 	}
3017f9d68acSXin Long 
3027f9d68acSXin Long 	out = params->srs_flags & SCTP_STREAM_RESET_OUTGOING;
3037f9d68acSXin Long 	in  = params->srs_flags & SCTP_STREAM_RESET_INCOMING;
3047f9d68acSXin Long 	if (!out && !in)
3057f9d68acSXin Long 		goto out;
3067f9d68acSXin Long 
3077f9d68acSXin Long 	str_nums = params->srs_number_streams;
3087f9d68acSXin Long 	str_list = params->srs_stream_list;
309423852f8SXin Long 	if (str_nums) {
310423852f8SXin Long 		int param_len = 0;
311423852f8SXin Long 
312423852f8SXin Long 		if (out) {
3137f9d68acSXin Long 			for (i = 0; i < str_nums; i++)
3147f9d68acSXin Long 				if (str_list[i] >= stream->outcnt)
3157f9d68acSXin Long 					goto out;
3167f9d68acSXin Long 
317423852f8SXin Long 			param_len = str_nums * sizeof(__u16) +
318423852f8SXin Long 				    sizeof(struct sctp_strreset_outreq);
319423852f8SXin Long 		}
320423852f8SXin Long 
321423852f8SXin Long 		if (in) {
3227f9d68acSXin Long 			for (i = 0; i < str_nums; i++)
3237f9d68acSXin Long 				if (str_list[i] >= stream->incnt)
3247f9d68acSXin Long 					goto out;
3257f9d68acSXin Long 
326423852f8SXin Long 			param_len += str_nums * sizeof(__u16) +
327423852f8SXin Long 				     sizeof(struct sctp_strreset_inreq);
328423852f8SXin Long 		}
329423852f8SXin Long 
330423852f8SXin Long 		if (param_len > SCTP_MAX_CHUNK_LEN -
331423852f8SXin Long 				sizeof(struct sctp_reconf_chunk))
332423852f8SXin Long 			goto out;
333423852f8SXin Long 	}
334423852f8SXin Long 
3351da4fc97SXin Long 	nstr_list = kcalloc(str_nums, sizeof(__be16), GFP_KERNEL);
3361da4fc97SXin Long 	if (!nstr_list) {
3371da4fc97SXin Long 		retval = -ENOMEM;
3381da4fc97SXin Long 		goto out;
3391da4fc97SXin Long 	}
34016e1a919SXin Long 
34116e1a919SXin Long 	for (i = 0; i < str_nums; i++)
3421da4fc97SXin Long 		nstr_list[i] = htons(str_list[i]);
3431da4fc97SXin Long 
344d570a59cSXin Long 	if (out && !sctp_stream_outq_is_empty(stream, str_nums, nstr_list)) {
345d570a59cSXin Long 		retval = -EAGAIN;
346d570a59cSXin Long 		goto out;
347d570a59cSXin Long 	}
348d570a59cSXin Long 
3491da4fc97SXin Long 	chunk = sctp_make_strreset_req(asoc, str_nums, nstr_list, out, in);
3501da4fc97SXin Long 
3511da4fc97SXin Long 	kfree(nstr_list);
35216e1a919SXin Long 
353119aecbaSXin Long 	if (!chunk) {
354119aecbaSXin Long 		retval = -ENOMEM;
3557f9d68acSXin Long 		goto out;
356119aecbaSXin Long 	}
3577f9d68acSXin Long 
3587f9d68acSXin Long 	if (out) {
3597f9d68acSXin Long 		if (str_nums)
3607f9d68acSXin Long 			for (i = 0; i < str_nums; i++)
3617f9d68acSXin Long 				stream->out[str_list[i]].state =
3627f9d68acSXin Long 						       SCTP_STREAM_CLOSED;
3637f9d68acSXin Long 		else
3647f9d68acSXin Long 			for (i = 0; i < stream->outcnt; i++)
3657f9d68acSXin Long 				stream->out[i].state = SCTP_STREAM_CLOSED;
3667f9d68acSXin Long 	}
3677f9d68acSXin Long 
3687f9d68acSXin Long 	asoc->strreset_chunk = chunk;
3697f9d68acSXin Long 	sctp_chunk_hold(asoc->strreset_chunk);
3707f9d68acSXin Long 
3717f9d68acSXin Long 	retval = sctp_send_reconf(asoc, chunk);
3727f9d68acSXin Long 	if (retval) {
3737f9d68acSXin Long 		sctp_chunk_put(asoc->strreset_chunk);
3747f9d68acSXin Long 		asoc->strreset_chunk = NULL;
375119aecbaSXin Long 		if (!out)
376119aecbaSXin Long 			goto out;
377119aecbaSXin Long 
378119aecbaSXin Long 		if (str_nums)
379119aecbaSXin Long 			for (i = 0; i < str_nums; i++)
380119aecbaSXin Long 				stream->out[str_list[i]].state =
381119aecbaSXin Long 						       SCTP_STREAM_OPEN;
382119aecbaSXin Long 		else
383119aecbaSXin Long 			for (i = 0; i < stream->outcnt; i++)
384119aecbaSXin Long 				stream->out[i].state = SCTP_STREAM_OPEN;
385119aecbaSXin Long 
386119aecbaSXin Long 		goto out;
3877f9d68acSXin Long 	}
3887f9d68acSXin Long 
389119aecbaSXin Long 	asoc->strreset_outstanding = out + in;
390119aecbaSXin Long 
3917f9d68acSXin Long out:
3927f9d68acSXin Long 	return retval;
3937f9d68acSXin Long }
394a92ce1a4SXin Long 
395a92ce1a4SXin Long int sctp_send_reset_assoc(struct sctp_association *asoc)
396a92ce1a4SXin Long {
397cee360abSXin Long 	struct sctp_stream *stream = &asoc->stream;
398a92ce1a4SXin Long 	struct sctp_chunk *chunk = NULL;
399a92ce1a4SXin Long 	int retval;
400a92ce1a4SXin Long 	__u16 i;
401a92ce1a4SXin Long 
402a92ce1a4SXin Long 	if (!asoc->peer.reconf_capable ||
403a92ce1a4SXin Long 	    !(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
404a92ce1a4SXin Long 		return -ENOPROTOOPT;
405a92ce1a4SXin Long 
406a92ce1a4SXin Long 	if (asoc->strreset_outstanding)
407a92ce1a4SXin Long 		return -EINPROGRESS;
408a92ce1a4SXin Long 
409a92ce1a4SXin Long 	chunk = sctp_make_strreset_tsnreq(asoc);
410a92ce1a4SXin Long 	if (!chunk)
411a92ce1a4SXin Long 		return -ENOMEM;
412a92ce1a4SXin Long 
413a92ce1a4SXin Long 	/* Block further xmit of data until this request is completed */
414cee360abSXin Long 	for (i = 0; i < stream->outcnt; i++)
415cee360abSXin Long 		stream->out[i].state = SCTP_STREAM_CLOSED;
416a92ce1a4SXin Long 
417a92ce1a4SXin Long 	asoc->strreset_chunk = chunk;
418a92ce1a4SXin Long 	sctp_chunk_hold(asoc->strreset_chunk);
419a92ce1a4SXin Long 
420a92ce1a4SXin Long 	retval = sctp_send_reconf(asoc, chunk);
421a92ce1a4SXin Long 	if (retval) {
422a92ce1a4SXin Long 		sctp_chunk_put(asoc->strreset_chunk);
423a92ce1a4SXin Long 		asoc->strreset_chunk = NULL;
424a92ce1a4SXin Long 
425cee360abSXin Long 		for (i = 0; i < stream->outcnt; i++)
426cee360abSXin Long 			stream->out[i].state = SCTP_STREAM_OPEN;
427a92ce1a4SXin Long 
428a92ce1a4SXin Long 		return retval;
429a92ce1a4SXin Long 	}
430a92ce1a4SXin Long 
431a92ce1a4SXin Long 	asoc->strreset_outstanding = 1;
432a92ce1a4SXin Long 
433a92ce1a4SXin Long 	return 0;
434a92ce1a4SXin Long }
435242bd2d5SXin Long 
436242bd2d5SXin Long int sctp_send_add_streams(struct sctp_association *asoc,
437242bd2d5SXin Long 			  struct sctp_add_streams *params)
438242bd2d5SXin Long {
439cee360abSXin Long 	struct sctp_stream *stream = &asoc->stream;
440242bd2d5SXin Long 	struct sctp_chunk *chunk = NULL;
441dc82673fSWei Yongjun 	int retval;
442242bd2d5SXin Long 	__u32 outcnt, incnt;
443242bd2d5SXin Long 	__u16 out, in;
444242bd2d5SXin Long 
445242bd2d5SXin Long 	if (!asoc->peer.reconf_capable ||
446242bd2d5SXin Long 	    !(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
447242bd2d5SXin Long 		retval = -ENOPROTOOPT;
448242bd2d5SXin Long 		goto out;
449242bd2d5SXin Long 	}
450242bd2d5SXin Long 
451242bd2d5SXin Long 	if (asoc->strreset_outstanding) {
452242bd2d5SXin Long 		retval = -EINPROGRESS;
453242bd2d5SXin Long 		goto out;
454242bd2d5SXin Long 	}
455242bd2d5SXin Long 
456242bd2d5SXin Long 	out = params->sas_outstrms;
457242bd2d5SXin Long 	in  = params->sas_instrms;
458242bd2d5SXin Long 	outcnt = stream->outcnt + out;
459242bd2d5SXin Long 	incnt = stream->incnt + in;
460242bd2d5SXin Long 	if (outcnt > SCTP_MAX_STREAM || incnt > SCTP_MAX_STREAM ||
461242bd2d5SXin Long 	    (!out && !in)) {
462242bd2d5SXin Long 		retval = -EINVAL;
463242bd2d5SXin Long 		goto out;
464242bd2d5SXin Long 	}
465242bd2d5SXin Long 
466242bd2d5SXin Long 	if (out) {
467e090abd0SMarcelo Ricardo Leitner 		retval = sctp_stream_alloc_out(stream, outcnt, GFP_KERNEL);
468e090abd0SMarcelo Ricardo Leitner 		if (retval)
469242bd2d5SXin Long 			goto out;
470242bd2d5SXin Long 	}
471242bd2d5SXin Long 
472242bd2d5SXin Long 	chunk = sctp_make_strreset_addstrm(asoc, out, in);
473dc82673fSWei Yongjun 	if (!chunk) {
474dc82673fSWei Yongjun 		retval = -ENOMEM;
475242bd2d5SXin Long 		goto out;
476dc82673fSWei Yongjun 	}
477242bd2d5SXin Long 
478242bd2d5SXin Long 	asoc->strreset_chunk = chunk;
479242bd2d5SXin Long 	sctp_chunk_hold(asoc->strreset_chunk);
480242bd2d5SXin Long 
481242bd2d5SXin Long 	retval = sctp_send_reconf(asoc, chunk);
482242bd2d5SXin Long 	if (retval) {
483242bd2d5SXin Long 		sctp_chunk_put(asoc->strreset_chunk);
484242bd2d5SXin Long 		asoc->strreset_chunk = NULL;
485242bd2d5SXin Long 		goto out;
486242bd2d5SXin Long 	}
487242bd2d5SXin Long 
488242bd2d5SXin Long 	stream->incnt = incnt;
489242bd2d5SXin Long 	stream->outcnt = outcnt;
490242bd2d5SXin Long 
491242bd2d5SXin Long 	asoc->strreset_outstanding = !!out + !!in;
492242bd2d5SXin Long 
493242bd2d5SXin Long out:
494242bd2d5SXin Long 	return retval;
495242bd2d5SXin Long }
49681054476SXin Long 
4973c918704SXin Long static struct sctp_paramhdr *sctp_chunk_lookup_strreset_param(
4981da4fc97SXin Long 			struct sctp_association *asoc, __be32 resp_seq,
49950a41591SXin Long 			__be16 type)
50081054476SXin Long {
50181054476SXin Long 	struct sctp_chunk *chunk = asoc->strreset_chunk;
50281054476SXin Long 	struct sctp_reconf_chunk *hdr;
50381054476SXin Long 	union sctp_params param;
50481054476SXin Long 
50550a41591SXin Long 	if (!chunk)
50681054476SXin Long 		return NULL;
50781054476SXin Long 
50881054476SXin Long 	hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
50981054476SXin Long 	sctp_walk_params(param, hdr, params) {
51081054476SXin Long 		/* sctp_strreset_tsnreq is actually the basic structure
51181054476SXin Long 		 * of all stream reconf params, so it's safe to use it
51281054476SXin Long 		 * to access request_seq.
51381054476SXin Long 		 */
51481054476SXin Long 		struct sctp_strreset_tsnreq *req = param.v;
51581054476SXin Long 
51650a41591SXin Long 		if ((!resp_seq || req->request_seq == resp_seq) &&
51750a41591SXin Long 		    (!type || type == req->param_hdr.type))
51881054476SXin Long 			return param.v;
51981054476SXin Long 	}
52081054476SXin Long 
52181054476SXin Long 	return NULL;
52281054476SXin Long }
52381054476SXin Long 
524e4dc99c7SXin Long static void sctp_update_strreset_result(struct sctp_association *asoc,
525e4dc99c7SXin Long 					__u32 result)
526e4dc99c7SXin Long {
527e4dc99c7SXin Long 	asoc->strreset_result[1] = asoc->strreset_result[0];
528e4dc99c7SXin Long 	asoc->strreset_result[0] = result;
529e4dc99c7SXin Long }
530e4dc99c7SXin Long 
53181054476SXin Long struct sctp_chunk *sctp_process_strreset_outreq(
53281054476SXin Long 				struct sctp_association *asoc,
53381054476SXin Long 				union sctp_params param,
53481054476SXin Long 				struct sctp_ulpevent **evp)
53581054476SXin Long {
53681054476SXin Long 	struct sctp_strreset_outreq *outreq = param.v;
537cee360abSXin Long 	struct sctp_stream *stream = &asoc->stream;
53881054476SXin Long 	__u32 result = SCTP_STRRESET_DENIED;
5391da4fc97SXin Long 	__u16 i, nums, flags = 0;
5401da4fc97SXin Long 	__be16 *str_p = NULL;
54181054476SXin Long 	__u32 request_seq;
54281054476SXin Long 
54381054476SXin Long 	request_seq = ntohl(outreq->request_seq);
54481054476SXin Long 
54581054476SXin Long 	if (ntohl(outreq->send_reset_at_tsn) >
54681054476SXin Long 	    sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map)) {
54781054476SXin Long 		result = SCTP_STRRESET_IN_PROGRESS;
548e4dc99c7SXin Long 		goto err;
54981054476SXin Long 	}
55081054476SXin Long 
551e4dc99c7SXin Long 	if (TSN_lt(asoc->strreset_inseq, request_seq) ||
552e4dc99c7SXin Long 	    TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
55381054476SXin Long 		result = SCTP_STRRESET_ERR_BAD_SEQNO;
554e4dc99c7SXin Long 		goto err;
555e4dc99c7SXin Long 	} else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
556e4dc99c7SXin Long 		i = asoc->strreset_inseq - request_seq - 1;
557e4dc99c7SXin Long 		result = asoc->strreset_result[i];
558e4dc99c7SXin Long 		goto err;
55981054476SXin Long 	}
560e4dc99c7SXin Long 	asoc->strreset_inseq++;
56181054476SXin Long 
56281054476SXin Long 	/* Check strreset_enable after inseq inc, as sender cannot tell
56381054476SXin Long 	 * the peer doesn't enable strreset after receiving response with
56481054476SXin Long 	 * result denied, as well as to keep consistent with bsd.
56581054476SXin Long 	 */
56681054476SXin Long 	if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
56781054476SXin Long 		goto out;
56881054476SXin Long 
56981054476SXin Long 	if (asoc->strreset_chunk) {
57050a41591SXin Long 		if (!sctp_chunk_lookup_strreset_param(
57150a41591SXin Long 				asoc, outreq->response_seq,
57250a41591SXin Long 				SCTP_PARAM_RESET_IN_REQUEST)) {
57381054476SXin Long 			/* same process with outstanding isn't 0 */
57481054476SXin Long 			result = SCTP_STRRESET_ERR_IN_PROGRESS;
57581054476SXin Long 			goto out;
57681054476SXin Long 		}
57781054476SXin Long 
57881054476SXin Long 		asoc->strreset_outstanding--;
57981054476SXin Long 		asoc->strreset_outseq++;
58081054476SXin Long 
58181054476SXin Long 		if (!asoc->strreset_outstanding) {
58250a41591SXin Long 			struct sctp_transport *t;
58350a41591SXin Long 
58481054476SXin Long 			t = asoc->strreset_chunk->transport;
58581054476SXin Long 			if (del_timer(&t->reconf_timer))
58681054476SXin Long 				sctp_transport_put(t);
58781054476SXin Long 
58881054476SXin Long 			sctp_chunk_put(asoc->strreset_chunk);
58981054476SXin Long 			asoc->strreset_chunk = NULL;
59081054476SXin Long 		}
59181054476SXin Long 
59281054476SXin Long 		flags = SCTP_STREAM_RESET_INCOMING_SSN;
59381054476SXin Long 	}
59481054476SXin Long 
5953aa623daSXin Long 	nums = (ntohs(param.p->length) - sizeof(*outreq)) / sizeof(__u16);
59681054476SXin Long 	if (nums) {
59781054476SXin Long 		str_p = outreq->list_of_streams;
59881054476SXin Long 		for (i = 0; i < nums; i++) {
59981054476SXin Long 			if (ntohs(str_p[i]) >= stream->incnt) {
60081054476SXin Long 				result = SCTP_STRRESET_ERR_WRONG_SSN;
60181054476SXin Long 				goto out;
60281054476SXin Long 			}
60381054476SXin Long 		}
60481054476SXin Long 
60581054476SXin Long 		for (i = 0; i < nums; i++)
60681054476SXin Long 			stream->in[ntohs(str_p[i])].ssn = 0;
60781054476SXin Long 	} else {
60881054476SXin Long 		for (i = 0; i < stream->incnt; i++)
60981054476SXin Long 			stream->in[i].ssn = 0;
61081054476SXin Long 	}
61181054476SXin Long 
61281054476SXin Long 	result = SCTP_STRRESET_PERFORMED;
61381054476SXin Long 
61481054476SXin Long 	*evp = sctp_ulpevent_make_stream_reset_event(asoc,
61581054476SXin Long 		flags | SCTP_STREAM_RESET_OUTGOING_SSN, nums, str_p,
61681054476SXin Long 		GFP_ATOMIC);
61781054476SXin Long 
61881054476SXin Long out:
619e4dc99c7SXin Long 	sctp_update_strreset_result(asoc, result);
620e4dc99c7SXin Long err:
62181054476SXin Long 	return sctp_make_strreset_resp(asoc, result, request_seq);
62281054476SXin Long }
62316e1a919SXin Long 
62416e1a919SXin Long struct sctp_chunk *sctp_process_strreset_inreq(
62516e1a919SXin Long 				struct sctp_association *asoc,
62616e1a919SXin Long 				union sctp_params param,
62716e1a919SXin Long 				struct sctp_ulpevent **evp)
62816e1a919SXin Long {
62916e1a919SXin Long 	struct sctp_strreset_inreq *inreq = param.v;
630cee360abSXin Long 	struct sctp_stream *stream = &asoc->stream;
63116e1a919SXin Long 	__u32 result = SCTP_STRRESET_DENIED;
63216e1a919SXin Long 	struct sctp_chunk *chunk = NULL;
63316e1a919SXin Long 	__u32 request_seq;
6341da4fc97SXin Long 	__u16 i, nums;
6351da4fc97SXin Long 	__be16 *str_p;
63616e1a919SXin Long 
63716e1a919SXin Long 	request_seq = ntohl(inreq->request_seq);
638d0f025e6SXin Long 	if (TSN_lt(asoc->strreset_inseq, request_seq) ||
639d0f025e6SXin Long 	    TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
64016e1a919SXin Long 		result = SCTP_STRRESET_ERR_BAD_SEQNO;
641d0f025e6SXin Long 		goto err;
642d0f025e6SXin Long 	} else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
643d0f025e6SXin Long 		i = asoc->strreset_inseq - request_seq - 1;
644d0f025e6SXin Long 		result = asoc->strreset_result[i];
645d0f025e6SXin Long 		if (result == SCTP_STRRESET_PERFORMED)
646d0f025e6SXin Long 			return NULL;
647d0f025e6SXin Long 		goto err;
64816e1a919SXin Long 	}
649d0f025e6SXin Long 	asoc->strreset_inseq++;
65016e1a919SXin Long 
65116e1a919SXin Long 	if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
65216e1a919SXin Long 		goto out;
65316e1a919SXin Long 
65416e1a919SXin Long 	if (asoc->strreset_outstanding) {
65516e1a919SXin Long 		result = SCTP_STRRESET_ERR_IN_PROGRESS;
65616e1a919SXin Long 		goto out;
65716e1a919SXin Long 	}
65816e1a919SXin Long 
6593aa623daSXin Long 	nums = (ntohs(param.p->length) - sizeof(*inreq)) / sizeof(__u16);
66016e1a919SXin Long 	str_p = inreq->list_of_streams;
66116e1a919SXin Long 	for (i = 0; i < nums; i++) {
66216e1a919SXin Long 		if (ntohs(str_p[i]) >= stream->outcnt) {
66316e1a919SXin Long 			result = SCTP_STRRESET_ERR_WRONG_SSN;
66416e1a919SXin Long 			goto out;
66516e1a919SXin Long 		}
66616e1a919SXin Long 	}
66716e1a919SXin Long 
668d570a59cSXin Long 	if (!sctp_stream_outq_is_empty(stream, nums, str_p)) {
669d570a59cSXin Long 		result = SCTP_STRRESET_IN_PROGRESS;
670d570a59cSXin Long 		asoc->strreset_inseq--;
671d570a59cSXin Long 		goto err;
672d570a59cSXin Long 	}
673d570a59cSXin Long 
67416e1a919SXin Long 	chunk = sctp_make_strreset_req(asoc, nums, str_p, 1, 0);
67516e1a919SXin Long 	if (!chunk)
67616e1a919SXin Long 		goto out;
67716e1a919SXin Long 
67816e1a919SXin Long 	if (nums)
67916e1a919SXin Long 		for (i = 0; i < nums; i++)
68016e1a919SXin Long 			stream->out[ntohs(str_p[i])].state =
68116e1a919SXin Long 					       SCTP_STREAM_CLOSED;
68216e1a919SXin Long 	else
68316e1a919SXin Long 		for (i = 0; i < stream->outcnt; i++)
68416e1a919SXin Long 			stream->out[i].state = SCTP_STREAM_CLOSED;
68516e1a919SXin Long 
68616e1a919SXin Long 	asoc->strreset_chunk = chunk;
68716e1a919SXin Long 	asoc->strreset_outstanding = 1;
68816e1a919SXin Long 	sctp_chunk_hold(asoc->strreset_chunk);
68916e1a919SXin Long 
690d0f025e6SXin Long 	result = SCTP_STRRESET_PERFORMED;
691d0f025e6SXin Long 
69216e1a919SXin Long 	*evp = sctp_ulpevent_make_stream_reset_event(asoc,
69316e1a919SXin Long 		SCTP_STREAM_RESET_INCOMING_SSN, nums, str_p, GFP_ATOMIC);
69416e1a919SXin Long 
69516e1a919SXin Long out:
696d0f025e6SXin Long 	sctp_update_strreset_result(asoc, result);
697d0f025e6SXin Long err:
69816e1a919SXin Long 	if (!chunk)
69916e1a919SXin Long 		chunk =  sctp_make_strreset_resp(asoc, result, request_seq);
70016e1a919SXin Long 
70116e1a919SXin Long 	return chunk;
70216e1a919SXin Long }
703692787ceSXin Long 
704692787ceSXin Long struct sctp_chunk *sctp_process_strreset_tsnreq(
705692787ceSXin Long 				struct sctp_association *asoc,
706692787ceSXin Long 				union sctp_params param,
707692787ceSXin Long 				struct sctp_ulpevent **evp)
708692787ceSXin Long {
709692787ceSXin Long 	__u32 init_tsn = 0, next_tsn = 0, max_tsn_seen;
710692787ceSXin Long 	struct sctp_strreset_tsnreq *tsnreq = param.v;
711cee360abSXin Long 	struct sctp_stream *stream = &asoc->stream;
712692787ceSXin Long 	__u32 result = SCTP_STRRESET_DENIED;
713692787ceSXin Long 	__u32 request_seq;
714692787ceSXin Long 	__u16 i;
715692787ceSXin Long 
716692787ceSXin Long 	request_seq = ntohl(tsnreq->request_seq);
7176c801387SXin Long 	if (TSN_lt(asoc->strreset_inseq, request_seq) ||
7186c801387SXin Long 	    TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
719692787ceSXin Long 		result = SCTP_STRRESET_ERR_BAD_SEQNO;
7206c801387SXin Long 		goto err;
7216c801387SXin Long 	} else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
7226c801387SXin Long 		i = asoc->strreset_inseq - request_seq - 1;
7236c801387SXin Long 		result = asoc->strreset_result[i];
7246c801387SXin Long 		if (result == SCTP_STRRESET_PERFORMED) {
7256c801387SXin Long 			next_tsn = asoc->next_tsn;
7266c801387SXin Long 			init_tsn =
7276c801387SXin Long 				sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + 1;
728692787ceSXin Long 		}
7296c801387SXin Long 		goto err;
7306c801387SXin Long 	}
7316c801387SXin Long 	asoc->strreset_inseq++;
732692787ceSXin Long 
733692787ceSXin Long 	if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
734692787ceSXin Long 		goto out;
735692787ceSXin Long 
736692787ceSXin Long 	if (asoc->strreset_outstanding) {
737692787ceSXin Long 		result = SCTP_STRRESET_ERR_IN_PROGRESS;
738692787ceSXin Long 		goto out;
739692787ceSXin Long 	}
740692787ceSXin Long 
741692787ceSXin Long 	/* G3: The same processing as though a SACK chunk with no gap report
742692787ceSXin Long 	 *     and a cumulative TSN ACK of the Sender's Next TSN minus 1 were
743692787ceSXin Long 	 *     received MUST be performed.
744692787ceSXin Long 	 */
745692787ceSXin Long 	max_tsn_seen = sctp_tsnmap_get_max_tsn_seen(&asoc->peer.tsn_map);
746692787ceSXin Long 	sctp_ulpq_reasm_flushtsn(&asoc->ulpq, max_tsn_seen);
747692787ceSXin Long 	sctp_ulpq_abort_pd(&asoc->ulpq, GFP_ATOMIC);
748692787ceSXin Long 
749692787ceSXin Long 	/* G1: Compute an appropriate value for the Receiver's Next TSN -- the
750692787ceSXin Long 	 *     TSN that the peer should use to send the next DATA chunk.  The
751692787ceSXin Long 	 *     value SHOULD be the smallest TSN not acknowledged by the
752692787ceSXin Long 	 *     receiver of the request plus 2^31.
753692787ceSXin Long 	 */
754692787ceSXin Long 	init_tsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + (1 << 31);
755692787ceSXin Long 	sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
756692787ceSXin Long 			 init_tsn, GFP_ATOMIC);
757692787ceSXin Long 
758692787ceSXin Long 	/* G4: The same processing as though a FWD-TSN chunk (as defined in
759692787ceSXin Long 	 *     [RFC3758]) with all streams affected and a new cumulative TSN
760692787ceSXin Long 	 *     ACK of the Receiver's Next TSN minus 1 were received MUST be
761692787ceSXin Long 	 *     performed.
762692787ceSXin Long 	 */
763692787ceSXin Long 	sctp_outq_free(&asoc->outqueue);
764692787ceSXin Long 
765692787ceSXin Long 	/* G2: Compute an appropriate value for the local endpoint's next TSN,
766692787ceSXin Long 	 *     i.e., the next TSN assigned by the receiver of the SSN/TSN reset
767692787ceSXin Long 	 *     chunk.  The value SHOULD be the highest TSN sent by the receiver
768692787ceSXin Long 	 *     of the request plus 1.
769692787ceSXin Long 	 */
770692787ceSXin Long 	next_tsn = asoc->next_tsn;
771692787ceSXin Long 	asoc->ctsn_ack_point = next_tsn - 1;
772692787ceSXin Long 	asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
773692787ceSXin Long 
774692787ceSXin Long 	/* G5:  The next expected and outgoing SSNs MUST be reset to 0 for all
775692787ceSXin Long 	 *      incoming and outgoing streams.
776692787ceSXin Long 	 */
777692787ceSXin Long 	for (i = 0; i < stream->outcnt; i++)
778692787ceSXin Long 		stream->out[i].ssn = 0;
779692787ceSXin Long 	for (i = 0; i < stream->incnt; i++)
780692787ceSXin Long 		stream->in[i].ssn = 0;
781692787ceSXin Long 
782692787ceSXin Long 	result = SCTP_STRRESET_PERFORMED;
783692787ceSXin Long 
784692787ceSXin Long 	*evp = sctp_ulpevent_make_assoc_reset_event(asoc, 0, init_tsn,
785692787ceSXin Long 						    next_tsn, GFP_ATOMIC);
786692787ceSXin Long 
787692787ceSXin Long out:
7886c801387SXin Long 	sctp_update_strreset_result(asoc, result);
7896c801387SXin Long err:
790692787ceSXin Long 	return sctp_make_strreset_tsnresp(asoc, result, request_seq,
791692787ceSXin Long 					  next_tsn, init_tsn);
792692787ceSXin Long }
79350a41591SXin Long 
79450a41591SXin Long struct sctp_chunk *sctp_process_strreset_addstrm_out(
79550a41591SXin Long 				struct sctp_association *asoc,
79650a41591SXin Long 				union sctp_params param,
79750a41591SXin Long 				struct sctp_ulpevent **evp)
79850a41591SXin Long {
79950a41591SXin Long 	struct sctp_strreset_addstrm *addstrm = param.v;
800cee360abSXin Long 	struct sctp_stream *stream = &asoc->stream;
80150a41591SXin Long 	__u32 result = SCTP_STRRESET_DENIED;
80250a41591SXin Long 	__u32 request_seq, incnt;
803e4dc99c7SXin Long 	__u16 in, i;
80450a41591SXin Long 
80550a41591SXin Long 	request_seq = ntohl(addstrm->request_seq);
806e4dc99c7SXin Long 	if (TSN_lt(asoc->strreset_inseq, request_seq) ||
807e4dc99c7SXin Long 	    TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
80850a41591SXin Long 		result = SCTP_STRRESET_ERR_BAD_SEQNO;
809e4dc99c7SXin Long 		goto err;
810e4dc99c7SXin Long 	} else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
811e4dc99c7SXin Long 		i = asoc->strreset_inseq - request_seq - 1;
812e4dc99c7SXin Long 		result = asoc->strreset_result[i];
813e4dc99c7SXin Long 		goto err;
81450a41591SXin Long 	}
815e4dc99c7SXin Long 	asoc->strreset_inseq++;
81650a41591SXin Long 
81750a41591SXin Long 	if (!(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ))
81850a41591SXin Long 		goto out;
81950a41591SXin Long 
82050a41591SXin Long 	if (asoc->strreset_chunk) {
82150a41591SXin Long 		if (!sctp_chunk_lookup_strreset_param(
82250a41591SXin Long 			asoc, 0, SCTP_PARAM_RESET_ADD_IN_STREAMS)) {
82350a41591SXin Long 			/* same process with outstanding isn't 0 */
82450a41591SXin Long 			result = SCTP_STRRESET_ERR_IN_PROGRESS;
82550a41591SXin Long 			goto out;
82650a41591SXin Long 		}
82750a41591SXin Long 
82850a41591SXin Long 		asoc->strreset_outstanding--;
82950a41591SXin Long 		asoc->strreset_outseq++;
83050a41591SXin Long 
83150a41591SXin Long 		if (!asoc->strreset_outstanding) {
83250a41591SXin Long 			struct sctp_transport *t;
83350a41591SXin Long 
83450a41591SXin Long 			t = asoc->strreset_chunk->transport;
83550a41591SXin Long 			if (del_timer(&t->reconf_timer))
83650a41591SXin Long 				sctp_transport_put(t);
83750a41591SXin Long 
83850a41591SXin Long 			sctp_chunk_put(asoc->strreset_chunk);
83950a41591SXin Long 			asoc->strreset_chunk = NULL;
84050a41591SXin Long 		}
84150a41591SXin Long 	}
84250a41591SXin Long 
84350a41591SXin Long 	in = ntohs(addstrm->number_of_streams);
84450a41591SXin Long 	incnt = stream->incnt + in;
84550a41591SXin Long 	if (!in || incnt > SCTP_MAX_STREAM)
84650a41591SXin Long 		goto out;
84750a41591SXin Long 
8481fdb8d8fSMarcelo Ricardo Leitner 	if (sctp_stream_alloc_in(stream, incnt, GFP_ATOMIC))
84950a41591SXin Long 		goto out;
85050a41591SXin Long 
85150a41591SXin Long 	stream->incnt = incnt;
85250a41591SXin Long 
85350a41591SXin Long 	result = SCTP_STRRESET_PERFORMED;
85450a41591SXin Long 
85550a41591SXin Long 	*evp = sctp_ulpevent_make_stream_change_event(asoc,
85650a41591SXin Long 		0, ntohs(addstrm->number_of_streams), 0, GFP_ATOMIC);
85750a41591SXin Long 
85850a41591SXin Long out:
859e4dc99c7SXin Long 	sctp_update_strreset_result(asoc, result);
860e4dc99c7SXin Long err:
86150a41591SXin Long 	return sctp_make_strreset_resp(asoc, result, request_seq);
86250a41591SXin Long }
863c5c4ebb3SXin Long 
864c5c4ebb3SXin Long struct sctp_chunk *sctp_process_strreset_addstrm_in(
865c5c4ebb3SXin Long 				struct sctp_association *asoc,
866c5c4ebb3SXin Long 				union sctp_params param,
867c5c4ebb3SXin Long 				struct sctp_ulpevent **evp)
868c5c4ebb3SXin Long {
869c5c4ebb3SXin Long 	struct sctp_strreset_addstrm *addstrm = param.v;
870cee360abSXin Long 	struct sctp_stream *stream = &asoc->stream;
871c5c4ebb3SXin Long 	__u32 result = SCTP_STRRESET_DENIED;
872c5c4ebb3SXin Long 	struct sctp_chunk *chunk = NULL;
873c5c4ebb3SXin Long 	__u32 request_seq, outcnt;
874d0f025e6SXin Long 	__u16 out, i;
875e090abd0SMarcelo Ricardo Leitner 	int ret;
876c5c4ebb3SXin Long 
877c5c4ebb3SXin Long 	request_seq = ntohl(addstrm->request_seq);
878d0f025e6SXin Long 	if (TSN_lt(asoc->strreset_inseq, request_seq) ||
879d0f025e6SXin Long 	    TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
880c5c4ebb3SXin Long 		result = SCTP_STRRESET_ERR_BAD_SEQNO;
881d0f025e6SXin Long 		goto err;
882d0f025e6SXin Long 	} else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
883d0f025e6SXin Long 		i = asoc->strreset_inseq - request_seq - 1;
884d0f025e6SXin Long 		result = asoc->strreset_result[i];
885d0f025e6SXin Long 		if (result == SCTP_STRRESET_PERFORMED)
886d0f025e6SXin Long 			return NULL;
887d0f025e6SXin Long 		goto err;
888c5c4ebb3SXin Long 	}
889d0f025e6SXin Long 	asoc->strreset_inseq++;
890c5c4ebb3SXin Long 
891c5c4ebb3SXin Long 	if (!(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ))
892c5c4ebb3SXin Long 		goto out;
893c5c4ebb3SXin Long 
894c5c4ebb3SXin Long 	if (asoc->strreset_outstanding) {
895c5c4ebb3SXin Long 		result = SCTP_STRRESET_ERR_IN_PROGRESS;
896c5c4ebb3SXin Long 		goto out;
897c5c4ebb3SXin Long 	}
898c5c4ebb3SXin Long 
899c5c4ebb3SXin Long 	out = ntohs(addstrm->number_of_streams);
900c5c4ebb3SXin Long 	outcnt = stream->outcnt + out;
901c5c4ebb3SXin Long 	if (!out || outcnt > SCTP_MAX_STREAM)
902c5c4ebb3SXin Long 		goto out;
903c5c4ebb3SXin Long 
904e090abd0SMarcelo Ricardo Leitner 	ret = sctp_stream_alloc_out(stream, outcnt, GFP_ATOMIC);
905e090abd0SMarcelo Ricardo Leitner 	if (ret)
906c5c4ebb3SXin Long 		goto out;
907c5c4ebb3SXin Long 
908c5c4ebb3SXin Long 	chunk = sctp_make_strreset_addstrm(asoc, out, 0);
909c5c4ebb3SXin Long 	if (!chunk)
910c5c4ebb3SXin Long 		goto out;
911c5c4ebb3SXin Long 
912c5c4ebb3SXin Long 	asoc->strreset_chunk = chunk;
913c5c4ebb3SXin Long 	asoc->strreset_outstanding = 1;
914c5c4ebb3SXin Long 	sctp_chunk_hold(asoc->strreset_chunk);
915c5c4ebb3SXin Long 
916c5c4ebb3SXin Long 	stream->outcnt = outcnt;
917c5c4ebb3SXin Long 
918d0f025e6SXin Long 	result = SCTP_STRRESET_PERFORMED;
919d0f025e6SXin Long 
920c5c4ebb3SXin Long 	*evp = sctp_ulpevent_make_stream_change_event(asoc,
921c5c4ebb3SXin Long 		0, 0, ntohs(addstrm->number_of_streams), GFP_ATOMIC);
922c5c4ebb3SXin Long 
923c5c4ebb3SXin Long out:
924d0f025e6SXin Long 	sctp_update_strreset_result(asoc, result);
925d0f025e6SXin Long err:
926c5c4ebb3SXin Long 	if (!chunk)
927c5c4ebb3SXin Long 		chunk = sctp_make_strreset_resp(asoc, result, request_seq);
928c5c4ebb3SXin Long 
929c5c4ebb3SXin Long 	return chunk;
930c5c4ebb3SXin Long }
93111ae76e6SXin Long 
93211ae76e6SXin Long struct sctp_chunk *sctp_process_strreset_resp(
93311ae76e6SXin Long 				struct sctp_association *asoc,
93411ae76e6SXin Long 				union sctp_params param,
93511ae76e6SXin Long 				struct sctp_ulpevent **evp)
93611ae76e6SXin Long {
937cee360abSXin Long 	struct sctp_stream *stream = &asoc->stream;
93811ae76e6SXin Long 	struct sctp_strreset_resp *resp = param.v;
93911ae76e6SXin Long 	struct sctp_transport *t;
94011ae76e6SXin Long 	__u16 i, nums, flags = 0;
9413c918704SXin Long 	struct sctp_paramhdr *req;
94211ae76e6SXin Long 	__u32 result;
94311ae76e6SXin Long 
94411ae76e6SXin Long 	req = sctp_chunk_lookup_strreset_param(asoc, resp->response_seq, 0);
94511ae76e6SXin Long 	if (!req)
94611ae76e6SXin Long 		return NULL;
94711ae76e6SXin Long 
94811ae76e6SXin Long 	result = ntohl(resp->result);
94911ae76e6SXin Long 	if (result != SCTP_STRRESET_PERFORMED) {
95011ae76e6SXin Long 		/* if in progress, do nothing but retransmit */
95111ae76e6SXin Long 		if (result == SCTP_STRRESET_IN_PROGRESS)
95211ae76e6SXin Long 			return NULL;
95311ae76e6SXin Long 		else if (result == SCTP_STRRESET_DENIED)
95411ae76e6SXin Long 			flags = SCTP_STREAM_RESET_DENIED;
95511ae76e6SXin Long 		else
95611ae76e6SXin Long 			flags = SCTP_STREAM_RESET_FAILED;
95711ae76e6SXin Long 	}
95811ae76e6SXin Long 
95911ae76e6SXin Long 	if (req->type == SCTP_PARAM_RESET_OUT_REQUEST) {
96011ae76e6SXin Long 		struct sctp_strreset_outreq *outreq;
9611da4fc97SXin Long 		__be16 *str_p;
96211ae76e6SXin Long 
96311ae76e6SXin Long 		outreq = (struct sctp_strreset_outreq *)req;
964edb12f2dSXin Long 		str_p = outreq->list_of_streams;
9653aa623daSXin Long 		nums = (ntohs(outreq->param_hdr.length) - sizeof(*outreq)) /
9663aa623daSXin Long 		       sizeof(__u16);
96711ae76e6SXin Long 
96811ae76e6SXin Long 		if (result == SCTP_STRRESET_PERFORMED) {
96911ae76e6SXin Long 			if (nums) {
97011ae76e6SXin Long 				for (i = 0; i < nums; i++)
97111ae76e6SXin Long 					stream->out[ntohs(str_p[i])].ssn = 0;
97211ae76e6SXin Long 			} else {
97311ae76e6SXin Long 				for (i = 0; i < stream->outcnt; i++)
97411ae76e6SXin Long 					stream->out[i].ssn = 0;
97511ae76e6SXin Long 			}
97611ae76e6SXin Long 
97711ae76e6SXin Long 			flags = SCTP_STREAM_RESET_OUTGOING_SSN;
97811ae76e6SXin Long 		}
97911ae76e6SXin Long 
98011ae76e6SXin Long 		for (i = 0; i < stream->outcnt; i++)
98111ae76e6SXin Long 			stream->out[i].state = SCTP_STREAM_OPEN;
98211ae76e6SXin Long 
98311ae76e6SXin Long 		*evp = sctp_ulpevent_make_stream_reset_event(asoc, flags,
98411ae76e6SXin Long 			nums, str_p, GFP_ATOMIC);
98511ae76e6SXin Long 	} else if (req->type == SCTP_PARAM_RESET_IN_REQUEST) {
98611ae76e6SXin Long 		struct sctp_strreset_inreq *inreq;
9871da4fc97SXin Long 		__be16 *str_p;
98811ae76e6SXin Long 
98911ae76e6SXin Long 		/* if the result is performed, it's impossible for inreq */
99011ae76e6SXin Long 		if (result == SCTP_STRRESET_PERFORMED)
99111ae76e6SXin Long 			return NULL;
99211ae76e6SXin Long 
99311ae76e6SXin Long 		inreq = (struct sctp_strreset_inreq *)req;
994edb12f2dSXin Long 		str_p = inreq->list_of_streams;
9953aa623daSXin Long 		nums = (ntohs(inreq->param_hdr.length) - sizeof(*inreq)) /
9963aa623daSXin Long 		       sizeof(__u16);
99711ae76e6SXin Long 
99811ae76e6SXin Long 		*evp = sctp_ulpevent_make_stream_reset_event(asoc, flags,
99911ae76e6SXin Long 			nums, str_p, GFP_ATOMIC);
100011ae76e6SXin Long 	} else if (req->type == SCTP_PARAM_RESET_TSN_REQUEST) {
100111ae76e6SXin Long 		struct sctp_strreset_resptsn *resptsn;
100211ae76e6SXin Long 		__u32 stsn, rtsn;
100311ae76e6SXin Long 
100411ae76e6SXin Long 		/* check for resptsn, as sctp_verify_reconf didn't do it*/
100511ae76e6SXin Long 		if (ntohs(param.p->length) != sizeof(*resptsn))
100611ae76e6SXin Long 			return NULL;
100711ae76e6SXin Long 
100811ae76e6SXin Long 		resptsn = (struct sctp_strreset_resptsn *)resp;
100911ae76e6SXin Long 		stsn = ntohl(resptsn->senders_next_tsn);
101011ae76e6SXin Long 		rtsn = ntohl(resptsn->receivers_next_tsn);
101111ae76e6SXin Long 
101211ae76e6SXin Long 		if (result == SCTP_STRRESET_PERFORMED) {
101311ae76e6SXin Long 			__u32 mtsn = sctp_tsnmap_get_max_tsn_seen(
101411ae76e6SXin Long 						&asoc->peer.tsn_map);
101511ae76e6SXin Long 
101611ae76e6SXin Long 			sctp_ulpq_reasm_flushtsn(&asoc->ulpq, mtsn);
101711ae76e6SXin Long 			sctp_ulpq_abort_pd(&asoc->ulpq, GFP_ATOMIC);
101811ae76e6SXin Long 
101911ae76e6SXin Long 			sctp_tsnmap_init(&asoc->peer.tsn_map,
102011ae76e6SXin Long 					 SCTP_TSN_MAP_INITIAL,
102111ae76e6SXin Long 					 stsn, GFP_ATOMIC);
102211ae76e6SXin Long 
102311ae76e6SXin Long 			sctp_outq_free(&asoc->outqueue);
102411ae76e6SXin Long 
102511ae76e6SXin Long 			asoc->next_tsn = rtsn;
102611ae76e6SXin Long 			asoc->ctsn_ack_point = asoc->next_tsn - 1;
102711ae76e6SXin Long 			asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
102811ae76e6SXin Long 
102911ae76e6SXin Long 			for (i = 0; i < stream->outcnt; i++)
103011ae76e6SXin Long 				stream->out[i].ssn = 0;
103111ae76e6SXin Long 			for (i = 0; i < stream->incnt; i++)
103211ae76e6SXin Long 				stream->in[i].ssn = 0;
103311ae76e6SXin Long 		}
103411ae76e6SXin Long 
103511ae76e6SXin Long 		for (i = 0; i < stream->outcnt; i++)
103611ae76e6SXin Long 			stream->out[i].state = SCTP_STREAM_OPEN;
103711ae76e6SXin Long 
103811ae76e6SXin Long 		*evp = sctp_ulpevent_make_assoc_reset_event(asoc, flags,
103911ae76e6SXin Long 			stsn, rtsn, GFP_ATOMIC);
104011ae76e6SXin Long 	} else if (req->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS) {
104111ae76e6SXin Long 		struct sctp_strreset_addstrm *addstrm;
104211ae76e6SXin Long 		__u16 number;
104311ae76e6SXin Long 
104411ae76e6SXin Long 		addstrm = (struct sctp_strreset_addstrm *)req;
104511ae76e6SXin Long 		nums = ntohs(addstrm->number_of_streams);
104611ae76e6SXin Long 		number = stream->outcnt - nums;
104711ae76e6SXin Long 
104811ae76e6SXin Long 		if (result == SCTP_STRRESET_PERFORMED)
104911ae76e6SXin Long 			for (i = number; i < stream->outcnt; i++)
105011ae76e6SXin Long 				stream->out[i].state = SCTP_STREAM_OPEN;
105111ae76e6SXin Long 		else
105211ae76e6SXin Long 			stream->outcnt = number;
105311ae76e6SXin Long 
105411ae76e6SXin Long 		*evp = sctp_ulpevent_make_stream_change_event(asoc, flags,
105511ae76e6SXin Long 			0, nums, GFP_ATOMIC);
105611ae76e6SXin Long 	} else if (req->type == SCTP_PARAM_RESET_ADD_IN_STREAMS) {
105711ae76e6SXin Long 		struct sctp_strreset_addstrm *addstrm;
105811ae76e6SXin Long 
105911ae76e6SXin Long 		/* if the result is performed, it's impossible for addstrm in
106011ae76e6SXin Long 		 * request.
106111ae76e6SXin Long 		 */
106211ae76e6SXin Long 		if (result == SCTP_STRRESET_PERFORMED)
106311ae76e6SXin Long 			return NULL;
106411ae76e6SXin Long 
106511ae76e6SXin Long 		addstrm = (struct sctp_strreset_addstrm *)req;
106611ae76e6SXin Long 		nums = ntohs(addstrm->number_of_streams);
106711ae76e6SXin Long 
106811ae76e6SXin Long 		*evp = sctp_ulpevent_make_stream_change_event(asoc, flags,
106911ae76e6SXin Long 			nums, 0, GFP_ATOMIC);
107011ae76e6SXin Long 	}
107111ae76e6SXin Long 
107211ae76e6SXin Long 	asoc->strreset_outstanding--;
107311ae76e6SXin Long 	asoc->strreset_outseq++;
107411ae76e6SXin Long 
107511ae76e6SXin Long 	/* remove everything for this reconf request */
107611ae76e6SXin Long 	if (!asoc->strreset_outstanding) {
107711ae76e6SXin Long 		t = asoc->strreset_chunk->transport;
107811ae76e6SXin Long 		if (del_timer(&t->reconf_timer))
107911ae76e6SXin Long 			sctp_transport_put(t);
108011ae76e6SXin Long 
108111ae76e6SXin Long 		sctp_chunk_put(asoc->strreset_chunk);
108211ae76e6SXin Long 		asoc->strreset_chunk = NULL;
108311ae76e6SXin Long 	}
108411ae76e6SXin Long 
108511ae76e6SXin Long 	return NULL;
108611ae76e6SXin Long }
1087