ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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                 sctp_assoc_t assoc_id;
1838
1839                 asoc->ssnmap = sctp_ssnmap_new(asoc->c.sinit_max_instreams,
1840                                                asoc->c.sinit_num_ostreams, gfp);
1841                 if (!asoc->ssnmap)
1842                         goto clean_up;
1843
1844                 do {
1845                         if (unlikely(!idr_pre_get(&sctp_assocs_id, gfp)))
1846                                 goto clean_up;
1847                         spin_lock_bh(&sctp_assocs_id_lock);
1848                         assoc_id = (sctp_assoc_t)idr_get_new(&sctp_assocs_id,
1849                                                              (void *)asoc);
1850                         spin_unlock_bh(&sctp_assocs_id_lock);
1851                 } while (unlikely((int)assoc_id == -1));
1852
1853                 asoc->assoc_id = assoc_id;
1854         }
1855
1856         /* ADDIP Section 4.1 ASCONF Chunk Procedures
1857          *
1858          * When an endpoint has an ASCONF signaled change to be sent to the
1859          * remote endpoint it should do the following:
1860          * ...
1861          * A2) A serial number should be assigned to the Chunk. The serial
1862          * number should be a monotonically increasing number. All serial
1863          * numbers are defined to be initialized at the start of the
1864          * association to the same value as the Initial TSN.
1865          */
1866         asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
1867         return 1;
1868
1869 clean_up:
1870         /* Release the transport structures. */
1871         list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
1872                 transport = list_entry(pos, struct sctp_transport, transports);
1873                 list_del_init(pos);
1874                 sctp_transport_free(transport);
1875         }
1876 nomem:
1877         return 0;
1878 }
1879
1880
1881 /* Update asoc with the option described in param.
1882  *
1883  * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
1884  *
1885  * asoc is the association to update.
1886  * param is the variable length parameter to use for update.
1887  * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
1888  * If the current packet is an INIT we want to minimize the amount of
1889  * work we do.  In particular, we should not build transport
1890  * structures for the addresses.
1891  */
1892 int sctp_process_param(struct sctp_association *asoc, union sctp_params param,
1893                        const union sctp_addr *peer_addr, int gfp)
1894 {
1895         union sctp_addr addr;
1896         int i;
1897         __u16 sat;
1898         int retval = 1;
1899         sctp_scope_t scope;
1900         time_t stale;
1901         struct sctp_af *af;
1902
1903         /* We maintain all INIT parameters in network byte order all the
1904          * time.  This allows us to not worry about whether the parameters
1905          * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
1906          */
1907         switch (param.p->type) {
1908         case SCTP_PARAM_IPV6_ADDRESS:
1909                 if (PF_INET6 != asoc->base.sk->sk_family)
1910                         break;
1911                 /* Fall through. */
1912         case SCTP_PARAM_IPV4_ADDRESS:
1913                 af = sctp_get_af_specific(param_type2af(param.p->type));
1914                 af->from_addr_param(&addr, param.addr, asoc->peer.port, 0);
1915                 scope = sctp_scope(peer_addr);
1916                 if (sctp_in_scope(&addr, scope))
1917                         if (!sctp_assoc_add_peer(asoc, &addr, gfp))
1918                                 return 0;
1919                 break;
1920
1921         case SCTP_PARAM_COOKIE_PRESERVATIVE:
1922                 if (!sctp_cookie_preserve_enable)
1923                         break;
1924
1925                 stale = ntohl(param.life->lifespan_increment);
1926
1927                 /* Suggested Cookie Life span increment's unit is msec,
1928                  * (1/1000sec).
1929                  */
1930                 asoc->cookie_life.tv_sec += stale / 1000;
1931                 asoc->cookie_life.tv_usec += (stale % 1000) * 1000;
1932                 break;
1933
1934         case SCTP_PARAM_HOST_NAME_ADDRESS:
1935                 SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
1936                 break;
1937
1938         case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
1939                 /* Turn off the default values first so we'll know which
1940                  * ones are really set by the peer.
1941                  */
1942                 asoc->peer.ipv4_address = 0;
1943                 asoc->peer.ipv6_address = 0;
1944
1945                 /* Cycle through address types; avoid divide by 0. */
1946                 sat = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
1947                 if (sat)
1948                         sat /= sizeof(__u16);
1949
1950                 for (i = 0; i < sat; ++i) {
1951                         switch (param.sat->types[i]) {
1952                         case SCTP_PARAM_IPV4_ADDRESS:
1953                                 asoc->peer.ipv4_address = 1;
1954                                 break;
1955
1956                         case SCTP_PARAM_IPV6_ADDRESS:
1957                                 asoc->peer.ipv6_address = 1;
1958                                 break;
1959
1960                         case SCTP_PARAM_HOST_NAME_ADDRESS:
1961                                 asoc->peer.hostname_address = 1;
1962                                 break;
1963
1964                         default: /* Just ignore anything else.  */
1965                                 break;
1966                         };
1967                 }
1968                 break;
1969
1970         case SCTP_PARAM_STATE_COOKIE:
1971                 asoc->peer.cookie_len =
1972                         ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
1973                 asoc->peer.cookie = param.cookie->body;
1974                 break;
1975
1976         case SCTP_PARAM_HEARTBEAT_INFO:
1977                 /* Would be odd to receive, but it causes no problems. */
1978                 break;
1979
1980         case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
1981                 /* Rejected during verify stage. */
1982                 break;
1983
1984         case SCTP_PARAM_ECN_CAPABLE:
1985                 asoc->peer.ecn_capable = 1;
1986                 break;
1987
1988         case SCTP_PARAM_FWD_TSN_SUPPORT:
1989                 if (sctp_prsctp_enable) {
1990                         asoc->peer.prsctp_capable = 1;
1991                         break;
1992                 }
1993                 /* Fall Through */ 
1994         default:
1995                 /* Any unrecognized parameters should have been caught
1996                  * and handled by sctp_verify_param() which should be
1997                  * called prior to this routine.  Simply log the error
1998                  * here.
1999                  */
2000                 SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
2001                                   ntohs(param.p->type), asoc);
2002                 break;
2003         };
2004
2005         return retval;
2006 }
2007
2008 /* Select a new verification tag.  */
2009 __u32 sctp_generate_tag(const struct sctp_endpoint *ep)
2010 {
2011         /* I believe that this random number generator complies with RFC1750.
2012          * A tag of 0 is reserved for special cases (e.g. INIT).
2013          */
2014         __u32 x;
2015
2016         do {
2017                 get_random_bytes(&x, sizeof(__u32));
2018         } while (x == 0);
2019
2020         return x;
2021 }
2022
2023 /* Select an initial TSN to send during startup.  */
2024 __u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
2025 {
2026         __u32 retval;
2027
2028         get_random_bytes(&retval, sizeof(__u32));
2029         return retval;
2030 }
2031
2032 /*
2033  * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
2034  *      0                   1                   2                   3
2035  *      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
2036  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2037  *     | Type = 0xC1   |  Chunk Flags  |      Chunk Length             |
2038  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2039  *     |                       Serial Number                           |
2040  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2041  *     |                    Address Parameter                          |
2042  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2043  *     |                     ASCONF Parameter #1                       |
2044  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2045  *     \                                                               \
2046  *     /                             ....                              /
2047  *     \                                                               \
2048  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2049  *     |                     ASCONF Parameter #N                       |
2050  *      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2051  *
2052  * Address Parameter and other parameter will not be wrapped in this function 
2053  */
2054 struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
2055                                     union sctp_addr *addr, int vparam_len)
2056 {
2057         sctp_addiphdr_t asconf;
2058         struct sctp_chunk *retval;
2059         int length = sizeof(asconf) + vparam_len;
2060         union sctp_addr_param addrparam;
2061         int addrlen;
2062         struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2063
2064         addrlen = af->to_addr_param(addr, &addrparam);
2065         if (!addrlen)
2066                 return NULL;
2067         length += addrlen;
2068
2069         /* Create the chunk.  */
2070         retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF, 0, length);
2071         if (!retval)
2072                 return NULL;
2073
2074         asconf.serial = htonl(asoc->addip_serial++);
2075
2076         retval->subh.addip_hdr =
2077                 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2078         retval->param_hdr.v =
2079                 sctp_addto_chunk(retval, addrlen, &addrparam);
2080
2081         return retval;
2082 }
2083
2084 /* ADDIP
2085  * 3.2.1 Add IP Address
2086  *      0                   1                   2                   3
2087  *      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
2088  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2089  *     |        Type = 0xC001          |    Length = Variable          |
2090  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2091  *     |               ASCONF-Request Correlation ID                   |
2092  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2093  *     |                       Address Parameter                       |
2094  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2095  *
2096  * 3.2.2 Delete IP Address
2097  *      0                   1                   2                   3
2098  *      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
2099  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2100  *     |        Type = 0xC002          |    Length = Variable          |
2101  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2102  *     |               ASCONF-Request Correlation ID                   |
2103  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2104  *     |                       Address Parameter                       |
2105  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2106  *
2107  */
2108 struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
2109                                               union sctp_addr         *laddr,
2110                                               struct sockaddr         *addrs,
2111                                               int                     addrcnt,
2112                                               __u16                   flags)
2113 {
2114         sctp_addip_param_t      param;
2115         struct sctp_chunk       *retval;
2116         union sctp_addr_param   addr_param;
2117         union sctp_addr         *addr;
2118         void                    *addr_buf;
2119         struct sctp_af          *af;
2120         int                     paramlen = sizeof(param);
2121         int                     addr_param_len = 0;
2122         int                     totallen = 0;
2123         int                     i;
2124
2125         /* Get total length of all the address parameters. */
2126         addr_buf = addrs;
2127         for (i = 0; i < addrcnt; i++) {
2128                 addr = (union sctp_addr *)addr_buf;
2129                 af = sctp_get_af_specific(addr->v4.sin_family);
2130                 addr_param_len = af->to_addr_param(addr, &addr_param);
2131
2132                 totallen += paramlen;
2133                 totallen += addr_param_len;
2134
2135                 addr_buf += af->sockaddr_len;
2136         }
2137
2138         /* Create an asconf chunk with the required length. */
2139         retval = sctp_make_asconf(asoc, laddr, totallen);
2140         if (!retval)
2141                 return NULL;
2142
2143         /* Add the address parameters to the asconf chunk. */
2144         addr_buf = addrs;
2145         for (i = 0; i < addrcnt; i++) {
2146                 addr = (union sctp_addr *)addr_buf;
2147                 af = sctp_get_af_specific(addr->v4.sin_family);
2148                 addr_param_len = af->to_addr_param(addr, &addr_param);
2149                 param.param_hdr.type = flags;
2150                 param.param_hdr.length = htons(paramlen + addr_param_len);
2151                 param.crr_id = i;
2152
2153                 sctp_addto_chunk(retval, paramlen, &param);
2154                 sctp_addto_chunk(retval, addr_param_len, &addr_param);
2155
2156                 addr_buf += af->sockaddr_len;
2157         }
2158         return retval;
2159 }
2160
2161 /* ADDIP
2162  * 3.2.4 Set Primary IP Address
2163  *      0                   1                   2                   3
2164  *      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
2165  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2166  *     |        Type =0xC004           |    Length = Variable          |
2167  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2168  *     |               ASCONF-Request Correlation ID                   |
2169  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2170  *     |                       Address Parameter                       |
2171  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2172  *
2173  * Create an ASCONF chunk with Set Primary IP address parameter. 
2174  */
2175 struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
2176                                              union sctp_addr *addr)
2177 {
2178         sctp_addip_param_t      param;
2179         struct sctp_chunk       *retval;
2180         int                     len = sizeof(param);
2181         union sctp_addr_param   addrparam;
2182         int                     addrlen;
2183         struct sctp_af          *af = sctp_get_af_specific(addr->v4.sin_family);
2184
2185         addrlen = af->to_addr_param(addr, &addrparam);
2186         if (!addrlen)
2187                 return NULL;
2188         len += addrlen;
2189
2190         /* Create the chunk and make asconf header. */
2191         retval = sctp_make_asconf(asoc, addr, len);
2192         if (!retval)
2193                 return NULL;
2194
2195         param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
2196         param.param_hdr.length = htons(len);
2197         param.crr_id = 0;
2198
2199         sctp_addto_chunk(retval, sizeof(param), &param);
2200         sctp_addto_chunk(retval, addrlen, &addrparam);
2201
2202         return retval;
2203 }
2204
2205 /* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2206  *      0                   1                   2                   3
2207  *      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
2208  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2209  *     | Type = 0x80   |  Chunk Flags  |      Chunk Length             |
2210  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2211  *     |                       Serial Number                           |
2212  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2213  *     |                 ASCONF Parameter Response#1                   |
2214  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2215  *     \                                                               \
2216  *     /                             ....                              /
2217  *     \                                                               \
2218  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2219  *     |                 ASCONF Parameter Response#N                   |
2220  *     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2221  *
2222  * Create an ASCONF_ACK chunk with enough space for the parameter responses. 
2223  */
2224 struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
2225                                         __u32 serial, int vparam_len)
2226 {
2227         sctp_addiphdr_t         asconf;
2228         struct sctp_chunk       *retval;
2229         int                     length = sizeof(asconf) + vparam_len;
2230
2231         /* Create the chunk.  */
2232         retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF_ACK, 0, length);
2233         if (!retval)
2234                 return NULL;
2235
2236         asconf.serial = htonl(serial);
2237
2238         retval->subh.addip_hdr =
2239                 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2240
2241         return retval;
2242 }
2243
2244 /* Add response parameters to an ASCONF_ACK chunk. */
2245 static void sctp_add_asconf_response(struct sctp_chunk *chunk, __u32 crr_id,
2246                               __u16 err_code, sctp_addip_param_t *asconf_param)
2247 {
2248         sctp_addip_param_t      ack_param;
2249         sctp_errhdr_t           err_param;
2250         int                     asconf_param_len = 0;
2251         int                     err_param_len = 0;
2252         __u16                   response_type;
2253
2254         if (SCTP_ERROR_NO_ERROR == err_code) {
2255                 response_type = SCTP_PARAM_SUCCESS_REPORT;
2256         } else {
2257                 response_type = SCTP_PARAM_ERR_CAUSE;
2258                 err_param_len = sizeof(err_param);
2259                 if (asconf_param)
2260                         asconf_param_len =
2261                                  ntohs(asconf_param->param_hdr.length);
2262         }
2263
2264         /* Add Success Indication or Error Cause Indication parameter. */ 
2265         ack_param.param_hdr.type = response_type;
2266         ack_param.param_hdr.length = htons(sizeof(ack_param) +
2267                                            err_param_len +
2268                                            asconf_param_len);
2269         ack_param.crr_id = crr_id;
2270         sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
2271
2272         if (SCTP_ERROR_NO_ERROR == err_code)
2273                 return;
2274
2275         /* Add Error Cause parameter. */
2276         err_param.cause = err_code;
2277         err_param.length = htons(err_param_len + asconf_param_len);
2278         sctp_addto_chunk(chunk, err_param_len, &err_param);
2279
2280         /* Add the failed TLV copied from ASCONF chunk. */
2281         if (asconf_param)
2282                 sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
2283 }
2284
2285 /* Process a asconf parameter. */
2286 static __u16 sctp_process_asconf_param(struct sctp_association *asoc,
2287                                        struct sctp_chunk *asconf,
2288                                        sctp_addip_param_t *asconf_param)
2289 {
2290         struct sctp_transport *peer;
2291         struct sctp_af *af;
2292         union sctp_addr addr;
2293         struct list_head *pos;
2294         union sctp_addr_param *addr_param;
2295                                  
2296         addr_param = (union sctp_addr_param *)
2297                         ((void *)asconf_param + sizeof(sctp_addip_param_t));
2298
2299         af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2300         if (unlikely(!af))
2301                 return SCTP_ERROR_INV_PARAM;
2302
2303         af->from_addr_param(&addr, addr_param, asoc->peer.port, 0);
2304         switch (asconf_param->param_hdr.type) {
2305         case SCTP_PARAM_ADD_IP:
2306                 /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
2307                  * request and does not have the local resources to add this
2308                  * new address to the association, it MUST return an Error
2309                  * Cause TLV set to the new error code 'Operation Refused
2310                  * Due to Resource Shortage'.
2311                  */
2312
2313                 peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC);
2314                 if (!peer)
2315                         return SCTP_ERROR_RSRC_LOW;
2316
2317                 /* Start the heartbeat timer. */
2318                 if (!mod_timer(&peer->hb_timer, sctp_transport_timeout(peer)))
2319                         sctp_transport_hold(peer);
2320                 break;
2321         case SCTP_PARAM_DEL_IP:
2322                 /* ADDIP 4.3 D7) If a request is received to delete the
2323                  * last remaining IP address of a peer endpoint, the receiver
2324                  * MUST send an Error Cause TLV with the error cause set to the
2325                  * new error code 'Request to Delete Last Remaining IP Address'.
2326                  */
2327                 pos = asoc->peer.transport_addr_list.next;
2328                 if (pos->next == &asoc->peer.transport_addr_list)
2329                         return SCTP_ERROR_DEL_LAST_IP;
2330
2331                 /* ADDIP 4.3 D8) If a request is received to delete an IP
2332                  * address which is also the source address of the IP packet
2333                  * which contained the ASCONF chunk, the receiver MUST reject
2334                  * this request. To reject the request the receiver MUST send
2335                  * an Error Cause TLV set to the new error code 'Request to
2336                  * Delete Source IP Address'
2337                  */
2338                 if (sctp_cmp_addr_exact(sctp_source(asconf), &addr))
2339                         return SCTP_ERROR_DEL_SRC_IP;
2340
2341                 sctp_assoc_del_peer(asoc, &addr);
2342                 break;
2343         case SCTP_PARAM_SET_PRIMARY:
2344                 peer = sctp_assoc_lookup_paddr(asoc, &addr);
2345                 if (!peer)
2346                         return SCTP_ERROR_INV_PARAM;
2347
2348                 sctp_assoc_set_primary(asoc, peer);
2349                 break;
2350         default:
2351                 return SCTP_ERROR_INV_PARAM;
2352                 break;
2353         }
2354
2355         return SCTP_ERROR_NO_ERROR;
2356 }
2357
2358 /* Process an incoming ASCONF chunk with the next expected serial no. and 
2359  * return an ASCONF_ACK chunk to be sent in response.
2360  */
2361 struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
2362                                        struct sctp_chunk *asconf)
2363 {
2364         sctp_addiphdr_t         *hdr;
2365         union sctp_addr_param   *addr_param;
2366         sctp_addip_param_t      *asconf_param;
2367         struct sctp_chunk       *asconf_ack;
2368
2369         __u16   err_code;
2370         int     length = 0;
2371         int     chunk_len = asconf->skb->len;
2372         __u32   serial;
2373         int     all_param_pass = 1;
2374
2375         hdr = (sctp_addiphdr_t *)asconf->skb->data;
2376         serial = ntohl(hdr->serial);
2377
2378         /* Skip the addiphdr and store a pointer to address parameter.  */ 
2379         length = sizeof(sctp_addiphdr_t);
2380         addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
2381         chunk_len -= length;
2382
2383         /* Skip the address parameter and store a pointer to the first
2384          * asconf paramter.
2385          */ 
2386         length = ntohs(addr_param->v4.param_hdr.length);
2387         asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
2388         chunk_len -= length;
2389
2390         /* create an ASCONF_ACK chunk. 
2391          * Based on the definitions of parameters, we know that the size of
2392          * ASCONF_ACK parameters are less than or equal to the twice of ASCONF
2393          * paramters.
2394          */
2395         asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 2);
2396         if (!asconf_ack)
2397                 goto done;
2398
2399         /* Process the TLVs contained within the ASCONF chunk. */
2400         while (chunk_len > 0) {
2401                 err_code = sctp_process_asconf_param(asoc, asconf,
2402                                                      asconf_param);
2403                 /* ADDIP 4.1 A7)
2404                  * If an error response is received for a TLV parameter,
2405                  * all TLVs with no response before the failed TLV are
2406                  * considered successful if not reported.  All TLVs after
2407                  * the failed response are considered unsuccessful unless
2408                  * a specific success indication is present for the parameter.
2409                  */
2410                 if (SCTP_ERROR_NO_ERROR != err_code)
2411                         all_param_pass = 0;
2412
2413                 if (!all_param_pass)
2414                         sctp_add_asconf_response(asconf_ack,
2415                                                  asconf_param->crr_id, err_code,
2416                                                  asconf_param);
2417
2418                 /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
2419                  * an IP address sends an 'Out of Resource' in its response, it
2420                  * MUST also fail any subsequent add or delete requests bundled
2421                  * in the ASCONF. 
2422                  */
2423                 if (SCTP_ERROR_RSRC_LOW == err_code)
2424                         goto done;
2425
2426                 /* Move to the next ASCONF param. */
2427                 length = ntohs(asconf_param->param_hdr.length);
2428                 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
2429                                                       length);
2430                 chunk_len -= length;
2431         }
2432         
2433 done:
2434         asoc->peer.addip_serial++;
2435
2436         /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
2437          * after freeing the reference to old asconf ack if any. 
2438          */
2439         if (asconf_ack) {
2440                 if (asoc->addip_last_asconf_ack)
2441                         sctp_chunk_free(asoc->addip_last_asconf_ack);
2442
2443                 sctp_chunk_hold(asconf_ack);
2444                 asoc->addip_last_asconf_ack = asconf_ack;
2445         }
2446
2447         return asconf_ack;
2448 }
2449
2450 /* Process a asconf parameter that is successfully acked. */
2451 static int sctp_asconf_param_success(struct sctp_association *asoc,
2452                                      sctp_addip_param_t *asconf_param)
2453 {
2454         struct sctp_af *af;
2455         union sctp_addr addr;
2456         struct sctp_bind_addr *bp = &asoc->base.bind_addr;
2457         union sctp_addr_param *addr_param;
2458         int retval = 0;
2459
2460         addr_param = (union sctp_addr_param *)
2461                         ((void *)asconf_param + sizeof(sctp_addip_param_t));
2462
2463         /* We have checked the packet before, so we do not check again. */
2464         af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2465         af->from_addr_param(&addr, addr_param, bp->port, 0);
2466
2467         switch (asconf_param->param_hdr.type) {
2468         case SCTP_PARAM_ADD_IP:
2469                 sctp_local_bh_disable();
2470                 sctp_write_lock(&asoc->base.addr_lock);
2471                 retval = sctp_add_bind_addr(bp, &addr, GFP_ATOMIC);
2472                 sctp_write_unlock(&asoc->base.addr_lock);
2473                 sctp_local_bh_enable();
2474                 break;
2475         case SCTP_PARAM_DEL_IP:
2476                 sctp_local_bh_disable();
2477                 sctp_write_lock(&asoc->base.addr_lock);
2478                 retval = sctp_del_bind_addr(bp, &addr);
2479                 sctp_write_unlock(&asoc->base.addr_lock);
2480                 sctp_local_bh_enable();
2481                 break;
2482         default:
2483                 break;
2484         }
2485
2486         return retval;
2487 }
2488
2489 /* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
2490  * for the given asconf parameter.  If there is no response for this parameter,
2491  * return the error code based on the third argument 'no_err'. 
2492  * ADDIP 4.1
2493  * A7) If an error response is received for a TLV parameter, all TLVs with no
2494  * response before the failed TLV are considered successful if not reported.
2495  * All TLVs after the failed response are considered unsuccessful unless a
2496  * specific success indication is present for the parameter.
2497  */
2498 static __u16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
2499                                       sctp_addip_param_t *asconf_param,
2500                                       int no_err)
2501 {
2502         sctp_addip_param_t      *asconf_ack_param;
2503         sctp_errhdr_t           *err_param;
2504         int                     length;
2505         int                     asconf_ack_len = asconf_ack->skb->len;
2506         __u16                   err_code;
2507
2508         if (no_err)
2509                 err_code = SCTP_ERROR_NO_ERROR;
2510         else
2511                 err_code = SCTP_ERROR_REQ_REFUSED;
2512
2513         /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
2514          * the first asconf_ack parameter.
2515          */ 
2516         length = sizeof(sctp_addiphdr_t);
2517         asconf_ack_param = (sctp_addip_param_t *)(asconf_ack->skb->data +
2518                                                   length);
2519         asconf_ack_len -= length;
2520
2521         while (asconf_ack_len > 0) {
2522                 if (asconf_ack_param->crr_id == asconf_param->crr_id) {
2523                         switch(asconf_ack_param->param_hdr.type) {
2524                         case SCTP_PARAM_SUCCESS_REPORT:
2525                                 return SCTP_ERROR_NO_ERROR;
2526                         case SCTP_PARAM_ERR_CAUSE:
2527                                 length = sizeof(sctp_addip_param_t);
2528                                 err_param = (sctp_errhdr_t *)
2529                                            ((void *)asconf_ack_param + length);
2530                                 asconf_ack_len -= length;
2531                                 if (asconf_ack_len > 0)
2532                                         return err_param->cause;
2533                                 else
2534                                         return SCTP_ERROR_INV_PARAM;
2535                                 break;
2536                         default:
2537                                 return SCTP_ERROR_INV_PARAM;
2538                         }
2539                 }
2540
2541                 length = ntohs(asconf_ack_param->param_hdr.length);
2542                 asconf_ack_param = (sctp_addip_param_t *)
2543                                         ((void *)asconf_ack_param + length);
2544                 asconf_ack_len -= length;
2545         }
2546
2547         return err_code;
2548 }
2549
2550 /* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
2551 int sctp_process_asconf_ack(struct sctp_association *asoc,
2552                             struct sctp_chunk *asconf_ack)
2553 {
2554         struct sctp_chunk       *asconf = asoc->addip_last_asconf;
2555         union sctp_addr_param   *addr_param;
2556         sctp_addip_param_t      *asconf_param;
2557         int     length = 0;
2558         int     asconf_len = asconf->skb->len;
2559         int     all_param_pass = 0;
2560         int     no_err = 1;
2561         int     retval = 0;
2562         __u16   err_code = SCTP_ERROR_NO_ERROR;
2563
2564         /* Skip the chunkhdr and addiphdr from the last asconf sent and store
2565          * a pointer to address parameter.
2566          */ 
2567         length = sizeof(sctp_addip_chunk_t);
2568         addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
2569         asconf_len -= length;
2570
2571         /* Skip the address parameter in the last asconf sent and store a
2572          * pointer to the first asconf paramter.
2573          */ 
2574         length = ntohs(addr_param->v4.param_hdr.length);
2575         asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
2576         asconf_len -= length;
2577
2578         /* ADDIP 4.1
2579          * A8) If there is no response(s) to specific TLV parameter(s), and no
2580          * failures are indicated, then all request(s) are considered
2581          * successful.
2582          */
2583         if (asconf_ack->skb->len == sizeof(sctp_addiphdr_t))
2584                 all_param_pass = 1;
2585
2586         /* Process the TLVs contained in the last sent ASCONF chunk. */
2587         while (asconf_len > 0) {
2588                 if (all_param_pass)
2589                         err_code = SCTP_ERROR_NO_ERROR;
2590                 else {
2591                         err_code = sctp_get_asconf_response(asconf_ack,
2592                                                             asconf_param,
2593                                                             no_err);
2594                         if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
2595                                 no_err = 0;
2596                 }
2597
2598                 switch (err_code) {
2599                 case SCTP_ERROR_NO_ERROR:
2600                         retval = sctp_asconf_param_success(asoc, asconf_param);
2601                         break;
2602
2603                 case SCTP_ERROR_RSRC_LOW:
2604                         retval = 1;
2605                         break;
2606
2607                 case SCTP_ERROR_INV_PARAM:
2608                         /* Disable sending this type of asconf parameter in
2609                          * future.
2610                          */     
2611                         asoc->peer.addip_disabled_mask |=
2612                                 asconf_param->param_hdr.type;
2613                         break;
2614
2615                 case SCTP_ERROR_REQ_REFUSED:
2616                 case SCTP_ERROR_DEL_LAST_IP:
2617                 case SCTP_ERROR_DEL_SRC_IP:
2618                 default:
2619                          break;
2620                 }
2621
2622                 /* Skip the processed asconf parameter and move to the next
2623                  * one.
2624                  */ 
2625                 length = ntohs(asconf_param->param_hdr.length);
2626                 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
2627                                                       length);
2628                 asconf_len -= length;
2629         }
2630
2631         /* Free the cached last sent asconf chunk. */
2632         sctp_chunk_free(asconf);
2633         asoc->addip_last_asconf = NULL;
2634
2635         /* Send the next asconf chunk from the addip chunk queue. */
2636         asconf = (struct sctp_chunk *)__skb_dequeue(&asoc->addip_chunks);
2637         if (asconf) {
2638                 /* Hold the chunk until an ASCONF_ACK is received. */
2639                 sctp_chunk_hold(asconf);
2640                 if (sctp_primitive_ASCONF(asoc, asconf))
2641                         sctp_chunk_free(asconf);
2642                 else
2643                         asoc->addip_last_asconf = asconf;
2644         }
2645
2646         return retval;
2647 }
2648
2649 /* Make a FWD TSN chunk. */ 
2650 struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
2651                                     __u32 new_cum_tsn, size_t nstreams,
2652                                     struct sctp_fwdtsn_skip *skiplist)
2653 {
2654         struct sctp_chunk *retval = NULL;
2655         struct sctp_fwdtsn_chunk *ftsn_chunk;
2656         struct sctp_fwdtsn_hdr ftsn_hdr; 
2657         struct sctp_fwdtsn_skip skip;
2658         size_t hint;
2659         int i;
2660
2661         hint = (nstreams + 1) * sizeof(__u32);
2662
2663         /* Maybe set the T-bit if we have no association.  */
2664         retval = sctp_make_chunk(asoc, SCTP_CID_FWD_TSN, 0, hint);
2665
2666         if (!retval)
2667                 return NULL;
2668
2669         ftsn_chunk = (struct sctp_fwdtsn_chunk *)retval->subh.fwdtsn_hdr;
2670
2671         ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
2672         retval->subh.fwdtsn_hdr =
2673                 sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
2674
2675         for (i = 0; i < nstreams; i++) {
2676                 skip.stream = skiplist[i].stream;
2677                 skip.ssn = skiplist[i].ssn;
2678                 sctp_addto_chunk(retval, sizeof(skip), &skip);
2679         }
2680
2681         return retval;
2682 }