stream.c 28.3 KB
Newer Older
1 2 3 4 5 6 7 8
/* SCTP kernel implementation
 * (C) Copyright IBM Corp. 2001, 2004
 * Copyright (c) 1999-2000 Cisco, Inc.
 * Copyright (c) 1999-2001 Motorola, Inc.
 * Copyright (c) 2001 Intel Corp.
 *
 * This file is part of the SCTP kernel implementation
 *
9
 * This file contains sctp stream maniuplation primitives and helpers.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 *
 * This SCTP implementation is free software;
 * you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This SCTP implementation is distributed in the hope that it
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
 *                 ************************
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU CC; see the file COPYING.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Please send any bug reports or fixes you make to the
 * email address(es):
 *    lksctp developers <linux-sctp@vger.kernel.org>
 *
 * Written or modified by:
 *    Xin Long <lucien.xin@gmail.com>
 */

35
#include <linux/list.h>
36
#include <net/sctp/sctp.h>
37
#include <net/sctp/sm.h>
38 39
#include <net/sctp/stream_sched.h>

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
static struct flex_array *fa_alloc(size_t elem_size, size_t elem_count,
				   gfp_t gfp)
{
	struct flex_array *result;
	int err;

	result = flex_array_alloc(elem_size, elem_count, gfp);
	if (result) {
		err = flex_array_prealloc(result, 0, elem_count, gfp);
		if (err) {
			flex_array_free(result);
			result = NULL;
		}
	}

	return result;
}

static void fa_free(struct flex_array *fa)
{
	if (fa)
		flex_array_free(fa);
}

static void fa_copy(struct flex_array *fa, struct flex_array *from,
		    size_t index, size_t count)
{
	void *elem;

	while (count--) {
		elem = flex_array_get(from, index);
		flex_array_put(fa, index, elem, 0);
		index++;
	}
}

static void fa_zero(struct flex_array *fa, size_t index, size_t count)
{
	void *elem;

	while (count--) {
		elem = flex_array_get(fa, index);
		memset(elem, 0, fa->element_size);
		index++;
	}
}

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
/* Migrates chunks from stream queues to new stream queues if needed,
 * but not across associations. Also, removes those chunks to streams
 * higher than the new max.
 */
static void sctp_stream_outq_migrate(struct sctp_stream *stream,
				     struct sctp_stream *new, __u16 outcnt)
{
	struct sctp_association *asoc;
	struct sctp_chunk *ch, *temp;
	struct sctp_outq *outq;
	int i;

	asoc = container_of(stream, struct sctp_association, stream);
	outq = &asoc->outqueue;

	list_for_each_entry_safe(ch, temp, &outq->out_chunk_list, list) {
		__u16 sid = sctp_chunk_stream_no(ch);

		if (sid < outcnt)
			continue;

		sctp_sched_dequeue_common(outq, ch);
		/* No need to call dequeue_done here because
		 * the chunks are not scheduled by now.
		 */

		/* Mark as failed send. */
114
		sctp_chunk_fail(ch, (__force __u32)SCTP_ERROR_INV_STRM);
115 116 117 118 119 120 121 122 123 124 125 126 127
		if (asoc->peer.prsctp_capable &&
		    SCTP_PR_PRIO_ENABLED(ch->sinfo.sinfo_flags))
			asoc->sent_cnt_removable--;

		sctp_chunk_free(ch);
	}

	if (new) {
		/* Here we actually move the old ext stuff into the new
		 * buffer, because we want to keep it. Then
		 * sctp_stream_update will swap ->out pointers.
		 */
		for (i = 0; i < outcnt; i++) {
128 129 130
			kfree(SCTP_SO(new, i)->ext);
			SCTP_SO(new, i)->ext = SCTP_SO(stream, i)->ext;
			SCTP_SO(stream, i)->ext = NULL;
131 132 133 134
		}
	}

	for (i = outcnt; i < stream->outcnt; i++)
135
		kfree(SCTP_SO(stream, i)->ext);
136
}
137

138 139 140
static int sctp_stream_alloc_out(struct sctp_stream *stream, __u16 outcnt,
				 gfp_t gfp)
{
141 142
	struct flex_array *out;
	size_t elem_size = sizeof(struct sctp_stream_out);
143

144
	out = fa_alloc(elem_size, outcnt, gfp);
145 146 147 148
	if (!out)
		return -ENOMEM;

	if (stream->out) {
149 150
		fa_copy(out, stream->out, 0, min(outcnt, stream->outcnt));
		fa_free(stream->out);
151 152 153
	}

	if (outcnt > stream->outcnt)
154
		fa_zero(out, stream->outcnt, (outcnt - stream->outcnt));
155 156 157 158 159 160

	stream->out = out;

	return 0;
}

161 162 163
static int sctp_stream_alloc_in(struct sctp_stream *stream, __u16 incnt,
				gfp_t gfp)
{
164 165
	struct flex_array *in;
	size_t elem_size = sizeof(struct sctp_stream_in);
166

167
	in = fa_alloc(elem_size, incnt, gfp);
168 169 170 171
	if (!in)
		return -ENOMEM;

	if (stream->in) {
172 173
		fa_copy(in, stream->in, 0, min(incnt, stream->incnt));
		fa_free(stream->in);
174 175 176
	}

	if (incnt > stream->incnt)
177
		fa_zero(in, stream->incnt, (incnt - stream->incnt));
178 179 180 181 182 183

	stream->in = in;

	return 0;
}

184 185
int sctp_stream_init(struct sctp_stream *stream, __u16 outcnt, __u16 incnt,
		     gfp_t gfp)
186
{
187 188
	struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
	int i, ret = 0;
189

190 191
	gfp |= __GFP_NOWARN;

192
	/* Initial stream->out size may be very big, so free it and alloc
193
	 * a new one with new outcnt to save memory if needed.
194
	 */
195 196 197
	if (outcnt == stream->outcnt)
		goto in;

198 199 200 201 202
	/* Filter out chunks queued on streams that won't exist anymore */
	sched->unsched_all(stream);
	sctp_stream_outq_migrate(stream, NULL, outcnt);
	sched->sched_all(stream);

203 204 205
	ret = sctp_stream_alloc_out(stream, outcnt, gfp);
	if (ret)
		goto out;
206

207
	stream->outcnt = outcnt;
208
	for (i = 0; i < stream->outcnt; i++)
209
		SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
210

211 212
	sched->init(stream);

213
in:
214
	sctp_stream_interleave_init(stream);
215
	if (!incnt)
216
		goto out;
217

218 219 220
	ret = sctp_stream_alloc_in(stream, incnt, gfp);
	if (ret) {
		sched->free(stream);
221
		fa_free(stream->out);
222 223 224
		stream->out = NULL;
		stream->outcnt = 0;
		goto out;
225 226
	}

227 228
	stream->incnt = incnt;

229 230
out:
	return ret;
231 232
}

233 234 235 236 237 238 239
int sctp_stream_init_ext(struct sctp_stream *stream, __u16 sid)
{
	struct sctp_stream_out_ext *soute;

	soute = kzalloc(sizeof(*soute), GFP_KERNEL);
	if (!soute)
		return -ENOMEM;
240
	SCTP_SO(stream, sid)->ext = soute;
241

242
	return sctp_sched_init_sid(stream, sid, GFP_KERNEL);
243 244
}

245 246
void sctp_stream_free(struct sctp_stream *stream)
{
247
	struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
248 249
	int i;

250
	sched->free(stream);
251
	for (i = 0; i < stream->outcnt; i++)
252
		kfree(SCTP_SO(stream, i)->ext);
253 254
	fa_free(stream->out);
	fa_free(stream->in);
255 256 257 258 259 260
}

void sctp_stream_clear(struct sctp_stream *stream)
{
	int i;

261
	for (i = 0; i < stream->outcnt; i++) {
262 263
		SCTP_SO(stream, i)->mid = 0;
		SCTP_SO(stream, i)->mid_uo = 0;
264
	}
265 266

	for (i = 0; i < stream->incnt; i++)
267
		SCTP_SI(stream, i)->mid = 0;
268
}
269

270 271
void sctp_stream_update(struct sctp_stream *stream, struct sctp_stream *new)
{
272 273 274 275
	struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);

	sched->unsched_all(stream);
	sctp_stream_outq_migrate(stream, new, new->outcnt);
276 277 278 279 280 281 282
	sctp_stream_free(stream);

	stream->out = new->out;
	stream->in  = new->in;
	stream->outcnt = new->outcnt;
	stream->incnt  = new->incnt;

283 284
	sched->sched_all(stream);

285 286
	new->out = NULL;
	new->in  = NULL;
287 288
	new->outcnt = 0;
	new->incnt  = 0;
289 290
}

291 292 293 294 295 296 297 298 299 300 301 302 303
static int sctp_send_reconf(struct sctp_association *asoc,
			    struct sctp_chunk *chunk)
{
	struct net *net = sock_net(asoc->base.sk);
	int retval = 0;

	retval = sctp_primitive_RECONF(net, asoc, chunk);
	if (retval)
		sctp_chunk_free(chunk);

	return retval;
}

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
static bool sctp_stream_outq_is_empty(struct sctp_stream *stream,
				      __u16 str_nums, __be16 *str_list)
{
	struct sctp_association *asoc;
	__u16 i;

	asoc = container_of(stream, struct sctp_association, stream);
	if (!asoc->outqueue.out_qlen)
		return true;

	if (!str_nums)
		return false;

	for (i = 0; i < str_nums; i++) {
		__u16 sid = ntohs(str_list[i]);

320 321
		if (SCTP_SO(stream, sid)->ext &&
		    !list_empty(&SCTP_SO(stream, sid)->ext->outq))
322 323 324 325 326 327
			return false;
	}

	return true;
}

328 329 330
int sctp_send_reset_streams(struct sctp_association *asoc,
			    struct sctp_reset_streams *params)
{
331
	struct sctp_stream *stream = &asoc->stream;
332 333 334
	__u16 i, str_nums, *str_list;
	struct sctp_chunk *chunk;
	int retval = -EINVAL;
335
	__be16 *nstr_list;
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
	bool out, in;

	if (!asoc->peer.reconf_capable ||
	    !(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ)) {
		retval = -ENOPROTOOPT;
		goto out;
	}

	if (asoc->strreset_outstanding) {
		retval = -EINPROGRESS;
		goto out;
	}

	out = params->srs_flags & SCTP_STREAM_RESET_OUTGOING;
	in  = params->srs_flags & SCTP_STREAM_RESET_INCOMING;
	if (!out && !in)
		goto out;

	str_nums = params->srs_number_streams;
	str_list = params->srs_stream_list;
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
	if (str_nums) {
		int param_len = 0;

		if (out) {
			for (i = 0; i < str_nums; i++)
				if (str_list[i] >= stream->outcnt)
					goto out;

			param_len = str_nums * sizeof(__u16) +
				    sizeof(struct sctp_strreset_outreq);
		}

		if (in) {
			for (i = 0; i < str_nums; i++)
				if (str_list[i] >= stream->incnt)
					goto out;

			param_len += str_nums * sizeof(__u16) +
				     sizeof(struct sctp_strreset_inreq);
		}

		if (param_len > SCTP_MAX_CHUNK_LEN -
				sizeof(struct sctp_reconf_chunk))
			goto out;
	}
381

382 383 384 385 386 387
	nstr_list = kcalloc(str_nums, sizeof(__be16), GFP_KERNEL);
	if (!nstr_list) {
		retval = -ENOMEM;
		goto out;
	}

388
	for (i = 0; i < str_nums; i++)
389
		nstr_list[i] = htons(str_list[i]);
390

391 392 393 394 395
	if (out && !sctp_stream_outq_is_empty(stream, str_nums, nstr_list)) {
		retval = -EAGAIN;
		goto out;
	}

396
	chunk = sctp_make_strreset_req(asoc, str_nums, nstr_list, out, in);
397

398
	kfree(nstr_list);
399

400 401
	if (!chunk) {
		retval = -ENOMEM;
402
		goto out;
403
	}
404 405 406 407

	if (out) {
		if (str_nums)
			for (i = 0; i < str_nums; i++)
408
				SCTP_SO(stream, str_list[i])->state =
409 410 411
						       SCTP_STREAM_CLOSED;
		else
			for (i = 0; i < stream->outcnt; i++)
412
				SCTP_SO(stream, i)->state = SCTP_STREAM_CLOSED;
413 414 415 416 417 418 419 420 421
	}

	asoc->strreset_chunk = chunk;
	sctp_chunk_hold(asoc->strreset_chunk);

	retval = sctp_send_reconf(asoc, chunk);
	if (retval) {
		sctp_chunk_put(asoc->strreset_chunk);
		asoc->strreset_chunk = NULL;
422 423 424 425 426
		if (!out)
			goto out;

		if (str_nums)
			for (i = 0; i < str_nums; i++)
427
				SCTP_SO(stream, str_list[i])->state =
428 429 430
						       SCTP_STREAM_OPEN;
		else
			for (i = 0; i < stream->outcnt; i++)
431
				SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
432 433

		goto out;
434 435
	}

436 437
	asoc->strreset_outstanding = out + in;

438 439 440
out:
	return retval;
}
441 442 443

int sctp_send_reset_assoc(struct sctp_association *asoc)
{
444
	struct sctp_stream *stream = &asoc->stream;
445 446 447 448 449 450 451 452 453 454 455
	struct sctp_chunk *chunk = NULL;
	int retval;
	__u16 i;

	if (!asoc->peer.reconf_capable ||
	    !(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
		return -ENOPROTOOPT;

	if (asoc->strreset_outstanding)
		return -EINPROGRESS;

456 457 458
	if (!sctp_outq_is_empty(&asoc->outqueue))
		return -EAGAIN;

459 460 461 462 463
	chunk = sctp_make_strreset_tsnreq(asoc);
	if (!chunk)
		return -ENOMEM;

	/* Block further xmit of data until this request is completed */
464
	for (i = 0; i < stream->outcnt; i++)
465
		SCTP_SO(stream, i)->state = SCTP_STREAM_CLOSED;
466 467 468 469 470 471 472 473 474

	asoc->strreset_chunk = chunk;
	sctp_chunk_hold(asoc->strreset_chunk);

	retval = sctp_send_reconf(asoc, chunk);
	if (retval) {
		sctp_chunk_put(asoc->strreset_chunk);
		asoc->strreset_chunk = NULL;

475
		for (i = 0; i < stream->outcnt; i++)
476
			SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
477 478 479 480 481 482 483 484

		return retval;
	}

	asoc->strreset_outstanding = 1;

	return 0;
}
485 486 487 488

int sctp_send_add_streams(struct sctp_association *asoc,
			  struct sctp_add_streams *params)
{
489
	struct sctp_stream *stream = &asoc->stream;
490
	struct sctp_chunk *chunk = NULL;
491
	int retval;
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
	__u32 outcnt, incnt;
	__u16 out, in;

	if (!asoc->peer.reconf_capable ||
	    !(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
		retval = -ENOPROTOOPT;
		goto out;
	}

	if (asoc->strreset_outstanding) {
		retval = -EINPROGRESS;
		goto out;
	}

	out = params->sas_outstrms;
	in  = params->sas_instrms;
	outcnt = stream->outcnt + out;
	incnt = stream->incnt + in;
	if (outcnt > SCTP_MAX_STREAM || incnt > SCTP_MAX_STREAM ||
	    (!out && !in)) {
		retval = -EINVAL;
		goto out;
	}

	if (out) {
517 518
		retval = sctp_stream_alloc_out(stream, outcnt, GFP_KERNEL);
		if (retval)
519 520 521 522
			goto out;
	}

	chunk = sctp_make_strreset_addstrm(asoc, out, in);
523 524
	if (!chunk) {
		retval = -ENOMEM;
525
		goto out;
526
	}
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544

	asoc->strreset_chunk = chunk;
	sctp_chunk_hold(asoc->strreset_chunk);

	retval = sctp_send_reconf(asoc, chunk);
	if (retval) {
		sctp_chunk_put(asoc->strreset_chunk);
		asoc->strreset_chunk = NULL;
		goto out;
	}

	stream->outcnt = outcnt;

	asoc->strreset_outstanding = !!out + !!in;

out:
	return retval;
}
545

546
static struct sctp_paramhdr *sctp_chunk_lookup_strreset_param(
547
			struct sctp_association *asoc, __be32 resp_seq,
548
			__be16 type)
549 550 551 552 553
{
	struct sctp_chunk *chunk = asoc->strreset_chunk;
	struct sctp_reconf_chunk *hdr;
	union sctp_params param;

554
	if (!chunk)
555 556 557 558 559 560 561 562 563 564
		return NULL;

	hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
	sctp_walk_params(param, hdr, params) {
		/* sctp_strreset_tsnreq is actually the basic structure
		 * of all stream reconf params, so it's safe to use it
		 * to access request_seq.
		 */
		struct sctp_strreset_tsnreq *req = param.v;

565 566
		if ((!resp_seq || req->request_seq == resp_seq) &&
		    (!type || type == req->param_hdr.type))
567 568 569 570 571 572
			return param.v;
	}

	return NULL;
}

573 574 575 576 577 578 579
static void sctp_update_strreset_result(struct sctp_association *asoc,
					__u32 result)
{
	asoc->strreset_result[1] = asoc->strreset_result[0];
	asoc->strreset_result[0] = result;
}

580 581 582 583 584 585
struct sctp_chunk *sctp_process_strreset_outreq(
				struct sctp_association *asoc,
				union sctp_params param,
				struct sctp_ulpevent **evp)
{
	struct sctp_strreset_outreq *outreq = param.v;
586
	struct sctp_stream *stream = &asoc->stream;
587
	__u32 result = SCTP_STRRESET_DENIED;
588 589
	__u16 i, nums, flags = 0;
	__be16 *str_p = NULL;
590 591 592 593 594 595 596
	__u32 request_seq;

	request_seq = ntohl(outreq->request_seq);

	if (ntohl(outreq->send_reset_at_tsn) >
	    sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map)) {
		result = SCTP_STRRESET_IN_PROGRESS;
597
		goto err;
598 599
	}

600 601
	if (TSN_lt(asoc->strreset_inseq, request_seq) ||
	    TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
602
		result = SCTP_STRRESET_ERR_BAD_SEQNO;
603 604 605 606 607
		goto err;
	} else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
		i = asoc->strreset_inseq - request_seq - 1;
		result = asoc->strreset_result[i];
		goto err;
608
	}
609
	asoc->strreset_inseq++;
610 611 612 613 614 615 616 617 618

	/* Check strreset_enable after inseq inc, as sender cannot tell
	 * the peer doesn't enable strreset after receiving response with
	 * result denied, as well as to keep consistent with bsd.
	 */
	if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
		goto out;

	if (asoc->strreset_chunk) {
619 620 621
		if (!sctp_chunk_lookup_strreset_param(
				asoc, outreq->response_seq,
				SCTP_PARAM_RESET_IN_REQUEST)) {
622 623 624 625 626 627 628 629 630
			/* same process with outstanding isn't 0 */
			result = SCTP_STRRESET_ERR_IN_PROGRESS;
			goto out;
		}

		asoc->strreset_outstanding--;
		asoc->strreset_outseq++;

		if (!asoc->strreset_outstanding) {
631 632
			struct sctp_transport *t;

633 634 635 636 637 638 639 640 641 642 643
			t = asoc->strreset_chunk->transport;
			if (del_timer(&t->reconf_timer))
				sctp_transport_put(t);

			sctp_chunk_put(asoc->strreset_chunk);
			asoc->strreset_chunk = NULL;
		}

		flags = SCTP_STREAM_RESET_INCOMING_SSN;
	}

644
	nums = (ntohs(param.p->length) - sizeof(*outreq)) / sizeof(__u16);
645 646 647 648 649 650 651 652 653 654
	if (nums) {
		str_p = outreq->list_of_streams;
		for (i = 0; i < nums; i++) {
			if (ntohs(str_p[i]) >= stream->incnt) {
				result = SCTP_STRRESET_ERR_WRONG_SSN;
				goto out;
			}
		}

		for (i = 0; i < nums; i++)
655
			SCTP_SI(stream, ntohs(str_p[i]))->mid = 0;
656 657
	} else {
		for (i = 0; i < stream->incnt; i++)
658
			SCTP_SI(stream, i)->mid = 0;
659 660 661 662 663 664 665 666 667
	}

	result = SCTP_STRRESET_PERFORMED;

	*evp = sctp_ulpevent_make_stream_reset_event(asoc,
		flags | SCTP_STREAM_RESET_OUTGOING_SSN, nums, str_p,
		GFP_ATOMIC);

out:
668 669
	sctp_update_strreset_result(asoc, result);
err:
670 671
	return sctp_make_strreset_resp(asoc, result, request_seq);
}
672 673 674 675 676 677 678

struct sctp_chunk *sctp_process_strreset_inreq(
				struct sctp_association *asoc,
				union sctp_params param,
				struct sctp_ulpevent **evp)
{
	struct sctp_strreset_inreq *inreq = param.v;
679
	struct sctp_stream *stream = &asoc->stream;
680 681 682
	__u32 result = SCTP_STRRESET_DENIED;
	struct sctp_chunk *chunk = NULL;
	__u32 request_seq;
683 684
	__u16 i, nums;
	__be16 *str_p;
685 686

	request_seq = ntohl(inreq->request_seq);
687 688
	if (TSN_lt(asoc->strreset_inseq, request_seq) ||
	    TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
689
		result = SCTP_STRRESET_ERR_BAD_SEQNO;
690 691 692 693 694 695 696
		goto err;
	} else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
		i = asoc->strreset_inseq - request_seq - 1;
		result = asoc->strreset_result[i];
		if (result == SCTP_STRRESET_PERFORMED)
			return NULL;
		goto err;
697
	}
698
	asoc->strreset_inseq++;
699 700 701 702 703 704 705 706 707

	if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
		goto out;

	if (asoc->strreset_outstanding) {
		result = SCTP_STRRESET_ERR_IN_PROGRESS;
		goto out;
	}

708
	nums = (ntohs(param.p->length) - sizeof(*inreq)) / sizeof(__u16);
709 710 711 712 713 714 715 716
	str_p = inreq->list_of_streams;
	for (i = 0; i < nums; i++) {
		if (ntohs(str_p[i]) >= stream->outcnt) {
			result = SCTP_STRRESET_ERR_WRONG_SSN;
			goto out;
		}
	}

717 718 719 720 721 722
	if (!sctp_stream_outq_is_empty(stream, nums, str_p)) {
		result = SCTP_STRRESET_IN_PROGRESS;
		asoc->strreset_inseq--;
		goto err;
	}

723 724 725 726 727 728
	chunk = sctp_make_strreset_req(asoc, nums, str_p, 1, 0);
	if (!chunk)
		goto out;

	if (nums)
		for (i = 0; i < nums; i++)
729
			SCTP_SO(stream, ntohs(str_p[i]))->state =
730 731 732
					       SCTP_STREAM_CLOSED;
	else
		for (i = 0; i < stream->outcnt; i++)
733
			SCTP_SO(stream, i)->state = SCTP_STREAM_CLOSED;
734 735 736 737 738

	asoc->strreset_chunk = chunk;
	asoc->strreset_outstanding = 1;
	sctp_chunk_hold(asoc->strreset_chunk);

739 740
	result = SCTP_STRRESET_PERFORMED;

741 742 743 744
	*evp = sctp_ulpevent_make_stream_reset_event(asoc,
		SCTP_STREAM_RESET_INCOMING_SSN, nums, str_p, GFP_ATOMIC);

out:
745 746
	sctp_update_strreset_result(asoc, result);
err:
747 748 749 750 751
	if (!chunk)
		chunk =  sctp_make_strreset_resp(asoc, result, request_seq);

	return chunk;
}
752 753 754 755 756 757 758 759

struct sctp_chunk *sctp_process_strreset_tsnreq(
				struct sctp_association *asoc,
				union sctp_params param,
				struct sctp_ulpevent **evp)
{
	__u32 init_tsn = 0, next_tsn = 0, max_tsn_seen;
	struct sctp_strreset_tsnreq *tsnreq = param.v;
760
	struct sctp_stream *stream = &asoc->stream;
761 762 763 764 765
	__u32 result = SCTP_STRRESET_DENIED;
	__u32 request_seq;
	__u16 i;

	request_seq = ntohl(tsnreq->request_seq);
766 767
	if (TSN_lt(asoc->strreset_inseq, request_seq) ||
	    TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
768
		result = SCTP_STRRESET_ERR_BAD_SEQNO;
769 770 771 772 773
		goto err;
	} else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
		i = asoc->strreset_inseq - request_seq - 1;
		result = asoc->strreset_result[i];
		if (result == SCTP_STRRESET_PERFORMED) {
774
			next_tsn = asoc->ctsn_ack_point + 1;
775 776 777 778
			init_tsn =
				sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + 1;
		}
		goto err;
779
	}
780 781 782 783 784 785

	if (!sctp_outq_is_empty(&asoc->outqueue)) {
		result = SCTP_STRRESET_IN_PROGRESS;
		goto err;
	}

786
	asoc->strreset_inseq++;
787 788 789 790 791 792 793 794 795

	if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
		goto out;

	if (asoc->strreset_outstanding) {
		result = SCTP_STRRESET_ERR_IN_PROGRESS;
		goto out;
	}

796 797 798 799
	/* G4: The same processing as though a FWD-TSN chunk (as defined in
	 *     [RFC3758]) with all streams affected and a new cumulative TSN
	 *     ACK of the Receiver's Next TSN minus 1 were received MUST be
	 *     performed.
800 801
	 */
	max_tsn_seen = sctp_tsnmap_get_max_tsn_seen(&asoc->peer.tsn_map);
802
	asoc->stream.si->report_ftsn(&asoc->ulpq, max_tsn_seen);
803 804 805 806 807 808 809 810 811 812

	/* G1: Compute an appropriate value for the Receiver's Next TSN -- the
	 *     TSN that the peer should use to send the next DATA chunk.  The
	 *     value SHOULD be the smallest TSN not acknowledged by the
	 *     receiver of the request plus 2^31.
	 */
	init_tsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + (1 << 31);
	sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
			 init_tsn, GFP_ATOMIC);

813 814 815
	/* G3: The same processing as though a SACK chunk with no gap report
	 *     and a cumulative TSN ACK of the Sender's Next TSN minus 1 were
	 *     received MUST be performed.
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
	 */
	sctp_outq_free(&asoc->outqueue);

	/* G2: Compute an appropriate value for the local endpoint's next TSN,
	 *     i.e., the next TSN assigned by the receiver of the SSN/TSN reset
	 *     chunk.  The value SHOULD be the highest TSN sent by the receiver
	 *     of the request plus 1.
	 */
	next_tsn = asoc->next_tsn;
	asoc->ctsn_ack_point = next_tsn - 1;
	asoc->adv_peer_ack_point = asoc->ctsn_ack_point;

	/* G5:  The next expected and outgoing SSNs MUST be reset to 0 for all
	 *      incoming and outgoing streams.
	 */
831
	for (i = 0; i < stream->outcnt; i++) {
832 833
		SCTP_SO(stream, i)->mid = 0;
		SCTP_SO(stream, i)->mid_uo = 0;
834
	}
835
	for (i = 0; i < stream->incnt; i++)
836
		SCTP_SI(stream, i)->mid = 0;
837 838 839 840 841 842 843

	result = SCTP_STRRESET_PERFORMED;

	*evp = sctp_ulpevent_make_assoc_reset_event(asoc, 0, init_tsn,
						    next_tsn, GFP_ATOMIC);

out:
844 845
	sctp_update_strreset_result(asoc, result);
err:
846 847 848
	return sctp_make_strreset_tsnresp(asoc, result, request_seq,
					  next_tsn, init_tsn);
}
849 850 851 852 853 854 855

struct sctp_chunk *sctp_process_strreset_addstrm_out(
				struct sctp_association *asoc,
				union sctp_params param,
				struct sctp_ulpevent **evp)
{
	struct sctp_strreset_addstrm *addstrm = param.v;
856
	struct sctp_stream *stream = &asoc->stream;
857 858
	__u32 result = SCTP_STRRESET_DENIED;
	__u32 request_seq, incnt;
859
	__u16 in, i;
860 861

	request_seq = ntohl(addstrm->request_seq);
862 863
	if (TSN_lt(asoc->strreset_inseq, request_seq) ||
	    TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
864
		result = SCTP_STRRESET_ERR_BAD_SEQNO;
865 866 867 868 869
		goto err;
	} else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
		i = asoc->strreset_inseq - request_seq - 1;
		result = asoc->strreset_result[i];
		goto err;
870
	}
871
	asoc->strreset_inseq++;
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903

	if (!(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ))
		goto out;

	if (asoc->strreset_chunk) {
		if (!sctp_chunk_lookup_strreset_param(
			asoc, 0, SCTP_PARAM_RESET_ADD_IN_STREAMS)) {
			/* same process with outstanding isn't 0 */
			result = SCTP_STRRESET_ERR_IN_PROGRESS;
			goto out;
		}

		asoc->strreset_outstanding--;
		asoc->strreset_outseq++;

		if (!asoc->strreset_outstanding) {
			struct sctp_transport *t;

			t = asoc->strreset_chunk->transport;
			if (del_timer(&t->reconf_timer))
				sctp_transport_put(t);

			sctp_chunk_put(asoc->strreset_chunk);
			asoc->strreset_chunk = NULL;
		}
	}

	in = ntohs(addstrm->number_of_streams);
	incnt = stream->incnt + in;
	if (!in || incnt > SCTP_MAX_STREAM)
		goto out;

904
	if (sctp_stream_alloc_in(stream, incnt, GFP_ATOMIC))
905 906 907 908 909 910 911 912 913 914
		goto out;

	stream->incnt = incnt;

	result = SCTP_STRRESET_PERFORMED;

	*evp = sctp_ulpevent_make_stream_change_event(asoc,
		0, ntohs(addstrm->number_of_streams), 0, GFP_ATOMIC);

out:
915 916
	sctp_update_strreset_result(asoc, result);
err:
917 918
	return sctp_make_strreset_resp(asoc, result, request_seq);
}
919 920 921 922 923 924 925

struct sctp_chunk *sctp_process_strreset_addstrm_in(
				struct sctp_association *asoc,
				union sctp_params param,
				struct sctp_ulpevent **evp)
{
	struct sctp_strreset_addstrm *addstrm = param.v;
926
	struct sctp_stream *stream = &asoc->stream;
927 928 929
	__u32 result = SCTP_STRRESET_DENIED;
	struct sctp_chunk *chunk = NULL;
	__u32 request_seq, outcnt;
930
	__u16 out, i;
931
	int ret;
932 933

	request_seq = ntohl(addstrm->request_seq);
934 935
	if (TSN_lt(asoc->strreset_inseq, request_seq) ||
	    TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
936
		result = SCTP_STRRESET_ERR_BAD_SEQNO;
937 938 939 940 941 942 943
		goto err;
	} else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
		i = asoc->strreset_inseq - request_seq - 1;
		result = asoc->strreset_result[i];
		if (result == SCTP_STRRESET_PERFORMED)
			return NULL;
		goto err;
944
	}
945
	asoc->strreset_inseq++;
946 947 948 949 950 951 952 953 954 955 956 957 958 959

	if (!(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ))
		goto out;

	if (asoc->strreset_outstanding) {
		result = SCTP_STRRESET_ERR_IN_PROGRESS;
		goto out;
	}

	out = ntohs(addstrm->number_of_streams);
	outcnt = stream->outcnt + out;
	if (!out || outcnt > SCTP_MAX_STREAM)
		goto out;

960 961
	ret = sctp_stream_alloc_out(stream, outcnt, GFP_ATOMIC);
	if (ret)
962 963 964 965 966 967 968 969 970 971 972 973
		goto out;

	chunk = sctp_make_strreset_addstrm(asoc, out, 0);
	if (!chunk)
		goto out;

	asoc->strreset_chunk = chunk;
	asoc->strreset_outstanding = 1;
	sctp_chunk_hold(asoc->strreset_chunk);

	stream->outcnt = outcnt;

974 975
	result = SCTP_STRRESET_PERFORMED;

976 977 978 979
	*evp = sctp_ulpevent_make_stream_change_event(asoc,
		0, 0, ntohs(addstrm->number_of_streams), GFP_ATOMIC);

out:
980 981
	sctp_update_strreset_result(asoc, result);
err:
982 983 984 985 986
	if (!chunk)
		chunk = sctp_make_strreset_resp(asoc, result, request_seq);

	return chunk;
}
987 988 989 990 991 992

struct sctp_chunk *sctp_process_strreset_resp(
				struct sctp_association *asoc,
				union sctp_params param,
				struct sctp_ulpevent **evp)
{
993
	struct sctp_stream *stream = &asoc->stream;
994 995 996
	struct sctp_strreset_resp *resp = param.v;
	struct sctp_transport *t;
	__u16 i, nums, flags = 0;
997
	struct sctp_paramhdr *req;
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
	__u32 result;

	req = sctp_chunk_lookup_strreset_param(asoc, resp->response_seq, 0);
	if (!req)
		return NULL;

	result = ntohl(resp->result);
	if (result != SCTP_STRRESET_PERFORMED) {
		/* if in progress, do nothing but retransmit */
		if (result == SCTP_STRRESET_IN_PROGRESS)
			return NULL;
		else if (result == SCTP_STRRESET_DENIED)
			flags = SCTP_STREAM_RESET_DENIED;
		else
			flags = SCTP_STREAM_RESET_FAILED;
	}

	if (req->type == SCTP_PARAM_RESET_OUT_REQUEST) {
		struct sctp_strreset_outreq *outreq;
1017
		__be16 *str_p;
1018 1019

		outreq = (struct sctp_strreset_outreq *)req;
1020
		str_p = outreq->list_of_streams;
1021 1022
		nums = (ntohs(outreq->param_hdr.length) - sizeof(*outreq)) /
		       sizeof(__u16);
1023 1024

		if (result == SCTP_STRRESET_PERFORMED) {
1025
			struct sctp_stream_out *sout;
1026
			if (nums) {
1027
				for (i = 0; i < nums; i++) {
1028 1029 1030
					sout = SCTP_SO(stream, ntohs(str_p[i]));
					sout->mid = 0;
					sout->mid_uo = 0;
1031
				}
1032
			} else {
1033
				for (i = 0; i < stream->outcnt; i++) {
1034 1035 1036
					sout = SCTP_SO(stream, i);
					sout->mid = 0;
					sout->mid_uo = 0;
1037
				}
1038 1039 1040 1041 1042 1043
			}

			flags = SCTP_STREAM_RESET_OUTGOING_SSN;
		}

		for (i = 0; i < stream->outcnt; i++)
1044
			SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
1045 1046 1047 1048 1049

		*evp = sctp_ulpevent_make_stream_reset_event(asoc, flags,
			nums, str_p, GFP_ATOMIC);
	} else if (req->type == SCTP_PARAM_RESET_IN_REQUEST) {
		struct sctp_strreset_inreq *inreq;
1050
		__be16 *str_p;
1051 1052 1053 1054 1055 1056

		/* if the result is performed, it's impossible for inreq */
		if (result == SCTP_STRRESET_PERFORMED)
			return NULL;

		inreq = (struct sctp_strreset_inreq *)req;
1057
		str_p = inreq->list_of_streams;
1058 1059
		nums = (ntohs(inreq->param_hdr.length) - sizeof(*inreq)) /
		       sizeof(__u16);
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077

		*evp = sctp_ulpevent_make_stream_reset_event(asoc, flags,
			nums, str_p, GFP_ATOMIC);
	} else if (req->type == SCTP_PARAM_RESET_TSN_REQUEST) {
		struct sctp_strreset_resptsn *resptsn;
		__u32 stsn, rtsn;

		/* check for resptsn, as sctp_verify_reconf didn't do it*/
		if (ntohs(param.p->length) != sizeof(*resptsn))
			return NULL;

		resptsn = (struct sctp_strreset_resptsn *)resp;
		stsn = ntohl(resptsn->senders_next_tsn);
		rtsn = ntohl(resptsn->receivers_next_tsn);

		if (result == SCTP_STRRESET_PERFORMED) {
			__u32 mtsn = sctp_tsnmap_get_max_tsn_seen(
						&asoc->peer.tsn_map);
1078
			LIST_HEAD(temp);
1079

1080
			asoc->stream.si->report_ftsn(&asoc->ulpq, mtsn);
1081 1082 1083 1084 1085

			sctp_tsnmap_init(&asoc->peer.tsn_map,
					 SCTP_TSN_MAP_INITIAL,
					 stsn, GFP_ATOMIC);

1086 1087 1088 1089 1090
			/* Clean up sacked and abandoned queues only. As the
			 * out_chunk_list may not be empty, splice it to temp,
			 * then get it back after sctp_outq_free is done.
			 */
			list_splice_init(&asoc->outqueue.out_chunk_list, &temp);
1091
			sctp_outq_free(&asoc->outqueue);
1092
			list_splice_init(&temp, &asoc->outqueue.out_chunk_list);
1093 1094 1095 1096 1097

			asoc->next_tsn = rtsn;
			asoc->ctsn_ack_point = asoc->next_tsn - 1;
			asoc->adv_peer_ack_point = asoc->ctsn_ack_point;

1098
			for (i = 0; i < stream->outcnt; i++) {
1099 1100
				SCTP_SO(stream, i)->mid = 0;
				SCTP_SO(stream, i)->mid_uo = 0;
1101
			}
1102
			for (i = 0; i < stream->incnt; i++)
1103
				SCTP_SI(stream, i)->mid = 0;
1104 1105 1106
		}

		for (i = 0; i < stream->outcnt; i++)
1107
			SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120

		*evp = sctp_ulpevent_make_assoc_reset_event(asoc, flags,
			stsn, rtsn, GFP_ATOMIC);
	} else if (req->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS) {
		struct sctp_strreset_addstrm *addstrm;
		__u16 number;

		addstrm = (struct sctp_strreset_addstrm *)req;
		nums = ntohs(addstrm->number_of_streams);
		number = stream->outcnt - nums;

		if (result == SCTP_STRRESET_PERFORMED)
			for (i = number; i < stream->outcnt; i++)
1121
				SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
		else
			stream->outcnt = number;

		*evp = sctp_ulpevent_make_stream_change_event(asoc, flags,
			0, nums, GFP_ATOMIC);
	} else if (req->type == SCTP_PARAM_RESET_ADD_IN_STREAMS) {
		struct sctp_strreset_addstrm *addstrm;

		/* if the result is performed, it's impossible for addstrm in
		 * request.
		 */
		if (result == SCTP_STRRESET_PERFORMED)
			return NULL;

		addstrm = (struct sctp_strreset_addstrm *)req;
		nums = ntohs(addstrm->number_of_streams);

		*evp = sctp_ulpevent_make_stream_change_event(asoc, flags,
			nums, 0, GFP_ATOMIC);
	}

	asoc->strreset_outstanding--;
	asoc->strreset_outseq++;

	/* remove everything for this reconf request */
	if (!asoc->strreset_outstanding) {
		t = asoc->strreset_chunk->transport;
		if (del_timer(&t->reconf_timer))
			sctp_transport_put(t);

		sctp_chunk_put(asoc->strreset_chunk);
		asoc->strreset_chunk = NULL;
	}

	return NULL;
}