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 /* 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.
29 static void sctp_stream_outq_migrate(struct sctp_stream
*stream
,
30 struct sctp_stream
*new, __u16 outcnt
)
32 struct sctp_association
*asoc
;
33 struct sctp_chunk
*ch
, *temp
;
34 struct sctp_outq
*outq
;
37 asoc
= container_of(stream
, struct sctp_association
, stream
);
38 outq
= &asoc
->outqueue
;
40 list_for_each_entry_safe(ch
, temp
, &outq
->out_chunk_list
, list
) {
41 __u16 sid
= sctp_chunk_stream_no(ch
);
46 sctp_sched_dequeue_common(outq
, ch
);
47 /* No need to call dequeue_done here because
48 * the chunks are not scheduled by now.
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
--;
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.
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
;
72 for (i
= outcnt
; i
< stream
->outcnt
; i
++) {
73 kfree(SCTP_SO(stream
, i
)->ext
);
74 SCTP_SO(stream
, i
)->ext
= NULL
;
78 static int sctp_stream_alloc_out(struct sctp_stream
*stream
, __u16 outcnt
,
83 if (outcnt
<= stream
->outcnt
)
86 ret
= genradix_prealloc(&stream
->out
, outcnt
, gfp
);
90 stream
->outcnt
= outcnt
;
94 static int sctp_stream_alloc_in(struct sctp_stream
*stream
, __u16 incnt
,
99 if (incnt
<= stream
->incnt
)
102 ret
= genradix_prealloc(&stream
->in
, incnt
, gfp
);
106 stream
->incnt
= incnt
;
110 int sctp_stream_init(struct sctp_stream
*stream
, __u16 outcnt
, __u16 incnt
,
113 struct sctp_sched_ops
*sched
= sctp_sched_ops_from_stream(stream
);
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.
121 if (outcnt
== stream
->outcnt
)
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
);
129 ret
= sctp_stream_alloc_out(stream
, outcnt
, gfp
);
133 for (i
= 0; i
< stream
->outcnt
; i
++)
134 SCTP_SO(stream
, i
)->state
= SCTP_STREAM_OPEN
;
137 sctp_stream_interleave_init(stream
);
141 ret
= sctp_stream_alloc_in(stream
, incnt
, gfp
);
144 genradix_free(&stream
->out
);
153 int sctp_stream_init_ext(struct sctp_stream
*stream
, __u16 sid
)
155 struct sctp_stream_out_ext
*soute
;
158 soute
= kzalloc(sizeof(*soute
), GFP_KERNEL
);
161 SCTP_SO(stream
, sid
)->ext
= soute
;
163 ret
= sctp_sched_init_sid(stream
, sid
, GFP_KERNEL
);
165 kfree(SCTP_SO(stream
, sid
)->ext
);
166 SCTP_SO(stream
, sid
)->ext
= NULL
;
172 void sctp_stream_free(struct sctp_stream
*stream
)
174 struct sctp_sched_ops
*sched
= sctp_sched_ops_from_stream(stream
);
178 for (i
= 0; i
< stream
->outcnt
; i
++)
179 kfree(SCTP_SO(stream
, i
)->ext
);
180 genradix_free(&stream
->out
);
181 genradix_free(&stream
->in
);
184 void sctp_stream_clear(struct sctp_stream
*stream
)
188 for (i
= 0; i
< stream
->outcnt
; i
++) {
189 SCTP_SO(stream
, i
)->mid
= 0;
190 SCTP_SO(stream
, i
)->mid_uo
= 0;
193 for (i
= 0; i
< stream
->incnt
; i
++)
194 SCTP_SI(stream
, i
)->mid
= 0;
197 void sctp_stream_update(struct sctp_stream
*stream
, struct sctp_stream
*new)
199 struct sctp_sched_ops
*sched
= sctp_sched_ops_from_stream(stream
);
201 sched
->unsched_all(stream
);
202 sctp_stream_outq_migrate(stream
, new, new->outcnt
);
203 sctp_stream_free(stream
);
205 stream
->out
= new->out
;
206 stream
->in
= new->in
;
207 stream
->outcnt
= new->outcnt
;
208 stream
->incnt
= new->incnt
;
210 sched
->sched_all(stream
);
212 new->out
.tree
.root
= NULL
;
213 new->in
.tree
.root
= NULL
;
218 static int sctp_send_reconf(struct sctp_association
*asoc
,
219 struct sctp_chunk
*chunk
)
221 struct net
*net
= sock_net(asoc
->base
.sk
);
224 retval
= sctp_primitive_RECONF(net
, asoc
, chunk
);
226 sctp_chunk_free(chunk
);
231 static bool sctp_stream_outq_is_empty(struct sctp_stream
*stream
,
232 __u16 str_nums
, __be16
*str_list
)
234 struct sctp_association
*asoc
;
237 asoc
= container_of(stream
, struct sctp_association
, stream
);
238 if (!asoc
->outqueue
.out_qlen
)
244 for (i
= 0; i
< str_nums
; i
++) {
245 __u16 sid
= ntohs(str_list
[i
]);
247 if (SCTP_SO(stream
, sid
)->ext
&&
248 !list_empty(&SCTP_SO(stream
, sid
)->ext
->outq
))
255 int sctp_send_reset_streams(struct sctp_association
*asoc
,
256 struct sctp_reset_streams
*params
)
258 struct sctp_stream
*stream
= &asoc
->stream
;
259 __u16 i
, str_nums
, *str_list
;
260 struct sctp_chunk
*chunk
;
261 int retval
= -EINVAL
;
265 if (!asoc
->peer
.reconf_capable
||
266 !(asoc
->strreset_enable
& SCTP_ENABLE_RESET_STREAM_REQ
)) {
267 retval
= -ENOPROTOOPT
;
271 if (asoc
->strreset_outstanding
) {
272 retval
= -EINPROGRESS
;
276 out
= params
->srs_flags
& SCTP_STREAM_RESET_OUTGOING
;
277 in
= params
->srs_flags
& SCTP_STREAM_RESET_INCOMING
;
281 str_nums
= params
->srs_number_streams
;
282 str_list
= params
->srs_stream_list
;
287 for (i
= 0; i
< str_nums
; i
++)
288 if (str_list
[i
] >= stream
->outcnt
)
291 param_len
= str_nums
* sizeof(__u16
) +
292 sizeof(struct sctp_strreset_outreq
);
296 for (i
= 0; i
< str_nums
; i
++)
297 if (str_list
[i
] >= stream
->incnt
)
300 param_len
+= str_nums
* sizeof(__u16
) +
301 sizeof(struct sctp_strreset_inreq
);
304 if (param_len
> SCTP_MAX_CHUNK_LEN
-
305 sizeof(struct sctp_reconf_chunk
))
309 nstr_list
= kcalloc(str_nums
, sizeof(__be16
), GFP_KERNEL
);
315 for (i
= 0; i
< str_nums
; i
++)
316 nstr_list
[i
] = htons(str_list
[i
]);
318 if (out
&& !sctp_stream_outq_is_empty(stream
, str_nums
, nstr_list
)) {
324 chunk
= sctp_make_strreset_req(asoc
, str_nums
, nstr_list
, out
, in
);
335 for (i
= 0; i
< str_nums
; i
++)
336 SCTP_SO(stream
, str_list
[i
])->state
=
339 for (i
= 0; i
< stream
->outcnt
; i
++)
340 SCTP_SO(stream
, i
)->state
= SCTP_STREAM_CLOSED
;
343 asoc
->strreset_chunk
= chunk
;
344 sctp_chunk_hold(asoc
->strreset_chunk
);
346 retval
= sctp_send_reconf(asoc
, chunk
);
348 sctp_chunk_put(asoc
->strreset_chunk
);
349 asoc
->strreset_chunk
= NULL
;
354 for (i
= 0; i
< str_nums
; i
++)
355 SCTP_SO(stream
, str_list
[i
])->state
=
358 for (i
= 0; i
< stream
->outcnt
; i
++)
359 SCTP_SO(stream
, i
)->state
= SCTP_STREAM_OPEN
;
364 asoc
->strreset_outstanding
= out
+ in
;
370 int sctp_send_reset_assoc(struct sctp_association
*asoc
)
372 struct sctp_stream
*stream
= &asoc
->stream
;
373 struct sctp_chunk
*chunk
= NULL
;
377 if (!asoc
->peer
.reconf_capable
||
378 !(asoc
->strreset_enable
& SCTP_ENABLE_RESET_ASSOC_REQ
))
381 if (asoc
->strreset_outstanding
)
384 if (!sctp_outq_is_empty(&asoc
->outqueue
))
387 chunk
= sctp_make_strreset_tsnreq(asoc
);
391 /* Block further xmit of data until this request is completed */
392 for (i
= 0; i
< stream
->outcnt
; i
++)
393 SCTP_SO(stream
, i
)->state
= SCTP_STREAM_CLOSED
;
395 asoc
->strreset_chunk
= chunk
;
396 sctp_chunk_hold(asoc
->strreset_chunk
);
398 retval
= sctp_send_reconf(asoc
, chunk
);
400 sctp_chunk_put(asoc
->strreset_chunk
);
401 asoc
->strreset_chunk
= NULL
;
403 for (i
= 0; i
< stream
->outcnt
; i
++)
404 SCTP_SO(stream
, i
)->state
= SCTP_STREAM_OPEN
;
409 asoc
->strreset_outstanding
= 1;
414 int sctp_send_add_streams(struct sctp_association
*asoc
,
415 struct sctp_add_streams
*params
)
417 struct sctp_stream
*stream
= &asoc
->stream
;
418 struct sctp_chunk
*chunk
= NULL
;
423 if (!asoc
->peer
.reconf_capable
||
424 !(asoc
->strreset_enable
& SCTP_ENABLE_CHANGE_ASSOC_REQ
)) {
425 retval
= -ENOPROTOOPT
;
429 if (asoc
->strreset_outstanding
) {
430 retval
= -EINPROGRESS
;
434 out
= params
->sas_outstrms
;
435 in
= params
->sas_instrms
;
436 outcnt
= stream
->outcnt
+ out
;
437 incnt
= stream
->incnt
+ in
;
438 if (outcnt
> SCTP_MAX_STREAM
|| incnt
> SCTP_MAX_STREAM
||
445 retval
= sctp_stream_alloc_out(stream
, outcnt
, GFP_KERNEL
);
450 chunk
= sctp_make_strreset_addstrm(asoc
, out
, in
);
456 asoc
->strreset_chunk
= chunk
;
457 sctp_chunk_hold(asoc
->strreset_chunk
);
459 retval
= sctp_send_reconf(asoc
, chunk
);
461 sctp_chunk_put(asoc
->strreset_chunk
);
462 asoc
->strreset_chunk
= NULL
;
466 asoc
->strreset_outstanding
= !!out
+ !!in
;
472 static struct sctp_paramhdr
*sctp_chunk_lookup_strreset_param(
473 struct sctp_association
*asoc
, __be32 resp_seq
,
476 struct sctp_chunk
*chunk
= asoc
->strreset_chunk
;
477 struct sctp_reconf_chunk
*hdr
;
478 union sctp_params param
;
483 hdr
= (struct sctp_reconf_chunk
*)chunk
->chunk_hdr
;
484 sctp_walk_params(param
, hdr
, params
) {
485 /* sctp_strreset_tsnreq is actually the basic structure
486 * of all stream reconf params, so it's safe to use it
487 * to access request_seq.
489 struct sctp_strreset_tsnreq
*req
= param
.v
;
491 if ((!resp_seq
|| req
->request_seq
== resp_seq
) &&
492 (!type
|| type
== req
->param_hdr
.type
))
499 static void sctp_update_strreset_result(struct sctp_association
*asoc
,
502 asoc
->strreset_result
[1] = asoc
->strreset_result
[0];
503 asoc
->strreset_result
[0] = result
;
506 struct sctp_chunk
*sctp_process_strreset_outreq(
507 struct sctp_association
*asoc
,
508 union sctp_params param
,
509 struct sctp_ulpevent
**evp
)
511 struct sctp_strreset_outreq
*outreq
= param
.v
;
512 struct sctp_stream
*stream
= &asoc
->stream
;
513 __u32 result
= SCTP_STRRESET_DENIED
;
514 __be16
*str_p
= NULL
;
518 request_seq
= ntohl(outreq
->request_seq
);
520 if (ntohl(outreq
->send_reset_at_tsn
) >
521 sctp_tsnmap_get_ctsn(&asoc
->peer
.tsn_map
)) {
522 result
= SCTP_STRRESET_IN_PROGRESS
;
526 if (TSN_lt(asoc
->strreset_inseq
, request_seq
) ||
527 TSN_lt(request_seq
, asoc
->strreset_inseq
- 2)) {
528 result
= SCTP_STRRESET_ERR_BAD_SEQNO
;
530 } else if (TSN_lt(request_seq
, asoc
->strreset_inseq
)) {
531 i
= asoc
->strreset_inseq
- request_seq
- 1;
532 result
= asoc
->strreset_result
[i
];
535 asoc
->strreset_inseq
++;
537 /* Check strreset_enable after inseq inc, as sender cannot tell
538 * the peer doesn't enable strreset after receiving response with
539 * result denied, as well as to keep consistent with bsd.
541 if (!(asoc
->strreset_enable
& SCTP_ENABLE_RESET_STREAM_REQ
))
544 nums
= (ntohs(param
.p
->length
) - sizeof(*outreq
)) / sizeof(__u16
);
545 str_p
= outreq
->list_of_streams
;
546 for (i
= 0; i
< nums
; i
++) {
547 if (ntohs(str_p
[i
]) >= stream
->incnt
) {
548 result
= SCTP_STRRESET_ERR_WRONG_SSN
;
553 if (asoc
->strreset_chunk
) {
554 if (!sctp_chunk_lookup_strreset_param(
555 asoc
, outreq
->response_seq
,
556 SCTP_PARAM_RESET_IN_REQUEST
)) {
557 /* same process with outstanding isn't 0 */
558 result
= SCTP_STRRESET_ERR_IN_PROGRESS
;
562 asoc
->strreset_outstanding
--;
563 asoc
->strreset_outseq
++;
565 if (!asoc
->strreset_outstanding
) {
566 struct sctp_transport
*t
;
568 t
= asoc
->strreset_chunk
->transport
;
569 if (del_timer(&t
->reconf_timer
))
570 sctp_transport_put(t
);
572 sctp_chunk_put(asoc
->strreset_chunk
);
573 asoc
->strreset_chunk
= NULL
;
578 for (i
= 0; i
< nums
; i
++)
579 SCTP_SI(stream
, ntohs(str_p
[i
]))->mid
= 0;
581 for (i
= 0; i
< stream
->incnt
; i
++)
582 SCTP_SI(stream
, i
)->mid
= 0;
584 result
= SCTP_STRRESET_PERFORMED
;
586 *evp
= sctp_ulpevent_make_stream_reset_event(asoc
,
587 SCTP_STREAM_RESET_INCOMING_SSN
, nums
, str_p
, GFP_ATOMIC
);
590 sctp_update_strreset_result(asoc
, result
);
592 return sctp_make_strreset_resp(asoc
, result
, request_seq
);
595 struct sctp_chunk
*sctp_process_strreset_inreq(
596 struct sctp_association
*asoc
,
597 union sctp_params param
,
598 struct sctp_ulpevent
**evp
)
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
;
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
;
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
)
620 asoc
->strreset_inseq
++;
622 if (!(asoc
->strreset_enable
& SCTP_ENABLE_RESET_STREAM_REQ
))
625 if (asoc
->strreset_outstanding
) {
626 result
= SCTP_STRRESET_ERR_IN_PROGRESS
;
630 nums
= (ntohs(param
.p
->length
) - sizeof(*inreq
)) / sizeof(__u16
);
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
;
639 if (!sctp_stream_outq_is_empty(stream
, nums
, str_p
)) {
640 result
= SCTP_STRRESET_IN_PROGRESS
;
641 asoc
->strreset_inseq
--;
645 chunk
= sctp_make_strreset_req(asoc
, nums
, str_p
, 1, 0);
650 for (i
= 0; i
< nums
; i
++)
651 SCTP_SO(stream
, ntohs(str_p
[i
]))->state
=
654 for (i
= 0; i
< stream
->outcnt
; i
++)
655 SCTP_SO(stream
, i
)->state
= SCTP_STREAM_CLOSED
;
657 asoc
->strreset_chunk
= chunk
;
658 asoc
->strreset_outstanding
= 1;
659 sctp_chunk_hold(asoc
->strreset_chunk
);
661 result
= SCTP_STRRESET_PERFORMED
;
664 sctp_update_strreset_result(asoc
, result
);
667 chunk
= sctp_make_strreset_resp(asoc
, result
, request_seq
);
672 struct sctp_chunk
*sctp_process_strreset_tsnreq(
673 struct sctp_association
*asoc
,
674 union sctp_params param
,
675 struct sctp_ulpevent
**evp
)
677 __u32 init_tsn
= 0, next_tsn
= 0, max_tsn_seen
;
678 struct sctp_strreset_tsnreq
*tsnreq
= param
.v
;
679 struct sctp_stream
*stream
= &asoc
->stream
;
680 __u32 result
= SCTP_STRRESET_DENIED
;
684 request_seq
= ntohl(tsnreq
->request_seq
);
685 if (TSN_lt(asoc
->strreset_inseq
, request_seq
) ||
686 TSN_lt(request_seq
, asoc
->strreset_inseq
- 2)) {
687 result
= SCTP_STRRESET_ERR_BAD_SEQNO
;
689 } else if (TSN_lt(request_seq
, asoc
->strreset_inseq
)) {
690 i
= asoc
->strreset_inseq
- request_seq
- 1;
691 result
= asoc
->strreset_result
[i
];
692 if (result
== SCTP_STRRESET_PERFORMED
) {
693 next_tsn
= asoc
->ctsn_ack_point
+ 1;
695 sctp_tsnmap_get_ctsn(&asoc
->peer
.tsn_map
) + 1;
700 if (!sctp_outq_is_empty(&asoc
->outqueue
)) {
701 result
= SCTP_STRRESET_IN_PROGRESS
;
705 asoc
->strreset_inseq
++;
707 if (!(asoc
->strreset_enable
& SCTP_ENABLE_RESET_ASSOC_REQ
))
710 if (asoc
->strreset_outstanding
) {
711 result
= SCTP_STRRESET_ERR_IN_PROGRESS
;
715 /* G4: The same processing as though a FWD-TSN chunk (as defined in
716 * [RFC3758]) with all streams affected and a new cumulative TSN
717 * ACK of the Receiver's Next TSN minus 1 were received MUST be
720 max_tsn_seen
= sctp_tsnmap_get_max_tsn_seen(&asoc
->peer
.tsn_map
);
721 asoc
->stream
.si
->report_ftsn(&asoc
->ulpq
, max_tsn_seen
);
723 /* G1: Compute an appropriate value for the Receiver's Next TSN -- the
724 * TSN that the peer should use to send the next DATA chunk. The
725 * value SHOULD be the smallest TSN not acknowledged by the
726 * receiver of the request plus 2^31.
728 init_tsn
= sctp_tsnmap_get_ctsn(&asoc
->peer
.tsn_map
) + (1 << 31);
729 sctp_tsnmap_init(&asoc
->peer
.tsn_map
, SCTP_TSN_MAP_INITIAL
,
730 init_tsn
, GFP_ATOMIC
);
732 /* G3: The same processing as though a SACK chunk with no gap report
733 * and a cumulative TSN ACK of the Sender's Next TSN minus 1 were
734 * received MUST be performed.
736 sctp_outq_free(&asoc
->outqueue
);
738 /* G2: Compute an appropriate value for the local endpoint's next TSN,
739 * i.e., the next TSN assigned by the receiver of the SSN/TSN reset
740 * chunk. The value SHOULD be the highest TSN sent by the receiver
741 * of the request plus 1.
743 next_tsn
= asoc
->next_tsn
;
744 asoc
->ctsn_ack_point
= next_tsn
- 1;
745 asoc
->adv_peer_ack_point
= asoc
->ctsn_ack_point
;
747 /* G5: The next expected and outgoing SSNs MUST be reset to 0 for all
748 * incoming and outgoing streams.
750 for (i
= 0; i
< stream
->outcnt
; i
++) {
751 SCTP_SO(stream
, i
)->mid
= 0;
752 SCTP_SO(stream
, i
)->mid_uo
= 0;
754 for (i
= 0; i
< stream
->incnt
; i
++)
755 SCTP_SI(stream
, i
)->mid
= 0;
757 result
= SCTP_STRRESET_PERFORMED
;
759 *evp
= sctp_ulpevent_make_assoc_reset_event(asoc
, 0, init_tsn
,
760 next_tsn
, GFP_ATOMIC
);
763 sctp_update_strreset_result(asoc
, result
);
765 return sctp_make_strreset_tsnresp(asoc
, result
, request_seq
,
769 struct sctp_chunk
*sctp_process_strreset_addstrm_out(
770 struct sctp_association
*asoc
,
771 union sctp_params param
,
772 struct sctp_ulpevent
**evp
)
774 struct sctp_strreset_addstrm
*addstrm
= param
.v
;
775 struct sctp_stream
*stream
= &asoc
->stream
;
776 __u32 result
= SCTP_STRRESET_DENIED
;
777 __u32 request_seq
, incnt
;
780 request_seq
= ntohl(addstrm
->request_seq
);
781 if (TSN_lt(asoc
->strreset_inseq
, request_seq
) ||
782 TSN_lt(request_seq
, asoc
->strreset_inseq
- 2)) {
783 result
= SCTP_STRRESET_ERR_BAD_SEQNO
;
785 } else if (TSN_lt(request_seq
, asoc
->strreset_inseq
)) {
786 i
= asoc
->strreset_inseq
- request_seq
- 1;
787 result
= asoc
->strreset_result
[i
];
790 asoc
->strreset_inseq
++;
792 if (!(asoc
->strreset_enable
& SCTP_ENABLE_CHANGE_ASSOC_REQ
))
795 in
= ntohs(addstrm
->number_of_streams
);
796 incnt
= stream
->incnt
+ in
;
797 if (!in
|| incnt
> SCTP_MAX_STREAM
)
800 if (sctp_stream_alloc_in(stream
, incnt
, GFP_ATOMIC
))
803 if (asoc
->strreset_chunk
) {
804 if (!sctp_chunk_lookup_strreset_param(
805 asoc
, 0, SCTP_PARAM_RESET_ADD_IN_STREAMS
)) {
806 /* same process with outstanding isn't 0 */
807 result
= SCTP_STRRESET_ERR_IN_PROGRESS
;
811 asoc
->strreset_outstanding
--;
812 asoc
->strreset_outseq
++;
814 if (!asoc
->strreset_outstanding
) {
815 struct sctp_transport
*t
;
817 t
= asoc
->strreset_chunk
->transport
;
818 if (del_timer(&t
->reconf_timer
))
819 sctp_transport_put(t
);
821 sctp_chunk_put(asoc
->strreset_chunk
);
822 asoc
->strreset_chunk
= NULL
;
826 stream
->incnt
= incnt
;
828 result
= SCTP_STRRESET_PERFORMED
;
830 *evp
= sctp_ulpevent_make_stream_change_event(asoc
,
831 0, ntohs(addstrm
->number_of_streams
), 0, GFP_ATOMIC
);
834 sctp_update_strreset_result(asoc
, result
);
836 return sctp_make_strreset_resp(asoc
, result
, request_seq
);
839 struct sctp_chunk
*sctp_process_strreset_addstrm_in(
840 struct sctp_association
*asoc
,
841 union sctp_params param
,
842 struct sctp_ulpevent
**evp
)
844 struct sctp_strreset_addstrm
*addstrm
= param
.v
;
845 struct sctp_stream
*stream
= &asoc
->stream
;
846 __u32 result
= SCTP_STRRESET_DENIED
;
847 struct sctp_chunk
*chunk
= NULL
;
848 __u32 request_seq
, outcnt
;
852 request_seq
= ntohl(addstrm
->request_seq
);
853 if (TSN_lt(asoc
->strreset_inseq
, request_seq
) ||
854 TSN_lt(request_seq
, asoc
->strreset_inseq
- 2)) {
855 result
= SCTP_STRRESET_ERR_BAD_SEQNO
;
857 } else if (TSN_lt(request_seq
, asoc
->strreset_inseq
)) {
858 i
= asoc
->strreset_inseq
- request_seq
- 1;
859 result
= asoc
->strreset_result
[i
];
860 if (result
== SCTP_STRRESET_PERFORMED
)
864 asoc
->strreset_inseq
++;
866 if (!(asoc
->strreset_enable
& SCTP_ENABLE_CHANGE_ASSOC_REQ
))
869 if (asoc
->strreset_outstanding
) {
870 result
= SCTP_STRRESET_ERR_IN_PROGRESS
;
874 out
= ntohs(addstrm
->number_of_streams
);
875 outcnt
= stream
->outcnt
+ out
;
876 if (!out
|| outcnt
> SCTP_MAX_STREAM
)
879 ret
= sctp_stream_alloc_out(stream
, outcnt
, GFP_ATOMIC
);
883 chunk
= sctp_make_strreset_addstrm(asoc
, out
, 0);
887 asoc
->strreset_chunk
= chunk
;
888 asoc
->strreset_outstanding
= 1;
889 sctp_chunk_hold(asoc
->strreset_chunk
);
891 stream
->outcnt
= outcnt
;
893 result
= SCTP_STRRESET_PERFORMED
;
896 sctp_update_strreset_result(asoc
, result
);
899 chunk
= sctp_make_strreset_resp(asoc
, result
, request_seq
);
904 struct sctp_chunk
*sctp_process_strreset_resp(
905 struct sctp_association
*asoc
,
906 union sctp_params param
,
907 struct sctp_ulpevent
**evp
)
909 struct sctp_stream
*stream
= &asoc
->stream
;
910 struct sctp_strreset_resp
*resp
= param
.v
;
911 struct sctp_transport
*t
;
912 __u16 i
, nums
, flags
= 0;
913 struct sctp_paramhdr
*req
;
916 req
= sctp_chunk_lookup_strreset_param(asoc
, resp
->response_seq
, 0);
920 result
= ntohl(resp
->result
);
921 if (result
!= SCTP_STRRESET_PERFORMED
) {
922 /* if in progress, do nothing but retransmit */
923 if (result
== SCTP_STRRESET_IN_PROGRESS
)
925 else if (result
== SCTP_STRRESET_DENIED
)
926 flags
= SCTP_STREAM_RESET_DENIED
;
928 flags
= SCTP_STREAM_RESET_FAILED
;
931 if (req
->type
== SCTP_PARAM_RESET_OUT_REQUEST
) {
932 struct sctp_strreset_outreq
*outreq
;
935 outreq
= (struct sctp_strreset_outreq
*)req
;
936 str_p
= outreq
->list_of_streams
;
937 nums
= (ntohs(outreq
->param_hdr
.length
) - sizeof(*outreq
)) /
940 if (result
== SCTP_STRRESET_PERFORMED
) {
941 struct sctp_stream_out
*sout
;
943 for (i
= 0; i
< nums
; i
++) {
944 sout
= SCTP_SO(stream
, ntohs(str_p
[i
]));
949 for (i
= 0; i
< stream
->outcnt
; i
++) {
950 sout
= SCTP_SO(stream
, i
);
957 flags
|= SCTP_STREAM_RESET_OUTGOING_SSN
;
959 for (i
= 0; i
< stream
->outcnt
; i
++)
960 SCTP_SO(stream
, i
)->state
= SCTP_STREAM_OPEN
;
962 *evp
= sctp_ulpevent_make_stream_reset_event(asoc
, flags
,
963 nums
, str_p
, GFP_ATOMIC
);
964 } else if (req
->type
== SCTP_PARAM_RESET_IN_REQUEST
) {
965 struct sctp_strreset_inreq
*inreq
;
968 /* if the result is performed, it's impossible for inreq */
969 if (result
== SCTP_STRRESET_PERFORMED
)
972 inreq
= (struct sctp_strreset_inreq
*)req
;
973 str_p
= inreq
->list_of_streams
;
974 nums
= (ntohs(inreq
->param_hdr
.length
) - sizeof(*inreq
)) /
977 flags
|= SCTP_STREAM_RESET_INCOMING_SSN
;
979 *evp
= sctp_ulpevent_make_stream_reset_event(asoc
, flags
,
980 nums
, str_p
, GFP_ATOMIC
);
981 } else if (req
->type
== SCTP_PARAM_RESET_TSN_REQUEST
) {
982 struct sctp_strreset_resptsn
*resptsn
;
985 /* check for resptsn, as sctp_verify_reconf didn't do it*/
986 if (ntohs(param
.p
->length
) != sizeof(*resptsn
))
989 resptsn
= (struct sctp_strreset_resptsn
*)resp
;
990 stsn
= ntohl(resptsn
->senders_next_tsn
);
991 rtsn
= ntohl(resptsn
->receivers_next_tsn
);
993 if (result
== SCTP_STRRESET_PERFORMED
) {
994 __u32 mtsn
= sctp_tsnmap_get_max_tsn_seen(
995 &asoc
->peer
.tsn_map
);
998 asoc
->stream
.si
->report_ftsn(&asoc
->ulpq
, mtsn
);
1000 sctp_tsnmap_init(&asoc
->peer
.tsn_map
,
1001 SCTP_TSN_MAP_INITIAL
,
1004 /* Clean up sacked and abandoned queues only. As the
1005 * out_chunk_list may not be empty, splice it to temp,
1006 * then get it back after sctp_outq_free is done.
1008 list_splice_init(&asoc
->outqueue
.out_chunk_list
, &temp
);
1009 sctp_outq_free(&asoc
->outqueue
);
1010 list_splice_init(&temp
, &asoc
->outqueue
.out_chunk_list
);
1012 asoc
->next_tsn
= rtsn
;
1013 asoc
->ctsn_ack_point
= asoc
->next_tsn
- 1;
1014 asoc
->adv_peer_ack_point
= asoc
->ctsn_ack_point
;
1016 for (i
= 0; i
< stream
->outcnt
; i
++) {
1017 SCTP_SO(stream
, i
)->mid
= 0;
1018 SCTP_SO(stream
, i
)->mid_uo
= 0;
1020 for (i
= 0; i
< stream
->incnt
; i
++)
1021 SCTP_SI(stream
, i
)->mid
= 0;
1024 for (i
= 0; i
< stream
->outcnt
; i
++)
1025 SCTP_SO(stream
, i
)->state
= SCTP_STREAM_OPEN
;
1027 *evp
= sctp_ulpevent_make_assoc_reset_event(asoc
, flags
,
1028 stsn
, rtsn
, GFP_ATOMIC
);
1029 } else if (req
->type
== SCTP_PARAM_RESET_ADD_OUT_STREAMS
) {
1030 struct sctp_strreset_addstrm
*addstrm
;
1033 addstrm
= (struct sctp_strreset_addstrm
*)req
;
1034 nums
= ntohs(addstrm
->number_of_streams
);
1035 number
= stream
->outcnt
- nums
;
1037 if (result
== SCTP_STRRESET_PERFORMED
)
1038 for (i
= number
; i
< stream
->outcnt
; i
++)
1039 SCTP_SO(stream
, i
)->state
= SCTP_STREAM_OPEN
;
1041 stream
->outcnt
= number
;
1043 *evp
= sctp_ulpevent_make_stream_change_event(asoc
, flags
,
1044 0, nums
, GFP_ATOMIC
);
1045 } else if (req
->type
== SCTP_PARAM_RESET_ADD_IN_STREAMS
) {
1046 struct sctp_strreset_addstrm
*addstrm
;
1048 /* if the result is performed, it's impossible for addstrm in
1051 if (result
== SCTP_STRRESET_PERFORMED
)
1054 addstrm
= (struct sctp_strreset_addstrm
*)req
;
1055 nums
= ntohs(addstrm
->number_of_streams
);
1057 *evp
= sctp_ulpevent_make_stream_change_event(asoc
, flags
,
1058 nums
, 0, GFP_ATOMIC
);
1061 asoc
->strreset_outstanding
--;
1062 asoc
->strreset_outseq
++;
1064 /* remove everything for this reconf request */
1065 if (!asoc
->strreset_outstanding
) {
1066 t
= asoc
->strreset_chunk
->transport
;
1067 if (del_timer(&t
->reconf_timer
))
1068 sctp_transport_put(t
);
1070 sctp_chunk_put(asoc
->strreset_chunk
);
1071 asoc
->strreset_chunk
= NULL
;