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>
61 #include <linux/slab.h>
64 #include <linux/skbuff.h>
65 #include <linux/random.h> /* for get_random_bytes */
66 #include <net/sctp/sctp.h>
67 #include <net/sctp/sm.h>
70 struct sctp_chunk
*sctp_make_chunk(const struct sctp_association
*asoc
,
71 __u8 type
, __u8 flags
, int paylen
);
72 static sctp_cookie_param_t
*sctp_pack_cookie(const struct sctp_endpoint
*ep
,
73 const struct sctp_association
*asoc
,
74 const struct sctp_chunk
*init_chunk
,
76 const __u8
*raw_addrs
, int addrs_len
);
77 static int sctp_process_param(struct sctp_association
*asoc
,
78 union sctp_params param
,
79 const union sctp_addr
*peer_addr
,
81 static void *sctp_addto_param(struct sctp_chunk
*chunk
, int len
,
84 /* What was the inbound interface for this chunk? */
85 int sctp_chunk_iif(const struct sctp_chunk
*chunk
)
90 af
= sctp_get_af_specific(ipver2af(ip_hdr(chunk
->skb
)->version
));
92 iif
= af
->skb_iif(chunk
->skb
);
97 /* RFC 2960 3.3.2 Initiation (INIT) (1)
99 * Note 2: The ECN capable field is reserved for future use of
100 * Explicit Congestion Notification.
102 static const struct sctp_paramhdr ecap_param
= {
103 SCTP_PARAM_ECN_CAPABLE
,
104 cpu_to_be16(sizeof(struct sctp_paramhdr
)),
106 static const struct sctp_paramhdr prsctp_param
= {
107 SCTP_PARAM_FWD_TSN_SUPPORT
,
108 cpu_to_be16(sizeof(struct sctp_paramhdr
)),
111 /* A helper to initialize to initialize an op error inside a
112 * provided chunk, as most cause codes will be embedded inside an
115 void sctp_init_cause(struct sctp_chunk
*chunk
, __be16 cause_code
,
121 /* Cause code constants are now defined in network order. */
122 err
.cause
= cause_code
;
123 len
= sizeof(sctp_errhdr_t
) + paylen
;
124 err
.length
= htons(len
);
125 chunk
->subh
.err_hdr
= sctp_addto_chunk(chunk
, sizeof(sctp_errhdr_t
), &err
);
128 /* 3.3.2 Initiation (INIT) (1)
130 * This chunk is used to initiate a SCTP association between two
131 * endpoints. The format of the INIT chunk is shown below:
134 * 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
135 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
136 * | Type = 1 | Chunk Flags | Chunk Length |
137 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
139 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
140 * | Advertised Receiver Window Credit (a_rwnd) |
141 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
142 * | Number of Outbound Streams | Number of Inbound Streams |
143 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
145 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
147 * / Optional/Variable-Length Parameters /
149 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
152 * The INIT chunk contains the following parameters. Unless otherwise
153 * noted, each parameter MUST only be included once in the INIT chunk.
155 * Fixed Parameters Status
156 * ----------------------------------------------
157 * Initiate Tag Mandatory
158 * Advertised Receiver Window Credit Mandatory
159 * Number of Outbound Streams Mandatory
160 * Number of Inbound Streams Mandatory
161 * Initial TSN Mandatory
163 * Variable Parameters Status Type Value
164 * -------------------------------------------------------------
165 * IPv4 Address (Note 1) Optional 5
166 * IPv6 Address (Note 1) Optional 6
167 * Cookie Preservative Optional 9
168 * Reserved for ECN Capable (Note 2) Optional 32768 (0x8000)
169 * Host Name Address (Note 3) Optional 11
170 * Supported Address Types (Note 4) Optional 12
172 struct sctp_chunk
*sctp_make_init(const struct sctp_association
*asoc
,
173 const struct sctp_bind_addr
*bp
,
174 gfp_t gfp
, int vparam_len
)
177 union sctp_params addrs
;
179 struct sctp_chunk
*retval
= NULL
;
180 int num_types
, addrs_len
= 0;
181 struct sctp_sock
*sp
;
182 sctp_supported_addrs_param_t sat
;
184 sctp_adaptation_ind_param_t aiparam
;
185 sctp_supported_ext_param_t ext_param
;
188 sctp_paramhdr_t
*auth_chunks
= NULL
,
191 /* RFC 2960 3.3.2 Initiation (INIT) (1)
193 * Note 1: The INIT chunks can contain multiple addresses that
194 * can be IPv4 and/or IPv6 in any combination.
198 /* Convert the provided bind address list to raw format. */
199 addrs
= sctp_bind_addrs_to_raw(bp
, &addrs_len
, gfp
);
201 init
.init_tag
= htonl(asoc
->c
.my_vtag
);
202 init
.a_rwnd
= htonl(asoc
->rwnd
);
203 init
.num_outbound_streams
= htons(asoc
->c
.sinit_num_ostreams
);
204 init
.num_inbound_streams
= htons(asoc
->c
.sinit_max_instreams
);
205 init
.initial_tsn
= htonl(asoc
->c
.initial_tsn
);
207 /* How many address types are needed? */
208 sp
= sctp_sk(asoc
->base
.sk
);
209 num_types
= sp
->pf
->supported_addrs(sp
, types
);
211 chunksize
= sizeof(init
) + addrs_len
+ SCTP_SAT_LEN(num_types
);
212 chunksize
+= sizeof(ecap_param
);
214 if (sctp_prsctp_enable
)
215 chunksize
+= sizeof(prsctp_param
);
217 /* ADDIP: Section 4.2.7:
218 * An implementation supporting this extension [ADDIP] MUST list
219 * the ASCONF,the ASCONF-ACK, and the AUTH chunks in its INIT and
220 * INIT-ACK parameters.
222 if (sctp_addip_enable
) {
223 extensions
[num_ext
] = SCTP_CID_ASCONF
;
224 extensions
[num_ext
+1] = SCTP_CID_ASCONF_ACK
;
228 if (sp
->adaptation_ind
)
229 chunksize
+= sizeof(aiparam
);
231 chunksize
+= vparam_len
;
233 /* Account for AUTH related parameters */
234 if (sctp_auth_enable
) {
235 /* Add random parameter length*/
236 chunksize
+= sizeof(asoc
->c
.auth_random
);
238 /* Add HMACS parameter length if any were defined */
239 auth_hmacs
= (sctp_paramhdr_t
*)asoc
->c
.auth_hmacs
;
240 if (auth_hmacs
->length
)
241 chunksize
+= ntohs(auth_hmacs
->length
);
245 /* Add CHUNKS parameter length */
246 auth_chunks
= (sctp_paramhdr_t
*)asoc
->c
.auth_chunks
;
247 if (auth_chunks
->length
)
248 chunksize
+= ntohs(auth_chunks
->length
);
252 extensions
[num_ext
] = SCTP_CID_AUTH
;
256 /* If we have any extensions to report, account for that */
258 chunksize
+= sizeof(sctp_supported_ext_param_t
) + num_ext
;
260 /* RFC 2960 3.3.2 Initiation (INIT) (1)
262 * Note 3: An INIT chunk MUST NOT contain more than one Host
263 * Name address parameter. Moreover, the sender of the INIT
264 * MUST NOT combine any other address types with the Host Name
265 * address in the INIT. The receiver of INIT MUST ignore any
266 * other address types if the Host Name address parameter is
267 * present in the received INIT chunk.
269 * PLEASE DO NOT FIXME [This version does not support Host Name.]
272 retval
= sctp_make_chunk(asoc
, SCTP_CID_INIT
, 0, chunksize
);
276 retval
->subh
.init_hdr
=
277 sctp_addto_chunk(retval
, sizeof(init
), &init
);
278 retval
->param_hdr
.v
=
279 sctp_addto_chunk(retval
, addrs_len
, addrs
.v
);
281 /* RFC 2960 3.3.2 Initiation (INIT) (1)
283 * Note 4: This parameter, when present, specifies all the
284 * address types the sending endpoint can support. The absence
285 * of this parameter indicates that the sending endpoint can
286 * support any address type.
288 sat
.param_hdr
.type
= SCTP_PARAM_SUPPORTED_ADDRESS_TYPES
;
289 sat
.param_hdr
.length
= htons(SCTP_SAT_LEN(num_types
));
290 sctp_addto_chunk(retval
, sizeof(sat
), &sat
);
291 sctp_addto_chunk(retval
, num_types
* sizeof(__u16
), &types
);
293 sctp_addto_chunk(retval
, sizeof(ecap_param
), &ecap_param
);
295 /* Add the supported extensions parameter. Be nice and add this
296 * fist before addiding the parameters for the extensions themselves
299 ext_param
.param_hdr
.type
= SCTP_PARAM_SUPPORTED_EXT
;
300 ext_param
.param_hdr
.length
=
301 htons(sizeof(sctp_supported_ext_param_t
) + num_ext
);
302 sctp_addto_chunk(retval
, sizeof(sctp_supported_ext_param_t
),
304 sctp_addto_param(retval
, num_ext
, extensions
);
307 if (sctp_prsctp_enable
)
308 sctp_addto_chunk(retval
, sizeof(prsctp_param
), &prsctp_param
);
310 if (sp
->adaptation_ind
) {
311 aiparam
.param_hdr
.type
= SCTP_PARAM_ADAPTATION_LAYER_IND
;
312 aiparam
.param_hdr
.length
= htons(sizeof(aiparam
));
313 aiparam
.adaptation_ind
= htonl(sp
->adaptation_ind
);
314 sctp_addto_chunk(retval
, sizeof(aiparam
), &aiparam
);
317 /* Add SCTP-AUTH chunks to the parameter list */
318 if (sctp_auth_enable
) {
319 sctp_addto_chunk(retval
, sizeof(asoc
->c
.auth_random
),
320 asoc
->c
.auth_random
);
322 sctp_addto_chunk(retval
, ntohs(auth_hmacs
->length
),
325 sctp_addto_chunk(retval
, ntohs(auth_chunks
->length
),
333 struct sctp_chunk
*sctp_make_init_ack(const struct sctp_association
*asoc
,
334 const struct sctp_chunk
*chunk
,
335 gfp_t gfp
, int unkparam_len
)
337 sctp_inithdr_t initack
;
338 struct sctp_chunk
*retval
;
339 union sctp_params addrs
;
340 struct sctp_sock
*sp
;
342 sctp_cookie_param_t
*cookie
;
345 sctp_adaptation_ind_param_t aiparam
;
346 sctp_supported_ext_param_t ext_param
;
349 sctp_paramhdr_t
*auth_chunks
= NULL
,
355 /* Note: there may be no addresses to embed. */
356 addrs
= sctp_bind_addrs_to_raw(&asoc
->base
.bind_addr
, &addrs_len
, gfp
);
358 initack
.init_tag
= htonl(asoc
->c
.my_vtag
);
359 initack
.a_rwnd
= htonl(asoc
->rwnd
);
360 initack
.num_outbound_streams
= htons(asoc
->c
.sinit_num_ostreams
);
361 initack
.num_inbound_streams
= htons(asoc
->c
.sinit_max_instreams
);
362 initack
.initial_tsn
= htonl(asoc
->c
.initial_tsn
);
364 /* FIXME: We really ought to build the cookie right
365 * into the packet instead of allocating more fresh memory.
367 cookie
= sctp_pack_cookie(asoc
->ep
, asoc
, chunk
, &cookie_len
,
372 /* Calculate the total size of allocation, include the reserved
373 * space for reporting unknown parameters if it is specified.
375 sp
= sctp_sk(asoc
->base
.sk
);
376 chunksize
= sizeof(initack
) + addrs_len
+ cookie_len
+ unkparam_len
;
378 /* Tell peer that we'll do ECN only if peer advertised such cap. */
379 if (asoc
->peer
.ecn_capable
)
380 chunksize
+= sizeof(ecap_param
);
382 if (asoc
->peer
.prsctp_capable
)
383 chunksize
+= sizeof(prsctp_param
);
385 if (asoc
->peer
.asconf_capable
) {
386 extensions
[num_ext
] = SCTP_CID_ASCONF
;
387 extensions
[num_ext
+1] = SCTP_CID_ASCONF_ACK
;
391 if (sp
->adaptation_ind
)
392 chunksize
+= sizeof(aiparam
);
394 if (asoc
->peer
.auth_capable
) {
395 auth_random
= (sctp_paramhdr_t
*)asoc
->c
.auth_random
;
396 chunksize
+= ntohs(auth_random
->length
);
398 auth_hmacs
= (sctp_paramhdr_t
*)asoc
->c
.auth_hmacs
;
399 if (auth_hmacs
->length
)
400 chunksize
+= ntohs(auth_hmacs
->length
);
404 auth_chunks
= (sctp_paramhdr_t
*)asoc
->c
.auth_chunks
;
405 if (auth_chunks
->length
)
406 chunksize
+= ntohs(auth_chunks
->length
);
410 extensions
[num_ext
] = SCTP_CID_AUTH
;
415 chunksize
+= sizeof(sctp_supported_ext_param_t
) + num_ext
;
417 /* Now allocate and fill out the chunk. */
418 retval
= sctp_make_chunk(asoc
, SCTP_CID_INIT_ACK
, 0, chunksize
);
422 /* Per the advice in RFC 2960 6.4, send this reply to
423 * the source of the INIT packet.
425 retval
->transport
= chunk
->transport
;
426 retval
->subh
.init_hdr
=
427 sctp_addto_chunk(retval
, sizeof(initack
), &initack
);
428 retval
->param_hdr
.v
= sctp_addto_chunk(retval
, addrs_len
, addrs
.v
);
429 sctp_addto_chunk(retval
, cookie_len
, cookie
);
430 if (asoc
->peer
.ecn_capable
)
431 sctp_addto_chunk(retval
, sizeof(ecap_param
), &ecap_param
);
433 ext_param
.param_hdr
.type
= SCTP_PARAM_SUPPORTED_EXT
;
434 ext_param
.param_hdr
.length
=
435 htons(sizeof(sctp_supported_ext_param_t
) + num_ext
);
436 sctp_addto_chunk(retval
, sizeof(sctp_supported_ext_param_t
),
438 sctp_addto_param(retval
, num_ext
, extensions
);
440 if (asoc
->peer
.prsctp_capable
)
441 sctp_addto_chunk(retval
, sizeof(prsctp_param
), &prsctp_param
);
443 if (sp
->adaptation_ind
) {
444 aiparam
.param_hdr
.type
= SCTP_PARAM_ADAPTATION_LAYER_IND
;
445 aiparam
.param_hdr
.length
= htons(sizeof(aiparam
));
446 aiparam
.adaptation_ind
= htonl(sp
->adaptation_ind
);
447 sctp_addto_chunk(retval
, sizeof(aiparam
), &aiparam
);
450 if (asoc
->peer
.auth_capable
) {
451 sctp_addto_chunk(retval
, ntohs(auth_random
->length
),
454 sctp_addto_chunk(retval
, ntohs(auth_hmacs
->length
),
457 sctp_addto_chunk(retval
, ntohs(auth_chunks
->length
),
461 /* We need to remove the const qualifier at this point. */
462 retval
->asoc
= (struct sctp_association
*) asoc
;
464 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
466 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
467 * HEARTBEAT ACK, * etc.) to the same destination transport
468 * address from which it received the DATA or control chunk
469 * to which it is replying.
471 * [INIT ACK back to where the INIT came from.]
474 retval
->transport
= chunk
->transport
;
483 /* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
485 * This chunk is used only during the initialization of an association.
486 * It is sent by the initiator of an association to its peer to complete
487 * the initialization process. This chunk MUST precede any DATA chunk
488 * sent within the association, but MAY be bundled with one or more DATA
489 * chunks in the same packet.
492 * 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
493 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
494 * | Type = 10 |Chunk Flags | Length |
495 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
498 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
502 * Set to zero on transmit and ignored on receipt.
504 * Length: 16 bits (unsigned integer)
506 * Set to the size of the chunk in bytes, including the 4 bytes of
507 * the chunk header and the size of the Cookie.
509 * Cookie: variable size
511 * This field must contain the exact cookie received in the
512 * State Cookie parameter from the previous INIT ACK.
514 * An implementation SHOULD make the cookie as small as possible
515 * to insure interoperability.
517 struct sctp_chunk
*sctp_make_cookie_echo(const struct sctp_association
*asoc
,
518 const struct sctp_chunk
*chunk
)
520 struct sctp_chunk
*retval
;
524 cookie
= asoc
->peer
.cookie
;
525 cookie_len
= asoc
->peer
.cookie_len
;
527 /* Build a cookie echo chunk. */
528 retval
= sctp_make_chunk(asoc
, SCTP_CID_COOKIE_ECHO
, 0, cookie_len
);
531 retval
->subh
.cookie_hdr
=
532 sctp_addto_chunk(retval
, cookie_len
, cookie
);
534 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
536 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
537 * HEARTBEAT ACK, * etc.) to the same destination transport
538 * address from which it * received the DATA or control chunk
539 * to which it is replying.
541 * [COOKIE ECHO back to where the INIT ACK came from.]
544 retval
->transport
= chunk
->transport
;
550 /* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
552 * This chunk is used only during the initialization of an
553 * association. It is used to acknowledge the receipt of a COOKIE
554 * ECHO chunk. This chunk MUST precede any DATA or SACK chunk sent
555 * within the association, but MAY be bundled with one or more DATA
556 * chunks or SACK chunk in the same SCTP packet.
559 * 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
560 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
561 * | Type = 11 |Chunk Flags | Length = 4 |
562 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
564 * Chunk Flags: 8 bits
566 * Set to zero on transmit and ignored on receipt.
568 struct sctp_chunk
*sctp_make_cookie_ack(const struct sctp_association
*asoc
,
569 const struct sctp_chunk
*chunk
)
571 struct sctp_chunk
*retval
;
573 retval
= sctp_make_chunk(asoc
, SCTP_CID_COOKIE_ACK
, 0, 0);
575 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
577 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
578 * HEARTBEAT ACK, * etc.) to the same destination transport
579 * address from which it * received the DATA or control chunk
580 * to which it is replying.
582 * [COOKIE ACK back to where the COOKIE ECHO came from.]
585 retval
->transport
= chunk
->transport
;
591 * Appendix A: Explicit Congestion Notification:
594 * RFC 2481 details a specific bit for a sender to send in the header of
595 * its next outbound TCP segment to indicate to its peer that it has
596 * reduced its congestion window. This is termed the CWR bit. For
597 * SCTP the same indication is made by including the CWR chunk.
598 * This chunk contains one data element, i.e. the TSN number that
599 * was sent in the ECNE chunk. This element represents the lowest
600 * TSN number in the datagram that was originally marked with the
604 * 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
605 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
606 * | Chunk Type=13 | Flags=00000000| Chunk Length = 8 |
607 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
608 * | Lowest TSN Number |
609 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
611 * Note: The CWR is considered a Control chunk.
613 struct sctp_chunk
*sctp_make_cwr(const struct sctp_association
*asoc
,
614 const __u32 lowest_tsn
,
615 const struct sctp_chunk
*chunk
)
617 struct sctp_chunk
*retval
;
620 cwr
.lowest_tsn
= htonl(lowest_tsn
);
621 retval
= sctp_make_chunk(asoc
, SCTP_CID_ECN_CWR
, 0,
622 sizeof(sctp_cwrhdr_t
));
627 retval
->subh
.ecn_cwr_hdr
=
628 sctp_addto_chunk(retval
, sizeof(cwr
), &cwr
);
630 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
632 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
633 * HEARTBEAT ACK, * etc.) to the same destination transport
634 * address from which it * received the DATA or control chunk
635 * to which it is replying.
637 * [Report a reduced congestion window back to where the ECNE
641 retval
->transport
= chunk
->transport
;
647 /* Make an ECNE chunk. This is a congestion experienced report. */
648 struct sctp_chunk
*sctp_make_ecne(const struct sctp_association
*asoc
,
649 const __u32 lowest_tsn
)
651 struct sctp_chunk
*retval
;
654 ecne
.lowest_tsn
= htonl(lowest_tsn
);
655 retval
= sctp_make_chunk(asoc
, SCTP_CID_ECN_ECNE
, 0,
656 sizeof(sctp_ecnehdr_t
));
659 retval
->subh
.ecne_hdr
=
660 sctp_addto_chunk(retval
, sizeof(ecne
), &ecne
);
666 /* Make a DATA chunk for the given association from the provided
667 * parameters. However, do not populate the data payload.
669 struct sctp_chunk
*sctp_make_datafrag_empty(struct sctp_association
*asoc
,
670 const struct sctp_sndrcvinfo
*sinfo
,
671 int data_len
, __u8 flags
, __u16 ssn
)
673 struct sctp_chunk
*retval
;
674 struct sctp_datahdr dp
;
677 /* We assign the TSN as LATE as possible, not here when
678 * creating the chunk.
681 dp
.stream
= htons(sinfo
->sinfo_stream
);
682 dp
.ppid
= sinfo
->sinfo_ppid
;
684 /* Set the flags for an unordered send. */
685 if (sinfo
->sinfo_flags
& SCTP_UNORDERED
) {
686 flags
|= SCTP_DATA_UNORDERED
;
691 chunk_len
= sizeof(dp
) + data_len
;
692 retval
= sctp_make_chunk(asoc
, SCTP_CID_DATA
, flags
, chunk_len
);
696 retval
->subh
.data_hdr
= sctp_addto_chunk(retval
, sizeof(dp
), &dp
);
697 memcpy(&retval
->sinfo
, sinfo
, sizeof(struct sctp_sndrcvinfo
));
703 /* Create a selective ackowledgement (SACK) for the given
704 * association. This reports on which TSN's we've seen to date,
705 * including duplicates and gaps.
707 struct sctp_chunk
*sctp_make_sack(const struct sctp_association
*asoc
)
709 struct sctp_chunk
*retval
;
710 struct sctp_sackhdr sack
;
713 __u16 num_gabs
, num_dup_tsns
;
714 struct sctp_tsnmap
*map
= (struct sctp_tsnmap
*)&asoc
->peer
.tsn_map
;
715 struct sctp_gap_ack_block gabs
[SCTP_MAX_GABS
];
717 memset(gabs
, 0, sizeof(gabs
));
718 ctsn
= sctp_tsnmap_get_ctsn(map
);
719 SCTP_DEBUG_PRINTK("sackCTSNAck sent: 0x%x.\n", ctsn
);
721 /* How much room is needed in the chunk? */
722 num_gabs
= sctp_tsnmap_num_gabs(map
, gabs
);
723 num_dup_tsns
= sctp_tsnmap_num_dups(map
);
725 /* Initialize the SACK header. */
726 sack
.cum_tsn_ack
= htonl(ctsn
);
727 sack
.a_rwnd
= htonl(asoc
->a_rwnd
);
728 sack
.num_gap_ack_blocks
= htons(num_gabs
);
729 sack
.num_dup_tsns
= htons(num_dup_tsns
);
732 + sizeof(struct sctp_gap_ack_block
) * num_gabs
733 + sizeof(__u32
) * num_dup_tsns
;
735 /* Create the chunk. */
736 retval
= sctp_make_chunk(asoc
, SCTP_CID_SACK
, 0, len
);
740 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
742 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
743 * HEARTBEAT ACK, etc.) to the same destination transport
744 * address from which it received the DATA or control chunk to
745 * which it is replying. This rule should also be followed if
746 * the endpoint is bundling DATA chunks together with the
749 * However, when acknowledging multiple DATA chunks received
750 * in packets from different source addresses in a single
751 * SACK, the SACK chunk may be transmitted to one of the
752 * destination transport addresses from which the DATA or
753 * control chunks being acknowledged were received.
755 * [BUG: We do not implement the following paragraph.
756 * Perhaps we should remember the last transport we used for a
757 * SACK and avoid that (if possible) if we have seen any
758 * duplicates. --piggy]
760 * When a receiver of a duplicate DATA chunk sends a SACK to a
761 * multi- homed endpoint it MAY be beneficial to vary the
762 * destination address and not use the source address of the
763 * DATA chunk. The reason being that receiving a duplicate
764 * from a multi-homed endpoint might indicate that the return
765 * path (as specified in the source address of the DATA chunk)
766 * for the SACK is broken.
768 * [Send to the address from which we last received a DATA chunk.]
770 retval
->transport
= asoc
->peer
.last_data_from
;
772 retval
->subh
.sack_hdr
=
773 sctp_addto_chunk(retval
, sizeof(sack
), &sack
);
775 /* Add the gap ack block information. */
777 sctp_addto_chunk(retval
, sizeof(__u32
) * num_gabs
,
780 /* Add the duplicate TSN information. */
782 sctp_addto_chunk(retval
, sizeof(__u32
) * num_dup_tsns
,
783 sctp_tsnmap_get_dups(map
));
789 /* Make a SHUTDOWN chunk. */
790 struct sctp_chunk
*sctp_make_shutdown(const struct sctp_association
*asoc
,
791 const struct sctp_chunk
*chunk
)
793 struct sctp_chunk
*retval
;
794 sctp_shutdownhdr_t shut
;
797 ctsn
= sctp_tsnmap_get_ctsn(&asoc
->peer
.tsn_map
);
798 shut
.cum_tsn_ack
= htonl(ctsn
);
800 retval
= sctp_make_chunk(asoc
, SCTP_CID_SHUTDOWN
, 0,
801 sizeof(sctp_shutdownhdr_t
));
805 retval
->subh
.shutdown_hdr
=
806 sctp_addto_chunk(retval
, sizeof(shut
), &shut
);
809 retval
->transport
= chunk
->transport
;
814 struct sctp_chunk
*sctp_make_shutdown_ack(const struct sctp_association
*asoc
,
815 const struct sctp_chunk
*chunk
)
817 struct sctp_chunk
*retval
;
819 retval
= sctp_make_chunk(asoc
, SCTP_CID_SHUTDOWN_ACK
, 0, 0);
821 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
823 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
824 * HEARTBEAT ACK, * etc.) to the same destination transport
825 * address from which it * received the DATA or control chunk
826 * to which it is replying.
828 * [ACK back to where the SHUTDOWN came from.]
831 retval
->transport
= chunk
->transport
;
836 struct sctp_chunk
*sctp_make_shutdown_complete(
837 const struct sctp_association
*asoc
,
838 const struct sctp_chunk
*chunk
)
840 struct sctp_chunk
*retval
;
843 /* Set the T-bit if we have no association (vtag will be
846 flags
|= asoc
? 0 : SCTP_CHUNK_FLAG_T
;
848 retval
= sctp_make_chunk(asoc
, SCTP_CID_SHUTDOWN_COMPLETE
, flags
, 0);
850 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
852 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
853 * HEARTBEAT ACK, * etc.) to the same destination transport
854 * address from which it * received the DATA or control chunk
855 * to which it is replying.
857 * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
861 retval
->transport
= chunk
->transport
;
866 /* Create an ABORT. Note that we set the T bit if we have no
867 * association, except when responding to an INIT (sctpimpguide 2.41).
869 struct sctp_chunk
*sctp_make_abort(const struct sctp_association
*asoc
,
870 const struct sctp_chunk
*chunk
,
873 struct sctp_chunk
*retval
;
876 /* Set the T-bit if we have no association and 'chunk' is not
877 * an INIT (vtag will be reflected).
880 if (chunk
&& chunk
->chunk_hdr
&&
881 chunk
->chunk_hdr
->type
== SCTP_CID_INIT
)
884 flags
= SCTP_CHUNK_FLAG_T
;
887 retval
= sctp_make_chunk(asoc
, SCTP_CID_ABORT
, flags
, hint
);
889 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
891 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
892 * HEARTBEAT ACK, * etc.) to the same destination transport
893 * address from which it * received the DATA or control chunk
894 * to which it is replying.
896 * [ABORT back to where the offender came from.]
899 retval
->transport
= chunk
->transport
;
904 /* Helper to create ABORT with a NO_USER_DATA error. */
905 struct sctp_chunk
*sctp_make_abort_no_data(
906 const struct sctp_association
*asoc
,
907 const struct sctp_chunk
*chunk
, __u32 tsn
)
909 struct sctp_chunk
*retval
;
912 retval
= sctp_make_abort(asoc
, chunk
, sizeof(sctp_errhdr_t
)
918 /* Put the tsn back into network byte order. */
919 payload
= htonl(tsn
);
920 sctp_init_cause(retval
, SCTP_ERROR_NO_DATA
, sizeof(payload
));
921 sctp_addto_chunk(retval
, sizeof(payload
), (const void *)&payload
);
923 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
925 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
926 * HEARTBEAT ACK, * etc.) to the same destination transport
927 * address from which it * received the DATA or control chunk
928 * to which it is replying.
930 * [ABORT back to where the offender came from.]
933 retval
->transport
= chunk
->transport
;
939 /* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error. */
940 struct sctp_chunk
*sctp_make_abort_user(const struct sctp_association
*asoc
,
941 const struct msghdr
*msg
,
944 struct sctp_chunk
*retval
;
945 void *payload
= NULL
;
948 retval
= sctp_make_abort(asoc
, NULL
, sizeof(sctp_errhdr_t
) + paylen
);
953 /* Put the msg_iov together into payload. */
954 payload
= kmalloc(paylen
, GFP_KERNEL
);
958 err
= memcpy_fromiovec(payload
, msg
->msg_iov
, paylen
);
963 sctp_init_cause(retval
, SCTP_ERROR_USER_ABORT
, paylen
);
964 sctp_addto_chunk(retval
, paylen
, payload
);
974 sctp_chunk_free(retval
);
980 /* Append bytes to the end of a parameter. Will panic if chunk is not big
983 static void *sctp_addto_param(struct sctp_chunk
*chunk
, int len
,
987 int chunklen
= ntohs(chunk
->chunk_hdr
->length
);
989 target
= skb_put(chunk
->skb
, len
);
992 memcpy(target
, data
, len
);
994 memset(target
, 0, len
);
996 /* Adjust the chunk length field. */
997 chunk
->chunk_hdr
->length
= htons(chunklen
+ len
);
998 chunk
->chunk_end
= skb_tail_pointer(chunk
->skb
);
1003 /* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
1004 struct sctp_chunk
*sctp_make_abort_violation(
1005 const struct sctp_association
*asoc
,
1006 const struct sctp_chunk
*chunk
,
1007 const __u8
*payload
,
1008 const size_t paylen
)
1010 struct sctp_chunk
*retval
;
1011 struct sctp_paramhdr phdr
;
1013 retval
= sctp_make_abort(asoc
, chunk
, sizeof(sctp_errhdr_t
) + paylen
1014 + sizeof(sctp_paramhdr_t
));
1018 sctp_init_cause(retval
, SCTP_ERROR_PROTO_VIOLATION
, paylen
1019 + sizeof(sctp_paramhdr_t
));
1021 phdr
.type
= htons(chunk
->chunk_hdr
->type
);
1022 phdr
.length
= chunk
->chunk_hdr
->length
;
1023 sctp_addto_chunk(retval
, paylen
, payload
);
1024 sctp_addto_param(retval
, sizeof(sctp_paramhdr_t
), &phdr
);
1030 struct sctp_chunk
*sctp_make_violation_paramlen(
1031 const struct sctp_association
*asoc
,
1032 const struct sctp_chunk
*chunk
,
1033 struct sctp_paramhdr
*param
)
1035 struct sctp_chunk
*retval
;
1036 static const char error
[] = "The following parameter had invalid length:";
1037 size_t payload_len
= sizeof(error
) + sizeof(sctp_errhdr_t
) +
1038 sizeof(sctp_paramhdr_t
);
1040 retval
= sctp_make_abort(asoc
, chunk
, payload_len
);
1044 sctp_init_cause(retval
, SCTP_ERROR_PROTO_VIOLATION
,
1045 sizeof(error
) + sizeof(sctp_paramhdr_t
));
1046 sctp_addto_chunk(retval
, sizeof(error
), error
);
1047 sctp_addto_param(retval
, sizeof(sctp_paramhdr_t
), param
);
1053 /* Make a HEARTBEAT chunk. */
1054 struct sctp_chunk
*sctp_make_heartbeat(const struct sctp_association
*asoc
,
1055 const struct sctp_transport
*transport
,
1056 const void *payload
, const size_t paylen
)
1058 struct sctp_chunk
*retval
= sctp_make_chunk(asoc
, SCTP_CID_HEARTBEAT
,
1064 /* Cast away the 'const', as this is just telling the chunk
1065 * what transport it belongs to.
1067 retval
->transport
= (struct sctp_transport
*) transport
;
1068 retval
->subh
.hbs_hdr
= sctp_addto_chunk(retval
, paylen
, payload
);
1074 struct sctp_chunk
*sctp_make_heartbeat_ack(const struct sctp_association
*asoc
,
1075 const struct sctp_chunk
*chunk
,
1076 const void *payload
, const size_t paylen
)
1078 struct sctp_chunk
*retval
;
1080 retval
= sctp_make_chunk(asoc
, SCTP_CID_HEARTBEAT_ACK
, 0, paylen
);
1084 retval
->subh
.hbs_hdr
= sctp_addto_chunk(retval
, paylen
, payload
);
1086 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1088 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1089 * HEARTBEAT ACK, * etc.) to the same destination transport
1090 * address from which it * received the DATA or control chunk
1091 * to which it is replying.
1093 * [HBACK back to where the HEARTBEAT came from.]
1096 retval
->transport
= chunk
->transport
;
1102 /* Create an Operation Error chunk with the specified space reserved.
1103 * This routine can be used for containing multiple causes in the chunk.
1105 static struct sctp_chunk
*sctp_make_op_error_space(
1106 const struct sctp_association
*asoc
,
1107 const struct sctp_chunk
*chunk
,
1110 struct sctp_chunk
*retval
;
1112 retval
= sctp_make_chunk(asoc
, SCTP_CID_ERROR
, 0,
1113 sizeof(sctp_errhdr_t
) + size
);
1117 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1119 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1120 * HEARTBEAT ACK, etc.) to the same destination transport
1121 * address from which it received the DATA or control chunk
1122 * to which it is replying.
1126 retval
->transport
= chunk
->transport
;
1132 /* Create an Operation Error chunk. */
1133 struct sctp_chunk
*sctp_make_op_error(const struct sctp_association
*asoc
,
1134 const struct sctp_chunk
*chunk
,
1135 __be16 cause_code
, const void *payload
,
1136 size_t paylen
, size_t reserve_tail
)
1138 struct sctp_chunk
*retval
;
1140 retval
= sctp_make_op_error_space(asoc
, chunk
, paylen
+ reserve_tail
);
1144 sctp_init_cause(retval
, cause_code
, paylen
+ reserve_tail
);
1145 sctp_addto_chunk(retval
, paylen
, payload
);
1147 sctp_addto_param(retval
, reserve_tail
, NULL
);
1153 struct sctp_chunk
*sctp_make_auth(const struct sctp_association
*asoc
)
1155 struct sctp_chunk
*retval
;
1156 struct sctp_hmac
*hmac_desc
;
1157 struct sctp_authhdr auth_hdr
;
1160 /* Get the first hmac that the peer told us to use */
1161 hmac_desc
= sctp_auth_asoc_get_hmac(asoc
);
1162 if (unlikely(!hmac_desc
))
1165 retval
= sctp_make_chunk(asoc
, SCTP_CID_AUTH
, 0,
1166 hmac_desc
->hmac_len
+ sizeof(sctp_authhdr_t
));
1170 auth_hdr
.hmac_id
= htons(hmac_desc
->hmac_id
);
1171 auth_hdr
.shkey_id
= htons(asoc
->active_key_id
);
1173 retval
->subh
.auth_hdr
= sctp_addto_chunk(retval
, sizeof(sctp_authhdr_t
),
1176 hmac
= skb_put(retval
->skb
, hmac_desc
->hmac_len
);
1177 memset(hmac
, 0, hmac_desc
->hmac_len
);
1179 /* Adjust the chunk header to include the empty MAC */
1180 retval
->chunk_hdr
->length
=
1181 htons(ntohs(retval
->chunk_hdr
->length
) + hmac_desc
->hmac_len
);
1182 retval
->chunk_end
= skb_tail_pointer(retval
->skb
);
1188 /********************************************************************
1189 * 2nd Level Abstractions
1190 ********************************************************************/
1192 /* Turn an skb into a chunk.
1193 * FIXME: Eventually move the structure directly inside the skb->cb[].
1195 struct sctp_chunk
*sctp_chunkify(struct sk_buff
*skb
,
1196 const struct sctp_association
*asoc
,
1199 struct sctp_chunk
*retval
;
1201 retval
= kmem_cache_zalloc(sctp_chunk_cachep
, GFP_ATOMIC
);
1207 SCTP_DEBUG_PRINTK("chunkifying skb %p w/o an sk\n", skb
);
1210 INIT_LIST_HEAD(&retval
->list
);
1212 retval
->asoc
= (struct sctp_association
*)asoc
;
1214 retval
->has_tsn
= 0;
1215 retval
->has_ssn
= 0;
1216 retval
->rtt_in_progress
= 0;
1217 retval
->sent_at
= 0;
1218 retval
->singleton
= 1;
1219 retval
->end_of_packet
= 0;
1220 retval
->ecn_ce_done
= 0;
1221 retval
->pdiscard
= 0;
1223 /* sctpimpguide-05.txt Section 2.8.2
1224 * M1) Each time a new DATA chunk is transmitted
1225 * set the 'TSN.Missing.Report' count for that TSN to 0. The
1226 * 'TSN.Missing.Report' count will be used to determine missing chunks
1227 * and when to fast retransmit.
1229 retval
->tsn_missing_report
= 0;
1230 retval
->tsn_gap_acked
= 0;
1231 retval
->fast_retransmit
= SCTP_CAN_FRTX
;
1233 /* If this is a fragmented message, track all fragments
1234 * of the message (for SEND_FAILED).
1238 /* Polish the bead hole. */
1239 INIT_LIST_HEAD(&retval
->transmitted_list
);
1240 INIT_LIST_HEAD(&retval
->frag_list
);
1241 SCTP_DBG_OBJCNT_INC(chunk
);
1242 atomic_set(&retval
->refcnt
, 1);
1248 /* Set chunk->source and dest based on the IP header in chunk->skb. */
1249 void sctp_init_addrs(struct sctp_chunk
*chunk
, union sctp_addr
*src
,
1250 union sctp_addr
*dest
)
1252 memcpy(&chunk
->source
, src
, sizeof(union sctp_addr
));
1253 memcpy(&chunk
->dest
, dest
, sizeof(union sctp_addr
));
1256 /* Extract the source address from a chunk. */
1257 const union sctp_addr
*sctp_source(const struct sctp_chunk
*chunk
)
1259 /* If we have a known transport, use that. */
1260 if (chunk
->transport
) {
1261 return &chunk
->transport
->ipaddr
;
1263 /* Otherwise, extract it from the IP header. */
1264 return &chunk
->source
;
1268 /* Create a new chunk, setting the type and flags headers from the
1269 * arguments, reserving enough space for a 'paylen' byte payload.
1272 struct sctp_chunk
*sctp_make_chunk(const struct sctp_association
*asoc
,
1273 __u8 type
, __u8 flags
, int paylen
)
1275 struct sctp_chunk
*retval
;
1276 sctp_chunkhdr_t
*chunk_hdr
;
1277 struct sk_buff
*skb
;
1280 /* No need to allocate LL here, as this is only a chunk. */
1281 skb
= alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t
) + paylen
),
1286 /* Make room for the chunk header. */
1287 chunk_hdr
= (sctp_chunkhdr_t
*)skb_put(skb
, sizeof(sctp_chunkhdr_t
));
1288 chunk_hdr
->type
= type
;
1289 chunk_hdr
->flags
= flags
;
1290 chunk_hdr
->length
= htons(sizeof(sctp_chunkhdr_t
));
1292 sk
= asoc
? asoc
->base
.sk
: NULL
;
1293 retval
= sctp_chunkify(skb
, asoc
, sk
);
1299 retval
->chunk_hdr
= chunk_hdr
;
1300 retval
->chunk_end
= ((__u8
*)chunk_hdr
) + sizeof(struct sctp_chunkhdr
);
1302 /* Determine if the chunk needs to be authenticated */
1303 if (sctp_auth_send_cid(type
, asoc
))
1306 /* Set the skb to the belonging sock for accounting. */
1315 /* Release the memory occupied by a chunk. */
1316 static void sctp_chunk_destroy(struct sctp_chunk
*chunk
)
1318 BUG_ON(!list_empty(&chunk
->list
));
1319 list_del_init(&chunk
->transmitted_list
);
1321 /* Free the chunk skb data and the SCTP_chunk stub itself. */
1322 dev_kfree_skb(chunk
->skb
);
1324 SCTP_DBG_OBJCNT_DEC(chunk
);
1325 kmem_cache_free(sctp_chunk_cachep
, chunk
);
1328 /* Possibly, free the chunk. */
1329 void sctp_chunk_free(struct sctp_chunk
*chunk
)
1331 /* Release our reference on the message tracker. */
1333 sctp_datamsg_put(chunk
->msg
);
1335 sctp_chunk_put(chunk
);
1338 /* Grab a reference to the chunk. */
1339 void sctp_chunk_hold(struct sctp_chunk
*ch
)
1341 atomic_inc(&ch
->refcnt
);
1344 /* Release a reference to the chunk. */
1345 void sctp_chunk_put(struct sctp_chunk
*ch
)
1347 if (atomic_dec_and_test(&ch
->refcnt
))
1348 sctp_chunk_destroy(ch
);
1351 /* Append bytes to the end of a chunk. Will panic if chunk is not big
1354 void *sctp_addto_chunk(struct sctp_chunk
*chunk
, int len
, const void *data
)
1358 int chunklen
= ntohs(chunk
->chunk_hdr
->length
);
1359 int padlen
= WORD_ROUND(chunklen
) - chunklen
;
1361 padding
= skb_put(chunk
->skb
, padlen
);
1362 target
= skb_put(chunk
->skb
, len
);
1364 memset(padding
, 0, padlen
);
1365 memcpy(target
, data
, len
);
1367 /* Adjust the chunk length field. */
1368 chunk
->chunk_hdr
->length
= htons(chunklen
+ padlen
+ len
);
1369 chunk
->chunk_end
= skb_tail_pointer(chunk
->skb
);
1374 /* Append bytes from user space to the end of a chunk. Will panic if
1375 * chunk is not big enough.
1376 * Returns a kernel err value.
1378 int sctp_user_addto_chunk(struct sctp_chunk
*chunk
, int off
, int len
,
1384 /* Make room in chunk for data. */
1385 target
= skb_put(chunk
->skb
, len
);
1387 /* Copy data (whole iovec) into chunk */
1388 if ((err
= memcpy_fromiovecend(target
, data
, off
, len
)))
1391 /* Adjust the chunk length field. */
1392 chunk
->chunk_hdr
->length
=
1393 htons(ntohs(chunk
->chunk_hdr
->length
) + len
);
1394 chunk
->chunk_end
= skb_tail_pointer(chunk
->skb
);
1400 /* Helper function to assign a TSN if needed. This assumes that both
1401 * the data_hdr and association have already been assigned.
1403 void sctp_chunk_assign_ssn(struct sctp_chunk
*chunk
)
1405 struct sctp_datamsg
*msg
;
1406 struct sctp_chunk
*lchunk
;
1407 struct sctp_stream
*stream
;
1414 /* All fragments will be on the same stream */
1415 sid
= ntohs(chunk
->subh
.data_hdr
->stream
);
1416 stream
= &chunk
->asoc
->ssnmap
->out
;
1418 /* Now assign the sequence number to the entire message.
1419 * All fragments must have the same stream sequence number.
1422 list_for_each_entry(lchunk
, &msg
->chunks
, frag_list
) {
1423 if (lchunk
->chunk_hdr
->flags
& SCTP_DATA_UNORDERED
) {
1426 if (lchunk
->chunk_hdr
->flags
& SCTP_DATA_LAST_FRAG
)
1427 ssn
= sctp_ssn_next(stream
, sid
);
1429 ssn
= sctp_ssn_peek(stream
, sid
);
1432 lchunk
->subh
.data_hdr
->ssn
= htons(ssn
);
1433 lchunk
->has_ssn
= 1;
1437 /* Helper function to assign a TSN if needed. This assumes that both
1438 * the data_hdr and association have already been assigned.
1440 void sctp_chunk_assign_tsn(struct sctp_chunk
*chunk
)
1442 if (!chunk
->has_tsn
) {
1443 /* This is the last possible instant to
1446 chunk
->subh
.data_hdr
->tsn
=
1447 htonl(sctp_association_get_next_tsn(chunk
->asoc
));
1452 /* Create a CLOSED association to use with an incoming packet. */
1453 struct sctp_association
*sctp_make_temp_asoc(const struct sctp_endpoint
*ep
,
1454 struct sctp_chunk
*chunk
,
1457 struct sctp_association
*asoc
;
1458 struct sk_buff
*skb
;
1462 /* Create the bare association. */
1463 scope
= sctp_scope(sctp_source(chunk
));
1464 asoc
= sctp_association_new(ep
, ep
->base
.sk
, scope
, gfp
);
1469 /* Create an entry for the source address of the packet. */
1470 af
= sctp_get_af_specific(ipver2af(ip_hdr(skb
)->version
));
1473 af
->from_skb(&asoc
->c
.peer_addr
, skb
, 1);
1478 sctp_association_free(asoc
);
1482 /* Build a cookie representing asoc.
1483 * This INCLUDES the param header needed to put the cookie in the INIT ACK.
1485 static sctp_cookie_param_t
*sctp_pack_cookie(const struct sctp_endpoint
*ep
,
1486 const struct sctp_association
*asoc
,
1487 const struct sctp_chunk
*init_chunk
,
1489 const __u8
*raw_addrs
, int addrs_len
)
1491 sctp_cookie_param_t
*retval
;
1492 struct sctp_signed_cookie
*cookie
;
1493 struct scatterlist sg
;
1494 int headersize
, bodysize
;
1495 unsigned int keylen
;
1498 /* Header size is static data prior to the actual cookie, including
1501 headersize
= sizeof(sctp_paramhdr_t
) +
1502 (sizeof(struct sctp_signed_cookie
) -
1503 sizeof(struct sctp_cookie
));
1504 bodysize
= sizeof(struct sctp_cookie
)
1505 + ntohs(init_chunk
->chunk_hdr
->length
) + addrs_len
;
1507 /* Pad out the cookie to a multiple to make the signature
1508 * functions simpler to write.
1510 if (bodysize
% SCTP_COOKIE_MULTIPLE
)
1511 bodysize
+= SCTP_COOKIE_MULTIPLE
1512 - (bodysize
% SCTP_COOKIE_MULTIPLE
);
1513 *cookie_len
= headersize
+ bodysize
;
1515 /* Clear this memory since we are sending this data structure
1516 * out on the network.
1518 retval
= kzalloc(*cookie_len
, GFP_ATOMIC
);
1522 cookie
= (struct sctp_signed_cookie
*) retval
->body
;
1524 /* Set up the parameter header. */
1525 retval
->p
.type
= SCTP_PARAM_STATE_COOKIE
;
1526 retval
->p
.length
= htons(*cookie_len
);
1528 /* Copy the cookie part of the association itself. */
1529 cookie
->c
= asoc
->c
;
1530 /* Save the raw address list length in the cookie. */
1531 cookie
->c
.raw_addr_list_len
= addrs_len
;
1533 /* Remember PR-SCTP capability. */
1534 cookie
->c
.prsctp_capable
= asoc
->peer
.prsctp_capable
;
1536 /* Save adaptation indication in the cookie. */
1537 cookie
->c
.adaptation_ind
= asoc
->peer
.adaptation_ind
;
1539 /* Set an expiration time for the cookie. */
1540 do_gettimeofday(&cookie
->c
.expiration
);
1541 TIMEVAL_ADD(asoc
->cookie_life
, cookie
->c
.expiration
);
1543 /* Copy the peer's init packet. */
1544 memcpy(&cookie
->c
.peer_init
[0], init_chunk
->chunk_hdr
,
1545 ntohs(init_chunk
->chunk_hdr
->length
));
1547 /* Copy the raw local address list of the association. */
1548 memcpy((__u8
*)&cookie
->c
.peer_init
[0] +
1549 ntohs(init_chunk
->chunk_hdr
->length
), raw_addrs
, addrs_len
);
1551 if (sctp_sk(ep
->base
.sk
)->hmac
) {
1552 struct hash_desc desc
;
1554 /* Sign the message. */
1555 sg_init_one(&sg
, &cookie
->c
, bodysize
);
1556 keylen
= SCTP_SECRET_SIZE
;
1557 key
= (char *)ep
->secret_key
[ep
->current_key
];
1558 desc
.tfm
= sctp_sk(ep
->base
.sk
)->hmac
;
1561 if (crypto_hash_setkey(desc
.tfm
, key
, keylen
) ||
1562 crypto_hash_digest(&desc
, &sg
, bodysize
, cookie
->signature
))
1575 /* Unpack the cookie from COOKIE ECHO chunk, recreating the association. */
1576 struct sctp_association
*sctp_unpack_cookie(
1577 const struct sctp_endpoint
*ep
,
1578 const struct sctp_association
*asoc
,
1579 struct sctp_chunk
*chunk
, gfp_t gfp
,
1580 int *error
, struct sctp_chunk
**errp
)
1582 struct sctp_association
*retval
= NULL
;
1583 struct sctp_signed_cookie
*cookie
;
1584 struct sctp_cookie
*bear_cookie
;
1585 int headersize
, bodysize
, fixed_size
;
1586 __u8
*digest
= ep
->digest
;
1587 struct scatterlist sg
;
1588 unsigned int keylen
, len
;
1591 struct sk_buff
*skb
= chunk
->skb
;
1593 struct hash_desc desc
;
1595 /* Header size is static data prior to the actual cookie, including
1598 headersize
= sizeof(sctp_chunkhdr_t
) +
1599 (sizeof(struct sctp_signed_cookie
) -
1600 sizeof(struct sctp_cookie
));
1601 bodysize
= ntohs(chunk
->chunk_hdr
->length
) - headersize
;
1602 fixed_size
= headersize
+ sizeof(struct sctp_cookie
);
1604 /* Verify that the chunk looks like it even has a cookie.
1605 * There must be enough room for our cookie and our peer's
1608 len
= ntohs(chunk
->chunk_hdr
->length
);
1609 if (len
< fixed_size
+ sizeof(struct sctp_chunkhdr
))
1612 /* Verify that the cookie has been padded out. */
1613 if (bodysize
% SCTP_COOKIE_MULTIPLE
)
1616 /* Process the cookie. */
1617 cookie
= chunk
->subh
.cookie_hdr
;
1618 bear_cookie
= &cookie
->c
;
1620 if (!sctp_sk(ep
->base
.sk
)->hmac
)
1623 /* Check the signature. */
1624 keylen
= SCTP_SECRET_SIZE
;
1625 sg_init_one(&sg
, bear_cookie
, bodysize
);
1626 key
= (char *)ep
->secret_key
[ep
->current_key
];
1627 desc
.tfm
= sctp_sk(ep
->base
.sk
)->hmac
;
1630 memset(digest
, 0x00, SCTP_SIGNATURE_SIZE
);
1631 if (crypto_hash_setkey(desc
.tfm
, key
, keylen
) ||
1632 crypto_hash_digest(&desc
, &sg
, bodysize
, digest
)) {
1633 *error
= -SCTP_IERROR_NOMEM
;
1637 if (memcmp(digest
, cookie
->signature
, SCTP_SIGNATURE_SIZE
)) {
1638 /* Try the previous key. */
1639 key
= (char *)ep
->secret_key
[ep
->last_key
];
1640 memset(digest
, 0x00, SCTP_SIGNATURE_SIZE
);
1641 if (crypto_hash_setkey(desc
.tfm
, key
, keylen
) ||
1642 crypto_hash_digest(&desc
, &sg
, bodysize
, digest
)) {
1643 *error
= -SCTP_IERROR_NOMEM
;
1647 if (memcmp(digest
, cookie
->signature
, SCTP_SIGNATURE_SIZE
)) {
1648 /* Yikes! Still bad signature! */
1649 *error
= -SCTP_IERROR_BAD_SIG
;
1655 /* IG Section 2.35.2:
1656 * 3) Compare the port numbers and the verification tag contained
1657 * within the COOKIE ECHO chunk to the actual port numbers and the
1658 * verification tag within the SCTP common header of the received
1659 * packet. If these values do not match the packet MUST be silently
1662 if (ntohl(chunk
->sctp_hdr
->vtag
) != bear_cookie
->my_vtag
) {
1663 *error
= -SCTP_IERROR_BAD_TAG
;
1667 if (chunk
->sctp_hdr
->source
!= bear_cookie
->peer_addr
.v4
.sin_port
||
1668 ntohs(chunk
->sctp_hdr
->dest
) != bear_cookie
->my_port
) {
1669 *error
= -SCTP_IERROR_BAD_PORTS
;
1673 /* Check to see if the cookie is stale. If there is already
1674 * an association, there is no need to check cookie's expiration
1675 * for init collision case of lost COOKIE ACK.
1676 * If skb has been timestamped, then use the stamp, otherwise
1677 * use current time. This introduces a small possibility that
1678 * that a cookie may be considered expired, but his would only slow
1679 * down the new association establishment instead of every packet.
1681 if (sock_flag(ep
->base
.sk
, SOCK_TIMESTAMP
))
1682 skb_get_timestamp(skb
, &tv
);
1684 do_gettimeofday(&tv
);
1686 if (!asoc
&& tv_lt(bear_cookie
->expiration
, tv
)) {
1688 * Section 3.3.10.3 Stale Cookie Error (3)
1692 * Stale Cookie Error: Indicates the receipt of a valid State
1693 * Cookie that has expired.
1695 len
= ntohs(chunk
->chunk_hdr
->length
);
1696 *errp
= sctp_make_op_error_space(asoc
, chunk
, len
);
1698 suseconds_t usecs
= (tv
.tv_sec
-
1699 bear_cookie
->expiration
.tv_sec
) * 1000000L +
1700 tv
.tv_usec
- bear_cookie
->expiration
.tv_usec
;
1701 __be32 n
= htonl(usecs
);
1703 sctp_init_cause(*errp
, SCTP_ERROR_STALE_COOKIE
,
1705 sctp_addto_chunk(*errp
, sizeof(n
), &n
);
1706 *error
= -SCTP_IERROR_STALE_COOKIE
;
1708 *error
= -SCTP_IERROR_NOMEM
;
1713 /* Make a new base association. */
1714 scope
= sctp_scope(sctp_source(chunk
));
1715 retval
= sctp_association_new(ep
, ep
->base
.sk
, scope
, gfp
);
1717 *error
= -SCTP_IERROR_NOMEM
;
1721 /* Set up our peer's port number. */
1722 retval
->peer
.port
= ntohs(chunk
->sctp_hdr
->source
);
1724 /* Populate the association from the cookie. */
1725 memcpy(&retval
->c
, bear_cookie
, sizeof(*bear_cookie
));
1727 if (sctp_assoc_set_bind_addr_from_cookie(retval
, bear_cookie
,
1729 *error
= -SCTP_IERROR_NOMEM
;
1733 /* Also, add the destination address. */
1734 if (list_empty(&retval
->base
.bind_addr
.address_list
)) {
1735 sctp_add_bind_addr(&retval
->base
.bind_addr
, &chunk
->dest
,
1736 SCTP_ADDR_SRC
, GFP_ATOMIC
);
1739 retval
->next_tsn
= retval
->c
.initial_tsn
;
1740 retval
->ctsn_ack_point
= retval
->next_tsn
- 1;
1741 retval
->addip_serial
= retval
->c
.initial_tsn
;
1742 retval
->adv_peer_ack_point
= retval
->ctsn_ack_point
;
1743 retval
->peer
.prsctp_capable
= retval
->c
.prsctp_capable
;
1744 retval
->peer
.adaptation_ind
= retval
->c
.adaptation_ind
;
1746 /* The INIT stuff will be done by the side effects. */
1751 sctp_association_free(retval
);
1756 /* Yikes! The packet is either corrupt or deliberately
1759 *error
= -SCTP_IERROR_MALFORMED
;
1763 /********************************************************************
1764 * 3rd Level Abstractions
1765 ********************************************************************/
1767 struct __sctp_missing
{
1770 } __attribute__((packed
));
1773 * Report a missing mandatory parameter.
1775 static int sctp_process_missing_param(const struct sctp_association
*asoc
,
1776 sctp_param_t paramtype
,
1777 struct sctp_chunk
*chunk
,
1778 struct sctp_chunk
**errp
)
1780 struct __sctp_missing report
;
1783 len
= WORD_ROUND(sizeof(report
));
1785 /* Make an ERROR chunk, preparing enough room for
1786 * returning multiple unknown parameters.
1789 *errp
= sctp_make_op_error_space(asoc
, chunk
, len
);
1792 report
.num_missing
= htonl(1);
1793 report
.type
= paramtype
;
1794 sctp_init_cause(*errp
, SCTP_ERROR_MISS_PARAM
,
1796 sctp_addto_chunk(*errp
, sizeof(report
), &report
);
1799 /* Stop processing this chunk. */
1803 /* Report an Invalid Mandatory Parameter. */
1804 static int sctp_process_inv_mandatory(const struct sctp_association
*asoc
,
1805 struct sctp_chunk
*chunk
,
1806 struct sctp_chunk
**errp
)
1808 /* Invalid Mandatory Parameter Error has no payload. */
1811 *errp
= sctp_make_op_error_space(asoc
, chunk
, 0);
1814 sctp_init_cause(*errp
, SCTP_ERROR_INV_PARAM
, 0);
1816 /* Stop processing this chunk. */
1820 static int sctp_process_inv_paramlength(const struct sctp_association
*asoc
,
1821 struct sctp_paramhdr
*param
,
1822 const struct sctp_chunk
*chunk
,
1823 struct sctp_chunk
**errp
)
1825 /* This is a fatal error. Any accumulated non-fatal errors are
1829 sctp_chunk_free(*errp
);
1831 /* Create an error chunk and fill it in with our payload. */
1832 *errp
= sctp_make_violation_paramlen(asoc
, chunk
, param
);
1838 /* Do not attempt to handle the HOST_NAME parm. However, do
1839 * send back an indicator to the peer.
1841 static int sctp_process_hn_param(const struct sctp_association
*asoc
,
1842 union sctp_params param
,
1843 struct sctp_chunk
*chunk
,
1844 struct sctp_chunk
**errp
)
1846 __u16 len
= ntohs(param
.p
->length
);
1848 /* Processing of the HOST_NAME parameter will generate an
1849 * ABORT. If we've accumulated any non-fatal errors, they
1850 * would be unrecognized parameters and we should not include
1851 * them in the ABORT.
1854 sctp_chunk_free(*errp
);
1856 *errp
= sctp_make_op_error_space(asoc
, chunk
, len
);
1859 sctp_init_cause(*errp
, SCTP_ERROR_DNS_FAILED
, len
);
1860 sctp_addto_chunk(*errp
, len
, param
.v
);
1863 /* Stop processing this chunk. */
1867 static int sctp_verify_ext_param(union sctp_params param
)
1869 __u16 num_ext
= ntohs(param
.p
->length
) - sizeof(sctp_paramhdr_t
);
1871 int have_asconf
= 0;
1874 for (i
= 0; i
< num_ext
; i
++) {
1875 switch (param
.ext
->chunks
[i
]) {
1879 case SCTP_CID_ASCONF
:
1880 case SCTP_CID_ASCONF_ACK
:
1886 /* ADD-IP Security: The draft requires us to ABORT or ignore the
1887 * INIT/INIT-ACK if ADD-IP is listed, but AUTH is not. Do this
1888 * only if ADD-IP is turned on and we are not backward-compatible
1891 if (sctp_addip_noauth
)
1894 if (sctp_addip_enable
&& !have_auth
&& have_asconf
)
1900 static void sctp_process_ext_param(struct sctp_association
*asoc
,
1901 union sctp_params param
)
1903 __u16 num_ext
= ntohs(param
.p
->length
) - sizeof(sctp_paramhdr_t
);
1906 for (i
= 0; i
< num_ext
; i
++) {
1907 switch (param
.ext
->chunks
[i
]) {
1908 case SCTP_CID_FWD_TSN
:
1909 if (sctp_prsctp_enable
&&
1910 !asoc
->peer
.prsctp_capable
)
1911 asoc
->peer
.prsctp_capable
= 1;
1914 /* if the peer reports AUTH, assume that he
1917 if (sctp_auth_enable
)
1918 asoc
->peer
.auth_capable
= 1;
1920 case SCTP_CID_ASCONF
:
1921 case SCTP_CID_ASCONF_ACK
:
1922 if (sctp_addip_enable
)
1923 asoc
->peer
.asconf_capable
= 1;
1931 /* RFC 3.2.1 & the Implementers Guide 2.2.
1933 * The Parameter Types are encoded such that the
1934 * highest-order two bits specify the action that must be
1935 * taken if the processing endpoint does not recognize the
1938 * 00 - Stop processing this parameter; do not process any further
1939 * parameters within this chunk
1941 * 01 - Stop processing this parameter, do not process any further
1942 * parameters within this chunk, and report the unrecognized
1943 * parameter in an 'Unrecognized Parameter' ERROR chunk.
1945 * 10 - Skip this parameter and continue processing.
1947 * 11 - Skip this parameter and continue processing but
1948 * report the unrecognized parameter in an
1949 * 'Unrecognized Parameter' ERROR chunk.
1952 * SCTP_IERROR_NO_ERROR - continue with the chunk
1953 * SCTP_IERROR_ERROR - stop and report an error.
1954 * SCTP_IERROR_NOMEME - out of memory.
1956 static sctp_ierror_t
sctp_process_unk_param(const struct sctp_association
*asoc
,
1957 union sctp_params param
,
1958 struct sctp_chunk
*chunk
,
1959 struct sctp_chunk
**errp
)
1961 int retval
= SCTP_IERROR_NO_ERROR
;
1963 switch (param
.p
->type
& SCTP_PARAM_ACTION_MASK
) {
1964 case SCTP_PARAM_ACTION_DISCARD
:
1965 retval
= SCTP_IERROR_ERROR
;
1967 case SCTP_PARAM_ACTION_SKIP
:
1969 case SCTP_PARAM_ACTION_DISCARD_ERR
:
1970 retval
= SCTP_IERROR_ERROR
;
1972 case SCTP_PARAM_ACTION_SKIP_ERR
:
1973 /* Make an ERROR chunk, preparing enough room for
1974 * returning multiple unknown parameters.
1977 *errp
= sctp_make_op_error_space(asoc
, chunk
,
1978 ntohs(chunk
->chunk_hdr
->length
));
1981 sctp_init_cause(*errp
, SCTP_ERROR_UNKNOWN_PARAM
,
1982 WORD_ROUND(ntohs(param
.p
->length
)));
1983 sctp_addto_chunk(*errp
,
1984 WORD_ROUND(ntohs(param
.p
->length
)),
1987 /* If there is no memory for generating the ERROR
1988 * report as specified, an ABORT will be triggered
1989 * to the peer and the association won't be
1992 retval
= SCTP_IERROR_NOMEM
;
2002 /* Verify variable length parameters
2004 * SCTP_IERROR_ABORT - trigger an ABORT
2005 * SCTP_IERROR_NOMEM - out of memory (abort)
2006 * SCTP_IERROR_ERROR - stop processing, trigger an ERROR
2007 * SCTP_IERROR_NO_ERROR - continue with the chunk
2009 static sctp_ierror_t
sctp_verify_param(const struct sctp_association
*asoc
,
2010 union sctp_params param
,
2012 struct sctp_chunk
*chunk
,
2013 struct sctp_chunk
**err_chunk
)
2015 struct sctp_hmac_algo_param
*hmacs
;
2016 int retval
= SCTP_IERROR_NO_ERROR
;
2017 __u16 n_elt
, id
= 0;
2020 /* FIXME - This routine is not looking at each parameter per the
2021 * chunk type, i.e., unrecognized parameters should be further
2022 * identified based on the chunk id.
2025 switch (param
.p
->type
) {
2026 case SCTP_PARAM_IPV4_ADDRESS
:
2027 case SCTP_PARAM_IPV6_ADDRESS
:
2028 case SCTP_PARAM_COOKIE_PRESERVATIVE
:
2029 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES
:
2030 case SCTP_PARAM_STATE_COOKIE
:
2031 case SCTP_PARAM_HEARTBEAT_INFO
:
2032 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS
:
2033 case SCTP_PARAM_ECN_CAPABLE
:
2034 case SCTP_PARAM_ADAPTATION_LAYER_IND
:
2037 case SCTP_PARAM_SUPPORTED_EXT
:
2038 if (!sctp_verify_ext_param(param
))
2039 return SCTP_IERROR_ABORT
;
2042 case SCTP_PARAM_SET_PRIMARY
:
2043 if (sctp_addip_enable
)
2047 case SCTP_PARAM_HOST_NAME_ADDRESS
:
2048 /* Tell the peer, we won't support this param. */
2049 sctp_process_hn_param(asoc
, param
, chunk
, err_chunk
);
2050 retval
= SCTP_IERROR_ABORT
;
2053 case SCTP_PARAM_FWD_TSN_SUPPORT
:
2054 if (sctp_prsctp_enable
)
2058 case SCTP_PARAM_RANDOM
:
2059 if (!sctp_auth_enable
)
2062 /* SCTP-AUTH: Secion 6.1
2063 * If the random number is not 32 byte long the association
2064 * MUST be aborted. The ABORT chunk SHOULD contain the error
2065 * cause 'Protocol Violation'.
2067 if (SCTP_AUTH_RANDOM_LENGTH
!=
2068 ntohs(param
.p
->length
) - sizeof(sctp_paramhdr_t
)) {
2069 sctp_process_inv_paramlength(asoc
, param
.p
,
2071 retval
= SCTP_IERROR_ABORT
;
2075 case SCTP_PARAM_CHUNKS
:
2076 if (!sctp_auth_enable
)
2079 /* SCTP-AUTH: Section 3.2
2080 * The CHUNKS parameter MUST be included once in the INIT or
2081 * INIT-ACK chunk if the sender wants to receive authenticated
2082 * chunks. Its maximum length is 260 bytes.
2084 if (260 < ntohs(param
.p
->length
)) {
2085 sctp_process_inv_paramlength(asoc
, param
.p
,
2087 retval
= SCTP_IERROR_ABORT
;
2091 case SCTP_PARAM_HMAC_ALGO
:
2092 if (!sctp_auth_enable
)
2095 hmacs
= (struct sctp_hmac_algo_param
*)param
.p
;
2096 n_elt
= (ntohs(param
.p
->length
) - sizeof(sctp_paramhdr_t
)) >> 1;
2098 /* SCTP-AUTH: Section 6.1
2099 * The HMAC algorithm based on SHA-1 MUST be supported and
2100 * included in the HMAC-ALGO parameter.
2102 for (i
= 0; i
< n_elt
; i
++) {
2103 id
= ntohs(hmacs
->hmac_ids
[i
]);
2105 if (id
== SCTP_AUTH_HMAC_ID_SHA1
)
2109 if (id
!= SCTP_AUTH_HMAC_ID_SHA1
) {
2110 sctp_process_inv_paramlength(asoc
, param
.p
, chunk
,
2112 retval
= SCTP_IERROR_ABORT
;
2117 SCTP_DEBUG_PRINTK("Unrecognized param: %d for chunk %d.\n",
2118 ntohs(param
.p
->type
), cid
);
2119 retval
= sctp_process_unk_param(asoc
, param
, chunk
, err_chunk
);
2125 /* Verify the INIT packet before we process it. */
2126 int sctp_verify_init(const struct sctp_association
*asoc
,
2128 sctp_init_chunk_t
*peer_init
,
2129 struct sctp_chunk
*chunk
,
2130 struct sctp_chunk
**errp
)
2132 union sctp_params param
;
2136 /* Verify stream values are non-zero. */
2137 if ((0 == peer_init
->init_hdr
.num_outbound_streams
) ||
2138 (0 == peer_init
->init_hdr
.num_inbound_streams
) ||
2139 (0 == peer_init
->init_hdr
.init_tag
) ||
2140 (SCTP_DEFAULT_MINWINDOW
> ntohl(peer_init
->init_hdr
.a_rwnd
))) {
2142 return sctp_process_inv_mandatory(asoc
, chunk
, errp
);
2145 /* Check for missing mandatory parameters. */
2146 sctp_walk_params(param
, peer_init
, init_hdr
.params
) {
2148 if (SCTP_PARAM_STATE_COOKIE
== param
.p
->type
)
2151 } /* for (loop through all parameters) */
2153 /* There is a possibility that a parameter length was bad and
2154 * in that case we would have stoped walking the parameters.
2155 * The current param.p would point at the bad one.
2156 * Current consensus on the mailing list is to generate a PROTOCOL
2157 * VIOLATION error. We build the ERROR chunk here and let the normal
2158 * error handling code build and send the packet.
2160 if (param
.v
!= (void*)chunk
->chunk_end
)
2161 return sctp_process_inv_paramlength(asoc
, param
.p
, chunk
, errp
);
2163 /* The only missing mandatory param possible today is
2164 * the state cookie for an INIT-ACK chunk.
2166 if ((SCTP_CID_INIT_ACK
== cid
) && !has_cookie
)
2167 return sctp_process_missing_param(asoc
, SCTP_PARAM_STATE_COOKIE
,
2170 /* Verify all the variable length parameters */
2171 sctp_walk_params(param
, peer_init
, init_hdr
.params
) {
2173 result
= sctp_verify_param(asoc
, param
, cid
, chunk
, errp
);
2175 case SCTP_IERROR_ABORT
:
2176 case SCTP_IERROR_NOMEM
:
2178 case SCTP_IERROR_ERROR
:
2180 case SCTP_IERROR_NO_ERROR
:
2185 } /* for (loop through all parameters) */
2190 /* Unpack the parameters in an INIT packet into an association.
2191 * Returns 0 on failure, else success.
2192 * FIXME: This is an association method.
2194 int sctp_process_init(struct sctp_association
*asoc
, sctp_cid_t cid
,
2195 const union sctp_addr
*peer_addr
,
2196 sctp_init_chunk_t
*peer_init
, gfp_t gfp
)
2198 union sctp_params param
;
2199 struct sctp_transport
*transport
;
2200 struct list_head
*pos
, *temp
;
2203 /* We must include the address that the INIT packet came from.
2204 * This is the only address that matters for an INIT packet.
2205 * When processing a COOKIE ECHO, we retrieve the from address
2206 * of the INIT from the cookie.
2209 /* This implementation defaults to making the first transport
2210 * added as the primary transport. The source address seems to
2211 * be a a better choice than any of the embedded addresses.
2214 if(!sctp_assoc_add_peer(asoc
, peer_addr
, gfp
, SCTP_ACTIVE
))
2218 /* Process the initialization parameters. */
2219 sctp_walk_params(param
, peer_init
, init_hdr
.params
) {
2221 if (!sctp_process_param(asoc
, param
, peer_addr
, gfp
))
2225 /* AUTH: After processing the parameters, make sure that we
2226 * have all the required info to potentially do authentications.
2228 if (asoc
->peer
.auth_capable
&& (!asoc
->peer
.peer_random
||
2229 !asoc
->peer
.peer_hmacs
))
2230 asoc
->peer
.auth_capable
= 0;
2232 /* In a non-backward compatible mode, if the peer claims
2233 * support for ADD-IP but not AUTH, the ADD-IP spec states
2234 * that we MUST ABORT the association. Section 6. The section
2235 * also give us an option to silently ignore the packet, which
2236 * is what we'll do here.
2238 if (!sctp_addip_noauth
&&
2239 (asoc
->peer
.asconf_capable
&& !asoc
->peer
.auth_capable
)) {
2240 asoc
->peer
.addip_disabled_mask
|= (SCTP_PARAM_ADD_IP
|
2242 SCTP_PARAM_SET_PRIMARY
);
2243 asoc
->peer
.asconf_capable
= 0;
2247 /* Walk list of transports, removing transports in the UNKNOWN state. */
2248 list_for_each_safe(pos
, temp
, &asoc
->peer
.transport_addr_list
) {
2249 transport
= list_entry(pos
, struct sctp_transport
, transports
);
2250 if (transport
->state
== SCTP_UNKNOWN
) {
2251 sctp_assoc_rm_peer(asoc
, transport
);
2255 /* The fixed INIT headers are always in network byte
2258 asoc
->peer
.i
.init_tag
=
2259 ntohl(peer_init
->init_hdr
.init_tag
);
2260 asoc
->peer
.i
.a_rwnd
=
2261 ntohl(peer_init
->init_hdr
.a_rwnd
);
2262 asoc
->peer
.i
.num_outbound_streams
=
2263 ntohs(peer_init
->init_hdr
.num_outbound_streams
);
2264 asoc
->peer
.i
.num_inbound_streams
=
2265 ntohs(peer_init
->init_hdr
.num_inbound_streams
);
2266 asoc
->peer
.i
.initial_tsn
=
2267 ntohl(peer_init
->init_hdr
.initial_tsn
);
2269 /* Apply the upper bounds for output streams based on peer's
2270 * number of inbound streams.
2272 if (asoc
->c
.sinit_num_ostreams
>
2273 ntohs(peer_init
->init_hdr
.num_inbound_streams
)) {
2274 asoc
->c
.sinit_num_ostreams
=
2275 ntohs(peer_init
->init_hdr
.num_inbound_streams
);
2278 if (asoc
->c
.sinit_max_instreams
>
2279 ntohs(peer_init
->init_hdr
.num_outbound_streams
)) {
2280 asoc
->c
.sinit_max_instreams
=
2281 ntohs(peer_init
->init_hdr
.num_outbound_streams
);
2284 /* Copy Initiation tag from INIT to VT_peer in cookie. */
2285 asoc
->c
.peer_vtag
= asoc
->peer
.i
.init_tag
;
2287 /* Peer Rwnd : Current calculated value of the peer's rwnd. */
2288 asoc
->peer
.rwnd
= asoc
->peer
.i
.a_rwnd
;
2290 /* Copy cookie in case we need to resend COOKIE-ECHO. */
2291 cookie
= asoc
->peer
.cookie
;
2293 asoc
->peer
.cookie
= kmemdup(cookie
, asoc
->peer
.cookie_len
, gfp
);
2294 if (!asoc
->peer
.cookie
)
2298 /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
2299 * high (for example, implementations MAY use the size of the receiver
2300 * advertised window).
2302 list_for_each_entry(transport
, &asoc
->peer
.transport_addr_list
,
2304 transport
->ssthresh
= asoc
->peer
.i
.a_rwnd
;
2307 /* Set up the TSN tracking pieces. */
2308 if (!sctp_tsnmap_init(&asoc
->peer
.tsn_map
, SCTP_TSN_MAP_INITIAL
,
2309 asoc
->peer
.i
.initial_tsn
, gfp
))
2312 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
2314 * The stream sequence number in all the streams shall start
2315 * from 0 when the association is established. Also, when the
2316 * stream sequence number reaches the value 65535 the next
2317 * stream sequence number shall be set to 0.
2320 /* Allocate storage for the negotiated streams if it is not a temporary
2326 asoc
->ssnmap
= sctp_ssnmap_new(asoc
->c
.sinit_max_instreams
,
2327 asoc
->c
.sinit_num_ostreams
, gfp
);
2331 error
= sctp_assoc_set_id(asoc
, gfp
);
2336 /* ADDIP Section 4.1 ASCONF Chunk Procedures
2338 * When an endpoint has an ASCONF signaled change to be sent to the
2339 * remote endpoint it should do the following:
2341 * A2) A serial number should be assigned to the Chunk. The serial
2342 * number should be a monotonically increasing number. All serial
2343 * numbers are defined to be initialized at the start of the
2344 * association to the same value as the Initial TSN.
2346 asoc
->peer
.addip_serial
= asoc
->peer
.i
.initial_tsn
- 1;
2350 /* Release the transport structures. */
2351 list_for_each_safe(pos
, temp
, &asoc
->peer
.transport_addr_list
) {
2352 transport
= list_entry(pos
, struct sctp_transport
, transports
);
2353 if (transport
->state
!= SCTP_ACTIVE
)
2354 sctp_assoc_rm_peer(asoc
, transport
);
2362 /* Update asoc with the option described in param.
2364 * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
2366 * asoc is the association to update.
2367 * param is the variable length parameter to use for update.
2368 * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
2369 * If the current packet is an INIT we want to minimize the amount of
2370 * work we do. In particular, we should not build transport
2371 * structures for the addresses.
2373 static int sctp_process_param(struct sctp_association
*asoc
,
2374 union sctp_params param
,
2375 const union sctp_addr
*peer_addr
,
2378 union sctp_addr addr
;
2385 union sctp_addr_param
*addr_param
;
2386 struct sctp_transport
*t
;
2388 /* We maintain all INIT parameters in network byte order all the
2389 * time. This allows us to not worry about whether the parameters
2390 * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
2392 switch (param
.p
->type
) {
2393 case SCTP_PARAM_IPV6_ADDRESS
:
2394 if (PF_INET6
!= asoc
->base
.sk
->sk_family
)
2398 case SCTP_PARAM_IPV4_ADDRESS
:
2399 /* v4 addresses are not allowed on v6-only socket */
2400 if (ipv6_only_sock(asoc
->base
.sk
))
2403 af
= sctp_get_af_specific(param_type2af(param
.p
->type
));
2404 af
->from_addr_param(&addr
, param
.addr
, htons(asoc
->peer
.port
), 0);
2405 scope
= sctp_scope(peer_addr
);
2406 if (sctp_in_scope(&addr
, scope
))
2407 if (!sctp_assoc_add_peer(asoc
, &addr
, gfp
, SCTP_UNCONFIRMED
))
2411 case SCTP_PARAM_COOKIE_PRESERVATIVE
:
2412 if (!sctp_cookie_preserve_enable
)
2415 stale
= ntohl(param
.life
->lifespan_increment
);
2417 /* Suggested Cookie Life span increment's unit is msec,
2420 asoc
->cookie_life
.tv_sec
+= stale
/ 1000;
2421 asoc
->cookie_life
.tv_usec
+= (stale
% 1000) * 1000;
2424 case SCTP_PARAM_HOST_NAME_ADDRESS
:
2425 SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
2428 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES
:
2429 /* Turn off the default values first so we'll know which
2430 * ones are really set by the peer.
2432 asoc
->peer
.ipv4_address
= 0;
2433 asoc
->peer
.ipv6_address
= 0;
2435 /* Assume that peer supports the address family
2436 * by which it sends a packet.
2438 if (peer_addr
->sa
.sa_family
== AF_INET6
)
2439 asoc
->peer
.ipv6_address
= 1;
2440 else if (peer_addr
->sa
.sa_family
== AF_INET
)
2441 asoc
->peer
.ipv4_address
= 1;
2443 /* Cycle through address types; avoid divide by 0. */
2444 sat
= ntohs(param
.p
->length
) - sizeof(sctp_paramhdr_t
);
2446 sat
/= sizeof(__u16
);
2448 for (i
= 0; i
< sat
; ++i
) {
2449 switch (param
.sat
->types
[i
]) {
2450 case SCTP_PARAM_IPV4_ADDRESS
:
2451 asoc
->peer
.ipv4_address
= 1;
2454 case SCTP_PARAM_IPV6_ADDRESS
:
2455 if (PF_INET6
== asoc
->base
.sk
->sk_family
)
2456 asoc
->peer
.ipv6_address
= 1;
2459 case SCTP_PARAM_HOST_NAME_ADDRESS
:
2460 asoc
->peer
.hostname_address
= 1;
2463 default: /* Just ignore anything else. */
2469 case SCTP_PARAM_STATE_COOKIE
:
2470 asoc
->peer
.cookie_len
=
2471 ntohs(param
.p
->length
) - sizeof(sctp_paramhdr_t
);
2472 asoc
->peer
.cookie
= param
.cookie
->body
;
2475 case SCTP_PARAM_HEARTBEAT_INFO
:
2476 /* Would be odd to receive, but it causes no problems. */
2479 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS
:
2480 /* Rejected during verify stage. */
2483 case SCTP_PARAM_ECN_CAPABLE
:
2484 asoc
->peer
.ecn_capable
= 1;
2487 case SCTP_PARAM_ADAPTATION_LAYER_IND
:
2488 asoc
->peer
.adaptation_ind
= ntohl(param
.aind
->adaptation_ind
);
2491 case SCTP_PARAM_SET_PRIMARY
:
2492 if (!sctp_addip_enable
)
2495 addr_param
= param
.v
+ sizeof(sctp_addip_param_t
);
2497 af
= sctp_get_af_specific(param_type2af(param
.p
->type
));
2498 af
->from_addr_param(&addr
, addr_param
,
2499 htons(asoc
->peer
.port
), 0);
2501 /* if the address is invalid, we can't process it.
2502 * XXX: see spec for what to do.
2504 if (!af
->addr_valid(&addr
, NULL
, NULL
))
2507 t
= sctp_assoc_lookup_paddr(asoc
, &addr
);
2511 sctp_assoc_set_primary(asoc
, t
);
2514 case SCTP_PARAM_SUPPORTED_EXT
:
2515 sctp_process_ext_param(asoc
, param
);
2518 case SCTP_PARAM_FWD_TSN_SUPPORT
:
2519 if (sctp_prsctp_enable
) {
2520 asoc
->peer
.prsctp_capable
= 1;
2526 case SCTP_PARAM_RANDOM
:
2527 if (!sctp_auth_enable
)
2530 /* Save peer's random parameter */
2531 asoc
->peer
.peer_random
= kmemdup(param
.p
,
2532 ntohs(param
.p
->length
), gfp
);
2533 if (!asoc
->peer
.peer_random
) {
2539 case SCTP_PARAM_HMAC_ALGO
:
2540 if (!sctp_auth_enable
)
2543 /* Save peer's HMAC list */
2544 asoc
->peer
.peer_hmacs
= kmemdup(param
.p
,
2545 ntohs(param
.p
->length
), gfp
);
2546 if (!asoc
->peer
.peer_hmacs
) {
2551 /* Set the default HMAC the peer requested*/
2552 sctp_auth_asoc_set_default_hmac(asoc
, param
.hmac_algo
);
2555 case SCTP_PARAM_CHUNKS
:
2556 if (!sctp_auth_enable
)
2559 asoc
->peer
.peer_chunks
= kmemdup(param
.p
,
2560 ntohs(param
.p
->length
), gfp
);
2561 if (!asoc
->peer
.peer_chunks
)
2566 /* Any unrecognized parameters should have been caught
2567 * and handled by sctp_verify_param() which should be
2568 * called prior to this routine. Simply log the error
2571 SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
2572 ntohs(param
.p
->type
), asoc
);
2579 /* Select a new verification tag. */
2580 __u32
sctp_generate_tag(const struct sctp_endpoint
*ep
)
2582 /* I believe that this random number generator complies with RFC1750.
2583 * A tag of 0 is reserved for special cases (e.g. INIT).
2588 get_random_bytes(&x
, sizeof(__u32
));
2594 /* Select an initial TSN to send during startup. */
2595 __u32
sctp_generate_tsn(const struct sctp_endpoint
*ep
)
2599 get_random_bytes(&retval
, sizeof(__u32
));
2604 * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
2606 * 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
2607 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2608 * | Type = 0xC1 | Chunk Flags | Chunk Length |
2609 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2611 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2612 * | Address Parameter |
2613 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2614 * | ASCONF Parameter #1 |
2615 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2619 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2620 * | ASCONF Parameter #N |
2621 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2623 * Address Parameter and other parameter will not be wrapped in this function
2625 static struct sctp_chunk
*sctp_make_asconf(struct sctp_association
*asoc
,
2626 union sctp_addr
*addr
,
2629 sctp_addiphdr_t asconf
;
2630 struct sctp_chunk
*retval
;
2631 int length
= sizeof(asconf
) + vparam_len
;
2632 union sctp_addr_param addrparam
;
2634 struct sctp_af
*af
= sctp_get_af_specific(addr
->v4
.sin_family
);
2636 addrlen
= af
->to_addr_param(addr
, &addrparam
);
2641 /* Create the chunk. */
2642 retval
= sctp_make_chunk(asoc
, SCTP_CID_ASCONF
, 0, length
);
2646 asconf
.serial
= htonl(asoc
->addip_serial
++);
2648 retval
->subh
.addip_hdr
=
2649 sctp_addto_chunk(retval
, sizeof(asconf
), &asconf
);
2650 retval
->param_hdr
.v
=
2651 sctp_addto_chunk(retval
, addrlen
, &addrparam
);
2657 * 3.2.1 Add IP Address
2659 * 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
2660 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2661 * | Type = 0xC001 | Length = Variable |
2662 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2663 * | ASCONF-Request Correlation ID |
2664 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2665 * | Address Parameter |
2666 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2668 * 3.2.2 Delete IP Address
2670 * 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
2671 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2672 * | Type = 0xC002 | Length = Variable |
2673 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2674 * | ASCONF-Request Correlation ID |
2675 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2676 * | Address Parameter |
2677 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2680 struct sctp_chunk
*sctp_make_asconf_update_ip(struct sctp_association
*asoc
,
2681 union sctp_addr
*laddr
,
2682 struct sockaddr
*addrs
,
2686 sctp_addip_param_t param
;
2687 struct sctp_chunk
*retval
;
2688 union sctp_addr_param addr_param
;
2689 union sctp_addr
*addr
;
2692 int paramlen
= sizeof(param
);
2693 int addr_param_len
= 0;
2697 /* Get total length of all the address parameters. */
2699 for (i
= 0; i
< addrcnt
; i
++) {
2700 addr
= (union sctp_addr
*)addr_buf
;
2701 af
= sctp_get_af_specific(addr
->v4
.sin_family
);
2702 addr_param_len
= af
->to_addr_param(addr
, &addr_param
);
2704 totallen
+= paramlen
;
2705 totallen
+= addr_param_len
;
2707 addr_buf
+= af
->sockaddr_len
;
2710 /* Create an asconf chunk with the required length. */
2711 retval
= sctp_make_asconf(asoc
, laddr
, totallen
);
2715 /* Add the address parameters to the asconf chunk. */
2717 for (i
= 0; i
< addrcnt
; i
++) {
2718 addr
= (union sctp_addr
*)addr_buf
;
2719 af
= sctp_get_af_specific(addr
->v4
.sin_family
);
2720 addr_param_len
= af
->to_addr_param(addr
, &addr_param
);
2721 param
.param_hdr
.type
= flags
;
2722 param
.param_hdr
.length
= htons(paramlen
+ addr_param_len
);
2725 sctp_addto_chunk(retval
, paramlen
, ¶m
);
2726 sctp_addto_chunk(retval
, addr_param_len
, &addr_param
);
2728 addr_buf
+= af
->sockaddr_len
;
2734 * 3.2.4 Set Primary IP Address
2736 * 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
2737 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2738 * | Type =0xC004 | Length = Variable |
2739 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2740 * | ASCONF-Request Correlation ID |
2741 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2742 * | Address Parameter |
2743 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2745 * Create an ASCONF chunk with Set Primary IP address parameter.
2747 struct sctp_chunk
*sctp_make_asconf_set_prim(struct sctp_association
*asoc
,
2748 union sctp_addr
*addr
)
2750 sctp_addip_param_t param
;
2751 struct sctp_chunk
*retval
;
2752 int len
= sizeof(param
);
2753 union sctp_addr_param addrparam
;
2755 struct sctp_af
*af
= sctp_get_af_specific(addr
->v4
.sin_family
);
2757 addrlen
= af
->to_addr_param(addr
, &addrparam
);
2762 /* Create the chunk and make asconf header. */
2763 retval
= sctp_make_asconf(asoc
, addr
, len
);
2767 param
.param_hdr
.type
= SCTP_PARAM_SET_PRIMARY
;
2768 param
.param_hdr
.length
= htons(len
);
2771 sctp_addto_chunk(retval
, sizeof(param
), ¶m
);
2772 sctp_addto_chunk(retval
, addrlen
, &addrparam
);
2777 /* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2779 * 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
2780 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2781 * | Type = 0x80 | Chunk Flags | Chunk Length |
2782 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2784 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2785 * | ASCONF Parameter Response#1 |
2786 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2790 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2791 * | ASCONF Parameter Response#N |
2792 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2794 * Create an ASCONF_ACK chunk with enough space for the parameter responses.
2796 static struct sctp_chunk
*sctp_make_asconf_ack(const struct sctp_association
*asoc
,
2797 __u32 serial
, int vparam_len
)
2799 sctp_addiphdr_t asconf
;
2800 struct sctp_chunk
*retval
;
2801 int length
= sizeof(asconf
) + vparam_len
;
2803 /* Create the chunk. */
2804 retval
= sctp_make_chunk(asoc
, SCTP_CID_ASCONF_ACK
, 0, length
);
2808 asconf
.serial
= htonl(serial
);
2810 retval
->subh
.addip_hdr
=
2811 sctp_addto_chunk(retval
, sizeof(asconf
), &asconf
);
2816 /* Add response parameters to an ASCONF_ACK chunk. */
2817 static void sctp_add_asconf_response(struct sctp_chunk
*chunk
, __be32 crr_id
,
2818 __be16 err_code
, sctp_addip_param_t
*asconf_param
)
2820 sctp_addip_param_t ack_param
;
2821 sctp_errhdr_t err_param
;
2822 int asconf_param_len
= 0;
2823 int err_param_len
= 0;
2824 __be16 response_type
;
2826 if (SCTP_ERROR_NO_ERROR
== err_code
) {
2827 response_type
= SCTP_PARAM_SUCCESS_REPORT
;
2829 response_type
= SCTP_PARAM_ERR_CAUSE
;
2830 err_param_len
= sizeof(err_param
);
2833 ntohs(asconf_param
->param_hdr
.length
);
2836 /* Add Success Indication or Error Cause Indication parameter. */
2837 ack_param
.param_hdr
.type
= response_type
;
2838 ack_param
.param_hdr
.length
= htons(sizeof(ack_param
) +
2841 ack_param
.crr_id
= crr_id
;
2842 sctp_addto_chunk(chunk
, sizeof(ack_param
), &ack_param
);
2844 if (SCTP_ERROR_NO_ERROR
== err_code
)
2847 /* Add Error Cause parameter. */
2848 err_param
.cause
= err_code
;
2849 err_param
.length
= htons(err_param_len
+ asconf_param_len
);
2850 sctp_addto_chunk(chunk
, err_param_len
, &err_param
);
2852 /* Add the failed TLV copied from ASCONF chunk. */
2854 sctp_addto_chunk(chunk
, asconf_param_len
, asconf_param
);
2857 /* Process a asconf parameter. */
2858 static __be16
sctp_process_asconf_param(struct sctp_association
*asoc
,
2859 struct sctp_chunk
*asconf
,
2860 sctp_addip_param_t
*asconf_param
)
2862 struct sctp_transport
*peer
;
2864 union sctp_addr addr
;
2865 union sctp_addr_param
*addr_param
;
2867 addr_param
= (union sctp_addr_param
*)
2868 ((void *)asconf_param
+ sizeof(sctp_addip_param_t
));
2870 if (asconf_param
->param_hdr
.type
!= SCTP_PARAM_ADD_IP
&&
2871 asconf_param
->param_hdr
.type
!= SCTP_PARAM_DEL_IP
&&
2872 asconf_param
->param_hdr
.type
!= SCTP_PARAM_SET_PRIMARY
)
2873 return SCTP_ERROR_UNKNOWN_PARAM
;
2875 switch (addr_param
->v4
.param_hdr
.type
) {
2876 case SCTP_PARAM_IPV6_ADDRESS
:
2877 if (!asoc
->peer
.ipv6_address
)
2878 return SCTP_ERROR_DNS_FAILED
;
2880 case SCTP_PARAM_IPV4_ADDRESS
:
2881 if (!asoc
->peer
.ipv4_address
)
2882 return SCTP_ERROR_DNS_FAILED
;
2885 return SCTP_ERROR_DNS_FAILED
;
2888 af
= sctp_get_af_specific(param_type2af(addr_param
->v4
.param_hdr
.type
));
2890 return SCTP_ERROR_DNS_FAILED
;
2892 af
->from_addr_param(&addr
, addr_param
, htons(asoc
->peer
.port
), 0);
2894 /* ADDIP 4.2.1 This parameter MUST NOT contain a broadcast
2895 * or multicast address.
2896 * (note: wildcard is permitted and requires special handling so
2897 * make sure we check for that)
2899 if (!af
->is_any(&addr
) && !af
->addr_valid(&addr
, NULL
, asconf
->skb
))
2900 return SCTP_ERROR_DNS_FAILED
;
2902 switch (asconf_param
->param_hdr
.type
) {
2903 case SCTP_PARAM_ADD_IP
:
2905 * If the address 0.0.0.0 or ::0 is provided, the source
2906 * address of the packet MUST be added.
2908 if (af
->is_any(&addr
))
2909 memcpy(&addr
, &asconf
->source
, sizeof(addr
));
2911 /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
2912 * request and does not have the local resources to add this
2913 * new address to the association, it MUST return an Error
2914 * Cause TLV set to the new error code 'Operation Refused
2915 * Due to Resource Shortage'.
2918 peer
= sctp_assoc_add_peer(asoc
, &addr
, GFP_ATOMIC
, SCTP_UNCONFIRMED
);
2920 return SCTP_ERROR_RSRC_LOW
;
2922 /* Start the heartbeat timer. */
2923 if (!mod_timer(&peer
->hb_timer
, sctp_transport_timeout(peer
)))
2924 sctp_transport_hold(peer
);
2926 case SCTP_PARAM_DEL_IP
:
2927 /* ADDIP 4.3 D7) If a request is received to delete the
2928 * last remaining IP address of a peer endpoint, the receiver
2929 * MUST send an Error Cause TLV with the error cause set to the
2930 * new error code 'Request to Delete Last Remaining IP Address'.
2932 if (asoc
->peer
.transport_count
== 1)
2933 return SCTP_ERROR_DEL_LAST_IP
;
2935 /* ADDIP 4.3 D8) If a request is received to delete an IP
2936 * address which is also the source address of the IP packet
2937 * which contained the ASCONF chunk, the receiver MUST reject
2938 * this request. To reject the request the receiver MUST send
2939 * an Error Cause TLV set to the new error code 'Request to
2940 * Delete Source IP Address'
2942 if (sctp_cmp_addr_exact(sctp_source(asconf
), &addr
))
2943 return SCTP_ERROR_DEL_SRC_IP
;
2946 * If the address 0.0.0.0 or ::0 is provided, all
2947 * addresses of the peer except the source address of the
2948 * packet MUST be deleted.
2950 if (af
->is_any(&addr
)) {
2951 sctp_assoc_set_primary(asoc
, asconf
->transport
);
2952 sctp_assoc_del_nonprimary_peers(asoc
,
2955 sctp_assoc_del_peer(asoc
, &addr
);
2957 case SCTP_PARAM_SET_PRIMARY
:
2958 /* ADDIP Section 4.2.4
2959 * If the address 0.0.0.0 or ::0 is provided, the receiver
2960 * MAY mark the source address of the packet as its
2963 if (af
->is_any(&addr
))
2964 memcpy(&addr
.v4
, sctp_source(asconf
), sizeof(addr
));
2966 peer
= sctp_assoc_lookup_paddr(asoc
, &addr
);
2968 return SCTP_ERROR_DNS_FAILED
;
2970 sctp_assoc_set_primary(asoc
, peer
);
2974 return SCTP_ERROR_NO_ERROR
;
2977 /* Verify the ASCONF packet before we process it. */
2978 int sctp_verify_asconf(const struct sctp_association
*asoc
,
2979 struct sctp_paramhdr
*param_hdr
, void *chunk_end
,
2980 struct sctp_paramhdr
**errp
) {
2981 sctp_addip_param_t
*asconf_param
;
2982 union sctp_params param
;
2985 param
.v
= (sctp_paramhdr_t
*) param_hdr
;
2986 while (param
.v
<= chunk_end
- sizeof(sctp_paramhdr_t
)) {
2987 length
= ntohs(param
.p
->length
);
2990 if (param
.v
> chunk_end
- length
||
2991 length
< sizeof(sctp_paramhdr_t
))
2994 switch (param
.p
->type
) {
2995 case SCTP_PARAM_ADD_IP
:
2996 case SCTP_PARAM_DEL_IP
:
2997 case SCTP_PARAM_SET_PRIMARY
:
2998 asconf_param
= (sctp_addip_param_t
*)param
.v
;
2999 plen
= ntohs(asconf_param
->param_hdr
.length
);
3000 if (plen
< sizeof(sctp_addip_param_t
) +
3001 sizeof(sctp_paramhdr_t
))
3004 case SCTP_PARAM_SUCCESS_REPORT
:
3005 case SCTP_PARAM_ADAPTATION_LAYER_IND
:
3006 if (length
!= sizeof(sctp_addip_param_t
))
3014 param
.v
+= WORD_ROUND(length
);
3017 if (param
.v
!= chunk_end
)
3023 /* Process an incoming ASCONF chunk with the next expected serial no. and
3024 * return an ASCONF_ACK chunk to be sent in response.
3026 struct sctp_chunk
*sctp_process_asconf(struct sctp_association
*asoc
,
3027 struct sctp_chunk
*asconf
)
3029 sctp_addiphdr_t
*hdr
;
3030 union sctp_addr_param
*addr_param
;
3031 sctp_addip_param_t
*asconf_param
;
3032 struct sctp_chunk
*asconf_ack
;
3038 int all_param_pass
= 1;
3040 chunk_len
= ntohs(asconf
->chunk_hdr
->length
) - sizeof(sctp_chunkhdr_t
);
3041 hdr
= (sctp_addiphdr_t
*)asconf
->skb
->data
;
3042 serial
= ntohl(hdr
->serial
);
3044 /* Skip the addiphdr and store a pointer to address parameter. */
3045 length
= sizeof(sctp_addiphdr_t
);
3046 addr_param
= (union sctp_addr_param
*)(asconf
->skb
->data
+ length
);
3047 chunk_len
-= length
;
3049 /* Skip the address parameter and store a pointer to the first
3052 length
= ntohs(addr_param
->v4
.param_hdr
.length
);
3053 asconf_param
= (sctp_addip_param_t
*)((void *)addr_param
+ length
);
3054 chunk_len
-= length
;
3056 /* create an ASCONF_ACK chunk.
3057 * Based on the definitions of parameters, we know that the size of
3058 * ASCONF_ACK parameters are less than or equal to the twice of ASCONF
3061 asconf_ack
= sctp_make_asconf_ack(asoc
, serial
, chunk_len
* 2);
3065 /* Process the TLVs contained within the ASCONF chunk. */
3066 while (chunk_len
> 0) {
3067 err_code
= sctp_process_asconf_param(asoc
, asconf
,
3070 * If an error response is received for a TLV parameter,
3071 * all TLVs with no response before the failed TLV are
3072 * considered successful if not reported. All TLVs after
3073 * the failed response are considered unsuccessful unless
3074 * a specific success indication is present for the parameter.
3076 if (SCTP_ERROR_NO_ERROR
!= err_code
)
3079 if (!all_param_pass
)
3080 sctp_add_asconf_response(asconf_ack
,
3081 asconf_param
->crr_id
, err_code
,
3084 /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
3085 * an IP address sends an 'Out of Resource' in its response, it
3086 * MUST also fail any subsequent add or delete requests bundled
3089 if (SCTP_ERROR_RSRC_LOW
== err_code
)
3092 /* Move to the next ASCONF param. */
3093 length
= ntohs(asconf_param
->param_hdr
.length
);
3094 asconf_param
= (sctp_addip_param_t
*)((void *)asconf_param
+
3096 chunk_len
-= length
;
3100 asoc
->peer
.addip_serial
++;
3102 /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
3103 * after freeing the reference to old asconf ack if any.
3106 sctp_chunk_hold(asconf_ack
);
3107 list_add_tail(&asconf_ack
->transmitted_list
,
3108 &asoc
->asconf_ack_list
);
3114 /* Process a asconf parameter that is successfully acked. */
3115 static void sctp_asconf_param_success(struct sctp_association
*asoc
,
3116 sctp_addip_param_t
*asconf_param
)
3119 union sctp_addr addr
;
3120 struct sctp_bind_addr
*bp
= &asoc
->base
.bind_addr
;
3121 union sctp_addr_param
*addr_param
;
3122 struct sctp_transport
*transport
;
3123 struct sctp_sockaddr_entry
*saddr
;
3125 addr_param
= (union sctp_addr_param
*)
3126 ((void *)asconf_param
+ sizeof(sctp_addip_param_t
));
3128 /* We have checked the packet before, so we do not check again. */
3129 af
= sctp_get_af_specific(param_type2af(addr_param
->v4
.param_hdr
.type
));
3130 af
->from_addr_param(&addr
, addr_param
, htons(bp
->port
), 0);
3132 switch (asconf_param
->param_hdr
.type
) {
3133 case SCTP_PARAM_ADD_IP
:
3134 /* This is always done in BH context with a socket lock
3135 * held, so the list can not change.
3138 list_for_each_entry(saddr
, &bp
->address_list
, list
) {
3139 if (sctp_cmp_addr_exact(&saddr
->a
, &addr
))
3140 saddr
->state
= SCTP_ADDR_SRC
;
3143 list_for_each_entry(transport
, &asoc
->peer
.transport_addr_list
,
3145 if (transport
->state
== SCTP_ACTIVE
)
3147 dst_release(transport
->dst
);
3148 sctp_transport_route(transport
, NULL
,
3149 sctp_sk(asoc
->base
.sk
));
3152 case SCTP_PARAM_DEL_IP
:
3154 sctp_del_bind_addr(bp
, &addr
);
3156 list_for_each_entry(transport
, &asoc
->peer
.transport_addr_list
,
3158 dst_release(transport
->dst
);
3159 sctp_transport_route(transport
, NULL
,
3160 sctp_sk(asoc
->base
.sk
));
3168 /* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
3169 * for the given asconf parameter. If there is no response for this parameter,
3170 * return the error code based on the third argument 'no_err'.
3172 * A7) If an error response is received for a TLV parameter, all TLVs with no
3173 * response before the failed TLV are considered successful if not reported.
3174 * All TLVs after the failed response are considered unsuccessful unless a
3175 * specific success indication is present for the parameter.
3177 static __be16
sctp_get_asconf_response(struct sctp_chunk
*asconf_ack
,
3178 sctp_addip_param_t
*asconf_param
,
3181 sctp_addip_param_t
*asconf_ack_param
;
3182 sctp_errhdr_t
*err_param
;
3188 err_code
= SCTP_ERROR_NO_ERROR
;
3190 err_code
= SCTP_ERROR_REQ_REFUSED
;
3192 asconf_ack_len
= ntohs(asconf_ack
->chunk_hdr
->length
) -
3193 sizeof(sctp_chunkhdr_t
);
3195 /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
3196 * the first asconf_ack parameter.
3198 length
= sizeof(sctp_addiphdr_t
);
3199 asconf_ack_param
= (sctp_addip_param_t
*)(asconf_ack
->skb
->data
+
3201 asconf_ack_len
-= length
;
3203 while (asconf_ack_len
> 0) {
3204 if (asconf_ack_param
->crr_id
== asconf_param
->crr_id
) {
3205 switch(asconf_ack_param
->param_hdr
.type
) {
3206 case SCTP_PARAM_SUCCESS_REPORT
:
3207 return SCTP_ERROR_NO_ERROR
;
3208 case SCTP_PARAM_ERR_CAUSE
:
3209 length
= sizeof(sctp_addip_param_t
);
3210 err_param
= (sctp_errhdr_t
*)
3211 ((void *)asconf_ack_param
+ length
);
3212 asconf_ack_len
-= length
;
3213 if (asconf_ack_len
> 0)
3214 return err_param
->cause
;
3216 return SCTP_ERROR_INV_PARAM
;
3219 return SCTP_ERROR_INV_PARAM
;
3223 length
= ntohs(asconf_ack_param
->param_hdr
.length
);
3224 asconf_ack_param
= (sctp_addip_param_t
*)
3225 ((void *)asconf_ack_param
+ length
);
3226 asconf_ack_len
-= length
;
3232 /* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
3233 int sctp_process_asconf_ack(struct sctp_association
*asoc
,
3234 struct sctp_chunk
*asconf_ack
)
3236 struct sctp_chunk
*asconf
= asoc
->addip_last_asconf
;
3237 union sctp_addr_param
*addr_param
;
3238 sctp_addip_param_t
*asconf_param
;
3240 int asconf_len
= asconf
->skb
->len
;
3241 int all_param_pass
= 0;
3244 __be16 err_code
= SCTP_ERROR_NO_ERROR
;
3246 /* Skip the chunkhdr and addiphdr from the last asconf sent and store
3247 * a pointer to address parameter.
3249 length
= sizeof(sctp_addip_chunk_t
);
3250 addr_param
= (union sctp_addr_param
*)(asconf
->skb
->data
+ length
);
3251 asconf_len
-= length
;
3253 /* Skip the address parameter in the last asconf sent and store a
3254 * pointer to the first asconf parameter.
3256 length
= ntohs(addr_param
->v4
.param_hdr
.length
);
3257 asconf_param
= (sctp_addip_param_t
*)((void *)addr_param
+ length
);
3258 asconf_len
-= length
;
3261 * A8) If there is no response(s) to specific TLV parameter(s), and no
3262 * failures are indicated, then all request(s) are considered
3265 if (asconf_ack
->skb
->len
== sizeof(sctp_addiphdr_t
))
3268 /* Process the TLVs contained in the last sent ASCONF chunk. */
3269 while (asconf_len
> 0) {
3271 err_code
= SCTP_ERROR_NO_ERROR
;
3273 err_code
= sctp_get_asconf_response(asconf_ack
,
3276 if (no_err
&& (SCTP_ERROR_NO_ERROR
!= err_code
))
3281 case SCTP_ERROR_NO_ERROR
:
3282 sctp_asconf_param_success(asoc
, asconf_param
);
3285 case SCTP_ERROR_RSRC_LOW
:
3289 case SCTP_ERROR_UNKNOWN_PARAM
:
3290 /* Disable sending this type of asconf parameter in
3293 asoc
->peer
.addip_disabled_mask
|=
3294 asconf_param
->param_hdr
.type
;
3297 case SCTP_ERROR_REQ_REFUSED
:
3298 case SCTP_ERROR_DEL_LAST_IP
:
3299 case SCTP_ERROR_DEL_SRC_IP
:
3304 /* Skip the processed asconf parameter and move to the next
3307 length
= ntohs(asconf_param
->param_hdr
.length
);
3308 asconf_param
= (sctp_addip_param_t
*)((void *)asconf_param
+
3310 asconf_len
-= length
;
3313 /* Free the cached last sent asconf chunk. */
3314 list_del_init(&asconf
->transmitted_list
);
3315 sctp_chunk_free(asconf
);
3316 asoc
->addip_last_asconf
= NULL
;
3318 /* Send the next asconf chunk from the addip chunk queue. */
3319 if (!list_empty(&asoc
->addip_chunk_list
)) {
3320 struct list_head
*entry
= asoc
->addip_chunk_list
.next
;
3321 asconf
= list_entry(entry
, struct sctp_chunk
, list
);
3323 list_del_init(entry
);
3325 /* Hold the chunk until an ASCONF_ACK is received. */
3326 sctp_chunk_hold(asconf
);
3327 if (sctp_primitive_ASCONF(asoc
, asconf
))
3328 sctp_chunk_free(asconf
);
3330 asoc
->addip_last_asconf
= asconf
;
3336 /* Make a FWD TSN chunk. */
3337 struct sctp_chunk
*sctp_make_fwdtsn(const struct sctp_association
*asoc
,
3338 __u32 new_cum_tsn
, size_t nstreams
,
3339 struct sctp_fwdtsn_skip
*skiplist
)
3341 struct sctp_chunk
*retval
= NULL
;
3342 struct sctp_fwdtsn_chunk
*ftsn_chunk
;
3343 struct sctp_fwdtsn_hdr ftsn_hdr
;
3344 struct sctp_fwdtsn_skip skip
;
3348 hint
= (nstreams
+ 1) * sizeof(__u32
);
3350 retval
= sctp_make_chunk(asoc
, SCTP_CID_FWD_TSN
, 0, hint
);
3355 ftsn_chunk
= (struct sctp_fwdtsn_chunk
*)retval
->subh
.fwdtsn_hdr
;
3357 ftsn_hdr
.new_cum_tsn
= htonl(new_cum_tsn
);
3358 retval
->subh
.fwdtsn_hdr
=
3359 sctp_addto_chunk(retval
, sizeof(ftsn_hdr
), &ftsn_hdr
);
3361 for (i
= 0; i
< nstreams
; i
++) {
3362 skip
.stream
= skiplist
[i
].stream
;
3363 skip
.ssn
= skiplist
[i
].ssn
;
3364 sctp_addto_chunk(retval
, sizeof(skip
), &skip
);