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
;
212 chunksize
+= WORD_ROUND(SCTP_SAT_LEN(num_types
));
213 chunksize
+= sizeof(ecap_param
);
215 if (sctp_prsctp_enable
)
216 chunksize
+= sizeof(prsctp_param
);
218 /* ADDIP: Section 4.2.7:
219 * An implementation supporting this extension [ADDIP] MUST list
220 * the ASCONF,the ASCONF-ACK, and the AUTH chunks in its INIT and
221 * INIT-ACK parameters.
223 if (sctp_addip_enable
) {
224 extensions
[num_ext
] = SCTP_CID_ASCONF
;
225 extensions
[num_ext
+1] = SCTP_CID_ASCONF_ACK
;
229 if (sp
->adaptation_ind
)
230 chunksize
+= sizeof(aiparam
);
232 chunksize
+= vparam_len
;
234 /* Account for AUTH related parameters */
235 if (sctp_auth_enable
) {
236 /* Add random parameter length*/
237 chunksize
+= sizeof(asoc
->c
.auth_random
);
239 /* Add HMACS parameter length if any were defined */
240 auth_hmacs
= (sctp_paramhdr_t
*)asoc
->c
.auth_hmacs
;
241 if (auth_hmacs
->length
)
242 chunksize
+= WORD_ROUND(ntohs(auth_hmacs
->length
));
246 /* Add CHUNKS parameter length */
247 auth_chunks
= (sctp_paramhdr_t
*)asoc
->c
.auth_chunks
;
248 if (auth_chunks
->length
)
249 chunksize
+= WORD_ROUND(ntohs(auth_chunks
->length
));
253 extensions
[num_ext
] = SCTP_CID_AUTH
;
257 /* If we have any extensions to report, account for that */
259 chunksize
+= WORD_ROUND(sizeof(sctp_supported_ext_param_t
) +
262 /* RFC 2960 3.3.2 Initiation (INIT) (1)
264 * Note 3: An INIT chunk MUST NOT contain more than one Host
265 * Name address parameter. Moreover, the sender of the INIT
266 * MUST NOT combine any other address types with the Host Name
267 * address in the INIT. The receiver of INIT MUST ignore any
268 * other address types if the Host Name address parameter is
269 * present in the received INIT chunk.
271 * PLEASE DO NOT FIXME [This version does not support Host Name.]
274 retval
= sctp_make_chunk(asoc
, SCTP_CID_INIT
, 0, chunksize
);
278 retval
->subh
.init_hdr
=
279 sctp_addto_chunk(retval
, sizeof(init
), &init
);
280 retval
->param_hdr
.v
=
281 sctp_addto_chunk(retval
, addrs_len
, addrs
.v
);
283 /* RFC 2960 3.3.2 Initiation (INIT) (1)
285 * Note 4: This parameter, when present, specifies all the
286 * address types the sending endpoint can support. The absence
287 * of this parameter indicates that the sending endpoint can
288 * support any address type.
290 sat
.param_hdr
.type
= SCTP_PARAM_SUPPORTED_ADDRESS_TYPES
;
291 sat
.param_hdr
.length
= htons(SCTP_SAT_LEN(num_types
));
292 sctp_addto_chunk(retval
, sizeof(sat
), &sat
);
293 sctp_addto_chunk(retval
, num_types
* sizeof(__u16
), &types
);
295 sctp_addto_chunk(retval
, sizeof(ecap_param
), &ecap_param
);
297 /* Add the supported extensions parameter. Be nice and add this
298 * fist before addiding the parameters for the extensions themselves
301 ext_param
.param_hdr
.type
= SCTP_PARAM_SUPPORTED_EXT
;
302 ext_param
.param_hdr
.length
=
303 htons(sizeof(sctp_supported_ext_param_t
) + num_ext
);
304 sctp_addto_chunk(retval
, sizeof(sctp_supported_ext_param_t
),
306 sctp_addto_param(retval
, num_ext
, extensions
);
309 if (sctp_prsctp_enable
)
310 sctp_addto_chunk(retval
, sizeof(prsctp_param
), &prsctp_param
);
312 if (sp
->adaptation_ind
) {
313 aiparam
.param_hdr
.type
= SCTP_PARAM_ADAPTATION_LAYER_IND
;
314 aiparam
.param_hdr
.length
= htons(sizeof(aiparam
));
315 aiparam
.adaptation_ind
= htonl(sp
->adaptation_ind
);
316 sctp_addto_chunk(retval
, sizeof(aiparam
), &aiparam
);
319 /* Add SCTP-AUTH chunks to the parameter list */
320 if (sctp_auth_enable
) {
321 sctp_addto_chunk(retval
, sizeof(asoc
->c
.auth_random
),
322 asoc
->c
.auth_random
);
324 sctp_addto_chunk(retval
, ntohs(auth_hmacs
->length
),
327 sctp_addto_chunk(retval
, ntohs(auth_chunks
->length
),
335 struct sctp_chunk
*sctp_make_init_ack(const struct sctp_association
*asoc
,
336 const struct sctp_chunk
*chunk
,
337 gfp_t gfp
, int unkparam_len
)
339 sctp_inithdr_t initack
;
340 struct sctp_chunk
*retval
;
341 union sctp_params addrs
;
342 struct sctp_sock
*sp
;
344 sctp_cookie_param_t
*cookie
;
347 sctp_adaptation_ind_param_t aiparam
;
348 sctp_supported_ext_param_t ext_param
;
351 sctp_paramhdr_t
*auth_chunks
= NULL
,
357 /* Note: there may be no addresses to embed. */
358 addrs
= sctp_bind_addrs_to_raw(&asoc
->base
.bind_addr
, &addrs_len
, gfp
);
360 initack
.init_tag
= htonl(asoc
->c
.my_vtag
);
361 initack
.a_rwnd
= htonl(asoc
->rwnd
);
362 initack
.num_outbound_streams
= htons(asoc
->c
.sinit_num_ostreams
);
363 initack
.num_inbound_streams
= htons(asoc
->c
.sinit_max_instreams
);
364 initack
.initial_tsn
= htonl(asoc
->c
.initial_tsn
);
366 /* FIXME: We really ought to build the cookie right
367 * into the packet instead of allocating more fresh memory.
369 cookie
= sctp_pack_cookie(asoc
->ep
, asoc
, chunk
, &cookie_len
,
374 /* Calculate the total size of allocation, include the reserved
375 * space for reporting unknown parameters if it is specified.
377 sp
= sctp_sk(asoc
->base
.sk
);
378 chunksize
= sizeof(initack
) + addrs_len
+ cookie_len
+ unkparam_len
;
380 /* Tell peer that we'll do ECN only if peer advertised such cap. */
381 if (asoc
->peer
.ecn_capable
)
382 chunksize
+= sizeof(ecap_param
);
384 if (asoc
->peer
.prsctp_capable
)
385 chunksize
+= sizeof(prsctp_param
);
387 if (asoc
->peer
.asconf_capable
) {
388 extensions
[num_ext
] = SCTP_CID_ASCONF
;
389 extensions
[num_ext
+1] = SCTP_CID_ASCONF_ACK
;
393 if (sp
->adaptation_ind
)
394 chunksize
+= sizeof(aiparam
);
396 if (asoc
->peer
.auth_capable
) {
397 auth_random
= (sctp_paramhdr_t
*)asoc
->c
.auth_random
;
398 chunksize
+= ntohs(auth_random
->length
);
400 auth_hmacs
= (sctp_paramhdr_t
*)asoc
->c
.auth_hmacs
;
401 if (auth_hmacs
->length
)
402 chunksize
+= WORD_ROUND(ntohs(auth_hmacs
->length
));
406 auth_chunks
= (sctp_paramhdr_t
*)asoc
->c
.auth_chunks
;
407 if (auth_chunks
->length
)
408 chunksize
+= WORD_ROUND(ntohs(auth_chunks
->length
));
412 extensions
[num_ext
] = SCTP_CID_AUTH
;
417 chunksize
+= WORD_ROUND(sizeof(sctp_supported_ext_param_t
) +
420 /* Now allocate and fill out the chunk. */
421 retval
= sctp_make_chunk(asoc
, SCTP_CID_INIT_ACK
, 0, chunksize
);
425 /* Per the advice in RFC 2960 6.4, send this reply to
426 * the source of the INIT packet.
428 retval
->transport
= chunk
->transport
;
429 retval
->subh
.init_hdr
=
430 sctp_addto_chunk(retval
, sizeof(initack
), &initack
);
431 retval
->param_hdr
.v
= sctp_addto_chunk(retval
, addrs_len
, addrs
.v
);
432 sctp_addto_chunk(retval
, cookie_len
, cookie
);
433 if (asoc
->peer
.ecn_capable
)
434 sctp_addto_chunk(retval
, sizeof(ecap_param
), &ecap_param
);
436 ext_param
.param_hdr
.type
= SCTP_PARAM_SUPPORTED_EXT
;
437 ext_param
.param_hdr
.length
=
438 htons(sizeof(sctp_supported_ext_param_t
) + num_ext
);
439 sctp_addto_chunk(retval
, sizeof(sctp_supported_ext_param_t
),
441 sctp_addto_param(retval
, num_ext
, extensions
);
443 if (asoc
->peer
.prsctp_capable
)
444 sctp_addto_chunk(retval
, sizeof(prsctp_param
), &prsctp_param
);
446 if (sp
->adaptation_ind
) {
447 aiparam
.param_hdr
.type
= SCTP_PARAM_ADAPTATION_LAYER_IND
;
448 aiparam
.param_hdr
.length
= htons(sizeof(aiparam
));
449 aiparam
.adaptation_ind
= htonl(sp
->adaptation_ind
);
450 sctp_addto_chunk(retval
, sizeof(aiparam
), &aiparam
);
453 if (asoc
->peer
.auth_capable
) {
454 sctp_addto_chunk(retval
, ntohs(auth_random
->length
),
457 sctp_addto_chunk(retval
, ntohs(auth_hmacs
->length
),
460 sctp_addto_chunk(retval
, ntohs(auth_chunks
->length
),
464 /* We need to remove the const qualifier at this point. */
465 retval
->asoc
= (struct sctp_association
*) asoc
;
467 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
469 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
470 * HEARTBEAT ACK, * etc.) to the same destination transport
471 * address from which it received the DATA or control chunk
472 * to which it is replying.
474 * [INIT ACK back to where the INIT came from.]
477 retval
->transport
= chunk
->transport
;
486 /* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
488 * This chunk is used only during the initialization of an association.
489 * It is sent by the initiator of an association to its peer to complete
490 * the initialization process. This chunk MUST precede any DATA chunk
491 * sent within the association, but MAY be bundled with one or more DATA
492 * chunks in the same packet.
495 * 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
496 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
497 * | Type = 10 |Chunk Flags | Length |
498 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
501 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
505 * Set to zero on transmit and ignored on receipt.
507 * Length: 16 bits (unsigned integer)
509 * Set to the size of the chunk in bytes, including the 4 bytes of
510 * the chunk header and the size of the Cookie.
512 * Cookie: variable size
514 * This field must contain the exact cookie received in the
515 * State Cookie parameter from the previous INIT ACK.
517 * An implementation SHOULD make the cookie as small as possible
518 * to insure interoperability.
520 struct sctp_chunk
*sctp_make_cookie_echo(const struct sctp_association
*asoc
,
521 const struct sctp_chunk
*chunk
)
523 struct sctp_chunk
*retval
;
527 cookie
= asoc
->peer
.cookie
;
528 cookie_len
= asoc
->peer
.cookie_len
;
530 /* Build a cookie echo chunk. */
531 retval
= sctp_make_chunk(asoc
, SCTP_CID_COOKIE_ECHO
, 0, cookie_len
);
534 retval
->subh
.cookie_hdr
=
535 sctp_addto_chunk(retval
, cookie_len
, cookie
);
537 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
539 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
540 * HEARTBEAT ACK, * etc.) to the same destination transport
541 * address from which it * received the DATA or control chunk
542 * to which it is replying.
544 * [COOKIE ECHO back to where the INIT ACK came from.]
547 retval
->transport
= chunk
->transport
;
553 /* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
555 * This chunk is used only during the initialization of an
556 * association. It is used to acknowledge the receipt of a COOKIE
557 * ECHO chunk. This chunk MUST precede any DATA or SACK chunk sent
558 * within the association, but MAY be bundled with one or more DATA
559 * chunks or SACK chunk in the same SCTP packet.
562 * 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
563 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
564 * | Type = 11 |Chunk Flags | Length = 4 |
565 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
567 * Chunk Flags: 8 bits
569 * Set to zero on transmit and ignored on receipt.
571 struct sctp_chunk
*sctp_make_cookie_ack(const struct sctp_association
*asoc
,
572 const struct sctp_chunk
*chunk
)
574 struct sctp_chunk
*retval
;
576 retval
= sctp_make_chunk(asoc
, SCTP_CID_COOKIE_ACK
, 0, 0);
578 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
580 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
581 * HEARTBEAT ACK, * etc.) to the same destination transport
582 * address from which it * received the DATA or control chunk
583 * to which it is replying.
585 * [COOKIE ACK back to where the COOKIE ECHO came from.]
588 retval
->transport
= chunk
->transport
;
594 * Appendix A: Explicit Congestion Notification:
597 * RFC 2481 details a specific bit for a sender to send in the header of
598 * its next outbound TCP segment to indicate to its peer that it has
599 * reduced its congestion window. This is termed the CWR bit. For
600 * SCTP the same indication is made by including the CWR chunk.
601 * This chunk contains one data element, i.e. the TSN number that
602 * was sent in the ECNE chunk. This element represents the lowest
603 * TSN number in the datagram that was originally marked with the
607 * 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
608 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
609 * | Chunk Type=13 | Flags=00000000| Chunk Length = 8 |
610 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
611 * | Lowest TSN Number |
612 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
614 * Note: The CWR is considered a Control chunk.
616 struct sctp_chunk
*sctp_make_cwr(const struct sctp_association
*asoc
,
617 const __u32 lowest_tsn
,
618 const struct sctp_chunk
*chunk
)
620 struct sctp_chunk
*retval
;
623 cwr
.lowest_tsn
= htonl(lowest_tsn
);
624 retval
= sctp_make_chunk(asoc
, SCTP_CID_ECN_CWR
, 0,
625 sizeof(sctp_cwrhdr_t
));
630 retval
->subh
.ecn_cwr_hdr
=
631 sctp_addto_chunk(retval
, sizeof(cwr
), &cwr
);
633 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
635 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
636 * HEARTBEAT ACK, * etc.) to the same destination transport
637 * address from which it * received the DATA or control chunk
638 * to which it is replying.
640 * [Report a reduced congestion window back to where the ECNE
644 retval
->transport
= chunk
->transport
;
650 /* Make an ECNE chunk. This is a congestion experienced report. */
651 struct sctp_chunk
*sctp_make_ecne(const struct sctp_association
*asoc
,
652 const __u32 lowest_tsn
)
654 struct sctp_chunk
*retval
;
657 ecne
.lowest_tsn
= htonl(lowest_tsn
);
658 retval
= sctp_make_chunk(asoc
, SCTP_CID_ECN_ECNE
, 0,
659 sizeof(sctp_ecnehdr_t
));
662 retval
->subh
.ecne_hdr
=
663 sctp_addto_chunk(retval
, sizeof(ecne
), &ecne
);
669 /* Make a DATA chunk for the given association from the provided
670 * parameters. However, do not populate the data payload.
672 struct sctp_chunk
*sctp_make_datafrag_empty(struct sctp_association
*asoc
,
673 const struct sctp_sndrcvinfo
*sinfo
,
674 int data_len
, __u8 flags
, __u16 ssn
)
676 struct sctp_chunk
*retval
;
677 struct sctp_datahdr dp
;
680 /* We assign the TSN as LATE as possible, not here when
681 * creating the chunk.
684 dp
.stream
= htons(sinfo
->sinfo_stream
);
685 dp
.ppid
= sinfo
->sinfo_ppid
;
687 /* Set the flags for an unordered send. */
688 if (sinfo
->sinfo_flags
& SCTP_UNORDERED
) {
689 flags
|= SCTP_DATA_UNORDERED
;
694 chunk_len
= sizeof(dp
) + data_len
;
695 retval
= sctp_make_chunk(asoc
, SCTP_CID_DATA
, flags
, chunk_len
);
699 retval
->subh
.data_hdr
= sctp_addto_chunk(retval
, sizeof(dp
), &dp
);
700 memcpy(&retval
->sinfo
, sinfo
, sizeof(struct sctp_sndrcvinfo
));
706 /* Create a selective ackowledgement (SACK) for the given
707 * association. This reports on which TSN's we've seen to date,
708 * including duplicates and gaps.
710 struct sctp_chunk
*sctp_make_sack(const struct sctp_association
*asoc
)
712 struct sctp_chunk
*retval
;
713 struct sctp_sackhdr sack
;
716 __u16 num_gabs
, num_dup_tsns
;
717 struct sctp_tsnmap
*map
= (struct sctp_tsnmap
*)&asoc
->peer
.tsn_map
;
718 struct sctp_gap_ack_block gabs
[SCTP_MAX_GABS
];
720 memset(gabs
, 0, sizeof(gabs
));
721 ctsn
= sctp_tsnmap_get_ctsn(map
);
722 SCTP_DEBUG_PRINTK("sackCTSNAck sent: 0x%x.\n", ctsn
);
724 /* How much room is needed in the chunk? */
725 num_gabs
= sctp_tsnmap_num_gabs(map
, gabs
);
726 num_dup_tsns
= sctp_tsnmap_num_dups(map
);
728 /* Initialize the SACK header. */
729 sack
.cum_tsn_ack
= htonl(ctsn
);
730 sack
.a_rwnd
= htonl(asoc
->a_rwnd
);
731 sack
.num_gap_ack_blocks
= htons(num_gabs
);
732 sack
.num_dup_tsns
= htons(num_dup_tsns
);
735 + sizeof(struct sctp_gap_ack_block
) * num_gabs
736 + sizeof(__u32
) * num_dup_tsns
;
738 /* Create the chunk. */
739 retval
= sctp_make_chunk(asoc
, SCTP_CID_SACK
, 0, len
);
743 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
745 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
746 * HEARTBEAT ACK, etc.) to the same destination transport
747 * address from which it received the DATA or control chunk to
748 * which it is replying. This rule should also be followed if
749 * the endpoint is bundling DATA chunks together with the
752 * However, when acknowledging multiple DATA chunks received
753 * in packets from different source addresses in a single
754 * SACK, the SACK chunk may be transmitted to one of the
755 * destination transport addresses from which the DATA or
756 * control chunks being acknowledged were received.
758 * [BUG: We do not implement the following paragraph.
759 * Perhaps we should remember the last transport we used for a
760 * SACK and avoid that (if possible) if we have seen any
761 * duplicates. --piggy]
763 * When a receiver of a duplicate DATA chunk sends a SACK to a
764 * multi- homed endpoint it MAY be beneficial to vary the
765 * destination address and not use the source address of the
766 * DATA chunk. The reason being that receiving a duplicate
767 * from a multi-homed endpoint might indicate that the return
768 * path (as specified in the source address of the DATA chunk)
769 * for the SACK is broken.
771 * [Send to the address from which we last received a DATA chunk.]
773 retval
->transport
= asoc
->peer
.last_data_from
;
775 retval
->subh
.sack_hdr
=
776 sctp_addto_chunk(retval
, sizeof(sack
), &sack
);
778 /* Add the gap ack block information. */
780 sctp_addto_chunk(retval
, sizeof(__u32
) * num_gabs
,
783 /* Add the duplicate TSN information. */
785 sctp_addto_chunk(retval
, sizeof(__u32
) * num_dup_tsns
,
786 sctp_tsnmap_get_dups(map
));
792 /* Make a SHUTDOWN chunk. */
793 struct sctp_chunk
*sctp_make_shutdown(const struct sctp_association
*asoc
,
794 const struct sctp_chunk
*chunk
)
796 struct sctp_chunk
*retval
;
797 sctp_shutdownhdr_t shut
;
800 ctsn
= sctp_tsnmap_get_ctsn(&asoc
->peer
.tsn_map
);
801 shut
.cum_tsn_ack
= htonl(ctsn
);
803 retval
= sctp_make_chunk(asoc
, SCTP_CID_SHUTDOWN
, 0,
804 sizeof(sctp_shutdownhdr_t
));
808 retval
->subh
.shutdown_hdr
=
809 sctp_addto_chunk(retval
, sizeof(shut
), &shut
);
812 retval
->transport
= chunk
->transport
;
817 struct sctp_chunk
*sctp_make_shutdown_ack(const struct sctp_association
*asoc
,
818 const struct sctp_chunk
*chunk
)
820 struct sctp_chunk
*retval
;
822 retval
= sctp_make_chunk(asoc
, SCTP_CID_SHUTDOWN_ACK
, 0, 0);
824 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
826 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
827 * HEARTBEAT ACK, * etc.) to the same destination transport
828 * address from which it * received the DATA or control chunk
829 * to which it is replying.
831 * [ACK back to where the SHUTDOWN came from.]
834 retval
->transport
= chunk
->transport
;
839 struct sctp_chunk
*sctp_make_shutdown_complete(
840 const struct sctp_association
*asoc
,
841 const struct sctp_chunk
*chunk
)
843 struct sctp_chunk
*retval
;
846 /* Set the T-bit if we have no association (vtag will be
849 flags
|= asoc
? 0 : SCTP_CHUNK_FLAG_T
;
851 retval
= sctp_make_chunk(asoc
, SCTP_CID_SHUTDOWN_COMPLETE
, flags
, 0);
853 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
855 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
856 * HEARTBEAT ACK, * etc.) to the same destination transport
857 * address from which it * received the DATA or control chunk
858 * to which it is replying.
860 * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
864 retval
->transport
= chunk
->transport
;
869 /* Create an ABORT. Note that we set the T bit if we have no
870 * association, except when responding to an INIT (sctpimpguide 2.41).
872 struct sctp_chunk
*sctp_make_abort(const struct sctp_association
*asoc
,
873 const struct sctp_chunk
*chunk
,
876 struct sctp_chunk
*retval
;
879 /* Set the T-bit if we have no association and 'chunk' is not
880 * an INIT (vtag will be reflected).
883 if (chunk
&& chunk
->chunk_hdr
&&
884 chunk
->chunk_hdr
->type
== SCTP_CID_INIT
)
887 flags
= SCTP_CHUNK_FLAG_T
;
890 retval
= sctp_make_chunk(asoc
, SCTP_CID_ABORT
, flags
, hint
);
892 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
894 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
895 * HEARTBEAT ACK, * etc.) to the same destination transport
896 * address from which it * received the DATA or control chunk
897 * to which it is replying.
899 * [ABORT back to where the offender came from.]
902 retval
->transport
= chunk
->transport
;
907 /* Helper to create ABORT with a NO_USER_DATA error. */
908 struct sctp_chunk
*sctp_make_abort_no_data(
909 const struct sctp_association
*asoc
,
910 const struct sctp_chunk
*chunk
, __u32 tsn
)
912 struct sctp_chunk
*retval
;
915 retval
= sctp_make_abort(asoc
, chunk
, sizeof(sctp_errhdr_t
)
921 /* Put the tsn back into network byte order. */
922 payload
= htonl(tsn
);
923 sctp_init_cause(retval
, SCTP_ERROR_NO_DATA
, sizeof(payload
));
924 sctp_addto_chunk(retval
, sizeof(payload
), (const void *)&payload
);
926 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
928 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
929 * HEARTBEAT ACK, * etc.) to the same destination transport
930 * address from which it * received the DATA or control chunk
931 * to which it is replying.
933 * [ABORT back to where the offender came from.]
936 retval
->transport
= chunk
->transport
;
942 /* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error. */
943 struct sctp_chunk
*sctp_make_abort_user(const struct sctp_association
*asoc
,
944 const struct msghdr
*msg
,
947 struct sctp_chunk
*retval
;
948 void *payload
= NULL
;
951 retval
= sctp_make_abort(asoc
, NULL
, sizeof(sctp_errhdr_t
) + paylen
);
956 /* Put the msg_iov together into payload. */
957 payload
= kmalloc(paylen
, GFP_KERNEL
);
961 err
= memcpy_fromiovec(payload
, msg
->msg_iov
, paylen
);
966 sctp_init_cause(retval
, SCTP_ERROR_USER_ABORT
, paylen
);
967 sctp_addto_chunk(retval
, paylen
, payload
);
977 sctp_chunk_free(retval
);
983 /* Append bytes to the end of a parameter. Will panic if chunk is not big
986 static void *sctp_addto_param(struct sctp_chunk
*chunk
, int len
,
990 int chunklen
= ntohs(chunk
->chunk_hdr
->length
);
992 target
= skb_put(chunk
->skb
, len
);
995 memcpy(target
, data
, len
);
997 memset(target
, 0, len
);
999 /* Adjust the chunk length field. */
1000 chunk
->chunk_hdr
->length
= htons(chunklen
+ len
);
1001 chunk
->chunk_end
= skb_tail_pointer(chunk
->skb
);
1006 /* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
1007 struct sctp_chunk
*sctp_make_abort_violation(
1008 const struct sctp_association
*asoc
,
1009 const struct sctp_chunk
*chunk
,
1010 const __u8
*payload
,
1011 const size_t paylen
)
1013 struct sctp_chunk
*retval
;
1014 struct sctp_paramhdr phdr
;
1016 retval
= sctp_make_abort(asoc
, chunk
, sizeof(sctp_errhdr_t
) + paylen
1017 + sizeof(sctp_paramhdr_t
));
1021 sctp_init_cause(retval
, SCTP_ERROR_PROTO_VIOLATION
, paylen
1022 + sizeof(sctp_paramhdr_t
));
1024 phdr
.type
= htons(chunk
->chunk_hdr
->type
);
1025 phdr
.length
= chunk
->chunk_hdr
->length
;
1026 sctp_addto_chunk(retval
, paylen
, payload
);
1027 sctp_addto_param(retval
, sizeof(sctp_paramhdr_t
), &phdr
);
1033 struct sctp_chunk
*sctp_make_violation_paramlen(
1034 const struct sctp_association
*asoc
,
1035 const struct sctp_chunk
*chunk
,
1036 struct sctp_paramhdr
*param
)
1038 struct sctp_chunk
*retval
;
1039 static const char error
[] = "The following parameter had invalid length:";
1040 size_t payload_len
= sizeof(error
) + sizeof(sctp_errhdr_t
) +
1041 sizeof(sctp_paramhdr_t
);
1043 retval
= sctp_make_abort(asoc
, chunk
, payload_len
);
1047 sctp_init_cause(retval
, SCTP_ERROR_PROTO_VIOLATION
,
1048 sizeof(error
) + sizeof(sctp_paramhdr_t
));
1049 sctp_addto_chunk(retval
, sizeof(error
), error
);
1050 sctp_addto_param(retval
, sizeof(sctp_paramhdr_t
), param
);
1056 /* Make a HEARTBEAT chunk. */
1057 struct sctp_chunk
*sctp_make_heartbeat(const struct sctp_association
*asoc
,
1058 const struct sctp_transport
*transport
,
1059 const void *payload
, const size_t paylen
)
1061 struct sctp_chunk
*retval
= sctp_make_chunk(asoc
, SCTP_CID_HEARTBEAT
,
1067 /* Cast away the 'const', as this is just telling the chunk
1068 * what transport it belongs to.
1070 retval
->transport
= (struct sctp_transport
*) transport
;
1071 retval
->subh
.hbs_hdr
= sctp_addto_chunk(retval
, paylen
, payload
);
1077 struct sctp_chunk
*sctp_make_heartbeat_ack(const struct sctp_association
*asoc
,
1078 const struct sctp_chunk
*chunk
,
1079 const void *payload
, const size_t paylen
)
1081 struct sctp_chunk
*retval
;
1083 retval
= sctp_make_chunk(asoc
, SCTP_CID_HEARTBEAT_ACK
, 0, paylen
);
1087 retval
->subh
.hbs_hdr
= sctp_addto_chunk(retval
, paylen
, payload
);
1089 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1091 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1092 * HEARTBEAT ACK, * etc.) to the same destination transport
1093 * address from which it * received the DATA or control chunk
1094 * to which it is replying.
1096 * [HBACK back to where the HEARTBEAT came from.]
1099 retval
->transport
= chunk
->transport
;
1105 /* Create an Operation Error chunk with the specified space reserved.
1106 * This routine can be used for containing multiple causes in the chunk.
1108 static struct sctp_chunk
*sctp_make_op_error_space(
1109 const struct sctp_association
*asoc
,
1110 const struct sctp_chunk
*chunk
,
1113 struct sctp_chunk
*retval
;
1115 retval
= sctp_make_chunk(asoc
, SCTP_CID_ERROR
, 0,
1116 sizeof(sctp_errhdr_t
) + size
);
1120 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1122 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1123 * HEARTBEAT ACK, etc.) to the same destination transport
1124 * address from which it received the DATA or control chunk
1125 * to which it is replying.
1129 retval
->transport
= chunk
->transport
;
1135 /* Create an Operation Error chunk. */
1136 struct sctp_chunk
*sctp_make_op_error(const struct sctp_association
*asoc
,
1137 const struct sctp_chunk
*chunk
,
1138 __be16 cause_code
, const void *payload
,
1139 size_t paylen
, size_t reserve_tail
)
1141 struct sctp_chunk
*retval
;
1143 retval
= sctp_make_op_error_space(asoc
, chunk
, paylen
+ reserve_tail
);
1147 sctp_init_cause(retval
, cause_code
, paylen
+ reserve_tail
);
1148 sctp_addto_chunk(retval
, paylen
, payload
);
1150 sctp_addto_param(retval
, reserve_tail
, NULL
);
1156 struct sctp_chunk
*sctp_make_auth(const struct sctp_association
*asoc
)
1158 struct sctp_chunk
*retval
;
1159 struct sctp_hmac
*hmac_desc
;
1160 struct sctp_authhdr auth_hdr
;
1163 /* Get the first hmac that the peer told us to use */
1164 hmac_desc
= sctp_auth_asoc_get_hmac(asoc
);
1165 if (unlikely(!hmac_desc
))
1168 retval
= sctp_make_chunk(asoc
, SCTP_CID_AUTH
, 0,
1169 hmac_desc
->hmac_len
+ sizeof(sctp_authhdr_t
));
1173 auth_hdr
.hmac_id
= htons(hmac_desc
->hmac_id
);
1174 auth_hdr
.shkey_id
= htons(asoc
->active_key_id
);
1176 retval
->subh
.auth_hdr
= sctp_addto_chunk(retval
, sizeof(sctp_authhdr_t
),
1179 hmac
= skb_put(retval
->skb
, hmac_desc
->hmac_len
);
1180 memset(hmac
, 0, hmac_desc
->hmac_len
);
1182 /* Adjust the chunk header to include the empty MAC */
1183 retval
->chunk_hdr
->length
=
1184 htons(ntohs(retval
->chunk_hdr
->length
) + hmac_desc
->hmac_len
);
1185 retval
->chunk_end
= skb_tail_pointer(retval
->skb
);
1191 /********************************************************************
1192 * 2nd Level Abstractions
1193 ********************************************************************/
1195 /* Turn an skb into a chunk.
1196 * FIXME: Eventually move the structure directly inside the skb->cb[].
1198 struct sctp_chunk
*sctp_chunkify(struct sk_buff
*skb
,
1199 const struct sctp_association
*asoc
,
1202 struct sctp_chunk
*retval
;
1204 retval
= kmem_cache_zalloc(sctp_chunk_cachep
, GFP_ATOMIC
);
1210 SCTP_DEBUG_PRINTK("chunkifying skb %p w/o an sk\n", skb
);
1213 INIT_LIST_HEAD(&retval
->list
);
1215 retval
->asoc
= (struct sctp_association
*)asoc
;
1217 retval
->has_tsn
= 0;
1218 retval
->has_ssn
= 0;
1219 retval
->rtt_in_progress
= 0;
1220 retval
->sent_at
= 0;
1221 retval
->singleton
= 1;
1222 retval
->end_of_packet
= 0;
1223 retval
->ecn_ce_done
= 0;
1224 retval
->pdiscard
= 0;
1226 /* sctpimpguide-05.txt Section 2.8.2
1227 * M1) Each time a new DATA chunk is transmitted
1228 * set the 'TSN.Missing.Report' count for that TSN to 0. The
1229 * 'TSN.Missing.Report' count will be used to determine missing chunks
1230 * and when to fast retransmit.
1232 retval
->tsn_missing_report
= 0;
1233 retval
->tsn_gap_acked
= 0;
1234 retval
->fast_retransmit
= SCTP_CAN_FRTX
;
1236 /* If this is a fragmented message, track all fragments
1237 * of the message (for SEND_FAILED).
1241 /* Polish the bead hole. */
1242 INIT_LIST_HEAD(&retval
->transmitted_list
);
1243 INIT_LIST_HEAD(&retval
->frag_list
);
1244 SCTP_DBG_OBJCNT_INC(chunk
);
1245 atomic_set(&retval
->refcnt
, 1);
1251 /* Set chunk->source and dest based on the IP header in chunk->skb. */
1252 void sctp_init_addrs(struct sctp_chunk
*chunk
, union sctp_addr
*src
,
1253 union sctp_addr
*dest
)
1255 memcpy(&chunk
->source
, src
, sizeof(union sctp_addr
));
1256 memcpy(&chunk
->dest
, dest
, sizeof(union sctp_addr
));
1259 /* Extract the source address from a chunk. */
1260 const union sctp_addr
*sctp_source(const struct sctp_chunk
*chunk
)
1262 /* If we have a known transport, use that. */
1263 if (chunk
->transport
) {
1264 return &chunk
->transport
->ipaddr
;
1266 /* Otherwise, extract it from the IP header. */
1267 return &chunk
->source
;
1271 /* Create a new chunk, setting the type and flags headers from the
1272 * arguments, reserving enough space for a 'paylen' byte payload.
1275 struct sctp_chunk
*sctp_make_chunk(const struct sctp_association
*asoc
,
1276 __u8 type
, __u8 flags
, int paylen
)
1278 struct sctp_chunk
*retval
;
1279 sctp_chunkhdr_t
*chunk_hdr
;
1280 struct sk_buff
*skb
;
1283 /* No need to allocate LL here, as this is only a chunk. */
1284 skb
= alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t
) + paylen
),
1289 /* Make room for the chunk header. */
1290 chunk_hdr
= (sctp_chunkhdr_t
*)skb_put(skb
, sizeof(sctp_chunkhdr_t
));
1291 chunk_hdr
->type
= type
;
1292 chunk_hdr
->flags
= flags
;
1293 chunk_hdr
->length
= htons(sizeof(sctp_chunkhdr_t
));
1295 sk
= asoc
? asoc
->base
.sk
: NULL
;
1296 retval
= sctp_chunkify(skb
, asoc
, sk
);
1302 retval
->chunk_hdr
= chunk_hdr
;
1303 retval
->chunk_end
= ((__u8
*)chunk_hdr
) + sizeof(struct sctp_chunkhdr
);
1305 /* Determine if the chunk needs to be authenticated */
1306 if (sctp_auth_send_cid(type
, asoc
))
1309 /* Set the skb to the belonging sock for accounting. */
1318 /* Release the memory occupied by a chunk. */
1319 static void sctp_chunk_destroy(struct sctp_chunk
*chunk
)
1321 BUG_ON(!list_empty(&chunk
->list
));
1322 list_del_init(&chunk
->transmitted_list
);
1324 /* Free the chunk skb data and the SCTP_chunk stub itself. */
1325 dev_kfree_skb(chunk
->skb
);
1327 SCTP_DBG_OBJCNT_DEC(chunk
);
1328 kmem_cache_free(sctp_chunk_cachep
, chunk
);
1331 /* Possibly, free the chunk. */
1332 void sctp_chunk_free(struct sctp_chunk
*chunk
)
1334 /* Release our reference on the message tracker. */
1336 sctp_datamsg_put(chunk
->msg
);
1338 sctp_chunk_put(chunk
);
1341 /* Grab a reference to the chunk. */
1342 void sctp_chunk_hold(struct sctp_chunk
*ch
)
1344 atomic_inc(&ch
->refcnt
);
1347 /* Release a reference to the chunk. */
1348 void sctp_chunk_put(struct sctp_chunk
*ch
)
1350 if (atomic_dec_and_test(&ch
->refcnt
))
1351 sctp_chunk_destroy(ch
);
1354 /* Append bytes to the end of a chunk. Will panic if chunk is not big
1357 void *sctp_addto_chunk(struct sctp_chunk
*chunk
, int len
, const void *data
)
1361 int chunklen
= ntohs(chunk
->chunk_hdr
->length
);
1362 int padlen
= WORD_ROUND(chunklen
) - chunklen
;
1364 padding
= skb_put(chunk
->skb
, padlen
);
1365 target
= skb_put(chunk
->skb
, len
);
1367 memset(padding
, 0, padlen
);
1368 memcpy(target
, data
, len
);
1370 /* Adjust the chunk length field. */
1371 chunk
->chunk_hdr
->length
= htons(chunklen
+ padlen
+ len
);
1372 chunk
->chunk_end
= skb_tail_pointer(chunk
->skb
);
1377 /* Append bytes from user space to the end of a chunk. Will panic if
1378 * chunk is not big enough.
1379 * Returns a kernel err value.
1381 int sctp_user_addto_chunk(struct sctp_chunk
*chunk
, int off
, int len
,
1387 /* Make room in chunk for data. */
1388 target
= skb_put(chunk
->skb
, len
);
1390 /* Copy data (whole iovec) into chunk */
1391 if ((err
= memcpy_fromiovecend(target
, data
, off
, len
)))
1394 /* Adjust the chunk length field. */
1395 chunk
->chunk_hdr
->length
=
1396 htons(ntohs(chunk
->chunk_hdr
->length
) + len
);
1397 chunk
->chunk_end
= skb_tail_pointer(chunk
->skb
);
1403 /* Helper function to assign a TSN if needed. This assumes that both
1404 * the data_hdr and association have already been assigned.
1406 void sctp_chunk_assign_ssn(struct sctp_chunk
*chunk
)
1408 struct sctp_datamsg
*msg
;
1409 struct sctp_chunk
*lchunk
;
1410 struct sctp_stream
*stream
;
1417 /* All fragments will be on the same stream */
1418 sid
= ntohs(chunk
->subh
.data_hdr
->stream
);
1419 stream
= &chunk
->asoc
->ssnmap
->out
;
1421 /* Now assign the sequence number to the entire message.
1422 * All fragments must have the same stream sequence number.
1425 list_for_each_entry(lchunk
, &msg
->chunks
, frag_list
) {
1426 if (lchunk
->chunk_hdr
->flags
& SCTP_DATA_UNORDERED
) {
1429 if (lchunk
->chunk_hdr
->flags
& SCTP_DATA_LAST_FRAG
)
1430 ssn
= sctp_ssn_next(stream
, sid
);
1432 ssn
= sctp_ssn_peek(stream
, sid
);
1435 lchunk
->subh
.data_hdr
->ssn
= htons(ssn
);
1436 lchunk
->has_ssn
= 1;
1440 /* Helper function to assign a TSN if needed. This assumes that both
1441 * the data_hdr and association have already been assigned.
1443 void sctp_chunk_assign_tsn(struct sctp_chunk
*chunk
)
1445 if (!chunk
->has_tsn
) {
1446 /* This is the last possible instant to
1449 chunk
->subh
.data_hdr
->tsn
=
1450 htonl(sctp_association_get_next_tsn(chunk
->asoc
));
1455 /* Create a CLOSED association to use with an incoming packet. */
1456 struct sctp_association
*sctp_make_temp_asoc(const struct sctp_endpoint
*ep
,
1457 struct sctp_chunk
*chunk
,
1460 struct sctp_association
*asoc
;
1461 struct sk_buff
*skb
;
1465 /* Create the bare association. */
1466 scope
= sctp_scope(sctp_source(chunk
));
1467 asoc
= sctp_association_new(ep
, ep
->base
.sk
, scope
, gfp
);
1472 /* Create an entry for the source address of the packet. */
1473 af
= sctp_get_af_specific(ipver2af(ip_hdr(skb
)->version
));
1476 af
->from_skb(&asoc
->c
.peer_addr
, skb
, 1);
1481 sctp_association_free(asoc
);
1485 /* Build a cookie representing asoc.
1486 * This INCLUDES the param header needed to put the cookie in the INIT ACK.
1488 static sctp_cookie_param_t
*sctp_pack_cookie(const struct sctp_endpoint
*ep
,
1489 const struct sctp_association
*asoc
,
1490 const struct sctp_chunk
*init_chunk
,
1492 const __u8
*raw_addrs
, int addrs_len
)
1494 sctp_cookie_param_t
*retval
;
1495 struct sctp_signed_cookie
*cookie
;
1496 struct scatterlist sg
;
1497 int headersize
, bodysize
;
1498 unsigned int keylen
;
1501 /* Header size is static data prior to the actual cookie, including
1504 headersize
= sizeof(sctp_paramhdr_t
) +
1505 (sizeof(struct sctp_signed_cookie
) -
1506 sizeof(struct sctp_cookie
));
1507 bodysize
= sizeof(struct sctp_cookie
)
1508 + ntohs(init_chunk
->chunk_hdr
->length
) + addrs_len
;
1510 /* Pad out the cookie to a multiple to make the signature
1511 * functions simpler to write.
1513 if (bodysize
% SCTP_COOKIE_MULTIPLE
)
1514 bodysize
+= SCTP_COOKIE_MULTIPLE
1515 - (bodysize
% SCTP_COOKIE_MULTIPLE
);
1516 *cookie_len
= headersize
+ bodysize
;
1518 /* Clear this memory since we are sending this data structure
1519 * out on the network.
1521 retval
= kzalloc(*cookie_len
, GFP_ATOMIC
);
1525 cookie
= (struct sctp_signed_cookie
*) retval
->body
;
1527 /* Set up the parameter header. */
1528 retval
->p
.type
= SCTP_PARAM_STATE_COOKIE
;
1529 retval
->p
.length
= htons(*cookie_len
);
1531 /* Copy the cookie part of the association itself. */
1532 cookie
->c
= asoc
->c
;
1533 /* Save the raw address list length in the cookie. */
1534 cookie
->c
.raw_addr_list_len
= addrs_len
;
1536 /* Remember PR-SCTP capability. */
1537 cookie
->c
.prsctp_capable
= asoc
->peer
.prsctp_capable
;
1539 /* Save adaptation indication in the cookie. */
1540 cookie
->c
.adaptation_ind
= asoc
->peer
.adaptation_ind
;
1542 /* Set an expiration time for the cookie. */
1543 do_gettimeofday(&cookie
->c
.expiration
);
1544 TIMEVAL_ADD(asoc
->cookie_life
, cookie
->c
.expiration
);
1546 /* Copy the peer's init packet. */
1547 memcpy(&cookie
->c
.peer_init
[0], init_chunk
->chunk_hdr
,
1548 ntohs(init_chunk
->chunk_hdr
->length
));
1550 /* Copy the raw local address list of the association. */
1551 memcpy((__u8
*)&cookie
->c
.peer_init
[0] +
1552 ntohs(init_chunk
->chunk_hdr
->length
), raw_addrs
, addrs_len
);
1554 if (sctp_sk(ep
->base
.sk
)->hmac
) {
1555 struct hash_desc desc
;
1557 /* Sign the message. */
1558 sg_init_one(&sg
, &cookie
->c
, bodysize
);
1559 keylen
= SCTP_SECRET_SIZE
;
1560 key
= (char *)ep
->secret_key
[ep
->current_key
];
1561 desc
.tfm
= sctp_sk(ep
->base
.sk
)->hmac
;
1564 if (crypto_hash_setkey(desc
.tfm
, key
, keylen
) ||
1565 crypto_hash_digest(&desc
, &sg
, bodysize
, cookie
->signature
))
1578 /* Unpack the cookie from COOKIE ECHO chunk, recreating the association. */
1579 struct sctp_association
*sctp_unpack_cookie(
1580 const struct sctp_endpoint
*ep
,
1581 const struct sctp_association
*asoc
,
1582 struct sctp_chunk
*chunk
, gfp_t gfp
,
1583 int *error
, struct sctp_chunk
**errp
)
1585 struct sctp_association
*retval
= NULL
;
1586 struct sctp_signed_cookie
*cookie
;
1587 struct sctp_cookie
*bear_cookie
;
1588 int headersize
, bodysize
, fixed_size
;
1589 __u8
*digest
= ep
->digest
;
1590 struct scatterlist sg
;
1591 unsigned int keylen
, len
;
1594 struct sk_buff
*skb
= chunk
->skb
;
1596 struct hash_desc desc
;
1598 /* Header size is static data prior to the actual cookie, including
1601 headersize
= sizeof(sctp_chunkhdr_t
) +
1602 (sizeof(struct sctp_signed_cookie
) -
1603 sizeof(struct sctp_cookie
));
1604 bodysize
= ntohs(chunk
->chunk_hdr
->length
) - headersize
;
1605 fixed_size
= headersize
+ sizeof(struct sctp_cookie
);
1607 /* Verify that the chunk looks like it even has a cookie.
1608 * There must be enough room for our cookie and our peer's
1611 len
= ntohs(chunk
->chunk_hdr
->length
);
1612 if (len
< fixed_size
+ sizeof(struct sctp_chunkhdr
))
1615 /* Verify that the cookie has been padded out. */
1616 if (bodysize
% SCTP_COOKIE_MULTIPLE
)
1619 /* Process the cookie. */
1620 cookie
= chunk
->subh
.cookie_hdr
;
1621 bear_cookie
= &cookie
->c
;
1623 if (!sctp_sk(ep
->base
.sk
)->hmac
)
1626 /* Check the signature. */
1627 keylen
= SCTP_SECRET_SIZE
;
1628 sg_init_one(&sg
, bear_cookie
, bodysize
);
1629 key
= (char *)ep
->secret_key
[ep
->current_key
];
1630 desc
.tfm
= sctp_sk(ep
->base
.sk
)->hmac
;
1633 memset(digest
, 0x00, SCTP_SIGNATURE_SIZE
);
1634 if (crypto_hash_setkey(desc
.tfm
, key
, keylen
) ||
1635 crypto_hash_digest(&desc
, &sg
, bodysize
, digest
)) {
1636 *error
= -SCTP_IERROR_NOMEM
;
1640 if (memcmp(digest
, cookie
->signature
, SCTP_SIGNATURE_SIZE
)) {
1641 /* Try the previous key. */
1642 key
= (char *)ep
->secret_key
[ep
->last_key
];
1643 memset(digest
, 0x00, SCTP_SIGNATURE_SIZE
);
1644 if (crypto_hash_setkey(desc
.tfm
, key
, keylen
) ||
1645 crypto_hash_digest(&desc
, &sg
, bodysize
, digest
)) {
1646 *error
= -SCTP_IERROR_NOMEM
;
1650 if (memcmp(digest
, cookie
->signature
, SCTP_SIGNATURE_SIZE
)) {
1651 /* Yikes! Still bad signature! */
1652 *error
= -SCTP_IERROR_BAD_SIG
;
1658 /* IG Section 2.35.2:
1659 * 3) Compare the port numbers and the verification tag contained
1660 * within the COOKIE ECHO chunk to the actual port numbers and the
1661 * verification tag within the SCTP common header of the received
1662 * packet. If these values do not match the packet MUST be silently
1665 if (ntohl(chunk
->sctp_hdr
->vtag
) != bear_cookie
->my_vtag
) {
1666 *error
= -SCTP_IERROR_BAD_TAG
;
1670 if (chunk
->sctp_hdr
->source
!= bear_cookie
->peer_addr
.v4
.sin_port
||
1671 ntohs(chunk
->sctp_hdr
->dest
) != bear_cookie
->my_port
) {
1672 *error
= -SCTP_IERROR_BAD_PORTS
;
1676 /* Check to see if the cookie is stale. If there is already
1677 * an association, there is no need to check cookie's expiration
1678 * for init collision case of lost COOKIE ACK.
1679 * If skb has been timestamped, then use the stamp, otherwise
1680 * use current time. This introduces a small possibility that
1681 * that a cookie may be considered expired, but his would only slow
1682 * down the new association establishment instead of every packet.
1684 if (sock_flag(ep
->base
.sk
, SOCK_TIMESTAMP
))
1685 skb_get_timestamp(skb
, &tv
);
1687 do_gettimeofday(&tv
);
1689 if (!asoc
&& tv_lt(bear_cookie
->expiration
, tv
)) {
1691 * Section 3.3.10.3 Stale Cookie Error (3)
1695 * Stale Cookie Error: Indicates the receipt of a valid State
1696 * Cookie that has expired.
1698 len
= ntohs(chunk
->chunk_hdr
->length
);
1699 *errp
= sctp_make_op_error_space(asoc
, chunk
, len
);
1701 suseconds_t usecs
= (tv
.tv_sec
-
1702 bear_cookie
->expiration
.tv_sec
) * 1000000L +
1703 tv
.tv_usec
- bear_cookie
->expiration
.tv_usec
;
1704 __be32 n
= htonl(usecs
);
1706 sctp_init_cause(*errp
, SCTP_ERROR_STALE_COOKIE
,
1708 sctp_addto_chunk(*errp
, sizeof(n
), &n
);
1709 *error
= -SCTP_IERROR_STALE_COOKIE
;
1711 *error
= -SCTP_IERROR_NOMEM
;
1716 /* Make a new base association. */
1717 scope
= sctp_scope(sctp_source(chunk
));
1718 retval
= sctp_association_new(ep
, ep
->base
.sk
, scope
, gfp
);
1720 *error
= -SCTP_IERROR_NOMEM
;
1724 /* Set up our peer's port number. */
1725 retval
->peer
.port
= ntohs(chunk
->sctp_hdr
->source
);
1727 /* Populate the association from the cookie. */
1728 memcpy(&retval
->c
, bear_cookie
, sizeof(*bear_cookie
));
1730 if (sctp_assoc_set_bind_addr_from_cookie(retval
, bear_cookie
,
1732 *error
= -SCTP_IERROR_NOMEM
;
1736 /* Also, add the destination address. */
1737 if (list_empty(&retval
->base
.bind_addr
.address_list
)) {
1738 sctp_add_bind_addr(&retval
->base
.bind_addr
, &chunk
->dest
,
1739 SCTP_ADDR_SRC
, GFP_ATOMIC
);
1742 retval
->next_tsn
= retval
->c
.initial_tsn
;
1743 retval
->ctsn_ack_point
= retval
->next_tsn
- 1;
1744 retval
->addip_serial
= retval
->c
.initial_tsn
;
1745 retval
->adv_peer_ack_point
= retval
->ctsn_ack_point
;
1746 retval
->peer
.prsctp_capable
= retval
->c
.prsctp_capable
;
1747 retval
->peer
.adaptation_ind
= retval
->c
.adaptation_ind
;
1749 /* The INIT stuff will be done by the side effects. */
1754 sctp_association_free(retval
);
1759 /* Yikes! The packet is either corrupt or deliberately
1762 *error
= -SCTP_IERROR_MALFORMED
;
1766 /********************************************************************
1767 * 3rd Level Abstractions
1768 ********************************************************************/
1770 struct __sctp_missing
{
1773 } __attribute__((packed
));
1776 * Report a missing mandatory parameter.
1778 static int sctp_process_missing_param(const struct sctp_association
*asoc
,
1779 sctp_param_t paramtype
,
1780 struct sctp_chunk
*chunk
,
1781 struct sctp_chunk
**errp
)
1783 struct __sctp_missing report
;
1786 len
= WORD_ROUND(sizeof(report
));
1788 /* Make an ERROR chunk, preparing enough room for
1789 * returning multiple unknown parameters.
1792 *errp
= sctp_make_op_error_space(asoc
, chunk
, len
);
1795 report
.num_missing
= htonl(1);
1796 report
.type
= paramtype
;
1797 sctp_init_cause(*errp
, SCTP_ERROR_MISS_PARAM
,
1799 sctp_addto_chunk(*errp
, sizeof(report
), &report
);
1802 /* Stop processing this chunk. */
1806 /* Report an Invalid Mandatory Parameter. */
1807 static int sctp_process_inv_mandatory(const struct sctp_association
*asoc
,
1808 struct sctp_chunk
*chunk
,
1809 struct sctp_chunk
**errp
)
1811 /* Invalid Mandatory Parameter Error has no payload. */
1814 *errp
= sctp_make_op_error_space(asoc
, chunk
, 0);
1817 sctp_init_cause(*errp
, SCTP_ERROR_INV_PARAM
, 0);
1819 /* Stop processing this chunk. */
1823 static int sctp_process_inv_paramlength(const struct sctp_association
*asoc
,
1824 struct sctp_paramhdr
*param
,
1825 const struct sctp_chunk
*chunk
,
1826 struct sctp_chunk
**errp
)
1828 /* This is a fatal error. Any accumulated non-fatal errors are
1832 sctp_chunk_free(*errp
);
1834 /* Create an error chunk and fill it in with our payload. */
1835 *errp
= sctp_make_violation_paramlen(asoc
, chunk
, param
);
1841 /* Do not attempt to handle the HOST_NAME parm. However, do
1842 * send back an indicator to the peer.
1844 static int sctp_process_hn_param(const struct sctp_association
*asoc
,
1845 union sctp_params param
,
1846 struct sctp_chunk
*chunk
,
1847 struct sctp_chunk
**errp
)
1849 __u16 len
= ntohs(param
.p
->length
);
1851 /* Processing of the HOST_NAME parameter will generate an
1852 * ABORT. If we've accumulated any non-fatal errors, they
1853 * would be unrecognized parameters and we should not include
1854 * them in the ABORT.
1857 sctp_chunk_free(*errp
);
1859 *errp
= sctp_make_op_error_space(asoc
, chunk
, len
);
1862 sctp_init_cause(*errp
, SCTP_ERROR_DNS_FAILED
, len
);
1863 sctp_addto_chunk(*errp
, len
, param
.v
);
1866 /* Stop processing this chunk. */
1870 static int sctp_verify_ext_param(union sctp_params param
)
1872 __u16 num_ext
= ntohs(param
.p
->length
) - sizeof(sctp_paramhdr_t
);
1874 int have_asconf
= 0;
1877 for (i
= 0; i
< num_ext
; i
++) {
1878 switch (param
.ext
->chunks
[i
]) {
1882 case SCTP_CID_ASCONF
:
1883 case SCTP_CID_ASCONF_ACK
:
1889 /* ADD-IP Security: The draft requires us to ABORT or ignore the
1890 * INIT/INIT-ACK if ADD-IP is listed, but AUTH is not. Do this
1891 * only if ADD-IP is turned on and we are not backward-compatible
1894 if (sctp_addip_noauth
)
1897 if (sctp_addip_enable
&& !have_auth
&& have_asconf
)
1903 static void sctp_process_ext_param(struct sctp_association
*asoc
,
1904 union sctp_params param
)
1906 __u16 num_ext
= ntohs(param
.p
->length
) - sizeof(sctp_paramhdr_t
);
1909 for (i
= 0; i
< num_ext
; i
++) {
1910 switch (param
.ext
->chunks
[i
]) {
1911 case SCTP_CID_FWD_TSN
:
1912 if (sctp_prsctp_enable
&&
1913 !asoc
->peer
.prsctp_capable
)
1914 asoc
->peer
.prsctp_capable
= 1;
1917 /* if the peer reports AUTH, assume that he
1920 if (sctp_auth_enable
)
1921 asoc
->peer
.auth_capable
= 1;
1923 case SCTP_CID_ASCONF
:
1924 case SCTP_CID_ASCONF_ACK
:
1925 if (sctp_addip_enable
)
1926 asoc
->peer
.asconf_capable
= 1;
1934 /* RFC 3.2.1 & the Implementers Guide 2.2.
1936 * The Parameter Types are encoded such that the
1937 * highest-order two bits specify the action that must be
1938 * taken if the processing endpoint does not recognize the
1941 * 00 - Stop processing this parameter; do not process any further
1942 * parameters within this chunk
1944 * 01 - Stop processing this parameter, do not process any further
1945 * parameters within this chunk, and report the unrecognized
1946 * parameter in an 'Unrecognized Parameter' ERROR chunk.
1948 * 10 - Skip this parameter and continue processing.
1950 * 11 - Skip this parameter and continue processing but
1951 * report the unrecognized parameter in an
1952 * 'Unrecognized Parameter' ERROR chunk.
1955 * SCTP_IERROR_NO_ERROR - continue with the chunk
1956 * SCTP_IERROR_ERROR - stop and report an error.
1957 * SCTP_IERROR_NOMEME - out of memory.
1959 static sctp_ierror_t
sctp_process_unk_param(const struct sctp_association
*asoc
,
1960 union sctp_params param
,
1961 struct sctp_chunk
*chunk
,
1962 struct sctp_chunk
**errp
)
1964 int retval
= SCTP_IERROR_NO_ERROR
;
1966 switch (param
.p
->type
& SCTP_PARAM_ACTION_MASK
) {
1967 case SCTP_PARAM_ACTION_DISCARD
:
1968 retval
= SCTP_IERROR_ERROR
;
1970 case SCTP_PARAM_ACTION_SKIP
:
1972 case SCTP_PARAM_ACTION_DISCARD_ERR
:
1973 retval
= SCTP_IERROR_ERROR
;
1975 case SCTP_PARAM_ACTION_SKIP_ERR
:
1976 /* Make an ERROR chunk, preparing enough room for
1977 * returning multiple unknown parameters.
1980 *errp
= sctp_make_op_error_space(asoc
, chunk
,
1981 ntohs(chunk
->chunk_hdr
->length
));
1984 sctp_init_cause(*errp
, SCTP_ERROR_UNKNOWN_PARAM
,
1985 WORD_ROUND(ntohs(param
.p
->length
)));
1986 sctp_addto_chunk(*errp
,
1987 WORD_ROUND(ntohs(param
.p
->length
)),
1990 /* If there is no memory for generating the ERROR
1991 * report as specified, an ABORT will be triggered
1992 * to the peer and the association won't be
1995 retval
= SCTP_IERROR_NOMEM
;
2005 /* Verify variable length parameters
2007 * SCTP_IERROR_ABORT - trigger an ABORT
2008 * SCTP_IERROR_NOMEM - out of memory (abort)
2009 * SCTP_IERROR_ERROR - stop processing, trigger an ERROR
2010 * SCTP_IERROR_NO_ERROR - continue with the chunk
2012 static sctp_ierror_t
sctp_verify_param(const struct sctp_association
*asoc
,
2013 union sctp_params param
,
2015 struct sctp_chunk
*chunk
,
2016 struct sctp_chunk
**err_chunk
)
2018 struct sctp_hmac_algo_param
*hmacs
;
2019 int retval
= SCTP_IERROR_NO_ERROR
;
2020 __u16 n_elt
, id
= 0;
2023 /* FIXME - This routine is not looking at each parameter per the
2024 * chunk type, i.e., unrecognized parameters should be further
2025 * identified based on the chunk id.
2028 switch (param
.p
->type
) {
2029 case SCTP_PARAM_IPV4_ADDRESS
:
2030 case SCTP_PARAM_IPV6_ADDRESS
:
2031 case SCTP_PARAM_COOKIE_PRESERVATIVE
:
2032 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES
:
2033 case SCTP_PARAM_STATE_COOKIE
:
2034 case SCTP_PARAM_HEARTBEAT_INFO
:
2035 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS
:
2036 case SCTP_PARAM_ECN_CAPABLE
:
2037 case SCTP_PARAM_ADAPTATION_LAYER_IND
:
2040 case SCTP_PARAM_SUPPORTED_EXT
:
2041 if (!sctp_verify_ext_param(param
))
2042 return SCTP_IERROR_ABORT
;
2045 case SCTP_PARAM_SET_PRIMARY
:
2046 if (sctp_addip_enable
)
2050 case SCTP_PARAM_HOST_NAME_ADDRESS
:
2051 /* Tell the peer, we won't support this param. */
2052 sctp_process_hn_param(asoc
, param
, chunk
, err_chunk
);
2053 retval
= SCTP_IERROR_ABORT
;
2056 case SCTP_PARAM_FWD_TSN_SUPPORT
:
2057 if (sctp_prsctp_enable
)
2061 case SCTP_PARAM_RANDOM
:
2062 if (!sctp_auth_enable
)
2065 /* SCTP-AUTH: Secion 6.1
2066 * If the random number is not 32 byte long the association
2067 * MUST be aborted. The ABORT chunk SHOULD contain the error
2068 * cause 'Protocol Violation'.
2070 if (SCTP_AUTH_RANDOM_LENGTH
!=
2071 ntohs(param
.p
->length
) - sizeof(sctp_paramhdr_t
)) {
2072 sctp_process_inv_paramlength(asoc
, param
.p
,
2074 retval
= SCTP_IERROR_ABORT
;
2078 case SCTP_PARAM_CHUNKS
:
2079 if (!sctp_auth_enable
)
2082 /* SCTP-AUTH: Section 3.2
2083 * The CHUNKS parameter MUST be included once in the INIT or
2084 * INIT-ACK chunk if the sender wants to receive authenticated
2085 * chunks. Its maximum length is 260 bytes.
2087 if (260 < ntohs(param
.p
->length
)) {
2088 sctp_process_inv_paramlength(asoc
, param
.p
,
2090 retval
= SCTP_IERROR_ABORT
;
2094 case SCTP_PARAM_HMAC_ALGO
:
2095 if (!sctp_auth_enable
)
2098 hmacs
= (struct sctp_hmac_algo_param
*)param
.p
;
2099 n_elt
= (ntohs(param
.p
->length
) - sizeof(sctp_paramhdr_t
)) >> 1;
2101 /* SCTP-AUTH: Section 6.1
2102 * The HMAC algorithm based on SHA-1 MUST be supported and
2103 * included in the HMAC-ALGO parameter.
2105 for (i
= 0; i
< n_elt
; i
++) {
2106 id
= ntohs(hmacs
->hmac_ids
[i
]);
2108 if (id
== SCTP_AUTH_HMAC_ID_SHA1
)
2112 if (id
!= SCTP_AUTH_HMAC_ID_SHA1
) {
2113 sctp_process_inv_paramlength(asoc
, param
.p
, chunk
,
2115 retval
= SCTP_IERROR_ABORT
;
2120 SCTP_DEBUG_PRINTK("Unrecognized param: %d for chunk %d.\n",
2121 ntohs(param
.p
->type
), cid
);
2122 retval
= sctp_process_unk_param(asoc
, param
, chunk
, err_chunk
);
2128 /* Verify the INIT packet before we process it. */
2129 int sctp_verify_init(const struct sctp_association
*asoc
,
2131 sctp_init_chunk_t
*peer_init
,
2132 struct sctp_chunk
*chunk
,
2133 struct sctp_chunk
**errp
)
2135 union sctp_params param
;
2139 /* Verify stream values are non-zero. */
2140 if ((0 == peer_init
->init_hdr
.num_outbound_streams
) ||
2141 (0 == peer_init
->init_hdr
.num_inbound_streams
) ||
2142 (0 == peer_init
->init_hdr
.init_tag
) ||
2143 (SCTP_DEFAULT_MINWINDOW
> ntohl(peer_init
->init_hdr
.a_rwnd
))) {
2145 return sctp_process_inv_mandatory(asoc
, chunk
, errp
);
2148 /* Check for missing mandatory parameters. */
2149 sctp_walk_params(param
, peer_init
, init_hdr
.params
) {
2151 if (SCTP_PARAM_STATE_COOKIE
== param
.p
->type
)
2154 } /* for (loop through all parameters) */
2156 /* There is a possibility that a parameter length was bad and
2157 * in that case we would have stoped walking the parameters.
2158 * The current param.p would point at the bad one.
2159 * Current consensus on the mailing list is to generate a PROTOCOL
2160 * VIOLATION error. We build the ERROR chunk here and let the normal
2161 * error handling code build and send the packet.
2163 if (param
.v
!= (void*)chunk
->chunk_end
)
2164 return sctp_process_inv_paramlength(asoc
, param
.p
, chunk
, errp
);
2166 /* The only missing mandatory param possible today is
2167 * the state cookie for an INIT-ACK chunk.
2169 if ((SCTP_CID_INIT_ACK
== cid
) && !has_cookie
)
2170 return sctp_process_missing_param(asoc
, SCTP_PARAM_STATE_COOKIE
,
2173 /* Verify all the variable length parameters */
2174 sctp_walk_params(param
, peer_init
, init_hdr
.params
) {
2176 result
= sctp_verify_param(asoc
, param
, cid
, chunk
, errp
);
2178 case SCTP_IERROR_ABORT
:
2179 case SCTP_IERROR_NOMEM
:
2181 case SCTP_IERROR_ERROR
:
2183 case SCTP_IERROR_NO_ERROR
:
2188 } /* for (loop through all parameters) */
2193 /* Unpack the parameters in an INIT packet into an association.
2194 * Returns 0 on failure, else success.
2195 * FIXME: This is an association method.
2197 int sctp_process_init(struct sctp_association
*asoc
, sctp_cid_t cid
,
2198 const union sctp_addr
*peer_addr
,
2199 sctp_init_chunk_t
*peer_init
, gfp_t gfp
)
2201 union sctp_params param
;
2202 struct sctp_transport
*transport
;
2203 struct list_head
*pos
, *temp
;
2206 /* We must include the address that the INIT packet came from.
2207 * This is the only address that matters for an INIT packet.
2208 * When processing a COOKIE ECHO, we retrieve the from address
2209 * of the INIT from the cookie.
2212 /* This implementation defaults to making the first transport
2213 * added as the primary transport. The source address seems to
2214 * be a a better choice than any of the embedded addresses.
2217 if(!sctp_assoc_add_peer(asoc
, peer_addr
, gfp
, SCTP_ACTIVE
))
2221 /* Process the initialization parameters. */
2222 sctp_walk_params(param
, peer_init
, init_hdr
.params
) {
2224 if (!sctp_process_param(asoc
, param
, peer_addr
, gfp
))
2228 /* AUTH: After processing the parameters, make sure that we
2229 * have all the required info to potentially do authentications.
2231 if (asoc
->peer
.auth_capable
&& (!asoc
->peer
.peer_random
||
2232 !asoc
->peer
.peer_hmacs
))
2233 asoc
->peer
.auth_capable
= 0;
2235 /* In a non-backward compatible mode, if the peer claims
2236 * support for ADD-IP but not AUTH, the ADD-IP spec states
2237 * that we MUST ABORT the association. Section 6. The section
2238 * also give us an option to silently ignore the packet, which
2239 * is what we'll do here.
2241 if (!sctp_addip_noauth
&&
2242 (asoc
->peer
.asconf_capable
&& !asoc
->peer
.auth_capable
)) {
2243 asoc
->peer
.addip_disabled_mask
|= (SCTP_PARAM_ADD_IP
|
2245 SCTP_PARAM_SET_PRIMARY
);
2246 asoc
->peer
.asconf_capable
= 0;
2250 /* Walk list of transports, removing transports in the UNKNOWN state. */
2251 list_for_each_safe(pos
, temp
, &asoc
->peer
.transport_addr_list
) {
2252 transport
= list_entry(pos
, struct sctp_transport
, transports
);
2253 if (transport
->state
== SCTP_UNKNOWN
) {
2254 sctp_assoc_rm_peer(asoc
, transport
);
2258 /* The fixed INIT headers are always in network byte
2261 asoc
->peer
.i
.init_tag
=
2262 ntohl(peer_init
->init_hdr
.init_tag
);
2263 asoc
->peer
.i
.a_rwnd
=
2264 ntohl(peer_init
->init_hdr
.a_rwnd
);
2265 asoc
->peer
.i
.num_outbound_streams
=
2266 ntohs(peer_init
->init_hdr
.num_outbound_streams
);
2267 asoc
->peer
.i
.num_inbound_streams
=
2268 ntohs(peer_init
->init_hdr
.num_inbound_streams
);
2269 asoc
->peer
.i
.initial_tsn
=
2270 ntohl(peer_init
->init_hdr
.initial_tsn
);
2272 /* Apply the upper bounds for output streams based on peer's
2273 * number of inbound streams.
2275 if (asoc
->c
.sinit_num_ostreams
>
2276 ntohs(peer_init
->init_hdr
.num_inbound_streams
)) {
2277 asoc
->c
.sinit_num_ostreams
=
2278 ntohs(peer_init
->init_hdr
.num_inbound_streams
);
2281 if (asoc
->c
.sinit_max_instreams
>
2282 ntohs(peer_init
->init_hdr
.num_outbound_streams
)) {
2283 asoc
->c
.sinit_max_instreams
=
2284 ntohs(peer_init
->init_hdr
.num_outbound_streams
);
2287 /* Copy Initiation tag from INIT to VT_peer in cookie. */
2288 asoc
->c
.peer_vtag
= asoc
->peer
.i
.init_tag
;
2290 /* Peer Rwnd : Current calculated value of the peer's rwnd. */
2291 asoc
->peer
.rwnd
= asoc
->peer
.i
.a_rwnd
;
2293 /* Copy cookie in case we need to resend COOKIE-ECHO. */
2294 cookie
= asoc
->peer
.cookie
;
2296 asoc
->peer
.cookie
= kmemdup(cookie
, asoc
->peer
.cookie_len
, gfp
);
2297 if (!asoc
->peer
.cookie
)
2301 /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
2302 * high (for example, implementations MAY use the size of the receiver
2303 * advertised window).
2305 list_for_each_entry(transport
, &asoc
->peer
.transport_addr_list
,
2307 transport
->ssthresh
= asoc
->peer
.i
.a_rwnd
;
2310 /* Set up the TSN tracking pieces. */
2311 if (!sctp_tsnmap_init(&asoc
->peer
.tsn_map
, SCTP_TSN_MAP_INITIAL
,
2312 asoc
->peer
.i
.initial_tsn
, gfp
))
2315 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
2317 * The stream sequence number in all the streams shall start
2318 * from 0 when the association is established. Also, when the
2319 * stream sequence number reaches the value 65535 the next
2320 * stream sequence number shall be set to 0.
2323 /* Allocate storage for the negotiated streams if it is not a temporary
2329 asoc
->ssnmap
= sctp_ssnmap_new(asoc
->c
.sinit_max_instreams
,
2330 asoc
->c
.sinit_num_ostreams
, gfp
);
2334 error
= sctp_assoc_set_id(asoc
, gfp
);
2339 /* ADDIP Section 4.1 ASCONF Chunk Procedures
2341 * When an endpoint has an ASCONF signaled change to be sent to the
2342 * remote endpoint it should do the following:
2344 * A2) A serial number should be assigned to the Chunk. The serial
2345 * number should be a monotonically increasing number. All serial
2346 * numbers are defined to be initialized at the start of the
2347 * association to the same value as the Initial TSN.
2349 asoc
->peer
.addip_serial
= asoc
->peer
.i
.initial_tsn
- 1;
2353 /* Release the transport structures. */
2354 list_for_each_safe(pos
, temp
, &asoc
->peer
.transport_addr_list
) {
2355 transport
= list_entry(pos
, struct sctp_transport
, transports
);
2356 if (transport
->state
!= SCTP_ACTIVE
)
2357 sctp_assoc_rm_peer(asoc
, transport
);
2365 /* Update asoc with the option described in param.
2367 * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
2369 * asoc is the association to update.
2370 * param is the variable length parameter to use for update.
2371 * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
2372 * If the current packet is an INIT we want to minimize the amount of
2373 * work we do. In particular, we should not build transport
2374 * structures for the addresses.
2376 static int sctp_process_param(struct sctp_association
*asoc
,
2377 union sctp_params param
,
2378 const union sctp_addr
*peer_addr
,
2381 union sctp_addr addr
;
2388 union sctp_addr_param
*addr_param
;
2389 struct sctp_transport
*t
;
2391 /* We maintain all INIT parameters in network byte order all the
2392 * time. This allows us to not worry about whether the parameters
2393 * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
2395 switch (param
.p
->type
) {
2396 case SCTP_PARAM_IPV6_ADDRESS
:
2397 if (PF_INET6
!= asoc
->base
.sk
->sk_family
)
2401 case SCTP_PARAM_IPV4_ADDRESS
:
2402 /* v4 addresses are not allowed on v6-only socket */
2403 if (ipv6_only_sock(asoc
->base
.sk
))
2406 af
= sctp_get_af_specific(param_type2af(param
.p
->type
));
2407 af
->from_addr_param(&addr
, param
.addr
, htons(asoc
->peer
.port
), 0);
2408 scope
= sctp_scope(peer_addr
);
2409 if (sctp_in_scope(&addr
, scope
))
2410 if (!sctp_assoc_add_peer(asoc
, &addr
, gfp
, SCTP_UNCONFIRMED
))
2414 case SCTP_PARAM_COOKIE_PRESERVATIVE
:
2415 if (!sctp_cookie_preserve_enable
)
2418 stale
= ntohl(param
.life
->lifespan_increment
);
2420 /* Suggested Cookie Life span increment's unit is msec,
2423 asoc
->cookie_life
.tv_sec
+= stale
/ 1000;
2424 asoc
->cookie_life
.tv_usec
+= (stale
% 1000) * 1000;
2427 case SCTP_PARAM_HOST_NAME_ADDRESS
:
2428 SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
2431 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES
:
2432 /* Turn off the default values first so we'll know which
2433 * ones are really set by the peer.
2435 asoc
->peer
.ipv4_address
= 0;
2436 asoc
->peer
.ipv6_address
= 0;
2438 /* Assume that peer supports the address family
2439 * by which it sends a packet.
2441 if (peer_addr
->sa
.sa_family
== AF_INET6
)
2442 asoc
->peer
.ipv6_address
= 1;
2443 else if (peer_addr
->sa
.sa_family
== AF_INET
)
2444 asoc
->peer
.ipv4_address
= 1;
2446 /* Cycle through address types; avoid divide by 0. */
2447 sat
= ntohs(param
.p
->length
) - sizeof(sctp_paramhdr_t
);
2449 sat
/= sizeof(__u16
);
2451 for (i
= 0; i
< sat
; ++i
) {
2452 switch (param
.sat
->types
[i
]) {
2453 case SCTP_PARAM_IPV4_ADDRESS
:
2454 asoc
->peer
.ipv4_address
= 1;
2457 case SCTP_PARAM_IPV6_ADDRESS
:
2458 if (PF_INET6
== asoc
->base
.sk
->sk_family
)
2459 asoc
->peer
.ipv6_address
= 1;
2462 case SCTP_PARAM_HOST_NAME_ADDRESS
:
2463 asoc
->peer
.hostname_address
= 1;
2466 default: /* Just ignore anything else. */
2472 case SCTP_PARAM_STATE_COOKIE
:
2473 asoc
->peer
.cookie_len
=
2474 ntohs(param
.p
->length
) - sizeof(sctp_paramhdr_t
);
2475 asoc
->peer
.cookie
= param
.cookie
->body
;
2478 case SCTP_PARAM_HEARTBEAT_INFO
:
2479 /* Would be odd to receive, but it causes no problems. */
2482 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS
:
2483 /* Rejected during verify stage. */
2486 case SCTP_PARAM_ECN_CAPABLE
:
2487 asoc
->peer
.ecn_capable
= 1;
2490 case SCTP_PARAM_ADAPTATION_LAYER_IND
:
2491 asoc
->peer
.adaptation_ind
= ntohl(param
.aind
->adaptation_ind
);
2494 case SCTP_PARAM_SET_PRIMARY
:
2495 if (!sctp_addip_enable
)
2498 addr_param
= param
.v
+ sizeof(sctp_addip_param_t
);
2500 af
= sctp_get_af_specific(param_type2af(param
.p
->type
));
2501 af
->from_addr_param(&addr
, addr_param
,
2502 htons(asoc
->peer
.port
), 0);
2504 /* if the address is invalid, we can't process it.
2505 * XXX: see spec for what to do.
2507 if (!af
->addr_valid(&addr
, NULL
, NULL
))
2510 t
= sctp_assoc_lookup_paddr(asoc
, &addr
);
2514 sctp_assoc_set_primary(asoc
, t
);
2517 case SCTP_PARAM_SUPPORTED_EXT
:
2518 sctp_process_ext_param(asoc
, param
);
2521 case SCTP_PARAM_FWD_TSN_SUPPORT
:
2522 if (sctp_prsctp_enable
) {
2523 asoc
->peer
.prsctp_capable
= 1;
2529 case SCTP_PARAM_RANDOM
:
2530 if (!sctp_auth_enable
)
2533 /* Save peer's random parameter */
2534 asoc
->peer
.peer_random
= kmemdup(param
.p
,
2535 ntohs(param
.p
->length
), gfp
);
2536 if (!asoc
->peer
.peer_random
) {
2542 case SCTP_PARAM_HMAC_ALGO
:
2543 if (!sctp_auth_enable
)
2546 /* Save peer's HMAC list */
2547 asoc
->peer
.peer_hmacs
= kmemdup(param
.p
,
2548 ntohs(param
.p
->length
), gfp
);
2549 if (!asoc
->peer
.peer_hmacs
) {
2554 /* Set the default HMAC the peer requested*/
2555 sctp_auth_asoc_set_default_hmac(asoc
, param
.hmac_algo
);
2558 case SCTP_PARAM_CHUNKS
:
2559 if (!sctp_auth_enable
)
2562 asoc
->peer
.peer_chunks
= kmemdup(param
.p
,
2563 ntohs(param
.p
->length
), gfp
);
2564 if (!asoc
->peer
.peer_chunks
)
2569 /* Any unrecognized parameters should have been caught
2570 * and handled by sctp_verify_param() which should be
2571 * called prior to this routine. Simply log the error
2574 SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
2575 ntohs(param
.p
->type
), asoc
);
2582 /* Select a new verification tag. */
2583 __u32
sctp_generate_tag(const struct sctp_endpoint
*ep
)
2585 /* I believe that this random number generator complies with RFC1750.
2586 * A tag of 0 is reserved for special cases (e.g. INIT).
2591 get_random_bytes(&x
, sizeof(__u32
));
2597 /* Select an initial TSN to send during startup. */
2598 __u32
sctp_generate_tsn(const struct sctp_endpoint
*ep
)
2602 get_random_bytes(&retval
, sizeof(__u32
));
2607 * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
2609 * 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
2610 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2611 * | Type = 0xC1 | Chunk Flags | Chunk Length |
2612 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2614 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2615 * | Address Parameter |
2616 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2617 * | ASCONF Parameter #1 |
2618 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2622 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2623 * | ASCONF Parameter #N |
2624 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2626 * Address Parameter and other parameter will not be wrapped in this function
2628 static struct sctp_chunk
*sctp_make_asconf(struct sctp_association
*asoc
,
2629 union sctp_addr
*addr
,
2632 sctp_addiphdr_t asconf
;
2633 struct sctp_chunk
*retval
;
2634 int length
= sizeof(asconf
) + vparam_len
;
2635 union sctp_addr_param addrparam
;
2637 struct sctp_af
*af
= sctp_get_af_specific(addr
->v4
.sin_family
);
2639 addrlen
= af
->to_addr_param(addr
, &addrparam
);
2644 /* Create the chunk. */
2645 retval
= sctp_make_chunk(asoc
, SCTP_CID_ASCONF
, 0, length
);
2649 asconf
.serial
= htonl(asoc
->addip_serial
++);
2651 retval
->subh
.addip_hdr
=
2652 sctp_addto_chunk(retval
, sizeof(asconf
), &asconf
);
2653 retval
->param_hdr
.v
=
2654 sctp_addto_chunk(retval
, addrlen
, &addrparam
);
2660 * 3.2.1 Add IP Address
2662 * 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
2663 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2664 * | Type = 0xC001 | Length = Variable |
2665 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2666 * | ASCONF-Request Correlation ID |
2667 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2668 * | Address Parameter |
2669 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2671 * 3.2.2 Delete IP Address
2673 * 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
2674 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2675 * | Type = 0xC002 | Length = Variable |
2676 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2677 * | ASCONF-Request Correlation ID |
2678 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2679 * | Address Parameter |
2680 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2683 struct sctp_chunk
*sctp_make_asconf_update_ip(struct sctp_association
*asoc
,
2684 union sctp_addr
*laddr
,
2685 struct sockaddr
*addrs
,
2689 sctp_addip_param_t param
;
2690 struct sctp_chunk
*retval
;
2691 union sctp_addr_param addr_param
;
2692 union sctp_addr
*addr
;
2695 int paramlen
= sizeof(param
);
2696 int addr_param_len
= 0;
2700 /* Get total length of all the address parameters. */
2702 for (i
= 0; i
< addrcnt
; i
++) {
2703 addr
= (union sctp_addr
*)addr_buf
;
2704 af
= sctp_get_af_specific(addr
->v4
.sin_family
);
2705 addr_param_len
= af
->to_addr_param(addr
, &addr_param
);
2707 totallen
+= paramlen
;
2708 totallen
+= addr_param_len
;
2710 addr_buf
+= af
->sockaddr_len
;
2713 /* Create an asconf chunk with the required length. */
2714 retval
= sctp_make_asconf(asoc
, laddr
, totallen
);
2718 /* Add the address parameters to the asconf chunk. */
2720 for (i
= 0; i
< addrcnt
; i
++) {
2721 addr
= (union sctp_addr
*)addr_buf
;
2722 af
= sctp_get_af_specific(addr
->v4
.sin_family
);
2723 addr_param_len
= af
->to_addr_param(addr
, &addr_param
);
2724 param
.param_hdr
.type
= flags
;
2725 param
.param_hdr
.length
= htons(paramlen
+ addr_param_len
);
2728 sctp_addto_chunk(retval
, paramlen
, ¶m
);
2729 sctp_addto_chunk(retval
, addr_param_len
, &addr_param
);
2731 addr_buf
+= af
->sockaddr_len
;
2737 * 3.2.4 Set Primary IP Address
2739 * 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
2740 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2741 * | Type =0xC004 | Length = Variable |
2742 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2743 * | ASCONF-Request Correlation ID |
2744 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2745 * | Address Parameter |
2746 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2748 * Create an ASCONF chunk with Set Primary IP address parameter.
2750 struct sctp_chunk
*sctp_make_asconf_set_prim(struct sctp_association
*asoc
,
2751 union sctp_addr
*addr
)
2753 sctp_addip_param_t param
;
2754 struct sctp_chunk
*retval
;
2755 int len
= sizeof(param
);
2756 union sctp_addr_param addrparam
;
2758 struct sctp_af
*af
= sctp_get_af_specific(addr
->v4
.sin_family
);
2760 addrlen
= af
->to_addr_param(addr
, &addrparam
);
2765 /* Create the chunk and make asconf header. */
2766 retval
= sctp_make_asconf(asoc
, addr
, len
);
2770 param
.param_hdr
.type
= SCTP_PARAM_SET_PRIMARY
;
2771 param
.param_hdr
.length
= htons(len
);
2774 sctp_addto_chunk(retval
, sizeof(param
), ¶m
);
2775 sctp_addto_chunk(retval
, addrlen
, &addrparam
);
2780 /* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2782 * 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
2783 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2784 * | Type = 0x80 | Chunk Flags | Chunk Length |
2785 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2787 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2788 * | ASCONF Parameter Response#1 |
2789 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2793 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2794 * | ASCONF Parameter Response#N |
2795 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2797 * Create an ASCONF_ACK chunk with enough space for the parameter responses.
2799 static struct sctp_chunk
*sctp_make_asconf_ack(const struct sctp_association
*asoc
,
2800 __u32 serial
, int vparam_len
)
2802 sctp_addiphdr_t asconf
;
2803 struct sctp_chunk
*retval
;
2804 int length
= sizeof(asconf
) + vparam_len
;
2806 /* Create the chunk. */
2807 retval
= sctp_make_chunk(asoc
, SCTP_CID_ASCONF_ACK
, 0, length
);
2811 asconf
.serial
= htonl(serial
);
2813 retval
->subh
.addip_hdr
=
2814 sctp_addto_chunk(retval
, sizeof(asconf
), &asconf
);
2819 /* Add response parameters to an ASCONF_ACK chunk. */
2820 static void sctp_add_asconf_response(struct sctp_chunk
*chunk
, __be32 crr_id
,
2821 __be16 err_code
, sctp_addip_param_t
*asconf_param
)
2823 sctp_addip_param_t ack_param
;
2824 sctp_errhdr_t err_param
;
2825 int asconf_param_len
= 0;
2826 int err_param_len
= 0;
2827 __be16 response_type
;
2829 if (SCTP_ERROR_NO_ERROR
== err_code
) {
2830 response_type
= SCTP_PARAM_SUCCESS_REPORT
;
2832 response_type
= SCTP_PARAM_ERR_CAUSE
;
2833 err_param_len
= sizeof(err_param
);
2836 ntohs(asconf_param
->param_hdr
.length
);
2839 /* Add Success Indication or Error Cause Indication parameter. */
2840 ack_param
.param_hdr
.type
= response_type
;
2841 ack_param
.param_hdr
.length
= htons(sizeof(ack_param
) +
2844 ack_param
.crr_id
= crr_id
;
2845 sctp_addto_chunk(chunk
, sizeof(ack_param
), &ack_param
);
2847 if (SCTP_ERROR_NO_ERROR
== err_code
)
2850 /* Add Error Cause parameter. */
2851 err_param
.cause
= err_code
;
2852 err_param
.length
= htons(err_param_len
+ asconf_param_len
);
2853 sctp_addto_chunk(chunk
, err_param_len
, &err_param
);
2855 /* Add the failed TLV copied from ASCONF chunk. */
2857 sctp_addto_chunk(chunk
, asconf_param_len
, asconf_param
);
2860 /* Process a asconf parameter. */
2861 static __be16
sctp_process_asconf_param(struct sctp_association
*asoc
,
2862 struct sctp_chunk
*asconf
,
2863 sctp_addip_param_t
*asconf_param
)
2865 struct sctp_transport
*peer
;
2867 union sctp_addr addr
;
2868 union sctp_addr_param
*addr_param
;
2870 addr_param
= (union sctp_addr_param
*)
2871 ((void *)asconf_param
+ sizeof(sctp_addip_param_t
));
2873 if (asconf_param
->param_hdr
.type
!= SCTP_PARAM_ADD_IP
&&
2874 asconf_param
->param_hdr
.type
!= SCTP_PARAM_DEL_IP
&&
2875 asconf_param
->param_hdr
.type
!= SCTP_PARAM_SET_PRIMARY
)
2876 return SCTP_ERROR_UNKNOWN_PARAM
;
2878 switch (addr_param
->v4
.param_hdr
.type
) {
2879 case SCTP_PARAM_IPV6_ADDRESS
:
2880 if (!asoc
->peer
.ipv6_address
)
2881 return SCTP_ERROR_DNS_FAILED
;
2883 case SCTP_PARAM_IPV4_ADDRESS
:
2884 if (!asoc
->peer
.ipv4_address
)
2885 return SCTP_ERROR_DNS_FAILED
;
2888 return SCTP_ERROR_DNS_FAILED
;
2891 af
= sctp_get_af_specific(param_type2af(addr_param
->v4
.param_hdr
.type
));
2893 return SCTP_ERROR_DNS_FAILED
;
2895 af
->from_addr_param(&addr
, addr_param
, htons(asoc
->peer
.port
), 0);
2897 /* ADDIP 4.2.1 This parameter MUST NOT contain a broadcast
2898 * or multicast address.
2899 * (note: wildcard is permitted and requires special handling so
2900 * make sure we check for that)
2902 if (!af
->is_any(&addr
) && !af
->addr_valid(&addr
, NULL
, asconf
->skb
))
2903 return SCTP_ERROR_DNS_FAILED
;
2905 switch (asconf_param
->param_hdr
.type
) {
2906 case SCTP_PARAM_ADD_IP
:
2908 * If the address 0.0.0.0 or ::0 is provided, the source
2909 * address of the packet MUST be added.
2911 if (af
->is_any(&addr
))
2912 memcpy(&addr
, &asconf
->source
, sizeof(addr
));
2914 /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
2915 * request and does not have the local resources to add this
2916 * new address to the association, it MUST return an Error
2917 * Cause TLV set to the new error code 'Operation Refused
2918 * Due to Resource Shortage'.
2921 peer
= sctp_assoc_add_peer(asoc
, &addr
, GFP_ATOMIC
, SCTP_UNCONFIRMED
);
2923 return SCTP_ERROR_RSRC_LOW
;
2925 /* Start the heartbeat timer. */
2926 if (!mod_timer(&peer
->hb_timer
, sctp_transport_timeout(peer
)))
2927 sctp_transport_hold(peer
);
2929 case SCTP_PARAM_DEL_IP
:
2930 /* ADDIP 4.3 D7) If a request is received to delete the
2931 * last remaining IP address of a peer endpoint, the receiver
2932 * MUST send an Error Cause TLV with the error cause set to the
2933 * new error code 'Request to Delete Last Remaining IP Address'.
2935 if (asoc
->peer
.transport_count
== 1)
2936 return SCTP_ERROR_DEL_LAST_IP
;
2938 /* ADDIP 4.3 D8) If a request is received to delete an IP
2939 * address which is also the source address of the IP packet
2940 * which contained the ASCONF chunk, the receiver MUST reject
2941 * this request. To reject the request the receiver MUST send
2942 * an Error Cause TLV set to the new error code 'Request to
2943 * Delete Source IP Address'
2945 if (sctp_cmp_addr_exact(sctp_source(asconf
), &addr
))
2946 return SCTP_ERROR_DEL_SRC_IP
;
2949 * If the address 0.0.0.0 or ::0 is provided, all
2950 * addresses of the peer except the source address of the
2951 * packet MUST be deleted.
2953 if (af
->is_any(&addr
)) {
2954 sctp_assoc_set_primary(asoc
, asconf
->transport
);
2955 sctp_assoc_del_nonprimary_peers(asoc
,
2958 sctp_assoc_del_peer(asoc
, &addr
);
2960 case SCTP_PARAM_SET_PRIMARY
:
2961 /* ADDIP Section 4.2.4
2962 * If the address 0.0.0.0 or ::0 is provided, the receiver
2963 * MAY mark the source address of the packet as its
2966 if (af
->is_any(&addr
))
2967 memcpy(&addr
.v4
, sctp_source(asconf
), sizeof(addr
));
2969 peer
= sctp_assoc_lookup_paddr(asoc
, &addr
);
2971 return SCTP_ERROR_DNS_FAILED
;
2973 sctp_assoc_set_primary(asoc
, peer
);
2977 return SCTP_ERROR_NO_ERROR
;
2980 /* Verify the ASCONF packet before we process it. */
2981 int sctp_verify_asconf(const struct sctp_association
*asoc
,
2982 struct sctp_paramhdr
*param_hdr
, void *chunk_end
,
2983 struct sctp_paramhdr
**errp
) {
2984 sctp_addip_param_t
*asconf_param
;
2985 union sctp_params param
;
2988 param
.v
= (sctp_paramhdr_t
*) param_hdr
;
2989 while (param
.v
<= chunk_end
- sizeof(sctp_paramhdr_t
)) {
2990 length
= ntohs(param
.p
->length
);
2993 if (param
.v
> chunk_end
- length
||
2994 length
< sizeof(sctp_paramhdr_t
))
2997 switch (param
.p
->type
) {
2998 case SCTP_PARAM_ADD_IP
:
2999 case SCTP_PARAM_DEL_IP
:
3000 case SCTP_PARAM_SET_PRIMARY
:
3001 asconf_param
= (sctp_addip_param_t
*)param
.v
;
3002 plen
= ntohs(asconf_param
->param_hdr
.length
);
3003 if (plen
< sizeof(sctp_addip_param_t
) +
3004 sizeof(sctp_paramhdr_t
))
3007 case SCTP_PARAM_SUCCESS_REPORT
:
3008 case SCTP_PARAM_ADAPTATION_LAYER_IND
:
3009 if (length
!= sizeof(sctp_addip_param_t
))
3017 param
.v
+= WORD_ROUND(length
);
3020 if (param
.v
!= chunk_end
)
3026 /* Process an incoming ASCONF chunk with the next expected serial no. and
3027 * return an ASCONF_ACK chunk to be sent in response.
3029 struct sctp_chunk
*sctp_process_asconf(struct sctp_association
*asoc
,
3030 struct sctp_chunk
*asconf
)
3032 sctp_addiphdr_t
*hdr
;
3033 union sctp_addr_param
*addr_param
;
3034 sctp_addip_param_t
*asconf_param
;
3035 struct sctp_chunk
*asconf_ack
;
3041 int all_param_pass
= 1;
3043 chunk_len
= ntohs(asconf
->chunk_hdr
->length
) - sizeof(sctp_chunkhdr_t
);
3044 hdr
= (sctp_addiphdr_t
*)asconf
->skb
->data
;
3045 serial
= ntohl(hdr
->serial
);
3047 /* Skip the addiphdr and store a pointer to address parameter. */
3048 length
= sizeof(sctp_addiphdr_t
);
3049 addr_param
= (union sctp_addr_param
*)(asconf
->skb
->data
+ length
);
3050 chunk_len
-= length
;
3052 /* Skip the address parameter and store a pointer to the first
3055 length
= ntohs(addr_param
->v4
.param_hdr
.length
);
3056 asconf_param
= (sctp_addip_param_t
*)((void *)addr_param
+ length
);
3057 chunk_len
-= length
;
3059 /* create an ASCONF_ACK chunk.
3060 * Based on the definitions of parameters, we know that the size of
3061 * ASCONF_ACK parameters are less than or equal to the twice of ASCONF
3064 asconf_ack
= sctp_make_asconf_ack(asoc
, serial
, chunk_len
* 2);
3068 /* Process the TLVs contained within the ASCONF chunk. */
3069 while (chunk_len
> 0) {
3070 err_code
= sctp_process_asconf_param(asoc
, asconf
,
3073 * If an error response is received for a TLV parameter,
3074 * all TLVs with no response before the failed TLV are
3075 * considered successful if not reported. All TLVs after
3076 * the failed response are considered unsuccessful unless
3077 * a specific success indication is present for the parameter.
3079 if (SCTP_ERROR_NO_ERROR
!= err_code
)
3082 if (!all_param_pass
)
3083 sctp_add_asconf_response(asconf_ack
,
3084 asconf_param
->crr_id
, err_code
,
3087 /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
3088 * an IP address sends an 'Out of Resource' in its response, it
3089 * MUST also fail any subsequent add or delete requests bundled
3092 if (SCTP_ERROR_RSRC_LOW
== err_code
)
3095 /* Move to the next ASCONF param. */
3096 length
= ntohs(asconf_param
->param_hdr
.length
);
3097 asconf_param
= (sctp_addip_param_t
*)((void *)asconf_param
+
3099 chunk_len
-= length
;
3103 asoc
->peer
.addip_serial
++;
3105 /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
3106 * after freeing the reference to old asconf ack if any.
3109 sctp_chunk_hold(asconf_ack
);
3110 list_add_tail(&asconf_ack
->transmitted_list
,
3111 &asoc
->asconf_ack_list
);
3117 /* Process a asconf parameter that is successfully acked. */
3118 static void sctp_asconf_param_success(struct sctp_association
*asoc
,
3119 sctp_addip_param_t
*asconf_param
)
3122 union sctp_addr addr
;
3123 struct sctp_bind_addr
*bp
= &asoc
->base
.bind_addr
;
3124 union sctp_addr_param
*addr_param
;
3125 struct sctp_transport
*transport
;
3126 struct sctp_sockaddr_entry
*saddr
;
3128 addr_param
= (union sctp_addr_param
*)
3129 ((void *)asconf_param
+ sizeof(sctp_addip_param_t
));
3131 /* We have checked the packet before, so we do not check again. */
3132 af
= sctp_get_af_specific(param_type2af(addr_param
->v4
.param_hdr
.type
));
3133 af
->from_addr_param(&addr
, addr_param
, htons(bp
->port
), 0);
3135 switch (asconf_param
->param_hdr
.type
) {
3136 case SCTP_PARAM_ADD_IP
:
3137 /* This is always done in BH context with a socket lock
3138 * held, so the list can not change.
3141 list_for_each_entry(saddr
, &bp
->address_list
, list
) {
3142 if (sctp_cmp_addr_exact(&saddr
->a
, &addr
))
3143 saddr
->state
= SCTP_ADDR_SRC
;
3146 list_for_each_entry(transport
, &asoc
->peer
.transport_addr_list
,
3148 if (transport
->state
== SCTP_ACTIVE
)
3150 dst_release(transport
->dst
);
3151 sctp_transport_route(transport
, NULL
,
3152 sctp_sk(asoc
->base
.sk
));
3155 case SCTP_PARAM_DEL_IP
:
3157 sctp_del_bind_addr(bp
, &addr
);
3159 list_for_each_entry(transport
, &asoc
->peer
.transport_addr_list
,
3161 dst_release(transport
->dst
);
3162 sctp_transport_route(transport
, NULL
,
3163 sctp_sk(asoc
->base
.sk
));
3171 /* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
3172 * for the given asconf parameter. If there is no response for this parameter,
3173 * return the error code based on the third argument 'no_err'.
3175 * A7) If an error response is received for a TLV parameter, all TLVs with no
3176 * response before the failed TLV are considered successful if not reported.
3177 * All TLVs after the failed response are considered unsuccessful unless a
3178 * specific success indication is present for the parameter.
3180 static __be16
sctp_get_asconf_response(struct sctp_chunk
*asconf_ack
,
3181 sctp_addip_param_t
*asconf_param
,
3184 sctp_addip_param_t
*asconf_ack_param
;
3185 sctp_errhdr_t
*err_param
;
3191 err_code
= SCTP_ERROR_NO_ERROR
;
3193 err_code
= SCTP_ERROR_REQ_REFUSED
;
3195 asconf_ack_len
= ntohs(asconf_ack
->chunk_hdr
->length
) -
3196 sizeof(sctp_chunkhdr_t
);
3198 /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
3199 * the first asconf_ack parameter.
3201 length
= sizeof(sctp_addiphdr_t
);
3202 asconf_ack_param
= (sctp_addip_param_t
*)(asconf_ack
->skb
->data
+
3204 asconf_ack_len
-= length
;
3206 while (asconf_ack_len
> 0) {
3207 if (asconf_ack_param
->crr_id
== asconf_param
->crr_id
) {
3208 switch(asconf_ack_param
->param_hdr
.type
) {
3209 case SCTP_PARAM_SUCCESS_REPORT
:
3210 return SCTP_ERROR_NO_ERROR
;
3211 case SCTP_PARAM_ERR_CAUSE
:
3212 length
= sizeof(sctp_addip_param_t
);
3213 err_param
= (sctp_errhdr_t
*)
3214 ((void *)asconf_ack_param
+ length
);
3215 asconf_ack_len
-= length
;
3216 if (asconf_ack_len
> 0)
3217 return err_param
->cause
;
3219 return SCTP_ERROR_INV_PARAM
;
3222 return SCTP_ERROR_INV_PARAM
;
3226 length
= ntohs(asconf_ack_param
->param_hdr
.length
);
3227 asconf_ack_param
= (sctp_addip_param_t
*)
3228 ((void *)asconf_ack_param
+ length
);
3229 asconf_ack_len
-= length
;
3235 /* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
3236 int sctp_process_asconf_ack(struct sctp_association
*asoc
,
3237 struct sctp_chunk
*asconf_ack
)
3239 struct sctp_chunk
*asconf
= asoc
->addip_last_asconf
;
3240 union sctp_addr_param
*addr_param
;
3241 sctp_addip_param_t
*asconf_param
;
3243 int asconf_len
= asconf
->skb
->len
;
3244 int all_param_pass
= 0;
3247 __be16 err_code
= SCTP_ERROR_NO_ERROR
;
3249 /* Skip the chunkhdr and addiphdr from the last asconf sent and store
3250 * a pointer to address parameter.
3252 length
= sizeof(sctp_addip_chunk_t
);
3253 addr_param
= (union sctp_addr_param
*)(asconf
->skb
->data
+ length
);
3254 asconf_len
-= length
;
3256 /* Skip the address parameter in the last asconf sent and store a
3257 * pointer to the first asconf parameter.
3259 length
= ntohs(addr_param
->v4
.param_hdr
.length
);
3260 asconf_param
= (sctp_addip_param_t
*)((void *)addr_param
+ length
);
3261 asconf_len
-= length
;
3264 * A8) If there is no response(s) to specific TLV parameter(s), and no
3265 * failures are indicated, then all request(s) are considered
3268 if (asconf_ack
->skb
->len
== sizeof(sctp_addiphdr_t
))
3271 /* Process the TLVs contained in the last sent ASCONF chunk. */
3272 while (asconf_len
> 0) {
3274 err_code
= SCTP_ERROR_NO_ERROR
;
3276 err_code
= sctp_get_asconf_response(asconf_ack
,
3279 if (no_err
&& (SCTP_ERROR_NO_ERROR
!= err_code
))
3284 case SCTP_ERROR_NO_ERROR
:
3285 sctp_asconf_param_success(asoc
, asconf_param
);
3288 case SCTP_ERROR_RSRC_LOW
:
3292 case SCTP_ERROR_UNKNOWN_PARAM
:
3293 /* Disable sending this type of asconf parameter in
3296 asoc
->peer
.addip_disabled_mask
|=
3297 asconf_param
->param_hdr
.type
;
3300 case SCTP_ERROR_REQ_REFUSED
:
3301 case SCTP_ERROR_DEL_LAST_IP
:
3302 case SCTP_ERROR_DEL_SRC_IP
:
3307 /* Skip the processed asconf parameter and move to the next
3310 length
= ntohs(asconf_param
->param_hdr
.length
);
3311 asconf_param
= (sctp_addip_param_t
*)((void *)asconf_param
+
3313 asconf_len
-= length
;
3316 /* Free the cached last sent asconf chunk. */
3317 list_del_init(&asconf
->transmitted_list
);
3318 sctp_chunk_free(asconf
);
3319 asoc
->addip_last_asconf
= NULL
;
3324 /* Make a FWD TSN chunk. */
3325 struct sctp_chunk
*sctp_make_fwdtsn(const struct sctp_association
*asoc
,
3326 __u32 new_cum_tsn
, size_t nstreams
,
3327 struct sctp_fwdtsn_skip
*skiplist
)
3329 struct sctp_chunk
*retval
= NULL
;
3330 struct sctp_fwdtsn_chunk
*ftsn_chunk
;
3331 struct sctp_fwdtsn_hdr ftsn_hdr
;
3332 struct sctp_fwdtsn_skip skip
;
3336 hint
= (nstreams
+ 1) * sizeof(__u32
);
3338 retval
= sctp_make_chunk(asoc
, SCTP_CID_FWD_TSN
, 0, hint
);
3343 ftsn_chunk
= (struct sctp_fwdtsn_chunk
*)retval
->subh
.fwdtsn_hdr
;
3345 ftsn_hdr
.new_cum_tsn
= htonl(new_cum_tsn
);
3346 retval
->subh
.fwdtsn_hdr
=
3347 sctp_addto_chunk(retval
, sizeof(ftsn_hdr
), &ftsn_hdr
);
3349 for (i
= 0; i
< nstreams
; i
++) {
3350 skip
.stream
= skiplist
[i
].stream
;
3351 skip
.ssn
= skiplist
[i
].ssn
;
3352 sctp_addto_chunk(retval
, sizeof(skip
), &skip
);