]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/sctp/sm_make_chunk.c
sctp: cleanup: remove duplicate assignment
[mirror_ubuntu-artful-kernel.git] / net / sctp / sm_make_chunk.c
CommitLineData
60c778b2 1/* SCTP kernel implementation
1da177e4
LT
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.
6 *
60c778b2 7 * This file is part of the SCTP kernel implementation
1da177e4
LT
8 *
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.
12 *
60c778b2 13 * This SCTP implementation is free software;
1da177e4
LT
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.
18 *
60c778b2 19 * This SCTP implementation is distributed in the hope that it
1da177e4
LT
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.
24 *
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.
29 *
30 * Please send any bug reports or fixes you make to the
31 * email address(es):
32 * lksctp developers <lksctp-developers@lists.sourceforge.net>
33 *
34 * Or submit a bug report through the following website:
35 * http://www.sf.net/projects/lksctp
36 *
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>
48 *
49 * Any bugs reported given to us we will try to fix... any fixes shared will
50 * be incorporated into the next SCTP release.
51 */
52
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>
ebc3bbcf 59#include <linux/scatterlist.h>
1da177e4 60#include <linux/crypto.h>
5a0e3ad6 61#include <linux/slab.h>
1da177e4
LT
62#include <net/sock.h>
63
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>
68
1da177e4
LT
69SCTP_STATIC
70struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
71 __u8 type, __u8 flags, int paylen);
72static 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);
77static int sctp_process_param(struct sctp_association *asoc,
78 union sctp_params param,
79 const union sctp_addr *peer_addr,
dd0fc66f 80 gfp_t gfp);
8ee4be37
VY
81static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
82 const void *data);
1da177e4
LT
83
84/* What was the inbound interface for this chunk? */
85int sctp_chunk_iif(const struct sctp_chunk *chunk)
86{
87 struct sctp_af *af;
88 int iif = 0;
89
eddc9ec5 90 af = sctp_get_af_specific(ipver2af(ip_hdr(chunk->skb)->version));
1da177e4
LT
91 if (af)
92 iif = af->skb_iif(chunk->skb);
93
94 return iif;
95}
96
97/* RFC 2960 3.3.2 Initiation (INIT) (1)
98 *
99 * Note 2: The ECN capable field is reserved for future use of
100 * Explicit Congestion Notification.
101 */
102static const struct sctp_paramhdr ecap_param = {
103 SCTP_PARAM_ECN_CAPABLE,
09640e63 104 cpu_to_be16(sizeof(struct sctp_paramhdr)),
1da177e4
LT
105};
106static const struct sctp_paramhdr prsctp_param = {
107 SCTP_PARAM_FWD_TSN_SUPPORT,
09640e63 108 cpu_to_be16(sizeof(struct sctp_paramhdr)),
1da177e4
LT
109};
110
111/* A helper to initialize to initialize an op error inside a
112 * provided chunk, as most cause codes will be embedded inside an
113 * abort chunk.
114 */
5bf2db03 115void sctp_init_cause(struct sctp_chunk *chunk, __be16 cause_code,
00f1c2df 116 size_t paylen)
1da177e4
LT
117{
118 sctp_errhdr_t err;
1da177e4
LT
119 __u16 len;
120
d808ad9a 121 /* Cause code constants are now defined in network order. */
1da177e4
LT
122 err.cause = cause_code;
123 len = sizeof(sctp_errhdr_t) + paylen;
1da177e4 124 err.length = htons(len);
4a1c0107 125 chunk->subh.err_hdr = sctp_addto_chunk(chunk, sizeof(sctp_errhdr_t), &err);
1da177e4
LT
126}
127
128/* 3.3.2 Initiation (INIT) (1)
129 *
130 * This chunk is used to initiate a SCTP association between two
131 * endpoints. The format of the INIT chunk is shown below:
132 *
133 * 0 1 2 3
134 * 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
135 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
136 * | Type = 1 | Chunk Flags | Chunk Length |
137 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
138 * | Initiate Tag |
139 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
140 * | Advertised Receiver Window Credit (a_rwnd) |
141 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
142 * | Number of Outbound Streams | Number of Inbound Streams |
143 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
144 * | Initial TSN |
145 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
146 * \ \
147 * / Optional/Variable-Length Parameters /
148 * \ \
149 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
150 *
151 *
152 * The INIT chunk contains the following parameters. Unless otherwise
153 * noted, each parameter MUST only be included once in the INIT chunk.
154 *
155 * Fixed Parameters Status
156 * ----------------------------------------------
157 * Initiate Tag Mandatory
158 * Advertised Receiver Window Credit Mandatory
159 * Number of Outbound Streams Mandatory
160 * Number of Inbound Streams Mandatory
161 * Initial TSN Mandatory
162 *
163 * Variable Parameters Status Type Value
164 * -------------------------------------------------------------
165 * IPv4 Address (Note 1) Optional 5
166 * IPv6 Address (Note 1) Optional 6
167 * Cookie Preservative Optional 9
168 * Reserved for ECN Capable (Note 2) Optional 32768 (0x8000)
169 * Host Name Address (Note 3) Optional 11
170 * Supported Address Types (Note 4) Optional 12
171 */
172struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
173 const struct sctp_bind_addr *bp,
dd0fc66f 174 gfp_t gfp, int vparam_len)
1da177e4
LT
175{
176 sctp_inithdr_t init;
177 union sctp_params addrs;
178 size_t chunksize;
179 struct sctp_chunk *retval = NULL;
180 int num_types, addrs_len = 0;
181 struct sctp_sock *sp;
182 sctp_supported_addrs_param_t sat;
3dbe8656 183 __be16 types[2];
0f3fffd8 184 sctp_adaptation_ind_param_t aiparam;
131a47e3
VY
185 sctp_supported_ext_param_t ext_param;
186 int num_ext = 0;
187 __u8 extensions[3];
730fc3d0
VY
188 sctp_paramhdr_t *auth_chunks = NULL,
189 *auth_hmacs = NULL;
1da177e4
LT
190
191 /* RFC 2960 3.3.2 Initiation (INIT) (1)
192 *
193 * Note 1: The INIT chunks can contain multiple addresses that
194 * can be IPv4 and/or IPv6 in any combination.
195 */
196 retval = NULL;
197
198 /* Convert the provided bind address list to raw format. */
199 addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, gfp);
200
201 init.init_tag = htonl(asoc->c.my_vtag);
202 init.a_rwnd = htonl(asoc->rwnd);
203 init.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
204 init.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
205 init.initial_tsn = htonl(asoc->c.initial_tsn);
206
207 /* How many address types are needed? */
208 sp = sctp_sk(asoc->base.sk);
209 num_types = sp->pf->supported_addrs(sp, types);
210
211 chunksize = sizeof(init) + addrs_len + SCTP_SAT_LEN(num_types);
212 chunksize += sizeof(ecap_param);
8ee4be37 213
036b579b
VY
214 if (sctp_prsctp_enable)
215 chunksize += sizeof(prsctp_param);
216
131a47e3
VY
217 /* ADDIP: Section 4.2.7:
218 * An implementation supporting this extension [ADDIP] MUST list
219 * the ASCONF,the ASCONF-ACK, and the AUTH chunks in its INIT and
220 * INIT-ACK parameters.
131a47e3
VY
221 */
222 if (sctp_addip_enable) {
223 extensions[num_ext] = SCTP_CID_ASCONF;
224 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
225 num_ext += 2;
226 }
227
6fc791ee 228 if (sp->adaptation_ind)
229 chunksize += sizeof(aiparam);
230
1da177e4
LT
231 chunksize += vparam_len;
232
730fc3d0
VY
233 /* Account for AUTH related parameters */
234 if (sctp_auth_enable) {
235 /* Add random parameter length*/
236 chunksize += sizeof(asoc->c.auth_random);
237
238 /* Add HMACS parameter length if any were defined */
239 auth_hmacs = (sctp_paramhdr_t *)asoc->c.auth_hmacs;
240 if (auth_hmacs->length)
241 chunksize += ntohs(auth_hmacs->length);
242 else
243 auth_hmacs = NULL;
244
245 /* Add CHUNKS parameter length */
246 auth_chunks = (sctp_paramhdr_t *)asoc->c.auth_chunks;
247 if (auth_chunks->length)
248 chunksize += ntohs(auth_chunks->length);
249 else
9baffaa6 250 auth_chunks = NULL;
730fc3d0
VY
251
252 extensions[num_ext] = SCTP_CID_AUTH;
253 num_ext += 1;
254 }
255
131a47e3
VY
256 /* If we have any extensions to report, account for that */
257 if (num_ext)
258 chunksize += sizeof(sctp_supported_ext_param_t) + num_ext;
259
1da177e4
LT
260 /* RFC 2960 3.3.2 Initiation (INIT) (1)
261 *
262 * Note 3: An INIT chunk MUST NOT contain more than one Host
263 * Name address parameter. Moreover, the sender of the INIT
264 * MUST NOT combine any other address types with the Host Name
265 * address in the INIT. The receiver of INIT MUST ignore any
266 * other address types if the Host Name address parameter is
267 * present in the received INIT chunk.
268 *
269 * PLEASE DO NOT FIXME [This version does not support Host Name.]
270 */
271
272 retval = sctp_make_chunk(asoc, SCTP_CID_INIT, 0, chunksize);
273 if (!retval)
274 goto nodata;
275
276 retval->subh.init_hdr =
277 sctp_addto_chunk(retval, sizeof(init), &init);
278 retval->param_hdr.v =
279 sctp_addto_chunk(retval, addrs_len, addrs.v);
280
281 /* RFC 2960 3.3.2 Initiation (INIT) (1)
282 *
283 * Note 4: This parameter, when present, specifies all the
284 * address types the sending endpoint can support. The absence
285 * of this parameter indicates that the sending endpoint can
286 * support any address type.
287 */
288 sat.param_hdr.type = SCTP_PARAM_SUPPORTED_ADDRESS_TYPES;
289 sat.param_hdr.length = htons(SCTP_SAT_LEN(num_types));
290 sctp_addto_chunk(retval, sizeof(sat), &sat);
291 sctp_addto_chunk(retval, num_types * sizeof(__u16), &types);
292
293 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
131a47e3 294
7aa1b54b 295 /* Add the supported extensions parameter. Be nice and add this
131a47e3
VY
296 * fist before addiding the parameters for the extensions themselves
297 */
298 if (num_ext) {
299 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
300 ext_param.param_hdr.length =
301 htons(sizeof(sctp_supported_ext_param_t) + num_ext);
302 sctp_addto_chunk(retval, sizeof(sctp_supported_ext_param_t),
303 &ext_param);
8ee4be37 304 sctp_addto_param(retval, num_ext, extensions);
131a47e3
VY
305 }
306
1da177e4
LT
307 if (sctp_prsctp_enable)
308 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
131a47e3 309
6fc791ee 310 if (sp->adaptation_ind) {
311 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
312 aiparam.param_hdr.length = htons(sizeof(aiparam));
313 aiparam.adaptation_ind = htonl(sp->adaptation_ind);
314 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
315 }
131a47e3 316
730fc3d0
VY
317 /* Add SCTP-AUTH chunks to the parameter list */
318 if (sctp_auth_enable) {
319 sctp_addto_chunk(retval, sizeof(asoc->c.auth_random),
320 asoc->c.auth_random);
321 if (auth_hmacs)
322 sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
323 auth_hmacs);
324 if (auth_chunks)
325 sctp_addto_chunk(retval, ntohs(auth_chunks->length),
326 auth_chunks);
327 }
1da177e4 328nodata:
a51482bd 329 kfree(addrs.v);
1da177e4
LT
330 return retval;
331}
332
333struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
334 const struct sctp_chunk *chunk,
dd0fc66f 335 gfp_t gfp, int unkparam_len)
1da177e4
LT
336{
337 sctp_inithdr_t initack;
338 struct sctp_chunk *retval;
339 union sctp_params addrs;
6fc791ee 340 struct sctp_sock *sp;
1da177e4
LT
341 int addrs_len;
342 sctp_cookie_param_t *cookie;
343 int cookie_len;
344 size_t chunksize;
0f3fffd8 345 sctp_adaptation_ind_param_t aiparam;
131a47e3
VY
346 sctp_supported_ext_param_t ext_param;
347 int num_ext = 0;
348 __u8 extensions[3];
730fc3d0
VY
349 sctp_paramhdr_t *auth_chunks = NULL,
350 *auth_hmacs = NULL,
351 *auth_random = NULL;
1da177e4
LT
352
353 retval = NULL;
354
355 /* Note: there may be no addresses to embed. */
356 addrs = sctp_bind_addrs_to_raw(&asoc->base.bind_addr, &addrs_len, gfp);
357
358 initack.init_tag = htonl(asoc->c.my_vtag);
359 initack.a_rwnd = htonl(asoc->rwnd);
360 initack.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
361 initack.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
362 initack.initial_tsn = htonl(asoc->c.initial_tsn);
363
364 /* FIXME: We really ought to build the cookie right
365 * into the packet instead of allocating more fresh memory.
366 */
367 cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
368 addrs.v, addrs_len);
369 if (!cookie)
370 goto nomem_cookie;
371
372 /* Calculate the total size of allocation, include the reserved
373 * space for reporting unknown parameters if it is specified.
374 */
6fc791ee 375 sp = sctp_sk(asoc->base.sk);
1da177e4
LT
376 chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;
377
d808ad9a 378 /* Tell peer that we'll do ECN only if peer advertised such cap. */
1da177e4
LT
379 if (asoc->peer.ecn_capable)
380 chunksize += sizeof(ecap_param);
381
5ffad5ac 382 if (asoc->peer.prsctp_capable)
036b579b
VY
383 chunksize += sizeof(prsctp_param);
384
5ffad5ac 385 if (asoc->peer.asconf_capable) {
131a47e3
VY
386 extensions[num_ext] = SCTP_CID_ASCONF;
387 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
388 num_ext += 2;
389 }
390
6fc791ee 391 if (sp->adaptation_ind)
392 chunksize += sizeof(aiparam);
1da177e4 393
730fc3d0
VY
394 if (asoc->peer.auth_capable) {
395 auth_random = (sctp_paramhdr_t *)asoc->c.auth_random;
396 chunksize += ntohs(auth_random->length);
397
398 auth_hmacs = (sctp_paramhdr_t *)asoc->c.auth_hmacs;
399 if (auth_hmacs->length)
400 chunksize += ntohs(auth_hmacs->length);
401 else
402 auth_hmacs = NULL;
403
404 auth_chunks = (sctp_paramhdr_t *)asoc->c.auth_chunks;
405 if (auth_chunks->length)
406 chunksize += ntohs(auth_chunks->length);
407 else
408 auth_chunks = NULL;
409
410 extensions[num_ext] = SCTP_CID_AUTH;
411 num_ext += 1;
412 }
413
8ee4be37
VY
414 if (num_ext)
415 chunksize += sizeof(sctp_supported_ext_param_t) + num_ext;
416
1da177e4
LT
417 /* Now allocate and fill out the chunk. */
418 retval = sctp_make_chunk(asoc, SCTP_CID_INIT_ACK, 0, chunksize);
419 if (!retval)
420 goto nomem_chunk;
421
b99a4d53
DC
422 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
423 *
424 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
425 * HEARTBEAT ACK, * etc.) to the same destination transport
426 * address from which it received the DATA or control chunk
427 * to which it is replying.
428 *
429 * [INIT ACK back to where the INIT came from.]
1da177e4
LT
430 */
431 retval->transport = chunk->transport;
b99a4d53 432
1da177e4
LT
433 retval->subh.init_hdr =
434 sctp_addto_chunk(retval, sizeof(initack), &initack);
435 retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
436 sctp_addto_chunk(retval, cookie_len, cookie);
437 if (asoc->peer.ecn_capable)
438 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
131a47e3
VY
439 if (num_ext) {
440 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
441 ext_param.param_hdr.length =
442 htons(sizeof(sctp_supported_ext_param_t) + num_ext);
443 sctp_addto_chunk(retval, sizeof(sctp_supported_ext_param_t),
444 &ext_param);
8ee4be37 445 sctp_addto_param(retval, num_ext, extensions);
131a47e3 446 }
1da177e4
LT
447 if (asoc->peer.prsctp_capable)
448 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
449
6fc791ee 450 if (sp->adaptation_ind) {
451 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
452 aiparam.param_hdr.length = htons(sizeof(aiparam));
453 aiparam.adaptation_ind = htonl(sp->adaptation_ind);
454 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
455 }
1da177e4 456
730fc3d0
VY
457 if (asoc->peer.auth_capable) {
458 sctp_addto_chunk(retval, ntohs(auth_random->length),
459 auth_random);
460 if (auth_hmacs)
461 sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
462 auth_hmacs);
463 if (auth_chunks)
464 sctp_addto_chunk(retval, ntohs(auth_chunks->length),
465 auth_chunks);
466 }
467
1da177e4
LT
468 /* We need to remove the const qualifier at this point. */
469 retval->asoc = (struct sctp_association *) asoc;
470
1da177e4
LT
471nomem_chunk:
472 kfree(cookie);
473nomem_cookie:
a51482bd 474 kfree(addrs.v);
1da177e4
LT
475 return retval;
476}
477
478/* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
479 *
480 * This chunk is used only during the initialization of an association.
481 * It is sent by the initiator of an association to its peer to complete
482 * the initialization process. This chunk MUST precede any DATA chunk
483 * sent within the association, but MAY be bundled with one or more DATA
484 * chunks in the same packet.
485 *
486 * 0 1 2 3
487 * 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
488 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
489 * | Type = 10 |Chunk Flags | Length |
490 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
491 * / Cookie /
492 * \ \
493 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
494 *
495 * Chunk Flags: 8 bit
496 *
497 * Set to zero on transmit and ignored on receipt.
498 *
499 * Length: 16 bits (unsigned integer)
500 *
501 * Set to the size of the chunk in bytes, including the 4 bytes of
502 * the chunk header and the size of the Cookie.
503 *
504 * Cookie: variable size
505 *
506 * This field must contain the exact cookie received in the
507 * State Cookie parameter from the previous INIT ACK.
508 *
509 * An implementation SHOULD make the cookie as small as possible
510 * to insure interoperability.
511 */
512struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
513 const struct sctp_chunk *chunk)
514{
515 struct sctp_chunk *retval;
516 void *cookie;
517 int cookie_len;
518
519 cookie = asoc->peer.cookie;
520 cookie_len = asoc->peer.cookie_len;
521
522 /* Build a cookie echo chunk. */
523 retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ECHO, 0, cookie_len);
524 if (!retval)
525 goto nodata;
526 retval->subh.cookie_hdr =
527 sctp_addto_chunk(retval, cookie_len, cookie);
528
529 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
530 *
531 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
532 * HEARTBEAT ACK, * etc.) to the same destination transport
533 * address from which it * received the DATA or control chunk
534 * to which it is replying.
535 *
536 * [COOKIE ECHO back to where the INIT ACK came from.]
537 */
538 if (chunk)
539 retval->transport = chunk->transport;
540
541nodata:
542 return retval;
543}
544
545/* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
546 *
547 * This chunk is used only during the initialization of an
548 * association. It is used to acknowledge the receipt of a COOKIE
549 * ECHO chunk. This chunk MUST precede any DATA or SACK chunk sent
550 * within the association, but MAY be bundled with one or more DATA
551 * chunks or SACK chunk in the same SCTP packet.
552 *
553 * 0 1 2 3
554 * 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
555 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
556 * | Type = 11 |Chunk Flags | Length = 4 |
557 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
558 *
559 * Chunk Flags: 8 bits
560 *
561 * Set to zero on transmit and ignored on receipt.
562 */
563struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
564 const struct sctp_chunk *chunk)
565{
566 struct sctp_chunk *retval;
567
568 retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ACK, 0, 0);
569
570 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
571 *
572 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
573 * HEARTBEAT ACK, * etc.) to the same destination transport
574 * address from which it * received the DATA or control chunk
575 * to which it is replying.
576 *
577 * [COOKIE ACK back to where the COOKIE ECHO came from.]
578 */
579 if (retval && chunk)
580 retval->transport = chunk->transport;
581
582 return retval;
583}
584
585/*
586 * Appendix A: Explicit Congestion Notification:
587 * CWR:
588 *
589 * RFC 2481 details a specific bit for a sender to send in the header of
590 * its next outbound TCP segment to indicate to its peer that it has
591 * reduced its congestion window. This is termed the CWR bit. For
592 * SCTP the same indication is made by including the CWR chunk.
593 * This chunk contains one data element, i.e. the TSN number that
594 * was sent in the ECNE chunk. This element represents the lowest
595 * TSN number in the datagram that was originally marked with the
596 * CE bit.
597 *
598 * 0 1 2 3
599 * 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
600 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
601 * | Chunk Type=13 | Flags=00000000| Chunk Length = 8 |
602 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
603 * | Lowest TSN Number |
604 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
605 *
606 * Note: The CWR is considered a Control chunk.
607 */
608struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
609 const __u32 lowest_tsn,
610 const struct sctp_chunk *chunk)
611{
612 struct sctp_chunk *retval;
613 sctp_cwrhdr_t cwr;
614
615 cwr.lowest_tsn = htonl(lowest_tsn);
616 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_CWR, 0,
617 sizeof(sctp_cwrhdr_t));
618
619 if (!retval)
620 goto nodata;
621
622 retval->subh.ecn_cwr_hdr =
623 sctp_addto_chunk(retval, sizeof(cwr), &cwr);
624
625 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
626 *
627 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
628 * HEARTBEAT ACK, * etc.) to the same destination transport
629 * address from which it * received the DATA or control chunk
630 * to which it is replying.
631 *
632 * [Report a reduced congestion window back to where the ECNE
633 * came from.]
634 */
635 if (chunk)
636 retval->transport = chunk->transport;
637
638nodata:
639 return retval;
640}
641
642/* Make an ECNE chunk. This is a congestion experienced report. */
643struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
644 const __u32 lowest_tsn)
645{
646 struct sctp_chunk *retval;
647 sctp_ecnehdr_t ecne;
648
649 ecne.lowest_tsn = htonl(lowest_tsn);
650 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_ECNE, 0,
651 sizeof(sctp_ecnehdr_t));
652 if (!retval)
653 goto nodata;
654 retval->subh.ecne_hdr =
655 sctp_addto_chunk(retval, sizeof(ecne), &ecne);
656
657nodata:
658 return retval;
659}
660
661/* Make a DATA chunk for the given association from the provided
662 * parameters. However, do not populate the data payload.
663 */
664struct sctp_chunk *sctp_make_datafrag_empty(struct sctp_association *asoc,
665 const struct sctp_sndrcvinfo *sinfo,
666 int data_len, __u8 flags, __u16 ssn)
667{
668 struct sctp_chunk *retval;
669 struct sctp_datahdr dp;
670 int chunk_len;
671
672 /* We assign the TSN as LATE as possible, not here when
673 * creating the chunk.
674 */
675 dp.tsn = 0;
676 dp.stream = htons(sinfo->sinfo_stream);
677 dp.ppid = sinfo->sinfo_ppid;
678
679 /* Set the flags for an unordered send. */
eaa5c54d 680 if (sinfo->sinfo_flags & SCTP_UNORDERED) {
1da177e4
LT
681 flags |= SCTP_DATA_UNORDERED;
682 dp.ssn = 0;
683 } else
684 dp.ssn = htons(ssn);
685
686 chunk_len = sizeof(dp) + data_len;
687 retval = sctp_make_chunk(asoc, SCTP_CID_DATA, flags, chunk_len);
688 if (!retval)
689 goto nodata;
690
691 retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
692 memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
693
694nodata:
695 return retval;
696}
697
698/* Create a selective ackowledgement (SACK) for the given
699 * association. This reports on which TSN's we've seen to date,
700 * including duplicates and gaps.
701 */
702struct sctp_chunk *sctp_make_sack(const struct sctp_association *asoc)
703{
704 struct sctp_chunk *retval;
705 struct sctp_sackhdr sack;
706 int len;
707 __u32 ctsn;
708 __u16 num_gabs, num_dup_tsns;
709 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
02015180 710 struct sctp_gap_ack_block gabs[SCTP_MAX_GABS];
1da177e4 711
02015180 712 memset(gabs, 0, sizeof(gabs));
1da177e4
LT
713 ctsn = sctp_tsnmap_get_ctsn(map);
714 SCTP_DEBUG_PRINTK("sackCTSNAck sent: 0x%x.\n", ctsn);
715
716 /* How much room is needed in the chunk? */
02015180 717 num_gabs = sctp_tsnmap_num_gabs(map, gabs);
1da177e4
LT
718 num_dup_tsns = sctp_tsnmap_num_dups(map);
719
720 /* Initialize the SACK header. */
721 sack.cum_tsn_ack = htonl(ctsn);
722 sack.a_rwnd = htonl(asoc->a_rwnd);
723 sack.num_gap_ack_blocks = htons(num_gabs);
724 sack.num_dup_tsns = htons(num_dup_tsns);
725
726 len = sizeof(sack)
727 + sizeof(struct sctp_gap_ack_block) * num_gabs
728 + sizeof(__u32) * num_dup_tsns;
729
730 /* Create the chunk. */
731 retval = sctp_make_chunk(asoc, SCTP_CID_SACK, 0, len);
732 if (!retval)
733 goto nodata;
734
735 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
736 *
737 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
738 * HEARTBEAT ACK, etc.) to the same destination transport
739 * address from which it received the DATA or control chunk to
740 * which it is replying. This rule should also be followed if
741 * the endpoint is bundling DATA chunks together with the
742 * reply chunk.
743 *
744 * However, when acknowledging multiple DATA chunks received
745 * in packets from different source addresses in a single
746 * SACK, the SACK chunk may be transmitted to one of the
747 * destination transport addresses from which the DATA or
748 * control chunks being acknowledged were received.
749 *
750 * [BUG: We do not implement the following paragraph.
751 * Perhaps we should remember the last transport we used for a
752 * SACK and avoid that (if possible) if we have seen any
753 * duplicates. --piggy]
754 *
755 * When a receiver of a duplicate DATA chunk sends a SACK to a
756 * multi- homed endpoint it MAY be beneficial to vary the
757 * destination address and not use the source address of the
758 * DATA chunk. The reason being that receiving a duplicate
759 * from a multi-homed endpoint might indicate that the return
760 * path (as specified in the source address of the DATA chunk)
761 * for the SACK is broken.
762 *
763 * [Send to the address from which we last received a DATA chunk.]
764 */
765 retval->transport = asoc->peer.last_data_from;
766
767 retval->subh.sack_hdr =
768 sctp_addto_chunk(retval, sizeof(sack), &sack);
769
770 /* Add the gap ack block information. */
771 if (num_gabs)
772 sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
02015180 773 gabs);
1da177e4
LT
774
775 /* Add the duplicate TSN information. */
776 if (num_dup_tsns)
777 sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
778 sctp_tsnmap_get_dups(map));
779
780nodata:
781 return retval;
782}
783
784/* Make a SHUTDOWN chunk. */
785struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
786 const struct sctp_chunk *chunk)
787{
788 struct sctp_chunk *retval;
789 sctp_shutdownhdr_t shut;
790 __u32 ctsn;
791
792 ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
793 shut.cum_tsn_ack = htonl(ctsn);
794
795 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN, 0,
796 sizeof(sctp_shutdownhdr_t));
797 if (!retval)
798 goto nodata;
799
800 retval->subh.shutdown_hdr =
801 sctp_addto_chunk(retval, sizeof(shut), &shut);
802
803 if (chunk)
804 retval->transport = chunk->transport;
805nodata:
806 return retval;
807}
808
809struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
810 const struct sctp_chunk *chunk)
811{
812 struct sctp_chunk *retval;
813
814 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0);
815
816 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
817 *
818 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
819 * HEARTBEAT ACK, * etc.) to the same destination transport
820 * address from which it * received the DATA or control chunk
821 * to which it is replying.
822 *
823 * [ACK back to where the SHUTDOWN came from.]
824 */
825 if (retval && chunk)
826 retval->transport = chunk->transport;
827
828 return retval;
829}
830
831struct sctp_chunk *sctp_make_shutdown_complete(
832 const struct sctp_association *asoc,
833 const struct sctp_chunk *chunk)
834{
835 struct sctp_chunk *retval;
836 __u8 flags = 0;
837
047a2428
JF
838 /* Set the T-bit if we have no association (vtag will be
839 * reflected)
840 */
1da177e4
LT
841 flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
842
843 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags, 0);
844
845 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
846 *
847 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
848 * HEARTBEAT ACK, * etc.) to the same destination transport
849 * address from which it * received the DATA or control chunk
850 * to which it is replying.
851 *
852 * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
853 * came from.]
854 */
855 if (retval && chunk)
856 retval->transport = chunk->transport;
857
d808ad9a 858 return retval;
1da177e4
LT
859}
860
861/* Create an ABORT. Note that we set the T bit if we have no
047a2428 862 * association, except when responding to an INIT (sctpimpguide 2.41).
1da177e4
LT
863 */
864struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
865 const struct sctp_chunk *chunk,
866 const size_t hint)
867{
868 struct sctp_chunk *retval;
869 __u8 flags = 0;
870
047a2428
JF
871 /* Set the T-bit if we have no association and 'chunk' is not
872 * an INIT (vtag will be reflected).
873 */
874 if (!asoc) {
875 if (chunk && chunk->chunk_hdr &&
876 chunk->chunk_hdr->type == SCTP_CID_INIT)
877 flags = 0;
878 else
879 flags = SCTP_CHUNK_FLAG_T;
880 }
1da177e4
LT
881
882 retval = sctp_make_chunk(asoc, SCTP_CID_ABORT, flags, hint);
883
884 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
885 *
886 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
887 * HEARTBEAT ACK, * etc.) to the same destination transport
888 * address from which it * received the DATA or control chunk
889 * to which it is replying.
890 *
891 * [ABORT back to where the offender came from.]
892 */
893 if (retval && chunk)
894 retval->transport = chunk->transport;
895
896 return retval;
897}
898
899/* Helper to create ABORT with a NO_USER_DATA error. */
900struct sctp_chunk *sctp_make_abort_no_data(
901 const struct sctp_association *asoc,
902 const struct sctp_chunk *chunk, __u32 tsn)
903{
904 struct sctp_chunk *retval;
9f81bcd9 905 __be32 payload;
1da177e4
LT
906
907 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t)
908 + sizeof(tsn));
909
910 if (!retval)
911 goto no_mem;
912
913 /* Put the tsn back into network byte order. */
914 payload = htonl(tsn);
00f1c2df
WY
915 sctp_init_cause(retval, SCTP_ERROR_NO_DATA, sizeof(payload));
916 sctp_addto_chunk(retval, sizeof(payload), (const void *)&payload);
1da177e4
LT
917
918 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
919 *
920 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
921 * HEARTBEAT ACK, * etc.) to the same destination transport
922 * address from which it * received the DATA or control chunk
923 * to which it is replying.
924 *
925 * [ABORT back to where the offender came from.]
926 */
927 if (chunk)
928 retval->transport = chunk->transport;
929
930no_mem:
931 return retval;
932}
933
934/* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error. */
935struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
c164a9ba
SS
936 const struct msghdr *msg,
937 size_t paylen)
1da177e4
LT
938{
939 struct sctp_chunk *retval;
c164a9ba
SS
940 void *payload = NULL;
941 int err;
1da177e4 942
c164a9ba 943 retval = sctp_make_abort(asoc, NULL, sizeof(sctp_errhdr_t) + paylen);
1da177e4
LT
944 if (!retval)
945 goto err_chunk;
946
947 if (paylen) {
948 /* Put the msg_iov together into payload. */
c164a9ba 949 payload = kmalloc(paylen, GFP_KERNEL);
1da177e4
LT
950 if (!payload)
951 goto err_payload;
c164a9ba
SS
952
953 err = memcpy_fromiovec(payload, msg->msg_iov, paylen);
954 if (err < 0)
955 goto err_copy;
1da177e4
LT
956 }
957
00f1c2df
WY
958 sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, paylen);
959 sctp_addto_chunk(retval, paylen, payload);
1da177e4
LT
960
961 if (paylen)
962 kfree(payload);
963
964 return retval;
965
966err_copy:
967 kfree(payload);
968err_payload:
969 sctp_chunk_free(retval);
970 retval = NULL;
971err_chunk:
972 return retval;
973}
974
5c94bf86
AB
975/* Append bytes to the end of a parameter. Will panic if chunk is not big
976 * enough.
977 */
978static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
979 const void *data)
980{
981 void *target;
982 int chunklen = ntohs(chunk->chunk_hdr->length);
983
984 target = skb_put(chunk->skb, len);
985
6383cfb3
VY
986 if (data)
987 memcpy(target, data, len);
988 else
989 memset(target, 0, len);
5c94bf86
AB
990
991 /* Adjust the chunk length field. */
992 chunk->chunk_hdr->length = htons(chunklen + len);
993 chunk->chunk_end = skb_tail_pointer(chunk->skb);
994
995 return target;
996}
997
d808ad9a 998/* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
1da177e4
LT
999struct sctp_chunk *sctp_make_abort_violation(
1000 const struct sctp_association *asoc,
1001 const struct sctp_chunk *chunk,
1002 const __u8 *payload,
1003 const size_t paylen)
1004{
1005 struct sctp_chunk *retval;
1006 struct sctp_paramhdr phdr;
1007
1008 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t) + paylen
00f1c2df 1009 + sizeof(sctp_paramhdr_t));
1da177e4
LT
1010 if (!retval)
1011 goto end;
1012
00f1c2df
WY
1013 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, paylen
1014 + sizeof(sctp_paramhdr_t));
1da177e4
LT
1015
1016 phdr.type = htons(chunk->chunk_hdr->type);
1017 phdr.length = chunk->chunk_hdr->length;
00f1c2df
WY
1018 sctp_addto_chunk(retval, paylen, payload);
1019 sctp_addto_param(retval, sizeof(sctp_paramhdr_t), &phdr);
1da177e4
LT
1020
1021end:
1022 return retval;
1023}
1024
ba016670
WY
1025struct sctp_chunk *sctp_make_violation_paramlen(
1026 const struct sctp_association *asoc,
1027 const struct sctp_chunk *chunk,
1028 struct sctp_paramhdr *param)
1029{
1030 struct sctp_chunk *retval;
1031 static const char error[] = "The following parameter had invalid length:";
1032 size_t payload_len = sizeof(error) + sizeof(sctp_errhdr_t) +
1033 sizeof(sctp_paramhdr_t);
1034
1035 retval = sctp_make_abort(asoc, chunk, payload_len);
1036 if (!retval)
1037 goto nodata;
1038
1039 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION,
1040 sizeof(error) + sizeof(sctp_paramhdr_t));
1041 sctp_addto_chunk(retval, sizeof(error), error);
1042 sctp_addto_param(retval, sizeof(sctp_paramhdr_t), param);
1043
1044nodata:
1045 return retval;
1046}
1047
1da177e4
LT
1048/* Make a HEARTBEAT chunk. */
1049struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
1050 const struct sctp_transport *transport,
1051 const void *payload, const size_t paylen)
1052{
1053 struct sctp_chunk *retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT,
1054 0, paylen);
1055
1056 if (!retval)
1057 goto nodata;
1058
1059 /* Cast away the 'const', as this is just telling the chunk
1060 * what transport it belongs to.
1061 */
1062 retval->transport = (struct sctp_transport *) transport;
1063 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
1064
1065nodata:
1066 return retval;
1067}
1068
1069struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
1070 const struct sctp_chunk *chunk,
1071 const void *payload, const size_t paylen)
1072{
1073 struct sctp_chunk *retval;
1074
1075 retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen);
1076 if (!retval)
1077 goto nodata;
1078
1079 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
1080
1081 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1082 *
1083 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1084 * HEARTBEAT ACK, * etc.) to the same destination transport
1085 * address from which it * received the DATA or control chunk
1086 * to which it is replying.
1087 *
1088 * [HBACK back to where the HEARTBEAT came from.]
1089 */
1090 if (chunk)
1091 retval->transport = chunk->transport;
1092
1093nodata:
1094 return retval;
1095}
1096
1097/* Create an Operation Error chunk with the specified space reserved.
1098 * This routine can be used for containing multiple causes in the chunk.
1099 */
1100static struct sctp_chunk *sctp_make_op_error_space(
1101 const struct sctp_association *asoc,
1102 const struct sctp_chunk *chunk,
1103 size_t size)
1104{
1105 struct sctp_chunk *retval;
1106
1107 retval = sctp_make_chunk(asoc, SCTP_CID_ERROR, 0,
1108 sizeof(sctp_errhdr_t) + size);
1109 if (!retval)
1110 goto nodata;
1111
1112 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1113 *
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.
1118 *
1119 */
1120 if (chunk)
1121 retval->transport = chunk->transport;
1122
1123nodata:
1124 return retval;
1125}
1126
1127/* Create an Operation Error chunk. */
1128struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
1129 const struct sctp_chunk *chunk,
63706c5c 1130 __be16 cause_code, const void *payload,
6383cfb3 1131 size_t paylen, size_t reserve_tail)
1da177e4
LT
1132{
1133 struct sctp_chunk *retval;
1134
6383cfb3 1135 retval = sctp_make_op_error_space(asoc, chunk, paylen + reserve_tail);
1da177e4
LT
1136 if (!retval)
1137 goto nodata;
1138
6383cfb3 1139 sctp_init_cause(retval, cause_code, paylen + reserve_tail);
00f1c2df 1140 sctp_addto_chunk(retval, paylen, payload);
6383cfb3
VY
1141 if (reserve_tail)
1142 sctp_addto_param(retval, reserve_tail, NULL);
1da177e4
LT
1143
1144nodata:
1145 return retval;
1146}
1147
4cd57c80
VY
1148struct sctp_chunk *sctp_make_auth(const struct sctp_association *asoc)
1149{
1150 struct sctp_chunk *retval;
1151 struct sctp_hmac *hmac_desc;
1152 struct sctp_authhdr auth_hdr;
1153 __u8 *hmac;
1154
1155 /* Get the first hmac that the peer told us to use */
1156 hmac_desc = sctp_auth_asoc_get_hmac(asoc);
1157 if (unlikely(!hmac_desc))
1158 return NULL;
1159
1160 retval = sctp_make_chunk(asoc, SCTP_CID_AUTH, 0,
1161 hmac_desc->hmac_len + sizeof(sctp_authhdr_t));
1162 if (!retval)
1163 return NULL;
1164
1165 auth_hdr.hmac_id = htons(hmac_desc->hmac_id);
1166 auth_hdr.shkey_id = htons(asoc->active_key_id);
1167
1168 retval->subh.auth_hdr = sctp_addto_chunk(retval, sizeof(sctp_authhdr_t),
1169 &auth_hdr);
1170
1171 hmac = skb_put(retval->skb, hmac_desc->hmac_len);
1172 memset(hmac, 0, hmac_desc->hmac_len);
1173
1174 /* Adjust the chunk header to include the empty MAC */
1175 retval->chunk_hdr->length =
1176 htons(ntohs(retval->chunk_hdr->length) + hmac_desc->hmac_len);
1177 retval->chunk_end = skb_tail_pointer(retval->skb);
1178
1179 return retval;
1180}
1181
1182
1da177e4
LT
1183/********************************************************************
1184 * 2nd Level Abstractions
1185 ********************************************************************/
1186
1187/* Turn an skb into a chunk.
1188 * FIXME: Eventually move the structure directly inside the skb->cb[].
1189 */
1190struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
1191 const struct sctp_association *asoc,
1192 struct sock *sk)
1193{
1194 struct sctp_chunk *retval;
1195
c3762229 1196 retval = kmem_cache_zalloc(sctp_chunk_cachep, GFP_ATOMIC);
1da177e4
LT
1197
1198 if (!retval)
1199 goto nodata;
1da177e4
LT
1200
1201 if (!sk) {
1202 SCTP_DEBUG_PRINTK("chunkifying skb %p w/o an sk\n", skb);
1203 }
1204
79af02c2 1205 INIT_LIST_HEAD(&retval->list);
1da177e4
LT
1206 retval->skb = skb;
1207 retval->asoc = (struct sctp_association *)asoc;
1208 retval->resent = 0;
1209 retval->has_tsn = 0;
1210 retval->has_ssn = 0;
1211 retval->rtt_in_progress = 0;
1212 retval->sent_at = 0;
1213 retval->singleton = 1;
1214 retval->end_of_packet = 0;
1215 retval->ecn_ce_done = 0;
1216 retval->pdiscard = 0;
1217
1218 /* sctpimpguide-05.txt Section 2.8.2
1219 * M1) Each time a new DATA chunk is transmitted
1220 * set the 'TSN.Missing.Report' count for that TSN to 0. The
1221 * 'TSN.Missing.Report' count will be used to determine missing chunks
1222 * and when to fast retransmit.
1223 */
1224 retval->tsn_missing_report = 0;
1225 retval->tsn_gap_acked = 0;
c226ef9b 1226 retval->fast_retransmit = SCTP_CAN_FRTX;
1da177e4
LT
1227
1228 /* If this is a fragmented message, track all fragments
1229 * of the message (for SEND_FAILED).
1230 */
1231 retval->msg = NULL;
1232
1233 /* Polish the bead hole. */
1234 INIT_LIST_HEAD(&retval->transmitted_list);
1235 INIT_LIST_HEAD(&retval->frag_list);
1236 SCTP_DBG_OBJCNT_INC(chunk);
1237 atomic_set(&retval->refcnt, 1);
1238
1239nodata:
1240 return retval;
1241}
1242
1243/* Set chunk->source and dest based on the IP header in chunk->skb. */
1244void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
1245 union sctp_addr *dest)
1246{
f235fca3 1247 memcpy(&chunk->source, src, sizeof(union sctp_addr));
16b0a030 1248 memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
1da177e4
LT
1249}
1250
1251/* Extract the source address from a chunk. */
1252const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
1253{
1254 /* If we have a known transport, use that. */
1255 if (chunk->transport) {
6a1e5f33 1256 return &chunk->transport->ipaddr;
1da177e4
LT
1257 } else {
1258 /* Otherwise, extract it from the IP header. */
6a1e5f33 1259 return &chunk->source;
1da177e4
LT
1260 }
1261}
1262
1263/* Create a new chunk, setting the type and flags headers from the
1264 * arguments, reserving enough space for a 'paylen' byte payload.
1265 */
1266SCTP_STATIC
1267struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
1268 __u8 type, __u8 flags, int paylen)
1269{
1270 struct sctp_chunk *retval;
1271 sctp_chunkhdr_t *chunk_hdr;
1272 struct sk_buff *skb;
1273 struct sock *sk;
1274
1275 /* No need to allocate LL here, as this is only a chunk. */
1276 skb = alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t) + paylen),
1277 GFP_ATOMIC);
1278 if (!skb)
1279 goto nodata;
1280
1281 /* Make room for the chunk header. */
1282 chunk_hdr = (sctp_chunkhdr_t *)skb_put(skb, sizeof(sctp_chunkhdr_t));
1283 chunk_hdr->type = type;
1284 chunk_hdr->flags = flags;
1285 chunk_hdr->length = htons(sizeof(sctp_chunkhdr_t));
1286
1287 sk = asoc ? asoc->base.sk : NULL;
1288 retval = sctp_chunkify(skb, asoc, sk);
1289 if (!retval) {
1290 kfree_skb(skb);
1291 goto nodata;
1292 }
1293
1294 retval->chunk_hdr = chunk_hdr;
1295 retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(struct sctp_chunkhdr);
1296
4cd57c80
VY
1297 /* Determine if the chunk needs to be authenticated */
1298 if (sctp_auth_send_cid(type, asoc))
1299 retval->auth = 1;
1300
1da177e4
LT
1301 /* Set the skb to the belonging sock for accounting. */
1302 skb->sk = sk;
1303
1304 return retval;
1305nodata:
1306 return NULL;
1307}
1308
1309
1310/* Release the memory occupied by a chunk. */
1311static void sctp_chunk_destroy(struct sctp_chunk *chunk)
1312{
a08de64d
VY
1313 BUG_ON(!list_empty(&chunk->list));
1314 list_del_init(&chunk->transmitted_list);
1315
1da177e4
LT
1316 /* Free the chunk skb data and the SCTP_chunk stub itself. */
1317 dev_kfree_skb(chunk->skb);
1318
1319 SCTP_DBG_OBJCNT_DEC(chunk);
1320 kmem_cache_free(sctp_chunk_cachep, chunk);
1321}
1322
1323/* Possibly, free the chunk. */
1324void sctp_chunk_free(struct sctp_chunk *chunk)
1325{
1da177e4
LT
1326 /* Release our reference on the message tracker. */
1327 if (chunk->msg)
1328 sctp_datamsg_put(chunk->msg);
1329
1330 sctp_chunk_put(chunk);
1331}
1332
1333/* Grab a reference to the chunk. */
1334void sctp_chunk_hold(struct sctp_chunk *ch)
1335{
1336 atomic_inc(&ch->refcnt);
1337}
1338
1339/* Release a reference to the chunk. */
1340void sctp_chunk_put(struct sctp_chunk *ch)
1341{
1342 if (atomic_dec_and_test(&ch->refcnt))
1343 sctp_chunk_destroy(ch);
1344}
1345
1346/* Append bytes to the end of a chunk. Will panic if chunk is not big
1347 * enough.
1348 */
1349void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
1350{
1351 void *target;
1352 void *padding;
1353 int chunklen = ntohs(chunk->chunk_hdr->length);
8d614ade 1354 int padlen = WORD_ROUND(chunklen) - chunklen;
1da177e4
LT
1355
1356 padding = skb_put(chunk->skb, padlen);
1357 target = skb_put(chunk->skb, len);
1358
1359 memset(padding, 0, padlen);
1360 memcpy(target, data, len);
1361
1362 /* Adjust the chunk length field. */
1363 chunk->chunk_hdr->length = htons(chunklen + padlen + len);
27a884dc 1364 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1da177e4
LT
1365
1366 return target;
1367}
1368
1369/* Append bytes from user space to the end of a chunk. Will panic if
1370 * chunk is not big enough.
1371 * Returns a kernel err value.
1372 */
1373int sctp_user_addto_chunk(struct sctp_chunk *chunk, int off, int len,
1374 struct iovec *data)
1375{
1376 __u8 *target;
1377 int err = 0;
1378
1379 /* Make room in chunk for data. */
1380 target = skb_put(chunk->skb, len);
1381
1382 /* Copy data (whole iovec) into chunk */
1383 if ((err = memcpy_fromiovecend(target, data, off, len)))
1384 goto out;
1385
1386 /* Adjust the chunk length field. */
1387 chunk->chunk_hdr->length =
1388 htons(ntohs(chunk->chunk_hdr->length) + len);
27a884dc 1389 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1da177e4
LT
1390
1391out:
1392 return err;
1393}
1394
1395/* Helper function to assign a TSN if needed. This assumes that both
1396 * the data_hdr and association have already been assigned.
1397 */
1398void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
1399{
ab3e5e7b
VY
1400 struct sctp_datamsg *msg;
1401 struct sctp_chunk *lchunk;
1402 struct sctp_stream *stream;
1da177e4
LT
1403 __u16 ssn;
1404 __u16 sid;
1405
1406 if (chunk->has_ssn)
1407 return;
1408
ab3e5e7b
VY
1409 /* All fragments will be on the same stream */
1410 sid = ntohs(chunk->subh.data_hdr->stream);
1411 stream = &chunk->asoc->ssnmap->out;
1da177e4 1412
ab3e5e7b
VY
1413 /* Now assign the sequence number to the entire message.
1414 * All fragments must have the same stream sequence number.
1415 */
1416 msg = chunk->msg;
1417 list_for_each_entry(lchunk, &msg->chunks, frag_list) {
1418 if (lchunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
1419 ssn = 0;
1420 } else {
1421 if (lchunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
1422 ssn = sctp_ssn_next(stream, sid);
1423 else
1424 ssn = sctp_ssn_peek(stream, sid);
1425 }
1426
1427 lchunk->subh.data_hdr->ssn = htons(ssn);
1428 lchunk->has_ssn = 1;
1429 }
1da177e4
LT
1430}
1431
1432/* Helper function to assign a TSN if needed. This assumes that both
1433 * the data_hdr and association have already been assigned.
1434 */
1435void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
1436{
1437 if (!chunk->has_tsn) {
1438 /* This is the last possible instant to
1439 * assign a TSN.
1440 */
1441 chunk->subh.data_hdr->tsn =
1442 htonl(sctp_association_get_next_tsn(chunk->asoc));
1443 chunk->has_tsn = 1;
1444 }
1445}
1446
1447/* Create a CLOSED association to use with an incoming packet. */
1448struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
3182cd84 1449 struct sctp_chunk *chunk,
dd0fc66f 1450 gfp_t gfp)
1da177e4
LT
1451{
1452 struct sctp_association *asoc;
1453 struct sk_buff *skb;
1454 sctp_scope_t scope;
1455 struct sctp_af *af;
1456
1457 /* Create the bare association. */
1458 scope = sctp_scope(sctp_source(chunk));
1459 asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
1460 if (!asoc)
1461 goto nodata;
1462 asoc->temp = 1;
1463 skb = chunk->skb;
1464 /* Create an entry for the source address of the packet. */
eddc9ec5 1465 af = sctp_get_af_specific(ipver2af(ip_hdr(skb)->version));
1da177e4
LT
1466 if (unlikely(!af))
1467 goto fail;
d55c41b1 1468 af->from_skb(&asoc->c.peer_addr, skb, 1);
1da177e4
LT
1469nodata:
1470 return asoc;
1471
1472fail:
1473 sctp_association_free(asoc);
1474 return NULL;
1475}
1476
1477/* Build a cookie representing asoc.
1478 * This INCLUDES the param header needed to put the cookie in the INIT ACK.
1479 */
1480static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
1481 const struct sctp_association *asoc,
1482 const struct sctp_chunk *init_chunk,
1483 int *cookie_len,
1484 const __u8 *raw_addrs, int addrs_len)
1485{
1486 sctp_cookie_param_t *retval;
1487 struct sctp_signed_cookie *cookie;
1488 struct scatterlist sg;
1489 int headersize, bodysize;
1490 unsigned int keylen;
1491 char *key;
1492
9834a2bb
VY
1493 /* Header size is static data prior to the actual cookie, including
1494 * any padding.
1495 */
d808ad9a
YH
1496 headersize = sizeof(sctp_paramhdr_t) +
1497 (sizeof(struct sctp_signed_cookie) -
9834a2bb 1498 sizeof(struct sctp_cookie));
1da177e4
LT
1499 bodysize = sizeof(struct sctp_cookie)
1500 + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
1501
1502 /* Pad out the cookie to a multiple to make the signature
1503 * functions simpler to write.
1504 */
1505 if (bodysize % SCTP_COOKIE_MULTIPLE)
1506 bodysize += SCTP_COOKIE_MULTIPLE
1507 - (bodysize % SCTP_COOKIE_MULTIPLE);
1508 *cookie_len = headersize + bodysize;
1509
1da177e4
LT
1510 /* Clear this memory since we are sending this data structure
1511 * out on the network.
1512 */
af997d8c
ACM
1513 retval = kzalloc(*cookie_len, GFP_ATOMIC);
1514 if (!retval)
1515 goto nodata;
1516
1da177e4
LT
1517 cookie = (struct sctp_signed_cookie *) retval->body;
1518
1519 /* Set up the parameter header. */
1520 retval->p.type = SCTP_PARAM_STATE_COOKIE;
1521 retval->p.length = htons(*cookie_len);
1522
1523 /* Copy the cookie part of the association itself. */
1524 cookie->c = asoc->c;
1525 /* Save the raw address list length in the cookie. */
1526 cookie->c.raw_addr_list_len = addrs_len;
1527
1528 /* Remember PR-SCTP capability. */
1529 cookie->c.prsctp_capable = asoc->peer.prsctp_capable;
1530
0f3fffd8
ISJ
1531 /* Save adaptation indication in the cookie. */
1532 cookie->c.adaptation_ind = asoc->peer.adaptation_ind;
1da177e4
LT
1533
1534 /* Set an expiration time for the cookie. */
1535 do_gettimeofday(&cookie->c.expiration);
1536 TIMEVAL_ADD(asoc->cookie_life, cookie->c.expiration);
1537
1538 /* Copy the peer's init packet. */
1539 memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
1540 ntohs(init_chunk->chunk_hdr->length));
1541
1542 /* Copy the raw local address list of the association. */
1543 memcpy((__u8 *)&cookie->c.peer_init[0] +
1544 ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
1545
d808ad9a 1546 if (sctp_sk(ep->base.sk)->hmac) {
1b489e11
HX
1547 struct hash_desc desc;
1548
1da177e4 1549 /* Sign the message. */
68e3f5dd 1550 sg_init_one(&sg, &cookie->c, bodysize);
1da177e4
LT
1551 keylen = SCTP_SECRET_SIZE;
1552 key = (char *)ep->secret_key[ep->current_key];
d808ad9a
YH
1553 desc.tfm = sctp_sk(ep->base.sk)->hmac;
1554 desc.flags = 0;
1da177e4 1555
1b489e11
HX
1556 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1557 crypto_hash_digest(&desc, &sg, bodysize, cookie->signature))
1558 goto free_cookie;
1da177e4
LT
1559 }
1560
1da177e4 1561 return retval;
1b489e11
HX
1562
1563free_cookie:
1564 kfree(retval);
1565nodata:
1566 *cookie_len = 0;
1567 return NULL;
1da177e4
LT
1568}
1569
1570/* Unpack the cookie from COOKIE ECHO chunk, recreating the association. */
1571struct sctp_association *sctp_unpack_cookie(
1572 const struct sctp_endpoint *ep,
1573 const struct sctp_association *asoc,
dd0fc66f 1574 struct sctp_chunk *chunk, gfp_t gfp,
1da177e4
LT
1575 int *error, struct sctp_chunk **errp)
1576{
1577 struct sctp_association *retval = NULL;
1578 struct sctp_signed_cookie *cookie;
1579 struct sctp_cookie *bear_cookie;
1580 int headersize, bodysize, fixed_size;
313e7b4d 1581 __u8 *digest = ep->digest;
1da177e4
LT
1582 struct scatterlist sg;
1583 unsigned int keylen, len;
1584 char *key;
1585 sctp_scope_t scope;
1586 struct sk_buff *skb = chunk->skb;
a61bbcf2 1587 struct timeval tv;
1b489e11 1588 struct hash_desc desc;
1da177e4 1589
9834a2bb
VY
1590 /* Header size is static data prior to the actual cookie, including
1591 * any padding.
1592 */
1593 headersize = sizeof(sctp_chunkhdr_t) +
d808ad9a 1594 (sizeof(struct sctp_signed_cookie) -
9834a2bb 1595 sizeof(struct sctp_cookie));
1da177e4
LT
1596 bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
1597 fixed_size = headersize + sizeof(struct sctp_cookie);
1598
1599 /* Verify that the chunk looks like it even has a cookie.
1600 * There must be enough room for our cookie and our peer's
1601 * INIT chunk.
1602 */
1603 len = ntohs(chunk->chunk_hdr->length);
1604 if (len < fixed_size + sizeof(struct sctp_chunkhdr))
1605 goto malformed;
1606
1607 /* Verify that the cookie has been padded out. */
1608 if (bodysize % SCTP_COOKIE_MULTIPLE)
1609 goto malformed;
1610
1611 /* Process the cookie. */
1612 cookie = chunk->subh.cookie_hdr;
1613 bear_cookie = &cookie->c;
1614
1615 if (!sctp_sk(ep->base.sk)->hmac)
1616 goto no_hmac;
1617
1618 /* Check the signature. */
1619 keylen = SCTP_SECRET_SIZE;
68e3f5dd 1620 sg_init_one(&sg, bear_cookie, bodysize);
1da177e4 1621 key = (char *)ep->secret_key[ep->current_key];
1b489e11
HX
1622 desc.tfm = sctp_sk(ep->base.sk)->hmac;
1623 desc.flags = 0;
1da177e4 1624
8ca84481 1625 memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
1b489e11
HX
1626 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1627 crypto_hash_digest(&desc, &sg, bodysize, digest)) {
1628 *error = -SCTP_IERROR_NOMEM;
1629 goto fail;
1630 }
1da177e4
LT
1631
1632 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1633 /* Try the previous key. */
1634 key = (char *)ep->secret_key[ep->last_key];
8ca84481 1635 memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
1b489e11
HX
1636 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1637 crypto_hash_digest(&desc, &sg, bodysize, digest)) {
1638 *error = -SCTP_IERROR_NOMEM;
1639 goto fail;
1640 }
1da177e4
LT
1641
1642 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1643 /* Yikes! Still bad signature! */
1644 *error = -SCTP_IERROR_BAD_SIG;
1645 goto fail;
1646 }
1647 }
1648
1649no_hmac:
1650 /* IG Section 2.35.2:
1651 * 3) Compare the port numbers and the verification tag contained
1652 * within the COOKIE ECHO chunk to the actual port numbers and the
1653 * verification tag within the SCTP common header of the received
1654 * packet. If these values do not match the packet MUST be silently
1655 * discarded,
1656 */
1657 if (ntohl(chunk->sctp_hdr->vtag) != bear_cookie->my_vtag) {
1658 *error = -SCTP_IERROR_BAD_TAG;
1659 goto fail;
1660 }
1661
9b1dfad0 1662 if (chunk->sctp_hdr->source != bear_cookie->peer_addr.v4.sin_port ||
1da177e4
LT
1663 ntohs(chunk->sctp_hdr->dest) != bear_cookie->my_port) {
1664 *error = -SCTP_IERROR_BAD_PORTS;
1665 goto fail;
1666 }
1667
1668 /* Check to see if the cookie is stale. If there is already
1669 * an association, there is no need to check cookie's expiration
1670 * for init collision case of lost COOKIE ACK.
f236218b
VY
1671 * If skb has been timestamped, then use the stamp, otherwise
1672 * use current time. This introduces a small possibility that
1673 * that a cookie may be considered expired, but his would only slow
1674 * down the new association establishment instead of every packet.
1da177e4 1675 */
f236218b
VY
1676 if (sock_flag(ep->base.sk, SOCK_TIMESTAMP))
1677 skb_get_timestamp(skb, &tv);
1678 else
1679 do_gettimeofday(&tv);
1680
a61bbcf2 1681 if (!asoc && tv_lt(bear_cookie->expiration, tv)) {
1da177e4
LT
1682 /*
1683 * Section 3.3.10.3 Stale Cookie Error (3)
1684 *
1685 * Cause of error
1686 * ---------------
1687 * Stale Cookie Error: Indicates the receipt of a valid State
1688 * Cookie that has expired.
1689 */
1690 len = ntohs(chunk->chunk_hdr->length);
1691 *errp = sctp_make_op_error_space(asoc, chunk, len);
1692 if (*errp) {
a61bbcf2 1693 suseconds_t usecs = (tv.tv_sec -
1da177e4 1694 bear_cookie->expiration.tv_sec) * 1000000L +
a61bbcf2 1695 tv.tv_usec - bear_cookie->expiration.tv_usec;
34bcca28 1696 __be32 n = htonl(usecs);
1da177e4 1697
1da177e4 1698 sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
00f1c2df
WY
1699 sizeof(n));
1700 sctp_addto_chunk(*errp, sizeof(n), &n);
1da177e4
LT
1701 *error = -SCTP_IERROR_STALE_COOKIE;
1702 } else
1703 *error = -SCTP_IERROR_NOMEM;
1704
1705 goto fail;
1706 }
1707
1708 /* Make a new base association. */
1709 scope = sctp_scope(sctp_source(chunk));
1710 retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
1711 if (!retval) {
1712 *error = -SCTP_IERROR_NOMEM;
1713 goto fail;
1714 }
1715
1716 /* Set up our peer's port number. */
1717 retval->peer.port = ntohs(chunk->sctp_hdr->source);
1718
1719 /* Populate the association from the cookie. */
1720 memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
1721
1722 if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
1723 GFP_ATOMIC) < 0) {
1724 *error = -SCTP_IERROR_NOMEM;
1725 goto fail;
1726 }
1727
1728 /* Also, add the destination address. */
1729 if (list_empty(&retval->base.bind_addr.address_list)) {
f57d96b2
VY
1730 sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest,
1731 SCTP_ADDR_SRC, GFP_ATOMIC);
1da177e4
LT
1732 }
1733
1734 retval->next_tsn = retval->c.initial_tsn;
1735 retval->ctsn_ack_point = retval->next_tsn - 1;
1736 retval->addip_serial = retval->c.initial_tsn;
1737 retval->adv_peer_ack_point = retval->ctsn_ack_point;
1738 retval->peer.prsctp_capable = retval->c.prsctp_capable;
0f3fffd8 1739 retval->peer.adaptation_ind = retval->c.adaptation_ind;
1da177e4
LT
1740
1741 /* The INIT stuff will be done by the side effects. */
1742 return retval;
1743
1744fail:
1745 if (retval)
1746 sctp_association_free(retval);
1747
1748 return NULL;
1749
1750malformed:
1751 /* Yikes! The packet is either corrupt or deliberately
1752 * malformed.
1753 */
1754 *error = -SCTP_IERROR_MALFORMED;
1755 goto fail;
1756}
1757
1758/********************************************************************
1759 * 3rd Level Abstractions
1760 ********************************************************************/
1761
1762struct __sctp_missing {
9f81bcd9
AV
1763 __be32 num_missing;
1764 __be16 type;
1da177e4
LT
1765} __attribute__((packed));
1766
1767/*
1768 * Report a missing mandatory parameter.
1769 */
1770static int sctp_process_missing_param(const struct sctp_association *asoc,
1771 sctp_param_t paramtype,
1772 struct sctp_chunk *chunk,
1773 struct sctp_chunk **errp)
1774{
1775 struct __sctp_missing report;
1776 __u16 len;
1777
1778 len = WORD_ROUND(sizeof(report));
1779
1780 /* Make an ERROR chunk, preparing enough room for
1781 * returning multiple unknown parameters.
1782 */
1783 if (!*errp)
1784 *errp = sctp_make_op_error_space(asoc, chunk, len);
1785
1786 if (*errp) {
1787 report.num_missing = htonl(1);
1788 report.type = paramtype;
ebdfcad4 1789 sctp_init_cause(*errp, SCTP_ERROR_MISS_PARAM,
00f1c2df
WY
1790 sizeof(report));
1791 sctp_addto_chunk(*errp, sizeof(report), &report);
1da177e4
LT
1792 }
1793
1794 /* Stop processing this chunk. */
1795 return 0;
1796}
1797
1798/* Report an Invalid Mandatory Parameter. */
1799static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
1800 struct sctp_chunk *chunk,
1801 struct sctp_chunk **errp)
1802{
1803 /* Invalid Mandatory Parameter Error has no payload. */
1804
1805 if (!*errp)
1806 *errp = sctp_make_op_error_space(asoc, chunk, 0);
1807
1808 if (*errp)
00f1c2df 1809 sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, 0);
1da177e4
LT
1810
1811 /* Stop processing this chunk. */
1812 return 0;
1813}
1814
1815static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
1816 struct sctp_paramhdr *param,
1817 const struct sctp_chunk *chunk,
1818 struct sctp_chunk **errp)
1819{
7ab90804
VY
1820 /* This is a fatal error. Any accumulated non-fatal errors are
1821 * not reported.
1822 */
1823 if (*errp)
1824 sctp_chunk_free(*errp);
1825
1da177e4 1826 /* Create an error chunk and fill it in with our payload. */
ba016670 1827 *errp = sctp_make_violation_paramlen(asoc, chunk, param);
1da177e4
LT
1828
1829 return 0;
1830}
1831
1832
1833/* Do not attempt to handle the HOST_NAME parm. However, do
1834 * send back an indicator to the peer.
1835 */
1836static int sctp_process_hn_param(const struct sctp_association *asoc,
1837 union sctp_params param,
1838 struct sctp_chunk *chunk,
1839 struct sctp_chunk **errp)
1840{
1841 __u16 len = ntohs(param.p->length);
1842
7ab90804
VY
1843 /* Processing of the HOST_NAME parameter will generate an
1844 * ABORT. If we've accumulated any non-fatal errors, they
1845 * would be unrecognized parameters and we should not include
1846 * them in the ABORT.
1847 */
1848 if (*errp)
1849 sctp_chunk_free(*errp);
1850
1851 *errp = sctp_make_op_error_space(asoc, chunk, len);
1da177e4 1852
00f1c2df
WY
1853 if (*errp) {
1854 sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED, len);
1855 sctp_addto_chunk(*errp, len, param.v);
1856 }
1da177e4
LT
1857
1858 /* Stop processing this chunk. */
1859 return 0;
1860}
1861
d6701191
VY
1862static int sctp_verify_ext_param(union sctp_params param)
1863{
1864 __u16 num_ext = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
1865 int have_auth = 0;
1866 int have_asconf = 0;
1867 int i;
1868
1869 for (i = 0; i < num_ext; i++) {
1870 switch (param.ext->chunks[i]) {
1871 case SCTP_CID_AUTH:
1872 have_auth = 1;
1873 break;
1874 case SCTP_CID_ASCONF:
1875 case SCTP_CID_ASCONF_ACK:
1876 have_asconf = 1;
1877 break;
1878 }
1879 }
1880
1881 /* ADD-IP Security: The draft requires us to ABORT or ignore the
1882 * INIT/INIT-ACK if ADD-IP is listed, but AUTH is not. Do this
1883 * only if ADD-IP is turned on and we are not backward-compatible
1884 * mode.
1885 */
1886 if (sctp_addip_noauth)
1887 return 1;
1888
1889 if (sctp_addip_enable && !have_auth && have_asconf)
1890 return 0;
1891
1892 return 1;
1893}
1894
131a47e3
VY
1895static void sctp_process_ext_param(struct sctp_association *asoc,
1896 union sctp_params param)
1897{
1898 __u16 num_ext = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
1899 int i;
1900
1901 for (i = 0; i < num_ext; i++) {
1902 switch (param.ext->chunks[i]) {
1903 case SCTP_CID_FWD_TSN:
1904 if (sctp_prsctp_enable &&
1905 !asoc->peer.prsctp_capable)
1906 asoc->peer.prsctp_capable = 1;
1907 break;
730fc3d0
VY
1908 case SCTP_CID_AUTH:
1909 /* if the peer reports AUTH, assume that he
1910 * supports AUTH.
1911 */
0ef46e28
VY
1912 if (sctp_auth_enable)
1913 asoc->peer.auth_capable = 1;
730fc3d0 1914 break;
131a47e3
VY
1915 case SCTP_CID_ASCONF:
1916 case SCTP_CID_ASCONF_ACK:
0ef46e28
VY
1917 if (sctp_addip_enable)
1918 asoc->peer.asconf_capable = 1;
6b2f9cb6 1919 break;
131a47e3
VY
1920 default:
1921 break;
1922 }
1923 }
1924}
1925
1da177e4
LT
1926/* RFC 3.2.1 & the Implementers Guide 2.2.
1927 *
1928 * The Parameter Types are encoded such that the
1929 * highest-order two bits specify the action that must be
1930 * taken if the processing endpoint does not recognize the
1931 * Parameter Type.
1932 *
7ab90804
VY
1933 * 00 - Stop processing this parameter; do not process any further
1934 * parameters within this chunk
1da177e4 1935 *
7ab90804
VY
1936 * 01 - Stop processing this parameter, do not process any further
1937 * parameters within this chunk, and report the unrecognized
1938 * parameter in an 'Unrecognized Parameter' ERROR chunk.
1da177e4
LT
1939 *
1940 * 10 - Skip this parameter and continue processing.
1941 *
1942 * 11 - Skip this parameter and continue processing but
1943 * report the unrecognized parameter in an
7ab90804 1944 * 'Unrecognized Parameter' ERROR chunk.
1da177e4
LT
1945 *
1946 * Return value:
7ab90804
VY
1947 * SCTP_IERROR_NO_ERROR - continue with the chunk
1948 * SCTP_IERROR_ERROR - stop and report an error.
1949 * SCTP_IERROR_NOMEME - out of memory.
1da177e4 1950 */
7ab90804
VY
1951static sctp_ierror_t sctp_process_unk_param(const struct sctp_association *asoc,
1952 union sctp_params param,
1953 struct sctp_chunk *chunk,
1954 struct sctp_chunk **errp)
1da177e4 1955{
7ab90804 1956 int retval = SCTP_IERROR_NO_ERROR;
1da177e4
LT
1957
1958 switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
1959 case SCTP_PARAM_ACTION_DISCARD:
7ab90804 1960 retval = SCTP_IERROR_ERROR;
1da177e4
LT
1961 break;
1962 case SCTP_PARAM_ACTION_SKIP:
1963 break;
7ab90804
VY
1964 case SCTP_PARAM_ACTION_DISCARD_ERR:
1965 retval = SCTP_IERROR_ERROR;
1966 /* Fall through */
1da177e4
LT
1967 case SCTP_PARAM_ACTION_SKIP_ERR:
1968 /* Make an ERROR chunk, preparing enough room for
1969 * returning multiple unknown parameters.
1970 */
1971 if (NULL == *errp)
1972 *errp = sctp_make_op_error_space(asoc, chunk,
1973 ntohs(chunk->chunk_hdr->length));
1974
1975 if (*errp) {
1976 sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
1da177e4 1977 WORD_ROUND(ntohs(param.p->length)));
00f1c2df
WY
1978 sctp_addto_chunk(*errp,
1979 WORD_ROUND(ntohs(param.p->length)),
1980 param.v);
1da177e4
LT
1981 } else {
1982 /* If there is no memory for generating the ERROR
1983 * report as specified, an ABORT will be triggered
1984 * to the peer and the association won't be
1985 * established.
1986 */
7ab90804 1987 retval = SCTP_IERROR_NOMEM;
1da177e4 1988 }
1da177e4
LT
1989 break;
1990 default:
1991 break;
1992 }
1993
1994 return retval;
1995}
1996
7ab90804 1997/* Verify variable length parameters
1da177e4 1998 * Return values:
7ab90804
VY
1999 * SCTP_IERROR_ABORT - trigger an ABORT
2000 * SCTP_IERROR_NOMEM - out of memory (abort)
2001 * SCTP_IERROR_ERROR - stop processing, trigger an ERROR
2002 * SCTP_IERROR_NO_ERROR - continue with the chunk
1da177e4 2003 */
7ab90804
VY
2004static sctp_ierror_t sctp_verify_param(const struct sctp_association *asoc,
2005 union sctp_params param,
2006 sctp_cid_t cid,
2007 struct sctp_chunk *chunk,
2008 struct sctp_chunk **err_chunk)
1da177e4 2009{
72da7b38 2010 struct sctp_hmac_algo_param *hmacs;
7ab90804 2011 int retval = SCTP_IERROR_NO_ERROR;
72da7b38
WY
2012 __u16 n_elt, id = 0;
2013 int i;
1da177e4
LT
2014
2015 /* FIXME - This routine is not looking at each parameter per the
2016 * chunk type, i.e., unrecognized parameters should be further
2017 * identified based on the chunk id.
2018 */
2019
2020 switch (param.p->type) {
2021 case SCTP_PARAM_IPV4_ADDRESS:
2022 case SCTP_PARAM_IPV6_ADDRESS:
2023 case SCTP_PARAM_COOKIE_PRESERVATIVE:
2024 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2025 case SCTP_PARAM_STATE_COOKIE:
2026 case SCTP_PARAM_HEARTBEAT_INFO:
2027 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2028 case SCTP_PARAM_ECN_CAPABLE:
0f3fffd8 2029 case SCTP_PARAM_ADAPTATION_LAYER_IND:
d6701191
VY
2030 break;
2031
131a47e3 2032 case SCTP_PARAM_SUPPORTED_EXT:
d6701191
VY
2033 if (!sctp_verify_ext_param(param))
2034 return SCTP_IERROR_ABORT;
1da177e4
LT
2035 break;
2036
d6de3097
VY
2037 case SCTP_PARAM_SET_PRIMARY:
2038 if (sctp_addip_enable)
2039 break;
2040 goto fallthrough;
2041
1da177e4
LT
2042 case SCTP_PARAM_HOST_NAME_ADDRESS:
2043 /* Tell the peer, we won't support this param. */
7ab90804
VY
2044 sctp_process_hn_param(asoc, param, chunk, err_chunk);
2045 retval = SCTP_IERROR_ABORT;
2046 break;
131a47e3 2047
1da177e4
LT
2048 case SCTP_PARAM_FWD_TSN_SUPPORT:
2049 if (sctp_prsctp_enable)
2050 break;
730fc3d0
VY
2051 goto fallthrough;
2052
2053 case SCTP_PARAM_RANDOM:
2054 if (!sctp_auth_enable)
2055 goto fallthrough;
2056
2057 /* SCTP-AUTH: Secion 6.1
2058 * If the random number is not 32 byte long the association
2059 * MUST be aborted. The ABORT chunk SHOULD contain the error
2060 * cause 'Protocol Violation'.
2061 */
2062 if (SCTP_AUTH_RANDOM_LENGTH !=
7ab90804
VY
2063 ntohs(param.p->length) - sizeof(sctp_paramhdr_t)) {
2064 sctp_process_inv_paramlength(asoc, param.p,
730fc3d0 2065 chunk, err_chunk);
7ab90804
VY
2066 retval = SCTP_IERROR_ABORT;
2067 }
730fc3d0
VY
2068 break;
2069
2070 case SCTP_PARAM_CHUNKS:
2071 if (!sctp_auth_enable)
2072 goto fallthrough;
2073
2074 /* SCTP-AUTH: Section 3.2
2075 * The CHUNKS parameter MUST be included once in the INIT or
2076 * INIT-ACK chunk if the sender wants to receive authenticated
2077 * chunks. Its maximum length is 260 bytes.
2078 */
7ab90804
VY
2079 if (260 < ntohs(param.p->length)) {
2080 sctp_process_inv_paramlength(asoc, param.p,
2081 chunk, err_chunk);
2082 retval = SCTP_IERROR_ABORT;
2083 }
730fc3d0
VY
2084 break;
2085
2086 case SCTP_PARAM_HMAC_ALGO:
72da7b38
WY
2087 if (!sctp_auth_enable)
2088 goto fallthrough;
2089
2090 hmacs = (struct sctp_hmac_algo_param *)param.p;
2091 n_elt = (ntohs(param.p->length) - sizeof(sctp_paramhdr_t)) >> 1;
2092
2093 /* SCTP-AUTH: Section 6.1
2094 * The HMAC algorithm based on SHA-1 MUST be supported and
2095 * included in the HMAC-ALGO parameter.
2096 */
2097 for (i = 0; i < n_elt; i++) {
2098 id = ntohs(hmacs->hmac_ids[i]);
2099
2100 if (id == SCTP_AUTH_HMAC_ID_SHA1)
2101 break;
2102 }
2103
2104 if (id != SCTP_AUTH_HMAC_ID_SHA1) {
2105 sctp_process_inv_paramlength(asoc, param.p, chunk,
2106 err_chunk);
2107 retval = SCTP_IERROR_ABORT;
2108 }
2109 break;
730fc3d0 2110fallthrough:
1da177e4
LT
2111 default:
2112 SCTP_DEBUG_PRINTK("Unrecognized param: %d for chunk %d.\n",
2113 ntohs(param.p->type), cid);
7ab90804 2114 retval = sctp_process_unk_param(asoc, param, chunk, err_chunk);
1da177e4
LT
2115 break;
2116 }
2117 return retval;
2118}
2119
2120/* Verify the INIT packet before we process it. */
2121int sctp_verify_init(const struct sctp_association *asoc,
2122 sctp_cid_t cid,
2123 sctp_init_chunk_t *peer_init,
2124 struct sctp_chunk *chunk,
2125 struct sctp_chunk **errp)
2126{
2127 union sctp_params param;
2128 int has_cookie = 0;
7ab90804 2129 int result;
1da177e4
LT
2130
2131 /* Verify stream values are non-zero. */
2132 if ((0 == peer_init->init_hdr.num_outbound_streams) ||
d023f629
VY
2133 (0 == peer_init->init_hdr.num_inbound_streams) ||
2134 (0 == peer_init->init_hdr.init_tag) ||
2135 (SCTP_DEFAULT_MINWINDOW > ntohl(peer_init->init_hdr.a_rwnd))) {
1da177e4 2136
7ab90804 2137 return sctp_process_inv_mandatory(asoc, chunk, errp);
1da177e4
LT
2138 }
2139
2140 /* Check for missing mandatory parameters. */
2141 sctp_walk_params(param, peer_init, init_hdr.params) {
2142
2143 if (SCTP_PARAM_STATE_COOKIE == param.p->type)
2144 has_cookie = 1;
2145
2146 } /* for (loop through all parameters) */
2147
2148 /* There is a possibility that a parameter length was bad and
2149 * in that case we would have stoped walking the parameters.
2150 * The current param.p would point at the bad one.
2151 * Current consensus on the mailing list is to generate a PROTOCOL
2152 * VIOLATION error. We build the ERROR chunk here and let the normal
2153 * error handling code build and send the packet.
2154 */
7ab90804
VY
2155 if (param.v != (void*)chunk->chunk_end)
2156 return sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
1da177e4
LT
2157
2158 /* The only missing mandatory param possible today is
2159 * the state cookie for an INIT-ACK chunk.
2160 */
7ab90804
VY
2161 if ((SCTP_CID_INIT_ACK == cid) && !has_cookie)
2162 return sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
2163 chunk, errp);
1da177e4 2164
7ab90804 2165 /* Verify all the variable length parameters */
1da177e4
LT
2166 sctp_walk_params(param, peer_init, init_hdr.params) {
2167
7ab90804
VY
2168 result = sctp_verify_param(asoc, param, cid, chunk, errp);
2169 switch (result) {
2170 case SCTP_IERROR_ABORT:
2171 case SCTP_IERROR_NOMEM:
1da177e4 2172 return 0;
7ab90804 2173 case SCTP_IERROR_ERROR:
1da177e4 2174 return 1;
7ab90804
VY
2175 case SCTP_IERROR_NO_ERROR:
2176 default:
2177 break;
1da177e4
LT
2178 }
2179
2180 } /* for (loop through all parameters) */
2181
2182 return 1;
2183}
2184
2185/* Unpack the parameters in an INIT packet into an association.
2186 * Returns 0 on failure, else success.
2187 * FIXME: This is an association method.
2188 */
2189int sctp_process_init(struct sctp_association *asoc, sctp_cid_t cid,
2190 const union sctp_addr *peer_addr,
dd0fc66f 2191 sctp_init_chunk_t *peer_init, gfp_t gfp)
1da177e4
LT
2192{
2193 union sctp_params param;
2194 struct sctp_transport *transport;
2195 struct list_head *pos, *temp;
2196 char *cookie;
2197
2198 /* We must include the address that the INIT packet came from.
2199 * This is the only address that matters for an INIT packet.
2200 * When processing a COOKIE ECHO, we retrieve the from address
2201 * of the INIT from the cookie.
2202 */
2203
2204 /* This implementation defaults to making the first transport
2205 * added as the primary transport. The source address seems to
2206 * be a a better choice than any of the embedded addresses.
2207 */
4bdf4b5f 2208 if (peer_addr) {
6a1e5f33 2209 if(!sctp_assoc_add_peer(asoc, peer_addr, gfp, SCTP_ACTIVE))
1da177e4 2210 goto nomem;
4bdf4b5f 2211 }
1da177e4
LT
2212
2213 /* Process the initialization parameters. */
1da177e4
LT
2214 sctp_walk_params(param, peer_init, init_hdr.params) {
2215
2216 if (!sctp_process_param(asoc, param, peer_addr, gfp))
d808ad9a 2217 goto clean_up;
1da177e4
LT
2218 }
2219
730fc3d0
VY
2220 /* AUTH: After processing the parameters, make sure that we
2221 * have all the required info to potentially do authentications.
2222 */
2223 if (asoc->peer.auth_capable && (!asoc->peer.peer_random ||
2224 !asoc->peer.peer_hmacs))
2225 asoc->peer.auth_capable = 0;
2226
d6701191
VY
2227 /* In a non-backward compatible mode, if the peer claims
2228 * support for ADD-IP but not AUTH, the ADD-IP spec states
2229 * that we MUST ABORT the association. Section 6. The section
2230 * also give us an option to silently ignore the packet, which
2231 * is what we'll do here.
6b2f9cb6 2232 */
73d9c4fd
VY
2233 if (!sctp_addip_noauth &&
2234 (asoc->peer.asconf_capable && !asoc->peer.auth_capable)) {
6b2f9cb6
VY
2235 asoc->peer.addip_disabled_mask |= (SCTP_PARAM_ADD_IP |
2236 SCTP_PARAM_DEL_IP |
2237 SCTP_PARAM_SET_PRIMARY);
88799fe5 2238 asoc->peer.asconf_capable = 0;
d6701191 2239 goto clean_up;
6b2f9cb6
VY
2240 }
2241
3f7a87d2
FF
2242 /* Walk list of transports, removing transports in the UNKNOWN state. */
2243 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2244 transport = list_entry(pos, struct sctp_transport, transports);
2245 if (transport->state == SCTP_UNKNOWN) {
2246 sctp_assoc_rm_peer(asoc, transport);
2247 }
2248 }
2249
1da177e4
LT
2250 /* The fixed INIT headers are always in network byte
2251 * order.
2252 */
2253 asoc->peer.i.init_tag =
2254 ntohl(peer_init->init_hdr.init_tag);
2255 asoc->peer.i.a_rwnd =
2256 ntohl(peer_init->init_hdr.a_rwnd);
2257 asoc->peer.i.num_outbound_streams =
2258 ntohs(peer_init->init_hdr.num_outbound_streams);
2259 asoc->peer.i.num_inbound_streams =
2260 ntohs(peer_init->init_hdr.num_inbound_streams);
2261 asoc->peer.i.initial_tsn =
2262 ntohl(peer_init->init_hdr.initial_tsn);
2263
2264 /* Apply the upper bounds for output streams based on peer's
2265 * number of inbound streams.
2266 */
2267 if (asoc->c.sinit_num_ostreams >
2268 ntohs(peer_init->init_hdr.num_inbound_streams)) {
2269 asoc->c.sinit_num_ostreams =
2270 ntohs(peer_init->init_hdr.num_inbound_streams);
2271 }
2272
2273 if (asoc->c.sinit_max_instreams >
2274 ntohs(peer_init->init_hdr.num_outbound_streams)) {
2275 asoc->c.sinit_max_instreams =
2276 ntohs(peer_init->init_hdr.num_outbound_streams);
2277 }
2278
2279 /* Copy Initiation tag from INIT to VT_peer in cookie. */
2280 asoc->c.peer_vtag = asoc->peer.i.init_tag;
2281
2282 /* Peer Rwnd : Current calculated value of the peer's rwnd. */
2283 asoc->peer.rwnd = asoc->peer.i.a_rwnd;
2284
2285 /* Copy cookie in case we need to resend COOKIE-ECHO. */
2286 cookie = asoc->peer.cookie;
2287 if (cookie) {
af997d8c 2288 asoc->peer.cookie = kmemdup(cookie, asoc->peer.cookie_len, gfp);
1da177e4
LT
2289 if (!asoc->peer.cookie)
2290 goto clean_up;
1da177e4
LT
2291 }
2292
2293 /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
2294 * high (for example, implementations MAY use the size of the receiver
2295 * advertised window).
2296 */
9dbc15f0
RD
2297 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
2298 transports) {
1da177e4
LT
2299 transport->ssthresh = asoc->peer.i.a_rwnd;
2300 }
2301
2302 /* Set up the TSN tracking pieces. */
8e1ee18c
VY
2303 if (!sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
2304 asoc->peer.i.initial_tsn, gfp))
2305 goto clean_up;
1da177e4
LT
2306
2307 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
2308 *
2309 * The stream sequence number in all the streams shall start
2310 * from 0 when the association is established. Also, when the
2311 * stream sequence number reaches the value 65535 the next
2312 * stream sequence number shall be set to 0.
2313 */
2314
3f7a87d2 2315 /* Allocate storage for the negotiated streams if it is not a temporary
d808ad9a 2316 * association.
1da177e4
LT
2317 */
2318 if (!asoc->temp) {
1da177e4
LT
2319 int error;
2320
2321 asoc->ssnmap = sctp_ssnmap_new(asoc->c.sinit_max_instreams,
2322 asoc->c.sinit_num_ostreams, gfp);
2323 if (!asoc->ssnmap)
2324 goto clean_up;
2325
07d93967
VY
2326 error = sctp_assoc_set_id(asoc, gfp);
2327 if (error)
1da177e4 2328 goto clean_up;
1da177e4
LT
2329 }
2330
2331 /* ADDIP Section 4.1 ASCONF Chunk Procedures
2332 *
2333 * When an endpoint has an ASCONF signaled change to be sent to the
2334 * remote endpoint it should do the following:
2335 * ...
2336 * A2) A serial number should be assigned to the Chunk. The serial
2337 * number should be a monotonically increasing number. All serial
2338 * numbers are defined to be initialized at the start of the
2339 * association to the same value as the Initial TSN.
2340 */
2341 asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
2342 return 1;
2343
2344clean_up:
2345 /* Release the transport structures. */
2346 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2347 transport = list_entry(pos, struct sctp_transport, transports);
add52379
VY
2348 if (transport->state != SCTP_ACTIVE)
2349 sctp_assoc_rm_peer(asoc, transport);
1da177e4 2350 }
3f7a87d2 2351
1da177e4
LT
2352nomem:
2353 return 0;
2354}
2355
2356
2357/* Update asoc with the option described in param.
2358 *
2359 * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
2360 *
2361 * asoc is the association to update.
2362 * param is the variable length parameter to use for update.
2363 * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
2364 * If the current packet is an INIT we want to minimize the amount of
2365 * work we do. In particular, we should not build transport
2366 * structures for the addresses.
2367 */
2368static int sctp_process_param(struct sctp_association *asoc,
2369 union sctp_params param,
2370 const union sctp_addr *peer_addr,
dd0fc66f 2371 gfp_t gfp)
1da177e4
LT
2372{
2373 union sctp_addr addr;
2374 int i;
2375 __u16 sat;
2376 int retval = 1;
2377 sctp_scope_t scope;
2378 time_t stale;
2379 struct sctp_af *af;
d6de3097
VY
2380 union sctp_addr_param *addr_param;
2381 struct sctp_transport *t;
1da177e4
LT
2382
2383 /* We maintain all INIT parameters in network byte order all the
2384 * time. This allows us to not worry about whether the parameters
2385 * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
2386 */
2387 switch (param.p->type) {
2388 case SCTP_PARAM_IPV6_ADDRESS:
2389 if (PF_INET6 != asoc->base.sk->sk_family)
2390 break;
7dab83de
VY
2391 goto do_addr_param;
2392
1da177e4 2393 case SCTP_PARAM_IPV4_ADDRESS:
7dab83de
VY
2394 /* v4 addresses are not allowed on v6-only socket */
2395 if (ipv6_only_sock(asoc->base.sk))
2396 break;
2397do_addr_param:
1da177e4 2398 af = sctp_get_af_specific(param_type2af(param.p->type));
dd86d136 2399 af->from_addr_param(&addr, param.addr, htons(asoc->peer.port), 0);
1da177e4 2400 scope = sctp_scope(peer_addr);
dd86d136
AV
2401 if (sctp_in_scope(&addr, scope))
2402 if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_UNCONFIRMED))
1da177e4
LT
2403 return 0;
2404 break;
2405
2406 case SCTP_PARAM_COOKIE_PRESERVATIVE:
2407 if (!sctp_cookie_preserve_enable)
2408 break;
2409
2410 stale = ntohl(param.life->lifespan_increment);
2411
2412 /* Suggested Cookie Life span increment's unit is msec,
2413 * (1/1000sec).
2414 */
2415 asoc->cookie_life.tv_sec += stale / 1000;
2416 asoc->cookie_life.tv_usec += (stale % 1000) * 1000;
2417 break;
2418
2419 case SCTP_PARAM_HOST_NAME_ADDRESS:
2420 SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
2421 break;
2422
2423 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2424 /* Turn off the default values first so we'll know which
2425 * ones are really set by the peer.
2426 */
2427 asoc->peer.ipv4_address = 0;
2428 asoc->peer.ipv6_address = 0;
2429
140ee960
GJ
2430 /* Assume that peer supports the address family
2431 * by which it sends a packet.
2432 */
2433 if (peer_addr->sa.sa_family == AF_INET6)
2434 asoc->peer.ipv6_address = 1;
2435 else if (peer_addr->sa.sa_family == AF_INET)
2436 asoc->peer.ipv4_address = 1;
2437
1da177e4
LT
2438 /* Cycle through address types; avoid divide by 0. */
2439 sat = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2440 if (sat)
2441 sat /= sizeof(__u16);
2442
2443 for (i = 0; i < sat; ++i) {
2444 switch (param.sat->types[i]) {
2445 case SCTP_PARAM_IPV4_ADDRESS:
2446 asoc->peer.ipv4_address = 1;
2447 break;
2448
2449 case SCTP_PARAM_IPV6_ADDRESS:
6e40a915
WY
2450 if (PF_INET6 == asoc->base.sk->sk_family)
2451 asoc->peer.ipv6_address = 1;
1da177e4
LT
2452 break;
2453
2454 case SCTP_PARAM_HOST_NAME_ADDRESS:
2455 asoc->peer.hostname_address = 1;
2456 break;
2457
2458 default: /* Just ignore anything else. */
2459 break;
3ff50b79 2460 }
1da177e4
LT
2461 }
2462 break;
2463
2464 case SCTP_PARAM_STATE_COOKIE:
2465 asoc->peer.cookie_len =
2466 ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2467 asoc->peer.cookie = param.cookie->body;
2468 break;
2469
2470 case SCTP_PARAM_HEARTBEAT_INFO:
2471 /* Would be odd to receive, but it causes no problems. */
2472 break;
2473
2474 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2475 /* Rejected during verify stage. */
2476 break;
2477
2478 case SCTP_PARAM_ECN_CAPABLE:
2479 asoc->peer.ecn_capable = 1;
2480 break;
2481
0f3fffd8 2482 case SCTP_PARAM_ADAPTATION_LAYER_IND:
e69c4e0f 2483 asoc->peer.adaptation_ind = ntohl(param.aind->adaptation_ind);
1da177e4
LT
2484 break;
2485
d6de3097 2486 case SCTP_PARAM_SET_PRIMARY:
0ef46e28
VY
2487 if (!sctp_addip_enable)
2488 goto fall_through;
2489
d6de3097
VY
2490 addr_param = param.v + sizeof(sctp_addip_param_t);
2491
2492 af = sctp_get_af_specific(param_type2af(param.p->type));
2493 af->from_addr_param(&addr, addr_param,
2494 htons(asoc->peer.port), 0);
2495
2496 /* if the address is invalid, we can't process it.
2497 * XXX: see spec for what to do.
2498 */
2499 if (!af->addr_valid(&addr, NULL, NULL))
2500 break;
2501
2502 t = sctp_assoc_lookup_paddr(asoc, &addr);
2503 if (!t)
2504 break;
2505
2506 sctp_assoc_set_primary(asoc, t);
2507 break;
2508
131a47e3
VY
2509 case SCTP_PARAM_SUPPORTED_EXT:
2510 sctp_process_ext_param(asoc, param);
2511 break;
2512
1da177e4
LT
2513 case SCTP_PARAM_FWD_TSN_SUPPORT:
2514 if (sctp_prsctp_enable) {
2515 asoc->peer.prsctp_capable = 1;
2516 break;
2517 }
d808ad9a 2518 /* Fall Through */
730fc3d0
VY
2519 goto fall_through;
2520
2521 case SCTP_PARAM_RANDOM:
2522 if (!sctp_auth_enable)
2523 goto fall_through;
2524
2525 /* Save peer's random parameter */
2526 asoc->peer.peer_random = kmemdup(param.p,
2527 ntohs(param.p->length), gfp);
2528 if (!asoc->peer.peer_random) {
2529 retval = 0;
2530 break;
2531 }
2532 break;
2533
2534 case SCTP_PARAM_HMAC_ALGO:
2535 if (!sctp_auth_enable)
2536 goto fall_through;
2537
2538 /* Save peer's HMAC list */
2539 asoc->peer.peer_hmacs = kmemdup(param.p,
2540 ntohs(param.p->length), gfp);
2541 if (!asoc->peer.peer_hmacs) {
2542 retval = 0;
2543 break;
2544 }
2545
2546 /* Set the default HMAC the peer requested*/
2547 sctp_auth_asoc_set_default_hmac(asoc, param.hmac_algo);
2548 break;
2549
2550 case SCTP_PARAM_CHUNKS:
2551 if (!sctp_auth_enable)
2552 goto fall_through;
2553
2554 asoc->peer.peer_chunks = kmemdup(param.p,
2555 ntohs(param.p->length), gfp);
2556 if (!asoc->peer.peer_chunks)
2557 retval = 0;
2558 break;
2559fall_through:
1da177e4
LT
2560 default:
2561 /* Any unrecognized parameters should have been caught
2562 * and handled by sctp_verify_param() which should be
2563 * called prior to this routine. Simply log the error
2564 * here.
2565 */
2566 SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
2567 ntohs(param.p->type), asoc);
2568 break;
3ff50b79 2569 }
1da177e4
LT
2570
2571 return retval;
2572}
2573
2574/* Select a new verification tag. */
2575__u32 sctp_generate_tag(const struct sctp_endpoint *ep)
2576{
2577 /* I believe that this random number generator complies with RFC1750.
2578 * A tag of 0 is reserved for special cases (e.g. INIT).
2579 */
2580 __u32 x;
2581
2582 do {
2583 get_random_bytes(&x, sizeof(__u32));
2584 } while (x == 0);
2585
2586 return x;
2587}
2588
2589/* Select an initial TSN to send during startup. */
2590__u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
2591{
2592 __u32 retval;
2593
2594 get_random_bytes(&retval, sizeof(__u32));
2595 return retval;
2596}
2597
2598/*
2599 * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
2600 * 0 1 2 3
2601 * 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
2602 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2603 * | Type = 0xC1 | Chunk Flags | Chunk Length |
2604 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2605 * | Serial Number |
2606 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2607 * | Address Parameter |
2608 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2609 * | ASCONF Parameter #1 |
2610 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2611 * \ \
2612 * / .... /
2613 * \ \
2614 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2615 * | ASCONF Parameter #N |
2616 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2617 *
d808ad9a 2618 * Address Parameter and other parameter will not be wrapped in this function
1da177e4
LT
2619 */
2620static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
2621 union sctp_addr *addr,
2622 int vparam_len)
2623{
2624 sctp_addiphdr_t asconf;
2625 struct sctp_chunk *retval;
2626 int length = sizeof(asconf) + vparam_len;
2627 union sctp_addr_param addrparam;
2628 int addrlen;
2629 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2630
2631 addrlen = af->to_addr_param(addr, &addrparam);
2632 if (!addrlen)
2633 return NULL;
2634 length += addrlen;
2635
2636 /* Create the chunk. */
2637 retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF, 0, length);
2638 if (!retval)
2639 return NULL;
2640
2641 asconf.serial = htonl(asoc->addip_serial++);
2642
2643 retval->subh.addip_hdr =
2644 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2645 retval->param_hdr.v =
2646 sctp_addto_chunk(retval, addrlen, &addrparam);
2647
2648 return retval;
2649}
2650
2651/* ADDIP
2652 * 3.2.1 Add IP Address
2653 * 0 1 2 3
2654 * 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
2655 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2656 * | Type = 0xC001 | Length = Variable |
2657 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2658 * | ASCONF-Request Correlation ID |
2659 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2660 * | Address Parameter |
2661 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2662 *
2663 * 3.2.2 Delete IP Address
2664 * 0 1 2 3
2665 * 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
2666 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2667 * | Type = 0xC002 | Length = Variable |
2668 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2669 * | ASCONF-Request Correlation ID |
2670 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2671 * | Address Parameter |
2672 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2673 *
2674 */
2675struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
2676 union sctp_addr *laddr,
2677 struct sockaddr *addrs,
2678 int addrcnt,
dbc16db1 2679 __be16 flags)
1da177e4
LT
2680{
2681 sctp_addip_param_t param;
2682 struct sctp_chunk *retval;
2683 union sctp_addr_param addr_param;
2684 union sctp_addr *addr;
2685 void *addr_buf;
2686 struct sctp_af *af;
2687 int paramlen = sizeof(param);
2688 int addr_param_len = 0;
2689 int totallen = 0;
2690 int i;
2691
2692 /* Get total length of all the address parameters. */
2693 addr_buf = addrs;
2694 for (i = 0; i < addrcnt; i++) {
2695 addr = (union sctp_addr *)addr_buf;
2696 af = sctp_get_af_specific(addr->v4.sin_family);
2697 addr_param_len = af->to_addr_param(addr, &addr_param);
2698
2699 totallen += paramlen;
2700 totallen += addr_param_len;
2701
2702 addr_buf += af->sockaddr_len;
2703 }
2704
2705 /* Create an asconf chunk with the required length. */
2706 retval = sctp_make_asconf(asoc, laddr, totallen);
2707 if (!retval)
2708 return NULL;
2709
2710 /* Add the address parameters to the asconf chunk. */
2711 addr_buf = addrs;
2712 for (i = 0; i < addrcnt; i++) {
2713 addr = (union sctp_addr *)addr_buf;
2714 af = sctp_get_af_specific(addr->v4.sin_family);
2715 addr_param_len = af->to_addr_param(addr, &addr_param);
2716 param.param_hdr.type = flags;
2717 param.param_hdr.length = htons(paramlen + addr_param_len);
2718 param.crr_id = i;
2719
2720 sctp_addto_chunk(retval, paramlen, &param);
2721 sctp_addto_chunk(retval, addr_param_len, &addr_param);
2722
2723 addr_buf += af->sockaddr_len;
2724 }
2725 return retval;
2726}
2727
2728/* ADDIP
2729 * 3.2.4 Set Primary IP Address
2730 * 0 1 2 3
2731 * 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
2732 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2733 * | Type =0xC004 | Length = Variable |
2734 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2735 * | ASCONF-Request Correlation ID |
2736 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2737 * | Address Parameter |
2738 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2739 *
d808ad9a 2740 * Create an ASCONF chunk with Set Primary IP address parameter.
1da177e4
LT
2741 */
2742struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
2743 union sctp_addr *addr)
2744{
2745 sctp_addip_param_t param;
2746 struct sctp_chunk *retval;
2747 int len = sizeof(param);
2748 union sctp_addr_param addrparam;
2749 int addrlen;
2750 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2751
2752 addrlen = af->to_addr_param(addr, &addrparam);
2753 if (!addrlen)
2754 return NULL;
2755 len += addrlen;
2756
2757 /* Create the chunk and make asconf header. */
2758 retval = sctp_make_asconf(asoc, addr, len);
2759 if (!retval)
2760 return NULL;
2761
2762 param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
2763 param.param_hdr.length = htons(len);
2764 param.crr_id = 0;
2765
2766 sctp_addto_chunk(retval, sizeof(param), &param);
2767 sctp_addto_chunk(retval, addrlen, &addrparam);
2768
2769 return retval;
2770}
2771
2772/* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2773 * 0 1 2 3
2774 * 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
2775 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2776 * | Type = 0x80 | Chunk Flags | Chunk Length |
2777 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2778 * | Serial Number |
2779 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2780 * | ASCONF Parameter Response#1 |
2781 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2782 * \ \
2783 * / .... /
2784 * \ \
2785 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2786 * | ASCONF Parameter Response#N |
2787 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2788 *
d808ad9a 2789 * Create an ASCONF_ACK chunk with enough space for the parameter responses.
1da177e4
LT
2790 */
2791static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
2792 __u32 serial, int vparam_len)
2793{
2794 sctp_addiphdr_t asconf;
2795 struct sctp_chunk *retval;
2796 int length = sizeof(asconf) + vparam_len;
2797
2798 /* Create the chunk. */
2799 retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF_ACK, 0, length);
2800 if (!retval)
2801 return NULL;
2802
2803 asconf.serial = htonl(serial);
2804
2805 retval->subh.addip_hdr =
2806 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2807
2808 return retval;
2809}
2810
2811/* Add response parameters to an ASCONF_ACK chunk. */
9f81bcd9 2812static void sctp_add_asconf_response(struct sctp_chunk *chunk, __be32 crr_id,
dbc16db1 2813 __be16 err_code, sctp_addip_param_t *asconf_param)
1da177e4
LT
2814{
2815 sctp_addip_param_t ack_param;
2816 sctp_errhdr_t err_param;
2817 int asconf_param_len = 0;
2818 int err_param_len = 0;
dbc16db1 2819 __be16 response_type;
1da177e4
LT
2820
2821 if (SCTP_ERROR_NO_ERROR == err_code) {
2822 response_type = SCTP_PARAM_SUCCESS_REPORT;
2823 } else {
2824 response_type = SCTP_PARAM_ERR_CAUSE;
2825 err_param_len = sizeof(err_param);
2826 if (asconf_param)
2827 asconf_param_len =
2828 ntohs(asconf_param->param_hdr.length);
2829 }
2830
d808ad9a 2831 /* Add Success Indication or Error Cause Indication parameter. */
1da177e4
LT
2832 ack_param.param_hdr.type = response_type;
2833 ack_param.param_hdr.length = htons(sizeof(ack_param) +
2834 err_param_len +
2835 asconf_param_len);
2836 ack_param.crr_id = crr_id;
2837 sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
2838
2839 if (SCTP_ERROR_NO_ERROR == err_code)
2840 return;
2841
2842 /* Add Error Cause parameter. */
2843 err_param.cause = err_code;
2844 err_param.length = htons(err_param_len + asconf_param_len);
2845 sctp_addto_chunk(chunk, err_param_len, &err_param);
2846
2847 /* Add the failed TLV copied from ASCONF chunk. */
2848 if (asconf_param)
2849 sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
2850}
2851
2852/* Process a asconf parameter. */
dbc16db1 2853static __be16 sctp_process_asconf_param(struct sctp_association *asoc,
1da177e4
LT
2854 struct sctp_chunk *asconf,
2855 sctp_addip_param_t *asconf_param)
2856{
2857 struct sctp_transport *peer;
2858 struct sctp_af *af;
2859 union sctp_addr addr;
1da177e4 2860 union sctp_addr_param *addr_param;
5f242a13 2861
c1cc678a
PM
2862 addr_param = (union sctp_addr_param *)
2863 ((void *)asconf_param + sizeof(sctp_addip_param_t));
2864
44e65c1e
WY
2865 if (asconf_param->param_hdr.type != SCTP_PARAM_ADD_IP &&
2866 asconf_param->param_hdr.type != SCTP_PARAM_DEL_IP &&
2867 asconf_param->param_hdr.type != SCTP_PARAM_SET_PRIMARY)
2868 return SCTP_ERROR_UNKNOWN_PARAM;
2869
c4492586
WY
2870 switch (addr_param->v4.param_hdr.type) {
2871 case SCTP_PARAM_IPV6_ADDRESS:
2872 if (!asoc->peer.ipv6_address)
945e5abc 2873 return SCTP_ERROR_DNS_FAILED;
c4492586
WY
2874 break;
2875 case SCTP_PARAM_IPV4_ADDRESS:
2876 if (!asoc->peer.ipv4_address)
945e5abc 2877 return SCTP_ERROR_DNS_FAILED;
c4492586
WY
2878 break;
2879 default:
945e5abc 2880 return SCTP_ERROR_DNS_FAILED;
c4492586
WY
2881 }
2882
1da177e4
LT
2883 af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2884 if (unlikely(!af))
945e5abc 2885 return SCTP_ERROR_DNS_FAILED;
1da177e4 2886
dd86d136 2887 af->from_addr_param(&addr, addr_param, htons(asoc->peer.port), 0);
42e30bf3
VY
2888
2889 /* ADDIP 4.2.1 This parameter MUST NOT contain a broadcast
2890 * or multicast address.
2891 * (note: wildcard is permitted and requires special handling so
2892 * make sure we check for that)
2893 */
2894 if (!af->is_any(&addr) && !af->addr_valid(&addr, NULL, asconf->skb))
945e5abc 2895 return SCTP_ERROR_DNS_FAILED;
42e30bf3 2896
1da177e4
LT
2897 switch (asconf_param->param_hdr.type) {
2898 case SCTP_PARAM_ADD_IP:
42e30bf3
VY
2899 /* Section 4.2.1:
2900 * If the address 0.0.0.0 or ::0 is provided, the source
2901 * address of the packet MUST be added.
2902 */
2903 if (af->is_any(&addr))
2904 memcpy(&addr, &asconf->source, sizeof(addr));
2905
1da177e4 2906 /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
d808ad9a
YH
2907 * request and does not have the local resources to add this
2908 * new address to the association, it MUST return an Error
2909 * Cause TLV set to the new error code 'Operation Refused
2910 * Due to Resource Shortage'.
2911 */
1da177e4 2912
dd86d136 2913 peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC, SCTP_UNCONFIRMED);
1da177e4
LT
2914 if (!peer)
2915 return SCTP_ERROR_RSRC_LOW;
2916
2917 /* Start the heartbeat timer. */
2918 if (!mod_timer(&peer->hb_timer, sctp_transport_timeout(peer)))
2919 sctp_transport_hold(peer);
2920 break;
2921 case SCTP_PARAM_DEL_IP:
2922 /* ADDIP 4.3 D7) If a request is received to delete the
d808ad9a
YH
2923 * last remaining IP address of a peer endpoint, the receiver
2924 * MUST send an Error Cause TLV with the error cause set to the
2925 * new error code 'Request to Delete Last Remaining IP Address'.
2926 */
42e30bf3 2927 if (asoc->peer.transport_count == 1)
1da177e4
LT
2928 return SCTP_ERROR_DEL_LAST_IP;
2929
2930 /* ADDIP 4.3 D8) If a request is received to delete an IP
2931 * address which is also the source address of the IP packet
2932 * which contained the ASCONF chunk, the receiver MUST reject
2933 * this request. To reject the request the receiver MUST send
2934 * an Error Cause TLV set to the new error code 'Request to
2935 * Delete Source IP Address'
2936 */
6a1e5f33 2937 if (sctp_cmp_addr_exact(sctp_source(asconf), &addr))
1da177e4
LT
2938 return SCTP_ERROR_DEL_SRC_IP;
2939
42e30bf3
VY
2940 /* Section 4.2.2
2941 * If the address 0.0.0.0 or ::0 is provided, all
2942 * addresses of the peer except the source address of the
2943 * packet MUST be deleted.
2944 */
2945 if (af->is_any(&addr)) {
2946 sctp_assoc_set_primary(asoc, asconf->transport);
2947 sctp_assoc_del_nonprimary_peers(asoc,
2948 asconf->transport);
2949 } else
2950 sctp_assoc_del_peer(asoc, &addr);
1da177e4
LT
2951 break;
2952 case SCTP_PARAM_SET_PRIMARY:
42e30bf3
VY
2953 /* ADDIP Section 4.2.4
2954 * If the address 0.0.0.0 or ::0 is provided, the receiver
2955 * MAY mark the source address of the packet as its
2956 * primary.
2957 */
2958 if (af->is_any(&addr))
2959 memcpy(&addr.v4, sctp_source(asconf), sizeof(addr));
2960
dd86d136 2961 peer = sctp_assoc_lookup_paddr(asoc, &addr);
1da177e4 2962 if (!peer)
945e5abc 2963 return SCTP_ERROR_DNS_FAILED;
1da177e4
LT
2964
2965 sctp_assoc_set_primary(asoc, peer);
2966 break;
1da177e4
LT
2967 }
2968
2969 return SCTP_ERROR_NO_ERROR;
2970}
2971
6f4c618d
WY
2972/* Verify the ASCONF packet before we process it. */
2973int sctp_verify_asconf(const struct sctp_association *asoc,
2974 struct sctp_paramhdr *param_hdr, void *chunk_end,
2975 struct sctp_paramhdr **errp) {
2976 sctp_addip_param_t *asconf_param;
2977 union sctp_params param;
2978 int length, plen;
2979
2980 param.v = (sctp_paramhdr_t *) param_hdr;
2981 while (param.v <= chunk_end - sizeof(sctp_paramhdr_t)) {
2982 length = ntohs(param.p->length);
2983 *errp = param.p;
2984
2985 if (param.v > chunk_end - length ||
2986 length < sizeof(sctp_paramhdr_t))
2987 return 0;
2988
2989 switch (param.p->type) {
2990 case SCTP_PARAM_ADD_IP:
2991 case SCTP_PARAM_DEL_IP:
2992 case SCTP_PARAM_SET_PRIMARY:
2993 asconf_param = (sctp_addip_param_t *)param.v;
2994 plen = ntohs(asconf_param->param_hdr.length);
2995 if (plen < sizeof(sctp_addip_param_t) +
2996 sizeof(sctp_paramhdr_t))
2997 return 0;
2998 break;
2999 case SCTP_PARAM_SUCCESS_REPORT:
3000 case SCTP_PARAM_ADAPTATION_LAYER_IND:
3001 if (length != sizeof(sctp_addip_param_t))
3002 return 0;
3003
3004 break;
3005 default:
3006 break;
3007 }
3008
3009 param.v += WORD_ROUND(length);
3010 }
3011
3012 if (param.v != chunk_end)
3013 return 0;
3014
3015 return 1;
3016}
3017
d808ad9a 3018/* Process an incoming ASCONF chunk with the next expected serial no. and
1da177e4
LT
3019 * return an ASCONF_ACK chunk to be sent in response.
3020 */
3021struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
3022 struct sctp_chunk *asconf)
3023{
3024 sctp_addiphdr_t *hdr;
3025 union sctp_addr_param *addr_param;
3026 sctp_addip_param_t *asconf_param;
3027 struct sctp_chunk *asconf_ack;
3028
dbc16db1 3029 __be16 err_code;
1da177e4 3030 int length = 0;
f3830ccc 3031 int chunk_len;
1da177e4
LT
3032 __u32 serial;
3033 int all_param_pass = 1;
3034
f3830ccc 3035 chunk_len = ntohs(asconf->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
1da177e4
LT
3036 hdr = (sctp_addiphdr_t *)asconf->skb->data;
3037 serial = ntohl(hdr->serial);
3038
d808ad9a 3039 /* Skip the addiphdr and store a pointer to address parameter. */
1da177e4
LT
3040 length = sizeof(sctp_addiphdr_t);
3041 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
3042 chunk_len -= length;
3043
3044 /* Skip the address parameter and store a pointer to the first
7aa1b54b 3045 * asconf parameter.
d808ad9a 3046 */
1da177e4
LT
3047 length = ntohs(addr_param->v4.param_hdr.length);
3048 asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
3049 chunk_len -= length;
3050
d808ad9a 3051 /* create an ASCONF_ACK chunk.
1da177e4
LT
3052 * Based on the definitions of parameters, we know that the size of
3053 * ASCONF_ACK parameters are less than or equal to the twice of ASCONF
7aa1b54b 3054 * parameters.
1da177e4
LT
3055 */
3056 asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 2);
3057 if (!asconf_ack)
3058 goto done;
3059
3060 /* Process the TLVs contained within the ASCONF chunk. */
3061 while (chunk_len > 0) {
3062 err_code = sctp_process_asconf_param(asoc, asconf,
3063 asconf_param);
3064 /* ADDIP 4.1 A7)
3065 * If an error response is received for a TLV parameter,
3066 * all TLVs with no response before the failed TLV are
3067 * considered successful if not reported. All TLVs after
3068 * the failed response are considered unsuccessful unless
3069 * a specific success indication is present for the parameter.
3070 */
3071 if (SCTP_ERROR_NO_ERROR != err_code)
3072 all_param_pass = 0;
3073
3074 if (!all_param_pass)
3075 sctp_add_asconf_response(asconf_ack,
3076 asconf_param->crr_id, err_code,
3077 asconf_param);
3078
3079 /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
3080 * an IP address sends an 'Out of Resource' in its response, it
3081 * MUST also fail any subsequent add or delete requests bundled
d808ad9a 3082 * in the ASCONF.
1da177e4
LT
3083 */
3084 if (SCTP_ERROR_RSRC_LOW == err_code)
3085 goto done;
3086
3087 /* Move to the next ASCONF param. */
3088 length = ntohs(asconf_param->param_hdr.length);
3089 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
3090 length);
3091 chunk_len -= length;
3092 }
d808ad9a 3093
1da177e4
LT
3094done:
3095 asoc->peer.addip_serial++;
3096
3097 /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
d808ad9a 3098 * after freeing the reference to old asconf ack if any.
1da177e4
LT
3099 */
3100 if (asconf_ack) {
1da177e4 3101 sctp_chunk_hold(asconf_ack);
a08de64d
VY
3102 list_add_tail(&asconf_ack->transmitted_list,
3103 &asoc->asconf_ack_list);
1da177e4
LT
3104 }
3105
3106 return asconf_ack;
3107}
3108
3109/* Process a asconf parameter that is successfully acked. */
425e0f68 3110static void sctp_asconf_param_success(struct sctp_association *asoc,
1da177e4
LT
3111 sctp_addip_param_t *asconf_param)
3112{
3113 struct sctp_af *af;
3114 union sctp_addr addr;
3115 struct sctp_bind_addr *bp = &asoc->base.bind_addr;
3116 union sctp_addr_param *addr_param;
1da177e4 3117 struct sctp_transport *transport;
dc022a98 3118 struct sctp_sockaddr_entry *saddr;
1da177e4
LT
3119
3120 addr_param = (union sctp_addr_param *)
3121 ((void *)asconf_param + sizeof(sctp_addip_param_t));
3122
3123 /* We have checked the packet before, so we do not check again. */
3124 af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
dd86d136 3125 af->from_addr_param(&addr, addr_param, htons(bp->port), 0);
1da177e4
LT
3126
3127 switch (asconf_param->param_hdr.type) {
3128 case SCTP_PARAM_ADD_IP:
559cf710
VY
3129 /* This is always done in BH context with a socket lock
3130 * held, so the list can not change.
3131 */
0ed90fb0 3132 local_bh_disable();
559cf710 3133 list_for_each_entry(saddr, &bp->address_list, list) {
dd86d136 3134 if (sctp_cmp_addr_exact(&saddr->a, &addr))
f57d96b2 3135 saddr->state = SCTP_ADDR_SRC;
dc022a98 3136 }
0ed90fb0 3137 local_bh_enable();
3cd9749c
WY
3138 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
3139 transports) {
3140 if (transport->state == SCTP_ACTIVE)
3141 continue;
3142 dst_release(transport->dst);
3143 sctp_transport_route(transport, NULL,
3144 sctp_sk(asoc->base.sk));
3145 }
1da177e4
LT
3146 break;
3147 case SCTP_PARAM_DEL_IP:
0ed90fb0 3148 local_bh_disable();
425e0f68 3149 sctp_del_bind_addr(bp, &addr);
0ed90fb0 3150 local_bh_enable();
9dbc15f0
RD
3151 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
3152 transports) {
dc022a98 3153 dst_release(transport->dst);
1da177e4
LT
3154 sctp_transport_route(transport, NULL,
3155 sctp_sk(asoc->base.sk));
3156 }
3157 break;
3158 default:
3159 break;
3160 }
1da177e4
LT
3161}
3162
3163/* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
3164 * for the given asconf parameter. If there is no response for this parameter,
d808ad9a 3165 * return the error code based on the third argument 'no_err'.
1da177e4
LT
3166 * ADDIP 4.1
3167 * A7) If an error response is received for a TLV parameter, all TLVs with no
3168 * response before the failed TLV are considered successful if not reported.
3169 * All TLVs after the failed response are considered unsuccessful unless a
3170 * specific success indication is present for the parameter.
3171 */
dbc16db1 3172static __be16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
1da177e4
LT
3173 sctp_addip_param_t *asconf_param,
3174 int no_err)
3175{
3176 sctp_addip_param_t *asconf_ack_param;
3177 sctp_errhdr_t *err_param;
3178 int length;
f3830ccc 3179 int asconf_ack_len;
dbc16db1 3180 __be16 err_code;
1da177e4
LT
3181
3182 if (no_err)
3183 err_code = SCTP_ERROR_NO_ERROR;
3184 else
3185 err_code = SCTP_ERROR_REQ_REFUSED;
3186
f3830ccc
WY
3187 asconf_ack_len = ntohs(asconf_ack->chunk_hdr->length) -
3188 sizeof(sctp_chunkhdr_t);
3189
1da177e4
LT
3190 /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
3191 * the first asconf_ack parameter.
d808ad9a 3192 */
1da177e4
LT
3193 length = sizeof(sctp_addiphdr_t);
3194 asconf_ack_param = (sctp_addip_param_t *)(asconf_ack->skb->data +
3195 length);
3196 asconf_ack_len -= length;
3197
3198 while (asconf_ack_len > 0) {
3199 if (asconf_ack_param->crr_id == asconf_param->crr_id) {
3200 switch(asconf_ack_param->param_hdr.type) {
3201 case SCTP_PARAM_SUCCESS_REPORT:
3202 return SCTP_ERROR_NO_ERROR;
3203 case SCTP_PARAM_ERR_CAUSE:
3204 length = sizeof(sctp_addip_param_t);
3205 err_param = (sctp_errhdr_t *)
3206 ((void *)asconf_ack_param + length);
3207 asconf_ack_len -= length;
3208 if (asconf_ack_len > 0)
3209 return err_param->cause;
3210 else
3211 return SCTP_ERROR_INV_PARAM;
3212 break;
3213 default:
3214 return SCTP_ERROR_INV_PARAM;
3215 }
3216 }
3217
3218 length = ntohs(asconf_ack_param->param_hdr.length);
3219 asconf_ack_param = (sctp_addip_param_t *)
3220 ((void *)asconf_ack_param + length);
3221 asconf_ack_len -= length;
3222 }
3223
3224 return err_code;
3225}
3226
3227/* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
3228int sctp_process_asconf_ack(struct sctp_association *asoc,
3229 struct sctp_chunk *asconf_ack)
3230{
3231 struct sctp_chunk *asconf = asoc->addip_last_asconf;
3232 union sctp_addr_param *addr_param;
3233 sctp_addip_param_t *asconf_param;
3234 int length = 0;
3235 int asconf_len = asconf->skb->len;
3236 int all_param_pass = 0;
3237 int no_err = 1;
3238 int retval = 0;
dbc16db1 3239 __be16 err_code = SCTP_ERROR_NO_ERROR;
1da177e4
LT
3240
3241 /* Skip the chunkhdr and addiphdr from the last asconf sent and store
3242 * a pointer to address parameter.
d808ad9a 3243 */
1da177e4
LT
3244 length = sizeof(sctp_addip_chunk_t);
3245 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
3246 asconf_len -= length;
3247
3248 /* Skip the address parameter in the last asconf sent and store a
7aa1b54b 3249 * pointer to the first asconf parameter.
d808ad9a 3250 */
1da177e4
LT
3251 length = ntohs(addr_param->v4.param_hdr.length);
3252 asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
3253 asconf_len -= length;
3254
3255 /* ADDIP 4.1
3256 * A8) If there is no response(s) to specific TLV parameter(s), and no
3257 * failures are indicated, then all request(s) are considered
3258 * successful.
3259 */
3260 if (asconf_ack->skb->len == sizeof(sctp_addiphdr_t))
3261 all_param_pass = 1;
3262
3263 /* Process the TLVs contained in the last sent ASCONF chunk. */
3264 while (asconf_len > 0) {
3265 if (all_param_pass)
3266 err_code = SCTP_ERROR_NO_ERROR;
3267 else {
3268 err_code = sctp_get_asconf_response(asconf_ack,
3269 asconf_param,
3270 no_err);
3271 if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
3272 no_err = 0;
3273 }
3274
3275 switch (err_code) {
3276 case SCTP_ERROR_NO_ERROR:
425e0f68 3277 sctp_asconf_param_success(asoc, asconf_param);
1da177e4
LT
3278 break;
3279
3280 case SCTP_ERROR_RSRC_LOW:
3281 retval = 1;
3282 break;
3283
a987f762 3284 case SCTP_ERROR_UNKNOWN_PARAM:
1da177e4
LT
3285 /* Disable sending this type of asconf parameter in
3286 * future.
d808ad9a 3287 */
1da177e4
LT
3288 asoc->peer.addip_disabled_mask |=
3289 asconf_param->param_hdr.type;
3290 break;
3291
3292 case SCTP_ERROR_REQ_REFUSED:
3293 case SCTP_ERROR_DEL_LAST_IP:
3294 case SCTP_ERROR_DEL_SRC_IP:
3295 default:
3296 break;
3297 }
3298
3299 /* Skip the processed asconf parameter and move to the next
3300 * one.
d808ad9a 3301 */
1da177e4
LT
3302 length = ntohs(asconf_param->param_hdr.length);
3303 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
3304 length);
3305 asconf_len -= length;
3306 }
3307
3308 /* Free the cached last sent asconf chunk. */
5f9646c3 3309 list_del_init(&asconf->transmitted_list);
1da177e4
LT
3310 sctp_chunk_free(asconf);
3311 asoc->addip_last_asconf = NULL;
3312
3313 /* Send the next asconf chunk from the addip chunk queue. */
79af02c2
DM
3314 if (!list_empty(&asoc->addip_chunk_list)) {
3315 struct list_head *entry = asoc->addip_chunk_list.next;
3316 asconf = list_entry(entry, struct sctp_chunk, list);
3317
3318 list_del_init(entry);
3319
1da177e4
LT
3320 /* Hold the chunk until an ASCONF_ACK is received. */
3321 sctp_chunk_hold(asconf);
3322 if (sctp_primitive_ASCONF(asoc, asconf))
3323 sctp_chunk_free(asconf);
3324 else
3325 asoc->addip_last_asconf = asconf;
3326 }
3327
3328 return retval;
3329}
3330
d808ad9a 3331/* Make a FWD TSN chunk. */
1da177e4
LT
3332struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
3333 __u32 new_cum_tsn, size_t nstreams,
3334 struct sctp_fwdtsn_skip *skiplist)
3335{
3336 struct sctp_chunk *retval = NULL;
3337 struct sctp_fwdtsn_chunk *ftsn_chunk;
d808ad9a 3338 struct sctp_fwdtsn_hdr ftsn_hdr;
1da177e4
LT
3339 struct sctp_fwdtsn_skip skip;
3340 size_t hint;
3341 int i;
3342
3343 hint = (nstreams + 1) * sizeof(__u32);
3344
1da177e4
LT
3345 retval = sctp_make_chunk(asoc, SCTP_CID_FWD_TSN, 0, hint);
3346
3347 if (!retval)
3348 return NULL;
3349
3350 ftsn_chunk = (struct sctp_fwdtsn_chunk *)retval->subh.fwdtsn_hdr;
3351
3352 ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
3353 retval->subh.fwdtsn_hdr =
3354 sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
3355
3356 for (i = 0; i < nstreams; i++) {
3357 skip.stream = skiplist[i].stream;
3358 skip.ssn = skiplist[i].ssn;
3359 sctp_addto_chunk(retval, sizeof(skip), &skip);
3360 }
3361
3362 return retval;
3363}