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 sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
73 const struct sctp_association *asoc,
74 const struct sctp_chunk *init_chunk,
76 const __u8 *raw_addrs, int addrs_len);
77 static int sctp_process_param(struct sctp_association *asoc,
78 union sctp_params param,
79 const union sctp_addr *peer_addr,
81 static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
83 static void *sctp_addto_chunk_fixed(struct sctp_chunk *, int len,
86 /* Control chunk destructor */
87 static void sctp_control_release_owner(struct sk_buff *skb)
89 /*TODO: do memory release */
92 static void sctp_control_set_owner_w(struct sctp_chunk *chunk)
94 struct sctp_association *asoc = chunk->asoc;
95 struct sk_buff *skb = chunk->skb;
97 /* TODO: properly account for control chunks.
98 * To do it right we'll need:
99 * 1) endpoint if association isn't known.
100 * 2) proper memory accounting.
102 * For now don't do anything for now.
104 skb->sk = asoc ? asoc->base.sk : NULL;
105 skb->destructor = sctp_control_release_owner;
108 /* What was the inbound interface for this chunk? */
109 int sctp_chunk_iif(const struct sctp_chunk *chunk)
114 af = sctp_get_af_specific(ipver2af(ip_hdr(chunk->skb)->version));
116 iif = af->skb_iif(chunk->skb);
121 /* RFC 2960 3.3.2 Initiation (INIT) (1)
123 * Note 2: The ECN capable field is reserved for future use of
124 * Explicit Congestion Notification.
126 static const struct sctp_paramhdr ecap_param = {
127 SCTP_PARAM_ECN_CAPABLE,
128 cpu_to_be16(sizeof(struct sctp_paramhdr)),
130 static const struct sctp_paramhdr prsctp_param = {
131 SCTP_PARAM_FWD_TSN_SUPPORT,
132 cpu_to_be16(sizeof(struct sctp_paramhdr)),
135 /* A helper to initialize an op error inside a
136 * provided chunk, as most cause codes will be embedded inside an
139 void sctp_init_cause(struct sctp_chunk *chunk, __be16 cause_code,
145 /* Cause code constants are now defined in network order. */
146 err.cause = cause_code;
147 len = sizeof(sctp_errhdr_t) + paylen;
148 err.length = htons(len);
149 chunk->subh.err_hdr = sctp_addto_chunk(chunk, sizeof(sctp_errhdr_t), &err);
152 /* A helper to initialize an op error inside a
153 * provided chunk, as most cause codes will be embedded inside an
154 * abort chunk. Differs from sctp_init_cause in that it won't oops
155 * if there isn't enough space in the op error chunk
157 static int sctp_init_cause_fixed(struct sctp_chunk *chunk, __be16 cause_code,
163 /* Cause code constants are now defined in network order. */
164 err.cause = cause_code;
165 len = sizeof(sctp_errhdr_t) + paylen;
166 err.length = htons(len);
168 if (skb_tailroom(chunk->skb) < len)
170 chunk->subh.err_hdr = sctp_addto_chunk_fixed(chunk,
171 sizeof(sctp_errhdr_t),
175 /* 3.3.2 Initiation (INIT) (1)
177 * This chunk is used to initiate a SCTP association between two
178 * endpoints. The format of the INIT chunk is shown below:
181 * 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
182 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
183 * | Type = 1 | Chunk Flags | Chunk Length |
184 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
186 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
187 * | Advertised Receiver Window Credit (a_rwnd) |
188 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
189 * | Number of Outbound Streams | Number of Inbound Streams |
190 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
192 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
194 * / Optional/Variable-Length Parameters /
196 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
199 * The INIT chunk contains the following parameters. Unless otherwise
200 * noted, each parameter MUST only be included once in the INIT chunk.
202 * Fixed Parameters Status
203 * ----------------------------------------------
204 * Initiate Tag Mandatory
205 * Advertised Receiver Window Credit Mandatory
206 * Number of Outbound Streams Mandatory
207 * Number of Inbound Streams Mandatory
208 * Initial TSN Mandatory
210 * Variable Parameters Status Type Value
211 * -------------------------------------------------------------
212 * IPv4 Address (Note 1) Optional 5
213 * IPv6 Address (Note 1) Optional 6
214 * Cookie Preservative Optional 9
215 * Reserved for ECN Capable (Note 2) Optional 32768 (0x8000)
216 * Host Name Address (Note 3) Optional 11
217 * Supported Address Types (Note 4) Optional 12
219 struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
220 const struct sctp_bind_addr *bp,
221 gfp_t gfp, int vparam_len)
223 struct net *net = sock_net(asoc->base.sk);
224 struct sctp_endpoint *ep = asoc->ep;
226 union sctp_params addrs;
228 struct sctp_chunk *retval = NULL;
229 int num_types, addrs_len = 0;
230 struct sctp_sock *sp;
231 sctp_supported_addrs_param_t sat;
233 sctp_adaptation_ind_param_t aiparam;
234 sctp_supported_ext_param_t ext_param;
237 sctp_paramhdr_t *auth_chunks = NULL,
240 /* RFC 2960 3.3.2 Initiation (INIT) (1)
242 * Note 1: The INIT chunks can contain multiple addresses that
243 * can be IPv4 and/or IPv6 in any combination.
247 /* Convert the provided bind address list to raw format. */
248 addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, gfp);
250 init.init_tag = htonl(asoc->c.my_vtag);
251 init.a_rwnd = htonl(asoc->rwnd);
252 init.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
253 init.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
254 init.initial_tsn = htonl(asoc->c.initial_tsn);
256 /* How many address types are needed? */
257 sp = sctp_sk(asoc->base.sk);
258 num_types = sp->pf->supported_addrs(sp, types);
260 chunksize = sizeof(init) + addrs_len;
261 chunksize += WORD_ROUND(SCTP_SAT_LEN(num_types));
262 chunksize += sizeof(ecap_param);
264 if (asoc->prsctp_enable)
265 chunksize += sizeof(prsctp_param);
267 /* ADDIP: Section 4.2.7:
268 * An implementation supporting this extension [ADDIP] MUST list
269 * the ASCONF,the ASCONF-ACK, and the AUTH chunks in its INIT and
270 * INIT-ACK parameters.
272 if (net->sctp.addip_enable) {
273 extensions[num_ext] = SCTP_CID_ASCONF;
274 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
278 if (sp->adaptation_ind)
279 chunksize += sizeof(aiparam);
281 chunksize += vparam_len;
283 /* Account for AUTH related parameters */
284 if (ep->auth_enable) {
285 /* Add random parameter length*/
286 chunksize += sizeof(asoc->c.auth_random);
288 /* Add HMACS parameter length if any were defined */
289 auth_hmacs = (sctp_paramhdr_t *)asoc->c.auth_hmacs;
290 if (auth_hmacs->length)
291 chunksize += WORD_ROUND(ntohs(auth_hmacs->length));
295 /* Add CHUNKS parameter length */
296 auth_chunks = (sctp_paramhdr_t *)asoc->c.auth_chunks;
297 if (auth_chunks->length)
298 chunksize += WORD_ROUND(ntohs(auth_chunks->length));
302 extensions[num_ext] = SCTP_CID_AUTH;
306 /* If we have any extensions to report, account for that */
308 chunksize += WORD_ROUND(sizeof(sctp_supported_ext_param_t) +
311 /* RFC 2960 3.3.2 Initiation (INIT) (1)
313 * Note 3: An INIT chunk MUST NOT contain more than one Host
314 * Name address parameter. Moreover, the sender of the INIT
315 * MUST NOT combine any other address types with the Host Name
316 * address in the INIT. The receiver of INIT MUST ignore any
317 * other address types if the Host Name address parameter is
318 * present in the received INIT chunk.
320 * PLEASE DO NOT FIXME [This version does not support Host Name.]
323 retval = sctp_make_control(asoc, SCTP_CID_INIT, 0, chunksize, gfp);
327 retval->subh.init_hdr =
328 sctp_addto_chunk(retval, sizeof(init), &init);
329 retval->param_hdr.v =
330 sctp_addto_chunk(retval, addrs_len, addrs.v);
332 /* RFC 2960 3.3.2 Initiation (INIT) (1)
334 * Note 4: This parameter, when present, specifies all the
335 * address types the sending endpoint can support. The absence
336 * of this parameter indicates that the sending endpoint can
337 * support any address type.
339 sat.param_hdr.type = SCTP_PARAM_SUPPORTED_ADDRESS_TYPES;
340 sat.param_hdr.length = htons(SCTP_SAT_LEN(num_types));
341 sctp_addto_chunk(retval, sizeof(sat), &sat);
342 sctp_addto_chunk(retval, num_types * sizeof(__u16), &types);
344 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
346 /* Add the supported extensions parameter. Be nice and add this
347 * fist before addiding the parameters for the extensions themselves
350 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
351 ext_param.param_hdr.length =
352 htons(sizeof(sctp_supported_ext_param_t) + num_ext);
353 sctp_addto_chunk(retval, sizeof(sctp_supported_ext_param_t),
355 sctp_addto_param(retval, num_ext, extensions);
358 if (asoc->prsctp_enable)
359 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
361 if (sp->adaptation_ind) {
362 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
363 aiparam.param_hdr.length = htons(sizeof(aiparam));
364 aiparam.adaptation_ind = htonl(sp->adaptation_ind);
365 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
368 /* Add SCTP-AUTH chunks to the parameter list */
369 if (ep->auth_enable) {
370 sctp_addto_chunk(retval, sizeof(asoc->c.auth_random),
371 asoc->c.auth_random);
373 sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
376 sctp_addto_chunk(retval, ntohs(auth_chunks->length),
384 struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
385 const struct sctp_chunk *chunk,
386 gfp_t gfp, int unkparam_len)
388 sctp_inithdr_t initack;
389 struct sctp_chunk *retval;
390 union sctp_params addrs;
391 struct sctp_sock *sp;
393 sctp_cookie_param_t *cookie;
396 sctp_adaptation_ind_param_t aiparam;
397 sctp_supported_ext_param_t ext_param;
400 sctp_paramhdr_t *auth_chunks = NULL,
406 /* Note: there may be no addresses to embed. */
407 addrs = sctp_bind_addrs_to_raw(&asoc->base.bind_addr, &addrs_len, gfp);
409 initack.init_tag = htonl(asoc->c.my_vtag);
410 initack.a_rwnd = htonl(asoc->rwnd);
411 initack.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
412 initack.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
413 initack.initial_tsn = htonl(asoc->c.initial_tsn);
415 /* FIXME: We really ought to build the cookie right
416 * into the packet instead of allocating more fresh memory.
418 cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
423 /* Calculate the total size of allocation, include the reserved
424 * space for reporting unknown parameters if it is specified.
426 sp = sctp_sk(asoc->base.sk);
427 chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;
429 /* Tell peer that we'll do ECN only if peer advertised such cap. */
430 if (asoc->peer.ecn_capable)
431 chunksize += sizeof(ecap_param);
433 if (asoc->peer.prsctp_capable)
434 chunksize += sizeof(prsctp_param);
436 if (asoc->peer.asconf_capable) {
437 extensions[num_ext] = SCTP_CID_ASCONF;
438 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
442 if (sp->adaptation_ind)
443 chunksize += sizeof(aiparam);
445 if (asoc->peer.auth_capable) {
446 auth_random = (sctp_paramhdr_t *)asoc->c.auth_random;
447 chunksize += ntohs(auth_random->length);
449 auth_hmacs = (sctp_paramhdr_t *)asoc->c.auth_hmacs;
450 if (auth_hmacs->length)
451 chunksize += WORD_ROUND(ntohs(auth_hmacs->length));
455 auth_chunks = (sctp_paramhdr_t *)asoc->c.auth_chunks;
456 if (auth_chunks->length)
457 chunksize += WORD_ROUND(ntohs(auth_chunks->length));
461 extensions[num_ext] = SCTP_CID_AUTH;
466 chunksize += WORD_ROUND(sizeof(sctp_supported_ext_param_t) +
469 /* Now allocate and fill out the chunk. */
470 retval = sctp_make_control(asoc, SCTP_CID_INIT_ACK, 0, chunksize, gfp);
474 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
476 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
477 * HEARTBEAT ACK, * etc.) to the same destination transport
478 * address from which it received the DATA or control chunk
479 * to which it is replying.
481 * [INIT ACK back to where the INIT came from.]
483 retval->transport = chunk->transport;
485 retval->subh.init_hdr =
486 sctp_addto_chunk(retval, sizeof(initack), &initack);
487 retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
488 sctp_addto_chunk(retval, cookie_len, cookie);
489 if (asoc->peer.ecn_capable)
490 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
492 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
493 ext_param.param_hdr.length =
494 htons(sizeof(sctp_supported_ext_param_t) + num_ext);
495 sctp_addto_chunk(retval, sizeof(sctp_supported_ext_param_t),
497 sctp_addto_param(retval, num_ext, extensions);
499 if (asoc->peer.prsctp_capable)
500 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
502 if (sp->adaptation_ind) {
503 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
504 aiparam.param_hdr.length = htons(sizeof(aiparam));
505 aiparam.adaptation_ind = htonl(sp->adaptation_ind);
506 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
509 if (asoc->peer.auth_capable) {
510 sctp_addto_chunk(retval, ntohs(auth_random->length),
513 sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
516 sctp_addto_chunk(retval, ntohs(auth_chunks->length),
520 /* We need to remove the const qualifier at this point. */
521 retval->asoc = (struct sctp_association *) asoc;
530 /* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
532 * This chunk is used only during the initialization of an association.
533 * It is sent by the initiator of an association to its peer to complete
534 * the initialization process. This chunk MUST precede any DATA chunk
535 * sent within the association, but MAY be bundled with one or more DATA
536 * chunks in the same packet.
539 * 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
540 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
541 * | Type = 10 |Chunk Flags | Length |
542 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
545 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
549 * Set to zero on transmit and ignored on receipt.
551 * Length: 16 bits (unsigned integer)
553 * Set to the size of the chunk in bytes, including the 4 bytes of
554 * the chunk header and the size of the Cookie.
556 * Cookie: variable size
558 * This field must contain the exact cookie received in the
559 * State Cookie parameter from the previous INIT ACK.
561 * An implementation SHOULD make the cookie as small as possible
562 * to insure interoperability.
564 struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
565 const struct sctp_chunk *chunk)
567 struct sctp_chunk *retval;
571 cookie = asoc->peer.cookie;
572 cookie_len = asoc->peer.cookie_len;
574 /* Build a cookie echo chunk. */
575 retval = sctp_make_control(asoc, SCTP_CID_COOKIE_ECHO, 0,
576 cookie_len, GFP_ATOMIC);
579 retval->subh.cookie_hdr =
580 sctp_addto_chunk(retval, cookie_len, cookie);
582 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
584 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
585 * HEARTBEAT ACK, * etc.) to the same destination transport
586 * address from which it * received the DATA or control chunk
587 * to which it is replying.
589 * [COOKIE ECHO back to where the INIT ACK came from.]
592 retval->transport = chunk->transport;
598 /* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
600 * This chunk is used only during the initialization of an
601 * association. It is used to acknowledge the receipt of a COOKIE
602 * ECHO chunk. This chunk MUST precede any DATA or SACK chunk sent
603 * within the association, but MAY be bundled with one or more DATA
604 * chunks or SACK chunk in the same SCTP packet.
607 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
608 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
609 * | Type = 11 |Chunk Flags | Length = 4 |
610 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
612 * Chunk Flags: 8 bits
614 * Set to zero on transmit and ignored on receipt.
616 struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
617 const struct sctp_chunk *chunk)
619 struct sctp_chunk *retval;
621 retval = sctp_make_control(asoc, SCTP_CID_COOKIE_ACK, 0, 0, GFP_ATOMIC);
623 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
625 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
626 * HEARTBEAT ACK, * etc.) to the same destination transport
627 * address from which it * received the DATA or control chunk
628 * to which it is replying.
630 * [COOKIE ACK back to where the COOKIE ECHO came from.]
633 retval->transport = chunk->transport;
639 * Appendix A: Explicit Congestion Notification:
642 * RFC 2481 details a specific bit for a sender to send in the header of
643 * its next outbound TCP segment to indicate to its peer that it has
644 * reduced its congestion window. This is termed the CWR bit. For
645 * SCTP the same indication is made by including the CWR chunk.
646 * This chunk contains one data element, i.e. the TSN number that
647 * was sent in the ECNE chunk. This element represents the lowest
648 * TSN number in the datagram that was originally marked with the
652 * 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
653 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
654 * | Chunk Type=13 | Flags=00000000| Chunk Length = 8 |
655 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
656 * | Lowest TSN Number |
657 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
659 * Note: The CWR is considered a Control chunk.
661 struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
662 const __u32 lowest_tsn,
663 const struct sctp_chunk *chunk)
665 struct sctp_chunk *retval;
668 cwr.lowest_tsn = htonl(lowest_tsn);
669 retval = sctp_make_control(asoc, SCTP_CID_ECN_CWR, 0,
670 sizeof(sctp_cwrhdr_t), GFP_ATOMIC);
675 retval->subh.ecn_cwr_hdr =
676 sctp_addto_chunk(retval, sizeof(cwr), &cwr);
678 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
680 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
681 * HEARTBEAT ACK, * etc.) to the same destination transport
682 * address from which it * received the DATA or control chunk
683 * to which it is replying.
685 * [Report a reduced congestion window back to where the ECNE
689 retval->transport = chunk->transport;
695 /* Make an ECNE chunk. This is a congestion experienced report. */
696 struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
697 const __u32 lowest_tsn)
699 struct sctp_chunk *retval;
702 ecne.lowest_tsn = htonl(lowest_tsn);
703 retval = sctp_make_control(asoc, SCTP_CID_ECN_ECNE, 0,
704 sizeof(sctp_ecnehdr_t), GFP_ATOMIC);
707 retval->subh.ecne_hdr =
708 sctp_addto_chunk(retval, sizeof(ecne), &ecne);
714 static void sctp_set_prsctp_policy(struct sctp_chunk *chunk,
715 const struct sctp_sndrcvinfo *sinfo)
717 if (!chunk->asoc->prsctp_enable)
720 if (SCTP_PR_TTL_ENABLED(sinfo->sinfo_flags))
721 chunk->prsctp_param =
722 jiffies + msecs_to_jiffies(sinfo->sinfo_timetolive);
725 /* Make a DATA chunk for the given association from the provided
726 * parameters. However, do not populate the data payload.
728 struct sctp_chunk *sctp_make_datafrag_empty(struct sctp_association *asoc,
729 const struct sctp_sndrcvinfo *sinfo,
730 int data_len, __u8 flags, __u16 ssn,
733 struct sctp_chunk *retval;
734 struct sctp_datahdr dp;
737 /* We assign the TSN as LATE as possible, not here when
738 * creating the chunk.
741 dp.stream = htons(sinfo->sinfo_stream);
742 dp.ppid = sinfo->sinfo_ppid;
744 /* Set the flags for an unordered send. */
745 if (sinfo->sinfo_flags & SCTP_UNORDERED) {
746 flags |= SCTP_DATA_UNORDERED;
751 chunk_len = sizeof(dp) + data_len;
752 retval = sctp_make_data(asoc, flags, chunk_len, gfp);
756 retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
757 memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
758 sctp_set_prsctp_policy(retval, sinfo);
764 /* Create a selective ackowledgement (SACK) for the given
765 * association. This reports on which TSN's we've seen to date,
766 * including duplicates and gaps.
768 struct sctp_chunk *sctp_make_sack(const struct sctp_association *asoc)
770 struct sctp_chunk *retval;
771 struct sctp_sackhdr sack;
774 __u16 num_gabs, num_dup_tsns;
775 struct sctp_association *aptr = (struct sctp_association *)asoc;
776 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
777 struct sctp_gap_ack_block gabs[SCTP_MAX_GABS];
778 struct sctp_transport *trans;
780 memset(gabs, 0, sizeof(gabs));
781 ctsn = sctp_tsnmap_get_ctsn(map);
783 pr_debug("%s: sackCTSNAck sent:0x%x\n", __func__, ctsn);
785 /* How much room is needed in the chunk? */
786 num_gabs = sctp_tsnmap_num_gabs(map, gabs);
787 num_dup_tsns = sctp_tsnmap_num_dups(map);
789 /* Initialize the SACK header. */
790 sack.cum_tsn_ack = htonl(ctsn);
791 sack.a_rwnd = htonl(asoc->a_rwnd);
792 sack.num_gap_ack_blocks = htons(num_gabs);
793 sack.num_dup_tsns = htons(num_dup_tsns);
796 + sizeof(struct sctp_gap_ack_block) * num_gabs
797 + sizeof(__u32) * num_dup_tsns;
799 /* Create the chunk. */
800 retval = sctp_make_control(asoc, SCTP_CID_SACK, 0, len, GFP_ATOMIC);
804 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
806 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
807 * HEARTBEAT ACK, etc.) to the same destination transport
808 * address from which it received the DATA or control chunk to
809 * which it is replying. This rule should also be followed if
810 * the endpoint is bundling DATA chunks together with the
813 * However, when acknowledging multiple DATA chunks received
814 * in packets from different source addresses in a single
815 * SACK, the SACK chunk may be transmitted to one of the
816 * destination transport addresses from which the DATA or
817 * control chunks being acknowledged were received.
819 * [BUG: We do not implement the following paragraph.
820 * Perhaps we should remember the last transport we used for a
821 * SACK and avoid that (if possible) if we have seen any
822 * duplicates. --piggy]
824 * When a receiver of a duplicate DATA chunk sends a SACK to a
825 * multi- homed endpoint it MAY be beneficial to vary the
826 * destination address and not use the source address of the
827 * DATA chunk. The reason being that receiving a duplicate
828 * from a multi-homed endpoint might indicate that the return
829 * path (as specified in the source address of the DATA chunk)
830 * for the SACK is broken.
832 * [Send to the address from which we last received a DATA chunk.]
834 retval->transport = asoc->peer.last_data_from;
836 retval->subh.sack_hdr =
837 sctp_addto_chunk(retval, sizeof(sack), &sack);
839 /* Add the gap ack block information. */
841 sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
844 /* Add the duplicate TSN information. */
846 aptr->stats.idupchunks += num_dup_tsns;
847 sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
848 sctp_tsnmap_get_dups(map));
850 /* Once we have a sack generated, check to see what our sack
851 * generation is, if its 0, reset the transports to 0, and reset
852 * the association generation to 1
854 * The idea is that zero is never used as a valid generation for the
855 * association so no transport will match after a wrap event like this,
856 * Until the next sack
858 if (++aptr->peer.sack_generation == 0) {
859 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
861 trans->sack_generation = 0;
862 aptr->peer.sack_generation = 1;
868 /* Make a SHUTDOWN chunk. */
869 struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
870 const struct sctp_chunk *chunk)
872 struct sctp_chunk *retval;
873 sctp_shutdownhdr_t shut;
876 ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
877 shut.cum_tsn_ack = htonl(ctsn);
879 retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN, 0,
880 sizeof(sctp_shutdownhdr_t), GFP_ATOMIC);
884 retval->subh.shutdown_hdr =
885 sctp_addto_chunk(retval, sizeof(shut), &shut);
888 retval->transport = chunk->transport;
893 struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
894 const struct sctp_chunk *chunk)
896 struct sctp_chunk *retval;
898 retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0,
901 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
903 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
904 * HEARTBEAT ACK, * etc.) to the same destination transport
905 * address from which it * received the DATA or control chunk
906 * to which it is replying.
908 * [ACK back to where the SHUTDOWN came from.]
911 retval->transport = chunk->transport;
916 struct sctp_chunk *sctp_make_shutdown_complete(
917 const struct sctp_association *asoc,
918 const struct sctp_chunk *chunk)
920 struct sctp_chunk *retval;
923 /* Set the T-bit if we have no association (vtag will be
926 flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
928 retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags,
931 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
933 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
934 * HEARTBEAT ACK, * etc.) to the same destination transport
935 * address from which it * received the DATA or control chunk
936 * to which it is replying.
938 * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
942 retval->transport = chunk->transport;
947 /* Create an ABORT. Note that we set the T bit if we have no
948 * association, except when responding to an INIT (sctpimpguide 2.41).
950 struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
951 const struct sctp_chunk *chunk,
954 struct sctp_chunk *retval;
957 /* Set the T-bit if we have no association and 'chunk' is not
958 * an INIT (vtag will be reflected).
961 if (chunk && chunk->chunk_hdr &&
962 chunk->chunk_hdr->type == SCTP_CID_INIT)
965 flags = SCTP_CHUNK_FLAG_T;
968 retval = sctp_make_control(asoc, SCTP_CID_ABORT, flags, hint,
971 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
973 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
974 * HEARTBEAT ACK, * etc.) to the same destination transport
975 * address from which it * received the DATA or control chunk
976 * to which it is replying.
978 * [ABORT back to where the offender came from.]
981 retval->transport = chunk->transport;
986 /* Helper to create ABORT with a NO_USER_DATA error. */
987 struct sctp_chunk *sctp_make_abort_no_data(
988 const struct sctp_association *asoc,
989 const struct sctp_chunk *chunk, __u32 tsn)
991 struct sctp_chunk *retval;
994 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t)
1000 /* Put the tsn back into network byte order. */
1001 payload = htonl(tsn);
1002 sctp_init_cause(retval, SCTP_ERROR_NO_DATA, sizeof(payload));
1003 sctp_addto_chunk(retval, sizeof(payload), (const void *)&payload);
1005 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1007 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1008 * HEARTBEAT ACK, * etc.) to the same destination transport
1009 * address from which it * received the DATA or control chunk
1010 * to which it is replying.
1012 * [ABORT back to where the offender came from.]
1015 retval->transport = chunk->transport;
1021 /* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error. */
1022 struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
1026 struct sctp_chunk *retval;
1027 void *payload = NULL;
1030 retval = sctp_make_abort(asoc, NULL, sizeof(sctp_errhdr_t) + paylen);
1035 /* Put the msg_iov together into payload. */
1036 payload = kmalloc(paylen, GFP_KERNEL);
1040 err = memcpy_from_msg(payload, msg, paylen);
1045 sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, paylen);
1046 sctp_addto_chunk(retval, paylen, payload);
1056 sctp_chunk_free(retval);
1062 /* Append bytes to the end of a parameter. Will panic if chunk is not big
1065 static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
1069 int chunklen = ntohs(chunk->chunk_hdr->length);
1071 target = skb_put(chunk->skb, len);
1074 memcpy(target, data, len);
1076 memset(target, 0, len);
1078 /* Adjust the chunk length field. */
1079 chunk->chunk_hdr->length = htons(chunklen + len);
1080 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1085 /* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
1086 struct sctp_chunk *sctp_make_abort_violation(
1087 const struct sctp_association *asoc,
1088 const struct sctp_chunk *chunk,
1089 const __u8 *payload,
1090 const size_t paylen)
1092 struct sctp_chunk *retval;
1093 struct sctp_paramhdr phdr;
1095 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t) + paylen
1096 + sizeof(sctp_paramhdr_t));
1100 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, paylen
1101 + sizeof(sctp_paramhdr_t));
1103 phdr.type = htons(chunk->chunk_hdr->type);
1104 phdr.length = chunk->chunk_hdr->length;
1105 sctp_addto_chunk(retval, paylen, payload);
1106 sctp_addto_param(retval, sizeof(sctp_paramhdr_t), &phdr);
1112 struct sctp_chunk *sctp_make_violation_paramlen(
1113 const struct sctp_association *asoc,
1114 const struct sctp_chunk *chunk,
1115 struct sctp_paramhdr *param)
1117 struct sctp_chunk *retval;
1118 static const char error[] = "The following parameter had invalid length:";
1119 size_t payload_len = sizeof(error) + sizeof(sctp_errhdr_t) +
1120 sizeof(sctp_paramhdr_t);
1122 retval = sctp_make_abort(asoc, chunk, payload_len);
1126 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION,
1127 sizeof(error) + sizeof(sctp_paramhdr_t));
1128 sctp_addto_chunk(retval, sizeof(error), error);
1129 sctp_addto_param(retval, sizeof(sctp_paramhdr_t), param);
1135 struct sctp_chunk *sctp_make_violation_max_retrans(
1136 const struct sctp_association *asoc,
1137 const struct sctp_chunk *chunk)
1139 struct sctp_chunk *retval;
1140 static const char error[] = "Association exceeded its max_retans count";
1141 size_t payload_len = sizeof(error) + sizeof(sctp_errhdr_t);
1143 retval = sctp_make_abort(asoc, chunk, payload_len);
1147 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, sizeof(error));
1148 sctp_addto_chunk(retval, sizeof(error), error);
1154 /* Make a HEARTBEAT chunk. */
1155 struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
1156 const struct sctp_transport *transport)
1158 struct sctp_chunk *retval;
1159 sctp_sender_hb_info_t hbinfo;
1161 retval = sctp_make_control(asoc, SCTP_CID_HEARTBEAT, 0,
1162 sizeof(hbinfo), GFP_ATOMIC);
1167 hbinfo.param_hdr.type = SCTP_PARAM_HEARTBEAT_INFO;
1168 hbinfo.param_hdr.length = htons(sizeof(sctp_sender_hb_info_t));
1169 hbinfo.daddr = transport->ipaddr;
1170 hbinfo.sent_at = jiffies;
1171 hbinfo.hb_nonce = transport->hb_nonce;
1173 /* Cast away the 'const', as this is just telling the chunk
1174 * what transport it belongs to.
1176 retval->transport = (struct sctp_transport *) transport;
1177 retval->subh.hbs_hdr = sctp_addto_chunk(retval, sizeof(hbinfo),
1184 struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
1185 const struct sctp_chunk *chunk,
1186 const void *payload, const size_t paylen)
1188 struct sctp_chunk *retval;
1190 retval = sctp_make_control(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen,
1195 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
1197 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1199 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1200 * HEARTBEAT ACK, * etc.) to the same destination transport
1201 * address from which it * received the DATA or control chunk
1202 * to which it is replying.
1204 * [HBACK back to where the HEARTBEAT came from.]
1207 retval->transport = chunk->transport;
1213 /* Create an Operation Error chunk with the specified space reserved.
1214 * This routine can be used for containing multiple causes in the chunk.
1216 static struct sctp_chunk *sctp_make_op_error_space(
1217 const struct sctp_association *asoc,
1218 const struct sctp_chunk *chunk,
1221 struct sctp_chunk *retval;
1223 retval = sctp_make_control(asoc, SCTP_CID_ERROR, 0,
1224 sizeof(sctp_errhdr_t) + size, GFP_ATOMIC);
1228 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1230 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1231 * HEARTBEAT ACK, etc.) to the same destination transport
1232 * address from which it received the DATA or control chunk
1233 * to which it is replying.
1237 retval->transport = chunk->transport;
1243 /* Create an Operation Error chunk of a fixed size,
1244 * specifically, max(asoc->pathmtu, SCTP_DEFAULT_MAXSEGMENT)
1245 * This is a helper function to allocate an error chunk for
1246 * for those invalid parameter codes in which we may not want
1247 * to report all the errors, if the incoming chunk is large
1249 static inline struct sctp_chunk *sctp_make_op_error_fixed(
1250 const struct sctp_association *asoc,
1251 const struct sctp_chunk *chunk)
1253 size_t size = asoc ? asoc->pathmtu : 0;
1256 size = SCTP_DEFAULT_MAXSEGMENT;
1258 return sctp_make_op_error_space(asoc, chunk, size);
1261 /* Create an Operation Error chunk. */
1262 struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
1263 const struct sctp_chunk *chunk,
1264 __be16 cause_code, const void *payload,
1265 size_t paylen, size_t reserve_tail)
1267 struct sctp_chunk *retval;
1269 retval = sctp_make_op_error_space(asoc, chunk, paylen + reserve_tail);
1273 sctp_init_cause(retval, cause_code, paylen + reserve_tail);
1274 sctp_addto_chunk(retval, paylen, payload);
1276 sctp_addto_param(retval, reserve_tail, NULL);
1282 struct sctp_chunk *sctp_make_auth(const struct sctp_association *asoc)
1284 struct sctp_chunk *retval;
1285 struct sctp_hmac *hmac_desc;
1286 struct sctp_authhdr auth_hdr;
1289 /* Get the first hmac that the peer told us to use */
1290 hmac_desc = sctp_auth_asoc_get_hmac(asoc);
1291 if (unlikely(!hmac_desc))
1294 retval = sctp_make_control(asoc, SCTP_CID_AUTH, 0,
1295 hmac_desc->hmac_len + sizeof(sctp_authhdr_t),
1300 auth_hdr.hmac_id = htons(hmac_desc->hmac_id);
1301 auth_hdr.shkey_id = htons(asoc->active_key_id);
1303 retval->subh.auth_hdr = sctp_addto_chunk(retval, sizeof(sctp_authhdr_t),
1306 hmac = skb_put(retval->skb, hmac_desc->hmac_len);
1307 memset(hmac, 0, hmac_desc->hmac_len);
1309 /* Adjust the chunk header to include the empty MAC */
1310 retval->chunk_hdr->length =
1311 htons(ntohs(retval->chunk_hdr->length) + hmac_desc->hmac_len);
1312 retval->chunk_end = skb_tail_pointer(retval->skb);
1318 /********************************************************************
1319 * 2nd Level Abstractions
1320 ********************************************************************/
1322 /* Turn an skb into a chunk.
1323 * FIXME: Eventually move the structure directly inside the skb->cb[].
1325 * sctpimpguide-05.txt Section 2.8.2
1326 * M1) Each time a new DATA chunk is transmitted
1327 * set the 'TSN.Missing.Report' count for that TSN to 0. The
1328 * 'TSN.Missing.Report' count will be used to determine missing chunks
1329 * and when to fast retransmit.
1332 struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
1333 const struct sctp_association *asoc,
1334 struct sock *sk, gfp_t gfp)
1336 struct sctp_chunk *retval;
1338 retval = kmem_cache_zalloc(sctp_chunk_cachep, gfp);
1343 pr_debug("%s: chunkifying skb:%p w/o an sk\n", __func__, skb);
1345 INIT_LIST_HEAD(&retval->list);
1347 retval->asoc = (struct sctp_association *)asoc;
1348 retval->singleton = 1;
1350 retval->fast_retransmit = SCTP_CAN_FRTX;
1352 /* Polish the bead hole. */
1353 INIT_LIST_HEAD(&retval->transmitted_list);
1354 INIT_LIST_HEAD(&retval->frag_list);
1355 SCTP_DBG_OBJCNT_INC(chunk);
1356 atomic_set(&retval->refcnt, 1);
1362 /* Set chunk->source and dest based on the IP header in chunk->skb. */
1363 void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
1364 union sctp_addr *dest)
1366 memcpy(&chunk->source, src, sizeof(union sctp_addr));
1367 memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
1370 /* Extract the source address from a chunk. */
1371 const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
1373 /* If we have a known transport, use that. */
1374 if (chunk->transport) {
1375 return &chunk->transport->ipaddr;
1377 /* Otherwise, extract it from the IP header. */
1378 return &chunk->source;
1382 /* Create a new chunk, setting the type and flags headers from the
1383 * arguments, reserving enough space for a 'paylen' byte payload.
1385 static struct sctp_chunk *_sctp_make_chunk(const struct sctp_association *asoc,
1386 __u8 type, __u8 flags, int paylen,
1389 struct sctp_chunk *retval;
1390 sctp_chunkhdr_t *chunk_hdr;
1391 struct sk_buff *skb;
1394 /* No need to allocate LL here, as this is only a chunk. */
1395 skb = alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t) + paylen), gfp);
1399 /* Make room for the chunk header. */
1400 chunk_hdr = (sctp_chunkhdr_t *)skb_put(skb, sizeof(sctp_chunkhdr_t));
1401 chunk_hdr->type = type;
1402 chunk_hdr->flags = flags;
1403 chunk_hdr->length = htons(sizeof(sctp_chunkhdr_t));
1405 sk = asoc ? asoc->base.sk : NULL;
1406 retval = sctp_chunkify(skb, asoc, sk, gfp);
1412 retval->chunk_hdr = chunk_hdr;
1413 retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(struct sctp_chunkhdr);
1415 /* Determine if the chunk needs to be authenticated */
1416 if (sctp_auth_send_cid(type, asoc))
1424 static struct sctp_chunk *sctp_make_data(const struct sctp_association *asoc,
1425 __u8 flags, int paylen, gfp_t gfp)
1427 return _sctp_make_chunk(asoc, SCTP_CID_DATA, flags, paylen, gfp);
1430 static struct sctp_chunk *sctp_make_control(const struct sctp_association *asoc,
1431 __u8 type, __u8 flags, int paylen,
1434 struct sctp_chunk *chunk;
1436 chunk = _sctp_make_chunk(asoc, type, flags, paylen, gfp);
1438 sctp_control_set_owner_w(chunk);
1443 /* Release the memory occupied by a chunk. */
1444 static void sctp_chunk_destroy(struct sctp_chunk *chunk)
1446 BUG_ON(!list_empty(&chunk->list));
1447 list_del_init(&chunk->transmitted_list);
1449 consume_skb(chunk->skb);
1450 consume_skb(chunk->auth_chunk);
1452 SCTP_DBG_OBJCNT_DEC(chunk);
1453 kmem_cache_free(sctp_chunk_cachep, chunk);
1456 /* Possibly, free the chunk. */
1457 void sctp_chunk_free(struct sctp_chunk *chunk)
1459 /* Release our reference on the message tracker. */
1461 sctp_datamsg_put(chunk->msg);
1463 sctp_chunk_put(chunk);
1466 /* Grab a reference to the chunk. */
1467 void sctp_chunk_hold(struct sctp_chunk *ch)
1469 atomic_inc(&ch->refcnt);
1472 /* Release a reference to the chunk. */
1473 void sctp_chunk_put(struct sctp_chunk *ch)
1475 if (atomic_dec_and_test(&ch->refcnt))
1476 sctp_chunk_destroy(ch);
1479 /* Append bytes to the end of a chunk. Will panic if chunk is not big
1482 void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
1486 int chunklen = ntohs(chunk->chunk_hdr->length);
1487 int padlen = WORD_ROUND(chunklen) - chunklen;
1489 padding = skb_put(chunk->skb, padlen);
1490 target = skb_put(chunk->skb, len);
1492 memset(padding, 0, padlen);
1493 memcpy(target, data, len);
1495 /* Adjust the chunk length field. */
1496 chunk->chunk_hdr->length = htons(chunklen + padlen + len);
1497 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1502 /* Append bytes to the end of a chunk. Returns NULL if there isn't sufficient
1503 * space in the chunk
1505 static void *sctp_addto_chunk_fixed(struct sctp_chunk *chunk,
1506 int len, const void *data)
1508 if (skb_tailroom(chunk->skb) >= len)
1509 return sctp_addto_chunk(chunk, len, data);
1514 /* Append bytes from user space to the end of a chunk. Will panic if
1515 * chunk is not big enough.
1516 * Returns a kernel err value.
1518 int sctp_user_addto_chunk(struct sctp_chunk *chunk, int len,
1519 struct iov_iter *from)
1524 /* Make room in chunk for data. */
1525 target = skb_put(chunk->skb, len);
1527 /* Copy data (whole iovec) into chunk */
1528 copied = copy_from_iter(target, len, from);
1532 /* Adjust the chunk length field. */
1533 chunk->chunk_hdr->length =
1534 htons(ntohs(chunk->chunk_hdr->length) + len);
1535 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1540 /* Helper function to assign a TSN if needed. This assumes that both
1541 * the data_hdr and association have already been assigned.
1543 void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
1545 struct sctp_datamsg *msg;
1546 struct sctp_chunk *lchunk;
1547 struct sctp_stream *stream;
1554 /* All fragments will be on the same stream */
1555 sid = ntohs(chunk->subh.data_hdr->stream);
1556 stream = &chunk->asoc->ssnmap->out;
1558 /* Now assign the sequence number to the entire message.
1559 * All fragments must have the same stream sequence number.
1562 list_for_each_entry(lchunk, &msg->chunks, frag_list) {
1563 if (lchunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
1566 if (lchunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
1567 ssn = sctp_ssn_next(stream, sid);
1569 ssn = sctp_ssn_peek(stream, sid);
1572 lchunk->subh.data_hdr->ssn = htons(ssn);
1573 lchunk->has_ssn = 1;
1577 /* Helper function to assign a TSN if needed. This assumes that both
1578 * the data_hdr and association have already been assigned.
1580 void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
1582 if (!chunk->has_tsn) {
1583 /* This is the last possible instant to
1586 chunk->subh.data_hdr->tsn =
1587 htonl(sctp_association_get_next_tsn(chunk->asoc));
1592 /* Create a CLOSED association to use with an incoming packet. */
1593 struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
1594 struct sctp_chunk *chunk,
1597 struct sctp_association *asoc;
1598 struct sk_buff *skb;
1602 /* Create the bare association. */
1603 scope = sctp_scope(sctp_source(chunk));
1604 asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
1609 /* Create an entry for the source address of the packet. */
1610 af = sctp_get_af_specific(ipver2af(ip_hdr(skb)->version));
1613 af->from_skb(&asoc->c.peer_addr, skb, 1);
1618 sctp_association_free(asoc);
1622 /* Build a cookie representing asoc.
1623 * This INCLUDES the param header needed to put the cookie in the INIT ACK.
1625 static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
1626 const struct sctp_association *asoc,
1627 const struct sctp_chunk *init_chunk,
1629 const __u8 *raw_addrs, int addrs_len)
1631 sctp_cookie_param_t *retval;
1632 struct sctp_signed_cookie *cookie;
1633 int headersize, bodysize;
1635 /* Header size is static data prior to the actual cookie, including
1638 headersize = sizeof(sctp_paramhdr_t) +
1639 (sizeof(struct sctp_signed_cookie) -
1640 sizeof(struct sctp_cookie));
1641 bodysize = sizeof(struct sctp_cookie)
1642 + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
1644 /* Pad out the cookie to a multiple to make the signature
1645 * functions simpler to write.
1647 if (bodysize % SCTP_COOKIE_MULTIPLE)
1648 bodysize += SCTP_COOKIE_MULTIPLE
1649 - (bodysize % SCTP_COOKIE_MULTIPLE);
1650 *cookie_len = headersize + bodysize;
1652 /* Clear this memory since we are sending this data structure
1653 * out on the network.
1655 retval = kzalloc(*cookie_len, GFP_ATOMIC);
1659 cookie = (struct sctp_signed_cookie *) retval->body;
1661 /* Set up the parameter header. */
1662 retval->p.type = SCTP_PARAM_STATE_COOKIE;
1663 retval->p.length = htons(*cookie_len);
1665 /* Copy the cookie part of the association itself. */
1666 cookie->c = asoc->c;
1667 /* Save the raw address list length in the cookie. */
1668 cookie->c.raw_addr_list_len = addrs_len;
1670 /* Remember PR-SCTP capability. */
1671 cookie->c.prsctp_capable = asoc->peer.prsctp_capable;
1673 /* Save adaptation indication in the cookie. */
1674 cookie->c.adaptation_ind = asoc->peer.adaptation_ind;
1676 /* Set an expiration time for the cookie. */
1677 cookie->c.expiration = ktime_add(asoc->cookie_life,
1680 /* Copy the peer's init packet. */
1681 memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
1682 ntohs(init_chunk->chunk_hdr->length));
1684 /* Copy the raw local address list of the association. */
1685 memcpy((__u8 *)&cookie->c.peer_init[0] +
1686 ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
1688 if (sctp_sk(ep->base.sk)->hmac) {
1689 SHASH_DESC_ON_STACK(desc, sctp_sk(ep->base.sk)->hmac);
1692 /* Sign the message. */
1693 desc->tfm = sctp_sk(ep->base.sk)->hmac;
1696 err = crypto_shash_setkey(desc->tfm, ep->secret_key,
1697 sizeof(ep->secret_key)) ?:
1698 crypto_shash_digest(desc, (u8 *)&cookie->c, bodysize,
1700 shash_desc_zero(desc);
1714 /* Unpack the cookie from COOKIE ECHO chunk, recreating the association. */
1715 struct sctp_association *sctp_unpack_cookie(
1716 const struct sctp_endpoint *ep,
1717 const struct sctp_association *asoc,
1718 struct sctp_chunk *chunk, gfp_t gfp,
1719 int *error, struct sctp_chunk **errp)
1721 struct sctp_association *retval = NULL;
1722 struct sctp_signed_cookie *cookie;
1723 struct sctp_cookie *bear_cookie;
1724 int headersize, bodysize, fixed_size;
1725 __u8 *digest = ep->digest;
1728 struct sk_buff *skb = chunk->skb;
1731 /* Header size is static data prior to the actual cookie, including
1734 headersize = sizeof(sctp_chunkhdr_t) +
1735 (sizeof(struct sctp_signed_cookie) -
1736 sizeof(struct sctp_cookie));
1737 bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
1738 fixed_size = headersize + sizeof(struct sctp_cookie);
1740 /* Verify that the chunk looks like it even has a cookie.
1741 * There must be enough room for our cookie and our peer's
1744 len = ntohs(chunk->chunk_hdr->length);
1745 if (len < fixed_size + sizeof(struct sctp_chunkhdr))
1748 /* Verify that the cookie has been padded out. */
1749 if (bodysize % SCTP_COOKIE_MULTIPLE)
1752 /* Process the cookie. */
1753 cookie = chunk->subh.cookie_hdr;
1754 bear_cookie = &cookie->c;
1756 if (!sctp_sk(ep->base.sk)->hmac)
1759 /* Check the signature. */
1761 SHASH_DESC_ON_STACK(desc, sctp_sk(ep->base.sk)->hmac);
1764 desc->tfm = sctp_sk(ep->base.sk)->hmac;
1767 err = crypto_shash_setkey(desc->tfm, ep->secret_key,
1768 sizeof(ep->secret_key)) ?:
1769 crypto_shash_digest(desc, (u8 *)bear_cookie, bodysize,
1771 shash_desc_zero(desc);
1774 *error = -SCTP_IERROR_NOMEM;
1779 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1780 *error = -SCTP_IERROR_BAD_SIG;
1785 /* IG Section 2.35.2:
1786 * 3) Compare the port numbers and the verification tag contained
1787 * within the COOKIE ECHO chunk to the actual port numbers and the
1788 * verification tag within the SCTP common header of the received
1789 * packet. If these values do not match the packet MUST be silently
1792 if (ntohl(chunk->sctp_hdr->vtag) != bear_cookie->my_vtag) {
1793 *error = -SCTP_IERROR_BAD_TAG;
1797 if (chunk->sctp_hdr->source != bear_cookie->peer_addr.v4.sin_port ||
1798 ntohs(chunk->sctp_hdr->dest) != bear_cookie->my_port) {
1799 *error = -SCTP_IERROR_BAD_PORTS;
1803 /* Check to see if the cookie is stale. If there is already
1804 * an association, there is no need to check cookie's expiration
1805 * for init collision case of lost COOKIE ACK.
1806 * If skb has been timestamped, then use the stamp, otherwise
1807 * use current time. This introduces a small possibility that
1808 * that a cookie may be considered expired, but his would only slow
1809 * down the new association establishment instead of every packet.
1811 if (sock_flag(ep->base.sk, SOCK_TIMESTAMP))
1812 kt = skb_get_ktime(skb);
1814 kt = ktime_get_real();
1816 if (!asoc && ktime_before(bear_cookie->expiration, kt)) {
1818 * Section 3.3.10.3 Stale Cookie Error (3)
1822 * Stale Cookie Error: Indicates the receipt of a valid State
1823 * Cookie that has expired.
1825 len = ntohs(chunk->chunk_hdr->length);
1826 *errp = sctp_make_op_error_space(asoc, chunk, len);
1828 suseconds_t usecs = ktime_to_us(ktime_sub(kt, bear_cookie->expiration));
1829 __be32 n = htonl(usecs);
1831 sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
1833 sctp_addto_chunk(*errp, sizeof(n), &n);
1834 *error = -SCTP_IERROR_STALE_COOKIE;
1836 *error = -SCTP_IERROR_NOMEM;
1841 /* Make a new base association. */
1842 scope = sctp_scope(sctp_source(chunk));
1843 retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
1845 *error = -SCTP_IERROR_NOMEM;
1849 /* Set up our peer's port number. */
1850 retval->peer.port = ntohs(chunk->sctp_hdr->source);
1852 /* Populate the association from the cookie. */
1853 memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
1855 if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
1857 *error = -SCTP_IERROR_NOMEM;
1861 /* Also, add the destination address. */
1862 if (list_empty(&retval->base.bind_addr.address_list)) {
1863 sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest,
1864 sizeof(chunk->dest), SCTP_ADDR_SRC,
1868 retval->next_tsn = retval->c.initial_tsn;
1869 retval->ctsn_ack_point = retval->next_tsn - 1;
1870 retval->addip_serial = retval->c.initial_tsn;
1871 retval->adv_peer_ack_point = retval->ctsn_ack_point;
1872 retval->peer.prsctp_capable = retval->c.prsctp_capable;
1873 retval->peer.adaptation_ind = retval->c.adaptation_ind;
1875 /* The INIT stuff will be done by the side effects. */
1880 sctp_association_free(retval);
1885 /* Yikes! The packet is either corrupt or deliberately
1888 *error = -SCTP_IERROR_MALFORMED;
1892 /********************************************************************
1893 * 3rd Level Abstractions
1894 ********************************************************************/
1896 struct __sctp_missing {
1902 * Report a missing mandatory parameter.
1904 static int sctp_process_missing_param(const struct sctp_association *asoc,
1905 sctp_param_t paramtype,
1906 struct sctp_chunk *chunk,
1907 struct sctp_chunk **errp)
1909 struct __sctp_missing report;
1912 len = WORD_ROUND(sizeof(report));
1914 /* Make an ERROR chunk, preparing enough room for
1915 * returning multiple unknown parameters.
1918 *errp = sctp_make_op_error_space(asoc, chunk, len);
1921 report.num_missing = htonl(1);
1922 report.type = paramtype;
1923 sctp_init_cause(*errp, SCTP_ERROR_MISS_PARAM,
1925 sctp_addto_chunk(*errp, sizeof(report), &report);
1928 /* Stop processing this chunk. */
1932 /* Report an Invalid Mandatory Parameter. */
1933 static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
1934 struct sctp_chunk *chunk,
1935 struct sctp_chunk **errp)
1937 /* Invalid Mandatory Parameter Error has no payload. */
1940 *errp = sctp_make_op_error_space(asoc, chunk, 0);
1943 sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, 0);
1945 /* Stop processing this chunk. */
1949 static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
1950 struct sctp_paramhdr *param,
1951 const struct sctp_chunk *chunk,
1952 struct sctp_chunk **errp)
1954 /* This is a fatal error. Any accumulated non-fatal errors are
1958 sctp_chunk_free(*errp);
1960 /* Create an error chunk and fill it in with our payload. */
1961 *errp = sctp_make_violation_paramlen(asoc, chunk, param);
1967 /* Do not attempt to handle the HOST_NAME parm. However, do
1968 * send back an indicator to the peer.
1970 static int sctp_process_hn_param(const struct sctp_association *asoc,
1971 union sctp_params param,
1972 struct sctp_chunk *chunk,
1973 struct sctp_chunk **errp)
1975 __u16 len = ntohs(param.p->length);
1977 /* Processing of the HOST_NAME parameter will generate an
1978 * ABORT. If we've accumulated any non-fatal errors, they
1979 * would be unrecognized parameters and we should not include
1980 * them in the ABORT.
1983 sctp_chunk_free(*errp);
1985 *errp = sctp_make_op_error_space(asoc, chunk, len);
1988 sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED, len);
1989 sctp_addto_chunk(*errp, len, param.v);
1992 /* Stop processing this chunk. */
1996 static int sctp_verify_ext_param(struct net *net, union sctp_params param)
1998 __u16 num_ext = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2000 int have_asconf = 0;
2003 for (i = 0; i < num_ext; i++) {
2004 switch (param.ext->chunks[i]) {
2008 case SCTP_CID_ASCONF:
2009 case SCTP_CID_ASCONF_ACK:
2015 /* ADD-IP Security: The draft requires us to ABORT or ignore the
2016 * INIT/INIT-ACK if ADD-IP is listed, but AUTH is not. Do this
2017 * only if ADD-IP is turned on and we are not backward-compatible
2020 if (net->sctp.addip_noauth)
2023 if (net->sctp.addip_enable && !have_auth && have_asconf)
2029 static void sctp_process_ext_param(struct sctp_association *asoc,
2030 union sctp_params param)
2032 struct net *net = sock_net(asoc->base.sk);
2033 __u16 num_ext = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2036 for (i = 0; i < num_ext; i++) {
2037 switch (param.ext->chunks[i]) {
2038 case SCTP_CID_FWD_TSN:
2039 if (asoc->prsctp_enable && !asoc->peer.prsctp_capable)
2040 asoc->peer.prsctp_capable = 1;
2043 /* if the peer reports AUTH, assume that he
2046 if (asoc->ep->auth_enable)
2047 asoc->peer.auth_capable = 1;
2049 case SCTP_CID_ASCONF:
2050 case SCTP_CID_ASCONF_ACK:
2051 if (net->sctp.addip_enable)
2052 asoc->peer.asconf_capable = 1;
2060 /* RFC 3.2.1 & the Implementers Guide 2.2.
2062 * The Parameter Types are encoded such that the
2063 * highest-order two bits specify the action that must be
2064 * taken if the processing endpoint does not recognize the
2067 * 00 - Stop processing this parameter; do not process any further
2068 * parameters within this chunk
2070 * 01 - Stop processing this parameter, do not process any further
2071 * parameters within this chunk, and report the unrecognized
2072 * parameter in an 'Unrecognized Parameter' ERROR chunk.
2074 * 10 - Skip this parameter and continue processing.
2076 * 11 - Skip this parameter and continue processing but
2077 * report the unrecognized parameter in an
2078 * 'Unrecognized Parameter' ERROR chunk.
2081 * SCTP_IERROR_NO_ERROR - continue with the chunk
2082 * SCTP_IERROR_ERROR - stop and report an error.
2083 * SCTP_IERROR_NOMEME - out of memory.
2085 static sctp_ierror_t sctp_process_unk_param(const struct sctp_association *asoc,
2086 union sctp_params param,
2087 struct sctp_chunk *chunk,
2088 struct sctp_chunk **errp)
2090 int retval = SCTP_IERROR_NO_ERROR;
2092 switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
2093 case SCTP_PARAM_ACTION_DISCARD:
2094 retval = SCTP_IERROR_ERROR;
2096 case SCTP_PARAM_ACTION_SKIP:
2098 case SCTP_PARAM_ACTION_DISCARD_ERR:
2099 retval = SCTP_IERROR_ERROR;
2101 case SCTP_PARAM_ACTION_SKIP_ERR:
2102 /* Make an ERROR chunk, preparing enough room for
2103 * returning multiple unknown parameters.
2106 *errp = sctp_make_op_error_fixed(asoc, chunk);
2109 if (!sctp_init_cause_fixed(*errp, SCTP_ERROR_UNKNOWN_PARAM,
2110 WORD_ROUND(ntohs(param.p->length))))
2111 sctp_addto_chunk_fixed(*errp,
2112 WORD_ROUND(ntohs(param.p->length)),
2115 /* If there is no memory for generating the ERROR
2116 * report as specified, an ABORT will be triggered
2117 * to the peer and the association won't be
2120 retval = SCTP_IERROR_NOMEM;
2130 /* Verify variable length parameters
2132 * SCTP_IERROR_ABORT - trigger an ABORT
2133 * SCTP_IERROR_NOMEM - out of memory (abort)
2134 * SCTP_IERROR_ERROR - stop processing, trigger an ERROR
2135 * SCTP_IERROR_NO_ERROR - continue with the chunk
2137 static sctp_ierror_t sctp_verify_param(struct net *net,
2138 const struct sctp_endpoint *ep,
2139 const struct sctp_association *asoc,
2140 union sctp_params param,
2142 struct sctp_chunk *chunk,
2143 struct sctp_chunk **err_chunk)
2145 struct sctp_hmac_algo_param *hmacs;
2146 int retval = SCTP_IERROR_NO_ERROR;
2147 __u16 n_elt, id = 0;
2150 /* FIXME - This routine is not looking at each parameter per the
2151 * chunk type, i.e., unrecognized parameters should be further
2152 * identified based on the chunk id.
2155 switch (param.p->type) {
2156 case SCTP_PARAM_IPV4_ADDRESS:
2157 case SCTP_PARAM_IPV6_ADDRESS:
2158 case SCTP_PARAM_COOKIE_PRESERVATIVE:
2159 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2160 case SCTP_PARAM_STATE_COOKIE:
2161 case SCTP_PARAM_HEARTBEAT_INFO:
2162 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2163 case SCTP_PARAM_ECN_CAPABLE:
2164 case SCTP_PARAM_ADAPTATION_LAYER_IND:
2167 case SCTP_PARAM_SUPPORTED_EXT:
2168 if (!sctp_verify_ext_param(net, param))
2169 return SCTP_IERROR_ABORT;
2172 case SCTP_PARAM_SET_PRIMARY:
2173 if (net->sctp.addip_enable)
2177 case SCTP_PARAM_HOST_NAME_ADDRESS:
2178 /* Tell the peer, we won't support this param. */
2179 sctp_process_hn_param(asoc, param, chunk, err_chunk);
2180 retval = SCTP_IERROR_ABORT;
2183 case SCTP_PARAM_FWD_TSN_SUPPORT:
2184 if (ep->prsctp_enable)
2188 case SCTP_PARAM_RANDOM:
2189 if (!ep->auth_enable)
2192 /* SCTP-AUTH: Secion 6.1
2193 * If the random number is not 32 byte long the association
2194 * MUST be aborted. The ABORT chunk SHOULD contain the error
2195 * cause 'Protocol Violation'.
2197 if (SCTP_AUTH_RANDOM_LENGTH !=
2198 ntohs(param.p->length) - sizeof(sctp_paramhdr_t)) {
2199 sctp_process_inv_paramlength(asoc, param.p,
2201 retval = SCTP_IERROR_ABORT;
2205 case SCTP_PARAM_CHUNKS:
2206 if (!ep->auth_enable)
2209 /* SCTP-AUTH: Section 3.2
2210 * The CHUNKS parameter MUST be included once in the INIT or
2211 * INIT-ACK chunk if the sender wants to receive authenticated
2212 * chunks. Its maximum length is 260 bytes.
2214 if (260 < ntohs(param.p->length)) {
2215 sctp_process_inv_paramlength(asoc, param.p,
2217 retval = SCTP_IERROR_ABORT;
2221 case SCTP_PARAM_HMAC_ALGO:
2222 if (!ep->auth_enable)
2225 hmacs = (struct sctp_hmac_algo_param *)param.p;
2226 n_elt = (ntohs(param.p->length) - sizeof(sctp_paramhdr_t)) >> 1;
2228 /* SCTP-AUTH: Section 6.1
2229 * The HMAC algorithm based on SHA-1 MUST be supported and
2230 * included in the HMAC-ALGO parameter.
2232 for (i = 0; i < n_elt; i++) {
2233 id = ntohs(hmacs->hmac_ids[i]);
2235 if (id == SCTP_AUTH_HMAC_ID_SHA1)
2239 if (id != SCTP_AUTH_HMAC_ID_SHA1) {
2240 sctp_process_inv_paramlength(asoc, param.p, chunk,
2242 retval = SCTP_IERROR_ABORT;
2247 pr_debug("%s: unrecognized param:%d for chunk:%d\n",
2248 __func__, ntohs(param.p->type), cid);
2250 retval = sctp_process_unk_param(asoc, param, chunk, err_chunk);
2256 /* Verify the INIT packet before we process it. */
2257 int sctp_verify_init(struct net *net, const struct sctp_endpoint *ep,
2258 const struct sctp_association *asoc, sctp_cid_t cid,
2259 sctp_init_chunk_t *peer_init, struct sctp_chunk *chunk,
2260 struct sctp_chunk **errp)
2262 union sctp_params param;
2263 bool has_cookie = false;
2266 /* Check for missing mandatory parameters. Note: Initial TSN is
2267 * also mandatory, but is not checked here since the valid range
2268 * is 0..2**32-1. RFC4960, section 3.3.3.
2270 if (peer_init->init_hdr.num_outbound_streams == 0 ||
2271 peer_init->init_hdr.num_inbound_streams == 0 ||
2272 peer_init->init_hdr.init_tag == 0 ||
2273 ntohl(peer_init->init_hdr.a_rwnd) < SCTP_DEFAULT_MINWINDOW)
2274 return sctp_process_inv_mandatory(asoc, chunk, errp);
2276 sctp_walk_params(param, peer_init, init_hdr.params) {
2277 if (param.p->type == SCTP_PARAM_STATE_COOKIE)
2281 /* There is a possibility that a parameter length was bad and
2282 * in that case we would have stoped walking the parameters.
2283 * The current param.p would point at the bad one.
2284 * Current consensus on the mailing list is to generate a PROTOCOL
2285 * VIOLATION error. We build the ERROR chunk here and let the normal
2286 * error handling code build and send the packet.
2288 if (param.v != (void *)chunk->chunk_end)
2289 return sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
2291 /* The only missing mandatory param possible today is
2292 * the state cookie for an INIT-ACK chunk.
2294 if ((SCTP_CID_INIT_ACK == cid) && !has_cookie)
2295 return sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
2298 /* Verify all the variable length parameters */
2299 sctp_walk_params(param, peer_init, init_hdr.params) {
2300 result = sctp_verify_param(net, ep, asoc, param, cid,
2303 case SCTP_IERROR_ABORT:
2304 case SCTP_IERROR_NOMEM:
2306 case SCTP_IERROR_ERROR:
2308 case SCTP_IERROR_NO_ERROR:
2313 } /* for (loop through all parameters) */
2318 /* Unpack the parameters in an INIT packet into an association.
2319 * Returns 0 on failure, else success.
2320 * FIXME: This is an association method.
2322 int sctp_process_init(struct sctp_association *asoc, struct sctp_chunk *chunk,
2323 const union sctp_addr *peer_addr,
2324 sctp_init_chunk_t *peer_init, gfp_t gfp)
2326 struct net *net = sock_net(asoc->base.sk);
2327 union sctp_params param;
2328 struct sctp_transport *transport;
2329 struct list_head *pos, *temp;
2331 union sctp_addr addr;
2335 /* We must include the address that the INIT packet came from.
2336 * This is the only address that matters for an INIT packet.
2337 * When processing a COOKIE ECHO, we retrieve the from address
2338 * of the INIT from the cookie.
2341 /* This implementation defaults to making the first transport
2342 * added as the primary transport. The source address seems to
2343 * be a a better choice than any of the embedded addresses.
2345 if (!sctp_assoc_add_peer(asoc, peer_addr, gfp, SCTP_ACTIVE))
2348 if (sctp_cmp_addr_exact(sctp_source(chunk), peer_addr))
2351 /* Process the initialization parameters. */
2352 sctp_walk_params(param, peer_init, init_hdr.params) {
2353 if (!src_match && (param.p->type == SCTP_PARAM_IPV4_ADDRESS ||
2354 param.p->type == SCTP_PARAM_IPV6_ADDRESS)) {
2355 af = sctp_get_af_specific(param_type2af(param.p->type));
2356 af->from_addr_param(&addr, param.addr,
2357 chunk->sctp_hdr->source, 0);
2358 if (sctp_cmp_addr_exact(sctp_source(chunk), &addr))
2362 if (!sctp_process_param(asoc, param, peer_addr, gfp))
2366 /* source address of chunk may not match any valid address */
2370 /* AUTH: After processing the parameters, make sure that we
2371 * have all the required info to potentially do authentications.
2373 if (asoc->peer.auth_capable && (!asoc->peer.peer_random ||
2374 !asoc->peer.peer_hmacs))
2375 asoc->peer.auth_capable = 0;
2377 /* In a non-backward compatible mode, if the peer claims
2378 * support for ADD-IP but not AUTH, the ADD-IP spec states
2379 * that we MUST ABORT the association. Section 6. The section
2380 * also give us an option to silently ignore the packet, which
2381 * is what we'll do here.
2383 if (!net->sctp.addip_noauth &&
2384 (asoc->peer.asconf_capable && !asoc->peer.auth_capable)) {
2385 asoc->peer.addip_disabled_mask |= (SCTP_PARAM_ADD_IP |
2387 SCTP_PARAM_SET_PRIMARY);
2388 asoc->peer.asconf_capable = 0;
2392 /* Walk list of transports, removing transports in the UNKNOWN state. */
2393 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2394 transport = list_entry(pos, struct sctp_transport, transports);
2395 if (transport->state == SCTP_UNKNOWN) {
2396 sctp_assoc_rm_peer(asoc, transport);
2400 /* The fixed INIT headers are always in network byte
2403 asoc->peer.i.init_tag =
2404 ntohl(peer_init->init_hdr.init_tag);
2405 asoc->peer.i.a_rwnd =
2406 ntohl(peer_init->init_hdr.a_rwnd);
2407 asoc->peer.i.num_outbound_streams =
2408 ntohs(peer_init->init_hdr.num_outbound_streams);
2409 asoc->peer.i.num_inbound_streams =
2410 ntohs(peer_init->init_hdr.num_inbound_streams);
2411 asoc->peer.i.initial_tsn =
2412 ntohl(peer_init->init_hdr.initial_tsn);
2414 /* Apply the upper bounds for output streams based on peer's
2415 * number of inbound streams.
2417 if (asoc->c.sinit_num_ostreams >
2418 ntohs(peer_init->init_hdr.num_inbound_streams)) {
2419 asoc->c.sinit_num_ostreams =
2420 ntohs(peer_init->init_hdr.num_inbound_streams);
2423 if (asoc->c.sinit_max_instreams >
2424 ntohs(peer_init->init_hdr.num_outbound_streams)) {
2425 asoc->c.sinit_max_instreams =
2426 ntohs(peer_init->init_hdr.num_outbound_streams);
2429 /* Copy Initiation tag from INIT to VT_peer in cookie. */
2430 asoc->c.peer_vtag = asoc->peer.i.init_tag;
2432 /* Peer Rwnd : Current calculated value of the peer's rwnd. */
2433 asoc->peer.rwnd = asoc->peer.i.a_rwnd;
2435 /* Copy cookie in case we need to resend COOKIE-ECHO. */
2436 cookie = asoc->peer.cookie;
2438 asoc->peer.cookie = kmemdup(cookie, asoc->peer.cookie_len, gfp);
2439 if (!asoc->peer.cookie)
2443 /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
2444 * high (for example, implementations MAY use the size of the receiver
2445 * advertised window).
2447 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
2449 transport->ssthresh = asoc->peer.i.a_rwnd;
2452 /* Set up the TSN tracking pieces. */
2453 if (!sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
2454 asoc->peer.i.initial_tsn, gfp))
2457 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
2459 * The stream sequence number in all the streams shall start
2460 * from 0 when the association is established. Also, when the
2461 * stream sequence number reaches the value 65535 the next
2462 * stream sequence number shall be set to 0.
2465 /* Allocate storage for the negotiated streams if it is not a temporary
2471 asoc->ssnmap = sctp_ssnmap_new(asoc->c.sinit_max_instreams,
2472 asoc->c.sinit_num_ostreams, gfp);
2476 error = sctp_assoc_set_id(asoc, gfp);
2481 /* ADDIP Section 4.1 ASCONF Chunk Procedures
2483 * When an endpoint has an ASCONF signaled change to be sent to the
2484 * remote endpoint it should do the following:
2486 * A2) A serial number should be assigned to the Chunk. The serial
2487 * number should be a monotonically increasing number. All serial
2488 * numbers are defined to be initialized at the start of the
2489 * association to the same value as the Initial TSN.
2491 asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
2495 /* Release the transport structures. */
2496 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2497 transport = list_entry(pos, struct sctp_transport, transports);
2498 if (transport->state != SCTP_ACTIVE)
2499 sctp_assoc_rm_peer(asoc, transport);
2507 /* Update asoc with the option described in param.
2509 * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
2511 * asoc is the association to update.
2512 * param is the variable length parameter to use for update.
2513 * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
2514 * If the current packet is an INIT we want to minimize the amount of
2515 * work we do. In particular, we should not build transport
2516 * structures for the addresses.
2518 static int sctp_process_param(struct sctp_association *asoc,
2519 union sctp_params param,
2520 const union sctp_addr *peer_addr,
2523 struct net *net = sock_net(asoc->base.sk);
2524 union sctp_addr addr;
2531 union sctp_addr_param *addr_param;
2532 struct sctp_transport *t;
2533 struct sctp_endpoint *ep = asoc->ep;
2535 /* We maintain all INIT parameters in network byte order all the
2536 * time. This allows us to not worry about whether the parameters
2537 * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
2539 switch (param.p->type) {
2540 case SCTP_PARAM_IPV6_ADDRESS:
2541 if (PF_INET6 != asoc->base.sk->sk_family)
2545 case SCTP_PARAM_IPV4_ADDRESS:
2546 /* v4 addresses are not allowed on v6-only socket */
2547 if (ipv6_only_sock(asoc->base.sk))
2550 af = sctp_get_af_specific(param_type2af(param.p->type));
2551 af->from_addr_param(&addr, param.addr, htons(asoc->peer.port), 0);
2552 scope = sctp_scope(peer_addr);
2553 if (sctp_in_scope(net, &addr, scope))
2554 if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_UNCONFIRMED))
2558 case SCTP_PARAM_COOKIE_PRESERVATIVE:
2559 if (!net->sctp.cookie_preserve_enable)
2562 stale = ntohl(param.life->lifespan_increment);
2564 /* Suggested Cookie Life span increment's unit is msec,
2567 asoc->cookie_life = ktime_add_ms(asoc->cookie_life, stale);
2570 case SCTP_PARAM_HOST_NAME_ADDRESS:
2571 pr_debug("%s: unimplemented SCTP_HOST_NAME_ADDRESS\n", __func__);
2574 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2575 /* Turn off the default values first so we'll know which
2576 * ones are really set by the peer.
2578 asoc->peer.ipv4_address = 0;
2579 asoc->peer.ipv6_address = 0;
2581 /* Assume that peer supports the address family
2582 * by which it sends a packet.
2584 if (peer_addr->sa.sa_family == AF_INET6)
2585 asoc->peer.ipv6_address = 1;
2586 else if (peer_addr->sa.sa_family == AF_INET)
2587 asoc->peer.ipv4_address = 1;
2589 /* Cycle through address types; avoid divide by 0. */
2590 sat = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2592 sat /= sizeof(__u16);
2594 for (i = 0; i < sat; ++i) {
2595 switch (param.sat->types[i]) {
2596 case SCTP_PARAM_IPV4_ADDRESS:
2597 asoc->peer.ipv4_address = 1;
2600 case SCTP_PARAM_IPV6_ADDRESS:
2601 if (PF_INET6 == asoc->base.sk->sk_family)
2602 asoc->peer.ipv6_address = 1;
2605 case SCTP_PARAM_HOST_NAME_ADDRESS:
2606 asoc->peer.hostname_address = 1;
2609 default: /* Just ignore anything else. */
2615 case SCTP_PARAM_STATE_COOKIE:
2616 asoc->peer.cookie_len =
2617 ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2618 asoc->peer.cookie = param.cookie->body;
2621 case SCTP_PARAM_HEARTBEAT_INFO:
2622 /* Would be odd to receive, but it causes no problems. */
2625 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2626 /* Rejected during verify stage. */
2629 case SCTP_PARAM_ECN_CAPABLE:
2630 asoc->peer.ecn_capable = 1;
2633 case SCTP_PARAM_ADAPTATION_LAYER_IND:
2634 asoc->peer.adaptation_ind = ntohl(param.aind->adaptation_ind);
2637 case SCTP_PARAM_SET_PRIMARY:
2638 if (!net->sctp.addip_enable)
2641 addr_param = param.v + sizeof(sctp_addip_param_t);
2643 af = sctp_get_af_specific(param_type2af(addr_param->p.type));
2647 af->from_addr_param(&addr, addr_param,
2648 htons(asoc->peer.port), 0);
2650 /* if the address is invalid, we can't process it.
2651 * XXX: see spec for what to do.
2653 if (!af->addr_valid(&addr, NULL, NULL))
2656 t = sctp_assoc_lookup_paddr(asoc, &addr);
2660 sctp_assoc_set_primary(asoc, t);
2663 case SCTP_PARAM_SUPPORTED_EXT:
2664 sctp_process_ext_param(asoc, param);
2667 case SCTP_PARAM_FWD_TSN_SUPPORT:
2668 if (asoc->prsctp_enable) {
2669 asoc->peer.prsctp_capable = 1;
2675 case SCTP_PARAM_RANDOM:
2676 if (!ep->auth_enable)
2679 /* Save peer's random parameter */
2680 asoc->peer.peer_random = kmemdup(param.p,
2681 ntohs(param.p->length), gfp);
2682 if (!asoc->peer.peer_random) {
2688 case SCTP_PARAM_HMAC_ALGO:
2689 if (!ep->auth_enable)
2692 /* Save peer's HMAC list */
2693 asoc->peer.peer_hmacs = kmemdup(param.p,
2694 ntohs(param.p->length), gfp);
2695 if (!asoc->peer.peer_hmacs) {
2700 /* Set the default HMAC the peer requested*/
2701 sctp_auth_asoc_set_default_hmac(asoc, param.hmac_algo);
2704 case SCTP_PARAM_CHUNKS:
2705 if (!ep->auth_enable)
2708 asoc->peer.peer_chunks = kmemdup(param.p,
2709 ntohs(param.p->length), gfp);
2710 if (!asoc->peer.peer_chunks)
2715 /* Any unrecognized parameters should have been caught
2716 * and handled by sctp_verify_param() which should be
2717 * called prior to this routine. Simply log the error
2720 pr_debug("%s: ignoring param:%d for association:%p.\n",
2721 __func__, ntohs(param.p->type), asoc);
2728 /* Select a new verification tag. */
2729 __u32 sctp_generate_tag(const struct sctp_endpoint *ep)
2731 /* I believe that this random number generator complies with RFC1750.
2732 * A tag of 0 is reserved for special cases (e.g. INIT).
2737 get_random_bytes(&x, sizeof(__u32));
2743 /* Select an initial TSN to send during startup. */
2744 __u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
2748 get_random_bytes(&retval, sizeof(__u32));
2753 * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
2755 * 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
2756 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2757 * | Type = 0xC1 | Chunk Flags | Chunk Length |
2758 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2760 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2761 * | Address Parameter |
2762 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2763 * | ASCONF Parameter #1 |
2764 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2768 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2769 * | ASCONF Parameter #N |
2770 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2772 * Address Parameter and other parameter will not be wrapped in this function
2774 static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
2775 union sctp_addr *addr,
2778 sctp_addiphdr_t asconf;
2779 struct sctp_chunk *retval;
2780 int length = sizeof(asconf) + vparam_len;
2781 union sctp_addr_param addrparam;
2783 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2785 addrlen = af->to_addr_param(addr, &addrparam);
2790 /* Create the chunk. */
2791 retval = sctp_make_control(asoc, SCTP_CID_ASCONF, 0, length,
2796 asconf.serial = htonl(asoc->addip_serial++);
2798 retval->subh.addip_hdr =
2799 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2800 retval->param_hdr.v =
2801 sctp_addto_chunk(retval, addrlen, &addrparam);
2807 * 3.2.1 Add IP Address
2809 * 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
2810 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2811 * | Type = 0xC001 | Length = Variable |
2812 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2813 * | ASCONF-Request Correlation ID |
2814 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2815 * | Address Parameter |
2816 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2818 * 3.2.2 Delete IP Address
2820 * 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
2821 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2822 * | Type = 0xC002 | Length = Variable |
2823 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2824 * | ASCONF-Request Correlation ID |
2825 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2826 * | Address Parameter |
2827 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2830 struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
2831 union sctp_addr *laddr,
2832 struct sockaddr *addrs,
2836 sctp_addip_param_t param;
2837 struct sctp_chunk *retval;
2838 union sctp_addr_param addr_param;
2839 union sctp_addr *addr;
2842 int paramlen = sizeof(param);
2843 int addr_param_len = 0;
2848 /* Get total length of all the address parameters. */
2850 for (i = 0; i < addrcnt; i++) {
2852 af = sctp_get_af_specific(addr->v4.sin_family);
2853 addr_param_len = af->to_addr_param(addr, &addr_param);
2855 totallen += paramlen;
2856 totallen += addr_param_len;
2858 addr_buf += af->sockaddr_len;
2859 if (asoc->asconf_addr_del_pending && !del_pickup) {
2860 /* reuse the parameter length from the same scope one */
2861 totallen += paramlen;
2862 totallen += addr_param_len;
2865 pr_debug("%s: picked same-scope del_pending addr, "
2866 "totallen for all addresses is %d\n",
2867 __func__, totallen);
2871 /* Create an asconf chunk with the required length. */
2872 retval = sctp_make_asconf(asoc, laddr, totallen);
2876 /* Add the address parameters to the asconf chunk. */
2878 for (i = 0; i < addrcnt; i++) {
2880 af = sctp_get_af_specific(addr->v4.sin_family);
2881 addr_param_len = af->to_addr_param(addr, &addr_param);
2882 param.param_hdr.type = flags;
2883 param.param_hdr.length = htons(paramlen + addr_param_len);
2886 sctp_addto_chunk(retval, paramlen, ¶m);
2887 sctp_addto_chunk(retval, addr_param_len, &addr_param);
2889 addr_buf += af->sockaddr_len;
2891 if (flags == SCTP_PARAM_ADD_IP && del_pickup) {
2892 addr = asoc->asconf_addr_del_pending;
2893 af = sctp_get_af_specific(addr->v4.sin_family);
2894 addr_param_len = af->to_addr_param(addr, &addr_param);
2895 param.param_hdr.type = SCTP_PARAM_DEL_IP;
2896 param.param_hdr.length = htons(paramlen + addr_param_len);
2899 sctp_addto_chunk(retval, paramlen, ¶m);
2900 sctp_addto_chunk(retval, addr_param_len, &addr_param);
2906 * 3.2.4 Set Primary IP Address
2908 * 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
2909 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2910 * | Type =0xC004 | Length = Variable |
2911 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2912 * | ASCONF-Request Correlation ID |
2913 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2914 * | Address Parameter |
2915 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2917 * Create an ASCONF chunk with Set Primary IP address parameter.
2919 struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
2920 union sctp_addr *addr)
2922 sctp_addip_param_t param;
2923 struct sctp_chunk *retval;
2924 int len = sizeof(param);
2925 union sctp_addr_param addrparam;
2927 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2929 addrlen = af->to_addr_param(addr, &addrparam);
2934 /* Create the chunk and make asconf header. */
2935 retval = sctp_make_asconf(asoc, addr, len);
2939 param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
2940 param.param_hdr.length = htons(len);
2943 sctp_addto_chunk(retval, sizeof(param), ¶m);
2944 sctp_addto_chunk(retval, addrlen, &addrparam);
2949 /* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2951 * 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
2952 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2953 * | Type = 0x80 | Chunk Flags | Chunk Length |
2954 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2956 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2957 * | ASCONF Parameter Response#1 |
2958 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2962 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2963 * | ASCONF Parameter Response#N |
2964 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2966 * Create an ASCONF_ACK chunk with enough space for the parameter responses.
2968 static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
2969 __u32 serial, int vparam_len)
2971 sctp_addiphdr_t asconf;
2972 struct sctp_chunk *retval;
2973 int length = sizeof(asconf) + vparam_len;
2975 /* Create the chunk. */
2976 retval = sctp_make_control(asoc, SCTP_CID_ASCONF_ACK, 0, length,
2981 asconf.serial = htonl(serial);
2983 retval->subh.addip_hdr =
2984 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2989 /* Add response parameters to an ASCONF_ACK chunk. */
2990 static void sctp_add_asconf_response(struct sctp_chunk *chunk, __be32 crr_id,
2991 __be16 err_code, sctp_addip_param_t *asconf_param)
2993 sctp_addip_param_t ack_param;
2994 sctp_errhdr_t err_param;
2995 int asconf_param_len = 0;
2996 int err_param_len = 0;
2997 __be16 response_type;
2999 if (SCTP_ERROR_NO_ERROR == err_code) {
3000 response_type = SCTP_PARAM_SUCCESS_REPORT;
3002 response_type = SCTP_PARAM_ERR_CAUSE;
3003 err_param_len = sizeof(err_param);
3006 ntohs(asconf_param->param_hdr.length);
3009 /* Add Success Indication or Error Cause Indication parameter. */
3010 ack_param.param_hdr.type = response_type;
3011 ack_param.param_hdr.length = htons(sizeof(ack_param) +
3014 ack_param.crr_id = crr_id;
3015 sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
3017 if (SCTP_ERROR_NO_ERROR == err_code)
3020 /* Add Error Cause parameter. */
3021 err_param.cause = err_code;
3022 err_param.length = htons(err_param_len + asconf_param_len);
3023 sctp_addto_chunk(chunk, err_param_len, &err_param);
3025 /* Add the failed TLV copied from ASCONF chunk. */
3027 sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
3030 /* Process a asconf parameter. */
3031 static __be16 sctp_process_asconf_param(struct sctp_association *asoc,
3032 struct sctp_chunk *asconf,
3033 sctp_addip_param_t *asconf_param)
3035 struct sctp_transport *peer;
3037 union sctp_addr addr;
3038 union sctp_addr_param *addr_param;
3040 addr_param = (void *)asconf_param + sizeof(sctp_addip_param_t);
3042 if (asconf_param->param_hdr.type != SCTP_PARAM_ADD_IP &&
3043 asconf_param->param_hdr.type != SCTP_PARAM_DEL_IP &&
3044 asconf_param->param_hdr.type != SCTP_PARAM_SET_PRIMARY)
3045 return SCTP_ERROR_UNKNOWN_PARAM;
3047 switch (addr_param->p.type) {
3048 case SCTP_PARAM_IPV6_ADDRESS:
3049 if (!asoc->peer.ipv6_address)
3050 return SCTP_ERROR_DNS_FAILED;
3052 case SCTP_PARAM_IPV4_ADDRESS:
3053 if (!asoc->peer.ipv4_address)
3054 return SCTP_ERROR_DNS_FAILED;
3057 return SCTP_ERROR_DNS_FAILED;
3060 af = sctp_get_af_specific(param_type2af(addr_param->p.type));
3062 return SCTP_ERROR_DNS_FAILED;
3064 af->from_addr_param(&addr, addr_param, htons(asoc->peer.port), 0);
3066 /* ADDIP 4.2.1 This parameter MUST NOT contain a broadcast
3067 * or multicast address.
3068 * (note: wildcard is permitted and requires special handling so
3069 * make sure we check for that)
3071 if (!af->is_any(&addr) && !af->addr_valid(&addr, NULL, asconf->skb))
3072 return SCTP_ERROR_DNS_FAILED;
3074 switch (asconf_param->param_hdr.type) {
3075 case SCTP_PARAM_ADD_IP:
3077 * If the address 0.0.0.0 or ::0 is provided, the source
3078 * address of the packet MUST be added.
3080 if (af->is_any(&addr))
3081 memcpy(&addr, &asconf->source, sizeof(addr));
3083 /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
3084 * request and does not have the local resources to add this
3085 * new address to the association, it MUST return an Error
3086 * Cause TLV set to the new error code 'Operation Refused
3087 * Due to Resource Shortage'.
3090 peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC, SCTP_UNCONFIRMED);
3092 return SCTP_ERROR_RSRC_LOW;
3094 /* Start the heartbeat timer. */
3095 sctp_transport_reset_hb_timer(peer);
3096 asoc->new_transport = peer;
3098 case SCTP_PARAM_DEL_IP:
3099 /* ADDIP 4.3 D7) If a request is received to delete the
3100 * last remaining IP address of a peer endpoint, the receiver
3101 * MUST send an Error Cause TLV with the error cause set to the
3102 * new error code 'Request to Delete Last Remaining IP Address'.
3104 if (asoc->peer.transport_count == 1)
3105 return SCTP_ERROR_DEL_LAST_IP;
3107 /* ADDIP 4.3 D8) If a request is received to delete an IP
3108 * address which is also the source address of the IP packet
3109 * which contained the ASCONF chunk, the receiver MUST reject
3110 * this request. To reject the request the receiver MUST send
3111 * an Error Cause TLV set to the new error code 'Request to
3112 * Delete Source IP Address'
3114 if (sctp_cmp_addr_exact(&asconf->source, &addr))
3115 return SCTP_ERROR_DEL_SRC_IP;
3118 * If the address 0.0.0.0 or ::0 is provided, all
3119 * addresses of the peer except the source address of the
3120 * packet MUST be deleted.
3122 if (af->is_any(&addr)) {
3123 sctp_assoc_set_primary(asoc, asconf->transport);
3124 sctp_assoc_del_nonprimary_peers(asoc,
3126 return SCTP_ERROR_NO_ERROR;
3129 /* If the address is not part of the association, the
3130 * ASCONF-ACK with Error Cause Indication Parameter
3131 * which including cause of Unresolvable Address should
3134 peer = sctp_assoc_lookup_paddr(asoc, &addr);
3136 return SCTP_ERROR_DNS_FAILED;
3138 sctp_assoc_rm_peer(asoc, peer);
3140 case SCTP_PARAM_SET_PRIMARY:
3141 /* ADDIP Section 4.2.4
3142 * If the address 0.0.0.0 or ::0 is provided, the receiver
3143 * MAY mark the source address of the packet as its
3146 if (af->is_any(&addr))
3147 memcpy(&addr.v4, sctp_source(asconf), sizeof(addr));
3149 peer = sctp_assoc_lookup_paddr(asoc, &addr);
3151 return SCTP_ERROR_DNS_FAILED;
3153 sctp_assoc_set_primary(asoc, peer);
3157 return SCTP_ERROR_NO_ERROR;
3160 /* Verify the ASCONF packet before we process it. */
3161 bool sctp_verify_asconf(const struct sctp_association *asoc,
3162 struct sctp_chunk *chunk, bool addr_param_needed,
3163 struct sctp_paramhdr **errp)
3165 sctp_addip_chunk_t *addip = (sctp_addip_chunk_t *) chunk->chunk_hdr;
3166 union sctp_params param;
3167 bool addr_param_seen = false;
3169 sctp_walk_params(param, addip, addip_hdr.params) {
3170 size_t length = ntohs(param.p->length);
3173 switch (param.p->type) {
3174 case SCTP_PARAM_ERR_CAUSE:
3176 case SCTP_PARAM_IPV4_ADDRESS:
3177 if (length != sizeof(sctp_ipv4addr_param_t))
3179 /* ensure there is only one addr param and it's in the
3180 * beginning of addip_hdr params, or we reject it.
3182 if (param.v != addip->addip_hdr.params)
3184 addr_param_seen = true;
3186 case SCTP_PARAM_IPV6_ADDRESS:
3187 if (length != sizeof(sctp_ipv6addr_param_t))
3189 if (param.v != addip->addip_hdr.params)
3191 addr_param_seen = true;
3193 case SCTP_PARAM_ADD_IP:
3194 case SCTP_PARAM_DEL_IP:
3195 case SCTP_PARAM_SET_PRIMARY:
3196 /* In ASCONF chunks, these need to be first. */
3197 if (addr_param_needed && !addr_param_seen)
3199 length = ntohs(param.addip->param_hdr.length);
3200 if (length < sizeof(sctp_addip_param_t) +
3201 sizeof(sctp_paramhdr_t))
3204 case SCTP_PARAM_SUCCESS_REPORT:
3205 case SCTP_PARAM_ADAPTATION_LAYER_IND:
3206 if (length != sizeof(sctp_addip_param_t))
3210 /* This is unkown to us, reject! */
3215 /* Remaining sanity checks. */
3216 if (addr_param_needed && !addr_param_seen)
3218 if (!addr_param_needed && addr_param_seen)
3220 if (param.v != chunk->chunk_end)
3226 /* Process an incoming ASCONF chunk with the next expected serial no. and
3227 * return an ASCONF_ACK chunk to be sent in response.
3229 struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
3230 struct sctp_chunk *asconf)
3232 sctp_addip_chunk_t *addip = (sctp_addip_chunk_t *) asconf->chunk_hdr;
3233 bool all_param_pass = true;
3234 union sctp_params param;
3235 sctp_addiphdr_t *hdr;
3236 union sctp_addr_param *addr_param;
3237 sctp_addip_param_t *asconf_param;
3238 struct sctp_chunk *asconf_ack;
3244 chunk_len = ntohs(asconf->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
3245 hdr = (sctp_addiphdr_t *)asconf->skb->data;
3246 serial = ntohl(hdr->serial);
3248 /* Skip the addiphdr and store a pointer to address parameter. */
3249 length = sizeof(sctp_addiphdr_t);
3250 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
3251 chunk_len -= length;
3253 /* Skip the address parameter and store a pointer to the first
3256 length = ntohs(addr_param->p.length);
3257 asconf_param = (void *)addr_param + length;
3258 chunk_len -= length;
3260 /* create an ASCONF_ACK chunk.
3261 * Based on the definitions of parameters, we know that the size of
3262 * ASCONF_ACK parameters are less than or equal to the fourfold of ASCONF
3265 asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 4);
3269 /* Process the TLVs contained within the ASCONF chunk. */
3270 sctp_walk_params(param, addip, addip_hdr.params) {
3271 /* Skip preceeding address parameters. */
3272 if (param.p->type == SCTP_PARAM_IPV4_ADDRESS ||
3273 param.p->type == SCTP_PARAM_IPV6_ADDRESS)
3276 err_code = sctp_process_asconf_param(asoc, asconf,
3279 * If an error response is received for a TLV parameter,
3280 * all TLVs with no response before the failed TLV are
3281 * considered successful if not reported. All TLVs after
3282 * the failed response are considered unsuccessful unless
3283 * a specific success indication is present for the parameter.
3285 if (err_code != SCTP_ERROR_NO_ERROR)
3286 all_param_pass = false;
3287 if (!all_param_pass)
3288 sctp_add_asconf_response(asconf_ack, param.addip->crr_id,
3289 err_code, param.addip);
3291 /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
3292 * an IP address sends an 'Out of Resource' in its response, it
3293 * MUST also fail any subsequent add or delete requests bundled
3296 if (err_code == SCTP_ERROR_RSRC_LOW)
3300 asoc->peer.addip_serial++;
3302 /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
3303 * after freeing the reference to old asconf ack if any.
3306 sctp_chunk_hold(asconf_ack);
3307 list_add_tail(&asconf_ack->transmitted_list,
3308 &asoc->asconf_ack_list);
3314 /* Process a asconf parameter that is successfully acked. */
3315 static void sctp_asconf_param_success(struct sctp_association *asoc,
3316 sctp_addip_param_t *asconf_param)
3319 union sctp_addr addr;
3320 struct sctp_bind_addr *bp = &asoc->base.bind_addr;
3321 union sctp_addr_param *addr_param;
3322 struct sctp_transport *transport;
3323 struct sctp_sockaddr_entry *saddr;
3325 addr_param = (void *)asconf_param + sizeof(sctp_addip_param_t);
3327 /* We have checked the packet before, so we do not check again. */
3328 af = sctp_get_af_specific(param_type2af(addr_param->p.type));
3329 af->from_addr_param(&addr, addr_param, htons(bp->port), 0);
3331 switch (asconf_param->param_hdr.type) {
3332 case SCTP_PARAM_ADD_IP:
3333 /* This is always done in BH context with a socket lock
3334 * held, so the list can not change.
3337 list_for_each_entry(saddr, &bp->address_list, list) {
3338 if (sctp_cmp_addr_exact(&saddr->a, &addr))
3339 saddr->state = SCTP_ADDR_SRC;
3342 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
3344 dst_release(transport->dst);
3345 transport->dst = NULL;
3348 case SCTP_PARAM_DEL_IP:
3350 sctp_del_bind_addr(bp, &addr);
3351 if (asoc->asconf_addr_del_pending != NULL &&
3352 sctp_cmp_addr_exact(asoc->asconf_addr_del_pending, &addr)) {
3353 kfree(asoc->asconf_addr_del_pending);
3354 asoc->asconf_addr_del_pending = NULL;
3357 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
3359 dst_release(transport->dst);
3360 transport->dst = NULL;
3368 /* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
3369 * for the given asconf parameter. If there is no response for this parameter,
3370 * return the error code based on the third argument 'no_err'.
3372 * A7) If an error response is received for a TLV parameter, all TLVs with no
3373 * response before the failed TLV are considered successful if not reported.
3374 * All TLVs after the failed response are considered unsuccessful unless a
3375 * specific success indication is present for the parameter.
3377 static __be16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
3378 sctp_addip_param_t *asconf_param,
3381 sctp_addip_param_t *asconf_ack_param;
3382 sctp_errhdr_t *err_param;
3388 err_code = SCTP_ERROR_NO_ERROR;
3390 err_code = SCTP_ERROR_REQ_REFUSED;
3392 asconf_ack_len = ntohs(asconf_ack->chunk_hdr->length) -
3393 sizeof(sctp_chunkhdr_t);
3395 /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
3396 * the first asconf_ack parameter.
3398 length = sizeof(sctp_addiphdr_t);
3399 asconf_ack_param = (sctp_addip_param_t *)(asconf_ack->skb->data +
3401 asconf_ack_len -= length;
3403 while (asconf_ack_len > 0) {
3404 if (asconf_ack_param->crr_id == asconf_param->crr_id) {
3405 switch (asconf_ack_param->param_hdr.type) {
3406 case SCTP_PARAM_SUCCESS_REPORT:
3407 return SCTP_ERROR_NO_ERROR;
3408 case SCTP_PARAM_ERR_CAUSE:
3409 length = sizeof(sctp_addip_param_t);
3410 err_param = (void *)asconf_ack_param + length;
3411 asconf_ack_len -= length;
3412 if (asconf_ack_len > 0)
3413 return err_param->cause;
3415 return SCTP_ERROR_INV_PARAM;
3418 return SCTP_ERROR_INV_PARAM;
3422 length = ntohs(asconf_ack_param->param_hdr.length);
3423 asconf_ack_param = (void *)asconf_ack_param + length;
3424 asconf_ack_len -= length;
3430 /* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
3431 int sctp_process_asconf_ack(struct sctp_association *asoc,
3432 struct sctp_chunk *asconf_ack)
3434 struct sctp_chunk *asconf = asoc->addip_last_asconf;
3435 union sctp_addr_param *addr_param;
3436 sctp_addip_param_t *asconf_param;
3438 int asconf_len = asconf->skb->len;
3439 int all_param_pass = 0;
3442 __be16 err_code = SCTP_ERROR_NO_ERROR;
3444 /* Skip the chunkhdr and addiphdr from the last asconf sent and store
3445 * a pointer to address parameter.
3447 length = sizeof(sctp_addip_chunk_t);
3448 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
3449 asconf_len -= length;
3451 /* Skip the address parameter in the last asconf sent and store a
3452 * pointer to the first asconf parameter.
3454 length = ntohs(addr_param->p.length);
3455 asconf_param = (void *)addr_param + length;
3456 asconf_len -= length;
3459 * A8) If there is no response(s) to specific TLV parameter(s), and no
3460 * failures are indicated, then all request(s) are considered
3463 if (asconf_ack->skb->len == sizeof(sctp_addiphdr_t))
3466 /* Process the TLVs contained in the last sent ASCONF chunk. */
3467 while (asconf_len > 0) {
3469 err_code = SCTP_ERROR_NO_ERROR;
3471 err_code = sctp_get_asconf_response(asconf_ack,
3474 if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
3479 case SCTP_ERROR_NO_ERROR:
3480 sctp_asconf_param_success(asoc, asconf_param);
3483 case SCTP_ERROR_RSRC_LOW:
3487 case SCTP_ERROR_UNKNOWN_PARAM:
3488 /* Disable sending this type of asconf parameter in
3491 asoc->peer.addip_disabled_mask |=
3492 asconf_param->param_hdr.type;
3495 case SCTP_ERROR_REQ_REFUSED:
3496 case SCTP_ERROR_DEL_LAST_IP:
3497 case SCTP_ERROR_DEL_SRC_IP:
3502 /* Skip the processed asconf parameter and move to the next
3505 length = ntohs(asconf_param->param_hdr.length);
3506 asconf_param = (void *)asconf_param + length;
3507 asconf_len -= length;
3510 if (no_err && asoc->src_out_of_asoc_ok) {
3511 asoc->src_out_of_asoc_ok = 0;
3512 sctp_transport_immediate_rtx(asoc->peer.primary_path);
3515 /* Free the cached last sent asconf chunk. */
3516 list_del_init(&asconf->transmitted_list);
3517 sctp_chunk_free(asconf);
3518 asoc->addip_last_asconf = NULL;
3523 /* Make a FWD TSN chunk. */
3524 struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
3525 __u32 new_cum_tsn, size_t nstreams,
3526 struct sctp_fwdtsn_skip *skiplist)
3528 struct sctp_chunk *retval = NULL;
3529 struct sctp_fwdtsn_hdr ftsn_hdr;
3530 struct sctp_fwdtsn_skip skip;
3534 hint = (nstreams + 1) * sizeof(__u32);
3536 retval = sctp_make_control(asoc, SCTP_CID_FWD_TSN, 0, hint, GFP_ATOMIC);
3541 ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
3542 retval->subh.fwdtsn_hdr =
3543 sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
3545 for (i = 0; i < nstreams; i++) {
3546 skip.stream = skiplist[i].stream;
3547 skip.ssn = skiplist[i].ssn;
3548 sctp_addto_chunk(retval, sizeof(skip), &skip);