blob: 46f54188f00a5e5c3828283ea61549aea8d17948 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* SCTP kernel reference Implementation
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2002 Intel Corp.
6 *
7 * This file is part of the SCTP kernel reference Implementation
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 *
13 * The SCTP reference implementation is free software;
14 * you can redistribute it and/or modify it under the terms of
15 * the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
17 * any later version.
18 *
19 * The SCTP reference implementation is distributed in the hope that it
20 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
21 * ************************
22 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 * See the GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with GNU CC; see the file COPYING. If not, write to
27 * the Free Software Foundation, 59 Temple Place - Suite 330,
28 * Boston, MA 02111-1307, USA.
29 *
30 * Please send any bug reports or fixes you make to the
31 * email address(es):
32 * lksctp developers <lksctp-developers@lists.sourceforge.net>
33 *
34 * Or submit a bug report through the following website:
35 * http://www.sf.net/projects/lksctp
36 *
37 * Written or modified by:
38 * La Monte H.P. Yarroll <piggy@acm.org>
39 * Karl Knutson <karl@athena.chicago.il.us>
40 * C. Robin <chris@hundredacre.ac.uk>
41 * Jon Grimm <jgrimm@us.ibm.com>
42 * Xingang Guo <xingang.guo@intel.com>
43 * Dajiang Zhang <dajiang.zhang@nokia.com>
44 * Sridhar Samudrala <sri@us.ibm.com>
45 * Daisy Chang <daisyc@us.ibm.com>
46 * Ardelle Fan <ardelle.fan@intel.com>
47 * Kevin Gao <kevin.gao@intel.com>
48 *
49 * Any bugs reported given to us we will try to fix... any fixes shared will
50 * be incorporated into the next SCTP release.
51 */
52
53#include <linux/types.h>
54#include <linux/kernel.h>
55#include <linux/ip.h>
56#include <linux/ipv6.h>
57#include <linux/net.h>
58#include <linux/inet.h>
Christian Borntraegerebc3bbc2007-10-23 12:46:32 +020059#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/crypto.h>
61#include <net/sock.h>
62
63#include <linux/skbuff.h>
64#include <linux/random.h> /* for get_random_bytes */
65#include <net/sctp/sctp.h>
66#include <net/sctp/sm.h>
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068SCTP_STATIC
69struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
70 __u8 type, __u8 flags, int paylen);
71static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
72 const struct sctp_association *asoc,
73 const struct sctp_chunk *init_chunk,
74 int *cookie_len,
75 const __u8 *raw_addrs, int addrs_len);
76static int sctp_process_param(struct sctp_association *asoc,
77 union sctp_params param,
78 const union sctp_addr *peer_addr,
Al Virodd0fc662005-10-07 07:46:04 +010079 gfp_t gfp);
Vlad Yasevich8ee4be32007-11-29 08:50:35 -050080static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
81 const void *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/* What was the inbound interface for this chunk? */
84int sctp_chunk_iif(const struct sctp_chunk *chunk)
85{
86 struct sctp_af *af;
87 int iif = 0;
88
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070089 af = sctp_get_af_specific(ipver2af(ip_hdr(chunk->skb)->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if (af)
91 iif = af->skb_iif(chunk->skb);
92
93 return iif;
94}
95
96/* RFC 2960 3.3.2 Initiation (INIT) (1)
97 *
98 * Note 2: The ECN capable field is reserved for future use of
99 * Explicit Congestion Notification.
100 */
101static const struct sctp_paramhdr ecap_param = {
102 SCTP_PARAM_ECN_CAPABLE,
103 __constant_htons(sizeof(struct sctp_paramhdr)),
104};
105static const struct sctp_paramhdr prsctp_param = {
106 SCTP_PARAM_FWD_TSN_SUPPORT,
107 __constant_htons(sizeof(struct sctp_paramhdr)),
108};
109
110/* A helper to initialize to initialize an op error inside a
111 * provided chunk, as most cause codes will be embedded inside an
112 * abort chunk.
113 */
Al Viro5bf2db02006-11-20 16:59:45 -0800114void sctp_init_cause(struct sctp_chunk *chunk, __be16 cause_code,
Wei Yongjun00f1c2d2007-08-21 15:50:01 +0800115 size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 sctp_errhdr_t err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 __u16 len;
119
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900120 /* Cause code constants are now defined in network order. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 err.cause = cause_code;
122 len = sizeof(sctp_errhdr_t) + paylen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 err.length = htons(len);
Vlad Yasevich4a1c0102007-01-09 14:35:51 -0800124 chunk->subh.err_hdr = sctp_addto_chunk(chunk, sizeof(sctp_errhdr_t), &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
127/* 3.3.2 Initiation (INIT) (1)
128 *
129 * This chunk is used to initiate a SCTP association between two
130 * endpoints. The format of the INIT chunk is shown below:
131 *
132 * 0 1 2 3
133 * 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
134 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
135 * | Type = 1 | Chunk Flags | Chunk Length |
136 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
137 * | Initiate Tag |
138 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
139 * | Advertised Receiver Window Credit (a_rwnd) |
140 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
141 * | Number of Outbound Streams | Number of Inbound Streams |
142 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
143 * | Initial TSN |
144 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
145 * \ \
146 * / Optional/Variable-Length Parameters /
147 * \ \
148 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
149 *
150 *
151 * The INIT chunk contains the following parameters. Unless otherwise
152 * noted, each parameter MUST only be included once in the INIT chunk.
153 *
154 * Fixed Parameters Status
155 * ----------------------------------------------
156 * Initiate Tag Mandatory
157 * Advertised Receiver Window Credit Mandatory
158 * Number of Outbound Streams Mandatory
159 * Number of Inbound Streams Mandatory
160 * Initial TSN Mandatory
161 *
162 * Variable Parameters Status Type Value
163 * -------------------------------------------------------------
164 * IPv4 Address (Note 1) Optional 5
165 * IPv6 Address (Note 1) Optional 6
166 * Cookie Preservative Optional 9
167 * Reserved for ECN Capable (Note 2) Optional 32768 (0x8000)
168 * Host Name Address (Note 3) Optional 11
169 * Supported Address Types (Note 4) Optional 12
170 */
171struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
172 const struct sctp_bind_addr *bp,
Al Virodd0fc662005-10-07 07:46:04 +0100173 gfp_t gfp, int vparam_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 sctp_inithdr_t init;
176 union sctp_params addrs;
177 size_t chunksize;
178 struct sctp_chunk *retval = NULL;
179 int num_types, addrs_len = 0;
180 struct sctp_sock *sp;
181 sctp_supported_addrs_param_t sat;
Al Viro3dbe8652006-11-20 17:25:49 -0800182 __be16 types[2];
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800183 sctp_adaptation_ind_param_t aiparam;
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700184 sctp_supported_ext_param_t ext_param;
185 int num_ext = 0;
186 __u8 extensions[3];
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700187 sctp_paramhdr_t *auth_chunks = NULL,
188 *auth_hmacs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 /* RFC 2960 3.3.2 Initiation (INIT) (1)
191 *
192 * Note 1: The INIT chunks can contain multiple addresses that
193 * can be IPv4 and/or IPv6 in any combination.
194 */
195 retval = NULL;
196
197 /* Convert the provided bind address list to raw format. */
198 addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, gfp);
199
200 init.init_tag = htonl(asoc->c.my_vtag);
201 init.a_rwnd = htonl(asoc->rwnd);
202 init.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
203 init.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
204 init.initial_tsn = htonl(asoc->c.initial_tsn);
205
206 /* How many address types are needed? */
207 sp = sctp_sk(asoc->base.sk);
208 num_types = sp->pf->supported_addrs(sp, types);
209
210 chunksize = sizeof(init) + addrs_len + SCTP_SAT_LEN(num_types);
211 chunksize += sizeof(ecap_param);
Vlad Yasevich8ee4be32007-11-29 08:50:35 -0500212
Vlad Yasevich036b5792008-01-07 00:28:16 -0800213 if (sctp_prsctp_enable)
214 chunksize += sizeof(prsctp_param);
215
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700216 /* ADDIP: Section 4.2.7:
217 * An implementation supporting this extension [ADDIP] MUST list
218 * the ASCONF,the ASCONF-ACK, and the AUTH chunks in its INIT and
219 * INIT-ACK parameters.
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700220 */
221 if (sctp_addip_enable) {
222 extensions[num_ext] = SCTP_CID_ASCONF;
223 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
224 num_ext += 2;
225 }
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 chunksize += sizeof(aiparam);
228 chunksize += vparam_len;
229
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700230 /* Account for AUTH related parameters */
231 if (sctp_auth_enable) {
232 /* Add random parameter length*/
233 chunksize += sizeof(asoc->c.auth_random);
234
235 /* Add HMACS parameter length if any were defined */
236 auth_hmacs = (sctp_paramhdr_t *)asoc->c.auth_hmacs;
237 if (auth_hmacs->length)
238 chunksize += ntohs(auth_hmacs->length);
239 else
240 auth_hmacs = NULL;
241
242 /* Add CHUNKS parameter length */
243 auth_chunks = (sctp_paramhdr_t *)asoc->c.auth_chunks;
244 if (auth_chunks->length)
245 chunksize += ntohs(auth_chunks->length);
246 else
Vlad Yasevich9baffaa2007-11-29 08:44:34 -0500247 auth_chunks = NULL;
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700248
249 extensions[num_ext] = SCTP_CID_AUTH;
250 num_ext += 1;
251 }
252
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700253 /* If we have any extensions to report, account for that */
254 if (num_ext)
255 chunksize += sizeof(sctp_supported_ext_param_t) + num_ext;
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 /* RFC 2960 3.3.2 Initiation (INIT) (1)
258 *
259 * Note 3: An INIT chunk MUST NOT contain more than one Host
260 * Name address parameter. Moreover, the sender of the INIT
261 * MUST NOT combine any other address types with the Host Name
262 * address in the INIT. The receiver of INIT MUST ignore any
263 * other address types if the Host Name address parameter is
264 * present in the received INIT chunk.
265 *
266 * PLEASE DO NOT FIXME [This version does not support Host Name.]
267 */
268
269 retval = sctp_make_chunk(asoc, SCTP_CID_INIT, 0, chunksize);
270 if (!retval)
271 goto nodata;
272
273 retval->subh.init_hdr =
274 sctp_addto_chunk(retval, sizeof(init), &init);
275 retval->param_hdr.v =
276 sctp_addto_chunk(retval, addrs_len, addrs.v);
277
278 /* RFC 2960 3.3.2 Initiation (INIT) (1)
279 *
280 * Note 4: This parameter, when present, specifies all the
281 * address types the sending endpoint can support. The absence
282 * of this parameter indicates that the sending endpoint can
283 * support any address type.
284 */
285 sat.param_hdr.type = SCTP_PARAM_SUPPORTED_ADDRESS_TYPES;
286 sat.param_hdr.length = htons(SCTP_SAT_LEN(num_types));
287 sctp_addto_chunk(retval, sizeof(sat), &sat);
288 sctp_addto_chunk(retval, num_types * sizeof(__u16), &types);
289
290 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700291
Joe Perches7aa1b542007-12-20 14:03:52 -0800292 /* Add the supported extensions parameter. Be nice and add this
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700293 * fist before addiding the parameters for the extensions themselves
294 */
295 if (num_ext) {
296 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
297 ext_param.param_hdr.length =
298 htons(sizeof(sctp_supported_ext_param_t) + num_ext);
299 sctp_addto_chunk(retval, sizeof(sctp_supported_ext_param_t),
300 &ext_param);
Vlad Yasevich8ee4be32007-11-29 08:50:35 -0500301 sctp_addto_param(retval, num_ext, extensions);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700302 }
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (sctp_prsctp_enable)
305 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700306
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800307 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 aiparam.param_hdr.length = htons(sizeof(aiparam));
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800309 aiparam.adaptation_ind = htonl(sp->adaptation_ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700311
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700312 /* Add SCTP-AUTH chunks to the parameter list */
313 if (sctp_auth_enable) {
314 sctp_addto_chunk(retval, sizeof(asoc->c.auth_random),
315 asoc->c.auth_random);
316 if (auth_hmacs)
317 sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
318 auth_hmacs);
319 if (auth_chunks)
320 sctp_addto_chunk(retval, ntohs(auth_chunks->length),
321 auth_chunks);
322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323nodata:
Jesper Juhla51482b2005-11-08 09:41:34 -0800324 kfree(addrs.v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return retval;
326}
327
328struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
329 const struct sctp_chunk *chunk,
Al Virodd0fc662005-10-07 07:46:04 +0100330 gfp_t gfp, int unkparam_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 sctp_inithdr_t initack;
333 struct sctp_chunk *retval;
334 union sctp_params addrs;
335 int addrs_len;
336 sctp_cookie_param_t *cookie;
337 int cookie_len;
338 size_t chunksize;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800339 sctp_adaptation_ind_param_t aiparam;
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700340 sctp_supported_ext_param_t ext_param;
341 int num_ext = 0;
342 __u8 extensions[3];
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700343 sctp_paramhdr_t *auth_chunks = NULL,
344 *auth_hmacs = NULL,
345 *auth_random = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 retval = NULL;
348
349 /* Note: there may be no addresses to embed. */
350 addrs = sctp_bind_addrs_to_raw(&asoc->base.bind_addr, &addrs_len, gfp);
351
352 initack.init_tag = htonl(asoc->c.my_vtag);
353 initack.a_rwnd = htonl(asoc->rwnd);
354 initack.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
355 initack.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
356 initack.initial_tsn = htonl(asoc->c.initial_tsn);
357
358 /* FIXME: We really ought to build the cookie right
359 * into the packet instead of allocating more fresh memory.
360 */
361 cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
362 addrs.v, addrs_len);
363 if (!cookie)
364 goto nomem_cookie;
365
366 /* Calculate the total size of allocation, include the reserved
367 * space for reporting unknown parameters if it is specified.
368 */
369 chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;
370
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900371 /* Tell peer that we'll do ECN only if peer advertised such cap. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (asoc->peer.ecn_capable)
373 chunksize += sizeof(ecap_param);
374
Vlad Yasevich036b5792008-01-07 00:28:16 -0800375 if (sctp_prsctp_enable)
376 chunksize += sizeof(prsctp_param);
377
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700378 if (sctp_addip_enable) {
379 extensions[num_ext] = SCTP_CID_ASCONF;
380 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
381 num_ext += 2;
382 }
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 chunksize += sizeof(aiparam);
385
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700386 if (asoc->peer.auth_capable) {
387 auth_random = (sctp_paramhdr_t *)asoc->c.auth_random;
388 chunksize += ntohs(auth_random->length);
389
390 auth_hmacs = (sctp_paramhdr_t *)asoc->c.auth_hmacs;
391 if (auth_hmacs->length)
392 chunksize += ntohs(auth_hmacs->length);
393 else
394 auth_hmacs = NULL;
395
396 auth_chunks = (sctp_paramhdr_t *)asoc->c.auth_chunks;
397 if (auth_chunks->length)
398 chunksize += ntohs(auth_chunks->length);
399 else
400 auth_chunks = NULL;
401
402 extensions[num_ext] = SCTP_CID_AUTH;
403 num_ext += 1;
404 }
405
Vlad Yasevich8ee4be32007-11-29 08:50:35 -0500406 if (num_ext)
407 chunksize += sizeof(sctp_supported_ext_param_t) + num_ext;
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /* Now allocate and fill out the chunk. */
410 retval = sctp_make_chunk(asoc, SCTP_CID_INIT_ACK, 0, chunksize);
411 if (!retval)
412 goto nomem_chunk;
413
414 /* Per the advice in RFC 2960 6.4, send this reply to
415 * the source of the INIT packet.
416 */
417 retval->transport = chunk->transport;
418 retval->subh.init_hdr =
419 sctp_addto_chunk(retval, sizeof(initack), &initack);
420 retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
421 sctp_addto_chunk(retval, cookie_len, cookie);
422 if (asoc->peer.ecn_capable)
423 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700424 if (num_ext) {
425 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
426 ext_param.param_hdr.length =
427 htons(sizeof(sctp_supported_ext_param_t) + num_ext);
428 sctp_addto_chunk(retval, sizeof(sctp_supported_ext_param_t),
429 &ext_param);
Vlad Yasevich8ee4be32007-11-29 08:50:35 -0500430 sctp_addto_param(retval, num_ext, extensions);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (asoc->peer.prsctp_capable)
433 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
434
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800435 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 aiparam.param_hdr.length = htons(sizeof(aiparam));
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800437 aiparam.adaptation_ind = htonl(sctp_sk(asoc->base.sk)->adaptation_ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
439
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700440 if (asoc->peer.auth_capable) {
441 sctp_addto_chunk(retval, ntohs(auth_random->length),
442 auth_random);
443 if (auth_hmacs)
444 sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
445 auth_hmacs);
446 if (auth_chunks)
447 sctp_addto_chunk(retval, ntohs(auth_chunks->length),
448 auth_chunks);
449 }
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 /* We need to remove the const qualifier at this point. */
452 retval->asoc = (struct sctp_association *) asoc;
453
454 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
455 *
456 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
457 * HEARTBEAT ACK, * etc.) to the same destination transport
458 * address from which it received the DATA or control chunk
459 * to which it is replying.
460 *
461 * [INIT ACK back to where the INIT came from.]
462 */
463 if (chunk)
464 retval->transport = chunk->transport;
465
466nomem_chunk:
467 kfree(cookie);
468nomem_cookie:
Jesper Juhla51482b2005-11-08 09:41:34 -0800469 kfree(addrs.v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return retval;
471}
472
473/* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
474 *
475 * This chunk is used only during the initialization of an association.
476 * It is sent by the initiator of an association to its peer to complete
477 * the initialization process. This chunk MUST precede any DATA chunk
478 * sent within the association, but MAY be bundled with one or more DATA
479 * chunks in the same packet.
480 *
481 * 0 1 2 3
482 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
483 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
484 * | Type = 10 |Chunk Flags | Length |
485 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
486 * / Cookie /
487 * \ \
488 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
489 *
490 * Chunk Flags: 8 bit
491 *
492 * Set to zero on transmit and ignored on receipt.
493 *
494 * Length: 16 bits (unsigned integer)
495 *
496 * Set to the size of the chunk in bytes, including the 4 bytes of
497 * the chunk header and the size of the Cookie.
498 *
499 * Cookie: variable size
500 *
501 * This field must contain the exact cookie received in the
502 * State Cookie parameter from the previous INIT ACK.
503 *
504 * An implementation SHOULD make the cookie as small as possible
505 * to insure interoperability.
506 */
507struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
508 const struct sctp_chunk *chunk)
509{
510 struct sctp_chunk *retval;
511 void *cookie;
512 int cookie_len;
513
514 cookie = asoc->peer.cookie;
515 cookie_len = asoc->peer.cookie_len;
516
517 /* Build a cookie echo chunk. */
518 retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ECHO, 0, cookie_len);
519 if (!retval)
520 goto nodata;
521 retval->subh.cookie_hdr =
522 sctp_addto_chunk(retval, cookie_len, cookie);
523
524 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
525 *
526 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
527 * HEARTBEAT ACK, * etc.) to the same destination transport
528 * address from which it * received the DATA or control chunk
529 * to which it is replying.
530 *
531 * [COOKIE ECHO back to where the INIT ACK came from.]
532 */
533 if (chunk)
534 retval->transport = chunk->transport;
535
536nodata:
537 return retval;
538}
539
540/* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
541 *
542 * This chunk is used only during the initialization of an
543 * association. It is used to acknowledge the receipt of a COOKIE
544 * ECHO chunk. This chunk MUST precede any DATA or SACK chunk sent
545 * within the association, but MAY be bundled with one or more DATA
546 * chunks or SACK chunk in the same SCTP packet.
547 *
548 * 0 1 2 3
549 * 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
550 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
551 * | Type = 11 |Chunk Flags | Length = 4 |
552 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
553 *
554 * Chunk Flags: 8 bits
555 *
556 * Set to zero on transmit and ignored on receipt.
557 */
558struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
559 const struct sctp_chunk *chunk)
560{
561 struct sctp_chunk *retval;
562
563 retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ACK, 0, 0);
564
565 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
566 *
567 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
568 * HEARTBEAT ACK, * etc.) to the same destination transport
569 * address from which it * received the DATA or control chunk
570 * to which it is replying.
571 *
572 * [COOKIE ACK back to where the COOKIE ECHO came from.]
573 */
574 if (retval && chunk)
575 retval->transport = chunk->transport;
576
577 return retval;
578}
579
580/*
581 * Appendix A: Explicit Congestion Notification:
582 * CWR:
583 *
584 * RFC 2481 details a specific bit for a sender to send in the header of
585 * its next outbound TCP segment to indicate to its peer that it has
586 * reduced its congestion window. This is termed the CWR bit. For
587 * SCTP the same indication is made by including the CWR chunk.
588 * This chunk contains one data element, i.e. the TSN number that
589 * was sent in the ECNE chunk. This element represents the lowest
590 * TSN number in the datagram that was originally marked with the
591 * CE bit.
592 *
593 * 0 1 2 3
594 * 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
595 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
596 * | Chunk Type=13 | Flags=00000000| Chunk Length = 8 |
597 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
598 * | Lowest TSN Number |
599 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
600 *
601 * Note: The CWR is considered a Control chunk.
602 */
603struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
604 const __u32 lowest_tsn,
605 const struct sctp_chunk *chunk)
606{
607 struct sctp_chunk *retval;
608 sctp_cwrhdr_t cwr;
609
610 cwr.lowest_tsn = htonl(lowest_tsn);
611 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_CWR, 0,
612 sizeof(sctp_cwrhdr_t));
613
614 if (!retval)
615 goto nodata;
616
617 retval->subh.ecn_cwr_hdr =
618 sctp_addto_chunk(retval, sizeof(cwr), &cwr);
619
620 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
621 *
622 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
623 * HEARTBEAT ACK, * etc.) to the same destination transport
624 * address from which it * received the DATA or control chunk
625 * to which it is replying.
626 *
627 * [Report a reduced congestion window back to where the ECNE
628 * came from.]
629 */
630 if (chunk)
631 retval->transport = chunk->transport;
632
633nodata:
634 return retval;
635}
636
637/* Make an ECNE chunk. This is a congestion experienced report. */
638struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
639 const __u32 lowest_tsn)
640{
641 struct sctp_chunk *retval;
642 sctp_ecnehdr_t ecne;
643
644 ecne.lowest_tsn = htonl(lowest_tsn);
645 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_ECNE, 0,
646 sizeof(sctp_ecnehdr_t));
647 if (!retval)
648 goto nodata;
649 retval->subh.ecne_hdr =
650 sctp_addto_chunk(retval, sizeof(ecne), &ecne);
651
652nodata:
653 return retval;
654}
655
656/* Make a DATA chunk for the given association from the provided
657 * parameters. However, do not populate the data payload.
658 */
659struct sctp_chunk *sctp_make_datafrag_empty(struct sctp_association *asoc,
660 const struct sctp_sndrcvinfo *sinfo,
661 int data_len, __u8 flags, __u16 ssn)
662{
663 struct sctp_chunk *retval;
664 struct sctp_datahdr dp;
665 int chunk_len;
666
667 /* We assign the TSN as LATE as possible, not here when
668 * creating the chunk.
669 */
670 dp.tsn = 0;
671 dp.stream = htons(sinfo->sinfo_stream);
672 dp.ppid = sinfo->sinfo_ppid;
673
674 /* Set the flags for an unordered send. */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -0700675 if (sinfo->sinfo_flags & SCTP_UNORDERED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 flags |= SCTP_DATA_UNORDERED;
677 dp.ssn = 0;
678 } else
679 dp.ssn = htons(ssn);
680
681 chunk_len = sizeof(dp) + data_len;
682 retval = sctp_make_chunk(asoc, SCTP_CID_DATA, flags, chunk_len);
683 if (!retval)
684 goto nodata;
685
686 retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
687 memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
688
689nodata:
690 return retval;
691}
692
693/* Create a selective ackowledgement (SACK) for the given
694 * association. This reports on which TSN's we've seen to date,
695 * including duplicates and gaps.
696 */
697struct sctp_chunk *sctp_make_sack(const struct sctp_association *asoc)
698{
699 struct sctp_chunk *retval;
700 struct sctp_sackhdr sack;
701 int len;
702 __u32 ctsn;
703 __u16 num_gabs, num_dup_tsns;
704 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
705
706 ctsn = sctp_tsnmap_get_ctsn(map);
707 SCTP_DEBUG_PRINTK("sackCTSNAck sent: 0x%x.\n", ctsn);
708
709 /* How much room is needed in the chunk? */
710 num_gabs = sctp_tsnmap_num_gabs(map);
711 num_dup_tsns = sctp_tsnmap_num_dups(map);
712
713 /* Initialize the SACK header. */
714 sack.cum_tsn_ack = htonl(ctsn);
715 sack.a_rwnd = htonl(asoc->a_rwnd);
716 sack.num_gap_ack_blocks = htons(num_gabs);
717 sack.num_dup_tsns = htons(num_dup_tsns);
718
719 len = sizeof(sack)
720 + sizeof(struct sctp_gap_ack_block) * num_gabs
721 + sizeof(__u32) * num_dup_tsns;
722
723 /* Create the chunk. */
724 retval = sctp_make_chunk(asoc, SCTP_CID_SACK, 0, len);
725 if (!retval)
726 goto nodata;
727
728 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
729 *
730 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
731 * HEARTBEAT ACK, etc.) to the same destination transport
732 * address from which it received the DATA or control chunk to
733 * which it is replying. This rule should also be followed if
734 * the endpoint is bundling DATA chunks together with the
735 * reply chunk.
736 *
737 * However, when acknowledging multiple DATA chunks received
738 * in packets from different source addresses in a single
739 * SACK, the SACK chunk may be transmitted to one of the
740 * destination transport addresses from which the DATA or
741 * control chunks being acknowledged were received.
742 *
743 * [BUG: We do not implement the following paragraph.
744 * Perhaps we should remember the last transport we used for a
745 * SACK and avoid that (if possible) if we have seen any
746 * duplicates. --piggy]
747 *
748 * When a receiver of a duplicate DATA chunk sends a SACK to a
749 * multi- homed endpoint it MAY be beneficial to vary the
750 * destination address and not use the source address of the
751 * DATA chunk. The reason being that receiving a duplicate
752 * from a multi-homed endpoint might indicate that the return
753 * path (as specified in the source address of the DATA chunk)
754 * for the SACK is broken.
755 *
756 * [Send to the address from which we last received a DATA chunk.]
757 */
758 retval->transport = asoc->peer.last_data_from;
759
760 retval->subh.sack_hdr =
761 sctp_addto_chunk(retval, sizeof(sack), &sack);
762
763 /* Add the gap ack block information. */
764 if (num_gabs)
765 sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
766 sctp_tsnmap_get_gabs(map));
767
768 /* Add the duplicate TSN information. */
769 if (num_dup_tsns)
770 sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
771 sctp_tsnmap_get_dups(map));
772
773nodata:
774 return retval;
775}
776
777/* Make a SHUTDOWN chunk. */
778struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
779 const struct sctp_chunk *chunk)
780{
781 struct sctp_chunk *retval;
782 sctp_shutdownhdr_t shut;
783 __u32 ctsn;
784
785 ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
786 shut.cum_tsn_ack = htonl(ctsn);
787
788 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN, 0,
789 sizeof(sctp_shutdownhdr_t));
790 if (!retval)
791 goto nodata;
792
793 retval->subh.shutdown_hdr =
794 sctp_addto_chunk(retval, sizeof(shut), &shut);
795
796 if (chunk)
797 retval->transport = chunk->transport;
798nodata:
799 return retval;
800}
801
802struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
803 const struct sctp_chunk *chunk)
804{
805 struct sctp_chunk *retval;
806
807 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0);
808
809 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
810 *
811 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
812 * HEARTBEAT ACK, * etc.) to the same destination transport
813 * address from which it * received the DATA or control chunk
814 * to which it is replying.
815 *
816 * [ACK back to where the SHUTDOWN came from.]
817 */
818 if (retval && chunk)
819 retval->transport = chunk->transport;
820
821 return retval;
822}
823
824struct sctp_chunk *sctp_make_shutdown_complete(
825 const struct sctp_association *asoc,
826 const struct sctp_chunk *chunk)
827{
828 struct sctp_chunk *retval;
829 __u8 flags = 0;
830
Jerome Forissier047a2422005-04-28 11:58:43 -0700831 /* Set the T-bit if we have no association (vtag will be
832 * reflected)
833 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
835
836 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags, 0);
837
838 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
839 *
840 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
841 * HEARTBEAT ACK, * etc.) to the same destination transport
842 * address from which it * received the DATA or control chunk
843 * to which it is replying.
844 *
845 * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
846 * came from.]
847 */
848 if (retval && chunk)
849 retval->transport = chunk->transport;
850
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900851 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
854/* Create an ABORT. Note that we set the T bit if we have no
Jerome Forissier047a2422005-04-28 11:58:43 -0700855 * association, except when responding to an INIT (sctpimpguide 2.41).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 */
857struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
858 const struct sctp_chunk *chunk,
859 const size_t hint)
860{
861 struct sctp_chunk *retval;
862 __u8 flags = 0;
863
Jerome Forissier047a2422005-04-28 11:58:43 -0700864 /* Set the T-bit if we have no association and 'chunk' is not
865 * an INIT (vtag will be reflected).
866 */
867 if (!asoc) {
868 if (chunk && chunk->chunk_hdr &&
869 chunk->chunk_hdr->type == SCTP_CID_INIT)
870 flags = 0;
871 else
872 flags = SCTP_CHUNK_FLAG_T;
873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 retval = sctp_make_chunk(asoc, SCTP_CID_ABORT, flags, hint);
876
877 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
878 *
879 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
880 * HEARTBEAT ACK, * etc.) to the same destination transport
881 * address from which it * received the DATA or control chunk
882 * to which it is replying.
883 *
884 * [ABORT back to where the offender came from.]
885 */
886 if (retval && chunk)
887 retval->transport = chunk->transport;
888
889 return retval;
890}
891
892/* Helper to create ABORT with a NO_USER_DATA error. */
893struct sctp_chunk *sctp_make_abort_no_data(
894 const struct sctp_association *asoc,
895 const struct sctp_chunk *chunk, __u32 tsn)
896{
897 struct sctp_chunk *retval;
Al Viro9f81bcd2006-11-20 17:26:34 -0800898 __be32 payload;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t)
901 + sizeof(tsn));
902
903 if (!retval)
904 goto no_mem;
905
906 /* Put the tsn back into network byte order. */
907 payload = htonl(tsn);
Wei Yongjun00f1c2d2007-08-21 15:50:01 +0800908 sctp_init_cause(retval, SCTP_ERROR_NO_DATA, sizeof(payload));
909 sctp_addto_chunk(retval, sizeof(payload), (const void *)&payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
912 *
913 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
914 * HEARTBEAT ACK, * etc.) to the same destination transport
915 * address from which it * received the DATA or control chunk
916 * to which it is replying.
917 *
918 * [ABORT back to where the offender came from.]
919 */
920 if (chunk)
921 retval->transport = chunk->transport;
922
923no_mem:
924 return retval;
925}
926
927/* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error. */
928struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
Sridhar Samudralac164a9b2006-08-22 11:50:39 -0700929 const struct msghdr *msg,
930 size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
932 struct sctp_chunk *retval;
Sridhar Samudralac164a9b2006-08-22 11:50:39 -0700933 void *payload = NULL;
934 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Sridhar Samudralac164a9b2006-08-22 11:50:39 -0700936 retval = sctp_make_abort(asoc, NULL, sizeof(sctp_errhdr_t) + paylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (!retval)
938 goto err_chunk;
939
940 if (paylen) {
941 /* Put the msg_iov together into payload. */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -0700942 payload = kmalloc(paylen, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (!payload)
944 goto err_payload;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Sridhar Samudralac164a9b2006-08-22 11:50:39 -0700946 err = memcpy_fromiovec(payload, msg->msg_iov, paylen);
947 if (err < 0)
948 goto err_copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950
Wei Yongjun00f1c2d2007-08-21 15:50:01 +0800951 sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, paylen);
952 sctp_addto_chunk(retval, paylen, payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 if (paylen)
955 kfree(payload);
956
957 return retval;
958
959err_copy:
960 kfree(payload);
961err_payload:
962 sctp_chunk_free(retval);
963 retval = NULL;
964err_chunk:
965 return retval;
966}
967
Adrian Bunk5c94bf82007-09-12 15:16:21 +0200968/* Append bytes to the end of a parameter. Will panic if chunk is not big
969 * enough.
970 */
971static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
972 const void *data)
973{
974 void *target;
975 int chunklen = ntohs(chunk->chunk_hdr->length);
976
977 target = skb_put(chunk->skb, len);
978
979 memcpy(target, data, len);
980
981 /* Adjust the chunk length field. */
982 chunk->chunk_hdr->length = htons(chunklen + len);
983 chunk->chunk_end = skb_tail_pointer(chunk->skb);
984
985 return target;
986}
987
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900988/* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989struct sctp_chunk *sctp_make_abort_violation(
990 const struct sctp_association *asoc,
991 const struct sctp_chunk *chunk,
992 const __u8 *payload,
993 const size_t paylen)
994{
995 struct sctp_chunk *retval;
996 struct sctp_paramhdr phdr;
997
998 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t) + paylen
Wei Yongjun00f1c2d2007-08-21 15:50:01 +0800999 + sizeof(sctp_paramhdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (!retval)
1001 goto end;
1002
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001003 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, paylen
1004 + sizeof(sctp_paramhdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 phdr.type = htons(chunk->chunk_hdr->type);
1007 phdr.length = chunk->chunk_hdr->length;
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001008 sctp_addto_chunk(retval, paylen, payload);
1009 sctp_addto_param(retval, sizeof(sctp_paramhdr_t), &phdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011end:
1012 return retval;
1013}
1014
1015/* Make a HEARTBEAT chunk. */
1016struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
1017 const struct sctp_transport *transport,
1018 const void *payload, const size_t paylen)
1019{
1020 struct sctp_chunk *retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT,
1021 0, paylen);
1022
1023 if (!retval)
1024 goto nodata;
1025
1026 /* Cast away the 'const', as this is just telling the chunk
1027 * what transport it belongs to.
1028 */
1029 retval->transport = (struct sctp_transport *) transport;
1030 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
1031
1032nodata:
1033 return retval;
1034}
1035
1036struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
1037 const struct sctp_chunk *chunk,
1038 const void *payload, const size_t paylen)
1039{
1040 struct sctp_chunk *retval;
1041
1042 retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen);
1043 if (!retval)
1044 goto nodata;
1045
1046 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
1047
1048 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1049 *
1050 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1051 * HEARTBEAT ACK, * etc.) to the same destination transport
1052 * address from which it * received the DATA or control chunk
1053 * to which it is replying.
1054 *
1055 * [HBACK back to where the HEARTBEAT came from.]
1056 */
1057 if (chunk)
1058 retval->transport = chunk->transport;
1059
1060nodata:
1061 return retval;
1062}
1063
1064/* Create an Operation Error chunk with the specified space reserved.
1065 * This routine can be used for containing multiple causes in the chunk.
1066 */
1067static struct sctp_chunk *sctp_make_op_error_space(
1068 const struct sctp_association *asoc,
1069 const struct sctp_chunk *chunk,
1070 size_t size)
1071{
1072 struct sctp_chunk *retval;
1073
1074 retval = sctp_make_chunk(asoc, SCTP_CID_ERROR, 0,
1075 sizeof(sctp_errhdr_t) + size);
1076 if (!retval)
1077 goto nodata;
1078
1079 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1080 *
1081 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1082 * HEARTBEAT ACK, etc.) to the same destination transport
1083 * address from which it received the DATA or control chunk
1084 * to which it is replying.
1085 *
1086 */
1087 if (chunk)
1088 retval->transport = chunk->transport;
1089
1090nodata:
1091 return retval;
1092}
1093
1094/* Create an Operation Error chunk. */
1095struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
1096 const struct sctp_chunk *chunk,
Al Viro63706c52006-11-20 17:00:05 -08001097 __be16 cause_code, const void *payload,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 size_t paylen)
1099{
1100 struct sctp_chunk *retval;
1101
1102 retval = sctp_make_op_error_space(asoc, chunk, paylen);
1103 if (!retval)
1104 goto nodata;
1105
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001106 sctp_init_cause(retval, cause_code, paylen);
1107 sctp_addto_chunk(retval, paylen, payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109nodata:
1110 return retval;
1111}
1112
Vlad Yasevich4cd57c82007-09-16 19:32:45 -07001113struct sctp_chunk *sctp_make_auth(const struct sctp_association *asoc)
1114{
1115 struct sctp_chunk *retval;
1116 struct sctp_hmac *hmac_desc;
1117 struct sctp_authhdr auth_hdr;
1118 __u8 *hmac;
1119
1120 /* Get the first hmac that the peer told us to use */
1121 hmac_desc = sctp_auth_asoc_get_hmac(asoc);
1122 if (unlikely(!hmac_desc))
1123 return NULL;
1124
1125 retval = sctp_make_chunk(asoc, SCTP_CID_AUTH, 0,
1126 hmac_desc->hmac_len + sizeof(sctp_authhdr_t));
1127 if (!retval)
1128 return NULL;
1129
1130 auth_hdr.hmac_id = htons(hmac_desc->hmac_id);
1131 auth_hdr.shkey_id = htons(asoc->active_key_id);
1132
1133 retval->subh.auth_hdr = sctp_addto_chunk(retval, sizeof(sctp_authhdr_t),
1134 &auth_hdr);
1135
1136 hmac = skb_put(retval->skb, hmac_desc->hmac_len);
1137 memset(hmac, 0, hmac_desc->hmac_len);
1138
1139 /* Adjust the chunk header to include the empty MAC */
1140 retval->chunk_hdr->length =
1141 htons(ntohs(retval->chunk_hdr->length) + hmac_desc->hmac_len);
1142 retval->chunk_end = skb_tail_pointer(retval->skb);
1143
1144 return retval;
1145}
1146
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148/********************************************************************
1149 * 2nd Level Abstractions
1150 ********************************************************************/
1151
1152/* Turn an skb into a chunk.
1153 * FIXME: Eventually move the structure directly inside the skb->cb[].
1154 */
1155struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
1156 const struct sctp_association *asoc,
1157 struct sock *sk)
1158{
1159 struct sctp_chunk *retval;
1160
Robert P. J. Dayc3762222007-02-10 01:45:03 -08001161 retval = kmem_cache_zalloc(sctp_chunk_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 if (!retval)
1164 goto nodata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 if (!sk) {
1167 SCTP_DEBUG_PRINTK("chunkifying skb %p w/o an sk\n", skb);
1168 }
1169
David S. Miller79af02c2005-07-08 21:47:49 -07001170 INIT_LIST_HEAD(&retval->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 retval->skb = skb;
1172 retval->asoc = (struct sctp_association *)asoc;
1173 retval->resent = 0;
1174 retval->has_tsn = 0;
1175 retval->has_ssn = 0;
1176 retval->rtt_in_progress = 0;
1177 retval->sent_at = 0;
1178 retval->singleton = 1;
1179 retval->end_of_packet = 0;
1180 retval->ecn_ce_done = 0;
1181 retval->pdiscard = 0;
1182
1183 /* sctpimpguide-05.txt Section 2.8.2
1184 * M1) Each time a new DATA chunk is transmitted
1185 * set the 'TSN.Missing.Report' count for that TSN to 0. The
1186 * 'TSN.Missing.Report' count will be used to determine missing chunks
1187 * and when to fast retransmit.
1188 */
1189 retval->tsn_missing_report = 0;
1190 retval->tsn_gap_acked = 0;
1191 retval->fast_retransmit = 0;
1192
1193 /* If this is a fragmented message, track all fragments
1194 * of the message (for SEND_FAILED).
1195 */
1196 retval->msg = NULL;
1197
1198 /* Polish the bead hole. */
1199 INIT_LIST_HEAD(&retval->transmitted_list);
1200 INIT_LIST_HEAD(&retval->frag_list);
1201 SCTP_DBG_OBJCNT_INC(chunk);
1202 atomic_set(&retval->refcnt, 1);
1203
1204nodata:
1205 return retval;
1206}
1207
1208/* Set chunk->source and dest based on the IP header in chunk->skb. */
1209void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
1210 union sctp_addr *dest)
1211{
Al Virof235fca2006-11-20 17:09:01 -08001212 memcpy(&chunk->source, src, sizeof(union sctp_addr));
Al Viro16b0a032006-11-20 17:13:38 -08001213 memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
1216/* Extract the source address from a chunk. */
1217const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
1218{
1219 /* If we have a known transport, use that. */
1220 if (chunk->transport) {
Al Viro6a1e5f32006-11-20 17:12:25 -08001221 return &chunk->transport->ipaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 } else {
1223 /* Otherwise, extract it from the IP header. */
Al Viro6a1e5f32006-11-20 17:12:25 -08001224 return &chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
1226}
1227
1228/* Create a new chunk, setting the type and flags headers from the
1229 * arguments, reserving enough space for a 'paylen' byte payload.
1230 */
1231SCTP_STATIC
1232struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
1233 __u8 type, __u8 flags, int paylen)
1234{
1235 struct sctp_chunk *retval;
1236 sctp_chunkhdr_t *chunk_hdr;
1237 struct sk_buff *skb;
1238 struct sock *sk;
1239
1240 /* No need to allocate LL here, as this is only a chunk. */
1241 skb = alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t) + paylen),
1242 GFP_ATOMIC);
1243 if (!skb)
1244 goto nodata;
1245
1246 /* Make room for the chunk header. */
1247 chunk_hdr = (sctp_chunkhdr_t *)skb_put(skb, sizeof(sctp_chunkhdr_t));
1248 chunk_hdr->type = type;
1249 chunk_hdr->flags = flags;
1250 chunk_hdr->length = htons(sizeof(sctp_chunkhdr_t));
1251
1252 sk = asoc ? asoc->base.sk : NULL;
1253 retval = sctp_chunkify(skb, asoc, sk);
1254 if (!retval) {
1255 kfree_skb(skb);
1256 goto nodata;
1257 }
1258
1259 retval->chunk_hdr = chunk_hdr;
1260 retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(struct sctp_chunkhdr);
1261
Vlad Yasevich4cd57c82007-09-16 19:32:45 -07001262 /* Determine if the chunk needs to be authenticated */
1263 if (sctp_auth_send_cid(type, asoc))
1264 retval->auth = 1;
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 /* Set the skb to the belonging sock for accounting. */
1267 skb->sk = sk;
1268
1269 return retval;
1270nodata:
1271 return NULL;
1272}
1273
1274
1275/* Release the memory occupied by a chunk. */
1276static void sctp_chunk_destroy(struct sctp_chunk *chunk)
1277{
Vlad Yasevicha08de642007-12-20 14:11:47 -08001278 BUG_ON(!list_empty(&chunk->list));
1279 list_del_init(&chunk->transmitted_list);
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 /* Free the chunk skb data and the SCTP_chunk stub itself. */
1282 dev_kfree_skb(chunk->skb);
1283
1284 SCTP_DBG_OBJCNT_DEC(chunk);
1285 kmem_cache_free(sctp_chunk_cachep, chunk);
1286}
1287
1288/* Possibly, free the chunk. */
1289void sctp_chunk_free(struct sctp_chunk *chunk)
1290{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 /* Release our reference on the message tracker. */
1292 if (chunk->msg)
1293 sctp_datamsg_put(chunk->msg);
1294
1295 sctp_chunk_put(chunk);
1296}
1297
1298/* Grab a reference to the chunk. */
1299void sctp_chunk_hold(struct sctp_chunk *ch)
1300{
1301 atomic_inc(&ch->refcnt);
1302}
1303
1304/* Release a reference to the chunk. */
1305void sctp_chunk_put(struct sctp_chunk *ch)
1306{
1307 if (atomic_dec_and_test(&ch->refcnt))
1308 sctp_chunk_destroy(ch);
1309}
1310
1311/* Append bytes to the end of a chunk. Will panic if chunk is not big
1312 * enough.
1313 */
1314void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
1315{
1316 void *target;
1317 void *padding;
1318 int chunklen = ntohs(chunk->chunk_hdr->length);
Wei Yongjun8d614ad2007-08-06 13:55:47 +08001319 int padlen = WORD_ROUND(chunklen) - chunklen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 padding = skb_put(chunk->skb, padlen);
1322 target = skb_put(chunk->skb, len);
1323
1324 memset(padding, 0, padlen);
1325 memcpy(target, data, len);
1326
1327 /* Adjust the chunk length field. */
1328 chunk->chunk_hdr->length = htons(chunklen + padlen + len);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001329 chunk->chunk_end = skb_tail_pointer(chunk->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 return target;
1332}
1333
1334/* Append bytes from user space to the end of a chunk. Will panic if
1335 * chunk is not big enough.
1336 * Returns a kernel err value.
1337 */
1338int sctp_user_addto_chunk(struct sctp_chunk *chunk, int off, int len,
1339 struct iovec *data)
1340{
1341 __u8 *target;
1342 int err = 0;
1343
1344 /* Make room in chunk for data. */
1345 target = skb_put(chunk->skb, len);
1346
1347 /* Copy data (whole iovec) into chunk */
1348 if ((err = memcpy_fromiovecend(target, data, off, len)))
1349 goto out;
1350
1351 /* Adjust the chunk length field. */
1352 chunk->chunk_hdr->length =
1353 htons(ntohs(chunk->chunk_hdr->length) + len);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001354 chunk->chunk_end = skb_tail_pointer(chunk->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356out:
1357 return err;
1358}
1359
1360/* Helper function to assign a TSN if needed. This assumes that both
1361 * the data_hdr and association have already been assigned.
1362 */
1363void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
1364{
Vlad Yasevichab3e5e72007-08-02 16:51:42 -04001365 struct sctp_datamsg *msg;
1366 struct sctp_chunk *lchunk;
1367 struct sctp_stream *stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 __u16 ssn;
1369 __u16 sid;
1370
1371 if (chunk->has_ssn)
1372 return;
1373
Vlad Yasevichab3e5e72007-08-02 16:51:42 -04001374 /* All fragments will be on the same stream */
1375 sid = ntohs(chunk->subh.data_hdr->stream);
1376 stream = &chunk->asoc->ssnmap->out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Vlad Yasevichab3e5e72007-08-02 16:51:42 -04001378 /* Now assign the sequence number to the entire message.
1379 * All fragments must have the same stream sequence number.
1380 */
1381 msg = chunk->msg;
1382 list_for_each_entry(lchunk, &msg->chunks, frag_list) {
1383 if (lchunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
1384 ssn = 0;
1385 } else {
1386 if (lchunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
1387 ssn = sctp_ssn_next(stream, sid);
1388 else
1389 ssn = sctp_ssn_peek(stream, sid);
1390 }
1391
1392 lchunk->subh.data_hdr->ssn = htons(ssn);
1393 lchunk->has_ssn = 1;
1394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
1397/* Helper function to assign a TSN if needed. This assumes that both
1398 * the data_hdr and association have already been assigned.
1399 */
1400void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
1401{
1402 if (!chunk->has_tsn) {
1403 /* This is the last possible instant to
1404 * assign a TSN.
1405 */
1406 chunk->subh.data_hdr->tsn =
1407 htonl(sctp_association_get_next_tsn(chunk->asoc));
1408 chunk->has_tsn = 1;
1409 }
1410}
1411
1412/* Create a CLOSED association to use with an incoming packet. */
1413struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
Alexey Dobriyan3182cd82005-07-11 20:57:47 -07001414 struct sctp_chunk *chunk,
Al Virodd0fc662005-10-07 07:46:04 +01001415 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
1417 struct sctp_association *asoc;
1418 struct sk_buff *skb;
1419 sctp_scope_t scope;
1420 struct sctp_af *af;
1421
1422 /* Create the bare association. */
1423 scope = sctp_scope(sctp_source(chunk));
1424 asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
1425 if (!asoc)
1426 goto nodata;
1427 asoc->temp = 1;
1428 skb = chunk->skb;
1429 /* Create an entry for the source address of the packet. */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001430 af = sctp_get_af_specific(ipver2af(ip_hdr(skb)->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if (unlikely(!af))
1432 goto fail;
Al Virod55c41b2006-11-20 17:09:40 -08001433 af->from_skb(&asoc->c.peer_addr, skb, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434nodata:
1435 return asoc;
1436
1437fail:
1438 sctp_association_free(asoc);
1439 return NULL;
1440}
1441
1442/* Build a cookie representing asoc.
1443 * This INCLUDES the param header needed to put the cookie in the INIT ACK.
1444 */
1445static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
1446 const struct sctp_association *asoc,
1447 const struct sctp_chunk *init_chunk,
1448 int *cookie_len,
1449 const __u8 *raw_addrs, int addrs_len)
1450{
1451 sctp_cookie_param_t *retval;
1452 struct sctp_signed_cookie *cookie;
1453 struct scatterlist sg;
1454 int headersize, bodysize;
1455 unsigned int keylen;
1456 char *key;
1457
Vlad Yasevich9834a2b2006-01-17 11:52:12 -08001458 /* Header size is static data prior to the actual cookie, including
1459 * any padding.
1460 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001461 headersize = sizeof(sctp_paramhdr_t) +
1462 (sizeof(struct sctp_signed_cookie) -
Vlad Yasevich9834a2b2006-01-17 11:52:12 -08001463 sizeof(struct sctp_cookie));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 bodysize = sizeof(struct sctp_cookie)
1465 + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
1466
1467 /* Pad out the cookie to a multiple to make the signature
1468 * functions simpler to write.
1469 */
1470 if (bodysize % SCTP_COOKIE_MULTIPLE)
1471 bodysize += SCTP_COOKIE_MULTIPLE
1472 - (bodysize % SCTP_COOKIE_MULTIPLE);
1473 *cookie_len = headersize + bodysize;
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 /* Clear this memory since we are sending this data structure
1476 * out on the network.
1477 */
Arnaldo Carvalho de Meloaf997d82006-11-21 01:20:33 -02001478 retval = kzalloc(*cookie_len, GFP_ATOMIC);
1479 if (!retval)
1480 goto nodata;
1481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 cookie = (struct sctp_signed_cookie *) retval->body;
1483
1484 /* Set up the parameter header. */
1485 retval->p.type = SCTP_PARAM_STATE_COOKIE;
1486 retval->p.length = htons(*cookie_len);
1487
1488 /* Copy the cookie part of the association itself. */
1489 cookie->c = asoc->c;
1490 /* Save the raw address list length in the cookie. */
1491 cookie->c.raw_addr_list_len = addrs_len;
1492
1493 /* Remember PR-SCTP capability. */
1494 cookie->c.prsctp_capable = asoc->peer.prsctp_capable;
1495
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001496 /* Save adaptation indication in the cookie. */
1497 cookie->c.adaptation_ind = asoc->peer.adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499 /* Set an expiration time for the cookie. */
1500 do_gettimeofday(&cookie->c.expiration);
1501 TIMEVAL_ADD(asoc->cookie_life, cookie->c.expiration);
1502
1503 /* Copy the peer's init packet. */
1504 memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
1505 ntohs(init_chunk->chunk_hdr->length));
1506
1507 /* Copy the raw local address list of the association. */
1508 memcpy((__u8 *)&cookie->c.peer_init[0] +
1509 ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
1510
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001511 if (sctp_sk(ep->base.sk)->hmac) {
Herbert Xu1b489e12006-08-20 15:07:14 +10001512 struct hash_desc desc;
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 /* Sign the message. */
Herbert Xu68e3f5d2007-10-27 00:52:07 -07001515 sg_init_one(&sg, &cookie->c, bodysize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 keylen = SCTP_SECRET_SIZE;
1517 key = (char *)ep->secret_key[ep->current_key];
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001518 desc.tfm = sctp_sk(ep->base.sk)->hmac;
1519 desc.flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Herbert Xu1b489e12006-08-20 15:07:14 +10001521 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1522 crypto_hash_digest(&desc, &sg, bodysize, cookie->signature))
1523 goto free_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 }
1525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 return retval;
Herbert Xu1b489e12006-08-20 15:07:14 +10001527
1528free_cookie:
1529 kfree(retval);
1530nodata:
1531 *cookie_len = 0;
1532 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
1534
1535/* Unpack the cookie from COOKIE ECHO chunk, recreating the association. */
1536struct sctp_association *sctp_unpack_cookie(
1537 const struct sctp_endpoint *ep,
1538 const struct sctp_association *asoc,
Al Virodd0fc662005-10-07 07:46:04 +01001539 struct sctp_chunk *chunk, gfp_t gfp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 int *error, struct sctp_chunk **errp)
1541{
1542 struct sctp_association *retval = NULL;
1543 struct sctp_signed_cookie *cookie;
1544 struct sctp_cookie *bear_cookie;
1545 int headersize, bodysize, fixed_size;
Vlad Yasevich313e7b42006-01-17 11:55:57 -08001546 __u8 *digest = ep->digest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 struct scatterlist sg;
1548 unsigned int keylen, len;
1549 char *key;
1550 sctp_scope_t scope;
1551 struct sk_buff *skb = chunk->skb;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001552 struct timeval tv;
Herbert Xu1b489e12006-08-20 15:07:14 +10001553 struct hash_desc desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Vlad Yasevich9834a2b2006-01-17 11:52:12 -08001555 /* Header size is static data prior to the actual cookie, including
1556 * any padding.
1557 */
1558 headersize = sizeof(sctp_chunkhdr_t) +
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001559 (sizeof(struct sctp_signed_cookie) -
Vlad Yasevich9834a2b2006-01-17 11:52:12 -08001560 sizeof(struct sctp_cookie));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
1562 fixed_size = headersize + sizeof(struct sctp_cookie);
1563
1564 /* Verify that the chunk looks like it even has a cookie.
1565 * There must be enough room for our cookie and our peer's
1566 * INIT chunk.
1567 */
1568 len = ntohs(chunk->chunk_hdr->length);
1569 if (len < fixed_size + sizeof(struct sctp_chunkhdr))
1570 goto malformed;
1571
1572 /* Verify that the cookie has been padded out. */
1573 if (bodysize % SCTP_COOKIE_MULTIPLE)
1574 goto malformed;
1575
1576 /* Process the cookie. */
1577 cookie = chunk->subh.cookie_hdr;
1578 bear_cookie = &cookie->c;
1579
1580 if (!sctp_sk(ep->base.sk)->hmac)
1581 goto no_hmac;
1582
1583 /* Check the signature. */
1584 keylen = SCTP_SECRET_SIZE;
Herbert Xu68e3f5d2007-10-27 00:52:07 -07001585 sg_init_one(&sg, bear_cookie, bodysize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 key = (char *)ep->secret_key[ep->current_key];
Herbert Xu1b489e12006-08-20 15:07:14 +10001587 desc.tfm = sctp_sk(ep->base.sk)->hmac;
1588 desc.flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Al Viro8ca84482006-06-20 03:26:14 -07001590 memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
Herbert Xu1b489e12006-08-20 15:07:14 +10001591 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1592 crypto_hash_digest(&desc, &sg, bodysize, digest)) {
1593 *error = -SCTP_IERROR_NOMEM;
1594 goto fail;
1595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1598 /* Try the previous key. */
1599 key = (char *)ep->secret_key[ep->last_key];
Al Viro8ca84482006-06-20 03:26:14 -07001600 memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
Herbert Xu1b489e12006-08-20 15:07:14 +10001601 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1602 crypto_hash_digest(&desc, &sg, bodysize, digest)) {
1603 *error = -SCTP_IERROR_NOMEM;
1604 goto fail;
1605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1608 /* Yikes! Still bad signature! */
1609 *error = -SCTP_IERROR_BAD_SIG;
1610 goto fail;
1611 }
1612 }
1613
1614no_hmac:
1615 /* IG Section 2.35.2:
1616 * 3) Compare the port numbers and the verification tag contained
1617 * within the COOKIE ECHO chunk to the actual port numbers and the
1618 * verification tag within the SCTP common header of the received
1619 * packet. If these values do not match the packet MUST be silently
1620 * discarded,
1621 */
1622 if (ntohl(chunk->sctp_hdr->vtag) != bear_cookie->my_vtag) {
1623 *error = -SCTP_IERROR_BAD_TAG;
1624 goto fail;
1625 }
1626
Al Viro9b1dfad2006-11-20 17:09:17 -08001627 if (chunk->sctp_hdr->source != bear_cookie->peer_addr.v4.sin_port ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 ntohs(chunk->sctp_hdr->dest) != bear_cookie->my_port) {
1629 *error = -SCTP_IERROR_BAD_PORTS;
1630 goto fail;
1631 }
1632
1633 /* Check to see if the cookie is stale. If there is already
1634 * an association, there is no need to check cookie's expiration
1635 * for init collision case of lost COOKIE ACK.
Vlad Yasevichf236218b2006-09-29 17:10:03 -07001636 * If skb has been timestamped, then use the stamp, otherwise
1637 * use current time. This introduces a small possibility that
1638 * that a cookie may be considered expired, but his would only slow
1639 * down the new association establishment instead of every packet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 */
Vlad Yasevichf236218b2006-09-29 17:10:03 -07001641 if (sock_flag(ep->base.sk, SOCK_TIMESTAMP))
1642 skb_get_timestamp(skb, &tv);
1643 else
1644 do_gettimeofday(&tv);
1645
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001646 if (!asoc && tv_lt(bear_cookie->expiration, tv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 /*
1648 * Section 3.3.10.3 Stale Cookie Error (3)
1649 *
1650 * Cause of error
1651 * ---------------
1652 * Stale Cookie Error: Indicates the receipt of a valid State
1653 * Cookie that has expired.
1654 */
1655 len = ntohs(chunk->chunk_hdr->length);
1656 *errp = sctp_make_op_error_space(asoc, chunk, len);
1657 if (*errp) {
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001658 suseconds_t usecs = (tv.tv_sec -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 bear_cookie->expiration.tv_sec) * 1000000L +
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001660 tv.tv_usec - bear_cookie->expiration.tv_usec;
Al Viro34bcca22006-11-20 17:27:15 -08001661 __be32 n = htonl(usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001664 sizeof(n));
1665 sctp_addto_chunk(*errp, sizeof(n), &n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 *error = -SCTP_IERROR_STALE_COOKIE;
1667 } else
1668 *error = -SCTP_IERROR_NOMEM;
1669
1670 goto fail;
1671 }
1672
1673 /* Make a new base association. */
1674 scope = sctp_scope(sctp_source(chunk));
1675 retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
1676 if (!retval) {
1677 *error = -SCTP_IERROR_NOMEM;
1678 goto fail;
1679 }
1680
1681 /* Set up our peer's port number. */
1682 retval->peer.port = ntohs(chunk->sctp_hdr->source);
1683
1684 /* Populate the association from the cookie. */
1685 memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
1686
1687 if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
1688 GFP_ATOMIC) < 0) {
1689 *error = -SCTP_IERROR_NOMEM;
1690 goto fail;
1691 }
1692
1693 /* Also, add the destination address. */
1694 if (list_empty(&retval->base.bind_addr.address_list)) {
Vlad Yasevichf57d96b2007-12-20 14:12:24 -08001695 sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest,
1696 SCTP_ADDR_SRC, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 }
1698
1699 retval->next_tsn = retval->c.initial_tsn;
1700 retval->ctsn_ack_point = retval->next_tsn - 1;
1701 retval->addip_serial = retval->c.initial_tsn;
1702 retval->adv_peer_ack_point = retval->ctsn_ack_point;
1703 retval->peer.prsctp_capable = retval->c.prsctp_capable;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001704 retval->peer.adaptation_ind = retval->c.adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 /* The INIT stuff will be done by the side effects. */
1707 return retval;
1708
1709fail:
1710 if (retval)
1711 sctp_association_free(retval);
1712
1713 return NULL;
1714
1715malformed:
1716 /* Yikes! The packet is either corrupt or deliberately
1717 * malformed.
1718 */
1719 *error = -SCTP_IERROR_MALFORMED;
1720 goto fail;
1721}
1722
1723/********************************************************************
1724 * 3rd Level Abstractions
1725 ********************************************************************/
1726
1727struct __sctp_missing {
Al Viro9f81bcd2006-11-20 17:26:34 -08001728 __be32 num_missing;
1729 __be16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730} __attribute__((packed));
1731
1732/*
1733 * Report a missing mandatory parameter.
1734 */
1735static int sctp_process_missing_param(const struct sctp_association *asoc,
1736 sctp_param_t paramtype,
1737 struct sctp_chunk *chunk,
1738 struct sctp_chunk **errp)
1739{
1740 struct __sctp_missing report;
1741 __u16 len;
1742
1743 len = WORD_ROUND(sizeof(report));
1744
1745 /* Make an ERROR chunk, preparing enough room for
1746 * returning multiple unknown parameters.
1747 */
1748 if (!*errp)
1749 *errp = sctp_make_op_error_space(asoc, chunk, len);
1750
1751 if (*errp) {
1752 report.num_missing = htonl(1);
1753 report.type = paramtype;
Vlad Yasevichebdfcad2007-01-15 19:12:31 -08001754 sctp_init_cause(*errp, SCTP_ERROR_MISS_PARAM,
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001755 sizeof(report));
1756 sctp_addto_chunk(*errp, sizeof(report), &report);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 }
1758
1759 /* Stop processing this chunk. */
1760 return 0;
1761}
1762
1763/* Report an Invalid Mandatory Parameter. */
1764static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
1765 struct sctp_chunk *chunk,
1766 struct sctp_chunk **errp)
1767{
1768 /* Invalid Mandatory Parameter Error has no payload. */
1769
1770 if (!*errp)
1771 *errp = sctp_make_op_error_space(asoc, chunk, 0);
1772
1773 if (*errp)
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001774 sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
1776 /* Stop processing this chunk. */
1777 return 0;
1778}
1779
1780static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
1781 struct sctp_paramhdr *param,
1782 const struct sctp_chunk *chunk,
1783 struct sctp_chunk **errp)
1784{
1785 char error[] = "The following parameter had invalid length:";
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001786 size_t payload_len = WORD_ROUND(sizeof(error)) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 sizeof(sctp_paramhdr_t);
1788
1789
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001790 /* This is a fatal error. Any accumulated non-fatal errors are
1791 * not reported.
1792 */
1793 if (*errp)
1794 sctp_chunk_free(*errp);
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 /* Create an error chunk and fill it in with our payload. */
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001797 *errp = sctp_make_op_error_space(asoc, chunk, payload_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
1799 if (*errp) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001800 sctp_init_cause(*errp, SCTP_ERROR_PROTO_VIOLATION,
1801 sizeof(error) + sizeof(sctp_paramhdr_t));
1802 sctp_addto_chunk(*errp, sizeof(error), error);
1803 sctp_addto_param(*errp, sizeof(sctp_paramhdr_t), param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 }
1805
1806 return 0;
1807}
1808
1809
1810/* Do not attempt to handle the HOST_NAME parm. However, do
1811 * send back an indicator to the peer.
1812 */
1813static int sctp_process_hn_param(const struct sctp_association *asoc,
1814 union sctp_params param,
1815 struct sctp_chunk *chunk,
1816 struct sctp_chunk **errp)
1817{
1818 __u16 len = ntohs(param.p->length);
1819
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001820 /* Processing of the HOST_NAME parameter will generate an
1821 * ABORT. If we've accumulated any non-fatal errors, they
1822 * would be unrecognized parameters and we should not include
1823 * them in the ABORT.
1824 */
1825 if (*errp)
1826 sctp_chunk_free(*errp);
1827
1828 *errp = sctp_make_op_error_space(asoc, chunk, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001830 if (*errp) {
1831 sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED, len);
1832 sctp_addto_chunk(*errp, len, param.v);
1833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835 /* Stop processing this chunk. */
1836 return 0;
1837}
1838
Vlad Yasevich131a47e2007-09-16 15:53:56 -07001839static void sctp_process_ext_param(struct sctp_association *asoc,
1840 union sctp_params param)
1841{
1842 __u16 num_ext = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
1843 int i;
1844
1845 for (i = 0; i < num_ext; i++) {
1846 switch (param.ext->chunks[i]) {
1847 case SCTP_CID_FWD_TSN:
1848 if (sctp_prsctp_enable &&
1849 !asoc->peer.prsctp_capable)
1850 asoc->peer.prsctp_capable = 1;
1851 break;
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07001852 case SCTP_CID_AUTH:
1853 /* if the peer reports AUTH, assume that he
1854 * supports AUTH.
1855 */
1856 asoc->peer.auth_capable = 1;
1857 break;
Vlad Yasevich131a47e2007-09-16 15:53:56 -07001858 case SCTP_CID_ASCONF:
1859 case SCTP_CID_ASCONF_ACK:
Vlad Yasevich88799fe2007-10-24 17:24:23 -04001860 asoc->peer.asconf_capable = 1;
Vlad Yasevich6b2f9cb2007-09-16 19:35:39 -07001861 break;
Vlad Yasevich131a47e2007-09-16 15:53:56 -07001862 default:
1863 break;
1864 }
1865 }
1866}
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868/* RFC 3.2.1 & the Implementers Guide 2.2.
1869 *
1870 * The Parameter Types are encoded such that the
1871 * highest-order two bits specify the action that must be
1872 * taken if the processing endpoint does not recognize the
1873 * Parameter Type.
1874 *
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001875 * 00 - Stop processing this parameter; do not process any further
1876 * parameters within this chunk
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 *
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001878 * 01 - Stop processing this parameter, do not process any further
1879 * parameters within this chunk, and report the unrecognized
1880 * parameter in an 'Unrecognized Parameter' ERROR chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 *
1882 * 10 - Skip this parameter and continue processing.
1883 *
1884 * 11 - Skip this parameter and continue processing but
1885 * report the unrecognized parameter in an
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001886 * 'Unrecognized Parameter' ERROR chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 *
1888 * Return value:
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001889 * SCTP_IERROR_NO_ERROR - continue with the chunk
1890 * SCTP_IERROR_ERROR - stop and report an error.
1891 * SCTP_IERROR_NOMEME - out of memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 */
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001893static sctp_ierror_t sctp_process_unk_param(const struct sctp_association *asoc,
1894 union sctp_params param,
1895 struct sctp_chunk *chunk,
1896 struct sctp_chunk **errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001898 int retval = SCTP_IERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900 switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
1901 case SCTP_PARAM_ACTION_DISCARD:
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001902 retval = SCTP_IERROR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 break;
1904 case SCTP_PARAM_ACTION_SKIP:
1905 break;
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001906 case SCTP_PARAM_ACTION_DISCARD_ERR:
1907 retval = SCTP_IERROR_ERROR;
1908 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 case SCTP_PARAM_ACTION_SKIP_ERR:
1910 /* Make an ERROR chunk, preparing enough room for
1911 * returning multiple unknown parameters.
1912 */
1913 if (NULL == *errp)
1914 *errp = sctp_make_op_error_space(asoc, chunk,
1915 ntohs(chunk->chunk_hdr->length));
1916
1917 if (*errp) {
1918 sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 WORD_ROUND(ntohs(param.p->length)));
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001920 sctp_addto_chunk(*errp,
1921 WORD_ROUND(ntohs(param.p->length)),
1922 param.v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 } else {
1924 /* If there is no memory for generating the ERROR
1925 * report as specified, an ABORT will be triggered
1926 * to the peer and the association won't be
1927 * established.
1928 */
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001929 retval = SCTP_IERROR_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 break;
1932 default:
1933 break;
1934 }
1935
1936 return retval;
1937}
1938
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001939/* Verify variable length parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 * Return values:
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001941 * SCTP_IERROR_ABORT - trigger an ABORT
1942 * SCTP_IERROR_NOMEM - out of memory (abort)
1943 * SCTP_IERROR_ERROR - stop processing, trigger an ERROR
1944 * SCTP_IERROR_NO_ERROR - continue with the chunk
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 */
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001946static sctp_ierror_t sctp_verify_param(const struct sctp_association *asoc,
1947 union sctp_params param,
1948 sctp_cid_t cid,
1949 struct sctp_chunk *chunk,
1950 struct sctp_chunk **err_chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001952 int retval = SCTP_IERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
1954 /* FIXME - This routine is not looking at each parameter per the
1955 * chunk type, i.e., unrecognized parameters should be further
1956 * identified based on the chunk id.
1957 */
1958
1959 switch (param.p->type) {
1960 case SCTP_PARAM_IPV4_ADDRESS:
1961 case SCTP_PARAM_IPV6_ADDRESS:
1962 case SCTP_PARAM_COOKIE_PRESERVATIVE:
1963 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
1964 case SCTP_PARAM_STATE_COOKIE:
1965 case SCTP_PARAM_HEARTBEAT_INFO:
1966 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
1967 case SCTP_PARAM_ECN_CAPABLE:
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001968 case SCTP_PARAM_ADAPTATION_LAYER_IND:
Vlad Yasevich131a47e2007-09-16 15:53:56 -07001969 case SCTP_PARAM_SUPPORTED_EXT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 break;
1971
Vlad Yasevichd6de3092007-12-20 14:10:00 -08001972 case SCTP_PARAM_SET_PRIMARY:
1973 if (sctp_addip_enable)
1974 break;
1975 goto fallthrough;
1976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 case SCTP_PARAM_HOST_NAME_ADDRESS:
1978 /* Tell the peer, we won't support this param. */
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001979 sctp_process_hn_param(asoc, param, chunk, err_chunk);
1980 retval = SCTP_IERROR_ABORT;
1981 break;
Vlad Yasevich131a47e2007-09-16 15:53:56 -07001982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 case SCTP_PARAM_FWD_TSN_SUPPORT:
1984 if (sctp_prsctp_enable)
1985 break;
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07001986 goto fallthrough;
1987
1988 case SCTP_PARAM_RANDOM:
1989 if (!sctp_auth_enable)
1990 goto fallthrough;
1991
1992 /* SCTP-AUTH: Secion 6.1
1993 * If the random number is not 32 byte long the association
1994 * MUST be aborted. The ABORT chunk SHOULD contain the error
1995 * cause 'Protocol Violation'.
1996 */
1997 if (SCTP_AUTH_RANDOM_LENGTH !=
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001998 ntohs(param.p->length) - sizeof(sctp_paramhdr_t)) {
1999 sctp_process_inv_paramlength(asoc, param.p,
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002000 chunk, err_chunk);
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002001 retval = SCTP_IERROR_ABORT;
2002 }
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002003 break;
2004
2005 case SCTP_PARAM_CHUNKS:
2006 if (!sctp_auth_enable)
2007 goto fallthrough;
2008
2009 /* SCTP-AUTH: Section 3.2
2010 * The CHUNKS parameter MUST be included once in the INIT or
2011 * INIT-ACK chunk if the sender wants to receive authenticated
2012 * chunks. Its maximum length is 260 bytes.
2013 */
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002014 if (260 < ntohs(param.p->length)) {
2015 sctp_process_inv_paramlength(asoc, param.p,
2016 chunk, err_chunk);
2017 retval = SCTP_IERROR_ABORT;
2018 }
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002019 break;
2020
2021 case SCTP_PARAM_HMAC_ALGO:
2022 if (!sctp_auth_enable)
2023 break;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002024 /* Fall Through */
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002025fallthrough:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 default:
2027 SCTP_DEBUG_PRINTK("Unrecognized param: %d for chunk %d.\n",
2028 ntohs(param.p->type), cid);
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002029 retval = sctp_process_unk_param(asoc, param, chunk, err_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 break;
2031 }
2032 return retval;
2033}
2034
2035/* Verify the INIT packet before we process it. */
2036int sctp_verify_init(const struct sctp_association *asoc,
2037 sctp_cid_t cid,
2038 sctp_init_chunk_t *peer_init,
2039 struct sctp_chunk *chunk,
2040 struct sctp_chunk **errp)
2041{
2042 union sctp_params param;
2043 int has_cookie = 0;
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002044 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 /* Verify stream values are non-zero. */
2047 if ((0 == peer_init->init_hdr.num_outbound_streams) ||
Vlad Yasevichd023f622007-01-15 19:15:45 -08002048 (0 == peer_init->init_hdr.num_inbound_streams) ||
2049 (0 == peer_init->init_hdr.init_tag) ||
2050 (SCTP_DEFAULT_MINWINDOW > ntohl(peer_init->init_hdr.a_rwnd))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002052 return sctp_process_inv_mandatory(asoc, chunk, errp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
2054
2055 /* Check for missing mandatory parameters. */
2056 sctp_walk_params(param, peer_init, init_hdr.params) {
2057
2058 if (SCTP_PARAM_STATE_COOKIE == param.p->type)
2059 has_cookie = 1;
2060
2061 } /* for (loop through all parameters) */
2062
2063 /* There is a possibility that a parameter length was bad and
2064 * in that case we would have stoped walking the parameters.
2065 * The current param.p would point at the bad one.
2066 * Current consensus on the mailing list is to generate a PROTOCOL
2067 * VIOLATION error. We build the ERROR chunk here and let the normal
2068 * error handling code build and send the packet.
2069 */
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002070 if (param.v != (void*)chunk->chunk_end)
2071 return sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 /* The only missing mandatory param possible today is
2074 * the state cookie for an INIT-ACK chunk.
2075 */
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002076 if ((SCTP_CID_INIT_ACK == cid) && !has_cookie)
2077 return sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
2078 chunk, errp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002080 /* Verify all the variable length parameters */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 sctp_walk_params(param, peer_init, init_hdr.params) {
2082
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002083 result = sctp_verify_param(asoc, param, cid, chunk, errp);
2084 switch (result) {
2085 case SCTP_IERROR_ABORT:
2086 case SCTP_IERROR_NOMEM:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 return 0;
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002088 case SCTP_IERROR_ERROR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 return 1;
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002090 case SCTP_IERROR_NO_ERROR:
2091 default:
2092 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
2094
2095 } /* for (loop through all parameters) */
2096
2097 return 1;
2098}
2099
2100/* Unpack the parameters in an INIT packet into an association.
2101 * Returns 0 on failure, else success.
2102 * FIXME: This is an association method.
2103 */
2104int sctp_process_init(struct sctp_association *asoc, sctp_cid_t cid,
2105 const union sctp_addr *peer_addr,
Al Virodd0fc662005-10-07 07:46:04 +01002106 sctp_init_chunk_t *peer_init, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107{
2108 union sctp_params param;
2109 struct sctp_transport *transport;
2110 struct list_head *pos, *temp;
2111 char *cookie;
2112
2113 /* We must include the address that the INIT packet came from.
2114 * This is the only address that matters for an INIT packet.
2115 * When processing a COOKIE ECHO, we retrieve the from address
2116 * of the INIT from the cookie.
2117 */
2118
2119 /* This implementation defaults to making the first transport
2120 * added as the primary transport. The source address seems to
2121 * be a a better choice than any of the embedded addresses.
2122 */
Al Viro4bdf4b52006-11-20 17:10:20 -08002123 if (peer_addr) {
Al Viro6a1e5f32006-11-20 17:12:25 -08002124 if(!sctp_assoc_add_peer(asoc, peer_addr, gfp, SCTP_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 goto nomem;
Al Viro4bdf4b52006-11-20 17:10:20 -08002126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
2128 /* Process the initialization parameters. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 sctp_walk_params(param, peer_init, init_hdr.params) {
2130
2131 if (!sctp_process_param(asoc, param, peer_addr, gfp))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002132 goto clean_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
2134
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002135 /* AUTH: After processing the parameters, make sure that we
2136 * have all the required info to potentially do authentications.
2137 */
2138 if (asoc->peer.auth_capable && (!asoc->peer.peer_random ||
2139 !asoc->peer.peer_hmacs))
2140 asoc->peer.auth_capable = 0;
2141
Vlad Yasevich6b2f9cb2007-09-16 19:35:39 -07002142
2143 /* If the peer claims support for ADD-IP without support
2144 * for AUTH, disable support for ADD-IP.
Vlad Yasevich73d9c4f2007-10-24 17:24:26 -04002145 * Do this only if backward compatible mode is turned off.
Vlad Yasevich6b2f9cb2007-09-16 19:35:39 -07002146 */
Vlad Yasevich73d9c4f2007-10-24 17:24:26 -04002147 if (!sctp_addip_noauth &&
2148 (asoc->peer.asconf_capable && !asoc->peer.auth_capable)) {
Vlad Yasevich6b2f9cb2007-09-16 19:35:39 -07002149 asoc->peer.addip_disabled_mask |= (SCTP_PARAM_ADD_IP |
2150 SCTP_PARAM_DEL_IP |
2151 SCTP_PARAM_SET_PRIMARY);
Vlad Yasevich88799fe2007-10-24 17:24:23 -04002152 asoc->peer.asconf_capable = 0;
Vlad Yasevich6b2f9cb2007-09-16 19:35:39 -07002153 }
2154
Frank Filz3f7a87d2005-06-20 13:14:57 -07002155 /* Walk list of transports, removing transports in the UNKNOWN state. */
2156 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2157 transport = list_entry(pos, struct sctp_transport, transports);
2158 if (transport->state == SCTP_UNKNOWN) {
2159 sctp_assoc_rm_peer(asoc, transport);
2160 }
2161 }
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 /* The fixed INIT headers are always in network byte
2164 * order.
2165 */
2166 asoc->peer.i.init_tag =
2167 ntohl(peer_init->init_hdr.init_tag);
2168 asoc->peer.i.a_rwnd =
2169 ntohl(peer_init->init_hdr.a_rwnd);
2170 asoc->peer.i.num_outbound_streams =
2171 ntohs(peer_init->init_hdr.num_outbound_streams);
2172 asoc->peer.i.num_inbound_streams =
2173 ntohs(peer_init->init_hdr.num_inbound_streams);
2174 asoc->peer.i.initial_tsn =
2175 ntohl(peer_init->init_hdr.initial_tsn);
2176
2177 /* Apply the upper bounds for output streams based on peer's
2178 * number of inbound streams.
2179 */
2180 if (asoc->c.sinit_num_ostreams >
2181 ntohs(peer_init->init_hdr.num_inbound_streams)) {
2182 asoc->c.sinit_num_ostreams =
2183 ntohs(peer_init->init_hdr.num_inbound_streams);
2184 }
2185
2186 if (asoc->c.sinit_max_instreams >
2187 ntohs(peer_init->init_hdr.num_outbound_streams)) {
2188 asoc->c.sinit_max_instreams =
2189 ntohs(peer_init->init_hdr.num_outbound_streams);
2190 }
2191
2192 /* Copy Initiation tag from INIT to VT_peer in cookie. */
2193 asoc->c.peer_vtag = asoc->peer.i.init_tag;
2194
2195 /* Peer Rwnd : Current calculated value of the peer's rwnd. */
2196 asoc->peer.rwnd = asoc->peer.i.a_rwnd;
2197
2198 /* Copy cookie in case we need to resend COOKIE-ECHO. */
2199 cookie = asoc->peer.cookie;
2200 if (cookie) {
Arnaldo Carvalho de Meloaf997d82006-11-21 01:20:33 -02002201 asoc->peer.cookie = kmemdup(cookie, asoc->peer.cookie_len, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (!asoc->peer.cookie)
2203 goto clean_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 }
2205
2206 /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
2207 * high (for example, implementations MAY use the size of the receiver
2208 * advertised window).
2209 */
2210 list_for_each(pos, &asoc->peer.transport_addr_list) {
2211 transport = list_entry(pos, struct sctp_transport, transports);
2212 transport->ssthresh = asoc->peer.i.a_rwnd;
2213 }
2214
2215 /* Set up the TSN tracking pieces. */
2216 sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_SIZE,
2217 asoc->peer.i.initial_tsn);
2218
2219 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
2220 *
2221 * The stream sequence number in all the streams shall start
2222 * from 0 when the association is established. Also, when the
2223 * stream sequence number reaches the value 65535 the next
2224 * stream sequence number shall be set to 0.
2225 */
2226
Frank Filz3f7a87d2005-06-20 13:14:57 -07002227 /* Allocate storage for the negotiated streams if it is not a temporary
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002228 * association.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 */
2230 if (!asoc->temp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 int error;
2232
2233 asoc->ssnmap = sctp_ssnmap_new(asoc->c.sinit_max_instreams,
2234 asoc->c.sinit_num_ostreams, gfp);
2235 if (!asoc->ssnmap)
2236 goto clean_up;
2237
Vlad Yasevich07d93962007-05-04 13:55:27 -07002238 error = sctp_assoc_set_id(asoc, gfp);
2239 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 goto clean_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 }
2242
2243 /* ADDIP Section 4.1 ASCONF Chunk Procedures
2244 *
2245 * When an endpoint has an ASCONF signaled change to be sent to the
2246 * remote endpoint it should do the following:
2247 * ...
2248 * A2) A serial number should be assigned to the Chunk. The serial
2249 * number should be a monotonically increasing number. All serial
2250 * numbers are defined to be initialized at the start of the
2251 * association to the same value as the Initial TSN.
2252 */
2253 asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
2254 return 1;
2255
2256clean_up:
2257 /* Release the transport structures. */
2258 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2259 transport = list_entry(pos, struct sctp_transport, transports);
2260 list_del_init(pos);
2261 sctp_transport_free(transport);
2262 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07002263
2264 asoc->peer.transport_count = 0;
2265
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266nomem:
2267 return 0;
2268}
2269
2270
2271/* Update asoc with the option described in param.
2272 *
2273 * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
2274 *
2275 * asoc is the association to update.
2276 * param is the variable length parameter to use for update.
2277 * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
2278 * If the current packet is an INIT we want to minimize the amount of
2279 * work we do. In particular, we should not build transport
2280 * structures for the addresses.
2281 */
2282static int sctp_process_param(struct sctp_association *asoc,
2283 union sctp_params param,
2284 const union sctp_addr *peer_addr,
Al Virodd0fc662005-10-07 07:46:04 +01002285 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
2287 union sctp_addr addr;
2288 int i;
2289 __u16 sat;
2290 int retval = 1;
2291 sctp_scope_t scope;
2292 time_t stale;
2293 struct sctp_af *af;
Vlad Yasevichd6de3092007-12-20 14:10:00 -08002294 union sctp_addr_param *addr_param;
2295 struct sctp_transport *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
2297 /* We maintain all INIT parameters in network byte order all the
2298 * time. This allows us to not worry about whether the parameters
2299 * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
2300 */
2301 switch (param.p->type) {
2302 case SCTP_PARAM_IPV6_ADDRESS:
2303 if (PF_INET6 != asoc->base.sk->sk_family)
2304 break;
2305 /* Fall through. */
2306 case SCTP_PARAM_IPV4_ADDRESS:
2307 af = sctp_get_af_specific(param_type2af(param.p->type));
Al Virodd86d132006-11-20 17:11:13 -08002308 af->from_addr_param(&addr, param.addr, htons(asoc->peer.port), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 scope = sctp_scope(peer_addr);
Al Virodd86d132006-11-20 17:11:13 -08002310 if (sctp_in_scope(&addr, scope))
2311 if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_UNCONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 return 0;
2313 break;
2314
2315 case SCTP_PARAM_COOKIE_PRESERVATIVE:
2316 if (!sctp_cookie_preserve_enable)
2317 break;
2318
2319 stale = ntohl(param.life->lifespan_increment);
2320
2321 /* Suggested Cookie Life span increment's unit is msec,
2322 * (1/1000sec).
2323 */
2324 asoc->cookie_life.tv_sec += stale / 1000;
2325 asoc->cookie_life.tv_usec += (stale % 1000) * 1000;
2326 break;
2327
2328 case SCTP_PARAM_HOST_NAME_ADDRESS:
2329 SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
2330 break;
2331
2332 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2333 /* Turn off the default values first so we'll know which
2334 * ones are really set by the peer.
2335 */
2336 asoc->peer.ipv4_address = 0;
2337 asoc->peer.ipv6_address = 0;
2338
2339 /* Cycle through address types; avoid divide by 0. */
2340 sat = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2341 if (sat)
2342 sat /= sizeof(__u16);
2343
2344 for (i = 0; i < sat; ++i) {
2345 switch (param.sat->types[i]) {
2346 case SCTP_PARAM_IPV4_ADDRESS:
2347 asoc->peer.ipv4_address = 1;
2348 break;
2349
2350 case SCTP_PARAM_IPV6_ADDRESS:
2351 asoc->peer.ipv6_address = 1;
2352 break;
2353
2354 case SCTP_PARAM_HOST_NAME_ADDRESS:
2355 asoc->peer.hostname_address = 1;
2356 break;
2357
2358 default: /* Just ignore anything else. */
2359 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 }
2362 break;
2363
2364 case SCTP_PARAM_STATE_COOKIE:
2365 asoc->peer.cookie_len =
2366 ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2367 asoc->peer.cookie = param.cookie->body;
2368 break;
2369
2370 case SCTP_PARAM_HEARTBEAT_INFO:
2371 /* Would be odd to receive, but it causes no problems. */
2372 break;
2373
2374 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2375 /* Rejected during verify stage. */
2376 break;
2377
2378 case SCTP_PARAM_ECN_CAPABLE:
2379 asoc->peer.ecn_capable = 1;
2380 break;
2381
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002382 case SCTP_PARAM_ADAPTATION_LAYER_IND:
2383 asoc->peer.adaptation_ind = param.aind->adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 break;
2385
Vlad Yasevichd6de3092007-12-20 14:10:00 -08002386 case SCTP_PARAM_SET_PRIMARY:
2387 addr_param = param.v + sizeof(sctp_addip_param_t);
2388
2389 af = sctp_get_af_specific(param_type2af(param.p->type));
2390 af->from_addr_param(&addr, addr_param,
2391 htons(asoc->peer.port), 0);
2392
2393 /* if the address is invalid, we can't process it.
2394 * XXX: see spec for what to do.
2395 */
2396 if (!af->addr_valid(&addr, NULL, NULL))
2397 break;
2398
2399 t = sctp_assoc_lookup_paddr(asoc, &addr);
2400 if (!t)
2401 break;
2402
2403 sctp_assoc_set_primary(asoc, t);
2404 break;
2405
Vlad Yasevich131a47e2007-09-16 15:53:56 -07002406 case SCTP_PARAM_SUPPORTED_EXT:
2407 sctp_process_ext_param(asoc, param);
2408 break;
2409
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 case SCTP_PARAM_FWD_TSN_SUPPORT:
2411 if (sctp_prsctp_enable) {
2412 asoc->peer.prsctp_capable = 1;
2413 break;
2414 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002415 /* Fall Through */
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002416 goto fall_through;
2417
2418 case SCTP_PARAM_RANDOM:
2419 if (!sctp_auth_enable)
2420 goto fall_through;
2421
2422 /* Save peer's random parameter */
2423 asoc->peer.peer_random = kmemdup(param.p,
2424 ntohs(param.p->length), gfp);
2425 if (!asoc->peer.peer_random) {
2426 retval = 0;
2427 break;
2428 }
2429 break;
2430
2431 case SCTP_PARAM_HMAC_ALGO:
2432 if (!sctp_auth_enable)
2433 goto fall_through;
2434
2435 /* Save peer's HMAC list */
2436 asoc->peer.peer_hmacs = kmemdup(param.p,
2437 ntohs(param.p->length), gfp);
2438 if (!asoc->peer.peer_hmacs) {
2439 retval = 0;
2440 break;
2441 }
2442
2443 /* Set the default HMAC the peer requested*/
2444 sctp_auth_asoc_set_default_hmac(asoc, param.hmac_algo);
2445 break;
2446
2447 case SCTP_PARAM_CHUNKS:
2448 if (!sctp_auth_enable)
2449 goto fall_through;
2450
2451 asoc->peer.peer_chunks = kmemdup(param.p,
2452 ntohs(param.p->length), gfp);
2453 if (!asoc->peer.peer_chunks)
2454 retval = 0;
2455 break;
2456fall_through:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 default:
2458 /* Any unrecognized parameters should have been caught
2459 * and handled by sctp_verify_param() which should be
2460 * called prior to this routine. Simply log the error
2461 * here.
2462 */
2463 SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
2464 ntohs(param.p->type), asoc);
2465 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
2468 return retval;
2469}
2470
2471/* Select a new verification tag. */
2472__u32 sctp_generate_tag(const struct sctp_endpoint *ep)
2473{
2474 /* I believe that this random number generator complies with RFC1750.
2475 * A tag of 0 is reserved for special cases (e.g. INIT).
2476 */
2477 __u32 x;
2478
2479 do {
2480 get_random_bytes(&x, sizeof(__u32));
2481 } while (x == 0);
2482
2483 return x;
2484}
2485
2486/* Select an initial TSN to send during startup. */
2487__u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
2488{
2489 __u32 retval;
2490
2491 get_random_bytes(&retval, sizeof(__u32));
2492 return retval;
2493}
2494
2495/*
2496 * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
2497 * 0 1 2 3
2498 * 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
2499 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2500 * | Type = 0xC1 | Chunk Flags | Chunk Length |
2501 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2502 * | Serial Number |
2503 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2504 * | Address Parameter |
2505 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2506 * | ASCONF Parameter #1 |
2507 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2508 * \ \
2509 * / .... /
2510 * \ \
2511 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2512 * | ASCONF Parameter #N |
2513 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2514 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002515 * Address Parameter and other parameter will not be wrapped in this function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 */
2517static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
2518 union sctp_addr *addr,
2519 int vparam_len)
2520{
2521 sctp_addiphdr_t asconf;
2522 struct sctp_chunk *retval;
2523 int length = sizeof(asconf) + vparam_len;
2524 union sctp_addr_param addrparam;
2525 int addrlen;
2526 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2527
2528 addrlen = af->to_addr_param(addr, &addrparam);
2529 if (!addrlen)
2530 return NULL;
2531 length += addrlen;
2532
2533 /* Create the chunk. */
2534 retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF, 0, length);
2535 if (!retval)
2536 return NULL;
2537
2538 asconf.serial = htonl(asoc->addip_serial++);
2539
2540 retval->subh.addip_hdr =
2541 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2542 retval->param_hdr.v =
2543 sctp_addto_chunk(retval, addrlen, &addrparam);
2544
2545 return retval;
2546}
2547
2548/* ADDIP
2549 * 3.2.1 Add IP Address
2550 * 0 1 2 3
2551 * 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
2552 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2553 * | Type = 0xC001 | Length = Variable |
2554 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2555 * | ASCONF-Request Correlation ID |
2556 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2557 * | Address Parameter |
2558 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2559 *
2560 * 3.2.2 Delete IP Address
2561 * 0 1 2 3
2562 * 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
2563 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2564 * | Type = 0xC002 | Length = Variable |
2565 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2566 * | ASCONF-Request Correlation ID |
2567 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2568 * | Address Parameter |
2569 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2570 *
2571 */
2572struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
2573 union sctp_addr *laddr,
2574 struct sockaddr *addrs,
2575 int addrcnt,
Al Virodbc16db2006-11-20 17:01:42 -08002576 __be16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577{
2578 sctp_addip_param_t param;
2579 struct sctp_chunk *retval;
2580 union sctp_addr_param addr_param;
2581 union sctp_addr *addr;
2582 void *addr_buf;
2583 struct sctp_af *af;
2584 int paramlen = sizeof(param);
2585 int addr_param_len = 0;
2586 int totallen = 0;
2587 int i;
2588
2589 /* Get total length of all the address parameters. */
2590 addr_buf = addrs;
2591 for (i = 0; i < addrcnt; i++) {
2592 addr = (union sctp_addr *)addr_buf;
2593 af = sctp_get_af_specific(addr->v4.sin_family);
2594 addr_param_len = af->to_addr_param(addr, &addr_param);
2595
2596 totallen += paramlen;
2597 totallen += addr_param_len;
2598
2599 addr_buf += af->sockaddr_len;
2600 }
2601
2602 /* Create an asconf chunk with the required length. */
2603 retval = sctp_make_asconf(asoc, laddr, totallen);
2604 if (!retval)
2605 return NULL;
2606
2607 /* Add the address parameters to the asconf chunk. */
2608 addr_buf = addrs;
2609 for (i = 0; i < addrcnt; i++) {
2610 addr = (union sctp_addr *)addr_buf;
2611 af = sctp_get_af_specific(addr->v4.sin_family);
2612 addr_param_len = af->to_addr_param(addr, &addr_param);
2613 param.param_hdr.type = flags;
2614 param.param_hdr.length = htons(paramlen + addr_param_len);
2615 param.crr_id = i;
2616
2617 sctp_addto_chunk(retval, paramlen, &param);
2618 sctp_addto_chunk(retval, addr_param_len, &addr_param);
2619
2620 addr_buf += af->sockaddr_len;
2621 }
2622 return retval;
2623}
2624
2625/* ADDIP
2626 * 3.2.4 Set Primary IP Address
2627 * 0 1 2 3
2628 * 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
2629 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2630 * | Type =0xC004 | Length = Variable |
2631 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2632 * | ASCONF-Request Correlation ID |
2633 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2634 * | Address Parameter |
2635 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2636 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002637 * Create an ASCONF chunk with Set Primary IP address parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 */
2639struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
2640 union sctp_addr *addr)
2641{
2642 sctp_addip_param_t param;
2643 struct sctp_chunk *retval;
2644 int len = sizeof(param);
2645 union sctp_addr_param addrparam;
2646 int addrlen;
2647 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2648
2649 addrlen = af->to_addr_param(addr, &addrparam);
2650 if (!addrlen)
2651 return NULL;
2652 len += addrlen;
2653
2654 /* Create the chunk and make asconf header. */
2655 retval = sctp_make_asconf(asoc, addr, len);
2656 if (!retval)
2657 return NULL;
2658
2659 param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
2660 param.param_hdr.length = htons(len);
2661 param.crr_id = 0;
2662
2663 sctp_addto_chunk(retval, sizeof(param), &param);
2664 sctp_addto_chunk(retval, addrlen, &addrparam);
2665
2666 return retval;
2667}
2668
2669/* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2670 * 0 1 2 3
2671 * 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
2672 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2673 * | Type = 0x80 | Chunk Flags | Chunk Length |
2674 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2675 * | Serial Number |
2676 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2677 * | ASCONF Parameter Response#1 |
2678 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2679 * \ \
2680 * / .... /
2681 * \ \
2682 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2683 * | ASCONF Parameter Response#N |
2684 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2685 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002686 * Create an ASCONF_ACK chunk with enough space for the parameter responses.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 */
2688static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
2689 __u32 serial, int vparam_len)
2690{
2691 sctp_addiphdr_t asconf;
2692 struct sctp_chunk *retval;
2693 int length = sizeof(asconf) + vparam_len;
2694
2695 /* Create the chunk. */
2696 retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF_ACK, 0, length);
2697 if (!retval)
2698 return NULL;
2699
2700 asconf.serial = htonl(serial);
2701
2702 retval->subh.addip_hdr =
2703 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2704
2705 return retval;
2706}
2707
2708/* Add response parameters to an ASCONF_ACK chunk. */
Al Viro9f81bcd2006-11-20 17:26:34 -08002709static void sctp_add_asconf_response(struct sctp_chunk *chunk, __be32 crr_id,
Al Virodbc16db2006-11-20 17:01:42 -08002710 __be16 err_code, sctp_addip_param_t *asconf_param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711{
2712 sctp_addip_param_t ack_param;
2713 sctp_errhdr_t err_param;
2714 int asconf_param_len = 0;
2715 int err_param_len = 0;
Al Virodbc16db2006-11-20 17:01:42 -08002716 __be16 response_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
2718 if (SCTP_ERROR_NO_ERROR == err_code) {
2719 response_type = SCTP_PARAM_SUCCESS_REPORT;
2720 } else {
2721 response_type = SCTP_PARAM_ERR_CAUSE;
2722 err_param_len = sizeof(err_param);
2723 if (asconf_param)
2724 asconf_param_len =
2725 ntohs(asconf_param->param_hdr.length);
2726 }
2727
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002728 /* Add Success Indication or Error Cause Indication parameter. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 ack_param.param_hdr.type = response_type;
2730 ack_param.param_hdr.length = htons(sizeof(ack_param) +
2731 err_param_len +
2732 asconf_param_len);
2733 ack_param.crr_id = crr_id;
2734 sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
2735
2736 if (SCTP_ERROR_NO_ERROR == err_code)
2737 return;
2738
2739 /* Add Error Cause parameter. */
2740 err_param.cause = err_code;
2741 err_param.length = htons(err_param_len + asconf_param_len);
2742 sctp_addto_chunk(chunk, err_param_len, &err_param);
2743
2744 /* Add the failed TLV copied from ASCONF chunk. */
2745 if (asconf_param)
2746 sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
2747}
2748
2749/* Process a asconf parameter. */
Al Virodbc16db2006-11-20 17:01:42 -08002750static __be16 sctp_process_asconf_param(struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 struct sctp_chunk *asconf,
2752 sctp_addip_param_t *asconf_param)
2753{
2754 struct sctp_transport *peer;
2755 struct sctp_af *af;
2756 union sctp_addr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 union sctp_addr_param *addr_param;
Al Viro5f242a12006-11-20 17:05:23 -08002758
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 addr_param = (union sctp_addr_param *)
2760 ((void *)asconf_param + sizeof(sctp_addip_param_t));
2761
2762 af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2763 if (unlikely(!af))
2764 return SCTP_ERROR_INV_PARAM;
2765
Al Virodd86d132006-11-20 17:11:13 -08002766 af->from_addr_param(&addr, addr_param, htons(asoc->peer.port), 0);
Vlad Yasevich42e30bf2007-12-20 14:08:56 -08002767
2768 /* ADDIP 4.2.1 This parameter MUST NOT contain a broadcast
2769 * or multicast address.
2770 * (note: wildcard is permitted and requires special handling so
2771 * make sure we check for that)
2772 */
2773 if (!af->is_any(&addr) && !af->addr_valid(&addr, NULL, asconf->skb))
2774 return SCTP_ERROR_INV_PARAM;
2775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 switch (asconf_param->param_hdr.type) {
2777 case SCTP_PARAM_ADD_IP:
Vlad Yasevich42e30bf2007-12-20 14:08:56 -08002778 /* Section 4.2.1:
2779 * If the address 0.0.0.0 or ::0 is provided, the source
2780 * address of the packet MUST be added.
2781 */
2782 if (af->is_any(&addr))
2783 memcpy(&addr, &asconf->source, sizeof(addr));
2784
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002786 * request and does not have the local resources to add this
2787 * new address to the association, it MUST return an Error
2788 * Cause TLV set to the new error code 'Operation Refused
2789 * Due to Resource Shortage'.
2790 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
Al Virodd86d132006-11-20 17:11:13 -08002792 peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC, SCTP_UNCONFIRMED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 if (!peer)
2794 return SCTP_ERROR_RSRC_LOW;
2795
2796 /* Start the heartbeat timer. */
2797 if (!mod_timer(&peer->hb_timer, sctp_transport_timeout(peer)))
2798 sctp_transport_hold(peer);
2799 break;
2800 case SCTP_PARAM_DEL_IP:
2801 /* ADDIP 4.3 D7) If a request is received to delete the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002802 * last remaining IP address of a peer endpoint, the receiver
2803 * MUST send an Error Cause TLV with the error cause set to the
2804 * new error code 'Request to Delete Last Remaining IP Address'.
2805 */
Vlad Yasevich42e30bf2007-12-20 14:08:56 -08002806 if (asoc->peer.transport_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 return SCTP_ERROR_DEL_LAST_IP;
2808
2809 /* ADDIP 4.3 D8) If a request is received to delete an IP
2810 * address which is also the source address of the IP packet
2811 * which contained the ASCONF chunk, the receiver MUST reject
2812 * this request. To reject the request the receiver MUST send
2813 * an Error Cause TLV set to the new error code 'Request to
2814 * Delete Source IP Address'
2815 */
Al Viro6a1e5f32006-11-20 17:12:25 -08002816 if (sctp_cmp_addr_exact(sctp_source(asconf), &addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 return SCTP_ERROR_DEL_SRC_IP;
2818
Vlad Yasevich42e30bf2007-12-20 14:08:56 -08002819 /* Section 4.2.2
2820 * If the address 0.0.0.0 or ::0 is provided, all
2821 * addresses of the peer except the source address of the
2822 * packet MUST be deleted.
2823 */
2824 if (af->is_any(&addr)) {
2825 sctp_assoc_set_primary(asoc, asconf->transport);
2826 sctp_assoc_del_nonprimary_peers(asoc,
2827 asconf->transport);
2828 } else
2829 sctp_assoc_del_peer(asoc, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 break;
2831 case SCTP_PARAM_SET_PRIMARY:
Vlad Yasevich42e30bf2007-12-20 14:08:56 -08002832 /* ADDIP Section 4.2.4
2833 * If the address 0.0.0.0 or ::0 is provided, the receiver
2834 * MAY mark the source address of the packet as its
2835 * primary.
2836 */
2837 if (af->is_any(&addr))
2838 memcpy(&addr.v4, sctp_source(asconf), sizeof(addr));
2839
Al Virodd86d132006-11-20 17:11:13 -08002840 peer = sctp_assoc_lookup_paddr(asoc, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 if (!peer)
2842 return SCTP_ERROR_INV_PARAM;
2843
2844 sctp_assoc_set_primary(asoc, peer);
2845 break;
2846 default:
2847 return SCTP_ERROR_INV_PARAM;
2848 break;
2849 }
2850
2851 return SCTP_ERROR_NO_ERROR;
2852}
2853
Wei Yongjun6f4c6182007-09-19 17:19:52 +08002854/* Verify the ASCONF packet before we process it. */
2855int sctp_verify_asconf(const struct sctp_association *asoc,
2856 struct sctp_paramhdr *param_hdr, void *chunk_end,
2857 struct sctp_paramhdr **errp) {
2858 sctp_addip_param_t *asconf_param;
2859 union sctp_params param;
2860 int length, plen;
2861
2862 param.v = (sctp_paramhdr_t *) param_hdr;
2863 while (param.v <= chunk_end - sizeof(sctp_paramhdr_t)) {
2864 length = ntohs(param.p->length);
2865 *errp = param.p;
2866
2867 if (param.v > chunk_end - length ||
2868 length < sizeof(sctp_paramhdr_t))
2869 return 0;
2870
2871 switch (param.p->type) {
2872 case SCTP_PARAM_ADD_IP:
2873 case SCTP_PARAM_DEL_IP:
2874 case SCTP_PARAM_SET_PRIMARY:
2875 asconf_param = (sctp_addip_param_t *)param.v;
2876 plen = ntohs(asconf_param->param_hdr.length);
2877 if (plen < sizeof(sctp_addip_param_t) +
2878 sizeof(sctp_paramhdr_t))
2879 return 0;
2880 break;
2881 case SCTP_PARAM_SUCCESS_REPORT:
2882 case SCTP_PARAM_ADAPTATION_LAYER_IND:
2883 if (length != sizeof(sctp_addip_param_t))
2884 return 0;
2885
2886 break;
2887 default:
2888 break;
2889 }
2890
2891 param.v += WORD_ROUND(length);
2892 }
2893
2894 if (param.v != chunk_end)
2895 return 0;
2896
2897 return 1;
2898}
2899
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002900/* Process an incoming ASCONF chunk with the next expected serial no. and
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 * return an ASCONF_ACK chunk to be sent in response.
2902 */
2903struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
2904 struct sctp_chunk *asconf)
2905{
2906 sctp_addiphdr_t *hdr;
2907 union sctp_addr_param *addr_param;
2908 sctp_addip_param_t *asconf_param;
2909 struct sctp_chunk *asconf_ack;
2910
Al Virodbc16db2006-11-20 17:01:42 -08002911 __be16 err_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 int length = 0;
Wei Yongjunf3830cc2007-10-15 11:51:03 +09002913 int chunk_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 __u32 serial;
2915 int all_param_pass = 1;
2916
Wei Yongjunf3830cc2007-10-15 11:51:03 +09002917 chunk_len = ntohs(asconf->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 hdr = (sctp_addiphdr_t *)asconf->skb->data;
2919 serial = ntohl(hdr->serial);
2920
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002921 /* Skip the addiphdr and store a pointer to address parameter. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 length = sizeof(sctp_addiphdr_t);
2923 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
2924 chunk_len -= length;
2925
2926 /* Skip the address parameter and store a pointer to the first
Joe Perches7aa1b542007-12-20 14:03:52 -08002927 * asconf parameter.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002928 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 length = ntohs(addr_param->v4.param_hdr.length);
2930 asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
2931 chunk_len -= length;
2932
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002933 /* create an ASCONF_ACK chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 * Based on the definitions of parameters, we know that the size of
2935 * ASCONF_ACK parameters are less than or equal to the twice of ASCONF
Joe Perches7aa1b542007-12-20 14:03:52 -08002936 * parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 */
2938 asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 2);
2939 if (!asconf_ack)
2940 goto done;
2941
2942 /* Process the TLVs contained within the ASCONF chunk. */
2943 while (chunk_len > 0) {
2944 err_code = sctp_process_asconf_param(asoc, asconf,
2945 asconf_param);
2946 /* ADDIP 4.1 A7)
2947 * If an error response is received for a TLV parameter,
2948 * all TLVs with no response before the failed TLV are
2949 * considered successful if not reported. All TLVs after
2950 * the failed response are considered unsuccessful unless
2951 * a specific success indication is present for the parameter.
2952 */
2953 if (SCTP_ERROR_NO_ERROR != err_code)
2954 all_param_pass = 0;
2955
2956 if (!all_param_pass)
2957 sctp_add_asconf_response(asconf_ack,
2958 asconf_param->crr_id, err_code,
2959 asconf_param);
2960
2961 /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
2962 * an IP address sends an 'Out of Resource' in its response, it
2963 * MUST also fail any subsequent add or delete requests bundled
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002964 * in the ASCONF.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 */
2966 if (SCTP_ERROR_RSRC_LOW == err_code)
2967 goto done;
2968
2969 /* Move to the next ASCONF param. */
2970 length = ntohs(asconf_param->param_hdr.length);
2971 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
2972 length);
2973 chunk_len -= length;
2974 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002975
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976done:
2977 asoc->peer.addip_serial++;
2978
2979 /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002980 * after freeing the reference to old asconf ack if any.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 */
2982 if (asconf_ack) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 sctp_chunk_hold(asconf_ack);
Vlad Yasevicha08de642007-12-20 14:11:47 -08002984 list_add_tail(&asconf_ack->transmitted_list,
2985 &asoc->asconf_ack_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 }
2987
2988 return asconf_ack;
2989}
2990
2991/* Process a asconf parameter that is successfully acked. */
2992static int sctp_asconf_param_success(struct sctp_association *asoc,
2993 sctp_addip_param_t *asconf_param)
2994{
2995 struct sctp_af *af;
2996 union sctp_addr addr;
2997 struct sctp_bind_addr *bp = &asoc->base.bind_addr;
2998 union sctp_addr_param *addr_param;
2999 struct list_head *pos;
3000 struct sctp_transport *transport;
Sridhar Samudraladc022a92006-07-21 14:49:25 -07003001 struct sctp_sockaddr_entry *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 int retval = 0;
3003
3004 addr_param = (union sctp_addr_param *)
3005 ((void *)asconf_param + sizeof(sctp_addip_param_t));
3006
3007 /* We have checked the packet before, so we do not check again. */
3008 af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
Al Virodd86d132006-11-20 17:11:13 -08003009 af->from_addr_param(&addr, addr_param, htons(bp->port), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
3011 switch (asconf_param->param_hdr.type) {
3012 case SCTP_PARAM_ADD_IP:
Vlad Yasevich559cf712007-09-16 16:03:28 -07003013 /* This is always done in BH context with a socket lock
3014 * held, so the list can not change.
3015 */
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -04003016 local_bh_disable();
Vlad Yasevich559cf712007-09-16 16:03:28 -07003017 list_for_each_entry(saddr, &bp->address_list, list) {
Al Virodd86d132006-11-20 17:11:13 -08003018 if (sctp_cmp_addr_exact(&saddr->a, &addr))
Vlad Yasevichf57d96b2007-12-20 14:12:24 -08003019 saddr->state = SCTP_ADDR_SRC;
Sridhar Samudraladc022a92006-07-21 14:49:25 -07003020 }
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -04003021 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 break;
3023 case SCTP_PARAM_DEL_IP:
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -04003024 local_bh_disable();
3025 retval = sctp_del_bind_addr(bp, &addr);
3026 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 list_for_each(pos, &asoc->peer.transport_addr_list) {
3028 transport = list_entry(pos, struct sctp_transport,
3029 transports);
Sridhar Samudraladc022a92006-07-21 14:49:25 -07003030 dst_release(transport->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 sctp_transport_route(transport, NULL,
3032 sctp_sk(asoc->base.sk));
3033 }
3034 break;
3035 default:
3036 break;
3037 }
3038
3039 return retval;
3040}
3041
3042/* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
3043 * for the given asconf parameter. If there is no response for this parameter,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003044 * return the error code based on the third argument 'no_err'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 * ADDIP 4.1
3046 * A7) If an error response is received for a TLV parameter, all TLVs with no
3047 * response before the failed TLV are considered successful if not reported.
3048 * All TLVs after the failed response are considered unsuccessful unless a
3049 * specific success indication is present for the parameter.
3050 */
Al Virodbc16db2006-11-20 17:01:42 -08003051static __be16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 sctp_addip_param_t *asconf_param,
3053 int no_err)
3054{
3055 sctp_addip_param_t *asconf_ack_param;
3056 sctp_errhdr_t *err_param;
3057 int length;
Wei Yongjunf3830cc2007-10-15 11:51:03 +09003058 int asconf_ack_len;
Al Virodbc16db2006-11-20 17:01:42 -08003059 __be16 err_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
3061 if (no_err)
3062 err_code = SCTP_ERROR_NO_ERROR;
3063 else
3064 err_code = SCTP_ERROR_REQ_REFUSED;
3065
Wei Yongjunf3830cc2007-10-15 11:51:03 +09003066 asconf_ack_len = ntohs(asconf_ack->chunk_hdr->length) -
3067 sizeof(sctp_chunkhdr_t);
3068
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
3070 * the first asconf_ack parameter.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003071 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 length = sizeof(sctp_addiphdr_t);
3073 asconf_ack_param = (sctp_addip_param_t *)(asconf_ack->skb->data +
3074 length);
3075 asconf_ack_len -= length;
3076
3077 while (asconf_ack_len > 0) {
3078 if (asconf_ack_param->crr_id == asconf_param->crr_id) {
3079 switch(asconf_ack_param->param_hdr.type) {
3080 case SCTP_PARAM_SUCCESS_REPORT:
3081 return SCTP_ERROR_NO_ERROR;
3082 case SCTP_PARAM_ERR_CAUSE:
3083 length = sizeof(sctp_addip_param_t);
3084 err_param = (sctp_errhdr_t *)
3085 ((void *)asconf_ack_param + length);
3086 asconf_ack_len -= length;
3087 if (asconf_ack_len > 0)
3088 return err_param->cause;
3089 else
3090 return SCTP_ERROR_INV_PARAM;
3091 break;
3092 default:
3093 return SCTP_ERROR_INV_PARAM;
3094 }
3095 }
3096
3097 length = ntohs(asconf_ack_param->param_hdr.length);
3098 asconf_ack_param = (sctp_addip_param_t *)
3099 ((void *)asconf_ack_param + length);
3100 asconf_ack_len -= length;
3101 }
3102
3103 return err_code;
3104}
3105
3106/* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
3107int sctp_process_asconf_ack(struct sctp_association *asoc,
3108 struct sctp_chunk *asconf_ack)
3109{
3110 struct sctp_chunk *asconf = asoc->addip_last_asconf;
3111 union sctp_addr_param *addr_param;
3112 sctp_addip_param_t *asconf_param;
3113 int length = 0;
3114 int asconf_len = asconf->skb->len;
3115 int all_param_pass = 0;
3116 int no_err = 1;
3117 int retval = 0;
Al Virodbc16db2006-11-20 17:01:42 -08003118 __be16 err_code = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
3120 /* Skip the chunkhdr and addiphdr from the last asconf sent and store
3121 * a pointer to address parameter.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003122 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 length = sizeof(sctp_addip_chunk_t);
3124 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
3125 asconf_len -= length;
3126
3127 /* Skip the address parameter in the last asconf sent and store a
Joe Perches7aa1b542007-12-20 14:03:52 -08003128 * pointer to the first asconf parameter.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003129 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 length = ntohs(addr_param->v4.param_hdr.length);
3131 asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
3132 asconf_len -= length;
3133
3134 /* ADDIP 4.1
3135 * A8) If there is no response(s) to specific TLV parameter(s), and no
3136 * failures are indicated, then all request(s) are considered
3137 * successful.
3138 */
3139 if (asconf_ack->skb->len == sizeof(sctp_addiphdr_t))
3140 all_param_pass = 1;
3141
3142 /* Process the TLVs contained in the last sent ASCONF chunk. */
3143 while (asconf_len > 0) {
3144 if (all_param_pass)
3145 err_code = SCTP_ERROR_NO_ERROR;
3146 else {
3147 err_code = sctp_get_asconf_response(asconf_ack,
3148 asconf_param,
3149 no_err);
3150 if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
3151 no_err = 0;
3152 }
3153
3154 switch (err_code) {
3155 case SCTP_ERROR_NO_ERROR:
3156 retval = sctp_asconf_param_success(asoc, asconf_param);
3157 break;
3158
3159 case SCTP_ERROR_RSRC_LOW:
3160 retval = 1;
3161 break;
3162
3163 case SCTP_ERROR_INV_PARAM:
3164 /* Disable sending this type of asconf parameter in
3165 * future.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003166 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 asoc->peer.addip_disabled_mask |=
3168 asconf_param->param_hdr.type;
3169 break;
3170
3171 case SCTP_ERROR_REQ_REFUSED:
3172 case SCTP_ERROR_DEL_LAST_IP:
3173 case SCTP_ERROR_DEL_SRC_IP:
3174 default:
3175 break;
3176 }
3177
3178 /* Skip the processed asconf parameter and move to the next
3179 * one.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003180 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 length = ntohs(asconf_param->param_hdr.length);
3182 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
3183 length);
3184 asconf_len -= length;
3185 }
3186
3187 /* Free the cached last sent asconf chunk. */
3188 sctp_chunk_free(asconf);
3189 asoc->addip_last_asconf = NULL;
3190
3191 /* Send the next asconf chunk from the addip chunk queue. */
David S. Miller79af02c2005-07-08 21:47:49 -07003192 if (!list_empty(&asoc->addip_chunk_list)) {
3193 struct list_head *entry = asoc->addip_chunk_list.next;
3194 asconf = list_entry(entry, struct sctp_chunk, list);
3195
3196 list_del_init(entry);
3197
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 /* Hold the chunk until an ASCONF_ACK is received. */
3199 sctp_chunk_hold(asconf);
3200 if (sctp_primitive_ASCONF(asoc, asconf))
3201 sctp_chunk_free(asconf);
3202 else
3203 asoc->addip_last_asconf = asconf;
3204 }
3205
3206 return retval;
3207}
3208
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003209/* Make a FWD TSN chunk. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
3211 __u32 new_cum_tsn, size_t nstreams,
3212 struct sctp_fwdtsn_skip *skiplist)
3213{
3214 struct sctp_chunk *retval = NULL;
3215 struct sctp_fwdtsn_chunk *ftsn_chunk;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003216 struct sctp_fwdtsn_hdr ftsn_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 struct sctp_fwdtsn_skip skip;
3218 size_t hint;
3219 int i;
3220
3221 hint = (nstreams + 1) * sizeof(__u32);
3222
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 retval = sctp_make_chunk(asoc, SCTP_CID_FWD_TSN, 0, hint);
3224
3225 if (!retval)
3226 return NULL;
3227
3228 ftsn_chunk = (struct sctp_fwdtsn_chunk *)retval->subh.fwdtsn_hdr;
3229
3230 ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
3231 retval->subh.fwdtsn_hdr =
3232 sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
3233
3234 for (i = 0; i < nstreams; i++) {
3235 skip.stream = skiplist[i].stream;
3236 skip.ssn = skiplist[i].ssn;
3237 sctp_addto_chunk(retval, sizeof(skip), &skip);
3238 }
3239
3240 return retval;
3241}