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