]> bbs.cooldavid.org Git - net-next-2.6.git/blob - net/sctp/sm_make_chunk.c
sctp: reduce memory footprint of sctp_chunk structure
[net-next-2.6.git] / net / sctp / sm_make_chunk.c
1 /* SCTP kernel implementation
2  * (C) Copyright IBM Corp. 2001, 2004
3  * Copyright (c) 1999-2000 Cisco, Inc.
4  * Copyright (c) 1999-2001 Motorola, Inc.
5  * Copyright (c) 2001-2002 Intel Corp.
6  *
7  * This file is part of the SCTP kernel implementation
8  *
9  * These functions work with the state functions in sctp_sm_statefuns.c
10  * to implement the state operations.  These functions implement the
11  * steps which require modifying existing data structures.
12  *
13  * This SCTP implementation is free software;
14  * you can redistribute it and/or modify it under the terms of
15  * the GNU General Public License as published by
16  * the Free Software Foundation; either version 2, or (at your option)
17  * any later version.
18  *
19  * This SCTP implementation is distributed in the hope that it
20  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
21  *                 ************************
22  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23  * See the GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with GNU CC; see the file COPYING.  If not, write to
27  * the Free Software Foundation, 59 Temple Place - Suite 330,
28  * Boston, MA 02111-1307, USA.
29  *
30  * Please send any bug reports or fixes you make to the
31  * email address(es):
32  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
33  *
34  * Or submit a bug report through the following website:
35  *    http://www.sf.net/projects/lksctp
36  *
37  * Written or modified by:
38  *    La Monte H.P. Yarroll <piggy@acm.org>
39  *    Karl Knutson          <karl@athena.chicago.il.us>
40  *    C. Robin              <chris@hundredacre.ac.uk>
41  *    Jon Grimm             <jgrimm@us.ibm.com>
42  *    Xingang Guo           <xingang.guo@intel.com>
43  *    Dajiang Zhang         <dajiang.zhang@nokia.com>
44  *    Sridhar Samudrala     <sri@us.ibm.com>
45  *    Daisy Chang           <daisyc@us.ibm.com>
46  *    Ardelle Fan           <ardelle.fan@intel.com>
47  *    Kevin Gao             <kevin.gao@intel.com>
48  *
49  * Any bugs reported given to us we will try to fix... any fixes shared will
50  * be incorporated into the next SCTP release.
51  */
52
53 #include <linux/types.h>
54 #include <linux/kernel.h>
55 #include <linux/ip.h>
56 #include <linux/ipv6.h>
57 #include <linux/net.h>
58 #include <linux/inet.h>
59 #include <linux/scatterlist.h>
60 #include <linux/crypto.h>
61 #include <net/sock.h>
62
63 #include <linux/skbuff.h>
64 #include <linux/random.h>       /* for get_random_bytes */
65 #include <net/sctp/sctp.h>
66 #include <net/sctp/sm.h>
67
68 SCTP_STATIC
69 struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
70                                    __u8 type, __u8 flags, int paylen);
71 static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
72                                         const struct sctp_association *asoc,
73                                         const struct sctp_chunk *init_chunk,
74                                         int *cookie_len,
75                                         const __u8 *raw_addrs, int addrs_len);
76 static int sctp_process_param(struct sctp_association *asoc,
77                               union sctp_params param,
78                               const union sctp_addr *peer_addr,
79                               gfp_t gfp);
80 static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
81                               const void *data);
82
83 /* What was the inbound interface for this chunk? */
84 int sctp_chunk_iif(const struct sctp_chunk *chunk)
85 {
86         struct sctp_af *af;
87         int iif = 0;
88
89         af = sctp_get_af_specific(ipver2af(ip_hdr(chunk->skb)->version));
90         if (af)
91                 iif = af->skb_iif(chunk->skb);
92
93         return iif;
94 }
95
96 /* RFC 2960 3.3.2 Initiation (INIT) (1)
97  *
98  * Note 2: The ECN capable field is reserved for future use of
99  * Explicit Congestion Notification.
100  */
101 static const struct sctp_paramhdr ecap_param = {
102         SCTP_PARAM_ECN_CAPABLE,
103         __constant_htons(sizeof(struct sctp_paramhdr)),
104 };
105 static const struct sctp_paramhdr prsctp_param = {
106         SCTP_PARAM_FWD_TSN_SUPPORT,
107         __constant_htons(sizeof(struct sctp_paramhdr)),
108 };
109
110 /* A helper to initialize to initialize an op error inside a
111  * provided chunk, as most cause codes will be embedded inside an
112  * abort chunk.
113  */
114 void  sctp_init_cause(struct sctp_chunk *chunk, __be16 cause_code,
115                       size_t paylen)
116 {
117         sctp_errhdr_t err;
118         __u16 len;
119
120         /* Cause code constants are now defined in network order.  */
121         err.cause = cause_code;
122         len = sizeof(sctp_errhdr_t) + paylen;
123         err.length  = htons(len);
124         chunk->subh.err_hdr = sctp_addto_chunk(chunk, sizeof(sctp_errhdr_t), &err);
125 }
126
127 /* 3.3.2 Initiation (INIT) (1)
128  *
129  * This chunk is used to initiate a SCTP association between two
130  * endpoints. The format of the INIT chunk is shown below:
131  *
132  *     0                   1                   2                   3
133  *     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
134  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
135  *    |   Type = 1    |  Chunk Flags  |      Chunk Length             |
136  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
137  *    |                         Initiate Tag                          |
138  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
139  *    |           Advertised Receiver Window Credit (a_rwnd)          |
140  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
141  *    |  Number of Outbound Streams   |  Number of Inbound Streams    |
142  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
143  *    |                          Initial TSN                          |
144  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
145  *    \                                                               \
146  *    /              Optional/Variable-Length Parameters              /
147  *    \                                                               \
148  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
149  *
150  *
151  * The INIT chunk contains the following parameters. Unless otherwise
152  * noted, each parameter MUST only be included once in the INIT chunk.
153  *
154  * Fixed Parameters                     Status
155  * ----------------------------------------------
156  * Initiate Tag                        Mandatory
157  * Advertised Receiver Window Credit   Mandatory
158  * Number of Outbound Streams          Mandatory
159  * Number of Inbound Streams           Mandatory
160  * Initial TSN                         Mandatory
161  *
162  * Variable Parameters                  Status     Type Value
163  * -------------------------------------------------------------
164  * IPv4 Address (Note 1)               Optional    5
165  * IPv6 Address (Note 1)               Optional    6
166  * Cookie Preservative                 Optional    9
167  * Reserved for ECN Capable (Note 2)   Optional    32768 (0x8000)
168  * Host Name Address (Note 3)          Optional    11
169  * Supported Address Types (Note 4)    Optional    12
170  */
171 struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
172                              const struct sctp_bind_addr *bp,
173                              gfp_t gfp, int vparam_len)
174 {
175         sctp_inithdr_t init;
176         union sctp_params addrs;
177         size_t chunksize;
178         struct sctp_chunk *retval = NULL;
179         int num_types, addrs_len = 0;
180         struct sctp_sock *sp;
181         sctp_supported_addrs_param_t sat;
182         __be16 types[2];
183         sctp_adaptation_ind_param_t aiparam;
184         sctp_supported_ext_param_t ext_param;
185         int num_ext = 0;
186         __u8 extensions[3];
187         sctp_paramhdr_t *auth_chunks = NULL,
188                         *auth_hmacs = NULL;
189
190         /* RFC 2960 3.3.2 Initiation (INIT) (1)
191          *
192          * Note 1: The INIT chunks can contain multiple addresses that
193          * can be IPv4 and/or IPv6 in any combination.
194          */
195         retval = NULL;
196
197         /* Convert the provided bind address list to raw format. */
198         addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, gfp);
199
200         init.init_tag              = htonl(asoc->c.my_vtag);
201         init.a_rwnd                = htonl(asoc->rwnd);
202         init.num_outbound_streams  = htons(asoc->c.sinit_num_ostreams);
203         init.num_inbound_streams   = htons(asoc->c.sinit_max_instreams);
204         init.initial_tsn           = htonl(asoc->c.initial_tsn);
205
206         /* How many address types are needed? */
207         sp = sctp_sk(asoc->base.sk);
208         num_types = sp->pf->supported_addrs(sp, types);
209
210         chunksize = sizeof(init) + addrs_len + SCTP_SAT_LEN(num_types);
211         chunksize += sizeof(ecap_param);
212
213         if (sctp_prsctp_enable)
214                 chunksize += sizeof(prsctp_param);
215
216         /* ADDIP: Section 4.2.7:
217          *  An implementation supporting this extension [ADDIP] MUST list
218          *  the ASCONF,the ASCONF-ACK, and the AUTH  chunks in its INIT and
219          *  INIT-ACK parameters.
220          */
221         if (sctp_addip_enable) {
222                 extensions[num_ext] = SCTP_CID_ASCONF;
223                 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
224                 num_ext += 2;
225         }
226
227         chunksize += sizeof(aiparam);
228         chunksize += vparam_len;
229
230         /* Account for AUTH related parameters */
231         if (sctp_auth_enable) {
232                 /* Add random parameter length*/
233                 chunksize += sizeof(asoc->c.auth_random);
234
235                 /* Add HMACS parameter length if any were defined */
236                 auth_hmacs = (sctp_paramhdr_t *)asoc->c.auth_hmacs;
237                 if (auth_hmacs->length)
238                         chunksize += ntohs(auth_hmacs->length);
239                 else
240                         auth_hmacs = NULL;
241
242                 /* Add CHUNKS parameter length */
243                 auth_chunks = (sctp_paramhdr_t *)asoc->c.auth_chunks;
244                 if (auth_chunks->length)
245                         chunksize += ntohs(auth_chunks->length);
246                 else
247                         auth_chunks = NULL;
248
249                 extensions[num_ext] = SCTP_CID_AUTH;
250                 num_ext += 1;
251         }
252
253         /* If we have any extensions to report, account for that */
254         if (num_ext)
255                 chunksize += sizeof(sctp_supported_ext_param_t) + num_ext;
256
257         /* RFC 2960 3.3.2 Initiation (INIT) (1)
258          *
259          * Note 3: An INIT chunk MUST NOT contain more than one Host
260          * Name address parameter. Moreover, the sender of the INIT
261          * MUST NOT combine any other address types with the Host Name
262          * address in the INIT. The receiver of INIT MUST ignore any
263          * other address types if the Host Name address parameter is
264          * present in the received INIT chunk.
265          *
266          * PLEASE DO NOT FIXME [This version does not support Host Name.]
267          */
268
269         retval = sctp_make_chunk(asoc, SCTP_CID_INIT, 0, chunksize);
270         if (!retval)
271                 goto nodata;
272
273         retval->subh.init_hdr =
274                 sctp_addto_chunk(retval, sizeof(init), &init);
275         retval->param_hdr.v =
276                 sctp_addto_chunk(retval, addrs_len, addrs.v);
277
278         /* RFC 2960 3.3.2 Initiation (INIT) (1)
279          *
280          * Note 4: This parameter, when present, specifies all the
281          * address types the sending endpoint can support. The absence
282          * of this parameter indicates that the sending endpoint can
283          * support any address type.
284          */
285         sat.param_hdr.type = SCTP_PARAM_SUPPORTED_ADDRESS_TYPES;
286         sat.param_hdr.length = htons(SCTP_SAT_LEN(num_types));
287         sctp_addto_chunk(retval, sizeof(sat), &sat);
288         sctp_addto_chunk(retval, num_types * sizeof(__u16), &types);
289
290         sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
291
292         /* Add the supported extensions parameter.  Be nice and add this
293          * fist before addiding the parameters for the extensions themselves
294          */
295         if (num_ext) {
296                 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
297                 ext_param.param_hdr.length =
298                             htons(sizeof(sctp_supported_ext_param_t) + num_ext);
299                 sctp_addto_chunk(retval, sizeof(sctp_supported_ext_param_t),
300                                 &ext_param);
301                 sctp_addto_param(retval, num_ext, extensions);
302         }
303
304         if (sctp_prsctp_enable)
305                 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
306
307         aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
308         aiparam.param_hdr.length = htons(sizeof(aiparam));
309         aiparam.adaptation_ind = htonl(sp->adaptation_ind);
310         sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
311
312         /* Add SCTP-AUTH chunks to the parameter list */
313         if (sctp_auth_enable) {
314                 sctp_addto_chunk(retval, sizeof(asoc->c.auth_random),
315                                  asoc->c.auth_random);
316                 if (auth_hmacs)
317                         sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
318                                         auth_hmacs);
319                 if (auth_chunks)
320                         sctp_addto_chunk(retval, ntohs(auth_chunks->length),
321                                         auth_chunks);
322         }
323 nodata:
324         kfree(addrs.v);
325         return retval;
326 }
327
328 struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
329                                  const struct sctp_chunk *chunk,
330                                  gfp_t gfp, int unkparam_len)
331 {
332         sctp_inithdr_t initack;
333         struct sctp_chunk *retval;
334         union sctp_params addrs;
335         int addrs_len;
336         sctp_cookie_param_t *cookie;
337         int cookie_len;
338         size_t chunksize;
339         sctp_adaptation_ind_param_t aiparam;
340         sctp_supported_ext_param_t ext_param;
341         int num_ext = 0;
342         __u8 extensions[3];
343         sctp_paramhdr_t *auth_chunks = NULL,
344                         *auth_hmacs = NULL,
345                         *auth_random = NULL;
346
347         retval = NULL;
348
349         /* Note: there may be no addresses to embed. */
350         addrs = sctp_bind_addrs_to_raw(&asoc->base.bind_addr, &addrs_len, gfp);
351
352         initack.init_tag                = htonl(asoc->c.my_vtag);
353         initack.a_rwnd                  = htonl(asoc->rwnd);
354         initack.num_outbound_streams    = htons(asoc->c.sinit_num_ostreams);
355         initack.num_inbound_streams     = htons(asoc->c.sinit_max_instreams);
356         initack.initial_tsn             = htonl(asoc->c.initial_tsn);
357
358         /* FIXME:  We really ought to build the cookie right
359          * into the packet instead of allocating more fresh memory.
360          */
361         cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
362                                   addrs.v, addrs_len);
363         if (!cookie)
364                 goto nomem_cookie;
365
366         /* Calculate the total size of allocation, include the reserved
367          * space for reporting unknown parameters if it is specified.
368          */
369         chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;
370
371         /* Tell peer that we'll do ECN only if peer advertised such cap.  */
372         if (asoc->peer.ecn_capable)
373                 chunksize += sizeof(ecap_param);
374
375         if (sctp_prsctp_enable)
376                 chunksize += sizeof(prsctp_param);
377
378         if (sctp_addip_enable) {
379                 extensions[num_ext] = SCTP_CID_ASCONF;
380                 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
381                 num_ext += 2;
382         }
383
384         chunksize += sizeof(aiparam);
385
386         if (asoc->peer.auth_capable) {
387                 auth_random = (sctp_paramhdr_t *)asoc->c.auth_random;
388                 chunksize += ntohs(auth_random->length);
389
390                 auth_hmacs = (sctp_paramhdr_t *)asoc->c.auth_hmacs;
391                 if (auth_hmacs->length)
392                         chunksize += ntohs(auth_hmacs->length);
393                 else
394                         auth_hmacs = NULL;
395
396                 auth_chunks = (sctp_paramhdr_t *)asoc->c.auth_chunks;
397                 if (auth_chunks->length)
398                         chunksize += ntohs(auth_chunks->length);
399                 else
400                         auth_chunks = NULL;
401
402                 extensions[num_ext] = SCTP_CID_AUTH;
403                 num_ext += 1;
404         }
405
406         if (num_ext)
407                 chunksize += sizeof(sctp_supported_ext_param_t) + num_ext;
408
409         /* Now allocate and fill out the chunk.  */
410         retval = sctp_make_chunk(asoc, SCTP_CID_INIT_ACK, 0, chunksize);
411         if (!retval)
412                 goto nomem_chunk;
413
414         /* Per the advice in RFC 2960 6.4, send this reply to
415          * the source of the INIT packet.
416          */
417         retval->transport = chunk->transport;
418         retval->subh.init_hdr =
419                 sctp_addto_chunk(retval, sizeof(initack), &initack);
420         retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
421         sctp_addto_chunk(retval, cookie_len, cookie);
422         if (asoc->peer.ecn_capable)
423                 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
424         if (num_ext) {
425                 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
426                 ext_param.param_hdr.length =
427                             htons(sizeof(sctp_supported_ext_param_t) + num_ext);
428                 sctp_addto_chunk(retval, sizeof(sctp_supported_ext_param_t),
429                                  &ext_param);
430                 sctp_addto_param(retval, num_ext, extensions);
431         }
432         if (asoc->peer.prsctp_capable)
433                 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
434
435         aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
436         aiparam.param_hdr.length = htons(sizeof(aiparam));
437         aiparam.adaptation_ind = htonl(sctp_sk(asoc->base.sk)->adaptation_ind);
438         sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
439
440         if (asoc->peer.auth_capable) {
441                 sctp_addto_chunk(retval, ntohs(auth_random->length),
442                                  auth_random);
443                 if (auth_hmacs)
444                         sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
445                                         auth_hmacs);
446                 if (auth_chunks)
447                         sctp_addto_chunk(retval, ntohs(auth_chunks->length),
448                                         auth_chunks);
449         }
450
451         /* We need to remove the const qualifier at this point.  */
452         retval->asoc = (struct sctp_association *) asoc;
453
454         /* RFC 2960 6.4 Multi-homed SCTP Endpoints
455          *
456          * An endpoint SHOULD transmit reply chunks (e.g., SACK,
457          * HEARTBEAT ACK, * etc.) to the same destination transport
458          * address from which it received the DATA or control chunk
459          * to which it is replying.
460          *
461          * [INIT ACK back to where the INIT came from.]
462          */
463         if (chunk)
464                 retval->transport = chunk->transport;
465
466 nomem_chunk:
467         kfree(cookie);
468 nomem_cookie:
469         kfree(addrs.v);
470         return retval;
471 }
472
473 /* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
474  *
475  * This chunk is used only during the initialization of an association.
476  * It is sent by the initiator of an association to its peer to complete
477  * the initialization process. This chunk MUST precede any DATA chunk
478  * sent within the association, but MAY be bundled with one or more DATA
479  * chunks in the same packet.
480  *
481  *      0                   1                   2                   3
482  *      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
483  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
484  *     |   Type = 10   |Chunk  Flags   |         Length                |
485  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
486  *     /                     Cookie                                    /
487  *     \                                                               \
488  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
489  *
490  * Chunk Flags: 8 bit
491  *
492  *   Set to zero on transmit and ignored on receipt.
493  *
494  * Length: 16 bits (unsigned integer)
495  *
496  *   Set to the size of the chunk in bytes, including the 4 bytes of
497  *   the chunk header and the size of the Cookie.
498  *
499  * Cookie: variable size
500  *
501  *   This field must contain the exact cookie received in the
502  *   State Cookie parameter from the previous INIT ACK.
503  *
504  *   An implementation SHOULD make the cookie as small as possible
505  *   to insure interoperability.
506  */
507 struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
508                                     const struct sctp_chunk *chunk)
509 {
510         struct sctp_chunk *retval;
511         void *cookie;
512         int cookie_len;
513
514         cookie = asoc->peer.cookie;
515         cookie_len = asoc->peer.cookie_len;
516
517         /* Build a cookie echo chunk.  */
518         retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ECHO, 0, cookie_len);
519         if (!retval)
520                 goto nodata;
521         retval->subh.cookie_hdr =
522                 sctp_addto_chunk(retval, cookie_len, cookie);
523
524         /* RFC 2960 6.4 Multi-homed SCTP Endpoints
525          *
526          * An endpoint SHOULD transmit reply chunks (e.g., SACK,
527          * HEARTBEAT ACK, * etc.) to the same destination transport
528          * address from which it * received the DATA or control chunk
529          * to which it is replying.
530          *
531          * [COOKIE ECHO back to where the INIT ACK came from.]
532          */
533         if (chunk)
534                 retval->transport = chunk->transport;
535
536 nodata:
537         return retval;
538 }
539
540 /* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
541  *
542  * This chunk is used only during the initialization of an
543  * association.  It is used to acknowledge the receipt of a COOKIE
544  * ECHO chunk.  This chunk MUST precede any DATA or SACK chunk sent
545  * within the association, but MAY be bundled with one or more DATA
546  * chunks or SACK chunk in the same SCTP packet.
547  *
548  *      0                   1                   2                   3
549  *      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
550  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
551  *     |   Type = 11   |Chunk  Flags   |     Length = 4                |
552  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
553  *
554  * Chunk Flags: 8 bits
555  *
556  *   Set to zero on transmit and ignored on receipt.
557  */
558 struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
559                                    const struct sctp_chunk *chunk)
560 {
561         struct sctp_chunk *retval;
562
563         retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ACK, 0, 0);
564
565         /* RFC 2960 6.4 Multi-homed SCTP Endpoints
566          *
567          * An endpoint SHOULD transmit reply chunks (e.g., SACK,
568          * HEARTBEAT ACK, * etc.) to the same destination transport
569          * address from which it * received the DATA or control chunk
570          * to which it is replying.
571          *
572          * [COOKIE ACK back to where the COOKIE ECHO came from.]
573          */
574         if (retval && chunk)
575                 retval->transport = chunk->transport;
576
577         return retval;
578 }
579
580 /*
581  *  Appendix A: Explicit Congestion Notification:
582  *  CWR:
583  *
584  *  RFC 2481 details a specific bit for a sender to send in the header of
585  *  its next outbound TCP segment to indicate to its peer that it has
586  *  reduced its congestion window.  This is termed the CWR bit.  For
587  *  SCTP the same indication is made by including the CWR chunk.
588  *  This chunk contains one data element, i.e. the TSN number that
589  *  was sent in the ECNE chunk.  This element represents the lowest
590  *  TSN number in the datagram that was originally marked with the
591  *  CE bit.
592  *
593  *     0                   1                   2                   3
594  *     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
595  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
596  *    | Chunk Type=13 | Flags=00000000|    Chunk Length = 8           |
597  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
598  *    |                      Lowest TSN Number                        |
599  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
600  *
601  *     Note: The CWR is considered a Control chunk.
602  */
603 struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
604                             const __u32 lowest_tsn,
605                             const struct sctp_chunk *chunk)
606 {
607         struct sctp_chunk *retval;
608         sctp_cwrhdr_t cwr;
609
610         cwr.lowest_tsn = htonl(lowest_tsn);
611         retval = sctp_make_chunk(asoc, SCTP_CID_ECN_CWR, 0,
612                                  sizeof(sctp_cwrhdr_t));
613
614         if (!retval)
615                 goto nodata;
616
617         retval->subh.ecn_cwr_hdr =
618                 sctp_addto_chunk(retval, sizeof(cwr), &cwr);
619
620         /* RFC 2960 6.4 Multi-homed SCTP Endpoints
621          *
622          * An endpoint SHOULD transmit reply chunks (e.g., SACK,
623          * HEARTBEAT ACK, * etc.) to the same destination transport
624          * address from which it * received the DATA or control chunk
625          * to which it is replying.
626          *
627          * [Report a reduced congestion window back to where the ECNE
628          * came from.]
629          */
630         if (chunk)
631                 retval->transport = chunk->transport;
632
633 nodata:
634         return retval;
635 }
636
637 /* Make an ECNE chunk.  This is a congestion experienced report.  */
638 struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
639                              const __u32 lowest_tsn)
640 {
641         struct sctp_chunk *retval;
642         sctp_ecnehdr_t ecne;
643
644         ecne.lowest_tsn = htonl(lowest_tsn);
645         retval = sctp_make_chunk(asoc, SCTP_CID_ECN_ECNE, 0,
646                                  sizeof(sctp_ecnehdr_t));
647         if (!retval)
648                 goto nodata;
649         retval->subh.ecne_hdr =
650                 sctp_addto_chunk(retval, sizeof(ecne), &ecne);
651
652 nodata:
653         return retval;
654 }
655
656 /* Make a DATA chunk for the given association from the provided
657  * parameters.  However, do not populate the data payload.
658  */
659 struct sctp_chunk *sctp_make_datafrag_empty(struct sctp_association *asoc,
660                                        const struct sctp_sndrcvinfo *sinfo,
661                                        int data_len, __u8 flags, __u16 ssn)
662 {
663         struct sctp_chunk *retval;
664         struct sctp_datahdr dp;
665         int chunk_len;
666
667         /* We assign the TSN as LATE as possible, not here when
668          * creating the chunk.
669          */
670         dp.tsn = 0;
671         dp.stream = htons(sinfo->sinfo_stream);
672         dp.ppid   = sinfo->sinfo_ppid;
673
674         /* Set the flags for an unordered send.  */
675         if (sinfo->sinfo_flags & SCTP_UNORDERED) {
676                 flags |= SCTP_DATA_UNORDERED;
677                 dp.ssn = 0;
678         } else
679                 dp.ssn = htons(ssn);
680
681         chunk_len = sizeof(dp) + data_len;
682         retval = sctp_make_chunk(asoc, SCTP_CID_DATA, flags, chunk_len);
683         if (!retval)
684                 goto nodata;
685
686         retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
687         memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
688
689 nodata:
690         return retval;
691 }
692
693 /* Create a selective ackowledgement (SACK) for the given
694  * association.  This reports on which TSN's we've seen to date,
695  * including duplicates and gaps.
696  */
697 struct sctp_chunk *sctp_make_sack(const struct sctp_association *asoc)
698 {
699         struct sctp_chunk *retval;
700         struct sctp_sackhdr sack;
701         int len;
702         __u32 ctsn;
703         __u16 num_gabs, num_dup_tsns;
704         struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
705
706         ctsn = sctp_tsnmap_get_ctsn(map);
707         SCTP_DEBUG_PRINTK("sackCTSNAck sent:  0x%x.\n", ctsn);
708
709         /* How much room is needed in the chunk? */
710         num_gabs = sctp_tsnmap_num_gabs(map);
711         num_dup_tsns = sctp_tsnmap_num_dups(map);
712
713         /* Initialize the SACK header.  */
714         sack.cum_tsn_ack            = htonl(ctsn);
715         sack.a_rwnd                 = htonl(asoc->a_rwnd);
716         sack.num_gap_ack_blocks     = htons(num_gabs);
717         sack.num_dup_tsns           = htons(num_dup_tsns);
718
719         len = sizeof(sack)
720                 + sizeof(struct sctp_gap_ack_block) * num_gabs
721                 + sizeof(__u32) * num_dup_tsns;
722
723         /* Create the chunk.  */
724         retval = sctp_make_chunk(asoc, SCTP_CID_SACK, 0, len);
725         if (!retval)
726                 goto nodata;
727
728         /* RFC 2960 6.4 Multi-homed SCTP Endpoints
729          *
730          * An endpoint SHOULD transmit reply chunks (e.g., SACK,
731          * HEARTBEAT ACK, etc.) to the same destination transport
732          * address from which it received the DATA or control chunk to
733          * which it is replying.  This rule should also be followed if
734          * the endpoint is bundling DATA chunks together with the
735          * reply chunk.
736          *
737          * However, when acknowledging multiple DATA chunks received
738          * in packets from different source addresses in a single
739          * SACK, the SACK chunk may be transmitted to one of the
740          * destination transport addresses from which the DATA or
741          * control chunks being acknowledged were received.
742          *
743          * [BUG:  We do not implement the following paragraph.
744          * Perhaps we should remember the last transport we used for a
745          * SACK and avoid that (if possible) if we have seen any
746          * duplicates. --piggy]
747          *
748          * When a receiver of a duplicate DATA chunk sends a SACK to a
749          * multi- homed endpoint it MAY be beneficial to vary the
750          * destination address and not use the source address of the
751          * DATA chunk.  The reason being that receiving a duplicate
752          * from a multi-homed endpoint might indicate that the return
753          * path (as specified in the source address of the DATA chunk)
754          * for the SACK is broken.
755          *
756          * [Send to the address from which we last received a DATA chunk.]
757          */
758         retval->transport = asoc->peer.last_data_from;
759
760         retval->subh.sack_hdr =
761                 sctp_addto_chunk(retval, sizeof(sack), &sack);
762
763         /* Add the gap ack block information.   */
764         if (num_gabs)
765                 sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
766                                  sctp_tsnmap_get_gabs(map));
767
768         /* Add the duplicate TSN information.  */
769         if (num_dup_tsns)
770                 sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
771                                  sctp_tsnmap_get_dups(map));
772
773 nodata:
774         return retval;
775 }
776
777 /* Make a SHUTDOWN chunk. */
778 struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
779                                       const struct sctp_chunk *chunk)
780 {
781         struct sctp_chunk *retval;
782         sctp_shutdownhdr_t shut;
783         __u32 ctsn;
784
785         ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
786         shut.cum_tsn_ack = htonl(ctsn);
787
788         retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN, 0,
789                                  sizeof(sctp_shutdownhdr_t));
790         if (!retval)
791                 goto nodata;
792
793         retval->subh.shutdown_hdr =
794                 sctp_addto_chunk(retval, sizeof(shut), &shut);
795
796         if (chunk)
797                 retval->transport = chunk->transport;
798 nodata:
799         return retval;
800 }
801
802 struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
803                                      const struct sctp_chunk *chunk)
804 {
805         struct sctp_chunk *retval;
806
807         retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0);
808
809         /* RFC 2960 6.4 Multi-homed SCTP Endpoints
810          *
811          * An endpoint SHOULD transmit reply chunks (e.g., SACK,
812          * HEARTBEAT ACK, * etc.) to the same destination transport
813          * address from which it * received the DATA or control chunk
814          * to which it is replying.
815          *
816          * [ACK back to where the SHUTDOWN came from.]
817          */
818         if (retval && chunk)
819                 retval->transport = chunk->transport;
820
821         return retval;
822 }
823
824 struct sctp_chunk *sctp_make_shutdown_complete(
825         const struct sctp_association *asoc,
826         const struct sctp_chunk *chunk)
827 {
828         struct sctp_chunk *retval;
829         __u8 flags = 0;
830
831         /* Set the T-bit if we have no association (vtag will be
832          * reflected)
833          */
834         flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
835
836         retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags, 0);
837
838         /* RFC 2960 6.4 Multi-homed SCTP Endpoints
839          *
840          * An endpoint SHOULD transmit reply chunks (e.g., SACK,
841          * HEARTBEAT ACK, * etc.) to the same destination transport
842          * address from which it * received the DATA or control chunk
843          * to which it is replying.
844          *
845          * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
846          * came from.]
847          */
848         if (retval && chunk)
849                 retval->transport = chunk->transport;
850
851         return retval;
852 }
853
854 /* Create an ABORT.  Note that we set the T bit if we have no
855  * association, except when responding to an INIT (sctpimpguide 2.41).
856  */
857 struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
858                               const struct sctp_chunk *chunk,
859                               const size_t hint)
860 {
861         struct sctp_chunk *retval;
862         __u8 flags = 0;
863
864         /* Set the T-bit if we have no association and 'chunk' is not
865          * an INIT (vtag will be reflected).
866          */
867         if (!asoc) {
868                 if (chunk && chunk->chunk_hdr &&
869                     chunk->chunk_hdr->type == SCTP_CID_INIT)
870                         flags = 0;
871                 else
872                         flags = SCTP_CHUNK_FLAG_T;
873         }
874
875         retval = sctp_make_chunk(asoc, SCTP_CID_ABORT, flags, hint);
876
877         /* RFC 2960 6.4 Multi-homed SCTP Endpoints
878          *
879          * An endpoint SHOULD transmit reply chunks (e.g., SACK,
880          * HEARTBEAT ACK, * etc.) to the same destination transport
881          * address from which it * received the DATA or control chunk
882          * to which it is replying.
883          *
884          * [ABORT back to where the offender came from.]
885          */
886         if (retval && chunk)
887                 retval->transport = chunk->transport;
888
889         return retval;
890 }
891
892 /* Helper to create ABORT with a NO_USER_DATA error.  */
893 struct sctp_chunk *sctp_make_abort_no_data(
894         const struct sctp_association *asoc,
895         const struct sctp_chunk *chunk, __u32 tsn)
896 {
897         struct sctp_chunk *retval;
898         __be32 payload;
899
900         retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t)
901                                  + sizeof(tsn));
902
903         if (!retval)
904                 goto no_mem;
905
906         /* Put the tsn back into network byte order.  */
907         payload = htonl(tsn);
908         sctp_init_cause(retval, SCTP_ERROR_NO_DATA, sizeof(payload));
909         sctp_addto_chunk(retval, sizeof(payload), (const void *)&payload);
910
911         /* RFC 2960 6.4 Multi-homed SCTP Endpoints
912          *
913          * An endpoint SHOULD transmit reply chunks (e.g., SACK,
914          * HEARTBEAT ACK, * etc.) to the same destination transport
915          * address from which it * received the DATA or control chunk
916          * to which it is replying.
917          *
918          * [ABORT back to where the offender came from.]
919          */
920         if (chunk)
921                 retval->transport = chunk->transport;
922
923 no_mem:
924         return retval;
925 }
926
927 /* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error.  */
928 struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
929                                         const struct msghdr *msg,
930                                         size_t paylen)
931 {
932         struct sctp_chunk *retval;
933         void *payload = NULL;
934         int err;
935
936         retval = sctp_make_abort(asoc, NULL, sizeof(sctp_errhdr_t) + paylen);
937         if (!retval)
938                 goto err_chunk;
939
940         if (paylen) {
941                 /* Put the msg_iov together into payload.  */
942                 payload = kmalloc(paylen, GFP_KERNEL);
943                 if (!payload)
944                         goto err_payload;
945
946                 err = memcpy_fromiovec(payload, msg->msg_iov, paylen);
947                 if (err < 0)
948                         goto err_copy;
949         }
950
951         sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, paylen);
952         sctp_addto_chunk(retval, paylen, payload);
953
954         if (paylen)
955                 kfree(payload);
956
957         return retval;
958
959 err_copy:
960         kfree(payload);
961 err_payload:
962         sctp_chunk_free(retval);
963         retval = NULL;
964 err_chunk:
965         return retval;
966 }
967
968 /* Append bytes to the end of a parameter.  Will panic if chunk is not big
969  * enough.
970  */
971 static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
972                               const void *data)
973 {
974         void *target;
975         int chunklen = ntohs(chunk->chunk_hdr->length);
976
977         target = skb_put(chunk->skb, len);
978
979         memcpy(target, data, len);
980
981         /* Adjust the chunk length field.  */
982         chunk->chunk_hdr->length = htons(chunklen + len);
983         chunk->chunk_end = skb_tail_pointer(chunk->skb);
984
985         return target;
986 }
987
988 /* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
989 struct sctp_chunk *sctp_make_abort_violation(
990         const struct sctp_association *asoc,
991         const struct sctp_chunk *chunk,
992         const __u8   *payload,
993         const size_t paylen)
994 {
995         struct sctp_chunk  *retval;
996         struct sctp_paramhdr phdr;
997
998         retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t) + paylen
999                                         + sizeof(sctp_paramhdr_t));
1000         if (!retval)
1001                 goto end;
1002
1003         sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, paylen
1004                                         + sizeof(sctp_paramhdr_t));
1005
1006         phdr.type = htons(chunk->chunk_hdr->type);
1007         phdr.length = chunk->chunk_hdr->length;
1008         sctp_addto_chunk(retval, paylen, payload);
1009         sctp_addto_param(retval, sizeof(sctp_paramhdr_t), &phdr);
1010
1011 end:
1012         return retval;
1013 }
1014
1015 struct sctp_chunk *sctp_make_violation_paramlen(
1016         const struct sctp_association *asoc,
1017         const struct sctp_chunk *chunk,
1018         struct sctp_paramhdr *param)
1019 {
1020         struct sctp_chunk *retval;
1021         static const char error[] = "The following parameter had invalid length:";
1022         size_t payload_len = sizeof(error) + sizeof(sctp_errhdr_t) +
1023                                 sizeof(sctp_paramhdr_t);
1024
1025         retval = sctp_make_abort(asoc, chunk, payload_len);
1026         if (!retval)
1027                 goto nodata;
1028
1029         sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION,
1030                         sizeof(error) + sizeof(sctp_paramhdr_t));
1031         sctp_addto_chunk(retval, sizeof(error), error);
1032         sctp_addto_param(retval, sizeof(sctp_paramhdr_t), param);
1033
1034 nodata:
1035         return retval;
1036 }
1037
1038 /* Make a HEARTBEAT chunk.  */
1039 struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
1040                                   const struct sctp_transport *transport,
1041                                   const void *payload, const size_t paylen)
1042 {
1043         struct sctp_chunk *retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT,
1044                                                     0, paylen);
1045
1046         if (!retval)
1047                 goto nodata;
1048
1049         /* Cast away the 'const', as this is just telling the chunk
1050          * what transport it belongs to.
1051          */
1052         retval->transport = (struct sctp_transport *) transport;
1053         retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
1054
1055 nodata:
1056         return retval;
1057 }
1058
1059 struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
1060                                       const struct sctp_chunk *chunk,
1061                                       const void *payload, const size_t paylen)
1062 {
1063         struct sctp_chunk *retval;
1064
1065         retval  = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen);
1066         if (!retval)
1067                 goto nodata;
1068
1069         retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
1070
1071         /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1072          *
1073          * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1074          * HEARTBEAT ACK, * etc.) to the same destination transport
1075          * address from which it * received the DATA or control chunk
1076          * to which it is replying.
1077          *
1078          * [HBACK back to where the HEARTBEAT came from.]
1079          */
1080         if (chunk)
1081                 retval->transport = chunk->transport;
1082
1083 nodata:
1084         return retval;
1085 }
1086
1087 /* Create an Operation Error chunk with the specified space reserved.
1088  * This routine can be used for containing multiple causes in the chunk.
1089  */
1090 static struct sctp_chunk *sctp_make_op_error_space(
1091         const struct sctp_association *asoc,
1092         const struct sctp_chunk *chunk,
1093         size_t size)
1094 {
1095         struct sctp_chunk *retval;
1096
1097         retval = sctp_make_chunk(asoc, SCTP_CID_ERROR, 0,
1098                                  sizeof(sctp_errhdr_t) + size);
1099         if (!retval)
1100                 goto nodata;
1101
1102         /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1103          *
1104          * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1105          * HEARTBEAT ACK, etc.) to the same destination transport
1106          * address from which it received the DATA or control chunk
1107          * to which it is replying.
1108          *
1109          */
1110         if (chunk)
1111                 retval->transport = chunk->transport;
1112
1113 nodata:
1114         return retval;
1115 }
1116
1117 /* Create an Operation Error chunk.  */
1118 struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
1119                                  const struct sctp_chunk *chunk,
1120                                  __be16 cause_code, const void *payload,
1121                                  size_t paylen)
1122 {
1123         struct sctp_chunk *retval;
1124
1125         retval = sctp_make_op_error_space(asoc, chunk, paylen);
1126         if (!retval)
1127                 goto nodata;
1128
1129         sctp_init_cause(retval, cause_code, paylen);
1130         sctp_addto_chunk(retval, paylen, payload);
1131
1132 nodata:
1133         return retval;
1134 }
1135
1136 struct sctp_chunk *sctp_make_auth(const struct sctp_association *asoc)
1137 {
1138         struct sctp_chunk *retval;
1139         struct sctp_hmac *hmac_desc;
1140         struct sctp_authhdr auth_hdr;
1141         __u8 *hmac;
1142
1143         /* Get the first hmac that the peer told us to use */
1144         hmac_desc = sctp_auth_asoc_get_hmac(asoc);
1145         if (unlikely(!hmac_desc))
1146                 return NULL;
1147
1148         retval = sctp_make_chunk(asoc, SCTP_CID_AUTH, 0,
1149                         hmac_desc->hmac_len + sizeof(sctp_authhdr_t));
1150         if (!retval)
1151                 return NULL;
1152
1153         auth_hdr.hmac_id = htons(hmac_desc->hmac_id);
1154         auth_hdr.shkey_id = htons(asoc->active_key_id);
1155
1156         retval->subh.auth_hdr = sctp_addto_chunk(retval, sizeof(sctp_authhdr_t),
1157                                                 &auth_hdr);
1158
1159         hmac = skb_put(retval->skb, hmac_desc->hmac_len);
1160         memset(hmac, 0, hmac_desc->hmac_len);
1161
1162         /* Adjust the chunk header to include the empty MAC */
1163         retval->chunk_hdr->length =
1164                 htons(ntohs(retval->chunk_hdr->length) + hmac_desc->hmac_len);
1165         retval->chunk_end = skb_tail_pointer(retval->skb);
1166
1167         return retval;
1168 }
1169
1170
1171 /********************************************************************
1172  * 2nd Level Abstractions
1173  ********************************************************************/
1174
1175 /* Turn an skb into a chunk.
1176  * FIXME: Eventually move the structure directly inside the skb->cb[].
1177  */
1178 struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
1179                             const struct sctp_association *asoc,
1180                             struct sock *sk)
1181 {
1182         struct sctp_chunk *retval;
1183
1184         retval = kmem_cache_zalloc(sctp_chunk_cachep, GFP_ATOMIC);
1185
1186         if (!retval)
1187                 goto nodata;
1188
1189         if (!sk) {
1190                 SCTP_DEBUG_PRINTK("chunkifying skb %p w/o an sk\n", skb);
1191         }
1192
1193         INIT_LIST_HEAD(&retval->list);
1194         retval->skb             = skb;
1195         retval->asoc            = (struct sctp_association *)asoc;
1196         retval->resent          = 0;
1197         retval->has_tsn         = 0;
1198         retval->has_ssn         = 0;
1199         retval->rtt_in_progress = 0;
1200         retval->sent_at         = 0;
1201         retval->singleton       = 1;
1202         retval->end_of_packet   = 0;
1203         retval->ecn_ce_done     = 0;
1204         retval->pdiscard        = 0;
1205
1206         /* sctpimpguide-05.txt Section 2.8.2
1207          * M1) Each time a new DATA chunk is transmitted
1208          * set the 'TSN.Missing.Report' count for that TSN to 0. The
1209          * 'TSN.Missing.Report' count will be used to determine missing chunks
1210          * and when to fast retransmit.
1211          */
1212         retval->tsn_missing_report = 0;
1213         retval->tsn_gap_acked = 0;
1214         retval->fast_retransmit = SCTP_CAN_FRTX;
1215
1216         /* If this is a fragmented message, track all fragments
1217          * of the message (for SEND_FAILED).
1218          */
1219         retval->msg = NULL;
1220
1221         /* Polish the bead hole.  */
1222         INIT_LIST_HEAD(&retval->transmitted_list);
1223         INIT_LIST_HEAD(&retval->frag_list);
1224         SCTP_DBG_OBJCNT_INC(chunk);
1225         atomic_set(&retval->refcnt, 1);
1226
1227 nodata:
1228         return retval;
1229 }
1230
1231 /* Set chunk->source and dest based on the IP header in chunk->skb.  */
1232 void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
1233                      union sctp_addr *dest)
1234 {
1235         memcpy(&chunk->source, src, sizeof(union sctp_addr));
1236         memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
1237 }
1238
1239 /* Extract the source address from a chunk.  */
1240 const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
1241 {
1242         /* If we have a known transport, use that.  */
1243         if (chunk->transport) {
1244                 return &chunk->transport->ipaddr;
1245         } else {
1246                 /* Otherwise, extract it from the IP header.  */
1247                 return &chunk->source;
1248         }
1249 }
1250
1251 /* Create a new chunk, setting the type and flags headers from the
1252  * arguments, reserving enough space for a 'paylen' byte payload.
1253  */
1254 SCTP_STATIC
1255 struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
1256                                    __u8 type, __u8 flags, int paylen)
1257 {
1258         struct sctp_chunk *retval;
1259         sctp_chunkhdr_t *chunk_hdr;
1260         struct sk_buff *skb;
1261         struct sock *sk;
1262
1263         /* No need to allocate LL here, as this is only a chunk. */
1264         skb = alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t) + paylen),
1265                         GFP_ATOMIC);
1266         if (!skb)
1267                 goto nodata;
1268
1269         /* Make room for the chunk header.  */
1270         chunk_hdr = (sctp_chunkhdr_t *)skb_put(skb, sizeof(sctp_chunkhdr_t));
1271         chunk_hdr->type   = type;
1272         chunk_hdr->flags  = flags;
1273         chunk_hdr->length = htons(sizeof(sctp_chunkhdr_t));
1274
1275         sk = asoc ? asoc->base.sk : NULL;
1276         retval = sctp_chunkify(skb, asoc, sk);
1277         if (!retval) {
1278                 kfree_skb(skb);
1279                 goto nodata;
1280         }
1281
1282         retval->chunk_hdr = chunk_hdr;
1283         retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(struct sctp_chunkhdr);
1284
1285         /* Determine if the chunk needs to be authenticated */
1286         if (sctp_auth_send_cid(type, asoc))
1287                 retval->auth = 1;
1288
1289         /* Set the skb to the belonging sock for accounting.  */
1290         skb->sk = sk;
1291
1292         return retval;
1293 nodata:
1294         return NULL;
1295 }
1296
1297
1298 /* Release the memory occupied by a chunk.  */
1299 static void sctp_chunk_destroy(struct sctp_chunk *chunk)
1300 {
1301         BUG_ON(!list_empty(&chunk->list));
1302         list_del_init(&chunk->transmitted_list);
1303
1304         /* Free the chunk skb data and the SCTP_chunk stub itself. */
1305         dev_kfree_skb(chunk->skb);
1306
1307         SCTP_DBG_OBJCNT_DEC(chunk);
1308         kmem_cache_free(sctp_chunk_cachep, chunk);
1309 }
1310
1311 /* Possibly, free the chunk.  */
1312 void sctp_chunk_free(struct sctp_chunk *chunk)
1313 {
1314         /* Release our reference on the message tracker. */
1315         if (chunk->msg)
1316                 sctp_datamsg_put(chunk->msg);
1317
1318         sctp_chunk_put(chunk);
1319 }
1320
1321 /* Grab a reference to the chunk. */
1322 void sctp_chunk_hold(struct sctp_chunk *ch)
1323 {
1324         atomic_inc(&ch->refcnt);
1325 }
1326
1327 /* Release a reference to the chunk. */
1328 void sctp_chunk_put(struct sctp_chunk *ch)
1329 {
1330         if (atomic_dec_and_test(&ch->refcnt))
1331                 sctp_chunk_destroy(ch);
1332 }
1333
1334 /* Append bytes to the end of a chunk.  Will panic if chunk is not big
1335  * enough.
1336  */
1337 void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
1338 {
1339         void *target;
1340         void *padding;
1341         int chunklen = ntohs(chunk->chunk_hdr->length);
1342         int padlen = WORD_ROUND(chunklen) - chunklen;
1343
1344         padding = skb_put(chunk->skb, padlen);
1345         target = skb_put(chunk->skb, len);
1346
1347         memset(padding, 0, padlen);
1348         memcpy(target, data, len);
1349
1350         /* Adjust the chunk length field.  */
1351         chunk->chunk_hdr->length = htons(chunklen + padlen + len);
1352         chunk->chunk_end = skb_tail_pointer(chunk->skb);
1353
1354         return target;
1355 }
1356
1357 /* Append bytes from user space to the end of a chunk.  Will panic if
1358  * chunk is not big enough.
1359  * Returns a kernel err value.
1360  */
1361 int sctp_user_addto_chunk(struct sctp_chunk *chunk, int off, int len,
1362                           struct iovec *data)
1363 {
1364         __u8 *target;
1365         int err = 0;
1366
1367         /* Make room in chunk for data.  */
1368         target = skb_put(chunk->skb, len);
1369
1370         /* Copy data (whole iovec) into chunk */
1371         if ((err = memcpy_fromiovecend(target, data, off, len)))
1372                 goto out;
1373
1374         /* Adjust the chunk length field.  */
1375         chunk->chunk_hdr->length =
1376                 htons(ntohs(chunk->chunk_hdr->length) + len);
1377         chunk->chunk_end = skb_tail_pointer(chunk->skb);
1378
1379 out:
1380         return err;
1381 }
1382
1383 /* Helper function to assign a TSN if needed.  This assumes that both
1384  * the data_hdr and association have already been assigned.
1385  */
1386 void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
1387 {
1388         struct sctp_datamsg *msg;
1389         struct sctp_chunk *lchunk;
1390         struct sctp_stream *stream;
1391         __u16 ssn;
1392         __u16 sid;
1393
1394         if (chunk->has_ssn)
1395                 return;
1396
1397         /* All fragments will be on the same stream */
1398         sid = ntohs(chunk->subh.data_hdr->stream);
1399         stream = &chunk->asoc->ssnmap->out;
1400
1401         /* Now assign the sequence number to the entire message.
1402          * All fragments must have the same stream sequence number.
1403          */
1404         msg = chunk->msg;
1405         list_for_each_entry(lchunk, &msg->chunks, frag_list) {
1406                 if (lchunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
1407                         ssn = 0;
1408                 } else {
1409                         if (lchunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
1410                                 ssn = sctp_ssn_next(stream, sid);
1411                         else
1412                                 ssn = sctp_ssn_peek(stream, sid);
1413                 }
1414
1415                 lchunk->subh.data_hdr->ssn = htons(ssn);
1416                 lchunk->has_ssn = 1;
1417         }
1418 }
1419
1420 /* Helper function to assign a TSN if needed.  This assumes that both
1421  * the data_hdr and association have already been assigned.
1422  */
1423 void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
1424 {
1425         if (!chunk->has_tsn) {
1426                 /* This is the last possible instant to
1427                  * assign a TSN.
1428                  */
1429                 chunk->subh.data_hdr->tsn =
1430                         htonl(sctp_association_get_next_tsn(chunk->asoc));
1431                 chunk->has_tsn = 1;
1432         }
1433 }
1434
1435 /* Create a CLOSED association to use with an incoming packet.  */
1436 struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
1437                                         struct sctp_chunk *chunk,
1438                                         gfp_t gfp)
1439 {
1440         struct sctp_association *asoc;
1441         struct sk_buff *skb;
1442         sctp_scope_t scope;
1443         struct sctp_af *af;
1444
1445         /* Create the bare association.  */
1446         scope = sctp_scope(sctp_source(chunk));
1447         asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
1448         if (!asoc)
1449                 goto nodata;
1450         asoc->temp = 1;
1451         skb = chunk->skb;
1452         /* Create an entry for the source address of the packet.  */
1453         af = sctp_get_af_specific(ipver2af(ip_hdr(skb)->version));
1454         if (unlikely(!af))
1455                 goto fail;
1456         af->from_skb(&asoc->c.peer_addr, skb, 1);
1457 nodata:
1458         return asoc;
1459
1460 fail:
1461         sctp_association_free(asoc);
1462         return NULL;
1463 }
1464
1465 /* Build a cookie representing asoc.
1466  * This INCLUDES the param header needed to put the cookie in the INIT ACK.
1467  */
1468 static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
1469                                       const struct sctp_association *asoc,
1470                                       const struct sctp_chunk *init_chunk,
1471                                       int *cookie_len,
1472                                       const __u8 *raw_addrs, int addrs_len)
1473 {
1474         sctp_cookie_param_t *retval;
1475         struct sctp_signed_cookie *cookie;
1476         struct scatterlist sg;
1477         int headersize, bodysize;
1478         unsigned int keylen;
1479         char *key;
1480
1481         /* Header size is static data prior to the actual cookie, including
1482          * any padding.
1483          */
1484         headersize = sizeof(sctp_paramhdr_t) +
1485                      (sizeof(struct sctp_signed_cookie) -
1486                       sizeof(struct sctp_cookie));
1487         bodysize = sizeof(struct sctp_cookie)
1488                 + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
1489
1490         /* Pad out the cookie to a multiple to make the signature
1491          * functions simpler to write.
1492          */
1493         if (bodysize % SCTP_COOKIE_MULTIPLE)
1494                 bodysize += SCTP_COOKIE_MULTIPLE
1495                         - (bodysize % SCTP_COOKIE_MULTIPLE);
1496         *cookie_len = headersize + bodysize;
1497
1498         /* Clear this memory since we are sending this data structure
1499          * out on the network.
1500          */
1501         retval = kzalloc(*cookie_len, GFP_ATOMIC);
1502         if (!retval)
1503                 goto nodata;
1504
1505         cookie = (struct sctp_signed_cookie *) retval->body;
1506
1507         /* Set up the parameter header.  */
1508         retval->p.type = SCTP_PARAM_STATE_COOKIE;
1509         retval->p.length = htons(*cookie_len);
1510
1511         /* Copy the cookie part of the association itself.  */
1512         cookie->c = asoc->c;
1513         /* Save the raw address list length in the cookie. */
1514         cookie->c.raw_addr_list_len = addrs_len;
1515
1516         /* Remember PR-SCTP capability. */
1517         cookie->c.prsctp_capable = asoc->peer.prsctp_capable;
1518
1519         /* Save adaptation indication in the cookie. */
1520         cookie->c.adaptation_ind = asoc->peer.adaptation_ind;
1521
1522         /* Set an expiration time for the cookie.  */
1523         do_gettimeofday(&cookie->c.expiration);
1524         TIMEVAL_ADD(asoc->cookie_life, cookie->c.expiration);
1525
1526         /* Copy the peer's init packet.  */
1527         memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
1528                ntohs(init_chunk->chunk_hdr->length));
1529
1530         /* Copy the raw local address list of the association. */
1531         memcpy((__u8 *)&cookie->c.peer_init[0] +
1532                ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
1533
1534         if (sctp_sk(ep->base.sk)->hmac) {
1535                 struct hash_desc desc;
1536
1537                 /* Sign the message.  */
1538                 sg_init_one(&sg, &cookie->c, bodysize);
1539                 keylen = SCTP_SECRET_SIZE;
1540                 key = (char *)ep->secret_key[ep->current_key];
1541                 desc.tfm = sctp_sk(ep->base.sk)->hmac;
1542                 desc.flags = 0;
1543
1544                 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1545                     crypto_hash_digest(&desc, &sg, bodysize, cookie->signature))
1546                         goto free_cookie;
1547         }
1548
1549         return retval;
1550
1551 free_cookie:
1552         kfree(retval);
1553 nodata:
1554         *cookie_len = 0;
1555         return NULL;
1556 }
1557
1558 /* Unpack the cookie from COOKIE ECHO chunk, recreating the association.  */
1559 struct sctp_association *sctp_unpack_cookie(
1560         const struct sctp_endpoint *ep,
1561         const struct sctp_association *asoc,
1562         struct sctp_chunk *chunk, gfp_t gfp,
1563         int *error, struct sctp_chunk **errp)
1564 {
1565         struct sctp_association *retval = NULL;
1566         struct sctp_signed_cookie *cookie;
1567         struct sctp_cookie *bear_cookie;
1568         int headersize, bodysize, fixed_size;
1569         __u8 *digest = ep->digest;
1570         struct scatterlist sg;
1571         unsigned int keylen, len;
1572         char *key;
1573         sctp_scope_t scope;
1574         struct sk_buff *skb = chunk->skb;
1575         struct timeval tv;
1576         struct hash_desc desc;
1577
1578         /* Header size is static data prior to the actual cookie, including
1579          * any padding.
1580          */
1581         headersize = sizeof(sctp_chunkhdr_t) +
1582                      (sizeof(struct sctp_signed_cookie) -
1583                       sizeof(struct sctp_cookie));
1584         bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
1585         fixed_size = headersize + sizeof(struct sctp_cookie);
1586
1587         /* Verify that the chunk looks like it even has a cookie.
1588          * There must be enough room for our cookie and our peer's
1589          * INIT chunk.
1590          */
1591         len = ntohs(chunk->chunk_hdr->length);
1592         if (len < fixed_size + sizeof(struct sctp_chunkhdr))
1593                 goto malformed;
1594
1595         /* Verify that the cookie has been padded out. */
1596         if (bodysize % SCTP_COOKIE_MULTIPLE)
1597                 goto malformed;
1598
1599         /* Process the cookie.  */
1600         cookie = chunk->subh.cookie_hdr;
1601         bear_cookie = &cookie->c;
1602
1603         if (!sctp_sk(ep->base.sk)->hmac)
1604                 goto no_hmac;
1605
1606         /* Check the signature.  */
1607         keylen = SCTP_SECRET_SIZE;
1608         sg_init_one(&sg, bear_cookie, bodysize);
1609         key = (char *)ep->secret_key[ep->current_key];
1610         desc.tfm = sctp_sk(ep->base.sk)->hmac;
1611         desc.flags = 0;
1612
1613         memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
1614         if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1615             crypto_hash_digest(&desc, &sg, bodysize, digest)) {
1616                 *error = -SCTP_IERROR_NOMEM;
1617                 goto fail;
1618         }
1619
1620         if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1621                 /* Try the previous key. */
1622                 key = (char *)ep->secret_key[ep->last_key];
1623                 memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
1624                 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1625                     crypto_hash_digest(&desc, &sg, bodysize, digest)) {
1626                         *error = -SCTP_IERROR_NOMEM;
1627                         goto fail;
1628                 }
1629
1630                 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1631                         /* Yikes!  Still bad signature! */
1632                         *error = -SCTP_IERROR_BAD_SIG;
1633                         goto fail;
1634                 }
1635         }
1636
1637 no_hmac:
1638         /* IG Section 2.35.2:
1639          *  3) Compare the port numbers and the verification tag contained
1640          *     within the COOKIE ECHO chunk to the actual port numbers and the
1641          *     verification tag within the SCTP common header of the received
1642          *     packet. If these values do not match the packet MUST be silently
1643          *     discarded,
1644          */
1645         if (ntohl(chunk->sctp_hdr->vtag) != bear_cookie->my_vtag) {
1646                 *error = -SCTP_IERROR_BAD_TAG;
1647                 goto fail;
1648         }
1649
1650         if (chunk->sctp_hdr->source != bear_cookie->peer_addr.v4.sin_port ||
1651             ntohs(chunk->sctp_hdr->dest) != bear_cookie->my_port) {
1652                 *error = -SCTP_IERROR_BAD_PORTS;
1653                 goto fail;
1654         }
1655
1656         /* Check to see if the cookie is stale.  If there is already
1657          * an association, there is no need to check cookie's expiration
1658          * for init collision case of lost COOKIE ACK.
1659          * If skb has been timestamped, then use the stamp, otherwise
1660          * use current time.  This introduces a small possibility that
1661          * that a cookie may be considered expired, but his would only slow
1662          * down the new association establishment instead of every packet.
1663          */
1664         if (sock_flag(ep->base.sk, SOCK_TIMESTAMP))
1665                 skb_get_timestamp(skb, &tv);
1666         else
1667                 do_gettimeofday(&tv);
1668
1669         if (!asoc && tv_lt(bear_cookie->expiration, tv)) {
1670                 /*
1671                  * Section 3.3.10.3 Stale Cookie Error (3)
1672                  *
1673                  * Cause of error
1674                  * ---------------
1675                  * Stale Cookie Error:  Indicates the receipt of a valid State
1676                  * Cookie that has expired.
1677                  */
1678                 len = ntohs(chunk->chunk_hdr->length);
1679                 *errp = sctp_make_op_error_space(asoc, chunk, len);
1680                 if (*errp) {
1681                         suseconds_t usecs = (tv.tv_sec -
1682                                 bear_cookie->expiration.tv_sec) * 1000000L +
1683                                 tv.tv_usec - bear_cookie->expiration.tv_usec;
1684                         __be32 n = htonl(usecs);
1685
1686                         sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
1687                                         sizeof(n));
1688                         sctp_addto_chunk(*errp, sizeof(n), &n);
1689                         *error = -SCTP_IERROR_STALE_COOKIE;
1690                 } else
1691                         *error = -SCTP_IERROR_NOMEM;
1692
1693                 goto fail;
1694         }
1695
1696         /* Make a new base association.  */
1697         scope = sctp_scope(sctp_source(chunk));
1698         retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
1699         if (!retval) {
1700                 *error = -SCTP_IERROR_NOMEM;
1701                 goto fail;
1702         }
1703
1704         /* Set up our peer's port number.  */
1705         retval->peer.port = ntohs(chunk->sctp_hdr->source);
1706
1707         /* Populate the association from the cookie.  */
1708         memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
1709
1710         if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
1711                                                  GFP_ATOMIC) < 0) {
1712                 *error = -SCTP_IERROR_NOMEM;
1713                 goto fail;
1714         }
1715
1716         /* Also, add the destination address. */
1717         if (list_empty(&retval->base.bind_addr.address_list)) {
1718                 sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest,
1719                                 SCTP_ADDR_SRC, GFP_ATOMIC);
1720         }
1721
1722         retval->next_tsn = retval->c.initial_tsn;
1723         retval->ctsn_ack_point = retval->next_tsn - 1;
1724         retval->addip_serial = retval->c.initial_tsn;
1725         retval->adv_peer_ack_point = retval->ctsn_ack_point;
1726         retval->peer.prsctp_capable = retval->c.prsctp_capable;
1727         retval->peer.adaptation_ind = retval->c.adaptation_ind;
1728
1729         /* The INIT stuff will be done by the side effects.  */
1730         return retval;
1731
1732 fail:
1733         if (retval)
1734                 sctp_association_free(retval);
1735
1736         return NULL;
1737
1738 malformed:
1739         /* Yikes!  The packet is either corrupt or deliberately
1740          * malformed.
1741          */
1742         *error = -SCTP_IERROR_MALFORMED;
1743         goto fail;
1744 }
1745
1746 /********************************************************************
1747  * 3rd Level Abstractions
1748  ********************************************************************/
1749
1750 struct __sctp_missing {
1751         __be32 num_missing;
1752         __be16 type;
1753 }  __attribute__((packed));
1754
1755 /*
1756  * Report a missing mandatory parameter.
1757  */
1758 static int sctp_process_missing_param(const struct sctp_association *asoc,
1759                                       sctp_param_t paramtype,
1760                                       struct sctp_chunk *chunk,
1761                                       struct sctp_chunk **errp)
1762 {
1763         struct __sctp_missing report;
1764         __u16 len;
1765
1766         len = WORD_ROUND(sizeof(report));
1767
1768         /* Make an ERROR chunk, preparing enough room for
1769          * returning multiple unknown parameters.
1770          */
1771         if (!*errp)
1772                 *errp = sctp_make_op_error_space(asoc, chunk, len);
1773
1774         if (*errp) {
1775                 report.num_missing = htonl(1);
1776                 report.type = paramtype;
1777                 sctp_init_cause(*errp, SCTP_ERROR_MISS_PARAM,
1778                                 sizeof(report));
1779                 sctp_addto_chunk(*errp, sizeof(report), &report);
1780         }
1781
1782         /* Stop processing this chunk. */
1783         return 0;
1784 }
1785
1786 /* Report an Invalid Mandatory Parameter.  */
1787 static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
1788                                       struct sctp_chunk *chunk,
1789                                       struct sctp_chunk **errp)
1790 {
1791         /* Invalid Mandatory Parameter Error has no payload. */
1792
1793         if (!*errp)
1794                 *errp = sctp_make_op_error_space(asoc, chunk, 0);
1795
1796         if (*errp)
1797                 sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, 0);
1798
1799         /* Stop processing this chunk. */
1800         return 0;
1801 }
1802
1803 static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
1804                                         struct sctp_paramhdr *param,
1805                                         const struct sctp_chunk *chunk,
1806                                         struct sctp_chunk **errp)
1807 {
1808         /* This is a fatal error.  Any accumulated non-fatal errors are
1809          * not reported.
1810          */
1811         if (*errp)
1812                 sctp_chunk_free(*errp);
1813
1814         /* Create an error chunk and fill it in with our payload. */
1815         *errp = sctp_make_violation_paramlen(asoc, chunk, param);
1816
1817         return 0;
1818 }
1819
1820
1821 /* Do not attempt to handle the HOST_NAME parm.  However, do
1822  * send back an indicator to the peer.
1823  */
1824 static int sctp_process_hn_param(const struct sctp_association *asoc,
1825                                  union sctp_params param,
1826                                  struct sctp_chunk *chunk,
1827                                  struct sctp_chunk **errp)
1828 {
1829         __u16 len = ntohs(param.p->length);
1830
1831         /* Processing of the HOST_NAME parameter will generate an
1832          * ABORT.  If we've accumulated any non-fatal errors, they
1833          * would be unrecognized parameters and we should not include
1834          * them in the ABORT.
1835          */
1836         if (*errp)
1837                 sctp_chunk_free(*errp);
1838
1839         *errp = sctp_make_op_error_space(asoc, chunk, len);
1840
1841         if (*errp) {
1842                 sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED, len);
1843                 sctp_addto_chunk(*errp, len, param.v);
1844         }
1845
1846         /* Stop processing this chunk. */
1847         return 0;
1848 }
1849
1850 static int sctp_verify_ext_param(union sctp_params param)
1851 {
1852         __u16 num_ext = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
1853         int have_auth = 0;
1854         int have_asconf = 0;
1855         int i;
1856
1857         for (i = 0; i < num_ext; i++) {
1858                 switch (param.ext->chunks[i]) {
1859                     case SCTP_CID_AUTH:
1860                             have_auth = 1;
1861                             break;
1862                     case SCTP_CID_ASCONF:
1863                     case SCTP_CID_ASCONF_ACK:
1864                             have_asconf = 1;
1865                             break;
1866                 }
1867         }
1868
1869         /* ADD-IP Security: The draft requires us to ABORT or ignore the
1870          * INIT/INIT-ACK if ADD-IP is listed, but AUTH is not.  Do this
1871          * only if ADD-IP is turned on and we are not backward-compatible
1872          * mode.
1873          */
1874         if (sctp_addip_noauth)
1875                 return 1;
1876
1877         if (sctp_addip_enable && !have_auth && have_asconf)
1878                 return 0;
1879
1880         return 1;
1881 }
1882
1883 static void sctp_process_ext_param(struct sctp_association *asoc,
1884                                     union sctp_params param)
1885 {
1886         __u16 num_ext = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
1887         int i;
1888
1889         for (i = 0; i < num_ext; i++) {
1890                 switch (param.ext->chunks[i]) {
1891                     case SCTP_CID_FWD_TSN:
1892                             if (sctp_prsctp_enable &&
1893                                 !asoc->peer.prsctp_capable)
1894                                     asoc->peer.prsctp_capable = 1;
1895                             break;
1896                     case SCTP_CID_AUTH:
1897                             /* if the peer reports AUTH, assume that he
1898                              * supports AUTH.
1899                              */
1900                             if (sctp_auth_enable)
1901                                     asoc->peer.auth_capable = 1;
1902                             break;
1903                     case SCTP_CID_ASCONF:
1904                     case SCTP_CID_ASCONF_ACK:
1905                             if (sctp_addip_enable)
1906                                     asoc->peer.asconf_capable = 1;
1907                             break;
1908                     default:
1909                             break;
1910                 }
1911         }
1912 }
1913
1914 /* RFC 3.2.1 & the Implementers Guide 2.2.
1915  *
1916  * The Parameter Types are encoded such that the
1917  * highest-order two bits specify the action that must be
1918  * taken if the processing endpoint does not recognize the
1919  * Parameter Type.
1920  *
1921  * 00 - Stop processing this parameter; do not process any further
1922  *      parameters within this chunk
1923  *
1924  * 01 - Stop processing this parameter, do not process any further
1925  *      parameters within this chunk, and report the unrecognized
1926  *      parameter in an 'Unrecognized Parameter' ERROR chunk.
1927  *
1928  * 10 - Skip this parameter and continue processing.
1929  *
1930  * 11 - Skip this parameter and continue processing but
1931  *      report the unrecognized parameter in an
1932  *      'Unrecognized Parameter' ERROR chunk.
1933  *
1934  * Return value:
1935  *      SCTP_IERROR_NO_ERROR - continue with the chunk
1936  *      SCTP_IERROR_ERROR    - stop and report an error.
1937  *      SCTP_IERROR_NOMEME   - out of memory.
1938  */
1939 static sctp_ierror_t sctp_process_unk_param(const struct sctp_association *asoc,
1940                                             union sctp_params param,
1941                                             struct sctp_chunk *chunk,
1942                                             struct sctp_chunk **errp)
1943 {
1944         int retval = SCTP_IERROR_NO_ERROR;
1945
1946         switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
1947         case SCTP_PARAM_ACTION_DISCARD:
1948                 retval =  SCTP_IERROR_ERROR;
1949                 break;
1950         case SCTP_PARAM_ACTION_SKIP:
1951                 break;
1952         case SCTP_PARAM_ACTION_DISCARD_ERR:
1953                 retval =  SCTP_IERROR_ERROR;
1954                 /* Fall through */
1955         case SCTP_PARAM_ACTION_SKIP_ERR:
1956                 /* Make an ERROR chunk, preparing enough room for
1957                  * returning multiple unknown parameters.
1958                  */
1959                 if (NULL == *errp)
1960                         *errp = sctp_make_op_error_space(asoc, chunk,
1961                                         ntohs(chunk->chunk_hdr->length));
1962
1963                 if (*errp) {
1964                         sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
1965                                         WORD_ROUND(ntohs(param.p->length)));
1966                         sctp_addto_chunk(*errp,
1967                                         WORD_ROUND(ntohs(param.p->length)),
1968                                         param.v);
1969                 } else {
1970                         /* If there is no memory for generating the ERROR
1971                          * report as specified, an ABORT will be triggered
1972                          * to the peer and the association won't be
1973                          * established.
1974                          */
1975                         retval = SCTP_IERROR_NOMEM;
1976                 }
1977                 break;
1978         default:
1979                 break;
1980         }
1981
1982         return retval;
1983 }
1984
1985 /* Verify variable length parameters
1986  * Return values:
1987  *      SCTP_IERROR_ABORT - trigger an ABORT
1988  *      SCTP_IERROR_NOMEM - out of memory (abort)
1989  *      SCTP_IERROR_ERROR - stop processing, trigger an ERROR
1990  *      SCTP_IERROR_NO_ERROR - continue with the chunk
1991  */
1992 static sctp_ierror_t sctp_verify_param(const struct sctp_association *asoc,
1993                                         union sctp_params param,
1994                                         sctp_cid_t cid,
1995                                         struct sctp_chunk *chunk,
1996                                         struct sctp_chunk **err_chunk)
1997 {
1998         struct sctp_hmac_algo_param *hmacs;
1999         int retval = SCTP_IERROR_NO_ERROR;
2000         __u16 n_elt, id = 0;
2001         int i;
2002
2003         /* FIXME - This routine is not looking at each parameter per the
2004          * chunk type, i.e., unrecognized parameters should be further
2005          * identified based on the chunk id.
2006          */
2007
2008         switch (param.p->type) {
2009         case SCTP_PARAM_IPV4_ADDRESS:
2010         case SCTP_PARAM_IPV6_ADDRESS:
2011         case SCTP_PARAM_COOKIE_PRESERVATIVE:
2012         case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2013         case SCTP_PARAM_STATE_COOKIE:
2014         case SCTP_PARAM_HEARTBEAT_INFO:
2015         case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2016         case SCTP_PARAM_ECN_CAPABLE:
2017         case SCTP_PARAM_ADAPTATION_LAYER_IND:
2018                 break;
2019
2020         case SCTP_PARAM_SUPPORTED_EXT:
2021                 if (!sctp_verify_ext_param(param))
2022                         return SCTP_IERROR_ABORT;
2023                 break;
2024
2025         case SCTP_PARAM_SET_PRIMARY:
2026                 if (sctp_addip_enable)
2027                         break;
2028                 goto fallthrough;
2029
2030         case SCTP_PARAM_HOST_NAME_ADDRESS:
2031                 /* Tell the peer, we won't support this param.  */
2032                 sctp_process_hn_param(asoc, param, chunk, err_chunk);
2033                 retval = SCTP_IERROR_ABORT;
2034                 break;
2035
2036         case SCTP_PARAM_FWD_TSN_SUPPORT:
2037                 if (sctp_prsctp_enable)
2038                         break;
2039                 goto fallthrough;
2040
2041         case SCTP_PARAM_RANDOM:
2042                 if (!sctp_auth_enable)
2043                         goto fallthrough;
2044
2045                 /* SCTP-AUTH: Secion 6.1
2046                  * If the random number is not 32 byte long the association
2047                  * MUST be aborted.  The ABORT chunk SHOULD contain the error
2048                  * cause 'Protocol Violation'.
2049                  */
2050                 if (SCTP_AUTH_RANDOM_LENGTH !=
2051                         ntohs(param.p->length) - sizeof(sctp_paramhdr_t)) {
2052                         sctp_process_inv_paramlength(asoc, param.p,
2053                                                         chunk, err_chunk);
2054                         retval = SCTP_IERROR_ABORT;
2055                 }
2056                 break;
2057
2058         case SCTP_PARAM_CHUNKS:
2059                 if (!sctp_auth_enable)
2060                         goto fallthrough;
2061
2062                 /* SCTP-AUTH: Section 3.2
2063                  * The CHUNKS parameter MUST be included once in the INIT or
2064                  *  INIT-ACK chunk if the sender wants to receive authenticated
2065                  *  chunks.  Its maximum length is 260 bytes.
2066                  */
2067                 if (260 < ntohs(param.p->length)) {
2068                         sctp_process_inv_paramlength(asoc, param.p,
2069                                                      chunk, err_chunk);
2070                         retval = SCTP_IERROR_ABORT;
2071                 }
2072                 break;
2073
2074         case SCTP_PARAM_HMAC_ALGO:
2075                 if (!sctp_auth_enable)
2076                         goto fallthrough;
2077
2078                 hmacs = (struct sctp_hmac_algo_param *)param.p;
2079                 n_elt = (ntohs(param.p->length) - sizeof(sctp_paramhdr_t)) >> 1;
2080
2081                 /* SCTP-AUTH: Section 6.1
2082                  * The HMAC algorithm based on SHA-1 MUST be supported and
2083                  * included in the HMAC-ALGO parameter.
2084                  */
2085                 for (i = 0; i < n_elt; i++) {
2086                         id = ntohs(hmacs->hmac_ids[i]);
2087
2088                         if (id == SCTP_AUTH_HMAC_ID_SHA1)
2089                                 break;
2090                 }
2091
2092                 if (id != SCTP_AUTH_HMAC_ID_SHA1) {
2093                         sctp_process_inv_paramlength(asoc, param.p, chunk,
2094                                                      err_chunk);
2095                         retval = SCTP_IERROR_ABORT;
2096                 }
2097                 break;
2098 fallthrough:
2099         default:
2100                 SCTP_DEBUG_PRINTK("Unrecognized param: %d for chunk %d.\n",
2101                                 ntohs(param.p->type), cid);
2102                 retval = sctp_process_unk_param(asoc, param, chunk, err_chunk);
2103                 break;
2104         }
2105         return retval;
2106 }
2107
2108 /* Verify the INIT packet before we process it.  */
2109 int sctp_verify_init(const struct sctp_association *asoc,
2110                      sctp_cid_t cid,
2111                      sctp_init_chunk_t *peer_init,
2112                      struct sctp_chunk *chunk,
2113                      struct sctp_chunk **errp)
2114 {
2115         union sctp_params param;
2116         int has_cookie = 0;
2117         int result;
2118
2119         /* Verify stream values are non-zero. */
2120         if ((0 == peer_init->init_hdr.num_outbound_streams) ||
2121             (0 == peer_init->init_hdr.num_inbound_streams) ||
2122             (0 == peer_init->init_hdr.init_tag) ||
2123             (SCTP_DEFAULT_MINWINDOW > ntohl(peer_init->init_hdr.a_rwnd))) {
2124
2125                 return sctp_process_inv_mandatory(asoc, chunk, errp);
2126         }
2127
2128         /* Check for missing mandatory parameters.  */
2129         sctp_walk_params(param, peer_init, init_hdr.params) {
2130
2131                 if (SCTP_PARAM_STATE_COOKIE == param.p->type)
2132                         has_cookie = 1;
2133
2134         } /* for (loop through all parameters) */
2135
2136         /* There is a possibility that a parameter length was bad and
2137          * in that case we would have stoped walking the parameters.
2138          * The current param.p would point at the bad one.
2139          * Current consensus on the mailing list is to generate a PROTOCOL
2140          * VIOLATION error.  We build the ERROR chunk here and let the normal
2141          * error handling code build and send the packet.
2142          */
2143         if (param.v != (void*)chunk->chunk_end)
2144                 return sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
2145
2146         /* The only missing mandatory param possible today is
2147          * the state cookie for an INIT-ACK chunk.
2148          */
2149         if ((SCTP_CID_INIT_ACK == cid) && !has_cookie)
2150                 return sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
2151                                                   chunk, errp);
2152
2153         /* Verify all the variable length parameters */
2154         sctp_walk_params(param, peer_init, init_hdr.params) {
2155
2156                 result = sctp_verify_param(asoc, param, cid, chunk, errp);
2157                 switch (result) {
2158                     case SCTP_IERROR_ABORT:
2159                     case SCTP_IERROR_NOMEM:
2160                                 return 0;
2161                     case SCTP_IERROR_ERROR:
2162                                 return 1;
2163                     case SCTP_IERROR_NO_ERROR:
2164                     default:
2165                                 break;
2166                 }
2167
2168         } /* for (loop through all parameters) */
2169
2170         return 1;
2171 }
2172
2173 /* Unpack the parameters in an INIT packet into an association.
2174  * Returns 0 on failure, else success.
2175  * FIXME:  This is an association method.
2176  */
2177 int sctp_process_init(struct sctp_association *asoc, sctp_cid_t cid,
2178                       const union sctp_addr *peer_addr,
2179                       sctp_init_chunk_t *peer_init, gfp_t gfp)
2180 {
2181         union sctp_params param;
2182         struct sctp_transport *transport;
2183         struct list_head *pos, *temp;
2184         char *cookie;
2185
2186         /* We must include the address that the INIT packet came from.
2187          * This is the only address that matters for an INIT packet.
2188          * When processing a COOKIE ECHO, we retrieve the from address
2189          * of the INIT from the cookie.
2190          */
2191
2192         /* This implementation defaults to making the first transport
2193          * added as the primary transport.  The source address seems to
2194          * be a a better choice than any of the embedded addresses.
2195          */
2196         if (peer_addr) {
2197                 if(!sctp_assoc_add_peer(asoc, peer_addr, gfp, SCTP_ACTIVE))
2198                         goto nomem;
2199         }
2200
2201         /* Process the initialization parameters.  */
2202         sctp_walk_params(param, peer_init, init_hdr.params) {
2203
2204                 if (!sctp_process_param(asoc, param, peer_addr, gfp))
2205                         goto clean_up;
2206         }
2207
2208         /* AUTH: After processing the parameters, make sure that we
2209          * have all the required info to potentially do authentications.
2210          */
2211         if (asoc->peer.auth_capable && (!asoc->peer.peer_random ||
2212                                         !asoc->peer.peer_hmacs))
2213                 asoc->peer.auth_capable = 0;
2214
2215         /* In a non-backward compatible mode, if the peer claims
2216          * support for ADD-IP but not AUTH,  the ADD-IP spec states
2217          * that we MUST ABORT the association. Section 6.  The section
2218          * also give us an option to silently ignore the packet, which
2219          * is what we'll do here.
2220          */
2221         if (!sctp_addip_noauth &&
2222              (asoc->peer.asconf_capable && !asoc->peer.auth_capable)) {
2223                 asoc->peer.addip_disabled_mask |= (SCTP_PARAM_ADD_IP |
2224                                                   SCTP_PARAM_DEL_IP |
2225                                                   SCTP_PARAM_SET_PRIMARY);
2226                 asoc->peer.asconf_capable = 0;
2227                 goto clean_up;
2228         }
2229
2230         /* Walk list of transports, removing transports in the UNKNOWN state. */
2231         list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2232                 transport = list_entry(pos, struct sctp_transport, transports);
2233                 if (transport->state == SCTP_UNKNOWN) {
2234                         sctp_assoc_rm_peer(asoc, transport);
2235                 }
2236         }
2237
2238         /* The fixed INIT headers are always in network byte
2239          * order.
2240          */
2241         asoc->peer.i.init_tag =
2242                 ntohl(peer_init->init_hdr.init_tag);
2243         asoc->peer.i.a_rwnd =
2244                 ntohl(peer_init->init_hdr.a_rwnd);
2245         asoc->peer.i.num_outbound_streams =
2246                 ntohs(peer_init->init_hdr.num_outbound_streams);
2247         asoc->peer.i.num_inbound_streams =
2248                 ntohs(peer_init->init_hdr.num_inbound_streams);
2249         asoc->peer.i.initial_tsn =
2250                 ntohl(peer_init->init_hdr.initial_tsn);
2251
2252         /* Apply the upper bounds for output streams based on peer's
2253          * number of inbound streams.
2254          */
2255         if (asoc->c.sinit_num_ostreams  >
2256             ntohs(peer_init->init_hdr.num_inbound_streams)) {
2257                 asoc->c.sinit_num_ostreams =
2258                         ntohs(peer_init->init_hdr.num_inbound_streams);
2259         }
2260
2261         if (asoc->c.sinit_max_instreams >
2262             ntohs(peer_init->init_hdr.num_outbound_streams)) {
2263                 asoc->c.sinit_max_instreams =
2264                         ntohs(peer_init->init_hdr.num_outbound_streams);
2265         }
2266
2267         /* Copy Initiation tag from INIT to VT_peer in cookie.   */
2268         asoc->c.peer_vtag = asoc->peer.i.init_tag;
2269
2270         /* Peer Rwnd   : Current calculated value of the peer's rwnd.  */
2271         asoc->peer.rwnd = asoc->peer.i.a_rwnd;
2272
2273         /* Copy cookie in case we need to resend COOKIE-ECHO. */
2274         cookie = asoc->peer.cookie;
2275         if (cookie) {
2276                 asoc->peer.cookie = kmemdup(cookie, asoc->peer.cookie_len, gfp);
2277                 if (!asoc->peer.cookie)
2278                         goto clean_up;
2279         }
2280
2281         /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
2282          * high (for example, implementations MAY use the size of the receiver
2283          * advertised window).
2284          */
2285         list_for_each_entry(transport, &asoc->peer.transport_addr_list,
2286                         transports) {
2287                 transport->ssthresh = asoc->peer.i.a_rwnd;
2288         }
2289
2290         /* Set up the TSN tracking pieces.  */
2291         sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_SIZE,
2292                          asoc->peer.i.initial_tsn);
2293
2294         /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
2295          *
2296          * The stream sequence number in all the streams shall start
2297          * from 0 when the association is established.  Also, when the
2298          * stream sequence number reaches the value 65535 the next
2299          * stream sequence number shall be set to 0.
2300          */
2301
2302         /* Allocate storage for the negotiated streams if it is not a temporary
2303          * association.
2304          */
2305         if (!asoc->temp) {
2306                 int error;
2307
2308                 asoc->ssnmap = sctp_ssnmap_new(asoc->c.sinit_max_instreams,
2309                                                asoc->c.sinit_num_ostreams, gfp);
2310                 if (!asoc->ssnmap)
2311                         goto clean_up;
2312
2313                 error = sctp_assoc_set_id(asoc, gfp);
2314                 if (error)
2315                         goto clean_up;
2316         }
2317
2318         /* ADDIP Section 4.1 ASCONF Chunk Procedures
2319          *
2320          * When an endpoint has an ASCONF signaled change to be sent to the
2321          * remote endpoint it should do the following:
2322          * ...
2323          * A2) A serial number should be assigned to the Chunk. The serial
2324          * number should be a monotonically increasing number. All serial
2325          * numbers are defined to be initialized at the start of the
2326          * association to the same value as the Initial TSN.
2327          */
2328         asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
2329         return 1;
2330
2331 clean_up:
2332         /* Release the transport structures. */
2333         list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2334                 transport = list_entry(pos, struct sctp_transport, transports);
2335                 if (transport->state != SCTP_ACTIVE)
2336                         sctp_assoc_rm_peer(asoc, transport);
2337         }
2338
2339 nomem:
2340         return 0;
2341 }
2342
2343
2344 /* Update asoc with the option described in param.
2345  *
2346  * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
2347  *
2348  * asoc is the association to update.
2349  * param is the variable length parameter to use for update.
2350  * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
2351  * If the current packet is an INIT we want to minimize the amount of
2352  * work we do.  In particular, we should not build transport
2353  * structures for the addresses.
2354  */
2355 static int sctp_process_param(struct sctp_association *asoc,
2356                               union sctp_params param,
2357                               const union sctp_addr *peer_addr,
2358                               gfp_t gfp)
2359 {
2360         union sctp_addr addr;
2361         int i;
2362         __u16 sat;
2363         int retval = 1;
2364         sctp_scope_t scope;
2365         time_t stale;
2366         struct sctp_af *af;
2367         union sctp_addr_param *addr_param;
2368         struct sctp_transport *t;
2369
2370         /* We maintain all INIT parameters in network byte order all the
2371          * time.  This allows us to not worry about whether the parameters
2372          * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
2373          */
2374         switch (param.p->type) {
2375         case SCTP_PARAM_IPV6_ADDRESS:
2376                 if (PF_INET6 != asoc->base.sk->sk_family)
2377                         break;
2378                 goto do_addr_param;
2379
2380         case SCTP_PARAM_IPV4_ADDRESS:
2381                 /* v4 addresses are not allowed on v6-only socket */
2382                 if (ipv6_only_sock(asoc->base.sk))
2383                         break;
2384 do_addr_param:
2385                 af = sctp_get_af_specific(param_type2af(param.p->type));
2386                 af->from_addr_param(&addr, param.addr, htons(asoc->peer.port), 0);
2387                 scope = sctp_scope(peer_addr);
2388                 if (sctp_in_scope(&addr, scope))
2389                         if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_UNCONFIRMED))
2390                                 return 0;
2391                 break;
2392
2393         case SCTP_PARAM_COOKIE_PRESERVATIVE:
2394                 if (!sctp_cookie_preserve_enable)
2395                         break;
2396
2397                 stale = ntohl(param.life->lifespan_increment);
2398
2399                 /* Suggested Cookie Life span increment's unit is msec,
2400                  * (1/1000sec).
2401                  */
2402                 asoc->cookie_life.tv_sec += stale / 1000;
2403                 asoc->cookie_life.tv_usec += (stale % 1000) * 1000;
2404                 break;
2405
2406         case SCTP_PARAM_HOST_NAME_ADDRESS:
2407                 SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
2408                 break;
2409
2410         case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2411                 /* Turn off the default values first so we'll know which
2412                  * ones are really set by the peer.
2413                  */
2414                 asoc->peer.ipv4_address = 0;
2415                 asoc->peer.ipv6_address = 0;
2416
2417                 /* Assume that peer supports the address family
2418                  * by which it sends a packet.
2419                  */
2420                 if (peer_addr->sa.sa_family == AF_INET6)
2421                         asoc->peer.ipv6_address = 1;
2422                 else if (peer_addr->sa.sa_family == AF_INET)
2423                         asoc->peer.ipv4_address = 1;
2424
2425                 /* Cycle through address types; avoid divide by 0. */
2426                 sat = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2427                 if (sat)
2428                         sat /= sizeof(__u16);
2429
2430                 for (i = 0; i < sat; ++i) {
2431                         switch (param.sat->types[i]) {
2432                         case SCTP_PARAM_IPV4_ADDRESS:
2433                                 asoc->peer.ipv4_address = 1;
2434                                 break;
2435
2436                         case SCTP_PARAM_IPV6_ADDRESS:
2437                                 if (PF_INET6 == asoc->base.sk->sk_family)
2438                                         asoc->peer.ipv6_address = 1;
2439                                 break;
2440
2441                         case SCTP_PARAM_HOST_NAME_ADDRESS:
2442                                 asoc->peer.hostname_address = 1;
2443                                 break;
2444
2445                         default: /* Just ignore anything else.  */
2446                                 break;
2447                         }
2448                 }
2449                 break;
2450
2451         case SCTP_PARAM_STATE_COOKIE:
2452                 asoc->peer.cookie_len =
2453                         ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2454                 asoc->peer.cookie = param.cookie->body;
2455                 break;
2456
2457         case SCTP_PARAM_HEARTBEAT_INFO:
2458                 /* Would be odd to receive, but it causes no problems. */
2459                 break;
2460
2461         case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2462                 /* Rejected during verify stage. */
2463                 break;
2464
2465         case SCTP_PARAM_ECN_CAPABLE:
2466                 asoc->peer.ecn_capable = 1;
2467                 break;
2468
2469         case SCTP_PARAM_ADAPTATION_LAYER_IND:
2470                 asoc->peer.adaptation_ind = param.aind->adaptation_ind;
2471                 break;
2472
2473         case SCTP_PARAM_SET_PRIMARY:
2474                 if (!sctp_addip_enable)
2475                         goto fall_through;
2476
2477                 addr_param = param.v + sizeof(sctp_addip_param_t);
2478
2479                 af = sctp_get_af_specific(param_type2af(param.p->type));
2480                 af->from_addr_param(&addr, addr_param,
2481                                     htons(asoc->peer.port), 0);
2482
2483                 /* if the address is invalid, we can't process it.
2484                  * XXX: see spec for what to do.
2485                  */
2486                 if (!af->addr_valid(&addr, NULL, NULL))
2487                         break;
2488
2489                 t = sctp_assoc_lookup_paddr(asoc, &addr);
2490                 if (!t)
2491                         break;
2492
2493                 sctp_assoc_set_primary(asoc, t);
2494                 break;
2495
2496         case SCTP_PARAM_SUPPORTED_EXT:
2497                 sctp_process_ext_param(asoc, param);
2498                 break;
2499
2500         case SCTP_PARAM_FWD_TSN_SUPPORT:
2501                 if (sctp_prsctp_enable) {
2502                         asoc->peer.prsctp_capable = 1;
2503                         break;
2504                 }
2505                 /* Fall Through */
2506                 goto fall_through;
2507
2508         case SCTP_PARAM_RANDOM:
2509                 if (!sctp_auth_enable)
2510                         goto fall_through;
2511
2512                 /* Save peer's random parameter */
2513                 asoc->peer.peer_random = kmemdup(param.p,
2514                                             ntohs(param.p->length), gfp);
2515                 if (!asoc->peer.peer_random) {
2516                         retval = 0;
2517                         break;
2518                 }
2519                 break;
2520
2521         case SCTP_PARAM_HMAC_ALGO:
2522                 if (!sctp_auth_enable)
2523                         goto fall_through;
2524
2525                 /* Save peer's HMAC list */
2526                 asoc->peer.peer_hmacs = kmemdup(param.p,
2527                                             ntohs(param.p->length), gfp);
2528                 if (!asoc->peer.peer_hmacs) {
2529                         retval = 0;
2530                         break;
2531                 }
2532
2533                 /* Set the default HMAC the peer requested*/
2534                 sctp_auth_asoc_set_default_hmac(asoc, param.hmac_algo);
2535                 break;
2536
2537         case SCTP_PARAM_CHUNKS:
2538                 if (!sctp_auth_enable)
2539                         goto fall_through;
2540
2541                 asoc->peer.peer_chunks = kmemdup(param.p,
2542                                             ntohs(param.p->length), gfp);
2543                 if (!asoc->peer.peer_chunks)
2544                         retval = 0;
2545                 break;
2546 fall_through:
2547         default:
2548                 /* Any unrecognized parameters should have been caught
2549                  * and handled by sctp_verify_param() which should be
2550                  * called prior to this routine.  Simply log the error
2551                  * here.
2552                  */
2553                 SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
2554                                   ntohs(param.p->type), asoc);
2555                 break;
2556         }
2557
2558         return retval;
2559 }
2560
2561 /* Select a new verification tag.  */
2562 __u32 sctp_generate_tag(const struct sctp_endpoint *ep)
2563 {
2564         /* I believe that this random number generator complies with RFC1750.
2565          * A tag of 0 is reserved for special cases (e.g. INIT).
2566          */
2567         __u32 x;
2568
2569         do {
2570                 get_random_bytes(&x, sizeof(__u32));
2571         } while (x == 0);
2572
2573         return x;
2574 }
2575
2576 /* Select an initial TSN to send during startup.  */
2577 __u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
2578 {
2579         __u32 retval;
2580
2581         get_random_bytes(&retval, sizeof(__u32));
2582         return retval;
2583 }
2584
2585 /*
2586  * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
2587  *      0                   1                   2                   3
2588  *      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
2589  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2590  *     | Type = 0xC1   |  Chunk Flags  |      Chunk Length             |
2591  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2592  *     |                       Serial Number                           |
2593  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2594  *     |                    Address Parameter                          |
2595  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2596  *     |                     ASCONF Parameter #1                       |
2597  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2598  *     \                                                               \
2599  *     /                             ....                              /
2600  *     \                                                               \
2601  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2602  *     |                     ASCONF Parameter #N                       |
2603  *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2604  *
2605  * Address Parameter and other parameter will not be wrapped in this function
2606  */
2607 static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
2608                                            union sctp_addr *addr,
2609                                            int vparam_len)
2610 {
2611         sctp_addiphdr_t asconf;
2612         struct sctp_chunk *retval;
2613         int length = sizeof(asconf) + vparam_len;
2614         union sctp_addr_param addrparam;
2615         int addrlen;
2616         struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2617
2618         addrlen = af->to_addr_param(addr, &addrparam);
2619         if (!addrlen)
2620                 return NULL;
2621         length += addrlen;
2622
2623         /* Create the chunk.  */
2624         retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF, 0, length);
2625         if (!retval)
2626                 return NULL;
2627
2628         asconf.serial = htonl(asoc->addip_serial++);
2629
2630         retval->subh.addip_hdr =
2631                 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2632         retval->param_hdr.v =
2633                 sctp_addto_chunk(retval, addrlen, &addrparam);
2634
2635         return retval;
2636 }
2637
2638 /* ADDIP
2639  * 3.2.1 Add IP Address
2640  *      0                   1                   2                   3
2641  *      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
2642  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2643  *     |        Type = 0xC001          |    Length = Variable          |
2644  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2645  *     |               ASCONF-Request Correlation ID                   |
2646  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2647  *     |                       Address Parameter                       |
2648  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2649  *
2650  * 3.2.2 Delete IP Address
2651  *      0                   1                   2                   3
2652  *      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
2653  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2654  *     |        Type = 0xC002          |    Length = Variable          |
2655  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2656  *     |               ASCONF-Request Correlation ID                   |
2657  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2658  *     |                       Address Parameter                       |
2659  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2660  *
2661  */
2662 struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
2663                                               union sctp_addr         *laddr,
2664                                               struct sockaddr         *addrs,
2665                                               int                     addrcnt,
2666                                               __be16                  flags)
2667 {
2668         sctp_addip_param_t      param;
2669         struct sctp_chunk       *retval;
2670         union sctp_addr_param   addr_param;
2671         union sctp_addr         *addr;
2672         void                    *addr_buf;
2673         struct sctp_af          *af;
2674         int                     paramlen = sizeof(param);
2675         int                     addr_param_len = 0;
2676         int                     totallen = 0;
2677         int                     i;
2678
2679         /* Get total length of all the address parameters. */
2680         addr_buf = addrs;
2681         for (i = 0; i < addrcnt; i++) {
2682                 addr = (union sctp_addr *)addr_buf;
2683                 af = sctp_get_af_specific(addr->v4.sin_family);
2684                 addr_param_len = af->to_addr_param(addr, &addr_param);
2685
2686                 totallen += paramlen;
2687                 totallen += addr_param_len;
2688
2689                 addr_buf += af->sockaddr_len;
2690         }
2691
2692         /* Create an asconf chunk with the required length. */
2693         retval = sctp_make_asconf(asoc, laddr, totallen);
2694         if (!retval)
2695                 return NULL;
2696
2697         /* Add the address parameters to the asconf chunk. */
2698         addr_buf = addrs;
2699         for (i = 0; i < addrcnt; i++) {
2700                 addr = (union sctp_addr *)addr_buf;
2701                 af = sctp_get_af_specific(addr->v4.sin_family);
2702                 addr_param_len = af->to_addr_param(addr, &addr_param);
2703                 param.param_hdr.type = flags;
2704                 param.param_hdr.length = htons(paramlen + addr_param_len);
2705                 param.crr_id = i;
2706
2707                 sctp_addto_chunk(retval, paramlen, &param);
2708                 sctp_addto_chunk(retval, addr_param_len, &addr_param);
2709
2710                 addr_buf += af->sockaddr_len;
2711         }
2712         return retval;
2713 }
2714
2715 /* ADDIP
2716  * 3.2.4 Set Primary IP Address
2717  *      0                   1                   2                   3
2718  *      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
2719  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2720  *     |        Type =0xC004           |    Length = Variable          |
2721  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2722  *     |               ASCONF-Request Correlation ID                   |
2723  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2724  *     |                       Address Parameter                       |
2725  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2726  *
2727  * Create an ASCONF chunk with Set Primary IP address parameter.
2728  */
2729 struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
2730                                              union sctp_addr *addr)
2731 {
2732         sctp_addip_param_t      param;
2733         struct sctp_chunk       *retval;
2734         int                     len = sizeof(param);
2735         union sctp_addr_param   addrparam;
2736         int                     addrlen;
2737         struct sctp_af          *af = sctp_get_af_specific(addr->v4.sin_family);
2738
2739         addrlen = af->to_addr_param(addr, &addrparam);
2740         if (!addrlen)
2741                 return NULL;
2742         len += addrlen;
2743
2744         /* Create the chunk and make asconf header. */
2745         retval = sctp_make_asconf(asoc, addr, len);
2746         if (!retval)
2747                 return NULL;
2748
2749         param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
2750         param.param_hdr.length = htons(len);
2751         param.crr_id = 0;
2752
2753         sctp_addto_chunk(retval, sizeof(param), &param);
2754         sctp_addto_chunk(retval, addrlen, &addrparam);
2755
2756         return retval;
2757 }
2758
2759 /* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2760  *      0                   1                   2                   3
2761  *      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
2762  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2763  *     | Type = 0x80   |  Chunk Flags  |      Chunk Length             |
2764  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2765  *     |                       Serial Number                           |
2766  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2767  *     |                 ASCONF Parameter Response#1                   |
2768  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2769  *     \                                                               \
2770  *     /                             ....                              /
2771  *     \                                                               \
2772  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2773  *     |                 ASCONF Parameter Response#N                   |
2774  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2775  *
2776  * Create an ASCONF_ACK chunk with enough space for the parameter responses.
2777  */
2778 static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
2779                                                __u32 serial, int vparam_len)
2780 {
2781         sctp_addiphdr_t         asconf;
2782         struct sctp_chunk       *retval;
2783         int                     length = sizeof(asconf) + vparam_len;
2784
2785         /* Create the chunk.  */
2786         retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF_ACK, 0, length);
2787         if (!retval)
2788                 return NULL;
2789
2790         asconf.serial = htonl(serial);
2791
2792         retval->subh.addip_hdr =
2793                 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2794
2795         return retval;
2796 }
2797
2798 /* Add response parameters to an ASCONF_ACK chunk. */
2799 static void sctp_add_asconf_response(struct sctp_chunk *chunk, __be32 crr_id,
2800                               __be16 err_code, sctp_addip_param_t *asconf_param)
2801 {
2802         sctp_addip_param_t      ack_param;
2803         sctp_errhdr_t           err_param;
2804         int                     asconf_param_len = 0;
2805         int                     err_param_len = 0;
2806         __be16                  response_type;
2807
2808         if (SCTP_ERROR_NO_ERROR == err_code) {
2809                 response_type = SCTP_PARAM_SUCCESS_REPORT;
2810         } else {
2811                 response_type = SCTP_PARAM_ERR_CAUSE;
2812                 err_param_len = sizeof(err_param);
2813                 if (asconf_param)
2814                         asconf_param_len =
2815                                  ntohs(asconf_param->param_hdr.length);
2816         }
2817
2818         /* Add Success Indication or Error Cause Indication parameter. */
2819         ack_param.param_hdr.type = response_type;
2820         ack_param.param_hdr.length = htons(sizeof(ack_param) +
2821                                            err_param_len +
2822                                            asconf_param_len);
2823         ack_param.crr_id = crr_id;
2824         sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
2825
2826         if (SCTP_ERROR_NO_ERROR == err_code)
2827                 return;
2828
2829         /* Add Error Cause parameter. */
2830         err_param.cause = err_code;
2831         err_param.length = htons(err_param_len + asconf_param_len);
2832         sctp_addto_chunk(chunk, err_param_len, &err_param);
2833
2834         /* Add the failed TLV copied from ASCONF chunk. */
2835         if (asconf_param)
2836                 sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
2837 }
2838
2839 /* Process a asconf parameter. */
2840 static __be16 sctp_process_asconf_param(struct sctp_association *asoc,
2841                                        struct sctp_chunk *asconf,
2842                                        sctp_addip_param_t *asconf_param)
2843 {
2844         struct sctp_transport *peer;
2845         struct sctp_af *af;
2846         union sctp_addr addr;
2847         union sctp_addr_param *addr_param;
2848
2849         addr_param = (union sctp_addr_param *)
2850                         ((void *)asconf_param + sizeof(sctp_addip_param_t));
2851
2852         switch (addr_param->v4.param_hdr.type) {
2853         case SCTP_PARAM_IPV6_ADDRESS:
2854                 if (!asoc->peer.ipv6_address)
2855                         return SCTP_ERROR_INV_PARAM;
2856                 break;
2857         case SCTP_PARAM_IPV4_ADDRESS:
2858                 if (!asoc->peer.ipv4_address)
2859                         return SCTP_ERROR_INV_PARAM;
2860                 break;
2861         default:
2862                 return SCTP_ERROR_INV_PARAM;
2863         }
2864
2865         af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2866         if (unlikely(!af))
2867                 return SCTP_ERROR_INV_PARAM;
2868
2869         af->from_addr_param(&addr, addr_param, htons(asoc->peer.port), 0);
2870
2871         /* ADDIP 4.2.1  This parameter MUST NOT contain a broadcast
2872          * or multicast address.
2873          * (note: wildcard is permitted and requires special handling so
2874          *  make sure we check for that)
2875          */
2876         if (!af->is_any(&addr) && !af->addr_valid(&addr, NULL, asconf->skb))
2877                 return SCTP_ERROR_INV_PARAM;
2878
2879         switch (asconf_param->param_hdr.type) {
2880         case SCTP_PARAM_ADD_IP:
2881                 /* Section 4.2.1:
2882                  * If the address 0.0.0.0 or ::0 is provided, the source
2883                  * address of the packet MUST be added.
2884                  */
2885                 if (af->is_any(&addr))
2886                         memcpy(&addr, &asconf->source, sizeof(addr));
2887
2888                 /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
2889                  * request and does not have the local resources to add this
2890                  * new address to the association, it MUST return an Error
2891                  * Cause TLV set to the new error code 'Operation Refused
2892                  * Due to Resource Shortage'.
2893                  */
2894
2895                 peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC, SCTP_UNCONFIRMED);
2896                 if (!peer)
2897                         return SCTP_ERROR_RSRC_LOW;
2898
2899                 /* Start the heartbeat timer. */
2900                 if (!mod_timer(&peer->hb_timer, sctp_transport_timeout(peer)))
2901                         sctp_transport_hold(peer);
2902                 break;
2903         case SCTP_PARAM_DEL_IP:
2904                 /* ADDIP 4.3 D7) If a request is received to delete the
2905                  * last remaining IP address of a peer endpoint, the receiver
2906                  * MUST send an Error Cause TLV with the error cause set to the
2907                  * new error code 'Request to Delete Last Remaining IP Address'.
2908                  */
2909                 if (asoc->peer.transport_count == 1)
2910                         return SCTP_ERROR_DEL_LAST_IP;
2911
2912                 /* ADDIP 4.3 D8) If a request is received to delete an IP
2913                  * address which is also the source address of the IP packet
2914                  * which contained the ASCONF chunk, the receiver MUST reject
2915                  * this request. To reject the request the receiver MUST send
2916                  * an Error Cause TLV set to the new error code 'Request to
2917                  * Delete Source IP Address'
2918                  */
2919                 if (sctp_cmp_addr_exact(sctp_source(asconf), &addr))
2920                         return SCTP_ERROR_DEL_SRC_IP;
2921
2922                 /* Section 4.2.2
2923                  * If the address 0.0.0.0 or ::0 is provided, all
2924                  * addresses of the peer except the source address of the
2925                  * packet MUST be deleted.
2926                  */
2927                 if (af->is_any(&addr)) {
2928                         sctp_assoc_set_primary(asoc, asconf->transport);
2929                         sctp_assoc_del_nonprimary_peers(asoc,
2930                                                         asconf->transport);
2931                 } else
2932                         sctp_assoc_del_peer(asoc, &addr);
2933                 break;
2934         case SCTP_PARAM_SET_PRIMARY:
2935                 /* ADDIP Section 4.2.4
2936                  * If the address 0.0.0.0 or ::0 is provided, the receiver
2937                  * MAY mark the source address of the packet as its
2938                  * primary.
2939                  */
2940                 if (af->is_any(&addr))
2941                         memcpy(&addr.v4, sctp_source(asconf), sizeof(addr));
2942
2943                 peer = sctp_assoc_lookup_paddr(asoc, &addr);
2944                 if (!peer)
2945                         return SCTP_ERROR_INV_PARAM;
2946
2947                 sctp_assoc_set_primary(asoc, peer);
2948                 break;
2949         default:
2950                 return SCTP_ERROR_INV_PARAM;
2951                 break;
2952         }
2953
2954         return SCTP_ERROR_NO_ERROR;
2955 }
2956
2957 /* Verify the ASCONF packet before we process it.  */
2958 int sctp_verify_asconf(const struct sctp_association *asoc,
2959                        struct sctp_paramhdr *param_hdr, void *chunk_end,
2960                        struct sctp_paramhdr **errp) {
2961         sctp_addip_param_t *asconf_param;
2962         union sctp_params param;
2963         int length, plen;
2964
2965         param.v = (sctp_paramhdr_t *) param_hdr;
2966         while (param.v <= chunk_end - sizeof(sctp_paramhdr_t)) {
2967                 length = ntohs(param.p->length);
2968                 *errp = param.p;
2969
2970                 if (param.v > chunk_end - length ||
2971                     length < sizeof(sctp_paramhdr_t))
2972                         return 0;
2973
2974                 switch (param.p->type) {
2975                 case SCTP_PARAM_ADD_IP:
2976                 case SCTP_PARAM_DEL_IP:
2977                 case SCTP_PARAM_SET_PRIMARY:
2978                         asconf_param = (sctp_addip_param_t *)param.v;
2979                         plen = ntohs(asconf_param->param_hdr.length);
2980                         if (plen < sizeof(sctp_addip_param_t) +
2981                             sizeof(sctp_paramhdr_t))
2982                                 return 0;
2983                         break;
2984                 case SCTP_PARAM_SUCCESS_REPORT:
2985                 case SCTP_PARAM_ADAPTATION_LAYER_IND:
2986                         if (length != sizeof(sctp_addip_param_t))
2987                                 return 0;
2988
2989                         break;
2990                 default:
2991                         break;
2992                 }
2993
2994                 param.v += WORD_ROUND(length);
2995         }
2996
2997         if (param.v != chunk_end)
2998                 return 0;
2999
3000         return 1;
3001 }
3002
3003 /* Process an incoming ASCONF chunk with the next expected serial no. and
3004  * return an ASCONF_ACK chunk to be sent in response.
3005  */
3006 struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
3007                                        struct sctp_chunk *asconf)
3008 {
3009         sctp_addiphdr_t         *hdr;
3010         union sctp_addr_param   *addr_param;
3011         sctp_addip_param_t      *asconf_param;
3012         struct sctp_chunk       *asconf_ack;
3013
3014         __be16  err_code;
3015         int     length = 0;
3016         int     chunk_len;
3017         __u32   serial;
3018         int     all_param_pass = 1;
3019
3020         chunk_len = ntohs(asconf->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
3021         hdr = (sctp_addiphdr_t *)asconf->skb->data;
3022         serial = ntohl(hdr->serial);
3023
3024         /* Skip the addiphdr and store a pointer to address parameter.  */
3025         length = sizeof(sctp_addiphdr_t);
3026         addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
3027         chunk_len -= length;
3028
3029         /* Skip the address parameter and store a pointer to the first
3030          * asconf parameter.
3031          */
3032         length = ntohs(addr_param->v4.param_hdr.length);
3033         asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
3034         chunk_len -= length;
3035
3036         /* create an ASCONF_ACK chunk.
3037          * Based on the definitions of parameters, we know that the size of
3038          * ASCONF_ACK parameters are less than or equal to the twice of ASCONF
3039          * parameters.
3040          */
3041         asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 2);
3042         if (!asconf_ack)
3043                 goto done;
3044
3045         /* Process the TLVs contained within the ASCONF chunk. */
3046         while (chunk_len > 0) {
3047                 err_code = sctp_process_asconf_param(asoc, asconf,
3048                                                      asconf_param);
3049                 /* ADDIP 4.1 A7)
3050                  * If an error response is received for a TLV parameter,
3051                  * all TLVs with no response before the failed TLV are
3052                  * considered successful if not reported.  All TLVs after
3053                  * the failed response are considered unsuccessful unless
3054                  * a specific success indication is present for the parameter.
3055                  */
3056                 if (SCTP_ERROR_NO_ERROR != err_code)
3057                         all_param_pass = 0;
3058
3059                 if (!all_param_pass)
3060                         sctp_add_asconf_response(asconf_ack,
3061                                                  asconf_param->crr_id, err_code,
3062                                                  asconf_param);
3063
3064                 /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
3065                  * an IP address sends an 'Out of Resource' in its response, it
3066                  * MUST also fail any subsequent add or delete requests bundled
3067                  * in the ASCONF.
3068                  */
3069                 if (SCTP_ERROR_RSRC_LOW == err_code)
3070                         goto done;
3071
3072                 /* Move to the next ASCONF param. */
3073                 length = ntohs(asconf_param->param_hdr.length);
3074                 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
3075                                                       length);
3076                 chunk_len -= length;
3077         }
3078
3079 done:
3080         asoc->peer.addip_serial++;
3081
3082         /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
3083          * after freeing the reference to old asconf ack if any.
3084          */
3085         if (asconf_ack) {
3086                 sctp_chunk_hold(asconf_ack);
3087                 list_add_tail(&asconf_ack->transmitted_list,
3088                               &asoc->asconf_ack_list);
3089         }
3090
3091         return asconf_ack;
3092 }
3093
3094 /* Process a asconf parameter that is successfully acked. */
3095 static int sctp_asconf_param_success(struct sctp_association *asoc,
3096                                      sctp_addip_param_t *asconf_param)
3097 {
3098         struct sctp_af *af;
3099         union sctp_addr addr;
3100         struct sctp_bind_addr *bp = &asoc->base.bind_addr;
3101         union sctp_addr_param *addr_param;
3102         struct sctp_transport *transport;
3103         struct sctp_sockaddr_entry *saddr;
3104         int retval = 0;
3105
3106         addr_param = (union sctp_addr_param *)
3107                         ((void *)asconf_param + sizeof(sctp_addip_param_t));
3108
3109         /* We have checked the packet before, so we do not check again. */
3110         af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
3111         af->from_addr_param(&addr, addr_param, htons(bp->port), 0);
3112
3113         switch (asconf_param->param_hdr.type) {
3114         case SCTP_PARAM_ADD_IP:
3115                 /* This is always done in BH context with a socket lock
3116                  * held, so the list can not change.
3117                  */
3118                 local_bh_disable();
3119                 list_for_each_entry(saddr, &bp->address_list, list) {
3120                         if (sctp_cmp_addr_exact(&saddr->a, &addr))
3121                                 saddr->state = SCTP_ADDR_SRC;
3122                 }
3123                 local_bh_enable();
3124                 break;
3125         case SCTP_PARAM_DEL_IP:
3126                 local_bh_disable();
3127                 retval = sctp_del_bind_addr(bp, &addr);
3128                 local_bh_enable();
3129                 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
3130                                 transports) {
3131                         dst_release(transport->dst);
3132                         sctp_transport_route(transport, NULL,
3133                                              sctp_sk(asoc->base.sk));
3134                 }
3135                 break;
3136         default:
3137                 break;
3138         }
3139
3140         return retval;
3141 }
3142
3143 /* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
3144  * for the given asconf parameter.  If there is no response for this parameter,
3145  * return the error code based on the third argument 'no_err'.
3146  * ADDIP 4.1
3147  * A7) If an error response is received for a TLV parameter, all TLVs with no
3148  * response before the failed TLV are considered successful if not reported.
3149  * All TLVs after the failed response are considered unsuccessful unless a
3150  * specific success indication is present for the parameter.
3151  */
3152 static __be16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
3153                                       sctp_addip_param_t *asconf_param,
3154                                       int no_err)
3155 {
3156         sctp_addip_param_t      *asconf_ack_param;
3157         sctp_errhdr_t           *err_param;
3158         int                     length;
3159         int                     asconf_ack_len;
3160         __be16                  err_code;
3161
3162         if (no_err)
3163                 err_code = SCTP_ERROR_NO_ERROR;
3164         else
3165                 err_code = SCTP_ERROR_REQ_REFUSED;
3166
3167         asconf_ack_len = ntohs(asconf_ack->chunk_hdr->length) -
3168                              sizeof(sctp_chunkhdr_t);
3169
3170         /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
3171          * the first asconf_ack parameter.
3172          */
3173         length = sizeof(sctp_addiphdr_t);
3174         asconf_ack_param = (sctp_addip_param_t *)(asconf_ack->skb->data +
3175                                                   length);
3176         asconf_ack_len -= length;
3177
3178         while (asconf_ack_len > 0) {
3179                 if (asconf_ack_param->crr_id == asconf_param->crr_id) {
3180                         switch(asconf_ack_param->param_hdr.type) {
3181                         case SCTP_PARAM_SUCCESS_REPORT:
3182                                 return SCTP_ERROR_NO_ERROR;
3183                         case SCTP_PARAM_ERR_CAUSE:
3184                                 length = sizeof(sctp_addip_param_t);
3185                                 err_param = (sctp_errhdr_t *)
3186                                            ((void *)asconf_ack_param + length);
3187                                 asconf_ack_len -= length;
3188                                 if (asconf_ack_len > 0)
3189                                         return err_param->cause;
3190                                 else
3191                                         return SCTP_ERROR_INV_PARAM;
3192                                 break;
3193                         default:
3194                                 return SCTP_ERROR_INV_PARAM;
3195                         }
3196                 }
3197
3198                 length = ntohs(asconf_ack_param->param_hdr.length);
3199                 asconf_ack_param = (sctp_addip_param_t *)
3200                                         ((void *)asconf_ack_param + length);
3201                 asconf_ack_len -= length;
3202         }
3203
3204         return err_code;
3205 }
3206
3207 /* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
3208 int sctp_process_asconf_ack(struct sctp_association *asoc,
3209                             struct sctp_chunk *asconf_ack)
3210 {
3211         struct sctp_chunk       *asconf = asoc->addip_last_asconf;
3212         union sctp_addr_param   *addr_param;
3213         sctp_addip_param_t      *asconf_param;
3214         int     length = 0;
3215         int     asconf_len = asconf->skb->len;
3216         int     all_param_pass = 0;
3217         int     no_err = 1;
3218         int     retval = 0;
3219         __be16  err_code = SCTP_ERROR_NO_ERROR;
3220
3221         /* Skip the chunkhdr and addiphdr from the last asconf sent and store
3222          * a pointer to address parameter.
3223          */
3224         length = sizeof(sctp_addip_chunk_t);
3225         addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
3226         asconf_len -= length;
3227
3228         /* Skip the address parameter in the last asconf sent and store a
3229          * pointer to the first asconf parameter.
3230          */
3231         length = ntohs(addr_param->v4.param_hdr.length);
3232         asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
3233         asconf_len -= length;
3234
3235         /* ADDIP 4.1
3236          * A8) If there is no response(s) to specific TLV parameter(s), and no
3237          * failures are indicated, then all request(s) are considered
3238          * successful.
3239          */
3240         if (asconf_ack->skb->len == sizeof(sctp_addiphdr_t))
3241                 all_param_pass = 1;
3242
3243         /* Process the TLVs contained in the last sent ASCONF chunk. */
3244         while (asconf_len > 0) {
3245                 if (all_param_pass)
3246                         err_code = SCTP_ERROR_NO_ERROR;
3247                 else {
3248                         err_code = sctp_get_asconf_response(asconf_ack,
3249                                                             asconf_param,
3250                                                             no_err);
3251                         if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
3252                                 no_err = 0;
3253                 }
3254
3255                 switch (err_code) {
3256                 case SCTP_ERROR_NO_ERROR:
3257                         retval = sctp_asconf_param_success(asoc, asconf_param);
3258                         break;
3259
3260                 case SCTP_ERROR_RSRC_LOW:
3261                         retval = 1;
3262                         break;
3263
3264                 case SCTP_ERROR_INV_PARAM:
3265                         /* Disable sending this type of asconf parameter in
3266                          * future.
3267                          */
3268                         asoc->peer.addip_disabled_mask |=
3269                                 asconf_param->param_hdr.type;
3270                         break;
3271
3272                 case SCTP_ERROR_REQ_REFUSED:
3273                 case SCTP_ERROR_DEL_LAST_IP:
3274                 case SCTP_ERROR_DEL_SRC_IP:
3275                 default:
3276                          break;
3277                 }
3278
3279                 /* Skip the processed asconf parameter and move to the next
3280                  * one.
3281                  */
3282                 length = ntohs(asconf_param->param_hdr.length);
3283                 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
3284                                                       length);
3285                 asconf_len -= length;
3286         }
3287
3288         /* Free the cached last sent asconf chunk. */
3289         list_del_init(&asconf->transmitted_list);
3290         sctp_chunk_free(asconf);
3291         asoc->addip_last_asconf = NULL;
3292
3293         /* Send the next asconf chunk from the addip chunk queue. */
3294         if (!list_empty(&asoc->addip_chunk_list)) {
3295                 struct list_head *entry = asoc->addip_chunk_list.next;
3296                 asconf = list_entry(entry, struct sctp_chunk, list);
3297
3298                 list_del_init(entry);
3299
3300                 /* Hold the chunk until an ASCONF_ACK is received. */
3301                 sctp_chunk_hold(asconf);
3302                 if (sctp_primitive_ASCONF(asoc, asconf))
3303                         sctp_chunk_free(asconf);
3304                 else
3305                         asoc->addip_last_asconf = asconf;
3306         }
3307
3308         return retval;
3309 }
3310
3311 /* Make a FWD TSN chunk. */
3312 struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
3313                                     __u32 new_cum_tsn, size_t nstreams,
3314                                     struct sctp_fwdtsn_skip *skiplist)
3315 {
3316         struct sctp_chunk *retval = NULL;
3317         struct sctp_fwdtsn_chunk *ftsn_chunk;
3318         struct sctp_fwdtsn_hdr ftsn_hdr;
3319         struct sctp_fwdtsn_skip skip;
3320         size_t hint;
3321         int i;
3322
3323         hint = (nstreams + 1) * sizeof(__u32);
3324
3325         retval = sctp_make_chunk(asoc, SCTP_CID_FWD_TSN, 0, hint);
3326
3327         if (!retval)
3328                 return NULL;
3329
3330         ftsn_chunk = (struct sctp_fwdtsn_chunk *)retval->subh.fwdtsn_hdr;
3331
3332         ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
3333         retval->subh.fwdtsn_hdr =
3334                 sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
3335
3336         for (i = 0; i < nstreams; i++) {
3337                 skip.stream = skiplist[i].stream;
3338                 skip.ssn = skiplist[i].ssn;
3339                 sctp_addto_chunk(retval, sizeof(skip), &skip);
3340         }
3341
3342         return retval;
3343 }