OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [net/] [sctp/] [sm_make_chunk.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/* SCTP kernel reference Implementation
2
 * (C) Copyright IBM Corp. 2001, 2003
3
 * Copyright (c) 1999-2000 Cisco, Inc.
4
 * Copyright (c) 1999-2001 Motorola, Inc.
5
 * Copyright (c) 2001-2002 Intel Corp.
6
 *
7
 * This file is part of the SCTP kernel reference Implementation
8
 *
9
 * This file includes part of the implementation of the add-IP extension,
10
 * based on <draft-ietf-tsvwg-addip-sctp-02.txt> June 29, 2001,
11
 * for the SCTP kernel reference Implementation.
12
 *
13
 * These functions work with the state functions in sctp_sm_statefuns.c
14
 * to implement the state operations.  These functions implement the
15
 * steps which require modifying existing data structures.
16
 *
17
 * The SCTP reference implementation is free software;
18
 * you can redistribute it and/or modify it under the terms of
19
 * the GNU General Public License as published by
20
 * the Free Software Foundation; either version 2, or (at your option)
21
 * any later version.
22
 *
23
 * The SCTP reference implementation is distributed in the hope that it
24
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
25
 *                 ************************
26
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
27
 * See the GNU General Public License for more details.
28
 *
29
 * You should have received a copy of the GNU General Public License
30
 * along with GNU CC; see the file COPYING.  If not, write to
31
 * the Free Software Foundation, 59 Temple Place - Suite 330,
32
 * Boston, MA 02111-1307, USA.
33
 *
34
 * Please send any bug reports or fixes you make to the
35
 * email address(es):
36
 *    lksctp developers <lksctp-developers@lists.sourceforge.net>
37
 *
38
 * Or submit a bug report through the following website:
39
 *    http://www.sf.net/projects/lksctp
40
 *
41
 * Written or modified by:
42
 *    La Monte H.P. Yarroll <piggy@acm.org>
43
 *    Karl Knutson          <karl@athena.chicago.il.us>
44
 *    C. Robin              <chris@hundredacre.ac.uk>
45
 *    Jon Grimm             <jgrimm@us.ibm.com>
46
 *    Xingang Guo           <xingang.guo@intel.com>
47
 *    Dajiang Zhang         <dajiang.zhang@nokia.com>
48
 *    Sridhar Samudrala     <sri@us.ibm.com>
49
 *    Daisy Chang           <daisyc@us.ibm.com>
50
 *    Ardelle Fan           <ardelle.fan@intel.com>
51
 *    Kevin Gao             <kevin.gao@intel.com>
52
 *
53
 * Any bugs reported given to us we will try to fix... any fixes shared will
54
 * be incorporated into the next SCTP release.
55
 */
56
 
57
#include <linux/types.h>
58
#include <linux/kernel.h>
59
#include <linux/ip.h>
60
#include <linux/ipv6.h>
61
#include <linux/net.h>
62
#include <linux/inet.h>
63
#include <asm/scatterlist.h>
64
#include <linux/crypto.h>
65
#include <net/sock.h>
66
 
67
#include <linux/skbuff.h>
68
#include <linux/random.h>       /* for get_random_bytes */
69
#include <net/sctp/sctp.h>
70
#include <net/sctp/sm.h>
71
 
72
extern kmem_cache_t *sctp_chunk_cachep;
73
 
74
/* What was the inbound interface for this chunk? */
75
int sctp_chunk_iif(const struct sctp_chunk *chunk)
76
{
77
        struct sctp_af *af;
78
        int iif = 0;
79
 
80
        af = sctp_get_af_specific(ipver2af(chunk->skb->nh.iph->version));
81
        if (af)
82
                iif = af->skb_iif(chunk->skb);
83
 
84
        return iif;
85
}
86
 
87
/* RFC 2960 3.3.2 Initiation (INIT) (1)
88
 *
89
 * Note 2: The ECN capable field is reserved for future use of
90
 * Explicit Congestion Notification.
91
 */
92
static const sctp_ecn_capable_param_t ecap_param = {
93
        {
94
                SCTP_PARAM_ECN_CAPABLE,
95
                __constant_htons(sizeof(sctp_ecn_capable_param_t)),
96
        }
97
};
98
 
99
/* A helper to initialize to initialize an op error inside a
100
 * provided chunk, as most cause codes will be embedded inside an
101
 * abort chunk.
102
 */
103
void  sctp_init_cause(struct sctp_chunk *chunk, __u16 cause_code,
104
                      const void *payload, size_t paylen)
105
{
106
        sctp_errhdr_t err;
107
        int padlen;
108
        __u16 len;
109
 
110
        /* Cause code constants are now defined in network order.  */
111
        err.cause = cause_code;
112
        len = sizeof(sctp_errhdr_t) + paylen;
113
        padlen = len % 4;
114
        err.length  = htons(len);
115
        len += padlen;
116
        sctp_addto_chunk(chunk, sizeof(sctp_errhdr_t), &err);
117
        chunk->subh.err_hdr = sctp_addto_chunk(chunk, paylen, payload);
118
}
119
 
120
/* 3.3.2 Initiation (INIT) (1)
121
 *
122
 * This chunk is used to initiate a SCTP association between two
123
 * endpoints. The format of the INIT chunk is shown below:
124
 *
125
 *     0                   1                   2                   3
126
 *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
127
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
128
 *    |   Type = 1    |  Chunk Flags  |      Chunk Length             |
129
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
130
 *    |                         Initiate Tag                          |
131
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
132
 *    |           Advertised Receiver Window Credit (a_rwnd)          |
133
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
134
 *    |  Number of Outbound Streams   |  Number of Inbound Streams    |
135
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
136
 *    |                          Initial TSN                          |
137
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
138
 *    \                                                               \
139
 *    /              Optional/Variable-Length Parameters              /
140
 *    \                                                               \
141
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
142
 *
143
 *
144
 * The INIT chunk contains the following parameters. Unless otherwise
145
 * noted, each parameter MUST only be included once in the INIT chunk.
146
 *
147
 * Fixed Parameters                     Status
148
 * ----------------------------------------------
149
 * Initiate Tag                        Mandatory
150
 * Advertised Receiver Window Credit   Mandatory
151
 * Number of Outbound Streams          Mandatory
152
 * Number of Inbound Streams           Mandatory
153
 * Initial TSN                         Mandatory
154
 *
155
 * Variable Parameters                  Status     Type Value
156
 * -------------------------------------------------------------
157
 * IPv4 Address (Note 1)               Optional    5
158
 * IPv6 Address (Note 1)               Optional    6
159
 * Cookie Preservative                 Optional    9
160
 * Reserved for ECN Capable (Note 2)   Optional    32768 (0x8000)
161
 * Host Name Address (Note 3)          Optional    11
162
 * Supported Address Types (Note 4)    Optional    12
163
 */
164
struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
165
                             const struct sctp_bind_addr *bp,
166
                             int gfp, int vparam_len)
167
{
168
        sctp_inithdr_t init;
169
        union sctp_params addrs;
170
        size_t chunksize;
171
        struct sctp_chunk *retval = NULL;
172
        int num_types, addrs_len = 0;
173
        struct sctp_opt *sp;
174
        sctp_supported_addrs_param_t sat;
175
        __u16 types[2];
176
 
177
        /* RFC 2960 3.3.2 Initiation (INIT) (1)
178
         *
179
         * Note 1: The INIT chunks can contain multiple addresses that
180
         * can be IPv4 and/or IPv6 in any combination.
181
         */
182
        retval = NULL;
183
 
184
        /* Convert the provided bind address list to raw format. */
185
        addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, gfp);
186
 
187
        init.init_tag              = htonl(asoc->c.my_vtag);
188
        init.a_rwnd                = htonl(asoc->rwnd);
189
        init.num_outbound_streams  = htons(asoc->c.sinit_num_ostreams);
190
        init.num_inbound_streams   = htons(asoc->c.sinit_max_instreams);
191
        init.initial_tsn           = htonl(asoc->c.initial_tsn);
192
 
193
        /* How many address types are needed? */
194
        sp = sctp_sk(asoc->base.sk);
195
        num_types = sp->pf->supported_addrs(sp, types);
196
 
197
        chunksize = sizeof(init) + addrs_len + SCTP_SAT_LEN(num_types);
198
        chunksize += sizeof(ecap_param);
199
        chunksize += vparam_len;
200
 
201
        /* RFC 2960 3.3.2 Initiation (INIT) (1)
202
         *
203
         * Note 3: An INIT chunk MUST NOT contain more than one Host
204
         * Name address parameter. Moreover, the sender of the INIT
205
         * MUST NOT combine any other address types with the Host Name
206
         * address in the INIT. The receiver of INIT MUST ignore any
207
         * other address types if the Host Name address parameter is
208
         * present in the received INIT chunk.
209
         *
210
         * PLEASE DO NOT FIXME [This version does not support Host Name.]
211
         */
212
 
213
        retval = sctp_make_chunk(asoc, SCTP_CID_INIT, 0, chunksize);
214
        if (!retval)
215
                goto nodata;
216
 
217
        retval->subh.init_hdr =
218
                sctp_addto_chunk(retval, sizeof(init), &init);
219
        retval->param_hdr.v =
220
                sctp_addto_chunk(retval, addrs_len, addrs.v);
221
 
222
        /* RFC 2960 3.3.2 Initiation (INIT) (1)
223
         *
224
         * Note 4: This parameter, when present, specifies all the
225
         * address types the sending endpoint can support. The absence
226
         * of this parameter indicates that the sending endpoint can
227
         * support any address type.
228
         */
229
        sat.param_hdr.type = SCTP_PARAM_SUPPORTED_ADDRESS_TYPES;
230
        sat.param_hdr.length = htons(SCTP_SAT_LEN(num_types));
231
        sctp_addto_chunk(retval, sizeof(sat), &sat);
232
        sctp_addto_chunk(retval, num_types * sizeof(__u16), &types);
233
 
234
        sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
235
nodata:
236
        if (addrs.v)
237
                kfree(addrs.v);
238
        return retval;
239
}
240
 
241
struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
242
                                 const struct sctp_chunk *chunk,
243
                                 int gfp, int unkparam_len)
244
{
245
        sctp_inithdr_t initack;
246
        struct sctp_chunk *retval;
247
        union sctp_params addrs;
248
        int addrs_len;
249
        sctp_cookie_param_t *cookie;
250
        int cookie_len;
251
        size_t chunksize;
252
 
253
        retval = NULL;
254
 
255
        /* Note: there may be no addresses to embed. */
256
        addrs = sctp_bind_addrs_to_raw(&asoc->base.bind_addr, &addrs_len, gfp);
257
 
258
        initack.init_tag                = htonl(asoc->c.my_vtag);
259
        initack.a_rwnd                  = htonl(asoc->rwnd);
260
        initack.num_outbound_streams    = htons(asoc->c.sinit_num_ostreams);
261
        initack.num_inbound_streams     = htons(asoc->c.sinit_max_instreams);
262
        initack.initial_tsn             = htonl(asoc->c.initial_tsn);
263
 
264
        /* FIXME:  We really ought to build the cookie right
265
         * into the packet instead of allocating more fresh memory.
266
         */
267
        cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
268
                                  addrs.v, addrs_len);
269
        if (!cookie)
270
                goto nomem_cookie;
271
 
272
        /* Calculate the total size of allocation, include the reserved
273
         * space for reporting unknown parameters if it is specified.
274
         */
275
        chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;
276
 
277
        /* Tell peer that we'll do ECN only if peer advertised such cap.  */
278
        if (asoc->peer.ecn_capable)
279
                chunksize += sizeof(ecap_param);
280
 
281
        /* Now allocate and fill out the chunk.  */
282
        retval = sctp_make_chunk(asoc, SCTP_CID_INIT_ACK, 0, chunksize);
283
        if (!retval)
284
                goto nomem_chunk;
285
 
286
        /* Per the advice in RFC 2960 6.4, send this reply to
287
         * the source of the INIT packet.
288
         */
289
        retval->transport = chunk->transport;
290
        retval->subh.init_hdr =
291
                sctp_addto_chunk(retval, sizeof(initack), &initack);
292
        retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
293
        sctp_addto_chunk(retval, cookie_len, cookie);
294
        if (asoc->peer.ecn_capable)
295
                sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
296
 
297
        /* We need to remove the const qualifier at this point.  */
298
        retval->asoc = (struct sctp_association *) asoc;
299
 
300
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
301
         *
302
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
303
         * HEARTBEAT ACK, * etc.) to the same destination transport
304
         * address from which it received the DATA or control chunk
305
         * to which it is replying.
306
         *
307
         * [INIT ACK back to where the INIT came from.]
308
         */
309
        if (chunk)
310
                retval->transport = chunk->transport;
311
 
312
nomem_chunk:
313
        kfree(cookie);
314
nomem_cookie:
315
        if (addrs.v)
316
                kfree(addrs.v);
317
        return retval;
318
}
319
 
320
/* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
321
 *
322
 * This chunk is used only during the initialization of an association.
323
 * It is sent by the initiator of an association to its peer to complete
324
 * the initialization process. This chunk MUST precede any DATA chunk
325
 * sent within the association, but MAY be bundled with one or more DATA
326
 * chunks in the same packet.
327
 *
328
 *      0                   1                   2                   3
329
 *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
330
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
331
 *     |   Type = 10   |Chunk  Flags   |         Length                |
332
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
333
 *     /                     Cookie                                    /
334
 *     \                                                               \
335
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
336
 *
337
 * Chunk Flags: 8 bit
338
 *
339
 *   Set to zero on transmit and ignored on receipt.
340
 *
341
 * Length: 16 bits (unsigned integer)
342
 *
343
 *   Set to the size of the chunk in bytes, including the 4 bytes of
344
 *   the chunk header and the size of the Cookie.
345
 *
346
 * Cookie: variable size
347
 *
348
 *   This field must contain the exact cookie received in the
349
 *   State Cookie parameter from the previous INIT ACK.
350
 *
351
 *   An implementation SHOULD make the cookie as small as possible
352
 *   to insure interoperability.
353
 */
354
struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
355
                                    const struct sctp_chunk *chunk)
356
{
357
        struct sctp_chunk *retval;
358
        void *cookie;
359
        int cookie_len;
360
 
361
        cookie = asoc->peer.cookie;
362
        cookie_len = asoc->peer.cookie_len;
363
 
364
        /* Build a cookie echo chunk.  */
365
        retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ECHO, 0, cookie_len);
366
        if (!retval)
367
                goto nodata;
368
        retval->subh.cookie_hdr =
369
                sctp_addto_chunk(retval, cookie_len, cookie);
370
 
371
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
372
         *
373
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
374
         * HEARTBEAT ACK, * etc.) to the same destination transport
375
         * address from which it * received the DATA or control chunk
376
         * to which it is replying.
377
         *
378
         * [COOKIE ECHO back to where the INIT ACK came from.]
379
         */
380
        if (chunk)
381
                retval->transport = chunk->transport;
382
 
383
nodata:
384
        return retval;
385
}
386
 
387
/* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
388
 *
389
 * This chunk is used only during the initialization of an
390
 * association.  It is used to acknowledge the receipt of a COOKIE
391
 * ECHO chunk.  This chunk MUST precede any DATA or SACK chunk sent
392
 * within the association, but MAY be bundled with one or more DATA
393
 * chunks or SACK chunk in the same SCTP packet.
394
 *
395
 *      0                   1                   2                   3
396
 *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
397
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
398
 *     |   Type = 11   |Chunk  Flags   |     Length = 4                |
399
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
400
 *
401
 * Chunk Flags: 8 bits
402
 *
403
 *   Set to zero on transmit and ignored on receipt.
404
 */
405
struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
406
                                   const struct sctp_chunk *chunk)
407
{
408
        struct sctp_chunk *retval;
409
 
410
        retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ACK, 0, 0);
411
 
412
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
413
         *
414
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
415
         * HEARTBEAT ACK, * etc.) to the same destination transport
416
         * address from which it * received the DATA or control chunk
417
         * to which it is replying.
418
         *
419
         * [COOKIE ACK back to where the COOKIE ECHO came from.]
420
         */
421
        if (retval && chunk)
422
                retval->transport = chunk->transport;
423
 
424
        return retval;
425
}
426
 
427
/*
428
 *  Appendix A: Explicit Congestion Notification:
429
 *  CWR:
430
 *
431
 *  RFC 2481 details a specific bit for a sender to send in the header of
432
 *  its next outbound TCP segment to indicate to its peer that it has
433
 *  reduced its congestion window.  This is termed the CWR bit.  For
434
 *  SCTP the same indication is made by including the CWR chunk.
435
 *  This chunk contains one data element, i.e. the TSN number that
436
 *  was sent in the ECNE chunk.  This element represents the lowest
437
 *  TSN number in the datagram that was originally marked with the
438
 *  CE bit.
439
 *
440
 *     0                   1                   2                   3
441
 *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
442
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
443
 *    | Chunk Type=13 | Flags=00000000|    Chunk Length = 8           |
444
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
445
 *    |                      Lowest TSN Number                        |
446
 *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
447
 *
448
 *     Note: The CWR is considered a Control chunk.
449
 */
450
struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
451
                            const __u32 lowest_tsn,
452
                            const struct sctp_chunk *chunk)
453
{
454
        struct sctp_chunk *retval;
455
        sctp_cwrhdr_t cwr;
456
 
457
        cwr.lowest_tsn = htonl(lowest_tsn);
458
        retval = sctp_make_chunk(asoc, SCTP_CID_ECN_CWR, 0,
459
                                 sizeof(sctp_cwrhdr_t));
460
 
461
        if (!retval)
462
                goto nodata;
463
 
464
        retval->subh.ecn_cwr_hdr =
465
                sctp_addto_chunk(retval, sizeof(cwr), &cwr);
466
 
467
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
468
         *
469
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
470
         * HEARTBEAT ACK, * etc.) to the same destination transport
471
         * address from which it * received the DATA or control chunk
472
         * to which it is replying.
473
         *
474
         * [Report a reduced congestion window back to where the ECNE
475
         * came from.]
476
         */
477
        if (chunk)
478
                retval->transport = chunk->transport;
479
 
480
nodata:
481
        return retval;
482
}
483
 
484
/* Make an ECNE chunk.  This is a congestion experienced report.  */
485
struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
486
                             const __u32 lowest_tsn)
487
{
488
        struct sctp_chunk *retval;
489
        sctp_ecnehdr_t ecne;
490
 
491
        ecne.lowest_tsn = htonl(lowest_tsn);
492
        retval = sctp_make_chunk(asoc, SCTP_CID_ECN_ECNE, 0,
493
                                 sizeof(sctp_ecnehdr_t));
494
        if (!retval)
495
                goto nodata;
496
        retval->subh.ecne_hdr =
497
                sctp_addto_chunk(retval, sizeof(ecne), &ecne);
498
 
499
nodata:
500
        return retval;
501
}
502
 
503
/* Make a DATA chunk for the given association from the provided
504
 * parameters.  However, do not populate the data payload.
505
 */
506
struct sctp_chunk *sctp_make_datafrag_empty(struct sctp_association *asoc,
507
                                       const struct sctp_sndrcvinfo *sinfo,
508
                                       int data_len, __u8 flags, __u16 ssn)
509
{
510
        struct sctp_chunk *retval;
511
        struct sctp_datahdr dp;
512
        int chunk_len;
513
 
514
        /* We assign the TSN as LATE as possible, not here when
515
         * creating the chunk.
516
         */
517
        dp.tsn = 0;
518
        dp.stream = htons(sinfo->sinfo_stream);
519
        dp.ppid   = sinfo->sinfo_ppid;
520
 
521
        /* Set the flags for an unordered send.  */
522
        if (sinfo->sinfo_flags & MSG_UNORDERED) {
523
                flags |= SCTP_DATA_UNORDERED;
524
                dp.ssn = 0;
525
        } else
526
                dp.ssn = htons(ssn);
527
 
528
        chunk_len = sizeof(dp) + data_len;
529
        retval = sctp_make_chunk(asoc, SCTP_CID_DATA, flags, chunk_len);
530
        if (!retval)
531
                goto nodata;
532
 
533
        retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
534
        memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
535
 
536
nodata:
537
        return retval;
538
}
539
 
540
/* Make a DATA chunk for the given association.  Populate the data
541
 * payload.
542
 */
543
struct sctp_chunk *sctp_make_datafrag(struct sctp_association *asoc,
544
                                 const struct sctp_sndrcvinfo *sinfo,
545
                                 int data_len, const __u8 *data,
546
                                 __u8 flags, __u16 ssn)
547
{
548
        struct sctp_chunk *retval;
549
 
550
        retval = sctp_make_datafrag_empty(asoc, sinfo, data_len, flags, ssn);
551
        if (retval)
552
                sctp_addto_chunk(retval, data_len, data);
553
 
554
        return retval;
555
}
556
 
557
/* Make a DATA chunk for the given association to ride on stream id
558
 * 'stream', with a payload id of 'payload', and a body of 'data'.
559
 */
560
struct sctp_chunk *sctp_make_data(struct sctp_association *asoc,
561
                             const struct sctp_sndrcvinfo *sinfo,
562
                             int data_len, const __u8 *data)
563
{
564
        struct sctp_chunk *retval = NULL;
565
 
566
        retval = sctp_make_data_empty(asoc, sinfo, data_len);
567
        if (retval)
568
                sctp_addto_chunk(retval, data_len, data);
569
        return retval;
570
}
571
 
572
/* Make a DATA chunk for the given association to ride on stream id
573
 * 'stream', with a payload id of 'payload', and a body big enough to
574
 * hold 'data_len' octets of data.  We use this version when we need
575
 * to build the message AFTER allocating memory.
576
 */
577
struct sctp_chunk *sctp_make_data_empty(struct sctp_association *asoc,
578
                                   const struct sctp_sndrcvinfo *sinfo,
579
                                   int data_len)
580
{
581
        __u8 flags = SCTP_DATA_NOT_FRAG;
582
 
583
        return sctp_make_datafrag_empty(asoc, sinfo, data_len, flags, 0);
584
}
585
 
586
/* Create a selective ackowledgement (SACK) for the given
587
 * association.  This reports on which TSN's we've seen to date,
588
 * including duplicates and gaps.
589
 */
590
struct sctp_chunk *sctp_make_sack(const struct sctp_association *asoc)
591
{
592
        struct sctp_chunk *retval;
593
        struct sctp_sackhdr sack;
594
        int len;
595
        __u32 ctsn;
596
        __u16 num_gabs, num_dup_tsns;
597
        struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
598
 
599
        ctsn = sctp_tsnmap_get_ctsn(map);
600
        SCTP_DEBUG_PRINTK("sackCTSNAck sent:  0x%x.\n", ctsn);
601
 
602
        /* How much room is needed in the chunk? */
603
        num_gabs = sctp_tsnmap_num_gabs(map);
604
        num_dup_tsns = sctp_tsnmap_num_dups(map);
605
 
606
        /* Initialize the SACK header.  */
607
        sack.cum_tsn_ack            = htonl(ctsn);
608
        sack.a_rwnd                 = htonl(asoc->a_rwnd);
609
        sack.num_gap_ack_blocks     = htons(num_gabs);
610
        sack.num_dup_tsns           = htons(num_dup_tsns);
611
 
612
        len = sizeof(sack)
613
                + sizeof(struct sctp_gap_ack_block) * num_gabs
614
                + sizeof(__u32) * num_dup_tsns;
615
 
616
        /* Create the chunk.  */
617
        retval = sctp_make_chunk(asoc, SCTP_CID_SACK, 0, len);
618
        if (!retval)
619
                goto nodata;
620
 
621
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
622
         *
623
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
624
         * HEARTBEAT ACK, etc.) to the same destination transport
625
         * address from which it received the DATA or control chunk to
626
         * which it is replying.  This rule should also be followed if
627
         * the endpoint is bundling DATA chunks together with the
628
         * reply chunk.
629
         *
630
         * However, when acknowledging multiple DATA chunks received
631
         * in packets from different source addresses in a single
632
         * SACK, the SACK chunk may be transmitted to one of the
633
         * destination transport addresses from which the DATA or
634
         * control chunks being acknowledged were received.
635
         *
636
         * [BUG:  We do not implement the following paragraph.
637
         * Perhaps we should remember the last transport we used for a
638
         * SACK and avoid that (if possible) if we have seen any
639
         * duplicates. --piggy]
640
         *
641
         * When a receiver of a duplicate DATA chunk sends a SACK to a
642
         * multi- homed endpoint it MAY be beneficial to vary the
643
         * destination address and not use the source address of the
644
         * DATA chunk.  The reason being that receiving a duplicate
645
         * from a multi-homed endpoint might indicate that the return
646
         * path (as specified in the source address of the DATA chunk)
647
         * for the SACK is broken.
648
         *
649
         * [Send to the address from which we last received a DATA chunk.]
650
         */
651
        retval->transport = asoc->peer.last_data_from;
652
 
653
        retval->subh.sack_hdr =
654
                sctp_addto_chunk(retval, sizeof(sack), &sack);
655
 
656
        /* Add the gap ack block information.   */
657
        if (num_gabs)
658
                sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
659
                                 sctp_tsnmap_get_gabs(map));
660
 
661
        /* Add the duplicate TSN information.  */
662
        if (num_dup_tsns)
663
                sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
664
                                 sctp_tsnmap_get_dups(map));
665
 
666
nodata:
667
        return retval;
668
}
669
 
670
/* Make a SHUTDOWN chunk. */
671
struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
672
                                      const struct sctp_chunk *chunk)
673
{
674
        struct sctp_chunk *retval;
675
        sctp_shutdownhdr_t shut;
676
        __u32 ctsn;
677
 
678
        ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
679
        shut.cum_tsn_ack = htonl(ctsn);
680
 
681
        retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN, 0,
682
                                 sizeof(sctp_shutdownhdr_t));
683
        if (!retval)
684
                goto nodata;
685
 
686
        retval->subh.shutdown_hdr =
687
                sctp_addto_chunk(retval, sizeof(shut), &shut);
688
 
689
        if (chunk)
690
                retval->transport = chunk->transport;
691
nodata:
692
        return retval;
693
}
694
 
695
struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
696
                                     const struct sctp_chunk *chunk)
697
{
698
        struct sctp_chunk *retval;
699
 
700
        retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0);
701
 
702
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
703
         *
704
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
705
         * HEARTBEAT ACK, * etc.) to the same destination transport
706
         * address from which it * received the DATA or control chunk
707
         * to which it is replying.
708
         *
709
         * [ACK back to where the SHUTDOWN came from.]
710
         */
711
        if (retval && chunk)
712
                retval->transport = chunk->transport;
713
 
714
        return retval;
715
}
716
 
717
struct sctp_chunk *sctp_make_shutdown_complete(
718
        const struct sctp_association *asoc,
719
        const struct sctp_chunk *chunk)
720
{
721
        struct sctp_chunk *retval;
722
        __u8 flags = 0;
723
 
724
        /* Maybe set the T-bit if we have no association. */
725
        flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
726
 
727
        retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags, 0);
728
 
729
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
730
         *
731
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
732
         * HEARTBEAT ACK, * etc.) to the same destination transport
733
         * address from which it * received the DATA or control chunk
734
         * to which it is replying.
735
         *
736
         * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
737
         * came from.]
738
         */
739
        if (retval && chunk)
740
                retval->transport = chunk->transport;
741
 
742
        return retval;
743
}
744
 
745
/* Create an ABORT.  Note that we set the T bit if we have no
746
 * association.
747
 */
748
struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
749
                              const struct sctp_chunk *chunk,
750
                              const size_t hint)
751
{
752
        struct sctp_chunk *retval;
753
        __u8 flags = 0;
754
 
755
        /* Maybe set the T-bit if we have no association.  */
756
        flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
757
 
758
        retval = sctp_make_chunk(asoc, SCTP_CID_ABORT, flags, hint);
759
 
760
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
761
         *
762
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
763
         * HEARTBEAT ACK, * etc.) to the same destination transport
764
         * address from which it * received the DATA or control chunk
765
         * to which it is replying.
766
         *
767
         * [ABORT back to where the offender came from.]
768
         */
769
        if (retval && chunk)
770
                retval->transport = chunk->transport;
771
 
772
        return retval;
773
}
774
 
775
/* Helper to create ABORT with a NO_USER_DATA error.  */
776
struct sctp_chunk *sctp_make_abort_no_data(
777
        const struct sctp_association *asoc,
778
        const struct sctp_chunk *chunk, __u32 tsn)
779
{
780
        struct sctp_chunk *retval;
781
        __u32 payload;
782
 
783
        retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t)
784
                                 + sizeof(tsn));
785
 
786
        if (!retval)
787
                goto no_mem;
788
 
789
        /* Put the tsn back into network byte order.  */
790
        payload = htonl(tsn);
791
        sctp_init_cause(retval, SCTP_ERROR_NO_DATA, (const void *)&payload,
792
                        sizeof(payload));
793
 
794
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
795
         *
796
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
797
         * HEARTBEAT ACK, * etc.) to the same destination transport
798
         * address from which it * received the DATA or control chunk
799
         * to which it is replying.
800
         *
801
         * [ABORT back to where the offender came from.]
802
         */
803
        if (chunk)
804
                retval->transport = chunk->transport;
805
 
806
no_mem:
807
        return retval;
808
}
809
 
810
/* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error.  */
811
struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
812
                                   const struct sctp_chunk *chunk,
813
                                   const struct msghdr *msg)
814
{
815
        struct sctp_chunk *retval;
816
        void *payload = NULL, *payoff;
817
        size_t paylen = 0;
818
        struct iovec *iov = NULL;
819
        int iovlen = 0;
820
 
821
        if (msg) {
822
                iov = msg->msg_iov;
823
                iovlen = msg->msg_iovlen;
824
                paylen = get_user_iov_size(iov, iovlen);
825
        }
826
 
827
        retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t) + paylen);
828
        if (!retval)
829
                goto err_chunk;
830
 
831
        if (paylen) {
832
                /* Put the msg_iov together into payload.  */
833
                payload = kmalloc(paylen, GFP_ATOMIC);
834
                if (!payload)
835
                        goto err_payload;
836
                payoff = payload;
837
 
838
                for (; iovlen > 0; --iovlen) {
839
                        if (copy_from_user(payoff, iov->iov_base,iov->iov_len))
840
                                goto err_copy;
841
                        payoff += iov->iov_len;
842
                        iov++;
843
                }
844
        }
845
 
846
        sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, payload, paylen);
847
 
848
        if (paylen)
849
                kfree(payload);
850
 
851
        return retval;
852
 
853
err_copy:
854
        kfree(payload);
855
err_payload:
856
        sctp_chunk_free(retval);
857
        retval = NULL;
858
err_chunk:
859
        return retval;
860
}
861
 
862
/* Make a HEARTBEAT chunk.  */
863
struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
864
                                  const struct sctp_transport *transport,
865
                                  const void *payload, const size_t paylen)
866
{
867
        struct sctp_chunk *retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT,
868
                                                    0, paylen);
869
 
870
        if (!retval)
871
                goto nodata;
872
 
873
        /* Cast away the 'const', as this is just telling the chunk
874
         * what transport it belongs to.
875
         */
876
        retval->transport = (struct sctp_transport *) transport;
877
        retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
878
 
879
nodata:
880
        return retval;
881
}
882
 
883
struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
884
                                      const struct sctp_chunk *chunk,
885
                                      const void *payload, const size_t paylen)
886
{
887
        struct sctp_chunk *retval;
888
 
889
        retval  = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen);
890
        if (!retval)
891
                goto nodata;
892
 
893
        retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
894
 
895
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
896
         *
897
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
898
         * HEARTBEAT ACK, * etc.) to the same destination transport
899
         * address from which it * received the DATA or control chunk
900
         * to which it is replying.
901
         *
902
         * [HBACK back to where the HEARTBEAT came from.]
903
         */
904
        if (chunk)
905
                retval->transport = chunk->transport;
906
 
907
nodata:
908
        return retval;
909
}
910
 
911
/* Create an Operation Error chunk with the specified space reserved.
912
 * This routine can be used for containing multiple causes in the chunk.
913
 */
914
struct sctp_chunk *sctp_make_op_error_space(
915
        const struct sctp_association *asoc,
916
        const struct sctp_chunk *chunk,
917
        size_t size)
918
{
919
        struct sctp_chunk *retval;
920
 
921
        retval = sctp_make_chunk(asoc, SCTP_CID_ERROR, 0,
922
                                 sizeof(sctp_errhdr_t) + size);
923
        if (!retval)
924
                goto nodata;
925
 
926
        /* RFC 2960 6.4 Multi-homed SCTP Endpoints
927
         *
928
         * An endpoint SHOULD transmit reply chunks (e.g., SACK,
929
         * HEARTBEAT ACK, etc.) to the same destination transport
930
         * address from which it received the DATA or control chunk
931
         * to which it is replying.
932
         *
933
         */
934
        if (chunk)
935
                retval->transport = chunk->transport;
936
 
937
nodata:
938
        return retval;
939
}
940
 
941
/* Create an Operation Error chunk.  */
942
struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
943
                                 const struct sctp_chunk *chunk,
944
                                 __u16 cause_code, const void *payload,
945
                                 size_t paylen)
946
{
947
        struct sctp_chunk *retval;
948
 
949
        retval = sctp_make_op_error_space(asoc, chunk, paylen);
950
        if (!retval)
951
                goto nodata;
952
 
953
        sctp_init_cause(retval, cause_code, payload, paylen);
954
 
955
nodata:
956
        return retval;
957
}
958
 
959
/********************************************************************
960
 * 2nd Level Abstractions
961
 ********************************************************************/
962
 
963
/* Turn an skb into a chunk.
964
 * FIXME: Eventually move the structure directly inside the skb->cb[].
965
 */
966
struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
967
                            const struct sctp_association *asoc,
968
                            struct sock *sk)
969
{
970
        struct sctp_chunk *retval;
971
 
972
        retval = kmem_cache_alloc(sctp_chunk_cachep, SLAB_ATOMIC);
973
 
974
        if (!retval)
975
                goto nodata;
976
        memset(retval, 0, sizeof(struct sctp_chunk));
977
 
978
        if (!sk) {
979
                SCTP_DEBUG_PRINTK("chunkifying skb %p w/o an sk\n", skb);
980
        }
981
 
982
        retval->skb             = skb;
983
        retval->asoc            = (struct sctp_association *)asoc;
984
        retval->resent          = 0;
985
        retval->has_tsn         = 0;
986
        retval->has_ssn         = 0;
987
        retval->rtt_in_progress = 0;
988
        retval->sent_at         = 0;
989
        retval->singleton       = 1;
990
        retval->end_of_packet   = 0;
991
        retval->ecn_ce_done     = 0;
992
        retval->pdiscard        = 0;
993
 
994
        /* sctpimpguide-05.txt Section 2.8.2
995
         * M1) Each time a new DATA chunk is transmitted
996
         * set the 'TSN.Missing.Report' count for that TSN to 0. The
997
         * 'TSN.Missing.Report' count will be used to determine missing chunks
998
         * and when to fast retransmit.
999
         */
1000
        retval->tsn_missing_report = 0;
1001
        retval->tsn_gap_acked = 0;
1002
        retval->fast_retransmit = 0;
1003
 
1004
        /* If this is a fragmented message, track all fragments
1005
         * of the message (for SEND_FAILED).
1006
         */
1007
        retval->msg = NULL;
1008
 
1009
        /* Polish the bead hole.  */
1010
        INIT_LIST_HEAD(&retval->transmitted_list);
1011
        INIT_LIST_HEAD(&retval->frag_list);
1012
        SCTP_DBG_OBJCNT_INC(chunk);
1013
        atomic_set(&retval->refcnt, 1);
1014
 
1015
 
1016
nodata:
1017
        return retval;
1018
}
1019
 
1020
/* Set chunk->source and dest based on the IP header in chunk->skb.  */
1021
void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
1022
                     union sctp_addr *dest)
1023
{
1024
        memcpy(&chunk->source, src, sizeof(union sctp_addr));
1025
        memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
1026
}
1027
 
1028
/* Extract the source address from a chunk.  */
1029
const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
1030
{
1031
        /* If we have a known transport, use that.  */
1032
        if (chunk->transport) {
1033
                return &chunk->transport->ipaddr;
1034
        } else {
1035
                /* Otherwise, extract it from the IP header.  */
1036
                return &chunk->source;
1037
        }
1038
}
1039
 
1040
/* Create a new chunk, setting the type and flags headers from the
1041
 * arguments, reserving enough space for a 'paylen' byte payload.
1042
 */
1043
struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
1044
                                   __u8 type, __u8 flags, int paylen)
1045
{
1046
        struct sctp_chunk *retval;
1047
        sctp_chunkhdr_t *chunk_hdr;
1048
        struct sk_buff *skb;
1049
        struct sock *sk;
1050
 
1051
        /* No need to allocate LL here, as this is only a chunk. */
1052
        skb = alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t) + paylen),
1053
                        GFP_ATOMIC);
1054
        if (!skb)
1055
                goto nodata;
1056
 
1057
        /* Make room for the chunk header.  */
1058
        chunk_hdr = (sctp_chunkhdr_t *)skb_put(skb, sizeof(sctp_chunkhdr_t));
1059
        chunk_hdr->type   = type;
1060
        chunk_hdr->flags  = flags;
1061
        chunk_hdr->length = htons(sizeof(sctp_chunkhdr_t));
1062
 
1063
        sk = asoc ? asoc->base.sk : NULL;
1064
        retval = sctp_chunkify(skb, asoc, sk);
1065
        if (!retval) {
1066
                kfree_skb(skb);
1067
                goto nodata;
1068
        }
1069
 
1070
        retval->chunk_hdr = chunk_hdr;
1071
        retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(struct sctp_chunkhdr);
1072
 
1073
        /* Set the skb to the belonging sock for accounting.  */
1074
        skb->sk = sk;
1075
 
1076
        return retval;
1077
nodata:
1078
        return NULL;
1079
}
1080
 
1081
 
1082
/* Release the memory occupied by a chunk.  */
1083
static void sctp_chunk_destroy(struct sctp_chunk *chunk)
1084
{
1085
        /* Free the chunk skb data and the SCTP_chunk stub itself. */
1086
        dev_kfree_skb(chunk->skb);
1087
 
1088
        SCTP_DBG_OBJCNT_DEC(chunk);
1089
        kmem_cache_free(sctp_chunk_cachep, chunk);
1090
}
1091
 
1092
/* Possibly, free the chunk.  */
1093
void sctp_chunk_free(struct sctp_chunk *chunk)
1094
{
1095
        /* Make sure that we are not on any list.  */
1096
        skb_unlink((struct sk_buff *) chunk);
1097
        list_del_init(&chunk->transmitted_list);
1098
 
1099
        /* Release our reference on the message tracker. */
1100
        if (chunk->msg)
1101
                sctp_datamsg_put(chunk->msg);
1102
 
1103
        sctp_chunk_put(chunk);
1104
}
1105
 
1106
/* Grab a reference to the chunk. */
1107
void sctp_chunk_hold(struct sctp_chunk *ch)
1108
{
1109
        atomic_inc(&ch->refcnt);
1110
}
1111
 
1112
/* Release a reference to the chunk. */
1113
void sctp_chunk_put(struct sctp_chunk *ch)
1114
{
1115
        if (atomic_dec_and_test(&ch->refcnt))
1116
                sctp_chunk_destroy(ch);
1117
}
1118
 
1119
/* Append bytes to the end of a chunk.  Will panic if chunk is not big
1120
 * enough.
1121
 */
1122
void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
1123
{
1124
        void *target;
1125
        void *padding;
1126
        int chunklen = ntohs(chunk->chunk_hdr->length);
1127
        int padlen = chunklen % 4;
1128
 
1129
        padding = skb_put(chunk->skb, padlen);
1130
        target = skb_put(chunk->skb, len);
1131
 
1132
        memset(padding, 0, padlen);
1133
        memcpy(target, data, len);
1134
 
1135
        /* Adjust the chunk length field.  */
1136
        chunk->chunk_hdr->length = htons(chunklen + padlen + len);
1137
        chunk->chunk_end = chunk->skb->tail;
1138
 
1139
        return target;
1140
}
1141
 
1142
/* Append bytes from user space to the end of a chunk.  Will panic if
1143
 * chunk is not big enough.
1144
 * Returns a kernel err value.
1145
 */
1146
int sctp_user_addto_chunk(struct sctp_chunk *chunk, int off, int len,
1147
                          struct iovec *data)
1148
{
1149
        __u8 *target;
1150
        int err = 0;
1151
 
1152
        /* Make room in chunk for data.  */
1153
        target = skb_put(chunk->skb, len);
1154
 
1155
        /* Copy data (whole iovec) into chunk */
1156
        if ((err = memcpy_fromiovecend(target, data, off, len)))
1157
                goto out;
1158
 
1159
        /* Adjust the chunk length field.  */
1160
        chunk->chunk_hdr->length =
1161
                htons(ntohs(chunk->chunk_hdr->length) + len);
1162
        chunk->chunk_end = chunk->skb->tail;
1163
 
1164
out:
1165
        return err;
1166
}
1167
 
1168
/* Helper function to assign a TSN if needed.  This assumes that both
1169
 * the data_hdr and association have already been assigned.
1170
 */
1171
void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
1172
{
1173
        __u16 ssn;
1174
        __u16 sid;
1175
 
1176
        if (chunk->has_ssn)
1177
                return;
1178
 
1179
        /* This is the last possible instant to assign a SSN. */
1180
        if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
1181
                ssn = 0;
1182
        } else {
1183
                sid = htons(chunk->subh.data_hdr->stream);
1184
                if (chunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
1185
                        ssn = sctp_ssn_next(&chunk->asoc->ssnmap->out, sid);
1186
                else
1187
                        ssn = sctp_ssn_peek(&chunk->asoc->ssnmap->out, sid);
1188
                ssn = htons(ssn);
1189
        }
1190
 
1191
        chunk->subh.data_hdr->ssn = ssn;
1192
        chunk->has_ssn = 1;
1193
}
1194
 
1195
/* Helper function to assign a TSN if needed.  This assumes that both
1196
 * the data_hdr and association have already been assigned.
1197
 */
1198
void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
1199
{
1200
        if (!chunk->has_tsn) {
1201
                /* This is the last possible instant to
1202
                 * assign a TSN.
1203
                 */
1204
                chunk->subh.data_hdr->tsn =
1205
                        htonl(sctp_association_get_next_tsn(chunk->asoc));
1206
                chunk->has_tsn = 1;
1207
        }
1208
}
1209
 
1210
/* Create a CLOSED association to use with an incoming packet.  */
1211
struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
1212
                                        struct sctp_chunk *chunk, int gfp)
1213
{
1214
        struct sctp_association *asoc;
1215
        struct sk_buff *skb;
1216
        sctp_scope_t scope;
1217
        struct sctp_af *af;
1218
 
1219
        /* Create the bare association.  */
1220
        scope = sctp_scope(sctp_source(chunk));
1221
        asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
1222
        if (!asoc)
1223
                goto nodata;
1224
        asoc->temp = 1;
1225
        skb = chunk->skb;
1226
        /* Create an entry for the source address of the packet.  */
1227
        af = sctp_get_af_specific(ipver2af(skb->nh.iph->version));
1228
        if (unlikely(!af))
1229
                goto fail;
1230
        af->from_skb(&asoc->c.peer_addr, skb, 1);
1231
nodata:
1232
        return asoc;
1233
 
1234
fail:
1235
        sctp_association_free(asoc);
1236
        return NULL;
1237
}
1238
 
1239
/* Build a cookie representing asoc.
1240
 * This INCLUDES the param header needed to put the cookie in the INIT ACK.
1241
 */
1242
sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
1243
                                      const struct sctp_association *asoc,
1244
                                      const struct sctp_chunk *init_chunk,
1245
                                      int *cookie_len,
1246
                                      const __u8 *raw_addrs, int addrs_len)
1247
{
1248
        sctp_cookie_param_t *retval;
1249
        struct sctp_signed_cookie *cookie;
1250
        struct scatterlist sg;
1251
        int headersize, bodysize;
1252
        unsigned int keylen;
1253
        char *key;
1254
 
1255
        headersize = sizeof(sctp_paramhdr_t) + SCTP_SECRET_SIZE;
1256
        bodysize = sizeof(struct sctp_cookie)
1257
                + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
1258
 
1259
        /* Pad out the cookie to a multiple to make the signature
1260
         * functions simpler to write.
1261
         */
1262
        if (bodysize % SCTP_COOKIE_MULTIPLE)
1263
                bodysize += SCTP_COOKIE_MULTIPLE
1264
                        - (bodysize % SCTP_COOKIE_MULTIPLE);
1265
        *cookie_len = headersize + bodysize;
1266
 
1267
        retval = (sctp_cookie_param_t *)kmalloc(*cookie_len, GFP_ATOMIC);
1268
 
1269
        if (!retval) {
1270
                *cookie_len = 0;
1271
                goto nodata;
1272
        }
1273
 
1274
        /* Clear this memory since we are sending this data structure
1275
         * out on the network.
1276
         */
1277
        memset(retval, 0x00, *cookie_len);
1278
        cookie = (struct sctp_signed_cookie *) retval->body;
1279
 
1280
        /* Set up the parameter header.  */
1281
        retval->p.type = SCTP_PARAM_STATE_COOKIE;
1282
        retval->p.length = htons(*cookie_len);
1283
 
1284
        /* Copy the cookie part of the association itself.  */
1285
        cookie->c = asoc->c;
1286
        /* Save the raw address list length in the cookie. */
1287
        cookie->c.raw_addr_list_len = addrs_len;
1288
 
1289
        /* Set an expiration time for the cookie.  */
1290
        do_gettimeofday(&cookie->c.expiration);
1291
        TIMEVAL_ADD(asoc->cookie_life, cookie->c.expiration);
1292
 
1293
        /* Copy the peer's init packet.  */
1294
        memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
1295
               ntohs(init_chunk->chunk_hdr->length));
1296
 
1297
        /* Copy the raw local address list of the association. */
1298
        memcpy((__u8 *)&cookie->c.peer_init[0] +
1299
               ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
1300
 
1301
        if (sctp_sk(ep->base.sk)->hmac) {
1302
                /* Sign the message.  */
1303
                sg.page = virt_to_page(&cookie->c);
1304
                sg.offset = (unsigned long)(&cookie->c) % PAGE_SIZE;
1305
                sg.length = bodysize;
1306
                keylen = SCTP_SECRET_SIZE;
1307
                key = (char *)ep->secret_key[ep->current_key];
1308
 
1309
                sctp_crypto_hmac(sctp_sk(ep->base.sk)->hmac, key, &keylen,
1310
                                 &sg, 1, cookie->signature);
1311
        }
1312
 
1313
nodata:
1314
        return retval;
1315
}
1316
 
1317
/* Unpack the cookie from COOKIE ECHO chunk, recreating the association.  */
1318
struct sctp_association *sctp_unpack_cookie(
1319
        const struct sctp_endpoint *ep,
1320
        const struct sctp_association *asoc,
1321
        struct sctp_chunk *chunk, int gfp,
1322
        int *error, struct sctp_chunk **errp)
1323
{
1324
        struct sctp_association *retval = NULL;
1325
        struct sctp_signed_cookie *cookie;
1326
        struct sctp_cookie *bear_cookie;
1327
        int headersize, bodysize, fixed_size;
1328
        __u8 digest[SCTP_SIGNATURE_SIZE];
1329
        struct scatterlist sg;
1330
        unsigned int keylen, len;
1331
        char *key;
1332
        sctp_scope_t scope;
1333
        struct sk_buff *skb = chunk->skb;
1334
 
1335
        headersize = sizeof(sctp_chunkhdr_t) + SCTP_SECRET_SIZE;
1336
        bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
1337
        fixed_size = headersize + sizeof(struct sctp_cookie);
1338
 
1339
        /* Verify that the chunk looks like it even has a cookie.
1340
         * There must be enough room for our cookie and our peer's
1341
         * INIT chunk.
1342
         */
1343
        len = ntohs(chunk->chunk_hdr->length);
1344
        if (len < fixed_size + sizeof(struct sctp_chunkhdr))
1345
                goto malformed;
1346
 
1347
        /* Verify that the cookie has been padded out. */
1348
        if (bodysize % SCTP_COOKIE_MULTIPLE)
1349
                goto malformed;
1350
 
1351
        /* Process the cookie.  */
1352
        cookie = chunk->subh.cookie_hdr;
1353
        bear_cookie = &cookie->c;
1354
 
1355
        if (!sctp_sk(ep->base.sk)->hmac)
1356
                goto no_hmac;
1357
 
1358
        /* Check the signature.  */
1359
        keylen = SCTP_SECRET_SIZE;
1360
        sg.page = virt_to_page(bear_cookie);
1361
        sg.offset = (unsigned long)(bear_cookie) % PAGE_SIZE;
1362
        sg.length = bodysize;
1363
        key = (char *)ep->secret_key[ep->current_key];
1364
 
1365
        memset(digest, 0x00, sizeof(digest));
1366
        sctp_crypto_hmac(sctp_sk(ep->base.sk)->hmac, key, &keylen, &sg,
1367
                         1, digest);
1368
 
1369
        if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1370
                /* Try the previous key. */
1371
                key = (char *)ep->secret_key[ep->last_key];
1372
                memset(digest, 0x00, sizeof(digest));
1373
                sctp_crypto_hmac(sctp_sk(ep->base.sk)->hmac, key, &keylen,
1374
                                 &sg, 1, digest);
1375
 
1376
                if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1377
                        /* Yikes!  Still bad signature! */
1378
                        *error = -SCTP_IERROR_BAD_SIG;
1379
                        goto fail;
1380
                }
1381
        }
1382
 
1383
no_hmac:
1384
        /* Check to see if the cookie is stale.  If there is already
1385
         * an association, there is no need to check cookie's expiration
1386
         * for init collision case of lost COOKIE ACK.
1387
         */
1388
        if (!asoc && tv_lt(bear_cookie->expiration, skb->stamp)) {
1389
                __u16 len;
1390
                /*
1391
                 * Section 3.3.10.3 Stale Cookie Error (3)
1392
                 *
1393
                 * Cause of error
1394
                 * ---------------
1395
                 * Stale Cookie Error:  Indicates the receipt of a valid State
1396
                 * Cookie that has expired.
1397
                 */
1398
                len = ntohs(chunk->chunk_hdr->length);
1399
                *errp = sctp_make_op_error_space(asoc, chunk, len);
1400
                if (*errp) {
1401
                        suseconds_t usecs = (skb->stamp.tv_sec -
1402
                                bear_cookie->expiration.tv_sec) * 1000000L +
1403
                                skb->stamp.tv_usec -
1404
                                bear_cookie->expiration.tv_usec;
1405
 
1406
                        usecs = htonl(usecs);
1407
                        sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
1408
                                        &usecs, sizeof(usecs));
1409
                        *error = -SCTP_IERROR_STALE_COOKIE;
1410
                } else
1411
                        *error = -SCTP_IERROR_NOMEM;
1412
 
1413
                goto fail;
1414
        }
1415
 
1416
        /* Make a new base association.  */
1417
        scope = sctp_scope(sctp_source(chunk));
1418
        retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
1419
        if (!retval) {
1420
                *error = -SCTP_IERROR_NOMEM;
1421
                goto fail;
1422
        }
1423
 
1424
        /* Set up our peer's port number.  */
1425
        retval->peer.port = ntohs(chunk->sctp_hdr->source);
1426
 
1427
        /* Populate the association from the cookie.  */
1428
        memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
1429
 
1430
        if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
1431
                                                 GFP_ATOMIC) < 0) {
1432
                *error = -SCTP_IERROR_NOMEM;
1433
                goto fail;
1434
        }
1435
 
1436
        /* Also, add the destination address. */
1437
        if (list_empty(&retval->base.bind_addr.address_list)) {
1438
                sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest,
1439
                                   GFP_ATOMIC);
1440
        }
1441
 
1442
        retval->next_tsn = retval->c.initial_tsn;
1443
        retval->ctsn_ack_point = retval->next_tsn - 1;
1444
        retval->addip_serial = retval->c.initial_tsn;
1445
 
1446
        /* The INIT stuff will be done by the side effects.  */
1447
        return retval;
1448
 
1449
fail:
1450
        if (retval)
1451
                sctp_association_free(retval);
1452
 
1453
        return NULL;
1454
 
1455
malformed:
1456
        /* Yikes!  The packet is either corrupt or deliberately
1457
         * malformed.
1458
         */
1459
        *error = -SCTP_IERROR_MALFORMED;
1460
        goto fail;
1461
}
1462
 
1463
/********************************************************************
1464
 * 3rd Level Abstractions
1465
 ********************************************************************/
1466
 
1467
struct __sctp_missing {
1468
        __u32 num_missing;
1469
        __u16 type;
1470
}  __attribute__((packed));;
1471
 
1472
/*
1473
 * Report a missing mandatory parameter.
1474
 */
1475
static int sctp_process_missing_param(const struct sctp_association *asoc,
1476
                                      sctp_param_t paramtype,
1477
                                      struct sctp_chunk *chunk,
1478
                                      struct sctp_chunk **errp)
1479
{
1480
        struct __sctp_missing report;
1481
        __u16 len;
1482
 
1483
        len = WORD_ROUND(sizeof(report));
1484
 
1485
        /* Make an ERROR chunk, preparing enough room for
1486
         * returning multiple unknown parameters.
1487
         */
1488
        if (!*errp)
1489
                *errp = sctp_make_op_error_space(asoc, chunk, len);
1490
 
1491
        if (*errp) {
1492
                report.num_missing = htonl(1);
1493
                report.type = paramtype;
1494
                sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM,
1495
                                &report, sizeof(report));
1496
        }
1497
 
1498
        /* Stop processing this chunk. */
1499
        return 0;
1500
}
1501
 
1502
/* Report an Invalid Mandatory Parameter.  */
1503
static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
1504
                                      struct sctp_chunk *chunk,
1505
                                      struct sctp_chunk **errp)
1506
{
1507
        /* Invalid Mandatory Parameter Error has no payload. */
1508
 
1509
        if (!*errp)
1510
                *errp = sctp_make_op_error_space(asoc, chunk, 0);
1511
 
1512
        if (*errp)
1513
                sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, NULL, 0);
1514
 
1515
        /* Stop processing this chunk. */
1516
        return 0;
1517
}
1518
 
1519
/* Do not attempt to handle the HOST_NAME parm.  However, do
1520
 * send back an indicator to the peer.
1521
 */
1522
static int sctp_process_hn_param(const struct sctp_association *asoc,
1523
                                 union sctp_params param,
1524
                                 struct sctp_chunk *chunk,
1525
                                 struct sctp_chunk **errp)
1526
{
1527
        __u16 len = ntohs(param.p->length);
1528
 
1529
        /* Make an ERROR chunk. */
1530
        if (!*errp)
1531
                *errp = sctp_make_op_error_space(asoc, chunk, len);
1532
 
1533
        if (*errp)
1534
                sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED,
1535
                                param.v, len);
1536
 
1537
        /* Stop processing this chunk. */
1538
        return 0;
1539
}
1540
 
1541
/* RFC 3.2.1 & the Implementers Guide 2.2.
1542
 *
1543
 * The Parameter Types are encoded such that the
1544
 * highest-order two bits specify the action that must be
1545
 * taken if the processing endpoint does not recognize the
1546
 * Parameter Type.
1547
 *
1548
 * 00 - Stop processing this SCTP chunk and discard it,
1549
 *      do not process any further chunks within it.
1550
 *
1551
 * 01 - Stop processing this SCTP chunk and discard it,
1552
 *      do not process any further chunks within it, and report
1553
 *      the unrecognized parameter in an 'Unrecognized
1554
 *      Parameter Type' (in either an ERROR or in the INIT ACK).
1555
 *
1556
 * 10 - Skip this parameter and continue processing.
1557
 *
1558
 * 11 - Skip this parameter and continue processing but
1559
 *      report the unrecognized parameter in an
1560
 *      'Unrecognized Parameter Type' (in either an ERROR or in
1561
 *      the INIT ACK).
1562
 *
1563
 * Return value:
1564
 *      0 - discard the chunk
1565
 *      1 - continue with the chunk
1566
 */
1567
static int sctp_process_unk_param(const struct sctp_association *asoc,
1568
                                  union sctp_params param,
1569
                                  struct sctp_chunk *chunk,
1570
                                  struct sctp_chunk **errp)
1571
{
1572
        int retval = 1;
1573
 
1574
        switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
1575
        case SCTP_PARAM_ACTION_DISCARD:
1576
                retval =  0;
1577
                break;
1578
        case SCTP_PARAM_ACTION_DISCARD_ERR:
1579
                retval =  0;
1580
                /* Make an ERROR chunk, preparing enough room for
1581
                 * returning multiple unknown parameters.
1582
                 */
1583
                if (NULL == *errp)
1584
                        *errp = sctp_make_op_error_space(asoc, chunk,
1585
                                        ntohs(chunk->chunk_hdr->length));
1586
 
1587
                if (*errp)
1588
                        sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
1589
                                        param.v,
1590
                                        WORD_ROUND(ntohs(param.p->length)));
1591
 
1592
                break;
1593
        case SCTP_PARAM_ACTION_SKIP:
1594
                break;
1595
        case SCTP_PARAM_ACTION_SKIP_ERR:
1596
                /* Make an ERROR chunk, preparing enough room for
1597
                 * returning multiple unknown parameters.
1598
                 */
1599
                if (NULL == *errp)
1600
                        *errp = sctp_make_op_error_space(asoc, chunk,
1601
                                        ntohs(chunk->chunk_hdr->length));
1602
 
1603
                if (*errp) {
1604
                        sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
1605
                                        param.v,
1606
                                        WORD_ROUND(ntohs(param.p->length)));
1607
                } else {
1608
                        /* If there is no memory for generating the ERROR
1609
                         * report as specified, an ABORT will be triggered
1610
                         * to the peer and the association won't be
1611
                         * established.
1612
                         */
1613
                        retval = 0;
1614
                }
1615
 
1616
                break;
1617
        default:
1618
                break;
1619
        }
1620
 
1621
        return retval;
1622
}
1623
 
1624
/* Find unrecognized parameters in the chunk.
1625
 * Return values:
1626
 *      0 - discard the chunk
1627
 *      1 - continue with the chunk
1628
 */
1629
static int sctp_verify_param(const struct sctp_association *asoc,
1630
                             union sctp_params param,
1631
                             sctp_cid_t cid,
1632
                             struct sctp_chunk *chunk,
1633
                             struct sctp_chunk **err_chunk)
1634
{
1635
        int retval = 1;
1636
 
1637
        /* FIXME - This routine is not looking at each parameter per the
1638
         * chunk type, i.e., unrecognized parameters should be further
1639
         * identified based on the chunk id.
1640
         */
1641
 
1642
        switch (param.p->type) {
1643
        case SCTP_PARAM_IPV4_ADDRESS:
1644
        case SCTP_PARAM_IPV6_ADDRESS:
1645
        case SCTP_PARAM_COOKIE_PRESERVATIVE:
1646
        case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
1647
        case SCTP_PARAM_STATE_COOKIE:
1648
        case SCTP_PARAM_HEARTBEAT_INFO:
1649
        case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
1650
        case SCTP_PARAM_ECN_CAPABLE:
1651
                break;
1652
 
1653
        case SCTP_PARAM_HOST_NAME_ADDRESS:
1654
                /* Tell the peer, we won't support this param.  */
1655
                return sctp_process_hn_param(asoc, param, chunk, err_chunk);
1656
        default:
1657
                SCTP_DEBUG_PRINTK("Unrecognized param: %d for chunk %d.\n",
1658
                                ntohs(param.p->type), cid);
1659
                return sctp_process_unk_param(asoc, param, chunk, err_chunk);
1660
 
1661
                break;
1662
        }
1663
        return retval;
1664
}
1665
 
1666
/* Verify the INIT packet before we process it.  */
1667
int sctp_verify_init(const struct sctp_association *asoc,
1668
                     sctp_cid_t cid,
1669
                     sctp_init_chunk_t *peer_init,
1670
                     struct sctp_chunk *chunk,
1671
                     struct sctp_chunk **errp)
1672
{
1673
        union sctp_params param;
1674
        int has_cookie = 0;
1675
 
1676
        /* Verify stream values are non-zero. */
1677
        if ((0 == peer_init->init_hdr.num_outbound_streams) ||
1678
            (0 == peer_init->init_hdr.num_inbound_streams)) {
1679
 
1680
                sctp_process_inv_mandatory(asoc, chunk, errp);
1681
                return 0;
1682
        }
1683
 
1684
        /* Check for missing mandatory parameters.  */
1685
        sctp_walk_params(param, peer_init, init_hdr.params) {
1686
 
1687
                if (SCTP_PARAM_STATE_COOKIE == param.p->type)
1688
                        has_cookie = 1;
1689
 
1690
        } /* for (loop through all parameters) */
1691
 
1692
        /* The only missing mandatory param possible today is
1693
         * the state cookie for an INIT-ACK chunk.
1694
         */
1695
        if ((SCTP_CID_INIT_ACK == cid) && !has_cookie) {
1696
                sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
1697
                                           chunk, errp);
1698
                return 0;
1699
        }
1700
 
1701
        /* Find unrecognized parameters. */
1702
 
1703
        sctp_walk_params(param, peer_init, init_hdr.params) {
1704
 
1705
                if (!sctp_verify_param(asoc, param, cid, chunk, errp)) {
1706
                        if (SCTP_PARAM_HOST_NAME_ADDRESS == param.p->type)
1707
                                return 0;
1708
                        else
1709
                                return 1;
1710
                }
1711
 
1712
        } /* for (loop through all parameters) */
1713
 
1714
        return 1;
1715
}
1716
 
1717
/* Unpack the parameters in an INIT packet into an association.
1718
 * Returns 0 on failure, else success.
1719
 * FIXME:  This is an association method.
1720
 */
1721
int sctp_process_init(struct sctp_association *asoc, sctp_cid_t cid,
1722
                      const union sctp_addr *peer_addr,
1723
                      sctp_init_chunk_t *peer_init, int gfp)
1724
{
1725
        union sctp_params param;
1726
        struct sctp_transport *transport;
1727
        struct list_head *pos, *temp;
1728
        char *cookie;
1729
 
1730
        /* We must include the address that the INIT packet came from.
1731
         * This is the only address that matters for an INIT packet.
1732
         * When processing a COOKIE ECHO, we retrieve the from address
1733
         * of the INIT from the cookie.
1734
         */
1735
 
1736
        /* This implementation defaults to making the first transport
1737
         * added as the primary transport.  The source address seems to
1738
         * be a a better choice than any of the embedded addresses.
1739
         */
1740
        if (peer_addr)
1741
                if(!sctp_assoc_add_peer(asoc, peer_addr, gfp))
1742
                        goto nomem;
1743
 
1744
        /* Process the initialization parameters.  */
1745
 
1746
        sctp_walk_params(param, peer_init, init_hdr.params) {
1747
 
1748
                if (!sctp_process_param(asoc, param, peer_addr, gfp))
1749
                        goto clean_up;
1750
        }
1751
 
1752
        /* The fixed INIT headers are always in network byte
1753
         * order.
1754
         */
1755
        asoc->peer.i.init_tag =
1756
                ntohl(peer_init->init_hdr.init_tag);
1757
        asoc->peer.i.a_rwnd =
1758
                ntohl(peer_init->init_hdr.a_rwnd);
1759
        asoc->peer.i.num_outbound_streams =
1760
                ntohs(peer_init->init_hdr.num_outbound_streams);
1761
        asoc->peer.i.num_inbound_streams =
1762
                ntohs(peer_init->init_hdr.num_inbound_streams);
1763
        asoc->peer.i.initial_tsn =
1764
                ntohl(peer_init->init_hdr.initial_tsn);
1765
 
1766
        /* Apply the upper bounds for output streams based on peer's
1767
         * number of inbound streams.
1768
         */
1769
        if (asoc->c.sinit_num_ostreams  >
1770
            ntohs(peer_init->init_hdr.num_inbound_streams)) {
1771
                asoc->c.sinit_num_ostreams =
1772
                        ntohs(peer_init->init_hdr.num_inbound_streams);
1773
        }
1774
 
1775
        if (asoc->c.sinit_max_instreams >
1776
            ntohs(peer_init->init_hdr.num_outbound_streams)) {
1777
                asoc->c.sinit_max_instreams =
1778
                        ntohs(peer_init->init_hdr.num_outbound_streams);
1779
        }
1780
 
1781
        /* Copy Initiation tag from INIT to VT_peer in cookie.   */
1782
        asoc->c.peer_vtag = asoc->peer.i.init_tag;
1783
 
1784
        /* Peer Rwnd   : Current calculated value of the peer's rwnd.  */
1785
        asoc->peer.rwnd = asoc->peer.i.a_rwnd;
1786
 
1787
        /* Copy cookie in case we need to resend COOKIE-ECHO. */
1788
        cookie = asoc->peer.cookie;
1789
        if (cookie) {
1790
                asoc->peer.cookie = kmalloc(asoc->peer.cookie_len, gfp);
1791
                if (!asoc->peer.cookie)
1792
                        goto clean_up;
1793
                memcpy(asoc->peer.cookie, cookie, asoc->peer.cookie_len);
1794
        }
1795
 
1796
        /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
1797
         * high (for example, implementations MAY use the size of the receiver
1798
         * advertised window).
1799
         */
1800
        list_for_each(pos, &asoc->peer.transport_addr_list) {
1801
                transport = list_entry(pos, struct sctp_transport, transports);
1802
                transport->ssthresh = asoc->peer.i.a_rwnd;
1803
        }
1804
 
1805
        /* Set up the TSN tracking pieces.  */
1806
        sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_SIZE,
1807
                         asoc->peer.i.initial_tsn);
1808
 
1809
        /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
1810
         *
1811
         * The stream sequence number in all the streams shall start
1812
         * from 0 when the association is established.  Also, when the
1813
         * stream sequence number reaches the value 65535 the next
1814
         * stream sequence number shall be set to 0.
1815
         */
1816
 
1817
        /* Allocate storage for the negotiated streams if it is not a temporary          * association.
1818
         */
1819
        if (!asoc->temp) {
1820
                asoc->ssnmap = sctp_ssnmap_new(asoc->c.sinit_max_instreams,
1821
                                               asoc->c.sinit_num_ostreams, gfp);
1822
                if (!asoc->ssnmap)
1823
                        goto nomem_ssnmap;
1824
        }
1825
 
1826
        /* ADDIP Section 4.1 ASCONF Chunk Procedures
1827
         *
1828
         * When an endpoint has an ASCONF signaled change to be sent to the
1829
         * remote endpoint it should do the following:
1830
         * ...
1831
         * A2) A serial number should be assigned to the Chunk. The serial
1832
         * number should be a monotonically increasing number. All serial
1833
         * numbers are defined to be initialized at the start of the
1834
         * association to the same value as the Initial TSN.
1835
         */
1836
        asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
1837
        return 1;
1838
 
1839
nomem_ssnmap:
1840
clean_up:
1841
        /* Release the transport structures. */
1842
        list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
1843
                transport = list_entry(pos, struct sctp_transport, transports);
1844
                list_del_init(pos);
1845
                sctp_transport_free(transport);
1846
        }
1847
nomem:
1848
        return 0;
1849
}
1850
 
1851
 
1852
/* Update asoc with the option described in param.
1853
 *
1854
 * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
1855
 *
1856
 * asoc is the association to update.
1857
 * param is the variable length parameter to use for update.
1858
 * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
1859
 * If the current packet is an INIT we want to minimize the amount of
1860
 * work we do.  In particular, we should not build transport
1861
 * structures for the addresses.
1862
 */
1863
int sctp_process_param(struct sctp_association *asoc, union sctp_params param,
1864
                       const union sctp_addr *peer_addr, int gfp)
1865
{
1866
        union sctp_addr addr;
1867
        int i;
1868
        __u16 sat;
1869
        int retval = 1;
1870
        sctp_scope_t scope;
1871
        time_t stale;
1872
        struct sctp_af *af;
1873
 
1874
        /* We maintain all INIT parameters in network byte order all the
1875
         * time.  This allows us to not worry about whether the parameters
1876
         * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
1877
         */
1878
        switch (param.p->type) {
1879
        case SCTP_PARAM_IPV6_ADDRESS:
1880
                if (PF_INET6 != asoc->base.sk->sk_family)
1881
                        break;
1882
                /* Fall through. */
1883
        case SCTP_PARAM_IPV4_ADDRESS:
1884
                af = sctp_get_af_specific(param_type2af(param.p->type));
1885
                af->from_addr_param(&addr, param.addr, asoc->peer.port, 0);
1886
                scope = sctp_scope(peer_addr);
1887
                if (sctp_in_scope(&addr, scope))
1888
                        if (!sctp_assoc_add_peer(asoc, &addr, gfp))
1889
                                return 0;
1890
                break;
1891
 
1892
        case SCTP_PARAM_COOKIE_PRESERVATIVE:
1893
                if (!sctp_cookie_preserve_enable)
1894
                        break;
1895
 
1896
                stale = ntohl(param.life->lifespan_increment);
1897
 
1898
                /* Suggested Cookie Life span increment's unit is msec,
1899
                 * (1/1000sec).
1900
                 */
1901
                asoc->cookie_life.tv_sec += stale / 1000;
1902
                asoc->cookie_life.tv_usec += (stale % 1000) * 1000;
1903
                break;
1904
 
1905
        case SCTP_PARAM_HOST_NAME_ADDRESS:
1906
                SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
1907
                break;
1908
 
1909
        case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
1910
                /* Turn off the default values first so we'll know which
1911
                 * ones are really set by the peer.
1912
                 */
1913
                asoc->peer.ipv4_address = 0;
1914
                asoc->peer.ipv6_address = 0;
1915
 
1916
                /* Cycle through address types; avoid divide by 0. */
1917
                sat = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
1918
                if (sat)
1919
                        sat /= sizeof(__u16);
1920
 
1921
                for (i = 0; i < sat; ++i) {
1922
                        switch (param.sat->types[i]) {
1923
                        case SCTP_PARAM_IPV4_ADDRESS:
1924
                                asoc->peer.ipv4_address = 1;
1925
                                break;
1926
 
1927
                        case SCTP_PARAM_IPV6_ADDRESS:
1928
                                asoc->peer.ipv6_address = 1;
1929
                                break;
1930
 
1931
                        case SCTP_PARAM_HOST_NAME_ADDRESS:
1932
                                asoc->peer.hostname_address = 1;
1933
                                break;
1934
 
1935
                        default: /* Just ignore anything else.  */
1936
                                break;
1937
                        };
1938
                }
1939
                break;
1940
 
1941
        case SCTP_PARAM_STATE_COOKIE:
1942
                asoc->peer.cookie_len =
1943
                        ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
1944
                asoc->peer.cookie = param.cookie->body;
1945
                break;
1946
 
1947
        case SCTP_PARAM_HEARTBEAT_INFO:
1948
                /* Would be odd to receive, but it causes no problems. */
1949
                break;
1950
 
1951
        case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
1952
                /* Rejected during verify stage. */
1953
                break;
1954
 
1955
        case SCTP_PARAM_ECN_CAPABLE:
1956
                asoc->peer.ecn_capable = 1;
1957
                break;
1958
 
1959
        default:
1960
                /* Any unrecognized parameters should have been caught
1961
                 * and handled by sctp_verify_param() which should be
1962
                 * called prior to this routine.  Simply log the error
1963
                 * here.
1964
                 */
1965
                SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
1966
                                  ntohs(param.p->type), asoc);
1967
                break;
1968
        };
1969
 
1970
        return retval;
1971
}
1972
 
1973
/* Select a new verification tag.  */
1974
__u32 sctp_generate_tag(const struct sctp_endpoint *ep)
1975
{
1976
        /* I believe that this random number generator complies with RFC1750.
1977
         * A tag of 0 is reserved for special cases (e.g. INIT).
1978
         */
1979
        __u32 x;
1980
 
1981
        do {
1982
                get_random_bytes(&x, sizeof(__u32));
1983
        } while (x == 0);
1984
 
1985
        return x;
1986
}
1987
 
1988
/* Select an initial TSN to send during startup.  */
1989
__u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
1990
{
1991
        __u32 retval;
1992
 
1993
        get_random_bytes(&retval, sizeof(__u32));
1994
        return retval;
1995
}
1996
 
1997
/*
1998
 * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
1999
 *      0                   1                   2                   3
2000
 *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
2001
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2002
 *     | Type = 0xC1   |  Chunk Flags  |      Chunk Length             |
2003
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2004
 *     |                       Serial Number                           |
2005
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2006
 *     |                    Address Parameter                          |
2007
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2008
 *     |                     ASCONF Parameter #1                       |
2009
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2010
 *     \                                                               \
2011
 *     /                             ....                              /
2012
 *     \                                                               \
2013
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2014
 *     |                     ASCONF Parameter #N                       |
2015
 *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2016
 *
2017
 * Address Parameter and other parameter will not be wrapped in this function
2018
 */
2019
struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
2020
                                    union sctp_addr *addr, int vparam_len)
2021
{
2022
        sctp_addiphdr_t asconf;
2023
        struct sctp_chunk *retval;
2024
        int length = sizeof(asconf) + vparam_len;
2025
        union sctp_addr_param addrparam;
2026
        int addrlen;
2027
        struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2028
 
2029
        addrlen = af->to_addr_param(addr, &addrparam);
2030
        if (!addrlen)
2031
                return NULL;
2032
        length += addrlen;
2033
 
2034
        /* Create the chunk.  */
2035
        retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF, 0, length);
2036
        if (!retval)
2037
                return NULL;
2038
 
2039
        asconf.serial = htonl(asoc->addip_serial++);
2040
 
2041
        retval->subh.addip_hdr =
2042
                sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2043
        retval->param_hdr.v =
2044
                sctp_addto_chunk(retval, addrlen, &addrparam);
2045
 
2046
        return retval;
2047
}
2048
 
2049
/* ADDIP
2050
 * 3.2.1 Add IP Address
2051
 *      0                   1                   2                   3
2052
 *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
2053
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2054
 *     |        Type = 0xC001          |    Length = Variable          |
2055
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2056
 *     |               ASCONF-Request Correlation ID                   |
2057
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2058
 *     |                       Address Parameter                       |
2059
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2060
 *
2061
 * 3.2.2 Delete IP Address
2062
 *      0                   1                   2                   3
2063
 *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
2064
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2065
 *     |        Type = 0xC002          |    Length = Variable          |
2066
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2067
 *     |               ASCONF-Request Correlation ID                   |
2068
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2069
 *     |                       Address Parameter                       |
2070
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2071
 *
2072
 */
2073
struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
2074
                                              union sctp_addr         *laddr,
2075
                                              struct sockaddr         *addrs,
2076
                                              int                     addrcnt,
2077
                                              __u16                   flags)
2078
{
2079
        sctp_addip_param_t      param;
2080
        struct sctp_chunk       *retval;
2081
        union sctp_addr_param   addr_param;
2082
        union sctp_addr         *addr;
2083
        void                    *addr_buf;
2084
        struct sctp_af          *af;
2085
        int                     paramlen = sizeof(param);
2086
        int                     addr_param_len = 0;
2087
        int                     totallen = 0;
2088
        int                     i;
2089
 
2090
        /* Get total length of all the address parameters. */
2091
        addr_buf = addrs;
2092
        for (i = 0; i < addrcnt; i++) {
2093
                addr = (union sctp_addr *)addr_buf;
2094
                af = sctp_get_af_specific(addr->v4.sin_family);
2095
                addr_param_len = af->to_addr_param(addr, &addr_param);
2096
 
2097
                totallen += paramlen;
2098
                totallen += addr_param_len;
2099
 
2100
                addr_buf += af->sockaddr_len;
2101
        }
2102
 
2103
        /* Create an asconf chunk with the required length. */
2104
        retval = sctp_make_asconf(asoc, laddr, totallen);
2105
        if (!retval)
2106
                return NULL;
2107
 
2108
        /* Add the address parameters to the asconf chunk. */
2109
        addr_buf = addrs;
2110
        for (i = 0; i < addrcnt; i++) {
2111
                addr = (union sctp_addr *)addr_buf;
2112
                af = sctp_get_af_specific(addr->v4.sin_family);
2113
                addr_param_len = af->to_addr_param(addr, &addr_param);
2114
                param.param_hdr.type = flags;
2115
                param.param_hdr.length = htons(paramlen + addr_param_len);
2116
                param.crr_id = i;
2117
 
2118
                sctp_addto_chunk(retval, paramlen, &param);
2119
                sctp_addto_chunk(retval, addr_param_len, &addr_param);
2120
 
2121
                addr_buf += af->sockaddr_len;
2122
        }
2123
        return retval;
2124
}
2125
 
2126
/* ADDIP
2127
 * 3.2.4 Set Primary IP Address
2128
 *      0                   1                   2                   3
2129
 *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
2130
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2131
 *     |        Type =0xC004           |    Length = Variable          |
2132
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2133
 *     |               ASCONF-Request Correlation ID                   |
2134
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2135
 *     |                       Address Parameter                       |
2136
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2137
 *
2138
 * Create an ASCONF chunk with Set Primary IP address parameter.
2139
 */
2140
struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
2141
                                             union sctp_addr *addr)
2142
{
2143
        sctp_addip_param_t      param;
2144
        struct sctp_chunk       *retval;
2145
        int                     len = sizeof(param);
2146
        union sctp_addr_param   addrparam;
2147
        int                     addrlen;
2148
        struct sctp_af          *af = sctp_get_af_specific(addr->v4.sin_family);
2149
 
2150
        addrlen = af->to_addr_param(addr, &addrparam);
2151
        if (!addrlen)
2152
                return NULL;
2153
        len += addrlen;
2154
 
2155
        /* Create the chunk and make asconf header. */
2156
        retval = sctp_make_asconf(asoc, addr, len);
2157
        if (!retval)
2158
                return NULL;
2159
 
2160
        param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
2161
        param.param_hdr.length = htons(len);
2162
        param.crr_id = 0;
2163
 
2164
        sctp_addto_chunk(retval, sizeof(param), &param);
2165
        sctp_addto_chunk(retval, addrlen, &addrparam);
2166
 
2167
        return retval;
2168
}
2169
 
2170
/* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2171
 *      0                   1                   2                   3
2172
 *      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
2173
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2174
 *     | Type = 0x80   |  Chunk Flags  |      Chunk Length             |
2175
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2176
 *     |                       Serial Number                           |
2177
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2178
 *     |                 ASCONF Parameter Response#1                   |
2179
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2180
 *     \                                                               \
2181
 *     /                             ....                              /
2182
 *     \                                                               \
2183
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2184
 *     |                 ASCONF Parameter Response#N                   |
2185
 *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2186
 *
2187
 * Create an ASCONF_ACK chunk with enough space for the parameter responses.
2188
 */
2189
struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
2190
                                        __u32 serial, int vparam_len)
2191
{
2192
        sctp_addiphdr_t         asconf;
2193
        struct sctp_chunk       *retval;
2194
        int                     length = sizeof(asconf) + vparam_len;
2195
 
2196
        /* Create the chunk.  */
2197
        retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF_ACK, 0, length);
2198
        if (!retval)
2199
                return NULL;
2200
 
2201
        asconf.serial = htonl(serial);
2202
 
2203
        retval->subh.addip_hdr =
2204
                sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2205
 
2206
        return retval;
2207
}
2208
 
2209
/* Add response parameters to an ASCONF_ACK chunk. */
2210
static void sctp_add_asconf_response(struct sctp_chunk *chunk, __u32 crr_id,
2211
                              __u16 err_code, sctp_addip_param_t *asconf_param)
2212
{
2213
        sctp_addip_param_t      ack_param;
2214
        sctp_errhdr_t           err_param;
2215
        int                     asconf_param_len = 0;
2216
        int                     err_param_len = 0;
2217
        __u16                   response_type;
2218
 
2219
        if (SCTP_ERROR_NO_ERROR == err_code) {
2220
                response_type = SCTP_PARAM_SUCCESS_REPORT;
2221
        } else {
2222
                response_type = SCTP_PARAM_ERR_CAUSE;
2223
                err_param_len = sizeof(err_param);
2224
                if (asconf_param)
2225
                        asconf_param_len =
2226
                                 ntohs(asconf_param->param_hdr.length);
2227
        }
2228
 
2229
        /* Add Success Indication or Error Cause Indication parameter. */
2230
        ack_param.param_hdr.type = response_type;
2231
        ack_param.param_hdr.length = htons(sizeof(ack_param) +
2232
                                           err_param_len +
2233
                                           asconf_param_len);
2234
        ack_param.crr_id = crr_id;
2235
        sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
2236
 
2237
        if (SCTP_ERROR_NO_ERROR == err_code)
2238
                return;
2239
 
2240
        /* Add Error Cause parameter. */
2241
        err_param.cause = err_code;
2242
        err_param.length = htons(err_param_len + asconf_param_len);
2243
        sctp_addto_chunk(chunk, err_param_len, &err_param);
2244
 
2245
        /* Add the failed TLV copied from ASCONF chunk. */
2246
        if (asconf_param)
2247
                sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
2248
}
2249
 
2250
/* Process a asconf parameter. */
2251
static __u16 sctp_process_asconf_param(struct sctp_association *asoc,
2252
                                       struct sctp_chunk *asconf,
2253
                                       sctp_addip_param_t *asconf_param)
2254
{
2255
        struct sctp_transport *peer;
2256
        struct sctp_af *af;
2257
        union sctp_addr addr;
2258
        struct list_head *pos;
2259
        union sctp_addr_param *addr_param;
2260
 
2261
        addr_param = (union sctp_addr_param *)
2262
                        ((void *)asconf_param + sizeof(sctp_addip_param_t));
2263
 
2264
        af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2265
        if (unlikely(!af))
2266
                return SCTP_ERROR_INV_PARAM;
2267
 
2268
        af->from_addr_param(&addr, addr_param, asoc->peer.port, 0);
2269
        switch (asconf_param->param_hdr.type) {
2270
        case SCTP_PARAM_ADD_IP:
2271
                /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
2272
                 * request and does not have the local resources to add this
2273
                 * new address to the association, it MUST return an Error
2274
                 * Cause TLV set to the new error code 'Operation Refused
2275
                 * Due to Resource Shortage'.
2276
                 */
2277
 
2278
                peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC);
2279
                if (!peer)
2280
                        return SCTP_ERROR_RSRC_LOW;
2281
 
2282
                /* Start the heartbeat timer. */
2283
                if (!mod_timer(&peer->hb_timer, sctp_transport_timeout(peer)))
2284
                        sctp_transport_hold(peer);
2285
                break;
2286
        case SCTP_PARAM_DEL_IP:
2287
                /* ADDIP 4.3 D7) If a request is received to delete the
2288
                 * last remaining IP address of a peer endpoint, the receiver
2289
                 * MUST send an Error Cause TLV with the error cause set to the
2290
                 * new error code 'Request to Delete Last Remaining IP Address'.
2291
                 */
2292
                pos = asoc->peer.transport_addr_list.next;
2293
                if (pos->next == &asoc->peer.transport_addr_list)
2294
                        return SCTP_ERROR_DEL_LAST_IP;
2295
 
2296
                /* ADDIP 4.3 D8) If a request is received to delete an IP
2297
                 * address which is also the source address of the IP packet
2298
                 * which contained the ASCONF chunk, the receiver MUST reject
2299
                 * this request. To reject the request the receiver MUST send
2300
                 * an Error Cause TLV set to the new error code 'Request to
2301
                 * Delete Source IP Address'
2302
                 */
2303
                if (sctp_cmp_addr_exact(sctp_source(asconf), &addr))
2304
                        return SCTP_ERROR_DEL_SRC_IP;
2305
 
2306
                sctp_assoc_del_peer(asoc, &addr);
2307
                break;
2308
        case SCTP_PARAM_SET_PRIMARY:
2309
                peer = sctp_assoc_lookup_paddr(asoc, &addr);
2310
                if (!peer)
2311
                        return SCTP_ERROR_INV_PARAM;
2312
 
2313
                sctp_assoc_set_primary(asoc, peer);
2314
                break;
2315
        default:
2316
                return SCTP_ERROR_INV_PARAM;
2317
                break;
2318
        }
2319
 
2320
        return SCTP_ERROR_NO_ERROR;
2321
}
2322
 
2323
/* Process an incoming ASCONF chunk with the next expected serial no. and
2324
 * return an ASCONF_ACK chunk to be sent in response.
2325
 */
2326
struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
2327
                                       struct sctp_chunk *asconf)
2328
{
2329
        sctp_addiphdr_t         *hdr;
2330
        union sctp_addr_param   *addr_param;
2331
        sctp_addip_param_t      *asconf_param;
2332
        struct sctp_chunk       *asconf_ack;
2333
 
2334
        __u16   err_code;
2335
        int     length = 0;
2336
        int     chunk_len = asconf->skb->len;
2337
        __u32   serial;
2338
        int     all_param_pass = 1;
2339
 
2340
        hdr = (sctp_addiphdr_t *)asconf->skb->data;
2341
        serial = ntohl(hdr->serial);
2342
 
2343
        /* Skip the addiphdr and store a pointer to address parameter.  */
2344
        length = sizeof(sctp_addiphdr_t);
2345
        addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
2346
        chunk_len -= length;
2347
 
2348
        /* Skip the address parameter and store a pointer to the first
2349
         * asconf paramter.
2350
         */
2351
        length = ntohs(addr_param->v4.param_hdr.length);
2352
        asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
2353
        chunk_len -= length;
2354
 
2355
        /* create an ASCONF_ACK chunk.
2356
         * Based on the definitions of parameters, we know that the size of
2357
         * ASCONF_ACK parameters are less than or equal to the twice of ASCONF
2358
         * paramters.
2359
         */
2360
        asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 2);
2361
        if (!asconf_ack)
2362
                goto done;
2363
 
2364
        /* Process the TLVs contained within the ASCONF chunk. */
2365
        while (chunk_len > 0) {
2366
                err_code = sctp_process_asconf_param(asoc, asconf,
2367
                                                     asconf_param);
2368
                /* ADDIP 4.1 A7)
2369
                 * If an error response is received for a TLV parameter,
2370
                 * all TLVs with no response before the failed TLV are
2371
                 * considered successful if not reported.  All TLVs after
2372
                 * the failed response are considered unsuccessful unless
2373
                 * a specific success indication is present for the parameter.
2374
                 */
2375
                if (SCTP_ERROR_NO_ERROR != err_code)
2376
                        all_param_pass = 0;
2377
 
2378
                if (!all_param_pass)
2379
                        sctp_add_asconf_response(asconf_ack,
2380
                                                 asconf_param->crr_id, err_code,
2381
                                                 asconf_param);
2382
 
2383
                /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
2384
                 * an IP address sends an 'Out of Resource' in its response, it
2385
                 * MUST also fail any subsequent add or delete requests bundled
2386
                 * in the ASCONF.
2387
                 */
2388
                if (SCTP_ERROR_RSRC_LOW == err_code)
2389
                        goto done;
2390
 
2391
                /* Move to the next ASCONF param. */
2392
                length = ntohs(asconf_param->param_hdr.length);
2393
                asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
2394
                                                      length);
2395
                chunk_len -= length;
2396
        }
2397
 
2398
done:
2399
        asoc->peer.addip_serial++;
2400
 
2401
        /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
2402
         * after freeing the reference to old asconf ack if any.
2403
         */
2404
        if (asconf_ack) {
2405
                if (asoc->addip_last_asconf_ack)
2406
                        sctp_chunk_free(asoc->addip_last_asconf_ack);
2407
 
2408
                sctp_chunk_hold(asconf_ack);
2409
                asoc->addip_last_asconf_ack = asconf_ack;
2410
        }
2411
 
2412
        return asconf_ack;
2413
}
2414
 
2415
/* Process a asconf parameter that is successfully acked. */
2416
static int sctp_asconf_param_success(struct sctp_association *asoc,
2417
                                     sctp_addip_param_t *asconf_param)
2418
{
2419
        struct sctp_af *af;
2420
        union sctp_addr addr;
2421
        struct sctp_bind_addr *bp = &asoc->base.bind_addr;
2422
        union sctp_addr_param *addr_param;
2423
        int retval = 0;
2424
 
2425
        addr_param = (union sctp_addr_param *)
2426
                        ((void *)asconf_param + sizeof(sctp_addip_param_t));
2427
 
2428
        /* We have checked the packet before, so we do not check again. */
2429
        af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2430
        af->from_addr_param(&addr, addr_param, bp->port, 0);
2431
 
2432
        switch (asconf_param->param_hdr.type) {
2433
        case SCTP_PARAM_ADD_IP:
2434
                sctp_local_bh_disable();
2435
                sctp_write_lock(&asoc->base.addr_lock);
2436
                retval = sctp_add_bind_addr(bp, &addr, GFP_ATOMIC);
2437
                sctp_write_unlock(&asoc->base.addr_lock);
2438
                sctp_local_bh_enable();
2439
                break;
2440
        case SCTP_PARAM_DEL_IP:
2441
                sctp_local_bh_disable();
2442
                sctp_write_lock(&asoc->base.addr_lock);
2443
                retval = sctp_del_bind_addr(bp, &addr);
2444
                sctp_write_unlock(&asoc->base.addr_lock);
2445
                sctp_local_bh_enable();
2446
                break;
2447
        default:
2448
                break;
2449
        }
2450
 
2451
        return retval;
2452
}
2453
 
2454
/* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
2455
 * for the given asconf parameter.  If there is no response for this parameter,
2456
 * return the error code based on the third argument 'no_err'.
2457
 * ADDIP 4.1
2458
 * A7) If an error response is received for a TLV parameter, all TLVs with no
2459
 * response before the failed TLV are considered successful if not reported.
2460
 * All TLVs after the failed response are considered unsuccessful unless a
2461
 * specific success indication is present for the parameter.
2462
 */
2463
static __u16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
2464
                                      sctp_addip_param_t *asconf_param,
2465
                                      int no_err)
2466
{
2467
        sctp_addip_param_t      *asconf_ack_param;
2468
        sctp_errhdr_t           *err_param;
2469
        int                     length;
2470
        int                     asconf_ack_len = asconf_ack->skb->len;
2471
        __u16                   err_code;
2472
 
2473
        if (no_err)
2474
                err_code = SCTP_ERROR_NO_ERROR;
2475
        else
2476
                err_code = SCTP_ERROR_REQ_REFUSED;
2477
 
2478
        /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
2479
         * the first asconf_ack parameter.
2480
         */
2481
        length = sizeof(sctp_addiphdr_t);
2482
        asconf_ack_param = (sctp_addip_param_t *)(asconf_ack->skb->data +
2483
                                                  length);
2484
        asconf_ack_len -= length;
2485
 
2486
        while (asconf_ack_len > 0) {
2487
                if (asconf_ack_param->crr_id == asconf_param->crr_id) {
2488
                        switch(asconf_ack_param->param_hdr.type) {
2489
                        case SCTP_PARAM_SUCCESS_REPORT:
2490
                                return SCTP_ERROR_NO_ERROR;
2491
                        case SCTP_PARAM_ERR_CAUSE:
2492
                                length = sizeof(sctp_addip_param_t);
2493
                                err_param = (sctp_errhdr_t *)
2494
                                           ((void *)asconf_ack_param + length);
2495
                                asconf_ack_len -= length;
2496
                                if (asconf_ack_len > 0)
2497
                                        return err_param->cause;
2498
                                else
2499
                                        return SCTP_ERROR_INV_PARAM;
2500
                                break;
2501
                        default:
2502
                                return SCTP_ERROR_INV_PARAM;
2503
                        }
2504
                }
2505
 
2506
                length = ntohs(asconf_ack_param->param_hdr.length);
2507
                asconf_ack_param = (sctp_addip_param_t *)
2508
                                        ((void *)asconf_ack_param + length);
2509
                asconf_ack_len -= length;
2510
        }
2511
 
2512
        return err_code;
2513
}
2514
 
2515
/* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
2516
int sctp_process_asconf_ack(struct sctp_association *asoc,
2517
                            struct sctp_chunk *asconf_ack)
2518
{
2519
        struct sctp_chunk       *asconf = asoc->addip_last_asconf;
2520
        union sctp_addr_param   *addr_param;
2521
        sctp_addip_param_t      *asconf_param;
2522
        int     length = 0;
2523
        int     asconf_len = asconf->skb->len;
2524
        int     all_param_pass = 0;
2525
        int     no_err = 1;
2526
        int     retval = 0;
2527
        __u16   err_code = SCTP_ERROR_NO_ERROR;
2528
 
2529
        /* Skip the chunkhdr and addiphdr from the last asconf sent and store
2530
         * a pointer to address parameter.
2531
         */
2532
        length = sizeof(sctp_addip_chunk_t);
2533
        addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
2534
        asconf_len -= length;
2535
 
2536
        /* Skip the address parameter in the last asconf sent and store a
2537
         * pointer to the first asconf paramter.
2538
         */
2539
        length = ntohs(addr_param->v4.param_hdr.length);
2540
        asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
2541
        asconf_len -= length;
2542
 
2543
        /* ADDIP 4.1
2544
         * A8) If there is no response(s) to specific TLV parameter(s), and no
2545
         * failures are indicated, then all request(s) are considered
2546
         * successful.
2547
         */
2548
        if (asconf_ack->skb->len == sizeof(sctp_addiphdr_t))
2549
                all_param_pass = 1;
2550
 
2551
        /* Process the TLVs contained in the last sent ASCONF chunk. */
2552
        while (asconf_len > 0) {
2553
                if (all_param_pass)
2554
                        err_code = SCTP_ERROR_NO_ERROR;
2555
                else {
2556
                        err_code = sctp_get_asconf_response(asconf_ack,
2557
                                                            asconf_param,
2558
                                                            no_err);
2559
                        if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
2560
                                no_err = 0;
2561
                }
2562
 
2563
                switch (err_code) {
2564
                case SCTP_ERROR_NO_ERROR:
2565
                        retval = sctp_asconf_param_success(asoc, asconf_param);
2566
                        break;
2567
 
2568
                case SCTP_ERROR_RSRC_LOW:
2569
                        retval = 1;
2570
                        break;
2571
 
2572
                case SCTP_ERROR_INV_PARAM:
2573
                        /* Disable sending this type of asconf parameter in
2574
                         * future.
2575
                         */
2576
                        asoc->peer.addip_disabled_mask |=
2577
                                asconf_param->param_hdr.type;
2578
                        break;
2579
 
2580
                case SCTP_ERROR_REQ_REFUSED:
2581
                case SCTP_ERROR_DEL_LAST_IP:
2582
                case SCTP_ERROR_DEL_SRC_IP:
2583
                default:
2584
                         break;
2585
                }
2586
 
2587
                /* Skip the processed asconf parameter and move to the next
2588
                 * one.
2589
                 */
2590
                length = ntohs(asconf_param->param_hdr.length);
2591
                asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
2592
                                                      length);
2593
                asconf_len -= length;
2594
        }
2595
 
2596
        /* Free the cached last sent asconf chunk. */
2597
        sctp_chunk_free(asconf);
2598
        asoc->addip_last_asconf = NULL;
2599
 
2600
        /* Send the next asconf chunk from the addip chunk queue. */
2601
        asconf = (struct sctp_chunk *)__skb_dequeue(&asoc->addip_chunks);
2602
        if (asconf) {
2603
                /* Hold the chunk until an ASCONF_ACK is received. */
2604
                sctp_chunk_hold(asconf);
2605
                if (sctp_primitive_ASCONF(asoc, asconf))
2606
                        sctp_chunk_free(asconf);
2607
                else
2608
                        asoc->addip_last_asconf = asconf;
2609
        }
2610
 
2611
        return retval;
2612
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.