147505b8bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2a8386317SXin Long /* SCTP kernel implementation
3a8386317SXin Long * (C) Copyright IBM Corp. 2001, 2004
4a8386317SXin Long * Copyright (c) 1999-2000 Cisco, Inc.
5a8386317SXin Long * Copyright (c) 1999-2001 Motorola, Inc.
6a8386317SXin Long * Copyright (c) 2001 Intel Corp.
7a8386317SXin Long *
8a8386317SXin Long * This file is part of the SCTP kernel implementation
9a8386317SXin Long *
10fae8b6f4SXin Long * This file contains sctp stream maniuplation primitives and helpers.
11a8386317SXin Long *
12a8386317SXin Long * Please send any bug reports or fixes you make to the
13a8386317SXin Long * email address(es):
14a8386317SXin Long * lksctp developers <linux-sctp@vger.kernel.org>
15a8386317SXin Long *
16a8386317SXin Long * Written or modified by:
17a8386317SXin Long * Xin Long <lucien.xin@gmail.com>
18a8386317SXin Long */
19a8386317SXin Long
205bbbbe32SMarcelo Ricardo Leitner #include <linux/list.h>
21a8386317SXin Long #include <net/sctp/sctp.h>
227f9d68acSXin Long #include <net/sctp/sm.h>
235bbbbe32SMarcelo Ricardo Leitner #include <net/sctp/stream_sched.h>
245bbbbe32SMarcelo Ricardo Leitner
sctp_stream_shrink_out(struct sctp_stream * stream,__u16 outcnt)258f13399dSXin Long static void sctp_stream_shrink_out(struct sctp_stream *stream, __u16 outcnt)
265bbbbe32SMarcelo Ricardo Leitner {
275bbbbe32SMarcelo Ricardo Leitner struct sctp_association *asoc;
285bbbbe32SMarcelo Ricardo Leitner struct sctp_chunk *ch, *temp;
295bbbbe32SMarcelo Ricardo Leitner struct sctp_outq *outq;
305bbbbe32SMarcelo Ricardo Leitner
315bbbbe32SMarcelo Ricardo Leitner asoc = container_of(stream, struct sctp_association, stream);
325bbbbe32SMarcelo Ricardo Leitner outq = &asoc->outqueue;
335bbbbe32SMarcelo Ricardo Leitner
345bbbbe32SMarcelo Ricardo Leitner list_for_each_entry_safe(ch, temp, &outq->out_chunk_list, list) {
355bbbbe32SMarcelo Ricardo Leitner __u16 sid = sctp_chunk_stream_no(ch);
365bbbbe32SMarcelo Ricardo Leitner
375bbbbe32SMarcelo Ricardo Leitner if (sid < outcnt)
385bbbbe32SMarcelo Ricardo Leitner continue;
395bbbbe32SMarcelo Ricardo Leitner
405bbbbe32SMarcelo Ricardo Leitner sctp_sched_dequeue_common(outq, ch);
415bbbbe32SMarcelo Ricardo Leitner /* No need to call dequeue_done here because
425bbbbe32SMarcelo Ricardo Leitner * the chunks are not scheduled by now.
435bbbbe32SMarcelo Ricardo Leitner */
445bbbbe32SMarcelo Ricardo Leitner
455bbbbe32SMarcelo Ricardo Leitner /* Mark as failed send. */
4608f46070SXin Long sctp_chunk_fail(ch, (__force __u32)SCTP_ERROR_INV_STRM);
475bbbbe32SMarcelo Ricardo Leitner if (asoc->peer.prsctp_capable &&
485bbbbe32SMarcelo Ricardo Leitner SCTP_PR_PRIO_ENABLED(ch->sinfo.sinfo_flags))
495bbbbe32SMarcelo Ricardo Leitner asoc->sent_cnt_removable--;
505bbbbe32SMarcelo Ricardo Leitner
515bbbbe32SMarcelo Ricardo Leitner sctp_chunk_free(ch);
525bbbbe32SMarcelo Ricardo Leitner }
538f13399dSXin Long }
548f13399dSXin Long
sctp_stream_free_ext(struct sctp_stream * stream,__u16 sid)559ed7bfc7SZhengchao Shao static void sctp_stream_free_ext(struct sctp_stream *stream, __u16 sid)
569ed7bfc7SZhengchao Shao {
579ed7bfc7SZhengchao Shao struct sctp_sched_ops *sched;
589ed7bfc7SZhengchao Shao
599ed7bfc7SZhengchao Shao if (!SCTP_SO(stream, sid)->ext)
609ed7bfc7SZhengchao Shao return;
619ed7bfc7SZhengchao Shao
629ed7bfc7SZhengchao Shao sched = sctp_sched_ops_from_stream(stream);
639ed7bfc7SZhengchao Shao sched->free_sid(stream, sid);
649ed7bfc7SZhengchao Shao kfree(SCTP_SO(stream, sid)->ext);
659ed7bfc7SZhengchao Shao SCTP_SO(stream, sid)->ext = NULL;
669ed7bfc7SZhengchao Shao }
679ed7bfc7SZhengchao Shao
688f13399dSXin Long /* Migrates chunks from stream queues to new stream queues if needed,
698f13399dSXin Long * but not across associations. Also, removes those chunks to streams
708f13399dSXin Long * higher than the new max.
718f13399dSXin Long */
sctp_stream_outq_migrate(struct sctp_stream * stream,struct sctp_stream * new,__u16 outcnt)728f13399dSXin Long static void sctp_stream_outq_migrate(struct sctp_stream *stream,
738f13399dSXin Long struct sctp_stream *new, __u16 outcnt)
748f13399dSXin Long {
758f13399dSXin Long int i;
768f13399dSXin Long
778f13399dSXin Long if (stream->outcnt > outcnt)
788f13399dSXin Long sctp_stream_shrink_out(stream, outcnt);
795bbbbe32SMarcelo Ricardo Leitner
805bbbbe32SMarcelo Ricardo Leitner if (new) {
815bbbbe32SMarcelo Ricardo Leitner /* Here we actually move the old ext stuff into the new
825bbbbe32SMarcelo Ricardo Leitner * buffer, because we want to keep it. Then
835bbbbe32SMarcelo Ricardo Leitner * sctp_stream_update will swap ->out pointers.
845bbbbe32SMarcelo Ricardo Leitner */
855bbbbe32SMarcelo Ricardo Leitner for (i = 0; i < outcnt; i++) {
869ed7bfc7SZhengchao Shao sctp_stream_free_ext(new, i);
870d493b4dSKonstantin Khorenko SCTP_SO(new, i)->ext = SCTP_SO(stream, i)->ext;
880d493b4dSKonstantin Khorenko SCTP_SO(stream, i)->ext = NULL;
895bbbbe32SMarcelo Ricardo Leitner }
905bbbbe32SMarcelo Ricardo Leitner }
915bbbbe32SMarcelo Ricardo Leitner
929ed7bfc7SZhengchao Shao for (i = outcnt; i < stream->outcnt; i++)
939ed7bfc7SZhengchao Shao sctp_stream_free_ext(stream, i);
945bbbbe32SMarcelo Ricardo Leitner }
95a8386317SXin Long
sctp_stream_alloc_out(struct sctp_stream * stream,__u16 outcnt,gfp_t gfp)96e090abd0SMarcelo Ricardo Leitner static int sctp_stream_alloc_out(struct sctp_stream *stream, __u16 outcnt,
97e090abd0SMarcelo Ricardo Leitner gfp_t gfp)
98e090abd0SMarcelo Ricardo Leitner {
992075e50cSKent Overstreet int ret;
100e090abd0SMarcelo Ricardo Leitner
1012075e50cSKent Overstreet if (outcnt <= stream->outcnt)
102ab921f3cSDavid Laight goto out;
103e090abd0SMarcelo Ricardo Leitner
1042075e50cSKent Overstreet ret = genradix_prealloc(&stream->out, outcnt, gfp);
10561d5d406SMarcelo Ricardo Leitner if (ret)
1062075e50cSKent Overstreet return ret;
107cfe4bd7aSXin Long
108ab921f3cSDavid Laight out:
1092075e50cSKent Overstreet stream->outcnt = outcnt;
110e090abd0SMarcelo Ricardo Leitner return 0;
111e090abd0SMarcelo Ricardo Leitner }
112e090abd0SMarcelo Ricardo Leitner
sctp_stream_alloc_in(struct sctp_stream * stream,__u16 incnt,gfp_t gfp)1131fdb8d8fSMarcelo Ricardo Leitner static int sctp_stream_alloc_in(struct sctp_stream *stream, __u16 incnt,
1141fdb8d8fSMarcelo Ricardo Leitner gfp_t gfp)
1151fdb8d8fSMarcelo Ricardo Leitner {
1162075e50cSKent Overstreet int ret;
1171fdb8d8fSMarcelo Ricardo Leitner
1182075e50cSKent Overstreet if (incnt <= stream->incnt)
119ab921f3cSDavid Laight goto out;
1201fdb8d8fSMarcelo Ricardo Leitner
1212075e50cSKent Overstreet ret = genradix_prealloc(&stream->in, incnt, gfp);
12261d5d406SMarcelo Ricardo Leitner if (ret)
1232075e50cSKent Overstreet return ret;
1241fdb8d8fSMarcelo Ricardo Leitner
125ab921f3cSDavid Laight out:
1262075e50cSKent Overstreet stream->incnt = incnt;
1271fdb8d8fSMarcelo Ricardo Leitner return 0;
1281fdb8d8fSMarcelo Ricardo Leitner }
1291fdb8d8fSMarcelo Ricardo Leitner
sctp_stream_init(struct sctp_stream * stream,__u16 outcnt,__u16 incnt,gfp_t gfp)130ff356414SXin Long int sctp_stream_init(struct sctp_stream *stream, __u16 outcnt, __u16 incnt,
131ff356414SXin Long gfp_t gfp)
132a8386317SXin Long {
1335bbbbe32SMarcelo Ricardo Leitner struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
1345bbbbe32SMarcelo Ricardo Leitner int i, ret = 0;
1353dbcc105SXin Long
1361ae2eaaaSMarcelo Ricardo Leitner gfp |= __GFP_NOWARN;
1371ae2eaaaSMarcelo Ricardo Leitner
1383dbcc105SXin Long /* Initial stream->out size may be very big, so free it and alloc
1391ae2eaaaSMarcelo Ricardo Leitner * a new one with new outcnt to save memory if needed.
1403dbcc105SXin Long */
1411ae2eaaaSMarcelo Ricardo Leitner if (outcnt == stream->outcnt)
14261d5d406SMarcelo Ricardo Leitner goto handle_in;
1431ae2eaaaSMarcelo Ricardo Leitner
1445bbbbe32SMarcelo Ricardo Leitner /* Filter out chunks queued on streams that won't exist anymore */
1455bbbbe32SMarcelo Ricardo Leitner sched->unsched_all(stream);
1465bbbbe32SMarcelo Ricardo Leitner sctp_stream_outq_migrate(stream, NULL, outcnt);
1475bbbbe32SMarcelo Ricardo Leitner sched->sched_all(stream);
1485bbbbe32SMarcelo Ricardo Leitner
14979d08951SMarcelo Ricardo Leitner ret = sctp_stream_alloc_out(stream, outcnt, gfp);
15079d08951SMarcelo Ricardo Leitner if (ret)
151181d8d20SXin Long return ret;
1523dbcc105SXin Long
1533dbcc105SXin Long for (i = 0; i < stream->outcnt; i++)
15405364ca0SKonstantin Khorenko SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
1553dbcc105SXin Long
15661d5d406SMarcelo Ricardo Leitner handle_in:
1570c3f6f65SXin Long sctp_stream_interleave_init(stream);
158ff356414SXin Long if (!incnt)
159181d8d20SXin Long return 0;
160ff356414SXin Long
161181d8d20SXin Long return sctp_stream_alloc_in(stream, incnt, gfp);
162a8386317SXin Long }
163a8386317SXin Long
sctp_stream_init_ext(struct sctp_stream * stream,__u16 sid)164f952be79SMarcelo Ricardo Leitner int sctp_stream_init_ext(struct sctp_stream *stream, __u16 sid)
165f952be79SMarcelo Ricardo Leitner {
166f952be79SMarcelo Ricardo Leitner struct sctp_stream_out_ext *soute;
1674d141581SMarcelo Ricardo Leitner int ret;
168f952be79SMarcelo Ricardo Leitner
169f952be79SMarcelo Ricardo Leitner soute = kzalloc(sizeof(*soute), GFP_KERNEL);
170f952be79SMarcelo Ricardo Leitner if (!soute)
171f952be79SMarcelo Ricardo Leitner return -ENOMEM;
17205364ca0SKonstantin Khorenko SCTP_SO(stream, sid)->ext = soute;
173f952be79SMarcelo Ricardo Leitner
1744d141581SMarcelo Ricardo Leitner ret = sctp_sched_init_sid(stream, sid, GFP_KERNEL);
1754d141581SMarcelo Ricardo Leitner if (ret) {
1764d141581SMarcelo Ricardo Leitner kfree(SCTP_SO(stream, sid)->ext);
1774d141581SMarcelo Ricardo Leitner SCTP_SO(stream, sid)->ext = NULL;
1784d141581SMarcelo Ricardo Leitner }
1794d141581SMarcelo Ricardo Leitner
1804d141581SMarcelo Ricardo Leitner return ret;
181f952be79SMarcelo Ricardo Leitner }
182f952be79SMarcelo Ricardo Leitner
sctp_stream_free(struct sctp_stream * stream)183a8386317SXin Long void sctp_stream_free(struct sctp_stream *stream)
184a8386317SXin Long {
1855bbbbe32SMarcelo Ricardo Leitner struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
186f952be79SMarcelo Ricardo Leitner int i;
187f952be79SMarcelo Ricardo Leitner
1889ed7bfc7SZhengchao Shao sched->unsched_all(stream);
189f952be79SMarcelo Ricardo Leitner for (i = 0; i < stream->outcnt; i++)
1909ed7bfc7SZhengchao Shao sctp_stream_free_ext(stream, i);
1912075e50cSKent Overstreet genradix_free(&stream->out);
1922075e50cSKent Overstreet genradix_free(&stream->in);
193a8386317SXin Long }
194a8386317SXin Long
sctp_stream_clear(struct sctp_stream * stream)195a8386317SXin Long void sctp_stream_clear(struct sctp_stream *stream)
196a8386317SXin Long {
197a8386317SXin Long int i;
198a8386317SXin Long
199107e2425SXin Long for (i = 0; i < stream->outcnt; i++) {
20005364ca0SKonstantin Khorenko SCTP_SO(stream, i)->mid = 0;
20105364ca0SKonstantin Khorenko SCTP_SO(stream, i)->mid_uo = 0;
202107e2425SXin Long }
203a8386317SXin Long
204a8386317SXin Long for (i = 0; i < stream->incnt; i++)
20505364ca0SKonstantin Khorenko SCTP_SI(stream, i)->mid = 0;
206a8386317SXin Long }
2077f9d68acSXin Long
sctp_stream_update(struct sctp_stream * stream,struct sctp_stream * new)208cee360abSXin Long void sctp_stream_update(struct sctp_stream *stream, struct sctp_stream *new)
209cee360abSXin Long {
2105bbbbe32SMarcelo Ricardo Leitner struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
2115bbbbe32SMarcelo Ricardo Leitner
2125bbbbe32SMarcelo Ricardo Leitner sched->unsched_all(stream);
2135bbbbe32SMarcelo Ricardo Leitner sctp_stream_outq_migrate(stream, new, new->outcnt);
214cee360abSXin Long sctp_stream_free(stream);
215cee360abSXin Long
216cee360abSXin Long stream->out = new->out;
217cee360abSXin Long stream->in = new->in;
218cee360abSXin Long stream->outcnt = new->outcnt;
219cee360abSXin Long stream->incnt = new->incnt;
220cee360abSXin Long
2215bbbbe32SMarcelo Ricardo Leitner sched->sched_all(stream);
2225bbbbe32SMarcelo Ricardo Leitner
2232075e50cSKent Overstreet new->out.tree.root = NULL;
2242075e50cSKent Overstreet new->in.tree.root = NULL;
2256a9a27d5SXin Long new->outcnt = 0;
2266a9a27d5SXin Long new->incnt = 0;
227cee360abSXin Long }
228cee360abSXin Long
sctp_send_reconf(struct sctp_association * asoc,struct sctp_chunk * chunk)2297f9d68acSXin Long static int sctp_send_reconf(struct sctp_association *asoc,
2307f9d68acSXin Long struct sctp_chunk *chunk)
2317f9d68acSXin Long {
2327f9d68acSXin Long int retval = 0;
2337f9d68acSXin Long
2344e7696d9SXin Long retval = sctp_primitive_RECONF(asoc->base.net, asoc, chunk);
2357f9d68acSXin Long if (retval)
2367f9d68acSXin Long sctp_chunk_free(chunk);
2377f9d68acSXin Long
2387f9d68acSXin Long return retval;
2397f9d68acSXin Long }
2407f9d68acSXin Long
sctp_stream_outq_is_empty(struct sctp_stream * stream,__u16 str_nums,__be16 * str_list)241d570a59cSXin Long static bool sctp_stream_outq_is_empty(struct sctp_stream *stream,
242d570a59cSXin Long __u16 str_nums, __be16 *str_list)
243d570a59cSXin Long {
244d570a59cSXin Long struct sctp_association *asoc;
245d570a59cSXin Long __u16 i;
246d570a59cSXin Long
247d570a59cSXin Long asoc = container_of(stream, struct sctp_association, stream);
248d570a59cSXin Long if (!asoc->outqueue.out_qlen)
249d570a59cSXin Long return true;
250d570a59cSXin Long
251d570a59cSXin Long if (!str_nums)
252d570a59cSXin Long return false;
253d570a59cSXin Long
254d570a59cSXin Long for (i = 0; i < str_nums; i++) {
255d570a59cSXin Long __u16 sid = ntohs(str_list[i]);
256d570a59cSXin Long
25705364ca0SKonstantin Khorenko if (SCTP_SO(stream, sid)->ext &&
25805364ca0SKonstantin Khorenko !list_empty(&SCTP_SO(stream, sid)->ext->outq))
259d570a59cSXin Long return false;
260d570a59cSXin Long }
261d570a59cSXin Long
262d570a59cSXin Long return true;
263d570a59cSXin Long }
264d570a59cSXin Long
sctp_send_reset_streams(struct sctp_association * asoc,struct sctp_reset_streams * params)2657f9d68acSXin Long int sctp_send_reset_streams(struct sctp_association *asoc,
2667f9d68acSXin Long struct sctp_reset_streams *params)
2677f9d68acSXin Long {
268cee360abSXin Long struct sctp_stream *stream = &asoc->stream;
2697f9d68acSXin Long __u16 i, str_nums, *str_list;
2707f9d68acSXin Long struct sctp_chunk *chunk;
2717f9d68acSXin Long int retval = -EINVAL;
2721da4fc97SXin Long __be16 *nstr_list;
2737f9d68acSXin Long bool out, in;
2747f9d68acSXin Long
2757f9d68acSXin Long if (!asoc->peer.reconf_capable ||
2767f9d68acSXin Long !(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ)) {
2777f9d68acSXin Long retval = -ENOPROTOOPT;
2787f9d68acSXin Long goto out;
2797f9d68acSXin Long }
2807f9d68acSXin Long
2817f9d68acSXin Long if (asoc->strreset_outstanding) {
2827f9d68acSXin Long retval = -EINPROGRESS;
2837f9d68acSXin Long goto out;
2847f9d68acSXin Long }
2857f9d68acSXin Long
2867f9d68acSXin Long out = params->srs_flags & SCTP_STREAM_RESET_OUTGOING;
2877f9d68acSXin Long in = params->srs_flags & SCTP_STREAM_RESET_INCOMING;
2887f9d68acSXin Long if (!out && !in)
2897f9d68acSXin Long goto out;
2907f9d68acSXin Long
2917f9d68acSXin Long str_nums = params->srs_number_streams;
2927f9d68acSXin Long str_list = params->srs_stream_list;
293423852f8SXin Long if (str_nums) {
294423852f8SXin Long int param_len = 0;
295423852f8SXin Long
296423852f8SXin Long if (out) {
2977f9d68acSXin Long for (i = 0; i < str_nums; i++)
2987f9d68acSXin Long if (str_list[i] >= stream->outcnt)
2997f9d68acSXin Long goto out;
3007f9d68acSXin Long
301423852f8SXin Long param_len = str_nums * sizeof(__u16) +
302423852f8SXin Long sizeof(struct sctp_strreset_outreq);
303423852f8SXin Long }
304423852f8SXin Long
305423852f8SXin Long if (in) {
3067f9d68acSXin Long for (i = 0; i < str_nums; i++)
3077f9d68acSXin Long if (str_list[i] >= stream->incnt)
3087f9d68acSXin Long goto out;
3097f9d68acSXin Long
310423852f8SXin Long param_len += str_nums * sizeof(__u16) +
311423852f8SXin Long sizeof(struct sctp_strreset_inreq);
312423852f8SXin Long }
313423852f8SXin Long
314423852f8SXin Long if (param_len > SCTP_MAX_CHUNK_LEN -
315423852f8SXin Long sizeof(struct sctp_reconf_chunk))
316423852f8SXin Long goto out;
317423852f8SXin Long }
318423852f8SXin Long
3191da4fc97SXin Long nstr_list = kcalloc(str_nums, sizeof(__be16), GFP_KERNEL);
3201da4fc97SXin Long if (!nstr_list) {
3211da4fc97SXin Long retval = -ENOMEM;
3221da4fc97SXin Long goto out;
3231da4fc97SXin Long }
32416e1a919SXin Long
32516e1a919SXin Long for (i = 0; i < str_nums; i++)
3261da4fc97SXin Long nstr_list[i] = htons(str_list[i]);
3271da4fc97SXin Long
328d570a59cSXin Long if (out && !sctp_stream_outq_is_empty(stream, str_nums, nstr_list)) {
3296d5afe20Szhengbin kfree(nstr_list);
330d570a59cSXin Long retval = -EAGAIN;
331d570a59cSXin Long goto out;
332d570a59cSXin Long }
333d570a59cSXin Long
3341da4fc97SXin Long chunk = sctp_make_strreset_req(asoc, str_nums, nstr_list, out, in);
3351da4fc97SXin Long
3361da4fc97SXin Long kfree(nstr_list);
33716e1a919SXin Long
338119aecbaSXin Long if (!chunk) {
339119aecbaSXin Long retval = -ENOMEM;
3407f9d68acSXin Long goto out;
341119aecbaSXin Long }
3427f9d68acSXin Long
3437f9d68acSXin Long if (out) {
3447f9d68acSXin Long if (str_nums)
3457f9d68acSXin Long for (i = 0; i < str_nums; i++)
34605364ca0SKonstantin Khorenko SCTP_SO(stream, str_list[i])->state =
3477f9d68acSXin Long SCTP_STREAM_CLOSED;
3487f9d68acSXin Long else
3497f9d68acSXin Long for (i = 0; i < stream->outcnt; i++)
35005364ca0SKonstantin Khorenko SCTP_SO(stream, i)->state = SCTP_STREAM_CLOSED;
3517f9d68acSXin Long }
3527f9d68acSXin Long
3537f9d68acSXin Long asoc->strreset_chunk = chunk;
3547f9d68acSXin Long sctp_chunk_hold(asoc->strreset_chunk);
3557f9d68acSXin Long
3567f9d68acSXin Long retval = sctp_send_reconf(asoc, chunk);
3577f9d68acSXin Long if (retval) {
3587f9d68acSXin Long sctp_chunk_put(asoc->strreset_chunk);
3597f9d68acSXin Long asoc->strreset_chunk = NULL;
360119aecbaSXin Long if (!out)
361119aecbaSXin Long goto out;
362119aecbaSXin Long
363119aecbaSXin Long if (str_nums)
364119aecbaSXin Long for (i = 0; i < str_nums; i++)
36505364ca0SKonstantin Khorenko SCTP_SO(stream, str_list[i])->state =
366119aecbaSXin Long SCTP_STREAM_OPEN;
367119aecbaSXin Long else
368119aecbaSXin Long for (i = 0; i < stream->outcnt; i++)
36905364ca0SKonstantin Khorenko SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
370119aecbaSXin Long
371119aecbaSXin Long goto out;
3727f9d68acSXin Long }
3737f9d68acSXin Long
374119aecbaSXin Long asoc->strreset_outstanding = out + in;
375119aecbaSXin Long
3767f9d68acSXin Long out:
3777f9d68acSXin Long return retval;
3787f9d68acSXin Long }
379a92ce1a4SXin Long
sctp_send_reset_assoc(struct sctp_association * asoc)380a92ce1a4SXin Long int sctp_send_reset_assoc(struct sctp_association *asoc)
381a92ce1a4SXin Long {
382cee360abSXin Long struct sctp_stream *stream = &asoc->stream;
383a92ce1a4SXin Long struct sctp_chunk *chunk = NULL;
384a92ce1a4SXin Long int retval;
385a92ce1a4SXin Long __u16 i;
386a92ce1a4SXin Long
387a92ce1a4SXin Long if (!asoc->peer.reconf_capable ||
388a92ce1a4SXin Long !(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
389a92ce1a4SXin Long return -ENOPROTOOPT;
390a92ce1a4SXin Long
391a92ce1a4SXin Long if (asoc->strreset_outstanding)
392a92ce1a4SXin Long return -EINPROGRESS;
393a92ce1a4SXin Long
3945c6144a0SXin Long if (!sctp_outq_is_empty(&asoc->outqueue))
3955c6144a0SXin Long return -EAGAIN;
3965c6144a0SXin Long
397a92ce1a4SXin Long chunk = sctp_make_strreset_tsnreq(asoc);
398a92ce1a4SXin Long if (!chunk)
399a92ce1a4SXin Long return -ENOMEM;
400a92ce1a4SXin Long
401a92ce1a4SXin Long /* Block further xmit of data until this request is completed */
402cee360abSXin Long for (i = 0; i < stream->outcnt; i++)
40305364ca0SKonstantin Khorenko SCTP_SO(stream, i)->state = SCTP_STREAM_CLOSED;
404a92ce1a4SXin Long
405a92ce1a4SXin Long asoc->strreset_chunk = chunk;
406a92ce1a4SXin Long sctp_chunk_hold(asoc->strreset_chunk);
407a92ce1a4SXin Long
408a92ce1a4SXin Long retval = sctp_send_reconf(asoc, chunk);
409a92ce1a4SXin Long if (retval) {
410a92ce1a4SXin Long sctp_chunk_put(asoc->strreset_chunk);
411a92ce1a4SXin Long asoc->strreset_chunk = NULL;
412a92ce1a4SXin Long
413cee360abSXin Long for (i = 0; i < stream->outcnt; i++)
41405364ca0SKonstantin Khorenko SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
415a92ce1a4SXin Long
416a92ce1a4SXin Long return retval;
417a92ce1a4SXin Long }
418a92ce1a4SXin Long
419a92ce1a4SXin Long asoc->strreset_outstanding = 1;
420a92ce1a4SXin Long
421a92ce1a4SXin Long return 0;
422a92ce1a4SXin Long }
423242bd2d5SXin Long
sctp_send_add_streams(struct sctp_association * asoc,struct sctp_add_streams * params)424242bd2d5SXin Long int sctp_send_add_streams(struct sctp_association *asoc,
425242bd2d5SXin Long struct sctp_add_streams *params)
426242bd2d5SXin Long {
427cee360abSXin Long struct sctp_stream *stream = &asoc->stream;
428242bd2d5SXin Long struct sctp_chunk *chunk = NULL;
429dc82673fSWei Yongjun int retval;
430242bd2d5SXin Long __u32 outcnt, incnt;
431242bd2d5SXin Long __u16 out, in;
432242bd2d5SXin Long
433242bd2d5SXin Long if (!asoc->peer.reconf_capable ||
434242bd2d5SXin Long !(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
435242bd2d5SXin Long retval = -ENOPROTOOPT;
436242bd2d5SXin Long goto out;
437242bd2d5SXin Long }
438242bd2d5SXin Long
439242bd2d5SXin Long if (asoc->strreset_outstanding) {
440242bd2d5SXin Long retval = -EINPROGRESS;
441242bd2d5SXin Long goto out;
442242bd2d5SXin Long }
443242bd2d5SXin Long
444242bd2d5SXin Long out = params->sas_outstrms;
445242bd2d5SXin Long in = params->sas_instrms;
446242bd2d5SXin Long outcnt = stream->outcnt + out;
447242bd2d5SXin Long incnt = stream->incnt + in;
448242bd2d5SXin Long if (outcnt > SCTP_MAX_STREAM || incnt > SCTP_MAX_STREAM ||
449242bd2d5SXin Long (!out && !in)) {
450242bd2d5SXin Long retval = -EINVAL;
451242bd2d5SXin Long goto out;
452242bd2d5SXin Long }
453242bd2d5SXin Long
454242bd2d5SXin Long if (out) {
455e090abd0SMarcelo Ricardo Leitner retval = sctp_stream_alloc_out(stream, outcnt, GFP_KERNEL);
456e090abd0SMarcelo Ricardo Leitner if (retval)
457242bd2d5SXin Long goto out;
458242bd2d5SXin Long }
459242bd2d5SXin Long
460242bd2d5SXin Long chunk = sctp_make_strreset_addstrm(asoc, out, in);
461dc82673fSWei Yongjun if (!chunk) {
462dc82673fSWei Yongjun retval = -ENOMEM;
463242bd2d5SXin Long goto out;
464dc82673fSWei Yongjun }
465242bd2d5SXin Long
466242bd2d5SXin Long asoc->strreset_chunk = chunk;
467242bd2d5SXin Long sctp_chunk_hold(asoc->strreset_chunk);
468242bd2d5SXin Long
469242bd2d5SXin Long retval = sctp_send_reconf(asoc, chunk);
470242bd2d5SXin Long if (retval) {
471242bd2d5SXin Long sctp_chunk_put(asoc->strreset_chunk);
472242bd2d5SXin Long asoc->strreset_chunk = NULL;
473242bd2d5SXin Long goto out;
474242bd2d5SXin Long }
475242bd2d5SXin Long
476242bd2d5SXin Long asoc->strreset_outstanding = !!out + !!in;
477242bd2d5SXin Long
478242bd2d5SXin Long out:
479242bd2d5SXin Long return retval;
480242bd2d5SXin Long }
48181054476SXin Long
sctp_chunk_lookup_strreset_param(struct sctp_association * asoc,__be32 resp_seq,__be16 type)4823c918704SXin Long static struct sctp_paramhdr *sctp_chunk_lookup_strreset_param(
4831da4fc97SXin Long struct sctp_association *asoc, __be32 resp_seq,
48450a41591SXin Long __be16 type)
48581054476SXin Long {
48681054476SXin Long struct sctp_chunk *chunk = asoc->strreset_chunk;
48781054476SXin Long struct sctp_reconf_chunk *hdr;
48881054476SXin Long union sctp_params param;
48981054476SXin Long
49050a41591SXin Long if (!chunk)
49181054476SXin Long return NULL;
49281054476SXin Long
49381054476SXin Long hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
494add7370aSXin Long sctp_walk_params(param, hdr) {
49581054476SXin Long /* sctp_strreset_tsnreq is actually the basic structure
49681054476SXin Long * of all stream reconf params, so it's safe to use it
49781054476SXin Long * to access request_seq.
49881054476SXin Long */
49981054476SXin Long struct sctp_strreset_tsnreq *req = param.v;
50081054476SXin Long
50150a41591SXin Long if ((!resp_seq || req->request_seq == resp_seq) &&
50250a41591SXin Long (!type || type == req->param_hdr.type))
50381054476SXin Long return param.v;
50481054476SXin Long }
50581054476SXin Long
50681054476SXin Long return NULL;
50781054476SXin Long }
50881054476SXin Long
sctp_update_strreset_result(struct sctp_association * asoc,__u32 result)509e4dc99c7SXin Long static void sctp_update_strreset_result(struct sctp_association *asoc,
510e4dc99c7SXin Long __u32 result)
511e4dc99c7SXin Long {
512e4dc99c7SXin Long asoc->strreset_result[1] = asoc->strreset_result[0];
513e4dc99c7SXin Long asoc->strreset_result[0] = result;
514e4dc99c7SXin Long }
515e4dc99c7SXin Long
sctp_process_strreset_outreq(struct sctp_association * asoc,union sctp_params param,struct sctp_ulpevent ** evp)51681054476SXin Long struct sctp_chunk *sctp_process_strreset_outreq(
51781054476SXin Long struct sctp_association *asoc,
51881054476SXin Long union sctp_params param,
51981054476SXin Long struct sctp_ulpevent **evp)
52081054476SXin Long {
52181054476SXin Long struct sctp_strreset_outreq *outreq = param.v;
522cee360abSXin Long struct sctp_stream *stream = &asoc->stream;
52381054476SXin Long __u32 result = SCTP_STRRESET_DENIED;
5241da4fc97SXin Long __be16 *str_p = NULL;
52581054476SXin Long __u32 request_seq;
5262e6dc4d9SXin Long __u16 i, nums;
52781054476SXin Long
52881054476SXin Long request_seq = ntohl(outreq->request_seq);
52981054476SXin Long
53081054476SXin Long if (ntohl(outreq->send_reset_at_tsn) >
53181054476SXin Long sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map)) {
53281054476SXin Long result = SCTP_STRRESET_IN_PROGRESS;
533e4dc99c7SXin Long goto err;
53481054476SXin Long }
53581054476SXin Long
536e4dc99c7SXin Long if (TSN_lt(asoc->strreset_inseq, request_seq) ||
537e4dc99c7SXin Long TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
53881054476SXin Long result = SCTP_STRRESET_ERR_BAD_SEQNO;
539e4dc99c7SXin Long goto err;
540e4dc99c7SXin Long } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
541e4dc99c7SXin Long i = asoc->strreset_inseq - request_seq - 1;
542e4dc99c7SXin Long result = asoc->strreset_result[i];
543e4dc99c7SXin Long goto err;
54481054476SXin Long }
545e4dc99c7SXin Long asoc->strreset_inseq++;
54681054476SXin Long
54781054476SXin Long /* Check strreset_enable after inseq inc, as sender cannot tell
54881054476SXin Long * the peer doesn't enable strreset after receiving response with
54981054476SXin Long * result denied, as well as to keep consistent with bsd.
55081054476SXin Long */
55181054476SXin Long if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
55281054476SXin Long goto out;
55381054476SXin Long
5542e6dc4d9SXin Long nums = (ntohs(param.p->length) - sizeof(*outreq)) / sizeof(__u16);
5552e6dc4d9SXin Long str_p = outreq->list_of_streams;
5562e6dc4d9SXin Long for (i = 0; i < nums; i++) {
5572e6dc4d9SXin Long if (ntohs(str_p[i]) >= stream->incnt) {
5582e6dc4d9SXin Long result = SCTP_STRRESET_ERR_WRONG_SSN;
5592e6dc4d9SXin Long goto out;
5602e6dc4d9SXin Long }
5612e6dc4d9SXin Long }
5622e6dc4d9SXin Long
56381054476SXin Long if (asoc->strreset_chunk) {
56450a41591SXin Long if (!sctp_chunk_lookup_strreset_param(
56550a41591SXin Long asoc, outreq->response_seq,
56650a41591SXin Long SCTP_PARAM_RESET_IN_REQUEST)) {
56781054476SXin Long /* same process with outstanding isn't 0 */
56881054476SXin Long result = SCTP_STRRESET_ERR_IN_PROGRESS;
56981054476SXin Long goto out;
57081054476SXin Long }
57181054476SXin Long
57281054476SXin Long asoc->strreset_outstanding--;
57381054476SXin Long asoc->strreset_outseq++;
57481054476SXin Long
57581054476SXin Long if (!asoc->strreset_outstanding) {
57650a41591SXin Long struct sctp_transport *t;
57750a41591SXin Long
57881054476SXin Long t = asoc->strreset_chunk->transport;
57981054476SXin Long if (del_timer(&t->reconf_timer))
58081054476SXin Long sctp_transport_put(t);
58181054476SXin Long
58281054476SXin Long sctp_chunk_put(asoc->strreset_chunk);
58381054476SXin Long asoc->strreset_chunk = NULL;
58481054476SXin Long }
58581054476SXin Long }
58681054476SXin Long
5872e6dc4d9SXin Long if (nums)
58881054476SXin Long for (i = 0; i < nums; i++)
58905364ca0SKonstantin Khorenko SCTP_SI(stream, ntohs(str_p[i]))->mid = 0;
5902e6dc4d9SXin Long else
59181054476SXin Long for (i = 0; i < stream->incnt; i++)
59205364ca0SKonstantin Khorenko SCTP_SI(stream, i)->mid = 0;
59381054476SXin Long
59481054476SXin Long result = SCTP_STRRESET_PERFORMED;
59581054476SXin Long
59681054476SXin Long *evp = sctp_ulpevent_make_stream_reset_event(asoc,
5972e6dc4d9SXin Long SCTP_STREAM_RESET_INCOMING_SSN, nums, str_p, GFP_ATOMIC);
59881054476SXin Long
59981054476SXin Long out:
600e4dc99c7SXin Long sctp_update_strreset_result(asoc, result);
601e4dc99c7SXin Long err:
60281054476SXin Long return sctp_make_strreset_resp(asoc, result, request_seq);
60381054476SXin Long }
60416e1a919SXin Long
sctp_process_strreset_inreq(struct sctp_association * asoc,union sctp_params param,struct sctp_ulpevent ** evp)60516e1a919SXin Long struct sctp_chunk *sctp_process_strreset_inreq(
60616e1a919SXin Long struct sctp_association *asoc,
60716e1a919SXin Long union sctp_params param,
60816e1a919SXin Long struct sctp_ulpevent **evp)
60916e1a919SXin Long {
61016e1a919SXin Long struct sctp_strreset_inreq *inreq = param.v;
611cee360abSXin Long struct sctp_stream *stream = &asoc->stream;
61216e1a919SXin Long __u32 result = SCTP_STRRESET_DENIED;
61316e1a919SXin Long struct sctp_chunk *chunk = NULL;
61416e1a919SXin Long __u32 request_seq;
6151da4fc97SXin Long __u16 i, nums;
6161da4fc97SXin Long __be16 *str_p;
61716e1a919SXin Long
61816e1a919SXin Long request_seq = ntohl(inreq->request_seq);
619d0f025e6SXin Long if (TSN_lt(asoc->strreset_inseq, request_seq) ||
620d0f025e6SXin Long TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
62116e1a919SXin Long result = SCTP_STRRESET_ERR_BAD_SEQNO;
622d0f025e6SXin Long goto err;
623d0f025e6SXin Long } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
624d0f025e6SXin Long i = asoc->strreset_inseq - request_seq - 1;
625d0f025e6SXin Long result = asoc->strreset_result[i];
626d0f025e6SXin Long if (result == SCTP_STRRESET_PERFORMED)
627d0f025e6SXin Long return NULL;
628d0f025e6SXin Long goto err;
62916e1a919SXin Long }
630d0f025e6SXin Long asoc->strreset_inseq++;
63116e1a919SXin Long
63216e1a919SXin Long if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
63316e1a919SXin Long goto out;
63416e1a919SXin Long
63516e1a919SXin Long if (asoc->strreset_outstanding) {
63616e1a919SXin Long result = SCTP_STRRESET_ERR_IN_PROGRESS;
63716e1a919SXin Long goto out;
63816e1a919SXin Long }
63916e1a919SXin Long
6403aa623daSXin Long nums = (ntohs(param.p->length) - sizeof(*inreq)) / sizeof(__u16);
64116e1a919SXin Long str_p = inreq->list_of_streams;
64216e1a919SXin Long for (i = 0; i < nums; i++) {
64316e1a919SXin Long if (ntohs(str_p[i]) >= stream->outcnt) {
64416e1a919SXin Long result = SCTP_STRRESET_ERR_WRONG_SSN;
64516e1a919SXin Long goto out;
64616e1a919SXin Long }
64716e1a919SXin Long }
64816e1a919SXin Long
649d570a59cSXin Long if (!sctp_stream_outq_is_empty(stream, nums, str_p)) {
650d570a59cSXin Long result = SCTP_STRRESET_IN_PROGRESS;
651d570a59cSXin Long asoc->strreset_inseq--;
652d570a59cSXin Long goto err;
653d570a59cSXin Long }
654d570a59cSXin Long
65516e1a919SXin Long chunk = sctp_make_strreset_req(asoc, nums, str_p, 1, 0);
65616e1a919SXin Long if (!chunk)
65716e1a919SXin Long goto out;
65816e1a919SXin Long
65916e1a919SXin Long if (nums)
66016e1a919SXin Long for (i = 0; i < nums; i++)
66105364ca0SKonstantin Khorenko SCTP_SO(stream, ntohs(str_p[i]))->state =
66216e1a919SXin Long SCTP_STREAM_CLOSED;
66316e1a919SXin Long else
66416e1a919SXin Long for (i = 0; i < stream->outcnt; i++)
66505364ca0SKonstantin Khorenko SCTP_SO(stream, i)->state = SCTP_STREAM_CLOSED;
66616e1a919SXin Long
66716e1a919SXin Long asoc->strreset_chunk = chunk;
66816e1a919SXin Long asoc->strreset_outstanding = 1;
66916e1a919SXin Long sctp_chunk_hold(asoc->strreset_chunk);
67016e1a919SXin Long
671d0f025e6SXin Long result = SCTP_STRRESET_PERFORMED;
672d0f025e6SXin Long
67316e1a919SXin Long out:
674d0f025e6SXin Long sctp_update_strreset_result(asoc, result);
675d0f025e6SXin Long err:
67616e1a919SXin Long if (!chunk)
67716e1a919SXin Long chunk = sctp_make_strreset_resp(asoc, result, request_seq);
67816e1a919SXin Long
67916e1a919SXin Long return chunk;
68016e1a919SXin Long }
681692787ceSXin Long
sctp_process_strreset_tsnreq(struct sctp_association * asoc,union sctp_params param,struct sctp_ulpevent ** evp)682692787ceSXin Long struct sctp_chunk *sctp_process_strreset_tsnreq(
683692787ceSXin Long struct sctp_association *asoc,
684692787ceSXin Long union sctp_params param,
685692787ceSXin Long struct sctp_ulpevent **evp)
686692787ceSXin Long {
687692787ceSXin Long __u32 init_tsn = 0, next_tsn = 0, max_tsn_seen;
688692787ceSXin Long struct sctp_strreset_tsnreq *tsnreq = param.v;
689cee360abSXin Long struct sctp_stream *stream = &asoc->stream;
690692787ceSXin Long __u32 result = SCTP_STRRESET_DENIED;
691692787ceSXin Long __u32 request_seq;
692692787ceSXin Long __u16 i;
693692787ceSXin Long
694692787ceSXin Long request_seq = ntohl(tsnreq->request_seq);
6956c801387SXin Long if (TSN_lt(asoc->strreset_inseq, request_seq) ||
6966c801387SXin Long TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
697692787ceSXin Long result = SCTP_STRRESET_ERR_BAD_SEQNO;
6986c801387SXin Long goto err;
6996c801387SXin Long } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
7006c801387SXin Long i = asoc->strreset_inseq - request_seq - 1;
7016c801387SXin Long result = asoc->strreset_result[i];
7026c801387SXin Long if (result == SCTP_STRRESET_PERFORMED) {
70352a39589SXin Long next_tsn = asoc->ctsn_ack_point + 1;
7046c801387SXin Long init_tsn =
7056c801387SXin Long sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + 1;
706692787ceSXin Long }
7076c801387SXin Long goto err;
7086c801387SXin Long }
7095c6144a0SXin Long
7105c6144a0SXin Long if (!sctp_outq_is_empty(&asoc->outqueue)) {
7115c6144a0SXin Long result = SCTP_STRRESET_IN_PROGRESS;
7125c6144a0SXin Long goto err;
7135c6144a0SXin Long }
7145c6144a0SXin Long
7156c801387SXin Long asoc->strreset_inseq++;
716692787ceSXin Long
717692787ceSXin Long if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
718692787ceSXin Long goto out;
719692787ceSXin Long
720692787ceSXin Long if (asoc->strreset_outstanding) {
721692787ceSXin Long result = SCTP_STRRESET_ERR_IN_PROGRESS;
722692787ceSXin Long goto out;
723692787ceSXin Long }
724692787ceSXin Long
725159f2a74SXin Long /* G4: The same processing as though a FWD-TSN chunk (as defined in
726159f2a74SXin Long * [RFC3758]) with all streams affected and a new cumulative TSN
727159f2a74SXin Long * ACK of the Receiver's Next TSN minus 1 were received MUST be
728159f2a74SXin Long * performed.
729692787ceSXin Long */
730692787ceSXin Long max_tsn_seen = sctp_tsnmap_get_max_tsn_seen(&asoc->peer.tsn_map);
73147b20a88SXin Long asoc->stream.si->report_ftsn(&asoc->ulpq, max_tsn_seen);
732692787ceSXin Long
733692787ceSXin Long /* G1: Compute an appropriate value for the Receiver's Next TSN -- the
734692787ceSXin Long * TSN that the peer should use to send the next DATA chunk. The
735692787ceSXin Long * value SHOULD be the smallest TSN not acknowledged by the
736692787ceSXin Long * receiver of the request plus 2^31.
737692787ceSXin Long */
738*6df3939dSYu-Chun Lin init_tsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + (1U << 31);
739692787ceSXin Long sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
740692787ceSXin Long init_tsn, GFP_ATOMIC);
741692787ceSXin Long
742159f2a74SXin Long /* G3: The same processing as though a SACK chunk with no gap report
743159f2a74SXin Long * and a cumulative TSN ACK of the Sender's Next TSN minus 1 were
744159f2a74SXin Long * received MUST be performed.
745692787ceSXin Long */
746692787ceSXin Long sctp_outq_free(&asoc->outqueue);
747692787ceSXin Long
748692787ceSXin Long /* G2: Compute an appropriate value for the local endpoint's next TSN,
749692787ceSXin Long * i.e., the next TSN assigned by the receiver of the SSN/TSN reset
750692787ceSXin Long * chunk. The value SHOULD be the highest TSN sent by the receiver
751692787ceSXin Long * of the request plus 1.
752692787ceSXin Long */
753692787ceSXin Long next_tsn = asoc->next_tsn;
754692787ceSXin Long asoc->ctsn_ack_point = next_tsn - 1;
755692787ceSXin Long asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
756692787ceSXin Long
757692787ceSXin Long /* G5: The next expected and outgoing SSNs MUST be reset to 0 for all
758692787ceSXin Long * incoming and outgoing streams.
759692787ceSXin Long */
760107e2425SXin Long for (i = 0; i < stream->outcnt; i++) {
76105364ca0SKonstantin Khorenko SCTP_SO(stream, i)->mid = 0;
76205364ca0SKonstantin Khorenko SCTP_SO(stream, i)->mid_uo = 0;
763107e2425SXin Long }
764692787ceSXin Long for (i = 0; i < stream->incnt; i++)
76505364ca0SKonstantin Khorenko SCTP_SI(stream, i)->mid = 0;
766692787ceSXin Long
767692787ceSXin Long result = SCTP_STRRESET_PERFORMED;
768692787ceSXin Long
769692787ceSXin Long *evp = sctp_ulpevent_make_assoc_reset_event(asoc, 0, init_tsn,
770692787ceSXin Long next_tsn, GFP_ATOMIC);
771692787ceSXin Long
772692787ceSXin Long out:
7736c801387SXin Long sctp_update_strreset_result(asoc, result);
7746c801387SXin Long err:
775692787ceSXin Long return sctp_make_strreset_tsnresp(asoc, result, request_seq,
776692787ceSXin Long next_tsn, init_tsn);
777692787ceSXin Long }
77850a41591SXin Long
sctp_process_strreset_addstrm_out(struct sctp_association * asoc,union sctp_params param,struct sctp_ulpevent ** evp)77950a41591SXin Long struct sctp_chunk *sctp_process_strreset_addstrm_out(
78050a41591SXin Long struct sctp_association *asoc,
78150a41591SXin Long union sctp_params param,
78250a41591SXin Long struct sctp_ulpevent **evp)
78350a41591SXin Long {
78450a41591SXin Long struct sctp_strreset_addstrm *addstrm = param.v;
785cee360abSXin Long struct sctp_stream *stream = &asoc->stream;
78650a41591SXin Long __u32 result = SCTP_STRRESET_DENIED;
78750a41591SXin Long __u32 request_seq, incnt;
788e4dc99c7SXin Long __u16 in, i;
78950a41591SXin Long
79050a41591SXin Long request_seq = ntohl(addstrm->request_seq);
791e4dc99c7SXin Long if (TSN_lt(asoc->strreset_inseq, request_seq) ||
792e4dc99c7SXin Long TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
79350a41591SXin Long result = SCTP_STRRESET_ERR_BAD_SEQNO;
794e4dc99c7SXin Long goto err;
795e4dc99c7SXin Long } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
796e4dc99c7SXin Long i = asoc->strreset_inseq - request_seq - 1;
797e4dc99c7SXin Long result = asoc->strreset_result[i];
798e4dc99c7SXin Long goto err;
79950a41591SXin Long }
800e4dc99c7SXin Long asoc->strreset_inseq++;
80150a41591SXin Long
80250a41591SXin Long if (!(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ))
80350a41591SXin Long goto out;
80450a41591SXin Long
8058220c870SXin Long in = ntohs(addstrm->number_of_streams);
8068220c870SXin Long incnt = stream->incnt + in;
8078220c870SXin Long if (!in || incnt > SCTP_MAX_STREAM)
8088220c870SXin Long goto out;
8098220c870SXin Long
8108220c870SXin Long if (sctp_stream_alloc_in(stream, incnt, GFP_ATOMIC))
8118220c870SXin Long goto out;
8128220c870SXin Long
81350a41591SXin Long if (asoc->strreset_chunk) {
81450a41591SXin Long if (!sctp_chunk_lookup_strreset_param(
81550a41591SXin Long asoc, 0, SCTP_PARAM_RESET_ADD_IN_STREAMS)) {
81650a41591SXin Long /* same process with outstanding isn't 0 */
81750a41591SXin Long result = SCTP_STRRESET_ERR_IN_PROGRESS;
81850a41591SXin Long goto out;
81950a41591SXin Long }
82050a41591SXin Long
82150a41591SXin Long asoc->strreset_outstanding--;
82250a41591SXin Long asoc->strreset_outseq++;
82350a41591SXin Long
82450a41591SXin Long if (!asoc->strreset_outstanding) {
82550a41591SXin Long struct sctp_transport *t;
82650a41591SXin Long
82750a41591SXin Long t = asoc->strreset_chunk->transport;
82850a41591SXin Long if (del_timer(&t->reconf_timer))
82950a41591SXin Long sctp_transport_put(t);
83050a41591SXin Long
83150a41591SXin Long sctp_chunk_put(asoc->strreset_chunk);
83250a41591SXin Long asoc->strreset_chunk = NULL;
83350a41591SXin Long }
83450a41591SXin Long }
83550a41591SXin Long
83650a41591SXin Long stream->incnt = incnt;
83750a41591SXin Long
83850a41591SXin Long result = SCTP_STRRESET_PERFORMED;
83950a41591SXin Long
84050a41591SXin Long *evp = sctp_ulpevent_make_stream_change_event(asoc,
84150a41591SXin Long 0, ntohs(addstrm->number_of_streams), 0, GFP_ATOMIC);
84250a41591SXin Long
84350a41591SXin Long out:
844e4dc99c7SXin Long sctp_update_strreset_result(asoc, result);
845e4dc99c7SXin Long err:
84650a41591SXin Long return sctp_make_strreset_resp(asoc, result, request_seq);
84750a41591SXin Long }
848c5c4ebb3SXin Long
sctp_process_strreset_addstrm_in(struct sctp_association * asoc,union sctp_params param,struct sctp_ulpevent ** evp)849c5c4ebb3SXin Long struct sctp_chunk *sctp_process_strreset_addstrm_in(
850c5c4ebb3SXin Long struct sctp_association *asoc,
851c5c4ebb3SXin Long union sctp_params param,
852c5c4ebb3SXin Long struct sctp_ulpevent **evp)
853c5c4ebb3SXin Long {
854c5c4ebb3SXin Long struct sctp_strreset_addstrm *addstrm = param.v;
855cee360abSXin Long struct sctp_stream *stream = &asoc->stream;
856c5c4ebb3SXin Long __u32 result = SCTP_STRRESET_DENIED;
857c5c4ebb3SXin Long struct sctp_chunk *chunk = NULL;
858c5c4ebb3SXin Long __u32 request_seq, outcnt;
859d0f025e6SXin Long __u16 out, i;
860e090abd0SMarcelo Ricardo Leitner int ret;
861c5c4ebb3SXin Long
862c5c4ebb3SXin Long request_seq = ntohl(addstrm->request_seq);
863d0f025e6SXin Long if (TSN_lt(asoc->strreset_inseq, request_seq) ||
864d0f025e6SXin Long TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
865c5c4ebb3SXin Long result = SCTP_STRRESET_ERR_BAD_SEQNO;
866d0f025e6SXin Long goto err;
867d0f025e6SXin Long } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
868d0f025e6SXin Long i = asoc->strreset_inseq - request_seq - 1;
869d0f025e6SXin Long result = asoc->strreset_result[i];
870d0f025e6SXin Long if (result == SCTP_STRRESET_PERFORMED)
871d0f025e6SXin Long return NULL;
872d0f025e6SXin Long goto err;
873c5c4ebb3SXin Long }
874d0f025e6SXin Long asoc->strreset_inseq++;
875c5c4ebb3SXin Long
876c5c4ebb3SXin Long if (!(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ))
877c5c4ebb3SXin Long goto out;
878c5c4ebb3SXin Long
879c5c4ebb3SXin Long if (asoc->strreset_outstanding) {
880c5c4ebb3SXin Long result = SCTP_STRRESET_ERR_IN_PROGRESS;
881c5c4ebb3SXin Long goto out;
882c5c4ebb3SXin Long }
883c5c4ebb3SXin Long
884c5c4ebb3SXin Long out = ntohs(addstrm->number_of_streams);
885c5c4ebb3SXin Long outcnt = stream->outcnt + out;
886c5c4ebb3SXin Long if (!out || outcnt > SCTP_MAX_STREAM)
887c5c4ebb3SXin Long goto out;
888c5c4ebb3SXin Long
889e090abd0SMarcelo Ricardo Leitner ret = sctp_stream_alloc_out(stream, outcnt, GFP_ATOMIC);
890e090abd0SMarcelo Ricardo Leitner if (ret)
891c5c4ebb3SXin Long goto out;
892c5c4ebb3SXin Long
893c5c4ebb3SXin Long chunk = sctp_make_strreset_addstrm(asoc, out, 0);
894c5c4ebb3SXin Long if (!chunk)
895c5c4ebb3SXin Long goto out;
896c5c4ebb3SXin Long
897c5c4ebb3SXin Long asoc->strreset_chunk = chunk;
898c5c4ebb3SXin Long asoc->strreset_outstanding = 1;
899c5c4ebb3SXin Long sctp_chunk_hold(asoc->strreset_chunk);
900c5c4ebb3SXin Long
901c5c4ebb3SXin Long stream->outcnt = outcnt;
902c5c4ebb3SXin Long
903d0f025e6SXin Long result = SCTP_STRRESET_PERFORMED;
904d0f025e6SXin Long
905c5c4ebb3SXin Long out:
906d0f025e6SXin Long sctp_update_strreset_result(asoc, result);
907d0f025e6SXin Long err:
908c5c4ebb3SXin Long if (!chunk)
909c5c4ebb3SXin Long chunk = sctp_make_strreset_resp(asoc, result, request_seq);
910c5c4ebb3SXin Long
911c5c4ebb3SXin Long return chunk;
912c5c4ebb3SXin Long }
91311ae76e6SXin Long
sctp_process_strreset_resp(struct sctp_association * asoc,union sctp_params param,struct sctp_ulpevent ** evp)91411ae76e6SXin Long struct sctp_chunk *sctp_process_strreset_resp(
91511ae76e6SXin Long struct sctp_association *asoc,
91611ae76e6SXin Long union sctp_params param,
91711ae76e6SXin Long struct sctp_ulpevent **evp)
91811ae76e6SXin Long {
919cee360abSXin Long struct sctp_stream *stream = &asoc->stream;
92011ae76e6SXin Long struct sctp_strreset_resp *resp = param.v;
92111ae76e6SXin Long struct sctp_transport *t;
92211ae76e6SXin Long __u16 i, nums, flags = 0;
9233c918704SXin Long struct sctp_paramhdr *req;
92411ae76e6SXin Long __u32 result;
92511ae76e6SXin Long
92611ae76e6SXin Long req = sctp_chunk_lookup_strreset_param(asoc, resp->response_seq, 0);
92711ae76e6SXin Long if (!req)
92811ae76e6SXin Long return NULL;
92911ae76e6SXin Long
93011ae76e6SXin Long result = ntohl(resp->result);
93111ae76e6SXin Long if (result != SCTP_STRRESET_PERFORMED) {
93211ae76e6SXin Long /* if in progress, do nothing but retransmit */
93311ae76e6SXin Long if (result == SCTP_STRRESET_IN_PROGRESS)
93411ae76e6SXin Long return NULL;
93511ae76e6SXin Long else if (result == SCTP_STRRESET_DENIED)
93611ae76e6SXin Long flags = SCTP_STREAM_RESET_DENIED;
93711ae76e6SXin Long else
93811ae76e6SXin Long flags = SCTP_STREAM_RESET_FAILED;
93911ae76e6SXin Long }
94011ae76e6SXin Long
94111ae76e6SXin Long if (req->type == SCTP_PARAM_RESET_OUT_REQUEST) {
94211ae76e6SXin Long struct sctp_strreset_outreq *outreq;
9431da4fc97SXin Long __be16 *str_p;
94411ae76e6SXin Long
94511ae76e6SXin Long outreq = (struct sctp_strreset_outreq *)req;
946edb12f2dSXin Long str_p = outreq->list_of_streams;
9473aa623daSXin Long nums = (ntohs(outreq->param_hdr.length) - sizeof(*outreq)) /
9483aa623daSXin Long sizeof(__u16);
94911ae76e6SXin Long
95011ae76e6SXin Long if (result == SCTP_STRRESET_PERFORMED) {
95105364ca0SKonstantin Khorenko struct sctp_stream_out *sout;
95211ae76e6SXin Long if (nums) {
953107e2425SXin Long for (i = 0; i < nums; i++) {
95405364ca0SKonstantin Khorenko sout = SCTP_SO(stream, ntohs(str_p[i]));
95505364ca0SKonstantin Khorenko sout->mid = 0;
95605364ca0SKonstantin Khorenko sout->mid_uo = 0;
957107e2425SXin Long }
95811ae76e6SXin Long } else {
959107e2425SXin Long for (i = 0; i < stream->outcnt; i++) {
96005364ca0SKonstantin Khorenko sout = SCTP_SO(stream, i);
96105364ca0SKonstantin Khorenko sout->mid = 0;
96205364ca0SKonstantin Khorenko sout->mid_uo = 0;
963107e2425SXin Long }
96411ae76e6SXin Long }
96511ae76e6SXin Long }
96611ae76e6SXin Long
9672e6dc4d9SXin Long flags |= SCTP_STREAM_RESET_OUTGOING_SSN;
9682e6dc4d9SXin Long
96911ae76e6SXin Long for (i = 0; i < stream->outcnt; i++)
97005364ca0SKonstantin Khorenko SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
97111ae76e6SXin Long
97211ae76e6SXin Long *evp = sctp_ulpevent_make_stream_reset_event(asoc, flags,
97311ae76e6SXin Long nums, str_p, GFP_ATOMIC);
97411ae76e6SXin Long } else if (req->type == SCTP_PARAM_RESET_IN_REQUEST) {
97511ae76e6SXin Long struct sctp_strreset_inreq *inreq;
9761da4fc97SXin Long __be16 *str_p;
97711ae76e6SXin Long
97811ae76e6SXin Long /* if the result is performed, it's impossible for inreq */
97911ae76e6SXin Long if (result == SCTP_STRRESET_PERFORMED)
98011ae76e6SXin Long return NULL;
98111ae76e6SXin Long
98211ae76e6SXin Long inreq = (struct sctp_strreset_inreq *)req;
983edb12f2dSXin Long str_p = inreq->list_of_streams;
9843aa623daSXin Long nums = (ntohs(inreq->param_hdr.length) - sizeof(*inreq)) /
9853aa623daSXin Long sizeof(__u16);
98611ae76e6SXin Long
9872e6dc4d9SXin Long flags |= SCTP_STREAM_RESET_INCOMING_SSN;
9882e6dc4d9SXin Long
98911ae76e6SXin Long *evp = sctp_ulpevent_make_stream_reset_event(asoc, flags,
99011ae76e6SXin Long nums, str_p, GFP_ATOMIC);
99111ae76e6SXin Long } else if (req->type == SCTP_PARAM_RESET_TSN_REQUEST) {
99211ae76e6SXin Long struct sctp_strreset_resptsn *resptsn;
99311ae76e6SXin Long __u32 stsn, rtsn;
99411ae76e6SXin Long
99511ae76e6SXin Long /* check for resptsn, as sctp_verify_reconf didn't do it*/
99611ae76e6SXin Long if (ntohs(param.p->length) != sizeof(*resptsn))
99711ae76e6SXin Long return NULL;
99811ae76e6SXin Long
99911ae76e6SXin Long resptsn = (struct sctp_strreset_resptsn *)resp;
100011ae76e6SXin Long stsn = ntohl(resptsn->senders_next_tsn);
100111ae76e6SXin Long rtsn = ntohl(resptsn->receivers_next_tsn);
100211ae76e6SXin Long
100311ae76e6SXin Long if (result == SCTP_STRRESET_PERFORMED) {
100411ae76e6SXin Long __u32 mtsn = sctp_tsnmap_get_max_tsn_seen(
100511ae76e6SXin Long &asoc->peer.tsn_map);
1006159f2a74SXin Long LIST_HEAD(temp);
100711ae76e6SXin Long
100847b20a88SXin Long asoc->stream.si->report_ftsn(&asoc->ulpq, mtsn);
100911ae76e6SXin Long
101011ae76e6SXin Long sctp_tsnmap_init(&asoc->peer.tsn_map,
101111ae76e6SXin Long SCTP_TSN_MAP_INITIAL,
101211ae76e6SXin Long stsn, GFP_ATOMIC);
101311ae76e6SXin Long
1014159f2a74SXin Long /* Clean up sacked and abandoned queues only. As the
1015159f2a74SXin Long * out_chunk_list may not be empty, splice it to temp,
1016159f2a74SXin Long * then get it back after sctp_outq_free is done.
1017159f2a74SXin Long */
1018159f2a74SXin Long list_splice_init(&asoc->outqueue.out_chunk_list, &temp);
101911ae76e6SXin Long sctp_outq_free(&asoc->outqueue);
1020159f2a74SXin Long list_splice_init(&temp, &asoc->outqueue.out_chunk_list);
102111ae76e6SXin Long
102211ae76e6SXin Long asoc->next_tsn = rtsn;
102311ae76e6SXin Long asoc->ctsn_ack_point = asoc->next_tsn - 1;
102411ae76e6SXin Long asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
102511ae76e6SXin Long
1026107e2425SXin Long for (i = 0; i < stream->outcnt; i++) {
102705364ca0SKonstantin Khorenko SCTP_SO(stream, i)->mid = 0;
102805364ca0SKonstantin Khorenko SCTP_SO(stream, i)->mid_uo = 0;
1029107e2425SXin Long }
103011ae76e6SXin Long for (i = 0; i < stream->incnt; i++)
103105364ca0SKonstantin Khorenko SCTP_SI(stream, i)->mid = 0;
103211ae76e6SXin Long }
103311ae76e6SXin Long
103411ae76e6SXin Long for (i = 0; i < stream->outcnt; i++)
103505364ca0SKonstantin Khorenko SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
103611ae76e6SXin Long
103711ae76e6SXin Long *evp = sctp_ulpevent_make_assoc_reset_event(asoc, flags,
103811ae76e6SXin Long stsn, rtsn, GFP_ATOMIC);
103911ae76e6SXin Long } else if (req->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS) {
104011ae76e6SXin Long struct sctp_strreset_addstrm *addstrm;
104111ae76e6SXin Long __u16 number;
104211ae76e6SXin Long
104311ae76e6SXin Long addstrm = (struct sctp_strreset_addstrm *)req;
104411ae76e6SXin Long nums = ntohs(addstrm->number_of_streams);
104511ae76e6SXin Long number = stream->outcnt - nums;
104611ae76e6SXin Long
10473ecdda3eSXin Long if (result == SCTP_STRRESET_PERFORMED) {
104811ae76e6SXin Long for (i = number; i < stream->outcnt; i++)
104905364ca0SKonstantin Khorenko SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
10503ecdda3eSXin Long } else {
10513ecdda3eSXin Long sctp_stream_shrink_out(stream, number);
105211ae76e6SXin Long stream->outcnt = number;
10533ecdda3eSXin Long }
105411ae76e6SXin Long
105511ae76e6SXin Long *evp = sctp_ulpevent_make_stream_change_event(asoc, flags,
105611ae76e6SXin Long 0, nums, GFP_ATOMIC);
105711ae76e6SXin Long } else if (req->type == SCTP_PARAM_RESET_ADD_IN_STREAMS) {
105811ae76e6SXin Long struct sctp_strreset_addstrm *addstrm;
105911ae76e6SXin Long
106011ae76e6SXin Long /* if the result is performed, it's impossible for addstrm in
106111ae76e6SXin Long * request.
106211ae76e6SXin Long */
106311ae76e6SXin Long if (result == SCTP_STRRESET_PERFORMED)
106411ae76e6SXin Long return NULL;
106511ae76e6SXin Long
106611ae76e6SXin Long addstrm = (struct sctp_strreset_addstrm *)req;
106711ae76e6SXin Long nums = ntohs(addstrm->number_of_streams);
106811ae76e6SXin Long
106911ae76e6SXin Long *evp = sctp_ulpevent_make_stream_change_event(asoc, flags,
107011ae76e6SXin Long nums, 0, GFP_ATOMIC);
107111ae76e6SXin Long }
107211ae76e6SXin Long
107311ae76e6SXin Long asoc->strreset_outstanding--;
107411ae76e6SXin Long asoc->strreset_outseq++;
107511ae76e6SXin Long
107611ae76e6SXin Long /* remove everything for this reconf request */
107711ae76e6SXin Long if (!asoc->strreset_outstanding) {
107811ae76e6SXin Long t = asoc->strreset_chunk->transport;
107911ae76e6SXin Long if (del_timer(&t->reconf_timer))
108011ae76e6SXin Long sctp_transport_put(t);
108111ae76e6SXin Long
108211ae76e6SXin Long sctp_chunk_put(asoc->strreset_chunk);
108311ae76e6SXin Long asoc->strreset_chunk = NULL;
108411ae76e6SXin Long }
108511ae76e6SXin Long
108611ae76e6SXin Long return NULL;
108711ae76e6SXin Long }
1088