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