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