sctp: move global declaration to header file.
[linux-2.6] / 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                 __u16 len;
1456                 /*
1457                  * Section 3.3.10.3 Stale Cookie Error (3)
1458                  *
1459                  * Cause of error
1460                  * ---------------
1461                  * Stale Cookie Error:  Indicates the receipt of a valid State
1462                  * Cookie that has expired.
1463                  */
1464                 len = ntohs(chunk->chunk_hdr->length);
1465                 *errp = sctp_make_op_error_space(asoc, chunk, len);
1466                 if (*errp) {
1467                         suseconds_t usecs = (tv.tv_sec -
1468                                 bear_cookie->expiration.tv_sec) * 1000000L +
1469                                 tv.tv_usec - bear_cookie->expiration.tv_usec;
1470                         __be32 n = htonl(usecs);
1471
1472                         sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
1473                                         &n, sizeof(n));
1474                         *error = -SCTP_IERROR_STALE_COOKIE;
1475                 } else
1476                         *error = -SCTP_IERROR_NOMEM;
1477
1478                 goto fail;
1479         }
1480
1481         /* Make a new base association.  */
1482         scope = sctp_scope(sctp_source(chunk));
1483         retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
1484         if (!retval) {
1485                 *error = -SCTP_IERROR_NOMEM;
1486                 goto fail;
1487         }
1488
1489         /* Set up our peer's port number.  */
1490         retval->peer.port = ntohs(chunk->sctp_hdr->source);
1491
1492         /* Populate the association from the cookie.  */
1493         memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
1494
1495         if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
1496                                                  GFP_ATOMIC) < 0) {
1497                 *error = -SCTP_IERROR_NOMEM;
1498                 goto fail;
1499         }
1500
1501         /* Also, add the destination address. */
1502         if (list_empty(&retval->base.bind_addr.address_list)) {
1503                 sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest, 1,
1504                                    GFP_ATOMIC);
1505         }
1506
1507         retval->next_tsn = retval->c.initial_tsn;
1508         retval->ctsn_ack_point = retval->next_tsn - 1;
1509         retval->addip_serial = retval->c.initial_tsn;
1510         retval->adv_peer_ack_point = retval->ctsn_ack_point;
1511         retval->peer.prsctp_capable = retval->c.prsctp_capable;
1512         retval->peer.adaptation_ind = retval->c.adaptation_ind;
1513
1514         /* The INIT stuff will be done by the side effects.  */
1515         return retval;
1516
1517 fail:
1518         if (retval)
1519                 sctp_association_free(retval);
1520
1521         return NULL;
1522
1523 malformed:
1524         /* Yikes!  The packet is either corrupt or deliberately
1525          * malformed.
1526          */
1527         *error = -SCTP_IERROR_MALFORMED;
1528         goto fail;
1529 }
1530
1531 /********************************************************************
1532  * 3rd Level Abstractions
1533  ********************************************************************/
1534
1535 struct __sctp_missing {
1536         __be32 num_missing;
1537         __be16 type;
1538 }  __attribute__((packed));
1539
1540 /*
1541  * Report a missing mandatory parameter.
1542  */
1543 static int sctp_process_missing_param(const struct sctp_association *asoc,
1544                                       sctp_param_t paramtype,
1545                                       struct sctp_chunk *chunk,
1546                                       struct sctp_chunk **errp)
1547 {
1548         struct __sctp_missing report;
1549         __u16 len;
1550
1551         len = WORD_ROUND(sizeof(report));
1552
1553         /* Make an ERROR chunk, preparing enough room for
1554          * returning multiple unknown parameters.
1555          */
1556         if (!*errp)
1557                 *errp = sctp_make_op_error_space(asoc, chunk, len);
1558
1559         if (*errp) {
1560                 report.num_missing = htonl(1);
1561                 report.type = paramtype;
1562                 sctp_init_cause(*errp, SCTP_ERROR_MISS_PARAM,
1563                                 &report, sizeof(report));
1564         }
1565
1566         /* Stop processing this chunk. */
1567         return 0;
1568 }
1569
1570 /* Report an Invalid Mandatory Parameter.  */
1571 static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
1572                                       struct sctp_chunk *chunk,
1573                                       struct sctp_chunk **errp)
1574 {
1575         /* Invalid Mandatory Parameter Error has no payload. */
1576
1577         if (!*errp)
1578                 *errp = sctp_make_op_error_space(asoc, chunk, 0);
1579
1580         if (*errp)
1581                 sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, NULL, 0);
1582
1583         /* Stop processing this chunk. */
1584         return 0;
1585 }
1586
1587 static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
1588                                         struct sctp_paramhdr *param,
1589                                         const struct sctp_chunk *chunk,
1590                                         struct sctp_chunk **errp)
1591 {
1592         char            error[] = "The following parameter had invalid length:";
1593         size_t          payload_len = WORD_ROUND(sizeof(error)) +
1594                                                 sizeof(sctp_paramhdr_t);
1595
1596
1597         /* Create an error chunk and fill it in with our payload. */
1598         if (!*errp)
1599                 *errp = sctp_make_op_error_space(asoc, chunk, payload_len);
1600
1601         if (*errp) {
1602                 sctp_init_cause(*errp, SCTP_ERROR_PROTO_VIOLATION, error,
1603                                 sizeof(error));
1604                 sctp_addto_chunk(*errp, sizeof(sctp_paramhdr_t), param);
1605         }
1606
1607         return 0;
1608 }
1609
1610
1611 /* Do not attempt to handle the HOST_NAME parm.  However, do
1612  * send back an indicator to the peer.
1613  */
1614 static int sctp_process_hn_param(const struct sctp_association *asoc,
1615                                  union sctp_params param,
1616                                  struct sctp_chunk *chunk,
1617                                  struct sctp_chunk **errp)
1618 {
1619         __u16 len = ntohs(param.p->length);
1620
1621         /* Make an ERROR chunk. */
1622         if (!*errp)
1623                 *errp = sctp_make_op_error_space(asoc, chunk, len);
1624
1625         if (*errp)
1626                 sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED,
1627                                 param.v, len);
1628
1629         /* Stop processing this chunk. */
1630         return 0;
1631 }
1632
1633 /* RFC 3.2.1 & the Implementers Guide 2.2.
1634  *
1635  * The Parameter Types are encoded such that the
1636  * highest-order two bits specify the action that must be
1637  * taken if the processing endpoint does not recognize the
1638  * Parameter Type.
1639  *
1640  * 00 - Stop processing this SCTP chunk and discard it,
1641  *      do not process any further chunks within it.
1642  *
1643  * 01 - Stop processing this SCTP chunk and discard it,
1644  *      do not process any further chunks within it, and report
1645  *      the unrecognized parameter in an 'Unrecognized
1646  *      Parameter Type' (in either an ERROR or in the INIT ACK).
1647  *
1648  * 10 - Skip this parameter and continue processing.
1649  *
1650  * 11 - Skip this parameter and continue processing but
1651  *      report the unrecognized parameter in an
1652  *      'Unrecognized Parameter Type' (in either an ERROR or in
1653  *      the INIT ACK).
1654  *
1655  * Return value:
1656  *      0 - discard the chunk
1657  *      1 - continue with the chunk
1658  */
1659 static int sctp_process_unk_param(const struct sctp_association *asoc,
1660                                   union sctp_params param,
1661                                   struct sctp_chunk *chunk,
1662                                   struct sctp_chunk **errp)
1663 {
1664         int retval = 1;
1665
1666         switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
1667         case SCTP_PARAM_ACTION_DISCARD:
1668                 retval =  0;
1669                 break;
1670         case SCTP_PARAM_ACTION_DISCARD_ERR:
1671                 retval =  0;
1672                 /* Make an ERROR chunk, preparing enough room for
1673                  * returning multiple unknown parameters.
1674                  */
1675                 if (NULL == *errp)
1676                         *errp = sctp_make_op_error_space(asoc, chunk,
1677                                         ntohs(chunk->chunk_hdr->length));
1678
1679                 if (*errp)
1680                         sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
1681                                         param.v,
1682                                         WORD_ROUND(ntohs(param.p->length)));
1683
1684                 break;
1685         case SCTP_PARAM_ACTION_SKIP:
1686                 break;
1687         case SCTP_PARAM_ACTION_SKIP_ERR:
1688                 /* Make an ERROR chunk, preparing enough room for
1689                  * returning multiple unknown parameters.
1690                  */
1691                 if (NULL == *errp)
1692                         *errp = sctp_make_op_error_space(asoc, chunk,
1693                                         ntohs(chunk->chunk_hdr->length));
1694
1695                 if (*errp) {
1696                         sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
1697                                         param.v,
1698                                         WORD_ROUND(ntohs(param.p->length)));
1699                 } else {
1700                         /* If there is no memory for generating the ERROR
1701                          * report as specified, an ABORT will be triggered
1702                          * to the peer and the association won't be
1703                          * established.
1704                          */
1705                         retval = 0;
1706                 }
1707
1708                 break;
1709         default:
1710                 break;
1711         }
1712
1713         return retval;
1714 }
1715
1716 /* Find unrecognized parameters in the chunk.
1717  * Return values:
1718  *      0 - discard the chunk
1719  *      1 - continue with the chunk
1720  */
1721 static int sctp_verify_param(const struct sctp_association *asoc,
1722                              union sctp_params param,
1723                              sctp_cid_t cid,
1724                              struct sctp_chunk *chunk,
1725                              struct sctp_chunk **err_chunk)
1726 {
1727         int retval = 1;
1728
1729         /* FIXME - This routine is not looking at each parameter per the
1730          * chunk type, i.e., unrecognized parameters should be further
1731          * identified based on the chunk id.
1732          */
1733
1734         switch (param.p->type) {
1735         case SCTP_PARAM_IPV4_ADDRESS:
1736         case SCTP_PARAM_IPV6_ADDRESS:
1737         case SCTP_PARAM_COOKIE_PRESERVATIVE:
1738         case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
1739         case SCTP_PARAM_STATE_COOKIE:
1740         case SCTP_PARAM_HEARTBEAT_INFO:
1741         case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
1742         case SCTP_PARAM_ECN_CAPABLE:
1743         case SCTP_PARAM_ADAPTATION_LAYER_IND:
1744                 break;
1745
1746         case SCTP_PARAM_HOST_NAME_ADDRESS:
1747                 /* Tell the peer, we won't support this param.  */
1748                 return sctp_process_hn_param(asoc, param, chunk, err_chunk);
1749         case SCTP_PARAM_FWD_TSN_SUPPORT:
1750                 if (sctp_prsctp_enable)
1751                         break;
1752                 /* Fall Through */
1753         default:
1754                 SCTP_DEBUG_PRINTK("Unrecognized param: %d for chunk %d.\n",
1755                                 ntohs(param.p->type), cid);
1756                 return sctp_process_unk_param(asoc, param, chunk, err_chunk);
1757
1758                 break;
1759         }
1760         return retval;
1761 }
1762
1763 /* Verify the INIT packet before we process it.  */
1764 int sctp_verify_init(const struct sctp_association *asoc,
1765                      sctp_cid_t cid,
1766                      sctp_init_chunk_t *peer_init,
1767                      struct sctp_chunk *chunk,
1768                      struct sctp_chunk **errp)
1769 {
1770         union sctp_params param;
1771         int has_cookie = 0;
1772
1773         /* Verify stream values are non-zero. */
1774         if ((0 == peer_init->init_hdr.num_outbound_streams) ||
1775             (0 == peer_init->init_hdr.num_inbound_streams) ||
1776             (0 == peer_init->init_hdr.init_tag) ||
1777             (SCTP_DEFAULT_MINWINDOW > ntohl(peer_init->init_hdr.a_rwnd))) {
1778
1779                 sctp_process_inv_mandatory(asoc, chunk, errp);
1780                 return 0;
1781         }
1782
1783         /* Check for missing mandatory parameters.  */
1784         sctp_walk_params(param, peer_init, init_hdr.params) {
1785
1786                 if (SCTP_PARAM_STATE_COOKIE == param.p->type)
1787                         has_cookie = 1;
1788
1789         } /* for (loop through all parameters) */
1790
1791         /* There is a possibility that a parameter length was bad and
1792          * in that case we would have stoped walking the parameters.
1793          * The current param.p would point at the bad one.
1794          * Current consensus on the mailing list is to generate a PROTOCOL
1795          * VIOLATION error.  We build the ERROR chunk here and let the normal
1796          * error handling code build and send the packet.
1797          */
1798         if (param.v < (void*)chunk->chunk_end - sizeof(sctp_paramhdr_t)) {
1799                 sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
1800                 return 0;
1801         }
1802
1803         /* The only missing mandatory param possible today is
1804          * the state cookie for an INIT-ACK chunk.
1805          */
1806         if ((SCTP_CID_INIT_ACK == cid) && !has_cookie) {
1807                 sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
1808                                            chunk, errp);
1809                 return 0;
1810         }
1811
1812         /* Find unrecognized parameters. */
1813
1814         sctp_walk_params(param, peer_init, init_hdr.params) {
1815
1816                 if (!sctp_verify_param(asoc, param, cid, chunk, errp)) {
1817                         if (SCTP_PARAM_HOST_NAME_ADDRESS == param.p->type)
1818                                 return 0;
1819                         else
1820                                 return 1;
1821                 }
1822
1823         } /* for (loop through all parameters) */
1824
1825         return 1;
1826 }
1827
1828 /* Unpack the parameters in an INIT packet into an association.
1829  * Returns 0 on failure, else success.
1830  * FIXME:  This is an association method.
1831  */
1832 int sctp_process_init(struct sctp_association *asoc, sctp_cid_t cid,
1833                       const union sctp_addr *peer_addr,
1834                       sctp_init_chunk_t *peer_init, gfp_t gfp)
1835 {
1836         union sctp_params param;
1837         struct sctp_transport *transport;
1838         struct list_head *pos, *temp;
1839         char *cookie;
1840
1841         /* We must include the address that the INIT packet came from.
1842          * This is the only address that matters for an INIT packet.
1843          * When processing a COOKIE ECHO, we retrieve the from address
1844          * of the INIT from the cookie.
1845          */
1846
1847         /* This implementation defaults to making the first transport
1848          * added as the primary transport.  The source address seems to
1849          * be a a better choice than any of the embedded addresses.
1850          */
1851         if (peer_addr) {
1852                 if(!sctp_assoc_add_peer(asoc, peer_addr, gfp, SCTP_ACTIVE))
1853                         goto nomem;
1854         }
1855
1856         /* Process the initialization parameters.  */
1857
1858         sctp_walk_params(param, peer_init, init_hdr.params) {
1859
1860                 if (!sctp_process_param(asoc, param, peer_addr, gfp))
1861                         goto clean_up;
1862         }
1863
1864         /* Walk list of transports, removing transports in the UNKNOWN state. */
1865         list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
1866                 transport = list_entry(pos, struct sctp_transport, transports);
1867                 if (transport->state == SCTP_UNKNOWN) {
1868                         sctp_assoc_rm_peer(asoc, transport);
1869                 }
1870         }
1871
1872         /* The fixed INIT headers are always in network byte
1873          * order.
1874          */
1875         asoc->peer.i.init_tag =
1876                 ntohl(peer_init->init_hdr.init_tag);
1877         asoc->peer.i.a_rwnd =
1878                 ntohl(peer_init->init_hdr.a_rwnd);
1879         asoc->peer.i.num_outbound_streams =
1880                 ntohs(peer_init->init_hdr.num_outbound_streams);
1881         asoc->peer.i.num_inbound_streams =
1882                 ntohs(peer_init->init_hdr.num_inbound_streams);
1883         asoc->peer.i.initial_tsn =
1884                 ntohl(peer_init->init_hdr.initial_tsn);
1885
1886         /* Apply the upper bounds for output streams based on peer's
1887          * number of inbound streams.
1888          */
1889         if (asoc->c.sinit_num_ostreams  >
1890             ntohs(peer_init->init_hdr.num_inbound_streams)) {
1891                 asoc->c.sinit_num_ostreams =
1892                         ntohs(peer_init->init_hdr.num_inbound_streams);
1893         }
1894
1895         if (asoc->c.sinit_max_instreams >
1896             ntohs(peer_init->init_hdr.num_outbound_streams)) {
1897                 asoc->c.sinit_max_instreams =
1898                         ntohs(peer_init->init_hdr.num_outbound_streams);
1899         }
1900
1901         /* Copy Initiation tag from INIT to VT_peer in cookie.   */
1902         asoc->c.peer_vtag = asoc->peer.i.init_tag;
1903
1904         /* Peer Rwnd   : Current calculated value of the peer's rwnd.  */
1905         asoc->peer.rwnd = asoc->peer.i.a_rwnd;
1906
1907         /* Copy cookie in case we need to resend COOKIE-ECHO. */
1908         cookie = asoc->peer.cookie;
1909         if (cookie) {
1910                 asoc->peer.cookie = kmemdup(cookie, asoc->peer.cookie_len, gfp);
1911                 if (!asoc->peer.cookie)
1912                         goto clean_up;
1913         }
1914
1915         /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
1916          * high (for example, implementations MAY use the size of the receiver
1917          * advertised window).
1918          */
1919         list_for_each(pos, &asoc->peer.transport_addr_list) {
1920                 transport = list_entry(pos, struct sctp_transport, transports);
1921                 transport->ssthresh = asoc->peer.i.a_rwnd;
1922         }
1923
1924         /* Set up the TSN tracking pieces.  */
1925         sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_SIZE,
1926                          asoc->peer.i.initial_tsn);
1927
1928         /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
1929          *
1930          * The stream sequence number in all the streams shall start
1931          * from 0 when the association is established.  Also, when the
1932          * stream sequence number reaches the value 65535 the next
1933          * stream sequence number shall be set to 0.
1934          */
1935
1936         /* Allocate storage for the negotiated streams if it is not a temporary
1937          * association.
1938          */
1939         if (!asoc->temp) {
1940                 int error;
1941
1942                 asoc->ssnmap = sctp_ssnmap_new(asoc->c.sinit_max_instreams,
1943                                                asoc->c.sinit_num_ostreams, gfp);
1944                 if (!asoc->ssnmap)
1945                         goto clean_up;
1946
1947                 error = sctp_assoc_set_id(asoc, gfp);
1948                 if (error)
1949                         goto clean_up;
1950         }
1951
1952         /* ADDIP Section 4.1 ASCONF Chunk Procedures
1953          *
1954          * When an endpoint has an ASCONF signaled change to be sent to the
1955          * remote endpoint it should do the following:
1956          * ...
1957          * A2) A serial number should be assigned to the Chunk. The serial
1958          * number should be a monotonically increasing number. All serial
1959          * numbers are defined to be initialized at the start of the
1960          * association to the same value as the Initial TSN.
1961          */
1962         asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
1963         return 1;
1964
1965 clean_up:
1966         /* Release the transport structures. */
1967         list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
1968                 transport = list_entry(pos, struct sctp_transport, transports);
1969                 list_del_init(pos);
1970                 sctp_transport_free(transport);
1971         }
1972
1973         asoc->peer.transport_count = 0;
1974
1975 nomem:
1976         return 0;
1977 }
1978
1979
1980 /* Update asoc with the option described in param.
1981  *
1982  * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
1983  *
1984  * asoc is the association to update.
1985  * param is the variable length parameter to use for update.
1986  * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
1987  * If the current packet is an INIT we want to minimize the amount of
1988  * work we do.  In particular, we should not build transport
1989  * structures for the addresses.
1990  */
1991 static int sctp_process_param(struct sctp_association *asoc,
1992                               union sctp_params param,
1993                               const union sctp_addr *peer_addr,
1994                               gfp_t gfp)
1995 {
1996         union sctp_addr addr;
1997         int i;
1998         __u16 sat;
1999         int retval = 1;
2000         sctp_scope_t scope;
2001         time_t stale;
2002         struct sctp_af *af;
2003
2004         /* We maintain all INIT parameters in network byte order all the
2005          * time.  This allows us to not worry about whether the parameters
2006          * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
2007          */
2008         switch (param.p->type) {
2009         case SCTP_PARAM_IPV6_ADDRESS:
2010                 if (PF_INET6 != asoc->base.sk->sk_family)
2011                         break;
2012                 /* Fall through. */
2013         case SCTP_PARAM_IPV4_ADDRESS:
2014                 af = sctp_get_af_specific(param_type2af(param.p->type));
2015                 af->from_addr_param(&addr, param.addr, htons(asoc->peer.port), 0);
2016                 scope = sctp_scope(peer_addr);
2017                 if (sctp_in_scope(&addr, scope))
2018                         if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_UNCONFIRMED))
2019                                 return 0;
2020                 break;
2021
2022         case SCTP_PARAM_COOKIE_PRESERVATIVE:
2023                 if (!sctp_cookie_preserve_enable)
2024                         break;
2025
2026                 stale = ntohl(param.life->lifespan_increment);
2027
2028                 /* Suggested Cookie Life span increment's unit is msec,
2029                  * (1/1000sec).
2030                  */
2031                 asoc->cookie_life.tv_sec += stale / 1000;
2032                 asoc->cookie_life.tv_usec += (stale % 1000) * 1000;
2033                 break;
2034
2035         case SCTP_PARAM_HOST_NAME_ADDRESS:
2036                 SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
2037                 break;
2038
2039         case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2040                 /* Turn off the default values first so we'll know which
2041                  * ones are really set by the peer.
2042                  */
2043                 asoc->peer.ipv4_address = 0;
2044                 asoc->peer.ipv6_address = 0;
2045
2046                 /* Cycle through address types; avoid divide by 0. */
2047                 sat = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2048                 if (sat)
2049                         sat /= sizeof(__u16);
2050
2051                 for (i = 0; i < sat; ++i) {
2052                         switch (param.sat->types[i]) {
2053                         case SCTP_PARAM_IPV4_ADDRESS:
2054                                 asoc->peer.ipv4_address = 1;
2055                                 break;
2056
2057                         case SCTP_PARAM_IPV6_ADDRESS:
2058                                 asoc->peer.ipv6_address = 1;
2059                                 break;
2060
2061                         case SCTP_PARAM_HOST_NAME_ADDRESS:
2062                                 asoc->peer.hostname_address = 1;
2063                                 break;
2064
2065                         default: /* Just ignore anything else.  */
2066                                 break;
2067                         }
2068                 }
2069                 break;
2070
2071         case SCTP_PARAM_STATE_COOKIE:
2072                 asoc->peer.cookie_len =
2073                         ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2074                 asoc->peer.cookie = param.cookie->body;
2075                 break;
2076
2077         case SCTP_PARAM_HEARTBEAT_INFO:
2078                 /* Would be odd to receive, but it causes no problems. */
2079                 break;
2080
2081         case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2082                 /* Rejected during verify stage. */
2083                 break;
2084
2085         case SCTP_PARAM_ECN_CAPABLE:
2086                 asoc->peer.ecn_capable = 1;
2087                 break;
2088
2089         case SCTP_PARAM_ADAPTATION_LAYER_IND:
2090                 asoc->peer.adaptation_ind = param.aind->adaptation_ind;
2091                 break;
2092
2093         case SCTP_PARAM_FWD_TSN_SUPPORT:
2094                 if (sctp_prsctp_enable) {
2095                         asoc->peer.prsctp_capable = 1;
2096                         break;
2097                 }
2098                 /* Fall Through */
2099         default:
2100                 /* Any unrecognized parameters should have been caught
2101                  * and handled by sctp_verify_param() which should be
2102                  * called prior to this routine.  Simply log the error
2103                  * here.
2104                  */
2105                 SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
2106                                   ntohs(param.p->type), asoc);
2107                 break;
2108         }
2109
2110         return retval;
2111 }
2112
2113 /* Select a new verification tag.  */
2114 __u32 sctp_generate_tag(const struct sctp_endpoint *ep)
2115 {
2116         /* I believe that this random number generator complies with RFC1750.
2117          * A tag of 0 is reserved for special cases (e.g. INIT).
2118          */
2119         __u32 x;
2120
2121         do {
2122                 get_random_bytes(&x, sizeof(__u32));
2123         } while (x == 0);
2124
2125         return x;
2126 }
2127
2128 /* Select an initial TSN to send during startup.  */
2129 __u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
2130 {
2131         __u32 retval;
2132
2133         get_random_bytes(&retval, sizeof(__u32));
2134         return retval;
2135 }
2136
2137 /*
2138  * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
2139  *      0                   1                   2                   3
2140  *      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
2141  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2142  *     | Type = 0xC1   |  Chunk Flags  |      Chunk Length             |
2143  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2144  *     |                       Serial Number                           |
2145  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2146  *     |                    Address Parameter                          |
2147  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2148  *     |                     ASCONF Parameter #1                       |
2149  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2150  *     \                                                               \
2151  *     /                             ....                              /
2152  *     \                                                               \
2153  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2154  *     |                     ASCONF Parameter #N                       |
2155  *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2156  *
2157  * Address Parameter and other parameter will not be wrapped in this function
2158  */
2159 static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
2160                                            union sctp_addr *addr,
2161                                            int vparam_len)
2162 {
2163         sctp_addiphdr_t asconf;
2164         struct sctp_chunk *retval;
2165         int length = sizeof(asconf) + vparam_len;
2166         union sctp_addr_param addrparam;
2167         int addrlen;
2168         struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2169
2170         addrlen = af->to_addr_param(addr, &addrparam);
2171         if (!addrlen)
2172                 return NULL;
2173         length += addrlen;
2174
2175         /* Create the chunk.  */
2176         retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF, 0, length);
2177         if (!retval)
2178                 return NULL;
2179
2180         asconf.serial = htonl(asoc->addip_serial++);
2181
2182         retval->subh.addip_hdr =
2183                 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2184         retval->param_hdr.v =
2185                 sctp_addto_chunk(retval, addrlen, &addrparam);
2186
2187         return retval;
2188 }
2189
2190 /* ADDIP
2191  * 3.2.1 Add IP Address
2192  *      0                   1                   2                   3
2193  *      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
2194  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2195  *     |        Type = 0xC001          |    Length = Variable          |
2196  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2197  *     |               ASCONF-Request Correlation ID                   |
2198  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2199  *     |                       Address Parameter                       |
2200  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2201  *
2202  * 3.2.2 Delete IP Address
2203  *      0                   1                   2                   3
2204  *      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
2205  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2206  *     |        Type = 0xC002          |    Length = Variable          |
2207  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2208  *     |               ASCONF-Request Correlation ID                   |
2209  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2210  *     |                       Address Parameter                       |
2211  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2212  *
2213  */
2214 struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
2215                                               union sctp_addr         *laddr,
2216                                               struct sockaddr         *addrs,
2217                                               int                     addrcnt,
2218                                               __be16                  flags)
2219 {
2220         sctp_addip_param_t      param;
2221         struct sctp_chunk       *retval;
2222         union sctp_addr_param   addr_param;
2223         union sctp_addr         *addr;
2224         void                    *addr_buf;
2225         struct sctp_af          *af;
2226         int                     paramlen = sizeof(param);
2227         int                     addr_param_len = 0;
2228         int                     totallen = 0;
2229         int                     i;
2230
2231         /* Get total length of all the address parameters. */
2232         addr_buf = addrs;
2233         for (i = 0; i < addrcnt; i++) {
2234                 addr = (union sctp_addr *)addr_buf;
2235                 af = sctp_get_af_specific(addr->v4.sin_family);
2236                 addr_param_len = af->to_addr_param(addr, &addr_param);
2237
2238                 totallen += paramlen;
2239                 totallen += addr_param_len;
2240
2241                 addr_buf += af->sockaddr_len;
2242         }
2243
2244         /* Create an asconf chunk with the required length. */
2245         retval = sctp_make_asconf(asoc, laddr, totallen);
2246         if (!retval)
2247                 return NULL;
2248
2249         /* Add the address parameters to the asconf chunk. */
2250         addr_buf = addrs;
2251         for (i = 0; i < addrcnt; i++) {
2252                 addr = (union sctp_addr *)addr_buf;
2253                 af = sctp_get_af_specific(addr->v4.sin_family);
2254                 addr_param_len = af->to_addr_param(addr, &addr_param);
2255                 param.param_hdr.type = flags;
2256                 param.param_hdr.length = htons(paramlen + addr_param_len);
2257                 param.crr_id = i;
2258
2259                 sctp_addto_chunk(retval, paramlen, &param);
2260                 sctp_addto_chunk(retval, addr_param_len, &addr_param);
2261
2262                 addr_buf += af->sockaddr_len;
2263         }
2264         return retval;
2265 }
2266
2267 /* ADDIP
2268  * 3.2.4 Set Primary IP Address
2269  *      0                   1                   2                   3
2270  *      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
2271  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2272  *     |        Type =0xC004           |    Length = Variable          |
2273  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2274  *     |               ASCONF-Request Correlation ID                   |
2275  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2276  *     |                       Address Parameter                       |
2277  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2278  *
2279  * Create an ASCONF chunk with Set Primary IP address parameter.
2280  */
2281 struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
2282                                              union sctp_addr *addr)
2283 {
2284         sctp_addip_param_t      param;
2285         struct sctp_chunk       *retval;
2286         int                     len = sizeof(param);
2287         union sctp_addr_param   addrparam;
2288         int                     addrlen;
2289         struct sctp_af          *af = sctp_get_af_specific(addr->v4.sin_family);
2290
2291         addrlen = af->to_addr_param(addr, &addrparam);
2292         if (!addrlen)
2293                 return NULL;
2294         len += addrlen;
2295
2296         /* Create the chunk and make asconf header. */
2297         retval = sctp_make_asconf(asoc, addr, len);
2298         if (!retval)
2299                 return NULL;
2300
2301         param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
2302         param.param_hdr.length = htons(len);
2303         param.crr_id = 0;
2304
2305         sctp_addto_chunk(retval, sizeof(param), &param);
2306         sctp_addto_chunk(retval, addrlen, &addrparam);
2307
2308         return retval;
2309 }
2310
2311 /* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2312  *      0                   1                   2                   3
2313  *      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
2314  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2315  *     | Type = 0x80   |  Chunk Flags  |      Chunk Length             |
2316  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2317  *     |                       Serial Number                           |
2318  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2319  *     |                 ASCONF Parameter Response#1                   |
2320  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2321  *     \                                                               \
2322  *     /                             ....                              /
2323  *     \                                                               \
2324  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2325  *     |                 ASCONF Parameter Response#N                   |
2326  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2327  *
2328  * Create an ASCONF_ACK chunk with enough space for the parameter responses.
2329  */
2330 static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
2331                                                __u32 serial, int vparam_len)
2332 {
2333         sctp_addiphdr_t         asconf;
2334         struct sctp_chunk       *retval;
2335         int                     length = sizeof(asconf) + vparam_len;
2336
2337         /* Create the chunk.  */
2338         retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF_ACK, 0, length);
2339         if (!retval)
2340                 return NULL;
2341
2342         asconf.serial = htonl(serial);
2343
2344         retval->subh.addip_hdr =
2345                 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2346
2347         return retval;
2348 }
2349
2350 /* Add response parameters to an ASCONF_ACK chunk. */
2351 static void sctp_add_asconf_response(struct sctp_chunk *chunk, __be32 crr_id,
2352                               __be16 err_code, sctp_addip_param_t *asconf_param)
2353 {
2354         sctp_addip_param_t      ack_param;
2355         sctp_errhdr_t           err_param;
2356         int                     asconf_param_len = 0;
2357         int                     err_param_len = 0;
2358         __be16                  response_type;
2359
2360         if (SCTP_ERROR_NO_ERROR == err_code) {
2361                 response_type = SCTP_PARAM_SUCCESS_REPORT;
2362         } else {
2363                 response_type = SCTP_PARAM_ERR_CAUSE;
2364                 err_param_len = sizeof(err_param);
2365                 if (asconf_param)
2366                         asconf_param_len =
2367                                  ntohs(asconf_param->param_hdr.length);
2368         }
2369
2370         /* Add Success Indication or Error Cause Indication parameter. */
2371         ack_param.param_hdr.type = response_type;
2372         ack_param.param_hdr.length = htons(sizeof(ack_param) +
2373                                            err_param_len +
2374                                            asconf_param_len);
2375         ack_param.crr_id = crr_id;
2376         sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
2377
2378         if (SCTP_ERROR_NO_ERROR == err_code)
2379                 return;
2380
2381         /* Add Error Cause parameter. */
2382         err_param.cause = err_code;
2383         err_param.length = htons(err_param_len + asconf_param_len);
2384         sctp_addto_chunk(chunk, err_param_len, &err_param);
2385
2386         /* Add the failed TLV copied from ASCONF chunk. */
2387         if (asconf_param)
2388                 sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
2389 }
2390
2391 /* Process a asconf parameter. */
2392 static __be16 sctp_process_asconf_param(struct sctp_association *asoc,
2393                                        struct sctp_chunk *asconf,
2394                                        sctp_addip_param_t *asconf_param)
2395 {
2396         struct sctp_transport *peer;
2397         struct sctp_af *af;
2398         union sctp_addr addr;
2399         struct list_head *pos;
2400         union sctp_addr_param *addr_param;
2401
2402         addr_param = (union sctp_addr_param *)
2403                         ((void *)asconf_param + sizeof(sctp_addip_param_t));
2404
2405         af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2406         if (unlikely(!af))
2407                 return SCTP_ERROR_INV_PARAM;
2408
2409         af->from_addr_param(&addr, addr_param, htons(asoc->peer.port), 0);
2410         switch (asconf_param->param_hdr.type) {
2411         case SCTP_PARAM_ADD_IP:
2412                 /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
2413                  * request and does not have the local resources to add this
2414                  * new address to the association, it MUST return an Error
2415                  * Cause TLV set to the new error code 'Operation Refused
2416                  * Due to Resource Shortage'.
2417                  */
2418
2419                 peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC, SCTP_UNCONFIRMED);
2420                 if (!peer)
2421                         return SCTP_ERROR_RSRC_LOW;
2422
2423                 /* Start the heartbeat timer. */
2424                 if (!mod_timer(&peer->hb_timer, sctp_transport_timeout(peer)))
2425                         sctp_transport_hold(peer);
2426                 break;
2427         case SCTP_PARAM_DEL_IP:
2428                 /* ADDIP 4.3 D7) If a request is received to delete the
2429                  * last remaining IP address of a peer endpoint, the receiver
2430                  * MUST send an Error Cause TLV with the error cause set to the
2431                  * new error code 'Request to Delete Last Remaining IP Address'.
2432                  */
2433                 pos = asoc->peer.transport_addr_list.next;
2434                 if (pos->next == &asoc->peer.transport_addr_list)
2435                         return SCTP_ERROR_DEL_LAST_IP;
2436
2437                 /* ADDIP 4.3 D8) If a request is received to delete an IP
2438                  * address which is also the source address of the IP packet
2439                  * which contained the ASCONF chunk, the receiver MUST reject
2440                  * this request. To reject the request the receiver MUST send
2441                  * an Error Cause TLV set to the new error code 'Request to
2442                  * Delete Source IP Address'
2443                  */
2444                 if (sctp_cmp_addr_exact(sctp_source(asconf), &addr))
2445                         return SCTP_ERROR_DEL_SRC_IP;
2446
2447                 sctp_assoc_del_peer(asoc, &addr);
2448                 break;
2449         case SCTP_PARAM_SET_PRIMARY:
2450                 peer = sctp_assoc_lookup_paddr(asoc, &addr);
2451                 if (!peer)
2452                         return SCTP_ERROR_INV_PARAM;
2453
2454                 sctp_assoc_set_primary(asoc, peer);
2455                 break;
2456         default:
2457                 return SCTP_ERROR_INV_PARAM;
2458                 break;
2459         }
2460
2461         return SCTP_ERROR_NO_ERROR;
2462 }
2463
2464 /* Process an incoming ASCONF chunk with the next expected serial no. and
2465  * return an ASCONF_ACK chunk to be sent in response.
2466  */
2467 struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
2468                                        struct sctp_chunk *asconf)
2469 {
2470         sctp_addiphdr_t         *hdr;
2471         union sctp_addr_param   *addr_param;
2472         sctp_addip_param_t      *asconf_param;
2473         struct sctp_chunk       *asconf_ack;
2474
2475         __be16  err_code;
2476         int     length = 0;
2477         int     chunk_len = asconf->skb->len;
2478         __u32   serial;
2479         int     all_param_pass = 1;
2480
2481         hdr = (sctp_addiphdr_t *)asconf->skb->data;
2482         serial = ntohl(hdr->serial);
2483
2484         /* Skip the addiphdr and store a pointer to address parameter.  */
2485         length = sizeof(sctp_addiphdr_t);
2486         addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
2487         chunk_len -= length;
2488
2489         /* Skip the address parameter and store a pointer to the first
2490          * asconf paramter.
2491          */
2492         length = ntohs(addr_param->v4.param_hdr.length);
2493         asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
2494         chunk_len -= length;
2495
2496         /* create an ASCONF_ACK chunk.
2497          * Based on the definitions of parameters, we know that the size of
2498          * ASCONF_ACK parameters are less than or equal to the twice of ASCONF
2499          * paramters.
2500          */
2501         asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 2);
2502         if (!asconf_ack)
2503                 goto done;
2504
2505         /* Process the TLVs contained within the ASCONF chunk. */
2506         while (chunk_len > 0) {
2507                 err_code = sctp_process_asconf_param(asoc, asconf,
2508                                                      asconf_param);
2509                 /* ADDIP 4.1 A7)
2510                  * If an error response is received for a TLV parameter,
2511                  * all TLVs with no response before the failed TLV are
2512                  * considered successful if not reported.  All TLVs after
2513                  * the failed response are considered unsuccessful unless
2514                  * a specific success indication is present for the parameter.
2515                  */
2516                 if (SCTP_ERROR_NO_ERROR != err_code)
2517                         all_param_pass = 0;
2518
2519                 if (!all_param_pass)
2520                         sctp_add_asconf_response(asconf_ack,
2521                                                  asconf_param->crr_id, err_code,
2522                                                  asconf_param);
2523
2524                 /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
2525                  * an IP address sends an 'Out of Resource' in its response, it
2526                  * MUST also fail any subsequent add or delete requests bundled
2527                  * in the ASCONF.
2528                  */
2529                 if (SCTP_ERROR_RSRC_LOW == err_code)
2530                         goto done;
2531
2532                 /* Move to the next ASCONF param. */
2533                 length = ntohs(asconf_param->param_hdr.length);
2534                 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
2535                                                       length);
2536                 chunk_len -= length;
2537         }
2538
2539 done:
2540         asoc->peer.addip_serial++;
2541
2542         /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
2543          * after freeing the reference to old asconf ack if any.
2544          */
2545         if (asconf_ack) {
2546                 if (asoc->addip_last_asconf_ack)
2547                         sctp_chunk_free(asoc->addip_last_asconf_ack);
2548
2549                 sctp_chunk_hold(asconf_ack);
2550                 asoc->addip_last_asconf_ack = asconf_ack;
2551         }
2552
2553         return asconf_ack;
2554 }
2555
2556 /* Process a asconf parameter that is successfully acked. */
2557 static int sctp_asconf_param_success(struct sctp_association *asoc,
2558                                      sctp_addip_param_t *asconf_param)
2559 {
2560         struct sctp_af *af;
2561         union sctp_addr addr;
2562         struct sctp_bind_addr *bp = &asoc->base.bind_addr;
2563         union sctp_addr_param *addr_param;
2564         struct list_head *pos;
2565         struct sctp_transport *transport;
2566         struct sctp_sockaddr_entry *saddr;
2567         int retval = 0;
2568
2569         addr_param = (union sctp_addr_param *)
2570                         ((void *)asconf_param + sizeof(sctp_addip_param_t));
2571
2572         /* We have checked the packet before, so we do not check again. */
2573         af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2574         af->from_addr_param(&addr, addr_param, htons(bp->port), 0);
2575
2576         switch (asconf_param->param_hdr.type) {
2577         case SCTP_PARAM_ADD_IP:
2578                 sctp_local_bh_disable();
2579                 sctp_write_lock(&asoc->base.addr_lock);
2580                 list_for_each(pos, &bp->address_list) {
2581                         saddr = list_entry(pos, struct sctp_sockaddr_entry, list);
2582                         if (sctp_cmp_addr_exact(&saddr->a, &addr))
2583                                 saddr->use_as_src = 1;
2584                 }
2585                 sctp_write_unlock(&asoc->base.addr_lock);
2586                 sctp_local_bh_enable();
2587                 break;
2588         case SCTP_PARAM_DEL_IP:
2589                 sctp_local_bh_disable();
2590                 sctp_write_lock(&asoc->base.addr_lock);
2591                 retval = sctp_del_bind_addr(bp, &addr);
2592                 sctp_write_unlock(&asoc->base.addr_lock);
2593                 sctp_local_bh_enable();
2594                 list_for_each(pos, &asoc->peer.transport_addr_list) {
2595                         transport = list_entry(pos, struct sctp_transport,
2596                                                  transports);
2597                         dst_release(transport->dst);
2598                         sctp_transport_route(transport, NULL,
2599                                              sctp_sk(asoc->base.sk));
2600                 }
2601                 break;
2602         default:
2603                 break;
2604         }
2605
2606         return retval;
2607 }
2608
2609 /* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
2610  * for the given asconf parameter.  If there is no response for this parameter,
2611  * return the error code based on the third argument 'no_err'.
2612  * ADDIP 4.1
2613  * A7) If an error response is received for a TLV parameter, all TLVs with no
2614  * response before the failed TLV are considered successful if not reported.
2615  * All TLVs after the failed response are considered unsuccessful unless a
2616  * specific success indication is present for the parameter.
2617  */
2618 static __be16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
2619                                       sctp_addip_param_t *asconf_param,
2620                                       int no_err)
2621 {
2622         sctp_addip_param_t      *asconf_ack_param;
2623         sctp_errhdr_t           *err_param;
2624         int                     length;
2625         int                     asconf_ack_len = asconf_ack->skb->len;
2626         __be16                  err_code;
2627
2628         if (no_err)
2629                 err_code = SCTP_ERROR_NO_ERROR;
2630         else
2631                 err_code = SCTP_ERROR_REQ_REFUSED;
2632
2633         /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
2634          * the first asconf_ack parameter.
2635          */
2636         length = sizeof(sctp_addiphdr_t);
2637         asconf_ack_param = (sctp_addip_param_t *)(asconf_ack->skb->data +
2638                                                   length);
2639         asconf_ack_len -= length;
2640
2641         while (asconf_ack_len > 0) {
2642                 if (asconf_ack_param->crr_id == asconf_param->crr_id) {
2643                         switch(asconf_ack_param->param_hdr.type) {
2644                         case SCTP_PARAM_SUCCESS_REPORT:
2645                                 return SCTP_ERROR_NO_ERROR;
2646                         case SCTP_PARAM_ERR_CAUSE:
2647                                 length = sizeof(sctp_addip_param_t);
2648                                 err_param = (sctp_errhdr_t *)
2649                                            ((void *)asconf_ack_param + length);
2650                                 asconf_ack_len -= length;
2651                                 if (asconf_ack_len > 0)
2652                                         return err_param->cause;
2653                                 else
2654                                         return SCTP_ERROR_INV_PARAM;
2655                                 break;
2656                         default:
2657                                 return SCTP_ERROR_INV_PARAM;
2658                         }
2659                 }
2660
2661                 length = ntohs(asconf_ack_param->param_hdr.length);
2662                 asconf_ack_param = (sctp_addip_param_t *)
2663                                         ((void *)asconf_ack_param + length);
2664                 asconf_ack_len -= length;
2665         }
2666
2667         return err_code;
2668 }
2669
2670 /* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
2671 int sctp_process_asconf_ack(struct sctp_association *asoc,
2672                             struct sctp_chunk *asconf_ack)
2673 {
2674         struct sctp_chunk       *asconf = asoc->addip_last_asconf;
2675         union sctp_addr_param   *addr_param;
2676         sctp_addip_param_t      *asconf_param;
2677         int     length = 0;
2678         int     asconf_len = asconf->skb->len;
2679         int     all_param_pass = 0;
2680         int     no_err = 1;
2681         int     retval = 0;
2682         __be16  err_code = SCTP_ERROR_NO_ERROR;
2683
2684         /* Skip the chunkhdr and addiphdr from the last asconf sent and store
2685          * a pointer to address parameter.
2686          */
2687         length = sizeof(sctp_addip_chunk_t);
2688         addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
2689         asconf_len -= length;
2690
2691         /* Skip the address parameter in the last asconf sent and store a
2692          * pointer to the first asconf paramter.
2693          */
2694         length = ntohs(addr_param->v4.param_hdr.length);
2695         asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
2696         asconf_len -= length;
2697
2698         /* ADDIP 4.1
2699          * A8) If there is no response(s) to specific TLV parameter(s), and no
2700          * failures are indicated, then all request(s) are considered
2701          * successful.
2702          */
2703         if (asconf_ack->skb->len == sizeof(sctp_addiphdr_t))
2704                 all_param_pass = 1;
2705
2706         /* Process the TLVs contained in the last sent ASCONF chunk. */
2707         while (asconf_len > 0) {
2708                 if (all_param_pass)
2709                         err_code = SCTP_ERROR_NO_ERROR;
2710                 else {
2711                         err_code = sctp_get_asconf_response(asconf_ack,
2712                                                             asconf_param,
2713                                                             no_err);
2714                         if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
2715                                 no_err = 0;
2716                 }
2717
2718                 switch (err_code) {
2719                 case SCTP_ERROR_NO_ERROR:
2720                         retval = sctp_asconf_param_success(asoc, asconf_param);
2721                         break;
2722
2723                 case SCTP_ERROR_RSRC_LOW:
2724                         retval = 1;
2725                         break;
2726
2727                 case SCTP_ERROR_INV_PARAM:
2728                         /* Disable sending this type of asconf parameter in
2729                          * future.
2730                          */
2731                         asoc->peer.addip_disabled_mask |=
2732                                 asconf_param->param_hdr.type;
2733                         break;
2734
2735                 case SCTP_ERROR_REQ_REFUSED:
2736                 case SCTP_ERROR_DEL_LAST_IP:
2737                 case SCTP_ERROR_DEL_SRC_IP:
2738                 default:
2739                          break;
2740                 }
2741
2742                 /* Skip the processed asconf parameter and move to the next
2743                  * one.
2744                  */
2745                 length = ntohs(asconf_param->param_hdr.length);
2746                 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
2747                                                       length);
2748                 asconf_len -= length;
2749         }
2750
2751         /* Free the cached last sent asconf chunk. */
2752         sctp_chunk_free(asconf);
2753         asoc->addip_last_asconf = NULL;
2754
2755         /* Send the next asconf chunk from the addip chunk queue. */
2756         if (!list_empty(&asoc->addip_chunk_list)) {
2757                 struct list_head *entry = asoc->addip_chunk_list.next;
2758                 asconf = list_entry(entry, struct sctp_chunk, list);
2759
2760                 list_del_init(entry);
2761
2762                 /* Hold the chunk until an ASCONF_ACK is received. */
2763                 sctp_chunk_hold(asconf);
2764                 if (sctp_primitive_ASCONF(asoc, asconf))
2765                         sctp_chunk_free(asconf);
2766                 else
2767                         asoc->addip_last_asconf = asconf;
2768         }
2769
2770         return retval;
2771 }
2772
2773 /* Make a FWD TSN chunk. */
2774 struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
2775                                     __u32 new_cum_tsn, size_t nstreams,
2776                                     struct sctp_fwdtsn_skip *skiplist)
2777 {
2778         struct sctp_chunk *retval = NULL;
2779         struct sctp_fwdtsn_chunk *ftsn_chunk;
2780         struct sctp_fwdtsn_hdr ftsn_hdr;
2781         struct sctp_fwdtsn_skip skip;
2782         size_t hint;
2783         int i;
2784
2785         hint = (nstreams + 1) * sizeof(__u32);
2786
2787         retval = sctp_make_chunk(asoc, SCTP_CID_FWD_TSN, 0, hint);
2788
2789         if (!retval)
2790                 return NULL;
2791
2792         ftsn_chunk = (struct sctp_fwdtsn_chunk *)retval->subh.fwdtsn_hdr;
2793
2794         ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
2795         retval->subh.fwdtsn_hdr =
2796                 sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
2797
2798         for (i = 0; i < nstreams; i++) {
2799                 skip.stream = skiplist[i].stream;
2800                 skip.ssn = skiplist[i].ssn;
2801                 sctp_addto_chunk(retval, sizeof(skip), &skip);
2802         }
2803
2804         return retval;
2805 }