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, see
27 * <http://www.gnu.org/licenses/>.
29 * Please send any bug reports or fixes you make to the
31 * lksctp developers <linux-sctp@vger.kernel.org>
33 * Written or modified by:
34 * La Monte H.P. Yarroll <piggy@acm.org>
35 * Karl Knutson <karl@athena.chicago.il.us>
36 * C. Robin <chris@hundredacre.ac.uk>
37 * Jon Grimm <jgrimm@us.ibm.com>
38 * Xingang Guo <xingang.guo@intel.com>
39 * Dajiang Zhang <dajiang.zhang@nokia.com>
40 * Sridhar Samudrala <sri@us.ibm.com>
41 * Daisy Chang <daisyc@us.ibm.com>
42 * Ardelle Fan <ardelle.fan@intel.com>
43 * Kevin Gao <kevin.gao@intel.com>
46 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
48 #include <crypto/hash.h>
49 #include <linux/types.h>
50 #include <linux/kernel.h>
52 #include <linux/ipv6.h>
53 #include <linux/net.h>
54 #include <linux/inet.h>
55 #include <linux/scatterlist.h>
56 #include <linux/slab.h>
59 #include <linux/skbuff.h>
60 #include <linux/random.h> /* for get_random_bytes */
61 #include <net/sctp/sctp.h>
62 #include <net/sctp/sm.h>
64 static struct sctp_chunk *sctp_make_control(const struct sctp_association *asoc,
65 __u8 type, __u8 flags, int paylen,
67 static struct sctp_chunk *sctp_make_data(const struct sctp_association *asoc,
68 __u8 flags, int paylen, gfp_t gfp);
69 static struct sctp_chunk *_sctp_make_chunk(const struct sctp_association *asoc,
70 __u8 type, __u8 flags, int paylen,
72 static struct sctp_cookie_param *sctp_pack_cookie(
73 const struct sctp_endpoint *ep,
74 const struct sctp_association *asoc,
75 const struct sctp_chunk *init_chunk,
77 const __u8 *raw_addrs, int addrs_len);
78 static int sctp_process_param(struct sctp_association *asoc,
79 union sctp_params param,
80 const union sctp_addr *peer_addr,
82 static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
84 static void *sctp_addto_chunk_fixed(struct sctp_chunk *, int len,
87 /* Control chunk destructor */
88 static void sctp_control_release_owner(struct sk_buff *skb)
90 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
93 struct sctp_shared_key *shkey = chunk->shkey;
94 struct sctp_association *asoc = chunk->asoc;
96 /* refcnt == 2 and !list_empty mean after this release, it's
97 * not being used anywhere, and it's time to notify userland
98 * that this shkey can be freed if it's been deactivated.
100 if (shkey->deactivated && !list_empty(&shkey->key_list) &&
101 refcount_read(&shkey->refcnt) == 2) {
102 struct sctp_ulpevent *ev;
104 ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id,
108 asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
110 sctp_auth_shkey_release(chunk->shkey);
114 static void sctp_control_set_owner_w(struct sctp_chunk *chunk)
116 struct sctp_association *asoc = chunk->asoc;
117 struct sk_buff *skb = chunk->skb;
119 /* TODO: properly account for control chunks.
120 * To do it right we'll need:
121 * 1) endpoint if association isn't known.
122 * 2) proper memory accounting.
124 * For now don't do anything for now.
127 chunk->shkey = asoc->shkey;
128 sctp_auth_shkey_hold(chunk->shkey);
130 skb->sk = asoc ? asoc->base.sk : NULL;
131 skb_shinfo(skb)->destructor_arg = chunk;
132 skb->destructor = sctp_control_release_owner;
135 /* What was the inbound interface for this chunk? */
136 int sctp_chunk_iif(const struct sctp_chunk *chunk)
138 struct sk_buff *skb = chunk->skb;
140 return SCTP_INPUT_CB(skb)->af->skb_iif(skb);
143 /* RFC 2960 3.3.2 Initiation (INIT) (1)
145 * Note 2: The ECN capable field is reserved for future use of
146 * Explicit Congestion Notification.
148 static const struct sctp_paramhdr ecap_param = {
149 SCTP_PARAM_ECN_CAPABLE,
150 cpu_to_be16(sizeof(struct sctp_paramhdr)),
152 static const struct sctp_paramhdr prsctp_param = {
153 SCTP_PARAM_FWD_TSN_SUPPORT,
154 cpu_to_be16(sizeof(struct sctp_paramhdr)),
157 /* A helper to initialize an op error inside a
158 * provided chunk, as most cause codes will be embedded inside an
161 int sctp_init_cause(struct sctp_chunk *chunk, __be16 cause_code,
164 struct sctp_errhdr err;
167 /* Cause code constants are now defined in network order. */
168 err.cause = cause_code;
169 len = sizeof(err) + paylen;
170 err.length = htons(len);
172 if (skb_tailroom(chunk->skb) < len)
175 chunk->subh.err_hdr = sctp_addto_chunk(chunk, sizeof(err), &err);
180 /* A helper to initialize an op error inside a
181 * provided chunk, as most cause codes will be embedded inside an
182 * abort chunk. Differs from sctp_init_cause in that it won't oops
183 * if there isn't enough space in the op error chunk
185 static int sctp_init_cause_fixed(struct sctp_chunk *chunk, __be16 cause_code,
188 struct sctp_errhdr err;
191 /* Cause code constants are now defined in network order. */
192 err.cause = cause_code;
193 len = sizeof(err) + paylen;
194 err.length = htons(len);
196 if (skb_tailroom(chunk->skb) < len)
199 chunk->subh.err_hdr = sctp_addto_chunk_fixed(chunk, sizeof(err), &err);
203 /* 3.3.2 Initiation (INIT) (1)
205 * This chunk is used to initiate a SCTP association between two
206 * endpoints. The format of the INIT chunk is shown below:
209 * 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
210 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
211 * | Type = 1 | Chunk Flags | Chunk Length |
212 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
214 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
215 * | Advertised Receiver Window Credit (a_rwnd) |
216 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
217 * | Number of Outbound Streams | Number of Inbound Streams |
218 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
220 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
222 * / Optional/Variable-Length Parameters /
224 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
227 * The INIT chunk contains the following parameters. Unless otherwise
228 * noted, each parameter MUST only be included once in the INIT chunk.
230 * Fixed Parameters Status
231 * ----------------------------------------------
232 * Initiate Tag Mandatory
233 * Advertised Receiver Window Credit Mandatory
234 * Number of Outbound Streams Mandatory
235 * Number of Inbound Streams Mandatory
236 * Initial TSN Mandatory
238 * Variable Parameters Status Type Value
239 * -------------------------------------------------------------
240 * IPv4 Address (Note 1) Optional 5
241 * IPv6 Address (Note 1) Optional 6
242 * Cookie Preservative Optional 9
243 * Reserved for ECN Capable (Note 2) Optional 32768 (0x8000)
244 * Host Name Address (Note 3) Optional 11
245 * Supported Address Types (Note 4) Optional 12
247 struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
248 const struct sctp_bind_addr *bp,
249 gfp_t gfp, int vparam_len)
251 struct net *net = sock_net(asoc->base.sk);
252 struct sctp_supported_ext_param ext_param;
253 struct sctp_adaptation_ind_param aiparam;
254 struct sctp_paramhdr *auth_chunks = NULL;
255 struct sctp_paramhdr *auth_hmacs = NULL;
256 struct sctp_supported_addrs_param sat;
257 struct sctp_endpoint *ep = asoc->ep;
258 struct sctp_chunk *retval = NULL;
259 int num_types, addrs_len = 0;
260 struct sctp_inithdr init;
261 union sctp_params addrs;
262 struct sctp_sock *sp;
268 /* RFC 2960 3.3.2 Initiation (INIT) (1)
270 * Note 1: The INIT chunks can contain multiple addresses that
271 * can be IPv4 and/or IPv6 in any combination.
274 /* Convert the provided bind address list to raw format. */
275 addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, gfp);
277 init.init_tag = htonl(asoc->c.my_vtag);
278 init.a_rwnd = htonl(asoc->rwnd);
279 init.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
280 init.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
281 init.initial_tsn = htonl(asoc->c.initial_tsn);
283 /* How many address types are needed? */
284 sp = sctp_sk(asoc->base.sk);
285 num_types = sp->pf->supported_addrs(sp, types);
287 chunksize = sizeof(init) + addrs_len;
288 chunksize += SCTP_PAD4(SCTP_SAT_LEN(num_types));
289 chunksize += sizeof(ecap_param);
291 if (asoc->prsctp_enable)
292 chunksize += sizeof(prsctp_param);
294 /* ADDIP: Section 4.2.7:
295 * An implementation supporting this extension [ADDIP] MUST list
296 * the ASCONF,the ASCONF-ACK, and the AUTH chunks in its INIT and
297 * INIT-ACK parameters.
299 if (net->sctp.addip_enable) {
300 extensions[num_ext] = SCTP_CID_ASCONF;
301 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
305 if (asoc->reconf_enable) {
306 extensions[num_ext] = SCTP_CID_RECONF;
310 if (sp->adaptation_ind)
311 chunksize += sizeof(aiparam);
313 if (sp->strm_interleave) {
314 extensions[num_ext] = SCTP_CID_I_DATA;
318 chunksize += vparam_len;
320 /* Account for AUTH related parameters */
321 if (ep->auth_enable) {
322 /* Add random parameter length*/
323 chunksize += sizeof(asoc->c.auth_random);
325 /* Add HMACS parameter length if any were defined */
326 auth_hmacs = (struct sctp_paramhdr *)asoc->c.auth_hmacs;
327 if (auth_hmacs->length)
328 chunksize += SCTP_PAD4(ntohs(auth_hmacs->length));
332 /* Add CHUNKS parameter length */
333 auth_chunks = (struct sctp_paramhdr *)asoc->c.auth_chunks;
334 if (auth_chunks->length)
335 chunksize += SCTP_PAD4(ntohs(auth_chunks->length));
339 extensions[num_ext] = SCTP_CID_AUTH;
343 /* If we have any extensions to report, account for that */
345 chunksize += SCTP_PAD4(sizeof(ext_param) + num_ext);
347 /* RFC 2960 3.3.2 Initiation (INIT) (1)
349 * Note 3: An INIT chunk MUST NOT contain more than one Host
350 * Name address parameter. Moreover, the sender of the INIT
351 * MUST NOT combine any other address types with the Host Name
352 * address in the INIT. The receiver of INIT MUST ignore any
353 * other address types if the Host Name address parameter is
354 * present in the received INIT chunk.
356 * PLEASE DO NOT FIXME [This version does not support Host Name.]
359 retval = sctp_make_control(asoc, SCTP_CID_INIT, 0, chunksize, gfp);
363 retval->subh.init_hdr =
364 sctp_addto_chunk(retval, sizeof(init), &init);
365 retval->param_hdr.v =
366 sctp_addto_chunk(retval, addrs_len, addrs.v);
368 /* RFC 2960 3.3.2 Initiation (INIT) (1)
370 * Note 4: This parameter, when present, specifies all the
371 * address types the sending endpoint can support. The absence
372 * of this parameter indicates that the sending endpoint can
373 * support any address type.
375 sat.param_hdr.type = SCTP_PARAM_SUPPORTED_ADDRESS_TYPES;
376 sat.param_hdr.length = htons(SCTP_SAT_LEN(num_types));
377 sctp_addto_chunk(retval, sizeof(sat), &sat);
378 sctp_addto_chunk(retval, num_types * sizeof(__u16), &types);
380 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
382 /* Add the supported extensions parameter. Be nice and add this
383 * fist before addiding the parameters for the extensions themselves
386 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
387 ext_param.param_hdr.length = htons(sizeof(ext_param) + num_ext);
388 sctp_addto_chunk(retval, sizeof(ext_param), &ext_param);
389 sctp_addto_param(retval, num_ext, extensions);
392 if (asoc->prsctp_enable)
393 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
395 if (sp->adaptation_ind) {
396 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
397 aiparam.param_hdr.length = htons(sizeof(aiparam));
398 aiparam.adaptation_ind = htonl(sp->adaptation_ind);
399 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
402 /* Add SCTP-AUTH chunks to the parameter list */
403 if (ep->auth_enable) {
404 sctp_addto_chunk(retval, sizeof(asoc->c.auth_random),
405 asoc->c.auth_random);
407 sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
410 sctp_addto_chunk(retval, ntohs(auth_chunks->length),
418 struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
419 const struct sctp_chunk *chunk,
420 gfp_t gfp, int unkparam_len)
422 struct sctp_supported_ext_param ext_param;
423 struct sctp_adaptation_ind_param aiparam;
424 struct sctp_paramhdr *auth_chunks = NULL;
425 struct sctp_paramhdr *auth_random = NULL;
426 struct sctp_paramhdr *auth_hmacs = NULL;
427 struct sctp_chunk *retval = NULL;
428 struct sctp_cookie_param *cookie;
429 struct sctp_inithdr initack;
430 union sctp_params addrs;
431 struct sctp_sock *sp;
438 /* Note: there may be no addresses to embed. */
439 addrs = sctp_bind_addrs_to_raw(&asoc->base.bind_addr, &addrs_len, gfp);
441 initack.init_tag = htonl(asoc->c.my_vtag);
442 initack.a_rwnd = htonl(asoc->rwnd);
443 initack.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
444 initack.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
445 initack.initial_tsn = htonl(asoc->c.initial_tsn);
447 /* FIXME: We really ought to build the cookie right
448 * into the packet instead of allocating more fresh memory.
450 cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
455 /* Calculate the total size of allocation, include the reserved
456 * space for reporting unknown parameters if it is specified.
458 sp = sctp_sk(asoc->base.sk);
459 chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;
461 /* Tell peer that we'll do ECN only if peer advertised such cap. */
462 if (asoc->peer.ecn_capable)
463 chunksize += sizeof(ecap_param);
465 if (asoc->peer.prsctp_capable)
466 chunksize += sizeof(prsctp_param);
468 if (asoc->peer.asconf_capable) {
469 extensions[num_ext] = SCTP_CID_ASCONF;
470 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
474 if (asoc->peer.reconf_capable) {
475 extensions[num_ext] = SCTP_CID_RECONF;
479 if (sp->adaptation_ind)
480 chunksize += sizeof(aiparam);
482 if (asoc->intl_enable) {
483 extensions[num_ext] = SCTP_CID_I_DATA;
487 if (asoc->peer.auth_capable) {
488 auth_random = (struct sctp_paramhdr *)asoc->c.auth_random;
489 chunksize += ntohs(auth_random->length);
491 auth_hmacs = (struct sctp_paramhdr *)asoc->c.auth_hmacs;
492 if (auth_hmacs->length)
493 chunksize += SCTP_PAD4(ntohs(auth_hmacs->length));
497 auth_chunks = (struct sctp_paramhdr *)asoc->c.auth_chunks;
498 if (auth_chunks->length)
499 chunksize += SCTP_PAD4(ntohs(auth_chunks->length));
503 extensions[num_ext] = SCTP_CID_AUTH;
508 chunksize += SCTP_PAD4(sizeof(ext_param) + num_ext);
510 /* Now allocate and fill out the chunk. */
511 retval = sctp_make_control(asoc, SCTP_CID_INIT_ACK, 0, chunksize, gfp);
515 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
517 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
518 * HEARTBEAT ACK, * etc.) to the same destination transport
519 * address from which it received the DATA or control chunk
520 * to which it is replying.
522 * [INIT ACK back to where the INIT came from.]
524 retval->transport = chunk->transport;
526 retval->subh.init_hdr =
527 sctp_addto_chunk(retval, sizeof(initack), &initack);
528 retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
529 sctp_addto_chunk(retval, cookie_len, cookie);
530 if (asoc->peer.ecn_capable)
531 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
533 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
534 ext_param.param_hdr.length = htons(sizeof(ext_param) + num_ext);
535 sctp_addto_chunk(retval, sizeof(ext_param), &ext_param);
536 sctp_addto_param(retval, num_ext, extensions);
538 if (asoc->peer.prsctp_capable)
539 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
541 if (sp->adaptation_ind) {
542 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
543 aiparam.param_hdr.length = htons(sizeof(aiparam));
544 aiparam.adaptation_ind = htonl(sp->adaptation_ind);
545 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
548 if (asoc->peer.auth_capable) {
549 sctp_addto_chunk(retval, ntohs(auth_random->length),
552 sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
555 sctp_addto_chunk(retval, ntohs(auth_chunks->length),
559 /* We need to remove the const qualifier at this point. */
560 retval->asoc = (struct sctp_association *) asoc;
569 /* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
571 * This chunk is used only during the initialization of an association.
572 * It is sent by the initiator of an association to its peer to complete
573 * the initialization process. This chunk MUST precede any DATA chunk
574 * sent within the association, but MAY be bundled with one or more DATA
575 * chunks in the same packet.
578 * 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
579 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
580 * | Type = 10 |Chunk Flags | Length |
581 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
584 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
588 * Set to zero on transmit and ignored on receipt.
590 * Length: 16 bits (unsigned integer)
592 * Set to the size of the chunk in bytes, including the 4 bytes of
593 * the chunk header and the size of the Cookie.
595 * Cookie: variable size
597 * This field must contain the exact cookie received in the
598 * State Cookie parameter from the previous INIT ACK.
600 * An implementation SHOULD make the cookie as small as possible
601 * to insure interoperability.
603 struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
604 const struct sctp_chunk *chunk)
606 struct sctp_chunk *retval;
610 cookie = asoc->peer.cookie;
611 cookie_len = asoc->peer.cookie_len;
613 /* Build a cookie echo chunk. */
614 retval = sctp_make_control(asoc, SCTP_CID_COOKIE_ECHO, 0,
615 cookie_len, GFP_ATOMIC);
618 retval->subh.cookie_hdr =
619 sctp_addto_chunk(retval, cookie_len, cookie);
621 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
623 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
624 * HEARTBEAT ACK, * etc.) to the same destination transport
625 * address from which it * received the DATA or control chunk
626 * to which it is replying.
628 * [COOKIE ECHO back to where the INIT ACK came from.]
631 retval->transport = chunk->transport;
637 /* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
639 * This chunk is used only during the initialization of an
640 * association. It is used to acknowledge the receipt of a COOKIE
641 * ECHO chunk. This chunk MUST precede any DATA or SACK chunk sent
642 * within the association, but MAY be bundled with one or more DATA
643 * chunks or SACK chunk in the same SCTP packet.
646 * 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
647 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
648 * | Type = 11 |Chunk Flags | Length = 4 |
649 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
651 * Chunk Flags: 8 bits
653 * Set to zero on transmit and ignored on receipt.
655 struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
656 const struct sctp_chunk *chunk)
658 struct sctp_chunk *retval;
660 retval = sctp_make_control(asoc, SCTP_CID_COOKIE_ACK, 0, 0, GFP_ATOMIC);
662 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
664 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
665 * HEARTBEAT ACK, * etc.) to the same destination transport
666 * address from which it * received the DATA or control chunk
667 * to which it is replying.
669 * [COOKIE ACK back to where the COOKIE ECHO came from.]
672 retval->transport = chunk->transport;
678 * Appendix A: Explicit Congestion Notification:
681 * RFC 2481 details a specific bit for a sender to send in the header of
682 * its next outbound TCP segment to indicate to its peer that it has
683 * reduced its congestion window. This is termed the CWR bit. For
684 * SCTP the same indication is made by including the CWR chunk.
685 * This chunk contains one data element, i.e. the TSN number that
686 * was sent in the ECNE chunk. This element represents the lowest
687 * TSN number in the datagram that was originally marked with the
691 * 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
692 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
693 * | Chunk Type=13 | Flags=00000000| Chunk Length = 8 |
694 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
695 * | Lowest TSN Number |
696 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
698 * Note: The CWR is considered a Control chunk.
700 struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
701 const __u32 lowest_tsn,
702 const struct sctp_chunk *chunk)
704 struct sctp_chunk *retval;
705 struct sctp_cwrhdr cwr;
707 cwr.lowest_tsn = htonl(lowest_tsn);
708 retval = sctp_make_control(asoc, SCTP_CID_ECN_CWR, 0,
709 sizeof(cwr), GFP_ATOMIC);
714 retval->subh.ecn_cwr_hdr =
715 sctp_addto_chunk(retval, sizeof(cwr), &cwr);
717 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
719 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
720 * HEARTBEAT ACK, * etc.) to the same destination transport
721 * address from which it * received the DATA or control chunk
722 * to which it is replying.
724 * [Report a reduced congestion window back to where the ECNE
728 retval->transport = chunk->transport;
734 /* Make an ECNE chunk. This is a congestion experienced report. */
735 struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
736 const __u32 lowest_tsn)
738 struct sctp_chunk *retval;
739 struct sctp_ecnehdr ecne;
741 ecne.lowest_tsn = htonl(lowest_tsn);
742 retval = sctp_make_control(asoc, SCTP_CID_ECN_ECNE, 0,
743 sizeof(ecne), GFP_ATOMIC);
746 retval->subh.ecne_hdr =
747 sctp_addto_chunk(retval, sizeof(ecne), &ecne);
753 /* Make a DATA chunk for the given association from the provided
754 * parameters. However, do not populate the data payload.
756 struct sctp_chunk *sctp_make_datafrag_empty(const struct sctp_association *asoc,
757 const struct sctp_sndrcvinfo *sinfo,
758 int len, __u8 flags, gfp_t gfp)
760 struct sctp_chunk *retval;
761 struct sctp_datahdr dp;
763 /* We assign the TSN as LATE as possible, not here when
764 * creating the chunk.
766 memset(&dp, 0, sizeof(dp));
767 dp.ppid = sinfo->sinfo_ppid;
768 dp.stream = htons(sinfo->sinfo_stream);
770 /* Set the flags for an unordered send. */
771 if (sinfo->sinfo_flags & SCTP_UNORDERED)
772 flags |= SCTP_DATA_UNORDERED;
774 retval = sctp_make_data(asoc, flags, sizeof(dp) + len, gfp);
778 retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
779 memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
784 /* Create a selective ackowledgement (SACK) for the given
785 * association. This reports on which TSN's we've seen to date,
786 * including duplicates and gaps.
788 struct sctp_chunk *sctp_make_sack(struct sctp_association *asoc)
790 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
791 struct sctp_gap_ack_block gabs[SCTP_MAX_GABS];
792 __u16 num_gabs, num_dup_tsns;
793 struct sctp_transport *trans;
794 struct sctp_chunk *retval;
795 struct sctp_sackhdr sack;
799 memset(gabs, 0, sizeof(gabs));
800 ctsn = sctp_tsnmap_get_ctsn(map);
802 pr_debug("%s: sackCTSNAck sent:0x%x\n", __func__, ctsn);
804 /* How much room is needed in the chunk? */
805 num_gabs = sctp_tsnmap_num_gabs(map, gabs);
806 num_dup_tsns = sctp_tsnmap_num_dups(map);
808 /* Initialize the SACK header. */
809 sack.cum_tsn_ack = htonl(ctsn);
810 sack.a_rwnd = htonl(asoc->a_rwnd);
811 sack.num_gap_ack_blocks = htons(num_gabs);
812 sack.num_dup_tsns = htons(num_dup_tsns);
815 + sizeof(struct sctp_gap_ack_block) * num_gabs
816 + sizeof(__u32) * num_dup_tsns;
818 /* Create the chunk. */
819 retval = sctp_make_control(asoc, SCTP_CID_SACK, 0, len, GFP_ATOMIC);
823 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
825 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
826 * HEARTBEAT ACK, etc.) to the same destination transport
827 * address from which it received the DATA or control chunk to
828 * which it is replying. This rule should also be followed if
829 * the endpoint is bundling DATA chunks together with the
832 * However, when acknowledging multiple DATA chunks received
833 * in packets from different source addresses in a single
834 * SACK, the SACK chunk may be transmitted to one of the
835 * destination transport addresses from which the DATA or
836 * control chunks being acknowledged were received.
838 * [BUG: We do not implement the following paragraph.
839 * Perhaps we should remember the last transport we used for a
840 * SACK and avoid that (if possible) if we have seen any
841 * duplicates. --piggy]
843 * When a receiver of a duplicate DATA chunk sends a SACK to a
844 * multi- homed endpoint it MAY be beneficial to vary the
845 * destination address and not use the source address of the
846 * DATA chunk. The reason being that receiving a duplicate
847 * from a multi-homed endpoint might indicate that the return
848 * path (as specified in the source address of the DATA chunk)
849 * for the SACK is broken.
851 * [Send to the address from which we last received a DATA chunk.]
853 retval->transport = asoc->peer.last_data_from;
855 retval->subh.sack_hdr =
856 sctp_addto_chunk(retval, sizeof(sack), &sack);
858 /* Add the gap ack block information. */
860 sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
863 /* Add the duplicate TSN information. */
865 asoc->stats.idupchunks += num_dup_tsns;
866 sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
867 sctp_tsnmap_get_dups(map));
869 /* Once we have a sack generated, check to see what our sack
870 * generation is, if its 0, reset the transports to 0, and reset
871 * the association generation to 1
873 * The idea is that zero is never used as a valid generation for the
874 * association so no transport will match after a wrap event like this,
875 * Until the next sack
877 if (++asoc->peer.sack_generation == 0) {
878 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
880 trans->sack_generation = 0;
881 asoc->peer.sack_generation = 1;
887 /* Make a SHUTDOWN chunk. */
888 struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
889 const struct sctp_chunk *chunk)
891 struct sctp_shutdownhdr shut;
892 struct sctp_chunk *retval;
895 ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
896 shut.cum_tsn_ack = htonl(ctsn);
898 retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN, 0,
899 sizeof(shut), GFP_ATOMIC);
903 retval->subh.shutdown_hdr =
904 sctp_addto_chunk(retval, sizeof(shut), &shut);
907 retval->transport = chunk->transport;
912 struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
913 const struct sctp_chunk *chunk)
915 struct sctp_chunk *retval;
917 retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0,
920 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
922 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
923 * HEARTBEAT ACK, * etc.) to the same destination transport
924 * address from which it * received the DATA or control chunk
925 * to which it is replying.
927 * [ACK back to where the SHUTDOWN came from.]
930 retval->transport = chunk->transport;
935 struct sctp_chunk *sctp_make_shutdown_complete(
936 const struct sctp_association *asoc,
937 const struct sctp_chunk *chunk)
939 struct sctp_chunk *retval;
942 /* Set the T-bit if we have no association (vtag will be
945 flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
947 retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags,
950 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
952 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
953 * HEARTBEAT ACK, * etc.) to the same destination transport
954 * address from which it * received the DATA or control chunk
955 * to which it is replying.
957 * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
961 retval->transport = chunk->transport;
966 /* Create an ABORT. Note that we set the T bit if we have no
967 * association, except when responding to an INIT (sctpimpguide 2.41).
969 struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
970 const struct sctp_chunk *chunk,
973 struct sctp_chunk *retval;
976 /* Set the T-bit if we have no association and 'chunk' is not
977 * an INIT (vtag will be reflected).
980 if (chunk && chunk->chunk_hdr &&
981 chunk->chunk_hdr->type == SCTP_CID_INIT)
984 flags = SCTP_CHUNK_FLAG_T;
987 retval = sctp_make_control(asoc, SCTP_CID_ABORT, flags, hint,
990 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
992 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
993 * HEARTBEAT ACK, * etc.) to the same destination transport
994 * address from which it * received the DATA or control chunk
995 * to which it is replying.
997 * [ABORT back to where the offender came from.]
1000 retval->transport = chunk->transport;
1005 /* Helper to create ABORT with a NO_USER_DATA error. */
1006 struct sctp_chunk *sctp_make_abort_no_data(
1007 const struct sctp_association *asoc,
1008 const struct sctp_chunk *chunk,
1011 struct sctp_chunk *retval;
1014 retval = sctp_make_abort(asoc, chunk,
1015 sizeof(struct sctp_errhdr) + sizeof(tsn));
1020 /* Put the tsn back into network byte order. */
1021 payload = htonl(tsn);
1022 sctp_init_cause(retval, SCTP_ERROR_NO_DATA, sizeof(payload));
1023 sctp_addto_chunk(retval, sizeof(payload), (const void *)&payload);
1025 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1027 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1028 * HEARTBEAT ACK, * etc.) to the same destination transport
1029 * address from which it * received the DATA or control chunk
1030 * to which it is replying.
1032 * [ABORT back to where the offender came from.]
1035 retval->transport = chunk->transport;
1041 /* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error. */
1042 struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
1046 struct sctp_chunk *retval;
1047 void *payload = NULL;
1050 retval = sctp_make_abort(asoc, NULL,
1051 sizeof(struct sctp_errhdr) + paylen);
1056 /* Put the msg_iov together into payload. */
1057 payload = kmalloc(paylen, GFP_KERNEL);
1061 err = memcpy_from_msg(payload, msg, paylen);
1066 sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, paylen);
1067 sctp_addto_chunk(retval, paylen, payload);
1077 sctp_chunk_free(retval);
1083 /* Append bytes to the end of a parameter. Will panic if chunk is not big
1086 static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
1089 int chunklen = ntohs(chunk->chunk_hdr->length);
1092 target = skb_put(chunk->skb, len);
1095 memcpy(target, data, len);
1097 memset(target, 0, len);
1099 /* Adjust the chunk length field. */
1100 chunk->chunk_hdr->length = htons(chunklen + len);
1101 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1106 /* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
1107 struct sctp_chunk *sctp_make_abort_violation(
1108 const struct sctp_association *asoc,
1109 const struct sctp_chunk *chunk,
1110 const __u8 *payload,
1111 const size_t paylen)
1113 struct sctp_chunk *retval;
1114 struct sctp_paramhdr phdr;
1116 retval = sctp_make_abort(asoc, chunk, sizeof(struct sctp_errhdr) +
1117 paylen + sizeof(phdr));
1121 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, paylen +
1124 phdr.type = htons(chunk->chunk_hdr->type);
1125 phdr.length = chunk->chunk_hdr->length;
1126 sctp_addto_chunk(retval, paylen, payload);
1127 sctp_addto_param(retval, sizeof(phdr), &phdr);
1133 struct sctp_chunk *sctp_make_violation_paramlen(
1134 const struct sctp_association *asoc,
1135 const struct sctp_chunk *chunk,
1136 struct sctp_paramhdr *param)
1138 static const char error[] = "The following parameter had invalid length:";
1139 size_t payload_len = sizeof(error) + sizeof(struct sctp_errhdr) +
1141 struct sctp_chunk *retval;
1143 retval = sctp_make_abort(asoc, chunk, payload_len);
1147 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION,
1148 sizeof(error) + sizeof(*param));
1149 sctp_addto_chunk(retval, sizeof(error), error);
1150 sctp_addto_param(retval, sizeof(*param), param);
1156 struct sctp_chunk *sctp_make_violation_max_retrans(
1157 const struct sctp_association *asoc,
1158 const struct sctp_chunk *chunk)
1160 static const char error[] = "Association exceeded its max_retans count";
1161 size_t payload_len = sizeof(error) + sizeof(struct sctp_errhdr);
1162 struct sctp_chunk *retval;
1164 retval = sctp_make_abort(asoc, chunk, payload_len);
1168 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, sizeof(error));
1169 sctp_addto_chunk(retval, sizeof(error), error);
1175 /* Make a HEARTBEAT chunk. */
1176 struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
1177 const struct sctp_transport *transport)
1179 struct sctp_sender_hb_info hbinfo;
1180 struct sctp_chunk *retval;
1182 retval = sctp_make_control(asoc, SCTP_CID_HEARTBEAT, 0,
1183 sizeof(hbinfo), GFP_ATOMIC);
1188 hbinfo.param_hdr.type = SCTP_PARAM_HEARTBEAT_INFO;
1189 hbinfo.param_hdr.length = htons(sizeof(hbinfo));
1190 hbinfo.daddr = transport->ipaddr;
1191 hbinfo.sent_at = jiffies;
1192 hbinfo.hb_nonce = transport->hb_nonce;
1194 /* Cast away the 'const', as this is just telling the chunk
1195 * what transport it belongs to.
1197 retval->transport = (struct sctp_transport *) transport;
1198 retval->subh.hbs_hdr = sctp_addto_chunk(retval, sizeof(hbinfo),
1205 struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
1206 const struct sctp_chunk *chunk,
1207 const void *payload,
1208 const size_t paylen)
1210 struct sctp_chunk *retval;
1212 retval = sctp_make_control(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen,
1217 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
1219 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1221 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1222 * HEARTBEAT ACK, * etc.) to the same destination transport
1223 * address from which it * received the DATA or control chunk
1224 * to which it is replying.
1226 * [HBACK back to where the HEARTBEAT came from.]
1229 retval->transport = chunk->transport;
1235 /* Create an Operation Error chunk with the specified space reserved.
1236 * This routine can be used for containing multiple causes in the chunk.
1238 static struct sctp_chunk *sctp_make_op_error_space(
1239 const struct sctp_association *asoc,
1240 const struct sctp_chunk *chunk,
1243 struct sctp_chunk *retval;
1245 retval = sctp_make_control(asoc, SCTP_CID_ERROR, 0,
1246 sizeof(struct sctp_errhdr) + size,
1251 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1253 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1254 * HEARTBEAT ACK, etc.) to the same destination transport
1255 * address from which it received the DATA or control chunk
1256 * to which it is replying.
1260 retval->transport = chunk->transport;
1266 /* Create an Operation Error chunk of a fixed size,
1267 * specifically, max(asoc->pathmtu, SCTP_DEFAULT_MAXSEGMENT)
1268 * This is a helper function to allocate an error chunk for
1269 * for those invalid parameter codes in which we may not want
1270 * to report all the errors, if the incoming chunk is large
1272 static inline struct sctp_chunk *sctp_make_op_error_fixed(
1273 const struct sctp_association *asoc,
1274 const struct sctp_chunk *chunk)
1276 size_t size = asoc ? asoc->pathmtu : 0;
1279 size = SCTP_DEFAULT_MAXSEGMENT;
1281 return sctp_make_op_error_space(asoc, chunk, size);
1284 /* Create an Operation Error chunk. */
1285 struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
1286 const struct sctp_chunk *chunk,
1287 __be16 cause_code, const void *payload,
1288 size_t paylen, size_t reserve_tail)
1290 struct sctp_chunk *retval;
1292 retval = sctp_make_op_error_space(asoc, chunk, paylen + reserve_tail);
1296 sctp_init_cause(retval, cause_code, paylen + reserve_tail);
1297 sctp_addto_chunk(retval, paylen, payload);
1299 sctp_addto_param(retval, reserve_tail, NULL);
1305 struct sctp_chunk *sctp_make_auth(const struct sctp_association *asoc,
1308 struct sctp_authhdr auth_hdr;
1309 struct sctp_hmac *hmac_desc;
1310 struct sctp_chunk *retval;
1312 /* Get the first hmac that the peer told us to use */
1313 hmac_desc = sctp_auth_asoc_get_hmac(asoc);
1314 if (unlikely(!hmac_desc))
1317 retval = sctp_make_control(asoc, SCTP_CID_AUTH, 0,
1318 hmac_desc->hmac_len + sizeof(auth_hdr),
1323 auth_hdr.hmac_id = htons(hmac_desc->hmac_id);
1324 auth_hdr.shkey_id = htons(key_id);
1326 retval->subh.auth_hdr = sctp_addto_chunk(retval, sizeof(auth_hdr),
1329 skb_put_zero(retval->skb, hmac_desc->hmac_len);
1331 /* Adjust the chunk header to include the empty MAC */
1332 retval->chunk_hdr->length =
1333 htons(ntohs(retval->chunk_hdr->length) + hmac_desc->hmac_len);
1334 retval->chunk_end = skb_tail_pointer(retval->skb);
1340 /********************************************************************
1341 * 2nd Level Abstractions
1342 ********************************************************************/
1344 /* Turn an skb into a chunk.
1345 * FIXME: Eventually move the structure directly inside the skb->cb[].
1347 * sctpimpguide-05.txt Section 2.8.2
1348 * M1) Each time a new DATA chunk is transmitted
1349 * set the 'TSN.Missing.Report' count for that TSN to 0. The
1350 * 'TSN.Missing.Report' count will be used to determine missing chunks
1351 * and when to fast retransmit.
1354 struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
1355 const struct sctp_association *asoc,
1356 struct sock *sk, gfp_t gfp)
1358 struct sctp_chunk *retval;
1360 retval = kmem_cache_zalloc(sctp_chunk_cachep, gfp);
1365 pr_debug("%s: chunkifying skb:%p w/o an sk\n", __func__, skb);
1367 INIT_LIST_HEAD(&retval->list);
1369 retval->asoc = (struct sctp_association *)asoc;
1370 retval->singleton = 1;
1372 retval->fast_retransmit = SCTP_CAN_FRTX;
1374 /* Polish the bead hole. */
1375 INIT_LIST_HEAD(&retval->transmitted_list);
1376 INIT_LIST_HEAD(&retval->frag_list);
1377 SCTP_DBG_OBJCNT_INC(chunk);
1378 refcount_set(&retval->refcnt, 1);
1384 /* Set chunk->source and dest based on the IP header in chunk->skb. */
1385 void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
1386 union sctp_addr *dest)
1388 memcpy(&chunk->source, src, sizeof(union sctp_addr));
1389 memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
1392 /* Extract the source address from a chunk. */
1393 const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
1395 /* If we have a known transport, use that. */
1396 if (chunk->transport) {
1397 return &chunk->transport->ipaddr;
1399 /* Otherwise, extract it from the IP header. */
1400 return &chunk->source;
1404 /* Create a new chunk, setting the type and flags headers from the
1405 * arguments, reserving enough space for a 'paylen' byte payload.
1407 static struct sctp_chunk *_sctp_make_chunk(const struct sctp_association *asoc,
1408 __u8 type, __u8 flags, int paylen,
1411 struct sctp_chunkhdr *chunk_hdr;
1412 struct sctp_chunk *retval;
1413 struct sk_buff *skb;
1417 chunklen = SCTP_PAD4(sizeof(*chunk_hdr) + paylen);
1418 if (chunklen > SCTP_MAX_CHUNK_LEN)
1421 /* No need to allocate LL here, as this is only a chunk. */
1422 skb = alloc_skb(chunklen, gfp);
1426 /* Make room for the chunk header. */
1427 chunk_hdr = (struct sctp_chunkhdr *)skb_put(skb, sizeof(*chunk_hdr));
1428 chunk_hdr->type = type;
1429 chunk_hdr->flags = flags;
1430 chunk_hdr->length = htons(sizeof(*chunk_hdr));
1432 sk = asoc ? asoc->base.sk : NULL;
1433 retval = sctp_chunkify(skb, asoc, sk, gfp);
1439 retval->chunk_hdr = chunk_hdr;
1440 retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(*chunk_hdr);
1442 /* Determine if the chunk needs to be authenticated */
1443 if (sctp_auth_send_cid(type, asoc))
1451 static struct sctp_chunk *sctp_make_data(const struct sctp_association *asoc,
1452 __u8 flags, int paylen, gfp_t gfp)
1454 return _sctp_make_chunk(asoc, SCTP_CID_DATA, flags, paylen, gfp);
1457 struct sctp_chunk *sctp_make_idata(const struct sctp_association *asoc,
1458 __u8 flags, int paylen, gfp_t gfp)
1460 return _sctp_make_chunk(asoc, SCTP_CID_I_DATA, flags, paylen, gfp);
1463 static struct sctp_chunk *sctp_make_control(const struct sctp_association *asoc,
1464 __u8 type, __u8 flags, int paylen,
1467 struct sctp_chunk *chunk;
1469 chunk = _sctp_make_chunk(asoc, type, flags, paylen, gfp);
1471 sctp_control_set_owner_w(chunk);
1476 /* Release the memory occupied by a chunk. */
1477 static void sctp_chunk_destroy(struct sctp_chunk *chunk)
1479 BUG_ON(!list_empty(&chunk->list));
1480 list_del_init(&chunk->transmitted_list);
1482 consume_skb(chunk->skb);
1483 consume_skb(chunk->auth_chunk);
1485 SCTP_DBG_OBJCNT_DEC(chunk);
1486 kmem_cache_free(sctp_chunk_cachep, chunk);
1489 /* Possibly, free the chunk. */
1490 void sctp_chunk_free(struct sctp_chunk *chunk)
1492 /* Release our reference on the message tracker. */
1494 sctp_datamsg_put(chunk->msg);
1496 sctp_chunk_put(chunk);
1499 /* Grab a reference to the chunk. */
1500 void sctp_chunk_hold(struct sctp_chunk *ch)
1502 refcount_inc(&ch->refcnt);
1505 /* Release a reference to the chunk. */
1506 void sctp_chunk_put(struct sctp_chunk *ch)
1508 if (refcount_dec_and_test(&ch->refcnt))
1509 sctp_chunk_destroy(ch);
1512 /* Append bytes to the end of a chunk. Will panic if chunk is not big
1515 void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
1517 int chunklen = ntohs(chunk->chunk_hdr->length);
1518 int padlen = SCTP_PAD4(chunklen) - chunklen;
1521 skb_put_zero(chunk->skb, padlen);
1522 target = skb_put_data(chunk->skb, data, len);
1524 /* Adjust the chunk length field. */
1525 chunk->chunk_hdr->length = htons(chunklen + padlen + len);
1526 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1531 /* Append bytes to the end of a chunk. Returns NULL if there isn't sufficient
1532 * space in the chunk
1534 static void *sctp_addto_chunk_fixed(struct sctp_chunk *chunk,
1535 int len, const void *data)
1537 if (skb_tailroom(chunk->skb) >= len)
1538 return sctp_addto_chunk(chunk, len, data);
1543 /* Append bytes from user space to the end of a chunk. Will panic if
1544 * chunk is not big enough.
1545 * Returns a kernel err value.
1547 int sctp_user_addto_chunk(struct sctp_chunk *chunk, int len,
1548 struct iov_iter *from)
1552 /* Make room in chunk for data. */
1553 target = skb_put(chunk->skb, len);
1555 /* Copy data (whole iovec) into chunk */
1556 if (!copy_from_iter_full(target, len, from))
1559 /* Adjust the chunk length field. */
1560 chunk->chunk_hdr->length =
1561 htons(ntohs(chunk->chunk_hdr->length) + len);
1562 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1567 /* Helper function to assign a TSN if needed. This assumes that both
1568 * the data_hdr and association have already been assigned.
1570 void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
1572 struct sctp_stream *stream;
1573 struct sctp_chunk *lchunk;
1574 struct sctp_datamsg *msg;
1580 /* All fragments will be on the same stream */
1581 sid = ntohs(chunk->subh.data_hdr->stream);
1582 stream = &chunk->asoc->stream;
1584 /* Now assign the sequence number to the entire message.
1585 * All fragments must have the same stream sequence number.
1588 list_for_each_entry(lchunk, &msg->chunks, frag_list) {
1589 if (lchunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
1592 if (lchunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
1593 ssn = sctp_ssn_next(stream, out, sid);
1595 ssn = sctp_ssn_peek(stream, out, sid);
1598 lchunk->subh.data_hdr->ssn = htons(ssn);
1599 lchunk->has_ssn = 1;
1603 /* Helper function to assign a TSN if needed. This assumes that both
1604 * the data_hdr and association have already been assigned.
1606 void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
1608 if (!chunk->has_tsn) {
1609 /* This is the last possible instant to
1612 chunk->subh.data_hdr->tsn =
1613 htonl(sctp_association_get_next_tsn(chunk->asoc));
1618 /* Create a CLOSED association to use with an incoming packet. */
1619 struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
1620 struct sctp_chunk *chunk,
1623 struct sctp_association *asoc;
1624 enum sctp_scope scope;
1625 struct sk_buff *skb;
1627 /* Create the bare association. */
1628 scope = sctp_scope(sctp_source(chunk));
1629 asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
1634 /* Create an entry for the source address of the packet. */
1635 SCTP_INPUT_CB(skb)->af->from_skb(&asoc->c.peer_addr, skb, 1);
1641 /* Build a cookie representing asoc.
1642 * This INCLUDES the param header needed to put the cookie in the INIT ACK.
1644 static struct sctp_cookie_param *sctp_pack_cookie(
1645 const struct sctp_endpoint *ep,
1646 const struct sctp_association *asoc,
1647 const struct sctp_chunk *init_chunk,
1648 int *cookie_len, const __u8 *raw_addrs,
1651 struct sctp_signed_cookie *cookie;
1652 struct sctp_cookie_param *retval;
1653 int headersize, bodysize;
1655 /* Header size is static data prior to the actual cookie, including
1658 headersize = sizeof(struct sctp_paramhdr) +
1659 (sizeof(struct sctp_signed_cookie) -
1660 sizeof(struct sctp_cookie));
1661 bodysize = sizeof(struct sctp_cookie)
1662 + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
1664 /* Pad out the cookie to a multiple to make the signature
1665 * functions simpler to write.
1667 if (bodysize % SCTP_COOKIE_MULTIPLE)
1668 bodysize += SCTP_COOKIE_MULTIPLE
1669 - (bodysize % SCTP_COOKIE_MULTIPLE);
1670 *cookie_len = headersize + bodysize;
1672 /* Clear this memory since we are sending this data structure
1673 * out on the network.
1675 retval = kzalloc(*cookie_len, GFP_ATOMIC);
1679 cookie = (struct sctp_signed_cookie *) retval->body;
1681 /* Set up the parameter header. */
1682 retval->p.type = SCTP_PARAM_STATE_COOKIE;
1683 retval->p.length = htons(*cookie_len);
1685 /* Copy the cookie part of the association itself. */
1686 cookie->c = asoc->c;
1687 /* Save the raw address list length in the cookie. */
1688 cookie->c.raw_addr_list_len = addrs_len;
1690 /* Remember PR-SCTP capability. */
1691 cookie->c.prsctp_capable = asoc->peer.prsctp_capable;
1693 /* Save adaptation indication in the cookie. */
1694 cookie->c.adaptation_ind = asoc->peer.adaptation_ind;
1696 /* Set an expiration time for the cookie. */
1697 cookie->c.expiration = ktime_add(asoc->cookie_life,
1700 /* Copy the peer's init packet. */
1701 memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
1702 ntohs(init_chunk->chunk_hdr->length));
1704 /* Copy the raw local address list of the association. */
1705 memcpy((__u8 *)&cookie->c.peer_init[0] +
1706 ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
1708 if (sctp_sk(ep->base.sk)->hmac) {
1709 SHASH_DESC_ON_STACK(desc, sctp_sk(ep->base.sk)->hmac);
1712 /* Sign the message. */
1713 desc->tfm = sctp_sk(ep->base.sk)->hmac;
1716 err = crypto_shash_setkey(desc->tfm, ep->secret_key,
1717 sizeof(ep->secret_key)) ?:
1718 crypto_shash_digest(desc, (u8 *)&cookie->c, bodysize,
1720 shash_desc_zero(desc);
1734 /* Unpack the cookie from COOKIE ECHO chunk, recreating the association. */
1735 struct sctp_association *sctp_unpack_cookie(
1736 const struct sctp_endpoint *ep,
1737 const struct sctp_association *asoc,
1738 struct sctp_chunk *chunk, gfp_t gfp,
1739 int *error, struct sctp_chunk **errp)
1741 struct sctp_association *retval = NULL;
1742 int headersize, bodysize, fixed_size;
1743 struct sctp_signed_cookie *cookie;
1744 struct sk_buff *skb = chunk->skb;
1745 struct sctp_cookie *bear_cookie;
1746 __u8 *digest = ep->digest;
1747 enum sctp_scope scope;
1751 /* Header size is static data prior to the actual cookie, including
1754 headersize = sizeof(struct sctp_chunkhdr) +
1755 (sizeof(struct sctp_signed_cookie) -
1756 sizeof(struct sctp_cookie));
1757 bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
1758 fixed_size = headersize + sizeof(struct sctp_cookie);
1760 /* Verify that the chunk looks like it even has a cookie.
1761 * There must be enough room for our cookie and our peer's
1764 len = ntohs(chunk->chunk_hdr->length);
1765 if (len < fixed_size + sizeof(struct sctp_chunkhdr))
1768 /* Verify that the cookie has been padded out. */
1769 if (bodysize % SCTP_COOKIE_MULTIPLE)
1772 /* Process the cookie. */
1773 cookie = chunk->subh.cookie_hdr;
1774 bear_cookie = &cookie->c;
1776 if (!sctp_sk(ep->base.sk)->hmac)
1779 /* Check the signature. */
1781 SHASH_DESC_ON_STACK(desc, sctp_sk(ep->base.sk)->hmac);
1784 desc->tfm = sctp_sk(ep->base.sk)->hmac;
1787 err = crypto_shash_setkey(desc->tfm, ep->secret_key,
1788 sizeof(ep->secret_key)) ?:
1789 crypto_shash_digest(desc, (u8 *)bear_cookie, bodysize,
1791 shash_desc_zero(desc);
1794 *error = -SCTP_IERROR_NOMEM;
1799 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1800 *error = -SCTP_IERROR_BAD_SIG;
1805 /* IG Section 2.35.2:
1806 * 3) Compare the port numbers and the verification tag contained
1807 * within the COOKIE ECHO chunk to the actual port numbers and the
1808 * verification tag within the SCTP common header of the received
1809 * packet. If these values do not match the packet MUST be silently
1812 if (ntohl(chunk->sctp_hdr->vtag) != bear_cookie->my_vtag) {
1813 *error = -SCTP_IERROR_BAD_TAG;
1817 if (chunk->sctp_hdr->source != bear_cookie->peer_addr.v4.sin_port ||
1818 ntohs(chunk->sctp_hdr->dest) != bear_cookie->my_port) {
1819 *error = -SCTP_IERROR_BAD_PORTS;
1823 /* Check to see if the cookie is stale. If there is already
1824 * an association, there is no need to check cookie's expiration
1825 * for init collision case of lost COOKIE ACK.
1826 * If skb has been timestamped, then use the stamp, otherwise
1827 * use current time. This introduces a small possibility that
1828 * that a cookie may be considered expired, but his would only slow
1829 * down the new association establishment instead of every packet.
1831 if (sock_flag(ep->base.sk, SOCK_TIMESTAMP))
1832 kt = skb_get_ktime(skb);
1834 kt = ktime_get_real();
1836 if (!asoc && ktime_before(bear_cookie->expiration, kt)) {
1838 * Section 3.3.10.3 Stale Cookie Error (3)
1842 * Stale Cookie Error: Indicates the receipt of a valid State
1843 * Cookie that has expired.
1845 len = ntohs(chunk->chunk_hdr->length);
1846 *errp = sctp_make_op_error_space(asoc, chunk, len);
1848 suseconds_t usecs = ktime_to_us(ktime_sub(kt, bear_cookie->expiration));
1849 __be32 n = htonl(usecs);
1851 sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
1853 sctp_addto_chunk(*errp, sizeof(n), &n);
1854 *error = -SCTP_IERROR_STALE_COOKIE;
1856 *error = -SCTP_IERROR_NOMEM;
1861 /* Make a new base association. */
1862 scope = sctp_scope(sctp_source(chunk));
1863 retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
1865 *error = -SCTP_IERROR_NOMEM;
1869 /* Set up our peer's port number. */
1870 retval->peer.port = ntohs(chunk->sctp_hdr->source);
1872 /* Populate the association from the cookie. */
1873 memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
1875 if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
1877 *error = -SCTP_IERROR_NOMEM;
1881 /* Also, add the destination address. */
1882 if (list_empty(&retval->base.bind_addr.address_list)) {
1883 sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest,
1884 sizeof(chunk->dest), SCTP_ADDR_SRC,
1888 retval->next_tsn = retval->c.initial_tsn;
1889 retval->ctsn_ack_point = retval->next_tsn - 1;
1890 retval->addip_serial = retval->c.initial_tsn;
1891 retval->strreset_outseq = retval->c.initial_tsn;
1892 retval->adv_peer_ack_point = retval->ctsn_ack_point;
1893 retval->peer.prsctp_capable = retval->c.prsctp_capable;
1894 retval->peer.adaptation_ind = retval->c.adaptation_ind;
1896 /* The INIT stuff will be done by the side effects. */
1901 sctp_association_free(retval);
1906 /* Yikes! The packet is either corrupt or deliberately
1909 *error = -SCTP_IERROR_MALFORMED;
1913 /********************************************************************
1914 * 3rd Level Abstractions
1915 ********************************************************************/
1917 struct __sctp_missing {
1923 * Report a missing mandatory parameter.
1925 static int sctp_process_missing_param(const struct sctp_association *asoc,
1926 enum sctp_param paramtype,
1927 struct sctp_chunk *chunk,
1928 struct sctp_chunk **errp)
1930 struct __sctp_missing report;
1933 len = SCTP_PAD4(sizeof(report));
1935 /* Make an ERROR chunk, preparing enough room for
1936 * returning multiple unknown parameters.
1939 *errp = sctp_make_op_error_space(asoc, chunk, len);
1942 report.num_missing = htonl(1);
1943 report.type = paramtype;
1944 sctp_init_cause(*errp, SCTP_ERROR_MISS_PARAM,
1946 sctp_addto_chunk(*errp, sizeof(report), &report);
1949 /* Stop processing this chunk. */
1953 /* Report an Invalid Mandatory Parameter. */
1954 static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
1955 struct sctp_chunk *chunk,
1956 struct sctp_chunk **errp)
1958 /* Invalid Mandatory Parameter Error has no payload. */
1961 *errp = sctp_make_op_error_space(asoc, chunk, 0);
1964 sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, 0);
1966 /* Stop processing this chunk. */
1970 static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
1971 struct sctp_paramhdr *param,
1972 const struct sctp_chunk *chunk,
1973 struct sctp_chunk **errp)
1975 /* This is a fatal error. Any accumulated non-fatal errors are
1979 sctp_chunk_free(*errp);
1981 /* Create an error chunk and fill it in with our payload. */
1982 *errp = sctp_make_violation_paramlen(asoc, chunk, param);
1988 /* Do not attempt to handle the HOST_NAME parm. However, do
1989 * send back an indicator to the peer.
1991 static int sctp_process_hn_param(const struct sctp_association *asoc,
1992 union sctp_params param,
1993 struct sctp_chunk *chunk,
1994 struct sctp_chunk **errp)
1996 __u16 len = ntohs(param.p->length);
1998 /* Processing of the HOST_NAME parameter will generate an
1999 * ABORT. If we've accumulated any non-fatal errors, they
2000 * would be unrecognized parameters and we should not include
2001 * them in the ABORT.
2004 sctp_chunk_free(*errp);
2006 *errp = sctp_make_op_error_space(asoc, chunk, len);
2009 sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED, len);
2010 sctp_addto_chunk(*errp, len, param.v);
2013 /* Stop processing this chunk. */
2017 static int sctp_verify_ext_param(struct net *net, union sctp_params param)
2019 __u16 num_ext = ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
2020 int have_asconf = 0;
2024 for (i = 0; i < num_ext; i++) {
2025 switch (param.ext->chunks[i]) {
2029 case SCTP_CID_ASCONF:
2030 case SCTP_CID_ASCONF_ACK:
2036 /* ADD-IP Security: The draft requires us to ABORT or ignore the
2037 * INIT/INIT-ACK if ADD-IP is listed, but AUTH is not. Do this
2038 * only if ADD-IP is turned on and we are not backward-compatible
2041 if (net->sctp.addip_noauth)
2044 if (net->sctp.addip_enable && !have_auth && have_asconf)
2050 static void sctp_process_ext_param(struct sctp_association *asoc,
2051 union sctp_params param)
2053 __u16 num_ext = ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
2054 struct net *net = sock_net(asoc->base.sk);
2057 for (i = 0; i < num_ext; i++) {
2058 switch (param.ext->chunks[i]) {
2059 case SCTP_CID_RECONF:
2060 if (asoc->reconf_enable &&
2061 !asoc->peer.reconf_capable)
2062 asoc->peer.reconf_capable = 1;
2064 case SCTP_CID_FWD_TSN:
2065 if (asoc->prsctp_enable && !asoc->peer.prsctp_capable)
2066 asoc->peer.prsctp_capable = 1;
2069 /* if the peer reports AUTH, assume that he
2072 if (asoc->ep->auth_enable)
2073 asoc->peer.auth_capable = 1;
2075 case SCTP_CID_ASCONF:
2076 case SCTP_CID_ASCONF_ACK:
2077 if (net->sctp.addip_enable)
2078 asoc->peer.asconf_capable = 1;
2080 case SCTP_CID_I_DATA:
2081 if (sctp_sk(asoc->base.sk)->strm_interleave)
2082 asoc->intl_enable = 1;
2090 /* RFC 3.2.1 & the Implementers Guide 2.2.
2092 * The Parameter Types are encoded such that the
2093 * highest-order two bits specify the action that must be
2094 * taken if the processing endpoint does not recognize the
2097 * 00 - Stop processing this parameter; do not process any further
2098 * parameters within this chunk
2100 * 01 - Stop processing this parameter, do not process any further
2101 * parameters within this chunk, and report the unrecognized
2102 * parameter in an 'Unrecognized Parameter' ERROR chunk.
2104 * 10 - Skip this parameter and continue processing.
2106 * 11 - Skip this parameter and continue processing but
2107 * report the unrecognized parameter in an
2108 * 'Unrecognized Parameter' ERROR chunk.
2111 * SCTP_IERROR_NO_ERROR - continue with the chunk
2112 * SCTP_IERROR_ERROR - stop and report an error.
2113 * SCTP_IERROR_NOMEME - out of memory.
2115 static enum sctp_ierror sctp_process_unk_param(
2116 const struct sctp_association *asoc,
2117 union sctp_params param,
2118 struct sctp_chunk *chunk,
2119 struct sctp_chunk **errp)
2121 int retval = SCTP_IERROR_NO_ERROR;
2123 switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
2124 case SCTP_PARAM_ACTION_DISCARD:
2125 retval = SCTP_IERROR_ERROR;
2127 case SCTP_PARAM_ACTION_SKIP:
2129 case SCTP_PARAM_ACTION_DISCARD_ERR:
2130 retval = SCTP_IERROR_ERROR;
2132 case SCTP_PARAM_ACTION_SKIP_ERR:
2133 /* Make an ERROR chunk, preparing enough room for
2134 * returning multiple unknown parameters.
2137 *errp = sctp_make_op_error_fixed(asoc, chunk);
2140 if (!sctp_init_cause_fixed(*errp, SCTP_ERROR_UNKNOWN_PARAM,
2141 SCTP_PAD4(ntohs(param.p->length))))
2142 sctp_addto_chunk_fixed(*errp,
2143 SCTP_PAD4(ntohs(param.p->length)),
2146 /* If there is no memory for generating the ERROR
2147 * report as specified, an ABORT will be triggered
2148 * to the peer and the association won't be
2151 retval = SCTP_IERROR_NOMEM;
2161 /* Verify variable length parameters
2163 * SCTP_IERROR_ABORT - trigger an ABORT
2164 * SCTP_IERROR_NOMEM - out of memory (abort)
2165 * SCTP_IERROR_ERROR - stop processing, trigger an ERROR
2166 * SCTP_IERROR_NO_ERROR - continue with the chunk
2168 static enum sctp_ierror sctp_verify_param(struct net *net,
2169 const struct sctp_endpoint *ep,
2170 const struct sctp_association *asoc,
2171 union sctp_params param,
2173 struct sctp_chunk *chunk,
2174 struct sctp_chunk **err_chunk)
2176 struct sctp_hmac_algo_param *hmacs;
2177 int retval = SCTP_IERROR_NO_ERROR;
2178 __u16 n_elt, id = 0;
2181 /* FIXME - This routine is not looking at each parameter per the
2182 * chunk type, i.e., unrecognized parameters should be further
2183 * identified based on the chunk id.
2186 switch (param.p->type) {
2187 case SCTP_PARAM_IPV4_ADDRESS:
2188 case SCTP_PARAM_IPV6_ADDRESS:
2189 case SCTP_PARAM_COOKIE_PRESERVATIVE:
2190 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2191 case SCTP_PARAM_STATE_COOKIE:
2192 case SCTP_PARAM_HEARTBEAT_INFO:
2193 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2194 case SCTP_PARAM_ECN_CAPABLE:
2195 case SCTP_PARAM_ADAPTATION_LAYER_IND:
2198 case SCTP_PARAM_SUPPORTED_EXT:
2199 if (!sctp_verify_ext_param(net, param))
2200 return SCTP_IERROR_ABORT;
2203 case SCTP_PARAM_SET_PRIMARY:
2204 if (net->sctp.addip_enable)
2208 case SCTP_PARAM_HOST_NAME_ADDRESS:
2209 /* Tell the peer, we won't support this param. */
2210 sctp_process_hn_param(asoc, param, chunk, err_chunk);
2211 retval = SCTP_IERROR_ABORT;
2214 case SCTP_PARAM_FWD_TSN_SUPPORT:
2215 if (ep->prsctp_enable)
2219 case SCTP_PARAM_RANDOM:
2220 if (!ep->auth_enable)
2223 /* SCTP-AUTH: Secion 6.1
2224 * If the random number is not 32 byte long the association
2225 * MUST be aborted. The ABORT chunk SHOULD contain the error
2226 * cause 'Protocol Violation'.
2228 if (SCTP_AUTH_RANDOM_LENGTH !=
2229 ntohs(param.p->length) - sizeof(struct sctp_paramhdr)) {
2230 sctp_process_inv_paramlength(asoc, param.p,
2232 retval = SCTP_IERROR_ABORT;
2236 case SCTP_PARAM_CHUNKS:
2237 if (!ep->auth_enable)
2240 /* SCTP-AUTH: Section 3.2
2241 * The CHUNKS parameter MUST be included once in the INIT or
2242 * INIT-ACK chunk if the sender wants to receive authenticated
2243 * chunks. Its maximum length is 260 bytes.
2245 if (260 < ntohs(param.p->length)) {
2246 sctp_process_inv_paramlength(asoc, param.p,
2248 retval = SCTP_IERROR_ABORT;
2252 case SCTP_PARAM_HMAC_ALGO:
2253 if (!ep->auth_enable)
2256 hmacs = (struct sctp_hmac_algo_param *)param.p;
2257 n_elt = (ntohs(param.p->length) -
2258 sizeof(struct sctp_paramhdr)) >> 1;
2260 /* SCTP-AUTH: Section 6.1
2261 * The HMAC algorithm based on SHA-1 MUST be supported and
2262 * included in the HMAC-ALGO parameter.
2264 for (i = 0; i < n_elt; i++) {
2265 id = ntohs(hmacs->hmac_ids[i]);
2267 if (id == SCTP_AUTH_HMAC_ID_SHA1)
2271 if (id != SCTP_AUTH_HMAC_ID_SHA1) {
2272 sctp_process_inv_paramlength(asoc, param.p, chunk,
2274 retval = SCTP_IERROR_ABORT;
2279 pr_debug("%s: unrecognized param:%d for chunk:%d\n",
2280 __func__, ntohs(param.p->type), cid);
2282 retval = sctp_process_unk_param(asoc, param, chunk, err_chunk);
2288 /* Verify the INIT packet before we process it. */
2289 int sctp_verify_init(struct net *net, const struct sctp_endpoint *ep,
2290 const struct sctp_association *asoc, enum sctp_cid cid,
2291 struct sctp_init_chunk *peer_init,
2292 struct sctp_chunk *chunk, struct sctp_chunk **errp)
2294 union sctp_params param;
2295 bool has_cookie = false;
2298 /* Check for missing mandatory parameters. Note: Initial TSN is
2299 * also mandatory, but is not checked here since the valid range
2300 * is 0..2**32-1. RFC4960, section 3.3.3.
2302 if (peer_init->init_hdr.num_outbound_streams == 0 ||
2303 peer_init->init_hdr.num_inbound_streams == 0 ||
2304 peer_init->init_hdr.init_tag == 0 ||
2305 ntohl(peer_init->init_hdr.a_rwnd) < SCTP_DEFAULT_MINWINDOW)
2306 return sctp_process_inv_mandatory(asoc, chunk, errp);
2308 sctp_walk_params(param, peer_init, init_hdr.params) {
2309 if (param.p->type == SCTP_PARAM_STATE_COOKIE)
2313 /* There is a possibility that a parameter length was bad and
2314 * in that case we would have stoped walking the parameters.
2315 * The current param.p would point at the bad one.
2316 * Current consensus on the mailing list is to generate a PROTOCOL
2317 * VIOLATION error. We build the ERROR chunk here and let the normal
2318 * error handling code build and send the packet.
2320 if (param.v != (void *)chunk->chunk_end)
2321 return sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
2323 /* The only missing mandatory param possible today is
2324 * the state cookie for an INIT-ACK chunk.
2326 if ((SCTP_CID_INIT_ACK == cid) && !has_cookie)
2327 return sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
2330 /* Verify all the variable length parameters */
2331 sctp_walk_params(param, peer_init, init_hdr.params) {
2332 result = sctp_verify_param(net, ep, asoc, param, cid,
2335 case SCTP_IERROR_ABORT:
2336 case SCTP_IERROR_NOMEM:
2338 case SCTP_IERROR_ERROR:
2340 case SCTP_IERROR_NO_ERROR:
2345 } /* for (loop through all parameters) */
2350 /* Unpack the parameters in an INIT packet into an association.
2351 * Returns 0 on failure, else success.
2352 * FIXME: This is an association method.
2354 int sctp_process_init(struct sctp_association *asoc, struct sctp_chunk *chunk,
2355 const union sctp_addr *peer_addr,
2356 struct sctp_init_chunk *peer_init, gfp_t gfp)
2358 struct net *net = sock_net(asoc->base.sk);
2359 struct sctp_transport *transport;
2360 struct list_head *pos, *temp;
2361 union sctp_params param;
2362 union sctp_addr addr;
2367 /* We must include the address that the INIT packet came from.
2368 * This is the only address that matters for an INIT packet.
2369 * When processing a COOKIE ECHO, we retrieve the from address
2370 * of the INIT from the cookie.
2373 /* This implementation defaults to making the first transport
2374 * added as the primary transport. The source address seems to
2375 * be a a better choice than any of the embedded addresses.
2377 if (!sctp_assoc_add_peer(asoc, peer_addr, gfp, SCTP_ACTIVE))
2380 if (sctp_cmp_addr_exact(sctp_source(chunk), peer_addr))
2383 /* Process the initialization parameters. */
2384 sctp_walk_params(param, peer_init, init_hdr.params) {
2385 if (!src_match && (param.p->type == SCTP_PARAM_IPV4_ADDRESS ||
2386 param.p->type == SCTP_PARAM_IPV6_ADDRESS)) {
2387 af = sctp_get_af_specific(param_type2af(param.p->type));
2388 af->from_addr_param(&addr, param.addr,
2389 chunk->sctp_hdr->source, 0);
2390 if (sctp_cmp_addr_exact(sctp_source(chunk), &addr))
2394 if (!sctp_process_param(asoc, param, peer_addr, gfp))
2398 /* source address of chunk may not match any valid address */
2402 /* AUTH: After processing the parameters, make sure that we
2403 * have all the required info to potentially do authentications.
2405 if (asoc->peer.auth_capable && (!asoc->peer.peer_random ||
2406 !asoc->peer.peer_hmacs))
2407 asoc->peer.auth_capable = 0;
2409 /* In a non-backward compatible mode, if the peer claims
2410 * support for ADD-IP but not AUTH, the ADD-IP spec states
2411 * that we MUST ABORT the association. Section 6. The section
2412 * also give us an option to silently ignore the packet, which
2413 * is what we'll do here.
2415 if (!net->sctp.addip_noauth &&
2416 (asoc->peer.asconf_capable && !asoc->peer.auth_capable)) {
2417 asoc->peer.addip_disabled_mask |= (SCTP_PARAM_ADD_IP |
2419 SCTP_PARAM_SET_PRIMARY);
2420 asoc->peer.asconf_capable = 0;
2424 /* Walk list of transports, removing transports in the UNKNOWN state. */
2425 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2426 transport = list_entry(pos, struct sctp_transport, transports);
2427 if (transport->state == SCTP_UNKNOWN) {
2428 sctp_assoc_rm_peer(asoc, transport);
2432 /* The fixed INIT headers are always in network byte
2435 asoc->peer.i.init_tag =
2436 ntohl(peer_init->init_hdr.init_tag);
2437 asoc->peer.i.a_rwnd =
2438 ntohl(peer_init->init_hdr.a_rwnd);
2439 asoc->peer.i.num_outbound_streams =
2440 ntohs(peer_init->init_hdr.num_outbound_streams);
2441 asoc->peer.i.num_inbound_streams =
2442 ntohs(peer_init->init_hdr.num_inbound_streams);
2443 asoc->peer.i.initial_tsn =
2444 ntohl(peer_init->init_hdr.initial_tsn);
2446 asoc->strreset_inseq = asoc->peer.i.initial_tsn;
2448 /* Apply the upper bounds for output streams based on peer's
2449 * number of inbound streams.
2451 if (asoc->c.sinit_num_ostreams >
2452 ntohs(peer_init->init_hdr.num_inbound_streams)) {
2453 asoc->c.sinit_num_ostreams =
2454 ntohs(peer_init->init_hdr.num_inbound_streams);
2457 if (asoc->c.sinit_max_instreams >
2458 ntohs(peer_init->init_hdr.num_outbound_streams)) {
2459 asoc->c.sinit_max_instreams =
2460 ntohs(peer_init->init_hdr.num_outbound_streams);
2463 /* Copy Initiation tag from INIT to VT_peer in cookie. */
2464 asoc->c.peer_vtag = asoc->peer.i.init_tag;
2466 /* Peer Rwnd : Current calculated value of the peer's rwnd. */
2467 asoc->peer.rwnd = asoc->peer.i.a_rwnd;
2469 /* Copy cookie in case we need to resend COOKIE-ECHO. */
2470 cookie = asoc->peer.cookie;
2472 asoc->peer.cookie = kmemdup(cookie, asoc->peer.cookie_len, gfp);
2473 if (!asoc->peer.cookie)
2477 /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
2478 * high (for example, implementations MAY use the size of the receiver
2479 * advertised window).
2481 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
2483 transport->ssthresh = asoc->peer.i.a_rwnd;
2486 /* Set up the TSN tracking pieces. */
2487 if (!sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
2488 asoc->peer.i.initial_tsn, gfp))
2491 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
2493 * The stream sequence number in all the streams shall start
2494 * from 0 when the association is established. Also, when the
2495 * stream sequence number reaches the value 65535 the next
2496 * stream sequence number shall be set to 0.
2499 if (sctp_stream_init(&asoc->stream, asoc->c.sinit_num_ostreams,
2500 asoc->c.sinit_max_instreams, gfp))
2503 if (!asoc->temp && sctp_assoc_set_id(asoc, gfp))
2506 /* ADDIP Section 4.1 ASCONF Chunk Procedures
2508 * When an endpoint has an ASCONF signaled change to be sent to the
2509 * remote endpoint it should do the following:
2511 * A2) A serial number should be assigned to the Chunk. The serial
2512 * number should be a monotonically increasing number. All serial
2513 * numbers are defined to be initialized at the start of the
2514 * association to the same value as the Initial TSN.
2516 asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
2520 /* Release the transport structures. */
2521 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2522 transport = list_entry(pos, struct sctp_transport, transports);
2523 if (transport->state != SCTP_ACTIVE)
2524 sctp_assoc_rm_peer(asoc, transport);
2532 /* Update asoc with the option described in param.
2534 * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
2536 * asoc is the association to update.
2537 * param is the variable length parameter to use for update.
2538 * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
2539 * If the current packet is an INIT we want to minimize the amount of
2540 * work we do. In particular, we should not build transport
2541 * structures for the addresses.
2543 static int sctp_process_param(struct sctp_association *asoc,
2544 union sctp_params param,
2545 const union sctp_addr *peer_addr,
2548 struct net *net = sock_net(asoc->base.sk);
2549 struct sctp_endpoint *ep = asoc->ep;
2550 union sctp_addr_param *addr_param;
2551 struct sctp_transport *t;
2552 enum sctp_scope scope;
2553 union sctp_addr addr;
2559 /* We maintain all INIT parameters in network byte order all the
2560 * time. This allows us to not worry about whether the parameters
2561 * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
2563 switch (param.p->type) {
2564 case SCTP_PARAM_IPV6_ADDRESS:
2565 if (PF_INET6 != asoc->base.sk->sk_family)
2569 case SCTP_PARAM_IPV4_ADDRESS:
2570 /* v4 addresses are not allowed on v6-only socket */
2571 if (ipv6_only_sock(asoc->base.sk))
2574 af = sctp_get_af_specific(param_type2af(param.p->type));
2575 af->from_addr_param(&addr, param.addr, htons(asoc->peer.port), 0);
2576 scope = sctp_scope(peer_addr);
2577 if (sctp_in_scope(net, &addr, scope))
2578 if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_UNCONFIRMED))
2582 case SCTP_PARAM_COOKIE_PRESERVATIVE:
2583 if (!net->sctp.cookie_preserve_enable)
2586 stale = ntohl(param.life->lifespan_increment);
2588 /* Suggested Cookie Life span increment's unit is msec,
2591 asoc->cookie_life = ktime_add_ms(asoc->cookie_life, stale);
2594 case SCTP_PARAM_HOST_NAME_ADDRESS:
2595 pr_debug("%s: unimplemented SCTP_HOST_NAME_ADDRESS\n", __func__);
2598 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2599 /* Turn off the default values first so we'll know which
2600 * ones are really set by the peer.
2602 asoc->peer.ipv4_address = 0;
2603 asoc->peer.ipv6_address = 0;
2605 /* Assume that peer supports the address family
2606 * by which it sends a packet.
2608 if (peer_addr->sa.sa_family == AF_INET6)
2609 asoc->peer.ipv6_address = 1;
2610 else if (peer_addr->sa.sa_family == AF_INET)
2611 asoc->peer.ipv4_address = 1;
2613 /* Cycle through address types; avoid divide by 0. */
2614 sat = ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
2616 sat /= sizeof(__u16);
2618 for (i = 0; i < sat; ++i) {
2619 switch (param.sat->types[i]) {
2620 case SCTP_PARAM_IPV4_ADDRESS:
2621 asoc->peer.ipv4_address = 1;
2624 case SCTP_PARAM_IPV6_ADDRESS:
2625 if (PF_INET6 == asoc->base.sk->sk_family)
2626 asoc->peer.ipv6_address = 1;
2629 case SCTP_PARAM_HOST_NAME_ADDRESS:
2630 asoc->peer.hostname_address = 1;
2633 default: /* Just ignore anything else. */
2639 case SCTP_PARAM_STATE_COOKIE:
2640 asoc->peer.cookie_len =
2641 ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
2642 asoc->peer.cookie = param.cookie->body;
2645 case SCTP_PARAM_HEARTBEAT_INFO:
2646 /* Would be odd to receive, but it causes no problems. */
2649 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2650 /* Rejected during verify stage. */
2653 case SCTP_PARAM_ECN_CAPABLE:
2654 asoc->peer.ecn_capable = 1;
2657 case SCTP_PARAM_ADAPTATION_LAYER_IND:
2658 asoc->peer.adaptation_ind = ntohl(param.aind->adaptation_ind);
2661 case SCTP_PARAM_SET_PRIMARY:
2662 if (!net->sctp.addip_enable)
2665 addr_param = param.v + sizeof(struct sctp_addip_param);
2667 af = sctp_get_af_specific(param_type2af(addr_param->p.type));
2671 af->from_addr_param(&addr, addr_param,
2672 htons(asoc->peer.port), 0);
2674 /* if the address is invalid, we can't process it.
2675 * XXX: see spec for what to do.
2677 if (!af->addr_valid(&addr, NULL, NULL))
2680 t = sctp_assoc_lookup_paddr(asoc, &addr);
2684 sctp_assoc_set_primary(asoc, t);
2687 case SCTP_PARAM_SUPPORTED_EXT:
2688 sctp_process_ext_param(asoc, param);
2691 case SCTP_PARAM_FWD_TSN_SUPPORT:
2692 if (asoc->prsctp_enable) {
2693 asoc->peer.prsctp_capable = 1;
2699 case SCTP_PARAM_RANDOM:
2700 if (!ep->auth_enable)
2703 /* Save peer's random parameter */
2704 asoc->peer.peer_random = kmemdup(param.p,
2705 ntohs(param.p->length), gfp);
2706 if (!asoc->peer.peer_random) {
2712 case SCTP_PARAM_HMAC_ALGO:
2713 if (!ep->auth_enable)
2716 /* Save peer's HMAC list */
2717 asoc->peer.peer_hmacs = kmemdup(param.p,
2718 ntohs(param.p->length), gfp);
2719 if (!asoc->peer.peer_hmacs) {
2724 /* Set the default HMAC the peer requested*/
2725 sctp_auth_asoc_set_default_hmac(asoc, param.hmac_algo);
2728 case SCTP_PARAM_CHUNKS:
2729 if (!ep->auth_enable)
2732 asoc->peer.peer_chunks = kmemdup(param.p,
2733 ntohs(param.p->length), gfp);
2734 if (!asoc->peer.peer_chunks)
2739 /* Any unrecognized parameters should have been caught
2740 * and handled by sctp_verify_param() which should be
2741 * called prior to this routine. Simply log the error
2744 pr_debug("%s: ignoring param:%d for association:%p.\n",
2745 __func__, ntohs(param.p->type), asoc);
2752 /* Select a new verification tag. */
2753 __u32 sctp_generate_tag(const struct sctp_endpoint *ep)
2755 /* I believe that this random number generator complies with RFC1750.
2756 * A tag of 0 is reserved for special cases (e.g. INIT).
2761 get_random_bytes(&x, sizeof(__u32));
2767 /* Select an initial TSN to send during startup. */
2768 __u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
2772 get_random_bytes(&retval, sizeof(__u32));
2777 * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
2779 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
2780 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2781 * | Type = 0xC1 | Chunk Flags | Chunk Length |
2782 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2784 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2785 * | Address Parameter |
2786 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2787 * | ASCONF Parameter #1 |
2788 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2792 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2793 * | ASCONF Parameter #N |
2794 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2796 * Address Parameter and other parameter will not be wrapped in this function
2798 static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
2799 union sctp_addr *addr,
2802 struct sctp_addiphdr asconf;
2803 struct sctp_chunk *retval;
2804 int length = sizeof(asconf) + vparam_len;
2805 union sctp_addr_param addrparam;
2807 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2809 addrlen = af->to_addr_param(addr, &addrparam);
2814 /* Create the chunk. */
2815 retval = sctp_make_control(asoc, SCTP_CID_ASCONF, 0, length,
2820 asconf.serial = htonl(asoc->addip_serial++);
2822 retval->subh.addip_hdr =
2823 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2824 retval->param_hdr.v =
2825 sctp_addto_chunk(retval, addrlen, &addrparam);
2831 * 3.2.1 Add IP Address
2833 * 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
2834 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2835 * | Type = 0xC001 | Length = Variable |
2836 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2837 * | ASCONF-Request Correlation ID |
2838 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2839 * | Address Parameter |
2840 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2842 * 3.2.2 Delete IP Address
2844 * 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
2845 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2846 * | Type = 0xC002 | Length = Variable |
2847 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2848 * | ASCONF-Request Correlation ID |
2849 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2850 * | Address Parameter |
2851 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2854 struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
2855 union sctp_addr *laddr,
2856 struct sockaddr *addrs,
2857 int addrcnt, __be16 flags)
2859 union sctp_addr_param addr_param;
2860 struct sctp_addip_param param;
2861 int paramlen = sizeof(param);
2862 struct sctp_chunk *retval;
2863 int addr_param_len = 0;
2864 union sctp_addr *addr;
2865 int totallen = 0, i;
2870 /* Get total length of all the address parameters. */
2872 for (i = 0; i < addrcnt; i++) {
2874 af = sctp_get_af_specific(addr->v4.sin_family);
2875 addr_param_len = af->to_addr_param(addr, &addr_param);
2877 totallen += paramlen;
2878 totallen += addr_param_len;
2880 addr_buf += af->sockaddr_len;
2881 if (asoc->asconf_addr_del_pending && !del_pickup) {
2882 /* reuse the parameter length from the same scope one */
2883 totallen += paramlen;
2884 totallen += addr_param_len;
2887 pr_debug("%s: picked same-scope del_pending addr, "
2888 "totallen for all addresses is %d\n",
2889 __func__, totallen);
2893 /* Create an asconf chunk with the required length. */
2894 retval = sctp_make_asconf(asoc, laddr, totallen);
2898 /* Add the address parameters to the asconf chunk. */
2900 for (i = 0; i < addrcnt; i++) {
2902 af = sctp_get_af_specific(addr->v4.sin_family);
2903 addr_param_len = af->to_addr_param(addr, &addr_param);
2904 param.param_hdr.type = flags;
2905 param.param_hdr.length = htons(paramlen + addr_param_len);
2906 param.crr_id = htonl(i);
2908 sctp_addto_chunk(retval, paramlen, ¶m);
2909 sctp_addto_chunk(retval, addr_param_len, &addr_param);
2911 addr_buf += af->sockaddr_len;
2913 if (flags == SCTP_PARAM_ADD_IP && del_pickup) {
2914 addr = asoc->asconf_addr_del_pending;
2915 af = sctp_get_af_specific(addr->v4.sin_family);
2916 addr_param_len = af->to_addr_param(addr, &addr_param);
2917 param.param_hdr.type = SCTP_PARAM_DEL_IP;
2918 param.param_hdr.length = htons(paramlen + addr_param_len);
2919 param.crr_id = htonl(i);
2921 sctp_addto_chunk(retval, paramlen, ¶m);
2922 sctp_addto_chunk(retval, addr_param_len, &addr_param);
2928 * 3.2.4 Set Primary IP Address
2930 * 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
2931 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2932 * | Type =0xC004 | Length = Variable |
2933 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2934 * | ASCONF-Request Correlation ID |
2935 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2936 * | Address Parameter |
2937 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2939 * Create an ASCONF chunk with Set Primary IP address parameter.
2941 struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
2942 union sctp_addr *addr)
2944 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2945 union sctp_addr_param addrparam;
2946 struct sctp_addip_param param;
2947 struct sctp_chunk *retval;
2948 int len = sizeof(param);
2951 addrlen = af->to_addr_param(addr, &addrparam);
2956 /* Create the chunk and make asconf header. */
2957 retval = sctp_make_asconf(asoc, addr, len);
2961 param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
2962 param.param_hdr.length = htons(len);
2965 sctp_addto_chunk(retval, sizeof(param), ¶m);
2966 sctp_addto_chunk(retval, addrlen, &addrparam);
2971 /* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2973 * 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
2974 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2975 * | Type = 0x80 | Chunk Flags | Chunk Length |
2976 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2978 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2979 * | ASCONF Parameter Response#1 |
2980 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2984 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2985 * | ASCONF Parameter Response#N |
2986 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2988 * Create an ASCONF_ACK chunk with enough space for the parameter responses.
2990 static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
2991 __u32 serial, int vparam_len)
2993 struct sctp_addiphdr asconf;
2994 struct sctp_chunk *retval;
2995 int length = sizeof(asconf) + vparam_len;
2997 /* Create the chunk. */
2998 retval = sctp_make_control(asoc, SCTP_CID_ASCONF_ACK, 0, length,
3003 asconf.serial = htonl(serial);
3005 retval->subh.addip_hdr =
3006 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
3011 /* Add response parameters to an ASCONF_ACK chunk. */
3012 static void sctp_add_asconf_response(struct sctp_chunk *chunk, __be32 crr_id,
3014 struct sctp_addip_param *asconf_param)
3016 struct sctp_addip_param ack_param;
3017 struct sctp_errhdr err_param;
3018 int asconf_param_len = 0;
3019 int err_param_len = 0;
3020 __be16 response_type;
3022 if (SCTP_ERROR_NO_ERROR == err_code) {
3023 response_type = SCTP_PARAM_SUCCESS_REPORT;
3025 response_type = SCTP_PARAM_ERR_CAUSE;
3026 err_param_len = sizeof(err_param);
3029 ntohs(asconf_param->param_hdr.length);
3032 /* Add Success Indication or Error Cause Indication parameter. */
3033 ack_param.param_hdr.type = response_type;
3034 ack_param.param_hdr.length = htons(sizeof(ack_param) +
3037 ack_param.crr_id = crr_id;
3038 sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
3040 if (SCTP_ERROR_NO_ERROR == err_code)
3043 /* Add Error Cause parameter. */
3044 err_param.cause = err_code;
3045 err_param.length = htons(err_param_len + asconf_param_len);
3046 sctp_addto_chunk(chunk, err_param_len, &err_param);
3048 /* Add the failed TLV copied from ASCONF chunk. */
3050 sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
3053 /* Process a asconf parameter. */
3054 static __be16 sctp_process_asconf_param(struct sctp_association *asoc,
3055 struct sctp_chunk *asconf,
3056 struct sctp_addip_param *asconf_param)
3058 union sctp_addr_param *addr_param;
3059 struct sctp_transport *peer;
3060 union sctp_addr addr;
3063 addr_param = (void *)asconf_param + sizeof(*asconf_param);
3065 if (asconf_param->param_hdr.type != SCTP_PARAM_ADD_IP &&
3066 asconf_param->param_hdr.type != SCTP_PARAM_DEL_IP &&
3067 asconf_param->param_hdr.type != SCTP_PARAM_SET_PRIMARY)
3068 return SCTP_ERROR_UNKNOWN_PARAM;
3070 switch (addr_param->p.type) {
3071 case SCTP_PARAM_IPV6_ADDRESS:
3072 if (!asoc->peer.ipv6_address)
3073 return SCTP_ERROR_DNS_FAILED;
3075 case SCTP_PARAM_IPV4_ADDRESS:
3076 if (!asoc->peer.ipv4_address)
3077 return SCTP_ERROR_DNS_FAILED;
3080 return SCTP_ERROR_DNS_FAILED;
3083 af = sctp_get_af_specific(param_type2af(addr_param->p.type));
3085 return SCTP_ERROR_DNS_FAILED;
3087 af->from_addr_param(&addr, addr_param, htons(asoc->peer.port), 0);
3089 /* ADDIP 4.2.1 This parameter MUST NOT contain a broadcast
3090 * or multicast address.
3091 * (note: wildcard is permitted and requires special handling so
3092 * make sure we check for that)
3094 if (!af->is_any(&addr) && !af->addr_valid(&addr, NULL, asconf->skb))
3095 return SCTP_ERROR_DNS_FAILED;
3097 switch (asconf_param->param_hdr.type) {
3098 case SCTP_PARAM_ADD_IP:
3100 * If the address 0.0.0.0 or ::0 is provided, the source
3101 * address of the packet MUST be added.
3103 if (af->is_any(&addr))
3104 memcpy(&addr, &asconf->source, sizeof(addr));
3106 if (security_sctp_bind_connect(asoc->ep->base.sk,
3108 (struct sockaddr *)&addr,
3110 return SCTP_ERROR_REQ_REFUSED;
3112 /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
3113 * request and does not have the local resources to add this
3114 * new address to the association, it MUST return an Error
3115 * Cause TLV set to the new error code 'Operation Refused
3116 * Due to Resource Shortage'.
3119 peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC, SCTP_UNCONFIRMED);
3121 return SCTP_ERROR_RSRC_LOW;
3123 /* Start the heartbeat timer. */
3124 sctp_transport_reset_hb_timer(peer);
3125 asoc->new_transport = peer;
3127 case SCTP_PARAM_DEL_IP:
3128 /* ADDIP 4.3 D7) If a request is received to delete the
3129 * last remaining IP address of a peer endpoint, the receiver
3130 * MUST send an Error Cause TLV with the error cause set to the
3131 * new error code 'Request to Delete Last Remaining IP Address'.
3133 if (asoc->peer.transport_count == 1)
3134 return SCTP_ERROR_DEL_LAST_IP;
3136 /* ADDIP 4.3 D8) If a request is received to delete an IP
3137 * address which is also the source address of the IP packet
3138 * which contained the ASCONF chunk, the receiver MUST reject
3139 * this request. To reject the request the receiver MUST send
3140 * an Error Cause TLV set to the new error code 'Request to
3141 * Delete Source IP Address'
3143 if (sctp_cmp_addr_exact(&asconf->source, &addr))
3144 return SCTP_ERROR_DEL_SRC_IP;
3147 * If the address 0.0.0.0 or ::0 is provided, all
3148 * addresses of the peer except the source address of the
3149 * packet MUST be deleted.
3151 if (af->is_any(&addr)) {
3152 sctp_assoc_set_primary(asoc, asconf->transport);
3153 sctp_assoc_del_nonprimary_peers(asoc,
3155 return SCTP_ERROR_NO_ERROR;
3158 /* If the address is not part of the association, the
3159 * ASCONF-ACK with Error Cause Indication Parameter
3160 * which including cause of Unresolvable Address should
3163 peer = sctp_assoc_lookup_paddr(asoc, &addr);
3165 return SCTP_ERROR_DNS_FAILED;
3167 sctp_assoc_rm_peer(asoc, peer);
3169 case SCTP_PARAM_SET_PRIMARY:
3170 /* ADDIP Section 4.2.4
3171 * If the address 0.0.0.0 or ::0 is provided, the receiver
3172 * MAY mark the source address of the packet as its
3175 if (af->is_any(&addr))
3176 memcpy(&addr.v4, sctp_source(asconf), sizeof(addr));
3178 if (security_sctp_bind_connect(asoc->ep->base.sk,
3179 SCTP_PARAM_SET_PRIMARY,
3180 (struct sockaddr *)&addr,
3182 return SCTP_ERROR_REQ_REFUSED;
3184 peer = sctp_assoc_lookup_paddr(asoc, &addr);
3186 return SCTP_ERROR_DNS_FAILED;
3188 sctp_assoc_set_primary(asoc, peer);
3192 return SCTP_ERROR_NO_ERROR;
3195 /* Verify the ASCONF packet before we process it. */
3196 bool sctp_verify_asconf(const struct sctp_association *asoc,
3197 struct sctp_chunk *chunk, bool addr_param_needed,
3198 struct sctp_paramhdr **errp)
3200 struct sctp_addip_chunk *addip;
3201 bool addr_param_seen = false;
3202 union sctp_params param;
3204 addip = (struct sctp_addip_chunk *)chunk->chunk_hdr;
3205 sctp_walk_params(param, addip, addip_hdr.params) {
3206 size_t length = ntohs(param.p->length);
3209 switch (param.p->type) {
3210 case SCTP_PARAM_ERR_CAUSE:
3212 case SCTP_PARAM_IPV4_ADDRESS:
3213 if (length != sizeof(struct sctp_ipv4addr_param))
3215 /* ensure there is only one addr param and it's in the
3216 * beginning of addip_hdr params, or we reject it.
3218 if (param.v != addip->addip_hdr.params)
3220 addr_param_seen = true;
3222 case SCTP_PARAM_IPV6_ADDRESS:
3223 if (length != sizeof(struct sctp_ipv6addr_param))
3225 if (param.v != addip->addip_hdr.params)
3227 addr_param_seen = true;
3229 case SCTP_PARAM_ADD_IP:
3230 case SCTP_PARAM_DEL_IP:
3231 case SCTP_PARAM_SET_PRIMARY:
3232 /* In ASCONF chunks, these need to be first. */
3233 if (addr_param_needed && !addr_param_seen)
3235 length = ntohs(param.addip->param_hdr.length);
3236 if (length < sizeof(struct sctp_addip_param) +
3240 case SCTP_PARAM_SUCCESS_REPORT:
3241 case SCTP_PARAM_ADAPTATION_LAYER_IND:
3242 if (length != sizeof(struct sctp_addip_param))
3246 /* This is unkown to us, reject! */
3251 /* Remaining sanity checks. */
3252 if (addr_param_needed && !addr_param_seen)
3254 if (!addr_param_needed && addr_param_seen)
3256 if (param.v != chunk->chunk_end)
3262 /* Process an incoming ASCONF chunk with the next expected serial no. and
3263 * return an ASCONF_ACK chunk to be sent in response.
3265 struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
3266 struct sctp_chunk *asconf)
3268 union sctp_addr_param *addr_param;
3269 struct sctp_addip_chunk *addip;
3270 struct sctp_chunk *asconf_ack;
3271 bool all_param_pass = true;
3272 struct sctp_addiphdr *hdr;
3273 int length = 0, chunk_len;
3274 union sctp_params param;
3278 addip = (struct sctp_addip_chunk *)asconf->chunk_hdr;
3279 chunk_len = ntohs(asconf->chunk_hdr->length) -
3280 sizeof(struct sctp_chunkhdr);
3281 hdr = (struct sctp_addiphdr *)asconf->skb->data;
3282 serial = ntohl(hdr->serial);
3284 /* Skip the addiphdr and store a pointer to address parameter. */
3285 length = sizeof(*hdr);
3286 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
3287 chunk_len -= length;
3289 /* Skip the address parameter and store a pointer to the first
3292 length = ntohs(addr_param->p.length);
3293 chunk_len -= length;
3295 /* create an ASCONF_ACK chunk.
3296 * Based on the definitions of parameters, we know that the size of
3297 * ASCONF_ACK parameters are less than or equal to the fourfold of ASCONF
3300 asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 4);
3304 /* Process the TLVs contained within the ASCONF chunk. */
3305 sctp_walk_params(param, addip, addip_hdr.params) {
3306 /* Skip preceeding address parameters. */
3307 if (param.p->type == SCTP_PARAM_IPV4_ADDRESS ||
3308 param.p->type == SCTP_PARAM_IPV6_ADDRESS)
3311 err_code = sctp_process_asconf_param(asoc, asconf,
3314 * If an error response is received for a TLV parameter,
3315 * all TLVs with no response before the failed TLV are
3316 * considered successful if not reported. All TLVs after
3317 * the failed response are considered unsuccessful unless
3318 * a specific success indication is present for the parameter.
3320 if (err_code != SCTP_ERROR_NO_ERROR)
3321 all_param_pass = false;
3322 if (!all_param_pass)
3323 sctp_add_asconf_response(asconf_ack, param.addip->crr_id,
3324 err_code, param.addip);
3326 /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
3327 * an IP address sends an 'Out of Resource' in its response, it
3328 * MUST also fail any subsequent add or delete requests bundled
3331 if (err_code == SCTP_ERROR_RSRC_LOW)
3335 asoc->peer.addip_serial++;
3337 /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
3338 * after freeing the reference to old asconf ack if any.
3341 sctp_chunk_hold(asconf_ack);
3342 list_add_tail(&asconf_ack->transmitted_list,
3343 &asoc->asconf_ack_list);
3349 /* Process a asconf parameter that is successfully acked. */
3350 static void sctp_asconf_param_success(struct sctp_association *asoc,
3351 struct sctp_addip_param *asconf_param)
3353 struct sctp_bind_addr *bp = &asoc->base.bind_addr;
3354 union sctp_addr_param *addr_param;
3355 struct sctp_sockaddr_entry *saddr;
3356 struct sctp_transport *transport;
3357 union sctp_addr addr;
3360 addr_param = (void *)asconf_param + sizeof(*asconf_param);
3362 /* We have checked the packet before, so we do not check again. */
3363 af = sctp_get_af_specific(param_type2af(addr_param->p.type));
3364 af->from_addr_param(&addr, addr_param, htons(bp->port), 0);
3366 switch (asconf_param->param_hdr.type) {
3367 case SCTP_PARAM_ADD_IP:
3368 /* This is always done in BH context with a socket lock
3369 * held, so the list can not change.
3372 list_for_each_entry(saddr, &bp->address_list, list) {
3373 if (sctp_cmp_addr_exact(&saddr->a, &addr))
3374 saddr->state = SCTP_ADDR_SRC;
3377 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
3379 sctp_transport_dst_release(transport);
3382 case SCTP_PARAM_DEL_IP:
3384 sctp_del_bind_addr(bp, &addr);
3385 if (asoc->asconf_addr_del_pending != NULL &&
3386 sctp_cmp_addr_exact(asoc->asconf_addr_del_pending, &addr)) {
3387 kfree(asoc->asconf_addr_del_pending);
3388 asoc->asconf_addr_del_pending = NULL;
3391 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
3393 sctp_transport_dst_release(transport);
3401 /* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
3402 * for the given asconf parameter. If there is no response for this parameter,
3403 * return the error code based on the third argument 'no_err'.
3405 * A7) If an error response is received for a TLV parameter, all TLVs with no
3406 * response before the failed TLV are considered successful if not reported.
3407 * All TLVs after the failed response are considered unsuccessful unless a
3408 * specific success indication is present for the parameter.
3410 static __be16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
3411 struct sctp_addip_param *asconf_param,
3414 struct sctp_addip_param *asconf_ack_param;
3415 struct sctp_errhdr *err_param;
3421 err_code = SCTP_ERROR_NO_ERROR;
3423 err_code = SCTP_ERROR_REQ_REFUSED;
3425 asconf_ack_len = ntohs(asconf_ack->chunk_hdr->length) -
3426 sizeof(struct sctp_chunkhdr);
3428 /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
3429 * the first asconf_ack parameter.
3431 length = sizeof(struct sctp_addiphdr);
3432 asconf_ack_param = (struct sctp_addip_param *)(asconf_ack->skb->data +
3434 asconf_ack_len -= length;
3436 while (asconf_ack_len > 0) {
3437 if (asconf_ack_param->crr_id == asconf_param->crr_id) {
3438 switch (asconf_ack_param->param_hdr.type) {
3439 case SCTP_PARAM_SUCCESS_REPORT:
3440 return SCTP_ERROR_NO_ERROR;
3441 case SCTP_PARAM_ERR_CAUSE:
3442 length = sizeof(*asconf_ack_param);
3443 err_param = (void *)asconf_ack_param + length;
3444 asconf_ack_len -= length;
3445 if (asconf_ack_len > 0)
3446 return err_param->cause;
3448 return SCTP_ERROR_INV_PARAM;
3451 return SCTP_ERROR_INV_PARAM;
3455 length = ntohs(asconf_ack_param->param_hdr.length);
3456 asconf_ack_param = (void *)asconf_ack_param + length;
3457 asconf_ack_len -= length;
3463 /* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
3464 int sctp_process_asconf_ack(struct sctp_association *asoc,
3465 struct sctp_chunk *asconf_ack)
3467 struct sctp_chunk *asconf = asoc->addip_last_asconf;
3468 struct sctp_addip_param *asconf_param;
3469 __be16 err_code = SCTP_ERROR_NO_ERROR;
3470 union sctp_addr_param *addr_param;
3471 int asconf_len = asconf->skb->len;
3472 int all_param_pass = 0;
3477 /* Skip the chunkhdr and addiphdr from the last asconf sent and store
3478 * a pointer to address parameter.
3480 length = sizeof(struct sctp_addip_chunk);
3481 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
3482 asconf_len -= length;
3484 /* Skip the address parameter in the last asconf sent and store a
3485 * pointer to the first asconf parameter.
3487 length = ntohs(addr_param->p.length);
3488 asconf_param = (void *)addr_param + length;
3489 asconf_len -= length;
3492 * A8) If there is no response(s) to specific TLV parameter(s), and no
3493 * failures are indicated, then all request(s) are considered
3496 if (asconf_ack->skb->len == sizeof(struct sctp_addiphdr))
3499 /* Process the TLVs contained in the last sent ASCONF chunk. */
3500 while (asconf_len > 0) {
3502 err_code = SCTP_ERROR_NO_ERROR;
3504 err_code = sctp_get_asconf_response(asconf_ack,
3507 if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
3512 case SCTP_ERROR_NO_ERROR:
3513 sctp_asconf_param_success(asoc, asconf_param);
3516 case SCTP_ERROR_RSRC_LOW:
3520 case SCTP_ERROR_UNKNOWN_PARAM:
3521 /* Disable sending this type of asconf parameter in
3524 asoc->peer.addip_disabled_mask |=
3525 asconf_param->param_hdr.type;
3528 case SCTP_ERROR_REQ_REFUSED:
3529 case SCTP_ERROR_DEL_LAST_IP:
3530 case SCTP_ERROR_DEL_SRC_IP:
3535 /* Skip the processed asconf parameter and move to the next
3538 length = ntohs(asconf_param->param_hdr.length);
3539 asconf_param = (void *)asconf_param + length;
3540 asconf_len -= length;
3543 if (no_err && asoc->src_out_of_asoc_ok) {
3544 asoc->src_out_of_asoc_ok = 0;
3545 sctp_transport_immediate_rtx(asoc->peer.primary_path);
3548 /* Free the cached last sent asconf chunk. */
3549 list_del_init(&asconf->transmitted_list);
3550 sctp_chunk_free(asconf);
3551 asoc->addip_last_asconf = NULL;
3556 /* Make a FWD TSN chunk. */
3557 struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
3558 __u32 new_cum_tsn, size_t nstreams,
3559 struct sctp_fwdtsn_skip *skiplist)
3561 struct sctp_chunk *retval = NULL;
3562 struct sctp_fwdtsn_hdr ftsn_hdr;
3563 struct sctp_fwdtsn_skip skip;
3567 hint = (nstreams + 1) * sizeof(__u32);
3569 retval = sctp_make_control(asoc, SCTP_CID_FWD_TSN, 0, hint, GFP_ATOMIC);
3574 ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
3575 retval->subh.fwdtsn_hdr =
3576 sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
3578 for (i = 0; i < nstreams; i++) {
3579 skip.stream = skiplist[i].stream;
3580 skip.ssn = skiplist[i].ssn;
3581 sctp_addto_chunk(retval, sizeof(skip), &skip);
3587 struct sctp_chunk *sctp_make_ifwdtsn(const struct sctp_association *asoc,
3588 __u32 new_cum_tsn, size_t nstreams,
3589 struct sctp_ifwdtsn_skip *skiplist)
3591 struct sctp_chunk *retval = NULL;
3592 struct sctp_ifwdtsn_hdr ftsn_hdr;
3595 hint = (nstreams + 1) * sizeof(__u32);
3597 retval = sctp_make_control(asoc, SCTP_CID_I_FWD_TSN, 0, hint,
3602 ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
3603 retval->subh.ifwdtsn_hdr =
3604 sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
3606 sctp_addto_chunk(retval, nstreams * sizeof(skiplist[0]), skiplist);
3611 /* RE-CONFIG 3.1 (RE-CONFIG chunk)
3613 * 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
3614 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3615 * | Type = 130 | Chunk Flags | Chunk Length |
3616 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3618 * / Re-configuration Parameter /
3620 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3622 * / Re-configuration Parameter (optional) /
3624 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3626 static struct sctp_chunk *sctp_make_reconf(const struct sctp_association *asoc,
3629 struct sctp_reconf_chunk *reconf;
3630 struct sctp_chunk *retval;
3632 retval = sctp_make_control(asoc, SCTP_CID_RECONF, 0, length,
3637 reconf = (struct sctp_reconf_chunk *)retval->chunk_hdr;
3638 retval->param_hdr.v = reconf->params;
3643 /* RE-CONFIG 4.1 (STREAM OUT RESET)
3645 * 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
3646 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3647 * | Parameter Type = 13 | Parameter Length = 16 + 2 * N |
3648 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3649 * | Re-configuration Request Sequence Number |
3650 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3651 * | Re-configuration Response Sequence Number |
3652 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3653 * | Sender's Last Assigned TSN |
3654 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3655 * | Stream Number 1 (optional) | Stream Number 2 (optional) |
3656 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3658 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3659 * | Stream Number N-1 (optional) | Stream Number N (optional) |
3660 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3662 * RE-CONFIG 4.2 (STREAM IN RESET)
3664 * 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
3665 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3666 * | Parameter Type = 14 | Parameter Length = 8 + 2 * N |
3667 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3668 * | Re-configuration Request Sequence Number |
3669 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3670 * | Stream Number 1 (optional) | Stream Number 2 (optional) |
3671 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3673 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3674 * | Stream Number N-1 (optional) | Stream Number N (optional) |
3675 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3677 struct sctp_chunk *sctp_make_strreset_req(
3678 const struct sctp_association *asoc,
3679 __u16 stream_num, __be16 *stream_list,
3682 __u16 stream_len = stream_num * sizeof(__u16);
3683 struct sctp_strreset_outreq outreq;
3684 struct sctp_strreset_inreq inreq;
3685 struct sctp_chunk *retval;
3686 __u16 outlen, inlen;
3688 outlen = (sizeof(outreq) + stream_len) * out;
3689 inlen = (sizeof(inreq) + stream_len) * in;
3691 retval = sctp_make_reconf(asoc, outlen + inlen);
3696 outreq.param_hdr.type = SCTP_PARAM_RESET_OUT_REQUEST;
3697 outreq.param_hdr.length = htons(outlen);
3698 outreq.request_seq = htonl(asoc->strreset_outseq);
3699 outreq.response_seq = htonl(asoc->strreset_inseq - 1);
3700 outreq.send_reset_at_tsn = htonl(asoc->next_tsn - 1);
3702 sctp_addto_chunk(retval, sizeof(outreq), &outreq);
3705 sctp_addto_chunk(retval, stream_len, stream_list);
3709 inreq.param_hdr.type = SCTP_PARAM_RESET_IN_REQUEST;
3710 inreq.param_hdr.length = htons(inlen);
3711 inreq.request_seq = htonl(asoc->strreset_outseq + out);
3713 sctp_addto_chunk(retval, sizeof(inreq), &inreq);
3716 sctp_addto_chunk(retval, stream_len, stream_list);
3722 /* RE-CONFIG 4.3 (SSN/TSN RESET ALL)
3724 * 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
3725 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3726 * | Parameter Type = 15 | Parameter Length = 8 |
3727 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3728 * | Re-configuration Request Sequence Number |
3729 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3731 struct sctp_chunk *sctp_make_strreset_tsnreq(
3732 const struct sctp_association *asoc)
3734 struct sctp_strreset_tsnreq tsnreq;
3735 __u16 length = sizeof(tsnreq);
3736 struct sctp_chunk *retval;
3738 retval = sctp_make_reconf(asoc, length);
3742 tsnreq.param_hdr.type = SCTP_PARAM_RESET_TSN_REQUEST;
3743 tsnreq.param_hdr.length = htons(length);
3744 tsnreq.request_seq = htonl(asoc->strreset_outseq);
3746 sctp_addto_chunk(retval, sizeof(tsnreq), &tsnreq);
3751 /* RE-CONFIG 4.5/4.6 (ADD STREAM)
3753 * 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
3754 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3755 * | Parameter Type = 17 | Parameter Length = 12 |
3756 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3757 * | Re-configuration Request Sequence Number |
3758 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3759 * | Number of new streams | Reserved |
3760 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3762 struct sctp_chunk *sctp_make_strreset_addstrm(
3763 const struct sctp_association *asoc,
3764 __u16 out, __u16 in)
3766 struct sctp_strreset_addstrm addstrm;
3767 __u16 size = sizeof(addstrm);
3768 struct sctp_chunk *retval;
3770 retval = sctp_make_reconf(asoc, (!!out + !!in) * size);
3775 addstrm.param_hdr.type = SCTP_PARAM_RESET_ADD_OUT_STREAMS;
3776 addstrm.param_hdr.length = htons(size);
3777 addstrm.number_of_streams = htons(out);
3778 addstrm.request_seq = htonl(asoc->strreset_outseq);
3779 addstrm.reserved = 0;
3781 sctp_addto_chunk(retval, size, &addstrm);
3785 addstrm.param_hdr.type = SCTP_PARAM_RESET_ADD_IN_STREAMS;
3786 addstrm.param_hdr.length = htons(size);
3787 addstrm.number_of_streams = htons(in);
3788 addstrm.request_seq = htonl(asoc->strreset_outseq + !!out);
3789 addstrm.reserved = 0;
3791 sctp_addto_chunk(retval, size, &addstrm);
3797 /* RE-CONFIG 4.4 (RESP)
3799 * 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
3800 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3801 * | Parameter Type = 16 | Parameter Length |
3802 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3803 * | Re-configuration Response Sequence Number |
3804 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3806 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3808 struct sctp_chunk *sctp_make_strreset_resp(const struct sctp_association *asoc,
3809 __u32 result, __u32 sn)
3811 struct sctp_strreset_resp resp;
3812 __u16 length = sizeof(resp);
3813 struct sctp_chunk *retval;
3815 retval = sctp_make_reconf(asoc, length);
3819 resp.param_hdr.type = SCTP_PARAM_RESET_RESPONSE;
3820 resp.param_hdr.length = htons(length);
3821 resp.response_seq = htonl(sn);
3822 resp.result = htonl(result);
3824 sctp_addto_chunk(retval, sizeof(resp), &resp);
3829 /* RE-CONFIG 4.4 OPTIONAL (TSNRESP)
3831 * 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
3832 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3833 * | Parameter Type = 16 | Parameter Length |
3834 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3835 * | Re-configuration Response Sequence Number |
3836 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3838 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3839 * | Sender's Next TSN (optional) |
3840 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3841 * | Receiver's Next TSN (optional) |
3842 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3844 struct sctp_chunk *sctp_make_strreset_tsnresp(struct sctp_association *asoc,
3845 __u32 result, __u32 sn,
3849 struct sctp_strreset_resptsn tsnresp;
3850 __u16 length = sizeof(tsnresp);
3851 struct sctp_chunk *retval;
3853 retval = sctp_make_reconf(asoc, length);
3857 tsnresp.param_hdr.type = SCTP_PARAM_RESET_RESPONSE;
3858 tsnresp.param_hdr.length = htons(length);
3860 tsnresp.response_seq = htonl(sn);
3861 tsnresp.result = htonl(result);
3862 tsnresp.senders_next_tsn = htonl(sender_tsn);
3863 tsnresp.receivers_next_tsn = htonl(receiver_tsn);
3865 sctp_addto_chunk(retval, sizeof(tsnresp), &tsnresp);
3870 bool sctp_verify_reconf(const struct sctp_association *asoc,
3871 struct sctp_chunk *chunk,
3872 struct sctp_paramhdr **errp)
3874 struct sctp_reconf_chunk *hdr;
3875 union sctp_params param;
3879 hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
3880 sctp_walk_params(param, hdr, params) {
3881 __u16 length = ntohs(param.p->length);
3886 switch (param.p->type) {
3887 case SCTP_PARAM_RESET_OUT_REQUEST:
3888 if (length < sizeof(struct sctp_strreset_outreq) ||
3889 (last && last != SCTP_PARAM_RESET_RESPONSE &&
3890 last != SCTP_PARAM_RESET_IN_REQUEST))
3893 case SCTP_PARAM_RESET_IN_REQUEST:
3894 if (length < sizeof(struct sctp_strreset_inreq) ||
3895 (last && last != SCTP_PARAM_RESET_OUT_REQUEST))
3898 case SCTP_PARAM_RESET_RESPONSE:
3899 if ((length != sizeof(struct sctp_strreset_resp) &&
3900 length != sizeof(struct sctp_strreset_resptsn)) ||
3901 (last && last != SCTP_PARAM_RESET_RESPONSE &&
3902 last != SCTP_PARAM_RESET_OUT_REQUEST))
3905 case SCTP_PARAM_RESET_TSN_REQUEST:
3907 sizeof(struct sctp_strreset_tsnreq) || last)
3910 case SCTP_PARAM_RESET_ADD_IN_STREAMS:
3911 if (length != sizeof(struct sctp_strreset_addstrm) ||
3912 (last && last != SCTP_PARAM_RESET_ADD_OUT_STREAMS))
3915 case SCTP_PARAM_RESET_ADD_OUT_STREAMS:
3916 if (length != sizeof(struct sctp_strreset_addstrm) ||
3917 (last && last != SCTP_PARAM_RESET_ADD_IN_STREAMS))
3924 last = param.p->type;