blob: 93ed07877337eace4ef7f4775dda5868359ada37 [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);
87 if (ret)
88 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);
103 if (ret)
104 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)
122 goto in;
123
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)
131 goto out;
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 Leitner1ae2eaa2017-10-03 19:20:08 -0300136in:
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);
142 if (ret) {
143 sched->free(stream);
Kent Overstreet2075e502019-03-11 23:31:22 -0700144 genradix_free(&stream->out);
Marcelo Ricardo Leitner79d08952018-01-02 19:44:37 -0200145 stream->outcnt = 0;
146 goto out;
Xin Longa8386312017-01-06 22:18:33 +0800147 }
148
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300149out:
150 return ret;
Xin Longa8386312017-01-06 22:18:33 +0800151}
152
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -0300153int sctp_stream_init_ext(struct sctp_stream *stream, __u16 sid)
154{
155 struct sctp_stream_out_ext *soute;
156
157 soute = kzalloc(sizeof(*soute), GFP_KERNEL);
158 if (!soute)
159 return -ENOMEM;
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300160 SCTP_SO(stream, sid)->ext = soute;
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -0300161
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300162 return sctp_sched_init_sid(stream, sid, GFP_KERNEL);
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -0300163}
164
Xin Longa8386312017-01-06 22:18:33 +0800165void sctp_stream_free(struct sctp_stream *stream)
166{
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300167 struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -0300168 int i;
169
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300170 sched->free(stream);
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -0300171 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300172 kfree(SCTP_SO(stream, i)->ext);
Kent Overstreet2075e502019-03-11 23:31:22 -0700173 genradix_free(&stream->out);
174 genradix_free(&stream->in);
Xin Longa8386312017-01-06 22:18:33 +0800175}
176
177void sctp_stream_clear(struct sctp_stream *stream)
178{
179 int i;
180
Xin Long107e2422017-12-15 00:41:31 +0800181 for (i = 0; i < stream->outcnt; i++) {
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300182 SCTP_SO(stream, i)->mid = 0;
183 SCTP_SO(stream, i)->mid_uo = 0;
Xin Long107e2422017-12-15 00:41:31 +0800184 }
Xin Longa8386312017-01-06 22:18:33 +0800185
186 for (i = 0; i < stream->incnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300187 SCTP_SI(stream, i)->mid = 0;
Xin Longa8386312017-01-06 22:18:33 +0800188}
Xin Long7f9d68a2017-01-18 00:44:47 +0800189
Xin Longcee360a2017-05-31 16:36:31 +0800190void sctp_stream_update(struct sctp_stream *stream, struct sctp_stream *new)
191{
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300192 struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
193
194 sched->unsched_all(stream);
195 sctp_stream_outq_migrate(stream, new, new->outcnt);
Xin Longcee360a2017-05-31 16:36:31 +0800196 sctp_stream_free(stream);
197
198 stream->out = new->out;
199 stream->in = new->in;
200 stream->outcnt = new->outcnt;
201 stream->incnt = new->incnt;
202
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300203 sched->sched_all(stream);
204
Kent Overstreet2075e502019-03-11 23:31:22 -0700205 new->out.tree.root = NULL;
206 new->in.tree.root = NULL;
Xin Long6a9a27d2018-04-26 15:21:44 +0800207 new->outcnt = 0;
208 new->incnt = 0;
Xin Longcee360a2017-05-31 16:36:31 +0800209}
210
Xin Long7f9d68a2017-01-18 00:44:47 +0800211static int sctp_send_reconf(struct sctp_association *asoc,
212 struct sctp_chunk *chunk)
213{
214 struct net *net = sock_net(asoc->base.sk);
215 int retval = 0;
216
217 retval = sctp_primitive_RECONF(net, asoc, chunk);
218 if (retval)
219 sctp_chunk_free(chunk);
220
221 return retval;
222}
223
Xin Longd570a592017-11-25 21:05:33 +0800224static bool sctp_stream_outq_is_empty(struct sctp_stream *stream,
225 __u16 str_nums, __be16 *str_list)
226{
227 struct sctp_association *asoc;
228 __u16 i;
229
230 asoc = container_of(stream, struct sctp_association, stream);
231 if (!asoc->outqueue.out_qlen)
232 return true;
233
234 if (!str_nums)
235 return false;
236
237 for (i = 0; i < str_nums; i++) {
238 __u16 sid = ntohs(str_list[i]);
239
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300240 if (SCTP_SO(stream, sid)->ext &&
241 !list_empty(&SCTP_SO(stream, sid)->ext->outq))
Xin Longd570a592017-11-25 21:05:33 +0800242 return false;
243 }
244
245 return true;
246}
247
Xin Long7f9d68a2017-01-18 00:44:47 +0800248int sctp_send_reset_streams(struct sctp_association *asoc,
249 struct sctp_reset_streams *params)
250{
Xin Longcee360a2017-05-31 16:36:31 +0800251 struct sctp_stream *stream = &asoc->stream;
Xin Long7f9d68a2017-01-18 00:44:47 +0800252 __u16 i, str_nums, *str_list;
253 struct sctp_chunk *chunk;
254 int retval = -EINVAL;
Xin Long1da4fc92017-10-28 19:43:54 +0800255 __be16 *nstr_list;
Xin Long7f9d68a2017-01-18 00:44:47 +0800256 bool out, in;
257
258 if (!asoc->peer.reconf_capable ||
259 !(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ)) {
260 retval = -ENOPROTOOPT;
261 goto out;
262 }
263
264 if (asoc->strreset_outstanding) {
265 retval = -EINPROGRESS;
266 goto out;
267 }
268
269 out = params->srs_flags & SCTP_STREAM_RESET_OUTGOING;
270 in = params->srs_flags & SCTP_STREAM_RESET_INCOMING;
271 if (!out && !in)
272 goto out;
273
274 str_nums = params->srs_number_streams;
275 str_list = params->srs_stream_list;
Xin Long423852f2017-11-15 17:00:11 +0800276 if (str_nums) {
277 int param_len = 0;
Xin Long7f9d68a2017-01-18 00:44:47 +0800278
Xin Long423852f2017-11-15 17:00:11 +0800279 if (out) {
280 for (i = 0; i < str_nums; i++)
281 if (str_list[i] >= stream->outcnt)
282 goto out;
283
284 param_len = str_nums * sizeof(__u16) +
285 sizeof(struct sctp_strreset_outreq);
286 }
287
288 if (in) {
289 for (i = 0; i < str_nums; i++)
290 if (str_list[i] >= stream->incnt)
291 goto out;
292
293 param_len += str_nums * sizeof(__u16) +
294 sizeof(struct sctp_strreset_inreq);
295 }
296
297 if (param_len > SCTP_MAX_CHUNK_LEN -
298 sizeof(struct sctp_reconf_chunk))
299 goto out;
300 }
Xin Long7f9d68a2017-01-18 00:44:47 +0800301
Xin Long1da4fc92017-10-28 19:43:54 +0800302 nstr_list = kcalloc(str_nums, sizeof(__be16), GFP_KERNEL);
303 if (!nstr_list) {
304 retval = -ENOMEM;
305 goto out;
306 }
Xin Long16e1a912017-02-17 12:45:40 +0800307
308 for (i = 0; i < str_nums; i++)
Xin Long1da4fc92017-10-28 19:43:54 +0800309 nstr_list[i] = htons(str_list[i]);
310
Xin Longd570a592017-11-25 21:05:33 +0800311 if (out && !sctp_stream_outq_is_empty(stream, str_nums, nstr_list)) {
312 retval = -EAGAIN;
313 goto out;
314 }
315
Xin Long1da4fc92017-10-28 19:43:54 +0800316 chunk = sctp_make_strreset_req(asoc, str_nums, nstr_list, out, in);
317
318 kfree(nstr_list);
Xin Long16e1a912017-02-17 12:45:40 +0800319
Xin Long119aecb2017-02-09 01:18:16 +0800320 if (!chunk) {
321 retval = -ENOMEM;
Xin Long7f9d68a2017-01-18 00:44:47 +0800322 goto out;
Xin Long119aecb2017-02-09 01:18:16 +0800323 }
Xin Long7f9d68a2017-01-18 00:44:47 +0800324
325 if (out) {
326 if (str_nums)
327 for (i = 0; i < str_nums; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300328 SCTP_SO(stream, str_list[i])->state =
Xin Long7f9d68a2017-01-18 00:44:47 +0800329 SCTP_STREAM_CLOSED;
330 else
331 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300332 SCTP_SO(stream, i)->state = SCTP_STREAM_CLOSED;
Xin Long7f9d68a2017-01-18 00:44:47 +0800333 }
334
Xin Long7f9d68a2017-01-18 00:44:47 +0800335 asoc->strreset_chunk = chunk;
336 sctp_chunk_hold(asoc->strreset_chunk);
337
338 retval = sctp_send_reconf(asoc, chunk);
339 if (retval) {
340 sctp_chunk_put(asoc->strreset_chunk);
341 asoc->strreset_chunk = NULL;
Xin Long119aecb2017-02-09 01:18:16 +0800342 if (!out)
343 goto out;
344
345 if (str_nums)
346 for (i = 0; i < str_nums; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300347 SCTP_SO(stream, str_list[i])->state =
Xin Long119aecb2017-02-09 01:18:16 +0800348 SCTP_STREAM_OPEN;
349 else
350 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300351 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
Xin Long119aecb2017-02-09 01:18:16 +0800352
353 goto out;
Xin Long7f9d68a2017-01-18 00:44:47 +0800354 }
355
Xin Long119aecb2017-02-09 01:18:16 +0800356 asoc->strreset_outstanding = out + in;
357
Xin Long7f9d68a2017-01-18 00:44:47 +0800358out:
359 return retval;
360}
Xin Longa92ce1a2017-02-09 01:18:18 +0800361
362int sctp_send_reset_assoc(struct sctp_association *asoc)
363{
Xin Longcee360a2017-05-31 16:36:31 +0800364 struct sctp_stream *stream = &asoc->stream;
Xin Longa92ce1a2017-02-09 01:18:18 +0800365 struct sctp_chunk *chunk = NULL;
366 int retval;
367 __u16 i;
368
369 if (!asoc->peer.reconf_capable ||
370 !(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
371 return -ENOPROTOOPT;
372
373 if (asoc->strreset_outstanding)
374 return -EINPROGRESS;
375
Xin Long5c6144a2017-11-25 21:05:34 +0800376 if (!sctp_outq_is_empty(&asoc->outqueue))
377 return -EAGAIN;
378
Xin Longa92ce1a2017-02-09 01:18:18 +0800379 chunk = sctp_make_strreset_tsnreq(asoc);
380 if (!chunk)
381 return -ENOMEM;
382
383 /* Block further xmit of data until this request is completed */
Xin Longcee360a2017-05-31 16:36:31 +0800384 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300385 SCTP_SO(stream, i)->state = SCTP_STREAM_CLOSED;
Xin Longa92ce1a2017-02-09 01:18:18 +0800386
387 asoc->strreset_chunk = chunk;
388 sctp_chunk_hold(asoc->strreset_chunk);
389
390 retval = sctp_send_reconf(asoc, chunk);
391 if (retval) {
392 sctp_chunk_put(asoc->strreset_chunk);
393 asoc->strreset_chunk = NULL;
394
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_OPEN;
Xin Longa92ce1a2017-02-09 01:18:18 +0800397
398 return retval;
399 }
400
401 asoc->strreset_outstanding = 1;
402
403 return 0;
404}
Xin Long242bd2d2017-02-09 01:18:20 +0800405
406int sctp_send_add_streams(struct sctp_association *asoc,
407 struct sctp_add_streams *params)
408{
Xin Longcee360a2017-05-31 16:36:31 +0800409 struct sctp_stream *stream = &asoc->stream;
Xin Long242bd2d2017-02-09 01:18:20 +0800410 struct sctp_chunk *chunk = NULL;
Wei Yongjundc826732017-10-31 13:28:16 +0000411 int retval;
Xin Long242bd2d2017-02-09 01:18:20 +0800412 __u32 outcnt, incnt;
413 __u16 out, in;
414
415 if (!asoc->peer.reconf_capable ||
416 !(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
417 retval = -ENOPROTOOPT;
418 goto out;
419 }
420
421 if (asoc->strreset_outstanding) {
422 retval = -EINPROGRESS;
423 goto out;
424 }
425
426 out = params->sas_outstrms;
427 in = params->sas_instrms;
428 outcnt = stream->outcnt + out;
429 incnt = stream->incnt + in;
430 if (outcnt > SCTP_MAX_STREAM || incnt > SCTP_MAX_STREAM ||
431 (!out && !in)) {
432 retval = -EINVAL;
433 goto out;
434 }
435
436 if (out) {
Marcelo Ricardo Leitnere090abd2017-10-03 19:20:09 -0300437 retval = sctp_stream_alloc_out(stream, outcnt, GFP_KERNEL);
438 if (retval)
Xin Long242bd2d2017-02-09 01:18:20 +0800439 goto out;
Xin Long242bd2d2017-02-09 01:18:20 +0800440 }
441
Xin Long242bd2d2017-02-09 01:18:20 +0800442 chunk = sctp_make_strreset_addstrm(asoc, out, in);
Wei Yongjundc826732017-10-31 13:28:16 +0000443 if (!chunk) {
444 retval = -ENOMEM;
Xin Long242bd2d2017-02-09 01:18:20 +0800445 goto out;
Wei Yongjundc826732017-10-31 13:28:16 +0000446 }
Xin Long242bd2d2017-02-09 01:18:20 +0800447
448 asoc->strreset_chunk = chunk;
449 sctp_chunk_hold(asoc->strreset_chunk);
450
451 retval = sctp_send_reconf(asoc, chunk);
452 if (retval) {
453 sctp_chunk_put(asoc->strreset_chunk);
454 asoc->strreset_chunk = NULL;
455 goto out;
456 }
457
Xin Long242bd2d2017-02-09 01:18:20 +0800458 asoc->strreset_outstanding = !!out + !!in;
459
460out:
461 return retval;
462}
Xin Long81054472017-02-17 12:45:39 +0800463
Xin Long3c918702017-06-30 11:52:16 +0800464static struct sctp_paramhdr *sctp_chunk_lookup_strreset_param(
Xin Long1da4fc92017-10-28 19:43:54 +0800465 struct sctp_association *asoc, __be32 resp_seq,
Xin Long50a41592017-03-10 12:11:09 +0800466 __be16 type)
Xin Long81054472017-02-17 12:45:39 +0800467{
468 struct sctp_chunk *chunk = asoc->strreset_chunk;
469 struct sctp_reconf_chunk *hdr;
470 union sctp_params param;
471
Xin Long50a41592017-03-10 12:11:09 +0800472 if (!chunk)
Xin Long81054472017-02-17 12:45:39 +0800473 return NULL;
474
475 hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
476 sctp_walk_params(param, hdr, params) {
477 /* sctp_strreset_tsnreq is actually the basic structure
478 * of all stream reconf params, so it's safe to use it
479 * to access request_seq.
480 */
481 struct sctp_strreset_tsnreq *req = param.v;
482
Xin Long50a41592017-03-10 12:11:09 +0800483 if ((!resp_seq || req->request_seq == resp_seq) &&
484 (!type || type == req->param_hdr.type))
Xin Long81054472017-02-17 12:45:39 +0800485 return param.v;
486 }
487
488 return NULL;
489}
490
Xin Longe4dc99c2017-04-15 22:00:27 +0800491static void sctp_update_strreset_result(struct sctp_association *asoc,
492 __u32 result)
493{
494 asoc->strreset_result[1] = asoc->strreset_result[0];
495 asoc->strreset_result[0] = result;
496}
497
Xin Long81054472017-02-17 12:45:39 +0800498struct sctp_chunk *sctp_process_strreset_outreq(
499 struct sctp_association *asoc,
500 union sctp_params param,
501 struct sctp_ulpevent **evp)
502{
503 struct sctp_strreset_outreq *outreq = param.v;
Xin Longcee360a2017-05-31 16:36:31 +0800504 struct sctp_stream *stream = &asoc->stream;
Xin Long81054472017-02-17 12:45:39 +0800505 __u32 result = SCTP_STRRESET_DENIED;
Xin Long1da4fc92017-10-28 19:43:54 +0800506 __be16 *str_p = NULL;
Xin Long81054472017-02-17 12:45:39 +0800507 __u32 request_seq;
Xin Long2e6dc4d2019-01-22 02:39:34 +0800508 __u16 i, nums;
Xin Long81054472017-02-17 12:45:39 +0800509
510 request_seq = ntohl(outreq->request_seq);
511
512 if (ntohl(outreq->send_reset_at_tsn) >
513 sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map)) {
514 result = SCTP_STRRESET_IN_PROGRESS;
Xin Longe4dc99c2017-04-15 22:00:27 +0800515 goto err;
Xin Long81054472017-02-17 12:45:39 +0800516 }
517
Xin Longe4dc99c2017-04-15 22:00:27 +0800518 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
519 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
Xin Long81054472017-02-17 12:45:39 +0800520 result = SCTP_STRRESET_ERR_BAD_SEQNO;
Xin Longe4dc99c2017-04-15 22:00:27 +0800521 goto err;
522 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
523 i = asoc->strreset_inseq - request_seq - 1;
524 result = asoc->strreset_result[i];
525 goto err;
Xin Long81054472017-02-17 12:45:39 +0800526 }
Xin Longe4dc99c2017-04-15 22:00:27 +0800527 asoc->strreset_inseq++;
Xin Long81054472017-02-17 12:45:39 +0800528
529 /* Check strreset_enable after inseq inc, as sender cannot tell
530 * the peer doesn't enable strreset after receiving response with
531 * result denied, as well as to keep consistent with bsd.
532 */
533 if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
534 goto out;
535
Xin Long2e6dc4d2019-01-22 02:39:34 +0800536 nums = (ntohs(param.p->length) - sizeof(*outreq)) / sizeof(__u16);
537 str_p = outreq->list_of_streams;
538 for (i = 0; i < nums; i++) {
539 if (ntohs(str_p[i]) >= stream->incnt) {
540 result = SCTP_STRRESET_ERR_WRONG_SSN;
541 goto out;
542 }
543 }
544
Xin Long81054472017-02-17 12:45:39 +0800545 if (asoc->strreset_chunk) {
Xin Long50a41592017-03-10 12:11:09 +0800546 if (!sctp_chunk_lookup_strreset_param(
547 asoc, outreq->response_seq,
548 SCTP_PARAM_RESET_IN_REQUEST)) {
Xin Long81054472017-02-17 12:45:39 +0800549 /* same process with outstanding isn't 0 */
550 result = SCTP_STRRESET_ERR_IN_PROGRESS;
551 goto out;
552 }
553
554 asoc->strreset_outstanding--;
555 asoc->strreset_outseq++;
556
557 if (!asoc->strreset_outstanding) {
Xin Long50a41592017-03-10 12:11:09 +0800558 struct sctp_transport *t;
559
Xin Long81054472017-02-17 12:45:39 +0800560 t = asoc->strreset_chunk->transport;
561 if (del_timer(&t->reconf_timer))
562 sctp_transport_put(t);
563
564 sctp_chunk_put(asoc->strreset_chunk);
565 asoc->strreset_chunk = NULL;
566 }
Xin Long81054472017-02-17 12:45:39 +0800567 }
568
Xin Long2e6dc4d2019-01-22 02:39:34 +0800569 if (nums)
Xin Long81054472017-02-17 12:45:39 +0800570 for (i = 0; i < nums; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300571 SCTP_SI(stream, ntohs(str_p[i]))->mid = 0;
Xin Long2e6dc4d2019-01-22 02:39:34 +0800572 else
Xin Long81054472017-02-17 12:45:39 +0800573 for (i = 0; i < stream->incnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300574 SCTP_SI(stream, i)->mid = 0;
Xin Long81054472017-02-17 12:45:39 +0800575
576 result = SCTP_STRRESET_PERFORMED;
577
578 *evp = sctp_ulpevent_make_stream_reset_event(asoc,
Xin Long2e6dc4d2019-01-22 02:39:34 +0800579 SCTP_STREAM_RESET_INCOMING_SSN, nums, str_p, GFP_ATOMIC);
Xin Long81054472017-02-17 12:45:39 +0800580
581out:
Xin Longe4dc99c2017-04-15 22:00:27 +0800582 sctp_update_strreset_result(asoc, result);
583err:
Xin Long81054472017-02-17 12:45:39 +0800584 return sctp_make_strreset_resp(asoc, result, request_seq);
585}
Xin Long16e1a912017-02-17 12:45:40 +0800586
587struct sctp_chunk *sctp_process_strreset_inreq(
588 struct sctp_association *asoc,
589 union sctp_params param,
590 struct sctp_ulpevent **evp)
591{
592 struct sctp_strreset_inreq *inreq = param.v;
Xin Longcee360a2017-05-31 16:36:31 +0800593 struct sctp_stream *stream = &asoc->stream;
Xin Long16e1a912017-02-17 12:45:40 +0800594 __u32 result = SCTP_STRRESET_DENIED;
595 struct sctp_chunk *chunk = NULL;
Xin Long16e1a912017-02-17 12:45:40 +0800596 __u32 request_seq;
Xin Long1da4fc92017-10-28 19:43:54 +0800597 __u16 i, nums;
598 __be16 *str_p;
Xin Long16e1a912017-02-17 12:45:40 +0800599
600 request_seq = ntohl(inreq->request_seq);
Xin Longd0f025e2017-04-15 22:00:28 +0800601 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
602 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
Xin Long16e1a912017-02-17 12:45:40 +0800603 result = SCTP_STRRESET_ERR_BAD_SEQNO;
Xin Longd0f025e2017-04-15 22:00:28 +0800604 goto err;
605 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
606 i = asoc->strreset_inseq - request_seq - 1;
607 result = asoc->strreset_result[i];
608 if (result == SCTP_STRRESET_PERFORMED)
609 return NULL;
610 goto err;
Xin Long16e1a912017-02-17 12:45:40 +0800611 }
Xin Longd0f025e2017-04-15 22:00:28 +0800612 asoc->strreset_inseq++;
Xin Long16e1a912017-02-17 12:45:40 +0800613
614 if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
615 goto out;
616
617 if (asoc->strreset_outstanding) {
618 result = SCTP_STRRESET_ERR_IN_PROGRESS;
619 goto out;
620 }
621
Xin Long3aa623d2017-11-25 21:05:32 +0800622 nums = (ntohs(param.p->length) - sizeof(*inreq)) / sizeof(__u16);
Xin Long16e1a912017-02-17 12:45:40 +0800623 str_p = inreq->list_of_streams;
624 for (i = 0; i < nums; i++) {
625 if (ntohs(str_p[i]) >= stream->outcnt) {
626 result = SCTP_STRRESET_ERR_WRONG_SSN;
627 goto out;
628 }
629 }
630
Xin Longd570a592017-11-25 21:05:33 +0800631 if (!sctp_stream_outq_is_empty(stream, nums, str_p)) {
632 result = SCTP_STRRESET_IN_PROGRESS;
633 asoc->strreset_inseq--;
634 goto err;
635 }
636
Xin Long16e1a912017-02-17 12:45:40 +0800637 chunk = sctp_make_strreset_req(asoc, nums, str_p, 1, 0);
638 if (!chunk)
639 goto out;
640
641 if (nums)
642 for (i = 0; i < nums; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300643 SCTP_SO(stream, ntohs(str_p[i]))->state =
Xin Long16e1a912017-02-17 12:45:40 +0800644 SCTP_STREAM_CLOSED;
645 else
646 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300647 SCTP_SO(stream, i)->state = SCTP_STREAM_CLOSED;
Xin Long16e1a912017-02-17 12:45:40 +0800648
649 asoc->strreset_chunk = chunk;
650 asoc->strreset_outstanding = 1;
651 sctp_chunk_hold(asoc->strreset_chunk);
652
Xin Longd0f025e2017-04-15 22:00:28 +0800653 result = SCTP_STRRESET_PERFORMED;
654
Xin Long16e1a912017-02-17 12:45:40 +0800655out:
Xin Longd0f025e2017-04-15 22:00:28 +0800656 sctp_update_strreset_result(asoc, result);
657err:
Xin Long16e1a912017-02-17 12:45:40 +0800658 if (!chunk)
659 chunk = sctp_make_strreset_resp(asoc, result, request_seq);
660
661 return chunk;
662}
Xin Long692787c2017-03-10 12:11:07 +0800663
664struct sctp_chunk *sctp_process_strreset_tsnreq(
665 struct sctp_association *asoc,
666 union sctp_params param,
667 struct sctp_ulpevent **evp)
668{
669 __u32 init_tsn = 0, next_tsn = 0, max_tsn_seen;
670 struct sctp_strreset_tsnreq *tsnreq = param.v;
Xin Longcee360a2017-05-31 16:36:31 +0800671 struct sctp_stream *stream = &asoc->stream;
Xin Long692787c2017-03-10 12:11:07 +0800672 __u32 result = SCTP_STRRESET_DENIED;
673 __u32 request_seq;
674 __u16 i;
675
676 request_seq = ntohl(tsnreq->request_seq);
Xin Long6c801382017-04-15 22:00:29 +0800677 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
678 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
Xin Long692787c2017-03-10 12:11:07 +0800679 result = SCTP_STRRESET_ERR_BAD_SEQNO;
Xin Long6c801382017-04-15 22:00:29 +0800680 goto err;
681 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
682 i = asoc->strreset_inseq - request_seq - 1;
683 result = asoc->strreset_result[i];
684 if (result == SCTP_STRRESET_PERFORMED) {
Xin Long52a39582017-11-25 21:05:36 +0800685 next_tsn = asoc->ctsn_ack_point + 1;
Xin Long6c801382017-04-15 22:00:29 +0800686 init_tsn =
687 sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + 1;
688 }
689 goto err;
Xin Long692787c2017-03-10 12:11:07 +0800690 }
Xin Long5c6144a2017-11-25 21:05:34 +0800691
692 if (!sctp_outq_is_empty(&asoc->outqueue)) {
693 result = SCTP_STRRESET_IN_PROGRESS;
694 goto err;
695 }
696
Xin Long6c801382017-04-15 22:00:29 +0800697 asoc->strreset_inseq++;
Xin Long692787c2017-03-10 12:11:07 +0800698
699 if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
700 goto out;
701
702 if (asoc->strreset_outstanding) {
703 result = SCTP_STRRESET_ERR_IN_PROGRESS;
704 goto out;
705 }
706
Xin Long159f2a72017-11-25 21:05:35 +0800707 /* G4: The same processing as though a FWD-TSN chunk (as defined in
708 * [RFC3758]) with all streams affected and a new cumulative TSN
709 * ACK of the Receiver's Next TSN minus 1 were received MUST be
710 * performed.
Xin Long692787c2017-03-10 12:11:07 +0800711 */
712 max_tsn_seen = sctp_tsnmap_get_max_tsn_seen(&asoc->peer.tsn_map);
Xin Long47b20a82017-12-15 00:41:28 +0800713 asoc->stream.si->report_ftsn(&asoc->ulpq, max_tsn_seen);
Xin Long692787c2017-03-10 12:11:07 +0800714
715 /* G1: Compute an appropriate value for the Receiver's Next TSN -- the
716 * TSN that the peer should use to send the next DATA chunk. The
717 * value SHOULD be the smallest TSN not acknowledged by the
718 * receiver of the request plus 2^31.
719 */
720 init_tsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + (1 << 31);
721 sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
722 init_tsn, GFP_ATOMIC);
723
Xin Long159f2a72017-11-25 21:05:35 +0800724 /* G3: The same processing as though a SACK chunk with no gap report
725 * and a cumulative TSN ACK of the Sender's Next TSN minus 1 were
726 * received MUST be performed.
Xin Long692787c2017-03-10 12:11:07 +0800727 */
728 sctp_outq_free(&asoc->outqueue);
729
730 /* G2: Compute an appropriate value for the local endpoint's next TSN,
731 * i.e., the next TSN assigned by the receiver of the SSN/TSN reset
732 * chunk. The value SHOULD be the highest TSN sent by the receiver
733 * of the request plus 1.
734 */
735 next_tsn = asoc->next_tsn;
736 asoc->ctsn_ack_point = next_tsn - 1;
737 asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
738
739 /* G5: The next expected and outgoing SSNs MUST be reset to 0 for all
740 * incoming and outgoing streams.
741 */
Xin Long107e2422017-12-15 00:41:31 +0800742 for (i = 0; i < stream->outcnt; i++) {
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300743 SCTP_SO(stream, i)->mid = 0;
744 SCTP_SO(stream, i)->mid_uo = 0;
Xin Long107e2422017-12-15 00:41:31 +0800745 }
Xin Long692787c2017-03-10 12:11:07 +0800746 for (i = 0; i < stream->incnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300747 SCTP_SI(stream, i)->mid = 0;
Xin Long692787c2017-03-10 12:11:07 +0800748
749 result = SCTP_STRRESET_PERFORMED;
750
751 *evp = sctp_ulpevent_make_assoc_reset_event(asoc, 0, init_tsn,
752 next_tsn, GFP_ATOMIC);
753
754out:
Xin Long6c801382017-04-15 22:00:29 +0800755 sctp_update_strreset_result(asoc, result);
756err:
Xin Long692787c2017-03-10 12:11:07 +0800757 return sctp_make_strreset_tsnresp(asoc, result, request_seq,
758 next_tsn, init_tsn);
759}
Xin Long50a41592017-03-10 12:11:09 +0800760
761struct sctp_chunk *sctp_process_strreset_addstrm_out(
762 struct sctp_association *asoc,
763 union sctp_params param,
764 struct sctp_ulpevent **evp)
765{
766 struct sctp_strreset_addstrm *addstrm = param.v;
Xin Longcee360a2017-05-31 16:36:31 +0800767 struct sctp_stream *stream = &asoc->stream;
Xin Long50a41592017-03-10 12:11:09 +0800768 __u32 result = SCTP_STRRESET_DENIED;
Xin Long50a41592017-03-10 12:11:09 +0800769 __u32 request_seq, incnt;
Xin Longe4dc99c2017-04-15 22:00:27 +0800770 __u16 in, i;
Xin Long50a41592017-03-10 12:11:09 +0800771
772 request_seq = ntohl(addstrm->request_seq);
Xin Longe4dc99c2017-04-15 22:00:27 +0800773 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
774 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
Xin Long50a41592017-03-10 12:11:09 +0800775 result = SCTP_STRRESET_ERR_BAD_SEQNO;
Xin Longe4dc99c2017-04-15 22:00:27 +0800776 goto err;
777 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
778 i = asoc->strreset_inseq - request_seq - 1;
779 result = asoc->strreset_result[i];
780 goto err;
Xin Long50a41592017-03-10 12:11:09 +0800781 }
Xin Longe4dc99c2017-04-15 22:00:27 +0800782 asoc->strreset_inseq++;
Xin Long50a41592017-03-10 12:11:09 +0800783
784 if (!(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ))
785 goto out;
786
Xin Long8220c872019-01-22 02:40:12 +0800787 in = ntohs(addstrm->number_of_streams);
788 incnt = stream->incnt + in;
789 if (!in || incnt > SCTP_MAX_STREAM)
790 goto out;
791
792 if (sctp_stream_alloc_in(stream, incnt, GFP_ATOMIC))
793 goto out;
794
Xin Long50a41592017-03-10 12:11:09 +0800795 if (asoc->strreset_chunk) {
796 if (!sctp_chunk_lookup_strreset_param(
797 asoc, 0, SCTP_PARAM_RESET_ADD_IN_STREAMS)) {
798 /* same process with outstanding isn't 0 */
799 result = SCTP_STRRESET_ERR_IN_PROGRESS;
800 goto out;
801 }
802
803 asoc->strreset_outstanding--;
804 asoc->strreset_outseq++;
805
806 if (!asoc->strreset_outstanding) {
807 struct sctp_transport *t;
808
809 t = asoc->strreset_chunk->transport;
810 if (del_timer(&t->reconf_timer))
811 sctp_transport_put(t);
812
813 sctp_chunk_put(asoc->strreset_chunk);
814 asoc->strreset_chunk = NULL;
815 }
816 }
817
Xin Long50a41592017-03-10 12:11:09 +0800818 stream->incnt = incnt;
819
820 result = SCTP_STRRESET_PERFORMED;
821
822 *evp = sctp_ulpevent_make_stream_change_event(asoc,
823 0, ntohs(addstrm->number_of_streams), 0, GFP_ATOMIC);
824
825out:
Xin Longe4dc99c2017-04-15 22:00:27 +0800826 sctp_update_strreset_result(asoc, result);
827err:
Xin Long50a41592017-03-10 12:11:09 +0800828 return sctp_make_strreset_resp(asoc, result, request_seq);
829}
Xin Longc5c4ebb2017-03-10 12:11:10 +0800830
831struct sctp_chunk *sctp_process_strreset_addstrm_in(
832 struct sctp_association *asoc,
833 union sctp_params param,
834 struct sctp_ulpevent **evp)
835{
836 struct sctp_strreset_addstrm *addstrm = param.v;
Xin Longcee360a2017-05-31 16:36:31 +0800837 struct sctp_stream *stream = &asoc->stream;
Xin Longc5c4ebb2017-03-10 12:11:10 +0800838 __u32 result = SCTP_STRRESET_DENIED;
Xin Longc5c4ebb2017-03-10 12:11:10 +0800839 struct sctp_chunk *chunk = NULL;
840 __u32 request_seq, outcnt;
Xin Longd0f025e2017-04-15 22:00:28 +0800841 __u16 out, i;
Marcelo Ricardo Leitnere090abd2017-10-03 19:20:09 -0300842 int ret;
Xin Longc5c4ebb2017-03-10 12:11:10 +0800843
844 request_seq = ntohl(addstrm->request_seq);
Xin Longd0f025e2017-04-15 22:00:28 +0800845 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
846 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
Xin Longc5c4ebb2017-03-10 12:11:10 +0800847 result = SCTP_STRRESET_ERR_BAD_SEQNO;
Xin Longd0f025e2017-04-15 22:00:28 +0800848 goto err;
849 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
850 i = asoc->strreset_inseq - request_seq - 1;
851 result = asoc->strreset_result[i];
852 if (result == SCTP_STRRESET_PERFORMED)
853 return NULL;
854 goto err;
Xin Longc5c4ebb2017-03-10 12:11:10 +0800855 }
Xin Longd0f025e2017-04-15 22:00:28 +0800856 asoc->strreset_inseq++;
Xin Longc5c4ebb2017-03-10 12:11:10 +0800857
858 if (!(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ))
859 goto out;
860
861 if (asoc->strreset_outstanding) {
862 result = SCTP_STRRESET_ERR_IN_PROGRESS;
863 goto out;
864 }
865
866 out = ntohs(addstrm->number_of_streams);
867 outcnt = stream->outcnt + out;
868 if (!out || outcnt > SCTP_MAX_STREAM)
869 goto out;
870
Marcelo Ricardo Leitnere090abd2017-10-03 19:20:09 -0300871 ret = sctp_stream_alloc_out(stream, outcnt, GFP_ATOMIC);
872 if (ret)
Xin Longc5c4ebb2017-03-10 12:11:10 +0800873 goto out;
874
Xin Longc5c4ebb2017-03-10 12:11:10 +0800875 chunk = sctp_make_strreset_addstrm(asoc, out, 0);
876 if (!chunk)
877 goto out;
878
879 asoc->strreset_chunk = chunk;
880 asoc->strreset_outstanding = 1;
881 sctp_chunk_hold(asoc->strreset_chunk);
882
883 stream->outcnt = outcnt;
884
Xin Longd0f025e2017-04-15 22:00:28 +0800885 result = SCTP_STRRESET_PERFORMED;
886
Xin Longc5c4ebb2017-03-10 12:11:10 +0800887out:
Xin Longd0f025e2017-04-15 22:00:28 +0800888 sctp_update_strreset_result(asoc, result);
889err:
Xin Longc5c4ebb2017-03-10 12:11:10 +0800890 if (!chunk)
891 chunk = sctp_make_strreset_resp(asoc, result, request_seq);
892
893 return chunk;
894}
Xin Long11ae76e2017-03-10 12:11:11 +0800895
896struct sctp_chunk *sctp_process_strreset_resp(
897 struct sctp_association *asoc,
898 union sctp_params param,
899 struct sctp_ulpevent **evp)
900{
Xin Longcee360a2017-05-31 16:36:31 +0800901 struct sctp_stream *stream = &asoc->stream;
Xin Long11ae76e2017-03-10 12:11:11 +0800902 struct sctp_strreset_resp *resp = param.v;
Xin Long11ae76e2017-03-10 12:11:11 +0800903 struct sctp_transport *t;
904 __u16 i, nums, flags = 0;
Xin Long3c918702017-06-30 11:52:16 +0800905 struct sctp_paramhdr *req;
Xin Long11ae76e2017-03-10 12:11:11 +0800906 __u32 result;
907
908 req = sctp_chunk_lookup_strreset_param(asoc, resp->response_seq, 0);
909 if (!req)
910 return NULL;
911
912 result = ntohl(resp->result);
913 if (result != SCTP_STRRESET_PERFORMED) {
914 /* if in progress, do nothing but retransmit */
915 if (result == SCTP_STRRESET_IN_PROGRESS)
916 return NULL;
917 else if (result == SCTP_STRRESET_DENIED)
918 flags = SCTP_STREAM_RESET_DENIED;
919 else
920 flags = SCTP_STREAM_RESET_FAILED;
921 }
922
923 if (req->type == SCTP_PARAM_RESET_OUT_REQUEST) {
924 struct sctp_strreset_outreq *outreq;
Xin Long1da4fc92017-10-28 19:43:54 +0800925 __be16 *str_p;
Xin Long11ae76e2017-03-10 12:11:11 +0800926
927 outreq = (struct sctp_strreset_outreq *)req;
Xin Longedb12f22017-04-15 21:56:57 +0800928 str_p = outreq->list_of_streams;
Xin Long3aa623d2017-11-25 21:05:32 +0800929 nums = (ntohs(outreq->param_hdr.length) - sizeof(*outreq)) /
930 sizeof(__u16);
Xin Long11ae76e2017-03-10 12:11:11 +0800931
932 if (result == SCTP_STRRESET_PERFORMED) {
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300933 struct sctp_stream_out *sout;
Xin Long11ae76e2017-03-10 12:11:11 +0800934 if (nums) {
Xin Long107e2422017-12-15 00:41:31 +0800935 for (i = 0; i < nums; i++) {
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300936 sout = SCTP_SO(stream, ntohs(str_p[i]));
937 sout->mid = 0;
938 sout->mid_uo = 0;
Xin Long107e2422017-12-15 00:41:31 +0800939 }
Xin Long11ae76e2017-03-10 12:11:11 +0800940 } else {
Xin Long107e2422017-12-15 00:41:31 +0800941 for (i = 0; i < stream->outcnt; i++) {
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300942 sout = SCTP_SO(stream, i);
943 sout->mid = 0;
944 sout->mid_uo = 0;
Xin Long107e2422017-12-15 00:41:31 +0800945 }
Xin Long11ae76e2017-03-10 12:11:11 +0800946 }
Xin Long11ae76e2017-03-10 12:11:11 +0800947 }
948
Xin Long2e6dc4d2019-01-22 02:39:34 +0800949 flags |= SCTP_STREAM_RESET_OUTGOING_SSN;
950
Xin Long11ae76e2017-03-10 12:11:11 +0800951 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300952 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
Xin Long11ae76e2017-03-10 12:11:11 +0800953
954 *evp = sctp_ulpevent_make_stream_reset_event(asoc, flags,
955 nums, str_p, GFP_ATOMIC);
956 } else if (req->type == SCTP_PARAM_RESET_IN_REQUEST) {
957 struct sctp_strreset_inreq *inreq;
Xin Long1da4fc92017-10-28 19:43:54 +0800958 __be16 *str_p;
Xin Long11ae76e2017-03-10 12:11:11 +0800959
960 /* if the result is performed, it's impossible for inreq */
961 if (result == SCTP_STRRESET_PERFORMED)
962 return NULL;
963
964 inreq = (struct sctp_strreset_inreq *)req;
Xin Longedb12f22017-04-15 21:56:57 +0800965 str_p = inreq->list_of_streams;
Xin Long3aa623d2017-11-25 21:05:32 +0800966 nums = (ntohs(inreq->param_hdr.length) - sizeof(*inreq)) /
967 sizeof(__u16);
Xin Long11ae76e2017-03-10 12:11:11 +0800968
Xin Long2e6dc4d2019-01-22 02:39:34 +0800969 flags |= SCTP_STREAM_RESET_INCOMING_SSN;
970
Xin Long11ae76e2017-03-10 12:11:11 +0800971 *evp = sctp_ulpevent_make_stream_reset_event(asoc, flags,
972 nums, str_p, GFP_ATOMIC);
973 } else if (req->type == SCTP_PARAM_RESET_TSN_REQUEST) {
974 struct sctp_strreset_resptsn *resptsn;
975 __u32 stsn, rtsn;
976
977 /* check for resptsn, as sctp_verify_reconf didn't do it*/
978 if (ntohs(param.p->length) != sizeof(*resptsn))
979 return NULL;
980
981 resptsn = (struct sctp_strreset_resptsn *)resp;
982 stsn = ntohl(resptsn->senders_next_tsn);
983 rtsn = ntohl(resptsn->receivers_next_tsn);
984
985 if (result == SCTP_STRRESET_PERFORMED) {
986 __u32 mtsn = sctp_tsnmap_get_max_tsn_seen(
987 &asoc->peer.tsn_map);
Xin Long159f2a72017-11-25 21:05:35 +0800988 LIST_HEAD(temp);
Xin Long11ae76e2017-03-10 12:11:11 +0800989
Xin Long47b20a82017-12-15 00:41:28 +0800990 asoc->stream.si->report_ftsn(&asoc->ulpq, mtsn);
Xin Long11ae76e2017-03-10 12:11:11 +0800991
992 sctp_tsnmap_init(&asoc->peer.tsn_map,
993 SCTP_TSN_MAP_INITIAL,
994 stsn, GFP_ATOMIC);
995
Xin Long159f2a72017-11-25 21:05:35 +0800996 /* Clean up sacked and abandoned queues only. As the
997 * out_chunk_list may not be empty, splice it to temp,
998 * then get it back after sctp_outq_free is done.
999 */
1000 list_splice_init(&asoc->outqueue.out_chunk_list, &temp);
Xin Long11ae76e2017-03-10 12:11:11 +08001001 sctp_outq_free(&asoc->outqueue);
Xin Long159f2a72017-11-25 21:05:35 +08001002 list_splice_init(&temp, &asoc->outqueue.out_chunk_list);
Xin Long11ae76e2017-03-10 12:11:11 +08001003
1004 asoc->next_tsn = rtsn;
1005 asoc->ctsn_ack_point = asoc->next_tsn - 1;
1006 asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
1007
Xin Long107e2422017-12-15 00:41:31 +08001008 for (i = 0; i < stream->outcnt; i++) {
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03001009 SCTP_SO(stream, i)->mid = 0;
1010 SCTP_SO(stream, i)->mid_uo = 0;
Xin Long107e2422017-12-15 00:41:31 +08001011 }
Xin Long11ae76e2017-03-10 12:11:11 +08001012 for (i = 0; i < stream->incnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03001013 SCTP_SI(stream, i)->mid = 0;
Xin Long11ae76e2017-03-10 12:11:11 +08001014 }
1015
1016 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03001017 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
Xin Long11ae76e2017-03-10 12:11:11 +08001018
1019 *evp = sctp_ulpevent_make_assoc_reset_event(asoc, flags,
1020 stsn, rtsn, GFP_ATOMIC);
1021 } else if (req->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS) {
1022 struct sctp_strreset_addstrm *addstrm;
1023 __u16 number;
1024
1025 addstrm = (struct sctp_strreset_addstrm *)req;
1026 nums = ntohs(addstrm->number_of_streams);
1027 number = stream->outcnt - nums;
1028
1029 if (result == SCTP_STRRESET_PERFORMED)
1030 for (i = number; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03001031 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
Xin Long11ae76e2017-03-10 12:11:11 +08001032 else
1033 stream->outcnt = number;
1034
1035 *evp = sctp_ulpevent_make_stream_change_event(asoc, flags,
1036 0, nums, GFP_ATOMIC);
1037 } else if (req->type == SCTP_PARAM_RESET_ADD_IN_STREAMS) {
1038 struct sctp_strreset_addstrm *addstrm;
1039
1040 /* if the result is performed, it's impossible for addstrm in
1041 * request.
1042 */
1043 if (result == SCTP_STRRESET_PERFORMED)
1044 return NULL;
1045
1046 addstrm = (struct sctp_strreset_addstrm *)req;
1047 nums = ntohs(addstrm->number_of_streams);
1048
1049 *evp = sctp_ulpevent_make_stream_change_event(asoc, flags,
1050 nums, 0, GFP_ATOMIC);
1051 }
1052
1053 asoc->strreset_outstanding--;
1054 asoc->strreset_outseq++;
1055
1056 /* remove everything for this reconf request */
1057 if (!asoc->strreset_outstanding) {
1058 t = asoc->strreset_chunk->transport;
1059 if (del_timer(&t->reconf_timer))
1060 sctp_transport_put(t);
1061
1062 sctp_chunk_put(asoc->strreset_chunk);
1063 asoc->strreset_chunk = NULL;
1064 }
1065
1066 return NULL;
1067}