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