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