sctp: fix append error cause to ERROR chunk correctly
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / sctp / sm_make_chunk.c
blob8579b4f2f24f5bd5077bdaa9b142b1e292daeb53
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)
17 * any later version.
19 * This SCTP implementation is distributed in the hope that it
20 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
21 * ************************
22 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 * See the GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with GNU CC; see the file COPYING. If not, write to
27 * the Free Software Foundation, 59 Temple Place - Suite 330,
28 * Boston, MA 02111-1307, USA.
30 * Please send any bug reports or fixes you make to the
31 * email address(es):
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>
55 #include <linux/ip.h>
56 #include <linux/ipv6.h>
57 #include <linux/net.h>
58 #include <linux/inet.h>
59 #include <linux/scatterlist.h>
60 #include <linux/crypto.h>
61 #include <net/sock.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>
68 SCTP_STATIC
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,
74 int *cookie_len,
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,
79 gfp_t gfp);
80 static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
81 const void *data);
83 /* What was the inbound interface for this chunk? */
84 int sctp_chunk_iif(const struct sctp_chunk *chunk)
86 struct sctp_af *af;
87 int iif = 0;
89 af = sctp_get_af_specific(ipver2af(ip_hdr(chunk->skb)->version));
90 if (af)
91 iif = af->skb_iif(chunk->skb);
93 return iif;
96 /* RFC 2960 3.3.2 Initiation (INIT) (1)
98 * Note 2: The ECN capable field is reserved for future use of
99 * Explicit Congestion Notification.
101 static const struct sctp_paramhdr ecap_param = {
102 SCTP_PARAM_ECN_CAPABLE,
103 cpu_to_be16(sizeof(struct sctp_paramhdr)),
105 static const struct sctp_paramhdr prsctp_param = {
106 SCTP_PARAM_FWD_TSN_SUPPORT,
107 cpu_to_be16(sizeof(struct sctp_paramhdr)),
110 /* A helper to initialize an op error inside a
111 * provided chunk, as most cause codes will be embedded inside an
112 * abort chunk.
114 void sctp_init_cause(struct sctp_chunk *chunk, __be16 cause_code,
115 size_t paylen)
117 sctp_errhdr_t err;
118 __u16 len;
120 /* Cause code constants are now defined in network order. */
121 err.cause = cause_code;
122 len = sizeof(sctp_errhdr_t) + paylen;
123 err.length = htons(len);
124 chunk->subh.err_hdr = sctp_addto_chunk(chunk, sizeof(sctp_errhdr_t), &err);
127 /* A helper to initialize an op error inside a
128 * provided chunk, as most cause codes will be embedded inside an
129 * abort chunk. Differs from sctp_init_cause in that it won't oops
130 * if there isn't enough space in the op error chunk
132 int sctp_init_cause_fixed(struct sctp_chunk *chunk, __be16 cause_code,
133 size_t paylen)
135 sctp_errhdr_t err;
136 __u16 len;
138 /* Cause code constants are now defined in network order. */
139 err.cause = cause_code;
140 len = sizeof(sctp_errhdr_t) + paylen;
141 err.length = htons(len);
143 if (skb_tailroom(chunk->skb) < len)
144 return -ENOSPC;
145 chunk->subh.err_hdr = sctp_addto_chunk_fixed(chunk,
146 sizeof(sctp_errhdr_t),
147 &err);
148 return 0;
150 /* 3.3.2 Initiation (INIT) (1)
152 * This chunk is used to initiate a SCTP association between two
153 * endpoints. The format of the INIT chunk is shown below:
155 * 0 1 2 3
156 * 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
157 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
158 * | Type = 1 | Chunk Flags | Chunk Length |
159 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
160 * | Initiate Tag |
161 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
162 * | Advertised Receiver Window Credit (a_rwnd) |
163 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
164 * | Number of Outbound Streams | Number of Inbound Streams |
165 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
166 * | Initial TSN |
167 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
168 * \ \
169 * / Optional/Variable-Length Parameters /
170 * \ \
171 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
174 * The INIT chunk contains the following parameters. Unless otherwise
175 * noted, each parameter MUST only be included once in the INIT chunk.
177 * Fixed Parameters Status
178 * ----------------------------------------------
179 * Initiate Tag Mandatory
180 * Advertised Receiver Window Credit Mandatory
181 * Number of Outbound Streams Mandatory
182 * Number of Inbound Streams Mandatory
183 * Initial TSN Mandatory
185 * Variable Parameters Status Type Value
186 * -------------------------------------------------------------
187 * IPv4 Address (Note 1) Optional 5
188 * IPv6 Address (Note 1) Optional 6
189 * Cookie Preservative Optional 9
190 * Reserved for ECN Capable (Note 2) Optional 32768 (0x8000)
191 * Host Name Address (Note 3) Optional 11
192 * Supported Address Types (Note 4) Optional 12
194 struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
195 const struct sctp_bind_addr *bp,
196 gfp_t gfp, int vparam_len)
198 sctp_inithdr_t init;
199 union sctp_params addrs;
200 size_t chunksize;
201 struct sctp_chunk *retval = NULL;
202 int num_types, addrs_len = 0;
203 struct sctp_sock *sp;
204 sctp_supported_addrs_param_t sat;
205 __be16 types[2];
206 sctp_adaptation_ind_param_t aiparam;
207 sctp_supported_ext_param_t ext_param;
208 int num_ext = 0;
209 __u8 extensions[3];
210 sctp_paramhdr_t *auth_chunks = NULL,
211 *auth_hmacs = NULL;
213 /* RFC 2960 3.3.2 Initiation (INIT) (1)
215 * Note 1: The INIT chunks can contain multiple addresses that
216 * can be IPv4 and/or IPv6 in any combination.
218 retval = NULL;
220 /* Convert the provided bind address list to raw format. */
221 addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, gfp);
223 init.init_tag = htonl(asoc->c.my_vtag);
224 init.a_rwnd = htonl(asoc->rwnd);
225 init.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
226 init.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
227 init.initial_tsn = htonl(asoc->c.initial_tsn);
229 /* How many address types are needed? */
230 sp = sctp_sk(asoc->base.sk);
231 num_types = sp->pf->supported_addrs(sp, types);
233 chunksize = sizeof(init) + addrs_len + SCTP_SAT_LEN(num_types);
234 chunksize += sizeof(ecap_param);
236 if (sctp_prsctp_enable)
237 chunksize += sizeof(prsctp_param);
239 /* ADDIP: Section 4.2.7:
240 * An implementation supporting this extension [ADDIP] MUST list
241 * the ASCONF,the ASCONF-ACK, and the AUTH chunks in its INIT and
242 * INIT-ACK parameters.
244 if (sctp_addip_enable) {
245 extensions[num_ext] = SCTP_CID_ASCONF;
246 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
247 num_ext += 2;
250 if (sp->adaptation_ind)
251 chunksize += sizeof(aiparam);
253 chunksize += vparam_len;
255 /* Account for AUTH related parameters */
256 if (sctp_auth_enable) {
257 /* Add random parameter length*/
258 chunksize += sizeof(asoc->c.auth_random);
260 /* Add HMACS parameter length if any were defined */
261 auth_hmacs = (sctp_paramhdr_t *)asoc->c.auth_hmacs;
262 if (auth_hmacs->length)
263 chunksize += ntohs(auth_hmacs->length);
264 else
265 auth_hmacs = NULL;
267 /* Add CHUNKS parameter length */
268 auth_chunks = (sctp_paramhdr_t *)asoc->c.auth_chunks;
269 if (auth_chunks->length)
270 chunksize += ntohs(auth_chunks->length);
271 else
272 auth_chunks = NULL;
274 extensions[num_ext] = SCTP_CID_AUTH;
275 num_ext += 1;
278 /* If we have any extensions to report, account for that */
279 if (num_ext)
280 chunksize += sizeof(sctp_supported_ext_param_t) + num_ext;
282 /* RFC 2960 3.3.2 Initiation (INIT) (1)
284 * Note 3: An INIT chunk MUST NOT contain more than one Host
285 * Name address parameter. Moreover, the sender of the INIT
286 * MUST NOT combine any other address types with the Host Name
287 * address in the INIT. The receiver of INIT MUST ignore any
288 * other address types if the Host Name address parameter is
289 * present in the received INIT chunk.
291 * PLEASE DO NOT FIXME [This version does not support Host Name.]
294 retval = sctp_make_chunk(asoc, SCTP_CID_INIT, 0, chunksize);
295 if (!retval)
296 goto nodata;
298 retval->subh.init_hdr =
299 sctp_addto_chunk(retval, sizeof(init), &init);
300 retval->param_hdr.v =
301 sctp_addto_chunk(retval, addrs_len, addrs.v);
303 /* RFC 2960 3.3.2 Initiation (INIT) (1)
305 * Note 4: This parameter, when present, specifies all the
306 * address types the sending endpoint can support. The absence
307 * of this parameter indicates that the sending endpoint can
308 * support any address type.
310 sat.param_hdr.type = SCTP_PARAM_SUPPORTED_ADDRESS_TYPES;
311 sat.param_hdr.length = htons(SCTP_SAT_LEN(num_types));
312 sctp_addto_chunk(retval, sizeof(sat), &sat);
313 sctp_addto_chunk(retval, num_types * sizeof(__u16), &types);
315 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
317 /* Add the supported extensions parameter. Be nice and add this
318 * fist before addiding the parameters for the extensions themselves
320 if (num_ext) {
321 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
322 ext_param.param_hdr.length =
323 htons(sizeof(sctp_supported_ext_param_t) + num_ext);
324 sctp_addto_chunk(retval, sizeof(sctp_supported_ext_param_t),
325 &ext_param);
326 sctp_addto_param(retval, num_ext, extensions);
329 if (sctp_prsctp_enable)
330 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
332 if (sp->adaptation_ind) {
333 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
334 aiparam.param_hdr.length = htons(sizeof(aiparam));
335 aiparam.adaptation_ind = htonl(sp->adaptation_ind);
336 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
339 /* Add SCTP-AUTH chunks to the parameter list */
340 if (sctp_auth_enable) {
341 sctp_addto_chunk(retval, sizeof(asoc->c.auth_random),
342 asoc->c.auth_random);
343 if (auth_hmacs)
344 sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
345 auth_hmacs);
346 if (auth_chunks)
347 sctp_addto_chunk(retval, ntohs(auth_chunks->length),
348 auth_chunks);
350 nodata:
351 kfree(addrs.v);
352 return retval;
355 struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
356 const struct sctp_chunk *chunk,
357 gfp_t gfp, int unkparam_len)
359 sctp_inithdr_t initack;
360 struct sctp_chunk *retval;
361 union sctp_params addrs;
362 struct sctp_sock *sp;
363 int addrs_len;
364 sctp_cookie_param_t *cookie;
365 int cookie_len;
366 size_t chunksize;
367 sctp_adaptation_ind_param_t aiparam;
368 sctp_supported_ext_param_t ext_param;
369 int num_ext = 0;
370 __u8 extensions[3];
371 sctp_paramhdr_t *auth_chunks = NULL,
372 *auth_hmacs = NULL,
373 *auth_random = NULL;
375 retval = NULL;
377 /* Note: there may be no addresses to embed. */
378 addrs = sctp_bind_addrs_to_raw(&asoc->base.bind_addr, &addrs_len, gfp);
380 initack.init_tag = htonl(asoc->c.my_vtag);
381 initack.a_rwnd = htonl(asoc->rwnd);
382 initack.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
383 initack.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
384 initack.initial_tsn = htonl(asoc->c.initial_tsn);
386 /* FIXME: We really ought to build the cookie right
387 * into the packet instead of allocating more fresh memory.
389 cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
390 addrs.v, addrs_len);
391 if (!cookie)
392 goto nomem_cookie;
394 /* Calculate the total size of allocation, include the reserved
395 * space for reporting unknown parameters if it is specified.
397 sp = sctp_sk(asoc->base.sk);
398 chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;
400 /* Tell peer that we'll do ECN only if peer advertised such cap. */
401 if (asoc->peer.ecn_capable)
402 chunksize += sizeof(ecap_param);
404 if (asoc->peer.prsctp_capable)
405 chunksize += sizeof(prsctp_param);
407 if (asoc->peer.asconf_capable) {
408 extensions[num_ext] = SCTP_CID_ASCONF;
409 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
410 num_ext += 2;
413 if (sp->adaptation_ind)
414 chunksize += sizeof(aiparam);
416 if (asoc->peer.auth_capable) {
417 auth_random = (sctp_paramhdr_t *)asoc->c.auth_random;
418 chunksize += ntohs(auth_random->length);
420 auth_hmacs = (sctp_paramhdr_t *)asoc->c.auth_hmacs;
421 if (auth_hmacs->length)
422 chunksize += ntohs(auth_hmacs->length);
423 else
424 auth_hmacs = NULL;
426 auth_chunks = (sctp_paramhdr_t *)asoc->c.auth_chunks;
427 if (auth_chunks->length)
428 chunksize += ntohs(auth_chunks->length);
429 else
430 auth_chunks = NULL;
432 extensions[num_ext] = SCTP_CID_AUTH;
433 num_ext += 1;
436 if (num_ext)
437 chunksize += sizeof(sctp_supported_ext_param_t) + num_ext;
439 /* Now allocate and fill out the chunk. */
440 retval = sctp_make_chunk(asoc, SCTP_CID_INIT_ACK, 0, chunksize);
441 if (!retval)
442 goto nomem_chunk;
444 /* Per the advice in RFC 2960 6.4, send this reply to
445 * the source of the INIT packet.
447 retval->transport = chunk->transport;
448 retval->subh.init_hdr =
449 sctp_addto_chunk(retval, sizeof(initack), &initack);
450 retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
451 sctp_addto_chunk(retval, cookie_len, cookie);
452 if (asoc->peer.ecn_capable)
453 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
454 if (num_ext) {
455 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
456 ext_param.param_hdr.length =
457 htons(sizeof(sctp_supported_ext_param_t) + num_ext);
458 sctp_addto_chunk(retval, sizeof(sctp_supported_ext_param_t),
459 &ext_param);
460 sctp_addto_param(retval, num_ext, extensions);
462 if (asoc->peer.prsctp_capable)
463 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
465 if (sp->adaptation_ind) {
466 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
467 aiparam.param_hdr.length = htons(sizeof(aiparam));
468 aiparam.adaptation_ind = htonl(sp->adaptation_ind);
469 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
472 if (asoc->peer.auth_capable) {
473 sctp_addto_chunk(retval, ntohs(auth_random->length),
474 auth_random);
475 if (auth_hmacs)
476 sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
477 auth_hmacs);
478 if (auth_chunks)
479 sctp_addto_chunk(retval, ntohs(auth_chunks->length),
480 auth_chunks);
483 /* We need to remove the const qualifier at this point. */
484 retval->asoc = (struct sctp_association *) asoc;
486 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
488 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
489 * HEARTBEAT ACK, * etc.) to the same destination transport
490 * address from which it received the DATA or control chunk
491 * to which it is replying.
493 * [INIT ACK back to where the INIT came from.]
495 if (chunk)
496 retval->transport = chunk->transport;
498 nomem_chunk:
499 kfree(cookie);
500 nomem_cookie:
501 kfree(addrs.v);
502 return retval;
505 /* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
507 * This chunk is used only during the initialization of an association.
508 * It is sent by the initiator of an association to its peer to complete
509 * the initialization process. This chunk MUST precede any DATA chunk
510 * sent within the association, but MAY be bundled with one or more DATA
511 * chunks in the same packet.
513 * 0 1 2 3
514 * 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
515 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
516 * | Type = 10 |Chunk Flags | Length |
517 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
518 * / Cookie /
519 * \ \
520 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
522 * Chunk Flags: 8 bit
524 * Set to zero on transmit and ignored on receipt.
526 * Length: 16 bits (unsigned integer)
528 * Set to the size of the chunk in bytes, including the 4 bytes of
529 * the chunk header and the size of the Cookie.
531 * Cookie: variable size
533 * This field must contain the exact cookie received in the
534 * State Cookie parameter from the previous INIT ACK.
536 * An implementation SHOULD make the cookie as small as possible
537 * to insure interoperability.
539 struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
540 const struct sctp_chunk *chunk)
542 struct sctp_chunk *retval;
543 void *cookie;
544 int cookie_len;
546 cookie = asoc->peer.cookie;
547 cookie_len = asoc->peer.cookie_len;
549 /* Build a cookie echo chunk. */
550 retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ECHO, 0, cookie_len);
551 if (!retval)
552 goto nodata;
553 retval->subh.cookie_hdr =
554 sctp_addto_chunk(retval, cookie_len, cookie);
556 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
558 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
559 * HEARTBEAT ACK, * etc.) to the same destination transport
560 * address from which it * received the DATA or control chunk
561 * to which it is replying.
563 * [COOKIE ECHO back to where the INIT ACK came from.]
565 if (chunk)
566 retval->transport = chunk->transport;
568 nodata:
569 return retval;
572 /* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
574 * This chunk is used only during the initialization of an
575 * association. It is used to acknowledge the receipt of a COOKIE
576 * ECHO chunk. This chunk MUST precede any DATA or SACK chunk sent
577 * within the association, but MAY be bundled with one or more DATA
578 * chunks or SACK chunk in the same SCTP packet.
580 * 0 1 2 3
581 * 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
582 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
583 * | Type = 11 |Chunk Flags | Length = 4 |
584 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
586 * Chunk Flags: 8 bits
588 * Set to zero on transmit and ignored on receipt.
590 struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
591 const struct sctp_chunk *chunk)
593 struct sctp_chunk *retval;
595 retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ACK, 0, 0);
597 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
599 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
600 * HEARTBEAT ACK, * etc.) to the same destination transport
601 * address from which it * received the DATA or control chunk
602 * to which it is replying.
604 * [COOKIE ACK back to where the COOKIE ECHO came from.]
606 if (retval && chunk)
607 retval->transport = chunk->transport;
609 return retval;
613 * Appendix A: Explicit Congestion Notification:
614 * CWR:
616 * RFC 2481 details a specific bit for a sender to send in the header of
617 * its next outbound TCP segment to indicate to its peer that it has
618 * reduced its congestion window. This is termed the CWR bit. For
619 * SCTP the same indication is made by including the CWR chunk.
620 * This chunk contains one data element, i.e. the TSN number that
621 * was sent in the ECNE chunk. This element represents the lowest
622 * TSN number in the datagram that was originally marked with the
623 * CE bit.
625 * 0 1 2 3
626 * 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
627 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
628 * | Chunk Type=13 | Flags=00000000| Chunk Length = 8 |
629 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
630 * | Lowest TSN Number |
631 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
633 * Note: The CWR is considered a Control chunk.
635 struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
636 const __u32 lowest_tsn,
637 const struct sctp_chunk *chunk)
639 struct sctp_chunk *retval;
640 sctp_cwrhdr_t cwr;
642 cwr.lowest_tsn = htonl(lowest_tsn);
643 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_CWR, 0,
644 sizeof(sctp_cwrhdr_t));
646 if (!retval)
647 goto nodata;
649 retval->subh.ecn_cwr_hdr =
650 sctp_addto_chunk(retval, sizeof(cwr), &cwr);
652 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
654 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
655 * HEARTBEAT ACK, * etc.) to the same destination transport
656 * address from which it * received the DATA or control chunk
657 * to which it is replying.
659 * [Report a reduced congestion window back to where the ECNE
660 * came from.]
662 if (chunk)
663 retval->transport = chunk->transport;
665 nodata:
666 return retval;
669 /* Make an ECNE chunk. This is a congestion experienced report. */
670 struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
671 const __u32 lowest_tsn)
673 struct sctp_chunk *retval;
674 sctp_ecnehdr_t ecne;
676 ecne.lowest_tsn = htonl(lowest_tsn);
677 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_ECNE, 0,
678 sizeof(sctp_ecnehdr_t));
679 if (!retval)
680 goto nodata;
681 retval->subh.ecne_hdr =
682 sctp_addto_chunk(retval, sizeof(ecne), &ecne);
684 nodata:
685 return retval;
688 /* Make a DATA chunk for the given association from the provided
689 * parameters. However, do not populate the data payload.
691 struct sctp_chunk *sctp_make_datafrag_empty(struct sctp_association *asoc,
692 const struct sctp_sndrcvinfo *sinfo,
693 int data_len, __u8 flags, __u16 ssn)
695 struct sctp_chunk *retval;
696 struct sctp_datahdr dp;
697 int chunk_len;
699 /* We assign the TSN as LATE as possible, not here when
700 * creating the chunk.
702 dp.tsn = 0;
703 dp.stream = htons(sinfo->sinfo_stream);
704 dp.ppid = sinfo->sinfo_ppid;
706 /* Set the flags for an unordered send. */
707 if (sinfo->sinfo_flags & SCTP_UNORDERED) {
708 flags |= SCTP_DATA_UNORDERED;
709 dp.ssn = 0;
710 } else
711 dp.ssn = htons(ssn);
713 chunk_len = sizeof(dp) + data_len;
714 retval = sctp_make_chunk(asoc, SCTP_CID_DATA, flags, chunk_len);
715 if (!retval)
716 goto nodata;
718 retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
719 memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
721 nodata:
722 return retval;
725 /* Create a selective ackowledgement (SACK) for the given
726 * association. This reports on which TSN's we've seen to date,
727 * including duplicates and gaps.
729 struct sctp_chunk *sctp_make_sack(const struct sctp_association *asoc)
731 struct sctp_chunk *retval;
732 struct sctp_sackhdr sack;
733 int len;
734 __u32 ctsn;
735 __u16 num_gabs, num_dup_tsns;
736 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
737 struct sctp_gap_ack_block gabs[SCTP_MAX_GABS];
739 memset(gabs, 0, sizeof(gabs));
740 ctsn = sctp_tsnmap_get_ctsn(map);
741 SCTP_DEBUG_PRINTK("sackCTSNAck sent: 0x%x.\n", ctsn);
743 /* How much room is needed in the chunk? */
744 num_gabs = sctp_tsnmap_num_gabs(map, gabs);
745 num_dup_tsns = sctp_tsnmap_num_dups(map);
747 /* Initialize the SACK header. */
748 sack.cum_tsn_ack = htonl(ctsn);
749 sack.a_rwnd = htonl(asoc->a_rwnd);
750 sack.num_gap_ack_blocks = htons(num_gabs);
751 sack.num_dup_tsns = htons(num_dup_tsns);
753 len = sizeof(sack)
754 + sizeof(struct sctp_gap_ack_block) * num_gabs
755 + sizeof(__u32) * num_dup_tsns;
757 /* Create the chunk. */
758 retval = sctp_make_chunk(asoc, SCTP_CID_SACK, 0, len);
759 if (!retval)
760 goto nodata;
762 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
764 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
765 * HEARTBEAT ACK, etc.) to the same destination transport
766 * address from which it received the DATA or control chunk to
767 * which it is replying. This rule should also be followed if
768 * the endpoint is bundling DATA chunks together with the
769 * reply chunk.
771 * However, when acknowledging multiple DATA chunks received
772 * in packets from different source addresses in a single
773 * SACK, the SACK chunk may be transmitted to one of the
774 * destination transport addresses from which the DATA or
775 * control chunks being acknowledged were received.
777 * [BUG: We do not implement the following paragraph.
778 * Perhaps we should remember the last transport we used for a
779 * SACK and avoid that (if possible) if we have seen any
780 * duplicates. --piggy]
782 * When a receiver of a duplicate DATA chunk sends a SACK to a
783 * multi- homed endpoint it MAY be beneficial to vary the
784 * destination address and not use the source address of the
785 * DATA chunk. The reason being that receiving a duplicate
786 * from a multi-homed endpoint might indicate that the return
787 * path (as specified in the source address of the DATA chunk)
788 * for the SACK is broken.
790 * [Send to the address from which we last received a DATA chunk.]
792 retval->transport = asoc->peer.last_data_from;
794 retval->subh.sack_hdr =
795 sctp_addto_chunk(retval, sizeof(sack), &sack);
797 /* Add the gap ack block information. */
798 if (num_gabs)
799 sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
800 gabs);
802 /* Add the duplicate TSN information. */
803 if (num_dup_tsns)
804 sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
805 sctp_tsnmap_get_dups(map));
807 nodata:
808 return retval;
811 /* Make a SHUTDOWN chunk. */
812 struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
813 const struct sctp_chunk *chunk)
815 struct sctp_chunk *retval;
816 sctp_shutdownhdr_t shut;
817 __u32 ctsn;
819 ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
820 shut.cum_tsn_ack = htonl(ctsn);
822 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN, 0,
823 sizeof(sctp_shutdownhdr_t));
824 if (!retval)
825 goto nodata;
827 retval->subh.shutdown_hdr =
828 sctp_addto_chunk(retval, sizeof(shut), &shut);
830 if (chunk)
831 retval->transport = chunk->transport;
832 nodata:
833 return retval;
836 struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
837 const struct sctp_chunk *chunk)
839 struct sctp_chunk *retval;
841 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0);
843 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
845 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
846 * HEARTBEAT ACK, * etc.) to the same destination transport
847 * address from which it * received the DATA or control chunk
848 * to which it is replying.
850 * [ACK back to where the SHUTDOWN came from.]
852 if (retval && chunk)
853 retval->transport = chunk->transport;
855 return retval;
858 struct sctp_chunk *sctp_make_shutdown_complete(
859 const struct sctp_association *asoc,
860 const struct sctp_chunk *chunk)
862 struct sctp_chunk *retval;
863 __u8 flags = 0;
865 /* Set the T-bit if we have no association (vtag will be
866 * reflected)
868 flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
870 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags, 0);
872 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
874 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
875 * HEARTBEAT ACK, * etc.) to the same destination transport
876 * address from which it * received the DATA or control chunk
877 * to which it is replying.
879 * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
880 * came from.]
882 if (retval && chunk)
883 retval->transport = chunk->transport;
885 return retval;
888 /* Create an ABORT. Note that we set the T bit if we have no
889 * association, except when responding to an INIT (sctpimpguide 2.41).
891 struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
892 const struct sctp_chunk *chunk,
893 const size_t hint)
895 struct sctp_chunk *retval;
896 __u8 flags = 0;
898 /* Set the T-bit if we have no association and 'chunk' is not
899 * an INIT (vtag will be reflected).
901 if (!asoc) {
902 if (chunk && chunk->chunk_hdr &&
903 chunk->chunk_hdr->type == SCTP_CID_INIT)
904 flags = 0;
905 else
906 flags = SCTP_CHUNK_FLAG_T;
909 retval = sctp_make_chunk(asoc, SCTP_CID_ABORT, flags, hint);
911 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
913 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
914 * HEARTBEAT ACK, * etc.) to the same destination transport
915 * address from which it * received the DATA or control chunk
916 * to which it is replying.
918 * [ABORT back to where the offender came from.]
920 if (retval && chunk)
921 retval->transport = chunk->transport;
923 return retval;
926 /* Helper to create ABORT with a NO_USER_DATA error. */
927 struct sctp_chunk *sctp_make_abort_no_data(
928 const struct sctp_association *asoc,
929 const struct sctp_chunk *chunk, __u32 tsn)
931 struct sctp_chunk *retval;
932 __be32 payload;
934 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t)
935 + sizeof(tsn));
937 if (!retval)
938 goto no_mem;
940 /* Put the tsn back into network byte order. */
941 payload = htonl(tsn);
942 sctp_init_cause(retval, SCTP_ERROR_NO_DATA, sizeof(payload));
943 sctp_addto_chunk(retval, sizeof(payload), (const void *)&payload);
945 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
947 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
948 * HEARTBEAT ACK, * etc.) to the same destination transport
949 * address from which it * received the DATA or control chunk
950 * to which it is replying.
952 * [ABORT back to where the offender came from.]
954 if (chunk)
955 retval->transport = chunk->transport;
957 no_mem:
958 return retval;
961 /* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error. */
962 struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
963 const struct msghdr *msg,
964 size_t paylen)
966 struct sctp_chunk *retval;
967 void *payload = NULL;
968 int err;
970 retval = sctp_make_abort(asoc, NULL, sizeof(sctp_errhdr_t) + paylen);
971 if (!retval)
972 goto err_chunk;
974 if (paylen) {
975 /* Put the msg_iov together into payload. */
976 payload = kmalloc(paylen, GFP_KERNEL);
977 if (!payload)
978 goto err_payload;
980 err = memcpy_fromiovec(payload, msg->msg_iov, paylen);
981 if (err < 0)
982 goto err_copy;
985 sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, paylen);
986 sctp_addto_chunk(retval, paylen, payload);
988 if (paylen)
989 kfree(payload);
991 return retval;
993 err_copy:
994 kfree(payload);
995 err_payload:
996 sctp_chunk_free(retval);
997 retval = NULL;
998 err_chunk:
999 return retval;
1002 /* Append bytes to the end of a parameter. Will panic if chunk is not big
1003 * enough.
1005 static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
1006 const void *data)
1008 void *target;
1009 int chunklen = ntohs(chunk->chunk_hdr->length);
1011 target = skb_put(chunk->skb, len);
1013 memcpy(target, data, len);
1015 /* Adjust the chunk length field. */
1016 chunk->chunk_hdr->length = htons(chunklen + len);
1017 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1019 return target;
1022 /* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
1023 struct sctp_chunk *sctp_make_abort_violation(
1024 const struct sctp_association *asoc,
1025 const struct sctp_chunk *chunk,
1026 const __u8 *payload,
1027 const size_t paylen)
1029 struct sctp_chunk *retval;
1030 struct sctp_paramhdr phdr;
1032 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t) + paylen
1033 + sizeof(sctp_paramhdr_t));
1034 if (!retval)
1035 goto end;
1037 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, paylen
1038 + sizeof(sctp_paramhdr_t));
1040 phdr.type = htons(chunk->chunk_hdr->type);
1041 phdr.length = chunk->chunk_hdr->length;
1042 sctp_addto_chunk(retval, paylen, payload);
1043 sctp_addto_param(retval, sizeof(sctp_paramhdr_t), &phdr);
1045 end:
1046 return retval;
1049 struct sctp_chunk *sctp_make_violation_paramlen(
1050 const struct sctp_association *asoc,
1051 const struct sctp_chunk *chunk,
1052 struct sctp_paramhdr *param)
1054 struct sctp_chunk *retval;
1055 static const char error[] = "The following parameter had invalid length:";
1056 size_t payload_len = sizeof(error) + sizeof(sctp_errhdr_t) +
1057 sizeof(sctp_paramhdr_t);
1059 retval = sctp_make_abort(asoc, chunk, payload_len);
1060 if (!retval)
1061 goto nodata;
1063 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION,
1064 sizeof(error) + sizeof(sctp_paramhdr_t));
1065 sctp_addto_chunk(retval, sizeof(error), error);
1066 sctp_addto_param(retval, sizeof(sctp_paramhdr_t), param);
1068 nodata:
1069 return retval;
1072 /* Make a HEARTBEAT chunk. */
1073 struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
1074 const struct sctp_transport *transport,
1075 const void *payload, const size_t paylen)
1077 struct sctp_chunk *retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT,
1078 0, paylen);
1080 if (!retval)
1081 goto nodata;
1083 /* Cast away the 'const', as this is just telling the chunk
1084 * what transport it belongs to.
1086 retval->transport = (struct sctp_transport *) transport;
1087 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
1089 nodata:
1090 return retval;
1093 struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
1094 const struct sctp_chunk *chunk,
1095 const void *payload, const size_t paylen)
1097 struct sctp_chunk *retval;
1099 retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen);
1100 if (!retval)
1101 goto nodata;
1103 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
1105 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1107 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1108 * HEARTBEAT ACK, * etc.) to the same destination transport
1109 * address from which it * received the DATA or control chunk
1110 * to which it is replying.
1112 * [HBACK back to where the HEARTBEAT came from.]
1114 if (chunk)
1115 retval->transport = chunk->transport;
1117 nodata:
1118 return retval;
1121 /* Create an Operation Error chunk with the specified space reserved.
1122 * This routine can be used for containing multiple causes in the chunk.
1124 static struct sctp_chunk *sctp_make_op_error_space(
1125 const struct sctp_association *asoc,
1126 const struct sctp_chunk *chunk,
1127 size_t size)
1129 struct sctp_chunk *retval;
1131 retval = sctp_make_chunk(asoc, SCTP_CID_ERROR, 0,
1132 sizeof(sctp_errhdr_t) + size);
1133 if (!retval)
1134 goto nodata;
1136 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1138 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1139 * HEARTBEAT ACK, etc.) to the same destination transport
1140 * address from which it received the DATA or control chunk
1141 * to which it is replying.
1144 if (chunk)
1145 retval->transport = chunk->transport;
1147 nodata:
1148 return retval;
1151 /* Create an Operation Error chunk of a fixed size,
1152 * specifically, max(asoc->pathmtu, SCTP_DEFAULT_MAXSEGMENT)
1153 * This is a helper function to allocate an error chunk for
1154 * for those invalid parameter codes in which we may not want
1155 * to report all the errors, if the incomming chunk is large
1157 static inline struct sctp_chunk *sctp_make_op_error_fixed(
1158 const struct sctp_association *asoc,
1159 const struct sctp_chunk *chunk)
1161 size_t size = asoc ? asoc->pathmtu : 0;
1163 if (!size)
1164 size = SCTP_DEFAULT_MAXSEGMENT;
1166 return sctp_make_op_error_space(asoc, chunk, size);
1169 /* Create an Operation Error chunk. */
1170 struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
1171 const struct sctp_chunk *chunk,
1172 __be16 cause_code, const void *payload,
1173 size_t paylen)
1175 struct sctp_chunk *retval;
1177 retval = sctp_make_op_error_space(asoc, chunk, paylen);
1178 if (!retval)
1179 goto nodata;
1181 sctp_init_cause(retval, cause_code, paylen);
1182 sctp_addto_chunk(retval, paylen, payload);
1184 nodata:
1185 return retval;
1188 struct sctp_chunk *sctp_make_auth(const struct sctp_association *asoc)
1190 struct sctp_chunk *retval;
1191 struct sctp_hmac *hmac_desc;
1192 struct sctp_authhdr auth_hdr;
1193 __u8 *hmac;
1195 /* Get the first hmac that the peer told us to use */
1196 hmac_desc = sctp_auth_asoc_get_hmac(asoc);
1197 if (unlikely(!hmac_desc))
1198 return NULL;
1200 retval = sctp_make_chunk(asoc, SCTP_CID_AUTH, 0,
1201 hmac_desc->hmac_len + sizeof(sctp_authhdr_t));
1202 if (!retval)
1203 return NULL;
1205 auth_hdr.hmac_id = htons(hmac_desc->hmac_id);
1206 auth_hdr.shkey_id = htons(asoc->active_key_id);
1208 retval->subh.auth_hdr = sctp_addto_chunk(retval, sizeof(sctp_authhdr_t),
1209 &auth_hdr);
1211 hmac = skb_put(retval->skb, hmac_desc->hmac_len);
1212 memset(hmac, 0, hmac_desc->hmac_len);
1214 /* Adjust the chunk header to include the empty MAC */
1215 retval->chunk_hdr->length =
1216 htons(ntohs(retval->chunk_hdr->length) + hmac_desc->hmac_len);
1217 retval->chunk_end = skb_tail_pointer(retval->skb);
1219 return retval;
1223 /********************************************************************
1224 * 2nd Level Abstractions
1225 ********************************************************************/
1227 /* Turn an skb into a chunk.
1228 * FIXME: Eventually move the structure directly inside the skb->cb[].
1230 struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
1231 const struct sctp_association *asoc,
1232 struct sock *sk)
1234 struct sctp_chunk *retval;
1236 retval = kmem_cache_zalloc(sctp_chunk_cachep, GFP_ATOMIC);
1238 if (!retval)
1239 goto nodata;
1241 if (!sk) {
1242 SCTP_DEBUG_PRINTK("chunkifying skb %p w/o an sk\n", skb);
1245 INIT_LIST_HEAD(&retval->list);
1246 retval->skb = skb;
1247 retval->asoc = (struct sctp_association *)asoc;
1248 retval->resent = 0;
1249 retval->has_tsn = 0;
1250 retval->has_ssn = 0;
1251 retval->rtt_in_progress = 0;
1252 retval->sent_at = 0;
1253 retval->singleton = 1;
1254 retval->end_of_packet = 0;
1255 retval->ecn_ce_done = 0;
1256 retval->pdiscard = 0;
1258 /* sctpimpguide-05.txt Section 2.8.2
1259 * M1) Each time a new DATA chunk is transmitted
1260 * set the 'TSN.Missing.Report' count for that TSN to 0. The
1261 * 'TSN.Missing.Report' count will be used to determine missing chunks
1262 * and when to fast retransmit.
1264 retval->tsn_missing_report = 0;
1265 retval->tsn_gap_acked = 0;
1266 retval->fast_retransmit = SCTP_CAN_FRTX;
1268 /* If this is a fragmented message, track all fragments
1269 * of the message (for SEND_FAILED).
1271 retval->msg = NULL;
1273 /* Polish the bead hole. */
1274 INIT_LIST_HEAD(&retval->transmitted_list);
1275 INIT_LIST_HEAD(&retval->frag_list);
1276 SCTP_DBG_OBJCNT_INC(chunk);
1277 atomic_set(&retval->refcnt, 1);
1279 nodata:
1280 return retval;
1283 /* Set chunk->source and dest based on the IP header in chunk->skb. */
1284 void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
1285 union sctp_addr *dest)
1287 memcpy(&chunk->source, src, sizeof(union sctp_addr));
1288 memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
1291 /* Extract the source address from a chunk. */
1292 const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
1294 /* If we have a known transport, use that. */
1295 if (chunk->transport) {
1296 return &chunk->transport->ipaddr;
1297 } else {
1298 /* Otherwise, extract it from the IP header. */
1299 return &chunk->source;
1303 /* Create a new chunk, setting the type and flags headers from the
1304 * arguments, reserving enough space for a 'paylen' byte payload.
1306 SCTP_STATIC
1307 struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
1308 __u8 type, __u8 flags, int paylen)
1310 struct sctp_chunk *retval;
1311 sctp_chunkhdr_t *chunk_hdr;
1312 struct sk_buff *skb;
1313 struct sock *sk;
1315 /* No need to allocate LL here, as this is only a chunk. */
1316 skb = alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t) + paylen),
1317 GFP_ATOMIC);
1318 if (!skb)
1319 goto nodata;
1321 /* Make room for the chunk header. */
1322 chunk_hdr = (sctp_chunkhdr_t *)skb_put(skb, sizeof(sctp_chunkhdr_t));
1323 chunk_hdr->type = type;
1324 chunk_hdr->flags = flags;
1325 chunk_hdr->length = htons(sizeof(sctp_chunkhdr_t));
1327 sk = asoc ? asoc->base.sk : NULL;
1328 retval = sctp_chunkify(skb, asoc, sk);
1329 if (!retval) {
1330 kfree_skb(skb);
1331 goto nodata;
1334 retval->chunk_hdr = chunk_hdr;
1335 retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(struct sctp_chunkhdr);
1337 /* Determine if the chunk needs to be authenticated */
1338 if (sctp_auth_send_cid(type, asoc))
1339 retval->auth = 1;
1341 /* Set the skb to the belonging sock for accounting. */
1342 skb->sk = sk;
1344 return retval;
1345 nodata:
1346 return NULL;
1350 /* Release the memory occupied by a chunk. */
1351 static void sctp_chunk_destroy(struct sctp_chunk *chunk)
1353 BUG_ON(!list_empty(&chunk->list));
1354 list_del_init(&chunk->transmitted_list);
1356 /* Free the chunk skb data and the SCTP_chunk stub itself. */
1357 dev_kfree_skb(chunk->skb);
1359 SCTP_DBG_OBJCNT_DEC(chunk);
1360 kmem_cache_free(sctp_chunk_cachep, chunk);
1363 /* Possibly, free the chunk. */
1364 void sctp_chunk_free(struct sctp_chunk *chunk)
1366 /* Release our reference on the message tracker. */
1367 if (chunk->msg)
1368 sctp_datamsg_put(chunk->msg);
1370 sctp_chunk_put(chunk);
1373 /* Grab a reference to the chunk. */
1374 void sctp_chunk_hold(struct sctp_chunk *ch)
1376 atomic_inc(&ch->refcnt);
1379 /* Release a reference to the chunk. */
1380 void sctp_chunk_put(struct sctp_chunk *ch)
1382 if (atomic_dec_and_test(&ch->refcnt))
1383 sctp_chunk_destroy(ch);
1386 /* Append bytes to the end of a chunk. Will panic if chunk is not big
1387 * enough.
1389 void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
1391 void *target;
1392 void *padding;
1393 int chunklen = ntohs(chunk->chunk_hdr->length);
1394 int padlen = WORD_ROUND(chunklen) - chunklen;
1396 padding = skb_put(chunk->skb, padlen);
1397 target = skb_put(chunk->skb, len);
1399 memset(padding, 0, padlen);
1400 memcpy(target, data, len);
1402 /* Adjust the chunk length field. */
1403 chunk->chunk_hdr->length = htons(chunklen + padlen + len);
1404 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1406 return target;
1409 /* Append bytes to the end of a chunk. Returns NULL if there isn't sufficient
1410 * space in the chunk
1412 void *sctp_addto_chunk_fixed(struct sctp_chunk *chunk,
1413 int len, const void *data)
1415 if (skb_tailroom(chunk->skb) >= len)
1416 return sctp_addto_chunk(chunk, len, data);
1417 else
1418 return NULL;
1421 /* Append bytes from user space to the end of a chunk. Will panic if
1422 * chunk is not big enough.
1423 * Returns a kernel err value.
1425 int sctp_user_addto_chunk(struct sctp_chunk *chunk, int off, int len,
1426 struct iovec *data)
1428 __u8 *target;
1429 int err = 0;
1431 /* Make room in chunk for data. */
1432 target = skb_put(chunk->skb, len);
1434 /* Copy data (whole iovec) into chunk */
1435 if ((err = memcpy_fromiovecend(target, data, off, len)))
1436 goto out;
1438 /* Adjust the chunk length field. */
1439 chunk->chunk_hdr->length =
1440 htons(ntohs(chunk->chunk_hdr->length) + len);
1441 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1443 out:
1444 return err;
1447 /* Helper function to assign a TSN if needed. This assumes that both
1448 * the data_hdr and association have already been assigned.
1450 void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
1452 struct sctp_datamsg *msg;
1453 struct sctp_chunk *lchunk;
1454 struct sctp_stream *stream;
1455 __u16 ssn;
1456 __u16 sid;
1458 if (chunk->has_ssn)
1459 return;
1461 /* All fragments will be on the same stream */
1462 sid = ntohs(chunk->subh.data_hdr->stream);
1463 stream = &chunk->asoc->ssnmap->out;
1465 /* Now assign the sequence number to the entire message.
1466 * All fragments must have the same stream sequence number.
1468 msg = chunk->msg;
1469 list_for_each_entry(lchunk, &msg->chunks, frag_list) {
1470 if (lchunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
1471 ssn = 0;
1472 } else {
1473 if (lchunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
1474 ssn = sctp_ssn_next(stream, sid);
1475 else
1476 ssn = sctp_ssn_peek(stream, sid);
1479 lchunk->subh.data_hdr->ssn = htons(ssn);
1480 lchunk->has_ssn = 1;
1484 /* Helper function to assign a TSN if needed. This assumes that both
1485 * the data_hdr and association have already been assigned.
1487 void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
1489 if (!chunk->has_tsn) {
1490 /* This is the last possible instant to
1491 * assign a TSN.
1493 chunk->subh.data_hdr->tsn =
1494 htonl(sctp_association_get_next_tsn(chunk->asoc));
1495 chunk->has_tsn = 1;
1499 /* Create a CLOSED association to use with an incoming packet. */
1500 struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
1501 struct sctp_chunk *chunk,
1502 gfp_t gfp)
1504 struct sctp_association *asoc;
1505 struct sk_buff *skb;
1506 sctp_scope_t scope;
1507 struct sctp_af *af;
1509 /* Create the bare association. */
1510 scope = sctp_scope(sctp_source(chunk));
1511 asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
1512 if (!asoc)
1513 goto nodata;
1514 asoc->temp = 1;
1515 skb = chunk->skb;
1516 /* Create an entry for the source address of the packet. */
1517 af = sctp_get_af_specific(ipver2af(ip_hdr(skb)->version));
1518 if (unlikely(!af))
1519 goto fail;
1520 af->from_skb(&asoc->c.peer_addr, skb, 1);
1521 nodata:
1522 return asoc;
1524 fail:
1525 sctp_association_free(asoc);
1526 return NULL;
1529 /* Build a cookie representing asoc.
1530 * This INCLUDES the param header needed to put the cookie in the INIT ACK.
1532 static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
1533 const struct sctp_association *asoc,
1534 const struct sctp_chunk *init_chunk,
1535 int *cookie_len,
1536 const __u8 *raw_addrs, int addrs_len)
1538 sctp_cookie_param_t *retval;
1539 struct sctp_signed_cookie *cookie;
1540 struct scatterlist sg;
1541 int headersize, bodysize;
1542 unsigned int keylen;
1543 char *key;
1545 /* Header size is static data prior to the actual cookie, including
1546 * any padding.
1548 headersize = sizeof(sctp_paramhdr_t) +
1549 (sizeof(struct sctp_signed_cookie) -
1550 sizeof(struct sctp_cookie));
1551 bodysize = sizeof(struct sctp_cookie)
1552 + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
1554 /* Pad out the cookie to a multiple to make the signature
1555 * functions simpler to write.
1557 if (bodysize % SCTP_COOKIE_MULTIPLE)
1558 bodysize += SCTP_COOKIE_MULTIPLE
1559 - (bodysize % SCTP_COOKIE_MULTIPLE);
1560 *cookie_len = headersize + bodysize;
1562 /* Clear this memory since we are sending this data structure
1563 * out on the network.
1565 retval = kzalloc(*cookie_len, GFP_ATOMIC);
1566 if (!retval)
1567 goto nodata;
1569 cookie = (struct sctp_signed_cookie *) retval->body;
1571 /* Set up the parameter header. */
1572 retval->p.type = SCTP_PARAM_STATE_COOKIE;
1573 retval->p.length = htons(*cookie_len);
1575 /* Copy the cookie part of the association itself. */
1576 cookie->c = asoc->c;
1577 /* Save the raw address list length in the cookie. */
1578 cookie->c.raw_addr_list_len = addrs_len;
1580 /* Remember PR-SCTP capability. */
1581 cookie->c.prsctp_capable = asoc->peer.prsctp_capable;
1583 /* Save adaptation indication in the cookie. */
1584 cookie->c.adaptation_ind = asoc->peer.adaptation_ind;
1586 /* Set an expiration time for the cookie. */
1587 do_gettimeofday(&cookie->c.expiration);
1588 TIMEVAL_ADD(asoc->cookie_life, cookie->c.expiration);
1590 /* Copy the peer's init packet. */
1591 memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
1592 ntohs(init_chunk->chunk_hdr->length));
1594 /* Copy the raw local address list of the association. */
1595 memcpy((__u8 *)&cookie->c.peer_init[0] +
1596 ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
1598 if (sctp_sk(ep->base.sk)->hmac) {
1599 struct hash_desc desc;
1601 /* Sign the message. */
1602 sg_init_one(&sg, &cookie->c, bodysize);
1603 keylen = SCTP_SECRET_SIZE;
1604 key = (char *)ep->secret_key[ep->current_key];
1605 desc.tfm = sctp_sk(ep->base.sk)->hmac;
1606 desc.flags = 0;
1608 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1609 crypto_hash_digest(&desc, &sg, bodysize, cookie->signature))
1610 goto free_cookie;
1613 return retval;
1615 free_cookie:
1616 kfree(retval);
1617 nodata:
1618 *cookie_len = 0;
1619 return NULL;
1622 /* Unpack the cookie from COOKIE ECHO chunk, recreating the association. */
1623 struct sctp_association *sctp_unpack_cookie(
1624 const struct sctp_endpoint *ep,
1625 const struct sctp_association *asoc,
1626 struct sctp_chunk *chunk, gfp_t gfp,
1627 int *error, struct sctp_chunk **errp)
1629 struct sctp_association *retval = NULL;
1630 struct sctp_signed_cookie *cookie;
1631 struct sctp_cookie *bear_cookie;
1632 int headersize, bodysize, fixed_size;
1633 __u8 *digest = ep->digest;
1634 struct scatterlist sg;
1635 unsigned int keylen, len;
1636 char *key;
1637 sctp_scope_t scope;
1638 struct sk_buff *skb = chunk->skb;
1639 struct timeval tv;
1640 struct hash_desc desc;
1642 /* Header size is static data prior to the actual cookie, including
1643 * any padding.
1645 headersize = sizeof(sctp_chunkhdr_t) +
1646 (sizeof(struct sctp_signed_cookie) -
1647 sizeof(struct sctp_cookie));
1648 bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
1649 fixed_size = headersize + sizeof(struct sctp_cookie);
1651 /* Verify that the chunk looks like it even has a cookie.
1652 * There must be enough room for our cookie and our peer's
1653 * INIT chunk.
1655 len = ntohs(chunk->chunk_hdr->length);
1656 if (len < fixed_size + sizeof(struct sctp_chunkhdr))
1657 goto malformed;
1659 /* Verify that the cookie has been padded out. */
1660 if (bodysize % SCTP_COOKIE_MULTIPLE)
1661 goto malformed;
1663 /* Process the cookie. */
1664 cookie = chunk->subh.cookie_hdr;
1665 bear_cookie = &cookie->c;
1667 if (!sctp_sk(ep->base.sk)->hmac)
1668 goto no_hmac;
1670 /* Check the signature. */
1671 keylen = SCTP_SECRET_SIZE;
1672 sg_init_one(&sg, bear_cookie, bodysize);
1673 key = (char *)ep->secret_key[ep->current_key];
1674 desc.tfm = sctp_sk(ep->base.sk)->hmac;
1675 desc.flags = 0;
1677 memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
1678 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1679 crypto_hash_digest(&desc, &sg, bodysize, digest)) {
1680 *error = -SCTP_IERROR_NOMEM;
1681 goto fail;
1684 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1685 /* Try the previous key. */
1686 key = (char *)ep->secret_key[ep->last_key];
1687 memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
1688 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1689 crypto_hash_digest(&desc, &sg, bodysize, digest)) {
1690 *error = -SCTP_IERROR_NOMEM;
1691 goto fail;
1694 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1695 /* Yikes! Still bad signature! */
1696 *error = -SCTP_IERROR_BAD_SIG;
1697 goto fail;
1701 no_hmac:
1702 /* IG Section 2.35.2:
1703 * 3) Compare the port numbers and the verification tag contained
1704 * within the COOKIE ECHO chunk to the actual port numbers and the
1705 * verification tag within the SCTP common header of the received
1706 * packet. If these values do not match the packet MUST be silently
1707 * discarded,
1709 if (ntohl(chunk->sctp_hdr->vtag) != bear_cookie->my_vtag) {
1710 *error = -SCTP_IERROR_BAD_TAG;
1711 goto fail;
1714 if (chunk->sctp_hdr->source != bear_cookie->peer_addr.v4.sin_port ||
1715 ntohs(chunk->sctp_hdr->dest) != bear_cookie->my_port) {
1716 *error = -SCTP_IERROR_BAD_PORTS;
1717 goto fail;
1720 /* Check to see if the cookie is stale. If there is already
1721 * an association, there is no need to check cookie's expiration
1722 * for init collision case of lost COOKIE ACK.
1723 * If skb has been timestamped, then use the stamp, otherwise
1724 * use current time. This introduces a small possibility that
1725 * that a cookie may be considered expired, but his would only slow
1726 * down the new association establishment instead of every packet.
1728 if (sock_flag(ep->base.sk, SOCK_TIMESTAMP))
1729 skb_get_timestamp(skb, &tv);
1730 else
1731 do_gettimeofday(&tv);
1733 if (!asoc && tv_lt(bear_cookie->expiration, tv)) {
1735 * Section 3.3.10.3 Stale Cookie Error (3)
1737 * Cause of error
1738 * ---------------
1739 * Stale Cookie Error: Indicates the receipt of a valid State
1740 * Cookie that has expired.
1742 len = ntohs(chunk->chunk_hdr->length);
1743 *errp = sctp_make_op_error_space(asoc, chunk, len);
1744 if (*errp) {
1745 suseconds_t usecs = (tv.tv_sec -
1746 bear_cookie->expiration.tv_sec) * 1000000L +
1747 tv.tv_usec - bear_cookie->expiration.tv_usec;
1748 __be32 n = htonl(usecs);
1750 sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
1751 sizeof(n));
1752 sctp_addto_chunk(*errp, sizeof(n), &n);
1753 *error = -SCTP_IERROR_STALE_COOKIE;
1754 } else
1755 *error = -SCTP_IERROR_NOMEM;
1757 goto fail;
1760 /* Make a new base association. */
1761 scope = sctp_scope(sctp_source(chunk));
1762 retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
1763 if (!retval) {
1764 *error = -SCTP_IERROR_NOMEM;
1765 goto fail;
1768 /* Set up our peer's port number. */
1769 retval->peer.port = ntohs(chunk->sctp_hdr->source);
1771 /* Populate the association from the cookie. */
1772 memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
1774 if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
1775 GFP_ATOMIC) < 0) {
1776 *error = -SCTP_IERROR_NOMEM;
1777 goto fail;
1780 /* Also, add the destination address. */
1781 if (list_empty(&retval->base.bind_addr.address_list)) {
1782 sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest,
1783 SCTP_ADDR_SRC, GFP_ATOMIC);
1786 retval->next_tsn = retval->c.initial_tsn;
1787 retval->ctsn_ack_point = retval->next_tsn - 1;
1788 retval->addip_serial = retval->c.initial_tsn;
1789 retval->adv_peer_ack_point = retval->ctsn_ack_point;
1790 retval->peer.prsctp_capable = retval->c.prsctp_capable;
1791 retval->peer.adaptation_ind = retval->c.adaptation_ind;
1793 /* The INIT stuff will be done by the side effects. */
1794 return retval;
1796 fail:
1797 if (retval)
1798 sctp_association_free(retval);
1800 return NULL;
1802 malformed:
1803 /* Yikes! The packet is either corrupt or deliberately
1804 * malformed.
1806 *error = -SCTP_IERROR_MALFORMED;
1807 goto fail;
1810 /********************************************************************
1811 * 3rd Level Abstractions
1812 ********************************************************************/
1814 struct __sctp_missing {
1815 __be32 num_missing;
1816 __be16 type;
1817 } __attribute__((packed));
1820 * Report a missing mandatory parameter.
1822 static int sctp_process_missing_param(const struct sctp_association *asoc,
1823 sctp_param_t paramtype,
1824 struct sctp_chunk *chunk,
1825 struct sctp_chunk **errp)
1827 struct __sctp_missing report;
1828 __u16 len;
1830 len = WORD_ROUND(sizeof(report));
1832 /* Make an ERROR chunk, preparing enough room for
1833 * returning multiple unknown parameters.
1835 if (!*errp)
1836 *errp = sctp_make_op_error_space(asoc, chunk, len);
1838 if (*errp) {
1839 report.num_missing = htonl(1);
1840 report.type = paramtype;
1841 sctp_init_cause(*errp, SCTP_ERROR_MISS_PARAM,
1842 sizeof(report));
1843 sctp_addto_chunk(*errp, sizeof(report), &report);
1846 /* Stop processing this chunk. */
1847 return 0;
1850 /* Report an Invalid Mandatory Parameter. */
1851 static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
1852 struct sctp_chunk *chunk,
1853 struct sctp_chunk **errp)
1855 /* Invalid Mandatory Parameter Error has no payload. */
1857 if (!*errp)
1858 *errp = sctp_make_op_error_space(asoc, chunk, 0);
1860 if (*errp)
1861 sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, 0);
1863 /* Stop processing this chunk. */
1864 return 0;
1867 static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
1868 struct sctp_paramhdr *param,
1869 const struct sctp_chunk *chunk,
1870 struct sctp_chunk **errp)
1872 /* This is a fatal error. Any accumulated non-fatal errors are
1873 * not reported.
1875 if (*errp)
1876 sctp_chunk_free(*errp);
1878 /* Create an error chunk and fill it in with our payload. */
1879 *errp = sctp_make_violation_paramlen(asoc, chunk, param);
1881 return 0;
1885 /* Do not attempt to handle the HOST_NAME parm. However, do
1886 * send back an indicator to the peer.
1888 static int sctp_process_hn_param(const struct sctp_association *asoc,
1889 union sctp_params param,
1890 struct sctp_chunk *chunk,
1891 struct sctp_chunk **errp)
1893 __u16 len = ntohs(param.p->length);
1895 /* Processing of the HOST_NAME parameter will generate an
1896 * ABORT. If we've accumulated any non-fatal errors, they
1897 * would be unrecognized parameters and we should not include
1898 * them in the ABORT.
1900 if (*errp)
1901 sctp_chunk_free(*errp);
1903 *errp = sctp_make_op_error_space(asoc, chunk, len);
1905 if (*errp) {
1906 sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED, len);
1907 sctp_addto_chunk(*errp, len, param.v);
1910 /* Stop processing this chunk. */
1911 return 0;
1914 static int sctp_verify_ext_param(union sctp_params param)
1916 __u16 num_ext = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
1917 int have_auth = 0;
1918 int have_asconf = 0;
1919 int i;
1921 for (i = 0; i < num_ext; i++) {
1922 switch (param.ext->chunks[i]) {
1923 case SCTP_CID_AUTH:
1924 have_auth = 1;
1925 break;
1926 case SCTP_CID_ASCONF:
1927 case SCTP_CID_ASCONF_ACK:
1928 have_asconf = 1;
1929 break;
1933 /* ADD-IP Security: The draft requires us to ABORT or ignore the
1934 * INIT/INIT-ACK if ADD-IP is listed, but AUTH is not. Do this
1935 * only if ADD-IP is turned on and we are not backward-compatible
1936 * mode.
1938 if (sctp_addip_noauth)
1939 return 1;
1941 if (sctp_addip_enable && !have_auth && have_asconf)
1942 return 0;
1944 return 1;
1947 static void sctp_process_ext_param(struct sctp_association *asoc,
1948 union sctp_params param)
1950 __u16 num_ext = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
1951 int i;
1953 for (i = 0; i < num_ext; i++) {
1954 switch (param.ext->chunks[i]) {
1955 case SCTP_CID_FWD_TSN:
1956 if (sctp_prsctp_enable &&
1957 !asoc->peer.prsctp_capable)
1958 asoc->peer.prsctp_capable = 1;
1959 break;
1960 case SCTP_CID_AUTH:
1961 /* if the peer reports AUTH, assume that he
1962 * supports AUTH.
1964 if (sctp_auth_enable)
1965 asoc->peer.auth_capable = 1;
1966 break;
1967 case SCTP_CID_ASCONF:
1968 case SCTP_CID_ASCONF_ACK:
1969 if (sctp_addip_enable)
1970 asoc->peer.asconf_capable = 1;
1971 break;
1972 default:
1973 break;
1978 /* RFC 3.2.1 & the Implementers Guide 2.2.
1980 * The Parameter Types are encoded such that the
1981 * highest-order two bits specify the action that must be
1982 * taken if the processing endpoint does not recognize the
1983 * Parameter Type.
1985 * 00 - Stop processing this parameter; do not process any further
1986 * parameters within this chunk
1988 * 01 - Stop processing this parameter, do not process any further
1989 * parameters within this chunk, and report the unrecognized
1990 * parameter in an 'Unrecognized Parameter' ERROR chunk.
1992 * 10 - Skip this parameter and continue processing.
1994 * 11 - Skip this parameter and continue processing but
1995 * report the unrecognized parameter in an
1996 * 'Unrecognized Parameter' ERROR chunk.
1998 * Return value:
1999 * SCTP_IERROR_NO_ERROR - continue with the chunk
2000 * SCTP_IERROR_ERROR - stop and report an error.
2001 * SCTP_IERROR_NOMEME - out of memory.
2003 static sctp_ierror_t sctp_process_unk_param(const struct sctp_association *asoc,
2004 union sctp_params param,
2005 struct sctp_chunk *chunk,
2006 struct sctp_chunk **errp)
2008 int retval = SCTP_IERROR_NO_ERROR;
2010 switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
2011 case SCTP_PARAM_ACTION_DISCARD:
2012 retval = SCTP_IERROR_ERROR;
2013 break;
2014 case SCTP_PARAM_ACTION_SKIP:
2015 break;
2016 case SCTP_PARAM_ACTION_DISCARD_ERR:
2017 retval = SCTP_IERROR_ERROR;
2018 /* Fall through */
2019 case SCTP_PARAM_ACTION_SKIP_ERR:
2020 /* Make an ERROR chunk, preparing enough room for
2021 * returning multiple unknown parameters.
2023 if (NULL == *errp)
2024 *errp = sctp_make_op_error_fixed(asoc, chunk);
2026 if (*errp) {
2027 sctp_init_cause_fixed(*errp, SCTP_ERROR_UNKNOWN_PARAM,
2028 WORD_ROUND(ntohs(param.p->length)));
2029 sctp_addto_chunk_fixed(*errp,
2030 WORD_ROUND(ntohs(param.p->length)),
2031 param.v);
2032 } else {
2033 /* If there is no memory for generating the ERROR
2034 * report as specified, an ABORT will be triggered
2035 * to the peer and the association won't be
2036 * established.
2038 retval = SCTP_IERROR_NOMEM;
2040 break;
2041 default:
2042 break;
2045 return retval;
2048 /* Verify variable length parameters
2049 * Return values:
2050 * SCTP_IERROR_ABORT - trigger an ABORT
2051 * SCTP_IERROR_NOMEM - out of memory (abort)
2052 * SCTP_IERROR_ERROR - stop processing, trigger an ERROR
2053 * SCTP_IERROR_NO_ERROR - continue with the chunk
2055 static sctp_ierror_t sctp_verify_param(const struct sctp_association *asoc,
2056 union sctp_params param,
2057 sctp_cid_t cid,
2058 struct sctp_chunk *chunk,
2059 struct sctp_chunk **err_chunk)
2061 struct sctp_hmac_algo_param *hmacs;
2062 int retval = SCTP_IERROR_NO_ERROR;
2063 __u16 n_elt, id = 0;
2064 int i;
2066 /* FIXME - This routine is not looking at each parameter per the
2067 * chunk type, i.e., unrecognized parameters should be further
2068 * identified based on the chunk id.
2071 switch (param.p->type) {
2072 case SCTP_PARAM_IPV4_ADDRESS:
2073 case SCTP_PARAM_IPV6_ADDRESS:
2074 case SCTP_PARAM_COOKIE_PRESERVATIVE:
2075 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2076 case SCTP_PARAM_STATE_COOKIE:
2077 case SCTP_PARAM_HEARTBEAT_INFO:
2078 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2079 case SCTP_PARAM_ECN_CAPABLE:
2080 case SCTP_PARAM_ADAPTATION_LAYER_IND:
2081 break;
2083 case SCTP_PARAM_SUPPORTED_EXT:
2084 if (!sctp_verify_ext_param(param))
2085 return SCTP_IERROR_ABORT;
2086 break;
2088 case SCTP_PARAM_SET_PRIMARY:
2089 if (sctp_addip_enable)
2090 break;
2091 goto fallthrough;
2093 case SCTP_PARAM_HOST_NAME_ADDRESS:
2094 /* Tell the peer, we won't support this param. */
2095 sctp_process_hn_param(asoc, param, chunk, err_chunk);
2096 retval = SCTP_IERROR_ABORT;
2097 break;
2099 case SCTP_PARAM_FWD_TSN_SUPPORT:
2100 if (sctp_prsctp_enable)
2101 break;
2102 goto fallthrough;
2104 case SCTP_PARAM_RANDOM:
2105 if (!sctp_auth_enable)
2106 goto fallthrough;
2108 /* SCTP-AUTH: Secion 6.1
2109 * If the random number is not 32 byte long the association
2110 * MUST be aborted. The ABORT chunk SHOULD contain the error
2111 * cause 'Protocol Violation'.
2113 if (SCTP_AUTH_RANDOM_LENGTH !=
2114 ntohs(param.p->length) - sizeof(sctp_paramhdr_t)) {
2115 sctp_process_inv_paramlength(asoc, param.p,
2116 chunk, err_chunk);
2117 retval = SCTP_IERROR_ABORT;
2119 break;
2121 case SCTP_PARAM_CHUNKS:
2122 if (!sctp_auth_enable)
2123 goto fallthrough;
2125 /* SCTP-AUTH: Section 3.2
2126 * The CHUNKS parameter MUST be included once in the INIT or
2127 * INIT-ACK chunk if the sender wants to receive authenticated
2128 * chunks. Its maximum length is 260 bytes.
2130 if (260 < ntohs(param.p->length)) {
2131 sctp_process_inv_paramlength(asoc, param.p,
2132 chunk, err_chunk);
2133 retval = SCTP_IERROR_ABORT;
2135 break;
2137 case SCTP_PARAM_HMAC_ALGO:
2138 if (!sctp_auth_enable)
2139 goto fallthrough;
2141 hmacs = (struct sctp_hmac_algo_param *)param.p;
2142 n_elt = (ntohs(param.p->length) - sizeof(sctp_paramhdr_t)) >> 1;
2144 /* SCTP-AUTH: Section 6.1
2145 * The HMAC algorithm based on SHA-1 MUST be supported and
2146 * included in the HMAC-ALGO parameter.
2148 for (i = 0; i < n_elt; i++) {
2149 id = ntohs(hmacs->hmac_ids[i]);
2151 if (id == SCTP_AUTH_HMAC_ID_SHA1)
2152 break;
2155 if (id != SCTP_AUTH_HMAC_ID_SHA1) {
2156 sctp_process_inv_paramlength(asoc, param.p, chunk,
2157 err_chunk);
2158 retval = SCTP_IERROR_ABORT;
2160 break;
2161 fallthrough:
2162 default:
2163 SCTP_DEBUG_PRINTK("Unrecognized param: %d for chunk %d.\n",
2164 ntohs(param.p->type), cid);
2165 retval = sctp_process_unk_param(asoc, param, chunk, err_chunk);
2166 break;
2168 return retval;
2171 /* Verify the INIT packet before we process it. */
2172 int sctp_verify_init(const struct sctp_association *asoc,
2173 sctp_cid_t cid,
2174 sctp_init_chunk_t *peer_init,
2175 struct sctp_chunk *chunk,
2176 struct sctp_chunk **errp)
2178 union sctp_params param;
2179 int has_cookie = 0;
2180 int result;
2182 /* Verify stream values are non-zero. */
2183 if ((0 == peer_init->init_hdr.num_outbound_streams) ||
2184 (0 == peer_init->init_hdr.num_inbound_streams) ||
2185 (0 == peer_init->init_hdr.init_tag) ||
2186 (SCTP_DEFAULT_MINWINDOW > ntohl(peer_init->init_hdr.a_rwnd))) {
2188 return sctp_process_inv_mandatory(asoc, chunk, errp);
2191 /* Check for missing mandatory parameters. */
2192 sctp_walk_params(param, peer_init, init_hdr.params) {
2194 if (SCTP_PARAM_STATE_COOKIE == param.p->type)
2195 has_cookie = 1;
2197 } /* for (loop through all parameters) */
2199 /* There is a possibility that a parameter length was bad and
2200 * in that case we would have stoped walking the parameters.
2201 * The current param.p would point at the bad one.
2202 * Current consensus on the mailing list is to generate a PROTOCOL
2203 * VIOLATION error. We build the ERROR chunk here and let the normal
2204 * error handling code build and send the packet.
2206 if (param.v != (void*)chunk->chunk_end)
2207 return sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
2209 /* The only missing mandatory param possible today is
2210 * the state cookie for an INIT-ACK chunk.
2212 if ((SCTP_CID_INIT_ACK == cid) && !has_cookie)
2213 return sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
2214 chunk, errp);
2216 /* Verify all the variable length parameters */
2217 sctp_walk_params(param, peer_init, init_hdr.params) {
2219 result = sctp_verify_param(asoc, param, cid, chunk, errp);
2220 switch (result) {
2221 case SCTP_IERROR_ABORT:
2222 case SCTP_IERROR_NOMEM:
2223 return 0;
2224 case SCTP_IERROR_ERROR:
2225 return 1;
2226 case SCTP_IERROR_NO_ERROR:
2227 default:
2228 break;
2231 } /* for (loop through all parameters) */
2233 return 1;
2236 /* Unpack the parameters in an INIT packet into an association.
2237 * Returns 0 on failure, else success.
2238 * FIXME: This is an association method.
2240 int sctp_process_init(struct sctp_association *asoc, sctp_cid_t cid,
2241 const union sctp_addr *peer_addr,
2242 sctp_init_chunk_t *peer_init, gfp_t gfp)
2244 union sctp_params param;
2245 struct sctp_transport *transport;
2246 struct list_head *pos, *temp;
2247 char *cookie;
2249 /* We must include the address that the INIT packet came from.
2250 * This is the only address that matters for an INIT packet.
2251 * When processing a COOKIE ECHO, we retrieve the from address
2252 * of the INIT from the cookie.
2255 /* This implementation defaults to making the first transport
2256 * added as the primary transport. The source address seems to
2257 * be a a better choice than any of the embedded addresses.
2259 if (peer_addr) {
2260 if(!sctp_assoc_add_peer(asoc, peer_addr, gfp, SCTP_ACTIVE))
2261 goto nomem;
2264 /* Process the initialization parameters. */
2265 sctp_walk_params(param, peer_init, init_hdr.params) {
2267 if (!sctp_process_param(asoc, param, peer_addr, gfp))
2268 goto clean_up;
2271 /* AUTH: After processing the parameters, make sure that we
2272 * have all the required info to potentially do authentications.
2274 if (asoc->peer.auth_capable && (!asoc->peer.peer_random ||
2275 !asoc->peer.peer_hmacs))
2276 asoc->peer.auth_capable = 0;
2278 /* In a non-backward compatible mode, if the peer claims
2279 * support for ADD-IP but not AUTH, the ADD-IP spec states
2280 * that we MUST ABORT the association. Section 6. The section
2281 * also give us an option to silently ignore the packet, which
2282 * is what we'll do here.
2284 if (!sctp_addip_noauth &&
2285 (asoc->peer.asconf_capable && !asoc->peer.auth_capable)) {
2286 asoc->peer.addip_disabled_mask |= (SCTP_PARAM_ADD_IP |
2287 SCTP_PARAM_DEL_IP |
2288 SCTP_PARAM_SET_PRIMARY);
2289 asoc->peer.asconf_capable = 0;
2290 goto clean_up;
2293 /* Walk list of transports, removing transports in the UNKNOWN state. */
2294 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2295 transport = list_entry(pos, struct sctp_transport, transports);
2296 if (transport->state == SCTP_UNKNOWN) {
2297 sctp_assoc_rm_peer(asoc, transport);
2301 /* The fixed INIT headers are always in network byte
2302 * order.
2304 asoc->peer.i.init_tag =
2305 ntohl(peer_init->init_hdr.init_tag);
2306 asoc->peer.i.a_rwnd =
2307 ntohl(peer_init->init_hdr.a_rwnd);
2308 asoc->peer.i.num_outbound_streams =
2309 ntohs(peer_init->init_hdr.num_outbound_streams);
2310 asoc->peer.i.num_inbound_streams =
2311 ntohs(peer_init->init_hdr.num_inbound_streams);
2312 asoc->peer.i.initial_tsn =
2313 ntohl(peer_init->init_hdr.initial_tsn);
2315 /* Apply the upper bounds for output streams based on peer's
2316 * number of inbound streams.
2318 if (asoc->c.sinit_num_ostreams >
2319 ntohs(peer_init->init_hdr.num_inbound_streams)) {
2320 asoc->c.sinit_num_ostreams =
2321 ntohs(peer_init->init_hdr.num_inbound_streams);
2324 if (asoc->c.sinit_max_instreams >
2325 ntohs(peer_init->init_hdr.num_outbound_streams)) {
2326 asoc->c.sinit_max_instreams =
2327 ntohs(peer_init->init_hdr.num_outbound_streams);
2330 /* Copy Initiation tag from INIT to VT_peer in cookie. */
2331 asoc->c.peer_vtag = asoc->peer.i.init_tag;
2333 /* Peer Rwnd : Current calculated value of the peer's rwnd. */
2334 asoc->peer.rwnd = asoc->peer.i.a_rwnd;
2336 /* Copy cookie in case we need to resend COOKIE-ECHO. */
2337 cookie = asoc->peer.cookie;
2338 if (cookie) {
2339 asoc->peer.cookie = kmemdup(cookie, asoc->peer.cookie_len, gfp);
2340 if (!asoc->peer.cookie)
2341 goto clean_up;
2344 /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
2345 * high (for example, implementations MAY use the size of the receiver
2346 * advertised window).
2348 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
2349 transports) {
2350 transport->ssthresh = asoc->peer.i.a_rwnd;
2353 /* Set up the TSN tracking pieces. */
2354 if (!sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
2355 asoc->peer.i.initial_tsn, gfp))
2356 goto clean_up;
2358 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
2360 * The stream sequence number in all the streams shall start
2361 * from 0 when the association is established. Also, when the
2362 * stream sequence number reaches the value 65535 the next
2363 * stream sequence number shall be set to 0.
2366 /* Allocate storage for the negotiated streams if it is not a temporary
2367 * association.
2369 if (!asoc->temp) {
2370 int error;
2372 asoc->ssnmap = sctp_ssnmap_new(asoc->c.sinit_max_instreams,
2373 asoc->c.sinit_num_ostreams, gfp);
2374 if (!asoc->ssnmap)
2375 goto clean_up;
2377 error = sctp_assoc_set_id(asoc, gfp);
2378 if (error)
2379 goto clean_up;
2382 /* ADDIP Section 4.1 ASCONF Chunk Procedures
2384 * When an endpoint has an ASCONF signaled change to be sent to the
2385 * remote endpoint it should do the following:
2386 * ...
2387 * A2) A serial number should be assigned to the Chunk. The serial
2388 * number should be a monotonically increasing number. All serial
2389 * numbers are defined to be initialized at the start of the
2390 * association to the same value as the Initial TSN.
2392 asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
2393 return 1;
2395 clean_up:
2396 /* Release the transport structures. */
2397 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2398 transport = list_entry(pos, struct sctp_transport, transports);
2399 if (transport->state != SCTP_ACTIVE)
2400 sctp_assoc_rm_peer(asoc, transport);
2403 nomem:
2404 return 0;
2408 /* Update asoc with the option described in param.
2410 * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
2412 * asoc is the association to update.
2413 * param is the variable length parameter to use for update.
2414 * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
2415 * If the current packet is an INIT we want to minimize the amount of
2416 * work we do. In particular, we should not build transport
2417 * structures for the addresses.
2419 static int sctp_process_param(struct sctp_association *asoc,
2420 union sctp_params param,
2421 const union sctp_addr *peer_addr,
2422 gfp_t gfp)
2424 union sctp_addr addr;
2425 int i;
2426 __u16 sat;
2427 int retval = 1;
2428 sctp_scope_t scope;
2429 time_t stale;
2430 struct sctp_af *af;
2431 union sctp_addr_param *addr_param;
2432 struct sctp_transport *t;
2434 /* We maintain all INIT parameters in network byte order all the
2435 * time. This allows us to not worry about whether the parameters
2436 * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
2438 switch (param.p->type) {
2439 case SCTP_PARAM_IPV6_ADDRESS:
2440 if (PF_INET6 != asoc->base.sk->sk_family)
2441 break;
2442 goto do_addr_param;
2444 case SCTP_PARAM_IPV4_ADDRESS:
2445 /* v4 addresses are not allowed on v6-only socket */
2446 if (ipv6_only_sock(asoc->base.sk))
2447 break;
2448 do_addr_param:
2449 af = sctp_get_af_specific(param_type2af(param.p->type));
2450 af->from_addr_param(&addr, param.addr, htons(asoc->peer.port), 0);
2451 scope = sctp_scope(peer_addr);
2452 if (sctp_in_scope(&addr, scope))
2453 if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_UNCONFIRMED))
2454 return 0;
2455 break;
2457 case SCTP_PARAM_COOKIE_PRESERVATIVE:
2458 if (!sctp_cookie_preserve_enable)
2459 break;
2461 stale = ntohl(param.life->lifespan_increment);
2463 /* Suggested Cookie Life span increment's unit is msec,
2464 * (1/1000sec).
2466 asoc->cookie_life.tv_sec += stale / 1000;
2467 asoc->cookie_life.tv_usec += (stale % 1000) * 1000;
2468 break;
2470 case SCTP_PARAM_HOST_NAME_ADDRESS:
2471 SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
2472 break;
2474 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2475 /* Turn off the default values first so we'll know which
2476 * ones are really set by the peer.
2478 asoc->peer.ipv4_address = 0;
2479 asoc->peer.ipv6_address = 0;
2481 /* Assume that peer supports the address family
2482 * by which it sends a packet.
2484 if (peer_addr->sa.sa_family == AF_INET6)
2485 asoc->peer.ipv6_address = 1;
2486 else if (peer_addr->sa.sa_family == AF_INET)
2487 asoc->peer.ipv4_address = 1;
2489 /* Cycle through address types; avoid divide by 0. */
2490 sat = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2491 if (sat)
2492 sat /= sizeof(__u16);
2494 for (i = 0; i < sat; ++i) {
2495 switch (param.sat->types[i]) {
2496 case SCTP_PARAM_IPV4_ADDRESS:
2497 asoc->peer.ipv4_address = 1;
2498 break;
2500 case SCTP_PARAM_IPV6_ADDRESS:
2501 if (PF_INET6 == asoc->base.sk->sk_family)
2502 asoc->peer.ipv6_address = 1;
2503 break;
2505 case SCTP_PARAM_HOST_NAME_ADDRESS:
2506 asoc->peer.hostname_address = 1;
2507 break;
2509 default: /* Just ignore anything else. */
2510 break;
2513 break;
2515 case SCTP_PARAM_STATE_COOKIE:
2516 asoc->peer.cookie_len =
2517 ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2518 asoc->peer.cookie = param.cookie->body;
2519 break;
2521 case SCTP_PARAM_HEARTBEAT_INFO:
2522 /* Would be odd to receive, but it causes no problems. */
2523 break;
2525 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2526 /* Rejected during verify stage. */
2527 break;
2529 case SCTP_PARAM_ECN_CAPABLE:
2530 asoc->peer.ecn_capable = 1;
2531 break;
2533 case SCTP_PARAM_ADAPTATION_LAYER_IND:
2534 asoc->peer.adaptation_ind = ntohl(param.aind->adaptation_ind);
2535 break;
2537 case SCTP_PARAM_SET_PRIMARY:
2538 if (!sctp_addip_enable)
2539 goto fall_through;
2541 addr_param = param.v + sizeof(sctp_addip_param_t);
2543 af = sctp_get_af_specific(param_type2af(param.p->type));
2544 af->from_addr_param(&addr, addr_param,
2545 htons(asoc->peer.port), 0);
2547 /* if the address is invalid, we can't process it.
2548 * XXX: see spec for what to do.
2550 if (!af->addr_valid(&addr, NULL, NULL))
2551 break;
2553 t = sctp_assoc_lookup_paddr(asoc, &addr);
2554 if (!t)
2555 break;
2557 sctp_assoc_set_primary(asoc, t);
2558 break;
2560 case SCTP_PARAM_SUPPORTED_EXT:
2561 sctp_process_ext_param(asoc, param);
2562 break;
2564 case SCTP_PARAM_FWD_TSN_SUPPORT:
2565 if (sctp_prsctp_enable) {
2566 asoc->peer.prsctp_capable = 1;
2567 break;
2569 /* Fall Through */
2570 goto fall_through;
2572 case SCTP_PARAM_RANDOM:
2573 if (!sctp_auth_enable)
2574 goto fall_through;
2576 /* Save peer's random parameter */
2577 asoc->peer.peer_random = kmemdup(param.p,
2578 ntohs(param.p->length), gfp);
2579 if (!asoc->peer.peer_random) {
2580 retval = 0;
2581 break;
2583 break;
2585 case SCTP_PARAM_HMAC_ALGO:
2586 if (!sctp_auth_enable)
2587 goto fall_through;
2589 /* Save peer's HMAC list */
2590 asoc->peer.peer_hmacs = kmemdup(param.p,
2591 ntohs(param.p->length), gfp);
2592 if (!asoc->peer.peer_hmacs) {
2593 retval = 0;
2594 break;
2597 /* Set the default HMAC the peer requested*/
2598 sctp_auth_asoc_set_default_hmac(asoc, param.hmac_algo);
2599 break;
2601 case SCTP_PARAM_CHUNKS:
2602 if (!sctp_auth_enable)
2603 goto fall_through;
2605 asoc->peer.peer_chunks = kmemdup(param.p,
2606 ntohs(param.p->length), gfp);
2607 if (!asoc->peer.peer_chunks)
2608 retval = 0;
2609 break;
2610 fall_through:
2611 default:
2612 /* Any unrecognized parameters should have been caught
2613 * and handled by sctp_verify_param() which should be
2614 * called prior to this routine. Simply log the error
2615 * here.
2617 SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
2618 ntohs(param.p->type), asoc);
2619 break;
2622 return retval;
2625 /* Select a new verification tag. */
2626 __u32 sctp_generate_tag(const struct sctp_endpoint *ep)
2628 /* I believe that this random number generator complies with RFC1750.
2629 * A tag of 0 is reserved for special cases (e.g. INIT).
2631 __u32 x;
2633 do {
2634 get_random_bytes(&x, sizeof(__u32));
2635 } while (x == 0);
2637 return x;
2640 /* Select an initial TSN to send during startup. */
2641 __u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
2643 __u32 retval;
2645 get_random_bytes(&retval, sizeof(__u32));
2646 return retval;
2650 * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
2651 * 0 1 2 3
2652 * 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
2653 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2654 * | Type = 0xC1 | Chunk Flags | Chunk Length |
2655 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2656 * | Serial Number |
2657 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2658 * | Address Parameter |
2659 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2660 * | ASCONF Parameter #1 |
2661 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2662 * \ \
2663 * / .... /
2664 * \ \
2665 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2666 * | ASCONF Parameter #N |
2667 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2669 * Address Parameter and other parameter will not be wrapped in this function
2671 static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
2672 union sctp_addr *addr,
2673 int vparam_len)
2675 sctp_addiphdr_t asconf;
2676 struct sctp_chunk *retval;
2677 int length = sizeof(asconf) + vparam_len;
2678 union sctp_addr_param addrparam;
2679 int addrlen;
2680 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2682 addrlen = af->to_addr_param(addr, &addrparam);
2683 if (!addrlen)
2684 return NULL;
2685 length += addrlen;
2687 /* Create the chunk. */
2688 retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF, 0, length);
2689 if (!retval)
2690 return NULL;
2692 asconf.serial = htonl(asoc->addip_serial++);
2694 retval->subh.addip_hdr =
2695 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2696 retval->param_hdr.v =
2697 sctp_addto_chunk(retval, addrlen, &addrparam);
2699 return retval;
2702 /* ADDIP
2703 * 3.2.1 Add IP Address
2704 * 0 1 2 3
2705 * 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
2706 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2707 * | Type = 0xC001 | Length = Variable |
2708 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2709 * | ASCONF-Request Correlation ID |
2710 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2711 * | Address Parameter |
2712 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2714 * 3.2.2 Delete IP Address
2715 * 0 1 2 3
2716 * 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
2717 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2718 * | Type = 0xC002 | Length = Variable |
2719 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2720 * | ASCONF-Request Correlation ID |
2721 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2722 * | Address Parameter |
2723 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2726 struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
2727 union sctp_addr *laddr,
2728 struct sockaddr *addrs,
2729 int addrcnt,
2730 __be16 flags)
2732 sctp_addip_param_t param;
2733 struct sctp_chunk *retval;
2734 union sctp_addr_param addr_param;
2735 union sctp_addr *addr;
2736 void *addr_buf;
2737 struct sctp_af *af;
2738 int paramlen = sizeof(param);
2739 int addr_param_len = 0;
2740 int totallen = 0;
2741 int i;
2743 /* Get total length of all the address parameters. */
2744 addr_buf = addrs;
2745 for (i = 0; i < addrcnt; i++) {
2746 addr = (union sctp_addr *)addr_buf;
2747 af = sctp_get_af_specific(addr->v4.sin_family);
2748 addr_param_len = af->to_addr_param(addr, &addr_param);
2750 totallen += paramlen;
2751 totallen += addr_param_len;
2753 addr_buf += af->sockaddr_len;
2756 /* Create an asconf chunk with the required length. */
2757 retval = sctp_make_asconf(asoc, laddr, totallen);
2758 if (!retval)
2759 return NULL;
2761 /* Add the address parameters to the asconf chunk. */
2762 addr_buf = addrs;
2763 for (i = 0; i < addrcnt; i++) {
2764 addr = (union sctp_addr *)addr_buf;
2765 af = sctp_get_af_specific(addr->v4.sin_family);
2766 addr_param_len = af->to_addr_param(addr, &addr_param);
2767 param.param_hdr.type = flags;
2768 param.param_hdr.length = htons(paramlen + addr_param_len);
2769 param.crr_id = i;
2771 sctp_addto_chunk(retval, paramlen, &param);
2772 sctp_addto_chunk(retval, addr_param_len, &addr_param);
2774 addr_buf += af->sockaddr_len;
2776 return retval;
2779 /* ADDIP
2780 * 3.2.4 Set Primary IP Address
2781 * 0 1 2 3
2782 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
2783 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2784 * | Type =0xC004 | Length = Variable |
2785 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2786 * | ASCONF-Request Correlation ID |
2787 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2788 * | Address Parameter |
2789 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2791 * Create an ASCONF chunk with Set Primary IP address parameter.
2793 struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
2794 union sctp_addr *addr)
2796 sctp_addip_param_t param;
2797 struct sctp_chunk *retval;
2798 int len = sizeof(param);
2799 union sctp_addr_param addrparam;
2800 int addrlen;
2801 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2803 addrlen = af->to_addr_param(addr, &addrparam);
2804 if (!addrlen)
2805 return NULL;
2806 len += addrlen;
2808 /* Create the chunk and make asconf header. */
2809 retval = sctp_make_asconf(asoc, addr, len);
2810 if (!retval)
2811 return NULL;
2813 param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
2814 param.param_hdr.length = htons(len);
2815 param.crr_id = 0;
2817 sctp_addto_chunk(retval, sizeof(param), &param);
2818 sctp_addto_chunk(retval, addrlen, &addrparam);
2820 return retval;
2823 /* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2824 * 0 1 2 3
2825 * 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
2826 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2827 * | Type = 0x80 | Chunk Flags | Chunk Length |
2828 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2829 * | Serial Number |
2830 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2831 * | ASCONF Parameter Response#1 |
2832 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2833 * \ \
2834 * / .... /
2835 * \ \
2836 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2837 * | ASCONF Parameter Response#N |
2838 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2840 * Create an ASCONF_ACK chunk with enough space for the parameter responses.
2842 static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
2843 __u32 serial, int vparam_len)
2845 sctp_addiphdr_t asconf;
2846 struct sctp_chunk *retval;
2847 int length = sizeof(asconf) + vparam_len;
2849 /* Create the chunk. */
2850 retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF_ACK, 0, length);
2851 if (!retval)
2852 return NULL;
2854 asconf.serial = htonl(serial);
2856 retval->subh.addip_hdr =
2857 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2859 return retval;
2862 /* Add response parameters to an ASCONF_ACK chunk. */
2863 static void sctp_add_asconf_response(struct sctp_chunk *chunk, __be32 crr_id,
2864 __be16 err_code, sctp_addip_param_t *asconf_param)
2866 sctp_addip_param_t ack_param;
2867 sctp_errhdr_t err_param;
2868 int asconf_param_len = 0;
2869 int err_param_len = 0;
2870 __be16 response_type;
2872 if (SCTP_ERROR_NO_ERROR == err_code) {
2873 response_type = SCTP_PARAM_SUCCESS_REPORT;
2874 } else {
2875 response_type = SCTP_PARAM_ERR_CAUSE;
2876 err_param_len = sizeof(err_param);
2877 if (asconf_param)
2878 asconf_param_len =
2879 ntohs(asconf_param->param_hdr.length);
2882 /* Add Success Indication or Error Cause Indication parameter. */
2883 ack_param.param_hdr.type = response_type;
2884 ack_param.param_hdr.length = htons(sizeof(ack_param) +
2885 err_param_len +
2886 asconf_param_len);
2887 ack_param.crr_id = crr_id;
2888 sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
2890 if (SCTP_ERROR_NO_ERROR == err_code)
2891 return;
2893 /* Add Error Cause parameter. */
2894 err_param.cause = err_code;
2895 err_param.length = htons(err_param_len + asconf_param_len);
2896 sctp_addto_chunk(chunk, err_param_len, &err_param);
2898 /* Add the failed TLV copied from ASCONF chunk. */
2899 if (asconf_param)
2900 sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
2903 /* Process a asconf parameter. */
2904 static __be16 sctp_process_asconf_param(struct sctp_association *asoc,
2905 struct sctp_chunk *asconf,
2906 sctp_addip_param_t *asconf_param)
2908 struct sctp_transport *peer;
2909 struct sctp_af *af;
2910 union sctp_addr addr;
2911 union sctp_addr_param *addr_param;
2913 addr_param = (union sctp_addr_param *)
2914 ((void *)asconf_param + sizeof(sctp_addip_param_t));
2916 if (asconf_param->param_hdr.type != SCTP_PARAM_ADD_IP &&
2917 asconf_param->param_hdr.type != SCTP_PARAM_DEL_IP &&
2918 asconf_param->param_hdr.type != SCTP_PARAM_SET_PRIMARY)
2919 return SCTP_ERROR_UNKNOWN_PARAM;
2921 switch (addr_param->v4.param_hdr.type) {
2922 case SCTP_PARAM_IPV6_ADDRESS:
2923 if (!asoc->peer.ipv6_address)
2924 return SCTP_ERROR_DNS_FAILED;
2925 break;
2926 case SCTP_PARAM_IPV4_ADDRESS:
2927 if (!asoc->peer.ipv4_address)
2928 return SCTP_ERROR_DNS_FAILED;
2929 break;
2930 default:
2931 return SCTP_ERROR_DNS_FAILED;
2934 af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2935 if (unlikely(!af))
2936 return SCTP_ERROR_DNS_FAILED;
2938 af->from_addr_param(&addr, addr_param, htons(asoc->peer.port), 0);
2940 /* ADDIP 4.2.1 This parameter MUST NOT contain a broadcast
2941 * or multicast address.
2942 * (note: wildcard is permitted and requires special handling so
2943 * make sure we check for that)
2945 if (!af->is_any(&addr) && !af->addr_valid(&addr, NULL, asconf->skb))
2946 return SCTP_ERROR_DNS_FAILED;
2948 switch (asconf_param->param_hdr.type) {
2949 case SCTP_PARAM_ADD_IP:
2950 /* Section 4.2.1:
2951 * If the address 0.0.0.0 or ::0 is provided, the source
2952 * address of the packet MUST be added.
2954 if (af->is_any(&addr))
2955 memcpy(&addr, &asconf->source, sizeof(addr));
2957 /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
2958 * request and does not have the local resources to add this
2959 * new address to the association, it MUST return an Error
2960 * Cause TLV set to the new error code 'Operation Refused
2961 * Due to Resource Shortage'.
2964 peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC, SCTP_UNCONFIRMED);
2965 if (!peer)
2966 return SCTP_ERROR_RSRC_LOW;
2968 /* Start the heartbeat timer. */
2969 if (!mod_timer(&peer->hb_timer, sctp_transport_timeout(peer)))
2970 sctp_transport_hold(peer);
2971 break;
2972 case SCTP_PARAM_DEL_IP:
2973 /* ADDIP 4.3 D7) If a request is received to delete the
2974 * last remaining IP address of a peer endpoint, the receiver
2975 * MUST send an Error Cause TLV with the error cause set to the
2976 * new error code 'Request to Delete Last Remaining IP Address'.
2978 if (asoc->peer.transport_count == 1)
2979 return SCTP_ERROR_DEL_LAST_IP;
2981 /* ADDIP 4.3 D8) If a request is received to delete an IP
2982 * address which is also the source address of the IP packet
2983 * which contained the ASCONF chunk, the receiver MUST reject
2984 * this request. To reject the request the receiver MUST send
2985 * an Error Cause TLV set to the new error code 'Request to
2986 * Delete Source IP Address'
2988 if (sctp_cmp_addr_exact(sctp_source(asconf), &addr))
2989 return SCTP_ERROR_DEL_SRC_IP;
2991 /* Section 4.2.2
2992 * If the address 0.0.0.0 or ::0 is provided, all
2993 * addresses of the peer except the source address of the
2994 * packet MUST be deleted.
2996 if (af->is_any(&addr)) {
2997 sctp_assoc_set_primary(asoc, asconf->transport);
2998 sctp_assoc_del_nonprimary_peers(asoc,
2999 asconf->transport);
3000 } else
3001 sctp_assoc_del_peer(asoc, &addr);
3002 break;
3003 case SCTP_PARAM_SET_PRIMARY:
3004 /* ADDIP Section 4.2.4
3005 * If the address 0.0.0.0 or ::0 is provided, the receiver
3006 * MAY mark the source address of the packet as its
3007 * primary.
3009 if (af->is_any(&addr))
3010 memcpy(&addr.v4, sctp_source(asconf), sizeof(addr));
3012 peer = sctp_assoc_lookup_paddr(asoc, &addr);
3013 if (!peer)
3014 return SCTP_ERROR_DNS_FAILED;
3016 sctp_assoc_set_primary(asoc, peer);
3017 break;
3020 return SCTP_ERROR_NO_ERROR;
3023 /* Verify the ASCONF packet before we process it. */
3024 int sctp_verify_asconf(const struct sctp_association *asoc,
3025 struct sctp_paramhdr *param_hdr, void *chunk_end,
3026 struct sctp_paramhdr **errp) {
3027 sctp_addip_param_t *asconf_param;
3028 union sctp_params param;
3029 int length, plen;
3031 param.v = (sctp_paramhdr_t *) param_hdr;
3032 while (param.v <= chunk_end - sizeof(sctp_paramhdr_t)) {
3033 length = ntohs(param.p->length);
3034 *errp = param.p;
3036 if (param.v > chunk_end - length ||
3037 length < sizeof(sctp_paramhdr_t))
3038 return 0;
3040 switch (param.p->type) {
3041 case SCTP_PARAM_ADD_IP:
3042 case SCTP_PARAM_DEL_IP:
3043 case SCTP_PARAM_SET_PRIMARY:
3044 asconf_param = (sctp_addip_param_t *)param.v;
3045 plen = ntohs(asconf_param->param_hdr.length);
3046 if (plen < sizeof(sctp_addip_param_t) +
3047 sizeof(sctp_paramhdr_t))
3048 return 0;
3049 break;
3050 case SCTP_PARAM_SUCCESS_REPORT:
3051 case SCTP_PARAM_ADAPTATION_LAYER_IND:
3052 if (length != sizeof(sctp_addip_param_t))
3053 return 0;
3055 break;
3056 default:
3057 break;
3060 param.v += WORD_ROUND(length);
3063 if (param.v != chunk_end)
3064 return 0;
3066 return 1;
3069 /* Process an incoming ASCONF chunk with the next expected serial no. and
3070 * return an ASCONF_ACK chunk to be sent in response.
3072 struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
3073 struct sctp_chunk *asconf)
3075 sctp_addiphdr_t *hdr;
3076 union sctp_addr_param *addr_param;
3077 sctp_addip_param_t *asconf_param;
3078 struct sctp_chunk *asconf_ack;
3080 __be16 err_code;
3081 int length = 0;
3082 int chunk_len;
3083 __u32 serial;
3084 int all_param_pass = 1;
3086 chunk_len = ntohs(asconf->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
3087 hdr = (sctp_addiphdr_t *)asconf->skb->data;
3088 serial = ntohl(hdr->serial);
3090 /* Skip the addiphdr and store a pointer to address parameter. */
3091 length = sizeof(sctp_addiphdr_t);
3092 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
3093 chunk_len -= length;
3095 /* Skip the address parameter and store a pointer to the first
3096 * asconf parameter.
3098 length = ntohs(addr_param->v4.param_hdr.length);
3099 asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
3100 chunk_len -= length;
3102 /* create an ASCONF_ACK chunk.
3103 * Based on the definitions of parameters, we know that the size of
3104 * ASCONF_ACK parameters are less than or equal to the twice of ASCONF
3105 * parameters.
3107 asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 2);
3108 if (!asconf_ack)
3109 goto done;
3111 /* Process the TLVs contained within the ASCONF chunk. */
3112 while (chunk_len > 0) {
3113 err_code = sctp_process_asconf_param(asoc, asconf,
3114 asconf_param);
3115 /* ADDIP 4.1 A7)
3116 * If an error response is received for a TLV parameter,
3117 * all TLVs with no response before the failed TLV are
3118 * considered successful if not reported. All TLVs after
3119 * the failed response are considered unsuccessful unless
3120 * a specific success indication is present for the parameter.
3122 if (SCTP_ERROR_NO_ERROR != err_code)
3123 all_param_pass = 0;
3125 if (!all_param_pass)
3126 sctp_add_asconf_response(asconf_ack,
3127 asconf_param->crr_id, err_code,
3128 asconf_param);
3130 /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
3131 * an IP address sends an 'Out of Resource' in its response, it
3132 * MUST also fail any subsequent add or delete requests bundled
3133 * in the ASCONF.
3135 if (SCTP_ERROR_RSRC_LOW == err_code)
3136 goto done;
3138 /* Move to the next ASCONF param. */
3139 length = ntohs(asconf_param->param_hdr.length);
3140 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
3141 length);
3142 chunk_len -= length;
3145 done:
3146 asoc->peer.addip_serial++;
3148 /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
3149 * after freeing the reference to old asconf ack if any.
3151 if (asconf_ack) {
3152 sctp_chunk_hold(asconf_ack);
3153 list_add_tail(&asconf_ack->transmitted_list,
3154 &asoc->asconf_ack_list);
3157 return asconf_ack;
3160 /* Process a asconf parameter that is successfully acked. */
3161 static void sctp_asconf_param_success(struct sctp_association *asoc,
3162 sctp_addip_param_t *asconf_param)
3164 struct sctp_af *af;
3165 union sctp_addr addr;
3166 struct sctp_bind_addr *bp = &asoc->base.bind_addr;
3167 union sctp_addr_param *addr_param;
3168 struct sctp_transport *transport;
3169 struct sctp_sockaddr_entry *saddr;
3171 addr_param = (union sctp_addr_param *)
3172 ((void *)asconf_param + sizeof(sctp_addip_param_t));
3174 /* We have checked the packet before, so we do not check again. */
3175 af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
3176 af->from_addr_param(&addr, addr_param, htons(bp->port), 0);
3178 switch (asconf_param->param_hdr.type) {
3179 case SCTP_PARAM_ADD_IP:
3180 /* This is always done in BH context with a socket lock
3181 * held, so the list can not change.
3183 local_bh_disable();
3184 list_for_each_entry(saddr, &bp->address_list, list) {
3185 if (sctp_cmp_addr_exact(&saddr->a, &addr))
3186 saddr->state = SCTP_ADDR_SRC;
3188 local_bh_enable();
3189 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
3190 transports) {
3191 if (transport->state == SCTP_ACTIVE)
3192 continue;
3193 dst_release(transport->dst);
3194 sctp_transport_route(transport, NULL,
3195 sctp_sk(asoc->base.sk));
3197 break;
3198 case SCTP_PARAM_DEL_IP:
3199 local_bh_disable();
3200 sctp_del_bind_addr(bp, &addr);
3201 local_bh_enable();
3202 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
3203 transports) {
3204 dst_release(transport->dst);
3205 sctp_transport_route(transport, NULL,
3206 sctp_sk(asoc->base.sk));
3208 break;
3209 default:
3210 break;
3214 /* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
3215 * for the given asconf parameter. If there is no response for this parameter,
3216 * return the error code based on the third argument 'no_err'.
3217 * ADDIP 4.1
3218 * A7) If an error response is received for a TLV parameter, all TLVs with no
3219 * response before the failed TLV are considered successful if not reported.
3220 * All TLVs after the failed response are considered unsuccessful unless a
3221 * specific success indication is present for the parameter.
3223 static __be16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
3224 sctp_addip_param_t *asconf_param,
3225 int no_err)
3227 sctp_addip_param_t *asconf_ack_param;
3228 sctp_errhdr_t *err_param;
3229 int length;
3230 int asconf_ack_len;
3231 __be16 err_code;
3233 if (no_err)
3234 err_code = SCTP_ERROR_NO_ERROR;
3235 else
3236 err_code = SCTP_ERROR_REQ_REFUSED;
3238 asconf_ack_len = ntohs(asconf_ack->chunk_hdr->length) -
3239 sizeof(sctp_chunkhdr_t);
3241 /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
3242 * the first asconf_ack parameter.
3244 length = sizeof(sctp_addiphdr_t);
3245 asconf_ack_param = (sctp_addip_param_t *)(asconf_ack->skb->data +
3246 length);
3247 asconf_ack_len -= length;
3249 while (asconf_ack_len > 0) {
3250 if (asconf_ack_param->crr_id == asconf_param->crr_id) {
3251 switch(asconf_ack_param->param_hdr.type) {
3252 case SCTP_PARAM_SUCCESS_REPORT:
3253 return SCTP_ERROR_NO_ERROR;
3254 case SCTP_PARAM_ERR_CAUSE:
3255 length = sizeof(sctp_addip_param_t);
3256 err_param = (sctp_errhdr_t *)
3257 ((void *)asconf_ack_param + length);
3258 asconf_ack_len -= length;
3259 if (asconf_ack_len > 0)
3260 return err_param->cause;
3261 else
3262 return SCTP_ERROR_INV_PARAM;
3263 break;
3264 default:
3265 return SCTP_ERROR_INV_PARAM;
3269 length = ntohs(asconf_ack_param->param_hdr.length);
3270 asconf_ack_param = (sctp_addip_param_t *)
3271 ((void *)asconf_ack_param + length);
3272 asconf_ack_len -= length;
3275 return err_code;
3278 /* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
3279 int sctp_process_asconf_ack(struct sctp_association *asoc,
3280 struct sctp_chunk *asconf_ack)
3282 struct sctp_chunk *asconf = asoc->addip_last_asconf;
3283 union sctp_addr_param *addr_param;
3284 sctp_addip_param_t *asconf_param;
3285 int length = 0;
3286 int asconf_len = asconf->skb->len;
3287 int all_param_pass = 0;
3288 int no_err = 1;
3289 int retval = 0;
3290 __be16 err_code = SCTP_ERROR_NO_ERROR;
3292 /* Skip the chunkhdr and addiphdr from the last asconf sent and store
3293 * a pointer to address parameter.
3295 length = sizeof(sctp_addip_chunk_t);
3296 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
3297 asconf_len -= length;
3299 /* Skip the address parameter in the last asconf sent and store a
3300 * pointer to the first asconf parameter.
3302 length = ntohs(addr_param->v4.param_hdr.length);
3303 asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
3304 asconf_len -= length;
3306 /* ADDIP 4.1
3307 * A8) If there is no response(s) to specific TLV parameter(s), and no
3308 * failures are indicated, then all request(s) are considered
3309 * successful.
3311 if (asconf_ack->skb->len == sizeof(sctp_addiphdr_t))
3312 all_param_pass = 1;
3314 /* Process the TLVs contained in the last sent ASCONF chunk. */
3315 while (asconf_len > 0) {
3316 if (all_param_pass)
3317 err_code = SCTP_ERROR_NO_ERROR;
3318 else {
3319 err_code = sctp_get_asconf_response(asconf_ack,
3320 asconf_param,
3321 no_err);
3322 if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
3323 no_err = 0;
3326 switch (err_code) {
3327 case SCTP_ERROR_NO_ERROR:
3328 sctp_asconf_param_success(asoc, asconf_param);
3329 break;
3331 case SCTP_ERROR_RSRC_LOW:
3332 retval = 1;
3333 break;
3335 case SCTP_ERROR_UNKNOWN_PARAM:
3336 /* Disable sending this type of asconf parameter in
3337 * future.
3339 asoc->peer.addip_disabled_mask |=
3340 asconf_param->param_hdr.type;
3341 break;
3343 case SCTP_ERROR_REQ_REFUSED:
3344 case SCTP_ERROR_DEL_LAST_IP:
3345 case SCTP_ERROR_DEL_SRC_IP:
3346 default:
3347 break;
3350 /* Skip the processed asconf parameter and move to the next
3351 * one.
3353 length = ntohs(asconf_param->param_hdr.length);
3354 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
3355 length);
3356 asconf_len -= length;
3359 /* Free the cached last sent asconf chunk. */
3360 list_del_init(&asconf->transmitted_list);
3361 sctp_chunk_free(asconf);
3362 asoc->addip_last_asconf = NULL;
3364 /* Send the next asconf chunk from the addip chunk queue. */
3365 if (!list_empty(&asoc->addip_chunk_list)) {
3366 struct list_head *entry = asoc->addip_chunk_list.next;
3367 asconf = list_entry(entry, struct sctp_chunk, list);
3369 list_del_init(entry);
3371 /* Hold the chunk until an ASCONF_ACK is received. */
3372 sctp_chunk_hold(asconf);
3373 if (sctp_primitive_ASCONF(asoc, asconf))
3374 sctp_chunk_free(asconf);
3375 else
3376 asoc->addip_last_asconf = asconf;
3379 return retval;
3382 /* Make a FWD TSN chunk. */
3383 struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
3384 __u32 new_cum_tsn, size_t nstreams,
3385 struct sctp_fwdtsn_skip *skiplist)
3387 struct sctp_chunk *retval = NULL;
3388 struct sctp_fwdtsn_chunk *ftsn_chunk;
3389 struct sctp_fwdtsn_hdr ftsn_hdr;
3390 struct sctp_fwdtsn_skip skip;
3391 size_t hint;
3392 int i;
3394 hint = (nstreams + 1) * sizeof(__u32);
3396 retval = sctp_make_chunk(asoc, SCTP_CID_FWD_TSN, 0, hint);
3398 if (!retval)
3399 return NULL;
3401 ftsn_chunk = (struct sctp_fwdtsn_chunk *)retval->subh.fwdtsn_hdr;
3403 ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
3404 retval->subh.fwdtsn_hdr =
3405 sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
3407 for (i = 0; i < nstreams; i++) {
3408 skip.stream = skiplist[i].stream;
3409 skip.ssn = skiplist[i].ssn;
3410 sctp_addto_chunk(retval, sizeof(skip), &skip);
3413 return retval;