code style scripts/checkpatch.pl (linux-3.9-rc1) formatting
[linux-2.6.34.14-moxart.git] / net / sctp / sm_make_chunk.c
blobad633d9244603f974d61b6587cbcf045218c994f
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 <linux/slab.h>
62 #include <net/sock.h>
64 #include <linux/skbuff.h>
65 #include <linux/random.h> /* for get_random_bytes */
66 #include <net/sctp/sctp.h>
67 #include <net/sctp/sm.h>
69 SCTP_STATIC
70 struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
71 __u8 type, __u8 flags, int paylen);
72 static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
73 const struct sctp_association *asoc,
74 const struct sctp_chunk *init_chunk,
75 int *cookie_len,
76 const __u8 *raw_addrs, int addrs_len);
77 static int sctp_process_param(struct sctp_association *asoc,
78 union sctp_params param,
79 const union sctp_addr *peer_addr,
80 gfp_t gfp);
81 static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
82 const void *data);
84 /* What was the inbound interface for this chunk? */
85 int sctp_chunk_iif(const struct sctp_chunk *chunk)
87 struct sctp_af *af;
88 int iif = 0;
90 af = sctp_get_af_specific(ipver2af(ip_hdr(chunk->skb)->version));
91 if (af)
92 iif = af->skb_iif(chunk->skb);
94 return iif;
97 /* RFC 2960 3.3.2 Initiation (INIT) (1)
99 * Note 2: The ECN capable field is reserved for future use of
100 * Explicit Congestion Notification.
102 static const struct sctp_paramhdr ecap_param = {
103 SCTP_PARAM_ECN_CAPABLE,
104 cpu_to_be16(sizeof(struct sctp_paramhdr)),
106 static const struct sctp_paramhdr prsctp_param = {
107 SCTP_PARAM_FWD_TSN_SUPPORT,
108 cpu_to_be16(sizeof(struct sctp_paramhdr)),
111 /* A helper to initialize an op error inside a
112 * provided chunk, as most cause codes will be embedded inside an
113 * abort chunk.
115 void sctp_init_cause(struct sctp_chunk *chunk, __be16 cause_code,
116 size_t paylen)
118 sctp_errhdr_t err;
119 __u16 len;
121 /* Cause code constants are now defined in network order. */
122 err.cause = cause_code;
123 len = sizeof(sctp_errhdr_t) + paylen;
124 err.length = htons(len);
125 chunk->subh.err_hdr = sctp_addto_chunk(chunk, sizeof(sctp_errhdr_t), &err);
128 /* A helper to initialize an op error inside a
129 * provided chunk, as most cause codes will be embedded inside an
130 * abort chunk. Differs from sctp_init_cause in that it won't oops
131 * if there isn't enough space in the op error chunk
133 int sctp_init_cause_fixed(struct sctp_chunk *chunk, __be16 cause_code,
134 size_t paylen)
136 sctp_errhdr_t err;
137 __u16 len;
139 /* Cause code constants are now defined in network order. */
140 err.cause = cause_code;
141 len = sizeof(sctp_errhdr_t) + paylen;
142 err.length = htons(len);
144 if (skb_tailroom(chunk->skb) < len)
145 return -ENOSPC;
146 chunk->subh.err_hdr = sctp_addto_chunk_fixed(chunk,
147 sizeof(sctp_errhdr_t),
148 &err);
149 return 0;
151 /* 3.3.2 Initiation (INIT) (1)
153 * This chunk is used to initiate a SCTP association between two
154 * endpoints. The format of the INIT chunk is shown below:
156 * 0 1 2 3
157 * 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
158 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
159 * | Type = 1 | Chunk Flags | Chunk Length |
160 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
161 * | Initiate Tag |
162 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
163 * | Advertised Receiver Window Credit (a_rwnd) |
164 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
165 * | Number of Outbound Streams | Number of Inbound Streams |
166 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
167 * | Initial TSN |
168 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
169 * \ \
170 * / Optional/Variable-Length Parameters /
171 * \ \
172 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
175 * The INIT chunk contains the following parameters. Unless otherwise
176 * noted, each parameter MUST only be included once in the INIT chunk.
178 * Fixed Parameters Status
179 * ----------------------------------------------
180 * Initiate Tag Mandatory
181 * Advertised Receiver Window Credit Mandatory
182 * Number of Outbound Streams Mandatory
183 * Number of Inbound Streams Mandatory
184 * Initial TSN Mandatory
186 * Variable Parameters Status Type Value
187 * -------------------------------------------------------------
188 * IPv4 Address (Note 1) Optional 5
189 * IPv6 Address (Note 1) Optional 6
190 * Cookie Preservative Optional 9
191 * Reserved for ECN Capable (Note 2) Optional 32768 (0x8000)
192 * Host Name Address (Note 3) Optional 11
193 * Supported Address Types (Note 4) Optional 12
195 struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
196 const struct sctp_bind_addr *bp,
197 gfp_t gfp, int vparam_len)
199 sctp_inithdr_t init;
200 union sctp_params addrs;
201 size_t chunksize;
202 struct sctp_chunk *retval = NULL;
203 int num_types, addrs_len = 0;
204 struct sctp_sock *sp;
205 sctp_supported_addrs_param_t sat;
206 __be16 types[2];
207 sctp_adaptation_ind_param_t aiparam;
208 sctp_supported_ext_param_t ext_param;
209 int num_ext = 0;
210 __u8 extensions[3];
211 sctp_paramhdr_t *auth_chunks = NULL,
212 *auth_hmacs = NULL;
214 /* RFC 2960 3.3.2 Initiation (INIT) (1)
216 * Note 1: The INIT chunks can contain multiple addresses that
217 * can be IPv4 and/or IPv6 in any combination.
219 retval = NULL;
221 /* Convert the provided bind address list to raw format. */
222 addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, gfp);
224 init.init_tag = htonl(asoc->c.my_vtag);
225 init.a_rwnd = htonl(asoc->rwnd);
226 init.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
227 init.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
228 init.initial_tsn = htonl(asoc->c.initial_tsn);
230 /* How many address types are needed? */
231 sp = sctp_sk(asoc->base.sk);
232 num_types = sp->pf->supported_addrs(sp, types);
234 chunksize = sizeof(init) + addrs_len;
235 chunksize += WORD_ROUND(SCTP_SAT_LEN(num_types));
236 chunksize += sizeof(ecap_param);
238 if (sctp_prsctp_enable)
239 chunksize += sizeof(prsctp_param);
241 /* ADDIP: Section 4.2.7:
242 * An implementation supporting this extension [ADDIP] MUST list
243 * the ASCONF,the ASCONF-ACK, and the AUTH chunks in its INIT and
244 * INIT-ACK parameters.
246 if (sctp_addip_enable) {
247 extensions[num_ext] = SCTP_CID_ASCONF;
248 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
249 num_ext += 2;
252 if (sp->adaptation_ind)
253 chunksize += sizeof(aiparam);
255 chunksize += vparam_len;
257 /* Account for AUTH related parameters */
258 if (sctp_auth_enable) {
259 /* Add random parameter length*/
260 chunksize += sizeof(asoc->c.auth_random);
262 /* Add HMACS parameter length if any were defined */
263 auth_hmacs = (sctp_paramhdr_t *)asoc->c.auth_hmacs;
264 if (auth_hmacs->length)
265 chunksize += WORD_ROUND(ntohs(auth_hmacs->length));
266 else
267 auth_hmacs = NULL;
269 /* Add CHUNKS parameter length */
270 auth_chunks = (sctp_paramhdr_t *)asoc->c.auth_chunks;
271 if (auth_chunks->length)
272 chunksize += WORD_ROUND(ntohs(auth_chunks->length));
273 else
274 auth_chunks = NULL;
276 extensions[num_ext] = SCTP_CID_AUTH;
277 num_ext += 1;
280 /* If we have any extensions to report, account for that */
281 if (num_ext)
282 chunksize += WORD_ROUND(sizeof(sctp_supported_ext_param_t) +
283 num_ext);
285 /* RFC 2960 3.3.2 Initiation (INIT) (1)
287 * Note 3: An INIT chunk MUST NOT contain more than one Host
288 * Name address parameter. Moreover, the sender of the INIT
289 * MUST NOT combine any other address types with the Host Name
290 * address in the INIT. The receiver of INIT MUST ignore any
291 * other address types if the Host Name address parameter is
292 * present in the received INIT chunk.
294 * PLEASE DO NOT FIXME [This version does not support Host Name.]
297 retval = sctp_make_chunk(asoc, SCTP_CID_INIT, 0, chunksize);
298 if (!retval)
299 goto nodata;
301 retval->subh.init_hdr =
302 sctp_addto_chunk(retval, sizeof(init), &init);
303 retval->param_hdr.v =
304 sctp_addto_chunk(retval, addrs_len, addrs.v);
306 /* RFC 2960 3.3.2 Initiation (INIT) (1)
308 * Note 4: This parameter, when present, specifies all the
309 * address types the sending endpoint can support. The absence
310 * of this parameter indicates that the sending endpoint can
311 * support any address type.
313 sat.param_hdr.type = SCTP_PARAM_SUPPORTED_ADDRESS_TYPES;
314 sat.param_hdr.length = htons(SCTP_SAT_LEN(num_types));
315 sctp_addto_chunk(retval, sizeof(sat), &sat);
316 sctp_addto_chunk(retval, num_types * sizeof(__u16), &types);
318 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
320 /* Add the supported extensions parameter. Be nice and add this
321 * fist before addiding the parameters for the extensions themselves
323 if (num_ext) {
324 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
325 ext_param.param_hdr.length =
326 htons(sizeof(sctp_supported_ext_param_t) + num_ext);
327 sctp_addto_chunk(retval, sizeof(sctp_supported_ext_param_t),
328 &ext_param);
329 sctp_addto_param(retval, num_ext, extensions);
332 if (sctp_prsctp_enable)
333 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
335 if (sp->adaptation_ind) {
336 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
337 aiparam.param_hdr.length = htons(sizeof(aiparam));
338 aiparam.adaptation_ind = htonl(sp->adaptation_ind);
339 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
342 /* Add SCTP-AUTH chunks to the parameter list */
343 if (sctp_auth_enable) {
344 sctp_addto_chunk(retval, sizeof(asoc->c.auth_random),
345 asoc->c.auth_random);
346 if (auth_hmacs)
347 sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
348 auth_hmacs);
349 if (auth_chunks)
350 sctp_addto_chunk(retval, ntohs(auth_chunks->length),
351 auth_chunks);
353 nodata:
354 kfree(addrs.v);
355 return retval;
358 struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
359 const struct sctp_chunk *chunk,
360 gfp_t gfp, int unkparam_len)
362 sctp_inithdr_t initack;
363 struct sctp_chunk *retval;
364 union sctp_params addrs;
365 struct sctp_sock *sp;
366 int addrs_len;
367 sctp_cookie_param_t *cookie;
368 int cookie_len;
369 size_t chunksize;
370 sctp_adaptation_ind_param_t aiparam;
371 sctp_supported_ext_param_t ext_param;
372 int num_ext = 0;
373 __u8 extensions[3];
374 sctp_paramhdr_t *auth_chunks = NULL,
375 *auth_hmacs = NULL,
376 *auth_random = NULL;
378 retval = NULL;
380 /* Note: there may be no addresses to embed. */
381 addrs = sctp_bind_addrs_to_raw(&asoc->base.bind_addr, &addrs_len, gfp);
383 initack.init_tag = htonl(asoc->c.my_vtag);
384 initack.a_rwnd = htonl(asoc->rwnd);
385 initack.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
386 initack.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
387 initack.initial_tsn = htonl(asoc->c.initial_tsn);
389 /* FIXME: We really ought to build the cookie right
390 * into the packet instead of allocating more fresh memory.
392 cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
393 addrs.v, addrs_len);
394 if (!cookie)
395 goto nomem_cookie;
397 /* Calculate the total size of allocation, include the reserved
398 * space for reporting unknown parameters if it is specified.
400 sp = sctp_sk(asoc->base.sk);
401 chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;
403 /* Tell peer that we'll do ECN only if peer advertised such cap. */
404 if (asoc->peer.ecn_capable)
405 chunksize += sizeof(ecap_param);
407 if (asoc->peer.prsctp_capable)
408 chunksize += sizeof(prsctp_param);
410 if (asoc->peer.asconf_capable) {
411 extensions[num_ext] = SCTP_CID_ASCONF;
412 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
413 num_ext += 2;
416 if (sp->adaptation_ind)
417 chunksize += sizeof(aiparam);
419 if (asoc->peer.auth_capable) {
420 auth_random = (sctp_paramhdr_t *)asoc->c.auth_random;
421 chunksize += ntohs(auth_random->length);
423 auth_hmacs = (sctp_paramhdr_t *)asoc->c.auth_hmacs;
424 if (auth_hmacs->length)
425 chunksize += WORD_ROUND(ntohs(auth_hmacs->length));
426 else
427 auth_hmacs = NULL;
429 auth_chunks = (sctp_paramhdr_t *)asoc->c.auth_chunks;
430 if (auth_chunks->length)
431 chunksize += WORD_ROUND(ntohs(auth_chunks->length));
432 else
433 auth_chunks = NULL;
435 extensions[num_ext] = SCTP_CID_AUTH;
436 num_ext += 1;
439 if (num_ext)
440 chunksize += WORD_ROUND(sizeof(sctp_supported_ext_param_t) +
441 num_ext);
443 /* Now allocate and fill out the chunk. */
444 retval = sctp_make_chunk(asoc, SCTP_CID_INIT_ACK, 0, chunksize);
445 if (!retval)
446 goto nomem_chunk;
448 /* Per the advice in RFC 2960 6.4, send this reply to
449 * the source of the INIT packet.
451 retval->transport = chunk->transport;
452 retval->subh.init_hdr =
453 sctp_addto_chunk(retval, sizeof(initack), &initack);
454 retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
455 sctp_addto_chunk(retval, cookie_len, cookie);
456 if (asoc->peer.ecn_capable)
457 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
458 if (num_ext) {
459 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
460 ext_param.param_hdr.length =
461 htons(sizeof(sctp_supported_ext_param_t) + num_ext);
462 sctp_addto_chunk(retval, sizeof(sctp_supported_ext_param_t),
463 &ext_param);
464 sctp_addto_param(retval, num_ext, extensions);
466 if (asoc->peer.prsctp_capable)
467 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
469 if (sp->adaptation_ind) {
470 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
471 aiparam.param_hdr.length = htons(sizeof(aiparam));
472 aiparam.adaptation_ind = htonl(sp->adaptation_ind);
473 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
476 if (asoc->peer.auth_capable) {
477 sctp_addto_chunk(retval, ntohs(auth_random->length),
478 auth_random);
479 if (auth_hmacs)
480 sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
481 auth_hmacs);
482 if (auth_chunks)
483 sctp_addto_chunk(retval, ntohs(auth_chunks->length),
484 auth_chunks);
487 /* We need to remove the const qualifier at this point. */
488 retval->asoc = (struct sctp_association *) asoc;
490 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
492 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
493 * HEARTBEAT ACK, * etc.) to the same destination transport
494 * address from which it received the DATA or control chunk
495 * to which it is replying.
497 * [INIT ACK back to where the INIT came from.]
499 if (chunk)
500 retval->transport = chunk->transport;
502 nomem_chunk:
503 kfree(cookie);
504 nomem_cookie:
505 kfree(addrs.v);
506 return retval;
509 /* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
511 * This chunk is used only during the initialization of an association.
512 * It is sent by the initiator of an association to its peer to complete
513 * the initialization process. This chunk MUST precede any DATA chunk
514 * sent within the association, but MAY be bundled with one or more DATA
515 * chunks in the same packet.
517 * 0 1 2 3
518 * 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
519 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
520 * | Type = 10 |Chunk Flags | Length |
521 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
522 * / Cookie /
523 * \ \
524 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
526 * Chunk Flags: 8 bit
528 * Set to zero on transmit and ignored on receipt.
530 * Length: 16 bits (unsigned integer)
532 * Set to the size of the chunk in bytes, including the 4 bytes of
533 * the chunk header and the size of the Cookie.
535 * Cookie: variable size
537 * This field must contain the exact cookie received in the
538 * State Cookie parameter from the previous INIT ACK.
540 * An implementation SHOULD make the cookie as small as possible
541 * to insure interoperability.
543 struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
544 const struct sctp_chunk *chunk)
546 struct sctp_chunk *retval;
547 void *cookie;
548 int cookie_len;
550 cookie = asoc->peer.cookie;
551 cookie_len = asoc->peer.cookie_len;
553 /* Build a cookie echo chunk. */
554 retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ECHO, 0, cookie_len);
555 if (!retval)
556 goto nodata;
557 retval->subh.cookie_hdr =
558 sctp_addto_chunk(retval, cookie_len, cookie);
560 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
562 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
563 * HEARTBEAT ACK, * etc.) to the same destination transport
564 * address from which it * received the DATA or control chunk
565 * to which it is replying.
567 * [COOKIE ECHO back to where the INIT ACK came from.]
569 if (chunk)
570 retval->transport = chunk->transport;
572 nodata:
573 return retval;
576 /* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
578 * This chunk is used only during the initialization of an
579 * association. It is used to acknowledge the receipt of a COOKIE
580 * ECHO chunk. This chunk MUST precede any DATA or SACK chunk sent
581 * within the association, but MAY be bundled with one or more DATA
582 * chunks or SACK chunk in the same SCTP packet.
584 * 0 1 2 3
585 * 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
586 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
587 * | Type = 11 |Chunk Flags | Length = 4 |
588 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
590 * Chunk Flags: 8 bits
592 * Set to zero on transmit and ignored on receipt.
594 struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
595 const struct sctp_chunk *chunk)
597 struct sctp_chunk *retval;
599 retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ACK, 0, 0);
601 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
603 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
604 * HEARTBEAT ACK, * etc.) to the same destination transport
605 * address from which it * received the DATA or control chunk
606 * to which it is replying.
608 * [COOKIE ACK back to where the COOKIE ECHO came from.]
610 if (retval && chunk)
611 retval->transport = chunk->transport;
613 return retval;
617 * Appendix A: Explicit Congestion Notification:
618 * CWR:
620 * RFC 2481 details a specific bit for a sender to send in the header of
621 * its next outbound TCP segment to indicate to its peer that it has
622 * reduced its congestion window. This is termed the CWR bit. For
623 * SCTP the same indication is made by including the CWR chunk.
624 * This chunk contains one data element, i.e. the TSN number that
625 * was sent in the ECNE chunk. This element represents the lowest
626 * TSN number in the datagram that was originally marked with the
627 * CE bit.
629 * 0 1 2 3
630 * 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
631 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
632 * | Chunk Type=13 | Flags=00000000| Chunk Length = 8 |
633 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
634 * | Lowest TSN Number |
635 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
637 * Note: The CWR is considered a Control chunk.
639 struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
640 const __u32 lowest_tsn,
641 const struct sctp_chunk *chunk)
643 struct sctp_chunk *retval;
644 sctp_cwrhdr_t cwr;
646 cwr.lowest_tsn = htonl(lowest_tsn);
647 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_CWR, 0,
648 sizeof(sctp_cwrhdr_t));
650 if (!retval)
651 goto nodata;
653 retval->subh.ecn_cwr_hdr =
654 sctp_addto_chunk(retval, sizeof(cwr), &cwr);
656 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
658 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
659 * HEARTBEAT ACK, * etc.) to the same destination transport
660 * address from which it * received the DATA or control chunk
661 * to which it is replying.
663 * [Report a reduced congestion window back to where the ECNE
664 * came from.]
666 if (chunk)
667 retval->transport = chunk->transport;
669 nodata:
670 return retval;
673 /* Make an ECNE chunk. This is a congestion experienced report. */
674 struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
675 const __u32 lowest_tsn)
677 struct sctp_chunk *retval;
678 sctp_ecnehdr_t ecne;
680 ecne.lowest_tsn = htonl(lowest_tsn);
681 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_ECNE, 0,
682 sizeof(sctp_ecnehdr_t));
683 if (!retval)
684 goto nodata;
685 retval->subh.ecne_hdr =
686 sctp_addto_chunk(retval, sizeof(ecne), &ecne);
688 nodata:
689 return retval;
692 /* Make a DATA chunk for the given association from the provided
693 * parameters. However, do not populate the data payload.
695 struct sctp_chunk *sctp_make_datafrag_empty(struct sctp_association *asoc,
696 const struct sctp_sndrcvinfo *sinfo,
697 int data_len, __u8 flags, __u16 ssn)
699 struct sctp_chunk *retval;
700 struct sctp_datahdr dp;
701 int chunk_len;
703 /* We assign the TSN as LATE as possible, not here when
704 * creating the chunk.
706 dp.tsn = 0;
707 dp.stream = htons(sinfo->sinfo_stream);
708 dp.ppid = sinfo->sinfo_ppid;
710 /* Set the flags for an unordered send. */
711 if (sinfo->sinfo_flags & SCTP_UNORDERED) {
712 flags |= SCTP_DATA_UNORDERED;
713 dp.ssn = 0;
714 } else
715 dp.ssn = htons(ssn);
717 chunk_len = sizeof(dp) + data_len;
718 retval = sctp_make_chunk(asoc, SCTP_CID_DATA, flags, chunk_len);
719 if (!retval)
720 goto nodata;
722 retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
723 memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
725 nodata:
726 return retval;
729 /* Create a selective ackowledgement (SACK) for the given
730 * association. This reports on which TSN's we've seen to date,
731 * including duplicates and gaps.
733 struct sctp_chunk *sctp_make_sack(const struct sctp_association *asoc)
735 struct sctp_chunk *retval;
736 struct sctp_sackhdr sack;
737 int len;
738 __u32 ctsn;
739 __u16 num_gabs, num_dup_tsns;
740 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
741 struct sctp_gap_ack_block gabs[SCTP_MAX_GABS];
743 memset(gabs, 0, sizeof(gabs));
744 ctsn = sctp_tsnmap_get_ctsn(map);
745 SCTP_DEBUG_PRINTK("sackCTSNAck sent: 0x%x.\n", ctsn);
747 /* How much room is needed in the chunk? */
748 num_gabs = sctp_tsnmap_num_gabs(map, gabs);
749 num_dup_tsns = sctp_tsnmap_num_dups(map);
751 /* Initialize the SACK header. */
752 sack.cum_tsn_ack = htonl(ctsn);
753 sack.a_rwnd = htonl(asoc->a_rwnd);
754 sack.num_gap_ack_blocks = htons(num_gabs);
755 sack.num_dup_tsns = htons(num_dup_tsns);
757 len = sizeof(sack)
758 + sizeof(struct sctp_gap_ack_block) * num_gabs
759 + sizeof(__u32) * num_dup_tsns;
761 /* Create the chunk. */
762 retval = sctp_make_chunk(asoc, SCTP_CID_SACK, 0, len);
763 if (!retval)
764 goto nodata;
766 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
768 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
769 * HEARTBEAT ACK, etc.) to the same destination transport
770 * address from which it received the DATA or control chunk to
771 * which it is replying. This rule should also be followed if
772 * the endpoint is bundling DATA chunks together with the
773 * reply chunk.
775 * However, when acknowledging multiple DATA chunks received
776 * in packets from different source addresses in a single
777 * SACK, the SACK chunk may be transmitted to one of the
778 * destination transport addresses from which the DATA or
779 * control chunks being acknowledged were received.
781 * [BUG: We do not implement the following paragraph.
782 * Perhaps we should remember the last transport we used for a
783 * SACK and avoid that (if possible) if we have seen any
784 * duplicates. --piggy]
786 * When a receiver of a duplicate DATA chunk sends a SACK to a
787 * multi- homed endpoint it MAY be beneficial to vary the
788 * destination address and not use the source address of the
789 * DATA chunk. The reason being that receiving a duplicate
790 * from a multi-homed endpoint might indicate that the return
791 * path (as specified in the source address of the DATA chunk)
792 * for the SACK is broken.
794 * [Send to the address from which we last received a DATA chunk.]
796 retval->transport = asoc->peer.last_data_from;
798 retval->subh.sack_hdr =
799 sctp_addto_chunk(retval, sizeof(sack), &sack);
801 /* Add the gap ack block information. */
802 if (num_gabs)
803 sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
804 gabs);
806 /* Add the duplicate TSN information. */
807 if (num_dup_tsns)
808 sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
809 sctp_tsnmap_get_dups(map));
811 nodata:
812 return retval;
815 /* Make a SHUTDOWN chunk. */
816 struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
817 const struct sctp_chunk *chunk)
819 struct sctp_chunk *retval;
820 sctp_shutdownhdr_t shut;
821 __u32 ctsn;
823 ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
824 shut.cum_tsn_ack = htonl(ctsn);
826 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN, 0,
827 sizeof(sctp_shutdownhdr_t));
828 if (!retval)
829 goto nodata;
831 retval->subh.shutdown_hdr =
832 sctp_addto_chunk(retval, sizeof(shut), &shut);
834 if (chunk)
835 retval->transport = chunk->transport;
836 nodata:
837 return retval;
840 struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
841 const struct sctp_chunk *chunk)
843 struct sctp_chunk *retval;
845 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0);
847 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
849 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
850 * HEARTBEAT ACK, * etc.) to the same destination transport
851 * address from which it * received the DATA or control chunk
852 * to which it is replying.
854 * [ACK back to where the SHUTDOWN came from.]
856 if (retval && chunk)
857 retval->transport = chunk->transport;
859 return retval;
862 struct sctp_chunk *sctp_make_shutdown_complete(
863 const struct sctp_association *asoc,
864 const struct sctp_chunk *chunk)
866 struct sctp_chunk *retval;
867 __u8 flags = 0;
869 /* Set the T-bit if we have no association (vtag will be
870 * reflected)
872 flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
874 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags, 0);
876 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
878 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
879 * HEARTBEAT ACK, * etc.) to the same destination transport
880 * address from which it * received the DATA or control chunk
881 * to which it is replying.
883 * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
884 * came from.]
886 if (retval && chunk)
887 retval->transport = chunk->transport;
889 return retval;
892 /* Create an ABORT. Note that we set the T bit if we have no
893 * association, except when responding to an INIT (sctpimpguide 2.41).
895 struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
896 const struct sctp_chunk *chunk,
897 const size_t hint)
899 struct sctp_chunk *retval;
900 __u8 flags = 0;
902 /* Set the T-bit if we have no association and 'chunk' is not
903 * an INIT (vtag will be reflected).
905 if (!asoc) {
906 if (chunk && chunk->chunk_hdr &&
907 chunk->chunk_hdr->type == SCTP_CID_INIT)
908 flags = 0;
909 else
910 flags = SCTP_CHUNK_FLAG_T;
913 retval = sctp_make_chunk(asoc, SCTP_CID_ABORT, flags, hint);
915 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
917 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
918 * HEARTBEAT ACK, * etc.) to the same destination transport
919 * address from which it * received the DATA or control chunk
920 * to which it is replying.
922 * [ABORT back to where the offender came from.]
924 if (retval && chunk)
925 retval->transport = chunk->transport;
927 return retval;
930 /* Helper to create ABORT with a NO_USER_DATA error. */
931 struct sctp_chunk *sctp_make_abort_no_data(
932 const struct sctp_association *asoc,
933 const struct sctp_chunk *chunk, __u32 tsn)
935 struct sctp_chunk *retval;
936 __be32 payload;
938 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t)
939 + sizeof(tsn));
941 if (!retval)
942 goto no_mem;
944 /* Put the tsn back into network byte order. */
945 payload = htonl(tsn);
946 sctp_init_cause(retval, SCTP_ERROR_NO_DATA, sizeof(payload));
947 sctp_addto_chunk(retval, sizeof(payload), (const void *)&payload);
949 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
951 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
952 * HEARTBEAT ACK, * etc.) to the same destination transport
953 * address from which it * received the DATA or control chunk
954 * to which it is replying.
956 * [ABORT back to where the offender came from.]
958 if (chunk)
959 retval->transport = chunk->transport;
961 no_mem:
962 return retval;
965 /* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error. */
966 struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
967 const struct msghdr *msg,
968 size_t paylen)
970 struct sctp_chunk *retval;
971 void *payload = NULL;
972 int err;
974 retval = sctp_make_abort(asoc, NULL, sizeof(sctp_errhdr_t) + paylen);
975 if (!retval)
976 goto err_chunk;
978 if (paylen) {
979 /* Put the msg_iov together into payload. */
980 payload = kmalloc(paylen, GFP_KERNEL);
981 if (!payload)
982 goto err_payload;
984 err = memcpy_fromiovec(payload, msg->msg_iov, paylen);
985 if (err < 0)
986 goto err_copy;
989 sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, paylen);
990 sctp_addto_chunk(retval, paylen, payload);
992 if (paylen)
993 kfree(payload);
995 return retval;
997 err_copy:
998 kfree(payload);
999 err_payload:
1000 sctp_chunk_free(retval);
1001 retval = NULL;
1002 err_chunk:
1003 return retval;
1006 /* Append bytes to the end of a parameter. Will panic if chunk is not big
1007 * enough.
1009 static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
1010 const void *data)
1012 void *target;
1013 int chunklen = ntohs(chunk->chunk_hdr->length);
1015 target = skb_put(chunk->skb, len);
1017 if (data)
1018 memcpy(target, data, len);
1019 else
1020 memset(target, 0, len);
1022 /* Adjust the chunk length field. */
1023 chunk->chunk_hdr->length = htons(chunklen + len);
1024 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1026 return target;
1029 /* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
1030 struct sctp_chunk *sctp_make_abort_violation(
1031 const struct sctp_association *asoc,
1032 const struct sctp_chunk *chunk,
1033 const __u8 *payload,
1034 const size_t paylen)
1036 struct sctp_chunk *retval;
1037 struct sctp_paramhdr phdr;
1039 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t) + paylen
1040 + sizeof(sctp_paramhdr_t));
1041 if (!retval)
1042 goto end;
1044 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, paylen
1045 + sizeof(sctp_paramhdr_t));
1047 phdr.type = htons(chunk->chunk_hdr->type);
1048 phdr.length = chunk->chunk_hdr->length;
1049 sctp_addto_chunk(retval, paylen, payload);
1050 sctp_addto_param(retval, sizeof(sctp_paramhdr_t), &phdr);
1052 end:
1053 return retval;
1056 struct sctp_chunk *sctp_make_violation_paramlen(
1057 const struct sctp_association *asoc,
1058 const struct sctp_chunk *chunk,
1059 struct sctp_paramhdr *param)
1061 struct sctp_chunk *retval;
1062 static const char error[] = "The following parameter had invalid length:";
1063 size_t payload_len = sizeof(error) + sizeof(sctp_errhdr_t) +
1064 sizeof(sctp_paramhdr_t);
1066 retval = sctp_make_abort(asoc, chunk, payload_len);
1067 if (!retval)
1068 goto nodata;
1070 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION,
1071 sizeof(error) + sizeof(sctp_paramhdr_t));
1072 sctp_addto_chunk(retval, sizeof(error), error);
1073 sctp_addto_param(retval, sizeof(sctp_paramhdr_t), param);
1075 nodata:
1076 return retval;
1079 /* Make a HEARTBEAT chunk. */
1080 struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
1081 const struct sctp_transport *transport,
1082 const void *payload, const size_t paylen)
1084 struct sctp_chunk *retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT,
1085 0, paylen);
1087 if (!retval)
1088 goto nodata;
1090 /* Cast away the 'const', as this is just telling the chunk
1091 * what transport it belongs to.
1093 retval->transport = (struct sctp_transport *) transport;
1094 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
1096 nodata:
1097 return retval;
1100 struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
1101 const struct sctp_chunk *chunk,
1102 const void *payload, const size_t paylen)
1104 struct sctp_chunk *retval;
1106 retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen);
1107 if (!retval)
1108 goto nodata;
1110 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
1112 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1114 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1115 * HEARTBEAT ACK, * etc.) to the same destination transport
1116 * address from which it * received the DATA or control chunk
1117 * to which it is replying.
1119 * [HBACK back to where the HEARTBEAT came from.]
1121 if (chunk)
1122 retval->transport = chunk->transport;
1124 nodata:
1125 return retval;
1128 /* Create an Operation Error chunk with the specified space reserved.
1129 * This routine can be used for containing multiple causes in the chunk.
1131 static struct sctp_chunk *sctp_make_op_error_space(
1132 const struct sctp_association *asoc,
1133 const struct sctp_chunk *chunk,
1134 size_t size)
1136 struct sctp_chunk *retval;
1138 retval = sctp_make_chunk(asoc, SCTP_CID_ERROR, 0,
1139 sizeof(sctp_errhdr_t) + size);
1140 if (!retval)
1141 goto nodata;
1143 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1145 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1146 * HEARTBEAT ACK, etc.) to the same destination transport
1147 * address from which it received the DATA or control chunk
1148 * to which it is replying.
1151 if (chunk)
1152 retval->transport = chunk->transport;
1154 nodata:
1155 return retval;
1158 /* Create an Operation Error chunk of a fixed size,
1159 * specifically, max(asoc->pathmtu, SCTP_DEFAULT_MAXSEGMENT)
1160 * This is a helper function to allocate an error chunk for
1161 * for those invalid parameter codes in which we may not want
1162 * to report all the errors, if the incomming chunk is large
1164 static inline struct sctp_chunk *sctp_make_op_error_fixed(
1165 const struct sctp_association *asoc,
1166 const struct sctp_chunk *chunk)
1168 size_t size = asoc ? asoc->pathmtu : 0;
1170 if (!size)
1171 size = SCTP_DEFAULT_MAXSEGMENT;
1173 return sctp_make_op_error_space(asoc, chunk, size);
1176 /* Create an Operation Error chunk. */
1177 struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
1178 const struct sctp_chunk *chunk,
1179 __be16 cause_code, const void *payload,
1180 size_t paylen, size_t reserve_tail)
1182 struct sctp_chunk *retval;
1184 retval = sctp_make_op_error_space(asoc, chunk, paylen + reserve_tail);
1185 if (!retval)
1186 goto nodata;
1188 sctp_init_cause(retval, cause_code, paylen + reserve_tail);
1189 sctp_addto_chunk(retval, paylen, payload);
1190 if (reserve_tail)
1191 sctp_addto_param(retval, reserve_tail, NULL);
1193 nodata:
1194 return retval;
1197 struct sctp_chunk *sctp_make_auth(const struct sctp_association *asoc)
1199 struct sctp_chunk *retval;
1200 struct sctp_hmac *hmac_desc;
1201 struct sctp_authhdr auth_hdr;
1202 __u8 *hmac;
1204 /* Get the first hmac that the peer told us to use */
1205 hmac_desc = sctp_auth_asoc_get_hmac(asoc);
1206 if (unlikely(!hmac_desc))
1207 return NULL;
1209 retval = sctp_make_chunk(asoc, SCTP_CID_AUTH, 0,
1210 hmac_desc->hmac_len + sizeof(sctp_authhdr_t));
1211 if (!retval)
1212 return NULL;
1214 auth_hdr.hmac_id = htons(hmac_desc->hmac_id);
1215 auth_hdr.shkey_id = htons(asoc->active_key_id);
1217 retval->subh.auth_hdr = sctp_addto_chunk(retval, sizeof(sctp_authhdr_t),
1218 &auth_hdr);
1220 hmac = skb_put(retval->skb, hmac_desc->hmac_len);
1221 memset(hmac, 0, hmac_desc->hmac_len);
1223 /* Adjust the chunk header to include the empty MAC */
1224 retval->chunk_hdr->length =
1225 htons(ntohs(retval->chunk_hdr->length) + hmac_desc->hmac_len);
1226 retval->chunk_end = skb_tail_pointer(retval->skb);
1228 return retval;
1232 /********************************************************************
1233 * 2nd Level Abstractions
1234 ********************************************************************/
1236 /* Turn an skb into a chunk.
1237 * FIXME: Eventually move the structure directly inside the skb->cb[].
1239 struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
1240 const struct sctp_association *asoc,
1241 struct sock *sk)
1243 struct sctp_chunk *retval;
1245 retval = kmem_cache_zalloc(sctp_chunk_cachep, GFP_ATOMIC);
1247 if (!retval)
1248 goto nodata;
1250 if (!sk) {
1251 SCTP_DEBUG_PRINTK("chunkifying skb %p w/o an sk\n", skb);
1254 INIT_LIST_HEAD(&retval->list);
1255 retval->skb = skb;
1256 retval->asoc = (struct sctp_association *)asoc;
1257 retval->resent = 0;
1258 retval->has_tsn = 0;
1259 retval->has_ssn = 0;
1260 retval->rtt_in_progress = 0;
1261 retval->sent_at = 0;
1262 retval->singleton = 1;
1263 retval->end_of_packet = 0;
1264 retval->ecn_ce_done = 0;
1265 retval->pdiscard = 0;
1267 /* sctpimpguide-05.txt Section 2.8.2
1268 * M1) Each time a new DATA chunk is transmitted
1269 * set the 'TSN.Missing.Report' count for that TSN to 0. The
1270 * 'TSN.Missing.Report' count will be used to determine missing chunks
1271 * and when to fast retransmit.
1273 retval->tsn_missing_report = 0;
1274 retval->tsn_gap_acked = 0;
1275 retval->fast_retransmit = SCTP_CAN_FRTX;
1277 /* If this is a fragmented message, track all fragments
1278 * of the message (for SEND_FAILED).
1280 retval->msg = NULL;
1282 /* Polish the bead hole. */
1283 INIT_LIST_HEAD(&retval->transmitted_list);
1284 INIT_LIST_HEAD(&retval->frag_list);
1285 SCTP_DBG_OBJCNT_INC(chunk);
1286 atomic_set(&retval->refcnt, 1);
1288 nodata:
1289 return retval;
1292 /* Set chunk->source and dest based on the IP header in chunk->skb. */
1293 void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
1294 union sctp_addr *dest)
1296 memcpy(&chunk->source, src, sizeof(union sctp_addr));
1297 memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
1300 /* Extract the source address from a chunk. */
1301 const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
1303 /* If we have a known transport, use that. */
1304 if (chunk->transport) {
1305 return &chunk->transport->ipaddr;
1306 } else {
1307 /* Otherwise, extract it from the IP header. */
1308 return &chunk->source;
1312 /* Create a new chunk, setting the type and flags headers from the
1313 * arguments, reserving enough space for a 'paylen' byte payload.
1315 SCTP_STATIC
1316 struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
1317 __u8 type, __u8 flags, int paylen)
1319 struct sctp_chunk *retval;
1320 sctp_chunkhdr_t *chunk_hdr;
1321 struct sk_buff *skb;
1322 struct sock *sk;
1324 /* No need to allocate LL here, as this is only a chunk. */
1325 skb = alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t) + paylen),
1326 GFP_ATOMIC);
1327 if (!skb)
1328 goto nodata;
1330 /* Make room for the chunk header. */
1331 chunk_hdr = (sctp_chunkhdr_t *)skb_put(skb, sizeof(sctp_chunkhdr_t));
1332 chunk_hdr->type = type;
1333 chunk_hdr->flags = flags;
1334 chunk_hdr->length = htons(sizeof(sctp_chunkhdr_t));
1336 sk = asoc ? asoc->base.sk : NULL;
1337 retval = sctp_chunkify(skb, asoc, sk);
1338 if (!retval) {
1339 kfree_skb(skb);
1340 goto nodata;
1343 retval->chunk_hdr = chunk_hdr;
1344 retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(struct sctp_chunkhdr);
1346 /* Determine if the chunk needs to be authenticated */
1347 if (sctp_auth_send_cid(type, asoc))
1348 retval->auth = 1;
1350 /* Set the skb to the belonging sock for accounting. */
1351 skb->sk = sk;
1353 return retval;
1354 nodata:
1355 return NULL;
1359 /* Release the memory occupied by a chunk. */
1360 static void sctp_chunk_destroy(struct sctp_chunk *chunk)
1362 BUG_ON(!list_empty(&chunk->list));
1363 list_del_init(&chunk->transmitted_list);
1365 /* Free the chunk skb data and the SCTP_chunk stub itself. */
1366 dev_kfree_skb(chunk->skb);
1368 SCTP_DBG_OBJCNT_DEC(chunk);
1369 kmem_cache_free(sctp_chunk_cachep, chunk);
1372 /* Possibly, free the chunk. */
1373 void sctp_chunk_free(struct sctp_chunk *chunk)
1375 /* Release our reference on the message tracker. */
1376 if (chunk->msg)
1377 sctp_datamsg_put(chunk->msg);
1379 sctp_chunk_put(chunk);
1382 /* Grab a reference to the chunk. */
1383 void sctp_chunk_hold(struct sctp_chunk *ch)
1385 atomic_inc(&ch->refcnt);
1388 /* Release a reference to the chunk. */
1389 void sctp_chunk_put(struct sctp_chunk *ch)
1391 if (atomic_dec_and_test(&ch->refcnt))
1392 sctp_chunk_destroy(ch);
1395 /* Append bytes to the end of a chunk. Will panic if chunk is not big
1396 * enough.
1398 void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
1400 void *target;
1401 void *padding;
1402 int chunklen = ntohs(chunk->chunk_hdr->length);
1403 int padlen = WORD_ROUND(chunklen) - chunklen;
1405 padding = skb_put(chunk->skb, padlen);
1406 target = skb_put(chunk->skb, len);
1408 memset(padding, 0, padlen);
1409 memcpy(target, data, len);
1411 /* Adjust the chunk length field. */
1412 chunk->chunk_hdr->length = htons(chunklen + padlen + len);
1413 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1415 return target;
1418 /* Append bytes to the end of a chunk. Returns NULL if there isn't sufficient
1419 * space in the chunk
1421 void *sctp_addto_chunk_fixed(struct sctp_chunk *chunk,
1422 int len, const void *data)
1424 if (skb_tailroom(chunk->skb) >= len)
1425 return sctp_addto_chunk(chunk, len, data);
1426 else
1427 return NULL;
1430 /* Append bytes from user space to the end of a chunk. Will panic if
1431 * chunk is not big enough.
1432 * Returns a kernel err value.
1434 int sctp_user_addto_chunk(struct sctp_chunk *chunk, int off, int len,
1435 struct iovec *data)
1437 __u8 *target;
1438 int err = 0;
1440 /* Make room in chunk for data. */
1441 target = skb_put(chunk->skb, len);
1443 /* Copy data (whole iovec) into chunk */
1444 if ((err = memcpy_fromiovecend(target, data, off, len)))
1445 goto out;
1447 /* Adjust the chunk length field. */
1448 chunk->chunk_hdr->length =
1449 htons(ntohs(chunk->chunk_hdr->length) + len);
1450 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1452 out:
1453 return err;
1456 /* Helper function to assign a TSN if needed. This assumes that both
1457 * the data_hdr and association have already been assigned.
1459 void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
1461 struct sctp_datamsg *msg;
1462 struct sctp_chunk *lchunk;
1463 struct sctp_stream *stream;
1464 __u16 ssn;
1465 __u16 sid;
1467 if (chunk->has_ssn)
1468 return;
1470 /* All fragments will be on the same stream */
1471 sid = ntohs(chunk->subh.data_hdr->stream);
1472 stream = &chunk->asoc->ssnmap->out;
1474 /* Now assign the sequence number to the entire message.
1475 * All fragments must have the same stream sequence number.
1477 msg = chunk->msg;
1478 list_for_each_entry(lchunk, &msg->chunks, frag_list) {
1479 if (lchunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
1480 ssn = 0;
1481 } else {
1482 if (lchunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
1483 ssn = sctp_ssn_next(stream, sid);
1484 else
1485 ssn = sctp_ssn_peek(stream, sid);
1488 lchunk->subh.data_hdr->ssn = htons(ssn);
1489 lchunk->has_ssn = 1;
1493 /* Helper function to assign a TSN if needed. This assumes that both
1494 * the data_hdr and association have already been assigned.
1496 void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
1498 if (!chunk->has_tsn) {
1499 /* This is the last possible instant to
1500 * assign a TSN.
1502 chunk->subh.data_hdr->tsn =
1503 htonl(sctp_association_get_next_tsn(chunk->asoc));
1504 chunk->has_tsn = 1;
1508 /* Create a CLOSED association to use with an incoming packet. */
1509 struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
1510 struct sctp_chunk *chunk,
1511 gfp_t gfp)
1513 struct sctp_association *asoc;
1514 struct sk_buff *skb;
1515 sctp_scope_t scope;
1516 struct sctp_af *af;
1518 /* Create the bare association. */
1519 scope = sctp_scope(sctp_source(chunk));
1520 asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
1521 if (!asoc)
1522 goto nodata;
1523 asoc->temp = 1;
1524 skb = chunk->skb;
1525 /* Create an entry for the source address of the packet. */
1526 af = sctp_get_af_specific(ipver2af(ip_hdr(skb)->version));
1527 if (unlikely(!af))
1528 goto fail;
1529 af->from_skb(&asoc->c.peer_addr, skb, 1);
1530 nodata:
1531 return asoc;
1533 fail:
1534 sctp_association_free(asoc);
1535 return NULL;
1538 /* Build a cookie representing asoc.
1539 * This INCLUDES the param header needed to put the cookie in the INIT ACK.
1541 static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
1542 const struct sctp_association *asoc,
1543 const struct sctp_chunk *init_chunk,
1544 int *cookie_len,
1545 const __u8 *raw_addrs, int addrs_len)
1547 sctp_cookie_param_t *retval;
1548 struct sctp_signed_cookie *cookie;
1549 struct scatterlist sg;
1550 int headersize, bodysize;
1551 unsigned int keylen;
1552 char *key;
1554 /* Header size is static data prior to the actual cookie, including
1555 * any padding.
1557 headersize = sizeof(sctp_paramhdr_t) +
1558 (sizeof(struct sctp_signed_cookie) -
1559 sizeof(struct sctp_cookie));
1560 bodysize = sizeof(struct sctp_cookie)
1561 + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
1563 /* Pad out the cookie to a multiple to make the signature
1564 * functions simpler to write.
1566 if (bodysize % SCTP_COOKIE_MULTIPLE)
1567 bodysize += SCTP_COOKIE_MULTIPLE
1568 - (bodysize % SCTP_COOKIE_MULTIPLE);
1569 *cookie_len = headersize + bodysize;
1571 /* Clear this memory since we are sending this data structure
1572 * out on the network.
1574 retval = kzalloc(*cookie_len, GFP_ATOMIC);
1575 if (!retval)
1576 goto nodata;
1578 cookie = (struct sctp_signed_cookie *) retval->body;
1580 /* Set up the parameter header. */
1581 retval->p.type = SCTP_PARAM_STATE_COOKIE;
1582 retval->p.length = htons(*cookie_len);
1584 /* Copy the cookie part of the association itself. */
1585 cookie->c = asoc->c;
1586 /* Save the raw address list length in the cookie. */
1587 cookie->c.raw_addr_list_len = addrs_len;
1589 /* Remember PR-SCTP capability. */
1590 cookie->c.prsctp_capable = asoc->peer.prsctp_capable;
1592 /* Save adaptation indication in the cookie. */
1593 cookie->c.adaptation_ind = asoc->peer.adaptation_ind;
1595 /* Set an expiration time for the cookie. */
1596 do_gettimeofday(&cookie->c.expiration);
1597 TIMEVAL_ADD(asoc->cookie_life, cookie->c.expiration);
1599 /* Copy the peer's init packet. */
1600 memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
1601 ntohs(init_chunk->chunk_hdr->length));
1603 /* Copy the raw local address list of the association. */
1604 memcpy((__u8 *)&cookie->c.peer_init[0] +
1605 ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
1607 if (sctp_sk(ep->base.sk)->hmac) {
1608 struct hash_desc desc;
1610 /* Sign the message. */
1611 sg_init_one(&sg, &cookie->c, bodysize);
1612 keylen = SCTP_SECRET_SIZE;
1613 key = (char *)ep->secret_key[ep->current_key];
1614 desc.tfm = sctp_sk(ep->base.sk)->hmac;
1615 desc.flags = 0;
1617 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1618 crypto_hash_digest(&desc, &sg, bodysize, cookie->signature))
1619 goto free_cookie;
1622 return retval;
1624 free_cookie:
1625 kfree(retval);
1626 nodata:
1627 *cookie_len = 0;
1628 return NULL;
1631 /* Unpack the cookie from COOKIE ECHO chunk, recreating the association. */
1632 struct sctp_association *sctp_unpack_cookie(
1633 const struct sctp_endpoint *ep,
1634 const struct sctp_association *asoc,
1635 struct sctp_chunk *chunk, gfp_t gfp,
1636 int *error, struct sctp_chunk **errp)
1638 struct sctp_association *retval = NULL;
1639 struct sctp_signed_cookie *cookie;
1640 struct sctp_cookie *bear_cookie;
1641 int headersize, bodysize, fixed_size;
1642 __u8 *digest = ep->digest;
1643 struct scatterlist sg;
1644 unsigned int keylen, len;
1645 char *key;
1646 sctp_scope_t scope;
1647 struct sk_buff *skb = chunk->skb;
1648 struct timeval tv;
1649 struct hash_desc desc;
1651 /* Header size is static data prior to the actual cookie, including
1652 * any padding.
1654 headersize = sizeof(sctp_chunkhdr_t) +
1655 (sizeof(struct sctp_signed_cookie) -
1656 sizeof(struct sctp_cookie));
1657 bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
1658 fixed_size = headersize + sizeof(struct sctp_cookie);
1660 /* Verify that the chunk looks like it even has a cookie.
1661 * There must be enough room for our cookie and our peer's
1662 * INIT chunk.
1664 len = ntohs(chunk->chunk_hdr->length);
1665 if (len < fixed_size + sizeof(struct sctp_chunkhdr))
1666 goto malformed;
1668 /* Verify that the cookie has been padded out. */
1669 if (bodysize % SCTP_COOKIE_MULTIPLE)
1670 goto malformed;
1672 /* Process the cookie. */
1673 cookie = chunk->subh.cookie_hdr;
1674 bear_cookie = &cookie->c;
1676 if (!sctp_sk(ep->base.sk)->hmac)
1677 goto no_hmac;
1679 /* Check the signature. */
1680 keylen = SCTP_SECRET_SIZE;
1681 sg_init_one(&sg, bear_cookie, bodysize);
1682 key = (char *)ep->secret_key[ep->current_key];
1683 desc.tfm = sctp_sk(ep->base.sk)->hmac;
1684 desc.flags = 0;
1686 memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
1687 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1688 crypto_hash_digest(&desc, &sg, bodysize, digest)) {
1689 *error = -SCTP_IERROR_NOMEM;
1690 goto fail;
1693 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1694 /* Try the previous key. */
1695 key = (char *)ep->secret_key[ep->last_key];
1696 memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
1697 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1698 crypto_hash_digest(&desc, &sg, bodysize, digest)) {
1699 *error = -SCTP_IERROR_NOMEM;
1700 goto fail;
1703 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1704 /* Yikes! Still bad signature! */
1705 *error = -SCTP_IERROR_BAD_SIG;
1706 goto fail;
1710 no_hmac:
1711 /* IG Section 2.35.2:
1712 * 3) Compare the port numbers and the verification tag contained
1713 * within the COOKIE ECHO chunk to the actual port numbers and the
1714 * verification tag within the SCTP common header of the received
1715 * packet. If these values do not match the packet MUST be silently
1716 * discarded,
1718 if (ntohl(chunk->sctp_hdr->vtag) != bear_cookie->my_vtag) {
1719 *error = -SCTP_IERROR_BAD_TAG;
1720 goto fail;
1723 if (chunk->sctp_hdr->source != bear_cookie->peer_addr.v4.sin_port ||
1724 ntohs(chunk->sctp_hdr->dest) != bear_cookie->my_port) {
1725 *error = -SCTP_IERROR_BAD_PORTS;
1726 goto fail;
1729 /* Check to see if the cookie is stale. If there is already
1730 * an association, there is no need to check cookie's expiration
1731 * for init collision case of lost COOKIE ACK.
1732 * If skb has been timestamped, then use the stamp, otherwise
1733 * use current time. This introduces a small possibility that
1734 * that a cookie may be considered expired, but his would only slow
1735 * down the new association establishment instead of every packet.
1737 if (sock_flag(ep->base.sk, SOCK_TIMESTAMP))
1738 skb_get_timestamp(skb, &tv);
1739 else
1740 do_gettimeofday(&tv);
1742 if (!asoc && tv_lt(bear_cookie->expiration, tv)) {
1744 * Section 3.3.10.3 Stale Cookie Error (3)
1746 * Cause of error
1747 * ---------------
1748 * Stale Cookie Error: Indicates the receipt of a valid State
1749 * Cookie that has expired.
1751 len = ntohs(chunk->chunk_hdr->length);
1752 *errp = sctp_make_op_error_space(asoc, chunk, len);
1753 if (*errp) {
1754 suseconds_t usecs = (tv.tv_sec -
1755 bear_cookie->expiration.tv_sec) * 1000000L +
1756 tv.tv_usec - bear_cookie->expiration.tv_usec;
1757 __be32 n = htonl(usecs);
1759 sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
1760 sizeof(n));
1761 sctp_addto_chunk(*errp, sizeof(n), &n);
1762 *error = -SCTP_IERROR_STALE_COOKIE;
1763 } else
1764 *error = -SCTP_IERROR_NOMEM;
1766 goto fail;
1769 /* Make a new base association. */
1770 scope = sctp_scope(sctp_source(chunk));
1771 retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
1772 if (!retval) {
1773 *error = -SCTP_IERROR_NOMEM;
1774 goto fail;
1777 /* Set up our peer's port number. */
1778 retval->peer.port = ntohs(chunk->sctp_hdr->source);
1780 /* Populate the association from the cookie. */
1781 memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
1783 if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
1784 GFP_ATOMIC) < 0) {
1785 *error = -SCTP_IERROR_NOMEM;
1786 goto fail;
1789 /* Also, add the destination address. */
1790 if (list_empty(&retval->base.bind_addr.address_list)) {
1791 sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest,
1792 SCTP_ADDR_SRC, GFP_ATOMIC);
1795 retval->next_tsn = retval->c.initial_tsn;
1796 retval->ctsn_ack_point = retval->next_tsn - 1;
1797 retval->addip_serial = retval->c.initial_tsn;
1798 retval->adv_peer_ack_point = retval->ctsn_ack_point;
1799 retval->peer.prsctp_capable = retval->c.prsctp_capable;
1800 retval->peer.adaptation_ind = retval->c.adaptation_ind;
1802 /* The INIT stuff will be done by the side effects. */
1803 return retval;
1805 fail:
1806 if (retval)
1807 sctp_association_free(retval);
1809 return NULL;
1811 malformed:
1812 /* Yikes! The packet is either corrupt or deliberately
1813 * malformed.
1815 *error = -SCTP_IERROR_MALFORMED;
1816 goto fail;
1819 /********************************************************************
1820 * 3rd Level Abstractions
1821 ********************************************************************/
1823 struct __sctp_missing {
1824 __be32 num_missing;
1825 __be16 type;
1826 } __attribute__((packed));
1829 * Report a missing mandatory parameter.
1831 static int sctp_process_missing_param(const struct sctp_association *asoc,
1832 sctp_param_t paramtype,
1833 struct sctp_chunk *chunk,
1834 struct sctp_chunk **errp)
1836 struct __sctp_missing report;
1837 __u16 len;
1839 len = WORD_ROUND(sizeof(report));
1841 /* Make an ERROR chunk, preparing enough room for
1842 * returning multiple unknown parameters.
1844 if (!*errp)
1845 *errp = sctp_make_op_error_space(asoc, chunk, len);
1847 if (*errp) {
1848 report.num_missing = htonl(1);
1849 report.type = paramtype;
1850 sctp_init_cause(*errp, SCTP_ERROR_MISS_PARAM,
1851 sizeof(report));
1852 sctp_addto_chunk(*errp, sizeof(report), &report);
1855 /* Stop processing this chunk. */
1856 return 0;
1859 /* Report an Invalid Mandatory Parameter. */
1860 static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
1861 struct sctp_chunk *chunk,
1862 struct sctp_chunk **errp)
1864 /* Invalid Mandatory Parameter Error has no payload. */
1866 if (!*errp)
1867 *errp = sctp_make_op_error_space(asoc, chunk, 0);
1869 if (*errp)
1870 sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, 0);
1872 /* Stop processing this chunk. */
1873 return 0;
1876 static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
1877 struct sctp_paramhdr *param,
1878 const struct sctp_chunk *chunk,
1879 struct sctp_chunk **errp)
1881 /* This is a fatal error. Any accumulated non-fatal errors are
1882 * not reported.
1884 if (*errp)
1885 sctp_chunk_free(*errp);
1887 /* Create an error chunk and fill it in with our payload. */
1888 *errp = sctp_make_violation_paramlen(asoc, chunk, param);
1890 return 0;
1894 /* Do not attempt to handle the HOST_NAME parm. However, do
1895 * send back an indicator to the peer.
1897 static int sctp_process_hn_param(const struct sctp_association *asoc,
1898 union sctp_params param,
1899 struct sctp_chunk *chunk,
1900 struct sctp_chunk **errp)
1902 __u16 len = ntohs(param.p->length);
1904 /* Processing of the HOST_NAME parameter will generate an
1905 * ABORT. If we've accumulated any non-fatal errors, they
1906 * would be unrecognized parameters and we should not include
1907 * them in the ABORT.
1909 if (*errp)
1910 sctp_chunk_free(*errp);
1912 *errp = sctp_make_op_error_space(asoc, chunk, len);
1914 if (*errp) {
1915 sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED, len);
1916 sctp_addto_chunk(*errp, len, param.v);
1919 /* Stop processing this chunk. */
1920 return 0;
1923 static int sctp_verify_ext_param(union sctp_params param)
1925 __u16 num_ext = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
1926 int have_auth = 0;
1927 int have_asconf = 0;
1928 int i;
1930 for (i = 0; i < num_ext; i++) {
1931 switch (param.ext->chunks[i]) {
1932 case SCTP_CID_AUTH:
1933 have_auth = 1;
1934 break;
1935 case SCTP_CID_ASCONF:
1936 case SCTP_CID_ASCONF_ACK:
1937 have_asconf = 1;
1938 break;
1942 /* ADD-IP Security: The draft requires us to ABORT or ignore the
1943 * INIT/INIT-ACK if ADD-IP is listed, but AUTH is not. Do this
1944 * only if ADD-IP is turned on and we are not backward-compatible
1945 * mode.
1947 if (sctp_addip_noauth)
1948 return 1;
1950 if (sctp_addip_enable && !have_auth && have_asconf)
1951 return 0;
1953 return 1;
1956 static void sctp_process_ext_param(struct sctp_association *asoc,
1957 union sctp_params param)
1959 __u16 num_ext = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
1960 int i;
1962 for (i = 0; i < num_ext; i++) {
1963 switch (param.ext->chunks[i]) {
1964 case SCTP_CID_FWD_TSN:
1965 if (sctp_prsctp_enable &&
1966 !asoc->peer.prsctp_capable)
1967 asoc->peer.prsctp_capable = 1;
1968 break;
1969 case SCTP_CID_AUTH:
1970 /* if the peer reports AUTH, assume that he
1971 * supports AUTH.
1973 if (sctp_auth_enable)
1974 asoc->peer.auth_capable = 1;
1975 break;
1976 case SCTP_CID_ASCONF:
1977 case SCTP_CID_ASCONF_ACK:
1978 if (sctp_addip_enable)
1979 asoc->peer.asconf_capable = 1;
1980 break;
1981 default:
1982 break;
1987 /* RFC 3.2.1 & the Implementers Guide 2.2.
1989 * The Parameter Types are encoded such that the
1990 * highest-order two bits specify the action that must be
1991 * taken if the processing endpoint does not recognize the
1992 * Parameter Type.
1994 * 00 - Stop processing this parameter; do not process any further
1995 * parameters within this chunk
1997 * 01 - Stop processing this parameter, do not process any further
1998 * parameters within this chunk, and report the unrecognized
1999 * parameter in an 'Unrecognized Parameter' ERROR chunk.
2001 * 10 - Skip this parameter and continue processing.
2003 * 11 - Skip this parameter and continue processing but
2004 * report the unrecognized parameter in an
2005 * 'Unrecognized Parameter' ERROR chunk.
2007 * Return value:
2008 * SCTP_IERROR_NO_ERROR - continue with the chunk
2009 * SCTP_IERROR_ERROR - stop and report an error.
2010 * SCTP_IERROR_NOMEME - out of memory.
2012 static sctp_ierror_t sctp_process_unk_param(const struct sctp_association *asoc,
2013 union sctp_params param,
2014 struct sctp_chunk *chunk,
2015 struct sctp_chunk **errp)
2017 int retval = SCTP_IERROR_NO_ERROR;
2019 switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
2020 case SCTP_PARAM_ACTION_DISCARD:
2021 retval = SCTP_IERROR_ERROR;
2022 break;
2023 case SCTP_PARAM_ACTION_SKIP:
2024 break;
2025 case SCTP_PARAM_ACTION_DISCARD_ERR:
2026 retval = SCTP_IERROR_ERROR;
2027 /* Fall through */
2028 case SCTP_PARAM_ACTION_SKIP_ERR:
2029 /* Make an ERROR chunk, preparing enough room for
2030 * returning multiple unknown parameters.
2032 if (NULL == *errp)
2033 *errp = sctp_make_op_error_fixed(asoc, chunk);
2035 if (*errp) {
2036 sctp_init_cause_fixed(*errp, SCTP_ERROR_UNKNOWN_PARAM,
2037 WORD_ROUND(ntohs(param.p->length)));
2038 sctp_addto_chunk_fixed(*errp,
2039 WORD_ROUND(ntohs(param.p->length)),
2040 param.v);
2041 } else {
2042 /* If there is no memory for generating the ERROR
2043 * report as specified, an ABORT will be triggered
2044 * to the peer and the association won't be
2045 * established.
2047 retval = SCTP_IERROR_NOMEM;
2049 break;
2050 default:
2051 break;
2054 return retval;
2057 /* Verify variable length parameters
2058 * Return values:
2059 * SCTP_IERROR_ABORT - trigger an ABORT
2060 * SCTP_IERROR_NOMEM - out of memory (abort)
2061 * SCTP_IERROR_ERROR - stop processing, trigger an ERROR
2062 * SCTP_IERROR_NO_ERROR - continue with the chunk
2064 static sctp_ierror_t sctp_verify_param(const struct sctp_association *asoc,
2065 union sctp_params param,
2066 sctp_cid_t cid,
2067 struct sctp_chunk *chunk,
2068 struct sctp_chunk **err_chunk)
2070 struct sctp_hmac_algo_param *hmacs;
2071 int retval = SCTP_IERROR_NO_ERROR;
2072 __u16 n_elt, id = 0;
2073 int i;
2075 /* FIXME - This routine is not looking at each parameter per the
2076 * chunk type, i.e., unrecognized parameters should be further
2077 * identified based on the chunk id.
2080 switch (param.p->type) {
2081 case SCTP_PARAM_IPV4_ADDRESS:
2082 case SCTP_PARAM_IPV6_ADDRESS:
2083 case SCTP_PARAM_COOKIE_PRESERVATIVE:
2084 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2085 case SCTP_PARAM_STATE_COOKIE:
2086 case SCTP_PARAM_HEARTBEAT_INFO:
2087 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2088 case SCTP_PARAM_ECN_CAPABLE:
2089 case SCTP_PARAM_ADAPTATION_LAYER_IND:
2090 break;
2092 case SCTP_PARAM_SUPPORTED_EXT:
2093 if (!sctp_verify_ext_param(param))
2094 return SCTP_IERROR_ABORT;
2095 break;
2097 case SCTP_PARAM_SET_PRIMARY:
2098 if (sctp_addip_enable)
2099 break;
2100 goto fallthrough;
2102 case SCTP_PARAM_HOST_NAME_ADDRESS:
2103 /* Tell the peer, we won't support this param. */
2104 sctp_process_hn_param(asoc, param, chunk, err_chunk);
2105 retval = SCTP_IERROR_ABORT;
2106 break;
2108 case SCTP_PARAM_FWD_TSN_SUPPORT:
2109 if (sctp_prsctp_enable)
2110 break;
2111 goto fallthrough;
2113 case SCTP_PARAM_RANDOM:
2114 if (!sctp_auth_enable)
2115 goto fallthrough;
2117 /* SCTP-AUTH: Secion 6.1
2118 * If the random number is not 32 byte long the association
2119 * MUST be aborted. The ABORT chunk SHOULD contain the error
2120 * cause 'Protocol Violation'.
2122 if (SCTP_AUTH_RANDOM_LENGTH !=
2123 ntohs(param.p->length) - sizeof(sctp_paramhdr_t)) {
2124 sctp_process_inv_paramlength(asoc, param.p,
2125 chunk, err_chunk);
2126 retval = SCTP_IERROR_ABORT;
2128 break;
2130 case SCTP_PARAM_CHUNKS:
2131 if (!sctp_auth_enable)
2132 goto fallthrough;
2134 /* SCTP-AUTH: Section 3.2
2135 * The CHUNKS parameter MUST be included once in the INIT or
2136 * INIT-ACK chunk if the sender wants to receive authenticated
2137 * chunks. Its maximum length is 260 bytes.
2139 if (260 < ntohs(param.p->length)) {
2140 sctp_process_inv_paramlength(asoc, param.p,
2141 chunk, err_chunk);
2142 retval = SCTP_IERROR_ABORT;
2144 break;
2146 case SCTP_PARAM_HMAC_ALGO:
2147 if (!sctp_auth_enable)
2148 goto fallthrough;
2150 hmacs = (struct sctp_hmac_algo_param *)param.p;
2151 n_elt = (ntohs(param.p->length) - sizeof(sctp_paramhdr_t)) >> 1;
2153 /* SCTP-AUTH: Section 6.1
2154 * The HMAC algorithm based on SHA-1 MUST be supported and
2155 * included in the HMAC-ALGO parameter.
2157 for (i = 0; i < n_elt; i++) {
2158 id = ntohs(hmacs->hmac_ids[i]);
2160 if (id == SCTP_AUTH_HMAC_ID_SHA1)
2161 break;
2164 if (id != SCTP_AUTH_HMAC_ID_SHA1) {
2165 sctp_process_inv_paramlength(asoc, param.p, chunk,
2166 err_chunk);
2167 retval = SCTP_IERROR_ABORT;
2169 break;
2170 fallthrough:
2171 default:
2172 SCTP_DEBUG_PRINTK("Unrecognized param: %d for chunk %d.\n",
2173 ntohs(param.p->type), cid);
2174 retval = sctp_process_unk_param(asoc, param, chunk, err_chunk);
2175 break;
2177 return retval;
2180 /* Verify the INIT packet before we process it. */
2181 int sctp_verify_init(const struct sctp_association *asoc,
2182 sctp_cid_t cid,
2183 sctp_init_chunk_t *peer_init,
2184 struct sctp_chunk *chunk,
2185 struct sctp_chunk **errp)
2187 union sctp_params param;
2188 int has_cookie = 0;
2189 int result;
2191 /* Verify stream values are non-zero. */
2192 if ((0 == peer_init->init_hdr.num_outbound_streams) ||
2193 (0 == peer_init->init_hdr.num_inbound_streams) ||
2194 (0 == peer_init->init_hdr.init_tag) ||
2195 (SCTP_DEFAULT_MINWINDOW > ntohl(peer_init->init_hdr.a_rwnd))) {
2197 return sctp_process_inv_mandatory(asoc, chunk, errp);
2200 /* Check for missing mandatory parameters. */
2201 sctp_walk_params(param, peer_init, init_hdr.params) {
2203 if (SCTP_PARAM_STATE_COOKIE == param.p->type)
2204 has_cookie = 1;
2206 } /* for (loop through all parameters) */
2208 /* There is a possibility that a parameter length was bad and
2209 * in that case we would have stoped walking the parameters.
2210 * The current param.p would point at the bad one.
2211 * Current consensus on the mailing list is to generate a PROTOCOL
2212 * VIOLATION error. We build the ERROR chunk here and let the normal
2213 * error handling code build and send the packet.
2215 if (param.v != (void*)chunk->chunk_end)
2216 return sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
2218 /* The only missing mandatory param possible today is
2219 * the state cookie for an INIT-ACK chunk.
2221 if ((SCTP_CID_INIT_ACK == cid) && !has_cookie)
2222 return sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
2223 chunk, errp);
2225 /* Verify all the variable length parameters */
2226 sctp_walk_params(param, peer_init, init_hdr.params) {
2228 result = sctp_verify_param(asoc, param, cid, chunk, errp);
2229 switch (result) {
2230 case SCTP_IERROR_ABORT:
2231 case SCTP_IERROR_NOMEM:
2232 return 0;
2233 case SCTP_IERROR_ERROR:
2234 return 1;
2235 case SCTP_IERROR_NO_ERROR:
2236 default:
2237 break;
2240 } /* for (loop through all parameters) */
2242 return 1;
2245 /* Unpack the parameters in an INIT packet into an association.
2246 * Returns 0 on failure, else success.
2247 * FIXME: This is an association method.
2249 int sctp_process_init(struct sctp_association *asoc, sctp_cid_t cid,
2250 const union sctp_addr *peer_addr,
2251 sctp_init_chunk_t *peer_init, gfp_t gfp)
2253 union sctp_params param;
2254 struct sctp_transport *transport;
2255 struct list_head *pos, *temp;
2256 char *cookie;
2258 /* We must include the address that the INIT packet came from.
2259 * This is the only address that matters for an INIT packet.
2260 * When processing a COOKIE ECHO, we retrieve the from address
2261 * of the INIT from the cookie.
2264 /* This implementation defaults to making the first transport
2265 * added as the primary transport. The source address seems to
2266 * be a a better choice than any of the embedded addresses.
2268 if (peer_addr) {
2269 if(!sctp_assoc_add_peer(asoc, peer_addr, gfp, SCTP_ACTIVE))
2270 goto nomem;
2273 /* Process the initialization parameters. */
2274 sctp_walk_params(param, peer_init, init_hdr.params) {
2276 if (!sctp_process_param(asoc, param, peer_addr, gfp))
2277 goto clean_up;
2280 /* AUTH: After processing the parameters, make sure that we
2281 * have all the required info to potentially do authentications.
2283 if (asoc->peer.auth_capable && (!asoc->peer.peer_random ||
2284 !asoc->peer.peer_hmacs))
2285 asoc->peer.auth_capable = 0;
2287 /* In a non-backward compatible mode, if the peer claims
2288 * support for ADD-IP but not AUTH, the ADD-IP spec states
2289 * that we MUST ABORT the association. Section 6. The section
2290 * also give us an option to silently ignore the packet, which
2291 * is what we'll do here.
2293 if (!sctp_addip_noauth &&
2294 (asoc->peer.asconf_capable && !asoc->peer.auth_capable)) {
2295 asoc->peer.addip_disabled_mask |= (SCTP_PARAM_ADD_IP |
2296 SCTP_PARAM_DEL_IP |
2297 SCTP_PARAM_SET_PRIMARY);
2298 asoc->peer.asconf_capable = 0;
2299 goto clean_up;
2302 /* Walk list of transports, removing transports in the UNKNOWN state. */
2303 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2304 transport = list_entry(pos, struct sctp_transport, transports);
2305 if (transport->state == SCTP_UNKNOWN) {
2306 sctp_assoc_rm_peer(asoc, transport);
2310 /* The fixed INIT headers are always in network byte
2311 * order.
2313 asoc->peer.i.init_tag =
2314 ntohl(peer_init->init_hdr.init_tag);
2315 asoc->peer.i.a_rwnd =
2316 ntohl(peer_init->init_hdr.a_rwnd);
2317 asoc->peer.i.num_outbound_streams =
2318 ntohs(peer_init->init_hdr.num_outbound_streams);
2319 asoc->peer.i.num_inbound_streams =
2320 ntohs(peer_init->init_hdr.num_inbound_streams);
2321 asoc->peer.i.initial_tsn =
2322 ntohl(peer_init->init_hdr.initial_tsn);
2324 /* Apply the upper bounds for output streams based on peer's
2325 * number of inbound streams.
2327 if (asoc->c.sinit_num_ostreams >
2328 ntohs(peer_init->init_hdr.num_inbound_streams)) {
2329 asoc->c.sinit_num_ostreams =
2330 ntohs(peer_init->init_hdr.num_inbound_streams);
2333 if (asoc->c.sinit_max_instreams >
2334 ntohs(peer_init->init_hdr.num_outbound_streams)) {
2335 asoc->c.sinit_max_instreams =
2336 ntohs(peer_init->init_hdr.num_outbound_streams);
2339 /* Copy Initiation tag from INIT to VT_peer in cookie. */
2340 asoc->c.peer_vtag = asoc->peer.i.init_tag;
2342 /* Peer Rwnd : Current calculated value of the peer's rwnd. */
2343 asoc->peer.rwnd = asoc->peer.i.a_rwnd;
2345 /* Copy cookie in case we need to resend COOKIE-ECHO. */
2346 cookie = asoc->peer.cookie;
2347 if (cookie) {
2348 asoc->peer.cookie = kmemdup(cookie, asoc->peer.cookie_len, gfp);
2349 if (!asoc->peer.cookie)
2350 goto clean_up;
2353 /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
2354 * high (for example, implementations MAY use the size of the receiver
2355 * advertised window).
2357 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
2358 transports) {
2359 transport->ssthresh = asoc->peer.i.a_rwnd;
2362 /* Set up the TSN tracking pieces. */
2363 if (!sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
2364 asoc->peer.i.initial_tsn, gfp))
2365 goto clean_up;
2367 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
2369 * The stream sequence number in all the streams shall start
2370 * from 0 when the association is established. Also, when the
2371 * stream sequence number reaches the value 65535 the next
2372 * stream sequence number shall be set to 0.
2375 /* Allocate storage for the negotiated streams if it is not a temporary
2376 * association.
2378 if (!asoc->temp) {
2379 int error;
2381 asoc->ssnmap = sctp_ssnmap_new(asoc->c.sinit_max_instreams,
2382 asoc->c.sinit_num_ostreams, gfp);
2383 if (!asoc->ssnmap)
2384 goto clean_up;
2386 error = sctp_assoc_set_id(asoc, gfp);
2387 if (error)
2388 goto clean_up;
2391 /* ADDIP Section 4.1 ASCONF Chunk Procedures
2393 * When an endpoint has an ASCONF signaled change to be sent to the
2394 * remote endpoint it should do the following:
2395 * ...
2396 * A2) A serial number should be assigned to the Chunk. The serial
2397 * number should be a monotonically increasing number. All serial
2398 * numbers are defined to be initialized at the start of the
2399 * association to the same value as the Initial TSN.
2401 asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
2402 return 1;
2404 clean_up:
2405 /* Release the transport structures. */
2406 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2407 transport = list_entry(pos, struct sctp_transport, transports);
2408 if (transport->state != SCTP_ACTIVE)
2409 sctp_assoc_rm_peer(asoc, transport);
2412 nomem:
2413 return 0;
2417 /* Update asoc with the option described in param.
2419 * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
2421 * asoc is the association to update.
2422 * param is the variable length parameter to use for update.
2423 * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
2424 * If the current packet is an INIT we want to minimize the amount of
2425 * work we do. In particular, we should not build transport
2426 * structures for the addresses.
2428 static int sctp_process_param(struct sctp_association *asoc,
2429 union sctp_params param,
2430 const union sctp_addr *peer_addr,
2431 gfp_t gfp)
2433 union sctp_addr addr;
2434 int i;
2435 __u16 sat;
2436 int retval = 1;
2437 sctp_scope_t scope;
2438 time_t stale;
2439 struct sctp_af *af;
2440 union sctp_addr_param *addr_param;
2441 struct sctp_transport *t;
2443 /* We maintain all INIT parameters in network byte order all the
2444 * time. This allows us to not worry about whether the parameters
2445 * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
2447 switch (param.p->type) {
2448 case SCTP_PARAM_IPV6_ADDRESS:
2449 if (PF_INET6 != asoc->base.sk->sk_family)
2450 break;
2451 goto do_addr_param;
2453 case SCTP_PARAM_IPV4_ADDRESS:
2454 /* v4 addresses are not allowed on v6-only socket */
2455 if (ipv6_only_sock(asoc->base.sk))
2456 break;
2457 do_addr_param:
2458 af = sctp_get_af_specific(param_type2af(param.p->type));
2459 af->from_addr_param(&addr, param.addr, htons(asoc->peer.port), 0);
2460 scope = sctp_scope(peer_addr);
2461 if (sctp_in_scope(&addr, scope))
2462 if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_UNCONFIRMED))
2463 return 0;
2464 break;
2466 case SCTP_PARAM_COOKIE_PRESERVATIVE:
2467 if (!sctp_cookie_preserve_enable)
2468 break;
2470 stale = ntohl(param.life->lifespan_increment);
2472 /* Suggested Cookie Life span increment's unit is msec,
2473 * (1/1000sec).
2475 asoc->cookie_life.tv_sec += stale / 1000;
2476 asoc->cookie_life.tv_usec += (stale % 1000) * 1000;
2477 break;
2479 case SCTP_PARAM_HOST_NAME_ADDRESS:
2480 SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
2481 break;
2483 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2484 /* Turn off the default values first so we'll know which
2485 * ones are really set by the peer.
2487 asoc->peer.ipv4_address = 0;
2488 asoc->peer.ipv6_address = 0;
2490 /* Assume that peer supports the address family
2491 * by which it sends a packet.
2493 if (peer_addr->sa.sa_family == AF_INET6)
2494 asoc->peer.ipv6_address = 1;
2495 else if (peer_addr->sa.sa_family == AF_INET)
2496 asoc->peer.ipv4_address = 1;
2498 /* Cycle through address types; avoid divide by 0. */
2499 sat = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2500 if (sat)
2501 sat /= sizeof(__u16);
2503 for (i = 0; i < sat; ++i) {
2504 switch (param.sat->types[i]) {
2505 case SCTP_PARAM_IPV4_ADDRESS:
2506 asoc->peer.ipv4_address = 1;
2507 break;
2509 case SCTP_PARAM_IPV6_ADDRESS:
2510 if (PF_INET6 == asoc->base.sk->sk_family)
2511 asoc->peer.ipv6_address = 1;
2512 break;
2514 case SCTP_PARAM_HOST_NAME_ADDRESS:
2515 asoc->peer.hostname_address = 1;
2516 break;
2518 default: /* Just ignore anything else. */
2519 break;
2522 break;
2524 case SCTP_PARAM_STATE_COOKIE:
2525 asoc->peer.cookie_len =
2526 ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2527 asoc->peer.cookie = param.cookie->body;
2528 break;
2530 case SCTP_PARAM_HEARTBEAT_INFO:
2531 /* Would be odd to receive, but it causes no problems. */
2532 break;
2534 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2535 /* Rejected during verify stage. */
2536 break;
2538 case SCTP_PARAM_ECN_CAPABLE:
2539 asoc->peer.ecn_capable = 1;
2540 break;
2542 case SCTP_PARAM_ADAPTATION_LAYER_IND:
2543 asoc->peer.adaptation_ind = ntohl(param.aind->adaptation_ind);
2544 break;
2546 case SCTP_PARAM_SET_PRIMARY:
2547 if (!sctp_addip_enable)
2548 goto fall_through;
2550 addr_param = param.v + sizeof(sctp_addip_param_t);
2552 af = sctp_get_af_specific(param_type2af(param.p->type));
2553 af->from_addr_param(&addr, addr_param,
2554 htons(asoc->peer.port), 0);
2556 /* if the address is invalid, we can't process it.
2557 * XXX: see spec for what to do.
2559 if (!af->addr_valid(&addr, NULL, NULL))
2560 break;
2562 t = sctp_assoc_lookup_paddr(asoc, &addr);
2563 if (!t)
2564 break;
2566 sctp_assoc_set_primary(asoc, t);
2567 break;
2569 case SCTP_PARAM_SUPPORTED_EXT:
2570 sctp_process_ext_param(asoc, param);
2571 break;
2573 case SCTP_PARAM_FWD_TSN_SUPPORT:
2574 if (sctp_prsctp_enable) {
2575 asoc->peer.prsctp_capable = 1;
2576 break;
2578 /* Fall Through */
2579 goto fall_through;
2581 case SCTP_PARAM_RANDOM:
2582 if (!sctp_auth_enable)
2583 goto fall_through;
2585 /* Save peer's random parameter */
2586 asoc->peer.peer_random = kmemdup(param.p,
2587 ntohs(param.p->length), gfp);
2588 if (!asoc->peer.peer_random) {
2589 retval = 0;
2590 break;
2592 break;
2594 case SCTP_PARAM_HMAC_ALGO:
2595 if (!sctp_auth_enable)
2596 goto fall_through;
2598 /* Save peer's HMAC list */
2599 asoc->peer.peer_hmacs = kmemdup(param.p,
2600 ntohs(param.p->length), gfp);
2601 if (!asoc->peer.peer_hmacs) {
2602 retval = 0;
2603 break;
2606 /* Set the default HMAC the peer requested*/
2607 sctp_auth_asoc_set_default_hmac(asoc, param.hmac_algo);
2608 break;
2610 case SCTP_PARAM_CHUNKS:
2611 if (!sctp_auth_enable)
2612 goto fall_through;
2614 asoc->peer.peer_chunks = kmemdup(param.p,
2615 ntohs(param.p->length), gfp);
2616 if (!asoc->peer.peer_chunks)
2617 retval = 0;
2618 break;
2619 fall_through:
2620 default:
2621 /* Any unrecognized parameters should have been caught
2622 * and handled by sctp_verify_param() which should be
2623 * called prior to this routine. Simply log the error
2624 * here.
2626 SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
2627 ntohs(param.p->type), asoc);
2628 break;
2631 return retval;
2634 /* Select a new verification tag. */
2635 __u32 sctp_generate_tag(const struct sctp_endpoint *ep)
2637 /* I believe that this random number generator complies with RFC1750.
2638 * A tag of 0 is reserved for special cases (e.g. INIT).
2640 __u32 x;
2642 do {
2643 get_random_bytes(&x, sizeof(__u32));
2644 } while (x == 0);
2646 return x;
2649 /* Select an initial TSN to send during startup. */
2650 __u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
2652 __u32 retval;
2654 get_random_bytes(&retval, sizeof(__u32));
2655 return retval;
2659 * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
2660 * 0 1 2 3
2661 * 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
2662 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2663 * | Type = 0xC1 | Chunk Flags | Chunk Length |
2664 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2665 * | Serial Number |
2666 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2667 * | Address Parameter |
2668 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2669 * | ASCONF Parameter #1 |
2670 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2671 * \ \
2672 * / .... /
2673 * \ \
2674 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2675 * | ASCONF Parameter #N |
2676 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2678 * Address Parameter and other parameter will not be wrapped in this function
2680 static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
2681 union sctp_addr *addr,
2682 int vparam_len)
2684 sctp_addiphdr_t asconf;
2685 struct sctp_chunk *retval;
2686 int length = sizeof(asconf) + vparam_len;
2687 union sctp_addr_param addrparam;
2688 int addrlen;
2689 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2691 addrlen = af->to_addr_param(addr, &addrparam);
2692 if (!addrlen)
2693 return NULL;
2694 length += addrlen;
2696 /* Create the chunk. */
2697 retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF, 0, length);
2698 if (!retval)
2699 return NULL;
2701 asconf.serial = htonl(asoc->addip_serial++);
2703 retval->subh.addip_hdr =
2704 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2705 retval->param_hdr.v =
2706 sctp_addto_chunk(retval, addrlen, &addrparam);
2708 return retval;
2711 /* ADDIP
2712 * 3.2.1 Add IP Address
2713 * 0 1 2 3
2714 * 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
2715 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2716 * | Type = 0xC001 | Length = Variable |
2717 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2718 * | ASCONF-Request Correlation ID |
2719 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2720 * | Address Parameter |
2721 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2723 * 3.2.2 Delete IP Address
2724 * 0 1 2 3
2725 * 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
2726 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2727 * | Type = 0xC002 | Length = Variable |
2728 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2729 * | ASCONF-Request Correlation ID |
2730 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2731 * | Address Parameter |
2732 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2735 struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
2736 union sctp_addr *laddr,
2737 struct sockaddr *addrs,
2738 int addrcnt,
2739 __be16 flags)
2741 sctp_addip_param_t param;
2742 struct sctp_chunk *retval;
2743 union sctp_addr_param addr_param;
2744 union sctp_addr *addr;
2745 void *addr_buf;
2746 struct sctp_af *af;
2747 int paramlen = sizeof(param);
2748 int addr_param_len = 0;
2749 int totallen = 0;
2750 int i;
2752 /* Get total length of all the address parameters. */
2753 addr_buf = addrs;
2754 for (i = 0; i < addrcnt; i++) {
2755 addr = (union sctp_addr *)addr_buf;
2756 af = sctp_get_af_specific(addr->v4.sin_family);
2757 addr_param_len = af->to_addr_param(addr, &addr_param);
2759 totallen += paramlen;
2760 totallen += addr_param_len;
2762 addr_buf += af->sockaddr_len;
2765 /* Create an asconf chunk with the required length. */
2766 retval = sctp_make_asconf(asoc, laddr, totallen);
2767 if (!retval)
2768 return NULL;
2770 /* Add the address parameters to the asconf chunk. */
2771 addr_buf = addrs;
2772 for (i = 0; i < addrcnt; i++) {
2773 addr = (union sctp_addr *)addr_buf;
2774 af = sctp_get_af_specific(addr->v4.sin_family);
2775 addr_param_len = af->to_addr_param(addr, &addr_param);
2776 param.param_hdr.type = flags;
2777 param.param_hdr.length = htons(paramlen + addr_param_len);
2778 param.crr_id = i;
2780 sctp_addto_chunk(retval, paramlen, &param);
2781 sctp_addto_chunk(retval, addr_param_len, &addr_param);
2783 addr_buf += af->sockaddr_len;
2785 return retval;
2788 /* ADDIP
2789 * 3.2.4 Set Primary IP Address
2790 * 0 1 2 3
2791 * 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
2792 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2793 * | Type =0xC004 | Length = Variable |
2794 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2795 * | ASCONF-Request Correlation ID |
2796 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2797 * | Address Parameter |
2798 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2800 * Create an ASCONF chunk with Set Primary IP address parameter.
2802 struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
2803 union sctp_addr *addr)
2805 sctp_addip_param_t param;
2806 struct sctp_chunk *retval;
2807 int len = sizeof(param);
2808 union sctp_addr_param addrparam;
2809 int addrlen;
2810 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2812 addrlen = af->to_addr_param(addr, &addrparam);
2813 if (!addrlen)
2814 return NULL;
2815 len += addrlen;
2817 /* Create the chunk and make asconf header. */
2818 retval = sctp_make_asconf(asoc, addr, len);
2819 if (!retval)
2820 return NULL;
2822 param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
2823 param.param_hdr.length = htons(len);
2824 param.crr_id = 0;
2826 sctp_addto_chunk(retval, sizeof(param), &param);
2827 sctp_addto_chunk(retval, addrlen, &addrparam);
2829 return retval;
2832 /* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2833 * 0 1 2 3
2834 * 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
2835 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2836 * | Type = 0x80 | Chunk Flags | Chunk Length |
2837 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2838 * | Serial Number |
2839 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2840 * | ASCONF Parameter Response#1 |
2841 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2842 * \ \
2843 * / .... /
2844 * \ \
2845 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2846 * | ASCONF Parameter Response#N |
2847 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2849 * Create an ASCONF_ACK chunk with enough space for the parameter responses.
2851 static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
2852 __u32 serial, int vparam_len)
2854 sctp_addiphdr_t asconf;
2855 struct sctp_chunk *retval;
2856 int length = sizeof(asconf) + vparam_len;
2858 /* Create the chunk. */
2859 retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF_ACK, 0, length);
2860 if (!retval)
2861 return NULL;
2863 asconf.serial = htonl(serial);
2865 retval->subh.addip_hdr =
2866 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2868 return retval;
2871 /* Add response parameters to an ASCONF_ACK chunk. */
2872 static void sctp_add_asconf_response(struct sctp_chunk *chunk, __be32 crr_id,
2873 __be16 err_code, sctp_addip_param_t *asconf_param)
2875 sctp_addip_param_t ack_param;
2876 sctp_errhdr_t err_param;
2877 int asconf_param_len = 0;
2878 int err_param_len = 0;
2879 __be16 response_type;
2881 if (SCTP_ERROR_NO_ERROR == err_code) {
2882 response_type = SCTP_PARAM_SUCCESS_REPORT;
2883 } else {
2884 response_type = SCTP_PARAM_ERR_CAUSE;
2885 err_param_len = sizeof(err_param);
2886 if (asconf_param)
2887 asconf_param_len =
2888 ntohs(asconf_param->param_hdr.length);
2891 /* Add Success Indication or Error Cause Indication parameter. */
2892 ack_param.param_hdr.type = response_type;
2893 ack_param.param_hdr.length = htons(sizeof(ack_param) +
2894 err_param_len +
2895 asconf_param_len);
2896 ack_param.crr_id = crr_id;
2897 sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
2899 if (SCTP_ERROR_NO_ERROR == err_code)
2900 return;
2902 /* Add Error Cause parameter. */
2903 err_param.cause = err_code;
2904 err_param.length = htons(err_param_len + asconf_param_len);
2905 sctp_addto_chunk(chunk, err_param_len, &err_param);
2907 /* Add the failed TLV copied from ASCONF chunk. */
2908 if (asconf_param)
2909 sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
2912 /* Process a asconf parameter. */
2913 static __be16 sctp_process_asconf_param(struct sctp_association *asoc,
2914 struct sctp_chunk *asconf,
2915 sctp_addip_param_t *asconf_param)
2917 struct sctp_transport *peer;
2918 struct sctp_af *af;
2919 union sctp_addr addr;
2920 union sctp_addr_param *addr_param;
2922 addr_param = (union sctp_addr_param *)
2923 ((void *)asconf_param + sizeof(sctp_addip_param_t));
2925 if (asconf_param->param_hdr.type != SCTP_PARAM_ADD_IP &&
2926 asconf_param->param_hdr.type != SCTP_PARAM_DEL_IP &&
2927 asconf_param->param_hdr.type != SCTP_PARAM_SET_PRIMARY)
2928 return SCTP_ERROR_UNKNOWN_PARAM;
2930 switch (addr_param->v4.param_hdr.type) {
2931 case SCTP_PARAM_IPV6_ADDRESS:
2932 if (!asoc->peer.ipv6_address)
2933 return SCTP_ERROR_DNS_FAILED;
2934 break;
2935 case SCTP_PARAM_IPV4_ADDRESS:
2936 if (!asoc->peer.ipv4_address)
2937 return SCTP_ERROR_DNS_FAILED;
2938 break;
2939 default:
2940 return SCTP_ERROR_DNS_FAILED;
2943 af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2944 if (unlikely(!af))
2945 return SCTP_ERROR_DNS_FAILED;
2947 af->from_addr_param(&addr, addr_param, htons(asoc->peer.port), 0);
2949 /* ADDIP 4.2.1 This parameter MUST NOT contain a broadcast
2950 * or multicast address.
2951 * (note: wildcard is permitted and requires special handling so
2952 * make sure we check for that)
2954 if (!af->is_any(&addr) && !af->addr_valid(&addr, NULL, asconf->skb))
2955 return SCTP_ERROR_DNS_FAILED;
2957 switch (asconf_param->param_hdr.type) {
2958 case SCTP_PARAM_ADD_IP:
2959 /* Section 4.2.1:
2960 * If the address 0.0.0.0 or ::0 is provided, the source
2961 * address of the packet MUST be added.
2963 if (af->is_any(&addr))
2964 memcpy(&addr, &asconf->source, sizeof(addr));
2966 /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
2967 * request and does not have the local resources to add this
2968 * new address to the association, it MUST return an Error
2969 * Cause TLV set to the new error code 'Operation Refused
2970 * Due to Resource Shortage'.
2973 peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC, SCTP_UNCONFIRMED);
2974 if (!peer)
2975 return SCTP_ERROR_RSRC_LOW;
2977 /* Start the heartbeat timer. */
2978 if (!mod_timer(&peer->hb_timer, sctp_transport_timeout(peer)))
2979 sctp_transport_hold(peer);
2980 break;
2981 case SCTP_PARAM_DEL_IP:
2982 /* ADDIP 4.3 D7) If a request is received to delete the
2983 * last remaining IP address of a peer endpoint, the receiver
2984 * MUST send an Error Cause TLV with the error cause set to the
2985 * new error code 'Request to Delete Last Remaining IP Address'.
2987 if (asoc->peer.transport_count == 1)
2988 return SCTP_ERROR_DEL_LAST_IP;
2990 /* ADDIP 4.3 D8) If a request is received to delete an IP
2991 * address which is also the source address of the IP packet
2992 * which contained the ASCONF chunk, the receiver MUST reject
2993 * this request. To reject the request the receiver MUST send
2994 * an Error Cause TLV set to the new error code 'Request to
2995 * Delete Source IP Address'
2997 if (sctp_cmp_addr_exact(sctp_source(asconf), &addr))
2998 return SCTP_ERROR_DEL_SRC_IP;
3000 /* Section 4.2.2
3001 * If the address 0.0.0.0 or ::0 is provided, all
3002 * addresses of the peer except the source address of the
3003 * packet MUST be deleted.
3005 if (af->is_any(&addr)) {
3006 sctp_assoc_set_primary(asoc, asconf->transport);
3007 sctp_assoc_del_nonprimary_peers(asoc,
3008 asconf->transport);
3009 } else
3010 sctp_assoc_del_peer(asoc, &addr);
3011 break;
3012 case SCTP_PARAM_SET_PRIMARY:
3013 /* ADDIP Section 4.2.4
3014 * If the address 0.0.0.0 or ::0 is provided, the receiver
3015 * MAY mark the source address of the packet as its
3016 * primary.
3018 if (af->is_any(&addr))
3019 memcpy(&addr.v4, sctp_source(asconf), sizeof(addr));
3021 peer = sctp_assoc_lookup_paddr(asoc, &addr);
3022 if (!peer)
3023 return SCTP_ERROR_DNS_FAILED;
3025 sctp_assoc_set_primary(asoc, peer);
3026 break;
3029 return SCTP_ERROR_NO_ERROR;
3032 /* Verify the ASCONF packet before we process it. */
3033 int sctp_verify_asconf(const struct sctp_association *asoc,
3034 struct sctp_paramhdr *param_hdr, void *chunk_end,
3035 struct sctp_paramhdr **errp) {
3036 sctp_addip_param_t *asconf_param;
3037 union sctp_params param;
3038 int length, plen;
3040 param.v = (sctp_paramhdr_t *) param_hdr;
3041 while (param.v <= chunk_end - sizeof(sctp_paramhdr_t)) {
3042 length = ntohs(param.p->length);
3043 *errp = param.p;
3045 if (param.v > chunk_end - length ||
3046 length < sizeof(sctp_paramhdr_t))
3047 return 0;
3049 switch (param.p->type) {
3050 case SCTP_PARAM_ADD_IP:
3051 case SCTP_PARAM_DEL_IP:
3052 case SCTP_PARAM_SET_PRIMARY:
3053 asconf_param = (sctp_addip_param_t *)param.v;
3054 plen = ntohs(asconf_param->param_hdr.length);
3055 if (plen < sizeof(sctp_addip_param_t) +
3056 sizeof(sctp_paramhdr_t))
3057 return 0;
3058 break;
3059 case SCTP_PARAM_SUCCESS_REPORT:
3060 case SCTP_PARAM_ADAPTATION_LAYER_IND:
3061 if (length != sizeof(sctp_addip_param_t))
3062 return 0;
3064 break;
3065 default:
3066 break;
3069 param.v += WORD_ROUND(length);
3072 if (param.v != chunk_end)
3073 return 0;
3075 return 1;
3078 /* Process an incoming ASCONF chunk with the next expected serial no. and
3079 * return an ASCONF_ACK chunk to be sent in response.
3081 struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
3082 struct sctp_chunk *asconf)
3084 sctp_addiphdr_t *hdr;
3085 union sctp_addr_param *addr_param;
3086 sctp_addip_param_t *asconf_param;
3087 struct sctp_chunk *asconf_ack;
3089 __be16 err_code;
3090 int length = 0;
3091 int chunk_len;
3092 __u32 serial;
3093 int all_param_pass = 1;
3095 chunk_len = ntohs(asconf->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
3096 hdr = (sctp_addiphdr_t *)asconf->skb->data;
3097 serial = ntohl(hdr->serial);
3099 /* Skip the addiphdr and store a pointer to address parameter. */
3100 length = sizeof(sctp_addiphdr_t);
3101 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
3102 chunk_len -= length;
3104 /* Skip the address parameter and store a pointer to the first
3105 * asconf parameter.
3107 length = ntohs(addr_param->v4.param_hdr.length);
3108 asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
3109 chunk_len -= length;
3111 /* create an ASCONF_ACK chunk.
3112 * Based on the definitions of parameters, we know that the size of
3113 * ASCONF_ACK parameters are less than or equal to the fourfold of ASCONF
3114 * parameters.
3116 asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 4);
3117 if (!asconf_ack)
3118 goto done;
3120 /* Process the TLVs contained within the ASCONF chunk. */
3121 while (chunk_len > 0) {
3122 err_code = sctp_process_asconf_param(asoc, asconf,
3123 asconf_param);
3124 /* ADDIP 4.1 A7)
3125 * If an error response is received for a TLV parameter,
3126 * all TLVs with no response before the failed TLV are
3127 * considered successful if not reported. All TLVs after
3128 * the failed response are considered unsuccessful unless
3129 * a specific success indication is present for the parameter.
3131 if (SCTP_ERROR_NO_ERROR != err_code)
3132 all_param_pass = 0;
3134 if (!all_param_pass)
3135 sctp_add_asconf_response(asconf_ack,
3136 asconf_param->crr_id, err_code,
3137 asconf_param);
3139 /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
3140 * an IP address sends an 'Out of Resource' in its response, it
3141 * MUST also fail any subsequent add or delete requests bundled
3142 * in the ASCONF.
3144 if (SCTP_ERROR_RSRC_LOW == err_code)
3145 goto done;
3147 /* Move to the next ASCONF param. */
3148 length = ntohs(asconf_param->param_hdr.length);
3149 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
3150 length);
3151 chunk_len -= length;
3154 done:
3155 asoc->peer.addip_serial++;
3157 /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
3158 * after freeing the reference to old asconf ack if any.
3160 if (asconf_ack) {
3161 sctp_chunk_hold(asconf_ack);
3162 list_add_tail(&asconf_ack->transmitted_list,
3163 &asoc->asconf_ack_list);
3166 return asconf_ack;
3169 /* Process a asconf parameter that is successfully acked. */
3170 static void sctp_asconf_param_success(struct sctp_association *asoc,
3171 sctp_addip_param_t *asconf_param)
3173 struct sctp_af *af;
3174 union sctp_addr addr;
3175 struct sctp_bind_addr *bp = &asoc->base.bind_addr;
3176 union sctp_addr_param *addr_param;
3177 struct sctp_transport *transport;
3178 struct sctp_sockaddr_entry *saddr;
3180 addr_param = (union sctp_addr_param *)
3181 ((void *)asconf_param + sizeof(sctp_addip_param_t));
3183 /* We have checked the packet before, so we do not check again. */
3184 af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
3185 af->from_addr_param(&addr, addr_param, htons(bp->port), 0);
3187 switch (asconf_param->param_hdr.type) {
3188 case SCTP_PARAM_ADD_IP:
3189 /* This is always done in BH context with a socket lock
3190 * held, so the list can not change.
3192 local_bh_disable();
3193 list_for_each_entry(saddr, &bp->address_list, list) {
3194 if (sctp_cmp_addr_exact(&saddr->a, &addr))
3195 saddr->state = SCTP_ADDR_SRC;
3197 local_bh_enable();
3198 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
3199 transports) {
3200 if (transport->state == SCTP_ACTIVE)
3201 continue;
3202 dst_release(transport->dst);
3203 sctp_transport_route(transport, NULL,
3204 sctp_sk(asoc->base.sk));
3206 break;
3207 case SCTP_PARAM_DEL_IP:
3208 local_bh_disable();
3209 sctp_del_bind_addr(bp, &addr);
3210 local_bh_enable();
3211 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
3212 transports) {
3213 dst_release(transport->dst);
3214 sctp_transport_route(transport, NULL,
3215 sctp_sk(asoc->base.sk));
3217 break;
3218 default:
3219 break;
3223 /* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
3224 * for the given asconf parameter. If there is no response for this parameter,
3225 * return the error code based on the third argument 'no_err'.
3226 * ADDIP 4.1
3227 * A7) If an error response is received for a TLV parameter, all TLVs with no
3228 * response before the failed TLV are considered successful if not reported.
3229 * All TLVs after the failed response are considered unsuccessful unless a
3230 * specific success indication is present for the parameter.
3232 static __be16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
3233 sctp_addip_param_t *asconf_param,
3234 int no_err)
3236 sctp_addip_param_t *asconf_ack_param;
3237 sctp_errhdr_t *err_param;
3238 int length;
3239 int asconf_ack_len;
3240 __be16 err_code;
3242 if (no_err)
3243 err_code = SCTP_ERROR_NO_ERROR;
3244 else
3245 err_code = SCTP_ERROR_REQ_REFUSED;
3247 asconf_ack_len = ntohs(asconf_ack->chunk_hdr->length) -
3248 sizeof(sctp_chunkhdr_t);
3250 /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
3251 * the first asconf_ack parameter.
3253 length = sizeof(sctp_addiphdr_t);
3254 asconf_ack_param = (sctp_addip_param_t *)(asconf_ack->skb->data +
3255 length);
3256 asconf_ack_len -= length;
3258 while (asconf_ack_len > 0) {
3259 if (asconf_ack_param->crr_id == asconf_param->crr_id) {
3260 switch(asconf_ack_param->param_hdr.type) {
3261 case SCTP_PARAM_SUCCESS_REPORT:
3262 return SCTP_ERROR_NO_ERROR;
3263 case SCTP_PARAM_ERR_CAUSE:
3264 length = sizeof(sctp_addip_param_t);
3265 err_param = (sctp_errhdr_t *)
3266 ((void *)asconf_ack_param + length);
3267 asconf_ack_len -= length;
3268 if (asconf_ack_len > 0)
3269 return err_param->cause;
3270 else
3271 return SCTP_ERROR_INV_PARAM;
3272 break;
3273 default:
3274 return SCTP_ERROR_INV_PARAM;
3278 length = ntohs(asconf_ack_param->param_hdr.length);
3279 asconf_ack_param = (sctp_addip_param_t *)
3280 ((void *)asconf_ack_param + length);
3281 asconf_ack_len -= length;
3284 return err_code;
3287 /* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
3288 int sctp_process_asconf_ack(struct sctp_association *asoc,
3289 struct sctp_chunk *asconf_ack)
3291 struct sctp_chunk *asconf = asoc->addip_last_asconf;
3292 union sctp_addr_param *addr_param;
3293 sctp_addip_param_t *asconf_param;
3294 int length = 0;
3295 int asconf_len = asconf->skb->len;
3296 int all_param_pass = 0;
3297 int no_err = 1;
3298 int retval = 0;
3299 __be16 err_code = SCTP_ERROR_NO_ERROR;
3301 /* Skip the chunkhdr and addiphdr from the last asconf sent and store
3302 * a pointer to address parameter.
3304 length = sizeof(sctp_addip_chunk_t);
3305 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
3306 asconf_len -= length;
3308 /* Skip the address parameter in the last asconf sent and store a
3309 * pointer to the first asconf parameter.
3311 length = ntohs(addr_param->v4.param_hdr.length);
3312 asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
3313 asconf_len -= length;
3315 /* ADDIP 4.1
3316 * A8) If there is no response(s) to specific TLV parameter(s), and no
3317 * failures are indicated, then all request(s) are considered
3318 * successful.
3320 if (asconf_ack->skb->len == sizeof(sctp_addiphdr_t))
3321 all_param_pass = 1;
3323 /* Process the TLVs contained in the last sent ASCONF chunk. */
3324 while (asconf_len > 0) {
3325 if (all_param_pass)
3326 err_code = SCTP_ERROR_NO_ERROR;
3327 else {
3328 err_code = sctp_get_asconf_response(asconf_ack,
3329 asconf_param,
3330 no_err);
3331 if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
3332 no_err = 0;
3335 switch (err_code) {
3336 case SCTP_ERROR_NO_ERROR:
3337 sctp_asconf_param_success(asoc, asconf_param);
3338 break;
3340 case SCTP_ERROR_RSRC_LOW:
3341 retval = 1;
3342 break;
3344 case SCTP_ERROR_UNKNOWN_PARAM:
3345 /* Disable sending this type of asconf parameter in
3346 * future.
3348 asoc->peer.addip_disabled_mask |=
3349 asconf_param->param_hdr.type;
3350 break;
3352 case SCTP_ERROR_REQ_REFUSED:
3353 case SCTP_ERROR_DEL_LAST_IP:
3354 case SCTP_ERROR_DEL_SRC_IP:
3355 default:
3356 break;
3359 /* Skip the processed asconf parameter and move to the next
3360 * one.
3362 length = ntohs(asconf_param->param_hdr.length);
3363 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
3364 length);
3365 asconf_len -= length;
3368 /* Free the cached last sent asconf chunk. */
3369 list_del_init(&asconf->transmitted_list);
3370 sctp_chunk_free(asconf);
3371 asoc->addip_last_asconf = NULL;
3373 return retval;
3376 /* Make a FWD TSN chunk. */
3377 struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
3378 __u32 new_cum_tsn, size_t nstreams,
3379 struct sctp_fwdtsn_skip *skiplist)
3381 struct sctp_chunk *retval = NULL;
3382 struct sctp_fwdtsn_chunk *ftsn_chunk;
3383 struct sctp_fwdtsn_hdr ftsn_hdr;
3384 struct sctp_fwdtsn_skip skip;
3385 size_t hint;
3386 int i;
3388 hint = (nstreams + 1) * sizeof(__u32);
3390 retval = sctp_make_chunk(asoc, SCTP_CID_FWD_TSN, 0, hint);
3392 if (!retval)
3393 return NULL;
3395 ftsn_chunk = (struct sctp_fwdtsn_chunk *)retval->subh.fwdtsn_hdr;
3397 ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
3398 retval->subh.fwdtsn_hdr =
3399 sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
3401 for (i = 0; i < nstreams; i++) {
3402 skip.stream = skiplist[i].stream;
3403 skip.ssn = skiplist[i].ssn;
3404 sctp_addto_chunk(retval, sizeof(skip), &skip);
3407 return retval;