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.
8 * This file is part of the SCTP kernel implementation
10 * This file contains sctp stream maniuplation primitives and helpers.
12 * Please send any bug reports or fixes you make to the
14 * lksctp developers <linux-sctp@vger.kernel.org>
16 * Written or modified by:
17 * Xin Long <lucien.xin@gmail.com>
20 #include <linux/list.h>
21 #include <net/sctp/sctp.h>
22 #include <net/sctp/sm.h>
23 #include <net/sctp/stream_sched.h>
25 static void sctp_stream_shrink_out(struct sctp_stream *stream, __u16 outcnt)
27 struct sctp_association *asoc;
28 struct sctp_chunk *ch, *temp;
29 struct sctp_outq *outq;
31 asoc = container_of(stream, struct sctp_association, stream);
32 outq = &asoc->outqueue;
34 list_for_each_entry_safe(ch, temp, &outq->out_chunk_list, list) {
35 __u16 sid = sctp_chunk_stream_no(ch);
40 sctp_sched_dequeue_common(outq, ch);
41 /* No need to call dequeue_done here because
42 * the chunks are not scheduled by now.
45 /* Mark as failed send. */
46 sctp_chunk_fail(ch, (__force __u32)SCTP_ERROR_INV_STRM);
47 if (asoc->peer.prsctp_capable &&
48 SCTP_PR_PRIO_ENABLED(ch->sinfo.sinfo_flags))
49 asoc->sent_cnt_removable--;
55 static void sctp_stream_free_ext(struct sctp_stream *stream, __u16 sid)
57 struct sctp_sched_ops *sched;
59 if (!SCTP_SO(stream, sid)->ext)
62 sched = sctp_sched_ops_from_stream(stream);
63 sched->free_sid(stream, sid);
64 kfree(SCTP_SO(stream, sid)->ext);
65 SCTP_SO(stream, sid)->ext = NULL;
68 /* Migrates chunks from stream queues to new stream queues if needed,
69 * but not across associations. Also, removes those chunks to streams
70 * higher than the new max.
72 static void sctp_stream_outq_migrate(struct sctp_stream *stream,
73 struct sctp_stream *new, __u16 outcnt)
77 if (stream->outcnt > outcnt)
78 sctp_stream_shrink_out(stream, outcnt);
81 /* Here we actually move the old ext stuff into the new
82 * buffer, because we want to keep it. Then
83 * sctp_stream_update will swap ->out pointers.
85 for (i = 0; i < outcnt; i++) {
86 sctp_stream_free_ext(new, i);
87 SCTP_SO(new, i)->ext = SCTP_SO(stream, i)->ext;
88 SCTP_SO(stream, i)->ext = NULL;
92 for (i = outcnt; i < stream->outcnt; i++)
93 sctp_stream_free_ext(stream, i);
96 static int sctp_stream_alloc_out(struct sctp_stream *stream, __u16 outcnt,
101 if (outcnt <= stream->outcnt)
104 ret = genradix_prealloc(&stream->out, outcnt, gfp);
109 stream->outcnt = outcnt;
113 static int sctp_stream_alloc_in(struct sctp_stream *stream, __u16 incnt,
118 if (incnt <= stream->incnt)
121 ret = genradix_prealloc(&stream->in, incnt, gfp);
126 stream->incnt = incnt;
130 int sctp_stream_init(struct sctp_stream *stream, __u16 outcnt, __u16 incnt,
133 struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
138 /* Initial stream->out size may be very big, so free it and alloc
139 * a new one with new outcnt to save memory if needed.
141 if (outcnt == stream->outcnt)
144 /* Filter out chunks queued on streams that won't exist anymore */
145 sched->unsched_all(stream);
146 sctp_stream_outq_migrate(stream, NULL, outcnt);
147 sched->sched_all(stream);
149 ret = sctp_stream_alloc_out(stream, outcnt, gfp);
153 for (i = 0; i < stream->outcnt; i++)
154 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
157 sctp_stream_interleave_init(stream);
161 return sctp_stream_alloc_in(stream, incnt, gfp);
164 int sctp_stream_init_ext(struct sctp_stream *stream, __u16 sid)
166 struct sctp_stream_out_ext *soute;
169 soute = kzalloc(sizeof(*soute), GFP_KERNEL);
172 SCTP_SO(stream, sid)->ext = soute;
174 ret = sctp_sched_init_sid(stream, sid, GFP_KERNEL);
176 kfree(SCTP_SO(stream, sid)->ext);
177 SCTP_SO(stream, sid)->ext = NULL;
183 void sctp_stream_free(struct sctp_stream *stream)
185 struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
188 sched->unsched_all(stream);
189 for (i = 0; i < stream->outcnt; i++)
190 sctp_stream_free_ext(stream, i);
191 genradix_free(&stream->out);
192 genradix_free(&stream->in);
195 void sctp_stream_clear(struct sctp_stream *stream)
199 for (i = 0; i < stream->outcnt; i++) {
200 SCTP_SO(stream, i)->mid = 0;
201 SCTP_SO(stream, i)->mid_uo = 0;
204 for (i = 0; i < stream->incnt; i++)
205 SCTP_SI(stream, i)->mid = 0;
208 void sctp_stream_update(struct sctp_stream *stream, struct sctp_stream *new)
210 struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
212 sched->unsched_all(stream);
213 sctp_stream_outq_migrate(stream, new, new->outcnt);
214 sctp_stream_free(stream);
216 stream->out = new->out;
217 stream->in = new->in;
218 stream->outcnt = new->outcnt;
219 stream->incnt = new->incnt;
221 sched->sched_all(stream);
223 new->out.tree.root = NULL;
224 new->in.tree.root = NULL;
229 static int sctp_send_reconf(struct sctp_association *asoc,
230 struct sctp_chunk *chunk)
234 retval = sctp_primitive_RECONF(asoc->base.net, asoc, chunk);
236 sctp_chunk_free(chunk);
241 static bool sctp_stream_outq_is_empty(struct sctp_stream *stream,
242 __u16 str_nums, __be16 *str_list)
244 struct sctp_association *asoc;
247 asoc = container_of(stream, struct sctp_association, stream);
248 if (!asoc->outqueue.out_qlen)
254 for (i = 0; i < str_nums; i++) {
255 __u16 sid = ntohs(str_list[i]);
257 if (SCTP_SO(stream, sid)->ext &&
258 !list_empty(&SCTP_SO(stream, sid)->ext->outq))
265 int sctp_send_reset_streams(struct sctp_association *asoc,
266 struct sctp_reset_streams *params)
268 struct sctp_stream *stream = &asoc->stream;
269 __u16 i, str_nums, *str_list;
270 struct sctp_chunk *chunk;
271 int retval = -EINVAL;
275 if (!asoc->peer.reconf_capable ||
276 !(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ)) {
277 retval = -ENOPROTOOPT;
281 if (asoc->strreset_outstanding) {
282 retval = -EINPROGRESS;
286 out = params->srs_flags & SCTP_STREAM_RESET_OUTGOING;
287 in = params->srs_flags & SCTP_STREAM_RESET_INCOMING;
291 str_nums = params->srs_number_streams;
292 str_list = params->srs_stream_list;
297 for (i = 0; i < str_nums; i++)
298 if (str_list[i] >= stream->outcnt)
301 param_len = str_nums * sizeof(__u16) +
302 sizeof(struct sctp_strreset_outreq);
306 for (i = 0; i < str_nums; i++)
307 if (str_list[i] >= stream->incnt)
310 param_len += str_nums * sizeof(__u16) +
311 sizeof(struct sctp_strreset_inreq);
314 if (param_len > SCTP_MAX_CHUNK_LEN -
315 sizeof(struct sctp_reconf_chunk))
319 nstr_list = kcalloc(str_nums, sizeof(__be16), GFP_KERNEL);
325 for (i = 0; i < str_nums; i++)
326 nstr_list[i] = htons(str_list[i]);
328 if (out && !sctp_stream_outq_is_empty(stream, str_nums, nstr_list)) {
334 chunk = sctp_make_strreset_req(asoc, str_nums, nstr_list, out, in);
345 for (i = 0; i < str_nums; i++)
346 SCTP_SO(stream, str_list[i])->state =
349 for (i = 0; i < stream->outcnt; i++)
350 SCTP_SO(stream, i)->state = SCTP_STREAM_CLOSED;
353 asoc->strreset_chunk = chunk;
354 sctp_chunk_hold(asoc->strreset_chunk);
356 retval = sctp_send_reconf(asoc, chunk);
358 sctp_chunk_put(asoc->strreset_chunk);
359 asoc->strreset_chunk = NULL;
364 for (i = 0; i < str_nums; i++)
365 SCTP_SO(stream, str_list[i])->state =
368 for (i = 0; i < stream->outcnt; i++)
369 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
374 asoc->strreset_outstanding = out + in;
380 int sctp_send_reset_assoc(struct sctp_association *asoc)
382 struct sctp_stream *stream = &asoc->stream;
383 struct sctp_chunk *chunk = NULL;
387 if (!asoc->peer.reconf_capable ||
388 !(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
391 if (asoc->strreset_outstanding)
394 if (!sctp_outq_is_empty(&asoc->outqueue))
397 chunk = sctp_make_strreset_tsnreq(asoc);
401 /* Block further xmit of data until this request is completed */
402 for (i = 0; i < stream->outcnt; i++)
403 SCTP_SO(stream, i)->state = SCTP_STREAM_CLOSED;
405 asoc->strreset_chunk = chunk;
406 sctp_chunk_hold(asoc->strreset_chunk);
408 retval = sctp_send_reconf(asoc, chunk);
410 sctp_chunk_put(asoc->strreset_chunk);
411 asoc->strreset_chunk = NULL;
413 for (i = 0; i < stream->outcnt; i++)
414 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
419 asoc->strreset_outstanding = 1;
424 int sctp_send_add_streams(struct sctp_association *asoc,
425 struct sctp_add_streams *params)
427 struct sctp_stream *stream = &asoc->stream;
428 struct sctp_chunk *chunk = NULL;
433 if (!asoc->peer.reconf_capable ||
434 !(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
435 retval = -ENOPROTOOPT;
439 if (asoc->strreset_outstanding) {
440 retval = -EINPROGRESS;
444 out = params->sas_outstrms;
445 in = params->sas_instrms;
446 outcnt = stream->outcnt + out;
447 incnt = stream->incnt + in;
448 if (outcnt > SCTP_MAX_STREAM || incnt > SCTP_MAX_STREAM ||
455 retval = sctp_stream_alloc_out(stream, outcnt, GFP_KERNEL);
460 chunk = sctp_make_strreset_addstrm(asoc, out, in);
466 asoc->strreset_chunk = chunk;
467 sctp_chunk_hold(asoc->strreset_chunk);
469 retval = sctp_send_reconf(asoc, chunk);
471 sctp_chunk_put(asoc->strreset_chunk);
472 asoc->strreset_chunk = NULL;
476 asoc->strreset_outstanding = !!out + !!in;
482 static struct sctp_paramhdr *sctp_chunk_lookup_strreset_param(
483 struct sctp_association *asoc, __be32 resp_seq,
486 struct sctp_chunk *chunk = asoc->strreset_chunk;
487 struct sctp_reconf_chunk *hdr;
488 union sctp_params param;
493 hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
494 sctp_walk_params(param, hdr, params) {
495 /* sctp_strreset_tsnreq is actually the basic structure
496 * of all stream reconf params, so it's safe to use it
497 * to access request_seq.
499 struct sctp_strreset_tsnreq *req = param.v;
501 if ((!resp_seq || req->request_seq == resp_seq) &&
502 (!type || type == req->param_hdr.type))
509 static void sctp_update_strreset_result(struct sctp_association *asoc,
512 asoc->strreset_result[1] = asoc->strreset_result[0];
513 asoc->strreset_result[0] = result;
516 struct sctp_chunk *sctp_process_strreset_outreq(
517 struct sctp_association *asoc,
518 union sctp_params param,
519 struct sctp_ulpevent **evp)
521 struct sctp_strreset_outreq *outreq = param.v;
522 struct sctp_stream *stream = &asoc->stream;
523 __u32 result = SCTP_STRRESET_DENIED;
524 __be16 *str_p = NULL;
528 request_seq = ntohl(outreq->request_seq);
530 if (ntohl(outreq->send_reset_at_tsn) >
531 sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map)) {
532 result = SCTP_STRRESET_IN_PROGRESS;
536 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
537 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
538 result = SCTP_STRRESET_ERR_BAD_SEQNO;
540 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
541 i = asoc->strreset_inseq - request_seq - 1;
542 result = asoc->strreset_result[i];
545 asoc->strreset_inseq++;
547 /* Check strreset_enable after inseq inc, as sender cannot tell
548 * the peer doesn't enable strreset after receiving response with
549 * result denied, as well as to keep consistent with bsd.
551 if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
554 nums = (ntohs(param.p->length) - sizeof(*outreq)) / sizeof(__u16);
555 str_p = outreq->list_of_streams;
556 for (i = 0; i < nums; i++) {
557 if (ntohs(str_p[i]) >= stream->incnt) {
558 result = SCTP_STRRESET_ERR_WRONG_SSN;
563 if (asoc->strreset_chunk) {
564 if (!sctp_chunk_lookup_strreset_param(
565 asoc, outreq->response_seq,
566 SCTP_PARAM_RESET_IN_REQUEST)) {
567 /* same process with outstanding isn't 0 */
568 result = SCTP_STRRESET_ERR_IN_PROGRESS;
572 asoc->strreset_outstanding--;
573 asoc->strreset_outseq++;
575 if (!asoc->strreset_outstanding) {
576 struct sctp_transport *t;
578 t = asoc->strreset_chunk->transport;
579 if (del_timer(&t->reconf_timer))
580 sctp_transport_put(t);
582 sctp_chunk_put(asoc->strreset_chunk);
583 asoc->strreset_chunk = NULL;
588 for (i = 0; i < nums; i++)
589 SCTP_SI(stream, ntohs(str_p[i]))->mid = 0;
591 for (i = 0; i < stream->incnt; i++)
592 SCTP_SI(stream, i)->mid = 0;
594 result = SCTP_STRRESET_PERFORMED;
596 *evp = sctp_ulpevent_make_stream_reset_event(asoc,
597 SCTP_STREAM_RESET_INCOMING_SSN, nums, str_p, GFP_ATOMIC);
600 sctp_update_strreset_result(asoc, result);
602 return sctp_make_strreset_resp(asoc, result, request_seq);
605 struct sctp_chunk *sctp_process_strreset_inreq(
606 struct sctp_association *asoc,
607 union sctp_params param,
608 struct sctp_ulpevent **evp)
610 struct sctp_strreset_inreq *inreq = param.v;
611 struct sctp_stream *stream = &asoc->stream;
612 __u32 result = SCTP_STRRESET_DENIED;
613 struct sctp_chunk *chunk = NULL;
618 request_seq = ntohl(inreq->request_seq);
619 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
620 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
621 result = SCTP_STRRESET_ERR_BAD_SEQNO;
623 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
624 i = asoc->strreset_inseq - request_seq - 1;
625 result = asoc->strreset_result[i];
626 if (result == SCTP_STRRESET_PERFORMED)
630 asoc->strreset_inseq++;
632 if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
635 if (asoc->strreset_outstanding) {
636 result = SCTP_STRRESET_ERR_IN_PROGRESS;
640 nums = (ntohs(param.p->length) - sizeof(*inreq)) / sizeof(__u16);
641 str_p = inreq->list_of_streams;
642 for (i = 0; i < nums; i++) {
643 if (ntohs(str_p[i]) >= stream->outcnt) {
644 result = SCTP_STRRESET_ERR_WRONG_SSN;
649 if (!sctp_stream_outq_is_empty(stream, nums, str_p)) {
650 result = SCTP_STRRESET_IN_PROGRESS;
651 asoc->strreset_inseq--;
655 chunk = sctp_make_strreset_req(asoc, nums, str_p, 1, 0);
660 for (i = 0; i < nums; i++)
661 SCTP_SO(stream, ntohs(str_p[i]))->state =
664 for (i = 0; i < stream->outcnt; i++)
665 SCTP_SO(stream, i)->state = SCTP_STREAM_CLOSED;
667 asoc->strreset_chunk = chunk;
668 asoc->strreset_outstanding = 1;
669 sctp_chunk_hold(asoc->strreset_chunk);
671 result = SCTP_STRRESET_PERFORMED;
674 sctp_update_strreset_result(asoc, result);
677 chunk = sctp_make_strreset_resp(asoc, result, request_seq);
682 struct sctp_chunk *sctp_process_strreset_tsnreq(
683 struct sctp_association *asoc,
684 union sctp_params param,
685 struct sctp_ulpevent **evp)
687 __u32 init_tsn = 0, next_tsn = 0, max_tsn_seen;
688 struct sctp_strreset_tsnreq *tsnreq = param.v;
689 struct sctp_stream *stream = &asoc->stream;
690 __u32 result = SCTP_STRRESET_DENIED;
694 request_seq = ntohl(tsnreq->request_seq);
695 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
696 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
697 result = SCTP_STRRESET_ERR_BAD_SEQNO;
699 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
700 i = asoc->strreset_inseq - request_seq - 1;
701 result = asoc->strreset_result[i];
702 if (result == SCTP_STRRESET_PERFORMED) {
703 next_tsn = asoc->ctsn_ack_point + 1;
705 sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + 1;
710 if (!sctp_outq_is_empty(&asoc->outqueue)) {
711 result = SCTP_STRRESET_IN_PROGRESS;
715 asoc->strreset_inseq++;
717 if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
720 if (asoc->strreset_outstanding) {
721 result = SCTP_STRRESET_ERR_IN_PROGRESS;
725 /* G4: The same processing as though a FWD-TSN chunk (as defined in
726 * [RFC3758]) with all streams affected and a new cumulative TSN
727 * ACK of the Receiver's Next TSN minus 1 were received MUST be
730 max_tsn_seen = sctp_tsnmap_get_max_tsn_seen(&asoc->peer.tsn_map);
731 asoc->stream.si->report_ftsn(&asoc->ulpq, max_tsn_seen);
733 /* G1: Compute an appropriate value for the Receiver's Next TSN -- the
734 * TSN that the peer should use to send the next DATA chunk. The
735 * value SHOULD be the smallest TSN not acknowledged by the
736 * receiver of the request plus 2^31.
738 init_tsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map) + (1 << 31);
739 sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
740 init_tsn, GFP_ATOMIC);
742 /* G3: The same processing as though a SACK chunk with no gap report
743 * and a cumulative TSN ACK of the Sender's Next TSN minus 1 were
744 * received MUST be performed.
746 sctp_outq_free(&asoc->outqueue);
748 /* G2: Compute an appropriate value for the local endpoint's next TSN,
749 * i.e., the next TSN assigned by the receiver of the SSN/TSN reset
750 * chunk. The value SHOULD be the highest TSN sent by the receiver
751 * of the request plus 1.
753 next_tsn = asoc->next_tsn;
754 asoc->ctsn_ack_point = next_tsn - 1;
755 asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
757 /* G5: The next expected and outgoing SSNs MUST be reset to 0 for all
758 * incoming and outgoing streams.
760 for (i = 0; i < stream->outcnt; i++) {
761 SCTP_SO(stream, i)->mid = 0;
762 SCTP_SO(stream, i)->mid_uo = 0;
764 for (i = 0; i < stream->incnt; i++)
765 SCTP_SI(stream, i)->mid = 0;
767 result = SCTP_STRRESET_PERFORMED;
769 *evp = sctp_ulpevent_make_assoc_reset_event(asoc, 0, init_tsn,
770 next_tsn, GFP_ATOMIC);
773 sctp_update_strreset_result(asoc, result);
775 return sctp_make_strreset_tsnresp(asoc, result, request_seq,
779 struct sctp_chunk *sctp_process_strreset_addstrm_out(
780 struct sctp_association *asoc,
781 union sctp_params param,
782 struct sctp_ulpevent **evp)
784 struct sctp_strreset_addstrm *addstrm = param.v;
785 struct sctp_stream *stream = &asoc->stream;
786 __u32 result = SCTP_STRRESET_DENIED;
787 __u32 request_seq, incnt;
790 request_seq = ntohl(addstrm->request_seq);
791 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
792 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
793 result = SCTP_STRRESET_ERR_BAD_SEQNO;
795 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
796 i = asoc->strreset_inseq - request_seq - 1;
797 result = asoc->strreset_result[i];
800 asoc->strreset_inseq++;
802 if (!(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ))
805 in = ntohs(addstrm->number_of_streams);
806 incnt = stream->incnt + in;
807 if (!in || incnt > SCTP_MAX_STREAM)
810 if (sctp_stream_alloc_in(stream, incnt, GFP_ATOMIC))
813 if (asoc->strreset_chunk) {
814 if (!sctp_chunk_lookup_strreset_param(
815 asoc, 0, SCTP_PARAM_RESET_ADD_IN_STREAMS)) {
816 /* same process with outstanding isn't 0 */
817 result = SCTP_STRRESET_ERR_IN_PROGRESS;
821 asoc->strreset_outstanding--;
822 asoc->strreset_outseq++;
824 if (!asoc->strreset_outstanding) {
825 struct sctp_transport *t;
827 t = asoc->strreset_chunk->transport;
828 if (del_timer(&t->reconf_timer))
829 sctp_transport_put(t);
831 sctp_chunk_put(asoc->strreset_chunk);
832 asoc->strreset_chunk = NULL;
836 stream->incnt = incnt;
838 result = SCTP_STRRESET_PERFORMED;
840 *evp = sctp_ulpevent_make_stream_change_event(asoc,
841 0, ntohs(addstrm->number_of_streams), 0, GFP_ATOMIC);
844 sctp_update_strreset_result(asoc, result);
846 return sctp_make_strreset_resp(asoc, result, request_seq);
849 struct sctp_chunk *sctp_process_strreset_addstrm_in(
850 struct sctp_association *asoc,
851 union sctp_params param,
852 struct sctp_ulpevent **evp)
854 struct sctp_strreset_addstrm *addstrm = param.v;
855 struct sctp_stream *stream = &asoc->stream;
856 __u32 result = SCTP_STRRESET_DENIED;
857 struct sctp_chunk *chunk = NULL;
858 __u32 request_seq, outcnt;
862 request_seq = ntohl(addstrm->request_seq);
863 if (TSN_lt(asoc->strreset_inseq, request_seq) ||
864 TSN_lt(request_seq, asoc->strreset_inseq - 2)) {
865 result = SCTP_STRRESET_ERR_BAD_SEQNO;
867 } else if (TSN_lt(request_seq, asoc->strreset_inseq)) {
868 i = asoc->strreset_inseq - request_seq - 1;
869 result = asoc->strreset_result[i];
870 if (result == SCTP_STRRESET_PERFORMED)
874 asoc->strreset_inseq++;
876 if (!(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ))
879 if (asoc->strreset_outstanding) {
880 result = SCTP_STRRESET_ERR_IN_PROGRESS;
884 out = ntohs(addstrm->number_of_streams);
885 outcnt = stream->outcnt + out;
886 if (!out || outcnt > SCTP_MAX_STREAM)
889 ret = sctp_stream_alloc_out(stream, outcnt, GFP_ATOMIC);
893 chunk = sctp_make_strreset_addstrm(asoc, out, 0);
897 asoc->strreset_chunk = chunk;
898 asoc->strreset_outstanding = 1;
899 sctp_chunk_hold(asoc->strreset_chunk);
901 stream->outcnt = outcnt;
903 result = SCTP_STRRESET_PERFORMED;
906 sctp_update_strreset_result(asoc, result);
909 chunk = sctp_make_strreset_resp(asoc, result, request_seq);
914 struct sctp_chunk *sctp_process_strreset_resp(
915 struct sctp_association *asoc,
916 union sctp_params param,
917 struct sctp_ulpevent **evp)
919 struct sctp_stream *stream = &asoc->stream;
920 struct sctp_strreset_resp *resp = param.v;
921 struct sctp_transport *t;
922 __u16 i, nums, flags = 0;
923 struct sctp_paramhdr *req;
926 req = sctp_chunk_lookup_strreset_param(asoc, resp->response_seq, 0);
930 result = ntohl(resp->result);
931 if (result != SCTP_STRRESET_PERFORMED) {
932 /* if in progress, do nothing but retransmit */
933 if (result == SCTP_STRRESET_IN_PROGRESS)
935 else if (result == SCTP_STRRESET_DENIED)
936 flags = SCTP_STREAM_RESET_DENIED;
938 flags = SCTP_STREAM_RESET_FAILED;
941 if (req->type == SCTP_PARAM_RESET_OUT_REQUEST) {
942 struct sctp_strreset_outreq *outreq;
945 outreq = (struct sctp_strreset_outreq *)req;
946 str_p = outreq->list_of_streams;
947 nums = (ntohs(outreq->param_hdr.length) - sizeof(*outreq)) /
950 if (result == SCTP_STRRESET_PERFORMED) {
951 struct sctp_stream_out *sout;
953 for (i = 0; i < nums; i++) {
954 sout = SCTP_SO(stream, ntohs(str_p[i]));
959 for (i = 0; i < stream->outcnt; i++) {
960 sout = SCTP_SO(stream, i);
967 flags |= SCTP_STREAM_RESET_OUTGOING_SSN;
969 for (i = 0; i < stream->outcnt; i++)
970 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
972 *evp = sctp_ulpevent_make_stream_reset_event(asoc, flags,
973 nums, str_p, GFP_ATOMIC);
974 } else if (req->type == SCTP_PARAM_RESET_IN_REQUEST) {
975 struct sctp_strreset_inreq *inreq;
978 /* if the result is performed, it's impossible for inreq */
979 if (result == SCTP_STRRESET_PERFORMED)
982 inreq = (struct sctp_strreset_inreq *)req;
983 str_p = inreq->list_of_streams;
984 nums = (ntohs(inreq->param_hdr.length) - sizeof(*inreq)) /
987 flags |= SCTP_STREAM_RESET_INCOMING_SSN;
989 *evp = sctp_ulpevent_make_stream_reset_event(asoc, flags,
990 nums, str_p, GFP_ATOMIC);
991 } else if (req->type == SCTP_PARAM_RESET_TSN_REQUEST) {
992 struct sctp_strreset_resptsn *resptsn;
995 /* check for resptsn, as sctp_verify_reconf didn't do it*/
996 if (ntohs(param.p->length) != sizeof(*resptsn))
999 resptsn = (struct sctp_strreset_resptsn *)resp;
1000 stsn = ntohl(resptsn->senders_next_tsn);
1001 rtsn = ntohl(resptsn->receivers_next_tsn);
1003 if (result == SCTP_STRRESET_PERFORMED) {
1004 __u32 mtsn = sctp_tsnmap_get_max_tsn_seen(
1005 &asoc->peer.tsn_map);
1008 asoc->stream.si->report_ftsn(&asoc->ulpq, mtsn);
1010 sctp_tsnmap_init(&asoc->peer.tsn_map,
1011 SCTP_TSN_MAP_INITIAL,
1014 /* Clean up sacked and abandoned queues only. As the
1015 * out_chunk_list may not be empty, splice it to temp,
1016 * then get it back after sctp_outq_free is done.
1018 list_splice_init(&asoc->outqueue.out_chunk_list, &temp);
1019 sctp_outq_free(&asoc->outqueue);
1020 list_splice_init(&temp, &asoc->outqueue.out_chunk_list);
1022 asoc->next_tsn = rtsn;
1023 asoc->ctsn_ack_point = asoc->next_tsn - 1;
1024 asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
1026 for (i = 0; i < stream->outcnt; i++) {
1027 SCTP_SO(stream, i)->mid = 0;
1028 SCTP_SO(stream, i)->mid_uo = 0;
1030 for (i = 0; i < stream->incnt; i++)
1031 SCTP_SI(stream, i)->mid = 0;
1034 for (i = 0; i < stream->outcnt; i++)
1035 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
1037 *evp = sctp_ulpevent_make_assoc_reset_event(asoc, flags,
1038 stsn, rtsn, GFP_ATOMIC);
1039 } else if (req->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS) {
1040 struct sctp_strreset_addstrm *addstrm;
1043 addstrm = (struct sctp_strreset_addstrm *)req;
1044 nums = ntohs(addstrm->number_of_streams);
1045 number = stream->outcnt - nums;
1047 if (result == SCTP_STRRESET_PERFORMED) {
1048 for (i = number; i < stream->outcnt; i++)
1049 SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN;
1051 sctp_stream_shrink_out(stream, number);
1052 stream->outcnt = number;
1055 *evp = sctp_ulpevent_make_stream_change_event(asoc, flags,
1056 0, nums, GFP_ATOMIC);
1057 } else if (req->type == SCTP_PARAM_RESET_ADD_IN_STREAMS) {
1058 struct sctp_strreset_addstrm *addstrm;
1060 /* if the result is performed, it's impossible for addstrm in
1063 if (result == SCTP_STRRESET_PERFORMED)
1066 addstrm = (struct sctp_strreset_addstrm *)req;
1067 nums = ntohs(addstrm->number_of_streams);
1069 *evp = sctp_ulpevent_make_stream_change_event(asoc, flags,
1070 nums, 0, GFP_ATOMIC);
1073 asoc->strreset_outstanding--;
1074 asoc->strreset_outseq++;
1076 /* remove everything for this reconf request */
1077 if (!asoc->strreset_outstanding) {
1078 t = asoc->strreset_chunk->transport;
1079 if (del_timer(&t->reconf_timer))
1080 sctp_transport_put(t);
1082 sctp_chunk_put(asoc->strreset_chunk);
1083 asoc->strreset_chunk = NULL;