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