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