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
);
149 genradix_free(&stream
->in
);
151 genradix_free(&stream
->out
);
157 int sctp_stream_init_ext(struct sctp_stream
*stream
, __u16 sid
)
159 struct sctp_stream_out_ext
*soute
;
162 soute
= kzalloc(sizeof(*soute
), GFP_KERNEL
);
165 SCTP_SO(stream
, sid
)->ext
= soute
;
167 ret
= sctp_sched_init_sid(stream
, sid
, GFP_KERNEL
);
169 kfree(SCTP_SO(stream
, sid
)->ext
);
170 SCTP_SO(stream
, sid
)->ext
= NULL
;
176 void sctp_stream_free(struct sctp_stream
*stream
)
178 struct sctp_sched_ops
*sched
= sctp_sched_ops_from_stream(stream
);
182 for (i
= 0; i
< stream
->outcnt
; i
++)
183 kfree(SCTP_SO(stream
, i
)->ext
);
184 genradix_free(&stream
->out
);
185 genradix_free(&stream
->in
);
188 void sctp_stream_clear(struct sctp_stream
*stream
)
192 for (i
= 0; i
< stream
->outcnt
; i
++) {
193 SCTP_SO(stream
, i
)->mid
= 0;
194 SCTP_SO(stream
, i
)->mid_uo
= 0;
197 for (i
= 0; i
< stream
->incnt
; i
++)
198 SCTP_SI(stream
, i
)->mid
= 0;
201 void sctp_stream_update(struct sctp_stream
*stream
, struct sctp_stream
*new)
203 struct sctp_sched_ops
*sched
= sctp_sched_ops_from_stream(stream
);
205 sched
->unsched_all(stream
);
206 sctp_stream_outq_migrate(stream
, new, new->outcnt
);
207 sctp_stream_free(stream
);
209 stream
->out
= new->out
;
210 stream
->in
= new->in
;
211 stream
->outcnt
= new->outcnt
;
212 stream
->incnt
= new->incnt
;
214 sched
->sched_all(stream
);
216 new->out
.tree
.root
= NULL
;
217 new->in
.tree
.root
= NULL
;
222 static int sctp_send_reconf(struct sctp_association
*asoc
,
223 struct sctp_chunk
*chunk
)
227 retval
= sctp_primitive_RECONF(asoc
->base
.net
, asoc
, chunk
);
229 sctp_chunk_free(chunk
);
234 static bool sctp_stream_outq_is_empty(struct sctp_stream
*stream
,
235 __u16 str_nums
, __be16
*str_list
)
237 struct sctp_association
*asoc
;
240 asoc
= container_of(stream
, struct sctp_association
, stream
);
241 if (!asoc
->outqueue
.out_qlen
)
247 for (i
= 0; i
< str_nums
; i
++) {
248 __u16 sid
= ntohs(str_list
[i
]);
250 if (SCTP_SO(stream
, sid
)->ext
&&
251 !list_empty(&SCTP_SO(stream
, sid
)->ext
->outq
))
258 int sctp_send_reset_streams(struct sctp_association
*asoc
,
259 struct sctp_reset_streams
*params
)
261 struct sctp_stream
*stream
= &asoc
->stream
;
262 __u16 i
, str_nums
, *str_list
;
263 struct sctp_chunk
*chunk
;
264 int retval
= -EINVAL
;
268 if (!asoc
->peer
.reconf_capable
||
269 !(asoc
->strreset_enable
& SCTP_ENABLE_RESET_STREAM_REQ
)) {
270 retval
= -ENOPROTOOPT
;
274 if (asoc
->strreset_outstanding
) {
275 retval
= -EINPROGRESS
;
279 out
= params
->srs_flags
& SCTP_STREAM_RESET_OUTGOING
;
280 in
= params
->srs_flags
& SCTP_STREAM_RESET_INCOMING
;
284 str_nums
= params
->srs_number_streams
;
285 str_list
= params
->srs_stream_list
;
290 for (i
= 0; i
< str_nums
; i
++)
291 if (str_list
[i
] >= stream
->outcnt
)
294 param_len
= str_nums
* sizeof(__u16
) +
295 sizeof(struct sctp_strreset_outreq
);
299 for (i
= 0; i
< str_nums
; i
++)
300 if (str_list
[i
] >= stream
->incnt
)
303 param_len
+= str_nums
* sizeof(__u16
) +
304 sizeof(struct sctp_strreset_inreq
);
307 if (param_len
> SCTP_MAX_CHUNK_LEN
-
308 sizeof(struct sctp_reconf_chunk
))
312 nstr_list
= kcalloc(str_nums
, sizeof(__be16
), GFP_KERNEL
);
318 for (i
= 0; i
< str_nums
; i
++)
319 nstr_list
[i
] = htons(str_list
[i
]);
321 if (out
&& !sctp_stream_outq_is_empty(stream
, str_nums
, nstr_list
)) {
327 chunk
= sctp_make_strreset_req(asoc
, str_nums
, nstr_list
, out
, in
);
338 for (i
= 0; i
< str_nums
; i
++)
339 SCTP_SO(stream
, str_list
[i
])->state
=
342 for (i
= 0; i
< stream
->outcnt
; i
++)
343 SCTP_SO(stream
, i
)->state
= SCTP_STREAM_CLOSED
;
346 asoc
->strreset_chunk
= chunk
;
347 sctp_chunk_hold(asoc
->strreset_chunk
);
349 retval
= sctp_send_reconf(asoc
, chunk
);
351 sctp_chunk_put(asoc
->strreset_chunk
);
352 asoc
->strreset_chunk
= NULL
;
357 for (i
= 0; i
< str_nums
; i
++)
358 SCTP_SO(stream
, str_list
[i
])->state
=
361 for (i
= 0; i
< stream
->outcnt
; i
++)
362 SCTP_SO(stream
, i
)->state
= SCTP_STREAM_OPEN
;
367 asoc
->strreset_outstanding
= out
+ in
;
373 int sctp_send_reset_assoc(struct sctp_association
*asoc
)
375 struct sctp_stream
*stream
= &asoc
->stream
;
376 struct sctp_chunk
*chunk
= NULL
;
380 if (!asoc
->peer
.reconf_capable
||
381 !(asoc
->strreset_enable
& SCTP_ENABLE_RESET_ASSOC_REQ
))
384 if (asoc
->strreset_outstanding
)
387 if (!sctp_outq_is_empty(&asoc
->outqueue
))
390 chunk
= sctp_make_strreset_tsnreq(asoc
);
394 /* Block further xmit of data until this request is completed */
395 for (i
= 0; i
< stream
->outcnt
; i
++)
396 SCTP_SO(stream
, i
)->state
= SCTP_STREAM_CLOSED
;
398 asoc
->strreset_chunk
= chunk
;
399 sctp_chunk_hold(asoc
->strreset_chunk
);
401 retval
= sctp_send_reconf(asoc
, chunk
);
403 sctp_chunk_put(asoc
->strreset_chunk
);
404 asoc
->strreset_chunk
= NULL
;
406 for (i
= 0; i
< stream
->outcnt
; i
++)
407 SCTP_SO(stream
, i
)->state
= SCTP_STREAM_OPEN
;
412 asoc
->strreset_outstanding
= 1;
417 int sctp_send_add_streams(struct sctp_association
*asoc
,
418 struct sctp_add_streams
*params
)
420 struct sctp_stream
*stream
= &asoc
->stream
;
421 struct sctp_chunk
*chunk
= NULL
;
426 if (!asoc
->peer
.reconf_capable
||
427 !(asoc
->strreset_enable
& SCTP_ENABLE_CHANGE_ASSOC_REQ
)) {
428 retval
= -ENOPROTOOPT
;
432 if (asoc
->strreset_outstanding
) {
433 retval
= -EINPROGRESS
;
437 out
= params
->sas_outstrms
;
438 in
= params
->sas_instrms
;
439 outcnt
= stream
->outcnt
+ out
;
440 incnt
= stream
->incnt
+ in
;
441 if (outcnt
> SCTP_MAX_STREAM
|| incnt
> SCTP_MAX_STREAM
||
448 retval
= sctp_stream_alloc_out(stream
, outcnt
, GFP_KERNEL
);
453 chunk
= sctp_make_strreset_addstrm(asoc
, out
, in
);
459 asoc
->strreset_chunk
= chunk
;
460 sctp_chunk_hold(asoc
->strreset_chunk
);
462 retval
= sctp_send_reconf(asoc
, chunk
);
464 sctp_chunk_put(asoc
->strreset_chunk
);
465 asoc
->strreset_chunk
= NULL
;
469 asoc
->strreset_outstanding
= !!out
+ !!in
;
475 static struct sctp_paramhdr
*sctp_chunk_lookup_strreset_param(
476 struct sctp_association
*asoc
, __be32 resp_seq
,
479 struct sctp_chunk
*chunk
= asoc
->strreset_chunk
;
480 struct sctp_reconf_chunk
*hdr
;
481 union sctp_params param
;
486 hdr
= (struct sctp_reconf_chunk
*)chunk
->chunk_hdr
;
487 sctp_walk_params(param
, hdr
, params
) {
488 /* sctp_strreset_tsnreq is actually the basic structure
489 * of all stream reconf params, so it's safe to use it
490 * to access request_seq.
492 struct sctp_strreset_tsnreq
*req
= param
.v
;
494 if ((!resp_seq
|| req
->request_seq
== resp_seq
) &&
495 (!type
|| type
== req
->param_hdr
.type
))
502 static void sctp_update_strreset_result(struct sctp_association
*asoc
,
505 asoc
->strreset_result
[1] = asoc
->strreset_result
[0];
506 asoc
->strreset_result
[0] = result
;
509 struct sctp_chunk
*sctp_process_strreset_outreq(
510 struct sctp_association
*asoc
,
511 union sctp_params param
,
512 struct sctp_ulpevent
**evp
)
514 struct sctp_strreset_outreq
*outreq
= param
.v
;
515 struct sctp_stream
*stream
= &asoc
->stream
;
516 __u32 result
= SCTP_STRRESET_DENIED
;
517 __be16
*str_p
= NULL
;
521 request_seq
= ntohl(outreq
->request_seq
);
523 if (ntohl(outreq
->send_reset_at_tsn
) >
524 sctp_tsnmap_get_ctsn(&asoc
->peer
.tsn_map
)) {
525 result
= SCTP_STRRESET_IN_PROGRESS
;
529 if (TSN_lt(asoc
->strreset_inseq
, request_seq
) ||
530 TSN_lt(request_seq
, asoc
->strreset_inseq
- 2)) {
531 result
= SCTP_STRRESET_ERR_BAD_SEQNO
;
533 } else if (TSN_lt(request_seq
, asoc
->strreset_inseq
)) {
534 i
= asoc
->strreset_inseq
- request_seq
- 1;
535 result
= asoc
->strreset_result
[i
];
538 asoc
->strreset_inseq
++;
540 /* Check strreset_enable after inseq inc, as sender cannot tell
541 * the peer doesn't enable strreset after receiving response with
542 * result denied, as well as to keep consistent with bsd.
544 if (!(asoc
->strreset_enable
& SCTP_ENABLE_RESET_STREAM_REQ
))
547 nums
= (ntohs(param
.p
->length
) - sizeof(*outreq
)) / sizeof(__u16
);
548 str_p
= outreq
->list_of_streams
;
549 for (i
= 0; i
< nums
; i
++) {
550 if (ntohs(str_p
[i
]) >= stream
->incnt
) {
551 result
= SCTP_STRRESET_ERR_WRONG_SSN
;
556 if (asoc
->strreset_chunk
) {
557 if (!sctp_chunk_lookup_strreset_param(
558 asoc
, outreq
->response_seq
,
559 SCTP_PARAM_RESET_IN_REQUEST
)) {
560 /* same process with outstanding isn't 0 */
561 result
= SCTP_STRRESET_ERR_IN_PROGRESS
;
565 asoc
->strreset_outstanding
--;
566 asoc
->strreset_outseq
++;
568 if (!asoc
->strreset_outstanding
) {
569 struct sctp_transport
*t
;
571 t
= asoc
->strreset_chunk
->transport
;
572 if (del_timer(&t
->reconf_timer
))
573 sctp_transport_put(t
);
575 sctp_chunk_put(asoc
->strreset_chunk
);
576 asoc
->strreset_chunk
= NULL
;
581 for (i
= 0; i
< nums
; i
++)
582 SCTP_SI(stream
, ntohs(str_p
[i
]))->mid
= 0;
584 for (i
= 0; i
< stream
->incnt
; i
++)
585 SCTP_SI(stream
, i
)->mid
= 0;
587 result
= SCTP_STRRESET_PERFORMED
;
589 *evp
= sctp_ulpevent_make_stream_reset_event(asoc
,
590 SCTP_STREAM_RESET_INCOMING_SSN
, nums
, str_p
, GFP_ATOMIC
);
593 sctp_update_strreset_result(asoc
, result
);
595 return sctp_make_strreset_resp(asoc
, result
, request_seq
);
598 struct sctp_chunk
*sctp_process_strreset_inreq(
599 struct sctp_association
*asoc
,
600 union sctp_params param
,
601 struct sctp_ulpevent
**evp
)
603 struct sctp_strreset_inreq
*inreq
= param
.v
;
604 struct sctp_stream
*stream
= &asoc
->stream
;
605 __u32 result
= SCTP_STRRESET_DENIED
;
606 struct sctp_chunk
*chunk
= NULL
;
611 request_seq
= ntohl(inreq
->request_seq
);
612 if (TSN_lt(asoc
->strreset_inseq
, request_seq
) ||
613 TSN_lt(request_seq
, asoc
->strreset_inseq
- 2)) {
614 result
= SCTP_STRRESET_ERR_BAD_SEQNO
;
616 } else if (TSN_lt(request_seq
, asoc
->strreset_inseq
)) {
617 i
= asoc
->strreset_inseq
- request_seq
- 1;
618 result
= asoc
->strreset_result
[i
];
619 if (result
== SCTP_STRRESET_PERFORMED
)
623 asoc
->strreset_inseq
++;
625 if (!(asoc
->strreset_enable
& SCTP_ENABLE_RESET_STREAM_REQ
))
628 if (asoc
->strreset_outstanding
) {
629 result
= SCTP_STRRESET_ERR_IN_PROGRESS
;
633 nums
= (ntohs(param
.p
->length
) - sizeof(*inreq
)) / sizeof(__u16
);
634 str_p
= inreq
->list_of_streams
;
635 for (i
= 0; i
< nums
; i
++) {
636 if (ntohs(str_p
[i
]) >= stream
->outcnt
) {
637 result
= SCTP_STRRESET_ERR_WRONG_SSN
;
642 if (!sctp_stream_outq_is_empty(stream
, nums
, str_p
)) {
643 result
= SCTP_STRRESET_IN_PROGRESS
;
644 asoc
->strreset_inseq
--;
648 chunk
= sctp_make_strreset_req(asoc
, nums
, str_p
, 1, 0);
653 for (i
= 0; i
< nums
; i
++)
654 SCTP_SO(stream
, ntohs(str_p
[i
]))->state
=
657 for (i
= 0; i
< stream
->outcnt
; i
++)
658 SCTP_SO(stream
, i
)->state
= SCTP_STREAM_CLOSED
;
660 asoc
->strreset_chunk
= chunk
;
661 asoc
->strreset_outstanding
= 1;
662 sctp_chunk_hold(asoc
->strreset_chunk
);
664 result
= SCTP_STRRESET_PERFORMED
;
667 sctp_update_strreset_result(asoc
, result
);
670 chunk
= sctp_make_strreset_resp(asoc
, result
, request_seq
);
675 struct sctp_chunk
*sctp_process_strreset_tsnreq(
676 struct sctp_association
*asoc
,
677 union sctp_params param
,
678 struct sctp_ulpevent
**evp
)
680 __u32 init_tsn
= 0, next_tsn
= 0, max_tsn_seen
;
681 struct sctp_strreset_tsnreq
*tsnreq
= param
.v
;
682 struct sctp_stream
*stream
= &asoc
->stream
;
683 __u32 result
= SCTP_STRRESET_DENIED
;
687 request_seq
= ntohl(tsnreq
->request_seq
);
688 if (TSN_lt(asoc
->strreset_inseq
, request_seq
) ||
689 TSN_lt(request_seq
, asoc
->strreset_inseq
- 2)) {
690 result
= SCTP_STRRESET_ERR_BAD_SEQNO
;
692 } else if (TSN_lt(request_seq
, asoc
->strreset_inseq
)) {
693 i
= asoc
->strreset_inseq
- request_seq
- 1;
694 result
= asoc
->strreset_result
[i
];
695 if (result
== SCTP_STRRESET_PERFORMED
) {
696 next_tsn
= asoc
->ctsn_ack_point
+ 1;
698 sctp_tsnmap_get_ctsn(&asoc
->peer
.tsn_map
) + 1;
703 if (!sctp_outq_is_empty(&asoc
->outqueue
)) {
704 result
= SCTP_STRRESET_IN_PROGRESS
;
708 asoc
->strreset_inseq
++;
710 if (!(asoc
->strreset_enable
& SCTP_ENABLE_RESET_ASSOC_REQ
))
713 if (asoc
->strreset_outstanding
) {
714 result
= SCTP_STRRESET_ERR_IN_PROGRESS
;
718 /* G4: The same processing as though a FWD-TSN chunk (as defined in
719 * [RFC3758]) with all streams affected and a new cumulative TSN
720 * ACK of the Receiver's Next TSN minus 1 were received MUST be
723 max_tsn_seen
= sctp_tsnmap_get_max_tsn_seen(&asoc
->peer
.tsn_map
);
724 asoc
->stream
.si
->report_ftsn(&asoc
->ulpq
, max_tsn_seen
);
726 /* G1: Compute an appropriate value for the Receiver's Next TSN -- the
727 * TSN that the peer should use to send the next DATA chunk. The
728 * value SHOULD be the smallest TSN not acknowledged by the
729 * receiver of the request plus 2^31.
731 init_tsn
= sctp_tsnmap_get_ctsn(&asoc
->peer
.tsn_map
) + (1 << 31);
732 sctp_tsnmap_init(&asoc
->peer
.tsn_map
, SCTP_TSN_MAP_INITIAL
,
733 init_tsn
, GFP_ATOMIC
);
735 /* G3: The same processing as though a SACK chunk with no gap report
736 * and a cumulative TSN ACK of the Sender's Next TSN minus 1 were
737 * received MUST be performed.
739 sctp_outq_free(&asoc
->outqueue
);
741 /* G2: Compute an appropriate value for the local endpoint's next TSN,
742 * i.e., the next TSN assigned by the receiver of the SSN/TSN reset
743 * chunk. The value SHOULD be the highest TSN sent by the receiver
744 * of the request plus 1.
746 next_tsn
= asoc
->next_tsn
;
747 asoc
->ctsn_ack_point
= next_tsn
- 1;
748 asoc
->adv_peer_ack_point
= asoc
->ctsn_ack_point
;
750 /* G5: The next expected and outgoing SSNs MUST be reset to 0 for all
751 * incoming and outgoing streams.
753 for (i
= 0; i
< stream
->outcnt
; i
++) {
754 SCTP_SO(stream
, i
)->mid
= 0;
755 SCTP_SO(stream
, i
)->mid_uo
= 0;
757 for (i
= 0; i
< stream
->incnt
; i
++)
758 SCTP_SI(stream
, i
)->mid
= 0;
760 result
= SCTP_STRRESET_PERFORMED
;
762 *evp
= sctp_ulpevent_make_assoc_reset_event(asoc
, 0, init_tsn
,
763 next_tsn
, GFP_ATOMIC
);
766 sctp_update_strreset_result(asoc
, result
);
768 return sctp_make_strreset_tsnresp(asoc
, result
, request_seq
,
772 struct sctp_chunk
*sctp_process_strreset_addstrm_out(
773 struct sctp_association
*asoc
,
774 union sctp_params param
,
775 struct sctp_ulpevent
**evp
)
777 struct sctp_strreset_addstrm
*addstrm
= param
.v
;
778 struct sctp_stream
*stream
= &asoc
->stream
;
779 __u32 result
= SCTP_STRRESET_DENIED
;
780 __u32 request_seq
, incnt
;
783 request_seq
= ntohl(addstrm
->request_seq
);
784 if (TSN_lt(asoc
->strreset_inseq
, request_seq
) ||
785 TSN_lt(request_seq
, asoc
->strreset_inseq
- 2)) {
786 result
= SCTP_STRRESET_ERR_BAD_SEQNO
;
788 } else if (TSN_lt(request_seq
, asoc
->strreset_inseq
)) {
789 i
= asoc
->strreset_inseq
- request_seq
- 1;
790 result
= asoc
->strreset_result
[i
];
793 asoc
->strreset_inseq
++;
795 if (!(asoc
->strreset_enable
& SCTP_ENABLE_CHANGE_ASSOC_REQ
))
798 in
= ntohs(addstrm
->number_of_streams
);
799 incnt
= stream
->incnt
+ in
;
800 if (!in
|| incnt
> SCTP_MAX_STREAM
)
803 if (sctp_stream_alloc_in(stream
, incnt
, GFP_ATOMIC
))
806 if (asoc
->strreset_chunk
) {
807 if (!sctp_chunk_lookup_strreset_param(
808 asoc
, 0, SCTP_PARAM_RESET_ADD_IN_STREAMS
)) {
809 /* same process with outstanding isn't 0 */
810 result
= SCTP_STRRESET_ERR_IN_PROGRESS
;
814 asoc
->strreset_outstanding
--;
815 asoc
->strreset_outseq
++;
817 if (!asoc
->strreset_outstanding
) {
818 struct sctp_transport
*t
;
820 t
= asoc
->strreset_chunk
->transport
;
821 if (del_timer(&t
->reconf_timer
))
822 sctp_transport_put(t
);
824 sctp_chunk_put(asoc
->strreset_chunk
);
825 asoc
->strreset_chunk
= NULL
;
829 stream
->incnt
= incnt
;
831 result
= SCTP_STRRESET_PERFORMED
;
833 *evp
= sctp_ulpevent_make_stream_change_event(asoc
,
834 0, ntohs(addstrm
->number_of_streams
), 0, GFP_ATOMIC
);
837 sctp_update_strreset_result(asoc
, result
);
839 return sctp_make_strreset_resp(asoc
, result
, request_seq
);
842 struct sctp_chunk
*sctp_process_strreset_addstrm_in(
843 struct sctp_association
*asoc
,
844 union sctp_params param
,
845 struct sctp_ulpevent
**evp
)
847 struct sctp_strreset_addstrm
*addstrm
= param
.v
;
848 struct sctp_stream
*stream
= &asoc
->stream
;
849 __u32 result
= SCTP_STRRESET_DENIED
;
850 struct sctp_chunk
*chunk
= NULL
;
851 __u32 request_seq
, outcnt
;
855 request_seq
= ntohl(addstrm
->request_seq
);
856 if (TSN_lt(asoc
->strreset_inseq
, request_seq
) ||
857 TSN_lt(request_seq
, asoc
->strreset_inseq
- 2)) {
858 result
= SCTP_STRRESET_ERR_BAD_SEQNO
;
860 } else if (TSN_lt(request_seq
, asoc
->strreset_inseq
)) {
861 i
= asoc
->strreset_inseq
- request_seq
- 1;
862 result
= asoc
->strreset_result
[i
];
863 if (result
== SCTP_STRRESET_PERFORMED
)
867 asoc
->strreset_inseq
++;
869 if (!(asoc
->strreset_enable
& SCTP_ENABLE_CHANGE_ASSOC_REQ
))
872 if (asoc
->strreset_outstanding
) {
873 result
= SCTP_STRRESET_ERR_IN_PROGRESS
;
877 out
= ntohs(addstrm
->number_of_streams
);
878 outcnt
= stream
->outcnt
+ out
;
879 if (!out
|| outcnt
> SCTP_MAX_STREAM
)
882 ret
= sctp_stream_alloc_out(stream
, outcnt
, GFP_ATOMIC
);
886 chunk
= sctp_make_strreset_addstrm(asoc
, out
, 0);
890 asoc
->strreset_chunk
= chunk
;
891 asoc
->strreset_outstanding
= 1;
892 sctp_chunk_hold(asoc
->strreset_chunk
);
894 stream
->outcnt
= outcnt
;
896 result
= SCTP_STRRESET_PERFORMED
;
899 sctp_update_strreset_result(asoc
, result
);
902 chunk
= sctp_make_strreset_resp(asoc
, result
, request_seq
);
907 struct sctp_chunk
*sctp_process_strreset_resp(
908 struct sctp_association
*asoc
,
909 union sctp_params param
,
910 struct sctp_ulpevent
**evp
)
912 struct sctp_stream
*stream
= &asoc
->stream
;
913 struct sctp_strreset_resp
*resp
= param
.v
;
914 struct sctp_transport
*t
;
915 __u16 i
, nums
, flags
= 0;
916 struct sctp_paramhdr
*req
;
919 req
= sctp_chunk_lookup_strreset_param(asoc
, resp
->response_seq
, 0);
923 result
= ntohl(resp
->result
);
924 if (result
!= SCTP_STRRESET_PERFORMED
) {
925 /* if in progress, do nothing but retransmit */
926 if (result
== SCTP_STRRESET_IN_PROGRESS
)
928 else if (result
== SCTP_STRRESET_DENIED
)
929 flags
= SCTP_STREAM_RESET_DENIED
;
931 flags
= SCTP_STREAM_RESET_FAILED
;
934 if (req
->type
== SCTP_PARAM_RESET_OUT_REQUEST
) {
935 struct sctp_strreset_outreq
*outreq
;
938 outreq
= (struct sctp_strreset_outreq
*)req
;
939 str_p
= outreq
->list_of_streams
;
940 nums
= (ntohs(outreq
->param_hdr
.length
) - sizeof(*outreq
)) /
943 if (result
== SCTP_STRRESET_PERFORMED
) {
944 struct sctp_stream_out
*sout
;
946 for (i
= 0; i
< nums
; i
++) {
947 sout
= SCTP_SO(stream
, ntohs(str_p
[i
]));
952 for (i
= 0; i
< stream
->outcnt
; i
++) {
953 sout
= SCTP_SO(stream
, i
);
960 flags
|= SCTP_STREAM_RESET_OUTGOING_SSN
;
962 for (i
= 0; i
< stream
->outcnt
; i
++)
963 SCTP_SO(stream
, i
)->state
= SCTP_STREAM_OPEN
;
965 *evp
= sctp_ulpevent_make_stream_reset_event(asoc
, flags
,
966 nums
, str_p
, GFP_ATOMIC
);
967 } else if (req
->type
== SCTP_PARAM_RESET_IN_REQUEST
) {
968 struct sctp_strreset_inreq
*inreq
;
971 /* if the result is performed, it's impossible for inreq */
972 if (result
== SCTP_STRRESET_PERFORMED
)
975 inreq
= (struct sctp_strreset_inreq
*)req
;
976 str_p
= inreq
->list_of_streams
;
977 nums
= (ntohs(inreq
->param_hdr
.length
) - sizeof(*inreq
)) /
980 flags
|= SCTP_STREAM_RESET_INCOMING_SSN
;
982 *evp
= sctp_ulpevent_make_stream_reset_event(asoc
, flags
,
983 nums
, str_p
, GFP_ATOMIC
);
984 } else if (req
->type
== SCTP_PARAM_RESET_TSN_REQUEST
) {
985 struct sctp_strreset_resptsn
*resptsn
;
988 /* check for resptsn, as sctp_verify_reconf didn't do it*/
989 if (ntohs(param
.p
->length
) != sizeof(*resptsn
))
992 resptsn
= (struct sctp_strreset_resptsn
*)resp
;
993 stsn
= ntohl(resptsn
->senders_next_tsn
);
994 rtsn
= ntohl(resptsn
->receivers_next_tsn
);
996 if (result
== SCTP_STRRESET_PERFORMED
) {
997 __u32 mtsn
= sctp_tsnmap_get_max_tsn_seen(
998 &asoc
->peer
.tsn_map
);
1001 asoc
->stream
.si
->report_ftsn(&asoc
->ulpq
, mtsn
);
1003 sctp_tsnmap_init(&asoc
->peer
.tsn_map
,
1004 SCTP_TSN_MAP_INITIAL
,
1007 /* Clean up sacked and abandoned queues only. As the
1008 * out_chunk_list may not be empty, splice it to temp,
1009 * then get it back after sctp_outq_free is done.
1011 list_splice_init(&asoc
->outqueue
.out_chunk_list
, &temp
);
1012 sctp_outq_free(&asoc
->outqueue
);
1013 list_splice_init(&temp
, &asoc
->outqueue
.out_chunk_list
);
1015 asoc
->next_tsn
= rtsn
;
1016 asoc
->ctsn_ack_point
= asoc
->next_tsn
- 1;
1017 asoc
->adv_peer_ack_point
= asoc
->ctsn_ack_point
;
1019 for (i
= 0; i
< stream
->outcnt
; i
++) {
1020 SCTP_SO(stream
, i
)->mid
= 0;
1021 SCTP_SO(stream
, i
)->mid_uo
= 0;
1023 for (i
= 0; i
< stream
->incnt
; i
++)
1024 SCTP_SI(stream
, i
)->mid
= 0;
1027 for (i
= 0; i
< stream
->outcnt
; i
++)
1028 SCTP_SO(stream
, i
)->state
= SCTP_STREAM_OPEN
;
1030 *evp
= sctp_ulpevent_make_assoc_reset_event(asoc
, flags
,
1031 stsn
, rtsn
, GFP_ATOMIC
);
1032 } else if (req
->type
== SCTP_PARAM_RESET_ADD_OUT_STREAMS
) {
1033 struct sctp_strreset_addstrm
*addstrm
;
1036 addstrm
= (struct sctp_strreset_addstrm
*)req
;
1037 nums
= ntohs(addstrm
->number_of_streams
);
1038 number
= stream
->outcnt
- nums
;
1040 if (result
== SCTP_STRRESET_PERFORMED
)
1041 for (i
= number
; i
< stream
->outcnt
; i
++)
1042 SCTP_SO(stream
, i
)->state
= SCTP_STREAM_OPEN
;
1044 stream
->outcnt
= number
;
1046 *evp
= sctp_ulpevent_make_stream_change_event(asoc
, flags
,
1047 0, nums
, GFP_ATOMIC
);
1048 } else if (req
->type
== SCTP_PARAM_RESET_ADD_IN_STREAMS
) {
1049 struct sctp_strreset_addstrm
*addstrm
;
1051 /* if the result is performed, it's impossible for addstrm in
1054 if (result
== SCTP_STRRESET_PERFORMED
)
1057 addstrm
= (struct sctp_strreset_addstrm
*)req
;
1058 nums
= ntohs(addstrm
->number_of_streams
);
1060 *evp
= sctp_ulpevent_make_stream_change_event(asoc
, flags
,
1061 nums
, 0, GFP_ATOMIC
);
1064 asoc
->strreset_outstanding
--;
1065 asoc
->strreset_outseq
++;
1067 /* remove everything for this reconf request */
1068 if (!asoc
->strreset_outstanding
) {
1069 t
= asoc
->strreset_chunk
->transport
;
1070 if (del_timer(&t
->reconf_timer
))
1071 sctp_transport_put(t
);
1073 sctp_chunk_put(asoc
->strreset_chunk
);
1074 asoc
->strreset_chunk
= NULL
;