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