1 /* SCTP kernel 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.
7 * This file is part of the SCTP kernel implementation
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.
13 * This SCTP 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)
19 * This SCTP 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.
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.
30 * Please send any bug reports or fixes you make to the
32 * lksctp developers <lksctp-developers@lists.sourceforge.net>
34 * Or submit a bug report through the following website:
35 * http://www.sf.net/projects/lksctp
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>
49 * Any bugs reported given to us we will try to fix... any fixes shared will
50 * be incorporated into the next SCTP release.
53 #include <linux/types.h>
54 #include <linux/kernel.h>
56 #include <linux/ipv6.h>
57 #include <linux/net.h>
58 #include <linux/inet.h>
59 #include <linux/scatterlist.h>
60 #include <linux/crypto.h>
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>
69 struct sctp_chunk
*sctp_make_chunk(const struct sctp_association
*asoc
,
70 __u8 type
, __u8 flags
, int paylen
);
71 static sctp_cookie_param_t
*sctp_pack_cookie(const struct sctp_endpoint
*ep
,
72 const struct sctp_association
*asoc
,
73 const struct sctp_chunk
*init_chunk
,
75 const __u8
*raw_addrs
, int addrs_len
);
76 static int sctp_process_param(struct sctp_association
*asoc
,
77 union sctp_params param
,
78 const union sctp_addr
*peer_addr
,
80 static void *sctp_addto_param(struct sctp_chunk
*chunk
, int len
,
83 /* What was the inbound interface for this chunk? */
84 int sctp_chunk_iif(const struct sctp_chunk
*chunk
)
89 af
= sctp_get_af_specific(ipver2af(ip_hdr(chunk
->skb
)->version
));
91 iif
= af
->skb_iif(chunk
->skb
);
96 /* RFC 2960 3.3.2 Initiation (INIT) (1)
98 * Note 2: The ECN capable field is reserved for future use of
99 * Explicit Congestion Notification.
101 static const struct sctp_paramhdr ecap_param
= {
102 SCTP_PARAM_ECN_CAPABLE
,
103 __constant_htons(sizeof(struct sctp_paramhdr
)),
105 static const struct sctp_paramhdr prsctp_param
= {
106 SCTP_PARAM_FWD_TSN_SUPPORT
,
107 __constant_htons(sizeof(struct sctp_paramhdr
)),
110 /* A helper to initialize to initialize an op error inside a
111 * provided chunk, as most cause codes will be embedded inside an
114 void sctp_init_cause(struct sctp_chunk
*chunk
, __be16 cause_code
,
120 /* Cause code constants are now defined in network order. */
121 err
.cause
= cause_code
;
122 len
= sizeof(sctp_errhdr_t
) + paylen
;
123 err
.length
= htons(len
);
124 chunk
->subh
.err_hdr
= sctp_addto_chunk(chunk
, sizeof(sctp_errhdr_t
), &err
);
127 /* 3.3.2 Initiation (INIT) (1)
129 * This chunk is used to initiate a SCTP association between two
130 * endpoints. The format of the INIT chunk is shown below:
133 * 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
134 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
135 * | Type = 1 | Chunk Flags | Chunk Length |
136 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
138 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
139 * | Advertised Receiver Window Credit (a_rwnd) |
140 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
141 * | Number of Outbound Streams | Number of Inbound Streams |
142 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
144 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
146 * / Optional/Variable-Length Parameters /
148 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
151 * The INIT chunk contains the following parameters. Unless otherwise
152 * noted, each parameter MUST only be included once in the INIT chunk.
154 * Fixed Parameters Status
155 * ----------------------------------------------
156 * Initiate Tag Mandatory
157 * Advertised Receiver Window Credit Mandatory
158 * Number of Outbound Streams Mandatory
159 * Number of Inbound Streams Mandatory
160 * Initial TSN Mandatory
162 * Variable Parameters Status Type Value
163 * -------------------------------------------------------------
164 * IPv4 Address (Note 1) Optional 5
165 * IPv6 Address (Note 1) Optional 6
166 * Cookie Preservative Optional 9
167 * Reserved for ECN Capable (Note 2) Optional 32768 (0x8000)
168 * Host Name Address (Note 3) Optional 11
169 * Supported Address Types (Note 4) Optional 12
171 struct sctp_chunk
*sctp_make_init(const struct sctp_association
*asoc
,
172 const struct sctp_bind_addr
*bp
,
173 gfp_t gfp
, int vparam_len
)
176 union sctp_params addrs
;
178 struct sctp_chunk
*retval
= NULL
;
179 int num_types
, addrs_len
= 0;
180 struct sctp_sock
*sp
;
181 sctp_supported_addrs_param_t sat
;
183 sctp_adaptation_ind_param_t aiparam
;
184 sctp_supported_ext_param_t ext_param
;
187 sctp_paramhdr_t
*auth_chunks
= NULL
,
190 /* RFC 2960 3.3.2 Initiation (INIT) (1)
192 * Note 1: The INIT chunks can contain multiple addresses that
193 * can be IPv4 and/or IPv6 in any combination.
197 /* Convert the provided bind address list to raw format. */
198 addrs
= sctp_bind_addrs_to_raw(bp
, &addrs_len
, gfp
);
200 init
.init_tag
= htonl(asoc
->c
.my_vtag
);
201 init
.a_rwnd
= htonl(asoc
->rwnd
);
202 init
.num_outbound_streams
= htons(asoc
->c
.sinit_num_ostreams
);
203 init
.num_inbound_streams
= htons(asoc
->c
.sinit_max_instreams
);
204 init
.initial_tsn
= htonl(asoc
->c
.initial_tsn
);
206 /* How many address types are needed? */
207 sp
= sctp_sk(asoc
->base
.sk
);
208 num_types
= sp
->pf
->supported_addrs(sp
, types
);
210 chunksize
= sizeof(init
) + addrs_len
+ SCTP_SAT_LEN(num_types
);
211 chunksize
+= sizeof(ecap_param
);
213 if (sctp_prsctp_enable
)
214 chunksize
+= sizeof(prsctp_param
);
216 /* ADDIP: Section 4.2.7:
217 * An implementation supporting this extension [ADDIP] MUST list
218 * the ASCONF,the ASCONF-ACK, and the AUTH chunks in its INIT and
219 * INIT-ACK parameters.
221 if (sctp_addip_enable
) {
222 extensions
[num_ext
] = SCTP_CID_ASCONF
;
223 extensions
[num_ext
+1] = SCTP_CID_ASCONF_ACK
;
227 chunksize
+= sizeof(aiparam
);
228 chunksize
+= vparam_len
;
230 /* Account for AUTH related parameters */
231 if (sctp_auth_enable
) {
232 /* Add random parameter length*/
233 chunksize
+= sizeof(asoc
->c
.auth_random
);
235 /* Add HMACS parameter length if any were defined */
236 auth_hmacs
= (sctp_paramhdr_t
*)asoc
->c
.auth_hmacs
;
237 if (auth_hmacs
->length
)
238 chunksize
+= ntohs(auth_hmacs
->length
);
242 /* Add CHUNKS parameter length */
243 auth_chunks
= (sctp_paramhdr_t
*)asoc
->c
.auth_chunks
;
244 if (auth_chunks
->length
)
245 chunksize
+= ntohs(auth_chunks
->length
);
249 extensions
[num_ext
] = SCTP_CID_AUTH
;
253 /* If we have any extensions to report, account for that */
255 chunksize
+= sizeof(sctp_supported_ext_param_t
) + num_ext
;
257 /* RFC 2960 3.3.2 Initiation (INIT) (1)
259 * Note 3: An INIT chunk MUST NOT contain more than one Host
260 * Name address parameter. Moreover, the sender of the INIT
261 * MUST NOT combine any other address types with the Host Name
262 * address in the INIT. The receiver of INIT MUST ignore any
263 * other address types if the Host Name address parameter is
264 * present in the received INIT chunk.
266 * PLEASE DO NOT FIXME [This version does not support Host Name.]
269 retval
= sctp_make_chunk(asoc
, SCTP_CID_INIT
, 0, chunksize
);
273 retval
->subh
.init_hdr
=
274 sctp_addto_chunk(retval
, sizeof(init
), &init
);
275 retval
->param_hdr
.v
=
276 sctp_addto_chunk(retval
, addrs_len
, addrs
.v
);
278 /* RFC 2960 3.3.2 Initiation (INIT) (1)
280 * Note 4: This parameter, when present, specifies all the
281 * address types the sending endpoint can support. The absence
282 * of this parameter indicates that the sending endpoint can
283 * support any address type.
285 sat
.param_hdr
.type
= SCTP_PARAM_SUPPORTED_ADDRESS_TYPES
;
286 sat
.param_hdr
.length
= htons(SCTP_SAT_LEN(num_types
));
287 sctp_addto_chunk(retval
, sizeof(sat
), &sat
);
288 sctp_addto_chunk(retval
, num_types
* sizeof(__u16
), &types
);
290 sctp_addto_chunk(retval
, sizeof(ecap_param
), &ecap_param
);
292 /* Add the supported extensions parameter. Be nice and add this
293 * fist before addiding the parameters for the extensions themselves
296 ext_param
.param_hdr
.type
= SCTP_PARAM_SUPPORTED_EXT
;
297 ext_param
.param_hdr
.length
=
298 htons(sizeof(sctp_supported_ext_param_t
) + num_ext
);
299 sctp_addto_chunk(retval
, sizeof(sctp_supported_ext_param_t
),
301 sctp_addto_param(retval
, num_ext
, extensions
);
304 if (sctp_prsctp_enable
)
305 sctp_addto_chunk(retval
, sizeof(prsctp_param
), &prsctp_param
);
307 aiparam
.param_hdr
.type
= SCTP_PARAM_ADAPTATION_LAYER_IND
;
308 aiparam
.param_hdr
.length
= htons(sizeof(aiparam
));
309 aiparam
.adaptation_ind
= htonl(sp
->adaptation_ind
);
310 sctp_addto_chunk(retval
, sizeof(aiparam
), &aiparam
);
312 /* Add SCTP-AUTH chunks to the parameter list */
313 if (sctp_auth_enable
) {
314 sctp_addto_chunk(retval
, sizeof(asoc
->c
.auth_random
),
315 asoc
->c
.auth_random
);
317 sctp_addto_chunk(retval
, ntohs(auth_hmacs
->length
),
320 sctp_addto_chunk(retval
, ntohs(auth_chunks
->length
),
328 struct sctp_chunk
*sctp_make_init_ack(const struct sctp_association
*asoc
,
329 const struct sctp_chunk
*chunk
,
330 gfp_t gfp
, int unkparam_len
)
332 sctp_inithdr_t initack
;
333 struct sctp_chunk
*retval
;
334 union sctp_params addrs
;
336 sctp_cookie_param_t
*cookie
;
339 sctp_adaptation_ind_param_t aiparam
;
340 sctp_supported_ext_param_t ext_param
;
343 sctp_paramhdr_t
*auth_chunks
= NULL
,
349 /* Note: there may be no addresses to embed. */
350 addrs
= sctp_bind_addrs_to_raw(&asoc
->base
.bind_addr
, &addrs_len
, gfp
);
352 initack
.init_tag
= htonl(asoc
->c
.my_vtag
);
353 initack
.a_rwnd
= htonl(asoc
->rwnd
);
354 initack
.num_outbound_streams
= htons(asoc
->c
.sinit_num_ostreams
);
355 initack
.num_inbound_streams
= htons(asoc
->c
.sinit_max_instreams
);
356 initack
.initial_tsn
= htonl(asoc
->c
.initial_tsn
);
358 /* FIXME: We really ought to build the cookie right
359 * into the packet instead of allocating more fresh memory.
361 cookie
= sctp_pack_cookie(asoc
->ep
, asoc
, chunk
, &cookie_len
,
366 /* Calculate the total size of allocation, include the reserved
367 * space for reporting unknown parameters if it is specified.
369 chunksize
= sizeof(initack
) + addrs_len
+ cookie_len
+ unkparam_len
;
371 /* Tell peer that we'll do ECN only if peer advertised such cap. */
372 if (asoc
->peer
.ecn_capable
)
373 chunksize
+= sizeof(ecap_param
);
375 if (sctp_prsctp_enable
)
376 chunksize
+= sizeof(prsctp_param
);
378 if (sctp_addip_enable
) {
379 extensions
[num_ext
] = SCTP_CID_ASCONF
;
380 extensions
[num_ext
+1] = SCTP_CID_ASCONF_ACK
;
384 chunksize
+= sizeof(aiparam
);
386 if (asoc
->peer
.auth_capable
) {
387 auth_random
= (sctp_paramhdr_t
*)asoc
->c
.auth_random
;
388 chunksize
+= ntohs(auth_random
->length
);
390 auth_hmacs
= (sctp_paramhdr_t
*)asoc
->c
.auth_hmacs
;
391 if (auth_hmacs
->length
)
392 chunksize
+= ntohs(auth_hmacs
->length
);
396 auth_chunks
= (sctp_paramhdr_t
*)asoc
->c
.auth_chunks
;
397 if (auth_chunks
->length
)
398 chunksize
+= ntohs(auth_chunks
->length
);
402 extensions
[num_ext
] = SCTP_CID_AUTH
;
407 chunksize
+= sizeof(sctp_supported_ext_param_t
) + num_ext
;
409 /* Now allocate and fill out the chunk. */
410 retval
= sctp_make_chunk(asoc
, SCTP_CID_INIT_ACK
, 0, chunksize
);
414 /* Per the advice in RFC 2960 6.4, send this reply to
415 * the source of the INIT packet.
417 retval
->transport
= chunk
->transport
;
418 retval
->subh
.init_hdr
=
419 sctp_addto_chunk(retval
, sizeof(initack
), &initack
);
420 retval
->param_hdr
.v
= sctp_addto_chunk(retval
, addrs_len
, addrs
.v
);
421 sctp_addto_chunk(retval
, cookie_len
, cookie
);
422 if (asoc
->peer
.ecn_capable
)
423 sctp_addto_chunk(retval
, sizeof(ecap_param
), &ecap_param
);
425 ext_param
.param_hdr
.type
= SCTP_PARAM_SUPPORTED_EXT
;
426 ext_param
.param_hdr
.length
=
427 htons(sizeof(sctp_supported_ext_param_t
) + num_ext
);
428 sctp_addto_chunk(retval
, sizeof(sctp_supported_ext_param_t
),
430 sctp_addto_param(retval
, num_ext
, extensions
);
432 if (asoc
->peer
.prsctp_capable
)
433 sctp_addto_chunk(retval
, sizeof(prsctp_param
), &prsctp_param
);
435 aiparam
.param_hdr
.type
= SCTP_PARAM_ADAPTATION_LAYER_IND
;
436 aiparam
.param_hdr
.length
= htons(sizeof(aiparam
));
437 aiparam
.adaptation_ind
= htonl(sctp_sk(asoc
->base
.sk
)->adaptation_ind
);
438 sctp_addto_chunk(retval
, sizeof(aiparam
), &aiparam
);
440 if (asoc
->peer
.auth_capable
) {
441 sctp_addto_chunk(retval
, ntohs(auth_random
->length
),
444 sctp_addto_chunk(retval
, ntohs(auth_hmacs
->length
),
447 sctp_addto_chunk(retval
, ntohs(auth_chunks
->length
),
451 /* We need to remove the const qualifier at this point. */
452 retval
->asoc
= (struct sctp_association
*) asoc
;
454 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
456 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
457 * HEARTBEAT ACK, * etc.) to the same destination transport
458 * address from which it received the DATA or control chunk
459 * to which it is replying.
461 * [INIT ACK back to where the INIT came from.]
464 retval
->transport
= chunk
->transport
;
473 /* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
475 * This chunk is used only during the initialization of an association.
476 * It is sent by the initiator of an association to its peer to complete
477 * the initialization process. This chunk MUST precede any DATA chunk
478 * sent within the association, but MAY be bundled with one or more DATA
479 * chunks in the same packet.
482 * 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
483 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
484 * | Type = 10 |Chunk Flags | Length |
485 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
488 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
492 * Set to zero on transmit and ignored on receipt.
494 * Length: 16 bits (unsigned integer)
496 * Set to the size of the chunk in bytes, including the 4 bytes of
497 * the chunk header and the size of the Cookie.
499 * Cookie: variable size
501 * This field must contain the exact cookie received in the
502 * State Cookie parameter from the previous INIT ACK.
504 * An implementation SHOULD make the cookie as small as possible
505 * to insure interoperability.
507 struct sctp_chunk
*sctp_make_cookie_echo(const struct sctp_association
*asoc
,
508 const struct sctp_chunk
*chunk
)
510 struct sctp_chunk
*retval
;
514 cookie
= asoc
->peer
.cookie
;
515 cookie_len
= asoc
->peer
.cookie_len
;
517 /* Build a cookie echo chunk. */
518 retval
= sctp_make_chunk(asoc
, SCTP_CID_COOKIE_ECHO
, 0, cookie_len
);
521 retval
->subh
.cookie_hdr
=
522 sctp_addto_chunk(retval
, cookie_len
, cookie
);
524 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
526 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
527 * HEARTBEAT ACK, * etc.) to the same destination transport
528 * address from which it * received the DATA or control chunk
529 * to which it is replying.
531 * [COOKIE ECHO back to where the INIT ACK came from.]
534 retval
->transport
= chunk
->transport
;
540 /* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
542 * This chunk is used only during the initialization of an
543 * association. It is used to acknowledge the receipt of a COOKIE
544 * ECHO chunk. This chunk MUST precede any DATA or SACK chunk sent
545 * within the association, but MAY be bundled with one or more DATA
546 * chunks or SACK chunk in the same SCTP packet.
549 * 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
550 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
551 * | Type = 11 |Chunk Flags | Length = 4 |
552 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
554 * Chunk Flags: 8 bits
556 * Set to zero on transmit and ignored on receipt.
558 struct sctp_chunk
*sctp_make_cookie_ack(const struct sctp_association
*asoc
,
559 const struct sctp_chunk
*chunk
)
561 struct sctp_chunk
*retval
;
563 retval
= sctp_make_chunk(asoc
, SCTP_CID_COOKIE_ACK
, 0, 0);
565 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
567 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
568 * HEARTBEAT ACK, * etc.) to the same destination transport
569 * address from which it * received the DATA or control chunk
570 * to which it is replying.
572 * [COOKIE ACK back to where the COOKIE ECHO came from.]
575 retval
->transport
= chunk
->transport
;
581 * Appendix A: Explicit Congestion Notification:
584 * RFC 2481 details a specific bit for a sender to send in the header of
585 * its next outbound TCP segment to indicate to its peer that it has
586 * reduced its congestion window. This is termed the CWR bit. For
587 * SCTP the same indication is made by including the CWR chunk.
588 * This chunk contains one data element, i.e. the TSN number that
589 * was sent in the ECNE chunk. This element represents the lowest
590 * TSN number in the datagram that was originally marked with the
594 * 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
595 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
596 * | Chunk Type=13 | Flags=00000000| Chunk Length = 8 |
597 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
598 * | Lowest TSN Number |
599 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
601 * Note: The CWR is considered a Control chunk.
603 struct sctp_chunk
*sctp_make_cwr(const struct sctp_association
*asoc
,
604 const __u32 lowest_tsn
,
605 const struct sctp_chunk
*chunk
)
607 struct sctp_chunk
*retval
;
610 cwr
.lowest_tsn
= htonl(lowest_tsn
);
611 retval
= sctp_make_chunk(asoc
, SCTP_CID_ECN_CWR
, 0,
612 sizeof(sctp_cwrhdr_t
));
617 retval
->subh
.ecn_cwr_hdr
=
618 sctp_addto_chunk(retval
, sizeof(cwr
), &cwr
);
620 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
622 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
623 * HEARTBEAT ACK, * etc.) to the same destination transport
624 * address from which it * received the DATA or control chunk
625 * to which it is replying.
627 * [Report a reduced congestion window back to where the ECNE
631 retval
->transport
= chunk
->transport
;
637 /* Make an ECNE chunk. This is a congestion experienced report. */
638 struct sctp_chunk
*sctp_make_ecne(const struct sctp_association
*asoc
,
639 const __u32 lowest_tsn
)
641 struct sctp_chunk
*retval
;
644 ecne
.lowest_tsn
= htonl(lowest_tsn
);
645 retval
= sctp_make_chunk(asoc
, SCTP_CID_ECN_ECNE
, 0,
646 sizeof(sctp_ecnehdr_t
));
649 retval
->subh
.ecne_hdr
=
650 sctp_addto_chunk(retval
, sizeof(ecne
), &ecne
);
656 /* Make a DATA chunk for the given association from the provided
657 * parameters. However, do not populate the data payload.
659 struct sctp_chunk
*sctp_make_datafrag_empty(struct sctp_association
*asoc
,
660 const struct sctp_sndrcvinfo
*sinfo
,
661 int data_len
, __u8 flags
, __u16 ssn
)
663 struct sctp_chunk
*retval
;
664 struct sctp_datahdr dp
;
667 /* We assign the TSN as LATE as possible, not here when
668 * creating the chunk.
671 dp
.stream
= htons(sinfo
->sinfo_stream
);
672 dp
.ppid
= sinfo
->sinfo_ppid
;
674 /* Set the flags for an unordered send. */
675 if (sinfo
->sinfo_flags
& SCTP_UNORDERED
) {
676 flags
|= SCTP_DATA_UNORDERED
;
681 chunk_len
= sizeof(dp
) + data_len
;
682 retval
= sctp_make_chunk(asoc
, SCTP_CID_DATA
, flags
, chunk_len
);
686 retval
->subh
.data_hdr
= sctp_addto_chunk(retval
, sizeof(dp
), &dp
);
687 memcpy(&retval
->sinfo
, sinfo
, sizeof(struct sctp_sndrcvinfo
));
693 /* Create a selective ackowledgement (SACK) for the given
694 * association. This reports on which TSN's we've seen to date,
695 * including duplicates and gaps.
697 struct sctp_chunk
*sctp_make_sack(const struct sctp_association
*asoc
)
699 struct sctp_chunk
*retval
;
700 struct sctp_sackhdr sack
;
703 __u16 num_gabs
, num_dup_tsns
;
704 struct sctp_tsnmap
*map
= (struct sctp_tsnmap
*)&asoc
->peer
.tsn_map
;
706 ctsn
= sctp_tsnmap_get_ctsn(map
);
707 SCTP_DEBUG_PRINTK("sackCTSNAck sent: 0x%x.\n", ctsn
);
709 /* How much room is needed in the chunk? */
710 num_gabs
= sctp_tsnmap_num_gabs(map
);
711 num_dup_tsns
= sctp_tsnmap_num_dups(map
);
713 /* Initialize the SACK header. */
714 sack
.cum_tsn_ack
= htonl(ctsn
);
715 sack
.a_rwnd
= htonl(asoc
->a_rwnd
);
716 sack
.num_gap_ack_blocks
= htons(num_gabs
);
717 sack
.num_dup_tsns
= htons(num_dup_tsns
);
720 + sizeof(struct sctp_gap_ack_block
) * num_gabs
721 + sizeof(__u32
) * num_dup_tsns
;
723 /* Create the chunk. */
724 retval
= sctp_make_chunk(asoc
, SCTP_CID_SACK
, 0, len
);
728 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
730 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
731 * HEARTBEAT ACK, etc.) to the same destination transport
732 * address from which it received the DATA or control chunk to
733 * which it is replying. This rule should also be followed if
734 * the endpoint is bundling DATA chunks together with the
737 * However, when acknowledging multiple DATA chunks received
738 * in packets from different source addresses in a single
739 * SACK, the SACK chunk may be transmitted to one of the
740 * destination transport addresses from which the DATA or
741 * control chunks being acknowledged were received.
743 * [BUG: We do not implement the following paragraph.
744 * Perhaps we should remember the last transport we used for a
745 * SACK and avoid that (if possible) if we have seen any
746 * duplicates. --piggy]
748 * When a receiver of a duplicate DATA chunk sends a SACK to a
749 * multi- homed endpoint it MAY be beneficial to vary the
750 * destination address and not use the source address of the
751 * DATA chunk. The reason being that receiving a duplicate
752 * from a multi-homed endpoint might indicate that the return
753 * path (as specified in the source address of the DATA chunk)
754 * for the SACK is broken.
756 * [Send to the address from which we last received a DATA chunk.]
758 retval
->transport
= asoc
->peer
.last_data_from
;
760 retval
->subh
.sack_hdr
=
761 sctp_addto_chunk(retval
, sizeof(sack
), &sack
);
763 /* Add the gap ack block information. */
765 sctp_addto_chunk(retval
, sizeof(__u32
) * num_gabs
,
766 sctp_tsnmap_get_gabs(map
));
768 /* Add the duplicate TSN information. */
770 sctp_addto_chunk(retval
, sizeof(__u32
) * num_dup_tsns
,
771 sctp_tsnmap_get_dups(map
));
777 /* Make a SHUTDOWN chunk. */
778 struct sctp_chunk
*sctp_make_shutdown(const struct sctp_association
*asoc
,
779 const struct sctp_chunk
*chunk
)
781 struct sctp_chunk
*retval
;
782 sctp_shutdownhdr_t shut
;
785 ctsn
= sctp_tsnmap_get_ctsn(&asoc
->peer
.tsn_map
);
786 shut
.cum_tsn_ack
= htonl(ctsn
);
788 retval
= sctp_make_chunk(asoc
, SCTP_CID_SHUTDOWN
, 0,
789 sizeof(sctp_shutdownhdr_t
));
793 retval
->subh
.shutdown_hdr
=
794 sctp_addto_chunk(retval
, sizeof(shut
), &shut
);
797 retval
->transport
= chunk
->transport
;
802 struct sctp_chunk
*sctp_make_shutdown_ack(const struct sctp_association
*asoc
,
803 const struct sctp_chunk
*chunk
)
805 struct sctp_chunk
*retval
;
807 retval
= sctp_make_chunk(asoc
, SCTP_CID_SHUTDOWN_ACK
, 0, 0);
809 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
811 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
812 * HEARTBEAT ACK, * etc.) to the same destination transport
813 * address from which it * received the DATA or control chunk
814 * to which it is replying.
816 * [ACK back to where the SHUTDOWN came from.]
819 retval
->transport
= chunk
->transport
;
824 struct sctp_chunk
*sctp_make_shutdown_complete(
825 const struct sctp_association
*asoc
,
826 const struct sctp_chunk
*chunk
)
828 struct sctp_chunk
*retval
;
831 /* Set the T-bit if we have no association (vtag will be
834 flags
|= asoc
? 0 : SCTP_CHUNK_FLAG_T
;
836 retval
= sctp_make_chunk(asoc
, SCTP_CID_SHUTDOWN_COMPLETE
, flags
, 0);
838 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
840 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
841 * HEARTBEAT ACK, * etc.) to the same destination transport
842 * address from which it * received the DATA or control chunk
843 * to which it is replying.
845 * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
849 retval
->transport
= chunk
->transport
;
854 /* Create an ABORT. Note that we set the T bit if we have no
855 * association, except when responding to an INIT (sctpimpguide 2.41).
857 struct sctp_chunk
*sctp_make_abort(const struct sctp_association
*asoc
,
858 const struct sctp_chunk
*chunk
,
861 struct sctp_chunk
*retval
;
864 /* Set the T-bit if we have no association and 'chunk' is not
865 * an INIT (vtag will be reflected).
868 if (chunk
&& chunk
->chunk_hdr
&&
869 chunk
->chunk_hdr
->type
== SCTP_CID_INIT
)
872 flags
= SCTP_CHUNK_FLAG_T
;
875 retval
= sctp_make_chunk(asoc
, SCTP_CID_ABORT
, flags
, hint
);
877 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
879 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
880 * HEARTBEAT ACK, * etc.) to the same destination transport
881 * address from which it * received the DATA or control chunk
882 * to which it is replying.
884 * [ABORT back to where the offender came from.]
887 retval
->transport
= chunk
->transport
;
892 /* Helper to create ABORT with a NO_USER_DATA error. */
893 struct sctp_chunk
*sctp_make_abort_no_data(
894 const struct sctp_association
*asoc
,
895 const struct sctp_chunk
*chunk
, __u32 tsn
)
897 struct sctp_chunk
*retval
;
900 retval
= sctp_make_abort(asoc
, chunk
, sizeof(sctp_errhdr_t
)
906 /* Put the tsn back into network byte order. */
907 payload
= htonl(tsn
);
908 sctp_init_cause(retval
, SCTP_ERROR_NO_DATA
, sizeof(payload
));
909 sctp_addto_chunk(retval
, sizeof(payload
), (const void *)&payload
);
911 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
913 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
914 * HEARTBEAT ACK, * etc.) to the same destination transport
915 * address from which it * received the DATA or control chunk
916 * to which it is replying.
918 * [ABORT back to where the offender came from.]
921 retval
->transport
= chunk
->transport
;
927 /* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error. */
928 struct sctp_chunk
*sctp_make_abort_user(const struct sctp_association
*asoc
,
929 const struct msghdr
*msg
,
932 struct sctp_chunk
*retval
;
933 void *payload
= NULL
;
936 retval
= sctp_make_abort(asoc
, NULL
, sizeof(sctp_errhdr_t
) + paylen
);
941 /* Put the msg_iov together into payload. */
942 payload
= kmalloc(paylen
, GFP_KERNEL
);
946 err
= memcpy_fromiovec(payload
, msg
->msg_iov
, paylen
);
951 sctp_init_cause(retval
, SCTP_ERROR_USER_ABORT
, paylen
);
952 sctp_addto_chunk(retval
, paylen
, payload
);
962 sctp_chunk_free(retval
);
968 /* Append bytes to the end of a parameter. Will panic if chunk is not big
971 static void *sctp_addto_param(struct sctp_chunk
*chunk
, int len
,
975 int chunklen
= ntohs(chunk
->chunk_hdr
->length
);
977 target
= skb_put(chunk
->skb
, len
);
979 memcpy(target
, data
, len
);
981 /* Adjust the chunk length field. */
982 chunk
->chunk_hdr
->length
= htons(chunklen
+ len
);
983 chunk
->chunk_end
= skb_tail_pointer(chunk
->skb
);
988 /* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
989 struct sctp_chunk
*sctp_make_abort_violation(
990 const struct sctp_association
*asoc
,
991 const struct sctp_chunk
*chunk
,
995 struct sctp_chunk
*retval
;
996 struct sctp_paramhdr phdr
;
998 retval
= sctp_make_abort(asoc
, chunk
, sizeof(sctp_errhdr_t
) + paylen
999 + sizeof(sctp_paramhdr_t
));
1003 sctp_init_cause(retval
, SCTP_ERROR_PROTO_VIOLATION
, paylen
1004 + sizeof(sctp_paramhdr_t
));
1006 phdr
.type
= htons(chunk
->chunk_hdr
->type
);
1007 phdr
.length
= chunk
->chunk_hdr
->length
;
1008 sctp_addto_chunk(retval
, paylen
, payload
);
1009 sctp_addto_param(retval
, sizeof(sctp_paramhdr_t
), &phdr
);
1015 /* Make a HEARTBEAT chunk. */
1016 struct sctp_chunk
*sctp_make_heartbeat(const struct sctp_association
*asoc
,
1017 const struct sctp_transport
*transport
,
1018 const void *payload
, const size_t paylen
)
1020 struct sctp_chunk
*retval
= sctp_make_chunk(asoc
, SCTP_CID_HEARTBEAT
,
1026 /* Cast away the 'const', as this is just telling the chunk
1027 * what transport it belongs to.
1029 retval
->transport
= (struct sctp_transport
*) transport
;
1030 retval
->subh
.hbs_hdr
= sctp_addto_chunk(retval
, paylen
, payload
);
1036 struct sctp_chunk
*sctp_make_heartbeat_ack(const struct sctp_association
*asoc
,
1037 const struct sctp_chunk
*chunk
,
1038 const void *payload
, const size_t paylen
)
1040 struct sctp_chunk
*retval
;
1042 retval
= sctp_make_chunk(asoc
, SCTP_CID_HEARTBEAT_ACK
, 0, paylen
);
1046 retval
->subh
.hbs_hdr
= sctp_addto_chunk(retval
, paylen
, payload
);
1048 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1050 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1051 * HEARTBEAT ACK, * etc.) to the same destination transport
1052 * address from which it * received the DATA or control chunk
1053 * to which it is replying.
1055 * [HBACK back to where the HEARTBEAT came from.]
1058 retval
->transport
= chunk
->transport
;
1064 /* Create an Operation Error chunk with the specified space reserved.
1065 * This routine can be used for containing multiple causes in the chunk.
1067 static struct sctp_chunk
*sctp_make_op_error_space(
1068 const struct sctp_association
*asoc
,
1069 const struct sctp_chunk
*chunk
,
1072 struct sctp_chunk
*retval
;
1074 retval
= sctp_make_chunk(asoc
, SCTP_CID_ERROR
, 0,
1075 sizeof(sctp_errhdr_t
) + size
);
1079 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1081 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1082 * HEARTBEAT ACK, etc.) to the same destination transport
1083 * address from which it received the DATA or control chunk
1084 * to which it is replying.
1088 retval
->transport
= chunk
->transport
;
1094 /* Create an Operation Error chunk. */
1095 struct sctp_chunk
*sctp_make_op_error(const struct sctp_association
*asoc
,
1096 const struct sctp_chunk
*chunk
,
1097 __be16 cause_code
, const void *payload
,
1100 struct sctp_chunk
*retval
;
1102 retval
= sctp_make_op_error_space(asoc
, chunk
, paylen
);
1106 sctp_init_cause(retval
, cause_code
, paylen
);
1107 sctp_addto_chunk(retval
, paylen
, payload
);
1113 struct sctp_chunk
*sctp_make_auth(const struct sctp_association
*asoc
)
1115 struct sctp_chunk
*retval
;
1116 struct sctp_hmac
*hmac_desc
;
1117 struct sctp_authhdr auth_hdr
;
1120 /* Get the first hmac that the peer told us to use */
1121 hmac_desc
= sctp_auth_asoc_get_hmac(asoc
);
1122 if (unlikely(!hmac_desc
))
1125 retval
= sctp_make_chunk(asoc
, SCTP_CID_AUTH
, 0,
1126 hmac_desc
->hmac_len
+ sizeof(sctp_authhdr_t
));
1130 auth_hdr
.hmac_id
= htons(hmac_desc
->hmac_id
);
1131 auth_hdr
.shkey_id
= htons(asoc
->active_key_id
);
1133 retval
->subh
.auth_hdr
= sctp_addto_chunk(retval
, sizeof(sctp_authhdr_t
),
1136 hmac
= skb_put(retval
->skb
, hmac_desc
->hmac_len
);
1137 memset(hmac
, 0, hmac_desc
->hmac_len
);
1139 /* Adjust the chunk header to include the empty MAC */
1140 retval
->chunk_hdr
->length
=
1141 htons(ntohs(retval
->chunk_hdr
->length
) + hmac_desc
->hmac_len
);
1142 retval
->chunk_end
= skb_tail_pointer(retval
->skb
);
1148 /********************************************************************
1149 * 2nd Level Abstractions
1150 ********************************************************************/
1152 /* Turn an skb into a chunk.
1153 * FIXME: Eventually move the structure directly inside the skb->cb[].
1155 struct sctp_chunk
*sctp_chunkify(struct sk_buff
*skb
,
1156 const struct sctp_association
*asoc
,
1159 struct sctp_chunk
*retval
;
1161 retval
= kmem_cache_zalloc(sctp_chunk_cachep
, GFP_ATOMIC
);
1167 SCTP_DEBUG_PRINTK("chunkifying skb %p w/o an sk\n", skb
);
1170 INIT_LIST_HEAD(&retval
->list
);
1172 retval
->asoc
= (struct sctp_association
*)asoc
;
1174 retval
->has_tsn
= 0;
1175 retval
->has_ssn
= 0;
1176 retval
->rtt_in_progress
= 0;
1177 retval
->sent_at
= 0;
1178 retval
->singleton
= 1;
1179 retval
->end_of_packet
= 0;
1180 retval
->ecn_ce_done
= 0;
1181 retval
->pdiscard
= 0;
1183 /* sctpimpguide-05.txt Section 2.8.2
1184 * M1) Each time a new DATA chunk is transmitted
1185 * set the 'TSN.Missing.Report' count for that TSN to 0. The
1186 * 'TSN.Missing.Report' count will be used to determine missing chunks
1187 * and when to fast retransmit.
1189 retval
->tsn_missing_report
= 0;
1190 retval
->tsn_gap_acked
= 0;
1191 retval
->fast_retransmit
= 0;
1193 /* If this is a fragmented message, track all fragments
1194 * of the message (for SEND_FAILED).
1198 /* Polish the bead hole. */
1199 INIT_LIST_HEAD(&retval
->transmitted_list
);
1200 INIT_LIST_HEAD(&retval
->frag_list
);
1201 SCTP_DBG_OBJCNT_INC(chunk
);
1202 atomic_set(&retval
->refcnt
, 1);
1208 /* Set chunk->source and dest based on the IP header in chunk->skb. */
1209 void sctp_init_addrs(struct sctp_chunk
*chunk
, union sctp_addr
*src
,
1210 union sctp_addr
*dest
)
1212 memcpy(&chunk
->source
, src
, sizeof(union sctp_addr
));
1213 memcpy(&chunk
->dest
, dest
, sizeof(union sctp_addr
));
1216 /* Extract the source address from a chunk. */
1217 const union sctp_addr
*sctp_source(const struct sctp_chunk
*chunk
)
1219 /* If we have a known transport, use that. */
1220 if (chunk
->transport
) {
1221 return &chunk
->transport
->ipaddr
;
1223 /* Otherwise, extract it from the IP header. */
1224 return &chunk
->source
;
1228 /* Create a new chunk, setting the type and flags headers from the
1229 * arguments, reserving enough space for a 'paylen' byte payload.
1232 struct sctp_chunk
*sctp_make_chunk(const struct sctp_association
*asoc
,
1233 __u8 type
, __u8 flags
, int paylen
)
1235 struct sctp_chunk
*retval
;
1236 sctp_chunkhdr_t
*chunk_hdr
;
1237 struct sk_buff
*skb
;
1240 /* No need to allocate LL here, as this is only a chunk. */
1241 skb
= alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t
) + paylen
),
1246 /* Make room for the chunk header. */
1247 chunk_hdr
= (sctp_chunkhdr_t
*)skb_put(skb
, sizeof(sctp_chunkhdr_t
));
1248 chunk_hdr
->type
= type
;
1249 chunk_hdr
->flags
= flags
;
1250 chunk_hdr
->length
= htons(sizeof(sctp_chunkhdr_t
));
1252 sk
= asoc
? asoc
->base
.sk
: NULL
;
1253 retval
= sctp_chunkify(skb
, asoc
, sk
);
1259 retval
->chunk_hdr
= chunk_hdr
;
1260 retval
->chunk_end
= ((__u8
*)chunk_hdr
) + sizeof(struct sctp_chunkhdr
);
1262 /* Determine if the chunk needs to be authenticated */
1263 if (sctp_auth_send_cid(type
, asoc
))
1266 /* Set the skb to the belonging sock for accounting. */
1275 /* Release the memory occupied by a chunk. */
1276 static void sctp_chunk_destroy(struct sctp_chunk
*chunk
)
1278 BUG_ON(!list_empty(&chunk
->list
));
1279 list_del_init(&chunk
->transmitted_list
);
1281 /* Free the chunk skb data and the SCTP_chunk stub itself. */
1282 dev_kfree_skb(chunk
->skb
);
1284 SCTP_DBG_OBJCNT_DEC(chunk
);
1285 kmem_cache_free(sctp_chunk_cachep
, chunk
);
1288 /* Possibly, free the chunk. */
1289 void sctp_chunk_free(struct sctp_chunk
*chunk
)
1291 /* Release our reference on the message tracker. */
1293 sctp_datamsg_put(chunk
->msg
);
1295 sctp_chunk_put(chunk
);
1298 /* Grab a reference to the chunk. */
1299 void sctp_chunk_hold(struct sctp_chunk
*ch
)
1301 atomic_inc(&ch
->refcnt
);
1304 /* Release a reference to the chunk. */
1305 void sctp_chunk_put(struct sctp_chunk
*ch
)
1307 if (atomic_dec_and_test(&ch
->refcnt
))
1308 sctp_chunk_destroy(ch
);
1311 /* Append bytes to the end of a chunk. Will panic if chunk is not big
1314 void *sctp_addto_chunk(struct sctp_chunk
*chunk
, int len
, const void *data
)
1318 int chunklen
= ntohs(chunk
->chunk_hdr
->length
);
1319 int padlen
= WORD_ROUND(chunklen
) - chunklen
;
1321 padding
= skb_put(chunk
->skb
, padlen
);
1322 target
= skb_put(chunk
->skb
, len
);
1324 memset(padding
, 0, padlen
);
1325 memcpy(target
, data
, len
);
1327 /* Adjust the chunk length field. */
1328 chunk
->chunk_hdr
->length
= htons(chunklen
+ padlen
+ len
);
1329 chunk
->chunk_end
= skb_tail_pointer(chunk
->skb
);
1334 /* Append bytes from user space to the end of a chunk. Will panic if
1335 * chunk is not big enough.
1336 * Returns a kernel err value.
1338 int sctp_user_addto_chunk(struct sctp_chunk
*chunk
, int off
, int len
,
1344 /* Make room in chunk for data. */
1345 target
= skb_put(chunk
->skb
, len
);
1347 /* Copy data (whole iovec) into chunk */
1348 if ((err
= memcpy_fromiovecend(target
, data
, off
, len
)))
1351 /* Adjust the chunk length field. */
1352 chunk
->chunk_hdr
->length
=
1353 htons(ntohs(chunk
->chunk_hdr
->length
) + len
);
1354 chunk
->chunk_end
= skb_tail_pointer(chunk
->skb
);
1360 /* Helper function to assign a TSN if needed. This assumes that both
1361 * the data_hdr and association have already been assigned.
1363 void sctp_chunk_assign_ssn(struct sctp_chunk
*chunk
)
1365 struct sctp_datamsg
*msg
;
1366 struct sctp_chunk
*lchunk
;
1367 struct sctp_stream
*stream
;
1374 /* All fragments will be on the same stream */
1375 sid
= ntohs(chunk
->subh
.data_hdr
->stream
);
1376 stream
= &chunk
->asoc
->ssnmap
->out
;
1378 /* Now assign the sequence number to the entire message.
1379 * All fragments must have the same stream sequence number.
1382 list_for_each_entry(lchunk
, &msg
->chunks
, frag_list
) {
1383 if (lchunk
->chunk_hdr
->flags
& SCTP_DATA_UNORDERED
) {
1386 if (lchunk
->chunk_hdr
->flags
& SCTP_DATA_LAST_FRAG
)
1387 ssn
= sctp_ssn_next(stream
, sid
);
1389 ssn
= sctp_ssn_peek(stream
, sid
);
1392 lchunk
->subh
.data_hdr
->ssn
= htons(ssn
);
1393 lchunk
->has_ssn
= 1;
1397 /* Helper function to assign a TSN if needed. This assumes that both
1398 * the data_hdr and association have already been assigned.
1400 void sctp_chunk_assign_tsn(struct sctp_chunk
*chunk
)
1402 if (!chunk
->has_tsn
) {
1403 /* This is the last possible instant to
1406 chunk
->subh
.data_hdr
->tsn
=
1407 htonl(sctp_association_get_next_tsn(chunk
->asoc
));
1412 /* Create a CLOSED association to use with an incoming packet. */
1413 struct sctp_association
*sctp_make_temp_asoc(const struct sctp_endpoint
*ep
,
1414 struct sctp_chunk
*chunk
,
1417 struct sctp_association
*asoc
;
1418 struct sk_buff
*skb
;
1422 /* Create the bare association. */
1423 scope
= sctp_scope(sctp_source(chunk
));
1424 asoc
= sctp_association_new(ep
, ep
->base
.sk
, scope
, gfp
);
1429 /* Create an entry for the source address of the packet. */
1430 af
= sctp_get_af_specific(ipver2af(ip_hdr(skb
)->version
));
1433 af
->from_skb(&asoc
->c
.peer_addr
, skb
, 1);
1438 sctp_association_free(asoc
);
1442 /* Build a cookie representing asoc.
1443 * This INCLUDES the param header needed to put the cookie in the INIT ACK.
1445 static sctp_cookie_param_t
*sctp_pack_cookie(const struct sctp_endpoint
*ep
,
1446 const struct sctp_association
*asoc
,
1447 const struct sctp_chunk
*init_chunk
,
1449 const __u8
*raw_addrs
, int addrs_len
)
1451 sctp_cookie_param_t
*retval
;
1452 struct sctp_signed_cookie
*cookie
;
1453 struct scatterlist sg
;
1454 int headersize
, bodysize
;
1455 unsigned int keylen
;
1458 /* Header size is static data prior to the actual cookie, including
1461 headersize
= sizeof(sctp_paramhdr_t
) +
1462 (sizeof(struct sctp_signed_cookie
) -
1463 sizeof(struct sctp_cookie
));
1464 bodysize
= sizeof(struct sctp_cookie
)
1465 + ntohs(init_chunk
->chunk_hdr
->length
) + addrs_len
;
1467 /* Pad out the cookie to a multiple to make the signature
1468 * functions simpler to write.
1470 if (bodysize
% SCTP_COOKIE_MULTIPLE
)
1471 bodysize
+= SCTP_COOKIE_MULTIPLE
1472 - (bodysize
% SCTP_COOKIE_MULTIPLE
);
1473 *cookie_len
= headersize
+ bodysize
;
1475 /* Clear this memory since we are sending this data structure
1476 * out on the network.
1478 retval
= kzalloc(*cookie_len
, GFP_ATOMIC
);
1482 cookie
= (struct sctp_signed_cookie
*) retval
->body
;
1484 /* Set up the parameter header. */
1485 retval
->p
.type
= SCTP_PARAM_STATE_COOKIE
;
1486 retval
->p
.length
= htons(*cookie_len
);
1488 /* Copy the cookie part of the association itself. */
1489 cookie
->c
= asoc
->c
;
1490 /* Save the raw address list length in the cookie. */
1491 cookie
->c
.raw_addr_list_len
= addrs_len
;
1493 /* Remember PR-SCTP capability. */
1494 cookie
->c
.prsctp_capable
= asoc
->peer
.prsctp_capable
;
1496 /* Save adaptation indication in the cookie. */
1497 cookie
->c
.adaptation_ind
= asoc
->peer
.adaptation_ind
;
1499 /* Set an expiration time for the cookie. */
1500 do_gettimeofday(&cookie
->c
.expiration
);
1501 TIMEVAL_ADD(asoc
->cookie_life
, cookie
->c
.expiration
);
1503 /* Copy the peer's init packet. */
1504 memcpy(&cookie
->c
.peer_init
[0], init_chunk
->chunk_hdr
,
1505 ntohs(init_chunk
->chunk_hdr
->length
));
1507 /* Copy the raw local address list of the association. */
1508 memcpy((__u8
*)&cookie
->c
.peer_init
[0] +
1509 ntohs(init_chunk
->chunk_hdr
->length
), raw_addrs
, addrs_len
);
1511 if (sctp_sk(ep
->base
.sk
)->hmac
) {
1512 struct hash_desc desc
;
1514 /* Sign the message. */
1515 sg_init_one(&sg
, &cookie
->c
, bodysize
);
1516 keylen
= SCTP_SECRET_SIZE
;
1517 key
= (char *)ep
->secret_key
[ep
->current_key
];
1518 desc
.tfm
= sctp_sk(ep
->base
.sk
)->hmac
;
1521 if (crypto_hash_setkey(desc
.tfm
, key
, keylen
) ||
1522 crypto_hash_digest(&desc
, &sg
, bodysize
, cookie
->signature
))
1535 /* Unpack the cookie from COOKIE ECHO chunk, recreating the association. */
1536 struct sctp_association
*sctp_unpack_cookie(
1537 const struct sctp_endpoint
*ep
,
1538 const struct sctp_association
*asoc
,
1539 struct sctp_chunk
*chunk
, gfp_t gfp
,
1540 int *error
, struct sctp_chunk
**errp
)
1542 struct sctp_association
*retval
= NULL
;
1543 struct sctp_signed_cookie
*cookie
;
1544 struct sctp_cookie
*bear_cookie
;
1545 int headersize
, bodysize
, fixed_size
;
1546 __u8
*digest
= ep
->digest
;
1547 struct scatterlist sg
;
1548 unsigned int keylen
, len
;
1551 struct sk_buff
*skb
= chunk
->skb
;
1553 struct hash_desc desc
;
1555 /* Header size is static data prior to the actual cookie, including
1558 headersize
= sizeof(sctp_chunkhdr_t
) +
1559 (sizeof(struct sctp_signed_cookie
) -
1560 sizeof(struct sctp_cookie
));
1561 bodysize
= ntohs(chunk
->chunk_hdr
->length
) - headersize
;
1562 fixed_size
= headersize
+ sizeof(struct sctp_cookie
);
1564 /* Verify that the chunk looks like it even has a cookie.
1565 * There must be enough room for our cookie and our peer's
1568 len
= ntohs(chunk
->chunk_hdr
->length
);
1569 if (len
< fixed_size
+ sizeof(struct sctp_chunkhdr
))
1572 /* Verify that the cookie has been padded out. */
1573 if (bodysize
% SCTP_COOKIE_MULTIPLE
)
1576 /* Process the cookie. */
1577 cookie
= chunk
->subh
.cookie_hdr
;
1578 bear_cookie
= &cookie
->c
;
1580 if (!sctp_sk(ep
->base
.sk
)->hmac
)
1583 /* Check the signature. */
1584 keylen
= SCTP_SECRET_SIZE
;
1585 sg_init_one(&sg
, bear_cookie
, bodysize
);
1586 key
= (char *)ep
->secret_key
[ep
->current_key
];
1587 desc
.tfm
= sctp_sk(ep
->base
.sk
)->hmac
;
1590 memset(digest
, 0x00, SCTP_SIGNATURE_SIZE
);
1591 if (crypto_hash_setkey(desc
.tfm
, key
, keylen
) ||
1592 crypto_hash_digest(&desc
, &sg
, bodysize
, digest
)) {
1593 *error
= -SCTP_IERROR_NOMEM
;
1597 if (memcmp(digest
, cookie
->signature
, SCTP_SIGNATURE_SIZE
)) {
1598 /* Try the previous key. */
1599 key
= (char *)ep
->secret_key
[ep
->last_key
];
1600 memset(digest
, 0x00, SCTP_SIGNATURE_SIZE
);
1601 if (crypto_hash_setkey(desc
.tfm
, key
, keylen
) ||
1602 crypto_hash_digest(&desc
, &sg
, bodysize
, digest
)) {
1603 *error
= -SCTP_IERROR_NOMEM
;
1607 if (memcmp(digest
, cookie
->signature
, SCTP_SIGNATURE_SIZE
)) {
1608 /* Yikes! Still bad signature! */
1609 *error
= -SCTP_IERROR_BAD_SIG
;
1615 /* IG Section 2.35.2:
1616 * 3) Compare the port numbers and the verification tag contained
1617 * within the COOKIE ECHO chunk to the actual port numbers and the
1618 * verification tag within the SCTP common header of the received
1619 * packet. If these values do not match the packet MUST be silently
1622 if (ntohl(chunk
->sctp_hdr
->vtag
) != bear_cookie
->my_vtag
) {
1623 *error
= -SCTP_IERROR_BAD_TAG
;
1627 if (chunk
->sctp_hdr
->source
!= bear_cookie
->peer_addr
.v4
.sin_port
||
1628 ntohs(chunk
->sctp_hdr
->dest
) != bear_cookie
->my_port
) {
1629 *error
= -SCTP_IERROR_BAD_PORTS
;
1633 /* Check to see if the cookie is stale. If there is already
1634 * an association, there is no need to check cookie's expiration
1635 * for init collision case of lost COOKIE ACK.
1636 * If skb has been timestamped, then use the stamp, otherwise
1637 * use current time. This introduces a small possibility that
1638 * that a cookie may be considered expired, but his would only slow
1639 * down the new association establishment instead of every packet.
1641 if (sock_flag(ep
->base
.sk
, SOCK_TIMESTAMP
))
1642 skb_get_timestamp(skb
, &tv
);
1644 do_gettimeofday(&tv
);
1646 if (!asoc
&& tv_lt(bear_cookie
->expiration
, tv
)) {
1648 * Section 3.3.10.3 Stale Cookie Error (3)
1652 * Stale Cookie Error: Indicates the receipt of a valid State
1653 * Cookie that has expired.
1655 len
= ntohs(chunk
->chunk_hdr
->length
);
1656 *errp
= sctp_make_op_error_space(asoc
, chunk
, len
);
1658 suseconds_t usecs
= (tv
.tv_sec
-
1659 bear_cookie
->expiration
.tv_sec
) * 1000000L +
1660 tv
.tv_usec
- bear_cookie
->expiration
.tv_usec
;
1661 __be32 n
= htonl(usecs
);
1663 sctp_init_cause(*errp
, SCTP_ERROR_STALE_COOKIE
,
1665 sctp_addto_chunk(*errp
, sizeof(n
), &n
);
1666 *error
= -SCTP_IERROR_STALE_COOKIE
;
1668 *error
= -SCTP_IERROR_NOMEM
;
1673 /* Make a new base association. */
1674 scope
= sctp_scope(sctp_source(chunk
));
1675 retval
= sctp_association_new(ep
, ep
->base
.sk
, scope
, gfp
);
1677 *error
= -SCTP_IERROR_NOMEM
;
1681 /* Set up our peer's port number. */
1682 retval
->peer
.port
= ntohs(chunk
->sctp_hdr
->source
);
1684 /* Populate the association from the cookie. */
1685 memcpy(&retval
->c
, bear_cookie
, sizeof(*bear_cookie
));
1687 if (sctp_assoc_set_bind_addr_from_cookie(retval
, bear_cookie
,
1689 *error
= -SCTP_IERROR_NOMEM
;
1693 /* Also, add the destination address. */
1694 if (list_empty(&retval
->base
.bind_addr
.address_list
)) {
1695 sctp_add_bind_addr(&retval
->base
.bind_addr
, &chunk
->dest
,
1696 SCTP_ADDR_SRC
, GFP_ATOMIC
);
1699 retval
->next_tsn
= retval
->c
.initial_tsn
;
1700 retval
->ctsn_ack_point
= retval
->next_tsn
- 1;
1701 retval
->addip_serial
= retval
->c
.initial_tsn
;
1702 retval
->adv_peer_ack_point
= retval
->ctsn_ack_point
;
1703 retval
->peer
.prsctp_capable
= retval
->c
.prsctp_capable
;
1704 retval
->peer
.adaptation_ind
= retval
->c
.adaptation_ind
;
1706 /* The INIT stuff will be done by the side effects. */
1711 sctp_association_free(retval
);
1716 /* Yikes! The packet is either corrupt or deliberately
1719 *error
= -SCTP_IERROR_MALFORMED
;
1723 /********************************************************************
1724 * 3rd Level Abstractions
1725 ********************************************************************/
1727 struct __sctp_missing
{
1730 } __attribute__((packed
));
1733 * Report a missing mandatory parameter.
1735 static int sctp_process_missing_param(const struct sctp_association
*asoc
,
1736 sctp_param_t paramtype
,
1737 struct sctp_chunk
*chunk
,
1738 struct sctp_chunk
**errp
)
1740 struct __sctp_missing report
;
1743 len
= WORD_ROUND(sizeof(report
));
1745 /* Make an ERROR chunk, preparing enough room for
1746 * returning multiple unknown parameters.
1749 *errp
= sctp_make_op_error_space(asoc
, chunk
, len
);
1752 report
.num_missing
= htonl(1);
1753 report
.type
= paramtype
;
1754 sctp_init_cause(*errp
, SCTP_ERROR_MISS_PARAM
,
1756 sctp_addto_chunk(*errp
, sizeof(report
), &report
);
1759 /* Stop processing this chunk. */
1763 /* Report an Invalid Mandatory Parameter. */
1764 static int sctp_process_inv_mandatory(const struct sctp_association
*asoc
,
1765 struct sctp_chunk
*chunk
,
1766 struct sctp_chunk
**errp
)
1768 /* Invalid Mandatory Parameter Error has no payload. */
1771 *errp
= sctp_make_op_error_space(asoc
, chunk
, 0);
1774 sctp_init_cause(*errp
, SCTP_ERROR_INV_PARAM
, 0);
1776 /* Stop processing this chunk. */
1780 static int sctp_process_inv_paramlength(const struct sctp_association
*asoc
,
1781 struct sctp_paramhdr
*param
,
1782 const struct sctp_chunk
*chunk
,
1783 struct sctp_chunk
**errp
)
1785 char error
[] = "The following parameter had invalid length:";
1786 size_t payload_len
= WORD_ROUND(sizeof(error
)) +
1787 sizeof(sctp_paramhdr_t
);
1790 /* This is a fatal error. Any accumulated non-fatal errors are
1794 sctp_chunk_free(*errp
);
1796 /* Create an error chunk and fill it in with our payload. */
1797 *errp
= sctp_make_op_error_space(asoc
, chunk
, payload_len
);
1800 sctp_init_cause(*errp
, SCTP_ERROR_PROTO_VIOLATION
,
1801 sizeof(error
) + sizeof(sctp_paramhdr_t
));
1802 sctp_addto_chunk(*errp
, sizeof(error
), error
);
1803 sctp_addto_param(*errp
, sizeof(sctp_paramhdr_t
), param
);
1810 /* Do not attempt to handle the HOST_NAME parm. However, do
1811 * send back an indicator to the peer.
1813 static int sctp_process_hn_param(const struct sctp_association
*asoc
,
1814 union sctp_params param
,
1815 struct sctp_chunk
*chunk
,
1816 struct sctp_chunk
**errp
)
1818 __u16 len
= ntohs(param
.p
->length
);
1820 /* Processing of the HOST_NAME parameter will generate an
1821 * ABORT. If we've accumulated any non-fatal errors, they
1822 * would be unrecognized parameters and we should not include
1823 * them in the ABORT.
1826 sctp_chunk_free(*errp
);
1828 *errp
= sctp_make_op_error_space(asoc
, chunk
, len
);
1831 sctp_init_cause(*errp
, SCTP_ERROR_DNS_FAILED
, len
);
1832 sctp_addto_chunk(*errp
, len
, param
.v
);
1835 /* Stop processing this chunk. */
1839 static int sctp_verify_ext_param(union sctp_params param
)
1841 __u16 num_ext
= ntohs(param
.p
->length
) - sizeof(sctp_paramhdr_t
);
1843 int have_asconf
= 0;
1846 for (i
= 0; i
< num_ext
; i
++) {
1847 switch (param
.ext
->chunks
[i
]) {
1851 case SCTP_CID_ASCONF
:
1852 case SCTP_CID_ASCONF_ACK
:
1858 /* ADD-IP Security: The draft requires us to ABORT or ignore the
1859 * INIT/INIT-ACK if ADD-IP is listed, but AUTH is not. Do this
1860 * only if ADD-IP is turned on and we are not backward-compatible
1863 if (sctp_addip_noauth
)
1866 if (sctp_addip_enable
&& !have_auth
&& have_asconf
)
1872 static void sctp_process_ext_param(struct sctp_association
*asoc
,
1873 union sctp_params param
)
1875 __u16 num_ext
= ntohs(param
.p
->length
) - sizeof(sctp_paramhdr_t
);
1878 for (i
= 0; i
< num_ext
; i
++) {
1879 switch (param
.ext
->chunks
[i
]) {
1880 case SCTP_CID_FWD_TSN
:
1881 if (sctp_prsctp_enable
&&
1882 !asoc
->peer
.prsctp_capable
)
1883 asoc
->peer
.prsctp_capable
= 1;
1886 /* if the peer reports AUTH, assume that he
1889 asoc
->peer
.auth_capable
= 1;
1891 case SCTP_CID_ASCONF
:
1892 case SCTP_CID_ASCONF_ACK
:
1893 asoc
->peer
.asconf_capable
= 1;
1901 /* RFC 3.2.1 & the Implementers Guide 2.2.
1903 * The Parameter Types are encoded such that the
1904 * highest-order two bits specify the action that must be
1905 * taken if the processing endpoint does not recognize the
1908 * 00 - Stop processing this parameter; do not process any further
1909 * parameters within this chunk
1911 * 01 - Stop processing this parameter, do not process any further
1912 * parameters within this chunk, and report the unrecognized
1913 * parameter in an 'Unrecognized Parameter' ERROR chunk.
1915 * 10 - Skip this parameter and continue processing.
1917 * 11 - Skip this parameter and continue processing but
1918 * report the unrecognized parameter in an
1919 * 'Unrecognized Parameter' ERROR chunk.
1922 * SCTP_IERROR_NO_ERROR - continue with the chunk
1923 * SCTP_IERROR_ERROR - stop and report an error.
1924 * SCTP_IERROR_NOMEME - out of memory.
1926 static sctp_ierror_t
sctp_process_unk_param(const struct sctp_association
*asoc
,
1927 union sctp_params param
,
1928 struct sctp_chunk
*chunk
,
1929 struct sctp_chunk
**errp
)
1931 int retval
= SCTP_IERROR_NO_ERROR
;
1933 switch (param
.p
->type
& SCTP_PARAM_ACTION_MASK
) {
1934 case SCTP_PARAM_ACTION_DISCARD
:
1935 retval
= SCTP_IERROR_ERROR
;
1937 case SCTP_PARAM_ACTION_SKIP
:
1939 case SCTP_PARAM_ACTION_DISCARD_ERR
:
1940 retval
= SCTP_IERROR_ERROR
;
1942 case SCTP_PARAM_ACTION_SKIP_ERR
:
1943 /* Make an ERROR chunk, preparing enough room for
1944 * returning multiple unknown parameters.
1947 *errp
= sctp_make_op_error_space(asoc
, chunk
,
1948 ntohs(chunk
->chunk_hdr
->length
));
1951 sctp_init_cause(*errp
, SCTP_ERROR_UNKNOWN_PARAM
,
1952 WORD_ROUND(ntohs(param
.p
->length
)));
1953 sctp_addto_chunk(*errp
,
1954 WORD_ROUND(ntohs(param
.p
->length
)),
1957 /* If there is no memory for generating the ERROR
1958 * report as specified, an ABORT will be triggered
1959 * to the peer and the association won't be
1962 retval
= SCTP_IERROR_NOMEM
;
1972 /* Verify variable length parameters
1974 * SCTP_IERROR_ABORT - trigger an ABORT
1975 * SCTP_IERROR_NOMEM - out of memory (abort)
1976 * SCTP_IERROR_ERROR - stop processing, trigger an ERROR
1977 * SCTP_IERROR_NO_ERROR - continue with the chunk
1979 static sctp_ierror_t
sctp_verify_param(const struct sctp_association
*asoc
,
1980 union sctp_params param
,
1982 struct sctp_chunk
*chunk
,
1983 struct sctp_chunk
**err_chunk
)
1985 int retval
= SCTP_IERROR_NO_ERROR
;
1987 /* FIXME - This routine is not looking at each parameter per the
1988 * chunk type, i.e., unrecognized parameters should be further
1989 * identified based on the chunk id.
1992 switch (param
.p
->type
) {
1993 case SCTP_PARAM_IPV4_ADDRESS
:
1994 case SCTP_PARAM_IPV6_ADDRESS
:
1995 case SCTP_PARAM_COOKIE_PRESERVATIVE
:
1996 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES
:
1997 case SCTP_PARAM_STATE_COOKIE
:
1998 case SCTP_PARAM_HEARTBEAT_INFO
:
1999 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS
:
2000 case SCTP_PARAM_ECN_CAPABLE
:
2001 case SCTP_PARAM_ADAPTATION_LAYER_IND
:
2004 case SCTP_PARAM_SUPPORTED_EXT
:
2005 if (!sctp_verify_ext_param(param
))
2006 return SCTP_IERROR_ABORT
;
2009 case SCTP_PARAM_SET_PRIMARY
:
2010 if (sctp_addip_enable
)
2014 case SCTP_PARAM_HOST_NAME_ADDRESS
:
2015 /* Tell the peer, we won't support this param. */
2016 sctp_process_hn_param(asoc
, param
, chunk
, err_chunk
);
2017 retval
= SCTP_IERROR_ABORT
;
2020 case SCTP_PARAM_FWD_TSN_SUPPORT
:
2021 if (sctp_prsctp_enable
)
2025 case SCTP_PARAM_RANDOM
:
2026 if (!sctp_auth_enable
)
2029 /* SCTP-AUTH: Secion 6.1
2030 * If the random number is not 32 byte long the association
2031 * MUST be aborted. The ABORT chunk SHOULD contain the error
2032 * cause 'Protocol Violation'.
2034 if (SCTP_AUTH_RANDOM_LENGTH
!=
2035 ntohs(param
.p
->length
) - sizeof(sctp_paramhdr_t
)) {
2036 sctp_process_inv_paramlength(asoc
, param
.p
,
2038 retval
= SCTP_IERROR_ABORT
;
2042 case SCTP_PARAM_CHUNKS
:
2043 if (!sctp_auth_enable
)
2046 /* SCTP-AUTH: Section 3.2
2047 * The CHUNKS parameter MUST be included once in the INIT or
2048 * INIT-ACK chunk if the sender wants to receive authenticated
2049 * chunks. Its maximum length is 260 bytes.
2051 if (260 < ntohs(param
.p
->length
)) {
2052 sctp_process_inv_paramlength(asoc
, param
.p
,
2054 retval
= SCTP_IERROR_ABORT
;
2058 case SCTP_PARAM_HMAC_ALGO
:
2059 if (sctp_auth_enable
)
2064 SCTP_DEBUG_PRINTK("Unrecognized param: %d for chunk %d.\n",
2065 ntohs(param
.p
->type
), cid
);
2066 retval
= sctp_process_unk_param(asoc
, param
, chunk
, err_chunk
);
2072 /* Verify the INIT packet before we process it. */
2073 int sctp_verify_init(const struct sctp_association
*asoc
,
2075 sctp_init_chunk_t
*peer_init
,
2076 struct sctp_chunk
*chunk
,
2077 struct sctp_chunk
**errp
)
2079 union sctp_params param
;
2083 /* Verify stream values are non-zero. */
2084 if ((0 == peer_init
->init_hdr
.num_outbound_streams
) ||
2085 (0 == peer_init
->init_hdr
.num_inbound_streams
) ||
2086 (0 == peer_init
->init_hdr
.init_tag
) ||
2087 (SCTP_DEFAULT_MINWINDOW
> ntohl(peer_init
->init_hdr
.a_rwnd
))) {
2089 return sctp_process_inv_mandatory(asoc
, chunk
, errp
);
2092 /* Check for missing mandatory parameters. */
2093 sctp_walk_params(param
, peer_init
, init_hdr
.params
) {
2095 if (SCTP_PARAM_STATE_COOKIE
== param
.p
->type
)
2098 } /* for (loop through all parameters) */
2100 /* There is a possibility that a parameter length was bad and
2101 * in that case we would have stoped walking the parameters.
2102 * The current param.p would point at the bad one.
2103 * Current consensus on the mailing list is to generate a PROTOCOL
2104 * VIOLATION error. We build the ERROR chunk here and let the normal
2105 * error handling code build and send the packet.
2107 if (param
.v
!= (void*)chunk
->chunk_end
)
2108 return sctp_process_inv_paramlength(asoc
, param
.p
, chunk
, errp
);
2110 /* The only missing mandatory param possible today is
2111 * the state cookie for an INIT-ACK chunk.
2113 if ((SCTP_CID_INIT_ACK
== cid
) && !has_cookie
)
2114 return sctp_process_missing_param(asoc
, SCTP_PARAM_STATE_COOKIE
,
2117 /* Verify all the variable length parameters */
2118 sctp_walk_params(param
, peer_init
, init_hdr
.params
) {
2120 result
= sctp_verify_param(asoc
, param
, cid
, chunk
, errp
);
2122 case SCTP_IERROR_ABORT
:
2123 case SCTP_IERROR_NOMEM
:
2125 case SCTP_IERROR_ERROR
:
2127 case SCTP_IERROR_NO_ERROR
:
2132 } /* for (loop through all parameters) */
2137 /* Unpack the parameters in an INIT packet into an association.
2138 * Returns 0 on failure, else success.
2139 * FIXME: This is an association method.
2141 int sctp_process_init(struct sctp_association
*asoc
, sctp_cid_t cid
,
2142 const union sctp_addr
*peer_addr
,
2143 sctp_init_chunk_t
*peer_init
, gfp_t gfp
)
2145 union sctp_params param
;
2146 struct sctp_transport
*transport
;
2147 struct list_head
*pos
, *temp
;
2150 /* We must include the address that the INIT packet came from.
2151 * This is the only address that matters for an INIT packet.
2152 * When processing a COOKIE ECHO, we retrieve the from address
2153 * of the INIT from the cookie.
2156 /* This implementation defaults to making the first transport
2157 * added as the primary transport. The source address seems to
2158 * be a a better choice than any of the embedded addresses.
2161 if(!sctp_assoc_add_peer(asoc
, peer_addr
, gfp
, SCTP_ACTIVE
))
2165 /* Process the initialization parameters. */
2166 sctp_walk_params(param
, peer_init
, init_hdr
.params
) {
2168 if (!sctp_process_param(asoc
, param
, peer_addr
, gfp
))
2172 /* AUTH: After processing the parameters, make sure that we
2173 * have all the required info to potentially do authentications.
2175 if (asoc
->peer
.auth_capable
&& (!asoc
->peer
.peer_random
||
2176 !asoc
->peer
.peer_hmacs
))
2177 asoc
->peer
.auth_capable
= 0;
2179 /* In a non-backward compatible mode, if the peer claims
2180 * support for ADD-IP but not AUTH, the ADD-IP spec states
2181 * that we MUST ABORT the association. Section 6. The section
2182 * also give us an option to silently ignore the packet, which
2183 * is what we'll do here.
2185 if (!sctp_addip_noauth
&&
2186 (asoc
->peer
.asconf_capable
&& !asoc
->peer
.auth_capable
)) {
2187 asoc
->peer
.addip_disabled_mask
|= (SCTP_PARAM_ADD_IP
|
2189 SCTP_PARAM_SET_PRIMARY
);
2190 asoc
->peer
.asconf_capable
= 0;
2194 /* Walk list of transports, removing transports in the UNKNOWN state. */
2195 list_for_each_safe(pos
, temp
, &asoc
->peer
.transport_addr_list
) {
2196 transport
= list_entry(pos
, struct sctp_transport
, transports
);
2197 if (transport
->state
== SCTP_UNKNOWN
) {
2198 sctp_assoc_rm_peer(asoc
, transport
);
2202 /* The fixed INIT headers are always in network byte
2205 asoc
->peer
.i
.init_tag
=
2206 ntohl(peer_init
->init_hdr
.init_tag
);
2207 asoc
->peer
.i
.a_rwnd
=
2208 ntohl(peer_init
->init_hdr
.a_rwnd
);
2209 asoc
->peer
.i
.num_outbound_streams
=
2210 ntohs(peer_init
->init_hdr
.num_outbound_streams
);
2211 asoc
->peer
.i
.num_inbound_streams
=
2212 ntohs(peer_init
->init_hdr
.num_inbound_streams
);
2213 asoc
->peer
.i
.initial_tsn
=
2214 ntohl(peer_init
->init_hdr
.initial_tsn
);
2216 /* Apply the upper bounds for output streams based on peer's
2217 * number of inbound streams.
2219 if (asoc
->c
.sinit_num_ostreams
>
2220 ntohs(peer_init
->init_hdr
.num_inbound_streams
)) {
2221 asoc
->c
.sinit_num_ostreams
=
2222 ntohs(peer_init
->init_hdr
.num_inbound_streams
);
2225 if (asoc
->c
.sinit_max_instreams
>
2226 ntohs(peer_init
->init_hdr
.num_outbound_streams
)) {
2227 asoc
->c
.sinit_max_instreams
=
2228 ntohs(peer_init
->init_hdr
.num_outbound_streams
);
2231 /* Copy Initiation tag from INIT to VT_peer in cookie. */
2232 asoc
->c
.peer_vtag
= asoc
->peer
.i
.init_tag
;
2234 /* Peer Rwnd : Current calculated value of the peer's rwnd. */
2235 asoc
->peer
.rwnd
= asoc
->peer
.i
.a_rwnd
;
2237 /* Copy cookie in case we need to resend COOKIE-ECHO. */
2238 cookie
= asoc
->peer
.cookie
;
2240 asoc
->peer
.cookie
= kmemdup(cookie
, asoc
->peer
.cookie_len
, gfp
);
2241 if (!asoc
->peer
.cookie
)
2245 /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
2246 * high (for example, implementations MAY use the size of the receiver
2247 * advertised window).
2249 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
2250 transport
= list_entry(pos
, struct sctp_transport
, transports
);
2251 transport
->ssthresh
= asoc
->peer
.i
.a_rwnd
;
2254 /* Set up the TSN tracking pieces. */
2255 sctp_tsnmap_init(&asoc
->peer
.tsn_map
, SCTP_TSN_MAP_SIZE
,
2256 asoc
->peer
.i
.initial_tsn
);
2258 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
2260 * The stream sequence number in all the streams shall start
2261 * from 0 when the association is established. Also, when the
2262 * stream sequence number reaches the value 65535 the next
2263 * stream sequence number shall be set to 0.
2266 /* Allocate storage for the negotiated streams if it is not a temporary
2272 asoc
->ssnmap
= sctp_ssnmap_new(asoc
->c
.sinit_max_instreams
,
2273 asoc
->c
.sinit_num_ostreams
, gfp
);
2277 error
= sctp_assoc_set_id(asoc
, gfp
);
2282 /* ADDIP Section 4.1 ASCONF Chunk Procedures
2284 * When an endpoint has an ASCONF signaled change to be sent to the
2285 * remote endpoint it should do the following:
2287 * A2) A serial number should be assigned to the Chunk. The serial
2288 * number should be a monotonically increasing number. All serial
2289 * numbers are defined to be initialized at the start of the
2290 * association to the same value as the Initial TSN.
2292 asoc
->peer
.addip_serial
= asoc
->peer
.i
.initial_tsn
- 1;
2296 /* Release the transport structures. */
2297 list_for_each_safe(pos
, temp
, &asoc
->peer
.transport_addr_list
) {
2298 transport
= list_entry(pos
, struct sctp_transport
, transports
);
2300 sctp_transport_free(transport
);
2303 asoc
->peer
.transport_count
= 0;
2310 /* Update asoc with the option described in param.
2312 * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
2314 * asoc is the association to update.
2315 * param is the variable length parameter to use for update.
2316 * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
2317 * If the current packet is an INIT we want to minimize the amount of
2318 * work we do. In particular, we should not build transport
2319 * structures for the addresses.
2321 static int sctp_process_param(struct sctp_association
*asoc
,
2322 union sctp_params param
,
2323 const union sctp_addr
*peer_addr
,
2326 union sctp_addr addr
;
2333 union sctp_addr_param
*addr_param
;
2334 struct sctp_transport
*t
;
2336 /* We maintain all INIT parameters in network byte order all the
2337 * time. This allows us to not worry about whether the parameters
2338 * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
2340 switch (param
.p
->type
) {
2341 case SCTP_PARAM_IPV6_ADDRESS
:
2342 if (PF_INET6
!= asoc
->base
.sk
->sk_family
)
2345 case SCTP_PARAM_IPV4_ADDRESS
:
2346 af
= sctp_get_af_specific(param_type2af(param
.p
->type
));
2347 af
->from_addr_param(&addr
, param
.addr
, htons(asoc
->peer
.port
), 0);
2348 scope
= sctp_scope(peer_addr
);
2349 if (sctp_in_scope(&addr
, scope
))
2350 if (!sctp_assoc_add_peer(asoc
, &addr
, gfp
, SCTP_UNCONFIRMED
))
2354 case SCTP_PARAM_COOKIE_PRESERVATIVE
:
2355 if (!sctp_cookie_preserve_enable
)
2358 stale
= ntohl(param
.life
->lifespan_increment
);
2360 /* Suggested Cookie Life span increment's unit is msec,
2363 asoc
->cookie_life
.tv_sec
+= stale
/ 1000;
2364 asoc
->cookie_life
.tv_usec
+= (stale
% 1000) * 1000;
2367 case SCTP_PARAM_HOST_NAME_ADDRESS
:
2368 SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
2371 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES
:
2372 /* Turn off the default values first so we'll know which
2373 * ones are really set by the peer.
2375 asoc
->peer
.ipv4_address
= 0;
2376 asoc
->peer
.ipv6_address
= 0;
2378 <<<<<<< HEAD
:net
/sctp
/sm_make_chunk
.c
2380 /* Assume that peer supports the address family
2381 * by which it sends a packet.
2383 if (peer_addr
->sa
.sa_family
== AF_INET6
)
2384 asoc
->peer
.ipv6_address
= 1;
2385 else if (peer_addr
->sa
.sa_family
== AF_INET
)
2386 asoc
->peer
.ipv4_address
= 1;
2388 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:net
/sctp
/sm_make_chunk
.c
2389 /* Cycle through address types; avoid divide by 0. */
2390 sat
= ntohs(param
.p
->length
) - sizeof(sctp_paramhdr_t
);
2392 sat
/= sizeof(__u16
);
2394 for (i
= 0; i
< sat
; ++i
) {
2395 switch (param
.sat
->types
[i
]) {
2396 case SCTP_PARAM_IPV4_ADDRESS
:
2397 asoc
->peer
.ipv4_address
= 1;
2400 case SCTP_PARAM_IPV6_ADDRESS
:
2401 asoc
->peer
.ipv6_address
= 1;
2404 case SCTP_PARAM_HOST_NAME_ADDRESS
:
2405 asoc
->peer
.hostname_address
= 1;
2408 default: /* Just ignore anything else. */
2414 case SCTP_PARAM_STATE_COOKIE
:
2415 asoc
->peer
.cookie_len
=
2416 ntohs(param
.p
->length
) - sizeof(sctp_paramhdr_t
);
2417 asoc
->peer
.cookie
= param
.cookie
->body
;
2420 case SCTP_PARAM_HEARTBEAT_INFO
:
2421 /* Would be odd to receive, but it causes no problems. */
2424 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS
:
2425 /* Rejected during verify stage. */
2428 case SCTP_PARAM_ECN_CAPABLE
:
2429 asoc
->peer
.ecn_capable
= 1;
2432 case SCTP_PARAM_ADAPTATION_LAYER_IND
:
2433 asoc
->peer
.adaptation_ind
= param
.aind
->adaptation_ind
;
2436 case SCTP_PARAM_SET_PRIMARY
:
2437 addr_param
= param
.v
+ sizeof(sctp_addip_param_t
);
2439 af
= sctp_get_af_specific(param_type2af(param
.p
->type
));
2440 af
->from_addr_param(&addr
, addr_param
,
2441 htons(asoc
->peer
.port
), 0);
2443 /* if the address is invalid, we can't process it.
2444 * XXX: see spec for what to do.
2446 if (!af
->addr_valid(&addr
, NULL
, NULL
))
2449 t
= sctp_assoc_lookup_paddr(asoc
, &addr
);
2453 sctp_assoc_set_primary(asoc
, t
);
2456 case SCTP_PARAM_SUPPORTED_EXT
:
2457 sctp_process_ext_param(asoc
, param
);
2460 case SCTP_PARAM_FWD_TSN_SUPPORT
:
2461 if (sctp_prsctp_enable
) {
2462 asoc
->peer
.prsctp_capable
= 1;
2468 case SCTP_PARAM_RANDOM
:
2469 if (!sctp_auth_enable
)
2472 /* Save peer's random parameter */
2473 asoc
->peer
.peer_random
= kmemdup(param
.p
,
2474 ntohs(param
.p
->length
), gfp
);
2475 if (!asoc
->peer
.peer_random
) {
2481 case SCTP_PARAM_HMAC_ALGO
:
2482 if (!sctp_auth_enable
)
2485 /* Save peer's HMAC list */
2486 asoc
->peer
.peer_hmacs
= kmemdup(param
.p
,
2487 ntohs(param
.p
->length
), gfp
);
2488 if (!asoc
->peer
.peer_hmacs
) {
2493 /* Set the default HMAC the peer requested*/
2494 sctp_auth_asoc_set_default_hmac(asoc
, param
.hmac_algo
);
2497 case SCTP_PARAM_CHUNKS
:
2498 if (!sctp_auth_enable
)
2501 asoc
->peer
.peer_chunks
= kmemdup(param
.p
,
2502 ntohs(param
.p
->length
), gfp
);
2503 if (!asoc
->peer
.peer_chunks
)
2508 /* Any unrecognized parameters should have been caught
2509 * and handled by sctp_verify_param() which should be
2510 * called prior to this routine. Simply log the error
2513 SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
2514 ntohs(param
.p
->type
), asoc
);
2521 /* Select a new verification tag. */
2522 __u32
sctp_generate_tag(const struct sctp_endpoint
*ep
)
2524 /* I believe that this random number generator complies with RFC1750.
2525 * A tag of 0 is reserved for special cases (e.g. INIT).
2530 get_random_bytes(&x
, sizeof(__u32
));
2536 /* Select an initial TSN to send during startup. */
2537 __u32
sctp_generate_tsn(const struct sctp_endpoint
*ep
)
2541 get_random_bytes(&retval
, sizeof(__u32
));
2546 * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
2548 * 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
2549 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2550 * | Type = 0xC1 | Chunk Flags | Chunk Length |
2551 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2553 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2554 * | Address Parameter |
2555 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2556 * | ASCONF Parameter #1 |
2557 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2561 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2562 * | ASCONF Parameter #N |
2563 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2565 * Address Parameter and other parameter will not be wrapped in this function
2567 static struct sctp_chunk
*sctp_make_asconf(struct sctp_association
*asoc
,
2568 union sctp_addr
*addr
,
2571 sctp_addiphdr_t asconf
;
2572 struct sctp_chunk
*retval
;
2573 int length
= sizeof(asconf
) + vparam_len
;
2574 union sctp_addr_param addrparam
;
2576 struct sctp_af
*af
= sctp_get_af_specific(addr
->v4
.sin_family
);
2578 addrlen
= af
->to_addr_param(addr
, &addrparam
);
2583 /* Create the chunk. */
2584 retval
= sctp_make_chunk(asoc
, SCTP_CID_ASCONF
, 0, length
);
2588 asconf
.serial
= htonl(asoc
->addip_serial
++);
2590 retval
->subh
.addip_hdr
=
2591 sctp_addto_chunk(retval
, sizeof(asconf
), &asconf
);
2592 retval
->param_hdr
.v
=
2593 sctp_addto_chunk(retval
, addrlen
, &addrparam
);
2599 * 3.2.1 Add IP Address
2601 * 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
2602 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2603 * | Type = 0xC001 | Length = Variable |
2604 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2605 * | ASCONF-Request Correlation ID |
2606 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2607 * | Address Parameter |
2608 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2610 * 3.2.2 Delete IP Address
2612 * 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
2613 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2614 * | Type = 0xC002 | Length = Variable |
2615 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2616 * | ASCONF-Request Correlation ID |
2617 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2618 * | Address Parameter |
2619 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2622 struct sctp_chunk
*sctp_make_asconf_update_ip(struct sctp_association
*asoc
,
2623 union sctp_addr
*laddr
,
2624 struct sockaddr
*addrs
,
2628 sctp_addip_param_t param
;
2629 struct sctp_chunk
*retval
;
2630 union sctp_addr_param addr_param
;
2631 union sctp_addr
*addr
;
2634 int paramlen
= sizeof(param
);
2635 int addr_param_len
= 0;
2639 /* Get total length of all the address parameters. */
2641 for (i
= 0; i
< addrcnt
; i
++) {
2642 addr
= (union sctp_addr
*)addr_buf
;
2643 af
= sctp_get_af_specific(addr
->v4
.sin_family
);
2644 addr_param_len
= af
->to_addr_param(addr
, &addr_param
);
2646 totallen
+= paramlen
;
2647 totallen
+= addr_param_len
;
2649 addr_buf
+= af
->sockaddr_len
;
2652 /* Create an asconf chunk with the required length. */
2653 retval
= sctp_make_asconf(asoc
, laddr
, totallen
);
2657 /* Add the address parameters to the asconf chunk. */
2659 for (i
= 0; i
< addrcnt
; i
++) {
2660 addr
= (union sctp_addr
*)addr_buf
;
2661 af
= sctp_get_af_specific(addr
->v4
.sin_family
);
2662 addr_param_len
= af
->to_addr_param(addr
, &addr_param
);
2663 param
.param_hdr
.type
= flags
;
2664 param
.param_hdr
.length
= htons(paramlen
+ addr_param_len
);
2667 sctp_addto_chunk(retval
, paramlen
, ¶m
);
2668 sctp_addto_chunk(retval
, addr_param_len
, &addr_param
);
2670 addr_buf
+= af
->sockaddr_len
;
2676 * 3.2.4 Set Primary IP Address
2678 * 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
2679 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2680 * | Type =0xC004 | Length = Variable |
2681 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2682 * | ASCONF-Request Correlation ID |
2683 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2684 * | Address Parameter |
2685 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2687 * Create an ASCONF chunk with Set Primary IP address parameter.
2689 struct sctp_chunk
*sctp_make_asconf_set_prim(struct sctp_association
*asoc
,
2690 union sctp_addr
*addr
)
2692 sctp_addip_param_t param
;
2693 struct sctp_chunk
*retval
;
2694 int len
= sizeof(param
);
2695 union sctp_addr_param addrparam
;
2697 struct sctp_af
*af
= sctp_get_af_specific(addr
->v4
.sin_family
);
2699 addrlen
= af
->to_addr_param(addr
, &addrparam
);
2704 /* Create the chunk and make asconf header. */
2705 retval
= sctp_make_asconf(asoc
, addr
, len
);
2709 param
.param_hdr
.type
= SCTP_PARAM_SET_PRIMARY
;
2710 param
.param_hdr
.length
= htons(len
);
2713 sctp_addto_chunk(retval
, sizeof(param
), ¶m
);
2714 sctp_addto_chunk(retval
, addrlen
, &addrparam
);
2719 /* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2721 * 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
2722 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2723 * | Type = 0x80 | Chunk Flags | Chunk Length |
2724 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2726 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2727 * | ASCONF Parameter Response#1 |
2728 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2732 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2733 * | ASCONF Parameter Response#N |
2734 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2736 * Create an ASCONF_ACK chunk with enough space for the parameter responses.
2738 static struct sctp_chunk
*sctp_make_asconf_ack(const struct sctp_association
*asoc
,
2739 __u32 serial
, int vparam_len
)
2741 sctp_addiphdr_t asconf
;
2742 struct sctp_chunk
*retval
;
2743 int length
= sizeof(asconf
) + vparam_len
;
2745 /* Create the chunk. */
2746 retval
= sctp_make_chunk(asoc
, SCTP_CID_ASCONF_ACK
, 0, length
);
2750 asconf
.serial
= htonl(serial
);
2752 retval
->subh
.addip_hdr
=
2753 sctp_addto_chunk(retval
, sizeof(asconf
), &asconf
);
2758 /* Add response parameters to an ASCONF_ACK chunk. */
2759 static void sctp_add_asconf_response(struct sctp_chunk
*chunk
, __be32 crr_id
,
2760 __be16 err_code
, sctp_addip_param_t
*asconf_param
)
2762 sctp_addip_param_t ack_param
;
2763 sctp_errhdr_t err_param
;
2764 int asconf_param_len
= 0;
2765 int err_param_len
= 0;
2766 __be16 response_type
;
2768 if (SCTP_ERROR_NO_ERROR
== err_code
) {
2769 response_type
= SCTP_PARAM_SUCCESS_REPORT
;
2771 response_type
= SCTP_PARAM_ERR_CAUSE
;
2772 err_param_len
= sizeof(err_param
);
2775 ntohs(asconf_param
->param_hdr
.length
);
2778 /* Add Success Indication or Error Cause Indication parameter. */
2779 ack_param
.param_hdr
.type
= response_type
;
2780 ack_param
.param_hdr
.length
= htons(sizeof(ack_param
) +
2783 ack_param
.crr_id
= crr_id
;
2784 sctp_addto_chunk(chunk
, sizeof(ack_param
), &ack_param
);
2786 if (SCTP_ERROR_NO_ERROR
== err_code
)
2789 /* Add Error Cause parameter. */
2790 err_param
.cause
= err_code
;
2791 err_param
.length
= htons(err_param_len
+ asconf_param_len
);
2792 sctp_addto_chunk(chunk
, err_param_len
, &err_param
);
2794 /* Add the failed TLV copied from ASCONF chunk. */
2796 sctp_addto_chunk(chunk
, asconf_param_len
, asconf_param
);
2799 /* Process a asconf parameter. */
2800 static __be16
sctp_process_asconf_param(struct sctp_association
*asoc
,
2801 struct sctp_chunk
*asconf
,
2802 sctp_addip_param_t
*asconf_param
)
2804 struct sctp_transport
*peer
;
2806 union sctp_addr addr
;
2807 union sctp_addr_param
*addr_param
;
2809 addr_param
= (union sctp_addr_param
*)
2810 ((void *)asconf_param
+ sizeof(sctp_addip_param_t
));
2812 af
= sctp_get_af_specific(param_type2af(addr_param
->v4
.param_hdr
.type
));
2814 return SCTP_ERROR_INV_PARAM
;
2816 af
->from_addr_param(&addr
, addr_param
, htons(asoc
->peer
.port
), 0);
2818 /* ADDIP 4.2.1 This parameter MUST NOT contain a broadcast
2819 * or multicast address.
2820 * (note: wildcard is permitted and requires special handling so
2821 * make sure we check for that)
2823 if (!af
->is_any(&addr
) && !af
->addr_valid(&addr
, NULL
, asconf
->skb
))
2824 return SCTP_ERROR_INV_PARAM
;
2826 switch (asconf_param
->param_hdr
.type
) {
2827 case SCTP_PARAM_ADD_IP
:
2829 * If the address 0.0.0.0 or ::0 is provided, the source
2830 * address of the packet MUST be added.
2832 if (af
->is_any(&addr
))
2833 memcpy(&addr
, &asconf
->source
, sizeof(addr
));
2835 /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
2836 * request and does not have the local resources to add this
2837 * new address to the association, it MUST return an Error
2838 * Cause TLV set to the new error code 'Operation Refused
2839 * Due to Resource Shortage'.
2842 peer
= sctp_assoc_add_peer(asoc
, &addr
, GFP_ATOMIC
, SCTP_UNCONFIRMED
);
2844 return SCTP_ERROR_RSRC_LOW
;
2846 /* Start the heartbeat timer. */
2847 if (!mod_timer(&peer
->hb_timer
, sctp_transport_timeout(peer
)))
2848 sctp_transport_hold(peer
);
2850 case SCTP_PARAM_DEL_IP
:
2851 /* ADDIP 4.3 D7) If a request is received to delete the
2852 * last remaining IP address of a peer endpoint, the receiver
2853 * MUST send an Error Cause TLV with the error cause set to the
2854 * new error code 'Request to Delete Last Remaining IP Address'.
2856 if (asoc
->peer
.transport_count
== 1)
2857 return SCTP_ERROR_DEL_LAST_IP
;
2859 /* ADDIP 4.3 D8) If a request is received to delete an IP
2860 * address which is also the source address of the IP packet
2861 * which contained the ASCONF chunk, the receiver MUST reject
2862 * this request. To reject the request the receiver MUST send
2863 * an Error Cause TLV set to the new error code 'Request to
2864 * Delete Source IP Address'
2866 if (sctp_cmp_addr_exact(sctp_source(asconf
), &addr
))
2867 return SCTP_ERROR_DEL_SRC_IP
;
2870 * If the address 0.0.0.0 or ::0 is provided, all
2871 * addresses of the peer except the source address of the
2872 * packet MUST be deleted.
2874 if (af
->is_any(&addr
)) {
2875 sctp_assoc_set_primary(asoc
, asconf
->transport
);
2876 sctp_assoc_del_nonprimary_peers(asoc
,
2879 sctp_assoc_del_peer(asoc
, &addr
);
2881 case SCTP_PARAM_SET_PRIMARY
:
2882 /* ADDIP Section 4.2.4
2883 * If the address 0.0.0.0 or ::0 is provided, the receiver
2884 * MAY mark the source address of the packet as its
2887 if (af
->is_any(&addr
))
2888 memcpy(&addr
.v4
, sctp_source(asconf
), sizeof(addr
));
2890 peer
= sctp_assoc_lookup_paddr(asoc
, &addr
);
2892 return SCTP_ERROR_INV_PARAM
;
2894 sctp_assoc_set_primary(asoc
, peer
);
2897 return SCTP_ERROR_INV_PARAM
;
2901 return SCTP_ERROR_NO_ERROR
;
2904 /* Verify the ASCONF packet before we process it. */
2905 int sctp_verify_asconf(const struct sctp_association
*asoc
,
2906 struct sctp_paramhdr
*param_hdr
, void *chunk_end
,
2907 struct sctp_paramhdr
**errp
) {
2908 sctp_addip_param_t
*asconf_param
;
2909 union sctp_params param
;
2912 param
.v
= (sctp_paramhdr_t
*) param_hdr
;
2913 while (param
.v
<= chunk_end
- sizeof(sctp_paramhdr_t
)) {
2914 length
= ntohs(param
.p
->length
);
2917 if (param
.v
> chunk_end
- length
||
2918 length
< sizeof(sctp_paramhdr_t
))
2921 switch (param
.p
->type
) {
2922 case SCTP_PARAM_ADD_IP
:
2923 case SCTP_PARAM_DEL_IP
:
2924 case SCTP_PARAM_SET_PRIMARY
:
2925 asconf_param
= (sctp_addip_param_t
*)param
.v
;
2926 plen
= ntohs(asconf_param
->param_hdr
.length
);
2927 if (plen
< sizeof(sctp_addip_param_t
) +
2928 sizeof(sctp_paramhdr_t
))
2931 case SCTP_PARAM_SUCCESS_REPORT
:
2932 case SCTP_PARAM_ADAPTATION_LAYER_IND
:
2933 if (length
!= sizeof(sctp_addip_param_t
))
2941 param
.v
+= WORD_ROUND(length
);
2944 if (param
.v
!= chunk_end
)
2950 /* Process an incoming ASCONF chunk with the next expected serial no. and
2951 * return an ASCONF_ACK chunk to be sent in response.
2953 struct sctp_chunk
*sctp_process_asconf(struct sctp_association
*asoc
,
2954 struct sctp_chunk
*asconf
)
2956 sctp_addiphdr_t
*hdr
;
2957 union sctp_addr_param
*addr_param
;
2958 sctp_addip_param_t
*asconf_param
;
2959 struct sctp_chunk
*asconf_ack
;
2965 int all_param_pass
= 1;
2967 chunk_len
= ntohs(asconf
->chunk_hdr
->length
) - sizeof(sctp_chunkhdr_t
);
2968 hdr
= (sctp_addiphdr_t
*)asconf
->skb
->data
;
2969 serial
= ntohl(hdr
->serial
);
2971 /* Skip the addiphdr and store a pointer to address parameter. */
2972 length
= sizeof(sctp_addiphdr_t
);
2973 addr_param
= (union sctp_addr_param
*)(asconf
->skb
->data
+ length
);
2974 chunk_len
-= length
;
2976 /* Skip the address parameter and store a pointer to the first
2979 length
= ntohs(addr_param
->v4
.param_hdr
.length
);
2980 asconf_param
= (sctp_addip_param_t
*)((void *)addr_param
+ length
);
2981 chunk_len
-= length
;
2983 /* create an ASCONF_ACK chunk.
2984 * Based on the definitions of parameters, we know that the size of
2985 * ASCONF_ACK parameters are less than or equal to the twice of ASCONF
2988 asconf_ack
= sctp_make_asconf_ack(asoc
, serial
, chunk_len
* 2);
2992 /* Process the TLVs contained within the ASCONF chunk. */
2993 while (chunk_len
> 0) {
2994 err_code
= sctp_process_asconf_param(asoc
, asconf
,
2997 * If an error response is received for a TLV parameter,
2998 * all TLVs with no response before the failed TLV are
2999 * considered successful if not reported. All TLVs after
3000 * the failed response are considered unsuccessful unless
3001 * a specific success indication is present for the parameter.
3003 if (SCTP_ERROR_NO_ERROR
!= err_code
)
3006 if (!all_param_pass
)
3007 sctp_add_asconf_response(asconf_ack
,
3008 asconf_param
->crr_id
, err_code
,
3011 /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
3012 * an IP address sends an 'Out of Resource' in its response, it
3013 * MUST also fail any subsequent add or delete requests bundled
3016 if (SCTP_ERROR_RSRC_LOW
== err_code
)
3019 /* Move to the next ASCONF param. */
3020 length
= ntohs(asconf_param
->param_hdr
.length
);
3021 asconf_param
= (sctp_addip_param_t
*)((void *)asconf_param
+
3023 chunk_len
-= length
;
3027 asoc
->peer
.addip_serial
++;
3029 /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
3030 * after freeing the reference to old asconf ack if any.
3033 sctp_chunk_hold(asconf_ack
);
3034 list_add_tail(&asconf_ack
->transmitted_list
,
3035 &asoc
->asconf_ack_list
);
3041 /* Process a asconf parameter that is successfully acked. */
3042 static int sctp_asconf_param_success(struct sctp_association
*asoc
,
3043 sctp_addip_param_t
*asconf_param
)
3046 union sctp_addr addr
;
3047 struct sctp_bind_addr
*bp
= &asoc
->base
.bind_addr
;
3048 union sctp_addr_param
*addr_param
;
3049 struct list_head
*pos
;
3050 struct sctp_transport
*transport
;
3051 struct sctp_sockaddr_entry
*saddr
;
3054 addr_param
= (union sctp_addr_param
*)
3055 ((void *)asconf_param
+ sizeof(sctp_addip_param_t
));
3057 /* We have checked the packet before, so we do not check again. */
3058 af
= sctp_get_af_specific(param_type2af(addr_param
->v4
.param_hdr
.type
));
3059 af
->from_addr_param(&addr
, addr_param
, htons(bp
->port
), 0);
3061 switch (asconf_param
->param_hdr
.type
) {
3062 case SCTP_PARAM_ADD_IP
:
3063 /* This is always done in BH context with a socket lock
3064 * held, so the list can not change.
3067 list_for_each_entry(saddr
, &bp
->address_list
, list
) {
3068 if (sctp_cmp_addr_exact(&saddr
->a
, &addr
))
3069 saddr
->state
= SCTP_ADDR_SRC
;
3073 case SCTP_PARAM_DEL_IP
:
3075 retval
= sctp_del_bind_addr(bp
, &addr
);
3077 list_for_each(pos
, &asoc
->peer
.transport_addr_list
) {
3078 transport
= list_entry(pos
, struct sctp_transport
,
3080 dst_release(transport
->dst
);
3081 sctp_transport_route(transport
, NULL
,
3082 sctp_sk(asoc
->base
.sk
));
3092 /* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
3093 * for the given asconf parameter. If there is no response for this parameter,
3094 * return the error code based on the third argument 'no_err'.
3096 * A7) If an error response is received for a TLV parameter, all TLVs with no
3097 * response before the failed TLV are considered successful if not reported.
3098 * All TLVs after the failed response are considered unsuccessful unless a
3099 * specific success indication is present for the parameter.
3101 static __be16
sctp_get_asconf_response(struct sctp_chunk
*asconf_ack
,
3102 sctp_addip_param_t
*asconf_param
,
3105 sctp_addip_param_t
*asconf_ack_param
;
3106 sctp_errhdr_t
*err_param
;
3112 err_code
= SCTP_ERROR_NO_ERROR
;
3114 err_code
= SCTP_ERROR_REQ_REFUSED
;
3116 asconf_ack_len
= ntohs(asconf_ack
->chunk_hdr
->length
) -
3117 sizeof(sctp_chunkhdr_t
);
3119 /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
3120 * the first asconf_ack parameter.
3122 length
= sizeof(sctp_addiphdr_t
);
3123 asconf_ack_param
= (sctp_addip_param_t
*)(asconf_ack
->skb
->data
+
3125 asconf_ack_len
-= length
;
3127 while (asconf_ack_len
> 0) {
3128 if (asconf_ack_param
->crr_id
== asconf_param
->crr_id
) {
3129 switch(asconf_ack_param
->param_hdr
.type
) {
3130 case SCTP_PARAM_SUCCESS_REPORT
:
3131 return SCTP_ERROR_NO_ERROR
;
3132 case SCTP_PARAM_ERR_CAUSE
:
3133 length
= sizeof(sctp_addip_param_t
);
3134 err_param
= (sctp_errhdr_t
*)
3135 ((void *)asconf_ack_param
+ length
);
3136 asconf_ack_len
-= length
;
3137 if (asconf_ack_len
> 0)
3138 return err_param
->cause
;
3140 return SCTP_ERROR_INV_PARAM
;
3143 return SCTP_ERROR_INV_PARAM
;
3147 length
= ntohs(asconf_ack_param
->param_hdr
.length
);
3148 asconf_ack_param
= (sctp_addip_param_t
*)
3149 ((void *)asconf_ack_param
+ length
);
3150 asconf_ack_len
-= length
;
3156 /* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
3157 int sctp_process_asconf_ack(struct sctp_association
*asoc
,
3158 struct sctp_chunk
*asconf_ack
)
3160 struct sctp_chunk
*asconf
= asoc
->addip_last_asconf
;
3161 union sctp_addr_param
*addr_param
;
3162 sctp_addip_param_t
*asconf_param
;
3164 int asconf_len
= asconf
->skb
->len
;
3165 int all_param_pass
= 0;
3168 __be16 err_code
= SCTP_ERROR_NO_ERROR
;
3170 /* Skip the chunkhdr and addiphdr from the last asconf sent and store
3171 * a pointer to address parameter.
3173 length
= sizeof(sctp_addip_chunk_t
);
3174 addr_param
= (union sctp_addr_param
*)(asconf
->skb
->data
+ length
);
3175 asconf_len
-= length
;
3177 /* Skip the address parameter in the last asconf sent and store a
3178 * pointer to the first asconf parameter.
3180 length
= ntohs(addr_param
->v4
.param_hdr
.length
);
3181 asconf_param
= (sctp_addip_param_t
*)((void *)addr_param
+ length
);
3182 asconf_len
-= length
;
3185 * A8) If there is no response(s) to specific TLV parameter(s), and no
3186 * failures are indicated, then all request(s) are considered
3189 if (asconf_ack
->skb
->len
== sizeof(sctp_addiphdr_t
))
3192 /* Process the TLVs contained in the last sent ASCONF chunk. */
3193 while (asconf_len
> 0) {
3195 err_code
= SCTP_ERROR_NO_ERROR
;
3197 err_code
= sctp_get_asconf_response(asconf_ack
,
3200 if (no_err
&& (SCTP_ERROR_NO_ERROR
!= err_code
))
3205 case SCTP_ERROR_NO_ERROR
:
3206 retval
= sctp_asconf_param_success(asoc
, asconf_param
);
3209 case SCTP_ERROR_RSRC_LOW
:
3213 case SCTP_ERROR_INV_PARAM
:
3214 /* Disable sending this type of asconf parameter in
3217 asoc
->peer
.addip_disabled_mask
|=
3218 asconf_param
->param_hdr
.type
;
3221 case SCTP_ERROR_REQ_REFUSED
:
3222 case SCTP_ERROR_DEL_LAST_IP
:
3223 case SCTP_ERROR_DEL_SRC_IP
:
3228 /* Skip the processed asconf parameter and move to the next
3231 length
= ntohs(asconf_param
->param_hdr
.length
);
3232 asconf_param
= (sctp_addip_param_t
*)((void *)asconf_param
+
3234 asconf_len
-= length
;
3237 /* Free the cached last sent asconf chunk. */
3238 list_del_init(&asconf
->transmitted_list
);
3239 sctp_chunk_free(asconf
);
3240 asoc
->addip_last_asconf
= NULL
;
3242 /* Send the next asconf chunk from the addip chunk queue. */
3243 if (!list_empty(&asoc
->addip_chunk_list
)) {
3244 struct list_head
*entry
= asoc
->addip_chunk_list
.next
;
3245 asconf
= list_entry(entry
, struct sctp_chunk
, list
);
3247 list_del_init(entry
);
3249 /* Hold the chunk until an ASCONF_ACK is received. */
3250 sctp_chunk_hold(asconf
);
3251 if (sctp_primitive_ASCONF(asoc
, asconf
))
3252 sctp_chunk_free(asconf
);
3254 asoc
->addip_last_asconf
= asconf
;
3260 /* Make a FWD TSN chunk. */
3261 struct sctp_chunk
*sctp_make_fwdtsn(const struct sctp_association
*asoc
,
3262 __u32 new_cum_tsn
, size_t nstreams
,
3263 struct sctp_fwdtsn_skip
*skiplist
)
3265 struct sctp_chunk
*retval
= NULL
;
3266 struct sctp_fwdtsn_chunk
*ftsn_chunk
;
3267 struct sctp_fwdtsn_hdr ftsn_hdr
;
3268 struct sctp_fwdtsn_skip skip
;
3272 hint
= (nstreams
+ 1) * sizeof(__u32
);
3274 retval
= sctp_make_chunk(asoc
, SCTP_CID_FWD_TSN
, 0, hint
);
3279 ftsn_chunk
= (struct sctp_fwdtsn_chunk
*)retval
->subh
.fwdtsn_hdr
;
3281 ftsn_hdr
.new_cum_tsn
= htonl(new_cum_tsn
);
3282 retval
->subh
.fwdtsn_hdr
=
3283 sctp_addto_chunk(retval
, sizeof(ftsn_hdr
), &ftsn_hdr
);
3285 for (i
= 0; i
< nstreams
; i
++) {
3286 skip
.stream
= skiplist
[i
].stream
;
3287 skip
.ssn
= skiplist
[i
].ssn
;
3288 sctp_addto_chunk(retval
, sizeof(skip
), &skip
);