]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/sctp/stream.c
531fcc4cd11256eb2d44f1f64936b561d9eedbd1
[karo-tx-linux.git] / net / sctp / stream.c
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.
6  *
7  * This file is part of the SCTP kernel implementation
8  *
9  * These functions manipulate sctp tsn mapping array.
10  *
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)
15  * any later version.
16  *
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.
22  *
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/>.
26  *
27  * Please send any bug reports or fixes you make to the
28  * email address(es):
29  *    lksctp developers <linux-sctp@vger.kernel.org>
30  *
31  * Written or modified by:
32  *    Xin Long <lucien.xin@gmail.com>
33  */
34
35 #include <net/sctp/sctp.h>
36 #include <net/sctp/sm.h>
37
38 struct sctp_stream *sctp_stream_new(__u16 incnt, __u16 outcnt, gfp_t gfp)
39 {
40         struct sctp_stream *stream;
41         int i;
42
43         stream = kzalloc(sizeof(*stream), gfp);
44         if (!stream)
45                 return NULL;
46
47         stream->outcnt = outcnt;
48         stream->out = kcalloc(stream->outcnt, sizeof(*stream->out), gfp);
49         if (!stream->out) {
50                 kfree(stream);
51                 return NULL;
52         }
53         for (i = 0; i < stream->outcnt; i++)
54                 stream->out[i].state = SCTP_STREAM_OPEN;
55
56         stream->incnt = incnt;
57         stream->in = kcalloc(stream->incnt, sizeof(*stream->in), gfp);
58         if (!stream->in) {
59                 kfree(stream->out);
60                 kfree(stream);
61                 return NULL;
62         }
63
64         return stream;
65 }
66
67 void sctp_stream_free(struct sctp_stream *stream)
68 {
69         if (unlikely(!stream))
70                 return;
71
72         kfree(stream->out);
73         kfree(stream->in);
74         kfree(stream);
75 }
76
77 void sctp_stream_clear(struct sctp_stream *stream)
78 {
79         int i;
80
81         for (i = 0; i < stream->outcnt; i++)
82                 stream->out[i].ssn = 0;
83
84         for (i = 0; i < stream->incnt; i++)
85                 stream->in[i].ssn = 0;
86 }
87
88 static int sctp_send_reconf(struct sctp_association *asoc,
89                             struct sctp_chunk *chunk)
90 {
91         struct net *net = sock_net(asoc->base.sk);
92         int retval = 0;
93
94         retval = sctp_primitive_RECONF(net, asoc, chunk);
95         if (retval)
96                 sctp_chunk_free(chunk);
97
98         return retval;
99 }
100
101 int sctp_send_reset_streams(struct sctp_association *asoc,
102                             struct sctp_reset_streams *params)
103 {
104         struct sctp_stream *stream = asoc->stream;
105         __u16 i, str_nums, *str_list;
106         struct sctp_chunk *chunk;
107         int retval = -EINVAL;
108         bool out, in;
109
110         if (!asoc->peer.reconf_capable ||
111             !(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ)) {
112                 retval = -ENOPROTOOPT;
113                 goto out;
114         }
115
116         if (asoc->strreset_outstanding) {
117                 retval = -EINPROGRESS;
118                 goto out;
119         }
120
121         out = params->srs_flags & SCTP_STREAM_RESET_OUTGOING;
122         in  = params->srs_flags & SCTP_STREAM_RESET_INCOMING;
123         if (!out && !in)
124                 goto out;
125
126         str_nums = params->srs_number_streams;
127         str_list = params->srs_stream_list;
128         if (out && str_nums)
129                 for (i = 0; i < str_nums; i++)
130                         if (str_list[i] >= stream->outcnt)
131                                 goto out;
132
133         if (in && str_nums)
134                 for (i = 0; i < str_nums; i++)
135                         if (str_list[i] >= stream->incnt)
136                                 goto out;
137
138         chunk = sctp_make_strreset_req(asoc, str_nums, str_list, out, in);
139         if (!chunk) {
140                 retval = -ENOMEM;
141                 goto out;
142         }
143
144         if (out) {
145                 if (str_nums)
146                         for (i = 0; i < str_nums; i++)
147                                 stream->out[str_list[i]].state =
148                                                        SCTP_STREAM_CLOSED;
149                 else
150                         for (i = 0; i < stream->outcnt; i++)
151                                 stream->out[i].state = SCTP_STREAM_CLOSED;
152         }
153
154         asoc->strreset_chunk = chunk;
155         sctp_chunk_hold(asoc->strreset_chunk);
156
157         retval = sctp_send_reconf(asoc, chunk);
158         if (retval) {
159                 sctp_chunk_put(asoc->strreset_chunk);
160                 asoc->strreset_chunk = NULL;
161                 if (!out)
162                         goto out;
163
164                 if (str_nums)
165                         for (i = 0; i < str_nums; i++)
166                                 stream->out[str_list[i]].state =
167                                                        SCTP_STREAM_OPEN;
168                 else
169                         for (i = 0; i < stream->outcnt; i++)
170                                 stream->out[i].state = SCTP_STREAM_OPEN;
171
172                 goto out;
173         }
174
175         asoc->strreset_outstanding = out + in;
176
177 out:
178         return retval;
179 }
180
181 int sctp_send_reset_assoc(struct sctp_association *asoc)
182 {
183         struct sctp_chunk *chunk = NULL;
184         int retval;
185         __u16 i;
186
187         if (!asoc->peer.reconf_capable ||
188             !(asoc->strreset_enable & SCTP_ENABLE_RESET_ASSOC_REQ))
189                 return -ENOPROTOOPT;
190
191         if (asoc->strreset_outstanding)
192                 return -EINPROGRESS;
193
194         chunk = sctp_make_strreset_tsnreq(asoc);
195         if (!chunk)
196                 return -ENOMEM;
197
198         /* Block further xmit of data until this request is completed */
199         for (i = 0; i < asoc->stream->outcnt; i++)
200                 asoc->stream->out[i].state = SCTP_STREAM_CLOSED;
201
202         asoc->strreset_chunk = chunk;
203         sctp_chunk_hold(asoc->strreset_chunk);
204
205         retval = sctp_send_reconf(asoc, chunk);
206         if (retval) {
207                 sctp_chunk_put(asoc->strreset_chunk);
208                 asoc->strreset_chunk = NULL;
209
210                 for (i = 0; i < asoc->stream->outcnt; i++)
211                         asoc->stream->out[i].state = SCTP_STREAM_OPEN;
212
213                 return retval;
214         }
215
216         asoc->strreset_outstanding = 1;
217
218         return 0;
219 }
220
221 int sctp_send_add_streams(struct sctp_association *asoc,
222                           struct sctp_add_streams *params)
223 {
224         struct sctp_stream *stream = asoc->stream;
225         struct sctp_chunk *chunk = NULL;
226         int retval = -ENOMEM;
227         __u32 outcnt, incnt;
228         __u16 out, in;
229
230         if (!asoc->peer.reconf_capable ||
231             !(asoc->strreset_enable & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
232                 retval = -ENOPROTOOPT;
233                 goto out;
234         }
235
236         if (asoc->strreset_outstanding) {
237                 retval = -EINPROGRESS;
238                 goto out;
239         }
240
241         out = params->sas_outstrms;
242         in  = params->sas_instrms;
243         outcnt = stream->outcnt + out;
244         incnt = stream->incnt + in;
245         if (outcnt > SCTP_MAX_STREAM || incnt > SCTP_MAX_STREAM ||
246             (!out && !in)) {
247                 retval = -EINVAL;
248                 goto out;
249         }
250
251         if (out) {
252                 struct sctp_stream_out *streamout;
253
254                 streamout = krealloc(stream->out, outcnt * sizeof(*streamout),
255                                      GFP_KERNEL);
256                 if (!streamout)
257                         goto out;
258
259                 memset(streamout + stream->outcnt, 0, out * sizeof(*streamout));
260                 stream->out = streamout;
261         }
262
263         if (in) {
264                 struct sctp_stream_in *streamin;
265
266                 streamin = krealloc(stream->in, incnt * sizeof(*streamin),
267                                     GFP_KERNEL);
268                 if (!streamin)
269                         goto out;
270
271                 memset(streamin + stream->incnt, 0, in * sizeof(*streamin));
272                 stream->in = streamin;
273         }
274
275         chunk = sctp_make_strreset_addstrm(asoc, out, in);
276         if (!chunk)
277                 goto out;
278
279         asoc->strreset_chunk = chunk;
280         sctp_chunk_hold(asoc->strreset_chunk);
281
282         retval = sctp_send_reconf(asoc, chunk);
283         if (retval) {
284                 sctp_chunk_put(asoc->strreset_chunk);
285                 asoc->strreset_chunk = NULL;
286                 goto out;
287         }
288
289         stream->incnt = incnt;
290         stream->outcnt = outcnt;
291
292         asoc->strreset_outstanding = !!out + !!in;
293
294 out:
295         return retval;
296 }
297
298 static sctp_paramhdr_t *sctp_chunk_lookup_strreset_param(
299                         struct sctp_association *asoc, __u32 resp_seq)
300 {
301         struct sctp_chunk *chunk = asoc->strreset_chunk;
302         struct sctp_reconf_chunk *hdr;
303         union sctp_params param;
304
305         if (ntohl(resp_seq) != asoc->strreset_outseq || !chunk)
306                 return NULL;
307
308         hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
309         sctp_walk_params(param, hdr, params) {
310                 /* sctp_strreset_tsnreq is actually the basic structure
311                  * of all stream reconf params, so it's safe to use it
312                  * to access request_seq.
313                  */
314                 struct sctp_strreset_tsnreq *req = param.v;
315
316                 if (req->request_seq == resp_seq)
317                         return param.v;
318         }
319
320         return NULL;
321 }
322
323 struct sctp_chunk *sctp_process_strreset_outreq(
324                                 struct sctp_association *asoc,
325                                 union sctp_params param,
326                                 struct sctp_ulpevent **evp)
327 {
328         struct sctp_strreset_outreq *outreq = param.v;
329         struct sctp_stream *stream = asoc->stream;
330         __u16 i, nums, flags = 0, *str_p = NULL;
331         __u32 result = SCTP_STRRESET_DENIED;
332         __u32 request_seq;
333
334         request_seq = ntohl(outreq->request_seq);
335
336         if (ntohl(outreq->send_reset_at_tsn) >
337             sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map)) {
338                 result = SCTP_STRRESET_IN_PROGRESS;
339                 goto out;
340         }
341
342         if (request_seq > asoc->strreset_inseq) {
343                 result = SCTP_STRRESET_ERR_BAD_SEQNO;
344                 goto out;
345         } else if (request_seq == asoc->strreset_inseq) {
346                 asoc->strreset_inseq++;
347         }
348
349         /* Check strreset_enable after inseq inc, as sender cannot tell
350          * the peer doesn't enable strreset after receiving response with
351          * result denied, as well as to keep consistent with bsd.
352          */
353         if (!(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ))
354                 goto out;
355
356         if (asoc->strreset_chunk) {
357                 sctp_paramhdr_t *param_hdr;
358                 struct sctp_transport *t;
359
360                 param_hdr = sctp_chunk_lookup_strreset_param(
361                                         asoc, outreq->response_seq);
362                 if (!param_hdr || param_hdr->type !=
363                                         SCTP_PARAM_RESET_IN_REQUEST) {
364                         /* same process with outstanding isn't 0 */
365                         result = SCTP_STRRESET_ERR_IN_PROGRESS;
366                         goto out;
367                 }
368
369                 asoc->strreset_outstanding--;
370                 asoc->strreset_outseq++;
371
372                 if (!asoc->strreset_outstanding) {
373                         t = asoc->strreset_chunk->transport;
374                         if (del_timer(&t->reconf_timer))
375                                 sctp_transport_put(t);
376
377                         sctp_chunk_put(asoc->strreset_chunk);
378                         asoc->strreset_chunk = NULL;
379                 }
380
381                 flags = SCTP_STREAM_RESET_INCOMING_SSN;
382         }
383
384         nums = (ntohs(param.p->length) - sizeof(*outreq)) / 2;
385         if (nums) {
386                 str_p = outreq->list_of_streams;
387                 for (i = 0; i < nums; i++) {
388                         if (ntohs(str_p[i]) >= stream->incnt) {
389                                 result = SCTP_STRRESET_ERR_WRONG_SSN;
390                                 goto out;
391                         }
392                 }
393
394                 for (i = 0; i < nums; i++)
395                         stream->in[ntohs(str_p[i])].ssn = 0;
396         } else {
397                 for (i = 0; i < stream->incnt; i++)
398                         stream->in[i].ssn = 0;
399         }
400
401         result = SCTP_STRRESET_PERFORMED;
402
403         *evp = sctp_ulpevent_make_stream_reset_event(asoc,
404                 flags | SCTP_STREAM_RESET_OUTGOING_SSN, nums, str_p,
405                 GFP_ATOMIC);
406
407 out:
408         return sctp_make_strreset_resp(asoc, result, request_seq);
409 }