blob: af6b49850344da04ad5ca7c5b42c48409a1fda0c [file] [log] [blame]
Xin Longa8386312017-01-06 22:18:33 +08001/* 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 <net/sctp/sctp.h>
Xin Long7f9d68a2017-01-18 00:44:47 +080036#include <net/sctp/sm.h>
Xin Longa8386312017-01-06 22:18:33 +080037
Xin Long3dbcc102017-03-30 01:00:53 +080038int sctp_stream_new(struct sctp_association *asoc, gfp_t gfp)
Xin Longa8386312017-01-06 22:18:33 +080039{
Xin Longcee360a2017-05-31 16:36:31 +080040 struct sctp_stream *stream = &asoc->stream;
Xin Longa8386312017-01-06 22:18:33 +080041 int i;
42
Xin Long3dbcc102017-03-30 01:00:53 +080043 stream->outcnt = asoc->c.sinit_num_ostreams;
Xin Longa8386312017-01-06 22:18:33 +080044 stream->out = kcalloc(stream->outcnt, sizeof(*stream->out), gfp);
Xin Longcee360a2017-05-31 16:36:31 +080045 if (!stream->out)
Xin Long3dbcc102017-03-30 01:00:53 +080046 return -ENOMEM;
Xin Longcee360a2017-05-31 16:36:31 +080047
Xin Longa8386312017-01-06 22:18:33 +080048 for (i = 0; i < stream->outcnt; i++)
49 stream->out[i].state = SCTP_STREAM_OPEN;
50
Xin Long3dbcc102017-03-30 01:00:53 +080051 return 0;
52}
53
54int sctp_stream_init(struct sctp_association *asoc, gfp_t gfp)
55{
Xin Longcee360a2017-05-31 16:36:31 +080056 struct sctp_stream *stream = &asoc->stream;
Xin Long3dbcc102017-03-30 01:00:53 +080057 int i;
58
59 /* Initial stream->out size may be very big, so free it and alloc
60 * a new one with new outcnt to save memory.
61 */
62 kfree(stream->out);
63 stream->outcnt = asoc->c.sinit_num_ostreams;
64 stream->out = kcalloc(stream->outcnt, sizeof(*stream->out), gfp);
65 if (!stream->out)
Xin Longcee360a2017-05-31 16:36:31 +080066 return -ENOMEM;
Xin Long3dbcc102017-03-30 01:00:53 +080067
68 for (i = 0; i < stream->outcnt; i++)
69 stream->out[i].state = SCTP_STREAM_OPEN;
70
71 stream->incnt = asoc->c.sinit_max_instreams;
Xin Longa8386312017-01-06 22:18:33 +080072 stream->in = kcalloc(stream->incnt, sizeof(*stream->in), gfp);
73 if (!stream->in) {
74 kfree(stream->out);
Xin Longcee360a2017-05-31 16:36:31 +080075 stream->out = NULL;
76 return -ENOMEM;
Xin Longa8386312017-01-06 22:18:33 +080077 }
78
Xin Long3dbcc102017-03-30 01:00:53 +080079 return 0;
Xin Longa8386312017-01-06 22:18:33 +080080}
81
82void sctp_stream_free(struct sctp_stream *stream)
83{
Xin Longa8386312017-01-06 22:18:33 +080084 kfree(stream->out);
85 kfree(stream->in);
Xin Longa8386312017-01-06 22:18:33 +080086}
87
88void sctp_stream_clear(struct sctp_stream *stream)
89{
90 int i;
91
92 for (i = 0; i < stream->outcnt; i++)
93 stream->out[i].ssn = 0;
94
95 for (i = 0; i < stream->incnt; i++)
96 stream->in[i].ssn = 0;
97}
Xin Long7f9d68a2017-01-18 00:44:47 +080098
Xin Longcee360a2017-05-31 16:36:31 +080099void sctp_stream_update(struct sctp_stream *stream, struct sctp_stream *new)
100{
101 sctp_stream_free(stream);
102
103 stream->out = new->out;
104 stream->in = new->in;
105 stream->outcnt = new->outcnt;
106 stream->incnt = new->incnt;
107
108 new->out = NULL;
109 new->in = NULL;
110}
111
Xin Long7f9d68a2017-01-18 00:44:47 +0800112static int sctp_send_reconf(struct sctp_association *asoc,
113 struct sctp_chunk *chunk)
114{
115 struct net *net = sock_net(asoc->base.sk);
116 int retval = 0;
117
118 retval = sctp_primitive_RECONF(net, asoc, chunk);
119 if (retval)
120 sctp_chunk_free(chunk);
121
122 return retval;
123}
124
125int sctp_send_reset_streams(struct sctp_association *asoc,
126 struct sctp_reset_streams *params)
127{
Xin Longcee360a2017-05-31 16:36:31 +0800128 struct sctp_stream *stream = &asoc->stream;
Xin Long7f9d68a2017-01-18 00:44:47 +0800129 __u16 i, str_nums, *str_list;
130 struct sctp_chunk *chunk;
131 int retval = -EINVAL;
132 bool out, in;
133
134 if (!asoc->peer.reconf_capable ||
135 !(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ)) {
136 retval = -ENOPROTOOPT;
137 goto out;
138 }
139
140 if (asoc->strreset_outstanding) {
141 retval = -EINPROGRESS;
142 goto out;
143 }
144
145 out = params->srs_flags & SCTP_STREAM_RESET_OUTGOING;
146 in = params->srs_flags & SCTP_STREAM_RESET_INCOMING;
147 if (!out && !in)
148 goto out;
149
150 str_nums = params->srs_number_streams;
151 str_list = params->srs_stream_list;
152 if (out && str_nums)
153 for (i = 0; i < str_nums; i++)
154 if (str_list[i] >= stream->outcnt)
155 goto out;
156
157 if (in && str_nums)
158 for (i = 0; i < str_nums; i++)
159 if (str_list[i] >= stream->incnt)
160 goto out;
161
Xin Long16e1a912017-02-17 12:45:40 +0800162 for (i = 0; i < str_nums; i++)
163 str_list[i] = htons(str_list[i]);
164
Xin Long7f9d68a2017-01-18 00:44:47 +0800165 chunk = sctp_make_strreset_req(asoc, str_nums, str_list, out, in);
Xin Long16e1a912017-02-17 12:45:40 +0800166
167 for (i = 0; i < str_nums; i++)
168 str_list[i] = ntohs(str_list[i]);
169
Xin Long119aecb2017-02-09 01:18:16 +0800170 if (!chunk) {
171 retval = -ENOMEM;
Xin Long7f9d68a2017-01-18 00:44:47 +0800172 goto out;
Xin Long119aecb2017-02-09 01:18:16 +0800173 }
Xin Long7f9d68a2017-01-18 00:44:47 +0800174
175 if (out) {
176 if (str_nums)
177 for (i = 0; i < str_nums; i++)
178 stream->out[str_list[i]].state =
179 SCTP_STREAM_CLOSED;
180 else
181 for (i = 0; i < stream->outcnt; i++)
182 stream->out[i].state = SCTP_STREAM_CLOSED;
183 }
184
Xin Long7f9d68a2017-01-18 00:44:47 +0800185 asoc->strreset_chunk = chunk;
186 sctp_chunk_hold(asoc->strreset_chunk);
187
188 retval = sctp_send_reconf(asoc, chunk);
189 if (retval) {
190 sctp_chunk_put(asoc->strreset_chunk);
191 asoc->strreset_chunk = NULL;
Xin Long119aecb2017-02-09 01:18:16 +0800192 if (!out)
193 goto out;
194
195 if (str_nums)
196 for (i = 0; i < str_nums; i++)
197 stream->out[str_list[i]].state =
198 SCTP_STREAM_OPEN;
199 else
200 for (i = 0; i < stream->outcnt; i++)
201 stream->out[i].state = SCTP_STREAM_OPEN;
202
203 goto out;
Xin Long7f9d68a2017-01-18 00:44:47 +0800204 }
205
Xin Long119aecb2017-02-09 01:18:16 +0800206 asoc->strreset_outstanding = out + in;
207
Xin Long7f9d68a2017-01-18 00:44:47 +0800208out:
209 return retval;
210}
Xin Longa92ce1a2017-02-09 01:18:18 +0800211
212int sctp_send_reset_assoc(struct sctp_association *asoc)
213{
Xin Longcee360a2017-05-31 16:36:31 +0800214 struct sctp_stream *stream = &asoc->stream;
Xin Longa92ce1a2017-02-09 01:18:18 +0800215 struct sctp_chunk *chunk = NULL;
216 int retval;
217 __u16 i;
218
219 if (!asoc->peer.reconf_capable ||
220 !(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
221 return -ENOPROTOOPT;
222
223 if (asoc->strreset_outstanding)
224 return -EINPROGRESS;
225
226 chunk = sctp_make_strreset_tsnreq(asoc);
227 if (!chunk)
228 return -ENOMEM;
229
230 /* Block further xmit of data until this request is completed */
Xin Longcee360a2017-05-31 16:36:31 +0800231 for (i = 0; i < stream->outcnt; i++)
232 stream->out[i].state = SCTP_STREAM_CLOSED;
Xin Longa92ce1a2017-02-09 01:18:18 +0800233
234 asoc->strreset_chunk = chunk;
235 sctp_chunk_hold(asoc->strreset_chunk);
236
237 retval = sctp_send_reconf(asoc, chunk);
238 if (retval) {
239 sctp_chunk_put(asoc->strreset_chunk);
240 asoc->strreset_chunk = NULL;
241
Xin Longcee360a2017-05-31 16:36:31 +0800242 for (i = 0; i < stream->outcnt; i++)
243 stream->out[i].state = SCTP_STREAM_OPEN;
Xin Longa92ce1a2017-02-09 01:18:18 +0800244
245 return retval;
246 }
247
248 asoc->strreset_outstanding = 1;
249
250 return 0;
251}
Xin Long242bd2d2017-02-09 01:18:20 +0800252
253int sctp_send_add_streams(struct sctp_association *asoc,
254 struct sctp_add_streams *params)
255{
Xin Longcee360a2017-05-31 16:36:31 +0800256 struct sctp_stream *stream = &asoc->stream;
Xin Long242bd2d2017-02-09 01:18:20 +0800257 struct sctp_chunk *chunk = NULL;
258 int retval = -ENOMEM;
259 __u32 outcnt, incnt;
260 __u16 out, in;
261
262 if (!asoc->peer.reconf_capable ||
263 !(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
264 retval = -ENOPROTOOPT;
265 goto out;
266 }
267
268 if (asoc->strreset_outstanding) {
269 retval = -EINPROGRESS;
270 goto out;
271 }
272
273 out = params->sas_outstrms;
274 in = params->sas_instrms;
275 outcnt = stream->outcnt + out;
276 incnt = stream->incnt + in;
277 if (outcnt > SCTP_MAX_STREAM || incnt > SCTP_MAX_STREAM ||
278 (!out && !in)) {
279 retval = -EINVAL;
280 goto out;
281 }
282
283 if (out) {
284 struct sctp_stream_out *streamout;
285
286 streamout = krealloc(stream->out, outcnt * sizeof(*streamout),
287 GFP_KERNEL);
288 if (!streamout)
289 goto out;
290
291 memset(streamout + stream->outcnt, 0, out * sizeof(*streamout));
292 stream->out = streamout;
293 }
294
Xin Long242bd2d2017-02-09 01:18:20 +0800295 chunk = sctp_make_strreset_addstrm(asoc, out, in);
296 if (!chunk)
297 goto out;
298
299 asoc->strreset_chunk = chunk;
300 sctp_chunk_hold(asoc->strreset_chunk);
301
302 retval = sctp_send_reconf(asoc, chunk);
303 if (retval) {
304 sctp_chunk_put(asoc->strreset_chunk);
305 asoc->strreset_chunk = NULL;
306 goto out;
307 }
308
309 stream->incnt = incnt;
310 stream->outcnt = outcnt;
311
312 asoc->strreset_outstanding = !!out + !!in;
313
314out:
315 return retval;
316}
Xin Long81054472017-02-17 12:45:39 +0800317
318static sctp_paramhdr_t *sctp_chunk_lookup_strreset_param(
Xin Long50a41592017-03-10 12:11:09 +0800319 struct sctp_association *asoc, __u32 resp_seq,
320 __be16 type)
Xin Long81054472017-02-17 12:45:39 +0800321{
322 struct sctp_chunk *chunk = asoc->strreset_chunk;
323 struct sctp_reconf_chunk *hdr;
324 union sctp_params param;
325
Xin Long50a41592017-03-10 12:11:09 +0800326 if (!chunk)
Xin Long81054472017-02-17 12:45:39 +0800327 return NULL;
328
329 hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
330 sctp_walk_params(param, hdr, params) {
331 /* sctp_strreset_tsnreq is actually the basic structure
332 * of all stream reconf params, so it's safe to use it
333 * to access request_seq.
334 */
335 struct sctp_strreset_tsnreq *req = param.v;
336
Xin Long50a41592017-03-10 12:11:09 +0800337 if ((!resp_seq || req->request_seq == resp_seq) &&
338 (!type || type == req->param_hdr.type))
Xin Long81054472017-02-17 12:45:39 +0800339 return param.v;
340 }
341
342 return NULL;
343}
344
Xin Longe4dc99c2017-04-15 22:00:27 +0800345static void sctp_update_strreset_result(struct sctp_association *asoc,
346 __u32 result)
347{
348 asoc->strreset_result[1] = asoc->strreset_result[0];
349 asoc->strreset_result[0] = result;
350}
351
Xin Long81054472017-02-17 12:45:39 +0800352struct sctp_chunk *sctp_process_strreset_outreq(
353 struct sctp_association *asoc,
354 union sctp_params param,
355 struct sctp_ulpevent **evp)
356{
357 struct sctp_strreset_outreq *outreq = param.v;
Xin Longcee360a2017-05-31 16:36:31 +0800358 struct sctp_stream *stream = &asoc->stream;
Xin Long81054472017-02-17 12:45:39 +0800359 __u16 i, nums, flags = 0, *str_p = NULL;
360 __u32 result = SCTP_STRRESET_DENIED;
361 __u32 request_seq;
362
363 request_seq = ntohl(outreq->request_seq);
364
365 if (ntohl(outreq->send_reset_at_tsn) >
366 sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map)) {
367 result = SCTP_STRRESET_IN_PROGRESS;
Xin Longe4dc99c2017-04-15 22:00:27 +0800368 goto err;
Xin Long81054472017-02-17 12:45:39 +0800369 }
370
Xin Longe4dc99c2017-04-15 22:00:27 +0800371 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
372 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
Xin Long81054472017-02-17 12:45:39 +0800373 result = SCTP_STRRESET_ERR_BAD_SEQNO;
Xin Longe4dc99c2017-04-15 22:00:27 +0800374 goto err;
375 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
376 i = asoc->strreset_inseq - request_seq - 1;
377 result = asoc->strreset_result[i];
378 goto err;
Xin Long81054472017-02-17 12:45:39 +0800379 }
Xin Longe4dc99c2017-04-15 22:00:27 +0800380 asoc->strreset_inseq++;
Xin Long81054472017-02-17 12:45:39 +0800381
382 /* Check strreset_enable after inseq inc, as sender cannot tell
383 * the peer doesn't enable strreset after receiving response with
384 * result denied, as well as to keep consistent with bsd.
385 */
386 if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
387 goto out;
388
389 if (asoc->strreset_chunk) {
Xin Long50a41592017-03-10 12:11:09 +0800390 if (!sctp_chunk_lookup_strreset_param(
391 asoc, outreq->response_seq,
392 SCTP_PARAM_RESET_IN_REQUEST)) {
Xin Long81054472017-02-17 12:45:39 +0800393 /* same process with outstanding isn't 0 */
394 result = SCTP_STRRESET_ERR_IN_PROGRESS;
395 goto out;
396 }
397
398 asoc->strreset_outstanding--;
399 asoc->strreset_outseq++;
400
401 if (!asoc->strreset_outstanding) {
Xin Long50a41592017-03-10 12:11:09 +0800402 struct sctp_transport *t;
403
Xin Long81054472017-02-17 12:45:39 +0800404 t = asoc->strreset_chunk->transport;
405 if (del_timer(&t->reconf_timer))
406 sctp_transport_put(t);
407
408 sctp_chunk_put(asoc->strreset_chunk);
409 asoc->strreset_chunk = NULL;
410 }
411
412 flags = SCTP_STREAM_RESET_INCOMING_SSN;
413 }
414
415 nums = (ntohs(param.p->length) - sizeof(*outreq)) / 2;
416 if (nums) {
417 str_p = outreq->list_of_streams;
418 for (i = 0; i < nums; i++) {
419 if (ntohs(str_p[i]) >= stream->incnt) {
420 result = SCTP_STRRESET_ERR_WRONG_SSN;
421 goto out;
422 }
423 }
424
425 for (i = 0; i < nums; i++)
426 stream->in[ntohs(str_p[i])].ssn = 0;
427 } else {
428 for (i = 0; i < stream->incnt; i++)
429 stream->in[i].ssn = 0;
430 }
431
432 result = SCTP_STRRESET_PERFORMED;
433
434 *evp = sctp_ulpevent_make_stream_reset_event(asoc,
435 flags | SCTP_STREAM_RESET_OUTGOING_SSN, nums, str_p,
436 GFP_ATOMIC);
437
438out:
Xin Longe4dc99c2017-04-15 22:00:27 +0800439 sctp_update_strreset_result(asoc, result);
440err:
Xin Long81054472017-02-17 12:45:39 +0800441 return sctp_make_strreset_resp(asoc, result, request_seq);
442}
Xin Long16e1a912017-02-17 12:45:40 +0800443
444struct sctp_chunk *sctp_process_strreset_inreq(
445 struct sctp_association *asoc,
446 union sctp_params param,
447 struct sctp_ulpevent **evp)
448{
449 struct sctp_strreset_inreq *inreq = param.v;
Xin Longcee360a2017-05-31 16:36:31 +0800450 struct sctp_stream *stream = &asoc->stream;
Xin Long16e1a912017-02-17 12:45:40 +0800451 __u32 result = SCTP_STRRESET_DENIED;
452 struct sctp_chunk *chunk = NULL;
453 __u16 i, nums, *str_p;
454 __u32 request_seq;
455
456 request_seq = ntohl(inreq->request_seq);
Xin Longd0f025e2017-04-15 22:00:28 +0800457 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
458 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
Xin Long16e1a912017-02-17 12:45:40 +0800459 result = SCTP_STRRESET_ERR_BAD_SEQNO;
Xin Longd0f025e2017-04-15 22:00:28 +0800460 goto err;
461 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
462 i = asoc->strreset_inseq - request_seq - 1;
463 result = asoc->strreset_result[i];
464 if (result == SCTP_STRRESET_PERFORMED)
465 return NULL;
466 goto err;
Xin Long16e1a912017-02-17 12:45:40 +0800467 }
Xin Longd0f025e2017-04-15 22:00:28 +0800468 asoc->strreset_inseq++;
Xin Long16e1a912017-02-17 12:45:40 +0800469
470 if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
471 goto out;
472
473 if (asoc->strreset_outstanding) {
474 result = SCTP_STRRESET_ERR_IN_PROGRESS;
475 goto out;
476 }
477
478 nums = (ntohs(param.p->length) - sizeof(*inreq)) / 2;
479 str_p = inreq->list_of_streams;
480 for (i = 0; i < nums; i++) {
481 if (ntohs(str_p[i]) >= stream->outcnt) {
482 result = SCTP_STRRESET_ERR_WRONG_SSN;
483 goto out;
484 }
485 }
486
487 chunk = sctp_make_strreset_req(asoc, nums, str_p, 1, 0);
488 if (!chunk)
489 goto out;
490
491 if (nums)
492 for (i = 0; i < nums; i++)
493 stream->out[ntohs(str_p[i])].state =
494 SCTP_STREAM_CLOSED;
495 else
496 for (i = 0; i < stream->outcnt; i++)
497 stream->out[i].state = SCTP_STREAM_CLOSED;
498
499 asoc->strreset_chunk = chunk;
500 asoc->strreset_outstanding = 1;
501 sctp_chunk_hold(asoc->strreset_chunk);
502
Xin Longd0f025e2017-04-15 22:00:28 +0800503 result = SCTP_STRRESET_PERFORMED;
504
Xin Long16e1a912017-02-17 12:45:40 +0800505 *evp = sctp_ulpevent_make_stream_reset_event(asoc,
506 SCTP_STREAM_RESET_INCOMING_SSN, nums, str_p, GFP_ATOMIC);
507
508out:
Xin Longd0f025e2017-04-15 22:00:28 +0800509 sctp_update_strreset_result(asoc, result);
510err:
Xin Long16e1a912017-02-17 12:45:40 +0800511 if (!chunk)
512 chunk = sctp_make_strreset_resp(asoc, result, request_seq);
513
514 return chunk;
515}
Xin Long692787c2017-03-10 12:11:07 +0800516
517struct sctp_chunk *sctp_process_strreset_tsnreq(
518 struct sctp_association *asoc,
519 union sctp_params param,
520 struct sctp_ulpevent **evp)
521{
522 __u32 init_tsn = 0, next_tsn = 0, max_tsn_seen;
523 struct sctp_strreset_tsnreq *tsnreq = param.v;
Xin Longcee360a2017-05-31 16:36:31 +0800524 struct sctp_stream *stream = &asoc->stream;
Xin Long692787c2017-03-10 12:11:07 +0800525 __u32 result = SCTP_STRRESET_DENIED;
526 __u32 request_seq;
527 __u16 i;
528
529 request_seq = ntohl(tsnreq->request_seq);
Xin Long6c801382017-04-15 22:00:29 +0800530 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
531 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
Xin Long692787c2017-03-10 12:11:07 +0800532 result = SCTP_STRRESET_ERR_BAD_SEQNO;
Xin Long6c801382017-04-15 22:00:29 +0800533 goto err;
534 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
535 i = asoc->strreset_inseq - request_seq - 1;
536 result = asoc->strreset_result[i];
537 if (result == SCTP_STRRESET_PERFORMED) {
538 next_tsn = asoc->next_tsn;
539 init_tsn =
540 sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + 1;
541 }
542 goto err;
Xin Long692787c2017-03-10 12:11:07 +0800543 }
Xin Long6c801382017-04-15 22:00:29 +0800544 asoc->strreset_inseq++;
Xin Long692787c2017-03-10 12:11:07 +0800545
546 if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
547 goto out;
548
549 if (asoc->strreset_outstanding) {
550 result = SCTP_STRRESET_ERR_IN_PROGRESS;
551 goto out;
552 }
553
554 /* G3: The same processing as though a SACK chunk with no gap report
555 * and a cumulative TSN ACK of the Sender's Next TSN minus 1 were
556 * received MUST be performed.
557 */
558 max_tsn_seen = sctp_tsnmap_get_max_tsn_seen(&asoc->peer.tsn_map);
559 sctp_ulpq_reasm_flushtsn(&asoc->ulpq, max_tsn_seen);
560 sctp_ulpq_abort_pd(&asoc->ulpq, GFP_ATOMIC);
561
562 /* G1: Compute an appropriate value for the Receiver's Next TSN -- the
563 * TSN that the peer should use to send the next DATA chunk. The
564 * value SHOULD be the smallest TSN not acknowledged by the
565 * receiver of the request plus 2^31.
566 */
567 init_tsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + (1 << 31);
568 sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
569 init_tsn, GFP_ATOMIC);
570
571 /* G4: The same processing as though a FWD-TSN chunk (as defined in
572 * [RFC3758]) with all streams affected and a new cumulative TSN
573 * ACK of the Receiver's Next TSN minus 1 were received MUST be
574 * performed.
575 */
576 sctp_outq_free(&asoc->outqueue);
577
578 /* G2: Compute an appropriate value for the local endpoint's next TSN,
579 * i.e., the next TSN assigned by the receiver of the SSN/TSN reset
580 * chunk. The value SHOULD be the highest TSN sent by the receiver
581 * of the request plus 1.
582 */
583 next_tsn = asoc->next_tsn;
584 asoc->ctsn_ack_point = next_tsn - 1;
585 asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
586
587 /* G5: The next expected and outgoing SSNs MUST be reset to 0 for all
588 * incoming and outgoing streams.
589 */
590 for (i = 0; i < stream->outcnt; i++)
591 stream->out[i].ssn = 0;
592 for (i = 0; i < stream->incnt; i++)
593 stream->in[i].ssn = 0;
594
595 result = SCTP_STRRESET_PERFORMED;
596
597 *evp = sctp_ulpevent_make_assoc_reset_event(asoc, 0, init_tsn,
598 next_tsn, GFP_ATOMIC);
599
600out:
Xin Long6c801382017-04-15 22:00:29 +0800601 sctp_update_strreset_result(asoc, result);
602err:
Xin Long692787c2017-03-10 12:11:07 +0800603 return sctp_make_strreset_tsnresp(asoc, result, request_seq,
604 next_tsn, init_tsn);
605}
Xin Long50a41592017-03-10 12:11:09 +0800606
607struct sctp_chunk *sctp_process_strreset_addstrm_out(
608 struct sctp_association *asoc,
609 union sctp_params param,
610 struct sctp_ulpevent **evp)
611{
612 struct sctp_strreset_addstrm *addstrm = param.v;
Xin Longcee360a2017-05-31 16:36:31 +0800613 struct sctp_stream *stream = &asoc->stream;
Xin Long50a41592017-03-10 12:11:09 +0800614 __u32 result = SCTP_STRRESET_DENIED;
615 struct sctp_stream_in *streamin;
616 __u32 request_seq, incnt;
Xin Longe4dc99c2017-04-15 22:00:27 +0800617 __u16 in, i;
Xin Long50a41592017-03-10 12:11:09 +0800618
619 request_seq = ntohl(addstrm->request_seq);
Xin Longe4dc99c2017-04-15 22:00:27 +0800620 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
621 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
Xin Long50a41592017-03-10 12:11:09 +0800622 result = SCTP_STRRESET_ERR_BAD_SEQNO;
Xin Longe4dc99c2017-04-15 22:00:27 +0800623 goto err;
624 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
625 i = asoc->strreset_inseq - request_seq - 1;
626 result = asoc->strreset_result[i];
627 goto err;
Xin Long50a41592017-03-10 12:11:09 +0800628 }
Xin Longe4dc99c2017-04-15 22:00:27 +0800629 asoc->strreset_inseq++;
Xin Long50a41592017-03-10 12:11:09 +0800630
631 if (!(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ))
632 goto out;
633
634 if (asoc->strreset_chunk) {
635 if (!sctp_chunk_lookup_strreset_param(
636 asoc, 0, SCTP_PARAM_RESET_ADD_IN_STREAMS)) {
637 /* same process with outstanding isn't 0 */
638 result = SCTP_STRRESET_ERR_IN_PROGRESS;
639 goto out;
640 }
641
642 asoc->strreset_outstanding--;
643 asoc->strreset_outseq++;
644
645 if (!asoc->strreset_outstanding) {
646 struct sctp_transport *t;
647
648 t = asoc->strreset_chunk->transport;
649 if (del_timer(&t->reconf_timer))
650 sctp_transport_put(t);
651
652 sctp_chunk_put(asoc->strreset_chunk);
653 asoc->strreset_chunk = NULL;
654 }
655 }
656
657 in = ntohs(addstrm->number_of_streams);
658 incnt = stream->incnt + in;
659 if (!in || incnt > SCTP_MAX_STREAM)
660 goto out;
661
662 streamin = krealloc(stream->in, incnt * sizeof(*streamin),
663 GFP_ATOMIC);
664 if (!streamin)
665 goto out;
666
667 memset(streamin + stream->incnt, 0, in * sizeof(*streamin));
668 stream->in = streamin;
669 stream->incnt = incnt;
670
671 result = SCTP_STRRESET_PERFORMED;
672
673 *evp = sctp_ulpevent_make_stream_change_event(asoc,
674 0, ntohs(addstrm->number_of_streams), 0, GFP_ATOMIC);
675
676out:
Xin Longe4dc99c2017-04-15 22:00:27 +0800677 sctp_update_strreset_result(asoc, result);
678err:
Xin Long50a41592017-03-10 12:11:09 +0800679 return sctp_make_strreset_resp(asoc, result, request_seq);
680}
Xin Longc5c4ebb2017-03-10 12:11:10 +0800681
682struct sctp_chunk *sctp_process_strreset_addstrm_in(
683 struct sctp_association *asoc,
684 union sctp_params param,
685 struct sctp_ulpevent **evp)
686{
687 struct sctp_strreset_addstrm *addstrm = param.v;
Xin Longcee360a2017-05-31 16:36:31 +0800688 struct sctp_stream *stream = &asoc->stream;
Xin Longc5c4ebb2017-03-10 12:11:10 +0800689 __u32 result = SCTP_STRRESET_DENIED;
690 struct sctp_stream_out *streamout;
691 struct sctp_chunk *chunk = NULL;
692 __u32 request_seq, outcnt;
Xin Longd0f025e2017-04-15 22:00:28 +0800693 __u16 out, i;
Xin Longc5c4ebb2017-03-10 12:11:10 +0800694
695 request_seq = ntohl(addstrm->request_seq);
Xin Longd0f025e2017-04-15 22:00:28 +0800696 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
697 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
Xin Longc5c4ebb2017-03-10 12:11:10 +0800698 result = SCTP_STRRESET_ERR_BAD_SEQNO;
Xin Longd0f025e2017-04-15 22:00:28 +0800699 goto err;
700 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
701 i = asoc->strreset_inseq - request_seq - 1;
702 result = asoc->strreset_result[i];
703 if (result == SCTP_STRRESET_PERFORMED)
704 return NULL;
705 goto err;
Xin Longc5c4ebb2017-03-10 12:11:10 +0800706 }
Xin Longd0f025e2017-04-15 22:00:28 +0800707 asoc->strreset_inseq++;
Xin Longc5c4ebb2017-03-10 12:11:10 +0800708
709 if (!(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ))
710 goto out;
711
712 if (asoc->strreset_outstanding) {
713 result = SCTP_STRRESET_ERR_IN_PROGRESS;
714 goto out;
715 }
716
717 out = ntohs(addstrm->number_of_streams);
718 outcnt = stream->outcnt + out;
719 if (!out || outcnt > SCTP_MAX_STREAM)
720 goto out;
721
722 streamout = krealloc(stream->out, outcnt * sizeof(*streamout),
723 GFP_ATOMIC);
724 if (!streamout)
725 goto out;
726
727 memset(streamout + stream->outcnt, 0, out * sizeof(*streamout));
728 stream->out = streamout;
729
730 chunk = sctp_make_strreset_addstrm(asoc, out, 0);
731 if (!chunk)
732 goto out;
733
734 asoc->strreset_chunk = chunk;
735 asoc->strreset_outstanding = 1;
736 sctp_chunk_hold(asoc->strreset_chunk);
737
738 stream->outcnt = outcnt;
739
Xin Longd0f025e2017-04-15 22:00:28 +0800740 result = SCTP_STRRESET_PERFORMED;
741
Xin Longc5c4ebb2017-03-10 12:11:10 +0800742 *evp = sctp_ulpevent_make_stream_change_event(asoc,
743 0, 0, ntohs(addstrm->number_of_streams), GFP_ATOMIC);
744
745out:
Xin Longd0f025e2017-04-15 22:00:28 +0800746 sctp_update_strreset_result(asoc, result);
747err:
Xin Longc5c4ebb2017-03-10 12:11:10 +0800748 if (!chunk)
749 chunk = sctp_make_strreset_resp(asoc, result, request_seq);
750
751 return chunk;
752}
Xin Long11ae76e2017-03-10 12:11:11 +0800753
754struct sctp_chunk *sctp_process_strreset_resp(
755 struct sctp_association *asoc,
756 union sctp_params param,
757 struct sctp_ulpevent **evp)
758{
Xin Longcee360a2017-05-31 16:36:31 +0800759 struct sctp_stream *stream = &asoc->stream;
Xin Long11ae76e2017-03-10 12:11:11 +0800760 struct sctp_strreset_resp *resp = param.v;
Xin Long11ae76e2017-03-10 12:11:11 +0800761 struct sctp_transport *t;
762 __u16 i, nums, flags = 0;
763 sctp_paramhdr_t *req;
764 __u32 result;
765
766 req = sctp_chunk_lookup_strreset_param(asoc, resp->response_seq, 0);
767 if (!req)
768 return NULL;
769
770 result = ntohl(resp->result);
771 if (result != SCTP_STRRESET_PERFORMED) {
772 /* if in progress, do nothing but retransmit */
773 if (result == SCTP_STRRESET_IN_PROGRESS)
774 return NULL;
775 else if (result == SCTP_STRRESET_DENIED)
776 flags = SCTP_STREAM_RESET_DENIED;
777 else
778 flags = SCTP_STREAM_RESET_FAILED;
779 }
780
781 if (req->type == SCTP_PARAM_RESET_OUT_REQUEST) {
782 struct sctp_strreset_outreq *outreq;
Xin Longedb12f22017-04-15 21:56:57 +0800783 __u16 *str_p;
Xin Long11ae76e2017-03-10 12:11:11 +0800784
785 outreq = (struct sctp_strreset_outreq *)req;
Xin Longedb12f22017-04-15 21:56:57 +0800786 str_p = outreq->list_of_streams;
Xin Long11ae76e2017-03-10 12:11:11 +0800787 nums = (ntohs(outreq->param_hdr.length) - sizeof(*outreq)) / 2;
788
789 if (result == SCTP_STRRESET_PERFORMED) {
790 if (nums) {
Xin Long11ae76e2017-03-10 12:11:11 +0800791 for (i = 0; i < nums; i++)
792 stream->out[ntohs(str_p[i])].ssn = 0;
793 } else {
794 for (i = 0; i < stream->outcnt; i++)
795 stream->out[i].ssn = 0;
796 }
797
798 flags = SCTP_STREAM_RESET_OUTGOING_SSN;
799 }
800
801 for (i = 0; i < stream->outcnt; i++)
802 stream->out[i].state = SCTP_STREAM_OPEN;
803
804 *evp = sctp_ulpevent_make_stream_reset_event(asoc, flags,
805 nums, str_p, GFP_ATOMIC);
806 } else if (req->type == SCTP_PARAM_RESET_IN_REQUEST) {
807 struct sctp_strreset_inreq *inreq;
Xin Longedb12f22017-04-15 21:56:57 +0800808 __u16 *str_p;
Xin Long11ae76e2017-03-10 12:11:11 +0800809
810 /* if the result is performed, it's impossible for inreq */
811 if (result == SCTP_STRRESET_PERFORMED)
812 return NULL;
813
814 inreq = (struct sctp_strreset_inreq *)req;
Xin Longedb12f22017-04-15 21:56:57 +0800815 str_p = inreq->list_of_streams;
Xin Long11ae76e2017-03-10 12:11:11 +0800816 nums = (ntohs(inreq->param_hdr.length) - sizeof(*inreq)) / 2;
817
Xin Long11ae76e2017-03-10 12:11:11 +0800818 *evp = sctp_ulpevent_make_stream_reset_event(asoc, flags,
819 nums, str_p, GFP_ATOMIC);
820 } else if (req->type == SCTP_PARAM_RESET_TSN_REQUEST) {
821 struct sctp_strreset_resptsn *resptsn;
822 __u32 stsn, rtsn;
823
824 /* check for resptsn, as sctp_verify_reconf didn't do it*/
825 if (ntohs(param.p->length) != sizeof(*resptsn))
826 return NULL;
827
828 resptsn = (struct sctp_strreset_resptsn *)resp;
829 stsn = ntohl(resptsn->senders_next_tsn);
830 rtsn = ntohl(resptsn->receivers_next_tsn);
831
832 if (result == SCTP_STRRESET_PERFORMED) {
833 __u32 mtsn = sctp_tsnmap_get_max_tsn_seen(
834 &asoc->peer.tsn_map);
835
836 sctp_ulpq_reasm_flushtsn(&asoc->ulpq, mtsn);
837 sctp_ulpq_abort_pd(&asoc->ulpq, GFP_ATOMIC);
838
839 sctp_tsnmap_init(&asoc->peer.tsn_map,
840 SCTP_TSN_MAP_INITIAL,
841 stsn, GFP_ATOMIC);
842
843 sctp_outq_free(&asoc->outqueue);
844
845 asoc->next_tsn = rtsn;
846 asoc->ctsn_ack_point = asoc->next_tsn - 1;
847 asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
848
849 for (i = 0; i < stream->outcnt; i++)
850 stream->out[i].ssn = 0;
851 for (i = 0; i < stream->incnt; i++)
852 stream->in[i].ssn = 0;
853 }
854
855 for (i = 0; i < stream->outcnt; i++)
856 stream->out[i].state = SCTP_STREAM_OPEN;
857
858 *evp = sctp_ulpevent_make_assoc_reset_event(asoc, flags,
859 stsn, rtsn, GFP_ATOMIC);
860 } else if (req->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS) {
861 struct sctp_strreset_addstrm *addstrm;
862 __u16 number;
863
864 addstrm = (struct sctp_strreset_addstrm *)req;
865 nums = ntohs(addstrm->number_of_streams);
866 number = stream->outcnt - nums;
867
868 if (result == SCTP_STRRESET_PERFORMED)
869 for (i = number; i < stream->outcnt; i++)
870 stream->out[i].state = SCTP_STREAM_OPEN;
871 else
872 stream->outcnt = number;
873
874 *evp = sctp_ulpevent_make_stream_change_event(asoc, flags,
875 0, nums, GFP_ATOMIC);
876 } else if (req->type == SCTP_PARAM_RESET_ADD_IN_STREAMS) {
877 struct sctp_strreset_addstrm *addstrm;
878
879 /* if the result is performed, it's impossible for addstrm in
880 * request.
881 */
882 if (result == SCTP_STRRESET_PERFORMED)
883 return NULL;
884
885 addstrm = (struct sctp_strreset_addstrm *)req;
886 nums = ntohs(addstrm->number_of_streams);
887
888 *evp = sctp_ulpevent_make_stream_change_event(asoc, flags,
889 nums, 0, GFP_ATOMIC);
890 }
891
892 asoc->strreset_outstanding--;
893 asoc->strreset_outseq++;
894
895 /* remove everything for this reconf request */
896 if (!asoc->strreset_outstanding) {
897 t = asoc->strreset_chunk->transport;
898 if (del_timer(&t->reconf_timer))
899 sctp_transport_put(t);
900
901 sctp_chunk_put(asoc->strreset_chunk);
902 asoc->strreset_chunk = NULL;
903 }
904
905 return NULL;
906}