Merge tag 'drm-fixes-for-v4.17-rc4' of git://people.freedesktop.org/~airlied/linux
[linux-2.6-block.git] / net / sctp / stream.c
CommitLineData
a8386317
XL
1/* 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 *
fae8b6f4 9 * This file contains sctp stream maniuplation primitives and helpers.
a8386317
XL
10 *
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
5bbbbe32 35#include <linux/list.h>
a8386317 36#include <net/sctp/sctp.h>
7f9d68ac 37#include <net/sctp/sm.h>
5bbbbe32
MRL
38#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. */
08f46070 67 sctp_chunk_fail(ch, (__force __u32)SCTP_ERROR_INV_STRM);
5bbbbe32
MRL
68 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++) {
81 kfree(new->out[i].ext);
82 new->out[i].ext = stream->out[i].ext;
83 stream->out[i].ext = NULL;
84 }
85 }
86
87 for (i = outcnt; i < stream->outcnt; i++)
88 kfree(stream->out[i].ext);
89}
a8386317 90
e090abd0
MRL
91static int sctp_stream_alloc_out(struct sctp_stream *stream, __u16 outcnt,
92 gfp_t gfp)
93{
94 struct sctp_stream_out *out;
95
96 out = kmalloc_array(outcnt, sizeof(*out), gfp);
97 if (!out)
98 return -ENOMEM;
99
100 if (stream->out) {
101 memcpy(out, stream->out, min(outcnt, stream->outcnt) *
102 sizeof(*out));
103 kfree(stream->out);
104 }
105
106 if (outcnt > stream->outcnt)
107 memset(out + stream->outcnt, 0,
108 (outcnt - stream->outcnt) * sizeof(*out));
109
110 stream->out = out;
111
112 return 0;
113}
114
1fdb8d8f
MRL
115static int sctp_stream_alloc_in(struct sctp_stream *stream, __u16 incnt,
116 gfp_t gfp)
117{
118 struct sctp_stream_in *in;
119
120 in = kmalloc_array(incnt, sizeof(*stream->in), gfp);
121
122 if (!in)
123 return -ENOMEM;
124
125 if (stream->in) {
126 memcpy(in, stream->in, min(incnt, stream->incnt) *
127 sizeof(*in));
128 kfree(stream->in);
129 }
130
131 if (incnt > stream->incnt)
132 memset(in + stream->incnt, 0,
133 (incnt - stream->incnt) * sizeof(*in));
134
135 stream->in = in;
136
137 return 0;
138}
139
ff356414
XL
140int sctp_stream_init(struct sctp_stream *stream, __u16 outcnt, __u16 incnt,
141 gfp_t gfp)
a8386317 142{
5bbbbe32
MRL
143 struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
144 int i, ret = 0;
3dbcc105 145
1ae2eaaa
MRL
146 gfp |= __GFP_NOWARN;
147
3dbcc105 148 /* Initial stream->out size may be very big, so free it and alloc
1ae2eaaa 149 * a new one with new outcnt to save memory if needed.
3dbcc105 150 */
1ae2eaaa
MRL
151 if (outcnt == stream->outcnt)
152 goto in;
153
5bbbbe32
MRL
154 /* Filter out chunks queued on streams that won't exist anymore */
155 sched->unsched_all(stream);
156 sctp_stream_outq_migrate(stream, NULL, outcnt);
157 sched->sched_all(stream);
158
79d08951
MRL
159 ret = sctp_stream_alloc_out(stream, outcnt, gfp);
160 if (ret)
161 goto out;
3dbcc105 162
ff356414 163 stream->outcnt = outcnt;
3dbcc105
XL
164 for (i = 0; i < stream->outcnt; i++)
165 stream->out[i].state = SCTP_STREAM_OPEN;
166
5bbbbe32
MRL
167 sched->init(stream);
168
1ae2eaaa 169in:
0c3f6f65 170 sctp_stream_interleave_init(stream);
ff356414 171 if (!incnt)
5bbbbe32 172 goto out;
ff356414 173
79d08951
MRL
174 ret = sctp_stream_alloc_in(stream, incnt, gfp);
175 if (ret) {
176 sched->free(stream);
177 kfree(stream->out);
178 stream->out = NULL;
179 stream->outcnt = 0;
180 goto out;
a8386317
XL
181 }
182
ff356414
XL
183 stream->incnt = incnt;
184
5bbbbe32
MRL
185out:
186 return ret;
a8386317
XL
187}
188
f952be79
MRL
189int sctp_stream_init_ext(struct sctp_stream *stream, __u16 sid)
190{
191 struct sctp_stream_out_ext *soute;
192
193 soute = kzalloc(sizeof(*soute), GFP_KERNEL);
194 if (!soute)
195 return -ENOMEM;
196 stream->out[sid].ext = soute;
197
5bbbbe32 198 return sctp_sched_init_sid(stream, sid, GFP_KERNEL);
f952be79
MRL
199}
200
a8386317
XL
201void sctp_stream_free(struct sctp_stream *stream)
202{
5bbbbe32 203 struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
f952be79
MRL
204 int i;
205
5bbbbe32 206 sched->free(stream);
f952be79
MRL
207 for (i = 0; i < stream->outcnt; i++)
208 kfree(stream->out[i].ext);
a8386317
XL
209 kfree(stream->out);
210 kfree(stream->in);
a8386317
XL
211}
212
213void sctp_stream_clear(struct sctp_stream *stream)
214{
215 int i;
216
107e2425
XL
217 for (i = 0; i < stream->outcnt; i++) {
218 stream->out[i].mid = 0;
219 stream->out[i].mid_uo = 0;
220 }
a8386317
XL
221
222 for (i = 0; i < stream->incnt; i++)
107e2425 223 stream->in[i].mid = 0;
a8386317 224}
7f9d68ac 225
cee360ab
XL
226void sctp_stream_update(struct sctp_stream *stream, struct sctp_stream *new)
227{
5bbbbe32
MRL
228 struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
229
230 sched->unsched_all(stream);
231 sctp_stream_outq_migrate(stream, new, new->outcnt);
cee360ab
XL
232 sctp_stream_free(stream);
233
234 stream->out = new->out;
235 stream->in = new->in;
236 stream->outcnt = new->outcnt;
237 stream->incnt = new->incnt;
238
5bbbbe32
MRL
239 sched->sched_all(stream);
240
cee360ab
XL
241 new->out = NULL;
242 new->in = NULL;
6a9a27d5
XL
243 new->outcnt = 0;
244 new->incnt = 0;
cee360ab
XL
245}
246
7f9d68ac
XL
247static int sctp_send_reconf(struct sctp_association *asoc,
248 struct sctp_chunk *chunk)
249{
250 struct net *net = sock_net(asoc->base.sk);
251 int retval = 0;
252
253 retval = sctp_primitive_RECONF(net, asoc, chunk);
254 if (retval)
255 sctp_chunk_free(chunk);
256
257 return retval;
258}
259
d570a59c
XL
260static bool sctp_stream_outq_is_empty(struct sctp_stream *stream,
261 __u16 str_nums, __be16 *str_list)
262{
263 struct sctp_association *asoc;
264 __u16 i;
265
266 asoc = container_of(stream, struct sctp_association, stream);
267 if (!asoc->outqueue.out_qlen)
268 return true;
269
270 if (!str_nums)
271 return false;
272
273 for (i = 0; i < str_nums; i++) {
274 __u16 sid = ntohs(str_list[i]);
275
276 if (stream->out[sid].ext &&
277 !list_empty(&stream->out[sid].ext->outq))
278 return false;
279 }
280
281 return true;
282}
283
7f9d68ac
XL
284int sctp_send_reset_streams(struct sctp_association *asoc,
285 struct sctp_reset_streams *params)
286{
cee360ab 287 struct sctp_stream *stream = &asoc->stream;
7f9d68ac
XL
288 __u16 i, str_nums, *str_list;
289 struct sctp_chunk *chunk;
290 int retval = -EINVAL;
1da4fc97 291 __be16 *nstr_list;
7f9d68ac
XL
292 bool out, in;
293
294 if (!asoc->peer.reconf_capable ||
295 !(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ)) {
296 retval = -ENOPROTOOPT;
297 goto out;
298 }
299
300 if (asoc->strreset_outstanding) {
301 retval = -EINPROGRESS;
302 goto out;
303 }
304
305 out = params->srs_flags & SCTP_STREAM_RESET_OUTGOING;
306 in = params->srs_flags & SCTP_STREAM_RESET_INCOMING;
307 if (!out && !in)
308 goto out;
309
310 str_nums = params->srs_number_streams;
311 str_list = params->srs_stream_list;
423852f8
XL
312 if (str_nums) {
313 int param_len = 0;
314
315 if (out) {
316 for (i = 0; i < str_nums; i++)
317 if (str_list[i] >= stream->outcnt)
318 goto out;
319
320 param_len = str_nums * sizeof(__u16) +
321 sizeof(struct sctp_strreset_outreq);
322 }
323
324 if (in) {
325 for (i = 0; i < str_nums; i++)
326 if (str_list[i] >= stream->incnt)
327 goto out;
328
329 param_len += str_nums * sizeof(__u16) +
330 sizeof(struct sctp_strreset_inreq);
331 }
332
333 if (param_len > SCTP_MAX_CHUNK_LEN -
334 sizeof(struct sctp_reconf_chunk))
335 goto out;
336 }
7f9d68ac 337
1da4fc97
XL
338 nstr_list = kcalloc(str_nums, sizeof(__be16), GFP_KERNEL);
339 if (!nstr_list) {
340 retval = -ENOMEM;
341 goto out;
342 }
343
16e1a919 344 for (i = 0; i < str_nums; i++)
1da4fc97 345 nstr_list[i] = htons(str_list[i]);
16e1a919 346
d570a59c
XL
347 if (out && !sctp_stream_outq_is_empty(stream, str_nums, nstr_list)) {
348 retval = -EAGAIN;
349 goto out;
350 }
351
1da4fc97 352 chunk = sctp_make_strreset_req(asoc, str_nums, nstr_list, out, in);
16e1a919 353
1da4fc97 354 kfree(nstr_list);
16e1a919 355
119aecba
XL
356 if (!chunk) {
357 retval = -ENOMEM;
7f9d68ac 358 goto out;
119aecba 359 }
7f9d68ac
XL
360
361 if (out) {
362 if (str_nums)
363 for (i = 0; i < str_nums; i++)
364 stream->out[str_list[i]].state =
365 SCTP_STREAM_CLOSED;
366 else
367 for (i = 0; i < stream->outcnt; i++)
368 stream->out[i].state = SCTP_STREAM_CLOSED;
369 }
370
7f9d68ac
XL
371 asoc->strreset_chunk = chunk;
372 sctp_chunk_hold(asoc->strreset_chunk);
373
374 retval = sctp_send_reconf(asoc, chunk);
375 if (retval) {
376 sctp_chunk_put(asoc->strreset_chunk);
377 asoc->strreset_chunk = NULL;
119aecba
XL
378 if (!out)
379 goto out;
380
381 if (str_nums)
382 for (i = 0; i < str_nums; i++)
383 stream->out[str_list[i]].state =
384 SCTP_STREAM_OPEN;
385 else
386 for (i = 0; i < stream->outcnt; i++)
387 stream->out[i].state = SCTP_STREAM_OPEN;
388
389 goto out;
7f9d68ac
XL
390 }
391
119aecba
XL
392 asoc->strreset_outstanding = out + in;
393
7f9d68ac
XL
394out:
395 return retval;
396}
a92ce1a4
XL
397
398int sctp_send_reset_assoc(struct sctp_association *asoc)
399{
cee360ab 400 struct sctp_stream *stream = &asoc->stream;
a92ce1a4
XL
401 struct sctp_chunk *chunk = NULL;
402 int retval;
403 __u16 i;
404
405 if (!asoc->peer.reconf_capable ||
406 !(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
407 return -ENOPROTOOPT;
408
409 if (asoc->strreset_outstanding)
410 return -EINPROGRESS;
411
5c6144a0
XL
412 if (!sctp_outq_is_empty(&asoc->outqueue))
413 return -EAGAIN;
414
a92ce1a4
XL
415 chunk = sctp_make_strreset_tsnreq(asoc);
416 if (!chunk)
417 return -ENOMEM;
418
419 /* Block further xmit of data until this request is completed */
cee360ab
XL
420 for (i = 0; i < stream->outcnt; i++)
421 stream->out[i].state = SCTP_STREAM_CLOSED;
a92ce1a4
XL
422
423 asoc->strreset_chunk = chunk;
424 sctp_chunk_hold(asoc->strreset_chunk);
425
426 retval = sctp_send_reconf(asoc, chunk);
427 if (retval) {
428 sctp_chunk_put(asoc->strreset_chunk);
429 asoc->strreset_chunk = NULL;
430
cee360ab
XL
431 for (i = 0; i < stream->outcnt; i++)
432 stream->out[i].state = SCTP_STREAM_OPEN;
a92ce1a4
XL
433
434 return retval;
435 }
436
437 asoc->strreset_outstanding = 1;
438
439 return 0;
440}
242bd2d5
XL
441
442int sctp_send_add_streams(struct sctp_association *asoc,
443 struct sctp_add_streams *params)
444{
cee360ab 445 struct sctp_stream *stream = &asoc->stream;
242bd2d5 446 struct sctp_chunk *chunk = NULL;
dc82673f 447 int retval;
242bd2d5
XL
448 __u32 outcnt, incnt;
449 __u16 out, in;
450
451 if (!asoc->peer.reconf_capable ||
452 !(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
453 retval = -ENOPROTOOPT;
454 goto out;
455 }
456
457 if (asoc->strreset_outstanding) {
458 retval = -EINPROGRESS;
459 goto out;
460 }
461
462 out = params->sas_outstrms;
463 in = params->sas_instrms;
464 outcnt = stream->outcnt + out;
465 incnt = stream->incnt + in;
466 if (outcnt > SCTP_MAX_STREAM || incnt > SCTP_MAX_STREAM ||
467 (!out && !in)) {
468 retval = -EINVAL;
469 goto out;
470 }
471
472 if (out) {
e090abd0
MRL
473 retval = sctp_stream_alloc_out(stream, outcnt, GFP_KERNEL);
474 if (retval)
242bd2d5 475 goto out;
242bd2d5
XL
476 }
477
242bd2d5 478 chunk = sctp_make_strreset_addstrm(asoc, out, in);
dc82673f
WY
479 if (!chunk) {
480 retval = -ENOMEM;
242bd2d5 481 goto out;
dc82673f 482 }
242bd2d5
XL
483
484 asoc->strreset_chunk = chunk;
485 sctp_chunk_hold(asoc->strreset_chunk);
486
487 retval = sctp_send_reconf(asoc, chunk);
488 if (retval) {
489 sctp_chunk_put(asoc->strreset_chunk);
490 asoc->strreset_chunk = NULL;
491 goto out;
492 }
493
494 stream->incnt = incnt;
495 stream->outcnt = outcnt;
496
497 asoc->strreset_outstanding = !!out + !!in;
498
499out:
500 return retval;
501}
81054476 502
3c918704 503static struct sctp_paramhdr *sctp_chunk_lookup_strreset_param(
1da4fc97 504 struct sctp_association *asoc, __be32 resp_seq,
50a41591 505 __be16 type)
81054476
XL
506{
507 struct sctp_chunk *chunk = asoc->strreset_chunk;
508 struct sctp_reconf_chunk *hdr;
509 union sctp_params param;
510
50a41591 511 if (!chunk)
81054476
XL
512 return NULL;
513
514 hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
515 sctp_walk_params(param, hdr, params) {
516 /* sctp_strreset_tsnreq is actually the basic structure
517 * of all stream reconf params, so it's safe to use it
518 * to access request_seq.
519 */
520 struct sctp_strreset_tsnreq *req = param.v;
521
50a41591
XL
522 if ((!resp_seq || req->request_seq == resp_seq) &&
523 (!type || type == req->param_hdr.type))
81054476
XL
524 return param.v;
525 }
526
527 return NULL;
528}
529
e4dc99c7
XL
530static void sctp_update_strreset_result(struct sctp_association *asoc,
531 __u32 result)
532{
533 asoc->strreset_result[1] = asoc->strreset_result[0];
534 asoc->strreset_result[0] = result;
535}
536
81054476
XL
537struct sctp_chunk *sctp_process_strreset_outreq(
538 struct sctp_association *asoc,
539 union sctp_params param,
540 struct sctp_ulpevent **evp)
541{
542 struct sctp_strreset_outreq *outreq = param.v;
cee360ab 543 struct sctp_stream *stream = &asoc->stream;
81054476 544 __u32 result = SCTP_STRRESET_DENIED;
1da4fc97
XL
545 __u16 i, nums, flags = 0;
546 __be16 *str_p = NULL;
81054476
XL
547 __u32 request_seq;
548
549 request_seq = ntohl(outreq->request_seq);
550
551 if (ntohl(outreq->send_reset_at_tsn) >
552 sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map)) {
553 result = SCTP_STRRESET_IN_PROGRESS;
e4dc99c7 554 goto err;
81054476
XL
555 }
556
e4dc99c7
XL
557 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
558 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
81054476 559 result = SCTP_STRRESET_ERR_BAD_SEQNO;
e4dc99c7
XL
560 goto err;
561 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
562 i = asoc->strreset_inseq - request_seq - 1;
563 result = asoc->strreset_result[i];
564 goto err;
81054476 565 }
e4dc99c7 566 asoc->strreset_inseq++;
81054476
XL
567
568 /* Check strreset_enable after inseq inc, as sender cannot tell
569 * the peer doesn't enable strreset after receiving response with
570 * result denied, as well as to keep consistent with bsd.
571 */
572 if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
573 goto out;
574
575 if (asoc->strreset_chunk) {
50a41591
XL
576 if (!sctp_chunk_lookup_strreset_param(
577 asoc, outreq->response_seq,
578 SCTP_PARAM_RESET_IN_REQUEST)) {
81054476
XL
579 /* same process with outstanding isn't 0 */
580 result = SCTP_STRRESET_ERR_IN_PROGRESS;
581 goto out;
582 }
583
584 asoc->strreset_outstanding--;
585 asoc->strreset_outseq++;
586
587 if (!asoc->strreset_outstanding) {
50a41591
XL
588 struct sctp_transport *t;
589
81054476
XL
590 t = asoc->strreset_chunk->transport;
591 if (del_timer(&t->reconf_timer))
592 sctp_transport_put(t);
593
594 sctp_chunk_put(asoc->strreset_chunk);
595 asoc->strreset_chunk = NULL;
596 }
597
598 flags = SCTP_STREAM_RESET_INCOMING_SSN;
599 }
600
3aa623da 601 nums = (ntohs(param.p->length) - sizeof(*outreq)) / sizeof(__u16);
81054476
XL
602 if (nums) {
603 str_p = outreq->list_of_streams;
604 for (i = 0; i < nums; i++) {
605 if (ntohs(str_p[i]) >= stream->incnt) {
606 result = SCTP_STRRESET_ERR_WRONG_SSN;
607 goto out;
608 }
609 }
610
611 for (i = 0; i < nums; i++)
107e2425 612 stream->in[ntohs(str_p[i])].mid = 0;
81054476
XL
613 } else {
614 for (i = 0; i < stream->incnt; i++)
107e2425 615 stream->in[i].mid = 0;
81054476
XL
616 }
617
618 result = SCTP_STRRESET_PERFORMED;
619
620 *evp = sctp_ulpevent_make_stream_reset_event(asoc,
621 flags | SCTP_STREAM_RESET_OUTGOING_SSN, nums, str_p,
622 GFP_ATOMIC);
623
624out:
e4dc99c7
XL
625 sctp_update_strreset_result(asoc, result);
626err:
81054476
XL
627 return sctp_make_strreset_resp(asoc, result, request_seq);
628}
16e1a919
XL
629
630struct sctp_chunk *sctp_process_strreset_inreq(
631 struct sctp_association *asoc,
632 union sctp_params param,
633 struct sctp_ulpevent **evp)
634{
635 struct sctp_strreset_inreq *inreq = param.v;
cee360ab 636 struct sctp_stream *stream = &asoc->stream;
16e1a919
XL
637 __u32 result = SCTP_STRRESET_DENIED;
638 struct sctp_chunk *chunk = NULL;
16e1a919 639 __u32 request_seq;
1da4fc97
XL
640 __u16 i, nums;
641 __be16 *str_p;
16e1a919
XL
642
643 request_seq = ntohl(inreq->request_seq);
d0f025e6
XL
644 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
645 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
16e1a919 646 result = SCTP_STRRESET_ERR_BAD_SEQNO;
d0f025e6
XL
647 goto err;
648 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
649 i = asoc->strreset_inseq - request_seq - 1;
650 result = asoc->strreset_result[i];
651 if (result == SCTP_STRRESET_PERFORMED)
652 return NULL;
653 goto err;
16e1a919 654 }
d0f025e6 655 asoc->strreset_inseq++;
16e1a919
XL
656
657 if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
658 goto out;
659
660 if (asoc->strreset_outstanding) {
661 result = SCTP_STRRESET_ERR_IN_PROGRESS;
662 goto out;
663 }
664
3aa623da 665 nums = (ntohs(param.p->length) - sizeof(*inreq)) / sizeof(__u16);
16e1a919
XL
666 str_p = inreq->list_of_streams;
667 for (i = 0; i < nums; i++) {
668 if (ntohs(str_p[i]) >= stream->outcnt) {
669 result = SCTP_STRRESET_ERR_WRONG_SSN;
670 goto out;
671 }
672 }
673
d570a59c
XL
674 if (!sctp_stream_outq_is_empty(stream, nums, str_p)) {
675 result = SCTP_STRRESET_IN_PROGRESS;
676 asoc->strreset_inseq--;
677 goto err;
678 }
679
16e1a919
XL
680 chunk = sctp_make_strreset_req(asoc, nums, str_p, 1, 0);
681 if (!chunk)
682 goto out;
683
684 if (nums)
685 for (i = 0; i < nums; i++)
686 stream->out[ntohs(str_p[i])].state =
687 SCTP_STREAM_CLOSED;
688 else
689 for (i = 0; i < stream->outcnt; i++)
690 stream->out[i].state = SCTP_STREAM_CLOSED;
691
692 asoc->strreset_chunk = chunk;
693 asoc->strreset_outstanding = 1;
694 sctp_chunk_hold(asoc->strreset_chunk);
695
d0f025e6
XL
696 result = SCTP_STRRESET_PERFORMED;
697
16e1a919
XL
698 *evp = sctp_ulpevent_make_stream_reset_event(asoc,
699 SCTP_STREAM_RESET_INCOMING_SSN, nums, str_p, GFP_ATOMIC);
700
701out:
d0f025e6
XL
702 sctp_update_strreset_result(asoc, result);
703err:
16e1a919
XL
704 if (!chunk)
705 chunk = sctp_make_strreset_resp(asoc, result, request_seq);
706
707 return chunk;
708}
692787ce
XL
709
710struct sctp_chunk *sctp_process_strreset_tsnreq(
711 struct sctp_association *asoc,
712 union sctp_params param,
713 struct sctp_ulpevent **evp)
714{
715 __u32 init_tsn = 0, next_tsn = 0, max_tsn_seen;
716 struct sctp_strreset_tsnreq *tsnreq = param.v;
cee360ab 717 struct sctp_stream *stream = &asoc->stream;
692787ce
XL
718 __u32 result = SCTP_STRRESET_DENIED;
719 __u32 request_seq;
720 __u16 i;
721
722 request_seq = ntohl(tsnreq->request_seq);
6c801387
XL
723 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
724 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
692787ce 725 result = SCTP_STRRESET_ERR_BAD_SEQNO;
6c801387
XL
726 goto err;
727 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
728 i = asoc->strreset_inseq - request_seq - 1;
729 result = asoc->strreset_result[i];
730 if (result == SCTP_STRRESET_PERFORMED) {
52a39589 731 next_tsn = asoc->ctsn_ack_point + 1;
6c801387
XL
732 init_tsn =
733 sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + 1;
734 }
735 goto err;
692787ce 736 }
5c6144a0
XL
737
738 if (!sctp_outq_is_empty(&asoc->outqueue)) {
739 result = SCTP_STRRESET_IN_PROGRESS;
740 goto err;
741 }
742
6c801387 743 asoc->strreset_inseq++;
692787ce
XL
744
745 if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
746 goto out;
747
748 if (asoc->strreset_outstanding) {
749 result = SCTP_STRRESET_ERR_IN_PROGRESS;
750 goto out;
751 }
752
159f2a74
XL
753 /* G4: The same processing as though a FWD-TSN chunk (as defined in
754 * [RFC3758]) with all streams affected and a new cumulative TSN
755 * ACK of the Receiver's Next TSN minus 1 were received MUST be
756 * performed.
692787ce
XL
757 */
758 max_tsn_seen = sctp_tsnmap_get_max_tsn_seen(&asoc->peer.tsn_map);
47b20a88 759 asoc->stream.si->report_ftsn(&asoc->ulpq, max_tsn_seen);
692787ce
XL
760
761 /* G1: Compute an appropriate value for the Receiver's Next TSN -- the
762 * TSN that the peer should use to send the next DATA chunk. The
763 * value SHOULD be the smallest TSN not acknowledged by the
764 * receiver of the request plus 2^31.
765 */
766 init_tsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + (1 << 31);
767 sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
768 init_tsn, GFP_ATOMIC);
769
159f2a74
XL
770 /* G3: The same processing as though a SACK chunk with no gap report
771 * and a cumulative TSN ACK of the Sender's Next TSN minus 1 were
772 * received MUST be performed.
692787ce
XL
773 */
774 sctp_outq_free(&asoc->outqueue);
775
776 /* G2: Compute an appropriate value for the local endpoint's next TSN,
777 * i.e., the next TSN assigned by the receiver of the SSN/TSN reset
778 * chunk. The value SHOULD be the highest TSN sent by the receiver
779 * of the request plus 1.
780 */
781 next_tsn = asoc->next_tsn;
782 asoc->ctsn_ack_point = next_tsn - 1;
783 asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
784
785 /* G5: The next expected and outgoing SSNs MUST be reset to 0 for all
786 * incoming and outgoing streams.
787 */
107e2425
XL
788 for (i = 0; i < stream->outcnt; i++) {
789 stream->out[i].mid = 0;
790 stream->out[i].mid_uo = 0;
791 }
692787ce 792 for (i = 0; i < stream->incnt; i++)
107e2425 793 stream->in[i].mid = 0;
692787ce
XL
794
795 result = SCTP_STRRESET_PERFORMED;
796
797 *evp = sctp_ulpevent_make_assoc_reset_event(asoc, 0, init_tsn,
798 next_tsn, GFP_ATOMIC);
799
800out:
6c801387
XL
801 sctp_update_strreset_result(asoc, result);
802err:
692787ce
XL
803 return sctp_make_strreset_tsnresp(asoc, result, request_seq,
804 next_tsn, init_tsn);
805}
50a41591
XL
806
807struct sctp_chunk *sctp_process_strreset_addstrm_out(
808 struct sctp_association *asoc,
809 union sctp_params param,
810 struct sctp_ulpevent **evp)
811{
812 struct sctp_strreset_addstrm *addstrm = param.v;
cee360ab 813 struct sctp_stream *stream = &asoc->stream;
50a41591 814 __u32 result = SCTP_STRRESET_DENIED;
50a41591 815 __u32 request_seq, incnt;
e4dc99c7 816 __u16 in, i;
50a41591
XL
817
818 request_seq = ntohl(addstrm->request_seq);
e4dc99c7
XL
819 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
820 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
50a41591 821 result = SCTP_STRRESET_ERR_BAD_SEQNO;
e4dc99c7
XL
822 goto err;
823 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
824 i = asoc->strreset_inseq - request_seq - 1;
825 result = asoc->strreset_result[i];
826 goto err;
50a41591 827 }
e4dc99c7 828 asoc->strreset_inseq++;
50a41591
XL
829
830 if (!(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ))
831 goto out;
832
833 if (asoc->strreset_chunk) {
834 if (!sctp_chunk_lookup_strreset_param(
835 asoc, 0, SCTP_PARAM_RESET_ADD_IN_STREAMS)) {
836 /* same process with outstanding isn't 0 */
837 result = SCTP_STRRESET_ERR_IN_PROGRESS;
838 goto out;
839 }
840
841 asoc->strreset_outstanding--;
842 asoc->strreset_outseq++;
843
844 if (!asoc->strreset_outstanding) {
845 struct sctp_transport *t;
846
847 t = asoc->strreset_chunk->transport;
848 if (del_timer(&t->reconf_timer))
849 sctp_transport_put(t);
850
851 sctp_chunk_put(asoc->strreset_chunk);
852 asoc->strreset_chunk = NULL;
853 }
854 }
855
856 in = ntohs(addstrm->number_of_streams);
857 incnt = stream->incnt + in;
858 if (!in || incnt > SCTP_MAX_STREAM)
859 goto out;
860
1fdb8d8f 861 if (sctp_stream_alloc_in(stream, incnt, GFP_ATOMIC))
50a41591
XL
862 goto out;
863
50a41591
XL
864 stream->incnt = incnt;
865
866 result = SCTP_STRRESET_PERFORMED;
867
868 *evp = sctp_ulpevent_make_stream_change_event(asoc,
869 0, ntohs(addstrm->number_of_streams), 0, GFP_ATOMIC);
870
871out:
e4dc99c7
XL
872 sctp_update_strreset_result(asoc, result);
873err:
50a41591
XL
874 return sctp_make_strreset_resp(asoc, result, request_seq);
875}
c5c4ebb3
XL
876
877struct sctp_chunk *sctp_process_strreset_addstrm_in(
878 struct sctp_association *asoc,
879 union sctp_params param,
880 struct sctp_ulpevent **evp)
881{
882 struct sctp_strreset_addstrm *addstrm = param.v;
cee360ab 883 struct sctp_stream *stream = &asoc->stream;
c5c4ebb3 884 __u32 result = SCTP_STRRESET_DENIED;
c5c4ebb3
XL
885 struct sctp_chunk *chunk = NULL;
886 __u32 request_seq, outcnt;
d0f025e6 887 __u16 out, i;
e090abd0 888 int ret;
c5c4ebb3
XL
889
890 request_seq = ntohl(addstrm->request_seq);
d0f025e6
XL
891 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
892 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
c5c4ebb3 893 result = SCTP_STRRESET_ERR_BAD_SEQNO;
d0f025e6
XL
894 goto err;
895 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
896 i = asoc->strreset_inseq - request_seq - 1;
897 result = asoc->strreset_result[i];
898 if (result == SCTP_STRRESET_PERFORMED)
899 return NULL;
900 goto err;
c5c4ebb3 901 }
d0f025e6 902 asoc->strreset_inseq++;
c5c4ebb3
XL
903
904 if (!(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ))
905 goto out;
906
907 if (asoc->strreset_outstanding) {
908 result = SCTP_STRRESET_ERR_IN_PROGRESS;
909 goto out;
910 }
911
912 out = ntohs(addstrm->number_of_streams);
913 outcnt = stream->outcnt + out;
914 if (!out || outcnt > SCTP_MAX_STREAM)
915 goto out;
916
e090abd0
MRL
917 ret = sctp_stream_alloc_out(stream, outcnt, GFP_ATOMIC);
918 if (ret)
c5c4ebb3
XL
919 goto out;
920
c5c4ebb3
XL
921 chunk = sctp_make_strreset_addstrm(asoc, out, 0);
922 if (!chunk)
923 goto out;
924
925 asoc->strreset_chunk = chunk;
926 asoc->strreset_outstanding = 1;
927 sctp_chunk_hold(asoc->strreset_chunk);
928
929 stream->outcnt = outcnt;
930
d0f025e6
XL
931 result = SCTP_STRRESET_PERFORMED;
932
c5c4ebb3
XL
933 *evp = sctp_ulpevent_make_stream_change_event(asoc,
934 0, 0, ntohs(addstrm->number_of_streams), GFP_ATOMIC);
935
936out:
d0f025e6
XL
937 sctp_update_strreset_result(asoc, result);
938err:
c5c4ebb3
XL
939 if (!chunk)
940 chunk = sctp_make_strreset_resp(asoc, result, request_seq);
941
942 return chunk;
943}
11ae76e6
XL
944
945struct sctp_chunk *sctp_process_strreset_resp(
946 struct sctp_association *asoc,
947 union sctp_params param,
948 struct sctp_ulpevent **evp)
949{
cee360ab 950 struct sctp_stream *stream = &asoc->stream;
11ae76e6 951 struct sctp_strreset_resp *resp = param.v;
11ae76e6
XL
952 struct sctp_transport *t;
953 __u16 i, nums, flags = 0;
3c918704 954 struct sctp_paramhdr *req;
11ae76e6
XL
955 __u32 result;
956
957 req = sctp_chunk_lookup_strreset_param(asoc, resp->response_seq, 0);
958 if (!req)
959 return NULL;
960
961 result = ntohl(resp->result);
962 if (result != SCTP_STRRESET_PERFORMED) {
963 /* if in progress, do nothing but retransmit */
964 if (result == SCTP_STRRESET_IN_PROGRESS)
965 return NULL;
966 else if (result == SCTP_STRRESET_DENIED)
967 flags = SCTP_STREAM_RESET_DENIED;
968 else
969 flags = SCTP_STREAM_RESET_FAILED;
970 }
971
972 if (req->type == SCTP_PARAM_RESET_OUT_REQUEST) {
973 struct sctp_strreset_outreq *outreq;
1da4fc97 974 __be16 *str_p;
11ae76e6
XL
975
976 outreq = (struct sctp_strreset_outreq *)req;
edb12f2d 977 str_p = outreq->list_of_streams;
3aa623da
XL
978 nums = (ntohs(outreq->param_hdr.length) - sizeof(*outreq)) /
979 sizeof(__u16);
11ae76e6
XL
980
981 if (result == SCTP_STRRESET_PERFORMED) {
982 if (nums) {
107e2425
XL
983 for (i = 0; i < nums; i++) {
984 stream->out[ntohs(str_p[i])].mid = 0;
985 stream->out[ntohs(str_p[i])].mid_uo = 0;
986 }
11ae76e6 987 } else {
107e2425
XL
988 for (i = 0; i < stream->outcnt; i++) {
989 stream->out[i].mid = 0;
990 stream->out[i].mid_uo = 0;
991 }
11ae76e6
XL
992 }
993
994 flags = SCTP_STREAM_RESET_OUTGOING_SSN;
995 }
996
997 for (i = 0; i < stream->outcnt; i++)
998 stream->out[i].state = SCTP_STREAM_OPEN;
999
1000 *evp = sctp_ulpevent_make_stream_reset_event(asoc, flags,
1001 nums, str_p, GFP_ATOMIC);
1002 } else if (req->type == SCTP_PARAM_RESET_IN_REQUEST) {
1003 struct sctp_strreset_inreq *inreq;
1da4fc97 1004 __be16 *str_p;
11ae76e6
XL
1005
1006 /* if the result is performed, it's impossible for inreq */
1007 if (result == SCTP_STRRESET_PERFORMED)
1008 return NULL;
1009
1010 inreq = (struct sctp_strreset_inreq *)req;
edb12f2d 1011 str_p = inreq->list_of_streams;
3aa623da
XL
1012 nums = (ntohs(inreq->param_hdr.length) - sizeof(*inreq)) /
1013 sizeof(__u16);
11ae76e6 1014
11ae76e6
XL
1015 *evp = sctp_ulpevent_make_stream_reset_event(asoc, flags,
1016 nums, str_p, GFP_ATOMIC);
1017 } else if (req->type == SCTP_PARAM_RESET_TSN_REQUEST) {
1018 struct sctp_strreset_resptsn *resptsn;
1019 __u32 stsn, rtsn;
1020
1021 /* check for resptsn, as sctp_verify_reconf didn't do it*/
1022 if (ntohs(param.p->length) != sizeof(*resptsn))
1023 return NULL;
1024
1025 resptsn = (struct sctp_strreset_resptsn *)resp;
1026 stsn = ntohl(resptsn->senders_next_tsn);
1027 rtsn = ntohl(resptsn->receivers_next_tsn);
1028
1029 if (result == SCTP_STRRESET_PERFORMED) {
1030 __u32 mtsn = sctp_tsnmap_get_max_tsn_seen(
1031 &asoc->peer.tsn_map);
159f2a74 1032 LIST_HEAD(temp);
11ae76e6 1033
47b20a88 1034 asoc->stream.si->report_ftsn(&asoc->ulpq, mtsn);
11ae76e6
XL
1035
1036 sctp_tsnmap_init(&asoc->peer.tsn_map,
1037 SCTP_TSN_MAP_INITIAL,
1038 stsn, GFP_ATOMIC);
1039
159f2a74
XL
1040 /* Clean up sacked and abandoned queues only. As the
1041 * out_chunk_list may not be empty, splice it to temp,
1042 * then get it back after sctp_outq_free is done.
1043 */
1044 list_splice_init(&asoc->outqueue.out_chunk_list, &temp);
11ae76e6 1045 sctp_outq_free(&asoc->outqueue);
159f2a74 1046 list_splice_init(&temp, &asoc->outqueue.out_chunk_list);
11ae76e6
XL
1047
1048 asoc->next_tsn = rtsn;
1049 asoc->ctsn_ack_point = asoc->next_tsn - 1;
1050 asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
1051
107e2425
XL
1052 for (i = 0; i < stream->outcnt; i++) {
1053 stream->out[i].mid = 0;
1054 stream->out[i].mid_uo = 0;
1055 }
11ae76e6 1056 for (i = 0; i < stream->incnt; i++)
107e2425 1057 stream->in[i].mid = 0;
11ae76e6
XL
1058 }
1059
1060 for (i = 0; i < stream->outcnt; i++)
1061 stream->out[i].state = SCTP_STREAM_OPEN;
1062
1063 *evp = sctp_ulpevent_make_assoc_reset_event(asoc, flags,
1064 stsn, rtsn, GFP_ATOMIC);
1065 } else if (req->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS) {
1066 struct sctp_strreset_addstrm *addstrm;
1067 __u16 number;
1068
1069 addstrm = (struct sctp_strreset_addstrm *)req;
1070 nums = ntohs(addstrm->number_of_streams);
1071 number = stream->outcnt - nums;
1072
1073 if (result == SCTP_STRRESET_PERFORMED)
1074 for (i = number; i < stream->outcnt; i++)
1075 stream->out[i].state = SCTP_STREAM_OPEN;
1076 else
1077 stream->outcnt = number;
1078
1079 *evp = sctp_ulpevent_make_stream_change_event(asoc, flags,
1080 0, nums, GFP_ATOMIC);
1081 } else if (req->type == SCTP_PARAM_RESET_ADD_IN_STREAMS) {
1082 struct sctp_strreset_addstrm *addstrm;
1083
1084 /* if the result is performed, it's impossible for addstrm in
1085 * request.
1086 */
1087 if (result == SCTP_STRRESET_PERFORMED)
1088 return NULL;
1089
1090 addstrm = (struct sctp_strreset_addstrm *)req;
1091 nums = ntohs(addstrm->number_of_streams);
1092
1093 *evp = sctp_ulpevent_make_stream_change_event(asoc, flags,
1094 nums, 0, GFP_ATOMIC);
1095 }
1096
1097 asoc->strreset_outstanding--;
1098 asoc->strreset_outseq++;
1099
1100 /* remove everything for this reconf request */
1101 if (!asoc->strreset_outstanding) {
1102 t = asoc->strreset_chunk->transport;
1103 if (del_timer(&t->reconf_timer))
1104 sctp_transport_put(t);
1105
1106 sctp_chunk_put(asoc->strreset_chunk);
1107 asoc->strreset_chunk = NULL;
1108 }
1109
1110 return NULL;
1111}