blob: 71cc204a9ea527a401f472ee65abc28b22760dd3 [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>
59#include <asm/scatterlist.h>
60#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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/* What was the inbound interface for this chunk? */
82int sctp_chunk_iif(const struct sctp_chunk *chunk)
83{
84 struct sctp_af *af;
85 int iif = 0;
86
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070087 af = sctp_get_af_specific(ipver2af(ip_hdr(chunk->skb)->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (af)
89 iif = af->skb_iif(chunk->skb);
90
91 return iif;
92}
93
94/* RFC 2960 3.3.2 Initiation (INIT) (1)
95 *
96 * Note 2: The ECN capable field is reserved for future use of
97 * Explicit Congestion Notification.
98 */
99static const struct sctp_paramhdr ecap_param = {
100 SCTP_PARAM_ECN_CAPABLE,
101 __constant_htons(sizeof(struct sctp_paramhdr)),
102};
103static const struct sctp_paramhdr prsctp_param = {
104 SCTP_PARAM_FWD_TSN_SUPPORT,
105 __constant_htons(sizeof(struct sctp_paramhdr)),
106};
107
108/* A helper to initialize to initialize an op error inside a
109 * provided chunk, as most cause codes will be embedded inside an
110 * abort chunk.
111 */
Al Viro5bf2db02006-11-20 16:59:45 -0800112void sctp_init_cause(struct sctp_chunk *chunk, __be16 cause_code,
Wei Yongjun00f1c2d2007-08-21 15:50:01 +0800113 size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 sctp_errhdr_t err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 __u16 len;
117
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900118 /* Cause code constants are now defined in network order. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 err.cause = cause_code;
120 len = sizeof(sctp_errhdr_t) + paylen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 err.length = htons(len);
Vlad Yasevich4a1c0102007-01-09 14:35:51 -0800122 chunk->subh.err_hdr = sctp_addto_chunk(chunk, sizeof(sctp_errhdr_t), &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
125/* 3.3.2 Initiation (INIT) (1)
126 *
127 * This chunk is used to initiate a SCTP association between two
128 * endpoints. The format of the INIT chunk is shown below:
129 *
130 * 0 1 2 3
131 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
132 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
133 * | Type = 1 | Chunk Flags | Chunk Length |
134 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
135 * | Initiate Tag |
136 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
137 * | Advertised Receiver Window Credit (a_rwnd) |
138 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
139 * | Number of Outbound Streams | Number of Inbound Streams |
140 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
141 * | Initial TSN |
142 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
143 * \ \
144 * / Optional/Variable-Length Parameters /
145 * \ \
146 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
147 *
148 *
149 * The INIT chunk contains the following parameters. Unless otherwise
150 * noted, each parameter MUST only be included once in the INIT chunk.
151 *
152 * Fixed Parameters Status
153 * ----------------------------------------------
154 * Initiate Tag Mandatory
155 * Advertised Receiver Window Credit Mandatory
156 * Number of Outbound Streams Mandatory
157 * Number of Inbound Streams Mandatory
158 * Initial TSN Mandatory
159 *
160 * Variable Parameters Status Type Value
161 * -------------------------------------------------------------
162 * IPv4 Address (Note 1) Optional 5
163 * IPv6 Address (Note 1) Optional 6
164 * Cookie Preservative Optional 9
165 * Reserved for ECN Capable (Note 2) Optional 32768 (0x8000)
166 * Host Name Address (Note 3) Optional 11
167 * Supported Address Types (Note 4) Optional 12
168 */
169struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
170 const struct sctp_bind_addr *bp,
Al Virodd0fc662005-10-07 07:46:04 +0100171 gfp_t gfp, int vparam_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 sctp_inithdr_t init;
174 union sctp_params addrs;
175 size_t chunksize;
176 struct sctp_chunk *retval = NULL;
177 int num_types, addrs_len = 0;
178 struct sctp_sock *sp;
179 sctp_supported_addrs_param_t sat;
Al Viro3dbe8652006-11-20 17:25:49 -0800180 __be16 types[2];
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800181 sctp_adaptation_ind_param_t aiparam;
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700182 sctp_supported_ext_param_t ext_param;
183 int num_ext = 0;
184 __u8 extensions[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 /* RFC 2960 3.3.2 Initiation (INIT) (1)
187 *
188 * Note 1: The INIT chunks can contain multiple addresses that
189 * can be IPv4 and/or IPv6 in any combination.
190 */
191 retval = NULL;
192
193 /* Convert the provided bind address list to raw format. */
194 addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, gfp);
195
196 init.init_tag = htonl(asoc->c.my_vtag);
197 init.a_rwnd = htonl(asoc->rwnd);
198 init.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
199 init.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
200 init.initial_tsn = htonl(asoc->c.initial_tsn);
201
202 /* How many address types are needed? */
203 sp = sctp_sk(asoc->base.sk);
204 num_types = sp->pf->supported_addrs(sp, types);
205
206 chunksize = sizeof(init) + addrs_len + SCTP_SAT_LEN(num_types);
207 chunksize += sizeof(ecap_param);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700208 if (sctp_prsctp_enable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 chunksize += sizeof(prsctp_param);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700210 extensions[num_ext] = SCTP_CID_FWD_TSN;
211 num_ext += 1;
212 }
213 /* ADDIP: Section 4.2.7:
214 * An implementation supporting this extension [ADDIP] MUST list
215 * the ASCONF,the ASCONF-ACK, and the AUTH chunks in its INIT and
216 * INIT-ACK parameters.
217 * XXX: We don't support AUTH just yet, so don't list it. AUTH
218 * support should add it.
219 */
220 if (sctp_addip_enable) {
221 extensions[num_ext] = SCTP_CID_ASCONF;
222 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
223 num_ext += 2;
224 }
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 chunksize += sizeof(aiparam);
227 chunksize += vparam_len;
228
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700229 /* If we have any extensions to report, account for that */
230 if (num_ext)
231 chunksize += sizeof(sctp_supported_ext_param_t) + num_ext;
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 /* RFC 2960 3.3.2 Initiation (INIT) (1)
234 *
235 * Note 3: An INIT chunk MUST NOT contain more than one Host
236 * Name address parameter. Moreover, the sender of the INIT
237 * MUST NOT combine any other address types with the Host Name
238 * address in the INIT. The receiver of INIT MUST ignore any
239 * other address types if the Host Name address parameter is
240 * present in the received INIT chunk.
241 *
242 * PLEASE DO NOT FIXME [This version does not support Host Name.]
243 */
244
245 retval = sctp_make_chunk(asoc, SCTP_CID_INIT, 0, chunksize);
246 if (!retval)
247 goto nodata;
248
249 retval->subh.init_hdr =
250 sctp_addto_chunk(retval, sizeof(init), &init);
251 retval->param_hdr.v =
252 sctp_addto_chunk(retval, addrs_len, addrs.v);
253
254 /* RFC 2960 3.3.2 Initiation (INIT) (1)
255 *
256 * Note 4: This parameter, when present, specifies all the
257 * address types the sending endpoint can support. The absence
258 * of this parameter indicates that the sending endpoint can
259 * support any address type.
260 */
261 sat.param_hdr.type = SCTP_PARAM_SUPPORTED_ADDRESS_TYPES;
262 sat.param_hdr.length = htons(SCTP_SAT_LEN(num_types));
263 sctp_addto_chunk(retval, sizeof(sat), &sat);
264 sctp_addto_chunk(retval, num_types * sizeof(__u16), &types);
265
266 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700267
268 /* Add the supported extensions paramter. Be nice and add this
269 * fist before addiding the parameters for the extensions themselves
270 */
271 if (num_ext) {
272 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
273 ext_param.param_hdr.length =
274 htons(sizeof(sctp_supported_ext_param_t) + num_ext);
275 sctp_addto_chunk(retval, sizeof(sctp_supported_ext_param_t),
276 &ext_param);
277 sctp_addto_chunk(retval, num_ext, extensions);
278 }
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (sctp_prsctp_enable)
281 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700282
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800283 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 aiparam.param_hdr.length = htons(sizeof(aiparam));
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800285 aiparam.adaptation_ind = htonl(sp->adaptation_ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288nodata:
Jesper Juhla51482b2005-11-08 09:41:34 -0800289 kfree(addrs.v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return retval;
291}
292
293struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
294 const struct sctp_chunk *chunk,
Al Virodd0fc662005-10-07 07:46:04 +0100295 gfp_t gfp, int unkparam_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 sctp_inithdr_t initack;
298 struct sctp_chunk *retval;
299 union sctp_params addrs;
300 int addrs_len;
301 sctp_cookie_param_t *cookie;
302 int cookie_len;
303 size_t chunksize;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800304 sctp_adaptation_ind_param_t aiparam;
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700305 sctp_supported_ext_param_t ext_param;
306 int num_ext = 0;
307 __u8 extensions[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 retval = NULL;
310
311 /* Note: there may be no addresses to embed. */
312 addrs = sctp_bind_addrs_to_raw(&asoc->base.bind_addr, &addrs_len, gfp);
313
314 initack.init_tag = htonl(asoc->c.my_vtag);
315 initack.a_rwnd = htonl(asoc->rwnd);
316 initack.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
317 initack.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
318 initack.initial_tsn = htonl(asoc->c.initial_tsn);
319
320 /* FIXME: We really ought to build the cookie right
321 * into the packet instead of allocating more fresh memory.
322 */
323 cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
324 addrs.v, addrs_len);
325 if (!cookie)
326 goto nomem_cookie;
327
328 /* Calculate the total size of allocation, include the reserved
329 * space for reporting unknown parameters if it is specified.
330 */
331 chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;
332
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900333 /* Tell peer that we'll do ECN only if peer advertised such cap. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (asoc->peer.ecn_capable)
335 chunksize += sizeof(ecap_param);
336
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900337 /* Tell peer that we'll do PR-SCTP only if peer advertised. */
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700338 if (asoc->peer.prsctp_capable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 chunksize += sizeof(prsctp_param);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700340 extensions[num_ext] = SCTP_CID_FWD_TSN;
341 num_ext += 1;
342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700344 if (sctp_addip_enable) {
345 extensions[num_ext] = SCTP_CID_ASCONF;
346 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
347 num_ext += 2;
348 }
349
350 chunksize += sizeof(ext_param) + num_ext;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 chunksize += sizeof(aiparam);
352
353 /* Now allocate and fill out the chunk. */
354 retval = sctp_make_chunk(asoc, SCTP_CID_INIT_ACK, 0, chunksize);
355 if (!retval)
356 goto nomem_chunk;
357
358 /* Per the advice in RFC 2960 6.4, send this reply to
359 * the source of the INIT packet.
360 */
361 retval->transport = chunk->transport;
362 retval->subh.init_hdr =
363 sctp_addto_chunk(retval, sizeof(initack), &initack);
364 retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
365 sctp_addto_chunk(retval, cookie_len, cookie);
366 if (asoc->peer.ecn_capable)
367 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700368 if (num_ext) {
369 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
370 ext_param.param_hdr.length =
371 htons(sizeof(sctp_supported_ext_param_t) + num_ext);
372 sctp_addto_chunk(retval, sizeof(sctp_supported_ext_param_t),
373 &ext_param);
374 sctp_addto_chunk(retval, num_ext, extensions);
375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (asoc->peer.prsctp_capable)
377 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
378
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800379 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 aiparam.param_hdr.length = htons(sizeof(aiparam));
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800381 aiparam.adaptation_ind = htonl(sctp_sk(asoc->base.sk)->adaptation_ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
383
384 /* We need to remove the const qualifier at this point. */
385 retval->asoc = (struct sctp_association *) asoc;
386
387 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
388 *
389 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
390 * HEARTBEAT ACK, * etc.) to the same destination transport
391 * address from which it received the DATA or control chunk
392 * to which it is replying.
393 *
394 * [INIT ACK back to where the INIT came from.]
395 */
396 if (chunk)
397 retval->transport = chunk->transport;
398
399nomem_chunk:
400 kfree(cookie);
401nomem_cookie:
Jesper Juhla51482b2005-11-08 09:41:34 -0800402 kfree(addrs.v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return retval;
404}
405
406/* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
407 *
408 * This chunk is used only during the initialization of an association.
409 * It is sent by the initiator of an association to its peer to complete
410 * the initialization process. This chunk MUST precede any DATA chunk
411 * sent within the association, but MAY be bundled with one or more DATA
412 * chunks in the same packet.
413 *
414 * 0 1 2 3
415 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
416 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
417 * | Type = 10 |Chunk Flags | Length |
418 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
419 * / Cookie /
420 * \ \
421 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
422 *
423 * Chunk Flags: 8 bit
424 *
425 * Set to zero on transmit and ignored on receipt.
426 *
427 * Length: 16 bits (unsigned integer)
428 *
429 * Set to the size of the chunk in bytes, including the 4 bytes of
430 * the chunk header and the size of the Cookie.
431 *
432 * Cookie: variable size
433 *
434 * This field must contain the exact cookie received in the
435 * State Cookie parameter from the previous INIT ACK.
436 *
437 * An implementation SHOULD make the cookie as small as possible
438 * to insure interoperability.
439 */
440struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
441 const struct sctp_chunk *chunk)
442{
443 struct sctp_chunk *retval;
444 void *cookie;
445 int cookie_len;
446
447 cookie = asoc->peer.cookie;
448 cookie_len = asoc->peer.cookie_len;
449
450 /* Build a cookie echo chunk. */
451 retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ECHO, 0, cookie_len);
452 if (!retval)
453 goto nodata;
454 retval->subh.cookie_hdr =
455 sctp_addto_chunk(retval, cookie_len, cookie);
456
457 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
458 *
459 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
460 * HEARTBEAT ACK, * etc.) to the same destination transport
461 * address from which it * received the DATA or control chunk
462 * to which it is replying.
463 *
464 * [COOKIE ECHO back to where the INIT ACK came from.]
465 */
466 if (chunk)
467 retval->transport = chunk->transport;
468
469nodata:
470 return retval;
471}
472
473/* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
474 *
475 * This chunk is used only during the initialization of an
476 * association. It is used to acknowledge the receipt of a COOKIE
477 * ECHO chunk. This chunk MUST precede any DATA or SACK chunk sent
478 * within the association, but MAY be bundled with one or more DATA
479 * chunks or SACK chunk in the same SCTP packet.
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 = 11 |Chunk Flags | Length = 4 |
485 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
486 *
487 * Chunk Flags: 8 bits
488 *
489 * Set to zero on transmit and ignored on receipt.
490 */
491struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
492 const struct sctp_chunk *chunk)
493{
494 struct sctp_chunk *retval;
495
496 retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ACK, 0, 0);
497
498 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
499 *
500 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
501 * HEARTBEAT ACK, * etc.) to the same destination transport
502 * address from which it * received the DATA or control chunk
503 * to which it is replying.
504 *
505 * [COOKIE ACK back to where the COOKIE ECHO came from.]
506 */
507 if (retval && chunk)
508 retval->transport = chunk->transport;
509
510 return retval;
511}
512
513/*
514 * Appendix A: Explicit Congestion Notification:
515 * CWR:
516 *
517 * RFC 2481 details a specific bit for a sender to send in the header of
518 * its next outbound TCP segment to indicate to its peer that it has
519 * reduced its congestion window. This is termed the CWR bit. For
520 * SCTP the same indication is made by including the CWR chunk.
521 * This chunk contains one data element, i.e. the TSN number that
522 * was sent in the ECNE chunk. This element represents the lowest
523 * TSN number in the datagram that was originally marked with the
524 * CE bit.
525 *
526 * 0 1 2 3
527 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
528 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
529 * | Chunk Type=13 | Flags=00000000| Chunk Length = 8 |
530 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
531 * | Lowest TSN Number |
532 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
533 *
534 * Note: The CWR is considered a Control chunk.
535 */
536struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
537 const __u32 lowest_tsn,
538 const struct sctp_chunk *chunk)
539{
540 struct sctp_chunk *retval;
541 sctp_cwrhdr_t cwr;
542
543 cwr.lowest_tsn = htonl(lowest_tsn);
544 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_CWR, 0,
545 sizeof(sctp_cwrhdr_t));
546
547 if (!retval)
548 goto nodata;
549
550 retval->subh.ecn_cwr_hdr =
551 sctp_addto_chunk(retval, sizeof(cwr), &cwr);
552
553 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
554 *
555 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
556 * HEARTBEAT ACK, * etc.) to the same destination transport
557 * address from which it * received the DATA or control chunk
558 * to which it is replying.
559 *
560 * [Report a reduced congestion window back to where the ECNE
561 * came from.]
562 */
563 if (chunk)
564 retval->transport = chunk->transport;
565
566nodata:
567 return retval;
568}
569
570/* Make an ECNE chunk. This is a congestion experienced report. */
571struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
572 const __u32 lowest_tsn)
573{
574 struct sctp_chunk *retval;
575 sctp_ecnehdr_t ecne;
576
577 ecne.lowest_tsn = htonl(lowest_tsn);
578 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_ECNE, 0,
579 sizeof(sctp_ecnehdr_t));
580 if (!retval)
581 goto nodata;
582 retval->subh.ecne_hdr =
583 sctp_addto_chunk(retval, sizeof(ecne), &ecne);
584
585nodata:
586 return retval;
587}
588
589/* Make a DATA chunk for the given association from the provided
590 * parameters. However, do not populate the data payload.
591 */
592struct sctp_chunk *sctp_make_datafrag_empty(struct sctp_association *asoc,
593 const struct sctp_sndrcvinfo *sinfo,
594 int data_len, __u8 flags, __u16 ssn)
595{
596 struct sctp_chunk *retval;
597 struct sctp_datahdr dp;
598 int chunk_len;
599
600 /* We assign the TSN as LATE as possible, not here when
601 * creating the chunk.
602 */
603 dp.tsn = 0;
604 dp.stream = htons(sinfo->sinfo_stream);
605 dp.ppid = sinfo->sinfo_ppid;
606
607 /* Set the flags for an unordered send. */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -0700608 if (sinfo->sinfo_flags & SCTP_UNORDERED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 flags |= SCTP_DATA_UNORDERED;
610 dp.ssn = 0;
611 } else
612 dp.ssn = htons(ssn);
613
614 chunk_len = sizeof(dp) + data_len;
615 retval = sctp_make_chunk(asoc, SCTP_CID_DATA, flags, chunk_len);
616 if (!retval)
617 goto nodata;
618
619 retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
620 memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
621
622nodata:
623 return retval;
624}
625
626/* Create a selective ackowledgement (SACK) for the given
627 * association. This reports on which TSN's we've seen to date,
628 * including duplicates and gaps.
629 */
630struct sctp_chunk *sctp_make_sack(const struct sctp_association *asoc)
631{
632 struct sctp_chunk *retval;
633 struct sctp_sackhdr sack;
634 int len;
635 __u32 ctsn;
636 __u16 num_gabs, num_dup_tsns;
637 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
638
639 ctsn = sctp_tsnmap_get_ctsn(map);
640 SCTP_DEBUG_PRINTK("sackCTSNAck sent: 0x%x.\n", ctsn);
641
642 /* How much room is needed in the chunk? */
643 num_gabs = sctp_tsnmap_num_gabs(map);
644 num_dup_tsns = sctp_tsnmap_num_dups(map);
645
646 /* Initialize the SACK header. */
647 sack.cum_tsn_ack = htonl(ctsn);
648 sack.a_rwnd = htonl(asoc->a_rwnd);
649 sack.num_gap_ack_blocks = htons(num_gabs);
650 sack.num_dup_tsns = htons(num_dup_tsns);
651
652 len = sizeof(sack)
653 + sizeof(struct sctp_gap_ack_block) * num_gabs
654 + sizeof(__u32) * num_dup_tsns;
655
656 /* Create the chunk. */
657 retval = sctp_make_chunk(asoc, SCTP_CID_SACK, 0, len);
658 if (!retval)
659 goto nodata;
660
661 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
662 *
663 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
664 * HEARTBEAT ACK, etc.) to the same destination transport
665 * address from which it received the DATA or control chunk to
666 * which it is replying. This rule should also be followed if
667 * the endpoint is bundling DATA chunks together with the
668 * reply chunk.
669 *
670 * However, when acknowledging multiple DATA chunks received
671 * in packets from different source addresses in a single
672 * SACK, the SACK chunk may be transmitted to one of the
673 * destination transport addresses from which the DATA or
674 * control chunks being acknowledged were received.
675 *
676 * [BUG: We do not implement the following paragraph.
677 * Perhaps we should remember the last transport we used for a
678 * SACK and avoid that (if possible) if we have seen any
679 * duplicates. --piggy]
680 *
681 * When a receiver of a duplicate DATA chunk sends a SACK to a
682 * multi- homed endpoint it MAY be beneficial to vary the
683 * destination address and not use the source address of the
684 * DATA chunk. The reason being that receiving a duplicate
685 * from a multi-homed endpoint might indicate that the return
686 * path (as specified in the source address of the DATA chunk)
687 * for the SACK is broken.
688 *
689 * [Send to the address from which we last received a DATA chunk.]
690 */
691 retval->transport = asoc->peer.last_data_from;
692
693 retval->subh.sack_hdr =
694 sctp_addto_chunk(retval, sizeof(sack), &sack);
695
696 /* Add the gap ack block information. */
697 if (num_gabs)
698 sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
699 sctp_tsnmap_get_gabs(map));
700
701 /* Add the duplicate TSN information. */
702 if (num_dup_tsns)
703 sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
704 sctp_tsnmap_get_dups(map));
705
706nodata:
707 return retval;
708}
709
710/* Make a SHUTDOWN chunk. */
711struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
712 const struct sctp_chunk *chunk)
713{
714 struct sctp_chunk *retval;
715 sctp_shutdownhdr_t shut;
716 __u32 ctsn;
717
718 ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
719 shut.cum_tsn_ack = htonl(ctsn);
720
721 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN, 0,
722 sizeof(sctp_shutdownhdr_t));
723 if (!retval)
724 goto nodata;
725
726 retval->subh.shutdown_hdr =
727 sctp_addto_chunk(retval, sizeof(shut), &shut);
728
729 if (chunk)
730 retval->transport = chunk->transport;
731nodata:
732 return retval;
733}
734
735struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
736 const struct sctp_chunk *chunk)
737{
738 struct sctp_chunk *retval;
739
740 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0);
741
742 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
743 *
744 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
745 * HEARTBEAT ACK, * etc.) to the same destination transport
746 * address from which it * received the DATA or control chunk
747 * to which it is replying.
748 *
749 * [ACK back to where the SHUTDOWN came from.]
750 */
751 if (retval && chunk)
752 retval->transport = chunk->transport;
753
754 return retval;
755}
756
757struct sctp_chunk *sctp_make_shutdown_complete(
758 const struct sctp_association *asoc,
759 const struct sctp_chunk *chunk)
760{
761 struct sctp_chunk *retval;
762 __u8 flags = 0;
763
Jerome Forissier047a2422005-04-28 11:58:43 -0700764 /* Set the T-bit if we have no association (vtag will be
765 * reflected)
766 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
768
769 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags, 0);
770
771 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
772 *
773 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
774 * HEARTBEAT ACK, * etc.) to the same destination transport
775 * address from which it * received the DATA or control chunk
776 * to which it is replying.
777 *
778 * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
779 * came from.]
780 */
781 if (retval && chunk)
782 retval->transport = chunk->transport;
783
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900784 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
787/* Create an ABORT. Note that we set the T bit if we have no
Jerome Forissier047a2422005-04-28 11:58:43 -0700788 * association, except when responding to an INIT (sctpimpguide 2.41).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 */
790struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
791 const struct sctp_chunk *chunk,
792 const size_t hint)
793{
794 struct sctp_chunk *retval;
795 __u8 flags = 0;
796
Jerome Forissier047a2422005-04-28 11:58:43 -0700797 /* Set the T-bit if we have no association and 'chunk' is not
798 * an INIT (vtag will be reflected).
799 */
800 if (!asoc) {
801 if (chunk && chunk->chunk_hdr &&
802 chunk->chunk_hdr->type == SCTP_CID_INIT)
803 flags = 0;
804 else
805 flags = SCTP_CHUNK_FLAG_T;
806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 retval = sctp_make_chunk(asoc, SCTP_CID_ABORT, flags, hint);
809
810 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
811 *
812 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
813 * HEARTBEAT ACK, * etc.) to the same destination transport
814 * address from which it * received the DATA or control chunk
815 * to which it is replying.
816 *
817 * [ABORT back to where the offender came from.]
818 */
819 if (retval && chunk)
820 retval->transport = chunk->transport;
821
822 return retval;
823}
824
825/* Helper to create ABORT with a NO_USER_DATA error. */
826struct sctp_chunk *sctp_make_abort_no_data(
827 const struct sctp_association *asoc,
828 const struct sctp_chunk *chunk, __u32 tsn)
829{
830 struct sctp_chunk *retval;
Al Viro9f81bcd2006-11-20 17:26:34 -0800831 __be32 payload;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t)
834 + sizeof(tsn));
835
836 if (!retval)
837 goto no_mem;
838
839 /* Put the tsn back into network byte order. */
840 payload = htonl(tsn);
Wei Yongjun00f1c2d2007-08-21 15:50:01 +0800841 sctp_init_cause(retval, SCTP_ERROR_NO_DATA, sizeof(payload));
842 sctp_addto_chunk(retval, sizeof(payload), (const void *)&payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
845 *
846 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
847 * HEARTBEAT ACK, * etc.) to the same destination transport
848 * address from which it * received the DATA or control chunk
849 * to which it is replying.
850 *
851 * [ABORT back to where the offender came from.]
852 */
853 if (chunk)
854 retval->transport = chunk->transport;
855
856no_mem:
857 return retval;
858}
859
860/* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error. */
861struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
Sridhar Samudralac164a9b2006-08-22 11:50:39 -0700862 const struct msghdr *msg,
863 size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
865 struct sctp_chunk *retval;
Sridhar Samudralac164a9b2006-08-22 11:50:39 -0700866 void *payload = NULL;
867 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Sridhar Samudralac164a9b2006-08-22 11:50:39 -0700869 retval = sctp_make_abort(asoc, NULL, sizeof(sctp_errhdr_t) + paylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (!retval)
871 goto err_chunk;
872
873 if (paylen) {
874 /* Put the msg_iov together into payload. */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -0700875 payload = kmalloc(paylen, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (!payload)
877 goto err_payload;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Sridhar Samudralac164a9b2006-08-22 11:50:39 -0700879 err = memcpy_fromiovec(payload, msg->msg_iov, paylen);
880 if (err < 0)
881 goto err_copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
883
Wei Yongjun00f1c2d2007-08-21 15:50:01 +0800884 sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, paylen);
885 sctp_addto_chunk(retval, paylen, payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 if (paylen)
888 kfree(payload);
889
890 return retval;
891
892err_copy:
893 kfree(payload);
894err_payload:
895 sctp_chunk_free(retval);
896 retval = NULL;
897err_chunk:
898 return retval;
899}
900
Adrian Bunk5c94bf82007-09-12 15:16:21 +0200901/* Append bytes to the end of a parameter. Will panic if chunk is not big
902 * enough.
903 */
904static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
905 const void *data)
906{
907 void *target;
908 int chunklen = ntohs(chunk->chunk_hdr->length);
909
910 target = skb_put(chunk->skb, len);
911
912 memcpy(target, data, len);
913
914 /* Adjust the chunk length field. */
915 chunk->chunk_hdr->length = htons(chunklen + len);
916 chunk->chunk_end = skb_tail_pointer(chunk->skb);
917
918 return target;
919}
920
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900921/* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922struct sctp_chunk *sctp_make_abort_violation(
923 const struct sctp_association *asoc,
924 const struct sctp_chunk *chunk,
925 const __u8 *payload,
926 const size_t paylen)
927{
928 struct sctp_chunk *retval;
929 struct sctp_paramhdr phdr;
930
931 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t) + paylen
Wei Yongjun00f1c2d2007-08-21 15:50:01 +0800932 + sizeof(sctp_paramhdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (!retval)
934 goto end;
935
Wei Yongjun00f1c2d2007-08-21 15:50:01 +0800936 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, paylen
937 + sizeof(sctp_paramhdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 phdr.type = htons(chunk->chunk_hdr->type);
940 phdr.length = chunk->chunk_hdr->length;
Wei Yongjun00f1c2d2007-08-21 15:50:01 +0800941 sctp_addto_chunk(retval, paylen, payload);
942 sctp_addto_param(retval, sizeof(sctp_paramhdr_t), &phdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944end:
945 return retval;
946}
947
948/* Make a HEARTBEAT chunk. */
949struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
950 const struct sctp_transport *transport,
951 const void *payload, const size_t paylen)
952{
953 struct sctp_chunk *retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT,
954 0, paylen);
955
956 if (!retval)
957 goto nodata;
958
959 /* Cast away the 'const', as this is just telling the chunk
960 * what transport it belongs to.
961 */
962 retval->transport = (struct sctp_transport *) transport;
963 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
964
965nodata:
966 return retval;
967}
968
969struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
970 const struct sctp_chunk *chunk,
971 const void *payload, const size_t paylen)
972{
973 struct sctp_chunk *retval;
974
975 retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen);
976 if (!retval)
977 goto nodata;
978
979 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
980
981 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
982 *
983 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
984 * HEARTBEAT ACK, * etc.) to the same destination transport
985 * address from which it * received the DATA or control chunk
986 * to which it is replying.
987 *
988 * [HBACK back to where the HEARTBEAT came from.]
989 */
990 if (chunk)
991 retval->transport = chunk->transport;
992
993nodata:
994 return retval;
995}
996
997/* Create an Operation Error chunk with the specified space reserved.
998 * This routine can be used for containing multiple causes in the chunk.
999 */
1000static struct sctp_chunk *sctp_make_op_error_space(
1001 const struct sctp_association *asoc,
1002 const struct sctp_chunk *chunk,
1003 size_t size)
1004{
1005 struct sctp_chunk *retval;
1006
1007 retval = sctp_make_chunk(asoc, SCTP_CID_ERROR, 0,
1008 sizeof(sctp_errhdr_t) + size);
1009 if (!retval)
1010 goto nodata;
1011
1012 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1013 *
1014 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1015 * HEARTBEAT ACK, etc.) to the same destination transport
1016 * address from which it received the DATA or control chunk
1017 * to which it is replying.
1018 *
1019 */
1020 if (chunk)
1021 retval->transport = chunk->transport;
1022
1023nodata:
1024 return retval;
1025}
1026
1027/* Create an Operation Error chunk. */
1028struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
1029 const struct sctp_chunk *chunk,
Al Viro63706c52006-11-20 17:00:05 -08001030 __be16 cause_code, const void *payload,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 size_t paylen)
1032{
1033 struct sctp_chunk *retval;
1034
1035 retval = sctp_make_op_error_space(asoc, chunk, paylen);
1036 if (!retval)
1037 goto nodata;
1038
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001039 sctp_init_cause(retval, cause_code, paylen);
1040 sctp_addto_chunk(retval, paylen, payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042nodata:
1043 return retval;
1044}
1045
1046/********************************************************************
1047 * 2nd Level Abstractions
1048 ********************************************************************/
1049
1050/* Turn an skb into a chunk.
1051 * FIXME: Eventually move the structure directly inside the skb->cb[].
1052 */
1053struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
1054 const struct sctp_association *asoc,
1055 struct sock *sk)
1056{
1057 struct sctp_chunk *retval;
1058
Robert P. J. Dayc3762222007-02-10 01:45:03 -08001059 retval = kmem_cache_zalloc(sctp_chunk_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 if (!retval)
1062 goto nodata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 if (!sk) {
1065 SCTP_DEBUG_PRINTK("chunkifying skb %p w/o an sk\n", skb);
1066 }
1067
David S. Miller79af02c2005-07-08 21:47:49 -07001068 INIT_LIST_HEAD(&retval->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 retval->skb = skb;
1070 retval->asoc = (struct sctp_association *)asoc;
1071 retval->resent = 0;
1072 retval->has_tsn = 0;
1073 retval->has_ssn = 0;
1074 retval->rtt_in_progress = 0;
1075 retval->sent_at = 0;
1076 retval->singleton = 1;
1077 retval->end_of_packet = 0;
1078 retval->ecn_ce_done = 0;
1079 retval->pdiscard = 0;
1080
1081 /* sctpimpguide-05.txt Section 2.8.2
1082 * M1) Each time a new DATA chunk is transmitted
1083 * set the 'TSN.Missing.Report' count for that TSN to 0. The
1084 * 'TSN.Missing.Report' count will be used to determine missing chunks
1085 * and when to fast retransmit.
1086 */
1087 retval->tsn_missing_report = 0;
1088 retval->tsn_gap_acked = 0;
1089 retval->fast_retransmit = 0;
1090
1091 /* If this is a fragmented message, track all fragments
1092 * of the message (for SEND_FAILED).
1093 */
1094 retval->msg = NULL;
1095
1096 /* Polish the bead hole. */
1097 INIT_LIST_HEAD(&retval->transmitted_list);
1098 INIT_LIST_HEAD(&retval->frag_list);
1099 SCTP_DBG_OBJCNT_INC(chunk);
1100 atomic_set(&retval->refcnt, 1);
1101
1102nodata:
1103 return retval;
1104}
1105
1106/* Set chunk->source and dest based on the IP header in chunk->skb. */
1107void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
1108 union sctp_addr *dest)
1109{
Al Virof235fca2006-11-20 17:09:01 -08001110 memcpy(&chunk->source, src, sizeof(union sctp_addr));
Al Viro16b0a032006-11-20 17:13:38 -08001111 memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
1113
1114/* Extract the source address from a chunk. */
1115const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
1116{
1117 /* If we have a known transport, use that. */
1118 if (chunk->transport) {
Al Viro6a1e5f32006-11-20 17:12:25 -08001119 return &chunk->transport->ipaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 } else {
1121 /* Otherwise, extract it from the IP header. */
Al Viro6a1e5f32006-11-20 17:12:25 -08001122 return &chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124}
1125
1126/* Create a new chunk, setting the type and flags headers from the
1127 * arguments, reserving enough space for a 'paylen' byte payload.
1128 */
1129SCTP_STATIC
1130struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
1131 __u8 type, __u8 flags, int paylen)
1132{
1133 struct sctp_chunk *retval;
1134 sctp_chunkhdr_t *chunk_hdr;
1135 struct sk_buff *skb;
1136 struct sock *sk;
1137
1138 /* No need to allocate LL here, as this is only a chunk. */
1139 skb = alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t) + paylen),
1140 GFP_ATOMIC);
1141 if (!skb)
1142 goto nodata;
1143
1144 /* Make room for the chunk header. */
1145 chunk_hdr = (sctp_chunkhdr_t *)skb_put(skb, sizeof(sctp_chunkhdr_t));
1146 chunk_hdr->type = type;
1147 chunk_hdr->flags = flags;
1148 chunk_hdr->length = htons(sizeof(sctp_chunkhdr_t));
1149
1150 sk = asoc ? asoc->base.sk : NULL;
1151 retval = sctp_chunkify(skb, asoc, sk);
1152 if (!retval) {
1153 kfree_skb(skb);
1154 goto nodata;
1155 }
1156
1157 retval->chunk_hdr = chunk_hdr;
1158 retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(struct sctp_chunkhdr);
1159
1160 /* Set the skb to the belonging sock for accounting. */
1161 skb->sk = sk;
1162
1163 return retval;
1164nodata:
1165 return NULL;
1166}
1167
1168
1169/* Release the memory occupied by a chunk. */
1170static void sctp_chunk_destroy(struct sctp_chunk *chunk)
1171{
1172 /* Free the chunk skb data and the SCTP_chunk stub itself. */
1173 dev_kfree_skb(chunk->skb);
1174
1175 SCTP_DBG_OBJCNT_DEC(chunk);
1176 kmem_cache_free(sctp_chunk_cachep, chunk);
1177}
1178
1179/* Possibly, free the chunk. */
1180void sctp_chunk_free(struct sctp_chunk *chunk)
1181{
David S. Miller79af02c2005-07-08 21:47:49 -07001182 BUG_ON(!list_empty(&chunk->list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 list_del_init(&chunk->transmitted_list);
1184
1185 /* Release our reference on the message tracker. */
1186 if (chunk->msg)
1187 sctp_datamsg_put(chunk->msg);
1188
1189 sctp_chunk_put(chunk);
1190}
1191
1192/* Grab a reference to the chunk. */
1193void sctp_chunk_hold(struct sctp_chunk *ch)
1194{
1195 atomic_inc(&ch->refcnt);
1196}
1197
1198/* Release a reference to the chunk. */
1199void sctp_chunk_put(struct sctp_chunk *ch)
1200{
1201 if (atomic_dec_and_test(&ch->refcnt))
1202 sctp_chunk_destroy(ch);
1203}
1204
1205/* Append bytes to the end of a chunk. Will panic if chunk is not big
1206 * enough.
1207 */
1208void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
1209{
1210 void *target;
1211 void *padding;
1212 int chunklen = ntohs(chunk->chunk_hdr->length);
Wei Yongjun8d614ad2007-08-06 13:55:47 +08001213 int padlen = WORD_ROUND(chunklen) - chunklen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 padding = skb_put(chunk->skb, padlen);
1216 target = skb_put(chunk->skb, len);
1217
1218 memset(padding, 0, padlen);
1219 memcpy(target, data, len);
1220
1221 /* Adjust the chunk length field. */
1222 chunk->chunk_hdr->length = htons(chunklen + padlen + len);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001223 chunk->chunk_end = skb_tail_pointer(chunk->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 return target;
1226}
1227
1228/* Append bytes from user space to the end of a chunk. Will panic if
1229 * chunk is not big enough.
1230 * Returns a kernel err value.
1231 */
1232int sctp_user_addto_chunk(struct sctp_chunk *chunk, int off, int len,
1233 struct iovec *data)
1234{
1235 __u8 *target;
1236 int err = 0;
1237
1238 /* Make room in chunk for data. */
1239 target = skb_put(chunk->skb, len);
1240
1241 /* Copy data (whole iovec) into chunk */
1242 if ((err = memcpy_fromiovecend(target, data, off, len)))
1243 goto out;
1244
1245 /* Adjust the chunk length field. */
1246 chunk->chunk_hdr->length =
1247 htons(ntohs(chunk->chunk_hdr->length) + len);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001248 chunk->chunk_end = skb_tail_pointer(chunk->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250out:
1251 return err;
1252}
1253
1254/* Helper function to assign a TSN if needed. This assumes that both
1255 * the data_hdr and association have already been assigned.
1256 */
1257void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
1258{
Vlad Yasevichab3e5e72007-08-02 16:51:42 -04001259 struct sctp_datamsg *msg;
1260 struct sctp_chunk *lchunk;
1261 struct sctp_stream *stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 __u16 ssn;
1263 __u16 sid;
1264
1265 if (chunk->has_ssn)
1266 return;
1267
Vlad Yasevichab3e5e72007-08-02 16:51:42 -04001268 /* All fragments will be on the same stream */
1269 sid = ntohs(chunk->subh.data_hdr->stream);
1270 stream = &chunk->asoc->ssnmap->out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Vlad Yasevichab3e5e72007-08-02 16:51:42 -04001272 /* Now assign the sequence number to the entire message.
1273 * All fragments must have the same stream sequence number.
1274 */
1275 msg = chunk->msg;
1276 list_for_each_entry(lchunk, &msg->chunks, frag_list) {
1277 if (lchunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
1278 ssn = 0;
1279 } else {
1280 if (lchunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
1281 ssn = sctp_ssn_next(stream, sid);
1282 else
1283 ssn = sctp_ssn_peek(stream, sid);
1284 }
1285
1286 lchunk->subh.data_hdr->ssn = htons(ssn);
1287 lchunk->has_ssn = 1;
1288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
1290
1291/* Helper function to assign a TSN if needed. This assumes that both
1292 * the data_hdr and association have already been assigned.
1293 */
1294void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
1295{
1296 if (!chunk->has_tsn) {
1297 /* This is the last possible instant to
1298 * assign a TSN.
1299 */
1300 chunk->subh.data_hdr->tsn =
1301 htonl(sctp_association_get_next_tsn(chunk->asoc));
1302 chunk->has_tsn = 1;
1303 }
1304}
1305
1306/* Create a CLOSED association to use with an incoming packet. */
1307struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
Alexey Dobriyan3182cd82005-07-11 20:57:47 -07001308 struct sctp_chunk *chunk,
Al Virodd0fc662005-10-07 07:46:04 +01001309 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
1311 struct sctp_association *asoc;
1312 struct sk_buff *skb;
1313 sctp_scope_t scope;
1314 struct sctp_af *af;
1315
1316 /* Create the bare association. */
1317 scope = sctp_scope(sctp_source(chunk));
1318 asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
1319 if (!asoc)
1320 goto nodata;
1321 asoc->temp = 1;
1322 skb = chunk->skb;
1323 /* Create an entry for the source address of the packet. */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001324 af = sctp_get_af_specific(ipver2af(ip_hdr(skb)->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 if (unlikely(!af))
1326 goto fail;
Al Virod55c41b2006-11-20 17:09:40 -08001327 af->from_skb(&asoc->c.peer_addr, skb, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328nodata:
1329 return asoc;
1330
1331fail:
1332 sctp_association_free(asoc);
1333 return NULL;
1334}
1335
1336/* Build a cookie representing asoc.
1337 * This INCLUDES the param header needed to put the cookie in the INIT ACK.
1338 */
1339static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
1340 const struct sctp_association *asoc,
1341 const struct sctp_chunk *init_chunk,
1342 int *cookie_len,
1343 const __u8 *raw_addrs, int addrs_len)
1344{
1345 sctp_cookie_param_t *retval;
1346 struct sctp_signed_cookie *cookie;
1347 struct scatterlist sg;
1348 int headersize, bodysize;
1349 unsigned int keylen;
1350 char *key;
1351
Vlad Yasevich9834a2b2006-01-17 11:52:12 -08001352 /* Header size is static data prior to the actual cookie, including
1353 * any padding.
1354 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001355 headersize = sizeof(sctp_paramhdr_t) +
1356 (sizeof(struct sctp_signed_cookie) -
Vlad Yasevich9834a2b2006-01-17 11:52:12 -08001357 sizeof(struct sctp_cookie));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 bodysize = sizeof(struct sctp_cookie)
1359 + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
1360
1361 /* Pad out the cookie to a multiple to make the signature
1362 * functions simpler to write.
1363 */
1364 if (bodysize % SCTP_COOKIE_MULTIPLE)
1365 bodysize += SCTP_COOKIE_MULTIPLE
1366 - (bodysize % SCTP_COOKIE_MULTIPLE);
1367 *cookie_len = headersize + bodysize;
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 /* Clear this memory since we are sending this data structure
1370 * out on the network.
1371 */
Arnaldo Carvalho de Meloaf997d82006-11-21 01:20:33 -02001372 retval = kzalloc(*cookie_len, GFP_ATOMIC);
1373 if (!retval)
1374 goto nodata;
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 cookie = (struct sctp_signed_cookie *) retval->body;
1377
1378 /* Set up the parameter header. */
1379 retval->p.type = SCTP_PARAM_STATE_COOKIE;
1380 retval->p.length = htons(*cookie_len);
1381
1382 /* Copy the cookie part of the association itself. */
1383 cookie->c = asoc->c;
1384 /* Save the raw address list length in the cookie. */
1385 cookie->c.raw_addr_list_len = addrs_len;
1386
1387 /* Remember PR-SCTP capability. */
1388 cookie->c.prsctp_capable = asoc->peer.prsctp_capable;
1389
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001390 /* Save adaptation indication in the cookie. */
1391 cookie->c.adaptation_ind = asoc->peer.adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 /* Set an expiration time for the cookie. */
1394 do_gettimeofday(&cookie->c.expiration);
1395 TIMEVAL_ADD(asoc->cookie_life, cookie->c.expiration);
1396
1397 /* Copy the peer's init packet. */
1398 memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
1399 ntohs(init_chunk->chunk_hdr->length));
1400
1401 /* Copy the raw local address list of the association. */
1402 memcpy((__u8 *)&cookie->c.peer_init[0] +
1403 ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
1404
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001405 if (sctp_sk(ep->base.sk)->hmac) {
Herbert Xu1b489e12006-08-20 15:07:14 +10001406 struct hash_desc desc;
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 /* Sign the message. */
1409 sg.page = virt_to_page(&cookie->c);
1410 sg.offset = (unsigned long)(&cookie->c) % PAGE_SIZE;
1411 sg.length = bodysize;
1412 keylen = SCTP_SECRET_SIZE;
1413 key = (char *)ep->secret_key[ep->current_key];
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001414 desc.tfm = sctp_sk(ep->base.sk)->hmac;
1415 desc.flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Herbert Xu1b489e12006-08-20 15:07:14 +10001417 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1418 crypto_hash_digest(&desc, &sg, bodysize, cookie->signature))
1419 goto free_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 return retval;
Herbert Xu1b489e12006-08-20 15:07:14 +10001423
1424free_cookie:
1425 kfree(retval);
1426nodata:
1427 *cookie_len = 0;
1428 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
1431/* Unpack the cookie from COOKIE ECHO chunk, recreating the association. */
1432struct sctp_association *sctp_unpack_cookie(
1433 const struct sctp_endpoint *ep,
1434 const struct sctp_association *asoc,
Al Virodd0fc662005-10-07 07:46:04 +01001435 struct sctp_chunk *chunk, gfp_t gfp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 int *error, struct sctp_chunk **errp)
1437{
1438 struct sctp_association *retval = NULL;
1439 struct sctp_signed_cookie *cookie;
1440 struct sctp_cookie *bear_cookie;
1441 int headersize, bodysize, fixed_size;
Vlad Yasevich313e7b42006-01-17 11:55:57 -08001442 __u8 *digest = ep->digest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 struct scatterlist sg;
1444 unsigned int keylen, len;
1445 char *key;
1446 sctp_scope_t scope;
1447 struct sk_buff *skb = chunk->skb;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001448 struct timeval tv;
Herbert Xu1b489e12006-08-20 15:07:14 +10001449 struct hash_desc desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Vlad Yasevich9834a2b2006-01-17 11:52:12 -08001451 /* Header size is static data prior to the actual cookie, including
1452 * any padding.
1453 */
1454 headersize = sizeof(sctp_chunkhdr_t) +
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001455 (sizeof(struct sctp_signed_cookie) -
Vlad Yasevich9834a2b2006-01-17 11:52:12 -08001456 sizeof(struct sctp_cookie));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
1458 fixed_size = headersize + sizeof(struct sctp_cookie);
1459
1460 /* Verify that the chunk looks like it even has a cookie.
1461 * There must be enough room for our cookie and our peer's
1462 * INIT chunk.
1463 */
1464 len = ntohs(chunk->chunk_hdr->length);
1465 if (len < fixed_size + sizeof(struct sctp_chunkhdr))
1466 goto malformed;
1467
1468 /* Verify that the cookie has been padded out. */
1469 if (bodysize % SCTP_COOKIE_MULTIPLE)
1470 goto malformed;
1471
1472 /* Process the cookie. */
1473 cookie = chunk->subh.cookie_hdr;
1474 bear_cookie = &cookie->c;
1475
1476 if (!sctp_sk(ep->base.sk)->hmac)
1477 goto no_hmac;
1478
1479 /* Check the signature. */
1480 keylen = SCTP_SECRET_SIZE;
1481 sg.page = virt_to_page(bear_cookie);
1482 sg.offset = (unsigned long)(bear_cookie) % PAGE_SIZE;
1483 sg.length = bodysize;
1484 key = (char *)ep->secret_key[ep->current_key];
Herbert Xu1b489e12006-08-20 15:07:14 +10001485 desc.tfm = sctp_sk(ep->base.sk)->hmac;
1486 desc.flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Al Viro8ca84482006-06-20 03:26:14 -07001488 memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
Herbert Xu1b489e12006-08-20 15:07:14 +10001489 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1490 crypto_hash_digest(&desc, &sg, bodysize, digest)) {
1491 *error = -SCTP_IERROR_NOMEM;
1492 goto fail;
1493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1496 /* Try the previous key. */
1497 key = (char *)ep->secret_key[ep->last_key];
Al Viro8ca84482006-06-20 03:26:14 -07001498 memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
Herbert Xu1b489e12006-08-20 15:07:14 +10001499 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1500 crypto_hash_digest(&desc, &sg, bodysize, digest)) {
1501 *error = -SCTP_IERROR_NOMEM;
1502 goto fail;
1503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1506 /* Yikes! Still bad signature! */
1507 *error = -SCTP_IERROR_BAD_SIG;
1508 goto fail;
1509 }
1510 }
1511
1512no_hmac:
1513 /* IG Section 2.35.2:
1514 * 3) Compare the port numbers and the verification tag contained
1515 * within the COOKIE ECHO chunk to the actual port numbers and the
1516 * verification tag within the SCTP common header of the received
1517 * packet. If these values do not match the packet MUST be silently
1518 * discarded,
1519 */
1520 if (ntohl(chunk->sctp_hdr->vtag) != bear_cookie->my_vtag) {
1521 *error = -SCTP_IERROR_BAD_TAG;
1522 goto fail;
1523 }
1524
Al Viro9b1dfad2006-11-20 17:09:17 -08001525 if (chunk->sctp_hdr->source != bear_cookie->peer_addr.v4.sin_port ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 ntohs(chunk->sctp_hdr->dest) != bear_cookie->my_port) {
1527 *error = -SCTP_IERROR_BAD_PORTS;
1528 goto fail;
1529 }
1530
1531 /* Check to see if the cookie is stale. If there is already
1532 * an association, there is no need to check cookie's expiration
1533 * for init collision case of lost COOKIE ACK.
Vlad Yasevichf236218b2006-09-29 17:10:03 -07001534 * If skb has been timestamped, then use the stamp, otherwise
1535 * use current time. This introduces a small possibility that
1536 * that a cookie may be considered expired, but his would only slow
1537 * down the new association establishment instead of every packet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 */
Vlad Yasevichf236218b2006-09-29 17:10:03 -07001539 if (sock_flag(ep->base.sk, SOCK_TIMESTAMP))
1540 skb_get_timestamp(skb, &tv);
1541 else
1542 do_gettimeofday(&tv);
1543
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001544 if (!asoc && tv_lt(bear_cookie->expiration, tv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 /*
1546 * Section 3.3.10.3 Stale Cookie Error (3)
1547 *
1548 * Cause of error
1549 * ---------------
1550 * Stale Cookie Error: Indicates the receipt of a valid State
1551 * Cookie that has expired.
1552 */
1553 len = ntohs(chunk->chunk_hdr->length);
1554 *errp = sctp_make_op_error_space(asoc, chunk, len);
1555 if (*errp) {
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001556 suseconds_t usecs = (tv.tv_sec -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 bear_cookie->expiration.tv_sec) * 1000000L +
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001558 tv.tv_usec - bear_cookie->expiration.tv_usec;
Al Viro34bcca22006-11-20 17:27:15 -08001559 __be32 n = htonl(usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001562 sizeof(n));
1563 sctp_addto_chunk(*errp, sizeof(n), &n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 *error = -SCTP_IERROR_STALE_COOKIE;
1565 } else
1566 *error = -SCTP_IERROR_NOMEM;
1567
1568 goto fail;
1569 }
1570
1571 /* Make a new base association. */
1572 scope = sctp_scope(sctp_source(chunk));
1573 retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
1574 if (!retval) {
1575 *error = -SCTP_IERROR_NOMEM;
1576 goto fail;
1577 }
1578
1579 /* Set up our peer's port number. */
1580 retval->peer.port = ntohs(chunk->sctp_hdr->source);
1581
1582 /* Populate the association from the cookie. */
1583 memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
1584
1585 if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
1586 GFP_ATOMIC) < 0) {
1587 *error = -SCTP_IERROR_NOMEM;
1588 goto fail;
1589 }
1590
1591 /* Also, add the destination address. */
1592 if (list_empty(&retval->base.bind_addr.address_list)) {
Al Viro16b0a032006-11-20 17:13:38 -08001593 sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest, 1,
Vlad Yasevich559cf712007-09-16 16:03:28 -07001594 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 }
1596
1597 retval->next_tsn = retval->c.initial_tsn;
1598 retval->ctsn_ack_point = retval->next_tsn - 1;
1599 retval->addip_serial = retval->c.initial_tsn;
1600 retval->adv_peer_ack_point = retval->ctsn_ack_point;
1601 retval->peer.prsctp_capable = retval->c.prsctp_capable;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001602 retval->peer.adaptation_ind = retval->c.adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 /* The INIT stuff will be done by the side effects. */
1605 return retval;
1606
1607fail:
1608 if (retval)
1609 sctp_association_free(retval);
1610
1611 return NULL;
1612
1613malformed:
1614 /* Yikes! The packet is either corrupt or deliberately
1615 * malformed.
1616 */
1617 *error = -SCTP_IERROR_MALFORMED;
1618 goto fail;
1619}
1620
1621/********************************************************************
1622 * 3rd Level Abstractions
1623 ********************************************************************/
1624
1625struct __sctp_missing {
Al Viro9f81bcd2006-11-20 17:26:34 -08001626 __be32 num_missing;
1627 __be16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628} __attribute__((packed));
1629
1630/*
1631 * Report a missing mandatory parameter.
1632 */
1633static int sctp_process_missing_param(const struct sctp_association *asoc,
1634 sctp_param_t paramtype,
1635 struct sctp_chunk *chunk,
1636 struct sctp_chunk **errp)
1637{
1638 struct __sctp_missing report;
1639 __u16 len;
1640
1641 len = WORD_ROUND(sizeof(report));
1642
1643 /* Make an ERROR chunk, preparing enough room for
1644 * returning multiple unknown parameters.
1645 */
1646 if (!*errp)
1647 *errp = sctp_make_op_error_space(asoc, chunk, len);
1648
1649 if (*errp) {
1650 report.num_missing = htonl(1);
1651 report.type = paramtype;
Vlad Yasevichebdfcad2007-01-15 19:12:31 -08001652 sctp_init_cause(*errp, SCTP_ERROR_MISS_PARAM,
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001653 sizeof(report));
1654 sctp_addto_chunk(*errp, sizeof(report), &report);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 }
1656
1657 /* Stop processing this chunk. */
1658 return 0;
1659}
1660
1661/* Report an Invalid Mandatory Parameter. */
1662static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
1663 struct sctp_chunk *chunk,
1664 struct sctp_chunk **errp)
1665{
1666 /* Invalid Mandatory Parameter Error has no payload. */
1667
1668 if (!*errp)
1669 *errp = sctp_make_op_error_space(asoc, chunk, 0);
1670
1671 if (*errp)
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001672 sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 /* Stop processing this chunk. */
1675 return 0;
1676}
1677
1678static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
1679 struct sctp_paramhdr *param,
1680 const struct sctp_chunk *chunk,
1681 struct sctp_chunk **errp)
1682{
1683 char error[] = "The following parameter had invalid length:";
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001684 size_t payload_len = WORD_ROUND(sizeof(error)) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 sizeof(sctp_paramhdr_t);
1686
1687
1688 /* Create an error chunk and fill it in with our payload. */
1689 if (!*errp)
1690 *errp = sctp_make_op_error_space(asoc, chunk, payload_len);
1691
1692 if (*errp) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001693 sctp_init_cause(*errp, SCTP_ERROR_PROTO_VIOLATION,
1694 sizeof(error) + sizeof(sctp_paramhdr_t));
1695 sctp_addto_chunk(*errp, sizeof(error), error);
1696 sctp_addto_param(*errp, sizeof(sctp_paramhdr_t), param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 }
1698
1699 return 0;
1700}
1701
1702
1703/* Do not attempt to handle the HOST_NAME parm. However, do
1704 * send back an indicator to the peer.
1705 */
1706static int sctp_process_hn_param(const struct sctp_association *asoc,
1707 union sctp_params param,
1708 struct sctp_chunk *chunk,
1709 struct sctp_chunk **errp)
1710{
1711 __u16 len = ntohs(param.p->length);
1712
1713 /* Make an ERROR chunk. */
1714 if (!*errp)
1715 *errp = sctp_make_op_error_space(asoc, chunk, len);
1716
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001717 if (*errp) {
1718 sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED, len);
1719 sctp_addto_chunk(*errp, len, param.v);
1720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 /* Stop processing this chunk. */
1723 return 0;
1724}
1725
Vlad Yasevich131a47e2007-09-16 15:53:56 -07001726static void sctp_process_ext_param(struct sctp_association *asoc,
1727 union sctp_params param)
1728{
1729 __u16 num_ext = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
1730 int i;
1731
1732 for (i = 0; i < num_ext; i++) {
1733 switch (param.ext->chunks[i]) {
1734 case SCTP_CID_FWD_TSN:
1735 if (sctp_prsctp_enable &&
1736 !asoc->peer.prsctp_capable)
1737 asoc->peer.prsctp_capable = 1;
1738 break;
1739 case SCTP_CID_ASCONF:
1740 case SCTP_CID_ASCONF_ACK:
1741 /* don't need to do anything for ASCONF */
1742 default:
1743 break;
1744 }
1745 }
1746}
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748/* RFC 3.2.1 & the Implementers Guide 2.2.
1749 *
1750 * The Parameter Types are encoded such that the
1751 * highest-order two bits specify the action that must be
1752 * taken if the processing endpoint does not recognize the
1753 * Parameter Type.
1754 *
1755 * 00 - Stop processing this SCTP chunk and discard it,
1756 * do not process any further chunks within it.
1757 *
1758 * 01 - Stop processing this SCTP chunk and discard it,
1759 * do not process any further chunks within it, and report
1760 * the unrecognized parameter in an 'Unrecognized
1761 * Parameter Type' (in either an ERROR or in the INIT ACK).
1762 *
1763 * 10 - Skip this parameter and continue processing.
1764 *
1765 * 11 - Skip this parameter and continue processing but
1766 * report the unrecognized parameter in an
1767 * 'Unrecognized Parameter Type' (in either an ERROR or in
1768 * the INIT ACK).
1769 *
1770 * Return value:
1771 * 0 - discard the chunk
1772 * 1 - continue with the chunk
1773 */
1774static int sctp_process_unk_param(const struct sctp_association *asoc,
1775 union sctp_params param,
1776 struct sctp_chunk *chunk,
1777 struct sctp_chunk **errp)
1778{
1779 int retval = 1;
1780
1781 switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
1782 case SCTP_PARAM_ACTION_DISCARD:
1783 retval = 0;
1784 break;
1785 case SCTP_PARAM_ACTION_DISCARD_ERR:
1786 retval = 0;
1787 /* Make an ERROR chunk, preparing enough room for
1788 * returning multiple unknown parameters.
1789 */
1790 if (NULL == *errp)
1791 *errp = sctp_make_op_error_space(asoc, chunk,
1792 ntohs(chunk->chunk_hdr->length));
1793
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001794 if (*errp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 WORD_ROUND(ntohs(param.p->length)));
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001797 sctp_addto_chunk(*errp,
1798 WORD_ROUND(ntohs(param.p->length)),
1799 param.v);
1800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 break;
1803 case SCTP_PARAM_ACTION_SKIP:
1804 break;
1805 case SCTP_PARAM_ACTION_SKIP_ERR:
1806 /* Make an ERROR chunk, preparing enough room for
1807 * returning multiple unknown parameters.
1808 */
1809 if (NULL == *errp)
1810 *errp = sctp_make_op_error_space(asoc, chunk,
1811 ntohs(chunk->chunk_hdr->length));
1812
1813 if (*errp) {
1814 sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 WORD_ROUND(ntohs(param.p->length)));
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001816 sctp_addto_chunk(*errp,
1817 WORD_ROUND(ntohs(param.p->length)),
1818 param.v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 } else {
1820 /* If there is no memory for generating the ERROR
1821 * report as specified, an ABORT will be triggered
1822 * to the peer and the association won't be
1823 * established.
1824 */
1825 retval = 0;
1826 }
1827
1828 break;
1829 default:
1830 break;
1831 }
1832
1833 return retval;
1834}
1835
1836/* Find unrecognized parameters in the chunk.
1837 * Return values:
1838 * 0 - discard the chunk
1839 * 1 - continue with the chunk
1840 */
1841static int sctp_verify_param(const struct sctp_association *asoc,
1842 union sctp_params param,
1843 sctp_cid_t cid,
1844 struct sctp_chunk *chunk,
1845 struct sctp_chunk **err_chunk)
1846{
1847 int retval = 1;
1848
1849 /* FIXME - This routine is not looking at each parameter per the
1850 * chunk type, i.e., unrecognized parameters should be further
1851 * identified based on the chunk id.
1852 */
1853
1854 switch (param.p->type) {
1855 case SCTP_PARAM_IPV4_ADDRESS:
1856 case SCTP_PARAM_IPV6_ADDRESS:
1857 case SCTP_PARAM_COOKIE_PRESERVATIVE:
1858 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
1859 case SCTP_PARAM_STATE_COOKIE:
1860 case SCTP_PARAM_HEARTBEAT_INFO:
1861 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
1862 case SCTP_PARAM_ECN_CAPABLE:
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001863 case SCTP_PARAM_ADAPTATION_LAYER_IND:
Vlad Yasevich131a47e2007-09-16 15:53:56 -07001864 case SCTP_PARAM_SUPPORTED_EXT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 break;
1866
1867 case SCTP_PARAM_HOST_NAME_ADDRESS:
1868 /* Tell the peer, we won't support this param. */
1869 return sctp_process_hn_param(asoc, param, chunk, err_chunk);
Vlad Yasevich131a47e2007-09-16 15:53:56 -07001870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 case SCTP_PARAM_FWD_TSN_SUPPORT:
1872 if (sctp_prsctp_enable)
1873 break;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001874 /* Fall Through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 default:
1876 SCTP_DEBUG_PRINTK("Unrecognized param: %d for chunk %d.\n",
1877 ntohs(param.p->type), cid);
1878 return sctp_process_unk_param(asoc, param, chunk, err_chunk);
1879
1880 break;
1881 }
1882 return retval;
1883}
1884
1885/* Verify the INIT packet before we process it. */
1886int sctp_verify_init(const struct sctp_association *asoc,
1887 sctp_cid_t cid,
1888 sctp_init_chunk_t *peer_init,
1889 struct sctp_chunk *chunk,
1890 struct sctp_chunk **errp)
1891{
1892 union sctp_params param;
1893 int has_cookie = 0;
1894
1895 /* Verify stream values are non-zero. */
1896 if ((0 == peer_init->init_hdr.num_outbound_streams) ||
Vlad Yasevichd023f622007-01-15 19:15:45 -08001897 (0 == peer_init->init_hdr.num_inbound_streams) ||
1898 (0 == peer_init->init_hdr.init_tag) ||
1899 (SCTP_DEFAULT_MINWINDOW > ntohl(peer_init->init_hdr.a_rwnd))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 sctp_process_inv_mandatory(asoc, chunk, errp);
1902 return 0;
1903 }
1904
1905 /* Check for missing mandatory parameters. */
1906 sctp_walk_params(param, peer_init, init_hdr.params) {
1907
1908 if (SCTP_PARAM_STATE_COOKIE == param.p->type)
1909 has_cookie = 1;
1910
1911 } /* for (loop through all parameters) */
1912
1913 /* There is a possibility that a parameter length was bad and
1914 * in that case we would have stoped walking the parameters.
1915 * The current param.p would point at the bad one.
1916 * Current consensus on the mailing list is to generate a PROTOCOL
1917 * VIOLATION error. We build the ERROR chunk here and let the normal
1918 * error handling code build and send the packet.
1919 */
Wei Yongjuncb243a12007-08-06 13:55:58 +08001920 if (param.v != (void*)chunk->chunk_end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
1922 return 0;
1923 }
1924
1925 /* The only missing mandatory param possible today is
1926 * the state cookie for an INIT-ACK chunk.
1927 */
1928 if ((SCTP_CID_INIT_ACK == cid) && !has_cookie) {
1929 sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
1930 chunk, errp);
1931 return 0;
1932 }
1933
1934 /* Find unrecognized parameters. */
1935
1936 sctp_walk_params(param, peer_init, init_hdr.params) {
1937
1938 if (!sctp_verify_param(asoc, param, cid, chunk, errp)) {
1939 if (SCTP_PARAM_HOST_NAME_ADDRESS == param.p->type)
1940 return 0;
1941 else
1942 return 1;
1943 }
1944
1945 } /* for (loop through all parameters) */
1946
1947 return 1;
1948}
1949
1950/* Unpack the parameters in an INIT packet into an association.
1951 * Returns 0 on failure, else success.
1952 * FIXME: This is an association method.
1953 */
1954int sctp_process_init(struct sctp_association *asoc, sctp_cid_t cid,
1955 const union sctp_addr *peer_addr,
Al Virodd0fc662005-10-07 07:46:04 +01001956 sctp_init_chunk_t *peer_init, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
1958 union sctp_params param;
1959 struct sctp_transport *transport;
1960 struct list_head *pos, *temp;
1961 char *cookie;
1962
1963 /* We must include the address that the INIT packet came from.
1964 * This is the only address that matters for an INIT packet.
1965 * When processing a COOKIE ECHO, we retrieve the from address
1966 * of the INIT from the cookie.
1967 */
1968
1969 /* This implementation defaults to making the first transport
1970 * added as the primary transport. The source address seems to
1971 * be a a better choice than any of the embedded addresses.
1972 */
Al Viro4bdf4b52006-11-20 17:10:20 -08001973 if (peer_addr) {
Al Viro6a1e5f32006-11-20 17:12:25 -08001974 if(!sctp_assoc_add_peer(asoc, peer_addr, gfp, SCTP_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 goto nomem;
Al Viro4bdf4b52006-11-20 17:10:20 -08001976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978 /* Process the initialization parameters. */
1979
1980 sctp_walk_params(param, peer_init, init_hdr.params) {
1981
1982 if (!sctp_process_param(asoc, param, peer_addr, gfp))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001983 goto clean_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 }
1985
Frank Filz3f7a87d2005-06-20 13:14:57 -07001986 /* Walk list of transports, removing transports in the UNKNOWN state. */
1987 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
1988 transport = list_entry(pos, struct sctp_transport, transports);
1989 if (transport->state == SCTP_UNKNOWN) {
1990 sctp_assoc_rm_peer(asoc, transport);
1991 }
1992 }
1993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 /* The fixed INIT headers are always in network byte
1995 * order.
1996 */
1997 asoc->peer.i.init_tag =
1998 ntohl(peer_init->init_hdr.init_tag);
1999 asoc->peer.i.a_rwnd =
2000 ntohl(peer_init->init_hdr.a_rwnd);
2001 asoc->peer.i.num_outbound_streams =
2002 ntohs(peer_init->init_hdr.num_outbound_streams);
2003 asoc->peer.i.num_inbound_streams =
2004 ntohs(peer_init->init_hdr.num_inbound_streams);
2005 asoc->peer.i.initial_tsn =
2006 ntohl(peer_init->init_hdr.initial_tsn);
2007
2008 /* Apply the upper bounds for output streams based on peer's
2009 * number of inbound streams.
2010 */
2011 if (asoc->c.sinit_num_ostreams >
2012 ntohs(peer_init->init_hdr.num_inbound_streams)) {
2013 asoc->c.sinit_num_ostreams =
2014 ntohs(peer_init->init_hdr.num_inbound_streams);
2015 }
2016
2017 if (asoc->c.sinit_max_instreams >
2018 ntohs(peer_init->init_hdr.num_outbound_streams)) {
2019 asoc->c.sinit_max_instreams =
2020 ntohs(peer_init->init_hdr.num_outbound_streams);
2021 }
2022
2023 /* Copy Initiation tag from INIT to VT_peer in cookie. */
2024 asoc->c.peer_vtag = asoc->peer.i.init_tag;
2025
2026 /* Peer Rwnd : Current calculated value of the peer's rwnd. */
2027 asoc->peer.rwnd = asoc->peer.i.a_rwnd;
2028
2029 /* Copy cookie in case we need to resend COOKIE-ECHO. */
2030 cookie = asoc->peer.cookie;
2031 if (cookie) {
Arnaldo Carvalho de Meloaf997d82006-11-21 01:20:33 -02002032 asoc->peer.cookie = kmemdup(cookie, asoc->peer.cookie_len, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 if (!asoc->peer.cookie)
2034 goto clean_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 }
2036
2037 /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
2038 * high (for example, implementations MAY use the size of the receiver
2039 * advertised window).
2040 */
2041 list_for_each(pos, &asoc->peer.transport_addr_list) {
2042 transport = list_entry(pos, struct sctp_transport, transports);
2043 transport->ssthresh = asoc->peer.i.a_rwnd;
2044 }
2045
2046 /* Set up the TSN tracking pieces. */
2047 sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_SIZE,
2048 asoc->peer.i.initial_tsn);
2049
2050 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
2051 *
2052 * The stream sequence number in all the streams shall start
2053 * from 0 when the association is established. Also, when the
2054 * stream sequence number reaches the value 65535 the next
2055 * stream sequence number shall be set to 0.
2056 */
2057
Frank Filz3f7a87d2005-06-20 13:14:57 -07002058 /* Allocate storage for the negotiated streams if it is not a temporary
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002059 * association.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 */
2061 if (!asoc->temp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 int error;
2063
2064 asoc->ssnmap = sctp_ssnmap_new(asoc->c.sinit_max_instreams,
2065 asoc->c.sinit_num_ostreams, gfp);
2066 if (!asoc->ssnmap)
2067 goto clean_up;
2068
Vlad Yasevich07d93962007-05-04 13:55:27 -07002069 error = sctp_assoc_set_id(asoc, gfp);
2070 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 goto clean_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 }
2073
2074 /* ADDIP Section 4.1 ASCONF Chunk Procedures
2075 *
2076 * When an endpoint has an ASCONF signaled change to be sent to the
2077 * remote endpoint it should do the following:
2078 * ...
2079 * A2) A serial number should be assigned to the Chunk. The serial
2080 * number should be a monotonically increasing number. All serial
2081 * numbers are defined to be initialized at the start of the
2082 * association to the same value as the Initial TSN.
2083 */
2084 asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
2085 return 1;
2086
2087clean_up:
2088 /* Release the transport structures. */
2089 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2090 transport = list_entry(pos, struct sctp_transport, transports);
2091 list_del_init(pos);
2092 sctp_transport_free(transport);
2093 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07002094
2095 asoc->peer.transport_count = 0;
2096
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097nomem:
2098 return 0;
2099}
2100
2101
2102/* Update asoc with the option described in param.
2103 *
2104 * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
2105 *
2106 * asoc is the association to update.
2107 * param is the variable length parameter to use for update.
2108 * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
2109 * If the current packet is an INIT we want to minimize the amount of
2110 * work we do. In particular, we should not build transport
2111 * structures for the addresses.
2112 */
2113static int sctp_process_param(struct sctp_association *asoc,
2114 union sctp_params param,
2115 const union sctp_addr *peer_addr,
Al Virodd0fc662005-10-07 07:46:04 +01002116 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
2118 union sctp_addr addr;
2119 int i;
2120 __u16 sat;
2121 int retval = 1;
2122 sctp_scope_t scope;
2123 time_t stale;
2124 struct sctp_af *af;
2125
2126 /* We maintain all INIT parameters in network byte order all the
2127 * time. This allows us to not worry about whether the parameters
2128 * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
2129 */
2130 switch (param.p->type) {
2131 case SCTP_PARAM_IPV6_ADDRESS:
2132 if (PF_INET6 != asoc->base.sk->sk_family)
2133 break;
2134 /* Fall through. */
2135 case SCTP_PARAM_IPV4_ADDRESS:
2136 af = sctp_get_af_specific(param_type2af(param.p->type));
Al Virodd86d132006-11-20 17:11:13 -08002137 af->from_addr_param(&addr, param.addr, htons(asoc->peer.port), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 scope = sctp_scope(peer_addr);
Al Virodd86d132006-11-20 17:11:13 -08002139 if (sctp_in_scope(&addr, scope))
2140 if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_UNCONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 return 0;
2142 break;
2143
2144 case SCTP_PARAM_COOKIE_PRESERVATIVE:
2145 if (!sctp_cookie_preserve_enable)
2146 break;
2147
2148 stale = ntohl(param.life->lifespan_increment);
2149
2150 /* Suggested Cookie Life span increment's unit is msec,
2151 * (1/1000sec).
2152 */
2153 asoc->cookie_life.tv_sec += stale / 1000;
2154 asoc->cookie_life.tv_usec += (stale % 1000) * 1000;
2155 break;
2156
2157 case SCTP_PARAM_HOST_NAME_ADDRESS:
2158 SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
2159 break;
2160
2161 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2162 /* Turn off the default values first so we'll know which
2163 * ones are really set by the peer.
2164 */
2165 asoc->peer.ipv4_address = 0;
2166 asoc->peer.ipv6_address = 0;
2167
2168 /* Cycle through address types; avoid divide by 0. */
2169 sat = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2170 if (sat)
2171 sat /= sizeof(__u16);
2172
2173 for (i = 0; i < sat; ++i) {
2174 switch (param.sat->types[i]) {
2175 case SCTP_PARAM_IPV4_ADDRESS:
2176 asoc->peer.ipv4_address = 1;
2177 break;
2178
2179 case SCTP_PARAM_IPV6_ADDRESS:
2180 asoc->peer.ipv6_address = 1;
2181 break;
2182
2183 case SCTP_PARAM_HOST_NAME_ADDRESS:
2184 asoc->peer.hostname_address = 1;
2185 break;
2186
2187 default: /* Just ignore anything else. */
2188 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 }
2191 break;
2192
2193 case SCTP_PARAM_STATE_COOKIE:
2194 asoc->peer.cookie_len =
2195 ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2196 asoc->peer.cookie = param.cookie->body;
2197 break;
2198
2199 case SCTP_PARAM_HEARTBEAT_INFO:
2200 /* Would be odd to receive, but it causes no problems. */
2201 break;
2202
2203 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2204 /* Rejected during verify stage. */
2205 break;
2206
2207 case SCTP_PARAM_ECN_CAPABLE:
2208 asoc->peer.ecn_capable = 1;
2209 break;
2210
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002211 case SCTP_PARAM_ADAPTATION_LAYER_IND:
2212 asoc->peer.adaptation_ind = param.aind->adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 break;
2214
Vlad Yasevich131a47e2007-09-16 15:53:56 -07002215 case SCTP_PARAM_SUPPORTED_EXT:
2216 sctp_process_ext_param(asoc, param);
2217 break;
2218
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 case SCTP_PARAM_FWD_TSN_SUPPORT:
2220 if (sctp_prsctp_enable) {
2221 asoc->peer.prsctp_capable = 1;
2222 break;
2223 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002224 /* Fall Through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 default:
2226 /* Any unrecognized parameters should have been caught
2227 * and handled by sctp_verify_param() which should be
2228 * called prior to this routine. Simply log the error
2229 * here.
2230 */
2231 SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
2232 ntohs(param.p->type), asoc);
2233 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
2236 return retval;
2237}
2238
2239/* Select a new verification tag. */
2240__u32 sctp_generate_tag(const struct sctp_endpoint *ep)
2241{
2242 /* I believe that this random number generator complies with RFC1750.
2243 * A tag of 0 is reserved for special cases (e.g. INIT).
2244 */
2245 __u32 x;
2246
2247 do {
2248 get_random_bytes(&x, sizeof(__u32));
2249 } while (x == 0);
2250
2251 return x;
2252}
2253
2254/* Select an initial TSN to send during startup. */
2255__u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
2256{
2257 __u32 retval;
2258
2259 get_random_bytes(&retval, sizeof(__u32));
2260 return retval;
2261}
2262
2263/*
2264 * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
2265 * 0 1 2 3
2266 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
2267 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2268 * | Type = 0xC1 | Chunk Flags | Chunk Length |
2269 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2270 * | Serial Number |
2271 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2272 * | Address Parameter |
2273 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2274 * | ASCONF Parameter #1 |
2275 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2276 * \ \
2277 * / .... /
2278 * \ \
2279 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2280 * | ASCONF Parameter #N |
2281 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2282 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002283 * Address Parameter and other parameter will not be wrapped in this function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 */
2285static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
2286 union sctp_addr *addr,
2287 int vparam_len)
2288{
2289 sctp_addiphdr_t asconf;
2290 struct sctp_chunk *retval;
2291 int length = sizeof(asconf) + vparam_len;
2292 union sctp_addr_param addrparam;
2293 int addrlen;
2294 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2295
2296 addrlen = af->to_addr_param(addr, &addrparam);
2297 if (!addrlen)
2298 return NULL;
2299 length += addrlen;
2300
2301 /* Create the chunk. */
2302 retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF, 0, length);
2303 if (!retval)
2304 return NULL;
2305
2306 asconf.serial = htonl(asoc->addip_serial++);
2307
2308 retval->subh.addip_hdr =
2309 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2310 retval->param_hdr.v =
2311 sctp_addto_chunk(retval, addrlen, &addrparam);
2312
2313 return retval;
2314}
2315
2316/* ADDIP
2317 * 3.2.1 Add IP Address
2318 * 0 1 2 3
2319 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
2320 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2321 * | Type = 0xC001 | Length = Variable |
2322 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2323 * | ASCONF-Request Correlation ID |
2324 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2325 * | Address Parameter |
2326 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2327 *
2328 * 3.2.2 Delete IP Address
2329 * 0 1 2 3
2330 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
2331 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2332 * | Type = 0xC002 | Length = Variable |
2333 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2334 * | ASCONF-Request Correlation ID |
2335 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2336 * | Address Parameter |
2337 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2338 *
2339 */
2340struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
2341 union sctp_addr *laddr,
2342 struct sockaddr *addrs,
2343 int addrcnt,
Al Virodbc16db2006-11-20 17:01:42 -08002344 __be16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
2346 sctp_addip_param_t param;
2347 struct sctp_chunk *retval;
2348 union sctp_addr_param addr_param;
2349 union sctp_addr *addr;
2350 void *addr_buf;
2351 struct sctp_af *af;
2352 int paramlen = sizeof(param);
2353 int addr_param_len = 0;
2354 int totallen = 0;
2355 int i;
2356
2357 /* Get total length of all the address parameters. */
2358 addr_buf = addrs;
2359 for (i = 0; i < addrcnt; i++) {
2360 addr = (union sctp_addr *)addr_buf;
2361 af = sctp_get_af_specific(addr->v4.sin_family);
2362 addr_param_len = af->to_addr_param(addr, &addr_param);
2363
2364 totallen += paramlen;
2365 totallen += addr_param_len;
2366
2367 addr_buf += af->sockaddr_len;
2368 }
2369
2370 /* Create an asconf chunk with the required length. */
2371 retval = sctp_make_asconf(asoc, laddr, totallen);
2372 if (!retval)
2373 return NULL;
2374
2375 /* Add the address parameters to the asconf chunk. */
2376 addr_buf = addrs;
2377 for (i = 0; i < addrcnt; i++) {
2378 addr = (union sctp_addr *)addr_buf;
2379 af = sctp_get_af_specific(addr->v4.sin_family);
2380 addr_param_len = af->to_addr_param(addr, &addr_param);
2381 param.param_hdr.type = flags;
2382 param.param_hdr.length = htons(paramlen + addr_param_len);
2383 param.crr_id = i;
2384
2385 sctp_addto_chunk(retval, paramlen, &param);
2386 sctp_addto_chunk(retval, addr_param_len, &addr_param);
2387
2388 addr_buf += af->sockaddr_len;
2389 }
2390 return retval;
2391}
2392
2393/* ADDIP
2394 * 3.2.4 Set Primary IP Address
2395 * 0 1 2 3
2396 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
2397 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2398 * | Type =0xC004 | Length = Variable |
2399 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2400 * | ASCONF-Request Correlation ID |
2401 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2402 * | Address Parameter |
2403 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2404 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002405 * Create an ASCONF chunk with Set Primary IP address parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 */
2407struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
2408 union sctp_addr *addr)
2409{
2410 sctp_addip_param_t param;
2411 struct sctp_chunk *retval;
2412 int len = sizeof(param);
2413 union sctp_addr_param addrparam;
2414 int addrlen;
2415 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2416
2417 addrlen = af->to_addr_param(addr, &addrparam);
2418 if (!addrlen)
2419 return NULL;
2420 len += addrlen;
2421
2422 /* Create the chunk and make asconf header. */
2423 retval = sctp_make_asconf(asoc, addr, len);
2424 if (!retval)
2425 return NULL;
2426
2427 param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
2428 param.param_hdr.length = htons(len);
2429 param.crr_id = 0;
2430
2431 sctp_addto_chunk(retval, sizeof(param), &param);
2432 sctp_addto_chunk(retval, addrlen, &addrparam);
2433
2434 return retval;
2435}
2436
2437/* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2438 * 0 1 2 3
2439 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
2440 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2441 * | Type = 0x80 | Chunk Flags | Chunk Length |
2442 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2443 * | Serial Number |
2444 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2445 * | ASCONF Parameter Response#1 |
2446 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2447 * \ \
2448 * / .... /
2449 * \ \
2450 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2451 * | ASCONF Parameter Response#N |
2452 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2453 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002454 * Create an ASCONF_ACK chunk with enough space for the parameter responses.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 */
2456static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
2457 __u32 serial, int vparam_len)
2458{
2459 sctp_addiphdr_t asconf;
2460 struct sctp_chunk *retval;
2461 int length = sizeof(asconf) + vparam_len;
2462
2463 /* Create the chunk. */
2464 retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF_ACK, 0, length);
2465 if (!retval)
2466 return NULL;
2467
2468 asconf.serial = htonl(serial);
2469
2470 retval->subh.addip_hdr =
2471 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2472
2473 return retval;
2474}
2475
2476/* Add response parameters to an ASCONF_ACK chunk. */
Al Viro9f81bcd2006-11-20 17:26:34 -08002477static void sctp_add_asconf_response(struct sctp_chunk *chunk, __be32 crr_id,
Al Virodbc16db2006-11-20 17:01:42 -08002478 __be16 err_code, sctp_addip_param_t *asconf_param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479{
2480 sctp_addip_param_t ack_param;
2481 sctp_errhdr_t err_param;
2482 int asconf_param_len = 0;
2483 int err_param_len = 0;
Al Virodbc16db2006-11-20 17:01:42 -08002484 __be16 response_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
2486 if (SCTP_ERROR_NO_ERROR == err_code) {
2487 response_type = SCTP_PARAM_SUCCESS_REPORT;
2488 } else {
2489 response_type = SCTP_PARAM_ERR_CAUSE;
2490 err_param_len = sizeof(err_param);
2491 if (asconf_param)
2492 asconf_param_len =
2493 ntohs(asconf_param->param_hdr.length);
2494 }
2495
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002496 /* Add Success Indication or Error Cause Indication parameter. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 ack_param.param_hdr.type = response_type;
2498 ack_param.param_hdr.length = htons(sizeof(ack_param) +
2499 err_param_len +
2500 asconf_param_len);
2501 ack_param.crr_id = crr_id;
2502 sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
2503
2504 if (SCTP_ERROR_NO_ERROR == err_code)
2505 return;
2506
2507 /* Add Error Cause parameter. */
2508 err_param.cause = err_code;
2509 err_param.length = htons(err_param_len + asconf_param_len);
2510 sctp_addto_chunk(chunk, err_param_len, &err_param);
2511
2512 /* Add the failed TLV copied from ASCONF chunk. */
2513 if (asconf_param)
2514 sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
2515}
2516
2517/* Process a asconf parameter. */
Al Virodbc16db2006-11-20 17:01:42 -08002518static __be16 sctp_process_asconf_param(struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 struct sctp_chunk *asconf,
2520 sctp_addip_param_t *asconf_param)
2521{
2522 struct sctp_transport *peer;
2523 struct sctp_af *af;
2524 union sctp_addr addr;
2525 struct list_head *pos;
2526 union sctp_addr_param *addr_param;
Al Viro5f242a12006-11-20 17:05:23 -08002527
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 addr_param = (union sctp_addr_param *)
2529 ((void *)asconf_param + sizeof(sctp_addip_param_t));
2530
2531 af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2532 if (unlikely(!af))
2533 return SCTP_ERROR_INV_PARAM;
2534
Al Virodd86d132006-11-20 17:11:13 -08002535 af->from_addr_param(&addr, addr_param, htons(asoc->peer.port), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 switch (asconf_param->param_hdr.type) {
2537 case SCTP_PARAM_ADD_IP:
2538 /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002539 * request and does not have the local resources to add this
2540 * new address to the association, it MUST return an Error
2541 * Cause TLV set to the new error code 'Operation Refused
2542 * Due to Resource Shortage'.
2543 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Al Virodd86d132006-11-20 17:11:13 -08002545 peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC, SCTP_UNCONFIRMED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 if (!peer)
2547 return SCTP_ERROR_RSRC_LOW;
2548
2549 /* Start the heartbeat timer. */
2550 if (!mod_timer(&peer->hb_timer, sctp_transport_timeout(peer)))
2551 sctp_transport_hold(peer);
2552 break;
2553 case SCTP_PARAM_DEL_IP:
2554 /* ADDIP 4.3 D7) If a request is received to delete the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002555 * last remaining IP address of a peer endpoint, the receiver
2556 * MUST send an Error Cause TLV with the error cause set to the
2557 * new error code 'Request to Delete Last Remaining IP Address'.
2558 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 pos = asoc->peer.transport_addr_list.next;
2560 if (pos->next == &asoc->peer.transport_addr_list)
2561 return SCTP_ERROR_DEL_LAST_IP;
2562
2563 /* ADDIP 4.3 D8) If a request is received to delete an IP
2564 * address which is also the source address of the IP packet
2565 * which contained the ASCONF chunk, the receiver MUST reject
2566 * this request. To reject the request the receiver MUST send
2567 * an Error Cause TLV set to the new error code 'Request to
2568 * Delete Source IP Address'
2569 */
Al Viro6a1e5f32006-11-20 17:12:25 -08002570 if (sctp_cmp_addr_exact(sctp_source(asconf), &addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 return SCTP_ERROR_DEL_SRC_IP;
2572
Al Virodd86d132006-11-20 17:11:13 -08002573 sctp_assoc_del_peer(asoc, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 break;
2575 case SCTP_PARAM_SET_PRIMARY:
Al Virodd86d132006-11-20 17:11:13 -08002576 peer = sctp_assoc_lookup_paddr(asoc, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 if (!peer)
2578 return SCTP_ERROR_INV_PARAM;
2579
2580 sctp_assoc_set_primary(asoc, peer);
2581 break;
2582 default:
2583 return SCTP_ERROR_INV_PARAM;
2584 break;
2585 }
2586
2587 return SCTP_ERROR_NO_ERROR;
2588}
2589
Wei Yongjun6f4c6182007-09-19 17:19:52 +08002590/* Verify the ASCONF packet before we process it. */
2591int sctp_verify_asconf(const struct sctp_association *asoc,
2592 struct sctp_paramhdr *param_hdr, void *chunk_end,
2593 struct sctp_paramhdr **errp) {
2594 sctp_addip_param_t *asconf_param;
2595 union sctp_params param;
2596 int length, plen;
2597
2598 param.v = (sctp_paramhdr_t *) param_hdr;
2599 while (param.v <= chunk_end - sizeof(sctp_paramhdr_t)) {
2600 length = ntohs(param.p->length);
2601 *errp = param.p;
2602
2603 if (param.v > chunk_end - length ||
2604 length < sizeof(sctp_paramhdr_t))
2605 return 0;
2606
2607 switch (param.p->type) {
2608 case SCTP_PARAM_ADD_IP:
2609 case SCTP_PARAM_DEL_IP:
2610 case SCTP_PARAM_SET_PRIMARY:
2611 asconf_param = (sctp_addip_param_t *)param.v;
2612 plen = ntohs(asconf_param->param_hdr.length);
2613 if (plen < sizeof(sctp_addip_param_t) +
2614 sizeof(sctp_paramhdr_t))
2615 return 0;
2616 break;
2617 case SCTP_PARAM_SUCCESS_REPORT:
2618 case SCTP_PARAM_ADAPTATION_LAYER_IND:
2619 if (length != sizeof(sctp_addip_param_t))
2620 return 0;
2621
2622 break;
2623 default:
2624 break;
2625 }
2626
2627 param.v += WORD_ROUND(length);
2628 }
2629
2630 if (param.v != chunk_end)
2631 return 0;
2632
2633 return 1;
2634}
2635
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002636/* Process an incoming ASCONF chunk with the next expected serial no. and
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 * return an ASCONF_ACK chunk to be sent in response.
2638 */
2639struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
2640 struct sctp_chunk *asconf)
2641{
2642 sctp_addiphdr_t *hdr;
2643 union sctp_addr_param *addr_param;
2644 sctp_addip_param_t *asconf_param;
2645 struct sctp_chunk *asconf_ack;
2646
Al Virodbc16db2006-11-20 17:01:42 -08002647 __be16 err_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 int length = 0;
2649 int chunk_len = asconf->skb->len;
2650 __u32 serial;
2651 int all_param_pass = 1;
2652
2653 hdr = (sctp_addiphdr_t *)asconf->skb->data;
2654 serial = ntohl(hdr->serial);
2655
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002656 /* Skip the addiphdr and store a pointer to address parameter. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 length = sizeof(sctp_addiphdr_t);
2658 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
2659 chunk_len -= length;
2660
2661 /* Skip the address parameter and store a pointer to the first
2662 * asconf paramter.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002663 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 length = ntohs(addr_param->v4.param_hdr.length);
2665 asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
2666 chunk_len -= length;
2667
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002668 /* create an ASCONF_ACK chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 * Based on the definitions of parameters, we know that the size of
2670 * ASCONF_ACK parameters are less than or equal to the twice of ASCONF
2671 * paramters.
2672 */
2673 asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 2);
2674 if (!asconf_ack)
2675 goto done;
2676
2677 /* Process the TLVs contained within the ASCONF chunk. */
2678 while (chunk_len > 0) {
2679 err_code = sctp_process_asconf_param(asoc, asconf,
2680 asconf_param);
2681 /* ADDIP 4.1 A7)
2682 * If an error response is received for a TLV parameter,
2683 * all TLVs with no response before the failed TLV are
2684 * considered successful if not reported. All TLVs after
2685 * the failed response are considered unsuccessful unless
2686 * a specific success indication is present for the parameter.
2687 */
2688 if (SCTP_ERROR_NO_ERROR != err_code)
2689 all_param_pass = 0;
2690
2691 if (!all_param_pass)
2692 sctp_add_asconf_response(asconf_ack,
2693 asconf_param->crr_id, err_code,
2694 asconf_param);
2695
2696 /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
2697 * an IP address sends an 'Out of Resource' in its response, it
2698 * MUST also fail any subsequent add or delete requests bundled
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002699 * in the ASCONF.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 */
2701 if (SCTP_ERROR_RSRC_LOW == err_code)
2702 goto done;
2703
2704 /* Move to the next ASCONF param. */
2705 length = ntohs(asconf_param->param_hdr.length);
2706 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
2707 length);
2708 chunk_len -= length;
2709 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711done:
2712 asoc->peer.addip_serial++;
2713
2714 /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002715 * after freeing the reference to old asconf ack if any.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 */
2717 if (asconf_ack) {
2718 if (asoc->addip_last_asconf_ack)
2719 sctp_chunk_free(asoc->addip_last_asconf_ack);
2720
2721 sctp_chunk_hold(asconf_ack);
2722 asoc->addip_last_asconf_ack = asconf_ack;
2723 }
2724
2725 return asconf_ack;
2726}
2727
2728/* Process a asconf parameter that is successfully acked. */
2729static int sctp_asconf_param_success(struct sctp_association *asoc,
2730 sctp_addip_param_t *asconf_param)
2731{
2732 struct sctp_af *af;
2733 union sctp_addr addr;
2734 struct sctp_bind_addr *bp = &asoc->base.bind_addr;
2735 union sctp_addr_param *addr_param;
2736 struct list_head *pos;
2737 struct sctp_transport *transport;
Sridhar Samudraladc022a92006-07-21 14:49:25 -07002738 struct sctp_sockaddr_entry *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 int retval = 0;
2740
2741 addr_param = (union sctp_addr_param *)
2742 ((void *)asconf_param + sizeof(sctp_addip_param_t));
2743
2744 /* We have checked the packet before, so we do not check again. */
2745 af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
Al Virodd86d132006-11-20 17:11:13 -08002746 af->from_addr_param(&addr, addr_param, htons(bp->port), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
2748 switch (asconf_param->param_hdr.type) {
2749 case SCTP_PARAM_ADD_IP:
Vlad Yasevich559cf712007-09-16 16:03:28 -07002750 /* This is always done in BH context with a socket lock
2751 * held, so the list can not change.
2752 */
2753 list_for_each_entry(saddr, &bp->address_list, list) {
Al Virodd86d132006-11-20 17:11:13 -08002754 if (sctp_cmp_addr_exact(&saddr->a, &addr))
Sridhar Samudraladc022a92006-07-21 14:49:25 -07002755 saddr->use_as_src = 1;
2756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 break;
2758 case SCTP_PARAM_DEL_IP:
Vlad Yasevich559cf712007-09-16 16:03:28 -07002759 retval = sctp_del_bind_addr(bp, &addr, call_rcu_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 list_for_each(pos, &asoc->peer.transport_addr_list) {
2761 transport = list_entry(pos, struct sctp_transport,
2762 transports);
Sridhar Samudraladc022a92006-07-21 14:49:25 -07002763 dst_release(transport->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 sctp_transport_route(transport, NULL,
2765 sctp_sk(asoc->base.sk));
2766 }
2767 break;
2768 default:
2769 break;
2770 }
2771
2772 return retval;
2773}
2774
2775/* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
2776 * for the given asconf parameter. If there is no response for this parameter,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002777 * return the error code based on the third argument 'no_err'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 * ADDIP 4.1
2779 * A7) If an error response is received for a TLV parameter, all TLVs with no
2780 * response before the failed TLV are considered successful if not reported.
2781 * All TLVs after the failed response are considered unsuccessful unless a
2782 * specific success indication is present for the parameter.
2783 */
Al Virodbc16db2006-11-20 17:01:42 -08002784static __be16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 sctp_addip_param_t *asconf_param,
2786 int no_err)
2787{
2788 sctp_addip_param_t *asconf_ack_param;
2789 sctp_errhdr_t *err_param;
2790 int length;
2791 int asconf_ack_len = asconf_ack->skb->len;
Al Virodbc16db2006-11-20 17:01:42 -08002792 __be16 err_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
2794 if (no_err)
2795 err_code = SCTP_ERROR_NO_ERROR;
2796 else
2797 err_code = SCTP_ERROR_REQ_REFUSED;
2798
2799 /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
2800 * the first asconf_ack parameter.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002801 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 length = sizeof(sctp_addiphdr_t);
2803 asconf_ack_param = (sctp_addip_param_t *)(asconf_ack->skb->data +
2804 length);
2805 asconf_ack_len -= length;
2806
2807 while (asconf_ack_len > 0) {
2808 if (asconf_ack_param->crr_id == asconf_param->crr_id) {
2809 switch(asconf_ack_param->param_hdr.type) {
2810 case SCTP_PARAM_SUCCESS_REPORT:
2811 return SCTP_ERROR_NO_ERROR;
2812 case SCTP_PARAM_ERR_CAUSE:
2813 length = sizeof(sctp_addip_param_t);
2814 err_param = (sctp_errhdr_t *)
2815 ((void *)asconf_ack_param + length);
2816 asconf_ack_len -= length;
2817 if (asconf_ack_len > 0)
2818 return err_param->cause;
2819 else
2820 return SCTP_ERROR_INV_PARAM;
2821 break;
2822 default:
2823 return SCTP_ERROR_INV_PARAM;
2824 }
2825 }
2826
2827 length = ntohs(asconf_ack_param->param_hdr.length);
2828 asconf_ack_param = (sctp_addip_param_t *)
2829 ((void *)asconf_ack_param + length);
2830 asconf_ack_len -= length;
2831 }
2832
2833 return err_code;
2834}
2835
2836/* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
2837int sctp_process_asconf_ack(struct sctp_association *asoc,
2838 struct sctp_chunk *asconf_ack)
2839{
2840 struct sctp_chunk *asconf = asoc->addip_last_asconf;
2841 union sctp_addr_param *addr_param;
2842 sctp_addip_param_t *asconf_param;
2843 int length = 0;
2844 int asconf_len = asconf->skb->len;
2845 int all_param_pass = 0;
2846 int no_err = 1;
2847 int retval = 0;
Al Virodbc16db2006-11-20 17:01:42 -08002848 __be16 err_code = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
2850 /* Skip the chunkhdr and addiphdr from the last asconf sent and store
2851 * a pointer to address parameter.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002852 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 length = sizeof(sctp_addip_chunk_t);
2854 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
2855 asconf_len -= length;
2856
2857 /* Skip the address parameter in the last asconf sent and store a
2858 * pointer to the first asconf paramter.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002859 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 length = ntohs(addr_param->v4.param_hdr.length);
2861 asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
2862 asconf_len -= length;
2863
2864 /* ADDIP 4.1
2865 * A8) If there is no response(s) to specific TLV parameter(s), and no
2866 * failures are indicated, then all request(s) are considered
2867 * successful.
2868 */
2869 if (asconf_ack->skb->len == sizeof(sctp_addiphdr_t))
2870 all_param_pass = 1;
2871
2872 /* Process the TLVs contained in the last sent ASCONF chunk. */
2873 while (asconf_len > 0) {
2874 if (all_param_pass)
2875 err_code = SCTP_ERROR_NO_ERROR;
2876 else {
2877 err_code = sctp_get_asconf_response(asconf_ack,
2878 asconf_param,
2879 no_err);
2880 if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
2881 no_err = 0;
2882 }
2883
2884 switch (err_code) {
2885 case SCTP_ERROR_NO_ERROR:
2886 retval = sctp_asconf_param_success(asoc, asconf_param);
2887 break;
2888
2889 case SCTP_ERROR_RSRC_LOW:
2890 retval = 1;
2891 break;
2892
2893 case SCTP_ERROR_INV_PARAM:
2894 /* Disable sending this type of asconf parameter in
2895 * future.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002896 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 asoc->peer.addip_disabled_mask |=
2898 asconf_param->param_hdr.type;
2899 break;
2900
2901 case SCTP_ERROR_REQ_REFUSED:
2902 case SCTP_ERROR_DEL_LAST_IP:
2903 case SCTP_ERROR_DEL_SRC_IP:
2904 default:
2905 break;
2906 }
2907
2908 /* Skip the processed asconf parameter and move to the next
2909 * one.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002910 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 length = ntohs(asconf_param->param_hdr.length);
2912 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
2913 length);
2914 asconf_len -= length;
2915 }
2916
2917 /* Free the cached last sent asconf chunk. */
2918 sctp_chunk_free(asconf);
2919 asoc->addip_last_asconf = NULL;
2920
2921 /* Send the next asconf chunk from the addip chunk queue. */
David S. Miller79af02c2005-07-08 21:47:49 -07002922 if (!list_empty(&asoc->addip_chunk_list)) {
2923 struct list_head *entry = asoc->addip_chunk_list.next;
2924 asconf = list_entry(entry, struct sctp_chunk, list);
2925
2926 list_del_init(entry);
2927
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 /* Hold the chunk until an ASCONF_ACK is received. */
2929 sctp_chunk_hold(asconf);
2930 if (sctp_primitive_ASCONF(asoc, asconf))
2931 sctp_chunk_free(asconf);
2932 else
2933 asoc->addip_last_asconf = asconf;
2934 }
2935
2936 return retval;
2937}
2938
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002939/* Make a FWD TSN chunk. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
2941 __u32 new_cum_tsn, size_t nstreams,
2942 struct sctp_fwdtsn_skip *skiplist)
2943{
2944 struct sctp_chunk *retval = NULL;
2945 struct sctp_fwdtsn_chunk *ftsn_chunk;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002946 struct sctp_fwdtsn_hdr ftsn_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 struct sctp_fwdtsn_skip skip;
2948 size_t hint;
2949 int i;
2950
2951 hint = (nstreams + 1) * sizeof(__u32);
2952
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 retval = sctp_make_chunk(asoc, SCTP_CID_FWD_TSN, 0, hint);
2954
2955 if (!retval)
2956 return NULL;
2957
2958 ftsn_chunk = (struct sctp_fwdtsn_chunk *)retval->subh.fwdtsn_hdr;
2959
2960 ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
2961 retval->subh.fwdtsn_hdr =
2962 sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
2963
2964 for (i = 0; i < nstreams; i++) {
2965 skip.stream = skiplist[i].stream;
2966 skip.ssn = skiplist[i].ssn;
2967 sctp_addto_chunk(retval, sizeof(skip), &skip);
2968 }
2969
2970 return retval;
2971}