[SCTP]: Implement the Supported Extensions Parameter
[linux-2.6/openmoko-kernel/knife-kernel.git] / net / sctp / sm_make_chunk.c
blob71cc204a9ea527a401f472ee65abc28b22760dd3
1 /* SCTP kernel reference Implementation
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2002 Intel Corp.
7 * This file is part of the SCTP kernel reference Implementation
9 * These functions work with the state functions in sctp_sm_statefuns.c
10 * to implement the state operations. These functions implement the
11 * steps which require modifying existing data structures.
13 * The SCTP reference implementation is free software;
14 * you can redistribute it and/or modify it under the terms of
15 * the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
17 * any later version.
19 * The SCTP reference implementation is distributed in the hope that it
20 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
21 * ************************
22 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 * See the GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with GNU CC; see the file COPYING. If not, write to
27 * the Free Software Foundation, 59 Temple Place - Suite 330,
28 * Boston, MA 02111-1307, USA.
30 * Please send any bug reports or fixes you make to the
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 <asm/scatterlist.h>
60 #include <linux/crypto.h>
61 #include <net/sock.h>
63 #include <linux/skbuff.h>
64 #include <linux/random.h> /* for get_random_bytes */
65 #include <net/sctp/sctp.h>
66 #include <net/sctp/sm.h>
68 SCTP_STATIC
69 struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
70 __u8 type, __u8 flags, int paylen);
71 static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
72 const struct sctp_association *asoc,
73 const struct sctp_chunk *init_chunk,
74 int *cookie_len,
75 const __u8 *raw_addrs, int addrs_len);
76 static int sctp_process_param(struct sctp_association *asoc,
77 union sctp_params param,
78 const union sctp_addr *peer_addr,
79 gfp_t gfp);
81 /* What was the inbound interface for this chunk? */
82 int sctp_chunk_iif(const struct sctp_chunk *chunk)
84 struct sctp_af *af;
85 int iif = 0;
87 af = sctp_get_af_specific(ipver2af(ip_hdr(chunk->skb)->version));
88 if (af)
89 iif = af->skb_iif(chunk->skb);
91 return iif;
94 /* RFC 2960 3.3.2 Initiation (INIT) (1)
96 * Note 2: The ECN capable field is reserved for future use of
97 * Explicit Congestion Notification.
99 static const struct sctp_paramhdr ecap_param = {
100 SCTP_PARAM_ECN_CAPABLE,
101 __constant_htons(sizeof(struct sctp_paramhdr)),
103 static const struct sctp_paramhdr prsctp_param = {
104 SCTP_PARAM_FWD_TSN_SUPPORT,
105 __constant_htons(sizeof(struct sctp_paramhdr)),
108 /* A helper to initialize to initialize an op error inside a
109 * provided chunk, as most cause codes will be embedded inside an
110 * abort chunk.
112 void sctp_init_cause(struct sctp_chunk *chunk, __be16 cause_code,
113 size_t paylen)
115 sctp_errhdr_t err;
116 __u16 len;
118 /* Cause code constants are now defined in network order. */
119 err.cause = cause_code;
120 len = sizeof(sctp_errhdr_t) + paylen;
121 err.length = htons(len);
122 chunk->subh.err_hdr = sctp_addto_chunk(chunk, sizeof(sctp_errhdr_t), &err);
125 /* 3.3.2 Initiation (INIT) (1)
127 * This chunk is used to initiate a SCTP association between two
128 * endpoints. The format of the INIT chunk is shown below:
130 * 0 1 2 3
131 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
132 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
133 * | Type = 1 | Chunk Flags | Chunk Length |
134 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
135 * | Initiate Tag |
136 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
137 * | Advertised Receiver Window Credit (a_rwnd) |
138 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
139 * | Number of Outbound Streams | Number of Inbound Streams |
140 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
141 * | Initial TSN |
142 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
143 * \ \
144 * / Optional/Variable-Length Parameters /
145 * \ \
146 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
149 * The INIT chunk contains the following parameters. Unless otherwise
150 * noted, each parameter MUST only be included once in the INIT chunk.
152 * Fixed Parameters Status
153 * ----------------------------------------------
154 * Initiate Tag Mandatory
155 * Advertised Receiver Window Credit Mandatory
156 * Number of Outbound Streams Mandatory
157 * Number of Inbound Streams Mandatory
158 * Initial TSN Mandatory
160 * Variable Parameters Status Type Value
161 * -------------------------------------------------------------
162 * IPv4 Address (Note 1) Optional 5
163 * IPv6 Address (Note 1) Optional 6
164 * Cookie Preservative Optional 9
165 * Reserved for ECN Capable (Note 2) Optional 32768 (0x8000)
166 * Host Name Address (Note 3) Optional 11
167 * Supported Address Types (Note 4) Optional 12
169 struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
170 const struct sctp_bind_addr *bp,
171 gfp_t gfp, int vparam_len)
173 sctp_inithdr_t init;
174 union sctp_params addrs;
175 size_t chunksize;
176 struct sctp_chunk *retval = NULL;
177 int num_types, addrs_len = 0;
178 struct sctp_sock *sp;
179 sctp_supported_addrs_param_t sat;
180 __be16 types[2];
181 sctp_adaptation_ind_param_t aiparam;
182 sctp_supported_ext_param_t ext_param;
183 int num_ext = 0;
184 __u8 extensions[3];
186 /* RFC 2960 3.3.2 Initiation (INIT) (1)
188 * Note 1: The INIT chunks can contain multiple addresses that
189 * can be IPv4 and/or IPv6 in any combination.
191 retval = NULL;
193 /* Convert the provided bind address list to raw format. */
194 addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, gfp);
196 init.init_tag = htonl(asoc->c.my_vtag);
197 init.a_rwnd = htonl(asoc->rwnd);
198 init.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
199 init.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
200 init.initial_tsn = htonl(asoc->c.initial_tsn);
202 /* How many address types are needed? */
203 sp = sctp_sk(asoc->base.sk);
204 num_types = sp->pf->supported_addrs(sp, types);
206 chunksize = sizeof(init) + addrs_len + SCTP_SAT_LEN(num_types);
207 chunksize += sizeof(ecap_param);
208 if (sctp_prsctp_enable) {
209 chunksize += sizeof(prsctp_param);
210 extensions[num_ext] = SCTP_CID_FWD_TSN;
211 num_ext += 1;
213 /* ADDIP: Section 4.2.7:
214 * An implementation supporting this extension [ADDIP] MUST list
215 * the ASCONF,the ASCONF-ACK, and the AUTH chunks in its INIT and
216 * INIT-ACK parameters.
217 * XXX: We don't support AUTH just yet, so don't list it. AUTH
218 * support should add it.
220 if (sctp_addip_enable) {
221 extensions[num_ext] = SCTP_CID_ASCONF;
222 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
223 num_ext += 2;
226 chunksize += sizeof(aiparam);
227 chunksize += vparam_len;
229 /* If we have any extensions to report, account for that */
230 if (num_ext)
231 chunksize += sizeof(sctp_supported_ext_param_t) + num_ext;
233 /* RFC 2960 3.3.2 Initiation (INIT) (1)
235 * Note 3: An INIT chunk MUST NOT contain more than one Host
236 * Name address parameter. Moreover, the sender of the INIT
237 * MUST NOT combine any other address types with the Host Name
238 * address in the INIT. The receiver of INIT MUST ignore any
239 * other address types if the Host Name address parameter is
240 * present in the received INIT chunk.
242 * PLEASE DO NOT FIXME [This version does not support Host Name.]
245 retval = sctp_make_chunk(asoc, SCTP_CID_INIT, 0, chunksize);
246 if (!retval)
247 goto nodata;
249 retval->subh.init_hdr =
250 sctp_addto_chunk(retval, sizeof(init), &init);
251 retval->param_hdr.v =
252 sctp_addto_chunk(retval, addrs_len, addrs.v);
254 /* RFC 2960 3.3.2 Initiation (INIT) (1)
256 * Note 4: This parameter, when present, specifies all the
257 * address types the sending endpoint can support. The absence
258 * of this parameter indicates that the sending endpoint can
259 * support any address type.
261 sat.param_hdr.type = SCTP_PARAM_SUPPORTED_ADDRESS_TYPES;
262 sat.param_hdr.length = htons(SCTP_SAT_LEN(num_types));
263 sctp_addto_chunk(retval, sizeof(sat), &sat);
264 sctp_addto_chunk(retval, num_types * sizeof(__u16), &types);
266 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
268 /* Add the supported extensions paramter. Be nice and add this
269 * fist before addiding the parameters for the extensions themselves
271 if (num_ext) {
272 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
273 ext_param.param_hdr.length =
274 htons(sizeof(sctp_supported_ext_param_t) + num_ext);
275 sctp_addto_chunk(retval, sizeof(sctp_supported_ext_param_t),
276 &ext_param);
277 sctp_addto_chunk(retval, num_ext, extensions);
280 if (sctp_prsctp_enable)
281 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
283 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
284 aiparam.param_hdr.length = htons(sizeof(aiparam));
285 aiparam.adaptation_ind = htonl(sp->adaptation_ind);
286 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
288 nodata:
289 kfree(addrs.v);
290 return retval;
293 struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
294 const struct sctp_chunk *chunk,
295 gfp_t gfp, int unkparam_len)
297 sctp_inithdr_t initack;
298 struct sctp_chunk *retval;
299 union sctp_params addrs;
300 int addrs_len;
301 sctp_cookie_param_t *cookie;
302 int cookie_len;
303 size_t chunksize;
304 sctp_adaptation_ind_param_t aiparam;
305 sctp_supported_ext_param_t ext_param;
306 int num_ext = 0;
307 __u8 extensions[3];
309 retval = NULL;
311 /* Note: there may be no addresses to embed. */
312 addrs = sctp_bind_addrs_to_raw(&asoc->base.bind_addr, &addrs_len, gfp);
314 initack.init_tag = htonl(asoc->c.my_vtag);
315 initack.a_rwnd = htonl(asoc->rwnd);
316 initack.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
317 initack.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
318 initack.initial_tsn = htonl(asoc->c.initial_tsn);
320 /* FIXME: We really ought to build the cookie right
321 * into the packet instead of allocating more fresh memory.
323 cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
324 addrs.v, addrs_len);
325 if (!cookie)
326 goto nomem_cookie;
328 /* Calculate the total size of allocation, include the reserved
329 * space for reporting unknown parameters if it is specified.
331 chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;
333 /* Tell peer that we'll do ECN only if peer advertised such cap. */
334 if (asoc->peer.ecn_capable)
335 chunksize += sizeof(ecap_param);
337 /* Tell peer that we'll do PR-SCTP only if peer advertised. */
338 if (asoc->peer.prsctp_capable) {
339 chunksize += sizeof(prsctp_param);
340 extensions[num_ext] = SCTP_CID_FWD_TSN;
341 num_ext += 1;
344 if (sctp_addip_enable) {
345 extensions[num_ext] = SCTP_CID_ASCONF;
346 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
347 num_ext += 2;
350 chunksize += sizeof(ext_param) + num_ext;
351 chunksize += sizeof(aiparam);
353 /* Now allocate and fill out the chunk. */
354 retval = sctp_make_chunk(asoc, SCTP_CID_INIT_ACK, 0, chunksize);
355 if (!retval)
356 goto nomem_chunk;
358 /* Per the advice in RFC 2960 6.4, send this reply to
359 * the source of the INIT packet.
361 retval->transport = chunk->transport;
362 retval->subh.init_hdr =
363 sctp_addto_chunk(retval, sizeof(initack), &initack);
364 retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
365 sctp_addto_chunk(retval, cookie_len, cookie);
366 if (asoc->peer.ecn_capable)
367 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
368 if (num_ext) {
369 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
370 ext_param.param_hdr.length =
371 htons(sizeof(sctp_supported_ext_param_t) + num_ext);
372 sctp_addto_chunk(retval, sizeof(sctp_supported_ext_param_t),
373 &ext_param);
374 sctp_addto_chunk(retval, num_ext, extensions);
376 if (asoc->peer.prsctp_capable)
377 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
379 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
380 aiparam.param_hdr.length = htons(sizeof(aiparam));
381 aiparam.adaptation_ind = htonl(sctp_sk(asoc->base.sk)->adaptation_ind);
382 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
384 /* We need to remove the const qualifier at this point. */
385 retval->asoc = (struct sctp_association *) asoc;
387 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
389 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
390 * HEARTBEAT ACK, * etc.) to the same destination transport
391 * address from which it received the DATA or control chunk
392 * to which it is replying.
394 * [INIT ACK back to where the INIT came from.]
396 if (chunk)
397 retval->transport = chunk->transport;
399 nomem_chunk:
400 kfree(cookie);
401 nomem_cookie:
402 kfree(addrs.v);
403 return retval;
406 /* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
408 * This chunk is used only during the initialization of an association.
409 * It is sent by the initiator of an association to its peer to complete
410 * the initialization process. This chunk MUST precede any DATA chunk
411 * sent within the association, but MAY be bundled with one or more DATA
412 * chunks in the same packet.
414 * 0 1 2 3
415 * 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
416 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
417 * | Type = 10 |Chunk Flags | Length |
418 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
419 * / Cookie /
420 * \ \
421 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
423 * Chunk Flags: 8 bit
425 * Set to zero on transmit and ignored on receipt.
427 * Length: 16 bits (unsigned integer)
429 * Set to the size of the chunk in bytes, including the 4 bytes of
430 * the chunk header and the size of the Cookie.
432 * Cookie: variable size
434 * This field must contain the exact cookie received in the
435 * State Cookie parameter from the previous INIT ACK.
437 * An implementation SHOULD make the cookie as small as possible
438 * to insure interoperability.
440 struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
441 const struct sctp_chunk *chunk)
443 struct sctp_chunk *retval;
444 void *cookie;
445 int cookie_len;
447 cookie = asoc->peer.cookie;
448 cookie_len = asoc->peer.cookie_len;
450 /* Build a cookie echo chunk. */
451 retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ECHO, 0, cookie_len);
452 if (!retval)
453 goto nodata;
454 retval->subh.cookie_hdr =
455 sctp_addto_chunk(retval, cookie_len, cookie);
457 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
459 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
460 * HEARTBEAT ACK, * etc.) to the same destination transport
461 * address from which it * received the DATA or control chunk
462 * to which it is replying.
464 * [COOKIE ECHO back to where the INIT ACK came from.]
466 if (chunk)
467 retval->transport = chunk->transport;
469 nodata:
470 return retval;
473 /* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
475 * This chunk is used only during the initialization of an
476 * association. It is used to acknowledge the receipt of a COOKIE
477 * ECHO chunk. This chunk MUST precede any DATA or SACK chunk sent
478 * within the association, but MAY be bundled with one or more DATA
479 * chunks or SACK chunk in the same SCTP packet.
481 * 0 1 2 3
482 * 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
483 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
484 * | Type = 11 |Chunk Flags | Length = 4 |
485 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
487 * Chunk Flags: 8 bits
489 * Set to zero on transmit and ignored on receipt.
491 struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
492 const struct sctp_chunk *chunk)
494 struct sctp_chunk *retval;
496 retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ACK, 0, 0);
498 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
500 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
501 * HEARTBEAT ACK, * etc.) to the same destination transport
502 * address from which it * received the DATA or control chunk
503 * to which it is replying.
505 * [COOKIE ACK back to where the COOKIE ECHO came from.]
507 if (retval && chunk)
508 retval->transport = chunk->transport;
510 return retval;
514 * Appendix A: Explicit Congestion Notification:
515 * CWR:
517 * RFC 2481 details a specific bit for a sender to send in the header of
518 * its next outbound TCP segment to indicate to its peer that it has
519 * reduced its congestion window. This is termed the CWR bit. For
520 * SCTP the same indication is made by including the CWR chunk.
521 * This chunk contains one data element, i.e. the TSN number that
522 * was sent in the ECNE chunk. This element represents the lowest
523 * TSN number in the datagram that was originally marked with the
524 * CE bit.
526 * 0 1 2 3
527 * 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
528 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
529 * | Chunk Type=13 | Flags=00000000| Chunk Length = 8 |
530 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
531 * | Lowest TSN Number |
532 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
534 * Note: The CWR is considered a Control chunk.
536 struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
537 const __u32 lowest_tsn,
538 const struct sctp_chunk *chunk)
540 struct sctp_chunk *retval;
541 sctp_cwrhdr_t cwr;
543 cwr.lowest_tsn = htonl(lowest_tsn);
544 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_CWR, 0,
545 sizeof(sctp_cwrhdr_t));
547 if (!retval)
548 goto nodata;
550 retval->subh.ecn_cwr_hdr =
551 sctp_addto_chunk(retval, sizeof(cwr), &cwr);
553 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
555 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
556 * HEARTBEAT ACK, * etc.) to the same destination transport
557 * address from which it * received the DATA or control chunk
558 * to which it is replying.
560 * [Report a reduced congestion window back to where the ECNE
561 * came from.]
563 if (chunk)
564 retval->transport = chunk->transport;
566 nodata:
567 return retval;
570 /* Make an ECNE chunk. This is a congestion experienced report. */
571 struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
572 const __u32 lowest_tsn)
574 struct sctp_chunk *retval;
575 sctp_ecnehdr_t ecne;
577 ecne.lowest_tsn = htonl(lowest_tsn);
578 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_ECNE, 0,
579 sizeof(sctp_ecnehdr_t));
580 if (!retval)
581 goto nodata;
582 retval->subh.ecne_hdr =
583 sctp_addto_chunk(retval, sizeof(ecne), &ecne);
585 nodata:
586 return retval;
589 /* Make a DATA chunk for the given association from the provided
590 * parameters. However, do not populate the data payload.
592 struct sctp_chunk *sctp_make_datafrag_empty(struct sctp_association *asoc,
593 const struct sctp_sndrcvinfo *sinfo,
594 int data_len, __u8 flags, __u16 ssn)
596 struct sctp_chunk *retval;
597 struct sctp_datahdr dp;
598 int chunk_len;
600 /* We assign the TSN as LATE as possible, not here when
601 * creating the chunk.
603 dp.tsn = 0;
604 dp.stream = htons(sinfo->sinfo_stream);
605 dp.ppid = sinfo->sinfo_ppid;
607 /* Set the flags for an unordered send. */
608 if (sinfo->sinfo_flags & SCTP_UNORDERED) {
609 flags |= SCTP_DATA_UNORDERED;
610 dp.ssn = 0;
611 } else
612 dp.ssn = htons(ssn);
614 chunk_len = sizeof(dp) + data_len;
615 retval = sctp_make_chunk(asoc, SCTP_CID_DATA, flags, chunk_len);
616 if (!retval)
617 goto nodata;
619 retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
620 memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
622 nodata:
623 return retval;
626 /* Create a selective ackowledgement (SACK) for the given
627 * association. This reports on which TSN's we've seen to date,
628 * including duplicates and gaps.
630 struct sctp_chunk *sctp_make_sack(const struct sctp_association *asoc)
632 struct sctp_chunk *retval;
633 struct sctp_sackhdr sack;
634 int len;
635 __u32 ctsn;
636 __u16 num_gabs, num_dup_tsns;
637 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
639 ctsn = sctp_tsnmap_get_ctsn(map);
640 SCTP_DEBUG_PRINTK("sackCTSNAck sent: 0x%x.\n", ctsn);
642 /* How much room is needed in the chunk? */
643 num_gabs = sctp_tsnmap_num_gabs(map);
644 num_dup_tsns = sctp_tsnmap_num_dups(map);
646 /* Initialize the SACK header. */
647 sack.cum_tsn_ack = htonl(ctsn);
648 sack.a_rwnd = htonl(asoc->a_rwnd);
649 sack.num_gap_ack_blocks = htons(num_gabs);
650 sack.num_dup_tsns = htons(num_dup_tsns);
652 len = sizeof(sack)
653 + sizeof(struct sctp_gap_ack_block) * num_gabs
654 + sizeof(__u32) * num_dup_tsns;
656 /* Create the chunk. */
657 retval = sctp_make_chunk(asoc, SCTP_CID_SACK, 0, len);
658 if (!retval)
659 goto nodata;
661 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
663 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
664 * HEARTBEAT ACK, etc.) to the same destination transport
665 * address from which it received the DATA or control chunk to
666 * which it is replying. This rule should also be followed if
667 * the endpoint is bundling DATA chunks together with the
668 * reply chunk.
670 * However, when acknowledging multiple DATA chunks received
671 * in packets from different source addresses in a single
672 * SACK, the SACK chunk may be transmitted to one of the
673 * destination transport addresses from which the DATA or
674 * control chunks being acknowledged were received.
676 * [BUG: We do not implement the following paragraph.
677 * Perhaps we should remember the last transport we used for a
678 * SACK and avoid that (if possible) if we have seen any
679 * duplicates. --piggy]
681 * When a receiver of a duplicate DATA chunk sends a SACK to a
682 * multi- homed endpoint it MAY be beneficial to vary the
683 * destination address and not use the source address of the
684 * DATA chunk. The reason being that receiving a duplicate
685 * from a multi-homed endpoint might indicate that the return
686 * path (as specified in the source address of the DATA chunk)
687 * for the SACK is broken.
689 * [Send to the address from which we last received a DATA chunk.]
691 retval->transport = asoc->peer.last_data_from;
693 retval->subh.sack_hdr =
694 sctp_addto_chunk(retval, sizeof(sack), &sack);
696 /* Add the gap ack block information. */
697 if (num_gabs)
698 sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
699 sctp_tsnmap_get_gabs(map));
701 /* Add the duplicate TSN information. */
702 if (num_dup_tsns)
703 sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
704 sctp_tsnmap_get_dups(map));
706 nodata:
707 return retval;
710 /* Make a SHUTDOWN chunk. */
711 struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
712 const struct sctp_chunk *chunk)
714 struct sctp_chunk *retval;
715 sctp_shutdownhdr_t shut;
716 __u32 ctsn;
718 ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
719 shut.cum_tsn_ack = htonl(ctsn);
721 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN, 0,
722 sizeof(sctp_shutdownhdr_t));
723 if (!retval)
724 goto nodata;
726 retval->subh.shutdown_hdr =
727 sctp_addto_chunk(retval, sizeof(shut), &shut);
729 if (chunk)
730 retval->transport = chunk->transport;
731 nodata:
732 return retval;
735 struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
736 const struct sctp_chunk *chunk)
738 struct sctp_chunk *retval;
740 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0);
742 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
744 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
745 * HEARTBEAT ACK, * etc.) to the same destination transport
746 * address from which it * received the DATA or control chunk
747 * to which it is replying.
749 * [ACK back to where the SHUTDOWN came from.]
751 if (retval && chunk)
752 retval->transport = chunk->transport;
754 return retval;
757 struct sctp_chunk *sctp_make_shutdown_complete(
758 const struct sctp_association *asoc,
759 const struct sctp_chunk *chunk)
761 struct sctp_chunk *retval;
762 __u8 flags = 0;
764 /* Set the T-bit if we have no association (vtag will be
765 * reflected)
767 flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
769 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags, 0);
771 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
773 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
774 * HEARTBEAT ACK, * etc.) to the same destination transport
775 * address from which it * received the DATA or control chunk
776 * to which it is replying.
778 * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
779 * came from.]
781 if (retval && chunk)
782 retval->transport = chunk->transport;
784 return retval;
787 /* Create an ABORT. Note that we set the T bit if we have no
788 * association, except when responding to an INIT (sctpimpguide 2.41).
790 struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
791 const struct sctp_chunk *chunk,
792 const size_t hint)
794 struct sctp_chunk *retval;
795 __u8 flags = 0;
797 /* Set the T-bit if we have no association and 'chunk' is not
798 * an INIT (vtag will be reflected).
800 if (!asoc) {
801 if (chunk && chunk->chunk_hdr &&
802 chunk->chunk_hdr->type == SCTP_CID_INIT)
803 flags = 0;
804 else
805 flags = SCTP_CHUNK_FLAG_T;
808 retval = sctp_make_chunk(asoc, SCTP_CID_ABORT, flags, hint);
810 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
812 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
813 * HEARTBEAT ACK, * etc.) to the same destination transport
814 * address from which it * received the DATA or control chunk
815 * to which it is replying.
817 * [ABORT back to where the offender came from.]
819 if (retval && chunk)
820 retval->transport = chunk->transport;
822 return retval;
825 /* Helper to create ABORT with a NO_USER_DATA error. */
826 struct sctp_chunk *sctp_make_abort_no_data(
827 const struct sctp_association *asoc,
828 const struct sctp_chunk *chunk, __u32 tsn)
830 struct sctp_chunk *retval;
831 __be32 payload;
833 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t)
834 + sizeof(tsn));
836 if (!retval)
837 goto no_mem;
839 /* Put the tsn back into network byte order. */
840 payload = htonl(tsn);
841 sctp_init_cause(retval, SCTP_ERROR_NO_DATA, sizeof(payload));
842 sctp_addto_chunk(retval, sizeof(payload), (const void *)&payload);
844 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
846 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
847 * HEARTBEAT ACK, * etc.) to the same destination transport
848 * address from which it * received the DATA or control chunk
849 * to which it is replying.
851 * [ABORT back to where the offender came from.]
853 if (chunk)
854 retval->transport = chunk->transport;
856 no_mem:
857 return retval;
860 /* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error. */
861 struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
862 const struct msghdr *msg,
863 size_t paylen)
865 struct sctp_chunk *retval;
866 void *payload = NULL;
867 int err;
869 retval = sctp_make_abort(asoc, NULL, sizeof(sctp_errhdr_t) + paylen);
870 if (!retval)
871 goto err_chunk;
873 if (paylen) {
874 /* Put the msg_iov together into payload. */
875 payload = kmalloc(paylen, GFP_KERNEL);
876 if (!payload)
877 goto err_payload;
879 err = memcpy_fromiovec(payload, msg->msg_iov, paylen);
880 if (err < 0)
881 goto err_copy;
884 sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, paylen);
885 sctp_addto_chunk(retval, paylen, payload);
887 if (paylen)
888 kfree(payload);
890 return retval;
892 err_copy:
893 kfree(payload);
894 err_payload:
895 sctp_chunk_free(retval);
896 retval = NULL;
897 err_chunk:
898 return retval;
901 /* Append bytes to the end of a parameter. Will panic if chunk is not big
902 * enough.
904 static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
905 const void *data)
907 void *target;
908 int chunklen = ntohs(chunk->chunk_hdr->length);
910 target = skb_put(chunk->skb, len);
912 memcpy(target, data, len);
914 /* Adjust the chunk length field. */
915 chunk->chunk_hdr->length = htons(chunklen + len);
916 chunk->chunk_end = skb_tail_pointer(chunk->skb);
918 return target;
921 /* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
922 struct sctp_chunk *sctp_make_abort_violation(
923 const struct sctp_association *asoc,
924 const struct sctp_chunk *chunk,
925 const __u8 *payload,
926 const size_t paylen)
928 struct sctp_chunk *retval;
929 struct sctp_paramhdr phdr;
931 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t) + paylen
932 + sizeof(sctp_paramhdr_t));
933 if (!retval)
934 goto end;
936 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, paylen
937 + sizeof(sctp_paramhdr_t));
939 phdr.type = htons(chunk->chunk_hdr->type);
940 phdr.length = chunk->chunk_hdr->length;
941 sctp_addto_chunk(retval, paylen, payload);
942 sctp_addto_param(retval, sizeof(sctp_paramhdr_t), &phdr);
944 end:
945 return retval;
948 /* Make a HEARTBEAT chunk. */
949 struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
950 const struct sctp_transport *transport,
951 const void *payload, const size_t paylen)
953 struct sctp_chunk *retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT,
954 0, paylen);
956 if (!retval)
957 goto nodata;
959 /* Cast away the 'const', as this is just telling the chunk
960 * what transport it belongs to.
962 retval->transport = (struct sctp_transport *) transport;
963 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
965 nodata:
966 return retval;
969 struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
970 const struct sctp_chunk *chunk,
971 const void *payload, const size_t paylen)
973 struct sctp_chunk *retval;
975 retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen);
976 if (!retval)
977 goto nodata;
979 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
981 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
983 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
984 * HEARTBEAT ACK, * etc.) to the same destination transport
985 * address from which it * received the DATA or control chunk
986 * to which it is replying.
988 * [HBACK back to where the HEARTBEAT came from.]
990 if (chunk)
991 retval->transport = chunk->transport;
993 nodata:
994 return retval;
997 /* Create an Operation Error chunk with the specified space reserved.
998 * This routine can be used for containing multiple causes in the chunk.
1000 static struct sctp_chunk *sctp_make_op_error_space(
1001 const struct sctp_association *asoc,
1002 const struct sctp_chunk *chunk,
1003 size_t size)
1005 struct sctp_chunk *retval;
1007 retval = sctp_make_chunk(asoc, SCTP_CID_ERROR, 0,
1008 sizeof(sctp_errhdr_t) + size);
1009 if (!retval)
1010 goto nodata;
1012 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1014 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1015 * HEARTBEAT ACK, etc.) to the same destination transport
1016 * address from which it received the DATA or control chunk
1017 * to which it is replying.
1020 if (chunk)
1021 retval->transport = chunk->transport;
1023 nodata:
1024 return retval;
1027 /* Create an Operation Error chunk. */
1028 struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
1029 const struct sctp_chunk *chunk,
1030 __be16 cause_code, const void *payload,
1031 size_t paylen)
1033 struct sctp_chunk *retval;
1035 retval = sctp_make_op_error_space(asoc, chunk, paylen);
1036 if (!retval)
1037 goto nodata;
1039 sctp_init_cause(retval, cause_code, paylen);
1040 sctp_addto_chunk(retval, paylen, payload);
1042 nodata:
1043 return retval;
1046 /********************************************************************
1047 * 2nd Level Abstractions
1048 ********************************************************************/
1050 /* Turn an skb into a chunk.
1051 * FIXME: Eventually move the structure directly inside the skb->cb[].
1053 struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
1054 const struct sctp_association *asoc,
1055 struct sock *sk)
1057 struct sctp_chunk *retval;
1059 retval = kmem_cache_zalloc(sctp_chunk_cachep, GFP_ATOMIC);
1061 if (!retval)
1062 goto nodata;
1064 if (!sk) {
1065 SCTP_DEBUG_PRINTK("chunkifying skb %p w/o an sk\n", skb);
1068 INIT_LIST_HEAD(&retval->list);
1069 retval->skb = skb;
1070 retval->asoc = (struct sctp_association *)asoc;
1071 retval->resent = 0;
1072 retval->has_tsn = 0;
1073 retval->has_ssn = 0;
1074 retval->rtt_in_progress = 0;
1075 retval->sent_at = 0;
1076 retval->singleton = 1;
1077 retval->end_of_packet = 0;
1078 retval->ecn_ce_done = 0;
1079 retval->pdiscard = 0;
1081 /* sctpimpguide-05.txt Section 2.8.2
1082 * M1) Each time a new DATA chunk is transmitted
1083 * set the 'TSN.Missing.Report' count for that TSN to 0. The
1084 * 'TSN.Missing.Report' count will be used to determine missing chunks
1085 * and when to fast retransmit.
1087 retval->tsn_missing_report = 0;
1088 retval->tsn_gap_acked = 0;
1089 retval->fast_retransmit = 0;
1091 /* If this is a fragmented message, track all fragments
1092 * of the message (for SEND_FAILED).
1094 retval->msg = NULL;
1096 /* Polish the bead hole. */
1097 INIT_LIST_HEAD(&retval->transmitted_list);
1098 INIT_LIST_HEAD(&retval->frag_list);
1099 SCTP_DBG_OBJCNT_INC(chunk);
1100 atomic_set(&retval->refcnt, 1);
1102 nodata:
1103 return retval;
1106 /* Set chunk->source and dest based on the IP header in chunk->skb. */
1107 void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
1108 union sctp_addr *dest)
1110 memcpy(&chunk->source, src, sizeof(union sctp_addr));
1111 memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
1114 /* Extract the source address from a chunk. */
1115 const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
1117 /* If we have a known transport, use that. */
1118 if (chunk->transport) {
1119 return &chunk->transport->ipaddr;
1120 } else {
1121 /* Otherwise, extract it from the IP header. */
1122 return &chunk->source;
1126 /* Create a new chunk, setting the type and flags headers from the
1127 * arguments, reserving enough space for a 'paylen' byte payload.
1129 SCTP_STATIC
1130 struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
1131 __u8 type, __u8 flags, int paylen)
1133 struct sctp_chunk *retval;
1134 sctp_chunkhdr_t *chunk_hdr;
1135 struct sk_buff *skb;
1136 struct sock *sk;
1138 /* No need to allocate LL here, as this is only a chunk. */
1139 skb = alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t) + paylen),
1140 GFP_ATOMIC);
1141 if (!skb)
1142 goto nodata;
1144 /* Make room for the chunk header. */
1145 chunk_hdr = (sctp_chunkhdr_t *)skb_put(skb, sizeof(sctp_chunkhdr_t));
1146 chunk_hdr->type = type;
1147 chunk_hdr->flags = flags;
1148 chunk_hdr->length = htons(sizeof(sctp_chunkhdr_t));
1150 sk = asoc ? asoc->base.sk : NULL;
1151 retval = sctp_chunkify(skb, asoc, sk);
1152 if (!retval) {
1153 kfree_skb(skb);
1154 goto nodata;
1157 retval->chunk_hdr = chunk_hdr;
1158 retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(struct sctp_chunkhdr);
1160 /* Set the skb to the belonging sock for accounting. */
1161 skb->sk = sk;
1163 return retval;
1164 nodata:
1165 return NULL;
1169 /* Release the memory occupied by a chunk. */
1170 static void sctp_chunk_destroy(struct sctp_chunk *chunk)
1172 /* Free the chunk skb data and the SCTP_chunk stub itself. */
1173 dev_kfree_skb(chunk->skb);
1175 SCTP_DBG_OBJCNT_DEC(chunk);
1176 kmem_cache_free(sctp_chunk_cachep, chunk);
1179 /* Possibly, free the chunk. */
1180 void sctp_chunk_free(struct sctp_chunk *chunk)
1182 BUG_ON(!list_empty(&chunk->list));
1183 list_del_init(&chunk->transmitted_list);
1185 /* Release our reference on the message tracker. */
1186 if (chunk->msg)
1187 sctp_datamsg_put(chunk->msg);
1189 sctp_chunk_put(chunk);
1192 /* Grab a reference to the chunk. */
1193 void sctp_chunk_hold(struct sctp_chunk *ch)
1195 atomic_inc(&ch->refcnt);
1198 /* Release a reference to the chunk. */
1199 void sctp_chunk_put(struct sctp_chunk *ch)
1201 if (atomic_dec_and_test(&ch->refcnt))
1202 sctp_chunk_destroy(ch);
1205 /* Append bytes to the end of a chunk. Will panic if chunk is not big
1206 * enough.
1208 void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
1210 void *target;
1211 void *padding;
1212 int chunklen = ntohs(chunk->chunk_hdr->length);
1213 int padlen = WORD_ROUND(chunklen) - chunklen;
1215 padding = skb_put(chunk->skb, padlen);
1216 target = skb_put(chunk->skb, len);
1218 memset(padding, 0, padlen);
1219 memcpy(target, data, len);
1221 /* Adjust the chunk length field. */
1222 chunk->chunk_hdr->length = htons(chunklen + padlen + len);
1223 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1225 return target;
1228 /* Append bytes from user space to the end of a chunk. Will panic if
1229 * chunk is not big enough.
1230 * Returns a kernel err value.
1232 int sctp_user_addto_chunk(struct sctp_chunk *chunk, int off, int len,
1233 struct iovec *data)
1235 __u8 *target;
1236 int err = 0;
1238 /* Make room in chunk for data. */
1239 target = skb_put(chunk->skb, len);
1241 /* Copy data (whole iovec) into chunk */
1242 if ((err = memcpy_fromiovecend(target, data, off, len)))
1243 goto out;
1245 /* Adjust the chunk length field. */
1246 chunk->chunk_hdr->length =
1247 htons(ntohs(chunk->chunk_hdr->length) + len);
1248 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1250 out:
1251 return err;
1254 /* Helper function to assign a TSN if needed. This assumes that both
1255 * the data_hdr and association have already been assigned.
1257 void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
1259 struct sctp_datamsg *msg;
1260 struct sctp_chunk *lchunk;
1261 struct sctp_stream *stream;
1262 __u16 ssn;
1263 __u16 sid;
1265 if (chunk->has_ssn)
1266 return;
1268 /* All fragments will be on the same stream */
1269 sid = ntohs(chunk->subh.data_hdr->stream);
1270 stream = &chunk->asoc->ssnmap->out;
1272 /* Now assign the sequence number to the entire message.
1273 * All fragments must have the same stream sequence number.
1275 msg = chunk->msg;
1276 list_for_each_entry(lchunk, &msg->chunks, frag_list) {
1277 if (lchunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
1278 ssn = 0;
1279 } else {
1280 if (lchunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
1281 ssn = sctp_ssn_next(stream, sid);
1282 else
1283 ssn = sctp_ssn_peek(stream, sid);
1286 lchunk->subh.data_hdr->ssn = htons(ssn);
1287 lchunk->has_ssn = 1;
1291 /* Helper function to assign a TSN if needed. This assumes that both
1292 * the data_hdr and association have already been assigned.
1294 void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
1296 if (!chunk->has_tsn) {
1297 /* This is the last possible instant to
1298 * assign a TSN.
1300 chunk->subh.data_hdr->tsn =
1301 htonl(sctp_association_get_next_tsn(chunk->asoc));
1302 chunk->has_tsn = 1;
1306 /* Create a CLOSED association to use with an incoming packet. */
1307 struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
1308 struct sctp_chunk *chunk,
1309 gfp_t gfp)
1311 struct sctp_association *asoc;
1312 struct sk_buff *skb;
1313 sctp_scope_t scope;
1314 struct sctp_af *af;
1316 /* Create the bare association. */
1317 scope = sctp_scope(sctp_source(chunk));
1318 asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
1319 if (!asoc)
1320 goto nodata;
1321 asoc->temp = 1;
1322 skb = chunk->skb;
1323 /* Create an entry for the source address of the packet. */
1324 af = sctp_get_af_specific(ipver2af(ip_hdr(skb)->version));
1325 if (unlikely(!af))
1326 goto fail;
1327 af->from_skb(&asoc->c.peer_addr, skb, 1);
1328 nodata:
1329 return asoc;
1331 fail:
1332 sctp_association_free(asoc);
1333 return NULL;
1336 /* Build a cookie representing asoc.
1337 * This INCLUDES the param header needed to put the cookie in the INIT ACK.
1339 static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
1340 const struct sctp_association *asoc,
1341 const struct sctp_chunk *init_chunk,
1342 int *cookie_len,
1343 const __u8 *raw_addrs, int addrs_len)
1345 sctp_cookie_param_t *retval;
1346 struct sctp_signed_cookie *cookie;
1347 struct scatterlist sg;
1348 int headersize, bodysize;
1349 unsigned int keylen;
1350 char *key;
1352 /* Header size is static data prior to the actual cookie, including
1353 * any padding.
1355 headersize = sizeof(sctp_paramhdr_t) +
1356 (sizeof(struct sctp_signed_cookie) -
1357 sizeof(struct sctp_cookie));
1358 bodysize = sizeof(struct sctp_cookie)
1359 + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
1361 /* Pad out the cookie to a multiple to make the signature
1362 * functions simpler to write.
1364 if (bodysize % SCTP_COOKIE_MULTIPLE)
1365 bodysize += SCTP_COOKIE_MULTIPLE
1366 - (bodysize % SCTP_COOKIE_MULTIPLE);
1367 *cookie_len = headersize + bodysize;
1369 /* Clear this memory since we are sending this data structure
1370 * out on the network.
1372 retval = kzalloc(*cookie_len, GFP_ATOMIC);
1373 if (!retval)
1374 goto nodata;
1376 cookie = (struct sctp_signed_cookie *) retval->body;
1378 /* Set up the parameter header. */
1379 retval->p.type = SCTP_PARAM_STATE_COOKIE;
1380 retval->p.length = htons(*cookie_len);
1382 /* Copy the cookie part of the association itself. */
1383 cookie->c = asoc->c;
1384 /* Save the raw address list length in the cookie. */
1385 cookie->c.raw_addr_list_len = addrs_len;
1387 /* Remember PR-SCTP capability. */
1388 cookie->c.prsctp_capable = asoc->peer.prsctp_capable;
1390 /* Save adaptation indication in the cookie. */
1391 cookie->c.adaptation_ind = asoc->peer.adaptation_ind;
1393 /* Set an expiration time for the cookie. */
1394 do_gettimeofday(&cookie->c.expiration);
1395 TIMEVAL_ADD(asoc->cookie_life, cookie->c.expiration);
1397 /* Copy the peer's init packet. */
1398 memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
1399 ntohs(init_chunk->chunk_hdr->length));
1401 /* Copy the raw local address list of the association. */
1402 memcpy((__u8 *)&cookie->c.peer_init[0] +
1403 ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
1405 if (sctp_sk(ep->base.sk)->hmac) {
1406 struct hash_desc desc;
1408 /* Sign the message. */
1409 sg.page = virt_to_page(&cookie->c);
1410 sg.offset = (unsigned long)(&cookie->c) % PAGE_SIZE;
1411 sg.length = bodysize;
1412 keylen = SCTP_SECRET_SIZE;
1413 key = (char *)ep->secret_key[ep->current_key];
1414 desc.tfm = sctp_sk(ep->base.sk)->hmac;
1415 desc.flags = 0;
1417 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1418 crypto_hash_digest(&desc, &sg, bodysize, cookie->signature))
1419 goto free_cookie;
1422 return retval;
1424 free_cookie:
1425 kfree(retval);
1426 nodata:
1427 *cookie_len = 0;
1428 return NULL;
1431 /* Unpack the cookie from COOKIE ECHO chunk, recreating the association. */
1432 struct sctp_association *sctp_unpack_cookie(
1433 const struct sctp_endpoint *ep,
1434 const struct sctp_association *asoc,
1435 struct sctp_chunk *chunk, gfp_t gfp,
1436 int *error, struct sctp_chunk **errp)
1438 struct sctp_association *retval = NULL;
1439 struct sctp_signed_cookie *cookie;
1440 struct sctp_cookie *bear_cookie;
1441 int headersize, bodysize, fixed_size;
1442 __u8 *digest = ep->digest;
1443 struct scatterlist sg;
1444 unsigned int keylen, len;
1445 char *key;
1446 sctp_scope_t scope;
1447 struct sk_buff *skb = chunk->skb;
1448 struct timeval tv;
1449 struct hash_desc desc;
1451 /* Header size is static data prior to the actual cookie, including
1452 * any padding.
1454 headersize = sizeof(sctp_chunkhdr_t) +
1455 (sizeof(struct sctp_signed_cookie) -
1456 sizeof(struct sctp_cookie));
1457 bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
1458 fixed_size = headersize + sizeof(struct sctp_cookie);
1460 /* Verify that the chunk looks like it even has a cookie.
1461 * There must be enough room for our cookie and our peer's
1462 * INIT chunk.
1464 len = ntohs(chunk->chunk_hdr->length);
1465 if (len < fixed_size + sizeof(struct sctp_chunkhdr))
1466 goto malformed;
1468 /* Verify that the cookie has been padded out. */
1469 if (bodysize % SCTP_COOKIE_MULTIPLE)
1470 goto malformed;
1472 /* Process the cookie. */
1473 cookie = chunk->subh.cookie_hdr;
1474 bear_cookie = &cookie->c;
1476 if (!sctp_sk(ep->base.sk)->hmac)
1477 goto no_hmac;
1479 /* Check the signature. */
1480 keylen = SCTP_SECRET_SIZE;
1481 sg.page = virt_to_page(bear_cookie);
1482 sg.offset = (unsigned long)(bear_cookie) % PAGE_SIZE;
1483 sg.length = bodysize;
1484 key = (char *)ep->secret_key[ep->current_key];
1485 desc.tfm = sctp_sk(ep->base.sk)->hmac;
1486 desc.flags = 0;
1488 memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
1489 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1490 crypto_hash_digest(&desc, &sg, bodysize, digest)) {
1491 *error = -SCTP_IERROR_NOMEM;
1492 goto fail;
1495 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1496 /* Try the previous key. */
1497 key = (char *)ep->secret_key[ep->last_key];
1498 memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
1499 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1500 crypto_hash_digest(&desc, &sg, bodysize, digest)) {
1501 *error = -SCTP_IERROR_NOMEM;
1502 goto fail;
1505 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1506 /* Yikes! Still bad signature! */
1507 *error = -SCTP_IERROR_BAD_SIG;
1508 goto fail;
1512 no_hmac:
1513 /* IG Section 2.35.2:
1514 * 3) Compare the port numbers and the verification tag contained
1515 * within the COOKIE ECHO chunk to the actual port numbers and the
1516 * verification tag within the SCTP common header of the received
1517 * packet. If these values do not match the packet MUST be silently
1518 * discarded,
1520 if (ntohl(chunk->sctp_hdr->vtag) != bear_cookie->my_vtag) {
1521 *error = -SCTP_IERROR_BAD_TAG;
1522 goto fail;
1525 if (chunk->sctp_hdr->source != bear_cookie->peer_addr.v4.sin_port ||
1526 ntohs(chunk->sctp_hdr->dest) != bear_cookie->my_port) {
1527 *error = -SCTP_IERROR_BAD_PORTS;
1528 goto fail;
1531 /* Check to see if the cookie is stale. If there is already
1532 * an association, there is no need to check cookie's expiration
1533 * for init collision case of lost COOKIE ACK.
1534 * If skb has been timestamped, then use the stamp, otherwise
1535 * use current time. This introduces a small possibility that
1536 * that a cookie may be considered expired, but his would only slow
1537 * down the new association establishment instead of every packet.
1539 if (sock_flag(ep->base.sk, SOCK_TIMESTAMP))
1540 skb_get_timestamp(skb, &tv);
1541 else
1542 do_gettimeofday(&tv);
1544 if (!asoc && tv_lt(bear_cookie->expiration, tv)) {
1546 * Section 3.3.10.3 Stale Cookie Error (3)
1548 * Cause of error
1549 * ---------------
1550 * Stale Cookie Error: Indicates the receipt of a valid State
1551 * Cookie that has expired.
1553 len = ntohs(chunk->chunk_hdr->length);
1554 *errp = sctp_make_op_error_space(asoc, chunk, len);
1555 if (*errp) {
1556 suseconds_t usecs = (tv.tv_sec -
1557 bear_cookie->expiration.tv_sec) * 1000000L +
1558 tv.tv_usec - bear_cookie->expiration.tv_usec;
1559 __be32 n = htonl(usecs);
1561 sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
1562 sizeof(n));
1563 sctp_addto_chunk(*errp, sizeof(n), &n);
1564 *error = -SCTP_IERROR_STALE_COOKIE;
1565 } else
1566 *error = -SCTP_IERROR_NOMEM;
1568 goto fail;
1571 /* Make a new base association. */
1572 scope = sctp_scope(sctp_source(chunk));
1573 retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
1574 if (!retval) {
1575 *error = -SCTP_IERROR_NOMEM;
1576 goto fail;
1579 /* Set up our peer's port number. */
1580 retval->peer.port = ntohs(chunk->sctp_hdr->source);
1582 /* Populate the association from the cookie. */
1583 memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
1585 if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
1586 GFP_ATOMIC) < 0) {
1587 *error = -SCTP_IERROR_NOMEM;
1588 goto fail;
1591 /* Also, add the destination address. */
1592 if (list_empty(&retval->base.bind_addr.address_list)) {
1593 sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest, 1,
1594 GFP_ATOMIC);
1597 retval->next_tsn = retval->c.initial_tsn;
1598 retval->ctsn_ack_point = retval->next_tsn - 1;
1599 retval->addip_serial = retval->c.initial_tsn;
1600 retval->adv_peer_ack_point = retval->ctsn_ack_point;
1601 retval->peer.prsctp_capable = retval->c.prsctp_capable;
1602 retval->peer.adaptation_ind = retval->c.adaptation_ind;
1604 /* The INIT stuff will be done by the side effects. */
1605 return retval;
1607 fail:
1608 if (retval)
1609 sctp_association_free(retval);
1611 return NULL;
1613 malformed:
1614 /* Yikes! The packet is either corrupt or deliberately
1615 * malformed.
1617 *error = -SCTP_IERROR_MALFORMED;
1618 goto fail;
1621 /********************************************************************
1622 * 3rd Level Abstractions
1623 ********************************************************************/
1625 struct __sctp_missing {
1626 __be32 num_missing;
1627 __be16 type;
1628 } __attribute__((packed));
1631 * Report a missing mandatory parameter.
1633 static int sctp_process_missing_param(const struct sctp_association *asoc,
1634 sctp_param_t paramtype,
1635 struct sctp_chunk *chunk,
1636 struct sctp_chunk **errp)
1638 struct __sctp_missing report;
1639 __u16 len;
1641 len = WORD_ROUND(sizeof(report));
1643 /* Make an ERROR chunk, preparing enough room for
1644 * returning multiple unknown parameters.
1646 if (!*errp)
1647 *errp = sctp_make_op_error_space(asoc, chunk, len);
1649 if (*errp) {
1650 report.num_missing = htonl(1);
1651 report.type = paramtype;
1652 sctp_init_cause(*errp, SCTP_ERROR_MISS_PARAM,
1653 sizeof(report));
1654 sctp_addto_chunk(*errp, sizeof(report), &report);
1657 /* Stop processing this chunk. */
1658 return 0;
1661 /* Report an Invalid Mandatory Parameter. */
1662 static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
1663 struct sctp_chunk *chunk,
1664 struct sctp_chunk **errp)
1666 /* Invalid Mandatory Parameter Error has no payload. */
1668 if (!*errp)
1669 *errp = sctp_make_op_error_space(asoc, chunk, 0);
1671 if (*errp)
1672 sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, 0);
1674 /* Stop processing this chunk. */
1675 return 0;
1678 static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
1679 struct sctp_paramhdr *param,
1680 const struct sctp_chunk *chunk,
1681 struct sctp_chunk **errp)
1683 char error[] = "The following parameter had invalid length:";
1684 size_t payload_len = WORD_ROUND(sizeof(error)) +
1685 sizeof(sctp_paramhdr_t);
1688 /* Create an error chunk and fill it in with our payload. */
1689 if (!*errp)
1690 *errp = sctp_make_op_error_space(asoc, chunk, payload_len);
1692 if (*errp) {
1693 sctp_init_cause(*errp, SCTP_ERROR_PROTO_VIOLATION,
1694 sizeof(error) + sizeof(sctp_paramhdr_t));
1695 sctp_addto_chunk(*errp, sizeof(error), error);
1696 sctp_addto_param(*errp, sizeof(sctp_paramhdr_t), param);
1699 return 0;
1703 /* Do not attempt to handle the HOST_NAME parm. However, do
1704 * send back an indicator to the peer.
1706 static int sctp_process_hn_param(const struct sctp_association *asoc,
1707 union sctp_params param,
1708 struct sctp_chunk *chunk,
1709 struct sctp_chunk **errp)
1711 __u16 len = ntohs(param.p->length);
1713 /* Make an ERROR chunk. */
1714 if (!*errp)
1715 *errp = sctp_make_op_error_space(asoc, chunk, len);
1717 if (*errp) {
1718 sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED, len);
1719 sctp_addto_chunk(*errp, len, param.v);
1722 /* Stop processing this chunk. */
1723 return 0;
1726 static void sctp_process_ext_param(struct sctp_association *asoc,
1727 union sctp_params param)
1729 __u16 num_ext = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
1730 int i;
1732 for (i = 0; i < num_ext; i++) {
1733 switch (param.ext->chunks[i]) {
1734 case SCTP_CID_FWD_TSN:
1735 if (sctp_prsctp_enable &&
1736 !asoc->peer.prsctp_capable)
1737 asoc->peer.prsctp_capable = 1;
1738 break;
1739 case SCTP_CID_ASCONF:
1740 case SCTP_CID_ASCONF_ACK:
1741 /* don't need to do anything for ASCONF */
1742 default:
1743 break;
1748 /* RFC 3.2.1 & the Implementers Guide 2.2.
1750 * The Parameter Types are encoded such that the
1751 * highest-order two bits specify the action that must be
1752 * taken if the processing endpoint does not recognize the
1753 * Parameter Type.
1755 * 00 - Stop processing this SCTP chunk and discard it,
1756 * do not process any further chunks within it.
1758 * 01 - Stop processing this SCTP chunk and discard it,
1759 * do not process any further chunks within it, and report
1760 * the unrecognized parameter in an 'Unrecognized
1761 * Parameter Type' (in either an ERROR or in the INIT ACK).
1763 * 10 - Skip this parameter and continue processing.
1765 * 11 - Skip this parameter and continue processing but
1766 * report the unrecognized parameter in an
1767 * 'Unrecognized Parameter Type' (in either an ERROR or in
1768 * the INIT ACK).
1770 * Return value:
1771 * 0 - discard the chunk
1772 * 1 - continue with the chunk
1774 static int sctp_process_unk_param(const struct sctp_association *asoc,
1775 union sctp_params param,
1776 struct sctp_chunk *chunk,
1777 struct sctp_chunk **errp)
1779 int retval = 1;
1781 switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
1782 case SCTP_PARAM_ACTION_DISCARD:
1783 retval = 0;
1784 break;
1785 case SCTP_PARAM_ACTION_DISCARD_ERR:
1786 retval = 0;
1787 /* Make an ERROR chunk, preparing enough room for
1788 * returning multiple unknown parameters.
1790 if (NULL == *errp)
1791 *errp = sctp_make_op_error_space(asoc, chunk,
1792 ntohs(chunk->chunk_hdr->length));
1794 if (*errp) {
1795 sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
1796 WORD_ROUND(ntohs(param.p->length)));
1797 sctp_addto_chunk(*errp,
1798 WORD_ROUND(ntohs(param.p->length)),
1799 param.v);
1802 break;
1803 case SCTP_PARAM_ACTION_SKIP:
1804 break;
1805 case SCTP_PARAM_ACTION_SKIP_ERR:
1806 /* Make an ERROR chunk, preparing enough room for
1807 * returning multiple unknown parameters.
1809 if (NULL == *errp)
1810 *errp = sctp_make_op_error_space(asoc, chunk,
1811 ntohs(chunk->chunk_hdr->length));
1813 if (*errp) {
1814 sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
1815 WORD_ROUND(ntohs(param.p->length)));
1816 sctp_addto_chunk(*errp,
1817 WORD_ROUND(ntohs(param.p->length)),
1818 param.v);
1819 } else {
1820 /* If there is no memory for generating the ERROR
1821 * report as specified, an ABORT will be triggered
1822 * to the peer and the association won't be
1823 * established.
1825 retval = 0;
1828 break;
1829 default:
1830 break;
1833 return retval;
1836 /* Find unrecognized parameters in the chunk.
1837 * Return values:
1838 * 0 - discard the chunk
1839 * 1 - continue with the chunk
1841 static int sctp_verify_param(const struct sctp_association *asoc,
1842 union sctp_params param,
1843 sctp_cid_t cid,
1844 struct sctp_chunk *chunk,
1845 struct sctp_chunk **err_chunk)
1847 int retval = 1;
1849 /* FIXME - This routine is not looking at each parameter per the
1850 * chunk type, i.e., unrecognized parameters should be further
1851 * identified based on the chunk id.
1854 switch (param.p->type) {
1855 case SCTP_PARAM_IPV4_ADDRESS:
1856 case SCTP_PARAM_IPV6_ADDRESS:
1857 case SCTP_PARAM_COOKIE_PRESERVATIVE:
1858 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
1859 case SCTP_PARAM_STATE_COOKIE:
1860 case SCTP_PARAM_HEARTBEAT_INFO:
1861 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
1862 case SCTP_PARAM_ECN_CAPABLE:
1863 case SCTP_PARAM_ADAPTATION_LAYER_IND:
1864 case SCTP_PARAM_SUPPORTED_EXT:
1865 break;
1867 case SCTP_PARAM_HOST_NAME_ADDRESS:
1868 /* Tell the peer, we won't support this param. */
1869 return sctp_process_hn_param(asoc, param, chunk, err_chunk);
1871 case SCTP_PARAM_FWD_TSN_SUPPORT:
1872 if (sctp_prsctp_enable)
1873 break;
1874 /* Fall Through */
1875 default:
1876 SCTP_DEBUG_PRINTK("Unrecognized param: %d for chunk %d.\n",
1877 ntohs(param.p->type), cid);
1878 return sctp_process_unk_param(asoc, param, chunk, err_chunk);
1880 break;
1882 return retval;
1885 /* Verify the INIT packet before we process it. */
1886 int sctp_verify_init(const struct sctp_association *asoc,
1887 sctp_cid_t cid,
1888 sctp_init_chunk_t *peer_init,
1889 struct sctp_chunk *chunk,
1890 struct sctp_chunk **errp)
1892 union sctp_params param;
1893 int has_cookie = 0;
1895 /* Verify stream values are non-zero. */
1896 if ((0 == peer_init->init_hdr.num_outbound_streams) ||
1897 (0 == peer_init->init_hdr.num_inbound_streams) ||
1898 (0 == peer_init->init_hdr.init_tag) ||
1899 (SCTP_DEFAULT_MINWINDOW > ntohl(peer_init->init_hdr.a_rwnd))) {
1901 sctp_process_inv_mandatory(asoc, chunk, errp);
1902 return 0;
1905 /* Check for missing mandatory parameters. */
1906 sctp_walk_params(param, peer_init, init_hdr.params) {
1908 if (SCTP_PARAM_STATE_COOKIE == param.p->type)
1909 has_cookie = 1;
1911 } /* for (loop through all parameters) */
1913 /* There is a possibility that a parameter length was bad and
1914 * in that case we would have stoped walking the parameters.
1915 * The current param.p would point at the bad one.
1916 * Current consensus on the mailing list is to generate a PROTOCOL
1917 * VIOLATION error. We build the ERROR chunk here and let the normal
1918 * error handling code build and send the packet.
1920 if (param.v != (void*)chunk->chunk_end) {
1921 sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
1922 return 0;
1925 /* The only missing mandatory param possible today is
1926 * the state cookie for an INIT-ACK chunk.
1928 if ((SCTP_CID_INIT_ACK == cid) && !has_cookie) {
1929 sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
1930 chunk, errp);
1931 return 0;
1934 /* Find unrecognized parameters. */
1936 sctp_walk_params(param, peer_init, init_hdr.params) {
1938 if (!sctp_verify_param(asoc, param, cid, chunk, errp)) {
1939 if (SCTP_PARAM_HOST_NAME_ADDRESS == param.p->type)
1940 return 0;
1941 else
1942 return 1;
1945 } /* for (loop through all parameters) */
1947 return 1;
1950 /* Unpack the parameters in an INIT packet into an association.
1951 * Returns 0 on failure, else success.
1952 * FIXME: This is an association method.
1954 int sctp_process_init(struct sctp_association *asoc, sctp_cid_t cid,
1955 const union sctp_addr *peer_addr,
1956 sctp_init_chunk_t *peer_init, gfp_t gfp)
1958 union sctp_params param;
1959 struct sctp_transport *transport;
1960 struct list_head *pos, *temp;
1961 char *cookie;
1963 /* We must include the address that the INIT packet came from.
1964 * This is the only address that matters for an INIT packet.
1965 * When processing a COOKIE ECHO, we retrieve the from address
1966 * of the INIT from the cookie.
1969 /* This implementation defaults to making the first transport
1970 * added as the primary transport. The source address seems to
1971 * be a a better choice than any of the embedded addresses.
1973 if (peer_addr) {
1974 if(!sctp_assoc_add_peer(asoc, peer_addr, gfp, SCTP_ACTIVE))
1975 goto nomem;
1978 /* Process the initialization parameters. */
1980 sctp_walk_params(param, peer_init, init_hdr.params) {
1982 if (!sctp_process_param(asoc, param, peer_addr, gfp))
1983 goto clean_up;
1986 /* Walk list of transports, removing transports in the UNKNOWN state. */
1987 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
1988 transport = list_entry(pos, struct sctp_transport, transports);
1989 if (transport->state == SCTP_UNKNOWN) {
1990 sctp_assoc_rm_peer(asoc, transport);
1994 /* The fixed INIT headers are always in network byte
1995 * order.
1997 asoc->peer.i.init_tag =
1998 ntohl(peer_init->init_hdr.init_tag);
1999 asoc->peer.i.a_rwnd =
2000 ntohl(peer_init->init_hdr.a_rwnd);
2001 asoc->peer.i.num_outbound_streams =
2002 ntohs(peer_init->init_hdr.num_outbound_streams);
2003 asoc->peer.i.num_inbound_streams =
2004 ntohs(peer_init->init_hdr.num_inbound_streams);
2005 asoc->peer.i.initial_tsn =
2006 ntohl(peer_init->init_hdr.initial_tsn);
2008 /* Apply the upper bounds for output streams based on peer's
2009 * number of inbound streams.
2011 if (asoc->c.sinit_num_ostreams >
2012 ntohs(peer_init->init_hdr.num_inbound_streams)) {
2013 asoc->c.sinit_num_ostreams =
2014 ntohs(peer_init->init_hdr.num_inbound_streams);
2017 if (asoc->c.sinit_max_instreams >
2018 ntohs(peer_init->init_hdr.num_outbound_streams)) {
2019 asoc->c.sinit_max_instreams =
2020 ntohs(peer_init->init_hdr.num_outbound_streams);
2023 /* Copy Initiation tag from INIT to VT_peer in cookie. */
2024 asoc->c.peer_vtag = asoc->peer.i.init_tag;
2026 /* Peer Rwnd : Current calculated value of the peer's rwnd. */
2027 asoc->peer.rwnd = asoc->peer.i.a_rwnd;
2029 /* Copy cookie in case we need to resend COOKIE-ECHO. */
2030 cookie = asoc->peer.cookie;
2031 if (cookie) {
2032 asoc->peer.cookie = kmemdup(cookie, asoc->peer.cookie_len, gfp);
2033 if (!asoc->peer.cookie)
2034 goto clean_up;
2037 /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
2038 * high (for example, implementations MAY use the size of the receiver
2039 * advertised window).
2041 list_for_each(pos, &asoc->peer.transport_addr_list) {
2042 transport = list_entry(pos, struct sctp_transport, transports);
2043 transport->ssthresh = asoc->peer.i.a_rwnd;
2046 /* Set up the TSN tracking pieces. */
2047 sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_SIZE,
2048 asoc->peer.i.initial_tsn);
2050 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
2052 * The stream sequence number in all the streams shall start
2053 * from 0 when the association is established. Also, when the
2054 * stream sequence number reaches the value 65535 the next
2055 * stream sequence number shall be set to 0.
2058 /* Allocate storage for the negotiated streams if it is not a temporary
2059 * association.
2061 if (!asoc->temp) {
2062 int error;
2064 asoc->ssnmap = sctp_ssnmap_new(asoc->c.sinit_max_instreams,
2065 asoc->c.sinit_num_ostreams, gfp);
2066 if (!asoc->ssnmap)
2067 goto clean_up;
2069 error = sctp_assoc_set_id(asoc, gfp);
2070 if (error)
2071 goto clean_up;
2074 /* ADDIP Section 4.1 ASCONF Chunk Procedures
2076 * When an endpoint has an ASCONF signaled change to be sent to the
2077 * remote endpoint it should do the following:
2078 * ...
2079 * A2) A serial number should be assigned to the Chunk. The serial
2080 * number should be a monotonically increasing number. All serial
2081 * numbers are defined to be initialized at the start of the
2082 * association to the same value as the Initial TSN.
2084 asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
2085 return 1;
2087 clean_up:
2088 /* Release the transport structures. */
2089 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2090 transport = list_entry(pos, struct sctp_transport, transports);
2091 list_del_init(pos);
2092 sctp_transport_free(transport);
2095 asoc->peer.transport_count = 0;
2097 nomem:
2098 return 0;
2102 /* Update asoc with the option described in param.
2104 * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
2106 * asoc is the association to update.
2107 * param is the variable length parameter to use for update.
2108 * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
2109 * If the current packet is an INIT we want to minimize the amount of
2110 * work we do. In particular, we should not build transport
2111 * structures for the addresses.
2113 static int sctp_process_param(struct sctp_association *asoc,
2114 union sctp_params param,
2115 const union sctp_addr *peer_addr,
2116 gfp_t gfp)
2118 union sctp_addr addr;
2119 int i;
2120 __u16 sat;
2121 int retval = 1;
2122 sctp_scope_t scope;
2123 time_t stale;
2124 struct sctp_af *af;
2126 /* We maintain all INIT parameters in network byte order all the
2127 * time. This allows us to not worry about whether the parameters
2128 * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
2130 switch (param.p->type) {
2131 case SCTP_PARAM_IPV6_ADDRESS:
2132 if (PF_INET6 != asoc->base.sk->sk_family)
2133 break;
2134 /* Fall through. */
2135 case SCTP_PARAM_IPV4_ADDRESS:
2136 af = sctp_get_af_specific(param_type2af(param.p->type));
2137 af->from_addr_param(&addr, param.addr, htons(asoc->peer.port), 0);
2138 scope = sctp_scope(peer_addr);
2139 if (sctp_in_scope(&addr, scope))
2140 if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_UNCONFIRMED))
2141 return 0;
2142 break;
2144 case SCTP_PARAM_COOKIE_PRESERVATIVE:
2145 if (!sctp_cookie_preserve_enable)
2146 break;
2148 stale = ntohl(param.life->lifespan_increment);
2150 /* Suggested Cookie Life span increment's unit is msec,
2151 * (1/1000sec).
2153 asoc->cookie_life.tv_sec += stale / 1000;
2154 asoc->cookie_life.tv_usec += (stale % 1000) * 1000;
2155 break;
2157 case SCTP_PARAM_HOST_NAME_ADDRESS:
2158 SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
2159 break;
2161 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2162 /* Turn off the default values first so we'll know which
2163 * ones are really set by the peer.
2165 asoc->peer.ipv4_address = 0;
2166 asoc->peer.ipv6_address = 0;
2168 /* Cycle through address types; avoid divide by 0. */
2169 sat = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2170 if (sat)
2171 sat /= sizeof(__u16);
2173 for (i = 0; i < sat; ++i) {
2174 switch (param.sat->types[i]) {
2175 case SCTP_PARAM_IPV4_ADDRESS:
2176 asoc->peer.ipv4_address = 1;
2177 break;
2179 case SCTP_PARAM_IPV6_ADDRESS:
2180 asoc->peer.ipv6_address = 1;
2181 break;
2183 case SCTP_PARAM_HOST_NAME_ADDRESS:
2184 asoc->peer.hostname_address = 1;
2185 break;
2187 default: /* Just ignore anything else. */
2188 break;
2191 break;
2193 case SCTP_PARAM_STATE_COOKIE:
2194 asoc->peer.cookie_len =
2195 ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2196 asoc->peer.cookie = param.cookie->body;
2197 break;
2199 case SCTP_PARAM_HEARTBEAT_INFO:
2200 /* Would be odd to receive, but it causes no problems. */
2201 break;
2203 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2204 /* Rejected during verify stage. */
2205 break;
2207 case SCTP_PARAM_ECN_CAPABLE:
2208 asoc->peer.ecn_capable = 1;
2209 break;
2211 case SCTP_PARAM_ADAPTATION_LAYER_IND:
2212 asoc->peer.adaptation_ind = param.aind->adaptation_ind;
2213 break;
2215 case SCTP_PARAM_SUPPORTED_EXT:
2216 sctp_process_ext_param(asoc, param);
2217 break;
2219 case SCTP_PARAM_FWD_TSN_SUPPORT:
2220 if (sctp_prsctp_enable) {
2221 asoc->peer.prsctp_capable = 1;
2222 break;
2224 /* Fall Through */
2225 default:
2226 /* Any unrecognized parameters should have been caught
2227 * and handled by sctp_verify_param() which should be
2228 * called prior to this routine. Simply log the error
2229 * here.
2231 SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
2232 ntohs(param.p->type), asoc);
2233 break;
2236 return retval;
2239 /* Select a new verification tag. */
2240 __u32 sctp_generate_tag(const struct sctp_endpoint *ep)
2242 /* I believe that this random number generator complies with RFC1750.
2243 * A tag of 0 is reserved for special cases (e.g. INIT).
2245 __u32 x;
2247 do {
2248 get_random_bytes(&x, sizeof(__u32));
2249 } while (x == 0);
2251 return x;
2254 /* Select an initial TSN to send during startup. */
2255 __u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
2257 __u32 retval;
2259 get_random_bytes(&retval, sizeof(__u32));
2260 return retval;
2264 * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
2265 * 0 1 2 3
2266 * 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
2267 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2268 * | Type = 0xC1 | Chunk Flags | Chunk Length |
2269 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2270 * | Serial Number |
2271 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2272 * | Address Parameter |
2273 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2274 * | ASCONF Parameter #1 |
2275 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2276 * \ \
2277 * / .... /
2278 * \ \
2279 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2280 * | ASCONF Parameter #N |
2281 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2283 * Address Parameter and other parameter will not be wrapped in this function
2285 static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
2286 union sctp_addr *addr,
2287 int vparam_len)
2289 sctp_addiphdr_t asconf;
2290 struct sctp_chunk *retval;
2291 int length = sizeof(asconf) + vparam_len;
2292 union sctp_addr_param addrparam;
2293 int addrlen;
2294 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2296 addrlen = af->to_addr_param(addr, &addrparam);
2297 if (!addrlen)
2298 return NULL;
2299 length += addrlen;
2301 /* Create the chunk. */
2302 retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF, 0, length);
2303 if (!retval)
2304 return NULL;
2306 asconf.serial = htonl(asoc->addip_serial++);
2308 retval->subh.addip_hdr =
2309 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2310 retval->param_hdr.v =
2311 sctp_addto_chunk(retval, addrlen, &addrparam);
2313 return retval;
2316 /* ADDIP
2317 * 3.2.1 Add IP Address
2318 * 0 1 2 3
2319 * 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
2320 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2321 * | Type = 0xC001 | Length = Variable |
2322 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2323 * | ASCONF-Request Correlation ID |
2324 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2325 * | Address Parameter |
2326 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2328 * 3.2.2 Delete IP Address
2329 * 0 1 2 3
2330 * 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
2331 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2332 * | Type = 0xC002 | Length = Variable |
2333 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2334 * | ASCONF-Request Correlation ID |
2335 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2336 * | Address Parameter |
2337 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2340 struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
2341 union sctp_addr *laddr,
2342 struct sockaddr *addrs,
2343 int addrcnt,
2344 __be16 flags)
2346 sctp_addip_param_t param;
2347 struct sctp_chunk *retval;
2348 union sctp_addr_param addr_param;
2349 union sctp_addr *addr;
2350 void *addr_buf;
2351 struct sctp_af *af;
2352 int paramlen = sizeof(param);
2353 int addr_param_len = 0;
2354 int totallen = 0;
2355 int i;
2357 /* Get total length of all the address parameters. */
2358 addr_buf = addrs;
2359 for (i = 0; i < addrcnt; i++) {
2360 addr = (union sctp_addr *)addr_buf;
2361 af = sctp_get_af_specific(addr->v4.sin_family);
2362 addr_param_len = af->to_addr_param(addr, &addr_param);
2364 totallen += paramlen;
2365 totallen += addr_param_len;
2367 addr_buf += af->sockaddr_len;
2370 /* Create an asconf chunk with the required length. */
2371 retval = sctp_make_asconf(asoc, laddr, totallen);
2372 if (!retval)
2373 return NULL;
2375 /* Add the address parameters to the asconf chunk. */
2376 addr_buf = addrs;
2377 for (i = 0; i < addrcnt; i++) {
2378 addr = (union sctp_addr *)addr_buf;
2379 af = sctp_get_af_specific(addr->v4.sin_family);
2380 addr_param_len = af->to_addr_param(addr, &addr_param);
2381 param.param_hdr.type = flags;
2382 param.param_hdr.length = htons(paramlen + addr_param_len);
2383 param.crr_id = i;
2385 sctp_addto_chunk(retval, paramlen, &param);
2386 sctp_addto_chunk(retval, addr_param_len, &addr_param);
2388 addr_buf += af->sockaddr_len;
2390 return retval;
2393 /* ADDIP
2394 * 3.2.4 Set Primary IP Address
2395 * 0 1 2 3
2396 * 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
2397 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2398 * | Type =0xC004 | Length = Variable |
2399 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2400 * | ASCONF-Request Correlation ID |
2401 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2402 * | Address Parameter |
2403 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2405 * Create an ASCONF chunk with Set Primary IP address parameter.
2407 struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
2408 union sctp_addr *addr)
2410 sctp_addip_param_t param;
2411 struct sctp_chunk *retval;
2412 int len = sizeof(param);
2413 union sctp_addr_param addrparam;
2414 int addrlen;
2415 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2417 addrlen = af->to_addr_param(addr, &addrparam);
2418 if (!addrlen)
2419 return NULL;
2420 len += addrlen;
2422 /* Create the chunk and make asconf header. */
2423 retval = sctp_make_asconf(asoc, addr, len);
2424 if (!retval)
2425 return NULL;
2427 param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
2428 param.param_hdr.length = htons(len);
2429 param.crr_id = 0;
2431 sctp_addto_chunk(retval, sizeof(param), &param);
2432 sctp_addto_chunk(retval, addrlen, &addrparam);
2434 return retval;
2437 /* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2438 * 0 1 2 3
2439 * 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
2440 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2441 * | Type = 0x80 | Chunk Flags | Chunk Length |
2442 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2443 * | Serial Number |
2444 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2445 * | ASCONF Parameter Response#1 |
2446 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2447 * \ \
2448 * / .... /
2449 * \ \
2450 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2451 * | ASCONF Parameter Response#N |
2452 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2454 * Create an ASCONF_ACK chunk with enough space for the parameter responses.
2456 static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
2457 __u32 serial, int vparam_len)
2459 sctp_addiphdr_t asconf;
2460 struct sctp_chunk *retval;
2461 int length = sizeof(asconf) + vparam_len;
2463 /* Create the chunk. */
2464 retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF_ACK, 0, length);
2465 if (!retval)
2466 return NULL;
2468 asconf.serial = htonl(serial);
2470 retval->subh.addip_hdr =
2471 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2473 return retval;
2476 /* Add response parameters to an ASCONF_ACK chunk. */
2477 static void sctp_add_asconf_response(struct sctp_chunk *chunk, __be32 crr_id,
2478 __be16 err_code, sctp_addip_param_t *asconf_param)
2480 sctp_addip_param_t ack_param;
2481 sctp_errhdr_t err_param;
2482 int asconf_param_len = 0;
2483 int err_param_len = 0;
2484 __be16 response_type;
2486 if (SCTP_ERROR_NO_ERROR == err_code) {
2487 response_type = SCTP_PARAM_SUCCESS_REPORT;
2488 } else {
2489 response_type = SCTP_PARAM_ERR_CAUSE;
2490 err_param_len = sizeof(err_param);
2491 if (asconf_param)
2492 asconf_param_len =
2493 ntohs(asconf_param->param_hdr.length);
2496 /* Add Success Indication or Error Cause Indication parameter. */
2497 ack_param.param_hdr.type = response_type;
2498 ack_param.param_hdr.length = htons(sizeof(ack_param) +
2499 err_param_len +
2500 asconf_param_len);
2501 ack_param.crr_id = crr_id;
2502 sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
2504 if (SCTP_ERROR_NO_ERROR == err_code)
2505 return;
2507 /* Add Error Cause parameter. */
2508 err_param.cause = err_code;
2509 err_param.length = htons(err_param_len + asconf_param_len);
2510 sctp_addto_chunk(chunk, err_param_len, &err_param);
2512 /* Add the failed TLV copied from ASCONF chunk. */
2513 if (asconf_param)
2514 sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
2517 /* Process a asconf parameter. */
2518 static __be16 sctp_process_asconf_param(struct sctp_association *asoc,
2519 struct sctp_chunk *asconf,
2520 sctp_addip_param_t *asconf_param)
2522 struct sctp_transport *peer;
2523 struct sctp_af *af;
2524 union sctp_addr addr;
2525 struct list_head *pos;
2526 union sctp_addr_param *addr_param;
2528 addr_param = (union sctp_addr_param *)
2529 ((void *)asconf_param + sizeof(sctp_addip_param_t));
2531 af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2532 if (unlikely(!af))
2533 return SCTP_ERROR_INV_PARAM;
2535 af->from_addr_param(&addr, addr_param, htons(asoc->peer.port), 0);
2536 switch (asconf_param->param_hdr.type) {
2537 case SCTP_PARAM_ADD_IP:
2538 /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
2539 * request and does not have the local resources to add this
2540 * new address to the association, it MUST return an Error
2541 * Cause TLV set to the new error code 'Operation Refused
2542 * Due to Resource Shortage'.
2545 peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC, SCTP_UNCONFIRMED);
2546 if (!peer)
2547 return SCTP_ERROR_RSRC_LOW;
2549 /* Start the heartbeat timer. */
2550 if (!mod_timer(&peer->hb_timer, sctp_transport_timeout(peer)))
2551 sctp_transport_hold(peer);
2552 break;
2553 case SCTP_PARAM_DEL_IP:
2554 /* ADDIP 4.3 D7) If a request is received to delete the
2555 * last remaining IP address of a peer endpoint, the receiver
2556 * MUST send an Error Cause TLV with the error cause set to the
2557 * new error code 'Request to Delete Last Remaining IP Address'.
2559 pos = asoc->peer.transport_addr_list.next;
2560 if (pos->next == &asoc->peer.transport_addr_list)
2561 return SCTP_ERROR_DEL_LAST_IP;
2563 /* ADDIP 4.3 D8) If a request is received to delete an IP
2564 * address which is also the source address of the IP packet
2565 * which contained the ASCONF chunk, the receiver MUST reject
2566 * this request. To reject the request the receiver MUST send
2567 * an Error Cause TLV set to the new error code 'Request to
2568 * Delete Source IP Address'
2570 if (sctp_cmp_addr_exact(sctp_source(asconf), &addr))
2571 return SCTP_ERROR_DEL_SRC_IP;
2573 sctp_assoc_del_peer(asoc, &addr);
2574 break;
2575 case SCTP_PARAM_SET_PRIMARY:
2576 peer = sctp_assoc_lookup_paddr(asoc, &addr);
2577 if (!peer)
2578 return SCTP_ERROR_INV_PARAM;
2580 sctp_assoc_set_primary(asoc, peer);
2581 break;
2582 default:
2583 return SCTP_ERROR_INV_PARAM;
2584 break;
2587 return SCTP_ERROR_NO_ERROR;
2590 /* Verify the ASCONF packet before we process it. */
2591 int sctp_verify_asconf(const struct sctp_association *asoc,
2592 struct sctp_paramhdr *param_hdr, void *chunk_end,
2593 struct sctp_paramhdr **errp) {
2594 sctp_addip_param_t *asconf_param;
2595 union sctp_params param;
2596 int length, plen;
2598 param.v = (sctp_paramhdr_t *) param_hdr;
2599 while (param.v <= chunk_end - sizeof(sctp_paramhdr_t)) {
2600 length = ntohs(param.p->length);
2601 *errp = param.p;
2603 if (param.v > chunk_end - length ||
2604 length < sizeof(sctp_paramhdr_t))
2605 return 0;
2607 switch (param.p->type) {
2608 case SCTP_PARAM_ADD_IP:
2609 case SCTP_PARAM_DEL_IP:
2610 case SCTP_PARAM_SET_PRIMARY:
2611 asconf_param = (sctp_addip_param_t *)param.v;
2612 plen = ntohs(asconf_param->param_hdr.length);
2613 if (plen < sizeof(sctp_addip_param_t) +
2614 sizeof(sctp_paramhdr_t))
2615 return 0;
2616 break;
2617 case SCTP_PARAM_SUCCESS_REPORT:
2618 case SCTP_PARAM_ADAPTATION_LAYER_IND:
2619 if (length != sizeof(sctp_addip_param_t))
2620 return 0;
2622 break;
2623 default:
2624 break;
2627 param.v += WORD_ROUND(length);
2630 if (param.v != chunk_end)
2631 return 0;
2633 return 1;
2636 /* Process an incoming ASCONF chunk with the next expected serial no. and
2637 * return an ASCONF_ACK chunk to be sent in response.
2639 struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
2640 struct sctp_chunk *asconf)
2642 sctp_addiphdr_t *hdr;
2643 union sctp_addr_param *addr_param;
2644 sctp_addip_param_t *asconf_param;
2645 struct sctp_chunk *asconf_ack;
2647 __be16 err_code;
2648 int length = 0;
2649 int chunk_len = asconf->skb->len;
2650 __u32 serial;
2651 int all_param_pass = 1;
2653 hdr = (sctp_addiphdr_t *)asconf->skb->data;
2654 serial = ntohl(hdr->serial);
2656 /* Skip the addiphdr and store a pointer to address parameter. */
2657 length = sizeof(sctp_addiphdr_t);
2658 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
2659 chunk_len -= length;
2661 /* Skip the address parameter and store a pointer to the first
2662 * asconf paramter.
2664 length = ntohs(addr_param->v4.param_hdr.length);
2665 asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
2666 chunk_len -= length;
2668 /* create an ASCONF_ACK chunk.
2669 * Based on the definitions of parameters, we know that the size of
2670 * ASCONF_ACK parameters are less than or equal to the twice of ASCONF
2671 * paramters.
2673 asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 2);
2674 if (!asconf_ack)
2675 goto done;
2677 /* Process the TLVs contained within the ASCONF chunk. */
2678 while (chunk_len > 0) {
2679 err_code = sctp_process_asconf_param(asoc, asconf,
2680 asconf_param);
2681 /* ADDIP 4.1 A7)
2682 * If an error response is received for a TLV parameter,
2683 * all TLVs with no response before the failed TLV are
2684 * considered successful if not reported. All TLVs after
2685 * the failed response are considered unsuccessful unless
2686 * a specific success indication is present for the parameter.
2688 if (SCTP_ERROR_NO_ERROR != err_code)
2689 all_param_pass = 0;
2691 if (!all_param_pass)
2692 sctp_add_asconf_response(asconf_ack,
2693 asconf_param->crr_id, err_code,
2694 asconf_param);
2696 /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
2697 * an IP address sends an 'Out of Resource' in its response, it
2698 * MUST also fail any subsequent add or delete requests bundled
2699 * in the ASCONF.
2701 if (SCTP_ERROR_RSRC_LOW == err_code)
2702 goto done;
2704 /* Move to the next ASCONF param. */
2705 length = ntohs(asconf_param->param_hdr.length);
2706 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
2707 length);
2708 chunk_len -= length;
2711 done:
2712 asoc->peer.addip_serial++;
2714 /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
2715 * after freeing the reference to old asconf ack if any.
2717 if (asconf_ack) {
2718 if (asoc->addip_last_asconf_ack)
2719 sctp_chunk_free(asoc->addip_last_asconf_ack);
2721 sctp_chunk_hold(asconf_ack);
2722 asoc->addip_last_asconf_ack = asconf_ack;
2725 return asconf_ack;
2728 /* Process a asconf parameter that is successfully acked. */
2729 static int sctp_asconf_param_success(struct sctp_association *asoc,
2730 sctp_addip_param_t *asconf_param)
2732 struct sctp_af *af;
2733 union sctp_addr addr;
2734 struct sctp_bind_addr *bp = &asoc->base.bind_addr;
2735 union sctp_addr_param *addr_param;
2736 struct list_head *pos;
2737 struct sctp_transport *transport;
2738 struct sctp_sockaddr_entry *saddr;
2739 int retval = 0;
2741 addr_param = (union sctp_addr_param *)
2742 ((void *)asconf_param + sizeof(sctp_addip_param_t));
2744 /* We have checked the packet before, so we do not check again. */
2745 af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2746 af->from_addr_param(&addr, addr_param, htons(bp->port), 0);
2748 switch (asconf_param->param_hdr.type) {
2749 case SCTP_PARAM_ADD_IP:
2750 /* This is always done in BH context with a socket lock
2751 * held, so the list can not change.
2753 list_for_each_entry(saddr, &bp->address_list, list) {
2754 if (sctp_cmp_addr_exact(&saddr->a, &addr))
2755 saddr->use_as_src = 1;
2757 break;
2758 case SCTP_PARAM_DEL_IP:
2759 retval = sctp_del_bind_addr(bp, &addr, call_rcu_bh);
2760 list_for_each(pos, &asoc->peer.transport_addr_list) {
2761 transport = list_entry(pos, struct sctp_transport,
2762 transports);
2763 dst_release(transport->dst);
2764 sctp_transport_route(transport, NULL,
2765 sctp_sk(asoc->base.sk));
2767 break;
2768 default:
2769 break;
2772 return retval;
2775 /* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
2776 * for the given asconf parameter. If there is no response for this parameter,
2777 * return the error code based on the third argument 'no_err'.
2778 * ADDIP 4.1
2779 * A7) If an error response is received for a TLV parameter, all TLVs with no
2780 * response before the failed TLV are considered successful if not reported.
2781 * All TLVs after the failed response are considered unsuccessful unless a
2782 * specific success indication is present for the parameter.
2784 static __be16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
2785 sctp_addip_param_t *asconf_param,
2786 int no_err)
2788 sctp_addip_param_t *asconf_ack_param;
2789 sctp_errhdr_t *err_param;
2790 int length;
2791 int asconf_ack_len = asconf_ack->skb->len;
2792 __be16 err_code;
2794 if (no_err)
2795 err_code = SCTP_ERROR_NO_ERROR;
2796 else
2797 err_code = SCTP_ERROR_REQ_REFUSED;
2799 /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
2800 * the first asconf_ack parameter.
2802 length = sizeof(sctp_addiphdr_t);
2803 asconf_ack_param = (sctp_addip_param_t *)(asconf_ack->skb->data +
2804 length);
2805 asconf_ack_len -= length;
2807 while (asconf_ack_len > 0) {
2808 if (asconf_ack_param->crr_id == asconf_param->crr_id) {
2809 switch(asconf_ack_param->param_hdr.type) {
2810 case SCTP_PARAM_SUCCESS_REPORT:
2811 return SCTP_ERROR_NO_ERROR;
2812 case SCTP_PARAM_ERR_CAUSE:
2813 length = sizeof(sctp_addip_param_t);
2814 err_param = (sctp_errhdr_t *)
2815 ((void *)asconf_ack_param + length);
2816 asconf_ack_len -= length;
2817 if (asconf_ack_len > 0)
2818 return err_param->cause;
2819 else
2820 return SCTP_ERROR_INV_PARAM;
2821 break;
2822 default:
2823 return SCTP_ERROR_INV_PARAM;
2827 length = ntohs(asconf_ack_param->param_hdr.length);
2828 asconf_ack_param = (sctp_addip_param_t *)
2829 ((void *)asconf_ack_param + length);
2830 asconf_ack_len -= length;
2833 return err_code;
2836 /* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
2837 int sctp_process_asconf_ack(struct sctp_association *asoc,
2838 struct sctp_chunk *asconf_ack)
2840 struct sctp_chunk *asconf = asoc->addip_last_asconf;
2841 union sctp_addr_param *addr_param;
2842 sctp_addip_param_t *asconf_param;
2843 int length = 0;
2844 int asconf_len = asconf->skb->len;
2845 int all_param_pass = 0;
2846 int no_err = 1;
2847 int retval = 0;
2848 __be16 err_code = SCTP_ERROR_NO_ERROR;
2850 /* Skip the chunkhdr and addiphdr from the last asconf sent and store
2851 * a pointer to address parameter.
2853 length = sizeof(sctp_addip_chunk_t);
2854 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
2855 asconf_len -= length;
2857 /* Skip the address parameter in the last asconf sent and store a
2858 * pointer to the first asconf paramter.
2860 length = ntohs(addr_param->v4.param_hdr.length);
2861 asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
2862 asconf_len -= length;
2864 /* ADDIP 4.1
2865 * A8) If there is no response(s) to specific TLV parameter(s), and no
2866 * failures are indicated, then all request(s) are considered
2867 * successful.
2869 if (asconf_ack->skb->len == sizeof(sctp_addiphdr_t))
2870 all_param_pass = 1;
2872 /* Process the TLVs contained in the last sent ASCONF chunk. */
2873 while (asconf_len > 0) {
2874 if (all_param_pass)
2875 err_code = SCTP_ERROR_NO_ERROR;
2876 else {
2877 err_code = sctp_get_asconf_response(asconf_ack,
2878 asconf_param,
2879 no_err);
2880 if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
2881 no_err = 0;
2884 switch (err_code) {
2885 case SCTP_ERROR_NO_ERROR:
2886 retval = sctp_asconf_param_success(asoc, asconf_param);
2887 break;
2889 case SCTP_ERROR_RSRC_LOW:
2890 retval = 1;
2891 break;
2893 case SCTP_ERROR_INV_PARAM:
2894 /* Disable sending this type of asconf parameter in
2895 * future.
2897 asoc->peer.addip_disabled_mask |=
2898 asconf_param->param_hdr.type;
2899 break;
2901 case SCTP_ERROR_REQ_REFUSED:
2902 case SCTP_ERROR_DEL_LAST_IP:
2903 case SCTP_ERROR_DEL_SRC_IP:
2904 default:
2905 break;
2908 /* Skip the processed asconf parameter and move to the next
2909 * one.
2911 length = ntohs(asconf_param->param_hdr.length);
2912 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
2913 length);
2914 asconf_len -= length;
2917 /* Free the cached last sent asconf chunk. */
2918 sctp_chunk_free(asconf);
2919 asoc->addip_last_asconf = NULL;
2921 /* Send the next asconf chunk from the addip chunk queue. */
2922 if (!list_empty(&asoc->addip_chunk_list)) {
2923 struct list_head *entry = asoc->addip_chunk_list.next;
2924 asconf = list_entry(entry, struct sctp_chunk, list);
2926 list_del_init(entry);
2928 /* Hold the chunk until an ASCONF_ACK is received. */
2929 sctp_chunk_hold(asconf);
2930 if (sctp_primitive_ASCONF(asoc, asconf))
2931 sctp_chunk_free(asconf);
2932 else
2933 asoc->addip_last_asconf = asconf;
2936 return retval;
2939 /* Make a FWD TSN chunk. */
2940 struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
2941 __u32 new_cum_tsn, size_t nstreams,
2942 struct sctp_fwdtsn_skip *skiplist)
2944 struct sctp_chunk *retval = NULL;
2945 struct sctp_fwdtsn_chunk *ftsn_chunk;
2946 struct sctp_fwdtsn_hdr ftsn_hdr;
2947 struct sctp_fwdtsn_skip skip;
2948 size_t hint;
2949 int i;
2951 hint = (nstreams + 1) * sizeof(__u32);
2953 retval = sctp_make_chunk(asoc, SCTP_CID_FWD_TSN, 0, hint);
2955 if (!retval)
2956 return NULL;
2958 ftsn_chunk = (struct sctp_fwdtsn_chunk *)retval->subh.fwdtsn_hdr;
2960 ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
2961 retval->subh.fwdtsn_hdr =
2962 sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
2964 for (i = 0; i < nstreams; i++) {
2965 skip.stream = skiplist[i].stream;
2966 skip.ssn = skiplist[i].ssn;
2967 sctp_addto_chunk(retval, sizeof(skip), &skip);
2970 return retval;