blob: b6bb68adac6e294b1d4caee5efaab8f93e6a683a [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
40/* Migrates chunks from stream queues to new stream queues if needed,
41 * but not across associations. Also, removes those chunks to streams
42 * higher than the new max.
43 */
44static void sctp_stream_outq_migrate(struct sctp_stream *stream,
45 struct sctp_stream *new, __u16 outcnt)
46{
47 struct sctp_association *asoc;
48 struct sctp_chunk *ch, *temp;
49 struct sctp_outq *outq;
50 int i;
51
52 asoc = container_of(stream, struct sctp_association, stream);
53 outq = &asoc->outqueue;
54
55 list_for_each_entry_safe(ch, temp, &outq->out_chunk_list, list) {
56 __u16 sid = sctp_chunk_stream_no(ch);
57
58 if (sid < outcnt)
59 continue;
60
61 sctp_sched_dequeue_common(outq, ch);
62 /* No need to call dequeue_done here because
63 * the chunks are not scheduled by now.
64 */
65
66 /* Mark as failed send. */
Xin Long08f46072017-11-26 20:16:06 +080067 sctp_chunk_fail(ch, (__force __u32)SCTP_ERROR_INV_STRM);
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -030068 if (asoc->peer.prsctp_capable &&
69 SCTP_PR_PRIO_ENABLED(ch->sinfo.sinfo_flags))
70 asoc->sent_cnt_removable--;
71
72 sctp_chunk_free(ch);
73 }
74
75 if (new) {
76 /* Here we actually move the old ext stuff into the new
77 * buffer, because we want to keep it. Then
78 * sctp_stream_update will swap ->out pointers.
79 */
80 for (i = 0; i < outcnt; i++) {
Konstantin Khorenko0d493b42018-08-10 20:11:43 +030081 kfree(SCTP_SO(new, i)->ext);
82 SCTP_SO(new, i)->ext = SCTP_SO(stream, i)->ext;
83 SCTP_SO(stream, i)->ext = NULL;
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -030084 }
85 }
86
Xin Longaf98c5a2019-02-12 18:51:01 +080087 for (i = outcnt; i < stream->outcnt; i++) {
Konstantin Khorenko0d493b42018-08-10 20:11:43 +030088 kfree(SCTP_SO(stream, i)->ext);
Xin Longaf98c5a2019-02-12 18:51:01 +080089 SCTP_SO(stream, i)->ext = NULL;
90 }
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -030091}
Xin Longa8386312017-01-06 22:18:33 +080092
Marcelo Ricardo Leitnere090abd2017-10-03 19:20:09 -030093static int sctp_stream_alloc_out(struct sctp_stream *stream, __u16 outcnt,
94 gfp_t gfp)
95{
Kent Overstreet2075e502019-03-11 23:31:22 -070096 int ret;
Marcelo Ricardo Leitnere090abd2017-10-03 19:20:09 -030097
Kent Overstreet2075e502019-03-11 23:31:22 -070098 if (outcnt <= stream->outcnt)
99 return 0;
Marcelo Ricardo Leitnere090abd2017-10-03 19:20:09 -0300100
Kent Overstreet2075e502019-03-11 23:31:22 -0700101 ret = genradix_prealloc(&stream->out, outcnt, gfp);
102 if (ret)
103 return ret;
Xin Longcfe4bd72019-02-04 03:27:58 +0800104
Kent Overstreet2075e502019-03-11 23:31:22 -0700105 stream->outcnt = outcnt;
Marcelo Ricardo Leitnere090abd2017-10-03 19:20:09 -0300106 return 0;
107}
108
Marcelo Ricardo Leitner1fdb8d82017-10-03 19:20:10 -0300109static int sctp_stream_alloc_in(struct sctp_stream *stream, __u16 incnt,
110 gfp_t gfp)
111{
Kent Overstreet2075e502019-03-11 23:31:22 -0700112 int ret;
Marcelo Ricardo Leitner1fdb8d82017-10-03 19:20:10 -0300113
Kent Overstreet2075e502019-03-11 23:31:22 -0700114 if (incnt <= stream->incnt)
115 return 0;
Marcelo Ricardo Leitner1fdb8d82017-10-03 19:20:10 -0300116
Kent Overstreet2075e502019-03-11 23:31:22 -0700117 ret = genradix_prealloc(&stream->in, incnt, gfp);
118 if (ret)
119 return ret;
Marcelo Ricardo Leitner1fdb8d82017-10-03 19:20:10 -0300120
Kent Overstreet2075e502019-03-11 23:31:22 -0700121 stream->incnt = incnt;
Marcelo Ricardo Leitner1fdb8d82017-10-03 19:20:10 -0300122 return 0;
123}
124
Xin Longff356412017-05-31 16:36:32 +0800125int sctp_stream_init(struct sctp_stream *stream, __u16 outcnt, __u16 incnt,
126 gfp_t gfp)
Xin Longa8386312017-01-06 22:18:33 +0800127{
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300128 struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
129 int i, ret = 0;
Xin Long3dbcc102017-03-30 01:00:53 +0800130
Marcelo Ricardo Leitner1ae2eaa2017-10-03 19:20:08 -0300131 gfp |= __GFP_NOWARN;
132
Xin Long3dbcc102017-03-30 01:00:53 +0800133 /* Initial stream->out size may be very big, so free it and alloc
Marcelo Ricardo Leitner1ae2eaa2017-10-03 19:20:08 -0300134 * a new one with new outcnt to save memory if needed.
Xin Long3dbcc102017-03-30 01:00:53 +0800135 */
Marcelo Ricardo Leitner1ae2eaa2017-10-03 19:20:08 -0300136 if (outcnt == stream->outcnt)
137 goto in;
138
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300139 /* Filter out chunks queued on streams that won't exist anymore */
140 sched->unsched_all(stream);
141 sctp_stream_outq_migrate(stream, NULL, outcnt);
142 sched->sched_all(stream);
143
Marcelo Ricardo Leitner79d08952018-01-02 19:44:37 -0200144 ret = sctp_stream_alloc_out(stream, outcnt, gfp);
145 if (ret)
146 goto out;
Xin Long3dbcc102017-03-30 01:00:53 +0800147
148 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300149 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
Xin Long3dbcc102017-03-30 01:00:53 +0800150
Marcelo Ricardo Leitner1ae2eaa2017-10-03 19:20:08 -0300151in:
Xin Long0c3f6f62017-12-08 21:04:01 +0800152 sctp_stream_interleave_init(stream);
Xin Longff356412017-05-31 16:36:32 +0800153 if (!incnt)
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300154 goto out;
Xin Longff356412017-05-31 16:36:32 +0800155
Marcelo Ricardo Leitner79d08952018-01-02 19:44:37 -0200156 ret = sctp_stream_alloc_in(stream, incnt, gfp);
157 if (ret) {
158 sched->free(stream);
Kent Overstreet2075e502019-03-11 23:31:22 -0700159 genradix_free(&stream->out);
Marcelo Ricardo Leitner79d08952018-01-02 19:44:37 -0200160 stream->outcnt = 0;
161 goto out;
Xin Longa8386312017-01-06 22:18:33 +0800162 }
163
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300164out:
165 return ret;
Xin Longa8386312017-01-06 22:18:33 +0800166}
167
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -0300168int sctp_stream_init_ext(struct sctp_stream *stream, __u16 sid)
169{
170 struct sctp_stream_out_ext *soute;
171
172 soute = kzalloc(sizeof(*soute), GFP_KERNEL);
173 if (!soute)
174 return -ENOMEM;
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300175 SCTP_SO(stream, sid)->ext = soute;
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -0300176
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300177 return sctp_sched_init_sid(stream, sid, GFP_KERNEL);
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -0300178}
179
Xin Longa8386312017-01-06 22:18:33 +0800180void sctp_stream_free(struct sctp_stream *stream)
181{
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300182 struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -0300183 int i;
184
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300185 sched->free(stream);
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -0300186 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300187 kfree(SCTP_SO(stream, i)->ext);
Kent Overstreet2075e502019-03-11 23:31:22 -0700188 genradix_free(&stream->out);
189 genradix_free(&stream->in);
Xin Longa8386312017-01-06 22:18:33 +0800190}
191
192void sctp_stream_clear(struct sctp_stream *stream)
193{
194 int i;
195
Xin Long107e2422017-12-15 00:41:31 +0800196 for (i = 0; i < stream->outcnt; i++) {
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300197 SCTP_SO(stream, i)->mid = 0;
198 SCTP_SO(stream, i)->mid_uo = 0;
Xin Long107e2422017-12-15 00:41:31 +0800199 }
Xin Longa8386312017-01-06 22:18:33 +0800200
201 for (i = 0; i < stream->incnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300202 SCTP_SI(stream, i)->mid = 0;
Xin Longa8386312017-01-06 22:18:33 +0800203}
Xin Long7f9d68a2017-01-18 00:44:47 +0800204
Xin Longcee360a2017-05-31 16:36:31 +0800205void sctp_stream_update(struct sctp_stream *stream, struct sctp_stream *new)
206{
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300207 struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
208
209 sched->unsched_all(stream);
210 sctp_stream_outq_migrate(stream, new, new->outcnt);
Xin Longcee360a2017-05-31 16:36:31 +0800211 sctp_stream_free(stream);
212
213 stream->out = new->out;
214 stream->in = new->in;
215 stream->outcnt = new->outcnt;
216 stream->incnt = new->incnt;
217
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300218 sched->sched_all(stream);
219
Kent Overstreet2075e502019-03-11 23:31:22 -0700220 new->out.tree.root = NULL;
221 new->in.tree.root = NULL;
Xin Long6a9a27d2018-04-26 15:21:44 +0800222 new->outcnt = 0;
223 new->incnt = 0;
Xin Longcee360a2017-05-31 16:36:31 +0800224}
225
Xin Long7f9d68a2017-01-18 00:44:47 +0800226static int sctp_send_reconf(struct sctp_association *asoc,
227 struct sctp_chunk *chunk)
228{
229 struct net *net = sock_net(asoc->base.sk);
230 int retval = 0;
231
232 retval = sctp_primitive_RECONF(net, asoc, chunk);
233 if (retval)
234 sctp_chunk_free(chunk);
235
236 return retval;
237}
238
Xin Longd570a592017-11-25 21:05:33 +0800239static bool sctp_stream_outq_is_empty(struct sctp_stream *stream,
240 __u16 str_nums, __be16 *str_list)
241{
242 struct sctp_association *asoc;
243 __u16 i;
244
245 asoc = container_of(stream, struct sctp_association, stream);
246 if (!asoc->outqueue.out_qlen)
247 return true;
248
249 if (!str_nums)
250 return false;
251
252 for (i = 0; i < str_nums; i++) {
253 __u16 sid = ntohs(str_list[i]);
254
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300255 if (SCTP_SO(stream, sid)->ext &&
256 !list_empty(&SCTP_SO(stream, sid)->ext->outq))
Xin Longd570a592017-11-25 21:05:33 +0800257 return false;
258 }
259
260 return true;
261}
262
Xin Long7f9d68a2017-01-18 00:44:47 +0800263int sctp_send_reset_streams(struct sctp_association *asoc,
264 struct sctp_reset_streams *params)
265{
Xin Longcee360a2017-05-31 16:36:31 +0800266 struct sctp_stream *stream = &asoc->stream;
Xin Long7f9d68a2017-01-18 00:44:47 +0800267 __u16 i, str_nums, *str_list;
268 struct sctp_chunk *chunk;
269 int retval = -EINVAL;
Xin Long1da4fc92017-10-28 19:43:54 +0800270 __be16 *nstr_list;
Xin Long7f9d68a2017-01-18 00:44:47 +0800271 bool out, in;
272
273 if (!asoc->peer.reconf_capable ||
274 !(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ)) {
275 retval = -ENOPROTOOPT;
276 goto out;
277 }
278
279 if (asoc->strreset_outstanding) {
280 retval = -EINPROGRESS;
281 goto out;
282 }
283
284 out = params->srs_flags & SCTP_STREAM_RESET_OUTGOING;
285 in = params->srs_flags & SCTP_STREAM_RESET_INCOMING;
286 if (!out && !in)
287 goto out;
288
289 str_nums = params->srs_number_streams;
290 str_list = params->srs_stream_list;
Xin Long423852f2017-11-15 17:00:11 +0800291 if (str_nums) {
292 int param_len = 0;
Xin Long7f9d68a2017-01-18 00:44:47 +0800293
Xin Long423852f2017-11-15 17:00:11 +0800294 if (out) {
295 for (i = 0; i < str_nums; i++)
296 if (str_list[i] >= stream->outcnt)
297 goto out;
298
299 param_len = str_nums * sizeof(__u16) +
300 sizeof(struct sctp_strreset_outreq);
301 }
302
303 if (in) {
304 for (i = 0; i < str_nums; i++)
305 if (str_list[i] >= stream->incnt)
306 goto out;
307
308 param_len += str_nums * sizeof(__u16) +
309 sizeof(struct sctp_strreset_inreq);
310 }
311
312 if (param_len > SCTP_MAX_CHUNK_LEN -
313 sizeof(struct sctp_reconf_chunk))
314 goto out;
315 }
Xin Long7f9d68a2017-01-18 00:44:47 +0800316
Xin Long1da4fc92017-10-28 19:43:54 +0800317 nstr_list = kcalloc(str_nums, sizeof(__be16), GFP_KERNEL);
318 if (!nstr_list) {
319 retval = -ENOMEM;
320 goto out;
321 }
Xin Long16e1a912017-02-17 12:45:40 +0800322
323 for (i = 0; i < str_nums; i++)
Xin Long1da4fc92017-10-28 19:43:54 +0800324 nstr_list[i] = htons(str_list[i]);
325
Xin Longd570a592017-11-25 21:05:33 +0800326 if (out && !sctp_stream_outq_is_empty(stream, str_nums, nstr_list)) {
327 retval = -EAGAIN;
328 goto out;
329 }
330
Xin Long1da4fc92017-10-28 19:43:54 +0800331 chunk = sctp_make_strreset_req(asoc, str_nums, nstr_list, out, in);
332
333 kfree(nstr_list);
Xin Long16e1a912017-02-17 12:45:40 +0800334
Xin Long119aecb2017-02-09 01:18:16 +0800335 if (!chunk) {
336 retval = -ENOMEM;
Xin Long7f9d68a2017-01-18 00:44:47 +0800337 goto out;
Xin Long119aecb2017-02-09 01:18:16 +0800338 }
Xin Long7f9d68a2017-01-18 00:44:47 +0800339
340 if (out) {
341 if (str_nums)
342 for (i = 0; i < str_nums; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300343 SCTP_SO(stream, str_list[i])->state =
Xin Long7f9d68a2017-01-18 00:44:47 +0800344 SCTP_STREAM_CLOSED;
345 else
346 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300347 SCTP_SO(stream, i)->state = SCTP_STREAM_CLOSED;
Xin Long7f9d68a2017-01-18 00:44:47 +0800348 }
349
Xin Long7f9d68a2017-01-18 00:44:47 +0800350 asoc->strreset_chunk = chunk;
351 sctp_chunk_hold(asoc->strreset_chunk);
352
353 retval = sctp_send_reconf(asoc, chunk);
354 if (retval) {
355 sctp_chunk_put(asoc->strreset_chunk);
356 asoc->strreset_chunk = NULL;
Xin Long119aecb2017-02-09 01:18:16 +0800357 if (!out)
358 goto out;
359
360 if (str_nums)
361 for (i = 0; i < str_nums; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300362 SCTP_SO(stream, str_list[i])->state =
Xin Long119aecb2017-02-09 01:18:16 +0800363 SCTP_STREAM_OPEN;
364 else
365 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300366 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
Xin Long119aecb2017-02-09 01:18:16 +0800367
368 goto out;
Xin Long7f9d68a2017-01-18 00:44:47 +0800369 }
370
Xin Long119aecb2017-02-09 01:18:16 +0800371 asoc->strreset_outstanding = out + in;
372
Xin Long7f9d68a2017-01-18 00:44:47 +0800373out:
374 return retval;
375}
Xin Longa92ce1a2017-02-09 01:18:18 +0800376
377int sctp_send_reset_assoc(struct sctp_association *asoc)
378{
Xin Longcee360a2017-05-31 16:36:31 +0800379 struct sctp_stream *stream = &asoc->stream;
Xin Longa92ce1a2017-02-09 01:18:18 +0800380 struct sctp_chunk *chunk = NULL;
381 int retval;
382 __u16 i;
383
384 if (!asoc->peer.reconf_capable ||
385 !(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
386 return -ENOPROTOOPT;
387
388 if (asoc->strreset_outstanding)
389 return -EINPROGRESS;
390
Xin Long5c6144a2017-11-25 21:05:34 +0800391 if (!sctp_outq_is_empty(&asoc->outqueue))
392 return -EAGAIN;
393
Xin Longa92ce1a2017-02-09 01:18:18 +0800394 chunk = sctp_make_strreset_tsnreq(asoc);
395 if (!chunk)
396 return -ENOMEM;
397
398 /* Block further xmit of data until this request is completed */
Xin Longcee360a2017-05-31 16:36:31 +0800399 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300400 SCTP_SO(stream, i)->state = SCTP_STREAM_CLOSED;
Xin Longa92ce1a2017-02-09 01:18:18 +0800401
402 asoc->strreset_chunk = chunk;
403 sctp_chunk_hold(asoc->strreset_chunk);
404
405 retval = sctp_send_reconf(asoc, chunk);
406 if (retval) {
407 sctp_chunk_put(asoc->strreset_chunk);
408 asoc->strreset_chunk = NULL;
409
Xin Longcee360a2017-05-31 16:36:31 +0800410 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300411 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
Xin Longa92ce1a2017-02-09 01:18:18 +0800412
413 return retval;
414 }
415
416 asoc->strreset_outstanding = 1;
417
418 return 0;
419}
Xin Long242bd2d2017-02-09 01:18:20 +0800420
421int sctp_send_add_streams(struct sctp_association *asoc,
422 struct sctp_add_streams *params)
423{
Xin Longcee360a2017-05-31 16:36:31 +0800424 struct sctp_stream *stream = &asoc->stream;
Xin Long242bd2d2017-02-09 01:18:20 +0800425 struct sctp_chunk *chunk = NULL;
Wei Yongjundc826732017-10-31 13:28:16 +0000426 int retval;
Xin Long242bd2d2017-02-09 01:18:20 +0800427 __u32 outcnt, incnt;
428 __u16 out, in;
429
430 if (!asoc->peer.reconf_capable ||
431 !(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
432 retval = -ENOPROTOOPT;
433 goto out;
434 }
435
436 if (asoc->strreset_outstanding) {
437 retval = -EINPROGRESS;
438 goto out;
439 }
440
441 out = params->sas_outstrms;
442 in = params->sas_instrms;
443 outcnt = stream->outcnt + out;
444 incnt = stream->incnt + in;
445 if (outcnt > SCTP_MAX_STREAM || incnt > SCTP_MAX_STREAM ||
446 (!out && !in)) {
447 retval = -EINVAL;
448 goto out;
449 }
450
451 if (out) {
Marcelo Ricardo Leitnere090abd2017-10-03 19:20:09 -0300452 retval = sctp_stream_alloc_out(stream, outcnt, GFP_KERNEL);
453 if (retval)
Xin Long242bd2d2017-02-09 01:18:20 +0800454 goto out;
Xin Long242bd2d2017-02-09 01:18:20 +0800455 }
456
Xin Long242bd2d2017-02-09 01:18:20 +0800457 chunk = sctp_make_strreset_addstrm(asoc, out, in);
Wei Yongjundc826732017-10-31 13:28:16 +0000458 if (!chunk) {
459 retval = -ENOMEM;
Xin Long242bd2d2017-02-09 01:18:20 +0800460 goto out;
Wei Yongjundc826732017-10-31 13:28:16 +0000461 }
Xin Long242bd2d2017-02-09 01:18:20 +0800462
463 asoc->strreset_chunk = chunk;
464 sctp_chunk_hold(asoc->strreset_chunk);
465
466 retval = sctp_send_reconf(asoc, chunk);
467 if (retval) {
468 sctp_chunk_put(asoc->strreset_chunk);
469 asoc->strreset_chunk = NULL;
470 goto out;
471 }
472
Xin Long242bd2d2017-02-09 01:18:20 +0800473 asoc->strreset_outstanding = !!out + !!in;
474
475out:
476 return retval;
477}
Xin Long81054472017-02-17 12:45:39 +0800478
Xin Long3c918702017-06-30 11:52:16 +0800479static struct sctp_paramhdr *sctp_chunk_lookup_strreset_param(
Xin Long1da4fc92017-10-28 19:43:54 +0800480 struct sctp_association *asoc, __be32 resp_seq,
Xin Long50a41592017-03-10 12:11:09 +0800481 __be16 type)
Xin Long81054472017-02-17 12:45:39 +0800482{
483 struct sctp_chunk *chunk = asoc->strreset_chunk;
484 struct sctp_reconf_chunk *hdr;
485 union sctp_params param;
486
Xin Long50a41592017-03-10 12:11:09 +0800487 if (!chunk)
Xin Long81054472017-02-17 12:45:39 +0800488 return NULL;
489
490 hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
491 sctp_walk_params(param, hdr, params) {
492 /* sctp_strreset_tsnreq is actually the basic structure
493 * of all stream reconf params, so it's safe to use it
494 * to access request_seq.
495 */
496 struct sctp_strreset_tsnreq *req = param.v;
497
Xin Long50a41592017-03-10 12:11:09 +0800498 if ((!resp_seq || req->request_seq == resp_seq) &&
499 (!type || type == req->param_hdr.type))
Xin Long81054472017-02-17 12:45:39 +0800500 return param.v;
501 }
502
503 return NULL;
504}
505
Xin Longe4dc99c2017-04-15 22:00:27 +0800506static void sctp_update_strreset_result(struct sctp_association *asoc,
507 __u32 result)
508{
509 asoc->strreset_result[1] = asoc->strreset_result[0];
510 asoc->strreset_result[0] = result;
511}
512
Xin Long81054472017-02-17 12:45:39 +0800513struct sctp_chunk *sctp_process_strreset_outreq(
514 struct sctp_association *asoc,
515 union sctp_params param,
516 struct sctp_ulpevent **evp)
517{
518 struct sctp_strreset_outreq *outreq = param.v;
Xin Longcee360a2017-05-31 16:36:31 +0800519 struct sctp_stream *stream = &asoc->stream;
Xin Long81054472017-02-17 12:45:39 +0800520 __u32 result = SCTP_STRRESET_DENIED;
Xin Long1da4fc92017-10-28 19:43:54 +0800521 __be16 *str_p = NULL;
Xin Long81054472017-02-17 12:45:39 +0800522 __u32 request_seq;
Xin Long2e6dc4d2019-01-22 02:39:34 +0800523 __u16 i, nums;
Xin Long81054472017-02-17 12:45:39 +0800524
525 request_seq = ntohl(outreq->request_seq);
526
527 if (ntohl(outreq->send_reset_at_tsn) >
528 sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map)) {
529 result = SCTP_STRRESET_IN_PROGRESS;
Xin Longe4dc99c2017-04-15 22:00:27 +0800530 goto err;
Xin Long81054472017-02-17 12:45:39 +0800531 }
532
Xin Longe4dc99c2017-04-15 22:00:27 +0800533 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
534 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
Xin Long81054472017-02-17 12:45:39 +0800535 result = SCTP_STRRESET_ERR_BAD_SEQNO;
Xin Longe4dc99c2017-04-15 22:00:27 +0800536 goto err;
537 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
538 i = asoc->strreset_inseq - request_seq - 1;
539 result = asoc->strreset_result[i];
540 goto err;
Xin Long81054472017-02-17 12:45:39 +0800541 }
Xin Longe4dc99c2017-04-15 22:00:27 +0800542 asoc->strreset_inseq++;
Xin Long81054472017-02-17 12:45:39 +0800543
544 /* Check strreset_enable after inseq inc, as sender cannot tell
545 * the peer doesn't enable strreset after receiving response with
546 * result denied, as well as to keep consistent with bsd.
547 */
548 if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
549 goto out;
550
Xin Long2e6dc4d2019-01-22 02:39:34 +0800551 nums = (ntohs(param.p->length) - sizeof(*outreq)) / sizeof(__u16);
552 str_p = outreq->list_of_streams;
553 for (i = 0; i < nums; i++) {
554 if (ntohs(str_p[i]) >= stream->incnt) {
555 result = SCTP_STRRESET_ERR_WRONG_SSN;
556 goto out;
557 }
558 }
559
Xin Long81054472017-02-17 12:45:39 +0800560 if (asoc->strreset_chunk) {
Xin Long50a41592017-03-10 12:11:09 +0800561 if (!sctp_chunk_lookup_strreset_param(
562 asoc, outreq->response_seq,
563 SCTP_PARAM_RESET_IN_REQUEST)) {
Xin Long81054472017-02-17 12:45:39 +0800564 /* same process with outstanding isn't 0 */
565 result = SCTP_STRRESET_ERR_IN_PROGRESS;
566 goto out;
567 }
568
569 asoc->strreset_outstanding--;
570 asoc->strreset_outseq++;
571
572 if (!asoc->strreset_outstanding) {
Xin Long50a41592017-03-10 12:11:09 +0800573 struct sctp_transport *t;
574
Xin Long81054472017-02-17 12:45:39 +0800575 t = asoc->strreset_chunk->transport;
576 if (del_timer(&t->reconf_timer))
577 sctp_transport_put(t);
578
579 sctp_chunk_put(asoc->strreset_chunk);
580 asoc->strreset_chunk = NULL;
581 }
Xin Long81054472017-02-17 12:45:39 +0800582 }
583
Xin Long2e6dc4d2019-01-22 02:39:34 +0800584 if (nums)
Xin Long81054472017-02-17 12:45:39 +0800585 for (i = 0; i < nums; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300586 SCTP_SI(stream, ntohs(str_p[i]))->mid = 0;
Xin Long2e6dc4d2019-01-22 02:39:34 +0800587 else
Xin Long81054472017-02-17 12:45:39 +0800588 for (i = 0; i < stream->incnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300589 SCTP_SI(stream, i)->mid = 0;
Xin Long81054472017-02-17 12:45:39 +0800590
591 result = SCTP_STRRESET_PERFORMED;
592
593 *evp = sctp_ulpevent_make_stream_reset_event(asoc,
Xin Long2e6dc4d2019-01-22 02:39:34 +0800594 SCTP_STREAM_RESET_INCOMING_SSN, nums, str_p, GFP_ATOMIC);
Xin Long81054472017-02-17 12:45:39 +0800595
596out:
Xin Longe4dc99c2017-04-15 22:00:27 +0800597 sctp_update_strreset_result(asoc, result);
598err:
Xin Long81054472017-02-17 12:45:39 +0800599 return sctp_make_strreset_resp(asoc, result, request_seq);
600}
Xin Long16e1a912017-02-17 12:45:40 +0800601
602struct sctp_chunk *sctp_process_strreset_inreq(
603 struct sctp_association *asoc,
604 union sctp_params param,
605 struct sctp_ulpevent **evp)
606{
607 struct sctp_strreset_inreq *inreq = param.v;
Xin Longcee360a2017-05-31 16:36:31 +0800608 struct sctp_stream *stream = &asoc->stream;
Xin Long16e1a912017-02-17 12:45:40 +0800609 __u32 result = SCTP_STRRESET_DENIED;
610 struct sctp_chunk *chunk = NULL;
Xin Long16e1a912017-02-17 12:45:40 +0800611 __u32 request_seq;
Xin Long1da4fc92017-10-28 19:43:54 +0800612 __u16 i, nums;
613 __be16 *str_p;
Xin Long16e1a912017-02-17 12:45:40 +0800614
615 request_seq = ntohl(inreq->request_seq);
Xin Longd0f025e2017-04-15 22:00:28 +0800616 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
617 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
Xin Long16e1a912017-02-17 12:45:40 +0800618 result = SCTP_STRRESET_ERR_BAD_SEQNO;
Xin Longd0f025e2017-04-15 22:00:28 +0800619 goto err;
620 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
621 i = asoc->strreset_inseq - request_seq - 1;
622 result = asoc->strreset_result[i];
623 if (result == SCTP_STRRESET_PERFORMED)
624 return NULL;
625 goto err;
Xin Long16e1a912017-02-17 12:45:40 +0800626 }
Xin Longd0f025e2017-04-15 22:00:28 +0800627 asoc->strreset_inseq++;
Xin Long16e1a912017-02-17 12:45:40 +0800628
629 if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
630 goto out;
631
632 if (asoc->strreset_outstanding) {
633 result = SCTP_STRRESET_ERR_IN_PROGRESS;
634 goto out;
635 }
636
Xin Long3aa623d2017-11-25 21:05:32 +0800637 nums = (ntohs(param.p->length) - sizeof(*inreq)) / sizeof(__u16);
Xin Long16e1a912017-02-17 12:45:40 +0800638 str_p = inreq->list_of_streams;
639 for (i = 0; i < nums; i++) {
640 if (ntohs(str_p[i]) >= stream->outcnt) {
641 result = SCTP_STRRESET_ERR_WRONG_SSN;
642 goto out;
643 }
644 }
645
Xin Longd570a592017-11-25 21:05:33 +0800646 if (!sctp_stream_outq_is_empty(stream, nums, str_p)) {
647 result = SCTP_STRRESET_IN_PROGRESS;
648 asoc->strreset_inseq--;
649 goto err;
650 }
651
Xin Long16e1a912017-02-17 12:45:40 +0800652 chunk = sctp_make_strreset_req(asoc, nums, str_p, 1, 0);
653 if (!chunk)
654 goto out;
655
656 if (nums)
657 for (i = 0; i < nums; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300658 SCTP_SO(stream, ntohs(str_p[i]))->state =
Xin Long16e1a912017-02-17 12:45:40 +0800659 SCTP_STREAM_CLOSED;
660 else
661 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300662 SCTP_SO(stream, i)->state = SCTP_STREAM_CLOSED;
Xin Long16e1a912017-02-17 12:45:40 +0800663
664 asoc->strreset_chunk = chunk;
665 asoc->strreset_outstanding = 1;
666 sctp_chunk_hold(asoc->strreset_chunk);
667
Xin Longd0f025e2017-04-15 22:00:28 +0800668 result = SCTP_STRRESET_PERFORMED;
669
Xin Long16e1a912017-02-17 12:45:40 +0800670out:
Xin Longd0f025e2017-04-15 22:00:28 +0800671 sctp_update_strreset_result(asoc, result);
672err:
Xin Long16e1a912017-02-17 12:45:40 +0800673 if (!chunk)
674 chunk = sctp_make_strreset_resp(asoc, result, request_seq);
675
676 return chunk;
677}
Xin Long692787c2017-03-10 12:11:07 +0800678
679struct sctp_chunk *sctp_process_strreset_tsnreq(
680 struct sctp_association *asoc,
681 union sctp_params param,
682 struct sctp_ulpevent **evp)
683{
684 __u32 init_tsn = 0, next_tsn = 0, max_tsn_seen;
685 struct sctp_strreset_tsnreq *tsnreq = param.v;
Xin Longcee360a2017-05-31 16:36:31 +0800686 struct sctp_stream *stream = &asoc->stream;
Xin Long692787c2017-03-10 12:11:07 +0800687 __u32 result = SCTP_STRRESET_DENIED;
688 __u32 request_seq;
689 __u16 i;
690
691 request_seq = ntohl(tsnreq->request_seq);
Xin Long6c801382017-04-15 22:00:29 +0800692 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
693 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
Xin Long692787c2017-03-10 12:11:07 +0800694 result = SCTP_STRRESET_ERR_BAD_SEQNO;
Xin Long6c801382017-04-15 22:00:29 +0800695 goto err;
696 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
697 i = asoc->strreset_inseq - request_seq - 1;
698 result = asoc->strreset_result[i];
699 if (result == SCTP_STRRESET_PERFORMED) {
Xin Long52a39582017-11-25 21:05:36 +0800700 next_tsn = asoc->ctsn_ack_point + 1;
Xin Long6c801382017-04-15 22:00:29 +0800701 init_tsn =
702 sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + 1;
703 }
704 goto err;
Xin Long692787c2017-03-10 12:11:07 +0800705 }
Xin Long5c6144a2017-11-25 21:05:34 +0800706
707 if (!sctp_outq_is_empty(&asoc->outqueue)) {
708 result = SCTP_STRRESET_IN_PROGRESS;
709 goto err;
710 }
711
Xin Long6c801382017-04-15 22:00:29 +0800712 asoc->strreset_inseq++;
Xin Long692787c2017-03-10 12:11:07 +0800713
714 if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
715 goto out;
716
717 if (asoc->strreset_outstanding) {
718 result = SCTP_STRRESET_ERR_IN_PROGRESS;
719 goto out;
720 }
721
Xin Long159f2a72017-11-25 21:05:35 +0800722 /* G4: The same processing as though a FWD-TSN chunk (as defined in
723 * [RFC3758]) with all streams affected and a new cumulative TSN
724 * ACK of the Receiver's Next TSN minus 1 were received MUST be
725 * performed.
Xin Long692787c2017-03-10 12:11:07 +0800726 */
727 max_tsn_seen = sctp_tsnmap_get_max_tsn_seen(&asoc->peer.tsn_map);
Xin Long47b20a82017-12-15 00:41:28 +0800728 asoc->stream.si->report_ftsn(&asoc->ulpq, max_tsn_seen);
Xin Long692787c2017-03-10 12:11:07 +0800729
730 /* G1: Compute an appropriate value for the Receiver's Next TSN -- the
731 * TSN that the peer should use to send the next DATA chunk. The
732 * value SHOULD be the smallest TSN not acknowledged by the
733 * receiver of the request plus 2^31.
734 */
735 init_tsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + (1 << 31);
736 sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
737 init_tsn, GFP_ATOMIC);
738
Xin Long159f2a72017-11-25 21:05:35 +0800739 /* G3: The same processing as though a SACK chunk with no gap report
740 * and a cumulative TSN ACK of the Sender's Next TSN minus 1 were
741 * received MUST be performed.
Xin Long692787c2017-03-10 12:11:07 +0800742 */
743 sctp_outq_free(&asoc->outqueue);
744
745 /* G2: Compute an appropriate value for the local endpoint's next TSN,
746 * i.e., the next TSN assigned by the receiver of the SSN/TSN reset
747 * chunk. The value SHOULD be the highest TSN sent by the receiver
748 * of the request plus 1.
749 */
750 next_tsn = asoc->next_tsn;
751 asoc->ctsn_ack_point = next_tsn - 1;
752 asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
753
754 /* G5: The next expected and outgoing SSNs MUST be reset to 0 for all
755 * incoming and outgoing streams.
756 */
Xin Long107e2422017-12-15 00:41:31 +0800757 for (i = 0; i < stream->outcnt; i++) {
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300758 SCTP_SO(stream, i)->mid = 0;
759 SCTP_SO(stream, i)->mid_uo = 0;
Xin Long107e2422017-12-15 00:41:31 +0800760 }
Xin Long692787c2017-03-10 12:11:07 +0800761 for (i = 0; i < stream->incnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300762 SCTP_SI(stream, i)->mid = 0;
Xin Long692787c2017-03-10 12:11:07 +0800763
764 result = SCTP_STRRESET_PERFORMED;
765
766 *evp = sctp_ulpevent_make_assoc_reset_event(asoc, 0, init_tsn,
767 next_tsn, GFP_ATOMIC);
768
769out:
Xin Long6c801382017-04-15 22:00:29 +0800770 sctp_update_strreset_result(asoc, result);
771err:
Xin Long692787c2017-03-10 12:11:07 +0800772 return sctp_make_strreset_tsnresp(asoc, result, request_seq,
773 next_tsn, init_tsn);
774}
Xin Long50a41592017-03-10 12:11:09 +0800775
776struct sctp_chunk *sctp_process_strreset_addstrm_out(
777 struct sctp_association *asoc,
778 union sctp_params param,
779 struct sctp_ulpevent **evp)
780{
781 struct sctp_strreset_addstrm *addstrm = param.v;
Xin Longcee360a2017-05-31 16:36:31 +0800782 struct sctp_stream *stream = &asoc->stream;
Xin Long50a41592017-03-10 12:11:09 +0800783 __u32 result = SCTP_STRRESET_DENIED;
Xin Long50a41592017-03-10 12:11:09 +0800784 __u32 request_seq, incnt;
Xin Longe4dc99c2017-04-15 22:00:27 +0800785 __u16 in, i;
Xin Long50a41592017-03-10 12:11:09 +0800786
787 request_seq = ntohl(addstrm->request_seq);
Xin Longe4dc99c2017-04-15 22:00:27 +0800788 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
789 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
Xin Long50a41592017-03-10 12:11:09 +0800790 result = SCTP_STRRESET_ERR_BAD_SEQNO;
Xin Longe4dc99c2017-04-15 22:00:27 +0800791 goto err;
792 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
793 i = asoc->strreset_inseq - request_seq - 1;
794 result = asoc->strreset_result[i];
795 goto err;
Xin Long50a41592017-03-10 12:11:09 +0800796 }
Xin Longe4dc99c2017-04-15 22:00:27 +0800797 asoc->strreset_inseq++;
Xin Long50a41592017-03-10 12:11:09 +0800798
799 if (!(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ))
800 goto out;
801
Xin Long8220c872019-01-22 02:40:12 +0800802 in = ntohs(addstrm->number_of_streams);
803 incnt = stream->incnt + in;
804 if (!in || incnt > SCTP_MAX_STREAM)
805 goto out;
806
807 if (sctp_stream_alloc_in(stream, incnt, GFP_ATOMIC))
808 goto out;
809
Xin Long50a41592017-03-10 12:11:09 +0800810 if (asoc->strreset_chunk) {
811 if (!sctp_chunk_lookup_strreset_param(
812 asoc, 0, SCTP_PARAM_RESET_ADD_IN_STREAMS)) {
813 /* same process with outstanding isn't 0 */
814 result = SCTP_STRRESET_ERR_IN_PROGRESS;
815 goto out;
816 }
817
818 asoc->strreset_outstanding--;
819 asoc->strreset_outseq++;
820
821 if (!asoc->strreset_outstanding) {
822 struct sctp_transport *t;
823
824 t = asoc->strreset_chunk->transport;
825 if (del_timer(&t->reconf_timer))
826 sctp_transport_put(t);
827
828 sctp_chunk_put(asoc->strreset_chunk);
829 asoc->strreset_chunk = NULL;
830 }
831 }
832
Xin Long50a41592017-03-10 12:11:09 +0800833 stream->incnt = incnt;
834
835 result = SCTP_STRRESET_PERFORMED;
836
837 *evp = sctp_ulpevent_make_stream_change_event(asoc,
838 0, ntohs(addstrm->number_of_streams), 0, GFP_ATOMIC);
839
840out:
Xin Longe4dc99c2017-04-15 22:00:27 +0800841 sctp_update_strreset_result(asoc, result);
842err:
Xin Long50a41592017-03-10 12:11:09 +0800843 return sctp_make_strreset_resp(asoc, result, request_seq);
844}
Xin Longc5c4ebb2017-03-10 12:11:10 +0800845
846struct sctp_chunk *sctp_process_strreset_addstrm_in(
847 struct sctp_association *asoc,
848 union sctp_params param,
849 struct sctp_ulpevent **evp)
850{
851 struct sctp_strreset_addstrm *addstrm = param.v;
Xin Longcee360a2017-05-31 16:36:31 +0800852 struct sctp_stream *stream = &asoc->stream;
Xin Longc5c4ebb2017-03-10 12:11:10 +0800853 __u32 result = SCTP_STRRESET_DENIED;
Xin Longc5c4ebb2017-03-10 12:11:10 +0800854 struct sctp_chunk *chunk = NULL;
855 __u32 request_seq, outcnt;
Xin Longd0f025e2017-04-15 22:00:28 +0800856 __u16 out, i;
Marcelo Ricardo Leitnere090abd2017-10-03 19:20:09 -0300857 int ret;
Xin Longc5c4ebb2017-03-10 12:11:10 +0800858
859 request_seq = ntohl(addstrm->request_seq);
Xin Longd0f025e2017-04-15 22:00:28 +0800860 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
861 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
Xin Longc5c4ebb2017-03-10 12:11:10 +0800862 result = SCTP_STRRESET_ERR_BAD_SEQNO;
Xin Longd0f025e2017-04-15 22:00:28 +0800863 goto err;
864 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
865 i = asoc->strreset_inseq - request_seq - 1;
866 result = asoc->strreset_result[i];
867 if (result == SCTP_STRRESET_PERFORMED)
868 return NULL;
869 goto err;
Xin Longc5c4ebb2017-03-10 12:11:10 +0800870 }
Xin Longd0f025e2017-04-15 22:00:28 +0800871 asoc->strreset_inseq++;
Xin Longc5c4ebb2017-03-10 12:11:10 +0800872
873 if (!(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ))
874 goto out;
875
876 if (asoc->strreset_outstanding) {
877 result = SCTP_STRRESET_ERR_IN_PROGRESS;
878 goto out;
879 }
880
881 out = ntohs(addstrm->number_of_streams);
882 outcnt = stream->outcnt + out;
883 if (!out || outcnt > SCTP_MAX_STREAM)
884 goto out;
885
Marcelo Ricardo Leitnere090abd2017-10-03 19:20:09 -0300886 ret = sctp_stream_alloc_out(stream, outcnt, GFP_ATOMIC);
887 if (ret)
Xin Longc5c4ebb2017-03-10 12:11:10 +0800888 goto out;
889
Xin Longc5c4ebb2017-03-10 12:11:10 +0800890 chunk = sctp_make_strreset_addstrm(asoc, out, 0);
891 if (!chunk)
892 goto out;
893
894 asoc->strreset_chunk = chunk;
895 asoc->strreset_outstanding = 1;
896 sctp_chunk_hold(asoc->strreset_chunk);
897
898 stream->outcnt = outcnt;
899
Xin Longd0f025e2017-04-15 22:00:28 +0800900 result = SCTP_STRRESET_PERFORMED;
901
Xin Longc5c4ebb2017-03-10 12:11:10 +0800902out:
Xin Longd0f025e2017-04-15 22:00:28 +0800903 sctp_update_strreset_result(asoc, result);
904err:
Xin Longc5c4ebb2017-03-10 12:11:10 +0800905 if (!chunk)
906 chunk = sctp_make_strreset_resp(asoc, result, request_seq);
907
908 return chunk;
909}
Xin Long11ae76e2017-03-10 12:11:11 +0800910
911struct sctp_chunk *sctp_process_strreset_resp(
912 struct sctp_association *asoc,
913 union sctp_params param,
914 struct sctp_ulpevent **evp)
915{
Xin Longcee360a2017-05-31 16:36:31 +0800916 struct sctp_stream *stream = &asoc->stream;
Xin Long11ae76e2017-03-10 12:11:11 +0800917 struct sctp_strreset_resp *resp = param.v;
Xin Long11ae76e2017-03-10 12:11:11 +0800918 struct sctp_transport *t;
919 __u16 i, nums, flags = 0;
Xin Long3c918702017-06-30 11:52:16 +0800920 struct sctp_paramhdr *req;
Xin Long11ae76e2017-03-10 12:11:11 +0800921 __u32 result;
922
923 req = sctp_chunk_lookup_strreset_param(asoc, resp->response_seq, 0);
924 if (!req)
925 return NULL;
926
927 result = ntohl(resp->result);
928 if (result != SCTP_STRRESET_PERFORMED) {
929 /* if in progress, do nothing but retransmit */
930 if (result == SCTP_STRRESET_IN_PROGRESS)
931 return NULL;
932 else if (result == SCTP_STRRESET_DENIED)
933 flags = SCTP_STREAM_RESET_DENIED;
934 else
935 flags = SCTP_STREAM_RESET_FAILED;
936 }
937
938 if (req->type == SCTP_PARAM_RESET_OUT_REQUEST) {
939 struct sctp_strreset_outreq *outreq;
Xin Long1da4fc92017-10-28 19:43:54 +0800940 __be16 *str_p;
Xin Long11ae76e2017-03-10 12:11:11 +0800941
942 outreq = (struct sctp_strreset_outreq *)req;
Xin Longedb12f22017-04-15 21:56:57 +0800943 str_p = outreq->list_of_streams;
Xin Long3aa623d2017-11-25 21:05:32 +0800944 nums = (ntohs(outreq->param_hdr.length) - sizeof(*outreq)) /
945 sizeof(__u16);
Xin Long11ae76e2017-03-10 12:11:11 +0800946
947 if (result == SCTP_STRRESET_PERFORMED) {
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300948 struct sctp_stream_out *sout;
Xin Long11ae76e2017-03-10 12:11:11 +0800949 if (nums) {
Xin Long107e2422017-12-15 00:41:31 +0800950 for (i = 0; i < nums; i++) {
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300951 sout = SCTP_SO(stream, ntohs(str_p[i]));
952 sout->mid = 0;
953 sout->mid_uo = 0;
Xin Long107e2422017-12-15 00:41:31 +0800954 }
Xin Long11ae76e2017-03-10 12:11:11 +0800955 } else {
Xin Long107e2422017-12-15 00:41:31 +0800956 for (i = 0; i < stream->outcnt; i++) {
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300957 sout = SCTP_SO(stream, i);
958 sout->mid = 0;
959 sout->mid_uo = 0;
Xin Long107e2422017-12-15 00:41:31 +0800960 }
Xin Long11ae76e2017-03-10 12:11:11 +0800961 }
Xin Long11ae76e2017-03-10 12:11:11 +0800962 }
963
Xin Long2e6dc4d2019-01-22 02:39:34 +0800964 flags |= SCTP_STREAM_RESET_OUTGOING_SSN;
965
Xin Long11ae76e2017-03-10 12:11:11 +0800966 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300967 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
Xin Long11ae76e2017-03-10 12:11:11 +0800968
969 *evp = sctp_ulpevent_make_stream_reset_event(asoc, flags,
970 nums, str_p, GFP_ATOMIC);
971 } else if (req->type == SCTP_PARAM_RESET_IN_REQUEST) {
972 struct sctp_strreset_inreq *inreq;
Xin Long1da4fc92017-10-28 19:43:54 +0800973 __be16 *str_p;
Xin Long11ae76e2017-03-10 12:11:11 +0800974
975 /* if the result is performed, it's impossible for inreq */
976 if (result == SCTP_STRRESET_PERFORMED)
977 return NULL;
978
979 inreq = (struct sctp_strreset_inreq *)req;
Xin Longedb12f22017-04-15 21:56:57 +0800980 str_p = inreq->list_of_streams;
Xin Long3aa623d2017-11-25 21:05:32 +0800981 nums = (ntohs(inreq->param_hdr.length) - sizeof(*inreq)) /
982 sizeof(__u16);
Xin Long11ae76e2017-03-10 12:11:11 +0800983
Xin Long2e6dc4d2019-01-22 02:39:34 +0800984 flags |= SCTP_STREAM_RESET_INCOMING_SSN;
985
Xin Long11ae76e2017-03-10 12:11:11 +0800986 *evp = sctp_ulpevent_make_stream_reset_event(asoc, flags,
987 nums, str_p, GFP_ATOMIC);
988 } else if (req->type == SCTP_PARAM_RESET_TSN_REQUEST) {
989 struct sctp_strreset_resptsn *resptsn;
990 __u32 stsn, rtsn;
991
992 /* check for resptsn, as sctp_verify_reconf didn't do it*/
993 if (ntohs(param.p->length) != sizeof(*resptsn))
994 return NULL;
995
996 resptsn = (struct sctp_strreset_resptsn *)resp;
997 stsn = ntohl(resptsn->senders_next_tsn);
998 rtsn = ntohl(resptsn->receivers_next_tsn);
999
1000 if (result == SCTP_STRRESET_PERFORMED) {
1001 __u32 mtsn = sctp_tsnmap_get_max_tsn_seen(
1002 &asoc->peer.tsn_map);
Xin Long159f2a72017-11-25 21:05:35 +08001003 LIST_HEAD(temp);
Xin Long11ae76e2017-03-10 12:11:11 +08001004
Xin Long47b20a82017-12-15 00:41:28 +08001005 asoc->stream.si->report_ftsn(&asoc->ulpq, mtsn);
Xin Long11ae76e2017-03-10 12:11:11 +08001006
1007 sctp_tsnmap_init(&asoc->peer.tsn_map,
1008 SCTP_TSN_MAP_INITIAL,
1009 stsn, GFP_ATOMIC);
1010
Xin Long159f2a72017-11-25 21:05:35 +08001011 /* Clean up sacked and abandoned queues only. As the
1012 * out_chunk_list may not be empty, splice it to temp,
1013 * then get it back after sctp_outq_free is done.
1014 */
1015 list_splice_init(&asoc->outqueue.out_chunk_list, &temp);
Xin Long11ae76e2017-03-10 12:11:11 +08001016 sctp_outq_free(&asoc->outqueue);
Xin Long159f2a72017-11-25 21:05:35 +08001017 list_splice_init(&temp, &asoc->outqueue.out_chunk_list);
Xin Long11ae76e2017-03-10 12:11:11 +08001018
1019 asoc->next_tsn = rtsn;
1020 asoc->ctsn_ack_point = asoc->next_tsn - 1;
1021 asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
1022
Xin Long107e2422017-12-15 00:41:31 +08001023 for (i = 0; i < stream->outcnt; i++) {
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03001024 SCTP_SO(stream, i)->mid = 0;
1025 SCTP_SO(stream, i)->mid_uo = 0;
Xin Long107e2422017-12-15 00:41:31 +08001026 }
Xin Long11ae76e2017-03-10 12:11:11 +08001027 for (i = 0; i < stream->incnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03001028 SCTP_SI(stream, i)->mid = 0;
Xin Long11ae76e2017-03-10 12:11:11 +08001029 }
1030
1031 for (i = 0; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03001032 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
Xin Long11ae76e2017-03-10 12:11:11 +08001033
1034 *evp = sctp_ulpevent_make_assoc_reset_event(asoc, flags,
1035 stsn, rtsn, GFP_ATOMIC);
1036 } else if (req->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS) {
1037 struct sctp_strreset_addstrm *addstrm;
1038 __u16 number;
1039
1040 addstrm = (struct sctp_strreset_addstrm *)req;
1041 nums = ntohs(addstrm->number_of_streams);
1042 number = stream->outcnt - nums;
1043
1044 if (result == SCTP_STRRESET_PERFORMED)
1045 for (i = number; i < stream->outcnt; i++)
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03001046 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
Xin Long11ae76e2017-03-10 12:11:11 +08001047 else
1048 stream->outcnt = number;
1049
1050 *evp = sctp_ulpevent_make_stream_change_event(asoc, flags,
1051 0, nums, GFP_ATOMIC);
1052 } else if (req->type == SCTP_PARAM_RESET_ADD_IN_STREAMS) {
1053 struct sctp_strreset_addstrm *addstrm;
1054
1055 /* if the result is performed, it's impossible for addstrm in
1056 * request.
1057 */
1058 if (result == SCTP_STRRESET_PERFORMED)
1059 return NULL;
1060
1061 addstrm = (struct sctp_strreset_addstrm *)req;
1062 nums = ntohs(addstrm->number_of_streams);
1063
1064 *evp = sctp_ulpevent_make_stream_change_event(asoc, flags,
1065 nums, 0, GFP_ATOMIC);
1066 }
1067
1068 asoc->strreset_outstanding--;
1069 asoc->strreset_outseq++;
1070
1071 /* remove everything for this reconf request */
1072 if (!asoc->strreset_outstanding) {
1073 t = asoc->strreset_chunk->transport;
1074 if (del_timer(&t->reconf_timer))
1075 sctp_transport_put(t);
1076
1077 sctp_chunk_put(asoc->strreset_chunk);
1078 asoc->strreset_chunk = NULL;
1079 }
1080
1081 return NULL;
1082}