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