blob: ffb940d3b57c1ca90dec8861b33da6d40f01824f [file] [log] [blame]
Xin Longa8386312017-01-06 22:18:33 +08001/* SCTP kernel implementation
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001 Intel Corp.
6 *
7 * This file is part of the SCTP kernel implementation
8 *
Xin Longfae8b6f2018-02-13 19:29:13 +08009 * This file contains sctp stream maniuplation primitives and helpers.
Xin Longa8386312017-01-06 22:18:33 +080010 *
11 * This SCTP implementation is free software;
12 * you can redistribute it and/or modify it under the terms of
13 * the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
17 * This SCTP implementation is distributed in the hope that it
18 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
19 * ************************
20 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21 * See the GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with GNU CC; see the file COPYING. If not, see
25 * <http://www.gnu.org/licenses/>.
26 *
27 * Please send any bug reports or fixes you make to the
28 * email address(es):
29 * lksctp developers <linux-sctp@vger.kernel.org>
30 *
31 * Written or modified by:
32 * Xin Long <lucien.xin@gmail.com>
33 */
34
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -030035#include <linux/list.h>
Xin Longa8386312017-01-06 22:18:33 +080036#include <net/sctp/sctp.h>
Xin Long7f9d68a2017-01-18 00:44:47 +080037#include <net/sctp/sm.h>
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -030038#include <net/sctp/stream_sched.h>
39
Konstantin Khorenko0d493b42018-08-10 20:11:43 +030040static struct flex_array *fa_alloc(size_t elem_size, size_t elem_count,
41 gfp_t gfp)
42{
43 struct flex_array *result;
44 int err;
45
46 result = flex_array_alloc(elem_size, elem_count, gfp);
47 if (result) {
48 err = flex_array_prealloc(result, 0, elem_count, gfp);
49 if (err) {
50 flex_array_free(result);
51 result = NULL;
52 }
53 }
54
55 return result;
56}
57
58static void fa_free(struct flex_array *fa)
59{
60 if (fa)
61 flex_array_free(fa);
62}
63
64static void fa_copy(struct flex_array *fa, struct flex_array *from,
65 size_t index, size_t count)
66{
67 void *elem;
68
69 while (count--) {
70 elem = flex_array_get(from, index);
71 flex_array_put(fa, index, elem, 0);
72 index++;
73 }
74}
75
76static void fa_zero(struct flex_array *fa, size_t index, size_t count)
77{
78 void *elem;
79
80 while (count--) {
81 elem = flex_array_get(fa, index);
82 memset(elem, 0, fa->element_size);
83 index++;
84 }
85}
86
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -030087/* Migrates chunks from stream queues to new stream queues if needed,
88 * but not across associations. Also, removes those chunks to streams
89 * higher than the new max.
90 */
91static void sctp_stream_outq_migrate(struct sctp_stream *stream,
92 struct sctp_stream *new, __u16 outcnt)
93{
94 struct sctp_association *asoc;
95 struct sctp_chunk *ch, *temp;
96 struct sctp_outq *outq;
97 int i;
98
99 asoc = container_of(stream, struct sctp_association, stream);
100 outq = &asoc->outqueue;
101
102 list_for_each_entry_safe(ch, temp, &outq->out_chunk_list, list) {
103 __u16 sid = sctp_chunk_stream_no(ch);
104
105 if (sid < outcnt)
106 continue;
107
108 sctp_sched_dequeue_common(outq, ch);
109 /* No need to call dequeue_done here because
110 * the chunks are not scheduled by now.
111 */
112
113 /* Mark as failed send. */
Xin Long08f46072017-11-26 20:16:06 +0800114 sctp_chunk_fail(ch, (__force __u32)SCTP_ERROR_INV_STRM);
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300115 if (asoc->peer.prsctp_capable &&
116 SCTP_PR_PRIO_ENABLED(ch->sinfo.sinfo_flags))
117 asoc->sent_cnt_removable--;
118
119 sctp_chunk_free(ch);
120 }
121
122 if (new) {
123 /* Here we actually move the old ext stuff into the new
124 * buffer, because we want to keep it. Then
125 * sctp_stream_update will swap ->out pointers.
126 */
127 for (i = 0; i < outcnt; i++) {
Konstantin Khorenko0d493b42018-08-10 20:11:43 +0300128 kfree(SCTP_SO(new, i)->ext);
129 SCTP_SO(new, i)->ext = SCTP_SO(stream, i)->ext;
130 SCTP_SO(stream, i)->ext = NULL;
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300131 }
132 }
133
134 for (i = outcnt; i < stream->outcnt; i++)
Konstantin Khorenko0d493b42018-08-10 20:11:43 +0300135 kfree(SCTP_SO(stream, i)->ext);
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300136}
Xin Longa8386312017-01-06 22:18:33 +0800137
Marcelo Ricardo Leitnere090abd2017-10-03 19:20:09 -0300138static int sctp_stream_alloc_out(struct sctp_stream *stream, __u16 outcnt,
139 gfp_t gfp)
140{
Konstantin Khorenko0d493b42018-08-10 20:11:43 +0300141 struct flex_array *out;
142 size_t elem_size = sizeof(struct sctp_stream_out);
Marcelo Ricardo Leitnere090abd2017-10-03 19:20:09 -0300143
Konstantin Khorenko0d493b42018-08-10 20:11:43 +0300144 out = fa_alloc(elem_size, outcnt, gfp);
Marcelo Ricardo Leitnere090abd2017-10-03 19:20:09 -0300145 if (!out)
146 return -ENOMEM;
147
148 if (stream->out) {
Konstantin Khorenko0d493b42018-08-10 20:11:43 +0300149 fa_copy(out, stream->out, 0, min(outcnt, stream->outcnt));
150 fa_free(stream->out);
Marcelo Ricardo Leitnere090abd2017-10-03 19:20:09 -0300151 }
152
153 if (outcnt > stream->outcnt)
Konstantin Khorenko0d493b42018-08-10 20:11:43 +0300154 fa_zero(out, stream->outcnt, (outcnt - stream->outcnt));
Marcelo Ricardo Leitnere090abd2017-10-03 19:20:09 -0300155
156 stream->out = out;
157
158 return 0;
159}
160
Marcelo Ricardo Leitner1fdb8d82017-10-03 19:20:10 -0300161static int sctp_stream_alloc_in(struct sctp_stream *stream, __u16 incnt,
162 gfp_t gfp)
163{
Konstantin Khorenko0d493b42018-08-10 20:11:43 +0300164 struct flex_array *in;
165 size_t elem_size = sizeof(struct sctp_stream_in);
Marcelo Ricardo Leitner1fdb8d82017-10-03 19:20:10 -0300166
Konstantin Khorenko0d493b42018-08-10 20:11:43 +0300167 in = fa_alloc(elem_size, incnt, gfp);
Marcelo Ricardo Leitner1fdb8d82017-10-03 19:20:10 -0300168 if (!in)
169 return -ENOMEM;
170
171 if (stream->in) {
Konstantin Khorenko0d493b42018-08-10 20:11:43 +0300172 fa_copy(in, stream->in, 0, min(incnt, stream->incnt));
173 fa_free(stream->in);
Marcelo Ricardo Leitner1fdb8d82017-10-03 19:20:10 -0300174 }
175
176 if (incnt > stream->incnt)
Konstantin Khorenko0d493b42018-08-10 20:11:43 +0300177 fa_zero(in, stream->incnt, (incnt - stream->incnt));
Marcelo Ricardo Leitner1fdb8d82017-10-03 19:20:10 -0300178
179 stream->in = in;
180
181 return 0;
182}
183
Xin Longff356412017-05-31 16:36:32 +0800184int sctp_stream_init(struct sctp_stream *stream, __u16 outcnt, __u16 incnt,
185 gfp_t gfp)
Xin Longa8386312017-01-06 22:18:33 +0800186{
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300187 struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
188 int i, ret = 0;
Xin Long3dbcc102017-03-30 01:00:53 +0800189
Marcelo Ricardo Leitner1ae2eaa2017-10-03 19:20:08 -0300190 gfp |= __GFP_NOWARN;
191
Xin Long3dbcc102017-03-30 01:00:53 +0800192 /* Initial stream->out size may be very big, so free it and alloc
Marcelo Ricardo Leitner1ae2eaa2017-10-03 19:20:08 -0300193 * a new one with new outcnt to save memory if needed.
Xin Long3dbcc102017-03-30 01:00:53 +0800194 */
Marcelo Ricardo Leitner1ae2eaa2017-10-03 19:20:08 -0300195 if (outcnt == stream->outcnt)
196 goto in;
197
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300198 /* Filter out chunks queued on streams that won't exist anymore */
199 sched->unsched_all(stream);
200 sctp_stream_outq_migrate(stream, NULL, outcnt);
201 sched->sched_all(stream);
202
Marcelo Ricardo Leitner79d08952018-01-02 19:44:37 -0200203 ret = sctp_stream_alloc_out(stream, outcnt, gfp);
204 if (ret)
205 goto out;
Xin Long3dbcc102017-03-30 01:00:53 +0800206
Xin Longff356412017-05-31 16:36:32 +0800207 stream->outcnt = outcnt;
Xin Long3dbcc102017-03-30 01:00:53 +0800208 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300209 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
Xin Long3dbcc102017-03-30 01:00:53 +0800210
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300211 sched->init(stream);
212
Marcelo Ricardo Leitner1ae2eaa2017-10-03 19:20:08 -0300213in:
Xin Long0c3f6f62017-12-08 21:04:01 +0800214 sctp_stream_interleave_init(stream);
Xin Longff356412017-05-31 16:36:32 +0800215 if (!incnt)
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300216 goto out;
Xin Longff356412017-05-31 16:36:32 +0800217
Marcelo Ricardo Leitner79d08952018-01-02 19:44:37 -0200218 ret = sctp_stream_alloc_in(stream, incnt, gfp);
219 if (ret) {
220 sched->free(stream);
Konstantin Khorenko0d493b42018-08-10 20:11:43 +0300221 fa_free(stream->out);
Marcelo Ricardo Leitner79d08952018-01-02 19:44:37 -0200222 stream->out = NULL;
223 stream->outcnt = 0;
224 goto out;
Xin Longa8386312017-01-06 22:18:33 +0800225 }
226
Xin Longff356412017-05-31 16:36:32 +0800227 stream->incnt = incnt;
228
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300229out:
230 return ret;
Xin Longa8386312017-01-06 22:18:33 +0800231}
232
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -0300233int sctp_stream_init_ext(struct sctp_stream *stream, __u16 sid)
234{
235 struct sctp_stream_out_ext *soute;
236
237 soute = kzalloc(sizeof(*soute), GFP_KERNEL);
238 if (!soute)
239 return -ENOMEM;
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300240 SCTP_SO(stream, sid)->ext = soute;
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -0300241
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300242 return sctp_sched_init_sid(stream, sid, GFP_KERNEL);
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -0300243}
244
Xin Longa8386312017-01-06 22:18:33 +0800245void sctp_stream_free(struct sctp_stream *stream)
246{
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300247 struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -0300248 int i;
249
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300250 sched->free(stream);
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -0300251 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300252 kfree(SCTP_SO(stream, i)->ext);
Konstantin Khorenko0d493b42018-08-10 20:11:43 +0300253 fa_free(stream->out);
254 fa_free(stream->in);
Xin Longa8386312017-01-06 22:18:33 +0800255}
256
257void sctp_stream_clear(struct sctp_stream *stream)
258{
259 int i;
260
Xin Long107e2422017-12-15 00:41:31 +0800261 for (i = 0; i < stream->outcnt; i++) {
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300262 SCTP_SO(stream, i)->mid = 0;
263 SCTP_SO(stream, i)->mid_uo = 0;
Xin Long107e2422017-12-15 00:41:31 +0800264 }
Xin Longa8386312017-01-06 22:18:33 +0800265
266 for (i = 0; i < stream->incnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300267 SCTP_SI(stream, i)->mid = 0;
Xin Longa8386312017-01-06 22:18:33 +0800268}
Xin Long7f9d68a2017-01-18 00:44:47 +0800269
Xin Longcee360a2017-05-31 16:36:31 +0800270void sctp_stream_update(struct sctp_stream *stream, struct sctp_stream *new)
271{
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300272 struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
273
274 sched->unsched_all(stream);
275 sctp_stream_outq_migrate(stream, new, new->outcnt);
Xin Longcee360a2017-05-31 16:36:31 +0800276 sctp_stream_free(stream);
277
278 stream->out = new->out;
279 stream->in = new->in;
280 stream->outcnt = new->outcnt;
281 stream->incnt = new->incnt;
282
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300283 sched->sched_all(stream);
284
Xin Longcee360a2017-05-31 16:36:31 +0800285 new->out = NULL;
286 new->in = NULL;
Xin Long6a9a27d2018-04-26 15:21:44 +0800287 new->outcnt = 0;
288 new->incnt = 0;
Xin Longcee360a2017-05-31 16:36:31 +0800289}
290
Xin Long7f9d68a2017-01-18 00:44:47 +0800291static int sctp_send_reconf(struct sctp_association *asoc,
292 struct sctp_chunk *chunk)
293{
294 struct net *net = sock_net(asoc->base.sk);
295 int retval = 0;
296
297 retval = sctp_primitive_RECONF(net, asoc, chunk);
298 if (retval)
299 sctp_chunk_free(chunk);
300
301 return retval;
302}
303
Xin Longd570a592017-11-25 21:05:33 +0800304static bool sctp_stream_outq_is_empty(struct sctp_stream *stream,
305 __u16 str_nums, __be16 *str_list)
306{
307 struct sctp_association *asoc;
308 __u16 i;
309
310 asoc = container_of(stream, struct sctp_association, stream);
311 if (!asoc->outqueue.out_qlen)
312 return true;
313
314 if (!str_nums)
315 return false;
316
317 for (i = 0; i < str_nums; i++) {
318 __u16 sid = ntohs(str_list[i]);
319
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300320 if (SCTP_SO(stream, sid)->ext &&
321 !list_empty(&SCTP_SO(stream, sid)->ext->outq))
Xin Longd570a592017-11-25 21:05:33 +0800322 return false;
323 }
324
325 return true;
326}
327
Xin Long7f9d68a2017-01-18 00:44:47 +0800328int sctp_send_reset_streams(struct sctp_association *asoc,
329 struct sctp_reset_streams *params)
330{
Xin Longcee360a2017-05-31 16:36:31 +0800331 struct sctp_stream *stream = &asoc->stream;
Xin Long7f9d68a2017-01-18 00:44:47 +0800332 __u16 i, str_nums, *str_list;
333 struct sctp_chunk *chunk;
334 int retval = -EINVAL;
Xin Long1da4fc92017-10-28 19:43:54 +0800335 __be16 *nstr_list;
Xin Long7f9d68a2017-01-18 00:44:47 +0800336 bool out, in;
337
338 if (!asoc->peer.reconf_capable ||
339 !(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ)) {
340 retval = -ENOPROTOOPT;
341 goto out;
342 }
343
344 if (asoc->strreset_outstanding) {
345 retval = -EINPROGRESS;
346 goto out;
347 }
348
349 out = params->srs_flags & SCTP_STREAM_RESET_OUTGOING;
350 in = params->srs_flags & SCTP_STREAM_RESET_INCOMING;
351 if (!out && !in)
352 goto out;
353
354 str_nums = params->srs_number_streams;
355 str_list = params->srs_stream_list;
Xin Long423852f2017-11-15 17:00:11 +0800356 if (str_nums) {
357 int param_len = 0;
Xin Long7f9d68a2017-01-18 00:44:47 +0800358
Xin Long423852f2017-11-15 17:00:11 +0800359 if (out) {
360 for (i = 0; i < str_nums; i++)
361 if (str_list[i] >= stream->outcnt)
362 goto out;
363
364 param_len = str_nums * sizeof(__u16) +
365 sizeof(struct sctp_strreset_outreq);
366 }
367
368 if (in) {
369 for (i = 0; i < str_nums; i++)
370 if (str_list[i] >= stream->incnt)
371 goto out;
372
373 param_len += str_nums * sizeof(__u16) +
374 sizeof(struct sctp_strreset_inreq);
375 }
376
377 if (param_len > SCTP_MAX_CHUNK_LEN -
378 sizeof(struct sctp_reconf_chunk))
379 goto out;
380 }
Xin Long7f9d68a2017-01-18 00:44:47 +0800381
Xin Long1da4fc92017-10-28 19:43:54 +0800382 nstr_list = kcalloc(str_nums, sizeof(__be16), GFP_KERNEL);
383 if (!nstr_list) {
384 retval = -ENOMEM;
385 goto out;
386 }
Xin Long16e1a912017-02-17 12:45:40 +0800387
388 for (i = 0; i < str_nums; i++)
Xin Long1da4fc92017-10-28 19:43:54 +0800389 nstr_list[i] = htons(str_list[i]);
390
Xin Longd570a592017-11-25 21:05:33 +0800391 if (out && !sctp_stream_outq_is_empty(stream, str_nums, nstr_list)) {
392 retval = -EAGAIN;
393 goto out;
394 }
395
Xin Long1da4fc92017-10-28 19:43:54 +0800396 chunk = sctp_make_strreset_req(asoc, str_nums, nstr_list, out, in);
397
398 kfree(nstr_list);
Xin Long16e1a912017-02-17 12:45:40 +0800399
Xin Long119aecb2017-02-09 01:18:16 +0800400 if (!chunk) {
401 retval = -ENOMEM;
Xin Long7f9d68a2017-01-18 00:44:47 +0800402 goto out;
Xin Long119aecb2017-02-09 01:18:16 +0800403 }
Xin Long7f9d68a2017-01-18 00:44:47 +0800404
405 if (out) {
406 if (str_nums)
407 for (i = 0; i < str_nums; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300408 SCTP_SO(stream, str_list[i])->state =
Xin Long7f9d68a2017-01-18 00:44:47 +0800409 SCTP_STREAM_CLOSED;
410 else
411 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300412 SCTP_SO(stream, i)->state = SCTP_STREAM_CLOSED;
Xin Long7f9d68a2017-01-18 00:44:47 +0800413 }
414
Xin Long7f9d68a2017-01-18 00:44:47 +0800415 asoc->strreset_chunk = chunk;
416 sctp_chunk_hold(asoc->strreset_chunk);
417
418 retval = sctp_send_reconf(asoc, chunk);
419 if (retval) {
420 sctp_chunk_put(asoc->strreset_chunk);
421 asoc->strreset_chunk = NULL;
Xin Long119aecb2017-02-09 01:18:16 +0800422 if (!out)
423 goto out;
424
425 if (str_nums)
426 for (i = 0; i < str_nums; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300427 SCTP_SO(stream, str_list[i])->state =
Xin Long119aecb2017-02-09 01:18:16 +0800428 SCTP_STREAM_OPEN;
429 else
430 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300431 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
Xin Long119aecb2017-02-09 01:18:16 +0800432
433 goto out;
Xin Long7f9d68a2017-01-18 00:44:47 +0800434 }
435
Xin Long119aecb2017-02-09 01:18:16 +0800436 asoc->strreset_outstanding = out + in;
437
Xin Long7f9d68a2017-01-18 00:44:47 +0800438out:
439 return retval;
440}
Xin Longa92ce1a2017-02-09 01:18:18 +0800441
442int sctp_send_reset_assoc(struct sctp_association *asoc)
443{
Xin Longcee360a2017-05-31 16:36:31 +0800444 struct sctp_stream *stream = &asoc->stream;
Xin Longa92ce1a2017-02-09 01:18:18 +0800445 struct sctp_chunk *chunk = NULL;
446 int retval;
447 __u16 i;
448
449 if (!asoc->peer.reconf_capable ||
450 !(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
451 return -ENOPROTOOPT;
452
453 if (asoc->strreset_outstanding)
454 return -EINPROGRESS;
455
Xin Long5c6144a2017-11-25 21:05:34 +0800456 if (!sctp_outq_is_empty(&asoc->outqueue))
457 return -EAGAIN;
458
Xin Longa92ce1a2017-02-09 01:18:18 +0800459 chunk = sctp_make_strreset_tsnreq(asoc);
460 if (!chunk)
461 return -ENOMEM;
462
463 /* Block further xmit of data until this request is completed */
Xin Longcee360a2017-05-31 16:36:31 +0800464 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300465 SCTP_SO(stream, i)->state = SCTP_STREAM_CLOSED;
Xin Longa92ce1a2017-02-09 01:18:18 +0800466
467 asoc->strreset_chunk = chunk;
468 sctp_chunk_hold(asoc->strreset_chunk);
469
470 retval = sctp_send_reconf(asoc, chunk);
471 if (retval) {
472 sctp_chunk_put(asoc->strreset_chunk);
473 asoc->strreset_chunk = NULL;
474
Xin Longcee360a2017-05-31 16:36:31 +0800475 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300476 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
Xin Longa92ce1a2017-02-09 01:18:18 +0800477
478 return retval;
479 }
480
481 asoc->strreset_outstanding = 1;
482
483 return 0;
484}
Xin Long242bd2d2017-02-09 01:18:20 +0800485
486int sctp_send_add_streams(struct sctp_association *asoc,
487 struct sctp_add_streams *params)
488{
Xin Longcee360a2017-05-31 16:36:31 +0800489 struct sctp_stream *stream = &asoc->stream;
Xin Long242bd2d2017-02-09 01:18:20 +0800490 struct sctp_chunk *chunk = NULL;
Wei Yongjundc826732017-10-31 13:28:16 +0000491 int retval;
Xin Long242bd2d2017-02-09 01:18:20 +0800492 __u32 outcnt, incnt;
493 __u16 out, in;
494
495 if (!asoc->peer.reconf_capable ||
496 !(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
497 retval = -ENOPROTOOPT;
498 goto out;
499 }
500
501 if (asoc->strreset_outstanding) {
502 retval = -EINPROGRESS;
503 goto out;
504 }
505
506 out = params->sas_outstrms;
507 in = params->sas_instrms;
508 outcnt = stream->outcnt + out;
509 incnt = stream->incnt + in;
510 if (outcnt > SCTP_MAX_STREAM || incnt > SCTP_MAX_STREAM ||
511 (!out && !in)) {
512 retval = -EINVAL;
513 goto out;
514 }
515
516 if (out) {
Marcelo Ricardo Leitnere090abd2017-10-03 19:20:09 -0300517 retval = sctp_stream_alloc_out(stream, outcnt, GFP_KERNEL);
518 if (retval)
Xin Long242bd2d2017-02-09 01:18:20 +0800519 goto out;
Xin Long242bd2d2017-02-09 01:18:20 +0800520 }
521
Xin Long242bd2d2017-02-09 01:18:20 +0800522 chunk = sctp_make_strreset_addstrm(asoc, out, in);
Wei Yongjundc826732017-10-31 13:28:16 +0000523 if (!chunk) {
524 retval = -ENOMEM;
Xin Long242bd2d2017-02-09 01:18:20 +0800525 goto out;
Wei Yongjundc826732017-10-31 13:28:16 +0000526 }
Xin Long242bd2d2017-02-09 01:18:20 +0800527
528 asoc->strreset_chunk = chunk;
529 sctp_chunk_hold(asoc->strreset_chunk);
530
531 retval = sctp_send_reconf(asoc, chunk);
532 if (retval) {
533 sctp_chunk_put(asoc->strreset_chunk);
534 asoc->strreset_chunk = NULL;
535 goto out;
536 }
537
538 stream->incnt = incnt;
539 stream->outcnt = outcnt;
540
541 asoc->strreset_outstanding = !!out + !!in;
542
543out:
544 return retval;
545}
Xin Long81054472017-02-17 12:45:39 +0800546
Xin Long3c918702017-06-30 11:52:16 +0800547static struct sctp_paramhdr *sctp_chunk_lookup_strreset_param(
Xin Long1da4fc92017-10-28 19:43:54 +0800548 struct sctp_association *asoc, __be32 resp_seq,
Xin Long50a41592017-03-10 12:11:09 +0800549 __be16 type)
Xin Long81054472017-02-17 12:45:39 +0800550{
551 struct sctp_chunk *chunk = asoc->strreset_chunk;
552 struct sctp_reconf_chunk *hdr;
553 union sctp_params param;
554
Xin Long50a41592017-03-10 12:11:09 +0800555 if (!chunk)
Xin Long81054472017-02-17 12:45:39 +0800556 return NULL;
557
558 hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
559 sctp_walk_params(param, hdr, params) {
560 /* sctp_strreset_tsnreq is actually the basic structure
561 * of all stream reconf params, so it's safe to use it
562 * to access request_seq.
563 */
564 struct sctp_strreset_tsnreq *req = param.v;
565
Xin Long50a41592017-03-10 12:11:09 +0800566 if ((!resp_seq || req->request_seq == resp_seq) &&
567 (!type || type == req->param_hdr.type))
Xin Long81054472017-02-17 12:45:39 +0800568 return param.v;
569 }
570
571 return NULL;
572}
573
Xin Longe4dc99c2017-04-15 22:00:27 +0800574static void sctp_update_strreset_result(struct sctp_association *asoc,
575 __u32 result)
576{
577 asoc->strreset_result[1] = asoc->strreset_result[0];
578 asoc->strreset_result[0] = result;
579}
580
Xin Long81054472017-02-17 12:45:39 +0800581struct sctp_chunk *sctp_process_strreset_outreq(
582 struct sctp_association *asoc,
583 union sctp_params param,
584 struct sctp_ulpevent **evp)
585{
586 struct sctp_strreset_outreq *outreq = param.v;
Xin Longcee360a2017-05-31 16:36:31 +0800587 struct sctp_stream *stream = &asoc->stream;
Xin Long81054472017-02-17 12:45:39 +0800588 __u32 result = SCTP_STRRESET_DENIED;
Xin Long1da4fc92017-10-28 19:43:54 +0800589 __u16 i, nums, flags = 0;
590 __be16 *str_p = NULL;
Xin Long81054472017-02-17 12:45:39 +0800591 __u32 request_seq;
592
593 request_seq = ntohl(outreq->request_seq);
594
595 if (ntohl(outreq->send_reset_at_tsn) >
596 sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map)) {
597 result = SCTP_STRRESET_IN_PROGRESS;
Xin Longe4dc99c2017-04-15 22:00:27 +0800598 goto err;
Xin Long81054472017-02-17 12:45:39 +0800599 }
600
Xin Longe4dc99c2017-04-15 22:00:27 +0800601 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
602 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
Xin Long81054472017-02-17 12:45:39 +0800603 result = SCTP_STRRESET_ERR_BAD_SEQNO;
Xin Longe4dc99c2017-04-15 22:00:27 +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 goto err;
Xin Long81054472017-02-17 12:45:39 +0800609 }
Xin Longe4dc99c2017-04-15 22:00:27 +0800610 asoc->strreset_inseq++;
Xin Long81054472017-02-17 12:45:39 +0800611
612 /* Check strreset_enable after inseq inc, as sender cannot tell
613 * the peer doesn't enable strreset after receiving response with
614 * result denied, as well as to keep consistent with bsd.
615 */
616 if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
617 goto out;
618
619 if (asoc->strreset_chunk) {
Xin Long50a41592017-03-10 12:11:09 +0800620 if (!sctp_chunk_lookup_strreset_param(
621 asoc, outreq->response_seq,
622 SCTP_PARAM_RESET_IN_REQUEST)) {
Xin Long81054472017-02-17 12:45:39 +0800623 /* same process with outstanding isn't 0 */
624 result = SCTP_STRRESET_ERR_IN_PROGRESS;
625 goto out;
626 }
627
628 asoc->strreset_outstanding--;
629 asoc->strreset_outseq++;
630
631 if (!asoc->strreset_outstanding) {
Xin Long50a41592017-03-10 12:11:09 +0800632 struct sctp_transport *t;
633
Xin Long81054472017-02-17 12:45:39 +0800634 t = asoc->strreset_chunk->transport;
635 if (del_timer(&t->reconf_timer))
636 sctp_transport_put(t);
637
638 sctp_chunk_put(asoc->strreset_chunk);
639 asoc->strreset_chunk = NULL;
640 }
641
642 flags = SCTP_STREAM_RESET_INCOMING_SSN;
643 }
644
Xin Long3aa623d2017-11-25 21:05:32 +0800645 nums = (ntohs(param.p->length) - sizeof(*outreq)) / sizeof(__u16);
Xin Long81054472017-02-17 12:45:39 +0800646 if (nums) {
647 str_p = outreq->list_of_streams;
648 for (i = 0; i < nums; i++) {
649 if (ntohs(str_p[i]) >= stream->incnt) {
650 result = SCTP_STRRESET_ERR_WRONG_SSN;
651 goto out;
652 }
653 }
654
655 for (i = 0; i < nums; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300656 SCTP_SI(stream, ntohs(str_p[i]))->mid = 0;
Xin Long81054472017-02-17 12:45:39 +0800657 } else {
658 for (i = 0; i < stream->incnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300659 SCTP_SI(stream, i)->mid = 0;
Xin Long81054472017-02-17 12:45:39 +0800660 }
661
662 result = SCTP_STRRESET_PERFORMED;
663
664 *evp = sctp_ulpevent_make_stream_reset_event(asoc,
665 flags | SCTP_STREAM_RESET_OUTGOING_SSN, nums, str_p,
666 GFP_ATOMIC);
667
668out:
Xin Longe4dc99c2017-04-15 22:00:27 +0800669 sctp_update_strreset_result(asoc, result);
670err:
Xin Long81054472017-02-17 12:45:39 +0800671 return sctp_make_strreset_resp(asoc, result, request_seq);
672}
Xin Long16e1a912017-02-17 12:45:40 +0800673
674struct sctp_chunk *sctp_process_strreset_inreq(
675 struct sctp_association *asoc,
676 union sctp_params param,
677 struct sctp_ulpevent **evp)
678{
679 struct sctp_strreset_inreq *inreq = param.v;
Xin Longcee360a2017-05-31 16:36:31 +0800680 struct sctp_stream *stream = &asoc->stream;
Xin Long16e1a912017-02-17 12:45:40 +0800681 __u32 result = SCTP_STRRESET_DENIED;
682 struct sctp_chunk *chunk = NULL;
Xin Long16e1a912017-02-17 12:45:40 +0800683 __u32 request_seq;
Xin Long1da4fc92017-10-28 19:43:54 +0800684 __u16 i, nums;
685 __be16 *str_p;
Xin Long16e1a912017-02-17 12:45:40 +0800686
687 request_seq = ntohl(inreq->request_seq);
Xin Longd0f025e2017-04-15 22:00:28 +0800688 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
689 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
Xin Long16e1a912017-02-17 12:45:40 +0800690 result = SCTP_STRRESET_ERR_BAD_SEQNO;
Xin Longd0f025e2017-04-15 22:00:28 +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)
696 return NULL;
697 goto err;
Xin Long16e1a912017-02-17 12:45:40 +0800698 }
Xin Longd0f025e2017-04-15 22:00:28 +0800699 asoc->strreset_inseq++;
Xin Long16e1a912017-02-17 12:45:40 +0800700
701 if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
702 goto out;
703
704 if (asoc->strreset_outstanding) {
705 result = SCTP_STRRESET_ERR_IN_PROGRESS;
706 goto out;
707 }
708
Xin Long3aa623d2017-11-25 21:05:32 +0800709 nums = (ntohs(param.p->length) - sizeof(*inreq)) / sizeof(__u16);
Xin Long16e1a912017-02-17 12:45:40 +0800710 str_p = inreq->list_of_streams;
711 for (i = 0; i < nums; i++) {
712 if (ntohs(str_p[i]) >= stream->outcnt) {
713 result = SCTP_STRRESET_ERR_WRONG_SSN;
714 goto out;
715 }
716 }
717
Xin Longd570a592017-11-25 21:05:33 +0800718 if (!sctp_stream_outq_is_empty(stream, nums, str_p)) {
719 result = SCTP_STRRESET_IN_PROGRESS;
720 asoc->strreset_inseq--;
721 goto err;
722 }
723
Xin Long16e1a912017-02-17 12:45:40 +0800724 chunk = sctp_make_strreset_req(asoc, nums, str_p, 1, 0);
725 if (!chunk)
726 goto out;
727
728 if (nums)
729 for (i = 0; i < nums; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300730 SCTP_SO(stream, ntohs(str_p[i]))->state =
Xin Long16e1a912017-02-17 12:45:40 +0800731 SCTP_STREAM_CLOSED;
732 else
733 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300734 SCTP_SO(stream, i)->state = SCTP_STREAM_CLOSED;
Xin Long16e1a912017-02-17 12:45:40 +0800735
736 asoc->strreset_chunk = chunk;
737 asoc->strreset_outstanding = 1;
738 sctp_chunk_hold(asoc->strreset_chunk);
739
Xin Longd0f025e2017-04-15 22:00:28 +0800740 result = SCTP_STRRESET_PERFORMED;
741
Xin Long16e1a912017-02-17 12:45:40 +0800742 *evp = sctp_ulpevent_make_stream_reset_event(asoc,
743 SCTP_STREAM_RESET_INCOMING_SSN, nums, str_p, GFP_ATOMIC);
744
745out:
Xin Longd0f025e2017-04-15 22:00:28 +0800746 sctp_update_strreset_result(asoc, result);
747err:
Xin Long16e1a912017-02-17 12:45:40 +0800748 if (!chunk)
749 chunk = sctp_make_strreset_resp(asoc, result, request_seq);
750
751 return chunk;
752}
Xin Long692787c2017-03-10 12:11:07 +0800753
754struct sctp_chunk *sctp_process_strreset_tsnreq(
755 struct sctp_association *asoc,
756 union sctp_params param,
757 struct sctp_ulpevent **evp)
758{
759 __u32 init_tsn = 0, next_tsn = 0, max_tsn_seen;
760 struct sctp_strreset_tsnreq *tsnreq = param.v;
Xin Longcee360a2017-05-31 16:36:31 +0800761 struct sctp_stream *stream = &asoc->stream;
Xin Long692787c2017-03-10 12:11:07 +0800762 __u32 result = SCTP_STRRESET_DENIED;
763 __u32 request_seq;
764 __u16 i;
765
766 request_seq = ntohl(tsnreq->request_seq);
Xin Long6c801382017-04-15 22:00:29 +0800767 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
768 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
Xin Long692787c2017-03-10 12:11:07 +0800769 result = SCTP_STRRESET_ERR_BAD_SEQNO;
Xin Long6c801382017-04-15 22:00:29 +0800770 goto err;
771 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
772 i = asoc->strreset_inseq - request_seq - 1;
773 result = asoc->strreset_result[i];
774 if (result == SCTP_STRRESET_PERFORMED) {
Xin Long52a39582017-11-25 21:05:36 +0800775 next_tsn = asoc->ctsn_ack_point + 1;
Xin Long6c801382017-04-15 22:00:29 +0800776 init_tsn =
777 sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + 1;
778 }
779 goto err;
Xin Long692787c2017-03-10 12:11:07 +0800780 }
Xin Long5c6144a2017-11-25 21:05:34 +0800781
782 if (!sctp_outq_is_empty(&asoc->outqueue)) {
783 result = SCTP_STRRESET_IN_PROGRESS;
784 goto err;
785 }
786
Xin Long6c801382017-04-15 22:00:29 +0800787 asoc->strreset_inseq++;
Xin Long692787c2017-03-10 12:11:07 +0800788
789 if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
790 goto out;
791
792 if (asoc->strreset_outstanding) {
793 result = SCTP_STRRESET_ERR_IN_PROGRESS;
794 goto out;
795 }
796
Xin Long159f2a72017-11-25 21:05:35 +0800797 /* G4: The same processing as though a FWD-TSN chunk (as defined in
798 * [RFC3758]) with all streams affected and a new cumulative TSN
799 * ACK of the Receiver's Next TSN minus 1 were received MUST be
800 * performed.
Xin Long692787c2017-03-10 12:11:07 +0800801 */
802 max_tsn_seen = sctp_tsnmap_get_max_tsn_seen(&asoc->peer.tsn_map);
Xin Long47b20a82017-12-15 00:41:28 +0800803 asoc->stream.si->report_ftsn(&asoc->ulpq, max_tsn_seen);
Xin Long692787c2017-03-10 12:11:07 +0800804
805 /* G1: Compute an appropriate value for the Receiver's Next TSN -- the
806 * TSN that the peer should use to send the next DATA chunk. The
807 * value SHOULD be the smallest TSN not acknowledged by the
808 * receiver of the request plus 2^31.
809 */
810 init_tsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + (1 << 31);
811 sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
812 init_tsn, GFP_ATOMIC);
813
Xin Long159f2a72017-11-25 21:05:35 +0800814 /* G3: The same processing as though a SACK chunk with no gap report
815 * and a cumulative TSN ACK of the Sender's Next TSN minus 1 were
816 * received MUST be performed.
Xin Long692787c2017-03-10 12:11:07 +0800817 */
818 sctp_outq_free(&asoc->outqueue);
819
820 /* G2: Compute an appropriate value for the local endpoint's next TSN,
821 * i.e., the next TSN assigned by the receiver of the SSN/TSN reset
822 * chunk. The value SHOULD be the highest TSN sent by the receiver
823 * of the request plus 1.
824 */
825 next_tsn = asoc->next_tsn;
826 asoc->ctsn_ack_point = next_tsn - 1;
827 asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
828
829 /* G5: The next expected and outgoing SSNs MUST be reset to 0 for all
830 * incoming and outgoing streams.
831 */
Xin Long107e2422017-12-15 00:41:31 +0800832 for (i = 0; i < stream->outcnt; i++) {
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300833 SCTP_SO(stream, i)->mid = 0;
834 SCTP_SO(stream, i)->mid_uo = 0;
Xin Long107e2422017-12-15 00:41:31 +0800835 }
Xin Long692787c2017-03-10 12:11:07 +0800836 for (i = 0; i < stream->incnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300837 SCTP_SI(stream, i)->mid = 0;
Xin Long692787c2017-03-10 12:11:07 +0800838
839 result = SCTP_STRRESET_PERFORMED;
840
841 *evp = sctp_ulpevent_make_assoc_reset_event(asoc, 0, init_tsn,
842 next_tsn, GFP_ATOMIC);
843
844out:
Xin Long6c801382017-04-15 22:00:29 +0800845 sctp_update_strreset_result(asoc, result);
846err:
Xin Long692787c2017-03-10 12:11:07 +0800847 return sctp_make_strreset_tsnresp(asoc, result, request_seq,
848 next_tsn, init_tsn);
849}
Xin Long50a41592017-03-10 12:11:09 +0800850
851struct sctp_chunk *sctp_process_strreset_addstrm_out(
852 struct sctp_association *asoc,
853 union sctp_params param,
854 struct sctp_ulpevent **evp)
855{
856 struct sctp_strreset_addstrm *addstrm = param.v;
Xin Longcee360a2017-05-31 16:36:31 +0800857 struct sctp_stream *stream = &asoc->stream;
Xin Long50a41592017-03-10 12:11:09 +0800858 __u32 result = SCTP_STRRESET_DENIED;
Xin Long50a41592017-03-10 12:11:09 +0800859 __u32 request_seq, incnt;
Xin Longe4dc99c2017-04-15 22:00:27 +0800860 __u16 in, i;
Xin Long50a41592017-03-10 12:11:09 +0800861
862 request_seq = ntohl(addstrm->request_seq);
Xin Longe4dc99c2017-04-15 22:00:27 +0800863 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
864 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
Xin Long50a41592017-03-10 12:11:09 +0800865 result = SCTP_STRRESET_ERR_BAD_SEQNO;
Xin Longe4dc99c2017-04-15 22:00:27 +0800866 goto err;
867 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
868 i = asoc->strreset_inseq - request_seq - 1;
869 result = asoc->strreset_result[i];
870 goto err;
Xin Long50a41592017-03-10 12:11:09 +0800871 }
Xin Longe4dc99c2017-04-15 22:00:27 +0800872 asoc->strreset_inseq++;
Xin Long50a41592017-03-10 12:11:09 +0800873
874 if (!(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ))
875 goto out;
876
877 if (asoc->strreset_chunk) {
878 if (!sctp_chunk_lookup_strreset_param(
879 asoc, 0, SCTP_PARAM_RESET_ADD_IN_STREAMS)) {
880 /* same process with outstanding isn't 0 */
881 result = SCTP_STRRESET_ERR_IN_PROGRESS;
882 goto out;
883 }
884
885 asoc->strreset_outstanding--;
886 asoc->strreset_outseq++;
887
888 if (!asoc->strreset_outstanding) {
889 struct sctp_transport *t;
890
891 t = asoc->strreset_chunk->transport;
892 if (del_timer(&t->reconf_timer))
893 sctp_transport_put(t);
894
895 sctp_chunk_put(asoc->strreset_chunk);
896 asoc->strreset_chunk = NULL;
897 }
898 }
899
900 in = ntohs(addstrm->number_of_streams);
901 incnt = stream->incnt + in;
902 if (!in || incnt > SCTP_MAX_STREAM)
903 goto out;
904
Marcelo Ricardo Leitner1fdb8d82017-10-03 19:20:10 -0300905 if (sctp_stream_alloc_in(stream, incnt, GFP_ATOMIC))
Xin Long50a41592017-03-10 12:11:09 +0800906 goto out;
907
Xin Long50a41592017-03-10 12:11:09 +0800908 stream->incnt = incnt;
909
910 result = SCTP_STRRESET_PERFORMED;
911
912 *evp = sctp_ulpevent_make_stream_change_event(asoc,
913 0, ntohs(addstrm->number_of_streams), 0, GFP_ATOMIC);
914
915out:
Xin Longe4dc99c2017-04-15 22:00:27 +0800916 sctp_update_strreset_result(asoc, result);
917err:
Xin Long50a41592017-03-10 12:11:09 +0800918 return sctp_make_strreset_resp(asoc, result, request_seq);
919}
Xin Longc5c4ebb2017-03-10 12:11:10 +0800920
921struct sctp_chunk *sctp_process_strreset_addstrm_in(
922 struct sctp_association *asoc,
923 union sctp_params param,
924 struct sctp_ulpevent **evp)
925{
926 struct sctp_strreset_addstrm *addstrm = param.v;
Xin Longcee360a2017-05-31 16:36:31 +0800927 struct sctp_stream *stream = &asoc->stream;
Xin Longc5c4ebb2017-03-10 12:11:10 +0800928 __u32 result = SCTP_STRRESET_DENIED;
Xin Longc5c4ebb2017-03-10 12:11:10 +0800929 struct sctp_chunk *chunk = NULL;
930 __u32 request_seq, outcnt;
Xin Longd0f025e2017-04-15 22:00:28 +0800931 __u16 out, i;
Marcelo Ricardo Leitnere090abd2017-10-03 19:20:09 -0300932 int ret;
Xin Longc5c4ebb2017-03-10 12:11:10 +0800933
934 request_seq = ntohl(addstrm->request_seq);
Xin Longd0f025e2017-04-15 22:00:28 +0800935 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
936 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
Xin Longc5c4ebb2017-03-10 12:11:10 +0800937 result = SCTP_STRRESET_ERR_BAD_SEQNO;
Xin Longd0f025e2017-04-15 22:00:28 +0800938 goto err;
939 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
940 i = asoc->strreset_inseq - request_seq - 1;
941 result = asoc->strreset_result[i];
942 if (result == SCTP_STRRESET_PERFORMED)
943 return NULL;
944 goto err;
Xin Longc5c4ebb2017-03-10 12:11:10 +0800945 }
Xin Longd0f025e2017-04-15 22:00:28 +0800946 asoc->strreset_inseq++;
Xin Longc5c4ebb2017-03-10 12:11:10 +0800947
948 if (!(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ))
949 goto out;
950
951 if (asoc->strreset_outstanding) {
952 result = SCTP_STRRESET_ERR_IN_PROGRESS;
953 goto out;
954 }
955
956 out = ntohs(addstrm->number_of_streams);
957 outcnt = stream->outcnt + out;
958 if (!out || outcnt > SCTP_MAX_STREAM)
959 goto out;
960
Marcelo Ricardo Leitnere090abd2017-10-03 19:20:09 -0300961 ret = sctp_stream_alloc_out(stream, outcnt, GFP_ATOMIC);
962 if (ret)
Xin Longc5c4ebb2017-03-10 12:11:10 +0800963 goto out;
964
Xin Longc5c4ebb2017-03-10 12:11:10 +0800965 chunk = sctp_make_strreset_addstrm(asoc, out, 0);
966 if (!chunk)
967 goto out;
968
969 asoc->strreset_chunk = chunk;
970 asoc->strreset_outstanding = 1;
971 sctp_chunk_hold(asoc->strreset_chunk);
972
973 stream->outcnt = outcnt;
974
Xin Longd0f025e2017-04-15 22:00:28 +0800975 result = SCTP_STRRESET_PERFORMED;
976
Xin Longc5c4ebb2017-03-10 12:11:10 +0800977 *evp = sctp_ulpevent_make_stream_change_event(asoc,
978 0, 0, ntohs(addstrm->number_of_streams), GFP_ATOMIC);
979
980out:
Xin Longd0f025e2017-04-15 22:00:28 +0800981 sctp_update_strreset_result(asoc, result);
982err:
Xin Longc5c4ebb2017-03-10 12:11:10 +0800983 if (!chunk)
984 chunk = sctp_make_strreset_resp(asoc, result, request_seq);
985
986 return chunk;
987}
Xin Long11ae76e2017-03-10 12:11:11 +0800988
989struct sctp_chunk *sctp_process_strreset_resp(
990 struct sctp_association *asoc,
991 union sctp_params param,
992 struct sctp_ulpevent **evp)
993{
Xin Longcee360a2017-05-31 16:36:31 +0800994 struct sctp_stream *stream = &asoc->stream;
Xin Long11ae76e2017-03-10 12:11:11 +0800995 struct sctp_strreset_resp *resp = param.v;
Xin Long11ae76e2017-03-10 12:11:11 +0800996 struct sctp_transport *t;
997 __u16 i, nums, flags = 0;
Xin Long3c918702017-06-30 11:52:16 +0800998 struct sctp_paramhdr *req;
Xin Long11ae76e2017-03-10 12:11:11 +0800999 __u32 result;
1000
1001 req = sctp_chunk_lookup_strreset_param(asoc, resp->response_seq, 0);
1002 if (!req)
1003 return NULL;
1004
1005 result = ntohl(resp->result);
1006 if (result != SCTP_STRRESET_PERFORMED) {
1007 /* if in progress, do nothing but retransmit */
1008 if (result == SCTP_STRRESET_IN_PROGRESS)
1009 return NULL;
1010 else if (result == SCTP_STRRESET_DENIED)
1011 flags = SCTP_STREAM_RESET_DENIED;
1012 else
1013 flags = SCTP_STREAM_RESET_FAILED;
1014 }
1015
1016 if (req->type == SCTP_PARAM_RESET_OUT_REQUEST) {
1017 struct sctp_strreset_outreq *outreq;
Xin Long1da4fc92017-10-28 19:43:54 +08001018 __be16 *str_p;
Xin Long11ae76e2017-03-10 12:11:11 +08001019
1020 outreq = (struct sctp_strreset_outreq *)req;
Xin Longedb12f22017-04-15 21:56:57 +08001021 str_p = outreq->list_of_streams;
Xin Long3aa623d2017-11-25 21:05:32 +08001022 nums = (ntohs(outreq->param_hdr.length) - sizeof(*outreq)) /
1023 sizeof(__u16);
Xin Long11ae76e2017-03-10 12:11:11 +08001024
1025 if (result == SCTP_STRRESET_PERFORMED) {
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03001026 struct sctp_stream_out *sout;
Xin Long11ae76e2017-03-10 12:11:11 +08001027 if (nums) {
Xin Long107e2422017-12-15 00:41:31 +08001028 for (i = 0; i < nums; i++) {
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03001029 sout = SCTP_SO(stream, ntohs(str_p[i]));
1030 sout->mid = 0;
1031 sout->mid_uo = 0;
Xin Long107e2422017-12-15 00:41:31 +08001032 }
Xin Long11ae76e2017-03-10 12:11:11 +08001033 } else {
Xin Long107e2422017-12-15 00:41:31 +08001034 for (i = 0; i < stream->outcnt; i++) {
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03001035 sout = SCTP_SO(stream, i);
1036 sout->mid = 0;
1037 sout->mid_uo = 0;
Xin Long107e2422017-12-15 00:41:31 +08001038 }
Xin Long11ae76e2017-03-10 12:11:11 +08001039 }
1040
1041 flags = SCTP_STREAM_RESET_OUTGOING_SSN;
1042 }
1043
1044 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03001045 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
Xin Long11ae76e2017-03-10 12:11:11 +08001046
1047 *evp = sctp_ulpevent_make_stream_reset_event(asoc, flags,
1048 nums, str_p, GFP_ATOMIC);
1049 } else if (req->type == SCTP_PARAM_RESET_IN_REQUEST) {
1050 struct sctp_strreset_inreq *inreq;
Xin Long1da4fc92017-10-28 19:43:54 +08001051 __be16 *str_p;
Xin Long11ae76e2017-03-10 12:11:11 +08001052
1053 /* if the result is performed, it's impossible for inreq */
1054 if (result == SCTP_STRRESET_PERFORMED)
1055 return NULL;
1056
1057 inreq = (struct sctp_strreset_inreq *)req;
Xin Longedb12f22017-04-15 21:56:57 +08001058 str_p = inreq->list_of_streams;
Xin Long3aa623d2017-11-25 21:05:32 +08001059 nums = (ntohs(inreq->param_hdr.length) - sizeof(*inreq)) /
1060 sizeof(__u16);
Xin Long11ae76e2017-03-10 12:11:11 +08001061
Xin Long11ae76e2017-03-10 12:11:11 +08001062 *evp = sctp_ulpevent_make_stream_reset_event(asoc, flags,
1063 nums, str_p, GFP_ATOMIC);
1064 } else if (req->type == SCTP_PARAM_RESET_TSN_REQUEST) {
1065 struct sctp_strreset_resptsn *resptsn;
1066 __u32 stsn, rtsn;
1067
1068 /* check for resptsn, as sctp_verify_reconf didn't do it*/
1069 if (ntohs(param.p->length) != sizeof(*resptsn))
1070 return NULL;
1071
1072 resptsn = (struct sctp_strreset_resptsn *)resp;
1073 stsn = ntohl(resptsn->senders_next_tsn);
1074 rtsn = ntohl(resptsn->receivers_next_tsn);
1075
1076 if (result == SCTP_STRRESET_PERFORMED) {
1077 __u32 mtsn = sctp_tsnmap_get_max_tsn_seen(
1078 &asoc->peer.tsn_map);
Xin Long159f2a72017-11-25 21:05:35 +08001079 LIST_HEAD(temp);
Xin Long11ae76e2017-03-10 12:11:11 +08001080
Xin Long47b20a82017-12-15 00:41:28 +08001081 asoc->stream.si->report_ftsn(&asoc->ulpq, mtsn);
Xin Long11ae76e2017-03-10 12:11:11 +08001082
1083 sctp_tsnmap_init(&asoc->peer.tsn_map,
1084 SCTP_TSN_MAP_INITIAL,
1085 stsn, GFP_ATOMIC);
1086
Xin Long159f2a72017-11-25 21:05:35 +08001087 /* Clean up sacked and abandoned queues only. As the
1088 * out_chunk_list may not be empty, splice it to temp,
1089 * then get it back after sctp_outq_free is done.
1090 */
1091 list_splice_init(&asoc->outqueue.out_chunk_list, &temp);
Xin Long11ae76e2017-03-10 12:11:11 +08001092 sctp_outq_free(&asoc->outqueue);
Xin Long159f2a72017-11-25 21:05:35 +08001093 list_splice_init(&temp, &asoc->outqueue.out_chunk_list);
Xin Long11ae76e2017-03-10 12:11:11 +08001094
1095 asoc->next_tsn = rtsn;
1096 asoc->ctsn_ack_point = asoc->next_tsn - 1;
1097 asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
1098
Xin Long107e2422017-12-15 00:41:31 +08001099 for (i = 0; i < stream->outcnt; i++) {
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03001100 SCTP_SO(stream, i)->mid = 0;
1101 SCTP_SO(stream, i)->mid_uo = 0;
Xin Long107e2422017-12-15 00:41:31 +08001102 }
Xin Long11ae76e2017-03-10 12:11:11 +08001103 for (i = 0; i < stream->incnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03001104 SCTP_SI(stream, i)->mid = 0;
Xin Long11ae76e2017-03-10 12:11:11 +08001105 }
1106
1107 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03001108 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
Xin Long11ae76e2017-03-10 12:11:11 +08001109
1110 *evp = sctp_ulpevent_make_assoc_reset_event(asoc, flags,
1111 stsn, rtsn, GFP_ATOMIC);
1112 } else if (req->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS) {
1113 struct sctp_strreset_addstrm *addstrm;
1114 __u16 number;
1115
1116 addstrm = (struct sctp_strreset_addstrm *)req;
1117 nums = ntohs(addstrm->number_of_streams);
1118 number = stream->outcnt - nums;
1119
1120 if (result == SCTP_STRRESET_PERFORMED)
1121 for (i = number; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03001122 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
Xin Long11ae76e2017-03-10 12:11:11 +08001123 else
1124 stream->outcnt = number;
1125
1126 *evp = sctp_ulpevent_make_stream_change_event(asoc, flags,
1127 0, nums, GFP_ATOMIC);
1128 } else if (req->type == SCTP_PARAM_RESET_ADD_IN_STREAMS) {
1129 struct sctp_strreset_addstrm *addstrm;
1130
1131 /* if the result is performed, it's impossible for addstrm in
1132 * request.
1133 */
1134 if (result == SCTP_STRRESET_PERFORMED)
1135 return NULL;
1136
1137 addstrm = (struct sctp_strreset_addstrm *)req;
1138 nums = ntohs(addstrm->number_of_streams);
1139
1140 *evp = sctp_ulpevent_make_stream_change_event(asoc, flags,
1141 nums, 0, GFP_ATOMIC);
1142 }
1143
1144 asoc->strreset_outstanding--;
1145 asoc->strreset_outseq++;
1146
1147 /* remove everything for this reconf request */
1148 if (!asoc->strreset_outstanding) {
1149 t = asoc->strreset_chunk->transport;
1150 if (del_timer(&t->reconf_timer))
1151 sctp_transport_put(t);
1152
1153 sctp_chunk_put(asoc->strreset_chunk);
1154 asoc->strreset_chunk = NULL;
1155 }
1156
1157 return NULL;
1158}