]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/sctp/stream.c
netrom: fix a memory leak in nr_rx_frame()
[mirror_ubuntu-jammy-kernel.git] / net / sctp / stream.c
CommitLineData
47505b8b 1// SPDX-License-Identifier: GPL-2.0-or-later
a8386317
XL
2/* 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 *
fae8b6f4 10 * This file contains sctp stream maniuplation primitives and helpers.
a8386317 11 *
a8386317
XL
12 * 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
5bbbbe32 20#include <linux/list.h>
a8386317 21#include <net/sctp/sctp.h>
7f9d68ac 22#include <net/sctp/sm.h>
5bbbbe32
MRL
23#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. */
08f46070 52 sctp_chunk_fail(ch, (__force __u32)SCTP_ERROR_INV_STRM);
5bbbbe32
MRL
53 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++) {
0d493b4d
KK
66 kfree(SCTP_SO(new, i)->ext);
67 SCTP_SO(new, i)->ext = SCTP_SO(stream, i)->ext;
68 SCTP_SO(stream, i)->ext = NULL;
5bbbbe32
MRL
69 }
70 }
71
af98c5a7 72 for (i = outcnt; i < stream->outcnt; i++) {
0d493b4d 73 kfree(SCTP_SO(stream, i)->ext);
af98c5a7
XL
74 SCTP_SO(stream, i)->ext = NULL;
75 }
5bbbbe32 76}
a8386317 77
e090abd0
MRL
78static int sctp_stream_alloc_out(struct sctp_stream *stream, __u16 outcnt,
79 gfp_t gfp)
80{
2075e50c 81 int ret;
e090abd0 82
2075e50c
KO
83 if (outcnt <= stream->outcnt)
84 return 0;
cfe4bd7a 85
2075e50c
KO
86 ret = genradix_prealloc(&stream->out, outcnt, gfp);
87 if (ret)
88 return ret;
e090abd0 89
2075e50c 90 stream->outcnt = outcnt;
e090abd0
MRL
91 return 0;
92}
93
1fdb8d8f
MRL
94static int sctp_stream_alloc_in(struct sctp_stream *stream, __u16 incnt,
95 gfp_t gfp)
96{
2075e50c 97 int ret;
1fdb8d8f 98
2075e50c
KO
99 if (incnt <= stream->incnt)
100 return 0;
1fdb8d8f 101
2075e50c
KO
102 ret = genradix_prealloc(&stream->in, incnt, gfp);
103 if (ret)
104 return ret;
1fdb8d8f 105
2075e50c 106 stream->incnt = incnt;
1fdb8d8f
MRL
107 return 0;
108}
109
ff356414
XL
110int sctp_stream_init(struct sctp_stream *stream, __u16 outcnt, __u16 incnt,
111 gfp_t gfp)
a8386317 112{
5bbbbe32
MRL
113 struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
114 int i, ret = 0;
3dbcc105 115
1ae2eaaa
MRL
116 gfp |= __GFP_NOWARN;
117
3dbcc105 118 /* Initial stream->out size may be very big, so free it and alloc
1ae2eaaa 119 * a new one with new outcnt to save memory if needed.
3dbcc105 120 */
1ae2eaaa
MRL
121 if (outcnt == stream->outcnt)
122 goto in;
123
5bbbbe32
MRL
124 /* 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
79d08951
MRL
129 ret = sctp_stream_alloc_out(stream, outcnt, gfp);
130 if (ret)
131 goto out;
3dbcc105
XL
132
133 for (i = 0; i < stream->outcnt; i++)
05364ca0 134 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
3dbcc105 135
1ae2eaaa 136in:
0c3f6f65 137 sctp_stream_interleave_init(stream);
ff356414 138 if (!incnt)
5bbbbe32 139 goto out;
ff356414 140
79d08951
MRL
141 ret = sctp_stream_alloc_in(stream, incnt, gfp);
142 if (ret) {
143 sched->free(stream);
2075e50c 144 genradix_free(&stream->out);
79d08951
MRL
145 stream->outcnt = 0;
146 goto out;
a8386317
XL
147 }
148
5bbbbe32
MRL
149out:
150 return ret;
a8386317
XL
151}
152
f952be79
MRL
153int 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;
05364ca0 160 SCTP_SO(stream, sid)->ext = soute;
f952be79 161
5bbbbe32 162 return sctp_sched_init_sid(stream, sid, GFP_KERNEL);
f952be79
MRL
163}
164
a8386317
XL
165void sctp_stream_free(struct sctp_stream *stream)
166{
5bbbbe32 167 struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
f952be79
MRL
168 int i;
169
5bbbbe32 170 sched->free(stream);
f952be79 171 for (i = 0; i < stream->outcnt; i++)
05364ca0 172 kfree(SCTP_SO(stream, i)->ext);
2075e50c
KO
173 genradix_free(&stream->out);
174 genradix_free(&stream->in);
a8386317
XL
175}
176
177void sctp_stream_clear(struct sctp_stream *stream)
178{
179 int i;
180
107e2425 181 for (i = 0; i < stream->outcnt; i++) {
05364ca0
KK
182 SCTP_SO(stream, i)->mid = 0;
183 SCTP_SO(stream, i)->mid_uo = 0;
107e2425 184 }
a8386317
XL
185
186 for (i = 0; i < stream->incnt; i++)
05364ca0 187 SCTP_SI(stream, i)->mid = 0;
a8386317 188}
7f9d68ac 189
cee360ab
XL
190void sctp_stream_update(struct sctp_stream *stream, struct sctp_stream *new)
191{
5bbbbe32
MRL
192 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);
cee360ab
XL
196 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
5bbbbe32
MRL
203 sched->sched_all(stream);
204
2075e50c
KO
205 new->out.tree.root = NULL;
206 new->in.tree.root = NULL;
6a9a27d5
XL
207 new->outcnt = 0;
208 new->incnt = 0;
cee360ab
XL
209}
210
7f9d68ac
XL
211static 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
d570a59c
XL
224static 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
05364ca0
KK
240 if (SCTP_SO(stream, sid)->ext &&
241 !list_empty(&SCTP_SO(stream, sid)->ext->outq))
d570a59c
XL
242 return false;
243 }
244
245 return true;
246}
247
7f9d68ac
XL
248int sctp_send_reset_streams(struct sctp_association *asoc,
249 struct sctp_reset_streams *params)
250{
cee360ab 251 struct sctp_stream *stream = &asoc->stream;
7f9d68ac
XL
252 __u16 i, str_nums, *str_list;
253 struct sctp_chunk *chunk;
254 int retval = -EINVAL;
1da4fc97 255 __be16 *nstr_list;
7f9d68ac
XL
256 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;
423852f8
XL
276 if (str_nums) {
277 int param_len = 0;
278
279 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 }
7f9d68ac 301
1da4fc97
XL
302 nstr_list = kcalloc(str_nums, sizeof(__be16), GFP_KERNEL);
303 if (!nstr_list) {
304 retval = -ENOMEM;
305 goto out;
306 }
307
16e1a919 308 for (i = 0; i < str_nums; i++)
1da4fc97 309 nstr_list[i] = htons(str_list[i]);
16e1a919 310
d570a59c
XL
311 if (out && !sctp_stream_outq_is_empty(stream, str_nums, nstr_list)) {
312 retval = -EAGAIN;
313 goto out;
314 }
315
1da4fc97 316 chunk = sctp_make_strreset_req(asoc, str_nums, nstr_list, out, in);
16e1a919 317
1da4fc97 318 kfree(nstr_list);
16e1a919 319
119aecba
XL
320 if (!chunk) {
321 retval = -ENOMEM;
7f9d68ac 322 goto out;
119aecba 323 }
7f9d68ac
XL
324
325 if (out) {
326 if (str_nums)
327 for (i = 0; i < str_nums; i++)
05364ca0 328 SCTP_SO(stream, str_list[i])->state =
7f9d68ac
XL
329 SCTP_STREAM_CLOSED;
330 else
331 for (i = 0; i < stream->outcnt; i++)
05364ca0 332 SCTP_SO(stream, i)->state = SCTP_STREAM_CLOSED;
7f9d68ac
XL
333 }
334
7f9d68ac
XL
335 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;
119aecba
XL
342 if (!out)
343 goto out;
344
345 if (str_nums)
346 for (i = 0; i < str_nums; i++)
05364ca0 347 SCTP_SO(stream, str_list[i])->state =
119aecba
XL
348 SCTP_STREAM_OPEN;
349 else
350 for (i = 0; i < stream->outcnt; i++)
05364ca0 351 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
119aecba
XL
352
353 goto out;
7f9d68ac
XL
354 }
355
119aecba
XL
356 asoc->strreset_outstanding = out + in;
357
7f9d68ac
XL
358out:
359 return retval;
360}
a92ce1a4
XL
361
362int sctp_send_reset_assoc(struct sctp_association *asoc)
363{
cee360ab 364 struct sctp_stream *stream = &asoc->stream;
a92ce1a4
XL
365 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
5c6144a0
XL
376 if (!sctp_outq_is_empty(&asoc->outqueue))
377 return -EAGAIN;
378
a92ce1a4
XL
379 chunk = sctp_make_strreset_tsnreq(asoc);
380 if (!chunk)
381 return -ENOMEM;
382
383 /* Block further xmit of data until this request is completed */
cee360ab 384 for (i = 0; i < stream->outcnt; i++)
05364ca0 385 SCTP_SO(stream, i)->state = SCTP_STREAM_CLOSED;
a92ce1a4
XL
386
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
cee360ab 395 for (i = 0; i < stream->outcnt; i++)
05364ca0 396 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
a92ce1a4
XL
397
398 return retval;
399 }
400
401 asoc->strreset_outstanding = 1;
402
403 return 0;
404}
242bd2d5
XL
405
406int sctp_send_add_streams(struct sctp_association *asoc,
407 struct sctp_add_streams *params)
408{
cee360ab 409 struct sctp_stream *stream = &asoc->stream;
242bd2d5 410 struct sctp_chunk *chunk = NULL;
dc82673f 411 int retval;
242bd2d5
XL
412 __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) {
e090abd0
MRL
437 retval = sctp_stream_alloc_out(stream, outcnt, GFP_KERNEL);
438 if (retval)
242bd2d5 439 goto out;
242bd2d5
XL
440 }
441
242bd2d5 442 chunk = sctp_make_strreset_addstrm(asoc, out, in);
dc82673f
WY
443 if (!chunk) {
444 retval = -ENOMEM;
242bd2d5 445 goto out;
dc82673f 446 }
242bd2d5
XL
447
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
242bd2d5
XL
458 asoc->strreset_outstanding = !!out + !!in;
459
460out:
461 return retval;
462}
81054476 463
3c918704 464static struct sctp_paramhdr *sctp_chunk_lookup_strreset_param(
1da4fc97 465 struct sctp_association *asoc, __be32 resp_seq,
50a41591 466 __be16 type)
81054476
XL
467{
468 struct sctp_chunk *chunk = asoc->strreset_chunk;
469 struct sctp_reconf_chunk *hdr;
470 union sctp_params param;
471
50a41591 472 if (!chunk)
81054476
XL
473 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
50a41591
XL
483 if ((!resp_seq || req->request_seq == resp_seq) &&
484 (!type || type == req->param_hdr.type))
81054476
XL
485 return param.v;
486 }
487
488 return NULL;
489}
490
e4dc99c7
XL
491static 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
81054476
XL
498struct 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;
cee360ab 504 struct sctp_stream *stream = &asoc->stream;
81054476 505 __u32 result = SCTP_STRRESET_DENIED;
1da4fc97 506 __be16 *str_p = NULL;
81054476 507 __u32 request_seq;
2e6dc4d9 508 __u16 i, nums;
81054476
XL
509
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;
e4dc99c7 515 goto err;
81054476
XL
516 }
517
e4dc99c7
XL
518 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
519 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
81054476 520 result = SCTP_STRRESET_ERR_BAD_SEQNO;
e4dc99c7
XL
521 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;
81054476 526 }
e4dc99c7 527 asoc->strreset_inseq++;
81054476
XL
528
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
2e6dc4d9
XL
536 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
81054476 545 if (asoc->strreset_chunk) {
50a41591
XL
546 if (!sctp_chunk_lookup_strreset_param(
547 asoc, outreq->response_seq,
548 SCTP_PARAM_RESET_IN_REQUEST)) {
81054476
XL
549 /* 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) {
50a41591
XL
558 struct sctp_transport *t;
559
81054476
XL
560 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 }
81054476
XL
567 }
568
2e6dc4d9 569 if (nums)
81054476 570 for (i = 0; i < nums; i++)
05364ca0 571 SCTP_SI(stream, ntohs(str_p[i]))->mid = 0;
2e6dc4d9 572 else
81054476 573 for (i = 0; i < stream->incnt; i++)
05364ca0 574 SCTP_SI(stream, i)->mid = 0;
81054476
XL
575
576 result = SCTP_STRRESET_PERFORMED;
577
578 *evp = sctp_ulpevent_make_stream_reset_event(asoc,
2e6dc4d9 579 SCTP_STREAM_RESET_INCOMING_SSN, nums, str_p, GFP_ATOMIC);
81054476
XL
580
581out:
e4dc99c7
XL
582 sctp_update_strreset_result(asoc, result);
583err:
81054476
XL
584 return sctp_make_strreset_resp(asoc, result, request_seq);
585}
16e1a919
XL
586
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;
cee360ab 593 struct sctp_stream *stream = &asoc->stream;
16e1a919
XL
594 __u32 result = SCTP_STRRESET_DENIED;
595 struct sctp_chunk *chunk = NULL;
16e1a919 596 __u32 request_seq;
1da4fc97
XL
597 __u16 i, nums;
598 __be16 *str_p;
16e1a919
XL
599
600 request_seq = ntohl(inreq->request_seq);
d0f025e6
XL
601 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
602 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
16e1a919 603 result = SCTP_STRRESET_ERR_BAD_SEQNO;
d0f025e6
XL
604 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;
16e1a919 611 }
d0f025e6 612 asoc->strreset_inseq++;
16e1a919
XL
613
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
3aa623da 622 nums = (ntohs(param.p->length) - sizeof(*inreq)) / sizeof(__u16);
16e1a919
XL
623 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
d570a59c
XL
631 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
16e1a919
XL
637 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++)
05364ca0 643 SCTP_SO(stream, ntohs(str_p[i]))->state =
16e1a919
XL
644 SCTP_STREAM_CLOSED;
645 else
646 for (i = 0; i < stream->outcnt; i++)
05364ca0 647 SCTP_SO(stream, i)->state = SCTP_STREAM_CLOSED;
16e1a919
XL
648
649 asoc->strreset_chunk = chunk;
650 asoc->strreset_outstanding = 1;
651 sctp_chunk_hold(asoc->strreset_chunk);
652
d0f025e6
XL
653 result = SCTP_STRRESET_PERFORMED;
654
16e1a919 655out:
d0f025e6
XL
656 sctp_update_strreset_result(asoc, result);
657err:
16e1a919
XL
658 if (!chunk)
659 chunk = sctp_make_strreset_resp(asoc, result, request_seq);
660
661 return chunk;
662}
692787ce
XL
663
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;
cee360ab 671 struct sctp_stream *stream = &asoc->stream;
692787ce
XL
672 __u32 result = SCTP_STRRESET_DENIED;
673 __u32 request_seq;
674 __u16 i;
675
676 request_seq = ntohl(tsnreq->request_seq);
6c801387
XL
677 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
678 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
692787ce 679 result = SCTP_STRRESET_ERR_BAD_SEQNO;
6c801387
XL
680 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) {
52a39589 685 next_tsn = asoc->ctsn_ack_point + 1;
6c801387
XL
686 init_tsn =
687 sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + 1;
688 }
689 goto err;
692787ce 690 }
5c6144a0
XL
691
692 if (!sctp_outq_is_empty(&asoc->outqueue)) {
693 result = SCTP_STRRESET_IN_PROGRESS;
694 goto err;
695 }
696
6c801387 697 asoc->strreset_inseq++;
692787ce
XL
698
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
159f2a74
XL
707 /* 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.
692787ce
XL
711 */
712 max_tsn_seen = sctp_tsnmap_get_max_tsn_seen(&asoc->peer.tsn_map);
47b20a88 713 asoc->stream.si->report_ftsn(&asoc->ulpq, max_tsn_seen);
692787ce
XL
714
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
159f2a74
XL
724 /* 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.
692787ce
XL
727 */
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 */
107e2425 742 for (i = 0; i < stream->outcnt; i++) {
05364ca0
KK
743 SCTP_SO(stream, i)->mid = 0;
744 SCTP_SO(stream, i)->mid_uo = 0;
107e2425 745 }
692787ce 746 for (i = 0; i < stream->incnt; i++)
05364ca0 747 SCTP_SI(stream, i)->mid = 0;
692787ce
XL
748
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:
6c801387
XL
755 sctp_update_strreset_result(asoc, result);
756err:
692787ce
XL
757 return sctp_make_strreset_tsnresp(asoc, result, request_seq,
758 next_tsn, init_tsn);
759}
50a41591
XL
760
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;
cee360ab 767 struct sctp_stream *stream = &asoc->stream;
50a41591 768 __u32 result = SCTP_STRRESET_DENIED;
50a41591 769 __u32 request_seq, incnt;
e4dc99c7 770 __u16 in, i;
50a41591
XL
771
772 request_seq = ntohl(addstrm->request_seq);
e4dc99c7
XL
773 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
774 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
50a41591 775 result = SCTP_STRRESET_ERR_BAD_SEQNO;
e4dc99c7
XL
776 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;
50a41591 781 }
e4dc99c7 782 asoc->strreset_inseq++;
50a41591
XL
783
784 if (!(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ))
785 goto out;
786
8220c870
XL
787 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
50a41591
XL
795 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
50a41591
XL
818 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:
e4dc99c7
XL
826 sctp_update_strreset_result(asoc, result);
827err:
50a41591
XL
828 return sctp_make_strreset_resp(asoc, result, request_seq);
829}
c5c4ebb3
XL
830
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;
cee360ab 837 struct sctp_stream *stream = &asoc->stream;
c5c4ebb3 838 __u32 result = SCTP_STRRESET_DENIED;
c5c4ebb3
XL
839 struct sctp_chunk *chunk = NULL;
840 __u32 request_seq, outcnt;
d0f025e6 841 __u16 out, i;
e090abd0 842 int ret;
c5c4ebb3
XL
843
844 request_seq = ntohl(addstrm->request_seq);
d0f025e6
XL
845 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
846 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
c5c4ebb3 847 result = SCTP_STRRESET_ERR_BAD_SEQNO;
d0f025e6
XL
848 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;
c5c4ebb3 855 }
d0f025e6 856 asoc->strreset_inseq++;
c5c4ebb3
XL
857
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
e090abd0
MRL
871 ret = sctp_stream_alloc_out(stream, outcnt, GFP_ATOMIC);
872 if (ret)
c5c4ebb3
XL
873 goto out;
874
c5c4ebb3
XL
875 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
d0f025e6
XL
885 result = SCTP_STRRESET_PERFORMED;
886
c5c4ebb3 887out:
d0f025e6
XL
888 sctp_update_strreset_result(asoc, result);
889err:
c5c4ebb3
XL
890 if (!chunk)
891 chunk = sctp_make_strreset_resp(asoc, result, request_seq);
892
893 return chunk;
894}
11ae76e6
XL
895
896struct sctp_chunk *sctp_process_strreset_resp(
897 struct sctp_association *asoc,
898 union sctp_params param,
899 struct sctp_ulpevent **evp)
900{
cee360ab 901 struct sctp_stream *stream = &asoc->stream;
11ae76e6 902 struct sctp_strreset_resp *resp = param.v;
11ae76e6
XL
903 struct sctp_transport *t;
904 __u16 i, nums, flags = 0;
3c918704 905 struct sctp_paramhdr *req;
11ae76e6
XL
906 __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;
1da4fc97 925 __be16 *str_p;
11ae76e6
XL
926
927 outreq = (struct sctp_strreset_outreq *)req;
edb12f2d 928 str_p = outreq->list_of_streams;
3aa623da
XL
929 nums = (ntohs(outreq->param_hdr.length) - sizeof(*outreq)) /
930 sizeof(__u16);
11ae76e6
XL
931
932 if (result == SCTP_STRRESET_PERFORMED) {
05364ca0 933 struct sctp_stream_out *sout;
11ae76e6 934 if (nums) {
107e2425 935 for (i = 0; i < nums; i++) {
05364ca0
KK
936 sout = SCTP_SO(stream, ntohs(str_p[i]));
937 sout->mid = 0;
938 sout->mid_uo = 0;
107e2425 939 }
11ae76e6 940 } else {
107e2425 941 for (i = 0; i < stream->outcnt; i++) {
05364ca0
KK
942 sout = SCTP_SO(stream, i);
943 sout->mid = 0;
944 sout->mid_uo = 0;
107e2425 945 }
11ae76e6 946 }
11ae76e6
XL
947 }
948
2e6dc4d9
XL
949 flags |= SCTP_STREAM_RESET_OUTGOING_SSN;
950
11ae76e6 951 for (i = 0; i < stream->outcnt; i++)
05364ca0 952 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
11ae76e6
XL
953
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;
1da4fc97 958 __be16 *str_p;
11ae76e6
XL
959
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;
edb12f2d 965 str_p = inreq->list_of_streams;
3aa623da
XL
966 nums = (ntohs(inreq->param_hdr.length) - sizeof(*inreq)) /
967 sizeof(__u16);
11ae76e6 968
2e6dc4d9
XL
969 flags |= SCTP_STREAM_RESET_INCOMING_SSN;
970
11ae76e6
XL
971 *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);
159f2a74 988 LIST_HEAD(temp);
11ae76e6 989
47b20a88 990 asoc->stream.si->report_ftsn(&asoc->ulpq, mtsn);
11ae76e6
XL
991
992 sctp_tsnmap_init(&asoc->peer.tsn_map,
993 SCTP_TSN_MAP_INITIAL,
994 stsn, GFP_ATOMIC);
995
159f2a74
XL
996 /* 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);
11ae76e6 1001 sctp_outq_free(&asoc->outqueue);
159f2a74 1002 list_splice_init(&temp, &asoc->outqueue.out_chunk_list);
11ae76e6
XL
1003
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
107e2425 1008 for (i = 0; i < stream->outcnt; i++) {
05364ca0
KK
1009 SCTP_SO(stream, i)->mid = 0;
1010 SCTP_SO(stream, i)->mid_uo = 0;
107e2425 1011 }
11ae76e6 1012 for (i = 0; i < stream->incnt; i++)
05364ca0 1013 SCTP_SI(stream, i)->mid = 0;
11ae76e6
XL
1014 }
1015
1016 for (i = 0; i < stream->outcnt; i++)
05364ca0 1017 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
11ae76e6
XL
1018
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++)
05364ca0 1031 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
11ae76e6
XL
1032 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}