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