1 /* SCTP kernel implementation
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001 Intel Corp.
7 * This file is part of the SCTP kernel implementation
9 * These functions manipulate sctp tsn mapping array.
11 * This SCTP implementation is free software;
12 * you can redistribute it and/or modify it under the terms of
13 * the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This SCTP implementation is distributed in the hope that it
18 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
19 * ************************
20 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21 * See the GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with GNU CC; see the file COPYING. If not, see
25 * <http://www.gnu.org/licenses/>.
27 * Please send any bug reports or fixes you make to the
29 * lksctp developers <linux-sctp@vger.kernel.org>
31 * Written or modified by:
32 * Xin Long <lucien.xin@gmail.com>
35 #include <net/sctp/sctp.h>
36 #include <net/sctp/sm.h>
38 int sctp_stream_new(struct sctp_association
*asoc
, gfp_t gfp
)
40 struct sctp_stream
*stream
;
43 stream
= kzalloc(sizeof(*stream
), gfp
);
47 stream
->outcnt
= asoc
->c
.sinit_num_ostreams
;
48 stream
->out
= kcalloc(stream
->outcnt
, sizeof(*stream
->out
), gfp
);
53 for (i
= 0; i
< stream
->outcnt
; i
++)
54 stream
->out
[i
].state
= SCTP_STREAM_OPEN
;
56 asoc
->stream
= stream
;
61 int sctp_stream_init(struct sctp_association
*asoc
, gfp_t gfp
)
63 struct sctp_stream
*stream
= asoc
->stream
;
66 /* Initial stream->out size may be very big, so free it and alloc
67 * a new one with new outcnt to save memory.
70 stream
->outcnt
= asoc
->c
.sinit_num_ostreams
;
71 stream
->out
= kcalloc(stream
->outcnt
, sizeof(*stream
->out
), gfp
);
75 for (i
= 0; i
< stream
->outcnt
; i
++)
76 stream
->out
[i
].state
= SCTP_STREAM_OPEN
;
78 stream
->incnt
= asoc
->c
.sinit_max_instreams
;
79 stream
->in
= kcalloc(stream
->incnt
, sizeof(*stream
->in
), gfp
);
94 void sctp_stream_free(struct sctp_stream
*stream
)
96 if (unlikely(!stream
))
104 void sctp_stream_clear(struct sctp_stream
*stream
)
108 for (i
= 0; i
< stream
->outcnt
; i
++)
109 stream
->out
[i
].ssn
= 0;
111 for (i
= 0; i
< stream
->incnt
; i
++)
112 stream
->in
[i
].ssn
= 0;
115 static int sctp_send_reconf(struct sctp_association
*asoc
,
116 struct sctp_chunk
*chunk
)
118 struct net
*net
= sock_net(asoc
->base
.sk
);
121 retval
= sctp_primitive_RECONF(net
, asoc
, chunk
);
123 sctp_chunk_free(chunk
);
128 int sctp_send_reset_streams(struct sctp_association
*asoc
,
129 struct sctp_reset_streams
*params
)
131 struct sctp_stream
*stream
= asoc
->stream
;
132 __u16 i
, str_nums
, *str_list
;
133 struct sctp_chunk
*chunk
;
134 int retval
= -EINVAL
;
137 if (!asoc
->peer
.reconf_capable
||
138 !(asoc
->strreset_enable
& SCTP_ENABLE_RESET_STREAM_REQ
)) {
139 retval
= -ENOPROTOOPT
;
143 if (asoc
->strreset_outstanding
) {
144 retval
= -EINPROGRESS
;
148 out
= params
->srs_flags
& SCTP_STREAM_RESET_OUTGOING
;
149 in
= params
->srs_flags
& SCTP_STREAM_RESET_INCOMING
;
153 str_nums
= params
->srs_number_streams
;
154 str_list
= params
->srs_stream_list
;
156 for (i
= 0; i
< str_nums
; i
++)
157 if (str_list
[i
] >= stream
->outcnt
)
161 for (i
= 0; i
< str_nums
; i
++)
162 if (str_list
[i
] >= stream
->incnt
)
165 for (i
= 0; i
< str_nums
; i
++)
166 str_list
[i
] = htons(str_list
[i
]);
168 chunk
= sctp_make_strreset_req(asoc
, str_nums
, str_list
, out
, in
);
170 for (i
= 0; i
< str_nums
; i
++)
171 str_list
[i
] = ntohs(str_list
[i
]);
180 for (i
= 0; i
< str_nums
; i
++)
181 stream
->out
[str_list
[i
]].state
=
184 for (i
= 0; i
< stream
->outcnt
; i
++)
185 stream
->out
[i
].state
= SCTP_STREAM_CLOSED
;
188 asoc
->strreset_chunk
= chunk
;
189 sctp_chunk_hold(asoc
->strreset_chunk
);
191 retval
= sctp_send_reconf(asoc
, chunk
);
193 sctp_chunk_put(asoc
->strreset_chunk
);
194 asoc
->strreset_chunk
= NULL
;
199 for (i
= 0; i
< str_nums
; i
++)
200 stream
->out
[str_list
[i
]].state
=
203 for (i
= 0; i
< stream
->outcnt
; i
++)
204 stream
->out
[i
].state
= SCTP_STREAM_OPEN
;
209 asoc
->strreset_outstanding
= out
+ in
;
215 int sctp_send_reset_assoc(struct sctp_association
*asoc
)
217 struct sctp_chunk
*chunk
= NULL
;
221 if (!asoc
->peer
.reconf_capable
||
222 !(asoc
->strreset_enable
& SCTP_ENABLE_RESET_ASSOC_REQ
))
225 if (asoc
->strreset_outstanding
)
228 chunk
= sctp_make_strreset_tsnreq(asoc
);
232 /* Block further xmit of data until this request is completed */
233 for (i
= 0; i
< asoc
->stream
->outcnt
; i
++)
234 asoc
->stream
->out
[i
].state
= SCTP_STREAM_CLOSED
;
236 asoc
->strreset_chunk
= chunk
;
237 sctp_chunk_hold(asoc
->strreset_chunk
);
239 retval
= sctp_send_reconf(asoc
, chunk
);
241 sctp_chunk_put(asoc
->strreset_chunk
);
242 asoc
->strreset_chunk
= NULL
;
244 for (i
= 0; i
< asoc
->stream
->outcnt
; i
++)
245 asoc
->stream
->out
[i
].state
= SCTP_STREAM_OPEN
;
250 asoc
->strreset_outstanding
= 1;
255 int sctp_send_add_streams(struct sctp_association
*asoc
,
256 struct sctp_add_streams
*params
)
258 struct sctp_stream
*stream
= asoc
->stream
;
259 struct sctp_chunk
*chunk
= NULL
;
260 int retval
= -ENOMEM
;
264 if (!asoc
->peer
.reconf_capable
||
265 !(asoc
->strreset_enable
& SCTP_ENABLE_CHANGE_ASSOC_REQ
)) {
266 retval
= -ENOPROTOOPT
;
270 if (asoc
->strreset_outstanding
) {
271 retval
= -EINPROGRESS
;
275 out
= params
->sas_outstrms
;
276 in
= params
->sas_instrms
;
277 outcnt
= stream
->outcnt
+ out
;
278 incnt
= stream
->incnt
+ in
;
279 if (outcnt
> SCTP_MAX_STREAM
|| incnt
> SCTP_MAX_STREAM
||
286 struct sctp_stream_out
*streamout
;
288 streamout
= krealloc(stream
->out
, outcnt
* sizeof(*streamout
),
293 memset(streamout
+ stream
->outcnt
, 0, out
* sizeof(*streamout
));
294 stream
->out
= streamout
;
298 struct sctp_stream_in
*streamin
;
300 streamin
= krealloc(stream
->in
, incnt
* sizeof(*streamin
),
305 memset(streamin
+ stream
->incnt
, 0, in
* sizeof(*streamin
));
306 stream
->in
= streamin
;
309 chunk
= sctp_make_strreset_addstrm(asoc
, out
, in
);
313 asoc
->strreset_chunk
= chunk
;
314 sctp_chunk_hold(asoc
->strreset_chunk
);
316 retval
= sctp_send_reconf(asoc
, chunk
);
318 sctp_chunk_put(asoc
->strreset_chunk
);
319 asoc
->strreset_chunk
= NULL
;
323 stream
->incnt
= incnt
;
324 stream
->outcnt
= outcnt
;
326 asoc
->strreset_outstanding
= !!out
+ !!in
;
332 static sctp_paramhdr_t
*sctp_chunk_lookup_strreset_param(
333 struct sctp_association
*asoc
, __u32 resp_seq
)
335 struct sctp_chunk
*chunk
= asoc
->strreset_chunk
;
336 struct sctp_reconf_chunk
*hdr
;
337 union sctp_params param
;
339 if (ntohl(resp_seq
) != asoc
->strreset_outseq
|| !chunk
)
342 hdr
= (struct sctp_reconf_chunk
*)chunk
->chunk_hdr
;
343 sctp_walk_params(param
, hdr
, params
) {
344 /* sctp_strreset_tsnreq is actually the basic structure
345 * of all stream reconf params, so it's safe to use it
346 * to access request_seq.
348 struct sctp_strreset_tsnreq
*req
= param
.v
;
350 if (req
->request_seq
== resp_seq
)
357 struct sctp_chunk
*sctp_process_strreset_outreq(
358 struct sctp_association
*asoc
,
359 union sctp_params param
,
360 struct sctp_ulpevent
**evp
)
362 struct sctp_strreset_outreq
*outreq
= param
.v
;
363 struct sctp_stream
*stream
= asoc
->stream
;
364 __u16 i
, nums
, flags
= 0, *str_p
= NULL
;
365 __u32 result
= SCTP_STRRESET_DENIED
;
368 request_seq
= ntohl(outreq
->request_seq
);
370 if (ntohl(outreq
->send_reset_at_tsn
) >
371 sctp_tsnmap_get_ctsn(&asoc
->peer
.tsn_map
)) {
372 result
= SCTP_STRRESET_IN_PROGRESS
;
376 if (request_seq
> asoc
->strreset_inseq
) {
377 result
= SCTP_STRRESET_ERR_BAD_SEQNO
;
379 } else if (request_seq
== asoc
->strreset_inseq
) {
380 asoc
->strreset_inseq
++;
383 /* Check strreset_enable after inseq inc, as sender cannot tell
384 * the peer doesn't enable strreset after receiving response with
385 * result denied, as well as to keep consistent with bsd.
387 if (!(asoc
->strreset_enable
& SCTP_ENABLE_RESET_STREAM_REQ
))
390 if (asoc
->strreset_chunk
) {
391 sctp_paramhdr_t
*param_hdr
;
392 struct sctp_transport
*t
;
394 param_hdr
= sctp_chunk_lookup_strreset_param(
395 asoc
, outreq
->response_seq
);
396 if (!param_hdr
|| param_hdr
->type
!=
397 SCTP_PARAM_RESET_IN_REQUEST
) {
398 /* same process with outstanding isn't 0 */
399 result
= SCTP_STRRESET_ERR_IN_PROGRESS
;
403 asoc
->strreset_outstanding
--;
404 asoc
->strreset_outseq
++;
406 if (!asoc
->strreset_outstanding
) {
407 t
= asoc
->strreset_chunk
->transport
;
408 if (del_timer(&t
->reconf_timer
))
409 sctp_transport_put(t
);
411 sctp_chunk_put(asoc
->strreset_chunk
);
412 asoc
->strreset_chunk
= NULL
;
415 flags
= SCTP_STREAM_RESET_INCOMING_SSN
;
418 nums
= (ntohs(param
.p
->length
) - sizeof(*outreq
)) / 2;
420 str_p
= outreq
->list_of_streams
;
421 for (i
= 0; i
< nums
; i
++) {
422 if (ntohs(str_p
[i
]) >= stream
->incnt
) {
423 result
= SCTP_STRRESET_ERR_WRONG_SSN
;
428 for (i
= 0; i
< nums
; i
++)
429 stream
->in
[ntohs(str_p
[i
])].ssn
= 0;
431 for (i
= 0; i
< stream
->incnt
; i
++)
432 stream
->in
[i
].ssn
= 0;
435 result
= SCTP_STRRESET_PERFORMED
;
437 *evp
= sctp_ulpevent_make_stream_reset_event(asoc
,
438 flags
| SCTP_STREAM_RESET_OUTGOING_SSN
, nums
, str_p
,
442 return sctp_make_strreset_resp(asoc
, result
, request_seq
);
445 struct sctp_chunk
*sctp_process_strreset_inreq(
446 struct sctp_association
*asoc
,
447 union sctp_params param
,
448 struct sctp_ulpevent
**evp
)
450 struct sctp_strreset_inreq
*inreq
= param
.v
;
451 struct sctp_stream
*stream
= asoc
->stream
;
452 __u32 result
= SCTP_STRRESET_DENIED
;
453 struct sctp_chunk
*chunk
= NULL
;
454 __u16 i
, nums
, *str_p
;
457 request_seq
= ntohl(inreq
->request_seq
);
458 if (request_seq
> asoc
->strreset_inseq
) {
459 result
= SCTP_STRRESET_ERR_BAD_SEQNO
;
461 } else if (request_seq
== asoc
->strreset_inseq
) {
462 asoc
->strreset_inseq
++;
465 if (!(asoc
->strreset_enable
& SCTP_ENABLE_RESET_STREAM_REQ
))
468 if (asoc
->strreset_outstanding
) {
469 result
= SCTP_STRRESET_ERR_IN_PROGRESS
;
473 nums
= (ntohs(param
.p
->length
) - sizeof(*inreq
)) / 2;
474 str_p
= inreq
->list_of_streams
;
475 for (i
= 0; i
< nums
; i
++) {
476 if (ntohs(str_p
[i
]) >= stream
->outcnt
) {
477 result
= SCTP_STRRESET_ERR_WRONG_SSN
;
482 chunk
= sctp_make_strreset_req(asoc
, nums
, str_p
, 1, 0);
487 for (i
= 0; i
< nums
; i
++)
488 stream
->out
[ntohs(str_p
[i
])].state
=
491 for (i
= 0; i
< stream
->outcnt
; i
++)
492 stream
->out
[i
].state
= SCTP_STREAM_CLOSED
;
494 asoc
->strreset_chunk
= chunk
;
495 asoc
->strreset_outstanding
= 1;
496 sctp_chunk_hold(asoc
->strreset_chunk
);
498 *evp
= sctp_ulpevent_make_stream_reset_event(asoc
,
499 SCTP_STREAM_RESET_INCOMING_SSN
, nums
, str_p
, GFP_ATOMIC
);
503 chunk
= sctp_make_strreset_resp(asoc
, result
, request_seq
);