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