blob: 67f7e71f91298811bfecea8070bc73c427a80872 [file] [log] [blame]
Thomas Gleixner47505b82019-05-23 11:14:41 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Xin Longa8386312017-01-06 22:18:33 +08002/* SCTP kernel implementation
3 * (C) Copyright IBM Corp. 2001, 2004
4 * Copyright (c) 1999-2000 Cisco, Inc.
5 * Copyright (c) 1999-2001 Motorola, Inc.
6 * Copyright (c) 2001 Intel Corp.
7 *
8 * This file is part of the SCTP kernel implementation
9 *
Xin Longfae8b6f2018-02-13 19:29:13 +080010 * This file contains sctp stream maniuplation primitives and helpers.
Xin Longa8386312017-01-06 22:18:33 +080011 *
Xin Longa8386312017-01-06 22:18:33 +080012 * Please send any bug reports or fixes you make to the
13 * email address(es):
14 * lksctp developers <linux-sctp@vger.kernel.org>
15 *
16 * Written or modified by:
17 * Xin Long <lucien.xin@gmail.com>
18 */
19
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -030020#include <linux/list.h>
Xin Longa8386312017-01-06 22:18:33 +080021#include <net/sctp/sctp.h>
Xin Long7f9d68a2017-01-18 00:44:47 +080022#include <net/sctp/sm.h>
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -030023#include <net/sctp/stream_sched.h>
24
25/* Migrates chunks from stream queues to new stream queues if needed,
26 * but not across associations. Also, removes those chunks to streams
27 * higher than the new max.
28 */
29static void sctp_stream_outq_migrate(struct sctp_stream *stream,
30 struct sctp_stream *new, __u16 outcnt)
31{
32 struct sctp_association *asoc;
33 struct sctp_chunk *ch, *temp;
34 struct sctp_outq *outq;
35 int i;
36
37 asoc = container_of(stream, struct sctp_association, stream);
38 outq = &asoc->outqueue;
39
40 list_for_each_entry_safe(ch, temp, &outq->out_chunk_list, list) {
41 __u16 sid = sctp_chunk_stream_no(ch);
42
43 if (sid < outcnt)
44 continue;
45
46 sctp_sched_dequeue_common(outq, ch);
47 /* No need to call dequeue_done here because
48 * the chunks are not scheduled by now.
49 */
50
51 /* Mark as failed send. */
Xin Long08f46072017-11-26 20:16:06 +080052 sctp_chunk_fail(ch, (__force __u32)SCTP_ERROR_INV_STRM);
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -030053 if (asoc->peer.prsctp_capable &&
54 SCTP_PR_PRIO_ENABLED(ch->sinfo.sinfo_flags))
55 asoc->sent_cnt_removable--;
56
57 sctp_chunk_free(ch);
58 }
59
60 if (new) {
61 /* Here we actually move the old ext stuff into the new
62 * buffer, because we want to keep it. Then
63 * sctp_stream_update will swap ->out pointers.
64 */
65 for (i = 0; i < outcnt; i++) {
Konstantin Khorenko0d493b42018-08-10 20:11:43 +030066 kfree(SCTP_SO(new, i)->ext);
67 SCTP_SO(new, i)->ext = SCTP_SO(stream, i)->ext;
68 SCTP_SO(stream, i)->ext = NULL;
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -030069 }
70 }
71
Xin Longaf98c5a2019-02-12 18:51:01 +080072 for (i = outcnt; i < stream->outcnt; i++) {
Konstantin Khorenko0d493b42018-08-10 20:11:43 +030073 kfree(SCTP_SO(stream, i)->ext);
Xin Longaf98c5a2019-02-12 18:51:01 +080074 SCTP_SO(stream, i)->ext = NULL;
75 }
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -030076}
Xin Longa8386312017-01-06 22:18:33 +080077
Marcelo Ricardo Leitnere090abd2017-10-03 19:20:09 -030078static int sctp_stream_alloc_out(struct sctp_stream *stream, __u16 outcnt,
79 gfp_t gfp)
80{
Kent Overstreet2075e502019-03-11 23:31:22 -070081 int ret;
Marcelo Ricardo Leitnere090abd2017-10-03 19:20:09 -030082
Kent Overstreet2075e502019-03-11 23:31:22 -070083 if (outcnt <= stream->outcnt)
84 return 0;
Marcelo Ricardo Leitnere090abd2017-10-03 19:20:09 -030085
Kent Overstreet2075e502019-03-11 23:31:22 -070086 ret = genradix_prealloc(&stream->out, outcnt, gfp);
Marcelo Ricardo Leitner61d5d402019-12-20 15:03:44 -030087 if (ret)
Kent Overstreet2075e502019-03-11 23:31:22 -070088 return ret;
Xin Longcfe4bd72019-02-04 03:27:58 +080089
Kent Overstreet2075e502019-03-11 23:31:22 -070090 stream->outcnt = outcnt;
Marcelo Ricardo Leitnere090abd2017-10-03 19:20:09 -030091 return 0;
92}
93
Marcelo Ricardo Leitner1fdb8d82017-10-03 19:20:10 -030094static int sctp_stream_alloc_in(struct sctp_stream *stream, __u16 incnt,
95 gfp_t gfp)
96{
Kent Overstreet2075e502019-03-11 23:31:22 -070097 int ret;
Marcelo Ricardo Leitner1fdb8d82017-10-03 19:20:10 -030098
Kent Overstreet2075e502019-03-11 23:31:22 -070099 if (incnt <= stream->incnt)
100 return 0;
Marcelo Ricardo Leitner1fdb8d82017-10-03 19:20:10 -0300101
Kent Overstreet2075e502019-03-11 23:31:22 -0700102 ret = genradix_prealloc(&stream->in, incnt, gfp);
Marcelo Ricardo Leitner61d5d402019-12-20 15:03:44 -0300103 if (ret)
Kent Overstreet2075e502019-03-11 23:31:22 -0700104 return ret;
Marcelo Ricardo Leitner1fdb8d82017-10-03 19:20:10 -0300105
Kent Overstreet2075e502019-03-11 23:31:22 -0700106 stream->incnt = incnt;
Marcelo Ricardo Leitner1fdb8d82017-10-03 19:20:10 -0300107 return 0;
108}
109
Xin Longff356412017-05-31 16:36:32 +0800110int sctp_stream_init(struct sctp_stream *stream, __u16 outcnt, __u16 incnt,
111 gfp_t gfp)
Xin Longa8386312017-01-06 22:18:33 +0800112{
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300113 struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
114 int i, ret = 0;
Xin Long3dbcc102017-03-30 01:00:53 +0800115
Marcelo Ricardo Leitner1ae2eaa2017-10-03 19:20:08 -0300116 gfp |= __GFP_NOWARN;
117
Xin Long3dbcc102017-03-30 01:00:53 +0800118 /* Initial stream->out size may be very big, so free it and alloc
Marcelo Ricardo Leitner1ae2eaa2017-10-03 19:20:08 -0300119 * a new one with new outcnt to save memory if needed.
Xin Long3dbcc102017-03-30 01:00:53 +0800120 */
Marcelo Ricardo Leitner1ae2eaa2017-10-03 19:20:08 -0300121 if (outcnt == stream->outcnt)
Marcelo Ricardo Leitner61d5d402019-12-20 15:03:44 -0300122 goto handle_in;
Marcelo Ricardo Leitner1ae2eaa2017-10-03 19:20:08 -0300123
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300124 /* Filter out chunks queued on streams that won't exist anymore */
125 sched->unsched_all(stream);
126 sctp_stream_outq_migrate(stream, NULL, outcnt);
127 sched->sched_all(stream);
128
Marcelo Ricardo Leitner79d08952018-01-02 19:44:37 -0200129 ret = sctp_stream_alloc_out(stream, outcnt, gfp);
130 if (ret)
Marcelo Ricardo Leitner61d5d402019-12-20 15:03:44 -0300131 goto out_err;
Xin Long3dbcc102017-03-30 01:00:53 +0800132
133 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300134 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
Xin Long3dbcc102017-03-30 01:00:53 +0800135
Marcelo Ricardo Leitner61d5d402019-12-20 15:03:44 -0300136handle_in:
Xin Long0c3f6f62017-12-08 21:04:01 +0800137 sctp_stream_interleave_init(stream);
Xin Longff356412017-05-31 16:36:32 +0800138 if (!incnt)
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300139 goto out;
Xin Longff356412017-05-31 16:36:32 +0800140
Marcelo Ricardo Leitner79d08952018-01-02 19:44:37 -0200141 ret = sctp_stream_alloc_in(stream, incnt, gfp);
Marcelo Ricardo Leitner61d5d402019-12-20 15:03:44 -0300142 if (ret)
143 goto in_err;
Xin Longa8386312017-01-06 22:18:33 +0800144
Marcelo Ricardo Leitner61d5d402019-12-20 15:03:44 -0300145 goto out;
146
147in_err:
148 sched->free(stream);
149 genradix_free(&stream->in);
150out_err:
151 genradix_free(&stream->out);
152 stream->outcnt = 0;
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300153out:
154 return ret;
Xin Longa8386312017-01-06 22:18:33 +0800155}
156
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -0300157int sctp_stream_init_ext(struct sctp_stream *stream, __u16 sid)
158{
159 struct sctp_stream_out_ext *soute;
Marcelo Ricardo Leitner4d141582019-06-27 19:48:10 -0300160 int ret;
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -0300161
162 soute = kzalloc(sizeof(*soute), GFP_KERNEL);
163 if (!soute)
164 return -ENOMEM;
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300165 SCTP_SO(stream, sid)->ext = soute;
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -0300166
Marcelo Ricardo Leitner4d141582019-06-27 19:48:10 -0300167 ret = sctp_sched_init_sid(stream, sid, GFP_KERNEL);
168 if (ret) {
169 kfree(SCTP_SO(stream, sid)->ext);
170 SCTP_SO(stream, sid)->ext = NULL;
171 }
172
173 return ret;
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -0300174}
175
Xin Longa8386312017-01-06 22:18:33 +0800176void sctp_stream_free(struct sctp_stream *stream)
177{
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300178 struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -0300179 int i;
180
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300181 sched->free(stream);
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -0300182 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300183 kfree(SCTP_SO(stream, i)->ext);
Kent Overstreet2075e502019-03-11 23:31:22 -0700184 genradix_free(&stream->out);
185 genradix_free(&stream->in);
Xin Longa8386312017-01-06 22:18:33 +0800186}
187
188void sctp_stream_clear(struct sctp_stream *stream)
189{
190 int i;
191
Xin Long107e2422017-12-15 00:41:31 +0800192 for (i = 0; i < stream->outcnt; i++) {
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300193 SCTP_SO(stream, i)->mid = 0;
194 SCTP_SO(stream, i)->mid_uo = 0;
Xin Long107e2422017-12-15 00:41:31 +0800195 }
Xin Longa8386312017-01-06 22:18:33 +0800196
197 for (i = 0; i < stream->incnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300198 SCTP_SI(stream, i)->mid = 0;
Xin Longa8386312017-01-06 22:18:33 +0800199}
Xin Long7f9d68a2017-01-18 00:44:47 +0800200
Xin Longcee360a2017-05-31 16:36:31 +0800201void sctp_stream_update(struct sctp_stream *stream, struct sctp_stream *new)
202{
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300203 struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
204
205 sched->unsched_all(stream);
206 sctp_stream_outq_migrate(stream, new, new->outcnt);
Xin Longcee360a2017-05-31 16:36:31 +0800207 sctp_stream_free(stream);
208
209 stream->out = new->out;
210 stream->in = new->in;
211 stream->outcnt = new->outcnt;
212 stream->incnt = new->incnt;
213
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300214 sched->sched_all(stream);
215
Kent Overstreet2075e502019-03-11 23:31:22 -0700216 new->out.tree.root = NULL;
217 new->in.tree.root = NULL;
Xin Long6a9a27d2018-04-26 15:21:44 +0800218 new->outcnt = 0;
219 new->incnt = 0;
Xin Longcee360a2017-05-31 16:36:31 +0800220}
221
Xin Long7f9d68a2017-01-18 00:44:47 +0800222static int sctp_send_reconf(struct sctp_association *asoc,
223 struct sctp_chunk *chunk)
224{
Xin Long7f9d68a2017-01-18 00:44:47 +0800225 int retval = 0;
226
Xin Long4e7696d2019-12-09 13:45:18 +0800227 retval = sctp_primitive_RECONF(asoc->base.net, asoc, chunk);
Xin Long7f9d68a2017-01-18 00:44:47 +0800228 if (retval)
229 sctp_chunk_free(chunk);
230
231 return retval;
232}
233
Xin Longd570a592017-11-25 21:05:33 +0800234static bool sctp_stream_outq_is_empty(struct sctp_stream *stream,
235 __u16 str_nums, __be16 *str_list)
236{
237 struct sctp_association *asoc;
238 __u16 i;
239
240 asoc = container_of(stream, struct sctp_association, stream);
241 if (!asoc->outqueue.out_qlen)
242 return true;
243
244 if (!str_nums)
245 return false;
246
247 for (i = 0; i < str_nums; i++) {
248 __u16 sid = ntohs(str_list[i]);
249
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300250 if (SCTP_SO(stream, sid)->ext &&
251 !list_empty(&SCTP_SO(stream, sid)->ext->outq))
Xin Longd570a592017-11-25 21:05:33 +0800252 return false;
253 }
254
255 return true;
256}
257
Xin Long7f9d68a2017-01-18 00:44:47 +0800258int sctp_send_reset_streams(struct sctp_association *asoc,
259 struct sctp_reset_streams *params)
260{
Xin Longcee360a2017-05-31 16:36:31 +0800261 struct sctp_stream *stream = &asoc->stream;
Xin Long7f9d68a2017-01-18 00:44:47 +0800262 __u16 i, str_nums, *str_list;
263 struct sctp_chunk *chunk;
264 int retval = -EINVAL;
Xin Long1da4fc92017-10-28 19:43:54 +0800265 __be16 *nstr_list;
Xin Long7f9d68a2017-01-18 00:44:47 +0800266 bool out, in;
267
268 if (!asoc->peer.reconf_capable ||
269 !(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ)) {
270 retval = -ENOPROTOOPT;
271 goto out;
272 }
273
274 if (asoc->strreset_outstanding) {
275 retval = -EINPROGRESS;
276 goto out;
277 }
278
279 out = params->srs_flags & SCTP_STREAM_RESET_OUTGOING;
280 in = params->srs_flags & SCTP_STREAM_RESET_INCOMING;
281 if (!out && !in)
282 goto out;
283
284 str_nums = params->srs_number_streams;
285 str_list = params->srs_stream_list;
Xin Long423852f2017-11-15 17:00:11 +0800286 if (str_nums) {
287 int param_len = 0;
Xin Long7f9d68a2017-01-18 00:44:47 +0800288
Xin Long423852f2017-11-15 17:00:11 +0800289 if (out) {
290 for (i = 0; i < str_nums; i++)
291 if (str_list[i] >= stream->outcnt)
292 goto out;
293
294 param_len = str_nums * sizeof(__u16) +
295 sizeof(struct sctp_strreset_outreq);
296 }
297
298 if (in) {
299 for (i = 0; i < str_nums; i++)
300 if (str_list[i] >= stream->incnt)
301 goto out;
302
303 param_len += str_nums * sizeof(__u16) +
304 sizeof(struct sctp_strreset_inreq);
305 }
306
307 if (param_len > SCTP_MAX_CHUNK_LEN -
308 sizeof(struct sctp_reconf_chunk))
309 goto out;
310 }
Xin Long7f9d68a2017-01-18 00:44:47 +0800311
Xin Long1da4fc92017-10-28 19:43:54 +0800312 nstr_list = kcalloc(str_nums, sizeof(__be16), GFP_KERNEL);
313 if (!nstr_list) {
314 retval = -ENOMEM;
315 goto out;
316 }
Xin Long16e1a912017-02-17 12:45:40 +0800317
318 for (i = 0; i < str_nums; i++)
Xin Long1da4fc92017-10-28 19:43:54 +0800319 nstr_list[i] = htons(str_list[i]);
320
Xin Longd570a592017-11-25 21:05:33 +0800321 if (out && !sctp_stream_outq_is_empty(stream, str_nums, nstr_list)) {
zhengbin6d5afe22019-08-13 22:05:50 +0800322 kfree(nstr_list);
Xin Longd570a592017-11-25 21:05:33 +0800323 retval = -EAGAIN;
324 goto out;
325 }
326
Xin Long1da4fc92017-10-28 19:43:54 +0800327 chunk = sctp_make_strreset_req(asoc, str_nums, nstr_list, out, in);
328
329 kfree(nstr_list);
Xin Long16e1a912017-02-17 12:45:40 +0800330
Xin Long119aecb2017-02-09 01:18:16 +0800331 if (!chunk) {
332 retval = -ENOMEM;
Xin Long7f9d68a2017-01-18 00:44:47 +0800333 goto out;
Xin Long119aecb2017-02-09 01:18:16 +0800334 }
Xin Long7f9d68a2017-01-18 00:44:47 +0800335
336 if (out) {
337 if (str_nums)
338 for (i = 0; i < str_nums; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300339 SCTP_SO(stream, str_list[i])->state =
Xin Long7f9d68a2017-01-18 00:44:47 +0800340 SCTP_STREAM_CLOSED;
341 else
342 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300343 SCTP_SO(stream, i)->state = SCTP_STREAM_CLOSED;
Xin Long7f9d68a2017-01-18 00:44:47 +0800344 }
345
Xin Long7f9d68a2017-01-18 00:44:47 +0800346 asoc->strreset_chunk = chunk;
347 sctp_chunk_hold(asoc->strreset_chunk);
348
349 retval = sctp_send_reconf(asoc, chunk);
350 if (retval) {
351 sctp_chunk_put(asoc->strreset_chunk);
352 asoc->strreset_chunk = NULL;
Xin Long119aecb2017-02-09 01:18:16 +0800353 if (!out)
354 goto out;
355
356 if (str_nums)
357 for (i = 0; i < str_nums; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300358 SCTP_SO(stream, str_list[i])->state =
Xin Long119aecb2017-02-09 01:18:16 +0800359 SCTP_STREAM_OPEN;
360 else
361 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300362 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
Xin Long119aecb2017-02-09 01:18:16 +0800363
364 goto out;
Xin Long7f9d68a2017-01-18 00:44:47 +0800365 }
366
Xin Long119aecb2017-02-09 01:18:16 +0800367 asoc->strreset_outstanding = out + in;
368
Xin Long7f9d68a2017-01-18 00:44:47 +0800369out:
370 return retval;
371}
Xin Longa92ce1a2017-02-09 01:18:18 +0800372
373int sctp_send_reset_assoc(struct sctp_association *asoc)
374{
Xin Longcee360a2017-05-31 16:36:31 +0800375 struct sctp_stream *stream = &asoc->stream;
Xin Longa92ce1a2017-02-09 01:18:18 +0800376 struct sctp_chunk *chunk = NULL;
377 int retval;
378 __u16 i;
379
380 if (!asoc->peer.reconf_capable ||
381 !(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
382 return -ENOPROTOOPT;
383
384 if (asoc->strreset_outstanding)
385 return -EINPROGRESS;
386
Xin Long5c6144a2017-11-25 21:05:34 +0800387 if (!sctp_outq_is_empty(&asoc->outqueue))
388 return -EAGAIN;
389
Xin Longa92ce1a2017-02-09 01:18:18 +0800390 chunk = sctp_make_strreset_tsnreq(asoc);
391 if (!chunk)
392 return -ENOMEM;
393
394 /* Block further xmit of data until this request is completed */
Xin Longcee360a2017-05-31 16:36:31 +0800395 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300396 SCTP_SO(stream, i)->state = SCTP_STREAM_CLOSED;
Xin Longa92ce1a2017-02-09 01:18:18 +0800397
398 asoc->strreset_chunk = chunk;
399 sctp_chunk_hold(asoc->strreset_chunk);
400
401 retval = sctp_send_reconf(asoc, chunk);
402 if (retval) {
403 sctp_chunk_put(asoc->strreset_chunk);
404 asoc->strreset_chunk = NULL;
405
Xin Longcee360a2017-05-31 16:36:31 +0800406 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300407 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
Xin Longa92ce1a2017-02-09 01:18:18 +0800408
409 return retval;
410 }
411
412 asoc->strreset_outstanding = 1;
413
414 return 0;
415}
Xin Long242bd2d2017-02-09 01:18:20 +0800416
417int sctp_send_add_streams(struct sctp_association *asoc,
418 struct sctp_add_streams *params)
419{
Xin Longcee360a2017-05-31 16:36:31 +0800420 struct sctp_stream *stream = &asoc->stream;
Xin Long242bd2d2017-02-09 01:18:20 +0800421 struct sctp_chunk *chunk = NULL;
Wei Yongjundc826732017-10-31 13:28:16 +0000422 int retval;
Xin Long242bd2d2017-02-09 01:18:20 +0800423 __u32 outcnt, incnt;
424 __u16 out, in;
425
426 if (!asoc->peer.reconf_capable ||
427 !(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
428 retval = -ENOPROTOOPT;
429 goto out;
430 }
431
432 if (asoc->strreset_outstanding) {
433 retval = -EINPROGRESS;
434 goto out;
435 }
436
437 out = params->sas_outstrms;
438 in = params->sas_instrms;
439 outcnt = stream->outcnt + out;
440 incnt = stream->incnt + in;
441 if (outcnt > SCTP_MAX_STREAM || incnt > SCTP_MAX_STREAM ||
442 (!out && !in)) {
443 retval = -EINVAL;
444 goto out;
445 }
446
447 if (out) {
Marcelo Ricardo Leitnere090abd2017-10-03 19:20:09 -0300448 retval = sctp_stream_alloc_out(stream, outcnt, GFP_KERNEL);
449 if (retval)
Xin Long242bd2d2017-02-09 01:18:20 +0800450 goto out;
Xin Long242bd2d2017-02-09 01:18:20 +0800451 }
452
Xin Long242bd2d2017-02-09 01:18:20 +0800453 chunk = sctp_make_strreset_addstrm(asoc, out, in);
Wei Yongjundc826732017-10-31 13:28:16 +0000454 if (!chunk) {
455 retval = -ENOMEM;
Xin Long242bd2d2017-02-09 01:18:20 +0800456 goto out;
Wei Yongjundc826732017-10-31 13:28:16 +0000457 }
Xin Long242bd2d2017-02-09 01:18:20 +0800458
459 asoc->strreset_chunk = chunk;
460 sctp_chunk_hold(asoc->strreset_chunk);
461
462 retval = sctp_send_reconf(asoc, chunk);
463 if (retval) {
464 sctp_chunk_put(asoc->strreset_chunk);
465 asoc->strreset_chunk = NULL;
466 goto out;
467 }
468
Xin Long242bd2d2017-02-09 01:18:20 +0800469 asoc->strreset_outstanding = !!out + !!in;
470
471out:
472 return retval;
473}
Xin Long81054472017-02-17 12:45:39 +0800474
Xin Long3c918702017-06-30 11:52:16 +0800475static struct sctp_paramhdr *sctp_chunk_lookup_strreset_param(
Xin Long1da4fc92017-10-28 19:43:54 +0800476 struct sctp_association *asoc, __be32 resp_seq,
Xin Long50a41592017-03-10 12:11:09 +0800477 __be16 type)
Xin Long81054472017-02-17 12:45:39 +0800478{
479 struct sctp_chunk *chunk = asoc->strreset_chunk;
480 struct sctp_reconf_chunk *hdr;
481 union sctp_params param;
482
Xin Long50a41592017-03-10 12:11:09 +0800483 if (!chunk)
Xin Long81054472017-02-17 12:45:39 +0800484 return NULL;
485
486 hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
487 sctp_walk_params(param, hdr, params) {
488 /* sctp_strreset_tsnreq is actually the basic structure
489 * of all stream reconf params, so it's safe to use it
490 * to access request_seq.
491 */
492 struct sctp_strreset_tsnreq *req = param.v;
493
Xin Long50a41592017-03-10 12:11:09 +0800494 if ((!resp_seq || req->request_seq == resp_seq) &&
495 (!type || type == req->param_hdr.type))
Xin Long81054472017-02-17 12:45:39 +0800496 return param.v;
497 }
498
499 return NULL;
500}
501
Xin Longe4dc99c2017-04-15 22:00:27 +0800502static void sctp_update_strreset_result(struct sctp_association *asoc,
503 __u32 result)
504{
505 asoc->strreset_result[1] = asoc->strreset_result[0];
506 asoc->strreset_result[0] = result;
507}
508
Xin Long81054472017-02-17 12:45:39 +0800509struct sctp_chunk *sctp_process_strreset_outreq(
510 struct sctp_association *asoc,
511 union sctp_params param,
512 struct sctp_ulpevent **evp)
513{
514 struct sctp_strreset_outreq *outreq = param.v;
Xin Longcee360a2017-05-31 16:36:31 +0800515 struct sctp_stream *stream = &asoc->stream;
Xin Long81054472017-02-17 12:45:39 +0800516 __u32 result = SCTP_STRRESET_DENIED;
Xin Long1da4fc92017-10-28 19:43:54 +0800517 __be16 *str_p = NULL;
Xin Long81054472017-02-17 12:45:39 +0800518 __u32 request_seq;
Xin Long2e6dc4d2019-01-22 02:39:34 +0800519 __u16 i, nums;
Xin Long81054472017-02-17 12:45:39 +0800520
521 request_seq = ntohl(outreq->request_seq);
522
523 if (ntohl(outreq->send_reset_at_tsn) >
524 sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map)) {
525 result = SCTP_STRRESET_IN_PROGRESS;
Xin Longe4dc99c2017-04-15 22:00:27 +0800526 goto err;
Xin Long81054472017-02-17 12:45:39 +0800527 }
528
Xin Longe4dc99c2017-04-15 22:00:27 +0800529 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
530 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
Xin Long81054472017-02-17 12:45:39 +0800531 result = SCTP_STRRESET_ERR_BAD_SEQNO;
Xin Longe4dc99c2017-04-15 22:00:27 +0800532 goto err;
533 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
534 i = asoc->strreset_inseq - request_seq - 1;
535 result = asoc->strreset_result[i];
536 goto err;
Xin Long81054472017-02-17 12:45:39 +0800537 }
Xin Longe4dc99c2017-04-15 22:00:27 +0800538 asoc->strreset_inseq++;
Xin Long81054472017-02-17 12:45:39 +0800539
540 /* Check strreset_enable after inseq inc, as sender cannot tell
541 * the peer doesn't enable strreset after receiving response with
542 * result denied, as well as to keep consistent with bsd.
543 */
544 if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
545 goto out;
546
Xin Long2e6dc4d2019-01-22 02:39:34 +0800547 nums = (ntohs(param.p->length) - sizeof(*outreq)) / sizeof(__u16);
548 str_p = outreq->list_of_streams;
549 for (i = 0; i < nums; i++) {
550 if (ntohs(str_p[i]) >= stream->incnt) {
551 result = SCTP_STRRESET_ERR_WRONG_SSN;
552 goto out;
553 }
554 }
555
Xin Long81054472017-02-17 12:45:39 +0800556 if (asoc->strreset_chunk) {
Xin Long50a41592017-03-10 12:11:09 +0800557 if (!sctp_chunk_lookup_strreset_param(
558 asoc, outreq->response_seq,
559 SCTP_PARAM_RESET_IN_REQUEST)) {
Xin Long81054472017-02-17 12:45:39 +0800560 /* same process with outstanding isn't 0 */
561 result = SCTP_STRRESET_ERR_IN_PROGRESS;
562 goto out;
563 }
564
565 asoc->strreset_outstanding--;
566 asoc->strreset_outseq++;
567
568 if (!asoc->strreset_outstanding) {
Xin Long50a41592017-03-10 12:11:09 +0800569 struct sctp_transport *t;
570
Xin Long81054472017-02-17 12:45:39 +0800571 t = asoc->strreset_chunk->transport;
572 if (del_timer(&t->reconf_timer))
573 sctp_transport_put(t);
574
575 sctp_chunk_put(asoc->strreset_chunk);
576 asoc->strreset_chunk = NULL;
577 }
Xin Long81054472017-02-17 12:45:39 +0800578 }
579
Xin Long2e6dc4d2019-01-22 02:39:34 +0800580 if (nums)
Xin Long81054472017-02-17 12:45:39 +0800581 for (i = 0; i < nums; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300582 SCTP_SI(stream, ntohs(str_p[i]))->mid = 0;
Xin Long2e6dc4d2019-01-22 02:39:34 +0800583 else
Xin Long81054472017-02-17 12:45:39 +0800584 for (i = 0; i < stream->incnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300585 SCTP_SI(stream, i)->mid = 0;
Xin Long81054472017-02-17 12:45:39 +0800586
587 result = SCTP_STRRESET_PERFORMED;
588
589 *evp = sctp_ulpevent_make_stream_reset_event(asoc,
Xin Long2e6dc4d2019-01-22 02:39:34 +0800590 SCTP_STREAM_RESET_INCOMING_SSN, nums, str_p, GFP_ATOMIC);
Xin Long81054472017-02-17 12:45:39 +0800591
592out:
Xin Longe4dc99c2017-04-15 22:00:27 +0800593 sctp_update_strreset_result(asoc, result);
594err:
Xin Long81054472017-02-17 12:45:39 +0800595 return sctp_make_strreset_resp(asoc, result, request_seq);
596}
Xin Long16e1a912017-02-17 12:45:40 +0800597
598struct sctp_chunk *sctp_process_strreset_inreq(
599 struct sctp_association *asoc,
600 union sctp_params param,
601 struct sctp_ulpevent **evp)
602{
603 struct sctp_strreset_inreq *inreq = param.v;
Xin Longcee360a2017-05-31 16:36:31 +0800604 struct sctp_stream *stream = &asoc->stream;
Xin Long16e1a912017-02-17 12:45:40 +0800605 __u32 result = SCTP_STRRESET_DENIED;
606 struct sctp_chunk *chunk = NULL;
Xin Long16e1a912017-02-17 12:45:40 +0800607 __u32 request_seq;
Xin Long1da4fc92017-10-28 19:43:54 +0800608 __u16 i, nums;
609 __be16 *str_p;
Xin Long16e1a912017-02-17 12:45:40 +0800610
611 request_seq = ntohl(inreq->request_seq);
Xin Longd0f025e2017-04-15 22:00:28 +0800612 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
613 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
Xin Long16e1a912017-02-17 12:45:40 +0800614 result = SCTP_STRRESET_ERR_BAD_SEQNO;
Xin Longd0f025e2017-04-15 22:00:28 +0800615 goto err;
616 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
617 i = asoc->strreset_inseq - request_seq - 1;
618 result = asoc->strreset_result[i];
619 if (result == SCTP_STRRESET_PERFORMED)
620 return NULL;
621 goto err;
Xin Long16e1a912017-02-17 12:45:40 +0800622 }
Xin Longd0f025e2017-04-15 22:00:28 +0800623 asoc->strreset_inseq++;
Xin Long16e1a912017-02-17 12:45:40 +0800624
625 if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
626 goto out;
627
628 if (asoc->strreset_outstanding) {
629 result = SCTP_STRRESET_ERR_IN_PROGRESS;
630 goto out;
631 }
632
Xin Long3aa623d2017-11-25 21:05:32 +0800633 nums = (ntohs(param.p->length) - sizeof(*inreq)) / sizeof(__u16);
Xin Long16e1a912017-02-17 12:45:40 +0800634 str_p = inreq->list_of_streams;
635 for (i = 0; i < nums; i++) {
636 if (ntohs(str_p[i]) >= stream->outcnt) {
637 result = SCTP_STRRESET_ERR_WRONG_SSN;
638 goto out;
639 }
640 }
641
Xin Longd570a592017-11-25 21:05:33 +0800642 if (!sctp_stream_outq_is_empty(stream, nums, str_p)) {
643 result = SCTP_STRRESET_IN_PROGRESS;
644 asoc->strreset_inseq--;
645 goto err;
646 }
647
Xin Long16e1a912017-02-17 12:45:40 +0800648 chunk = sctp_make_strreset_req(asoc, nums, str_p, 1, 0);
649 if (!chunk)
650 goto out;
651
652 if (nums)
653 for (i = 0; i < nums; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300654 SCTP_SO(stream, ntohs(str_p[i]))->state =
Xin Long16e1a912017-02-17 12:45:40 +0800655 SCTP_STREAM_CLOSED;
656 else
657 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300658 SCTP_SO(stream, i)->state = SCTP_STREAM_CLOSED;
Xin Long16e1a912017-02-17 12:45:40 +0800659
660 asoc->strreset_chunk = chunk;
661 asoc->strreset_outstanding = 1;
662 sctp_chunk_hold(asoc->strreset_chunk);
663
Xin Longd0f025e2017-04-15 22:00:28 +0800664 result = SCTP_STRRESET_PERFORMED;
665
Xin Long16e1a912017-02-17 12:45:40 +0800666out:
Xin Longd0f025e2017-04-15 22:00:28 +0800667 sctp_update_strreset_result(asoc, result);
668err:
Xin Long16e1a912017-02-17 12:45:40 +0800669 if (!chunk)
670 chunk = sctp_make_strreset_resp(asoc, result, request_seq);
671
672 return chunk;
673}
Xin Long692787c2017-03-10 12:11:07 +0800674
675struct sctp_chunk *sctp_process_strreset_tsnreq(
676 struct sctp_association *asoc,
677 union sctp_params param,
678 struct sctp_ulpevent **evp)
679{
680 __u32 init_tsn = 0, next_tsn = 0, max_tsn_seen;
681 struct sctp_strreset_tsnreq *tsnreq = param.v;
Xin Longcee360a2017-05-31 16:36:31 +0800682 struct sctp_stream *stream = &asoc->stream;
Xin Long692787c2017-03-10 12:11:07 +0800683 __u32 result = SCTP_STRRESET_DENIED;
684 __u32 request_seq;
685 __u16 i;
686
687 request_seq = ntohl(tsnreq->request_seq);
Xin Long6c801382017-04-15 22:00:29 +0800688 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
689 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
Xin Long692787c2017-03-10 12:11:07 +0800690 result = SCTP_STRRESET_ERR_BAD_SEQNO;
Xin Long6c801382017-04-15 22:00:29 +0800691 goto err;
692 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
693 i = asoc->strreset_inseq - request_seq - 1;
694 result = asoc->strreset_result[i];
695 if (result == SCTP_STRRESET_PERFORMED) {
Xin Long52a39582017-11-25 21:05:36 +0800696 next_tsn = asoc->ctsn_ack_point + 1;
Xin Long6c801382017-04-15 22:00:29 +0800697 init_tsn =
698 sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + 1;
699 }
700 goto err;
Xin Long692787c2017-03-10 12:11:07 +0800701 }
Xin Long5c6144a2017-11-25 21:05:34 +0800702
703 if (!sctp_outq_is_empty(&asoc->outqueue)) {
704 result = SCTP_STRRESET_IN_PROGRESS;
705 goto err;
706 }
707
Xin Long6c801382017-04-15 22:00:29 +0800708 asoc->strreset_inseq++;
Xin Long692787c2017-03-10 12:11:07 +0800709
710 if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
711 goto out;
712
713 if (asoc->strreset_outstanding) {
714 result = SCTP_STRRESET_ERR_IN_PROGRESS;
715 goto out;
716 }
717
Xin Long159f2a72017-11-25 21:05:35 +0800718 /* G4: The same processing as though a FWD-TSN chunk (as defined in
719 * [RFC3758]) with all streams affected and a new cumulative TSN
720 * ACK of the Receiver's Next TSN minus 1 were received MUST be
721 * performed.
Xin Long692787c2017-03-10 12:11:07 +0800722 */
723 max_tsn_seen = sctp_tsnmap_get_max_tsn_seen(&asoc->peer.tsn_map);
Xin Long47b20a82017-12-15 00:41:28 +0800724 asoc->stream.si->report_ftsn(&asoc->ulpq, max_tsn_seen);
Xin Long692787c2017-03-10 12:11:07 +0800725
726 /* G1: Compute an appropriate value for the Receiver's Next TSN -- the
727 * TSN that the peer should use to send the next DATA chunk. The
728 * value SHOULD be the smallest TSN not acknowledged by the
729 * receiver of the request plus 2^31.
730 */
731 init_tsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + (1 << 31);
732 sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
733 init_tsn, GFP_ATOMIC);
734
Xin Long159f2a72017-11-25 21:05:35 +0800735 /* G3: The same processing as though a SACK chunk with no gap report
736 * and a cumulative TSN ACK of the Sender's Next TSN minus 1 were
737 * received MUST be performed.
Xin Long692787c2017-03-10 12:11:07 +0800738 */
739 sctp_outq_free(&asoc->outqueue);
740
741 /* G2: Compute an appropriate value for the local endpoint's next TSN,
742 * i.e., the next TSN assigned by the receiver of the SSN/TSN reset
743 * chunk. The value SHOULD be the highest TSN sent by the receiver
744 * of the request plus 1.
745 */
746 next_tsn = asoc->next_tsn;
747 asoc->ctsn_ack_point = next_tsn - 1;
748 asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
749
750 /* G5: The next expected and outgoing SSNs MUST be reset to 0 for all
751 * incoming and outgoing streams.
752 */
Xin Long107e2422017-12-15 00:41:31 +0800753 for (i = 0; i < stream->outcnt; i++) {
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300754 SCTP_SO(stream, i)->mid = 0;
755 SCTP_SO(stream, i)->mid_uo = 0;
Xin Long107e2422017-12-15 00:41:31 +0800756 }
Xin Long692787c2017-03-10 12:11:07 +0800757 for (i = 0; i < stream->incnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300758 SCTP_SI(stream, i)->mid = 0;
Xin Long692787c2017-03-10 12:11:07 +0800759
760 result = SCTP_STRRESET_PERFORMED;
761
762 *evp = sctp_ulpevent_make_assoc_reset_event(asoc, 0, init_tsn,
763 next_tsn, GFP_ATOMIC);
764
765out:
Xin Long6c801382017-04-15 22:00:29 +0800766 sctp_update_strreset_result(asoc, result);
767err:
Xin Long692787c2017-03-10 12:11:07 +0800768 return sctp_make_strreset_tsnresp(asoc, result, request_seq,
769 next_tsn, init_tsn);
770}
Xin Long50a41592017-03-10 12:11:09 +0800771
772struct sctp_chunk *sctp_process_strreset_addstrm_out(
773 struct sctp_association *asoc,
774 union sctp_params param,
775 struct sctp_ulpevent **evp)
776{
777 struct sctp_strreset_addstrm *addstrm = param.v;
Xin Longcee360a2017-05-31 16:36:31 +0800778 struct sctp_stream *stream = &asoc->stream;
Xin Long50a41592017-03-10 12:11:09 +0800779 __u32 result = SCTP_STRRESET_DENIED;
Xin Long50a41592017-03-10 12:11:09 +0800780 __u32 request_seq, incnt;
Xin Longe4dc99c2017-04-15 22:00:27 +0800781 __u16 in, i;
Xin Long50a41592017-03-10 12:11:09 +0800782
783 request_seq = ntohl(addstrm->request_seq);
Xin Longe4dc99c2017-04-15 22:00:27 +0800784 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
785 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
Xin Long50a41592017-03-10 12:11:09 +0800786 result = SCTP_STRRESET_ERR_BAD_SEQNO;
Xin Longe4dc99c2017-04-15 22:00:27 +0800787 goto err;
788 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
789 i = asoc->strreset_inseq - request_seq - 1;
790 result = asoc->strreset_result[i];
791 goto err;
Xin Long50a41592017-03-10 12:11:09 +0800792 }
Xin Longe4dc99c2017-04-15 22:00:27 +0800793 asoc->strreset_inseq++;
Xin Long50a41592017-03-10 12:11:09 +0800794
795 if (!(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ))
796 goto out;
797
Xin Long8220c872019-01-22 02:40:12 +0800798 in = ntohs(addstrm->number_of_streams);
799 incnt = stream->incnt + in;
800 if (!in || incnt > SCTP_MAX_STREAM)
801 goto out;
802
803 if (sctp_stream_alloc_in(stream, incnt, GFP_ATOMIC))
804 goto out;
805
Xin Long50a41592017-03-10 12:11:09 +0800806 if (asoc->strreset_chunk) {
807 if (!sctp_chunk_lookup_strreset_param(
808 asoc, 0, SCTP_PARAM_RESET_ADD_IN_STREAMS)) {
809 /* same process with outstanding isn't 0 */
810 result = SCTP_STRRESET_ERR_IN_PROGRESS;
811 goto out;
812 }
813
814 asoc->strreset_outstanding--;
815 asoc->strreset_outseq++;
816
817 if (!asoc->strreset_outstanding) {
818 struct sctp_transport *t;
819
820 t = asoc->strreset_chunk->transport;
821 if (del_timer(&t->reconf_timer))
822 sctp_transport_put(t);
823
824 sctp_chunk_put(asoc->strreset_chunk);
825 asoc->strreset_chunk = NULL;
826 }
827 }
828
Xin Long50a41592017-03-10 12:11:09 +0800829 stream->incnt = incnt;
830
831 result = SCTP_STRRESET_PERFORMED;
832
833 *evp = sctp_ulpevent_make_stream_change_event(asoc,
834 0, ntohs(addstrm->number_of_streams), 0, GFP_ATOMIC);
835
836out:
Xin Longe4dc99c2017-04-15 22:00:27 +0800837 sctp_update_strreset_result(asoc, result);
838err:
Xin Long50a41592017-03-10 12:11:09 +0800839 return sctp_make_strreset_resp(asoc, result, request_seq);
840}
Xin Longc5c4ebb2017-03-10 12:11:10 +0800841
842struct sctp_chunk *sctp_process_strreset_addstrm_in(
843 struct sctp_association *asoc,
844 union sctp_params param,
845 struct sctp_ulpevent **evp)
846{
847 struct sctp_strreset_addstrm *addstrm = param.v;
Xin Longcee360a2017-05-31 16:36:31 +0800848 struct sctp_stream *stream = &asoc->stream;
Xin Longc5c4ebb2017-03-10 12:11:10 +0800849 __u32 result = SCTP_STRRESET_DENIED;
Xin Longc5c4ebb2017-03-10 12:11:10 +0800850 struct sctp_chunk *chunk = NULL;
851 __u32 request_seq, outcnt;
Xin Longd0f025e2017-04-15 22:00:28 +0800852 __u16 out, i;
Marcelo Ricardo Leitnere090abd2017-10-03 19:20:09 -0300853 int ret;
Xin Longc5c4ebb2017-03-10 12:11:10 +0800854
855 request_seq = ntohl(addstrm->request_seq);
Xin Longd0f025e2017-04-15 22:00:28 +0800856 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
857 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
Xin Longc5c4ebb2017-03-10 12:11:10 +0800858 result = SCTP_STRRESET_ERR_BAD_SEQNO;
Xin Longd0f025e2017-04-15 22:00:28 +0800859 goto err;
860 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
861 i = asoc->strreset_inseq - request_seq - 1;
862 result = asoc->strreset_result[i];
863 if (result == SCTP_STRRESET_PERFORMED)
864 return NULL;
865 goto err;
Xin Longc5c4ebb2017-03-10 12:11:10 +0800866 }
Xin Longd0f025e2017-04-15 22:00:28 +0800867 asoc->strreset_inseq++;
Xin Longc5c4ebb2017-03-10 12:11:10 +0800868
869 if (!(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ))
870 goto out;
871
872 if (asoc->strreset_outstanding) {
873 result = SCTP_STRRESET_ERR_IN_PROGRESS;
874 goto out;
875 }
876
877 out = ntohs(addstrm->number_of_streams);
878 outcnt = stream->outcnt + out;
879 if (!out || outcnt > SCTP_MAX_STREAM)
880 goto out;
881
Marcelo Ricardo Leitnere090abd2017-10-03 19:20:09 -0300882 ret = sctp_stream_alloc_out(stream, outcnt, GFP_ATOMIC);
883 if (ret)
Xin Longc5c4ebb2017-03-10 12:11:10 +0800884 goto out;
885
Xin Longc5c4ebb2017-03-10 12:11:10 +0800886 chunk = sctp_make_strreset_addstrm(asoc, out, 0);
887 if (!chunk)
888 goto out;
889
890 asoc->strreset_chunk = chunk;
891 asoc->strreset_outstanding = 1;
892 sctp_chunk_hold(asoc->strreset_chunk);
893
894 stream->outcnt = outcnt;
895
Xin Longd0f025e2017-04-15 22:00:28 +0800896 result = SCTP_STRRESET_PERFORMED;
897
Xin Longc5c4ebb2017-03-10 12:11:10 +0800898out:
Xin Longd0f025e2017-04-15 22:00:28 +0800899 sctp_update_strreset_result(asoc, result);
900err:
Xin Longc5c4ebb2017-03-10 12:11:10 +0800901 if (!chunk)
902 chunk = sctp_make_strreset_resp(asoc, result, request_seq);
903
904 return chunk;
905}
Xin Long11ae76e2017-03-10 12:11:11 +0800906
907struct sctp_chunk *sctp_process_strreset_resp(
908 struct sctp_association *asoc,
909 union sctp_params param,
910 struct sctp_ulpevent **evp)
911{
Xin Longcee360a2017-05-31 16:36:31 +0800912 struct sctp_stream *stream = &asoc->stream;
Xin Long11ae76e2017-03-10 12:11:11 +0800913 struct sctp_strreset_resp *resp = param.v;
Xin Long11ae76e2017-03-10 12:11:11 +0800914 struct sctp_transport *t;
915 __u16 i, nums, flags = 0;
Xin Long3c918702017-06-30 11:52:16 +0800916 struct sctp_paramhdr *req;
Xin Long11ae76e2017-03-10 12:11:11 +0800917 __u32 result;
918
919 req = sctp_chunk_lookup_strreset_param(asoc, resp->response_seq, 0);
920 if (!req)
921 return NULL;
922
923 result = ntohl(resp->result);
924 if (result != SCTP_STRRESET_PERFORMED) {
925 /* if in progress, do nothing but retransmit */
926 if (result == SCTP_STRRESET_IN_PROGRESS)
927 return NULL;
928 else if (result == SCTP_STRRESET_DENIED)
929 flags = SCTP_STREAM_RESET_DENIED;
930 else
931 flags = SCTP_STREAM_RESET_FAILED;
932 }
933
934 if (req->type == SCTP_PARAM_RESET_OUT_REQUEST) {
935 struct sctp_strreset_outreq *outreq;
Xin Long1da4fc92017-10-28 19:43:54 +0800936 __be16 *str_p;
Xin Long11ae76e2017-03-10 12:11:11 +0800937
938 outreq = (struct sctp_strreset_outreq *)req;
Xin Longedb12f22017-04-15 21:56:57 +0800939 str_p = outreq->list_of_streams;
Xin Long3aa623d2017-11-25 21:05:32 +0800940 nums = (ntohs(outreq->param_hdr.length) - sizeof(*outreq)) /
941 sizeof(__u16);
Xin Long11ae76e2017-03-10 12:11:11 +0800942
943 if (result == SCTP_STRRESET_PERFORMED) {
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300944 struct sctp_stream_out *sout;
Xin Long11ae76e2017-03-10 12:11:11 +0800945 if (nums) {
Xin Long107e2422017-12-15 00:41:31 +0800946 for (i = 0; i < nums; i++) {
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300947 sout = SCTP_SO(stream, ntohs(str_p[i]));
948 sout->mid = 0;
949 sout->mid_uo = 0;
Xin Long107e2422017-12-15 00:41:31 +0800950 }
Xin Long11ae76e2017-03-10 12:11:11 +0800951 } else {
Xin Long107e2422017-12-15 00:41:31 +0800952 for (i = 0; i < stream->outcnt; i++) {
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300953 sout = SCTP_SO(stream, i);
954 sout->mid = 0;
955 sout->mid_uo = 0;
Xin Long107e2422017-12-15 00:41:31 +0800956 }
Xin Long11ae76e2017-03-10 12:11:11 +0800957 }
Xin Long11ae76e2017-03-10 12:11:11 +0800958 }
959
Xin Long2e6dc4d2019-01-22 02:39:34 +0800960 flags |= SCTP_STREAM_RESET_OUTGOING_SSN;
961
Xin Long11ae76e2017-03-10 12:11:11 +0800962 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300963 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
Xin Long11ae76e2017-03-10 12:11:11 +0800964
965 *evp = sctp_ulpevent_make_stream_reset_event(asoc, flags,
966 nums, str_p, GFP_ATOMIC);
967 } else if (req->type == SCTP_PARAM_RESET_IN_REQUEST) {
968 struct sctp_strreset_inreq *inreq;
Xin Long1da4fc92017-10-28 19:43:54 +0800969 __be16 *str_p;
Xin Long11ae76e2017-03-10 12:11:11 +0800970
971 /* if the result is performed, it's impossible for inreq */
972 if (result == SCTP_STRRESET_PERFORMED)
973 return NULL;
974
975 inreq = (struct sctp_strreset_inreq *)req;
Xin Longedb12f22017-04-15 21:56:57 +0800976 str_p = inreq->list_of_streams;
Xin Long3aa623d2017-11-25 21:05:32 +0800977 nums = (ntohs(inreq->param_hdr.length) - sizeof(*inreq)) /
978 sizeof(__u16);
Xin Long11ae76e2017-03-10 12:11:11 +0800979
Xin Long2e6dc4d2019-01-22 02:39:34 +0800980 flags |= SCTP_STREAM_RESET_INCOMING_SSN;
981
Xin Long11ae76e2017-03-10 12:11:11 +0800982 *evp = sctp_ulpevent_make_stream_reset_event(asoc, flags,
983 nums, str_p, GFP_ATOMIC);
984 } else if (req->type == SCTP_PARAM_RESET_TSN_REQUEST) {
985 struct sctp_strreset_resptsn *resptsn;
986 __u32 stsn, rtsn;
987
988 /* check for resptsn, as sctp_verify_reconf didn't do it*/
989 if (ntohs(param.p->length) != sizeof(*resptsn))
990 return NULL;
991
992 resptsn = (struct sctp_strreset_resptsn *)resp;
993 stsn = ntohl(resptsn->senders_next_tsn);
994 rtsn = ntohl(resptsn->receivers_next_tsn);
995
996 if (result == SCTP_STRRESET_PERFORMED) {
997 __u32 mtsn = sctp_tsnmap_get_max_tsn_seen(
998 &asoc->peer.tsn_map);
Xin Long159f2a72017-11-25 21:05:35 +0800999 LIST_HEAD(temp);
Xin Long11ae76e2017-03-10 12:11:11 +08001000
Xin Long47b20a82017-12-15 00:41:28 +08001001 asoc->stream.si->report_ftsn(&asoc->ulpq, mtsn);
Xin Long11ae76e2017-03-10 12:11:11 +08001002
1003 sctp_tsnmap_init(&asoc->peer.tsn_map,
1004 SCTP_TSN_MAP_INITIAL,
1005 stsn, GFP_ATOMIC);
1006
Xin Long159f2a72017-11-25 21:05:35 +08001007 /* Clean up sacked and abandoned queues only. As the
1008 * out_chunk_list may not be empty, splice it to temp,
1009 * then get it back after sctp_outq_free is done.
1010 */
1011 list_splice_init(&asoc->outqueue.out_chunk_list, &temp);
Xin Long11ae76e2017-03-10 12:11:11 +08001012 sctp_outq_free(&asoc->outqueue);
Xin Long159f2a72017-11-25 21:05:35 +08001013 list_splice_init(&temp, &asoc->outqueue.out_chunk_list);
Xin Long11ae76e2017-03-10 12:11:11 +08001014
1015 asoc->next_tsn = rtsn;
1016 asoc->ctsn_ack_point = asoc->next_tsn - 1;
1017 asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
1018
Xin Long107e2422017-12-15 00:41:31 +08001019 for (i = 0; i < stream->outcnt; i++) {
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03001020 SCTP_SO(stream, i)->mid = 0;
1021 SCTP_SO(stream, i)->mid_uo = 0;
Xin Long107e2422017-12-15 00:41:31 +08001022 }
Xin Long11ae76e2017-03-10 12:11:11 +08001023 for (i = 0; i < stream->incnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03001024 SCTP_SI(stream, i)->mid = 0;
Xin Long11ae76e2017-03-10 12:11:11 +08001025 }
1026
1027 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03001028 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
Xin Long11ae76e2017-03-10 12:11:11 +08001029
1030 *evp = sctp_ulpevent_make_assoc_reset_event(asoc, flags,
1031 stsn, rtsn, GFP_ATOMIC);
1032 } else if (req->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS) {
1033 struct sctp_strreset_addstrm *addstrm;
1034 __u16 number;
1035
1036 addstrm = (struct sctp_strreset_addstrm *)req;
1037 nums = ntohs(addstrm->number_of_streams);
1038 number = stream->outcnt - nums;
1039
1040 if (result == SCTP_STRRESET_PERFORMED)
1041 for (i = number; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03001042 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
Xin Long11ae76e2017-03-10 12:11:11 +08001043 else
1044 stream->outcnt = number;
1045
1046 *evp = sctp_ulpevent_make_stream_change_event(asoc, flags,
1047 0, nums, GFP_ATOMIC);
1048 } else if (req->type == SCTP_PARAM_RESET_ADD_IN_STREAMS) {
1049 struct sctp_strreset_addstrm *addstrm;
1050
1051 /* if the result is performed, it's impossible for addstrm in
1052 * request.
1053 */
1054 if (result == SCTP_STRRESET_PERFORMED)
1055 return NULL;
1056
1057 addstrm = (struct sctp_strreset_addstrm *)req;
1058 nums = ntohs(addstrm->number_of_streams);
1059
1060 *evp = sctp_ulpevent_make_stream_change_event(asoc, flags,
1061 nums, 0, GFP_ATOMIC);
1062 }
1063
1064 asoc->strreset_outstanding--;
1065 asoc->strreset_outseq++;
1066
1067 /* remove everything for this reconf request */
1068 if (!asoc->strreset_outstanding) {
1069 t = asoc->strreset_chunk->transport;
1070 if (del_timer(&t->reconf_timer))
1071 sctp_transport_put(t);
1072
1073 sctp_chunk_put(asoc->strreset_chunk);
1074 asoc->strreset_chunk = NULL;
1075 }
1076
1077 return NULL;
1078}