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