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