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