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 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 /* A helper to initialize an op error inside a
128 * provided chunk, as most cause codes will be embedded inside an
129 * abort chunk. Differs from sctp_init_cause in that it won't oops
130 * if there isn't enough space in the op error chunk
132 int sctp_init_cause_fixed(struct sctp_chunk
*chunk
, __be16 cause_code
,
138 /* Cause code constants are now defined in network order. */
139 err
.cause
= cause_code
;
140 len
= sizeof(sctp_errhdr_t
) + paylen
;
141 err
.length
= htons(len
);
143 if (skb_tailroom(chunk
->skb
) < len
)
145 chunk
->subh
.err_hdr
= sctp_addto_chunk_fixed(chunk
,
146 sizeof(sctp_errhdr_t
),
150 /* 3.3.2 Initiation (INIT) (1)
152 * This chunk is used to initiate a SCTP association between two
153 * endpoints. The format of the INIT chunk is shown below:
156 * 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
157 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
158 * | Type = 1 | Chunk Flags | Chunk Length |
159 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
161 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
162 * | Advertised Receiver Window Credit (a_rwnd) |
163 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
164 * | Number of Outbound Streams | Number of Inbound Streams |
165 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
167 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
169 * / Optional/Variable-Length Parameters /
171 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
174 * The INIT chunk contains the following parameters. Unless otherwise
175 * noted, each parameter MUST only be included once in the INIT chunk.
177 * Fixed Parameters Status
178 * ----------------------------------------------
179 * Initiate Tag Mandatory
180 * Advertised Receiver Window Credit Mandatory
181 * Number of Outbound Streams Mandatory
182 * Number of Inbound Streams Mandatory
183 * Initial TSN Mandatory
185 * Variable Parameters Status Type Value
186 * -------------------------------------------------------------
187 * IPv4 Address (Note 1) Optional 5
188 * IPv6 Address (Note 1) Optional 6
189 * Cookie Preservative Optional 9
190 * Reserved for ECN Capable (Note 2) Optional 32768 (0x8000)
191 * Host Name Address (Note 3) Optional 11
192 * Supported Address Types (Note 4) Optional 12
194 struct sctp_chunk
*sctp_make_init(const struct sctp_association
*asoc
,
195 const struct sctp_bind_addr
*bp
,
196 gfp_t gfp
, int vparam_len
)
199 union sctp_params addrs
;
201 struct sctp_chunk
*retval
= NULL
;
202 int num_types
, addrs_len
= 0;
203 struct sctp_sock
*sp
;
204 sctp_supported_addrs_param_t sat
;
206 sctp_adaptation_ind_param_t aiparam
;
207 sctp_supported_ext_param_t ext_param
;
210 sctp_paramhdr_t
*auth_chunks
= NULL
,
213 /* RFC 2960 3.3.2 Initiation (INIT) (1)
215 * Note 1: The INIT chunks can contain multiple addresses that
216 * can be IPv4 and/or IPv6 in any combination.
220 /* Convert the provided bind address list to raw format. */
221 addrs
= sctp_bind_addrs_to_raw(bp
, &addrs_len
, gfp
);
223 init
.init_tag
= htonl(asoc
->c
.my_vtag
);
224 init
.a_rwnd
= htonl(asoc
->rwnd
);
225 init
.num_outbound_streams
= htons(asoc
->c
.sinit_num_ostreams
);
226 init
.num_inbound_streams
= htons(asoc
->c
.sinit_max_instreams
);
227 init
.initial_tsn
= htonl(asoc
->c
.initial_tsn
);
229 /* How many address types are needed? */
230 sp
= sctp_sk(asoc
->base
.sk
);
231 num_types
= sp
->pf
->supported_addrs(sp
, types
);
233 chunksize
= sizeof(init
) + addrs_len
;
234 chunksize
+= WORD_ROUND(SCTP_SAT_LEN(num_types
));
235 chunksize
+= sizeof(ecap_param
);
237 if (sctp_prsctp_enable
)
238 chunksize
+= sizeof(prsctp_param
);
240 /* ADDIP: Section 4.2.7:
241 * An implementation supporting this extension [ADDIP] MUST list
242 * the ASCONF,the ASCONF-ACK, and the AUTH chunks in its INIT and
243 * INIT-ACK parameters.
245 if (sctp_addip_enable
) {
246 extensions
[num_ext
] = SCTP_CID_ASCONF
;
247 extensions
[num_ext
+1] = SCTP_CID_ASCONF_ACK
;
251 chunksize
+= sizeof(aiparam
);
252 chunksize
+= vparam_len
;
254 /* Account for AUTH related parameters */
255 if (sctp_auth_enable
) {
256 /* Add random parameter length*/
257 chunksize
+= sizeof(asoc
->c
.auth_random
);
259 /* Add HMACS parameter length if any were defined */
260 auth_hmacs
= (sctp_paramhdr_t
*)asoc
->c
.auth_hmacs
;
261 if (auth_hmacs
->length
)
262 chunksize
+= WORD_ROUND(ntohs(auth_hmacs
->length
));
266 /* Add CHUNKS parameter length */
267 auth_chunks
= (sctp_paramhdr_t
*)asoc
->c
.auth_chunks
;
268 if (auth_chunks
->length
)
269 chunksize
+= WORD_ROUND(ntohs(auth_chunks
->length
));
273 extensions
[num_ext
] = SCTP_CID_AUTH
;
277 /* If we have any extensions to report, account for that */
279 chunksize
+= WORD_ROUND(sizeof(sctp_supported_ext_param_t
) +
282 /* RFC 2960 3.3.2 Initiation (INIT) (1)
284 * Note 3: An INIT chunk MUST NOT contain more than one Host
285 * Name address parameter. Moreover, the sender of the INIT
286 * MUST NOT combine any other address types with the Host Name
287 * address in the INIT. The receiver of INIT MUST ignore any
288 * other address types if the Host Name address parameter is
289 * present in the received INIT chunk.
291 * PLEASE DO NOT FIXME [This version does not support Host Name.]
294 retval
= sctp_make_chunk(asoc
, SCTP_CID_INIT
, 0, chunksize
);
298 retval
->subh
.init_hdr
=
299 sctp_addto_chunk(retval
, sizeof(init
), &init
);
300 retval
->param_hdr
.v
=
301 sctp_addto_chunk(retval
, addrs_len
, addrs
.v
);
303 /* RFC 2960 3.3.2 Initiation (INIT) (1)
305 * Note 4: This parameter, when present, specifies all the
306 * address types the sending endpoint can support. The absence
307 * of this parameter indicates that the sending endpoint can
308 * support any address type.
310 sat
.param_hdr
.type
= SCTP_PARAM_SUPPORTED_ADDRESS_TYPES
;
311 sat
.param_hdr
.length
= htons(SCTP_SAT_LEN(num_types
));
312 sctp_addto_chunk(retval
, sizeof(sat
), &sat
);
313 sctp_addto_chunk(retval
, num_types
* sizeof(__u16
), &types
);
315 sctp_addto_chunk(retval
, sizeof(ecap_param
), &ecap_param
);
317 /* Add the supported extensions parameter. Be nice and add this
318 * fist before addiding the parameters for the extensions themselves
321 ext_param
.param_hdr
.type
= SCTP_PARAM_SUPPORTED_EXT
;
322 ext_param
.param_hdr
.length
=
323 htons(sizeof(sctp_supported_ext_param_t
) + num_ext
);
324 sctp_addto_chunk(retval
, sizeof(sctp_supported_ext_param_t
),
326 sctp_addto_param(retval
, num_ext
, extensions
);
329 if (sctp_prsctp_enable
)
330 sctp_addto_chunk(retval
, sizeof(prsctp_param
), &prsctp_param
);
332 aiparam
.param_hdr
.type
= SCTP_PARAM_ADAPTATION_LAYER_IND
;
333 aiparam
.param_hdr
.length
= htons(sizeof(aiparam
));
334 aiparam
.adaptation_ind
= htonl(sp
->adaptation_ind
);
335 sctp_addto_chunk(retval
, sizeof(aiparam
), &aiparam
);
337 /* Add SCTP-AUTH chunks to the parameter list */
338 if (sctp_auth_enable
) {
339 sctp_addto_chunk(retval
, sizeof(asoc
->c
.auth_random
),
340 asoc
->c
.auth_random
);
342 sctp_addto_chunk(retval
, ntohs(auth_hmacs
->length
),
345 sctp_addto_chunk(retval
, ntohs(auth_chunks
->length
),
353 struct sctp_chunk
*sctp_make_init_ack(const struct sctp_association
*asoc
,
354 const struct sctp_chunk
*chunk
,
355 gfp_t gfp
, int unkparam_len
)
357 sctp_inithdr_t initack
;
358 struct sctp_chunk
*retval
;
359 union sctp_params addrs
;
361 sctp_cookie_param_t
*cookie
;
364 sctp_adaptation_ind_param_t aiparam
;
365 sctp_supported_ext_param_t ext_param
;
368 sctp_paramhdr_t
*auth_chunks
= NULL
,
374 /* Note: there may be no addresses to embed. */
375 addrs
= sctp_bind_addrs_to_raw(&asoc
->base
.bind_addr
, &addrs_len
, gfp
);
377 initack
.init_tag
= htonl(asoc
->c
.my_vtag
);
378 initack
.a_rwnd
= htonl(asoc
->rwnd
);
379 initack
.num_outbound_streams
= htons(asoc
->c
.sinit_num_ostreams
);
380 initack
.num_inbound_streams
= htons(asoc
->c
.sinit_max_instreams
);
381 initack
.initial_tsn
= htonl(asoc
->c
.initial_tsn
);
383 /* FIXME: We really ought to build the cookie right
384 * into the packet instead of allocating more fresh memory.
386 cookie
= sctp_pack_cookie(asoc
->ep
, asoc
, chunk
, &cookie_len
,
391 /* Calculate the total size of allocation, include the reserved
392 * space for reporting unknown parameters if it is specified.
394 chunksize
= sizeof(initack
) + addrs_len
+ cookie_len
+ unkparam_len
;
396 /* Tell peer that we'll do ECN only if peer advertised such cap. */
397 if (asoc
->peer
.ecn_capable
)
398 chunksize
+= sizeof(ecap_param
);
400 if (sctp_prsctp_enable
)
401 chunksize
+= sizeof(prsctp_param
);
403 if (sctp_addip_enable
) {
404 extensions
[num_ext
] = SCTP_CID_ASCONF
;
405 extensions
[num_ext
+1] = SCTP_CID_ASCONF_ACK
;
409 chunksize
+= sizeof(aiparam
);
411 if (asoc
->peer
.auth_capable
) {
412 auth_random
= (sctp_paramhdr_t
*)asoc
->c
.auth_random
;
413 chunksize
+= ntohs(auth_random
->length
);
415 auth_hmacs
= (sctp_paramhdr_t
*)asoc
->c
.auth_hmacs
;
416 if (auth_hmacs
->length
)
417 chunksize
+= WORD_ROUND(ntohs(auth_hmacs
->length
));
421 auth_chunks
= (sctp_paramhdr_t
*)asoc
->c
.auth_chunks
;
422 if (auth_chunks
->length
)
423 chunksize
+= WORD_ROUND(ntohs(auth_chunks
->length
));
427 extensions
[num_ext
] = SCTP_CID_AUTH
;
432 chunksize
+= WORD_ROUND(sizeof(sctp_supported_ext_param_t
) +
435 /* Now allocate and fill out the chunk. */
436 retval
= sctp_make_chunk(asoc
, SCTP_CID_INIT_ACK
, 0, chunksize
);
440 /* Per the advice in RFC 2960 6.4, send this reply to
441 * the source of the INIT packet.
443 retval
->transport
= chunk
->transport
;
444 retval
->subh
.init_hdr
=
445 sctp_addto_chunk(retval
, sizeof(initack
), &initack
);
446 retval
->param_hdr
.v
= sctp_addto_chunk(retval
, addrs_len
, addrs
.v
);
447 sctp_addto_chunk(retval
, cookie_len
, cookie
);
448 if (asoc
->peer
.ecn_capable
)
449 sctp_addto_chunk(retval
, sizeof(ecap_param
), &ecap_param
);
451 ext_param
.param_hdr
.type
= SCTP_PARAM_SUPPORTED_EXT
;
452 ext_param
.param_hdr
.length
=
453 htons(sizeof(sctp_supported_ext_param_t
) + num_ext
);
454 sctp_addto_chunk(retval
, sizeof(sctp_supported_ext_param_t
),
456 sctp_addto_param(retval
, num_ext
, extensions
);
458 if (asoc
->peer
.prsctp_capable
)
459 sctp_addto_chunk(retval
, sizeof(prsctp_param
), &prsctp_param
);
461 aiparam
.param_hdr
.type
= SCTP_PARAM_ADAPTATION_LAYER_IND
;
462 aiparam
.param_hdr
.length
= htons(sizeof(aiparam
));
463 aiparam
.adaptation_ind
= htonl(sctp_sk(asoc
->base
.sk
)->adaptation_ind
);
464 sctp_addto_chunk(retval
, sizeof(aiparam
), &aiparam
);
466 if (asoc
->peer
.auth_capable
) {
467 sctp_addto_chunk(retval
, ntohs(auth_random
->length
),
470 sctp_addto_chunk(retval
, ntohs(auth_hmacs
->length
),
473 sctp_addto_chunk(retval
, ntohs(auth_chunks
->length
),
477 /* We need to remove the const qualifier at this point. */
478 retval
->asoc
= (struct sctp_association
*) asoc
;
480 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
482 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
483 * HEARTBEAT ACK, * etc.) to the same destination transport
484 * address from which it received the DATA or control chunk
485 * to which it is replying.
487 * [INIT ACK back to where the INIT came from.]
490 retval
->transport
= chunk
->transport
;
499 /* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
501 * This chunk is used only during the initialization of an association.
502 * It is sent by the initiator of an association to its peer to complete
503 * the initialization process. This chunk MUST precede any DATA chunk
504 * sent within the association, but MAY be bundled with one or more DATA
505 * chunks in the same packet.
508 * 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
509 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
510 * | Type = 10 |Chunk Flags | Length |
511 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
514 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
518 * Set to zero on transmit and ignored on receipt.
520 * Length: 16 bits (unsigned integer)
522 * Set to the size of the chunk in bytes, including the 4 bytes of
523 * the chunk header and the size of the Cookie.
525 * Cookie: variable size
527 * This field must contain the exact cookie received in the
528 * State Cookie parameter from the previous INIT ACK.
530 * An implementation SHOULD make the cookie as small as possible
531 * to insure interoperability.
533 struct sctp_chunk
*sctp_make_cookie_echo(const struct sctp_association
*asoc
,
534 const struct sctp_chunk
*chunk
)
536 struct sctp_chunk
*retval
;
540 cookie
= asoc
->peer
.cookie
;
541 cookie_len
= asoc
->peer
.cookie_len
;
543 /* Build a cookie echo chunk. */
544 retval
= sctp_make_chunk(asoc
, SCTP_CID_COOKIE_ECHO
, 0, cookie_len
);
547 retval
->subh
.cookie_hdr
=
548 sctp_addto_chunk(retval
, cookie_len
, cookie
);
550 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
552 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
553 * HEARTBEAT ACK, * etc.) to the same destination transport
554 * address from which it * received the DATA or control chunk
555 * to which it is replying.
557 * [COOKIE ECHO back to where the INIT ACK came from.]
560 retval
->transport
= chunk
->transport
;
566 /* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
568 * This chunk is used only during the initialization of an
569 * association. It is used to acknowledge the receipt of a COOKIE
570 * ECHO chunk. This chunk MUST precede any DATA or SACK chunk sent
571 * within the association, but MAY be bundled with one or more DATA
572 * chunks or SACK chunk in the same SCTP packet.
575 * 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
576 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
577 * | Type = 11 |Chunk Flags | Length = 4 |
578 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
580 * Chunk Flags: 8 bits
582 * Set to zero on transmit and ignored on receipt.
584 struct sctp_chunk
*sctp_make_cookie_ack(const struct sctp_association
*asoc
,
585 const struct sctp_chunk
*chunk
)
587 struct sctp_chunk
*retval
;
589 retval
= sctp_make_chunk(asoc
, SCTP_CID_COOKIE_ACK
, 0, 0);
591 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
593 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
594 * HEARTBEAT ACK, * etc.) to the same destination transport
595 * address from which it * received the DATA or control chunk
596 * to which it is replying.
598 * [COOKIE ACK back to where the COOKIE ECHO came from.]
601 retval
->transport
= chunk
->transport
;
607 * Appendix A: Explicit Congestion Notification:
610 * RFC 2481 details a specific bit for a sender to send in the header of
611 * its next outbound TCP segment to indicate to its peer that it has
612 * reduced its congestion window. This is termed the CWR bit. For
613 * SCTP the same indication is made by including the CWR chunk.
614 * This chunk contains one data element, i.e. the TSN number that
615 * was sent in the ECNE chunk. This element represents the lowest
616 * TSN number in the datagram that was originally marked with the
620 * 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
621 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
622 * | Chunk Type=13 | Flags=00000000| Chunk Length = 8 |
623 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
624 * | Lowest TSN Number |
625 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
627 * Note: The CWR is considered a Control chunk.
629 struct sctp_chunk
*sctp_make_cwr(const struct sctp_association
*asoc
,
630 const __u32 lowest_tsn
,
631 const struct sctp_chunk
*chunk
)
633 struct sctp_chunk
*retval
;
636 cwr
.lowest_tsn
= htonl(lowest_tsn
);
637 retval
= sctp_make_chunk(asoc
, SCTP_CID_ECN_CWR
, 0,
638 sizeof(sctp_cwrhdr_t
));
643 retval
->subh
.ecn_cwr_hdr
=
644 sctp_addto_chunk(retval
, sizeof(cwr
), &cwr
);
646 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
648 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
649 * HEARTBEAT ACK, * etc.) to the same destination transport
650 * address from which it * received the DATA or control chunk
651 * to which it is replying.
653 * [Report a reduced congestion window back to where the ECNE
657 retval
->transport
= chunk
->transport
;
663 /* Make an ECNE chunk. This is a congestion experienced report. */
664 struct sctp_chunk
*sctp_make_ecne(const struct sctp_association
*asoc
,
665 const __u32 lowest_tsn
)
667 struct sctp_chunk
*retval
;
670 ecne
.lowest_tsn
= htonl(lowest_tsn
);
671 retval
= sctp_make_chunk(asoc
, SCTP_CID_ECN_ECNE
, 0,
672 sizeof(sctp_ecnehdr_t
));
675 retval
->subh
.ecne_hdr
=
676 sctp_addto_chunk(retval
, sizeof(ecne
), &ecne
);
682 /* Make a DATA chunk for the given association from the provided
683 * parameters. However, do not populate the data payload.
685 struct sctp_chunk
*sctp_make_datafrag_empty(struct sctp_association
*asoc
,
686 const struct sctp_sndrcvinfo
*sinfo
,
687 int data_len
, __u8 flags
, __u16 ssn
)
689 struct sctp_chunk
*retval
;
690 struct sctp_datahdr dp
;
693 /* We assign the TSN as LATE as possible, not here when
694 * creating the chunk.
697 dp
.stream
= htons(sinfo
->sinfo_stream
);
698 dp
.ppid
= sinfo
->sinfo_ppid
;
700 /* Set the flags for an unordered send. */
701 if (sinfo
->sinfo_flags
& SCTP_UNORDERED
) {
702 flags
|= SCTP_DATA_UNORDERED
;
707 chunk_len
= sizeof(dp
) + data_len
;
708 retval
= sctp_make_chunk(asoc
, SCTP_CID_DATA
, flags
, chunk_len
);
712 retval
->subh
.data_hdr
= sctp_addto_chunk(retval
, sizeof(dp
), &dp
);
713 memcpy(&retval
->sinfo
, sinfo
, sizeof(struct sctp_sndrcvinfo
));
719 /* Create a selective ackowledgement (SACK) for the given
720 * association. This reports on which TSN's we've seen to date,
721 * including duplicates and gaps.
723 struct sctp_chunk
*sctp_make_sack(const struct sctp_association
*asoc
)
725 struct sctp_chunk
*retval
;
726 struct sctp_sackhdr sack
;
729 __u16 num_gabs
, num_dup_tsns
;
730 struct sctp_tsnmap
*map
= (struct sctp_tsnmap
*)&asoc
->peer
.tsn_map
;
732 ctsn
= sctp_tsnmap_get_ctsn(map
);
733 SCTP_DEBUG_PRINTK("sackCTSNAck sent: 0x%x.\n", ctsn
);
735 /* How much room is needed in the chunk? */
736 num_gabs
= sctp_tsnmap_num_gabs(map
);
737 num_dup_tsns
= sctp_tsnmap_num_dups(map
);
739 /* Initialize the SACK header. */
740 sack
.cum_tsn_ack
= htonl(ctsn
);
741 sack
.a_rwnd
= htonl(asoc
->a_rwnd
);
742 sack
.num_gap_ack_blocks
= htons(num_gabs
);
743 sack
.num_dup_tsns
= htons(num_dup_tsns
);
746 + sizeof(struct sctp_gap_ack_block
) * num_gabs
747 + sizeof(__u32
) * num_dup_tsns
;
749 /* Create the chunk. */
750 retval
= sctp_make_chunk(asoc
, SCTP_CID_SACK
, 0, len
);
754 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
756 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
757 * HEARTBEAT ACK, etc.) to the same destination transport
758 * address from which it received the DATA or control chunk to
759 * which it is replying. This rule should also be followed if
760 * the endpoint is bundling DATA chunks together with the
763 * However, when acknowledging multiple DATA chunks received
764 * in packets from different source addresses in a single
765 * SACK, the SACK chunk may be transmitted to one of the
766 * destination transport addresses from which the DATA or
767 * control chunks being acknowledged were received.
769 * [BUG: We do not implement the following paragraph.
770 * Perhaps we should remember the last transport we used for a
771 * SACK and avoid that (if possible) if we have seen any
772 * duplicates. --piggy]
774 * When a receiver of a duplicate DATA chunk sends a SACK to a
775 * multi- homed endpoint it MAY be beneficial to vary the
776 * destination address and not use the source address of the
777 * DATA chunk. The reason being that receiving a duplicate
778 * from a multi-homed endpoint might indicate that the return
779 * path (as specified in the source address of the DATA chunk)
780 * for the SACK is broken.
782 * [Send to the address from which we last received a DATA chunk.]
784 retval
->transport
= asoc
->peer
.last_data_from
;
786 retval
->subh
.sack_hdr
=
787 sctp_addto_chunk(retval
, sizeof(sack
), &sack
);
789 /* Add the gap ack block information. */
791 sctp_addto_chunk(retval
, sizeof(__u32
) * num_gabs
,
792 sctp_tsnmap_get_gabs(map
));
794 /* Add the duplicate TSN information. */
796 sctp_addto_chunk(retval
, sizeof(__u32
) * num_dup_tsns
,
797 sctp_tsnmap_get_dups(map
));
803 /* Make a SHUTDOWN chunk. */
804 struct sctp_chunk
*sctp_make_shutdown(const struct sctp_association
*asoc
,
805 const struct sctp_chunk
*chunk
)
807 struct sctp_chunk
*retval
;
808 sctp_shutdownhdr_t shut
;
811 ctsn
= sctp_tsnmap_get_ctsn(&asoc
->peer
.tsn_map
);
812 shut
.cum_tsn_ack
= htonl(ctsn
);
814 retval
= sctp_make_chunk(asoc
, SCTP_CID_SHUTDOWN
, 0,
815 sizeof(sctp_shutdownhdr_t
));
819 retval
->subh
.shutdown_hdr
=
820 sctp_addto_chunk(retval
, sizeof(shut
), &shut
);
823 retval
->transport
= chunk
->transport
;
828 struct sctp_chunk
*sctp_make_shutdown_ack(const struct sctp_association
*asoc
,
829 const struct sctp_chunk
*chunk
)
831 struct sctp_chunk
*retval
;
833 retval
= sctp_make_chunk(asoc
, SCTP_CID_SHUTDOWN_ACK
, 0, 0);
835 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
837 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
838 * HEARTBEAT ACK, * etc.) to the same destination transport
839 * address from which it * received the DATA or control chunk
840 * to which it is replying.
842 * [ACK back to where the SHUTDOWN came from.]
845 retval
->transport
= chunk
->transport
;
850 struct sctp_chunk
*sctp_make_shutdown_complete(
851 const struct sctp_association
*asoc
,
852 const struct sctp_chunk
*chunk
)
854 struct sctp_chunk
*retval
;
857 /* Set the T-bit if we have no association (vtag will be
860 flags
|= asoc
? 0 : SCTP_CHUNK_FLAG_T
;
862 retval
= sctp_make_chunk(asoc
, SCTP_CID_SHUTDOWN_COMPLETE
, flags
, 0);
864 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
866 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
867 * HEARTBEAT ACK, * etc.) to the same destination transport
868 * address from which it * received the DATA or control chunk
869 * to which it is replying.
871 * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
875 retval
->transport
= chunk
->transport
;
880 /* Create an ABORT. Note that we set the T bit if we have no
881 * association, except when responding to an INIT (sctpimpguide 2.41).
883 struct sctp_chunk
*sctp_make_abort(const struct sctp_association
*asoc
,
884 const struct sctp_chunk
*chunk
,
887 struct sctp_chunk
*retval
;
890 /* Set the T-bit if we have no association and 'chunk' is not
891 * an INIT (vtag will be reflected).
894 if (chunk
&& chunk
->chunk_hdr
&&
895 chunk
->chunk_hdr
->type
== SCTP_CID_INIT
)
898 flags
= SCTP_CHUNK_FLAG_T
;
901 retval
= sctp_make_chunk(asoc
, SCTP_CID_ABORT
, flags
, hint
);
903 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
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.
910 * [ABORT back to where the offender came from.]
913 retval
->transport
= chunk
->transport
;
918 /* Helper to create ABORT with a NO_USER_DATA error. */
919 struct sctp_chunk
*sctp_make_abort_no_data(
920 const struct sctp_association
*asoc
,
921 const struct sctp_chunk
*chunk
, __u32 tsn
)
923 struct sctp_chunk
*retval
;
926 retval
= sctp_make_abort(asoc
, chunk
, sizeof(sctp_errhdr_t
)
932 /* Put the tsn back into network byte order. */
933 payload
= htonl(tsn
);
934 sctp_init_cause(retval
, SCTP_ERROR_NO_DATA
, sizeof(payload
));
935 sctp_addto_chunk(retval
, sizeof(payload
), (const void *)&payload
);
937 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
939 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
940 * HEARTBEAT ACK, * etc.) to the same destination transport
941 * address from which it * received the DATA or control chunk
942 * to which it is replying.
944 * [ABORT back to where the offender came from.]
947 retval
->transport
= chunk
->transport
;
953 /* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error. */
954 struct sctp_chunk
*sctp_make_abort_user(const struct sctp_association
*asoc
,
955 const struct msghdr
*msg
,
958 struct sctp_chunk
*retval
;
959 void *payload
= NULL
;
962 retval
= sctp_make_abort(asoc
, NULL
, sizeof(sctp_errhdr_t
) + paylen
);
967 /* Put the msg_iov together into payload. */
968 payload
= kmalloc(paylen
, GFP_KERNEL
);
972 err
= memcpy_fromiovec(payload
, msg
->msg_iov
, paylen
);
977 sctp_init_cause(retval
, SCTP_ERROR_USER_ABORT
, paylen
);
978 sctp_addto_chunk(retval
, paylen
, payload
);
988 sctp_chunk_free(retval
);
994 /* Append bytes to the end of a parameter. Will panic if chunk is not big
997 static void *sctp_addto_param(struct sctp_chunk
*chunk
, int len
,
1001 int chunklen
= ntohs(chunk
->chunk_hdr
->length
);
1003 target
= skb_put(chunk
->skb
, len
);
1005 memcpy(target
, data
, len
);
1007 /* Adjust the chunk length field. */
1008 chunk
->chunk_hdr
->length
= htons(chunklen
+ len
);
1009 chunk
->chunk_end
= skb_tail_pointer(chunk
->skb
);
1014 /* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
1015 struct sctp_chunk
*sctp_make_abort_violation(
1016 const struct sctp_association
*asoc
,
1017 const struct sctp_chunk
*chunk
,
1018 const __u8
*payload
,
1019 const size_t paylen
)
1021 struct sctp_chunk
*retval
;
1022 struct sctp_paramhdr phdr
;
1024 retval
= sctp_make_abort(asoc
, chunk
, sizeof(sctp_errhdr_t
) + paylen
1025 + sizeof(sctp_paramhdr_t
));
1029 sctp_init_cause(retval
, SCTP_ERROR_PROTO_VIOLATION
, paylen
1030 + sizeof(sctp_paramhdr_t
));
1032 phdr
.type
= htons(chunk
->chunk_hdr
->type
);
1033 phdr
.length
= chunk
->chunk_hdr
->length
;
1034 sctp_addto_chunk(retval
, paylen
, payload
);
1035 sctp_addto_param(retval
, sizeof(sctp_paramhdr_t
), &phdr
);
1041 struct sctp_chunk
*sctp_make_violation_paramlen(
1042 const struct sctp_association
*asoc
,
1043 const struct sctp_chunk
*chunk
,
1044 struct sctp_paramhdr
*param
)
1046 struct sctp_chunk
*retval
;
1047 static const char error
[] = "The following parameter had invalid length:";
1048 size_t payload_len
= sizeof(error
) + sizeof(sctp_errhdr_t
) +
1049 sizeof(sctp_paramhdr_t
);
1051 retval
= sctp_make_abort(asoc
, chunk
, payload_len
);
1055 sctp_init_cause(retval
, SCTP_ERROR_PROTO_VIOLATION
,
1056 sizeof(error
) + sizeof(sctp_paramhdr_t
));
1057 sctp_addto_chunk(retval
, sizeof(error
), error
);
1058 sctp_addto_param(retval
, sizeof(sctp_paramhdr_t
), param
);
1064 /* Make a HEARTBEAT chunk. */
1065 struct sctp_chunk
*sctp_make_heartbeat(const struct sctp_association
*asoc
,
1066 const struct sctp_transport
*transport
,
1067 const void *payload
, const size_t paylen
)
1069 struct sctp_chunk
*retval
= sctp_make_chunk(asoc
, SCTP_CID_HEARTBEAT
,
1075 /* Cast away the 'const', as this is just telling the chunk
1076 * what transport it belongs to.
1078 retval
->transport
= (struct sctp_transport
*) transport
;
1079 retval
->subh
.hbs_hdr
= sctp_addto_chunk(retval
, paylen
, payload
);
1085 struct sctp_chunk
*sctp_make_heartbeat_ack(const struct sctp_association
*asoc
,
1086 const struct sctp_chunk
*chunk
,
1087 const void *payload
, const size_t paylen
)
1089 struct sctp_chunk
*retval
;
1091 retval
= sctp_make_chunk(asoc
, SCTP_CID_HEARTBEAT_ACK
, 0, paylen
);
1095 retval
->subh
.hbs_hdr
= sctp_addto_chunk(retval
, paylen
, payload
);
1097 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1099 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1100 * HEARTBEAT ACK, * etc.) to the same destination transport
1101 * address from which it * received the DATA or control chunk
1102 * to which it is replying.
1104 * [HBACK back to where the HEARTBEAT came from.]
1107 retval
->transport
= chunk
->transport
;
1113 /* Create an Operation Error chunk with the specified space reserved.
1114 * This routine can be used for containing multiple causes in the chunk.
1116 static struct sctp_chunk
*sctp_make_op_error_space(
1117 const struct sctp_association
*asoc
,
1118 const struct sctp_chunk
*chunk
,
1121 struct sctp_chunk
*retval
;
1123 retval
= sctp_make_chunk(asoc
, SCTP_CID_ERROR
, 0,
1124 sizeof(sctp_errhdr_t
) + size
);
1128 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1130 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1131 * HEARTBEAT ACK, etc.) to the same destination transport
1132 * address from which it received the DATA or control chunk
1133 * to which it is replying.
1137 retval
->transport
= chunk
->transport
;
1143 /* Create an Operation Error chunk of a fixed size,
1144 * specifically, max(asoc->pathmtu, SCTP_DEFAULT_MAXSEGMENT)
1145 * This is a helper function to allocate an error chunk for
1146 * for those invalid parameter codes in which we may not want
1147 * to report all the errors, if the incomming chunk is large
1149 static inline struct sctp_chunk
*sctp_make_op_error_fixed(
1150 const struct sctp_association
*asoc
,
1151 const struct sctp_chunk
*chunk
)
1153 size_t size
= asoc
? asoc
->pathmtu
: 0;
1156 size
= SCTP_DEFAULT_MAXSEGMENT
;
1158 return sctp_make_op_error_space(asoc
, chunk
, size
);
1161 /* Create an Operation Error chunk. */
1162 struct sctp_chunk
*sctp_make_op_error(const struct sctp_association
*asoc
,
1163 const struct sctp_chunk
*chunk
,
1164 __be16 cause_code
, const void *payload
,
1167 struct sctp_chunk
*retval
;
1169 retval
= sctp_make_op_error_space(asoc
, chunk
, paylen
);
1173 sctp_init_cause(retval
, cause_code
, paylen
);
1174 sctp_addto_chunk(retval
, paylen
, payload
);
1180 struct sctp_chunk
*sctp_make_auth(const struct sctp_association
*asoc
)
1182 struct sctp_chunk
*retval
;
1183 struct sctp_hmac
*hmac_desc
;
1184 struct sctp_authhdr auth_hdr
;
1187 /* Get the first hmac that the peer told us to use */
1188 hmac_desc
= sctp_auth_asoc_get_hmac(asoc
);
1189 if (unlikely(!hmac_desc
))
1192 retval
= sctp_make_chunk(asoc
, SCTP_CID_AUTH
, 0,
1193 hmac_desc
->hmac_len
+ sizeof(sctp_authhdr_t
));
1197 auth_hdr
.hmac_id
= htons(hmac_desc
->hmac_id
);
1198 auth_hdr
.shkey_id
= htons(asoc
->active_key_id
);
1200 retval
->subh
.auth_hdr
= sctp_addto_chunk(retval
, sizeof(sctp_authhdr_t
),
1203 hmac
= skb_put(retval
->skb
, hmac_desc
->hmac_len
);
1204 memset(hmac
, 0, hmac_desc
->hmac_len
);
1206 /* Adjust the chunk header to include the empty MAC */
1207 retval
->chunk_hdr
->length
=
1208 htons(ntohs(retval
->chunk_hdr
->length
) + hmac_desc
->hmac_len
);
1209 retval
->chunk_end
= skb_tail_pointer(retval
->skb
);
1215 /********************************************************************
1216 * 2nd Level Abstractions
1217 ********************************************************************/
1219 /* Turn an skb into a chunk.
1220 * FIXME: Eventually move the structure directly inside the skb->cb[].
1222 struct sctp_chunk
*sctp_chunkify(struct sk_buff
*skb
,
1223 const struct sctp_association
*asoc
,
1226 struct sctp_chunk
*retval
;
1228 retval
= kmem_cache_zalloc(sctp_chunk_cachep
, GFP_ATOMIC
);
1234 SCTP_DEBUG_PRINTK("chunkifying skb %p w/o an sk\n", skb
);
1237 INIT_LIST_HEAD(&retval
->list
);
1239 retval
->asoc
= (struct sctp_association
*)asoc
;
1241 retval
->has_tsn
= 0;
1242 retval
->has_ssn
= 0;
1243 retval
->rtt_in_progress
= 0;
1244 retval
->sent_at
= 0;
1245 retval
->singleton
= 1;
1246 retval
->end_of_packet
= 0;
1247 retval
->ecn_ce_done
= 0;
1248 retval
->pdiscard
= 0;
1250 /* sctpimpguide-05.txt Section 2.8.2
1251 * M1) Each time a new DATA chunk is transmitted
1252 * set the 'TSN.Missing.Report' count for that TSN to 0. The
1253 * 'TSN.Missing.Report' count will be used to determine missing chunks
1254 * and when to fast retransmit.
1256 retval
->tsn_missing_report
= 0;
1257 retval
->tsn_gap_acked
= 0;
1258 retval
->fast_retransmit
= 0;
1260 /* If this is a fragmented message, track all fragments
1261 * of the message (for SEND_FAILED).
1265 /* Polish the bead hole. */
1266 INIT_LIST_HEAD(&retval
->transmitted_list
);
1267 INIT_LIST_HEAD(&retval
->frag_list
);
1268 SCTP_DBG_OBJCNT_INC(chunk
);
1269 atomic_set(&retval
->refcnt
, 1);
1275 /* Set chunk->source and dest based on the IP header in chunk->skb. */
1276 void sctp_init_addrs(struct sctp_chunk
*chunk
, union sctp_addr
*src
,
1277 union sctp_addr
*dest
)
1279 memcpy(&chunk
->source
, src
, sizeof(union sctp_addr
));
1280 memcpy(&chunk
->dest
, dest
, sizeof(union sctp_addr
));
1283 /* Extract the source address from a chunk. */
1284 const union sctp_addr
*sctp_source(const struct sctp_chunk
*chunk
)
1286 /* If we have a known transport, use that. */
1287 if (chunk
->transport
) {
1288 return &chunk
->transport
->ipaddr
;
1290 /* Otherwise, extract it from the IP header. */
1291 return &chunk
->source
;
1295 /* Create a new chunk, setting the type and flags headers from the
1296 * arguments, reserving enough space for a 'paylen' byte payload.
1299 struct sctp_chunk
*sctp_make_chunk(const struct sctp_association
*asoc
,
1300 __u8 type
, __u8 flags
, int paylen
)
1302 struct sctp_chunk
*retval
;
1303 sctp_chunkhdr_t
*chunk_hdr
;
1304 struct sk_buff
*skb
;
1307 /* No need to allocate LL here, as this is only a chunk. */
1308 skb
= alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t
) + paylen
),
1313 /* Make room for the chunk header. */
1314 chunk_hdr
= (sctp_chunkhdr_t
*)skb_put(skb
, sizeof(sctp_chunkhdr_t
));
1315 chunk_hdr
->type
= type
;
1316 chunk_hdr
->flags
= flags
;
1317 chunk_hdr
->length
= htons(sizeof(sctp_chunkhdr_t
));
1319 sk
= asoc
? asoc
->base
.sk
: NULL
;
1320 retval
= sctp_chunkify(skb
, asoc
, sk
);
1326 retval
->chunk_hdr
= chunk_hdr
;
1327 retval
->chunk_end
= ((__u8
*)chunk_hdr
) + sizeof(struct sctp_chunkhdr
);
1329 /* Determine if the chunk needs to be authenticated */
1330 if (sctp_auth_send_cid(type
, asoc
))
1333 /* Set the skb to the belonging sock for accounting. */
1342 /* Release the memory occupied by a chunk. */
1343 static void sctp_chunk_destroy(struct sctp_chunk
*chunk
)
1345 BUG_ON(!list_empty(&chunk
->list
));
1346 list_del_init(&chunk
->transmitted_list
);
1348 /* Free the chunk skb data and the SCTP_chunk stub itself. */
1349 dev_kfree_skb(chunk
->skb
);
1351 SCTP_DBG_OBJCNT_DEC(chunk
);
1352 kmem_cache_free(sctp_chunk_cachep
, chunk
);
1355 /* Possibly, free the chunk. */
1356 void sctp_chunk_free(struct sctp_chunk
*chunk
)
1358 /* Release our reference on the message tracker. */
1360 sctp_datamsg_put(chunk
->msg
);
1362 sctp_chunk_put(chunk
);
1365 /* Grab a reference to the chunk. */
1366 void sctp_chunk_hold(struct sctp_chunk
*ch
)
1368 atomic_inc(&ch
->refcnt
);
1371 /* Release a reference to the chunk. */
1372 void sctp_chunk_put(struct sctp_chunk
*ch
)
1374 if (atomic_dec_and_test(&ch
->refcnt
))
1375 sctp_chunk_destroy(ch
);
1378 /* Append bytes to the end of a chunk. Will panic if chunk is not big
1381 void *sctp_addto_chunk(struct sctp_chunk
*chunk
, int len
, const void *data
)
1385 int chunklen
= ntohs(chunk
->chunk_hdr
->length
);
1386 int padlen
= WORD_ROUND(chunklen
) - chunklen
;
1388 padding
= skb_put(chunk
->skb
, padlen
);
1389 target
= skb_put(chunk
->skb
, len
);
1391 memset(padding
, 0, padlen
);
1392 memcpy(target
, data
, len
);
1394 /* Adjust the chunk length field. */
1395 chunk
->chunk_hdr
->length
= htons(chunklen
+ padlen
+ len
);
1396 chunk
->chunk_end
= skb_tail_pointer(chunk
->skb
);
1401 /* Append bytes to the end of a chunk. Returns NULL if there isn't sufficient
1402 * space in the chunk
1404 void *sctp_addto_chunk_fixed(struct sctp_chunk
*chunk
,
1405 int len
, const void *data
)
1407 if (skb_tailroom(chunk
->skb
) >= len
)
1408 return sctp_addto_chunk(chunk
, len
, data
);
1413 /* Append bytes from user space to the end of a chunk. Will panic if
1414 * chunk is not big enough.
1415 * Returns a kernel err value.
1417 int sctp_user_addto_chunk(struct sctp_chunk
*chunk
, int off
, int len
,
1423 /* Make room in chunk for data. */
1424 target
= skb_put(chunk
->skb
, len
);
1426 /* Copy data (whole iovec) into chunk */
1427 if ((err
= memcpy_fromiovecend(target
, data
, off
, len
)))
1430 /* Adjust the chunk length field. */
1431 chunk
->chunk_hdr
->length
=
1432 htons(ntohs(chunk
->chunk_hdr
->length
) + len
);
1433 chunk
->chunk_end
= skb_tail_pointer(chunk
->skb
);
1439 /* Helper function to assign a TSN if needed. This assumes that both
1440 * the data_hdr and association have already been assigned.
1442 void sctp_chunk_assign_ssn(struct sctp_chunk
*chunk
)
1444 struct sctp_datamsg
*msg
;
1445 struct sctp_chunk
*lchunk
;
1446 struct sctp_stream
*stream
;
1453 /* All fragments will be on the same stream */
1454 sid
= ntohs(chunk
->subh
.data_hdr
->stream
);
1455 stream
= &chunk
->asoc
->ssnmap
->out
;
1457 /* Now assign the sequence number to the entire message.
1458 * All fragments must have the same stream sequence number.
1461 list_for_each_entry(lchunk
, &msg
->chunks
, frag_list
) {
1462 if (lchunk
->chunk_hdr
->flags
& SCTP_DATA_UNORDERED
) {
1465 if (lchunk
->chunk_hdr
->flags
& SCTP_DATA_LAST_FRAG
)
1466 ssn
= sctp_ssn_next(stream
, sid
);
1468 ssn
= sctp_ssn_peek(stream
, sid
);
1471 lchunk
->subh
.data_hdr
->ssn
= htons(ssn
);
1472 lchunk
->has_ssn
= 1;
1476 /* Helper function to assign a TSN if needed. This assumes that both
1477 * the data_hdr and association have already been assigned.
1479 void sctp_chunk_assign_tsn(struct sctp_chunk
*chunk
)
1481 if (!chunk
->has_tsn
) {
1482 /* This is the last possible instant to
1485 chunk
->subh
.data_hdr
->tsn
=
1486 htonl(sctp_association_get_next_tsn(chunk
->asoc
));
1491 /* Create a CLOSED association to use with an incoming packet. */
1492 struct sctp_association
*sctp_make_temp_asoc(const struct sctp_endpoint
*ep
,
1493 struct sctp_chunk
*chunk
,
1496 struct sctp_association
*asoc
;
1497 struct sk_buff
*skb
;
1501 /* Create the bare association. */
1502 scope
= sctp_scope(sctp_source(chunk
));
1503 asoc
= sctp_association_new(ep
, ep
->base
.sk
, scope
, gfp
);
1508 /* Create an entry for the source address of the packet. */
1509 af
= sctp_get_af_specific(ipver2af(ip_hdr(skb
)->version
));
1512 af
->from_skb(&asoc
->c
.peer_addr
, skb
, 1);
1517 sctp_association_free(asoc
);
1521 /* Build a cookie representing asoc.
1522 * This INCLUDES the param header needed to put the cookie in the INIT ACK.
1524 static sctp_cookie_param_t
*sctp_pack_cookie(const struct sctp_endpoint
*ep
,
1525 const struct sctp_association
*asoc
,
1526 const struct sctp_chunk
*init_chunk
,
1528 const __u8
*raw_addrs
, int addrs_len
)
1530 sctp_cookie_param_t
*retval
;
1531 struct sctp_signed_cookie
*cookie
;
1532 struct scatterlist sg
;
1533 int headersize
, bodysize
;
1534 unsigned int keylen
;
1537 /* Header size is static data prior to the actual cookie, including
1540 headersize
= sizeof(sctp_paramhdr_t
) +
1541 (sizeof(struct sctp_signed_cookie
) -
1542 sizeof(struct sctp_cookie
));
1543 bodysize
= sizeof(struct sctp_cookie
)
1544 + ntohs(init_chunk
->chunk_hdr
->length
) + addrs_len
;
1546 /* Pad out the cookie to a multiple to make the signature
1547 * functions simpler to write.
1549 if (bodysize
% SCTP_COOKIE_MULTIPLE
)
1550 bodysize
+= SCTP_COOKIE_MULTIPLE
1551 - (bodysize
% SCTP_COOKIE_MULTIPLE
);
1552 *cookie_len
= headersize
+ bodysize
;
1554 /* Clear this memory since we are sending this data structure
1555 * out on the network.
1557 retval
= kzalloc(*cookie_len
, GFP_ATOMIC
);
1561 cookie
= (struct sctp_signed_cookie
*) retval
->body
;
1563 /* Set up the parameter header. */
1564 retval
->p
.type
= SCTP_PARAM_STATE_COOKIE
;
1565 retval
->p
.length
= htons(*cookie_len
);
1567 /* Copy the cookie part of the association itself. */
1568 cookie
->c
= asoc
->c
;
1569 /* Save the raw address list length in the cookie. */
1570 cookie
->c
.raw_addr_list_len
= addrs_len
;
1572 /* Remember PR-SCTP capability. */
1573 cookie
->c
.prsctp_capable
= asoc
->peer
.prsctp_capable
;
1575 /* Save adaptation indication in the cookie. */
1576 cookie
->c
.adaptation_ind
= asoc
->peer
.adaptation_ind
;
1578 /* Set an expiration time for the cookie. */
1579 do_gettimeofday(&cookie
->c
.expiration
);
1580 TIMEVAL_ADD(asoc
->cookie_life
, cookie
->c
.expiration
);
1582 /* Copy the peer's init packet. */
1583 memcpy(&cookie
->c
.peer_init
[0], init_chunk
->chunk_hdr
,
1584 ntohs(init_chunk
->chunk_hdr
->length
));
1586 /* Copy the raw local address list of the association. */
1587 memcpy((__u8
*)&cookie
->c
.peer_init
[0] +
1588 ntohs(init_chunk
->chunk_hdr
->length
), raw_addrs
, addrs_len
);
1590 if (sctp_sk(ep
->base
.sk
)->hmac
) {
1591 struct hash_desc desc
;
1593 /* Sign the message. */
1594 sg_init_one(&sg
, &cookie
->c
, bodysize
);
1595 keylen
= SCTP_SECRET_SIZE
;
1596 key
= (char *)ep
->secret_key
[ep
->current_key
];
1597 desc
.tfm
= sctp_sk(ep
->base
.sk
)->hmac
;
1600 if (crypto_hash_setkey(desc
.tfm
, key
, keylen
) ||
1601 crypto_hash_digest(&desc
, &sg
, bodysize
, cookie
->signature
))
1614 /* Unpack the cookie from COOKIE ECHO chunk, recreating the association. */
1615 struct sctp_association
*sctp_unpack_cookie(
1616 const struct sctp_endpoint
*ep
,
1617 const struct sctp_association
*asoc
,
1618 struct sctp_chunk
*chunk
, gfp_t gfp
,
1619 int *error
, struct sctp_chunk
**errp
)
1621 struct sctp_association
*retval
= NULL
;
1622 struct sctp_signed_cookie
*cookie
;
1623 struct sctp_cookie
*bear_cookie
;
1624 int headersize
, bodysize
, fixed_size
;
1625 __u8
*digest
= ep
->digest
;
1626 struct scatterlist sg
;
1627 unsigned int keylen
, len
;
1630 struct sk_buff
*skb
= chunk
->skb
;
1632 struct hash_desc desc
;
1634 /* Header size is static data prior to the actual cookie, including
1637 headersize
= sizeof(sctp_chunkhdr_t
) +
1638 (sizeof(struct sctp_signed_cookie
) -
1639 sizeof(struct sctp_cookie
));
1640 bodysize
= ntohs(chunk
->chunk_hdr
->length
) - headersize
;
1641 fixed_size
= headersize
+ sizeof(struct sctp_cookie
);
1643 /* Verify that the chunk looks like it even has a cookie.
1644 * There must be enough room for our cookie and our peer's
1647 len
= ntohs(chunk
->chunk_hdr
->length
);
1648 if (len
< fixed_size
+ sizeof(struct sctp_chunkhdr
))
1651 /* Verify that the cookie has been padded out. */
1652 if (bodysize
% SCTP_COOKIE_MULTIPLE
)
1655 /* Process the cookie. */
1656 cookie
= chunk
->subh
.cookie_hdr
;
1657 bear_cookie
= &cookie
->c
;
1659 if (!sctp_sk(ep
->base
.sk
)->hmac
)
1662 /* Check the signature. */
1663 keylen
= SCTP_SECRET_SIZE
;
1664 sg_init_one(&sg
, bear_cookie
, bodysize
);
1665 key
= (char *)ep
->secret_key
[ep
->current_key
];
1666 desc
.tfm
= sctp_sk(ep
->base
.sk
)->hmac
;
1669 memset(digest
, 0x00, SCTP_SIGNATURE_SIZE
);
1670 if (crypto_hash_setkey(desc
.tfm
, key
, keylen
) ||
1671 crypto_hash_digest(&desc
, &sg
, bodysize
, digest
)) {
1672 *error
= -SCTP_IERROR_NOMEM
;
1676 if (memcmp(digest
, cookie
->signature
, SCTP_SIGNATURE_SIZE
)) {
1677 /* Try the previous key. */
1678 key
= (char *)ep
->secret_key
[ep
->last_key
];
1679 memset(digest
, 0x00, SCTP_SIGNATURE_SIZE
);
1680 if (crypto_hash_setkey(desc
.tfm
, key
, keylen
) ||
1681 crypto_hash_digest(&desc
, &sg
, bodysize
, digest
)) {
1682 *error
= -SCTP_IERROR_NOMEM
;
1686 if (memcmp(digest
, cookie
->signature
, SCTP_SIGNATURE_SIZE
)) {
1687 /* Yikes! Still bad signature! */
1688 *error
= -SCTP_IERROR_BAD_SIG
;
1694 /* IG Section 2.35.2:
1695 * 3) Compare the port numbers and the verification tag contained
1696 * within the COOKIE ECHO chunk to the actual port numbers and the
1697 * verification tag within the SCTP common header of the received
1698 * packet. If these values do not match the packet MUST be silently
1701 if (ntohl(chunk
->sctp_hdr
->vtag
) != bear_cookie
->my_vtag
) {
1702 *error
= -SCTP_IERROR_BAD_TAG
;
1706 if (chunk
->sctp_hdr
->source
!= bear_cookie
->peer_addr
.v4
.sin_port
||
1707 ntohs(chunk
->sctp_hdr
->dest
) != bear_cookie
->my_port
) {
1708 *error
= -SCTP_IERROR_BAD_PORTS
;
1712 /* Check to see if the cookie is stale. If there is already
1713 * an association, there is no need to check cookie's expiration
1714 * for init collision case of lost COOKIE ACK.
1715 * If skb has been timestamped, then use the stamp, otherwise
1716 * use current time. This introduces a small possibility that
1717 * that a cookie may be considered expired, but his would only slow
1718 * down the new association establishment instead of every packet.
1720 if (sock_flag(ep
->base
.sk
, SOCK_TIMESTAMP
))
1721 skb_get_timestamp(skb
, &tv
);
1723 do_gettimeofday(&tv
);
1725 if (!asoc
&& tv_lt(bear_cookie
->expiration
, tv
)) {
1727 * Section 3.3.10.3 Stale Cookie Error (3)
1731 * Stale Cookie Error: Indicates the receipt of a valid State
1732 * Cookie that has expired.
1734 len
= ntohs(chunk
->chunk_hdr
->length
);
1735 *errp
= sctp_make_op_error_space(asoc
, chunk
, len
);
1737 suseconds_t usecs
= (tv
.tv_sec
-
1738 bear_cookie
->expiration
.tv_sec
) * 1000000L +
1739 tv
.tv_usec
- bear_cookie
->expiration
.tv_usec
;
1740 __be32 n
= htonl(usecs
);
1742 sctp_init_cause(*errp
, SCTP_ERROR_STALE_COOKIE
,
1744 sctp_addto_chunk(*errp
, sizeof(n
), &n
);
1745 *error
= -SCTP_IERROR_STALE_COOKIE
;
1747 *error
= -SCTP_IERROR_NOMEM
;
1752 /* Make a new base association. */
1753 scope
= sctp_scope(sctp_source(chunk
));
1754 retval
= sctp_association_new(ep
, ep
->base
.sk
, scope
, gfp
);
1756 *error
= -SCTP_IERROR_NOMEM
;
1760 /* Set up our peer's port number. */
1761 retval
->peer
.port
= ntohs(chunk
->sctp_hdr
->source
);
1763 /* Populate the association from the cookie. */
1764 memcpy(&retval
->c
, bear_cookie
, sizeof(*bear_cookie
));
1766 if (sctp_assoc_set_bind_addr_from_cookie(retval
, bear_cookie
,
1768 *error
= -SCTP_IERROR_NOMEM
;
1772 /* Also, add the destination address. */
1773 if (list_empty(&retval
->base
.bind_addr
.address_list
)) {
1774 sctp_add_bind_addr(&retval
->base
.bind_addr
, &chunk
->dest
,
1775 SCTP_ADDR_SRC
, GFP_ATOMIC
);
1778 retval
->next_tsn
= retval
->c
.initial_tsn
;
1779 retval
->ctsn_ack_point
= retval
->next_tsn
- 1;
1780 retval
->addip_serial
= retval
->c
.initial_tsn
;
1781 retval
->adv_peer_ack_point
= retval
->ctsn_ack_point
;
1782 retval
->peer
.prsctp_capable
= retval
->c
.prsctp_capable
;
1783 retval
->peer
.adaptation_ind
= retval
->c
.adaptation_ind
;
1785 /* The INIT stuff will be done by the side effects. */
1790 sctp_association_free(retval
);
1795 /* Yikes! The packet is either corrupt or deliberately
1798 *error
= -SCTP_IERROR_MALFORMED
;
1802 /********************************************************************
1803 * 3rd Level Abstractions
1804 ********************************************************************/
1806 struct __sctp_missing
{
1809 } __attribute__((packed
));
1812 * Report a missing mandatory parameter.
1814 static int sctp_process_missing_param(const struct sctp_association
*asoc
,
1815 sctp_param_t paramtype
,
1816 struct sctp_chunk
*chunk
,
1817 struct sctp_chunk
**errp
)
1819 struct __sctp_missing report
;
1822 len
= WORD_ROUND(sizeof(report
));
1824 /* Make an ERROR chunk, preparing enough room for
1825 * returning multiple unknown parameters.
1828 *errp
= sctp_make_op_error_space(asoc
, chunk
, len
);
1831 report
.num_missing
= htonl(1);
1832 report
.type
= paramtype
;
1833 sctp_init_cause(*errp
, SCTP_ERROR_MISS_PARAM
,
1835 sctp_addto_chunk(*errp
, sizeof(report
), &report
);
1838 /* Stop processing this chunk. */
1842 /* Report an Invalid Mandatory Parameter. */
1843 static int sctp_process_inv_mandatory(const struct sctp_association
*asoc
,
1844 struct sctp_chunk
*chunk
,
1845 struct sctp_chunk
**errp
)
1847 /* Invalid Mandatory Parameter Error has no payload. */
1850 *errp
= sctp_make_op_error_space(asoc
, chunk
, 0);
1853 sctp_init_cause(*errp
, SCTP_ERROR_INV_PARAM
, 0);
1855 /* Stop processing this chunk. */
1859 static int sctp_process_inv_paramlength(const struct sctp_association
*asoc
,
1860 struct sctp_paramhdr
*param
,
1861 const struct sctp_chunk
*chunk
,
1862 struct sctp_chunk
**errp
)
1864 /* This is a fatal error. Any accumulated non-fatal errors are
1868 sctp_chunk_free(*errp
);
1870 /* Create an error chunk and fill it in with our payload. */
1871 *errp
= sctp_make_violation_paramlen(asoc
, chunk
, param
);
1877 /* Do not attempt to handle the HOST_NAME parm. However, do
1878 * send back an indicator to the peer.
1880 static int sctp_process_hn_param(const struct sctp_association
*asoc
,
1881 union sctp_params param
,
1882 struct sctp_chunk
*chunk
,
1883 struct sctp_chunk
**errp
)
1885 __u16 len
= ntohs(param
.p
->length
);
1887 /* Processing of the HOST_NAME parameter will generate an
1888 * ABORT. If we've accumulated any non-fatal errors, they
1889 * would be unrecognized parameters and we should not include
1890 * them in the ABORT.
1893 sctp_chunk_free(*errp
);
1895 *errp
= sctp_make_op_error_space(asoc
, chunk
, len
);
1898 sctp_init_cause(*errp
, SCTP_ERROR_DNS_FAILED
, len
);
1899 sctp_addto_chunk(*errp
, len
, param
.v
);
1902 /* Stop processing this chunk. */
1906 static int sctp_verify_ext_param(union sctp_params param
)
1908 __u16 num_ext
= ntohs(param
.p
->length
) - sizeof(sctp_paramhdr_t
);
1910 int have_asconf
= 0;
1913 for (i
= 0; i
< num_ext
; i
++) {
1914 switch (param
.ext
->chunks
[i
]) {
1918 case SCTP_CID_ASCONF
:
1919 case SCTP_CID_ASCONF_ACK
:
1925 /* ADD-IP Security: The draft requires us to ABORT or ignore the
1926 * INIT/INIT-ACK if ADD-IP is listed, but AUTH is not. Do this
1927 * only if ADD-IP is turned on and we are not backward-compatible
1930 if (sctp_addip_noauth
)
1933 if (sctp_addip_enable
&& !have_auth
&& have_asconf
)
1939 static void sctp_process_ext_param(struct sctp_association
*asoc
,
1940 union sctp_params param
)
1942 __u16 num_ext
= ntohs(param
.p
->length
) - sizeof(sctp_paramhdr_t
);
1945 for (i
= 0; i
< num_ext
; i
++) {
1946 switch (param
.ext
->chunks
[i
]) {
1947 case SCTP_CID_FWD_TSN
:
1948 if (sctp_prsctp_enable
&&
1949 !asoc
->peer
.prsctp_capable
)
1950 asoc
->peer
.prsctp_capable
= 1;
1953 /* if the peer reports AUTH, assume that he
1956 if (sctp_auth_enable
)
1957 asoc
->peer
.auth_capable
= 1;
1959 case SCTP_CID_ASCONF
:
1960 case SCTP_CID_ASCONF_ACK
:
1961 if (sctp_addip_enable
)
1962 asoc
->peer
.asconf_capable
= 1;
1970 /* RFC 3.2.1 & the Implementers Guide 2.2.
1972 * The Parameter Types are encoded such that the
1973 * highest-order two bits specify the action that must be
1974 * taken if the processing endpoint does not recognize the
1977 * 00 - Stop processing this parameter; do not process any further
1978 * parameters within this chunk
1980 * 01 - Stop processing this parameter, do not process any further
1981 * parameters within this chunk, and report the unrecognized
1982 * parameter in an 'Unrecognized Parameter' ERROR chunk.
1984 * 10 - Skip this parameter and continue processing.
1986 * 11 - Skip this parameter and continue processing but
1987 * report the unrecognized parameter in an
1988 * 'Unrecognized Parameter' ERROR chunk.
1991 * SCTP_IERROR_NO_ERROR - continue with the chunk
1992 * SCTP_IERROR_ERROR - stop and report an error.
1993 * SCTP_IERROR_NOMEME - out of memory.
1995 static sctp_ierror_t
sctp_process_unk_param(const struct sctp_association
*asoc
,
1996 union sctp_params param
,
1997 struct sctp_chunk
*chunk
,
1998 struct sctp_chunk
**errp
)
2000 int retval
= SCTP_IERROR_NO_ERROR
;
2002 switch (param
.p
->type
& SCTP_PARAM_ACTION_MASK
) {
2003 case SCTP_PARAM_ACTION_DISCARD
:
2004 retval
= SCTP_IERROR_ERROR
;
2006 case SCTP_PARAM_ACTION_SKIP
:
2008 case SCTP_PARAM_ACTION_DISCARD_ERR
:
2009 retval
= SCTP_IERROR_ERROR
;
2011 case SCTP_PARAM_ACTION_SKIP_ERR
:
2012 /* Make an ERROR chunk, preparing enough room for
2013 * returning multiple unknown parameters.
2016 *errp
= sctp_make_op_error_fixed(asoc
, chunk
);
2019 sctp_init_cause_fixed(*errp
, SCTP_ERROR_UNKNOWN_PARAM
,
2020 WORD_ROUND(ntohs(param
.p
->length
)));
2021 sctp_addto_chunk_fixed(*errp
,
2022 WORD_ROUND(ntohs(param
.p
->length
)),
2025 /* If there is no memory for generating the ERROR
2026 * report as specified, an ABORT will be triggered
2027 * to the peer and the association won't be
2030 retval
= SCTP_IERROR_NOMEM
;
2040 /* Verify variable length parameters
2042 * SCTP_IERROR_ABORT - trigger an ABORT
2043 * SCTP_IERROR_NOMEM - out of memory (abort)
2044 * SCTP_IERROR_ERROR - stop processing, trigger an ERROR
2045 * SCTP_IERROR_NO_ERROR - continue with the chunk
2047 static sctp_ierror_t
sctp_verify_param(const struct sctp_association
*asoc
,
2048 union sctp_params param
,
2050 struct sctp_chunk
*chunk
,
2051 struct sctp_chunk
**err_chunk
)
2053 struct sctp_hmac_algo_param
*hmacs
;
2054 int retval
= SCTP_IERROR_NO_ERROR
;
2055 __u16 n_elt
, id
= 0;
2058 /* FIXME - This routine is not looking at each parameter per the
2059 * chunk type, i.e., unrecognized parameters should be further
2060 * identified based on the chunk id.
2063 switch (param
.p
->type
) {
2064 case SCTP_PARAM_IPV4_ADDRESS
:
2065 case SCTP_PARAM_IPV6_ADDRESS
:
2066 case SCTP_PARAM_COOKIE_PRESERVATIVE
:
2067 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES
:
2068 case SCTP_PARAM_STATE_COOKIE
:
2069 case SCTP_PARAM_HEARTBEAT_INFO
:
2070 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS
:
2071 case SCTP_PARAM_ECN_CAPABLE
:
2072 case SCTP_PARAM_ADAPTATION_LAYER_IND
:
2075 case SCTP_PARAM_SUPPORTED_EXT
:
2076 if (!sctp_verify_ext_param(param
))
2077 return SCTP_IERROR_ABORT
;
2080 case SCTP_PARAM_SET_PRIMARY
:
2081 if (sctp_addip_enable
)
2085 case SCTP_PARAM_HOST_NAME_ADDRESS
:
2086 /* Tell the peer, we won't support this param. */
2087 sctp_process_hn_param(asoc
, param
, chunk
, err_chunk
);
2088 retval
= SCTP_IERROR_ABORT
;
2091 case SCTP_PARAM_FWD_TSN_SUPPORT
:
2092 if (sctp_prsctp_enable
)
2096 case SCTP_PARAM_RANDOM
:
2097 if (!sctp_auth_enable
)
2100 /* SCTP-AUTH: Secion 6.1
2101 * If the random number is not 32 byte long the association
2102 * MUST be aborted. The ABORT chunk SHOULD contain the error
2103 * cause 'Protocol Violation'.
2105 if (SCTP_AUTH_RANDOM_LENGTH
!=
2106 ntohs(param
.p
->length
) - sizeof(sctp_paramhdr_t
)) {
2107 sctp_process_inv_paramlength(asoc
, param
.p
,
2109 retval
= SCTP_IERROR_ABORT
;
2113 case SCTP_PARAM_CHUNKS
:
2114 if (!sctp_auth_enable
)
2117 /* SCTP-AUTH: Section 3.2
2118 * The CHUNKS parameter MUST be included once in the INIT or
2119 * INIT-ACK chunk if the sender wants to receive authenticated
2120 * chunks. Its maximum length is 260 bytes.
2122 if (260 < ntohs(param
.p
->length
)) {
2123 sctp_process_inv_paramlength(asoc
, param
.p
,
2125 retval
= SCTP_IERROR_ABORT
;
2129 case SCTP_PARAM_HMAC_ALGO
:
2130 if (!sctp_auth_enable
)
2133 hmacs
= (struct sctp_hmac_algo_param
*)param
.p
;
2134 n_elt
= (ntohs(param
.p
->length
) - sizeof(sctp_paramhdr_t
)) >> 1;
2136 /* SCTP-AUTH: Section 6.1
2137 * The HMAC algorithm based on SHA-1 MUST be supported and
2138 * included in the HMAC-ALGO parameter.
2140 for (i
= 0; i
< n_elt
; i
++) {
2141 id
= ntohs(hmacs
->hmac_ids
[i
]);
2143 if (id
== SCTP_AUTH_HMAC_ID_SHA1
)
2147 if (id
!= SCTP_AUTH_HMAC_ID_SHA1
) {
2148 sctp_process_inv_paramlength(asoc
, param
.p
, chunk
,
2150 retval
= SCTP_IERROR_ABORT
;
2155 SCTP_DEBUG_PRINTK("Unrecognized param: %d for chunk %d.\n",
2156 ntohs(param
.p
->type
), cid
);
2157 retval
= sctp_process_unk_param(asoc
, param
, chunk
, err_chunk
);
2163 /* Verify the INIT packet before we process it. */
2164 int sctp_verify_init(const struct sctp_association
*asoc
,
2166 sctp_init_chunk_t
*peer_init
,
2167 struct sctp_chunk
*chunk
,
2168 struct sctp_chunk
**errp
)
2170 union sctp_params param
;
2174 /* Verify stream values are non-zero. */
2175 if ((0 == peer_init
->init_hdr
.num_outbound_streams
) ||
2176 (0 == peer_init
->init_hdr
.num_inbound_streams
) ||
2177 (0 == peer_init
->init_hdr
.init_tag
) ||
2178 (SCTP_DEFAULT_MINWINDOW
> ntohl(peer_init
->init_hdr
.a_rwnd
))) {
2180 return sctp_process_inv_mandatory(asoc
, chunk
, errp
);
2183 /* Check for missing mandatory parameters. */
2184 sctp_walk_params(param
, peer_init
, init_hdr
.params
) {
2186 if (SCTP_PARAM_STATE_COOKIE
== param
.p
->type
)
2189 } /* for (loop through all parameters) */
2191 /* There is a possibility that a parameter length was bad and
2192 * in that case we would have stoped walking the parameters.
2193 * The current param.p would point at the bad one.
2194 * Current consensus on the mailing list is to generate a PROTOCOL
2195 * VIOLATION error. We build the ERROR chunk here and let the normal
2196 * error handling code build and send the packet.
2198 if (param
.v
!= (void*)chunk
->chunk_end
)
2199 return sctp_process_inv_paramlength(asoc
, param
.p
, chunk
, errp
);
2201 /* The only missing mandatory param possible today is
2202 * the state cookie for an INIT-ACK chunk.
2204 if ((SCTP_CID_INIT_ACK
== cid
) && !has_cookie
)
2205 return sctp_process_missing_param(asoc
, SCTP_PARAM_STATE_COOKIE
,
2208 /* Verify all the variable length parameters */
2209 sctp_walk_params(param
, peer_init
, init_hdr
.params
) {
2211 result
= sctp_verify_param(asoc
, param
, cid
, chunk
, errp
);
2213 case SCTP_IERROR_ABORT
:
2214 case SCTP_IERROR_NOMEM
:
2216 case SCTP_IERROR_ERROR
:
2218 case SCTP_IERROR_NO_ERROR
:
2223 } /* for (loop through all parameters) */
2228 /* Unpack the parameters in an INIT packet into an association.
2229 * Returns 0 on failure, else success.
2230 * FIXME: This is an association method.
2232 int sctp_process_init(struct sctp_association
*asoc
, sctp_cid_t cid
,
2233 const union sctp_addr
*peer_addr
,
2234 sctp_init_chunk_t
*peer_init
, gfp_t gfp
)
2236 union sctp_params param
;
2237 struct sctp_transport
*transport
;
2238 struct list_head
*pos
, *temp
;
2241 /* We must include the address that the INIT packet came from.
2242 * This is the only address that matters for an INIT packet.
2243 * When processing a COOKIE ECHO, we retrieve the from address
2244 * of the INIT from the cookie.
2247 /* This implementation defaults to making the first transport
2248 * added as the primary transport. The source address seems to
2249 * be a a better choice than any of the embedded addresses.
2252 if(!sctp_assoc_add_peer(asoc
, peer_addr
, gfp
, SCTP_ACTIVE
))
2256 /* Process the initialization parameters. */
2257 sctp_walk_params(param
, peer_init
, init_hdr
.params
) {
2259 if (!sctp_process_param(asoc
, param
, peer_addr
, gfp
))
2263 /* AUTH: After processing the parameters, make sure that we
2264 * have all the required info to potentially do authentications.
2266 if (asoc
->peer
.auth_capable
&& (!asoc
->peer
.peer_random
||
2267 !asoc
->peer
.peer_hmacs
))
2268 asoc
->peer
.auth_capable
= 0;
2270 /* In a non-backward compatible mode, if the peer claims
2271 * support for ADD-IP but not AUTH, the ADD-IP spec states
2272 * that we MUST ABORT the association. Section 6. The section
2273 * also give us an option to silently ignore the packet, which
2274 * is what we'll do here.
2276 if (!sctp_addip_noauth
&&
2277 (asoc
->peer
.asconf_capable
&& !asoc
->peer
.auth_capable
)) {
2278 asoc
->peer
.addip_disabled_mask
|= (SCTP_PARAM_ADD_IP
|
2280 SCTP_PARAM_SET_PRIMARY
);
2281 asoc
->peer
.asconf_capable
= 0;
2285 /* Walk list of transports, removing transports in the UNKNOWN state. */
2286 list_for_each_safe(pos
, temp
, &asoc
->peer
.transport_addr_list
) {
2287 transport
= list_entry(pos
, struct sctp_transport
, transports
);
2288 if (transport
->state
== SCTP_UNKNOWN
) {
2289 sctp_assoc_rm_peer(asoc
, transport
);
2293 /* The fixed INIT headers are always in network byte
2296 asoc
->peer
.i
.init_tag
=
2297 ntohl(peer_init
->init_hdr
.init_tag
);
2298 asoc
->peer
.i
.a_rwnd
=
2299 ntohl(peer_init
->init_hdr
.a_rwnd
);
2300 asoc
->peer
.i
.num_outbound_streams
=
2301 ntohs(peer_init
->init_hdr
.num_outbound_streams
);
2302 asoc
->peer
.i
.num_inbound_streams
=
2303 ntohs(peer_init
->init_hdr
.num_inbound_streams
);
2304 asoc
->peer
.i
.initial_tsn
=
2305 ntohl(peer_init
->init_hdr
.initial_tsn
);
2307 /* Apply the upper bounds for output streams based on peer's
2308 * number of inbound streams.
2310 if (asoc
->c
.sinit_num_ostreams
>
2311 ntohs(peer_init
->init_hdr
.num_inbound_streams
)) {
2312 asoc
->c
.sinit_num_ostreams
=
2313 ntohs(peer_init
->init_hdr
.num_inbound_streams
);
2316 if (asoc
->c
.sinit_max_instreams
>
2317 ntohs(peer_init
->init_hdr
.num_outbound_streams
)) {
2318 asoc
->c
.sinit_max_instreams
=
2319 ntohs(peer_init
->init_hdr
.num_outbound_streams
);
2322 /* Copy Initiation tag from INIT to VT_peer in cookie. */
2323 asoc
->c
.peer_vtag
= asoc
->peer
.i
.init_tag
;
2325 /* Peer Rwnd : Current calculated value of the peer's rwnd. */
2326 asoc
->peer
.rwnd
= asoc
->peer
.i
.a_rwnd
;
2328 /* Copy cookie in case we need to resend COOKIE-ECHO. */
2329 cookie
= asoc
->peer
.cookie
;
2331 asoc
->peer
.cookie
= kmemdup(cookie
, asoc
->peer
.cookie_len
, gfp
);
2332 if (!asoc
->peer
.cookie
)
2336 /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
2337 * high (for example, implementations MAY use the size of the receiver
2338 * advertised window).
2340 list_for_each_entry(transport
, &asoc
->peer
.transport_addr_list
,
2342 transport
->ssthresh
= asoc
->peer
.i
.a_rwnd
;
2345 /* Set up the TSN tracking pieces. */
2346 sctp_tsnmap_init(&asoc
->peer
.tsn_map
, SCTP_TSN_MAP_SIZE
,
2347 asoc
->peer
.i
.initial_tsn
);
2349 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
2351 * The stream sequence number in all the streams shall start
2352 * from 0 when the association is established. Also, when the
2353 * stream sequence number reaches the value 65535 the next
2354 * stream sequence number shall be set to 0.
2357 /* Allocate storage for the negotiated streams if it is not a temporary
2363 asoc
->ssnmap
= sctp_ssnmap_new(asoc
->c
.sinit_max_instreams
,
2364 asoc
->c
.sinit_num_ostreams
, gfp
);
2368 error
= sctp_assoc_set_id(asoc
, gfp
);
2373 /* ADDIP Section 4.1 ASCONF Chunk Procedures
2375 * When an endpoint has an ASCONF signaled change to be sent to the
2376 * remote endpoint it should do the following:
2378 * A2) A serial number should be assigned to the Chunk. The serial
2379 * number should be a monotonically increasing number. All serial
2380 * numbers are defined to be initialized at the start of the
2381 * association to the same value as the Initial TSN.
2383 asoc
->peer
.addip_serial
= asoc
->peer
.i
.initial_tsn
- 1;
2387 /* Release the transport structures. */
2388 list_for_each_safe(pos
, temp
, &asoc
->peer
.transport_addr_list
) {
2389 transport
= list_entry(pos
, struct sctp_transport
, transports
);
2390 if (transport
->state
!= SCTP_ACTIVE
)
2391 sctp_assoc_rm_peer(asoc
, transport
);
2399 /* Update asoc with the option described in param.
2401 * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
2403 * asoc is the association to update.
2404 * param is the variable length parameter to use for update.
2405 * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
2406 * If the current packet is an INIT we want to minimize the amount of
2407 * work we do. In particular, we should not build transport
2408 * structures for the addresses.
2410 static int sctp_process_param(struct sctp_association
*asoc
,
2411 union sctp_params param
,
2412 const union sctp_addr
*peer_addr
,
2415 union sctp_addr addr
;
2422 union sctp_addr_param
*addr_param
;
2423 struct sctp_transport
*t
;
2425 /* We maintain all INIT parameters in network byte order all the
2426 * time. This allows us to not worry about whether the parameters
2427 * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
2429 switch (param
.p
->type
) {
2430 case SCTP_PARAM_IPV6_ADDRESS
:
2431 if (PF_INET6
!= asoc
->base
.sk
->sk_family
)
2435 case SCTP_PARAM_IPV4_ADDRESS
:
2436 /* v4 addresses are not allowed on v6-only socket */
2437 if (ipv6_only_sock(asoc
->base
.sk
))
2440 af
= sctp_get_af_specific(param_type2af(param
.p
->type
));
2441 af
->from_addr_param(&addr
, param
.addr
, htons(asoc
->peer
.port
), 0);
2442 scope
= sctp_scope(peer_addr
);
2443 if (sctp_in_scope(&addr
, scope
))
2444 if (!sctp_assoc_add_peer(asoc
, &addr
, gfp
, SCTP_UNCONFIRMED
))
2448 case SCTP_PARAM_COOKIE_PRESERVATIVE
:
2449 if (!sctp_cookie_preserve_enable
)
2452 stale
= ntohl(param
.life
->lifespan_increment
);
2454 /* Suggested Cookie Life span increment's unit is msec,
2457 asoc
->cookie_life
.tv_sec
+= stale
/ 1000;
2458 asoc
->cookie_life
.tv_usec
+= (stale
% 1000) * 1000;
2461 case SCTP_PARAM_HOST_NAME_ADDRESS
:
2462 SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
2465 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES
:
2466 /* Turn off the default values first so we'll know which
2467 * ones are really set by the peer.
2469 asoc
->peer
.ipv4_address
= 0;
2470 asoc
->peer
.ipv6_address
= 0;
2472 /* Assume that peer supports the address family
2473 * by which it sends a packet.
2475 if (peer_addr
->sa
.sa_family
== AF_INET6
)
2476 asoc
->peer
.ipv6_address
= 1;
2477 else if (peer_addr
->sa
.sa_family
== AF_INET
)
2478 asoc
->peer
.ipv4_address
= 1;
2480 /* Cycle through address types; avoid divide by 0. */
2481 sat
= ntohs(param
.p
->length
) - sizeof(sctp_paramhdr_t
);
2483 sat
/= sizeof(__u16
);
2485 for (i
= 0; i
< sat
; ++i
) {
2486 switch (param
.sat
->types
[i
]) {
2487 case SCTP_PARAM_IPV4_ADDRESS
:
2488 asoc
->peer
.ipv4_address
= 1;
2491 case SCTP_PARAM_IPV6_ADDRESS
:
2492 if (PF_INET6
== asoc
->base
.sk
->sk_family
)
2493 asoc
->peer
.ipv6_address
= 1;
2496 case SCTP_PARAM_HOST_NAME_ADDRESS
:
2497 asoc
->peer
.hostname_address
= 1;
2500 default: /* Just ignore anything else. */
2506 case SCTP_PARAM_STATE_COOKIE
:
2507 asoc
->peer
.cookie_len
=
2508 ntohs(param
.p
->length
) - sizeof(sctp_paramhdr_t
);
2509 asoc
->peer
.cookie
= param
.cookie
->body
;
2512 case SCTP_PARAM_HEARTBEAT_INFO
:
2513 /* Would be odd to receive, but it causes no problems. */
2516 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS
:
2517 /* Rejected during verify stage. */
2520 case SCTP_PARAM_ECN_CAPABLE
:
2521 asoc
->peer
.ecn_capable
= 1;
2524 case SCTP_PARAM_ADAPTATION_LAYER_IND
:
2525 asoc
->peer
.adaptation_ind
= param
.aind
->adaptation_ind
;
2528 case SCTP_PARAM_SET_PRIMARY
:
2529 if (!sctp_addip_enable
)
2532 addr_param
= param
.v
+ sizeof(sctp_addip_param_t
);
2534 af
= sctp_get_af_specific(param_type2af(param
.p
->type
));
2535 af
->from_addr_param(&addr
, addr_param
,
2536 htons(asoc
->peer
.port
), 0);
2538 /* if the address is invalid, we can't process it.
2539 * XXX: see spec for what to do.
2541 if (!af
->addr_valid(&addr
, NULL
, NULL
))
2544 t
= sctp_assoc_lookup_paddr(asoc
, &addr
);
2548 sctp_assoc_set_primary(asoc
, t
);
2551 case SCTP_PARAM_SUPPORTED_EXT
:
2552 sctp_process_ext_param(asoc
, param
);
2555 case SCTP_PARAM_FWD_TSN_SUPPORT
:
2556 if (sctp_prsctp_enable
) {
2557 asoc
->peer
.prsctp_capable
= 1;
2563 case SCTP_PARAM_RANDOM
:
2564 if (!sctp_auth_enable
)
2567 /* Save peer's random parameter */
2568 asoc
->peer
.peer_random
= kmemdup(param
.p
,
2569 ntohs(param
.p
->length
), gfp
);
2570 if (!asoc
->peer
.peer_random
) {
2576 case SCTP_PARAM_HMAC_ALGO
:
2577 if (!sctp_auth_enable
)
2580 /* Save peer's HMAC list */
2581 asoc
->peer
.peer_hmacs
= kmemdup(param
.p
,
2582 ntohs(param
.p
->length
), gfp
);
2583 if (!asoc
->peer
.peer_hmacs
) {
2588 /* Set the default HMAC the peer requested*/
2589 sctp_auth_asoc_set_default_hmac(asoc
, param
.hmac_algo
);
2592 case SCTP_PARAM_CHUNKS
:
2593 if (!sctp_auth_enable
)
2596 asoc
->peer
.peer_chunks
= kmemdup(param
.p
,
2597 ntohs(param
.p
->length
), gfp
);
2598 if (!asoc
->peer
.peer_chunks
)
2603 /* Any unrecognized parameters should have been caught
2604 * and handled by sctp_verify_param() which should be
2605 * called prior to this routine. Simply log the error
2608 SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
2609 ntohs(param
.p
->type
), asoc
);
2616 /* Select a new verification tag. */
2617 __u32
sctp_generate_tag(const struct sctp_endpoint
*ep
)
2619 /* I believe that this random number generator complies with RFC1750.
2620 * A tag of 0 is reserved for special cases (e.g. INIT).
2625 get_random_bytes(&x
, sizeof(__u32
));
2631 /* Select an initial TSN to send during startup. */
2632 __u32
sctp_generate_tsn(const struct sctp_endpoint
*ep
)
2636 get_random_bytes(&retval
, sizeof(__u32
));
2641 * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
2643 * 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
2644 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2645 * | Type = 0xC1 | Chunk Flags | Chunk Length |
2646 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2648 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2649 * | Address Parameter |
2650 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2651 * | ASCONF Parameter #1 |
2652 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2656 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2657 * | ASCONF Parameter #N |
2658 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2660 * Address Parameter and other parameter will not be wrapped in this function
2662 static struct sctp_chunk
*sctp_make_asconf(struct sctp_association
*asoc
,
2663 union sctp_addr
*addr
,
2666 sctp_addiphdr_t asconf
;
2667 struct sctp_chunk
*retval
;
2668 int length
= sizeof(asconf
) + vparam_len
;
2669 union sctp_addr_param addrparam
;
2671 struct sctp_af
*af
= sctp_get_af_specific(addr
->v4
.sin_family
);
2673 addrlen
= af
->to_addr_param(addr
, &addrparam
);
2678 /* Create the chunk. */
2679 retval
= sctp_make_chunk(asoc
, SCTP_CID_ASCONF
, 0, length
);
2683 asconf
.serial
= htonl(asoc
->addip_serial
++);
2685 retval
->subh
.addip_hdr
=
2686 sctp_addto_chunk(retval
, sizeof(asconf
), &asconf
);
2687 retval
->param_hdr
.v
=
2688 sctp_addto_chunk(retval
, addrlen
, &addrparam
);
2694 * 3.2.1 Add IP Address
2696 * 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
2697 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2698 * | Type = 0xC001 | Length = Variable |
2699 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2700 * | ASCONF-Request Correlation ID |
2701 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2702 * | Address Parameter |
2703 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2705 * 3.2.2 Delete IP Address
2707 * 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
2708 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2709 * | Type = 0xC002 | Length = Variable |
2710 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2711 * | ASCONF-Request Correlation ID |
2712 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2713 * | Address Parameter |
2714 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2717 struct sctp_chunk
*sctp_make_asconf_update_ip(struct sctp_association
*asoc
,
2718 union sctp_addr
*laddr
,
2719 struct sockaddr
*addrs
,
2723 sctp_addip_param_t param
;
2724 struct sctp_chunk
*retval
;
2725 union sctp_addr_param addr_param
;
2726 union sctp_addr
*addr
;
2729 int paramlen
= sizeof(param
);
2730 int addr_param_len
= 0;
2734 /* Get total length of all the address parameters. */
2736 for (i
= 0; i
< addrcnt
; i
++) {
2737 addr
= (union sctp_addr
*)addr_buf
;
2738 af
= sctp_get_af_specific(addr
->v4
.sin_family
);
2739 addr_param_len
= af
->to_addr_param(addr
, &addr_param
);
2741 totallen
+= paramlen
;
2742 totallen
+= addr_param_len
;
2744 addr_buf
+= af
->sockaddr_len
;
2747 /* Create an asconf chunk with the required length. */
2748 retval
= sctp_make_asconf(asoc
, laddr
, totallen
);
2752 /* Add the address parameters to the asconf chunk. */
2754 for (i
= 0; i
< addrcnt
; i
++) {
2755 addr
= (union sctp_addr
*)addr_buf
;
2756 af
= sctp_get_af_specific(addr
->v4
.sin_family
);
2757 addr_param_len
= af
->to_addr_param(addr
, &addr_param
);
2758 param
.param_hdr
.type
= flags
;
2759 param
.param_hdr
.length
= htons(paramlen
+ addr_param_len
);
2762 sctp_addto_chunk(retval
, paramlen
, ¶m
);
2763 sctp_addto_chunk(retval
, addr_param_len
, &addr_param
);
2765 addr_buf
+= af
->sockaddr_len
;
2771 * 3.2.4 Set Primary IP Address
2773 * 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
2774 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2775 * | Type =0xC004 | Length = Variable |
2776 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2777 * | ASCONF-Request Correlation ID |
2778 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2779 * | Address Parameter |
2780 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2782 * Create an ASCONF chunk with Set Primary IP address parameter.
2784 struct sctp_chunk
*sctp_make_asconf_set_prim(struct sctp_association
*asoc
,
2785 union sctp_addr
*addr
)
2787 sctp_addip_param_t param
;
2788 struct sctp_chunk
*retval
;
2789 int len
= sizeof(param
);
2790 union sctp_addr_param addrparam
;
2792 struct sctp_af
*af
= sctp_get_af_specific(addr
->v4
.sin_family
);
2794 addrlen
= af
->to_addr_param(addr
, &addrparam
);
2799 /* Create the chunk and make asconf header. */
2800 retval
= sctp_make_asconf(asoc
, addr
, len
);
2804 param
.param_hdr
.type
= SCTP_PARAM_SET_PRIMARY
;
2805 param
.param_hdr
.length
= htons(len
);
2808 sctp_addto_chunk(retval
, sizeof(param
), ¶m
);
2809 sctp_addto_chunk(retval
, addrlen
, &addrparam
);
2814 /* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2816 * 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
2817 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2818 * | Type = 0x80 | Chunk Flags | Chunk Length |
2819 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2821 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2822 * | ASCONF Parameter Response#1 |
2823 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2827 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2828 * | ASCONF Parameter Response#N |
2829 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2831 * Create an ASCONF_ACK chunk with enough space for the parameter responses.
2833 static struct sctp_chunk
*sctp_make_asconf_ack(const struct sctp_association
*asoc
,
2834 __u32 serial
, int vparam_len
)
2836 sctp_addiphdr_t asconf
;
2837 struct sctp_chunk
*retval
;
2838 int length
= sizeof(asconf
) + vparam_len
;
2840 /* Create the chunk. */
2841 retval
= sctp_make_chunk(asoc
, SCTP_CID_ASCONF_ACK
, 0, length
);
2845 asconf
.serial
= htonl(serial
);
2847 retval
->subh
.addip_hdr
=
2848 sctp_addto_chunk(retval
, sizeof(asconf
), &asconf
);
2853 /* Add response parameters to an ASCONF_ACK chunk. */
2854 static void sctp_add_asconf_response(struct sctp_chunk
*chunk
, __be32 crr_id
,
2855 __be16 err_code
, sctp_addip_param_t
*asconf_param
)
2857 sctp_addip_param_t ack_param
;
2858 sctp_errhdr_t err_param
;
2859 int asconf_param_len
= 0;
2860 int err_param_len
= 0;
2861 __be16 response_type
;
2863 if (SCTP_ERROR_NO_ERROR
== err_code
) {
2864 response_type
= SCTP_PARAM_SUCCESS_REPORT
;
2866 response_type
= SCTP_PARAM_ERR_CAUSE
;
2867 err_param_len
= sizeof(err_param
);
2870 ntohs(asconf_param
->param_hdr
.length
);
2873 /* Add Success Indication or Error Cause Indication parameter. */
2874 ack_param
.param_hdr
.type
= response_type
;
2875 ack_param
.param_hdr
.length
= htons(sizeof(ack_param
) +
2878 ack_param
.crr_id
= crr_id
;
2879 sctp_addto_chunk(chunk
, sizeof(ack_param
), &ack_param
);
2881 if (SCTP_ERROR_NO_ERROR
== err_code
)
2884 /* Add Error Cause parameter. */
2885 err_param
.cause
= err_code
;
2886 err_param
.length
= htons(err_param_len
+ asconf_param_len
);
2887 sctp_addto_chunk(chunk
, err_param_len
, &err_param
);
2889 /* Add the failed TLV copied from ASCONF chunk. */
2891 sctp_addto_chunk(chunk
, asconf_param_len
, asconf_param
);
2894 /* Process a asconf parameter. */
2895 static __be16
sctp_process_asconf_param(struct sctp_association
*asoc
,
2896 struct sctp_chunk
*asconf
,
2897 sctp_addip_param_t
*asconf_param
)
2899 struct sctp_transport
*peer
;
2901 union sctp_addr addr
;
2902 union sctp_addr_param
*addr_param
;
2904 addr_param
= (union sctp_addr_param
*)
2905 ((void *)asconf_param
+ sizeof(sctp_addip_param_t
));
2907 switch (addr_param
->v4
.param_hdr
.type
) {
2908 case SCTP_PARAM_IPV6_ADDRESS
:
2909 if (!asoc
->peer
.ipv6_address
)
2910 return SCTP_ERROR_INV_PARAM
;
2912 case SCTP_PARAM_IPV4_ADDRESS
:
2913 if (!asoc
->peer
.ipv4_address
)
2914 return SCTP_ERROR_INV_PARAM
;
2917 return SCTP_ERROR_INV_PARAM
;
2920 af
= sctp_get_af_specific(param_type2af(addr_param
->v4
.param_hdr
.type
));
2922 return SCTP_ERROR_INV_PARAM
;
2924 af
->from_addr_param(&addr
, addr_param
, htons(asoc
->peer
.port
), 0);
2926 /* ADDIP 4.2.1 This parameter MUST NOT contain a broadcast
2927 * or multicast address.
2928 * (note: wildcard is permitted and requires special handling so
2929 * make sure we check for that)
2931 if (!af
->is_any(&addr
) && !af
->addr_valid(&addr
, NULL
, asconf
->skb
))
2932 return SCTP_ERROR_INV_PARAM
;
2934 switch (asconf_param
->param_hdr
.type
) {
2935 case SCTP_PARAM_ADD_IP
:
2937 * If the address 0.0.0.0 or ::0 is provided, the source
2938 * address of the packet MUST be added.
2940 if (af
->is_any(&addr
))
2941 memcpy(&addr
, &asconf
->source
, sizeof(addr
));
2943 /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
2944 * request and does not have the local resources to add this
2945 * new address to the association, it MUST return an Error
2946 * Cause TLV set to the new error code 'Operation Refused
2947 * Due to Resource Shortage'.
2950 peer
= sctp_assoc_add_peer(asoc
, &addr
, GFP_ATOMIC
, SCTP_UNCONFIRMED
);
2952 return SCTP_ERROR_RSRC_LOW
;
2954 /* Start the heartbeat timer. */
2955 if (!mod_timer(&peer
->hb_timer
, sctp_transport_timeout(peer
)))
2956 sctp_transport_hold(peer
);
2958 case SCTP_PARAM_DEL_IP
:
2959 /* ADDIP 4.3 D7) If a request is received to delete the
2960 * last remaining IP address of a peer endpoint, the receiver
2961 * MUST send an Error Cause TLV with the error cause set to the
2962 * new error code 'Request to Delete Last Remaining IP Address'.
2964 if (asoc
->peer
.transport_count
== 1)
2965 return SCTP_ERROR_DEL_LAST_IP
;
2967 /* ADDIP 4.3 D8) If a request is received to delete an IP
2968 * address which is also the source address of the IP packet
2969 * which contained the ASCONF chunk, the receiver MUST reject
2970 * this request. To reject the request the receiver MUST send
2971 * an Error Cause TLV set to the new error code 'Request to
2972 * Delete Source IP Address'
2974 if (sctp_cmp_addr_exact(sctp_source(asconf
), &addr
))
2975 return SCTP_ERROR_DEL_SRC_IP
;
2978 * If the address 0.0.0.0 or ::0 is provided, all
2979 * addresses of the peer except the source address of the
2980 * packet MUST be deleted.
2982 if (af
->is_any(&addr
)) {
2983 sctp_assoc_set_primary(asoc
, asconf
->transport
);
2984 sctp_assoc_del_nonprimary_peers(asoc
,
2987 sctp_assoc_del_peer(asoc
, &addr
);
2989 case SCTP_PARAM_SET_PRIMARY
:
2990 /* ADDIP Section 4.2.4
2991 * If the address 0.0.0.0 or ::0 is provided, the receiver
2992 * MAY mark the source address of the packet as its
2995 if (af
->is_any(&addr
))
2996 memcpy(&addr
.v4
, sctp_source(asconf
), sizeof(addr
));
2998 peer
= sctp_assoc_lookup_paddr(asoc
, &addr
);
3000 return SCTP_ERROR_INV_PARAM
;
3002 sctp_assoc_set_primary(asoc
, peer
);
3005 return SCTP_ERROR_INV_PARAM
;
3009 return SCTP_ERROR_NO_ERROR
;
3012 /* Verify the ASCONF packet before we process it. */
3013 int sctp_verify_asconf(const struct sctp_association
*asoc
,
3014 struct sctp_paramhdr
*param_hdr
, void *chunk_end
,
3015 struct sctp_paramhdr
**errp
) {
3016 sctp_addip_param_t
*asconf_param
;
3017 union sctp_params param
;
3020 param
.v
= (sctp_paramhdr_t
*) param_hdr
;
3021 while (param
.v
<= chunk_end
- sizeof(sctp_paramhdr_t
)) {
3022 length
= ntohs(param
.p
->length
);
3025 if (param
.v
> chunk_end
- length
||
3026 length
< sizeof(sctp_paramhdr_t
))
3029 switch (param
.p
->type
) {
3030 case SCTP_PARAM_ADD_IP
:
3031 case SCTP_PARAM_DEL_IP
:
3032 case SCTP_PARAM_SET_PRIMARY
:
3033 asconf_param
= (sctp_addip_param_t
*)param
.v
;
3034 plen
= ntohs(asconf_param
->param_hdr
.length
);
3035 if (plen
< sizeof(sctp_addip_param_t
) +
3036 sizeof(sctp_paramhdr_t
))
3039 case SCTP_PARAM_SUCCESS_REPORT
:
3040 case SCTP_PARAM_ADAPTATION_LAYER_IND
:
3041 if (length
!= sizeof(sctp_addip_param_t
))
3049 param
.v
+= WORD_ROUND(length
);
3052 if (param
.v
!= chunk_end
)
3058 /* Process an incoming ASCONF chunk with the next expected serial no. and
3059 * return an ASCONF_ACK chunk to be sent in response.
3061 struct sctp_chunk
*sctp_process_asconf(struct sctp_association
*asoc
,
3062 struct sctp_chunk
*asconf
)
3064 sctp_addiphdr_t
*hdr
;
3065 union sctp_addr_param
*addr_param
;
3066 sctp_addip_param_t
*asconf_param
;
3067 struct sctp_chunk
*asconf_ack
;
3073 int all_param_pass
= 1;
3075 chunk_len
= ntohs(asconf
->chunk_hdr
->length
) - sizeof(sctp_chunkhdr_t
);
3076 hdr
= (sctp_addiphdr_t
*)asconf
->skb
->data
;
3077 serial
= ntohl(hdr
->serial
);
3079 /* Skip the addiphdr and store a pointer to address parameter. */
3080 length
= sizeof(sctp_addiphdr_t
);
3081 addr_param
= (union sctp_addr_param
*)(asconf
->skb
->data
+ length
);
3082 chunk_len
-= length
;
3084 /* Skip the address parameter and store a pointer to the first
3087 length
= ntohs(addr_param
->v4
.param_hdr
.length
);
3088 asconf_param
= (sctp_addip_param_t
*)((void *)addr_param
+ length
);
3089 chunk_len
-= length
;
3091 /* create an ASCONF_ACK chunk.
3092 * Based on the definitions of parameters, we know that the size of
3093 * ASCONF_ACK parameters are less than or equal to the twice of ASCONF
3096 asconf_ack
= sctp_make_asconf_ack(asoc
, serial
, chunk_len
* 2);
3100 /* Process the TLVs contained within the ASCONF chunk. */
3101 while (chunk_len
> 0) {
3102 err_code
= sctp_process_asconf_param(asoc
, asconf
,
3105 * If an error response is received for a TLV parameter,
3106 * all TLVs with no response before the failed TLV are
3107 * considered successful if not reported. All TLVs after
3108 * the failed response are considered unsuccessful unless
3109 * a specific success indication is present for the parameter.
3111 if (SCTP_ERROR_NO_ERROR
!= err_code
)
3114 if (!all_param_pass
)
3115 sctp_add_asconf_response(asconf_ack
,
3116 asconf_param
->crr_id
, err_code
,
3119 /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
3120 * an IP address sends an 'Out of Resource' in its response, it
3121 * MUST also fail any subsequent add or delete requests bundled
3124 if (SCTP_ERROR_RSRC_LOW
== err_code
)
3127 /* Move to the next ASCONF param. */
3128 length
= ntohs(asconf_param
->param_hdr
.length
);
3129 asconf_param
= (sctp_addip_param_t
*)((void *)asconf_param
+
3131 chunk_len
-= length
;
3135 asoc
->peer
.addip_serial
++;
3137 /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
3138 * after freeing the reference to old asconf ack if any.
3141 sctp_chunk_hold(asconf_ack
);
3142 list_add_tail(&asconf_ack
->transmitted_list
,
3143 &asoc
->asconf_ack_list
);
3149 /* Process a asconf parameter that is successfully acked. */
3150 static int sctp_asconf_param_success(struct sctp_association
*asoc
,
3151 sctp_addip_param_t
*asconf_param
)
3154 union sctp_addr addr
;
3155 struct sctp_bind_addr
*bp
= &asoc
->base
.bind_addr
;
3156 union sctp_addr_param
*addr_param
;
3157 struct sctp_transport
*transport
;
3158 struct sctp_sockaddr_entry
*saddr
;
3161 addr_param
= (union sctp_addr_param
*)
3162 ((void *)asconf_param
+ sizeof(sctp_addip_param_t
));
3164 /* We have checked the packet before, so we do not check again. */
3165 af
= sctp_get_af_specific(param_type2af(addr_param
->v4
.param_hdr
.type
));
3166 af
->from_addr_param(&addr
, addr_param
, htons(bp
->port
), 0);
3168 switch (asconf_param
->param_hdr
.type
) {
3169 case SCTP_PARAM_ADD_IP
:
3170 /* This is always done in BH context with a socket lock
3171 * held, so the list can not change.
3174 list_for_each_entry(saddr
, &bp
->address_list
, list
) {
3175 if (sctp_cmp_addr_exact(&saddr
->a
, &addr
))
3176 saddr
->state
= SCTP_ADDR_SRC
;
3180 case SCTP_PARAM_DEL_IP
:
3182 retval
= sctp_del_bind_addr(bp
, &addr
);
3184 list_for_each_entry(transport
, &asoc
->peer
.transport_addr_list
,
3186 dst_release(transport
->dst
);
3187 sctp_transport_route(transport
, NULL
,
3188 sctp_sk(asoc
->base
.sk
));
3198 /* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
3199 * for the given asconf parameter. If there is no response for this parameter,
3200 * return the error code based on the third argument 'no_err'.
3202 * A7) If an error response is received for a TLV parameter, all TLVs with no
3203 * response before the failed TLV are considered successful if not reported.
3204 * All TLVs after the failed response are considered unsuccessful unless a
3205 * specific success indication is present for the parameter.
3207 static __be16
sctp_get_asconf_response(struct sctp_chunk
*asconf_ack
,
3208 sctp_addip_param_t
*asconf_param
,
3211 sctp_addip_param_t
*asconf_ack_param
;
3212 sctp_errhdr_t
*err_param
;
3218 err_code
= SCTP_ERROR_NO_ERROR
;
3220 err_code
= SCTP_ERROR_REQ_REFUSED
;
3222 asconf_ack_len
= ntohs(asconf_ack
->chunk_hdr
->length
) -
3223 sizeof(sctp_chunkhdr_t
);
3225 /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
3226 * the first asconf_ack parameter.
3228 length
= sizeof(sctp_addiphdr_t
);
3229 asconf_ack_param
= (sctp_addip_param_t
*)(asconf_ack
->skb
->data
+
3231 asconf_ack_len
-= length
;
3233 while (asconf_ack_len
> 0) {
3234 if (asconf_ack_param
->crr_id
== asconf_param
->crr_id
) {
3235 switch(asconf_ack_param
->param_hdr
.type
) {
3236 case SCTP_PARAM_SUCCESS_REPORT
:
3237 return SCTP_ERROR_NO_ERROR
;
3238 case SCTP_PARAM_ERR_CAUSE
:
3239 length
= sizeof(sctp_addip_param_t
);
3240 err_param
= (sctp_errhdr_t
*)
3241 ((void *)asconf_ack_param
+ length
);
3242 asconf_ack_len
-= length
;
3243 if (asconf_ack_len
> 0)
3244 return err_param
->cause
;
3246 return SCTP_ERROR_INV_PARAM
;
3249 return SCTP_ERROR_INV_PARAM
;
3253 length
= ntohs(asconf_ack_param
->param_hdr
.length
);
3254 asconf_ack_param
= (sctp_addip_param_t
*)
3255 ((void *)asconf_ack_param
+ length
);
3256 asconf_ack_len
-= length
;
3262 /* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
3263 int sctp_process_asconf_ack(struct sctp_association
*asoc
,
3264 struct sctp_chunk
*asconf_ack
)
3266 struct sctp_chunk
*asconf
= asoc
->addip_last_asconf
;
3267 union sctp_addr_param
*addr_param
;
3268 sctp_addip_param_t
*asconf_param
;
3270 int asconf_len
= asconf
->skb
->len
;
3271 int all_param_pass
= 0;
3274 __be16 err_code
= SCTP_ERROR_NO_ERROR
;
3276 /* Skip the chunkhdr and addiphdr from the last asconf sent and store
3277 * a pointer to address parameter.
3279 length
= sizeof(sctp_addip_chunk_t
);
3280 addr_param
= (union sctp_addr_param
*)(asconf
->skb
->data
+ length
);
3281 asconf_len
-= length
;
3283 /* Skip the address parameter in the last asconf sent and store a
3284 * pointer to the first asconf parameter.
3286 length
= ntohs(addr_param
->v4
.param_hdr
.length
);
3287 asconf_param
= (sctp_addip_param_t
*)((void *)addr_param
+ length
);
3288 asconf_len
-= length
;
3291 * A8) If there is no response(s) to specific TLV parameter(s), and no
3292 * failures are indicated, then all request(s) are considered
3295 if (asconf_ack
->skb
->len
== sizeof(sctp_addiphdr_t
))
3298 /* Process the TLVs contained in the last sent ASCONF chunk. */
3299 while (asconf_len
> 0) {
3301 err_code
= SCTP_ERROR_NO_ERROR
;
3303 err_code
= sctp_get_asconf_response(asconf_ack
,
3306 if (no_err
&& (SCTP_ERROR_NO_ERROR
!= err_code
))
3311 case SCTP_ERROR_NO_ERROR
:
3312 retval
= sctp_asconf_param_success(asoc
, asconf_param
);
3315 case SCTP_ERROR_RSRC_LOW
:
3319 case SCTP_ERROR_INV_PARAM
:
3320 /* Disable sending this type of asconf parameter in
3323 asoc
->peer
.addip_disabled_mask
|=
3324 asconf_param
->param_hdr
.type
;
3327 case SCTP_ERROR_REQ_REFUSED
:
3328 case SCTP_ERROR_DEL_LAST_IP
:
3329 case SCTP_ERROR_DEL_SRC_IP
:
3334 /* Skip the processed asconf parameter and move to the next
3337 length
= ntohs(asconf_param
->param_hdr
.length
);
3338 asconf_param
= (sctp_addip_param_t
*)((void *)asconf_param
+
3340 asconf_len
-= length
;
3343 /* Free the cached last sent asconf chunk. */
3344 list_del_init(&asconf
->transmitted_list
);
3345 sctp_chunk_free(asconf
);
3346 asoc
->addip_last_asconf
= NULL
;
3351 /* Make a FWD TSN chunk. */
3352 struct sctp_chunk
*sctp_make_fwdtsn(const struct sctp_association
*asoc
,
3353 __u32 new_cum_tsn
, size_t nstreams
,
3354 struct sctp_fwdtsn_skip
*skiplist
)
3356 struct sctp_chunk
*retval
= NULL
;
3357 struct sctp_fwdtsn_chunk
*ftsn_chunk
;
3358 struct sctp_fwdtsn_hdr ftsn_hdr
;
3359 struct sctp_fwdtsn_skip skip
;
3363 hint
= (nstreams
+ 1) * sizeof(__u32
);
3365 retval
= sctp_make_chunk(asoc
, SCTP_CID_FWD_TSN
, 0, hint
);
3370 ftsn_chunk
= (struct sctp_fwdtsn_chunk
*)retval
->subh
.fwdtsn_hdr
;
3372 ftsn_hdr
.new_cum_tsn
= htonl(new_cum_tsn
);
3373 retval
->subh
.fwdtsn_hdr
=
3374 sctp_addto_chunk(retval
, sizeof(ftsn_hdr
), &ftsn_hdr
);
3376 for (i
= 0; i
< nstreams
; i
++) {
3377 skip
.stream
= skiplist
[i
].stream
;
3378 skip
.ssn
= skiplist
[i
].ssn
;
3379 sctp_addto_chunk(retval
, sizeof(skip
), &skip
);