1 /* SCTP kernel reference 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 reference 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 * The SCTP reference 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 * The SCTP reference implementation is distributed in the hope that it
20 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
21 * ************************
22 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 * See the GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with GNU CC; see the file COPYING. If not, write to
27 * the Free Software Foundation, 59 Temple Place - Suite 330,
28 * Boston, MA 02111-1307, USA.
30 * Please send any bug reports or fixes you make to the
32 * lksctp developers <lksctp-developers@lists.sourceforge.net>
34 * Or submit a bug report through the following website:
35 * http://www.sf.net/projects/lksctp
37 * Written or modified by:
38 * La Monte H.P. Yarroll <piggy@acm.org>
39 * Karl Knutson <karl@athena.chicago.il.us>
40 * C. Robin <chris@hundredacre.ac.uk>
41 * Jon Grimm <jgrimm@us.ibm.com>
42 * Xingang Guo <xingang.guo@intel.com>
43 * Dajiang Zhang <dajiang.zhang@nokia.com>
44 * Sridhar Samudrala <sri@us.ibm.com>
45 * Daisy Chang <daisyc@us.ibm.com>
46 * Ardelle Fan <ardelle.fan@intel.com>
47 * Kevin Gao <kevin.gao@intel.com>
49 * Any bugs reported given to us we will try to fix... any fixes shared will
50 * be incorporated into the next SCTP release.
53 #include <linux/types.h>
54 #include <linux/kernel.h>
56 #include <linux/ipv6.h>
57 #include <linux/net.h>
58 #include <linux/inet.h>
59 #include <asm/scatterlist.h>
60 #include <linux/crypto.h>
63 #include <linux/skbuff.h>
64 #include <linux/random.h> /* for get_random_bytes */
65 #include <net/sctp/sctp.h>
66 #include <net/sctp/sm.h>
69 struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
70 __u8 type, __u8 flags, int paylen);
71 static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
72 const struct sctp_association *asoc,
73 const struct sctp_chunk *init_chunk,
75 const __u8 *raw_addrs, int addrs_len);
76 static int sctp_process_param(struct sctp_association *asoc,
77 union sctp_params param,
78 const union sctp_addr *peer_addr,
81 /* What was the inbound interface for this chunk? */
82 int sctp_chunk_iif(const struct sctp_chunk *chunk)
87 af = sctp_get_af_specific(ipver2af(ip_hdr(chunk->skb)->version));
89 iif = af->skb_iif(chunk->skb);
94 /* RFC 2960 3.3.2 Initiation (INIT) (1)
96 * Note 2: The ECN capable field is reserved for future use of
97 * Explicit Congestion Notification.
99 static const struct sctp_paramhdr ecap_param = {
100 SCTP_PARAM_ECN_CAPABLE,
101 __constant_htons(sizeof(struct sctp_paramhdr)),
103 static const struct sctp_paramhdr prsctp_param = {
104 SCTP_PARAM_FWD_TSN_SUPPORT,
105 __constant_htons(sizeof(struct sctp_paramhdr)),
108 /* A helper to initialize to initialize an op error inside a
109 * provided chunk, as most cause codes will be embedded inside an
112 void sctp_init_cause(struct sctp_chunk *chunk, __be16 cause_code,
118 /* Cause code constants are now defined in network order. */
119 err.cause = cause_code;
120 len = sizeof(sctp_errhdr_t) + paylen;
121 err.length = htons(len);
122 chunk->subh.err_hdr = sctp_addto_chunk(chunk, sizeof(sctp_errhdr_t), &err);
125 /* 3.3.2 Initiation (INIT) (1)
127 * This chunk is used to initiate a SCTP association between two
128 * endpoints. The format of the INIT chunk is shown below:
131 * 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
132 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
133 * | Type = 1 | Chunk Flags | Chunk Length |
134 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
136 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
137 * | Advertised Receiver Window Credit (a_rwnd) |
138 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
139 * | Number of Outbound Streams | Number of Inbound Streams |
140 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
142 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
144 * / Optional/Variable-Length Parameters /
146 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
149 * The INIT chunk contains the following parameters. Unless otherwise
150 * noted, each parameter MUST only be included once in the INIT chunk.
152 * Fixed Parameters Status
153 * ----------------------------------------------
154 * Initiate Tag Mandatory
155 * Advertised Receiver Window Credit Mandatory
156 * Number of Outbound Streams Mandatory
157 * Number of Inbound Streams Mandatory
158 * Initial TSN Mandatory
160 * Variable Parameters Status Type Value
161 * -------------------------------------------------------------
162 * IPv4 Address (Note 1) Optional 5
163 * IPv6 Address (Note 1) Optional 6
164 * Cookie Preservative Optional 9
165 * Reserved for ECN Capable (Note 2) Optional 32768 (0x8000)
166 * Host Name Address (Note 3) Optional 11
167 * Supported Address Types (Note 4) Optional 12
169 struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
170 const struct sctp_bind_addr *bp,
171 gfp_t gfp, int vparam_len)
174 union sctp_params addrs;
176 struct sctp_chunk *retval = NULL;
177 int num_types, addrs_len = 0;
178 struct sctp_sock *sp;
179 sctp_supported_addrs_param_t sat;
181 sctp_adaptation_ind_param_t aiparam;
182 sctp_supported_ext_param_t ext_param;
185 sctp_paramhdr_t *auth_chunks = NULL,
188 /* RFC 2960 3.3.2 Initiation (INIT) (1)
190 * Note 1: The INIT chunks can contain multiple addresses that
191 * can be IPv4 and/or IPv6 in any combination.
195 /* Convert the provided bind address list to raw format. */
196 addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, gfp);
198 init.init_tag = htonl(asoc->c.my_vtag);
199 init.a_rwnd = htonl(asoc->rwnd);
200 init.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
201 init.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
202 init.initial_tsn = htonl(asoc->c.initial_tsn);
204 /* How many address types are needed? */
205 sp = sctp_sk(asoc->base.sk);
206 num_types = sp->pf->supported_addrs(sp, types);
208 chunksize = sizeof(init) + addrs_len + SCTP_SAT_LEN(num_types);
209 chunksize += sizeof(ecap_param);
210 if (sctp_prsctp_enable) {
211 chunksize += sizeof(prsctp_param);
212 extensions[num_ext] = SCTP_CID_FWD_TSN;
215 /* ADDIP: Section 4.2.7:
216 * An implementation supporting this extension [ADDIP] MUST list
217 * the ASCONF,the ASCONF-ACK, and the AUTH chunks in its INIT and
218 * INIT-ACK parameters.
220 if (sctp_addip_enable) {
221 extensions[num_ext] = SCTP_CID_ASCONF;
222 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
226 chunksize += sizeof(aiparam);
227 chunksize += vparam_len;
229 /* Account for AUTH related parameters */
230 if (sctp_auth_enable) {
231 /* Add random parameter length*/
232 chunksize += sizeof(asoc->c.auth_random);
234 /* Add HMACS parameter length if any were defined */
235 auth_hmacs = (sctp_paramhdr_t *)asoc->c.auth_hmacs;
236 if (auth_hmacs->length)
237 chunksize += ntohs(auth_hmacs->length);
241 /* Add CHUNKS parameter length */
242 auth_chunks = (sctp_paramhdr_t *)asoc->c.auth_chunks;
243 if (auth_chunks->length)
244 chunksize += ntohs(auth_chunks->length);
248 extensions[num_ext] = SCTP_CID_AUTH;
252 /* If we have any extensions to report, account for that */
254 chunksize += sizeof(sctp_supported_ext_param_t) + num_ext;
256 /* RFC 2960 3.3.2 Initiation (INIT) (1)
258 * Note 3: An INIT chunk MUST NOT contain more than one Host
259 * Name address parameter. Moreover, the sender of the INIT
260 * MUST NOT combine any other address types with the Host Name
261 * address in the INIT. The receiver of INIT MUST ignore any
262 * other address types if the Host Name address parameter is
263 * present in the received INIT chunk.
265 * PLEASE DO NOT FIXME [This version does not support Host Name.]
268 retval = sctp_make_chunk(asoc, SCTP_CID_INIT, 0, chunksize);
272 retval->subh.init_hdr =
273 sctp_addto_chunk(retval, sizeof(init), &init);
274 retval->param_hdr.v =
275 sctp_addto_chunk(retval, addrs_len, addrs.v);
277 /* RFC 2960 3.3.2 Initiation (INIT) (1)
279 * Note 4: This parameter, when present, specifies all the
280 * address types the sending endpoint can support. The absence
281 * of this parameter indicates that the sending endpoint can
282 * support any address type.
284 sat.param_hdr.type = SCTP_PARAM_SUPPORTED_ADDRESS_TYPES;
285 sat.param_hdr.length = htons(SCTP_SAT_LEN(num_types));
286 sctp_addto_chunk(retval, sizeof(sat), &sat);
287 sctp_addto_chunk(retval, num_types * sizeof(__u16), &types);
289 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
291 /* Add the supported extensions paramter. Be nice and add this
292 * fist before addiding the parameters for the extensions themselves
295 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
296 ext_param.param_hdr.length =
297 htons(sizeof(sctp_supported_ext_param_t) + num_ext);
298 sctp_addto_chunk(retval, sizeof(sctp_supported_ext_param_t),
300 sctp_addto_chunk(retval, num_ext, extensions);
303 if (sctp_prsctp_enable)
304 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
306 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
307 aiparam.param_hdr.length = htons(sizeof(aiparam));
308 aiparam.adaptation_ind = htonl(sp->adaptation_ind);
309 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
311 /* Add SCTP-AUTH chunks to the parameter list */
312 if (sctp_auth_enable) {
313 sctp_addto_chunk(retval, sizeof(asoc->c.auth_random),
314 asoc->c.auth_random);
316 sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
319 sctp_addto_chunk(retval, ntohs(auth_chunks->length),
327 struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
328 const struct sctp_chunk *chunk,
329 gfp_t gfp, int unkparam_len)
331 sctp_inithdr_t initack;
332 struct sctp_chunk *retval;
333 union sctp_params addrs;
335 sctp_cookie_param_t *cookie;
338 sctp_adaptation_ind_param_t aiparam;
339 sctp_supported_ext_param_t ext_param;
342 sctp_paramhdr_t *auth_chunks = NULL,
348 /* Note: there may be no addresses to embed. */
349 addrs = sctp_bind_addrs_to_raw(&asoc->base.bind_addr, &addrs_len, gfp);
351 initack.init_tag = htonl(asoc->c.my_vtag);
352 initack.a_rwnd = htonl(asoc->rwnd);
353 initack.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
354 initack.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
355 initack.initial_tsn = htonl(asoc->c.initial_tsn);
357 /* FIXME: We really ought to build the cookie right
358 * into the packet instead of allocating more fresh memory.
360 cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
365 /* Calculate the total size of allocation, include the reserved
366 * space for reporting unknown parameters if it is specified.
368 chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;
370 /* Tell peer that we'll do ECN only if peer advertised such cap. */
371 if (asoc->peer.ecn_capable)
372 chunksize += sizeof(ecap_param);
374 /* Tell peer that we'll do PR-SCTP only if peer advertised. */
375 if (asoc->peer.prsctp_capable) {
376 chunksize += sizeof(prsctp_param);
377 extensions[num_ext] = SCTP_CID_FWD_TSN;
381 if (sctp_addip_enable) {
382 extensions[num_ext] = SCTP_CID_ASCONF;
383 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
387 chunksize += sizeof(ext_param) + num_ext;
388 chunksize += sizeof(aiparam);
390 if (asoc->peer.auth_capable) {
391 auth_random = (sctp_paramhdr_t *)asoc->c.auth_random;
392 chunksize += ntohs(auth_random->length);
394 auth_hmacs = (sctp_paramhdr_t *)asoc->c.auth_hmacs;
395 if (auth_hmacs->length)
396 chunksize += ntohs(auth_hmacs->length);
400 auth_chunks = (sctp_paramhdr_t *)asoc->c.auth_chunks;
401 if (auth_chunks->length)
402 chunksize += ntohs(auth_chunks->length);
406 extensions[num_ext] = SCTP_CID_AUTH;
410 /* Now allocate and fill out the chunk. */
411 retval = sctp_make_chunk(asoc, SCTP_CID_INIT_ACK, 0, chunksize);
415 /* Per the advice in RFC 2960 6.4, send this reply to
416 * the source of the INIT packet.
418 retval->transport = chunk->transport;
419 retval->subh.init_hdr =
420 sctp_addto_chunk(retval, sizeof(initack), &initack);
421 retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
422 sctp_addto_chunk(retval, cookie_len, cookie);
423 if (asoc->peer.ecn_capable)
424 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
426 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
427 ext_param.param_hdr.length =
428 htons(sizeof(sctp_supported_ext_param_t) + num_ext);
429 sctp_addto_chunk(retval, sizeof(sctp_supported_ext_param_t),
431 sctp_addto_chunk(retval, num_ext, extensions);
433 if (asoc->peer.prsctp_capable)
434 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
436 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
437 aiparam.param_hdr.length = htons(sizeof(aiparam));
438 aiparam.adaptation_ind = htonl(sctp_sk(asoc->base.sk)->adaptation_ind);
439 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
441 if (asoc->peer.auth_capable) {
442 sctp_addto_chunk(retval, ntohs(auth_random->length),
445 sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
448 sctp_addto_chunk(retval, ntohs(auth_chunks->length),
452 /* We need to remove the const qualifier at this point. */
453 retval->asoc = (struct sctp_association *) asoc;
455 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
457 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
458 * HEARTBEAT ACK, * etc.) to the same destination transport
459 * address from which it received the DATA or control chunk
460 * to which it is replying.
462 * [INIT ACK back to where the INIT came from.]
465 retval->transport = chunk->transport;
474 /* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
476 * This chunk is used only during the initialization of an association.
477 * It is sent by the initiator of an association to its peer to complete
478 * the initialization process. This chunk MUST precede any DATA chunk
479 * sent within the association, but MAY be bundled with one or more DATA
480 * chunks in the same packet.
483 * 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
484 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
485 * | Type = 10 |Chunk Flags | Length |
486 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
489 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
493 * Set to zero on transmit and ignored on receipt.
495 * Length: 16 bits (unsigned integer)
497 * Set to the size of the chunk in bytes, including the 4 bytes of
498 * the chunk header and the size of the Cookie.
500 * Cookie: variable size
502 * This field must contain the exact cookie received in the
503 * State Cookie parameter from the previous INIT ACK.
505 * An implementation SHOULD make the cookie as small as possible
506 * to insure interoperability.
508 struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
509 const struct sctp_chunk *chunk)
511 struct sctp_chunk *retval;
515 cookie = asoc->peer.cookie;
516 cookie_len = asoc->peer.cookie_len;
518 /* Build a cookie echo chunk. */
519 retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ECHO, 0, cookie_len);
522 retval->subh.cookie_hdr =
523 sctp_addto_chunk(retval, cookie_len, cookie);
525 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
527 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
528 * HEARTBEAT ACK, * etc.) to the same destination transport
529 * address from which it * received the DATA or control chunk
530 * to which it is replying.
532 * [COOKIE ECHO back to where the INIT ACK came from.]
535 retval->transport = chunk->transport;
541 /* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
543 * This chunk is used only during the initialization of an
544 * association. It is used to acknowledge the receipt of a COOKIE
545 * ECHO chunk. This chunk MUST precede any DATA or SACK chunk sent
546 * within the association, but MAY be bundled with one or more DATA
547 * chunks or SACK chunk in the same SCTP packet.
550 * 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
551 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
552 * | Type = 11 |Chunk Flags | Length = 4 |
553 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
555 * Chunk Flags: 8 bits
557 * Set to zero on transmit and ignored on receipt.
559 struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
560 const struct sctp_chunk *chunk)
562 struct sctp_chunk *retval;
564 retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ACK, 0, 0);
566 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
568 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
569 * HEARTBEAT ACK, * etc.) to the same destination transport
570 * address from which it * received the DATA or control chunk
571 * to which it is replying.
573 * [COOKIE ACK back to where the COOKIE ECHO came from.]
576 retval->transport = chunk->transport;
582 * Appendix A: Explicit Congestion Notification:
585 * RFC 2481 details a specific bit for a sender to send in the header of
586 * its next outbound TCP segment to indicate to its peer that it has
587 * reduced its congestion window. This is termed the CWR bit. For
588 * SCTP the same indication is made by including the CWR chunk.
589 * This chunk contains one data element, i.e. the TSN number that
590 * was sent in the ECNE chunk. This element represents the lowest
591 * TSN number in the datagram that was originally marked with the
595 * 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
596 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
597 * | Chunk Type=13 | Flags=00000000| Chunk Length = 8 |
598 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
599 * | Lowest TSN Number |
600 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
602 * Note: The CWR is considered a Control chunk.
604 struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
605 const __u32 lowest_tsn,
606 const struct sctp_chunk *chunk)
608 struct sctp_chunk *retval;
611 cwr.lowest_tsn = htonl(lowest_tsn);
612 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_CWR, 0,
613 sizeof(sctp_cwrhdr_t));
618 retval->subh.ecn_cwr_hdr =
619 sctp_addto_chunk(retval, sizeof(cwr), &cwr);
621 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
623 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
624 * HEARTBEAT ACK, * etc.) to the same destination transport
625 * address from which it * received the DATA or control chunk
626 * to which it is replying.
628 * [Report a reduced congestion window back to where the ECNE
632 retval->transport = chunk->transport;
638 /* Make an ECNE chunk. This is a congestion experienced report. */
639 struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
640 const __u32 lowest_tsn)
642 struct sctp_chunk *retval;
645 ecne.lowest_tsn = htonl(lowest_tsn);
646 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_ECNE, 0,
647 sizeof(sctp_ecnehdr_t));
650 retval->subh.ecne_hdr =
651 sctp_addto_chunk(retval, sizeof(ecne), &ecne);
657 /* Make a DATA chunk for the given association from the provided
658 * parameters. However, do not populate the data payload.
660 struct sctp_chunk *sctp_make_datafrag_empty(struct sctp_association *asoc,
661 const struct sctp_sndrcvinfo *sinfo,
662 int data_len, __u8 flags, __u16 ssn)
664 struct sctp_chunk *retval;
665 struct sctp_datahdr dp;
668 /* We assign the TSN as LATE as possible, not here when
669 * creating the chunk.
672 dp.stream = htons(sinfo->sinfo_stream);
673 dp.ppid = sinfo->sinfo_ppid;
675 /* Set the flags for an unordered send. */
676 if (sinfo->sinfo_flags & SCTP_UNORDERED) {
677 flags |= SCTP_DATA_UNORDERED;
682 chunk_len = sizeof(dp) + data_len;
683 retval = sctp_make_chunk(asoc, SCTP_CID_DATA, flags, chunk_len);
687 retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
688 memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
694 /* Create a selective ackowledgement (SACK) for the given
695 * association. This reports on which TSN's we've seen to date,
696 * including duplicates and gaps.
698 struct sctp_chunk *sctp_make_sack(const struct sctp_association *asoc)
700 struct sctp_chunk *retval;
701 struct sctp_sackhdr sack;
704 __u16 num_gabs, num_dup_tsns;
705 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
707 ctsn = sctp_tsnmap_get_ctsn(map);
708 SCTP_DEBUG_PRINTK("sackCTSNAck sent: 0x%x.\n", ctsn);
710 /* How much room is needed in the chunk? */
711 num_gabs = sctp_tsnmap_num_gabs(map);
712 num_dup_tsns = sctp_tsnmap_num_dups(map);
714 /* Initialize the SACK header. */
715 sack.cum_tsn_ack = htonl(ctsn);
716 sack.a_rwnd = htonl(asoc->a_rwnd);
717 sack.num_gap_ack_blocks = htons(num_gabs);
718 sack.num_dup_tsns = htons(num_dup_tsns);
721 + sizeof(struct sctp_gap_ack_block) * num_gabs
722 + sizeof(__u32) * num_dup_tsns;
724 /* Create the chunk. */
725 retval = sctp_make_chunk(asoc, SCTP_CID_SACK, 0, len);
729 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
731 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
732 * HEARTBEAT ACK, etc.) to the same destination transport
733 * address from which it received the DATA or control chunk to
734 * which it is replying. This rule should also be followed if
735 * the endpoint is bundling DATA chunks together with the
738 * However, when acknowledging multiple DATA chunks received
739 * in packets from different source addresses in a single
740 * SACK, the SACK chunk may be transmitted to one of the
741 * destination transport addresses from which the DATA or
742 * control chunks being acknowledged were received.
744 * [BUG: We do not implement the following paragraph.
745 * Perhaps we should remember the last transport we used for a
746 * SACK and avoid that (if possible) if we have seen any
747 * duplicates. --piggy]
749 * When a receiver of a duplicate DATA chunk sends a SACK to a
750 * multi- homed endpoint it MAY be beneficial to vary the
751 * destination address and not use the source address of the
752 * DATA chunk. The reason being that receiving a duplicate
753 * from a multi-homed endpoint might indicate that the return
754 * path (as specified in the source address of the DATA chunk)
755 * for the SACK is broken.
757 * [Send to the address from which we last received a DATA chunk.]
759 retval->transport = asoc->peer.last_data_from;
761 retval->subh.sack_hdr =
762 sctp_addto_chunk(retval, sizeof(sack), &sack);
764 /* Add the gap ack block information. */
766 sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
767 sctp_tsnmap_get_gabs(map));
769 /* Add the duplicate TSN information. */
771 sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
772 sctp_tsnmap_get_dups(map));
778 /* Make a SHUTDOWN chunk. */
779 struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
780 const struct sctp_chunk *chunk)
782 struct sctp_chunk *retval;
783 sctp_shutdownhdr_t shut;
786 ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
787 shut.cum_tsn_ack = htonl(ctsn);
789 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN, 0,
790 sizeof(sctp_shutdownhdr_t));
794 retval->subh.shutdown_hdr =
795 sctp_addto_chunk(retval, sizeof(shut), &shut);
798 retval->transport = chunk->transport;
803 struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
804 const struct sctp_chunk *chunk)
806 struct sctp_chunk *retval;
808 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0);
810 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
812 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
813 * HEARTBEAT ACK, * etc.) to the same destination transport
814 * address from which it * received the DATA or control chunk
815 * to which it is replying.
817 * [ACK back to where the SHUTDOWN came from.]
820 retval->transport = chunk->transport;
825 struct sctp_chunk *sctp_make_shutdown_complete(
826 const struct sctp_association *asoc,
827 const struct sctp_chunk *chunk)
829 struct sctp_chunk *retval;
832 /* Set the T-bit if we have no association (vtag will be
835 flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
837 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags, 0);
839 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
841 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
842 * HEARTBEAT ACK, * etc.) to the same destination transport
843 * address from which it * received the DATA or control chunk
844 * to which it is replying.
846 * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
850 retval->transport = chunk->transport;
855 /* Create an ABORT. Note that we set the T bit if we have no
856 * association, except when responding to an INIT (sctpimpguide 2.41).
858 struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
859 const struct sctp_chunk *chunk,
862 struct sctp_chunk *retval;
865 /* Set the T-bit if we have no association and 'chunk' is not
866 * an INIT (vtag will be reflected).
869 if (chunk && chunk->chunk_hdr &&
870 chunk->chunk_hdr->type == SCTP_CID_INIT)
873 flags = SCTP_CHUNK_FLAG_T;
876 retval = sctp_make_chunk(asoc, SCTP_CID_ABORT, flags, hint);
878 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
880 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
881 * HEARTBEAT ACK, * etc.) to the same destination transport
882 * address from which it * received the DATA or control chunk
883 * to which it is replying.
885 * [ABORT back to where the offender came from.]
888 retval->transport = chunk->transport;
893 /* Helper to create ABORT with a NO_USER_DATA error. */
894 struct sctp_chunk *sctp_make_abort_no_data(
895 const struct sctp_association *asoc,
896 const struct sctp_chunk *chunk, __u32 tsn)
898 struct sctp_chunk *retval;
901 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t)
907 /* Put the tsn back into network byte order. */
908 payload = htonl(tsn);
909 sctp_init_cause(retval, SCTP_ERROR_NO_DATA, sizeof(payload));
910 sctp_addto_chunk(retval, sizeof(payload), (const void *)&payload);
912 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
914 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
915 * HEARTBEAT ACK, * etc.) to the same destination transport
916 * address from which it * received the DATA or control chunk
917 * to which it is replying.
919 * [ABORT back to where the offender came from.]
922 retval->transport = chunk->transport;
928 /* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error. */
929 struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
930 const struct msghdr *msg,
933 struct sctp_chunk *retval;
934 void *payload = NULL;
937 retval = sctp_make_abort(asoc, NULL, sizeof(sctp_errhdr_t) + paylen);
942 /* Put the msg_iov together into payload. */
943 payload = kmalloc(paylen, GFP_KERNEL);
947 err = memcpy_fromiovec(payload, msg->msg_iov, paylen);
952 sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, paylen);
953 sctp_addto_chunk(retval, paylen, payload);
963 sctp_chunk_free(retval);
969 /* Append bytes to the end of a parameter. Will panic if chunk is not big
972 static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
976 int chunklen = ntohs(chunk->chunk_hdr->length);
978 target = skb_put(chunk->skb, len);
980 memcpy(target, data, len);
982 /* Adjust the chunk length field. */
983 chunk->chunk_hdr->length = htons(chunklen + len);
984 chunk->chunk_end = skb_tail_pointer(chunk->skb);
989 /* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
990 struct sctp_chunk *sctp_make_abort_violation(
991 const struct sctp_association *asoc,
992 const struct sctp_chunk *chunk,
996 struct sctp_chunk *retval;
997 struct sctp_paramhdr phdr;
999 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t) + paylen
1000 + sizeof(sctp_paramhdr_t));
1004 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, paylen
1005 + sizeof(sctp_paramhdr_t));
1007 phdr.type = htons(chunk->chunk_hdr->type);
1008 phdr.length = chunk->chunk_hdr->length;
1009 sctp_addto_chunk(retval, paylen, payload);
1010 sctp_addto_param(retval, sizeof(sctp_paramhdr_t), &phdr);
1016 /* Make a HEARTBEAT chunk. */
1017 struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
1018 const struct sctp_transport *transport,
1019 const void *payload, const size_t paylen)
1021 struct sctp_chunk *retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT,
1027 /* Cast away the 'const', as this is just telling the chunk
1028 * what transport it belongs to.
1030 retval->transport = (struct sctp_transport *) transport;
1031 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
1037 struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
1038 const struct sctp_chunk *chunk,
1039 const void *payload, const size_t paylen)
1041 struct sctp_chunk *retval;
1043 retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen);
1047 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
1049 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1051 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1052 * HEARTBEAT ACK, * etc.) to the same destination transport
1053 * address from which it * received the DATA or control chunk
1054 * to which it is replying.
1056 * [HBACK back to where the HEARTBEAT came from.]
1059 retval->transport = chunk->transport;
1065 /* Create an Operation Error chunk with the specified space reserved.
1066 * This routine can be used for containing multiple causes in the chunk.
1068 static struct sctp_chunk *sctp_make_op_error_space(
1069 const struct sctp_association *asoc,
1070 const struct sctp_chunk *chunk,
1073 struct sctp_chunk *retval;
1075 retval = sctp_make_chunk(asoc, SCTP_CID_ERROR, 0,
1076 sizeof(sctp_errhdr_t) + size);
1080 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1082 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1083 * HEARTBEAT ACK, etc.) to the same destination transport
1084 * address from which it received the DATA or control chunk
1085 * to which it is replying.
1089 retval->transport = chunk->transport;
1095 /* Create an Operation Error chunk. */
1096 struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
1097 const struct sctp_chunk *chunk,
1098 __be16 cause_code, const void *payload,
1101 struct sctp_chunk *retval;
1103 retval = sctp_make_op_error_space(asoc, chunk, paylen);
1107 sctp_init_cause(retval, cause_code, paylen);
1108 sctp_addto_chunk(retval, paylen, payload);
1114 struct sctp_chunk *sctp_make_auth(const struct sctp_association *asoc)
1116 struct sctp_chunk *retval;
1117 struct sctp_hmac *hmac_desc;
1118 struct sctp_authhdr auth_hdr;
1121 /* Get the first hmac that the peer told us to use */
1122 hmac_desc = sctp_auth_asoc_get_hmac(asoc);
1123 if (unlikely(!hmac_desc))
1126 retval = sctp_make_chunk(asoc, SCTP_CID_AUTH, 0,
1127 hmac_desc->hmac_len + sizeof(sctp_authhdr_t));
1131 auth_hdr.hmac_id = htons(hmac_desc->hmac_id);
1132 auth_hdr.shkey_id = htons(asoc->active_key_id);
1134 retval->subh.auth_hdr = sctp_addto_chunk(retval, sizeof(sctp_authhdr_t),
1137 hmac = skb_put(retval->skb, hmac_desc->hmac_len);
1138 memset(hmac, 0, hmac_desc->hmac_len);
1140 /* Adjust the chunk header to include the empty MAC */
1141 retval->chunk_hdr->length =
1142 htons(ntohs(retval->chunk_hdr->length) + hmac_desc->hmac_len);
1143 retval->chunk_end = skb_tail_pointer(retval->skb);
1149 /********************************************************************
1150 * 2nd Level Abstractions
1151 ********************************************************************/
1153 /* Turn an skb into a chunk.
1154 * FIXME: Eventually move the structure directly inside the skb->cb[].
1156 struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
1157 const struct sctp_association *asoc,
1160 struct sctp_chunk *retval;
1162 retval = kmem_cache_zalloc(sctp_chunk_cachep, GFP_ATOMIC);
1168 SCTP_DEBUG_PRINTK("chunkifying skb %p w/o an sk\n", skb);
1171 INIT_LIST_HEAD(&retval->list);
1173 retval->asoc = (struct sctp_association *)asoc;
1175 retval->has_tsn = 0;
1176 retval->has_ssn = 0;
1177 retval->rtt_in_progress = 0;
1178 retval->sent_at = 0;
1179 retval->singleton = 1;
1180 retval->end_of_packet = 0;
1181 retval->ecn_ce_done = 0;
1182 retval->pdiscard = 0;
1184 /* sctpimpguide-05.txt Section 2.8.2
1185 * M1) Each time a new DATA chunk is transmitted
1186 * set the 'TSN.Missing.Report' count for that TSN to 0. The
1187 * 'TSN.Missing.Report' count will be used to determine missing chunks
1188 * and when to fast retransmit.
1190 retval->tsn_missing_report = 0;
1191 retval->tsn_gap_acked = 0;
1192 retval->fast_retransmit = 0;
1194 /* If this is a fragmented message, track all fragments
1195 * of the message (for SEND_FAILED).
1199 /* Polish the bead hole. */
1200 INIT_LIST_HEAD(&retval->transmitted_list);
1201 INIT_LIST_HEAD(&retval->frag_list);
1202 SCTP_DBG_OBJCNT_INC(chunk);
1203 atomic_set(&retval->refcnt, 1);
1209 /* Set chunk->source and dest based on the IP header in chunk->skb. */
1210 void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
1211 union sctp_addr *dest)
1213 memcpy(&chunk->source, src, sizeof(union sctp_addr));
1214 memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
1217 /* Extract the source address from a chunk. */
1218 const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
1220 /* If we have a known transport, use that. */
1221 if (chunk->transport) {
1222 return &chunk->transport->ipaddr;
1224 /* Otherwise, extract it from the IP header. */
1225 return &chunk->source;
1229 /* Create a new chunk, setting the type and flags headers from the
1230 * arguments, reserving enough space for a 'paylen' byte payload.
1233 struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
1234 __u8 type, __u8 flags, int paylen)
1236 struct sctp_chunk *retval;
1237 sctp_chunkhdr_t *chunk_hdr;
1238 struct sk_buff *skb;
1241 /* No need to allocate LL here, as this is only a chunk. */
1242 skb = alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t) + paylen),
1247 /* Make room for the chunk header. */
1248 chunk_hdr = (sctp_chunkhdr_t *)skb_put(skb, sizeof(sctp_chunkhdr_t));
1249 chunk_hdr->type = type;
1250 chunk_hdr->flags = flags;
1251 chunk_hdr->length = htons(sizeof(sctp_chunkhdr_t));
1253 sk = asoc ? asoc->base.sk : NULL;
1254 retval = sctp_chunkify(skb, asoc, sk);
1260 retval->chunk_hdr = chunk_hdr;
1261 retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(struct sctp_chunkhdr);
1263 /* Determine if the chunk needs to be authenticated */
1264 if (sctp_auth_send_cid(type, asoc))
1267 /* Set the skb to the belonging sock for accounting. */
1276 /* Release the memory occupied by a chunk. */
1277 static void sctp_chunk_destroy(struct sctp_chunk *chunk)
1279 /* Free the chunk skb data and the SCTP_chunk stub itself. */
1280 dev_kfree_skb(chunk->skb);
1282 SCTP_DBG_OBJCNT_DEC(chunk);
1283 kmem_cache_free(sctp_chunk_cachep, chunk);
1286 /* Possibly, free the chunk. */
1287 void sctp_chunk_free(struct sctp_chunk *chunk)
1289 BUG_ON(!list_empty(&chunk->list));
1290 list_del_init(&chunk->transmitted_list);
1292 /* Release our reference on the message tracker. */
1294 sctp_datamsg_put(chunk->msg);
1296 sctp_chunk_put(chunk);
1299 /* Grab a reference to the chunk. */
1300 void sctp_chunk_hold(struct sctp_chunk *ch)
1302 atomic_inc(&ch->refcnt);
1305 /* Release a reference to the chunk. */
1306 void sctp_chunk_put(struct sctp_chunk *ch)
1308 if (atomic_dec_and_test(&ch->refcnt))
1309 sctp_chunk_destroy(ch);
1312 /* Append bytes to the end of a chunk. Will panic if chunk is not big
1315 void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
1319 int chunklen = ntohs(chunk->chunk_hdr->length);
1320 int padlen = WORD_ROUND(chunklen) - chunklen;
1322 padding = skb_put(chunk->skb, padlen);
1323 target = skb_put(chunk->skb, len);
1325 memset(padding, 0, padlen);
1326 memcpy(target, data, len);
1328 /* Adjust the chunk length field. */
1329 chunk->chunk_hdr->length = htons(chunklen + padlen + len);
1330 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1335 /* Append bytes from user space to the end of a chunk. Will panic if
1336 * chunk is not big enough.
1337 * Returns a kernel err value.
1339 int sctp_user_addto_chunk(struct sctp_chunk *chunk, int off, int len,
1345 /* Make room in chunk for data. */
1346 target = skb_put(chunk->skb, len);
1348 /* Copy data (whole iovec) into chunk */
1349 if ((err = memcpy_fromiovecend(target, data, off, len)))
1352 /* Adjust the chunk length field. */
1353 chunk->chunk_hdr->length =
1354 htons(ntohs(chunk->chunk_hdr->length) + len);
1355 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1361 /* Helper function to assign a TSN if needed. This assumes that both
1362 * the data_hdr and association have already been assigned.
1364 void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
1366 struct sctp_datamsg *msg;
1367 struct sctp_chunk *lchunk;
1368 struct sctp_stream *stream;
1375 /* All fragments will be on the same stream */
1376 sid = ntohs(chunk->subh.data_hdr->stream);
1377 stream = &chunk->asoc->ssnmap->out;
1379 /* Now assign the sequence number to the entire message.
1380 * All fragments must have the same stream sequence number.
1383 list_for_each_entry(lchunk, &msg->chunks, frag_list) {
1384 if (lchunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
1387 if (lchunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
1388 ssn = sctp_ssn_next(stream, sid);
1390 ssn = sctp_ssn_peek(stream, sid);
1393 lchunk->subh.data_hdr->ssn = htons(ssn);
1394 lchunk->has_ssn = 1;
1398 /* Helper function to assign a TSN if needed. This assumes that both
1399 * the data_hdr and association have already been assigned.
1401 void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
1403 if (!chunk->has_tsn) {
1404 /* This is the last possible instant to
1407 chunk->subh.data_hdr->tsn =
1408 htonl(sctp_association_get_next_tsn(chunk->asoc));
1413 /* Create a CLOSED association to use with an incoming packet. */
1414 struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
1415 struct sctp_chunk *chunk,
1418 struct sctp_association *asoc;
1419 struct sk_buff *skb;
1423 /* Create the bare association. */
1424 scope = sctp_scope(sctp_source(chunk));
1425 asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
1430 /* Create an entry for the source address of the packet. */
1431 af = sctp_get_af_specific(ipver2af(ip_hdr(skb)->version));
1434 af->from_skb(&asoc->c.peer_addr, skb, 1);
1439 sctp_association_free(asoc);
1443 /* Build a cookie representing asoc.
1444 * This INCLUDES the param header needed to put the cookie in the INIT ACK.
1446 static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
1447 const struct sctp_association *asoc,
1448 const struct sctp_chunk *init_chunk,
1450 const __u8 *raw_addrs, int addrs_len)
1452 sctp_cookie_param_t *retval;
1453 struct sctp_signed_cookie *cookie;
1454 struct scatterlist sg;
1455 int headersize, bodysize;
1456 unsigned int keylen;
1459 /* Header size is static data prior to the actual cookie, including
1462 headersize = sizeof(sctp_paramhdr_t) +
1463 (sizeof(struct sctp_signed_cookie) -
1464 sizeof(struct sctp_cookie));
1465 bodysize = sizeof(struct sctp_cookie)
1466 + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
1468 /* Pad out the cookie to a multiple to make the signature
1469 * functions simpler to write.
1471 if (bodysize % SCTP_COOKIE_MULTIPLE)
1472 bodysize += SCTP_COOKIE_MULTIPLE
1473 - (bodysize % SCTP_COOKIE_MULTIPLE);
1474 *cookie_len = headersize + bodysize;
1476 /* Clear this memory since we are sending this data structure
1477 * out on the network.
1479 retval = kzalloc(*cookie_len, GFP_ATOMIC);
1483 cookie = (struct sctp_signed_cookie *) retval->body;
1485 /* Set up the parameter header. */
1486 retval->p.type = SCTP_PARAM_STATE_COOKIE;
1487 retval->p.length = htons(*cookie_len);
1489 /* Copy the cookie part of the association itself. */
1490 cookie->c = asoc->c;
1491 /* Save the raw address list length in the cookie. */
1492 cookie->c.raw_addr_list_len = addrs_len;
1494 /* Remember PR-SCTP capability. */
1495 cookie->c.prsctp_capable = asoc->peer.prsctp_capable;
1497 /* Save adaptation indication in the cookie. */
1498 cookie->c.adaptation_ind = asoc->peer.adaptation_ind;
1500 /* Set an expiration time for the cookie. */
1501 do_gettimeofday(&cookie->c.expiration);
1502 TIMEVAL_ADD(asoc->cookie_life, cookie->c.expiration);
1504 /* Copy the peer's init packet. */
1505 memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
1506 ntohs(init_chunk->chunk_hdr->length));
1508 /* Copy the raw local address list of the association. */
1509 memcpy((__u8 *)&cookie->c.peer_init[0] +
1510 ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
1512 if (sctp_sk(ep->base.sk)->hmac) {
1513 struct hash_desc desc;
1515 /* Sign the message. */
1516 sg.page = virt_to_page(&cookie->c);
1517 sg.offset = (unsigned long)(&cookie->c) % PAGE_SIZE;
1518 sg.length = bodysize;
1519 keylen = SCTP_SECRET_SIZE;
1520 key = (char *)ep->secret_key[ep->current_key];
1521 desc.tfm = sctp_sk(ep->base.sk)->hmac;
1524 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1525 crypto_hash_digest(&desc, &sg, bodysize, cookie->signature))
1538 /* Unpack the cookie from COOKIE ECHO chunk, recreating the association. */
1539 struct sctp_association *sctp_unpack_cookie(
1540 const struct sctp_endpoint *ep,
1541 const struct sctp_association *asoc,
1542 struct sctp_chunk *chunk, gfp_t gfp,
1543 int *error, struct sctp_chunk **errp)
1545 struct sctp_association *retval = NULL;
1546 struct sctp_signed_cookie *cookie;
1547 struct sctp_cookie *bear_cookie;
1548 int headersize, bodysize, fixed_size;
1549 __u8 *digest = ep->digest;
1550 struct scatterlist sg;
1551 unsigned int keylen, len;
1554 struct sk_buff *skb = chunk->skb;
1556 struct hash_desc desc;
1558 /* Header size is static data prior to the actual cookie, including
1561 headersize = sizeof(sctp_chunkhdr_t) +
1562 (sizeof(struct sctp_signed_cookie) -
1563 sizeof(struct sctp_cookie));
1564 bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
1565 fixed_size = headersize + sizeof(struct sctp_cookie);
1567 /* Verify that the chunk looks like it even has a cookie.
1568 * There must be enough room for our cookie and our peer's
1571 len = ntohs(chunk->chunk_hdr->length);
1572 if (len < fixed_size + sizeof(struct sctp_chunkhdr))
1575 /* Verify that the cookie has been padded out. */
1576 if (bodysize % SCTP_COOKIE_MULTIPLE)
1579 /* Process the cookie. */
1580 cookie = chunk->subh.cookie_hdr;
1581 bear_cookie = &cookie->c;
1583 if (!sctp_sk(ep->base.sk)->hmac)
1586 /* Check the signature. */
1587 keylen = SCTP_SECRET_SIZE;
1588 sg.page = virt_to_page(bear_cookie);
1589 sg.offset = (unsigned long)(bear_cookie) % PAGE_SIZE;
1590 sg.length = bodysize;
1591 key = (char *)ep->secret_key[ep->current_key];
1592 desc.tfm = sctp_sk(ep->base.sk)->hmac;
1595 memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
1596 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1597 crypto_hash_digest(&desc, &sg, bodysize, digest)) {
1598 *error = -SCTP_IERROR_NOMEM;
1602 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1603 /* Try the previous key. */
1604 key = (char *)ep->secret_key[ep->last_key];
1605 memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
1606 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1607 crypto_hash_digest(&desc, &sg, bodysize, digest)) {
1608 *error = -SCTP_IERROR_NOMEM;
1612 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1613 /* Yikes! Still bad signature! */
1614 *error = -SCTP_IERROR_BAD_SIG;
1620 /* IG Section 2.35.2:
1621 * 3) Compare the port numbers and the verification tag contained
1622 * within the COOKIE ECHO chunk to the actual port numbers and the
1623 * verification tag within the SCTP common header of the received
1624 * packet. If these values do not match the packet MUST be silently
1627 if (ntohl(chunk->sctp_hdr->vtag) != bear_cookie->my_vtag) {
1628 *error = -SCTP_IERROR_BAD_TAG;
1632 if (chunk->sctp_hdr->source != bear_cookie->peer_addr.v4.sin_port ||
1633 ntohs(chunk->sctp_hdr->dest) != bear_cookie->my_port) {
1634 *error = -SCTP_IERROR_BAD_PORTS;
1638 /* Check to see if the cookie is stale. If there is already
1639 * an association, there is no need to check cookie's expiration
1640 * for init collision case of lost COOKIE ACK.
1641 * If skb has been timestamped, then use the stamp, otherwise
1642 * use current time. This introduces a small possibility that
1643 * that a cookie may be considered expired, but his would only slow
1644 * down the new association establishment instead of every packet.
1646 if (sock_flag(ep->base.sk, SOCK_TIMESTAMP))
1647 skb_get_timestamp(skb, &tv);
1649 do_gettimeofday(&tv);
1651 if (!asoc && tv_lt(bear_cookie->expiration, tv)) {
1653 * Section 3.3.10.3 Stale Cookie Error (3)
1657 * Stale Cookie Error: Indicates the receipt of a valid State
1658 * Cookie that has expired.
1660 len = ntohs(chunk->chunk_hdr->length);
1661 *errp = sctp_make_op_error_space(asoc, chunk, len);
1663 suseconds_t usecs = (tv.tv_sec -
1664 bear_cookie->expiration.tv_sec) * 1000000L +
1665 tv.tv_usec - bear_cookie->expiration.tv_usec;
1666 __be32 n = htonl(usecs);
1668 sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
1670 sctp_addto_chunk(*errp, sizeof(n), &n);
1671 *error = -SCTP_IERROR_STALE_COOKIE;
1673 *error = -SCTP_IERROR_NOMEM;
1678 /* Make a new base association. */
1679 scope = sctp_scope(sctp_source(chunk));
1680 retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
1682 *error = -SCTP_IERROR_NOMEM;
1686 /* Set up our peer's port number. */
1687 retval->peer.port = ntohs(chunk->sctp_hdr->source);
1689 /* Populate the association from the cookie. */
1690 memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
1692 if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
1694 *error = -SCTP_IERROR_NOMEM;
1698 /* Also, add the destination address. */
1699 if (list_empty(&retval->base.bind_addr.address_list)) {
1700 sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest, 1,
1704 retval->next_tsn = retval->c.initial_tsn;
1705 retval->ctsn_ack_point = retval->next_tsn - 1;
1706 retval->addip_serial = retval->c.initial_tsn;
1707 retval->adv_peer_ack_point = retval->ctsn_ack_point;
1708 retval->peer.prsctp_capable = retval->c.prsctp_capable;
1709 retval->peer.adaptation_ind = retval->c.adaptation_ind;
1711 /* The INIT stuff will be done by the side effects. */
1716 sctp_association_free(retval);
1721 /* Yikes! The packet is either corrupt or deliberately
1724 *error = -SCTP_IERROR_MALFORMED;
1728 /********************************************************************
1729 * 3rd Level Abstractions
1730 ********************************************************************/
1732 struct __sctp_missing {
1735 } __attribute__((packed));
1738 * Report a missing mandatory parameter.
1740 static int sctp_process_missing_param(const struct sctp_association *asoc,
1741 sctp_param_t paramtype,
1742 struct sctp_chunk *chunk,
1743 struct sctp_chunk **errp)
1745 struct __sctp_missing report;
1748 len = WORD_ROUND(sizeof(report));
1750 /* Make an ERROR chunk, preparing enough room for
1751 * returning multiple unknown parameters.
1754 *errp = sctp_make_op_error_space(asoc, chunk, len);
1757 report.num_missing = htonl(1);
1758 report.type = paramtype;
1759 sctp_init_cause(*errp, SCTP_ERROR_MISS_PARAM,
1761 sctp_addto_chunk(*errp, sizeof(report), &report);
1764 /* Stop processing this chunk. */
1768 /* Report an Invalid Mandatory Parameter. */
1769 static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
1770 struct sctp_chunk *chunk,
1771 struct sctp_chunk **errp)
1773 /* Invalid Mandatory Parameter Error has no payload. */
1776 *errp = sctp_make_op_error_space(asoc, chunk, 0);
1779 sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, 0);
1781 /* Stop processing this chunk. */
1785 static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
1786 struct sctp_paramhdr *param,
1787 const struct sctp_chunk *chunk,
1788 struct sctp_chunk **errp)
1790 char error[] = "The following parameter had invalid length:";
1791 size_t payload_len = WORD_ROUND(sizeof(error)) +
1792 sizeof(sctp_paramhdr_t);
1795 /* Create an error chunk and fill it in with our payload. */
1797 *errp = sctp_make_op_error_space(asoc, chunk, payload_len);
1800 sctp_init_cause(*errp, SCTP_ERROR_PROTO_VIOLATION,
1801 sizeof(error) + sizeof(sctp_paramhdr_t));
1802 sctp_addto_chunk(*errp, sizeof(error), error);
1803 sctp_addto_param(*errp, sizeof(sctp_paramhdr_t), param);
1810 /* Do not attempt to handle the HOST_NAME parm. However, do
1811 * send back an indicator to the peer.
1813 static int sctp_process_hn_param(const struct sctp_association *asoc,
1814 union sctp_params param,
1815 struct sctp_chunk *chunk,
1816 struct sctp_chunk **errp)
1818 __u16 len = ntohs(param.p->length);
1820 /* Make an ERROR chunk. */
1822 *errp = sctp_make_op_error_space(asoc, chunk, len);
1825 sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED, len);
1826 sctp_addto_chunk(*errp, len, param.v);
1829 /* Stop processing this chunk. */
1833 static void sctp_process_ext_param(struct sctp_association *asoc,
1834 union sctp_params param)
1836 __u16 num_ext = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
1839 for (i = 0; i < num_ext; i++) {
1840 switch (param.ext->chunks[i]) {
1841 case SCTP_CID_FWD_TSN:
1842 if (sctp_prsctp_enable &&
1843 !asoc->peer.prsctp_capable)
1844 asoc->peer.prsctp_capable = 1;
1847 /* if the peer reports AUTH, assume that he
1850 asoc->peer.auth_capable = 1;
1852 case SCTP_CID_ASCONF:
1853 case SCTP_CID_ASCONF_ACK:
1854 asoc->peer.addip_capable = 1;
1862 /* RFC 3.2.1 & the Implementers Guide 2.2.
1864 * The Parameter Types are encoded such that the
1865 * highest-order two bits specify the action that must be
1866 * taken if the processing endpoint does not recognize the
1869 * 00 - Stop processing this SCTP chunk and discard it,
1870 * do not process any further chunks within it.
1872 * 01 - Stop processing this SCTP chunk and discard it,
1873 * do not process any further chunks within it, and report
1874 * the unrecognized parameter in an 'Unrecognized
1875 * Parameter Type' (in either an ERROR or in the INIT ACK).
1877 * 10 - Skip this parameter and continue processing.
1879 * 11 - Skip this parameter and continue processing but
1880 * report the unrecognized parameter in an
1881 * 'Unrecognized Parameter Type' (in either an ERROR or in
1885 * 0 - discard the chunk
1886 * 1 - continue with the chunk
1888 static int sctp_process_unk_param(const struct sctp_association *asoc,
1889 union sctp_params param,
1890 struct sctp_chunk *chunk,
1891 struct sctp_chunk **errp)
1895 switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
1896 case SCTP_PARAM_ACTION_DISCARD:
1899 case SCTP_PARAM_ACTION_DISCARD_ERR:
1901 /* Make an ERROR chunk, preparing enough room for
1902 * returning multiple unknown parameters.
1905 *errp = sctp_make_op_error_space(asoc, chunk,
1906 ntohs(chunk->chunk_hdr->length));
1909 sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
1910 WORD_ROUND(ntohs(param.p->length)));
1911 sctp_addto_chunk(*errp,
1912 WORD_ROUND(ntohs(param.p->length)),
1917 case SCTP_PARAM_ACTION_SKIP:
1919 case SCTP_PARAM_ACTION_SKIP_ERR:
1920 /* Make an ERROR chunk, preparing enough room for
1921 * returning multiple unknown parameters.
1924 *errp = sctp_make_op_error_space(asoc, chunk,
1925 ntohs(chunk->chunk_hdr->length));
1928 sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
1929 WORD_ROUND(ntohs(param.p->length)));
1930 sctp_addto_chunk(*errp,
1931 WORD_ROUND(ntohs(param.p->length)),
1934 /* If there is no memory for generating the ERROR
1935 * report as specified, an ABORT will be triggered
1936 * to the peer and the association won't be
1950 /* Find unrecognized parameters in the chunk.
1952 * 0 - discard the chunk
1953 * 1 - continue with the chunk
1955 static int sctp_verify_param(const struct sctp_association *asoc,
1956 union sctp_params param,
1958 struct sctp_chunk *chunk,
1959 struct sctp_chunk **err_chunk)
1963 /* FIXME - This routine is not looking at each parameter per the
1964 * chunk type, i.e., unrecognized parameters should be further
1965 * identified based on the chunk id.
1968 switch (param.p->type) {
1969 case SCTP_PARAM_IPV4_ADDRESS:
1970 case SCTP_PARAM_IPV6_ADDRESS:
1971 case SCTP_PARAM_COOKIE_PRESERVATIVE:
1972 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
1973 case SCTP_PARAM_STATE_COOKIE:
1974 case SCTP_PARAM_HEARTBEAT_INFO:
1975 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
1976 case SCTP_PARAM_ECN_CAPABLE:
1977 case SCTP_PARAM_ADAPTATION_LAYER_IND:
1978 case SCTP_PARAM_SUPPORTED_EXT:
1981 case SCTP_PARAM_HOST_NAME_ADDRESS:
1982 /* Tell the peer, we won't support this param. */
1983 return sctp_process_hn_param(asoc, param, chunk, err_chunk);
1985 case SCTP_PARAM_FWD_TSN_SUPPORT:
1986 if (sctp_prsctp_enable)
1990 case SCTP_PARAM_RANDOM:
1991 if (!sctp_auth_enable)
1994 /* SCTP-AUTH: Secion 6.1
1995 * If the random number is not 32 byte long the association
1996 * MUST be aborted. The ABORT chunk SHOULD contain the error
1997 * cause 'Protocol Violation'.
1999 if (SCTP_AUTH_RANDOM_LENGTH !=
2000 ntohs(param.p->length) - sizeof(sctp_paramhdr_t))
2001 return sctp_process_inv_paramlength(asoc, param.p,
2005 case SCTP_PARAM_CHUNKS:
2006 if (!sctp_auth_enable)
2009 /* SCTP-AUTH: Section 3.2
2010 * The CHUNKS parameter MUST be included once in the INIT or
2011 * INIT-ACK chunk if the sender wants to receive authenticated
2012 * chunks. Its maximum length is 260 bytes.
2014 if (260 < ntohs(param.p->length))
2015 return sctp_process_inv_paramlength(asoc, param.p,
2019 case SCTP_PARAM_HMAC_ALGO:
2020 if (!sctp_auth_enable)
2025 SCTP_DEBUG_PRINTK("Unrecognized param: %d for chunk %d.\n",
2026 ntohs(param.p->type), cid);
2027 return sctp_process_unk_param(asoc, param, chunk, err_chunk);
2034 /* Verify the INIT packet before we process it. */
2035 int sctp_verify_init(const struct sctp_association *asoc,
2037 sctp_init_chunk_t *peer_init,
2038 struct sctp_chunk *chunk,
2039 struct sctp_chunk **errp)
2041 union sctp_params param;
2044 /* Verify stream values are non-zero. */
2045 if ((0 == peer_init->init_hdr.num_outbound_streams) ||
2046 (0 == peer_init->init_hdr.num_inbound_streams) ||
2047 (0 == peer_init->init_hdr.init_tag) ||
2048 (SCTP_DEFAULT_MINWINDOW > ntohl(peer_init->init_hdr.a_rwnd))) {
2050 sctp_process_inv_mandatory(asoc, chunk, errp);
2054 /* Check for missing mandatory parameters. */
2055 sctp_walk_params(param, peer_init, init_hdr.params) {
2057 if (SCTP_PARAM_STATE_COOKIE == param.p->type)
2060 } /* for (loop through all parameters) */
2062 /* There is a possibility that a parameter length was bad and
2063 * in that case we would have stoped walking the parameters.
2064 * The current param.p would point at the bad one.
2065 * Current consensus on the mailing list is to generate a PROTOCOL
2066 * VIOLATION error. We build the ERROR chunk here and let the normal
2067 * error handling code build and send the packet.
2069 if (param.v != (void*)chunk->chunk_end) {
2070 sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
2074 /* The only missing mandatory param possible today is
2075 * the state cookie for an INIT-ACK chunk.
2077 if ((SCTP_CID_INIT_ACK == cid) && !has_cookie) {
2078 sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
2083 /* Find unrecognized parameters. */
2085 sctp_walk_params(param, peer_init, init_hdr.params) {
2087 if (!sctp_verify_param(asoc, param, cid, chunk, errp)) {
2088 if (SCTP_PARAM_HOST_NAME_ADDRESS == param.p->type)
2094 } /* for (loop through all parameters) */
2099 /* Unpack the parameters in an INIT packet into an association.
2100 * Returns 0 on failure, else success.
2101 * FIXME: This is an association method.
2103 int sctp_process_init(struct sctp_association *asoc, sctp_cid_t cid,
2104 const union sctp_addr *peer_addr,
2105 sctp_init_chunk_t *peer_init, gfp_t gfp)
2107 union sctp_params param;
2108 struct sctp_transport *transport;
2109 struct list_head *pos, *temp;
2112 /* We must include the address that the INIT packet came from.
2113 * This is the only address that matters for an INIT packet.
2114 * When processing a COOKIE ECHO, we retrieve the from address
2115 * of the INIT from the cookie.
2118 /* This implementation defaults to making the first transport
2119 * added as the primary transport. The source address seems to
2120 * be a a better choice than any of the embedded addresses.
2123 if(!sctp_assoc_add_peer(asoc, peer_addr, gfp, SCTP_ACTIVE))
2127 /* Process the initialization parameters. */
2128 sctp_walk_params(param, peer_init, init_hdr.params) {
2130 if (!sctp_process_param(asoc, param, peer_addr, gfp))
2134 /* AUTH: After processing the parameters, make sure that we
2135 * have all the required info to potentially do authentications.
2137 if (asoc->peer.auth_capable && (!asoc->peer.peer_random ||
2138 !asoc->peer.peer_hmacs))
2139 asoc->peer.auth_capable = 0;
2142 /* If the peer claims support for ADD-IP without support
2143 * for AUTH, disable support for ADD-IP.
2145 if (asoc->peer.addip_capable && !asoc->peer.auth_capable) {
2146 asoc->peer.addip_disabled_mask |= (SCTP_PARAM_ADD_IP |
2148 SCTP_PARAM_SET_PRIMARY);
2151 /* Walk list of transports, removing transports in the UNKNOWN state. */
2152 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2153 transport = list_entry(pos, struct sctp_transport, transports);
2154 if (transport->state == SCTP_UNKNOWN) {
2155 sctp_assoc_rm_peer(asoc, transport);
2159 /* The fixed INIT headers are always in network byte
2162 asoc->peer.i.init_tag =
2163 ntohl(peer_init->init_hdr.init_tag);
2164 asoc->peer.i.a_rwnd =
2165 ntohl(peer_init->init_hdr.a_rwnd);
2166 asoc->peer.i.num_outbound_streams =
2167 ntohs(peer_init->init_hdr.num_outbound_streams);
2168 asoc->peer.i.num_inbound_streams =
2169 ntohs(peer_init->init_hdr.num_inbound_streams);
2170 asoc->peer.i.initial_tsn =
2171 ntohl(peer_init->init_hdr.initial_tsn);
2173 /* Apply the upper bounds for output streams based on peer's
2174 * number of inbound streams.
2176 if (asoc->c.sinit_num_ostreams >
2177 ntohs(peer_init->init_hdr.num_inbound_streams)) {
2178 asoc->c.sinit_num_ostreams =
2179 ntohs(peer_init->init_hdr.num_inbound_streams);
2182 if (asoc->c.sinit_max_instreams >
2183 ntohs(peer_init->init_hdr.num_outbound_streams)) {
2184 asoc->c.sinit_max_instreams =
2185 ntohs(peer_init->init_hdr.num_outbound_streams);
2188 /* Copy Initiation tag from INIT to VT_peer in cookie. */
2189 asoc->c.peer_vtag = asoc->peer.i.init_tag;
2191 /* Peer Rwnd : Current calculated value of the peer's rwnd. */
2192 asoc->peer.rwnd = asoc->peer.i.a_rwnd;
2194 /* Copy cookie in case we need to resend COOKIE-ECHO. */
2195 cookie = asoc->peer.cookie;
2197 asoc->peer.cookie = kmemdup(cookie, asoc->peer.cookie_len, gfp);
2198 if (!asoc->peer.cookie)
2202 /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
2203 * high (for example, implementations MAY use the size of the receiver
2204 * advertised window).
2206 list_for_each(pos, &asoc->peer.transport_addr_list) {
2207 transport = list_entry(pos, struct sctp_transport, transports);
2208 transport->ssthresh = asoc->peer.i.a_rwnd;
2211 /* Set up the TSN tracking pieces. */
2212 sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_SIZE,
2213 asoc->peer.i.initial_tsn);
2215 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
2217 * The stream sequence number in all the streams shall start
2218 * from 0 when the association is established. Also, when the
2219 * stream sequence number reaches the value 65535 the next
2220 * stream sequence number shall be set to 0.
2223 /* Allocate storage for the negotiated streams if it is not a temporary
2229 asoc->ssnmap = sctp_ssnmap_new(asoc->c.sinit_max_instreams,
2230 asoc->c.sinit_num_ostreams, gfp);
2234 error = sctp_assoc_set_id(asoc, gfp);
2239 /* ADDIP Section 4.1 ASCONF Chunk Procedures
2241 * When an endpoint has an ASCONF signaled change to be sent to the
2242 * remote endpoint it should do the following:
2244 * A2) A serial number should be assigned to the Chunk. The serial
2245 * number should be a monotonically increasing number. All serial
2246 * numbers are defined to be initialized at the start of the
2247 * association to the same value as the Initial TSN.
2249 asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
2253 /* Release the transport structures. */
2254 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2255 transport = list_entry(pos, struct sctp_transport, transports);
2257 sctp_transport_free(transport);
2260 asoc->peer.transport_count = 0;
2267 /* Update asoc with the option described in param.
2269 * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
2271 * asoc is the association to update.
2272 * param is the variable length parameter to use for update.
2273 * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
2274 * If the current packet is an INIT we want to minimize the amount of
2275 * work we do. In particular, we should not build transport
2276 * structures for the addresses.
2278 static int sctp_process_param(struct sctp_association *asoc,
2279 union sctp_params param,
2280 const union sctp_addr *peer_addr,
2283 union sctp_addr addr;
2291 /* We maintain all INIT parameters in network byte order all the
2292 * time. This allows us to not worry about whether the parameters
2293 * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
2295 switch (param.p->type) {
2296 case SCTP_PARAM_IPV6_ADDRESS:
2297 if (PF_INET6 != asoc->base.sk->sk_family)
2300 case SCTP_PARAM_IPV4_ADDRESS:
2301 af = sctp_get_af_specific(param_type2af(param.p->type));
2302 af->from_addr_param(&addr, param.addr, htons(asoc->peer.port), 0);
2303 scope = sctp_scope(peer_addr);
2304 if (sctp_in_scope(&addr, scope))
2305 if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_UNCONFIRMED))
2309 case SCTP_PARAM_COOKIE_PRESERVATIVE:
2310 if (!sctp_cookie_preserve_enable)
2313 stale = ntohl(param.life->lifespan_increment);
2315 /* Suggested Cookie Life span increment's unit is msec,
2318 asoc->cookie_life.tv_sec += stale / 1000;
2319 asoc->cookie_life.tv_usec += (stale % 1000) * 1000;
2322 case SCTP_PARAM_HOST_NAME_ADDRESS:
2323 SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
2326 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2327 /* Turn off the default values first so we'll know which
2328 * ones are really set by the peer.
2330 asoc->peer.ipv4_address = 0;
2331 asoc->peer.ipv6_address = 0;
2333 /* Cycle through address types; avoid divide by 0. */
2334 sat = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2336 sat /= sizeof(__u16);
2338 for (i = 0; i < sat; ++i) {
2339 switch (param.sat->types[i]) {
2340 case SCTP_PARAM_IPV4_ADDRESS:
2341 asoc->peer.ipv4_address = 1;
2344 case SCTP_PARAM_IPV6_ADDRESS:
2345 asoc->peer.ipv6_address = 1;
2348 case SCTP_PARAM_HOST_NAME_ADDRESS:
2349 asoc->peer.hostname_address = 1;
2352 default: /* Just ignore anything else. */
2358 case SCTP_PARAM_STATE_COOKIE:
2359 asoc->peer.cookie_len =
2360 ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2361 asoc->peer.cookie = param.cookie->body;
2364 case SCTP_PARAM_HEARTBEAT_INFO:
2365 /* Would be odd to receive, but it causes no problems. */
2368 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2369 /* Rejected during verify stage. */
2372 case SCTP_PARAM_ECN_CAPABLE:
2373 asoc->peer.ecn_capable = 1;
2376 case SCTP_PARAM_ADAPTATION_LAYER_IND:
2377 asoc->peer.adaptation_ind = param.aind->adaptation_ind;
2380 case SCTP_PARAM_SUPPORTED_EXT:
2381 sctp_process_ext_param(asoc, param);
2384 case SCTP_PARAM_FWD_TSN_SUPPORT:
2385 if (sctp_prsctp_enable) {
2386 asoc->peer.prsctp_capable = 1;
2392 case SCTP_PARAM_RANDOM:
2393 if (!sctp_auth_enable)
2396 /* Save peer's random parameter */
2397 asoc->peer.peer_random = kmemdup(param.p,
2398 ntohs(param.p->length), gfp);
2399 if (!asoc->peer.peer_random) {
2405 case SCTP_PARAM_HMAC_ALGO:
2406 if (!sctp_auth_enable)
2409 /* Save peer's HMAC list */
2410 asoc->peer.peer_hmacs = kmemdup(param.p,
2411 ntohs(param.p->length), gfp);
2412 if (!asoc->peer.peer_hmacs) {
2417 /* Set the default HMAC the peer requested*/
2418 sctp_auth_asoc_set_default_hmac(asoc, param.hmac_algo);
2421 case SCTP_PARAM_CHUNKS:
2422 if (!sctp_auth_enable)
2425 asoc->peer.peer_chunks = kmemdup(param.p,
2426 ntohs(param.p->length), gfp);
2427 if (!asoc->peer.peer_chunks)
2432 /* Any unrecognized parameters should have been caught
2433 * and handled by sctp_verify_param() which should be
2434 * called prior to this routine. Simply log the error
2437 SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
2438 ntohs(param.p->type), asoc);
2445 /* Select a new verification tag. */
2446 __u32 sctp_generate_tag(const struct sctp_endpoint *ep)
2448 /* I believe that this random number generator complies with RFC1750.
2449 * A tag of 0 is reserved for special cases (e.g. INIT).
2454 get_random_bytes(&x, sizeof(__u32));
2460 /* Select an initial TSN to send during startup. */
2461 __u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
2465 get_random_bytes(&retval, sizeof(__u32));
2470 * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
2472 * 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
2473 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2474 * | Type = 0xC1 | Chunk Flags | Chunk Length |
2475 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2477 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2478 * | Address Parameter |
2479 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2480 * | ASCONF Parameter #1 |
2481 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2485 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2486 * | ASCONF Parameter #N |
2487 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2489 * Address Parameter and other parameter will not be wrapped in this function
2491 static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
2492 union sctp_addr *addr,
2495 sctp_addiphdr_t asconf;
2496 struct sctp_chunk *retval;
2497 int length = sizeof(asconf) + vparam_len;
2498 union sctp_addr_param addrparam;
2500 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2502 addrlen = af->to_addr_param(addr, &addrparam);
2507 /* Create the chunk. */
2508 retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF, 0, length);
2512 asconf.serial = htonl(asoc->addip_serial++);
2514 retval->subh.addip_hdr =
2515 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2516 retval->param_hdr.v =
2517 sctp_addto_chunk(retval, addrlen, &addrparam);
2523 * 3.2.1 Add IP Address
2525 * 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
2526 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2527 * | Type = 0xC001 | Length = Variable |
2528 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2529 * | ASCONF-Request Correlation ID |
2530 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2531 * | Address Parameter |
2532 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2534 * 3.2.2 Delete IP Address
2536 * 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
2537 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2538 * | Type = 0xC002 | Length = Variable |
2539 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2540 * | ASCONF-Request Correlation ID |
2541 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2542 * | Address Parameter |
2543 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2546 struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
2547 union sctp_addr *laddr,
2548 struct sockaddr *addrs,
2552 sctp_addip_param_t param;
2553 struct sctp_chunk *retval;
2554 union sctp_addr_param addr_param;
2555 union sctp_addr *addr;
2558 int paramlen = sizeof(param);
2559 int addr_param_len = 0;
2563 /* Get total length of all the address parameters. */
2565 for (i = 0; i < addrcnt; i++) {
2566 addr = (union sctp_addr *)addr_buf;
2567 af = sctp_get_af_specific(addr->v4.sin_family);
2568 addr_param_len = af->to_addr_param(addr, &addr_param);
2570 totallen += paramlen;
2571 totallen += addr_param_len;
2573 addr_buf += af->sockaddr_len;
2576 /* Create an asconf chunk with the required length. */
2577 retval = sctp_make_asconf(asoc, laddr, totallen);
2581 /* Add the address parameters to the asconf chunk. */
2583 for (i = 0; i < addrcnt; i++) {
2584 addr = (union sctp_addr *)addr_buf;
2585 af = sctp_get_af_specific(addr->v4.sin_family);
2586 addr_param_len = af->to_addr_param(addr, &addr_param);
2587 param.param_hdr.type = flags;
2588 param.param_hdr.length = htons(paramlen + addr_param_len);
2591 sctp_addto_chunk(retval, paramlen, ¶m);
2592 sctp_addto_chunk(retval, addr_param_len, &addr_param);
2594 addr_buf += af->sockaddr_len;
2600 * 3.2.4 Set Primary IP Address
2602 * 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
2603 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2604 * | Type =0xC004 | Length = Variable |
2605 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2606 * | ASCONF-Request Correlation ID |
2607 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2608 * | Address Parameter |
2609 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2611 * Create an ASCONF chunk with Set Primary IP address parameter.
2613 struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
2614 union sctp_addr *addr)
2616 sctp_addip_param_t param;
2617 struct sctp_chunk *retval;
2618 int len = sizeof(param);
2619 union sctp_addr_param addrparam;
2621 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2623 addrlen = af->to_addr_param(addr, &addrparam);
2628 /* Create the chunk and make asconf header. */
2629 retval = sctp_make_asconf(asoc, addr, len);
2633 param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
2634 param.param_hdr.length = htons(len);
2637 sctp_addto_chunk(retval, sizeof(param), ¶m);
2638 sctp_addto_chunk(retval, addrlen, &addrparam);
2643 /* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2645 * 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
2646 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2647 * | Type = 0x80 | Chunk Flags | Chunk Length |
2648 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2650 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2651 * | ASCONF Parameter Response#1 |
2652 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2656 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2657 * | ASCONF Parameter Response#N |
2658 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2660 * Create an ASCONF_ACK chunk with enough space for the parameter responses.
2662 static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
2663 __u32 serial, int vparam_len)
2665 sctp_addiphdr_t asconf;
2666 struct sctp_chunk *retval;
2667 int length = sizeof(asconf) + vparam_len;
2669 /* Create the chunk. */
2670 retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF_ACK, 0, length);
2674 asconf.serial = htonl(serial);
2676 retval->subh.addip_hdr =
2677 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2682 /* Add response parameters to an ASCONF_ACK chunk. */
2683 static void sctp_add_asconf_response(struct sctp_chunk *chunk, __be32 crr_id,
2684 __be16 err_code, sctp_addip_param_t *asconf_param)
2686 sctp_addip_param_t ack_param;
2687 sctp_errhdr_t err_param;
2688 int asconf_param_len = 0;
2689 int err_param_len = 0;
2690 __be16 response_type;
2692 if (SCTP_ERROR_NO_ERROR == err_code) {
2693 response_type = SCTP_PARAM_SUCCESS_REPORT;
2695 response_type = SCTP_PARAM_ERR_CAUSE;
2696 err_param_len = sizeof(err_param);
2699 ntohs(asconf_param->param_hdr.length);
2702 /* Add Success Indication or Error Cause Indication parameter. */
2703 ack_param.param_hdr.type = response_type;
2704 ack_param.param_hdr.length = htons(sizeof(ack_param) +
2707 ack_param.crr_id = crr_id;
2708 sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
2710 if (SCTP_ERROR_NO_ERROR == err_code)
2713 /* Add Error Cause parameter. */
2714 err_param.cause = err_code;
2715 err_param.length = htons(err_param_len + asconf_param_len);
2716 sctp_addto_chunk(chunk, err_param_len, &err_param);
2718 /* Add the failed TLV copied from ASCONF chunk. */
2720 sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
2723 /* Process a asconf parameter. */
2724 static __be16 sctp_process_asconf_param(struct sctp_association *asoc,
2725 struct sctp_chunk *asconf,
2726 sctp_addip_param_t *asconf_param)
2728 struct sctp_transport *peer;
2730 union sctp_addr addr;
2731 struct list_head *pos;
2732 union sctp_addr_param *addr_param;
2734 addr_param = (union sctp_addr_param *)
2735 ((void *)asconf_param + sizeof(sctp_addip_param_t));
2737 af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2739 return SCTP_ERROR_INV_PARAM;
2741 af->from_addr_param(&addr, addr_param, htons(asoc->peer.port), 0);
2742 switch (asconf_param->param_hdr.type) {
2743 case SCTP_PARAM_ADD_IP:
2744 /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
2745 * request and does not have the local resources to add this
2746 * new address to the association, it MUST return an Error
2747 * Cause TLV set to the new error code 'Operation Refused
2748 * Due to Resource Shortage'.
2751 peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC, SCTP_UNCONFIRMED);
2753 return SCTP_ERROR_RSRC_LOW;
2755 /* Start the heartbeat timer. */
2756 if (!mod_timer(&peer->hb_timer, sctp_transport_timeout(peer)))
2757 sctp_transport_hold(peer);
2759 case SCTP_PARAM_DEL_IP:
2760 /* ADDIP 4.3 D7) If a request is received to delete the
2761 * last remaining IP address of a peer endpoint, the receiver
2762 * MUST send an Error Cause TLV with the error cause set to the
2763 * new error code 'Request to Delete Last Remaining IP Address'.
2765 pos = asoc->peer.transport_addr_list.next;
2766 if (pos->next == &asoc->peer.transport_addr_list)
2767 return SCTP_ERROR_DEL_LAST_IP;
2769 /* ADDIP 4.3 D8) If a request is received to delete an IP
2770 * address which is also the source address of the IP packet
2771 * which contained the ASCONF chunk, the receiver MUST reject
2772 * this request. To reject the request the receiver MUST send
2773 * an Error Cause TLV set to the new error code 'Request to
2774 * Delete Source IP Address'
2776 if (sctp_cmp_addr_exact(sctp_source(asconf), &addr))
2777 return SCTP_ERROR_DEL_SRC_IP;
2779 sctp_assoc_del_peer(asoc, &addr);
2781 case SCTP_PARAM_SET_PRIMARY:
2782 peer = sctp_assoc_lookup_paddr(asoc, &addr);
2784 return SCTP_ERROR_INV_PARAM;
2786 sctp_assoc_set_primary(asoc, peer);
2789 return SCTP_ERROR_INV_PARAM;
2793 return SCTP_ERROR_NO_ERROR;
2796 /* Verify the ASCONF packet before we process it. */
2797 int sctp_verify_asconf(const struct sctp_association *asoc,
2798 struct sctp_paramhdr *param_hdr, void *chunk_end,
2799 struct sctp_paramhdr **errp) {
2800 sctp_addip_param_t *asconf_param;
2801 union sctp_params param;
2804 param.v = (sctp_paramhdr_t *) param_hdr;
2805 while (param.v <= chunk_end - sizeof(sctp_paramhdr_t)) {
2806 length = ntohs(param.p->length);
2809 if (param.v > chunk_end - length ||
2810 length < sizeof(sctp_paramhdr_t))
2813 switch (param.p->type) {
2814 case SCTP_PARAM_ADD_IP:
2815 case SCTP_PARAM_DEL_IP:
2816 case SCTP_PARAM_SET_PRIMARY:
2817 asconf_param = (sctp_addip_param_t *)param.v;
2818 plen = ntohs(asconf_param->param_hdr.length);
2819 if (plen < sizeof(sctp_addip_param_t) +
2820 sizeof(sctp_paramhdr_t))
2823 case SCTP_PARAM_SUCCESS_REPORT:
2824 case SCTP_PARAM_ADAPTATION_LAYER_IND:
2825 if (length != sizeof(sctp_addip_param_t))
2833 param.v += WORD_ROUND(length);
2836 if (param.v != chunk_end)
2842 /* Process an incoming ASCONF chunk with the next expected serial no. and
2843 * return an ASCONF_ACK chunk to be sent in response.
2845 struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
2846 struct sctp_chunk *asconf)
2848 sctp_addiphdr_t *hdr;
2849 union sctp_addr_param *addr_param;
2850 sctp_addip_param_t *asconf_param;
2851 struct sctp_chunk *asconf_ack;
2855 int chunk_len = asconf->skb->len;
2857 int all_param_pass = 1;
2859 hdr = (sctp_addiphdr_t *)asconf->skb->data;
2860 serial = ntohl(hdr->serial);
2862 /* Skip the addiphdr and store a pointer to address parameter. */
2863 length = sizeof(sctp_addiphdr_t);
2864 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
2865 chunk_len -= length;
2867 /* Skip the address parameter and store a pointer to the first
2870 length = ntohs(addr_param->v4.param_hdr.length);
2871 asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
2872 chunk_len -= length;
2874 /* create an ASCONF_ACK chunk.
2875 * Based on the definitions of parameters, we know that the size of
2876 * ASCONF_ACK parameters are less than or equal to the twice of ASCONF
2879 asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 2);
2883 /* Process the TLVs contained within the ASCONF chunk. */
2884 while (chunk_len > 0) {
2885 err_code = sctp_process_asconf_param(asoc, asconf,
2888 * If an error response is received for a TLV parameter,
2889 * all TLVs with no response before the failed TLV are
2890 * considered successful if not reported. All TLVs after
2891 * the failed response are considered unsuccessful unless
2892 * a specific success indication is present for the parameter.
2894 if (SCTP_ERROR_NO_ERROR != err_code)
2897 if (!all_param_pass)
2898 sctp_add_asconf_response(asconf_ack,
2899 asconf_param->crr_id, err_code,
2902 /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
2903 * an IP address sends an 'Out of Resource' in its response, it
2904 * MUST also fail any subsequent add or delete requests bundled
2907 if (SCTP_ERROR_RSRC_LOW == err_code)
2910 /* Move to the next ASCONF param. */
2911 length = ntohs(asconf_param->param_hdr.length);
2912 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
2914 chunk_len -= length;
2918 asoc->peer.addip_serial++;
2920 /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
2921 * after freeing the reference to old asconf ack if any.
2924 if (asoc->addip_last_asconf_ack)
2925 sctp_chunk_free(asoc->addip_last_asconf_ack);
2927 sctp_chunk_hold(asconf_ack);
2928 asoc->addip_last_asconf_ack = asconf_ack;
2934 /* Process a asconf parameter that is successfully acked. */
2935 static int sctp_asconf_param_success(struct sctp_association *asoc,
2936 sctp_addip_param_t *asconf_param)
2939 union sctp_addr addr;
2940 struct sctp_bind_addr *bp = &asoc->base.bind_addr;
2941 union sctp_addr_param *addr_param;
2942 struct list_head *pos;
2943 struct sctp_transport *transport;
2944 struct sctp_sockaddr_entry *saddr;
2947 addr_param = (union sctp_addr_param *)
2948 ((void *)asconf_param + sizeof(sctp_addip_param_t));
2950 /* We have checked the packet before, so we do not check again. */
2951 af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2952 af->from_addr_param(&addr, addr_param, htons(bp->port), 0);
2954 switch (asconf_param->param_hdr.type) {
2955 case SCTP_PARAM_ADD_IP:
2956 /* This is always done in BH context with a socket lock
2957 * held, so the list can not change.
2959 list_for_each_entry(saddr, &bp->address_list, list) {
2960 if (sctp_cmp_addr_exact(&saddr->a, &addr))
2961 saddr->use_as_src = 1;
2964 case SCTP_PARAM_DEL_IP:
2965 retval = sctp_del_bind_addr(bp, &addr, call_rcu_bh);
2966 list_for_each(pos, &asoc->peer.transport_addr_list) {
2967 transport = list_entry(pos, struct sctp_transport,
2969 dst_release(transport->dst);
2970 sctp_transport_route(transport, NULL,
2971 sctp_sk(asoc->base.sk));
2981 /* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
2982 * for the given asconf parameter. If there is no response for this parameter,
2983 * return the error code based on the third argument 'no_err'.
2985 * A7) If an error response is received for a TLV parameter, all TLVs with no
2986 * response before the failed TLV are considered successful if not reported.
2987 * All TLVs after the failed response are considered unsuccessful unless a
2988 * specific success indication is present for the parameter.
2990 static __be16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
2991 sctp_addip_param_t *asconf_param,
2994 sctp_addip_param_t *asconf_ack_param;
2995 sctp_errhdr_t *err_param;
2997 int asconf_ack_len = asconf_ack->skb->len;
3001 err_code = SCTP_ERROR_NO_ERROR;
3003 err_code = SCTP_ERROR_REQ_REFUSED;
3005 /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
3006 * the first asconf_ack parameter.
3008 length = sizeof(sctp_addiphdr_t);
3009 asconf_ack_param = (sctp_addip_param_t *)(asconf_ack->skb->data +
3011 asconf_ack_len -= length;
3013 while (asconf_ack_len > 0) {
3014 if (asconf_ack_param->crr_id == asconf_param->crr_id) {
3015 switch(asconf_ack_param->param_hdr.type) {
3016 case SCTP_PARAM_SUCCESS_REPORT:
3017 return SCTP_ERROR_NO_ERROR;
3018 case SCTP_PARAM_ERR_CAUSE:
3019 length = sizeof(sctp_addip_param_t);
3020 err_param = (sctp_errhdr_t *)
3021 ((void *)asconf_ack_param + length);
3022 asconf_ack_len -= length;
3023 if (asconf_ack_len > 0)
3024 return err_param->cause;
3026 return SCTP_ERROR_INV_PARAM;
3029 return SCTP_ERROR_INV_PARAM;
3033 length = ntohs(asconf_ack_param->param_hdr.length);
3034 asconf_ack_param = (sctp_addip_param_t *)
3035 ((void *)asconf_ack_param + length);
3036 asconf_ack_len -= length;
3042 /* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
3043 int sctp_process_asconf_ack(struct sctp_association *asoc,
3044 struct sctp_chunk *asconf_ack)
3046 struct sctp_chunk *asconf = asoc->addip_last_asconf;
3047 union sctp_addr_param *addr_param;
3048 sctp_addip_param_t *asconf_param;
3050 int asconf_len = asconf->skb->len;
3051 int all_param_pass = 0;
3054 __be16 err_code = SCTP_ERROR_NO_ERROR;
3056 /* Skip the chunkhdr and addiphdr from the last asconf sent and store
3057 * a pointer to address parameter.
3059 length = sizeof(sctp_addip_chunk_t);
3060 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
3061 asconf_len -= length;
3063 /* Skip the address parameter in the last asconf sent and store a
3064 * pointer to the first asconf paramter.
3066 length = ntohs(addr_param->v4.param_hdr.length);
3067 asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
3068 asconf_len -= length;
3071 * A8) If there is no response(s) to specific TLV parameter(s), and no
3072 * failures are indicated, then all request(s) are considered
3075 if (asconf_ack->skb->len == sizeof(sctp_addiphdr_t))
3078 /* Process the TLVs contained in the last sent ASCONF chunk. */
3079 while (asconf_len > 0) {
3081 err_code = SCTP_ERROR_NO_ERROR;
3083 err_code = sctp_get_asconf_response(asconf_ack,
3086 if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
3091 case SCTP_ERROR_NO_ERROR:
3092 retval = sctp_asconf_param_success(asoc, asconf_param);
3095 case SCTP_ERROR_RSRC_LOW:
3099 case SCTP_ERROR_INV_PARAM:
3100 /* Disable sending this type of asconf parameter in
3103 asoc->peer.addip_disabled_mask |=
3104 asconf_param->param_hdr.type;
3107 case SCTP_ERROR_REQ_REFUSED:
3108 case SCTP_ERROR_DEL_LAST_IP:
3109 case SCTP_ERROR_DEL_SRC_IP:
3114 /* Skip the processed asconf parameter and move to the next
3117 length = ntohs(asconf_param->param_hdr.length);
3118 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
3120 asconf_len -= length;
3123 /* Free the cached last sent asconf chunk. */
3124 sctp_chunk_free(asconf);
3125 asoc->addip_last_asconf = NULL;
3127 /* Send the next asconf chunk from the addip chunk queue. */
3128 if (!list_empty(&asoc->addip_chunk_list)) {
3129 struct list_head *entry = asoc->addip_chunk_list.next;
3130 asconf = list_entry(entry, struct sctp_chunk, list);
3132 list_del_init(entry);
3134 /* Hold the chunk until an ASCONF_ACK is received. */
3135 sctp_chunk_hold(asconf);
3136 if (sctp_primitive_ASCONF(asoc, asconf))
3137 sctp_chunk_free(asconf);
3139 asoc->addip_last_asconf = asconf;
3145 /* Make a FWD TSN chunk. */
3146 struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
3147 __u32 new_cum_tsn, size_t nstreams,
3148 struct sctp_fwdtsn_skip *skiplist)
3150 struct sctp_chunk *retval = NULL;
3151 struct sctp_fwdtsn_chunk *ftsn_chunk;
3152 struct sctp_fwdtsn_hdr ftsn_hdr;
3153 struct sctp_fwdtsn_skip skip;
3157 hint = (nstreams + 1) * sizeof(__u32);
3159 retval = sctp_make_chunk(asoc, SCTP_CID_FWD_TSN, 0, hint);
3164 ftsn_chunk = (struct sctp_fwdtsn_chunk *)retval->subh.fwdtsn_hdr;
3166 ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
3167 retval->subh.fwdtsn_hdr =
3168 sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
3170 for (i = 0; i < nstreams; i++) {
3171 skip.stream = skiplist[i].stream;
3172 skip.ssn = skiplist[i].ssn;
3173 sctp_addto_chunk(retval, sizeof(skip), &skip);