blob: 23ae37ec871167d17c37ddbdbb9d4095e26a4575 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 /* RFC 2960 3.3.2 Initiation (INIT) (1)
184 *
185 * Note 1: The INIT chunks can contain multiple addresses that
186 * can be IPv4 and/or IPv6 in any combination.
187 */
188 retval = NULL;
189
190 /* Convert the provided bind address list to raw format. */
191 addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, gfp);
192
193 init.init_tag = htonl(asoc->c.my_vtag);
194 init.a_rwnd = htonl(asoc->rwnd);
195 init.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
196 init.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
197 init.initial_tsn = htonl(asoc->c.initial_tsn);
198
199 /* How many address types are needed? */
200 sp = sctp_sk(asoc->base.sk);
201 num_types = sp->pf->supported_addrs(sp, types);
202
203 chunksize = sizeof(init) + addrs_len + SCTP_SAT_LEN(num_types);
204 chunksize += sizeof(ecap_param);
205 if (sctp_prsctp_enable)
206 chunksize += sizeof(prsctp_param);
207 chunksize += sizeof(aiparam);
208 chunksize += vparam_len;
209
210 /* RFC 2960 3.3.2 Initiation (INIT) (1)
211 *
212 * Note 3: An INIT chunk MUST NOT contain more than one Host
213 * Name address parameter. Moreover, the sender of the INIT
214 * MUST NOT combine any other address types with the Host Name
215 * address in the INIT. The receiver of INIT MUST ignore any
216 * other address types if the Host Name address parameter is
217 * present in the received INIT chunk.
218 *
219 * PLEASE DO NOT FIXME [This version does not support Host Name.]
220 */
221
222 retval = sctp_make_chunk(asoc, SCTP_CID_INIT, 0, chunksize);
223 if (!retval)
224 goto nodata;
225
226 retval->subh.init_hdr =
227 sctp_addto_chunk(retval, sizeof(init), &init);
228 retval->param_hdr.v =
229 sctp_addto_chunk(retval, addrs_len, addrs.v);
230
231 /* RFC 2960 3.3.2 Initiation (INIT) (1)
232 *
233 * Note 4: This parameter, when present, specifies all the
234 * address types the sending endpoint can support. The absence
235 * of this parameter indicates that the sending endpoint can
236 * support any address type.
237 */
238 sat.param_hdr.type = SCTP_PARAM_SUPPORTED_ADDRESS_TYPES;
239 sat.param_hdr.length = htons(SCTP_SAT_LEN(num_types));
240 sctp_addto_chunk(retval, sizeof(sat), &sat);
241 sctp_addto_chunk(retval, num_types * sizeof(__u16), &types);
242
243 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
244 if (sctp_prsctp_enable)
245 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800246 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 aiparam.param_hdr.length = htons(sizeof(aiparam));
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800248 aiparam.adaptation_ind = htonl(sp->adaptation_ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
250nodata:
Jesper Juhla51482b2005-11-08 09:41:34 -0800251 kfree(addrs.v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return retval;
253}
254
255struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
256 const struct sctp_chunk *chunk,
Al Virodd0fc662005-10-07 07:46:04 +0100257 gfp_t gfp, int unkparam_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 sctp_inithdr_t initack;
260 struct sctp_chunk *retval;
261 union sctp_params addrs;
262 int addrs_len;
263 sctp_cookie_param_t *cookie;
264 int cookie_len;
265 size_t chunksize;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800266 sctp_adaptation_ind_param_t aiparam;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 retval = NULL;
269
270 /* Note: there may be no addresses to embed. */
271 addrs = sctp_bind_addrs_to_raw(&asoc->base.bind_addr, &addrs_len, gfp);
272
273 initack.init_tag = htonl(asoc->c.my_vtag);
274 initack.a_rwnd = htonl(asoc->rwnd);
275 initack.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
276 initack.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
277 initack.initial_tsn = htonl(asoc->c.initial_tsn);
278
279 /* FIXME: We really ought to build the cookie right
280 * into the packet instead of allocating more fresh memory.
281 */
282 cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
283 addrs.v, addrs_len);
284 if (!cookie)
285 goto nomem_cookie;
286
287 /* Calculate the total size of allocation, include the reserved
288 * space for reporting unknown parameters if it is specified.
289 */
290 chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;
291
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900292 /* Tell peer that we'll do ECN only if peer advertised such cap. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (asoc->peer.ecn_capable)
294 chunksize += sizeof(ecap_param);
295
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900296 /* Tell peer that we'll do PR-SCTP only if peer advertised. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (asoc->peer.prsctp_capable)
298 chunksize += sizeof(prsctp_param);
299
300 chunksize += sizeof(aiparam);
301
302 /* Now allocate and fill out the chunk. */
303 retval = sctp_make_chunk(asoc, SCTP_CID_INIT_ACK, 0, chunksize);
304 if (!retval)
305 goto nomem_chunk;
306
307 /* Per the advice in RFC 2960 6.4, send this reply to
308 * the source of the INIT packet.
309 */
310 retval->transport = chunk->transport;
311 retval->subh.init_hdr =
312 sctp_addto_chunk(retval, sizeof(initack), &initack);
313 retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
314 sctp_addto_chunk(retval, cookie_len, cookie);
315 if (asoc->peer.ecn_capable)
316 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
317 if (asoc->peer.prsctp_capable)
318 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
319
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800320 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 aiparam.param_hdr.length = htons(sizeof(aiparam));
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800322 aiparam.adaptation_ind = htonl(sctp_sk(asoc->base.sk)->adaptation_ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
324
325 /* We need to remove the const qualifier at this point. */
326 retval->asoc = (struct sctp_association *) asoc;
327
328 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
329 *
330 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
331 * HEARTBEAT ACK, * etc.) to the same destination transport
332 * address from which it received the DATA or control chunk
333 * to which it is replying.
334 *
335 * [INIT ACK back to where the INIT came from.]
336 */
337 if (chunk)
338 retval->transport = chunk->transport;
339
340nomem_chunk:
341 kfree(cookie);
342nomem_cookie:
Jesper Juhla51482b2005-11-08 09:41:34 -0800343 kfree(addrs.v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return retval;
345}
346
347/* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
348 *
349 * This chunk is used only during the initialization of an association.
350 * It is sent by the initiator of an association to its peer to complete
351 * the initialization process. This chunk MUST precede any DATA chunk
352 * sent within the association, but MAY be bundled with one or more DATA
353 * chunks in the same packet.
354 *
355 * 0 1 2 3
356 * 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
357 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
358 * | Type = 10 |Chunk Flags | Length |
359 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
360 * / Cookie /
361 * \ \
362 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
363 *
364 * Chunk Flags: 8 bit
365 *
366 * Set to zero on transmit and ignored on receipt.
367 *
368 * Length: 16 bits (unsigned integer)
369 *
370 * Set to the size of the chunk in bytes, including the 4 bytes of
371 * the chunk header and the size of the Cookie.
372 *
373 * Cookie: variable size
374 *
375 * This field must contain the exact cookie received in the
376 * State Cookie parameter from the previous INIT ACK.
377 *
378 * An implementation SHOULD make the cookie as small as possible
379 * to insure interoperability.
380 */
381struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
382 const struct sctp_chunk *chunk)
383{
384 struct sctp_chunk *retval;
385 void *cookie;
386 int cookie_len;
387
388 cookie = asoc->peer.cookie;
389 cookie_len = asoc->peer.cookie_len;
390
391 /* Build a cookie echo chunk. */
392 retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ECHO, 0, cookie_len);
393 if (!retval)
394 goto nodata;
395 retval->subh.cookie_hdr =
396 sctp_addto_chunk(retval, cookie_len, cookie);
397
398 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
399 *
400 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
401 * HEARTBEAT ACK, * etc.) to the same destination transport
402 * address from which it * received the DATA or control chunk
403 * to which it is replying.
404 *
405 * [COOKIE ECHO back to where the INIT ACK came from.]
406 */
407 if (chunk)
408 retval->transport = chunk->transport;
409
410nodata:
411 return retval;
412}
413
414/* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
415 *
416 * This chunk is used only during the initialization of an
417 * association. It is used to acknowledge the receipt of a COOKIE
418 * ECHO chunk. This chunk MUST precede any DATA or SACK chunk sent
419 * within the association, but MAY be bundled with one or more DATA
420 * chunks or SACK chunk in the same SCTP packet.
421 *
422 * 0 1 2 3
423 * 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
424 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
425 * | Type = 11 |Chunk Flags | Length = 4 |
426 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
427 *
428 * Chunk Flags: 8 bits
429 *
430 * Set to zero on transmit and ignored on receipt.
431 */
432struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
433 const struct sctp_chunk *chunk)
434{
435 struct sctp_chunk *retval;
436
437 retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ACK, 0, 0);
438
439 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
440 *
441 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
442 * HEARTBEAT ACK, * etc.) to the same destination transport
443 * address from which it * received the DATA or control chunk
444 * to which it is replying.
445 *
446 * [COOKIE ACK back to where the COOKIE ECHO came from.]
447 */
448 if (retval && chunk)
449 retval->transport = chunk->transport;
450
451 return retval;
452}
453
454/*
455 * Appendix A: Explicit Congestion Notification:
456 * CWR:
457 *
458 * RFC 2481 details a specific bit for a sender to send in the header of
459 * its next outbound TCP segment to indicate to its peer that it has
460 * reduced its congestion window. This is termed the CWR bit. For
461 * SCTP the same indication is made by including the CWR chunk.
462 * This chunk contains one data element, i.e. the TSN number that
463 * was sent in the ECNE chunk. This element represents the lowest
464 * TSN number in the datagram that was originally marked with the
465 * CE bit.
466 *
467 * 0 1 2 3
468 * 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
469 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
470 * | Chunk Type=13 | Flags=00000000| Chunk Length = 8 |
471 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
472 * | Lowest TSN Number |
473 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
474 *
475 * Note: The CWR is considered a Control chunk.
476 */
477struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
478 const __u32 lowest_tsn,
479 const struct sctp_chunk *chunk)
480{
481 struct sctp_chunk *retval;
482 sctp_cwrhdr_t cwr;
483
484 cwr.lowest_tsn = htonl(lowest_tsn);
485 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_CWR, 0,
486 sizeof(sctp_cwrhdr_t));
487
488 if (!retval)
489 goto nodata;
490
491 retval->subh.ecn_cwr_hdr =
492 sctp_addto_chunk(retval, sizeof(cwr), &cwr);
493
494 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
495 *
496 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
497 * HEARTBEAT ACK, * etc.) to the same destination transport
498 * address from which it * received the DATA or control chunk
499 * to which it is replying.
500 *
501 * [Report a reduced congestion window back to where the ECNE
502 * came from.]
503 */
504 if (chunk)
505 retval->transport = chunk->transport;
506
507nodata:
508 return retval;
509}
510
511/* Make an ECNE chunk. This is a congestion experienced report. */
512struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
513 const __u32 lowest_tsn)
514{
515 struct sctp_chunk *retval;
516 sctp_ecnehdr_t ecne;
517
518 ecne.lowest_tsn = htonl(lowest_tsn);
519 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_ECNE, 0,
520 sizeof(sctp_ecnehdr_t));
521 if (!retval)
522 goto nodata;
523 retval->subh.ecne_hdr =
524 sctp_addto_chunk(retval, sizeof(ecne), &ecne);
525
526nodata:
527 return retval;
528}
529
530/* Make a DATA chunk for the given association from the provided
531 * parameters. However, do not populate the data payload.
532 */
533struct sctp_chunk *sctp_make_datafrag_empty(struct sctp_association *asoc,
534 const struct sctp_sndrcvinfo *sinfo,
535 int data_len, __u8 flags, __u16 ssn)
536{
537 struct sctp_chunk *retval;
538 struct sctp_datahdr dp;
539 int chunk_len;
540
541 /* We assign the TSN as LATE as possible, not here when
542 * creating the chunk.
543 */
544 dp.tsn = 0;
545 dp.stream = htons(sinfo->sinfo_stream);
546 dp.ppid = sinfo->sinfo_ppid;
547
548 /* Set the flags for an unordered send. */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -0700549 if (sinfo->sinfo_flags & SCTP_UNORDERED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 flags |= SCTP_DATA_UNORDERED;
551 dp.ssn = 0;
552 } else
553 dp.ssn = htons(ssn);
554
555 chunk_len = sizeof(dp) + data_len;
556 retval = sctp_make_chunk(asoc, SCTP_CID_DATA, flags, chunk_len);
557 if (!retval)
558 goto nodata;
559
560 retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
561 memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
562
563nodata:
564 return retval;
565}
566
567/* Create a selective ackowledgement (SACK) for the given
568 * association. This reports on which TSN's we've seen to date,
569 * including duplicates and gaps.
570 */
571struct sctp_chunk *sctp_make_sack(const struct sctp_association *asoc)
572{
573 struct sctp_chunk *retval;
574 struct sctp_sackhdr sack;
575 int len;
576 __u32 ctsn;
577 __u16 num_gabs, num_dup_tsns;
578 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
579
580 ctsn = sctp_tsnmap_get_ctsn(map);
581 SCTP_DEBUG_PRINTK("sackCTSNAck sent: 0x%x.\n", ctsn);
582
583 /* How much room is needed in the chunk? */
584 num_gabs = sctp_tsnmap_num_gabs(map);
585 num_dup_tsns = sctp_tsnmap_num_dups(map);
586
587 /* Initialize the SACK header. */
588 sack.cum_tsn_ack = htonl(ctsn);
589 sack.a_rwnd = htonl(asoc->a_rwnd);
590 sack.num_gap_ack_blocks = htons(num_gabs);
591 sack.num_dup_tsns = htons(num_dup_tsns);
592
593 len = sizeof(sack)
594 + sizeof(struct sctp_gap_ack_block) * num_gabs
595 + sizeof(__u32) * num_dup_tsns;
596
597 /* Create the chunk. */
598 retval = sctp_make_chunk(asoc, SCTP_CID_SACK, 0, len);
599 if (!retval)
600 goto nodata;
601
602 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
603 *
604 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
605 * HEARTBEAT ACK, etc.) to the same destination transport
606 * address from which it received the DATA or control chunk to
607 * which it is replying. This rule should also be followed if
608 * the endpoint is bundling DATA chunks together with the
609 * reply chunk.
610 *
611 * However, when acknowledging multiple DATA chunks received
612 * in packets from different source addresses in a single
613 * SACK, the SACK chunk may be transmitted to one of the
614 * destination transport addresses from which the DATA or
615 * control chunks being acknowledged were received.
616 *
617 * [BUG: We do not implement the following paragraph.
618 * Perhaps we should remember the last transport we used for a
619 * SACK and avoid that (if possible) if we have seen any
620 * duplicates. --piggy]
621 *
622 * When a receiver of a duplicate DATA chunk sends a SACK to a
623 * multi- homed endpoint it MAY be beneficial to vary the
624 * destination address and not use the source address of the
625 * DATA chunk. The reason being that receiving a duplicate
626 * from a multi-homed endpoint might indicate that the return
627 * path (as specified in the source address of the DATA chunk)
628 * for the SACK is broken.
629 *
630 * [Send to the address from which we last received a DATA chunk.]
631 */
632 retval->transport = asoc->peer.last_data_from;
633
634 retval->subh.sack_hdr =
635 sctp_addto_chunk(retval, sizeof(sack), &sack);
636
637 /* Add the gap ack block information. */
638 if (num_gabs)
639 sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
640 sctp_tsnmap_get_gabs(map));
641
642 /* Add the duplicate TSN information. */
643 if (num_dup_tsns)
644 sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
645 sctp_tsnmap_get_dups(map));
646
647nodata:
648 return retval;
649}
650
651/* Make a SHUTDOWN chunk. */
652struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
653 const struct sctp_chunk *chunk)
654{
655 struct sctp_chunk *retval;
656 sctp_shutdownhdr_t shut;
657 __u32 ctsn;
658
659 ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
660 shut.cum_tsn_ack = htonl(ctsn);
661
662 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN, 0,
663 sizeof(sctp_shutdownhdr_t));
664 if (!retval)
665 goto nodata;
666
667 retval->subh.shutdown_hdr =
668 sctp_addto_chunk(retval, sizeof(shut), &shut);
669
670 if (chunk)
671 retval->transport = chunk->transport;
672nodata:
673 return retval;
674}
675
676struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
677 const struct sctp_chunk *chunk)
678{
679 struct sctp_chunk *retval;
680
681 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0);
682
683 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
684 *
685 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
686 * HEARTBEAT ACK, * etc.) to the same destination transport
687 * address from which it * received the DATA or control chunk
688 * to which it is replying.
689 *
690 * [ACK back to where the SHUTDOWN came from.]
691 */
692 if (retval && chunk)
693 retval->transport = chunk->transport;
694
695 return retval;
696}
697
698struct sctp_chunk *sctp_make_shutdown_complete(
699 const struct sctp_association *asoc,
700 const struct sctp_chunk *chunk)
701{
702 struct sctp_chunk *retval;
703 __u8 flags = 0;
704
Jerome Forissier047a2422005-04-28 11:58:43 -0700705 /* Set the T-bit if we have no association (vtag will be
706 * reflected)
707 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
709
710 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags, 0);
711
712 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
713 *
714 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
715 * HEARTBEAT ACK, * etc.) to the same destination transport
716 * address from which it * received the DATA or control chunk
717 * to which it is replying.
718 *
719 * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
720 * came from.]
721 */
722 if (retval && chunk)
723 retval->transport = chunk->transport;
724
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900725 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
728/* Create an ABORT. Note that we set the T bit if we have no
Jerome Forissier047a2422005-04-28 11:58:43 -0700729 * association, except when responding to an INIT (sctpimpguide 2.41).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 */
731struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
732 const struct sctp_chunk *chunk,
733 const size_t hint)
734{
735 struct sctp_chunk *retval;
736 __u8 flags = 0;
737
Jerome Forissier047a2422005-04-28 11:58:43 -0700738 /* Set the T-bit if we have no association and 'chunk' is not
739 * an INIT (vtag will be reflected).
740 */
741 if (!asoc) {
742 if (chunk && chunk->chunk_hdr &&
743 chunk->chunk_hdr->type == SCTP_CID_INIT)
744 flags = 0;
745 else
746 flags = SCTP_CHUNK_FLAG_T;
747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 retval = sctp_make_chunk(asoc, SCTP_CID_ABORT, flags, hint);
750
751 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
752 *
753 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
754 * HEARTBEAT ACK, * etc.) to the same destination transport
755 * address from which it * received the DATA or control chunk
756 * to which it is replying.
757 *
758 * [ABORT back to where the offender came from.]
759 */
760 if (retval && chunk)
761 retval->transport = chunk->transport;
762
763 return retval;
764}
765
766/* Helper to create ABORT with a NO_USER_DATA error. */
767struct sctp_chunk *sctp_make_abort_no_data(
768 const struct sctp_association *asoc,
769 const struct sctp_chunk *chunk, __u32 tsn)
770{
771 struct sctp_chunk *retval;
Al Viro9f81bcd2006-11-20 17:26:34 -0800772 __be32 payload;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t)
775 + sizeof(tsn));
776
777 if (!retval)
778 goto no_mem;
779
780 /* Put the tsn back into network byte order. */
781 payload = htonl(tsn);
Wei Yongjun00f1c2d2007-08-21 15:50:01 +0800782 sctp_init_cause(retval, SCTP_ERROR_NO_DATA, sizeof(payload));
783 sctp_addto_chunk(retval, sizeof(payload), (const void *)&payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
786 *
787 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
788 * HEARTBEAT ACK, * etc.) to the same destination transport
789 * address from which it * received the DATA or control chunk
790 * to which it is replying.
791 *
792 * [ABORT back to where the offender came from.]
793 */
794 if (chunk)
795 retval->transport = chunk->transport;
796
797no_mem:
798 return retval;
799}
800
801/* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error. */
802struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
Sridhar Samudralac164a9b2006-08-22 11:50:39 -0700803 const struct msghdr *msg,
804 size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
806 struct sctp_chunk *retval;
Sridhar Samudralac164a9b2006-08-22 11:50:39 -0700807 void *payload = NULL;
808 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Sridhar Samudralac164a9b2006-08-22 11:50:39 -0700810 retval = sctp_make_abort(asoc, NULL, sizeof(sctp_errhdr_t) + paylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (!retval)
812 goto err_chunk;
813
814 if (paylen) {
815 /* Put the msg_iov together into payload. */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -0700816 payload = kmalloc(paylen, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (!payload)
818 goto err_payload;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Sridhar Samudralac164a9b2006-08-22 11:50:39 -0700820 err = memcpy_fromiovec(payload, msg->msg_iov, paylen);
821 if (err < 0)
822 goto err_copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824
Wei Yongjun00f1c2d2007-08-21 15:50:01 +0800825 sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, paylen);
826 sctp_addto_chunk(retval, paylen, payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 if (paylen)
829 kfree(payload);
830
831 return retval;
832
833err_copy:
834 kfree(payload);
835err_payload:
836 sctp_chunk_free(retval);
837 retval = NULL;
838err_chunk:
839 return retval;
840}
841
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900842/* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843struct sctp_chunk *sctp_make_abort_violation(
844 const struct sctp_association *asoc,
845 const struct sctp_chunk *chunk,
846 const __u8 *payload,
847 const size_t paylen)
848{
849 struct sctp_chunk *retval;
850 struct sctp_paramhdr phdr;
851
852 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t) + paylen
Wei Yongjun00f1c2d2007-08-21 15:50:01 +0800853 + sizeof(sctp_paramhdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (!retval)
855 goto end;
856
Wei Yongjun00f1c2d2007-08-21 15:50:01 +0800857 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, paylen
858 + sizeof(sctp_paramhdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 phdr.type = htons(chunk->chunk_hdr->type);
861 phdr.length = chunk->chunk_hdr->length;
Wei Yongjun00f1c2d2007-08-21 15:50:01 +0800862 sctp_addto_chunk(retval, paylen, payload);
863 sctp_addto_param(retval, sizeof(sctp_paramhdr_t), &phdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865end:
866 return retval;
867}
868
869/* Make a HEARTBEAT chunk. */
870struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
871 const struct sctp_transport *transport,
872 const void *payload, const size_t paylen)
873{
874 struct sctp_chunk *retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT,
875 0, paylen);
876
877 if (!retval)
878 goto nodata;
879
880 /* Cast away the 'const', as this is just telling the chunk
881 * what transport it belongs to.
882 */
883 retval->transport = (struct sctp_transport *) transport;
884 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
885
886nodata:
887 return retval;
888}
889
890struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
891 const struct sctp_chunk *chunk,
892 const void *payload, const size_t paylen)
893{
894 struct sctp_chunk *retval;
895
896 retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen);
897 if (!retval)
898 goto nodata;
899
900 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
901
902 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
903 *
904 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
905 * HEARTBEAT ACK, * etc.) to the same destination transport
906 * address from which it * received the DATA or control chunk
907 * to which it is replying.
908 *
909 * [HBACK back to where the HEARTBEAT came from.]
910 */
911 if (chunk)
912 retval->transport = chunk->transport;
913
914nodata:
915 return retval;
916}
917
918/* Create an Operation Error chunk with the specified space reserved.
919 * This routine can be used for containing multiple causes in the chunk.
920 */
921static struct sctp_chunk *sctp_make_op_error_space(
922 const struct sctp_association *asoc,
923 const struct sctp_chunk *chunk,
924 size_t size)
925{
926 struct sctp_chunk *retval;
927
928 retval = sctp_make_chunk(asoc, SCTP_CID_ERROR, 0,
929 sizeof(sctp_errhdr_t) + size);
930 if (!retval)
931 goto nodata;
932
933 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
934 *
935 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
936 * HEARTBEAT ACK, etc.) to the same destination transport
937 * address from which it received the DATA or control chunk
938 * to which it is replying.
939 *
940 */
941 if (chunk)
942 retval->transport = chunk->transport;
943
944nodata:
945 return retval;
946}
947
948/* Create an Operation Error chunk. */
949struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
950 const struct sctp_chunk *chunk,
Al Viro63706c52006-11-20 17:00:05 -0800951 __be16 cause_code, const void *payload,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 size_t paylen)
953{
954 struct sctp_chunk *retval;
955
956 retval = sctp_make_op_error_space(asoc, chunk, paylen);
957 if (!retval)
958 goto nodata;
959
Wei Yongjun00f1c2d2007-08-21 15:50:01 +0800960 sctp_init_cause(retval, cause_code, paylen);
961 sctp_addto_chunk(retval, paylen, payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963nodata:
964 return retval;
965}
966
967/********************************************************************
968 * 2nd Level Abstractions
969 ********************************************************************/
970
971/* Turn an skb into a chunk.
972 * FIXME: Eventually move the structure directly inside the skb->cb[].
973 */
974struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
975 const struct sctp_association *asoc,
976 struct sock *sk)
977{
978 struct sctp_chunk *retval;
979
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800980 retval = kmem_cache_zalloc(sctp_chunk_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 if (!retval)
983 goto nodata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 if (!sk) {
986 SCTP_DEBUG_PRINTK("chunkifying skb %p w/o an sk\n", skb);
987 }
988
David S. Miller79af02c2005-07-08 21:47:49 -0700989 INIT_LIST_HEAD(&retval->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 retval->skb = skb;
991 retval->asoc = (struct sctp_association *)asoc;
992 retval->resent = 0;
993 retval->has_tsn = 0;
994 retval->has_ssn = 0;
995 retval->rtt_in_progress = 0;
996 retval->sent_at = 0;
997 retval->singleton = 1;
998 retval->end_of_packet = 0;
999 retval->ecn_ce_done = 0;
1000 retval->pdiscard = 0;
1001
1002 /* sctpimpguide-05.txt Section 2.8.2
1003 * M1) Each time a new DATA chunk is transmitted
1004 * set the 'TSN.Missing.Report' count for that TSN to 0. The
1005 * 'TSN.Missing.Report' count will be used to determine missing chunks
1006 * and when to fast retransmit.
1007 */
1008 retval->tsn_missing_report = 0;
1009 retval->tsn_gap_acked = 0;
1010 retval->fast_retransmit = 0;
1011
1012 /* If this is a fragmented message, track all fragments
1013 * of the message (for SEND_FAILED).
1014 */
1015 retval->msg = NULL;
1016
1017 /* Polish the bead hole. */
1018 INIT_LIST_HEAD(&retval->transmitted_list);
1019 INIT_LIST_HEAD(&retval->frag_list);
1020 SCTP_DBG_OBJCNT_INC(chunk);
1021 atomic_set(&retval->refcnt, 1);
1022
1023nodata:
1024 return retval;
1025}
1026
1027/* Set chunk->source and dest based on the IP header in chunk->skb. */
1028void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
1029 union sctp_addr *dest)
1030{
Al Virof235fca2006-11-20 17:09:01 -08001031 memcpy(&chunk->source, src, sizeof(union sctp_addr));
Al Viro16b0a032006-11-20 17:13:38 -08001032 memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
1035/* Extract the source address from a chunk. */
1036const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
1037{
1038 /* If we have a known transport, use that. */
1039 if (chunk->transport) {
Al Viro6a1e5f32006-11-20 17:12:25 -08001040 return &chunk->transport->ipaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 } else {
1042 /* Otherwise, extract it from the IP header. */
Al Viro6a1e5f32006-11-20 17:12:25 -08001043 return &chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
1045}
1046
1047/* Create a new chunk, setting the type and flags headers from the
1048 * arguments, reserving enough space for a 'paylen' byte payload.
1049 */
1050SCTP_STATIC
1051struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
1052 __u8 type, __u8 flags, int paylen)
1053{
1054 struct sctp_chunk *retval;
1055 sctp_chunkhdr_t *chunk_hdr;
1056 struct sk_buff *skb;
1057 struct sock *sk;
1058
1059 /* No need to allocate LL here, as this is only a chunk. */
1060 skb = alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t) + paylen),
1061 GFP_ATOMIC);
1062 if (!skb)
1063 goto nodata;
1064
1065 /* Make room for the chunk header. */
1066 chunk_hdr = (sctp_chunkhdr_t *)skb_put(skb, sizeof(sctp_chunkhdr_t));
1067 chunk_hdr->type = type;
1068 chunk_hdr->flags = flags;
1069 chunk_hdr->length = htons(sizeof(sctp_chunkhdr_t));
1070
1071 sk = asoc ? asoc->base.sk : NULL;
1072 retval = sctp_chunkify(skb, asoc, sk);
1073 if (!retval) {
1074 kfree_skb(skb);
1075 goto nodata;
1076 }
1077
1078 retval->chunk_hdr = chunk_hdr;
1079 retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(struct sctp_chunkhdr);
1080
1081 /* Set the skb to the belonging sock for accounting. */
1082 skb->sk = sk;
1083
1084 return retval;
1085nodata:
1086 return NULL;
1087}
1088
1089
1090/* Release the memory occupied by a chunk. */
1091static void sctp_chunk_destroy(struct sctp_chunk *chunk)
1092{
1093 /* Free the chunk skb data and the SCTP_chunk stub itself. */
1094 dev_kfree_skb(chunk->skb);
1095
1096 SCTP_DBG_OBJCNT_DEC(chunk);
1097 kmem_cache_free(sctp_chunk_cachep, chunk);
1098}
1099
1100/* Possibly, free the chunk. */
1101void sctp_chunk_free(struct sctp_chunk *chunk)
1102{
David S. Miller79af02c2005-07-08 21:47:49 -07001103 BUG_ON(!list_empty(&chunk->list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 list_del_init(&chunk->transmitted_list);
1105
1106 /* Release our reference on the message tracker. */
1107 if (chunk->msg)
1108 sctp_datamsg_put(chunk->msg);
1109
1110 sctp_chunk_put(chunk);
1111}
1112
1113/* Grab a reference to the chunk. */
1114void sctp_chunk_hold(struct sctp_chunk *ch)
1115{
1116 atomic_inc(&ch->refcnt);
1117}
1118
1119/* Release a reference to the chunk. */
1120void sctp_chunk_put(struct sctp_chunk *ch)
1121{
1122 if (atomic_dec_and_test(&ch->refcnt))
1123 sctp_chunk_destroy(ch);
1124}
1125
1126/* Append bytes to the end of a chunk. Will panic if chunk is not big
1127 * enough.
1128 */
1129void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
1130{
1131 void *target;
1132 void *padding;
1133 int chunklen = ntohs(chunk->chunk_hdr->length);
Wei Yongjun8d614ad2007-08-06 13:55:47 +08001134 int padlen = WORD_ROUND(chunklen) - chunklen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 padding = skb_put(chunk->skb, padlen);
1137 target = skb_put(chunk->skb, len);
1138
1139 memset(padding, 0, padlen);
1140 memcpy(target, data, len);
1141
1142 /* Adjust the chunk length field. */
1143 chunk->chunk_hdr->length = htons(chunklen + padlen + len);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001144 chunk->chunk_end = skb_tail_pointer(chunk->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 return target;
1147}
1148
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001149/* Append bytes to the end of a parameter. Will panic if chunk is not big
1150 * enough.
1151 */
1152void *sctp_addto_param(struct sctp_chunk *chunk, int len, const void *data)
1153{
1154 void *target;
1155 int chunklen = ntohs(chunk->chunk_hdr->length);
1156
1157 target = skb_put(chunk->skb, len);
1158
1159 memcpy(target, data, len);
1160
1161 /* Adjust the chunk length field. */
1162 chunk->chunk_hdr->length = htons(chunklen + len);
1163 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1164
1165 return target;
1166}
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168/* Append bytes from user space to the end of a chunk. Will panic if
1169 * chunk is not big enough.
1170 * Returns a kernel err value.
1171 */
1172int sctp_user_addto_chunk(struct sctp_chunk *chunk, int off, int len,
1173 struct iovec *data)
1174{
1175 __u8 *target;
1176 int err = 0;
1177
1178 /* Make room in chunk for data. */
1179 target = skb_put(chunk->skb, len);
1180
1181 /* Copy data (whole iovec) into chunk */
1182 if ((err = memcpy_fromiovecend(target, data, off, len)))
1183 goto out;
1184
1185 /* Adjust the chunk length field. */
1186 chunk->chunk_hdr->length =
1187 htons(ntohs(chunk->chunk_hdr->length) + len);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001188 chunk->chunk_end = skb_tail_pointer(chunk->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190out:
1191 return err;
1192}
1193
1194/* Helper function to assign a TSN if needed. This assumes that both
1195 * the data_hdr and association have already been assigned.
1196 */
1197void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
1198{
Vlad Yasevichab3e5e72007-08-02 16:51:42 -04001199 struct sctp_datamsg *msg;
1200 struct sctp_chunk *lchunk;
1201 struct sctp_stream *stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 __u16 ssn;
1203 __u16 sid;
1204
1205 if (chunk->has_ssn)
1206 return;
1207
Vlad Yasevichab3e5e72007-08-02 16:51:42 -04001208 /* All fragments will be on the same stream */
1209 sid = ntohs(chunk->subh.data_hdr->stream);
1210 stream = &chunk->asoc->ssnmap->out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Vlad Yasevichab3e5e72007-08-02 16:51:42 -04001212 /* Now assign the sequence number to the entire message.
1213 * All fragments must have the same stream sequence number.
1214 */
1215 msg = chunk->msg;
1216 list_for_each_entry(lchunk, &msg->chunks, frag_list) {
1217 if (lchunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
1218 ssn = 0;
1219 } else {
1220 if (lchunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
1221 ssn = sctp_ssn_next(stream, sid);
1222 else
1223 ssn = sctp_ssn_peek(stream, sid);
1224 }
1225
1226 lchunk->subh.data_hdr->ssn = htons(ssn);
1227 lchunk->has_ssn = 1;
1228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
1231/* Helper function to assign a TSN if needed. This assumes that both
1232 * the data_hdr and association have already been assigned.
1233 */
1234void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
1235{
1236 if (!chunk->has_tsn) {
1237 /* This is the last possible instant to
1238 * assign a TSN.
1239 */
1240 chunk->subh.data_hdr->tsn =
1241 htonl(sctp_association_get_next_tsn(chunk->asoc));
1242 chunk->has_tsn = 1;
1243 }
1244}
1245
1246/* Create a CLOSED association to use with an incoming packet. */
1247struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
Alexey Dobriyan3182cd842005-07-11 20:57:47 -07001248 struct sctp_chunk *chunk,
Al Virodd0fc662005-10-07 07:46:04 +01001249 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 struct sctp_association *asoc;
1252 struct sk_buff *skb;
1253 sctp_scope_t scope;
1254 struct sctp_af *af;
1255
1256 /* Create the bare association. */
1257 scope = sctp_scope(sctp_source(chunk));
1258 asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
1259 if (!asoc)
1260 goto nodata;
1261 asoc->temp = 1;
1262 skb = chunk->skb;
1263 /* Create an entry for the source address of the packet. */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001264 af = sctp_get_af_specific(ipver2af(ip_hdr(skb)->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (unlikely(!af))
1266 goto fail;
Al Virod55c41b2006-11-20 17:09:40 -08001267 af->from_skb(&asoc->c.peer_addr, skb, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268nodata:
1269 return asoc;
1270
1271fail:
1272 sctp_association_free(asoc);
1273 return NULL;
1274}
1275
1276/* Build a cookie representing asoc.
1277 * This INCLUDES the param header needed to put the cookie in the INIT ACK.
1278 */
1279static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
1280 const struct sctp_association *asoc,
1281 const struct sctp_chunk *init_chunk,
1282 int *cookie_len,
1283 const __u8 *raw_addrs, int addrs_len)
1284{
1285 sctp_cookie_param_t *retval;
1286 struct sctp_signed_cookie *cookie;
1287 struct scatterlist sg;
1288 int headersize, bodysize;
1289 unsigned int keylen;
1290 char *key;
1291
Vlad Yasevich9834a2b2006-01-17 11:52:12 -08001292 /* Header size is static data prior to the actual cookie, including
1293 * any padding.
1294 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001295 headersize = sizeof(sctp_paramhdr_t) +
1296 (sizeof(struct sctp_signed_cookie) -
Vlad Yasevich9834a2b2006-01-17 11:52:12 -08001297 sizeof(struct sctp_cookie));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 bodysize = sizeof(struct sctp_cookie)
1299 + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
1300
1301 /* Pad out the cookie to a multiple to make the signature
1302 * functions simpler to write.
1303 */
1304 if (bodysize % SCTP_COOKIE_MULTIPLE)
1305 bodysize += SCTP_COOKIE_MULTIPLE
1306 - (bodysize % SCTP_COOKIE_MULTIPLE);
1307 *cookie_len = headersize + bodysize;
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 /* Clear this memory since we are sending this data structure
1310 * out on the network.
1311 */
Arnaldo Carvalho de Meloaf997d82006-11-21 01:20:33 -02001312 retval = kzalloc(*cookie_len, GFP_ATOMIC);
1313 if (!retval)
1314 goto nodata;
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 cookie = (struct sctp_signed_cookie *) retval->body;
1317
1318 /* Set up the parameter header. */
1319 retval->p.type = SCTP_PARAM_STATE_COOKIE;
1320 retval->p.length = htons(*cookie_len);
1321
1322 /* Copy the cookie part of the association itself. */
1323 cookie->c = asoc->c;
1324 /* Save the raw address list length in the cookie. */
1325 cookie->c.raw_addr_list_len = addrs_len;
1326
1327 /* Remember PR-SCTP capability. */
1328 cookie->c.prsctp_capable = asoc->peer.prsctp_capable;
1329
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001330 /* Save adaptation indication in the cookie. */
1331 cookie->c.adaptation_ind = asoc->peer.adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 /* Set an expiration time for the cookie. */
1334 do_gettimeofday(&cookie->c.expiration);
1335 TIMEVAL_ADD(asoc->cookie_life, cookie->c.expiration);
1336
1337 /* Copy the peer's init packet. */
1338 memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
1339 ntohs(init_chunk->chunk_hdr->length));
1340
1341 /* Copy the raw local address list of the association. */
1342 memcpy((__u8 *)&cookie->c.peer_init[0] +
1343 ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
1344
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001345 if (sctp_sk(ep->base.sk)->hmac) {
Herbert Xu1b489e12006-08-20 15:07:14 +10001346 struct hash_desc desc;
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 /* Sign the message. */
1349 sg.page = virt_to_page(&cookie->c);
1350 sg.offset = (unsigned long)(&cookie->c) % PAGE_SIZE;
1351 sg.length = bodysize;
1352 keylen = SCTP_SECRET_SIZE;
1353 key = (char *)ep->secret_key[ep->current_key];
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001354 desc.tfm = sctp_sk(ep->base.sk)->hmac;
1355 desc.flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Herbert Xu1b489e12006-08-20 15:07:14 +10001357 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1358 crypto_hash_digest(&desc, &sg, bodysize, cookie->signature))
1359 goto free_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return retval;
Herbert Xu1b489e12006-08-20 15:07:14 +10001363
1364free_cookie:
1365 kfree(retval);
1366nodata:
1367 *cookie_len = 0;
1368 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369}
1370
1371/* Unpack the cookie from COOKIE ECHO chunk, recreating the association. */
1372struct sctp_association *sctp_unpack_cookie(
1373 const struct sctp_endpoint *ep,
1374 const struct sctp_association *asoc,
Al Virodd0fc662005-10-07 07:46:04 +01001375 struct sctp_chunk *chunk, gfp_t gfp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 int *error, struct sctp_chunk **errp)
1377{
1378 struct sctp_association *retval = NULL;
1379 struct sctp_signed_cookie *cookie;
1380 struct sctp_cookie *bear_cookie;
1381 int headersize, bodysize, fixed_size;
Vlad Yasevich313e7b42006-01-17 11:55:57 -08001382 __u8 *digest = ep->digest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 struct scatterlist sg;
1384 unsigned int keylen, len;
1385 char *key;
1386 sctp_scope_t scope;
1387 struct sk_buff *skb = chunk->skb;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001388 struct timeval tv;
Herbert Xu1b489e12006-08-20 15:07:14 +10001389 struct hash_desc desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Vlad Yasevich9834a2b2006-01-17 11:52:12 -08001391 /* Header size is static data prior to the actual cookie, including
1392 * any padding.
1393 */
1394 headersize = sizeof(sctp_chunkhdr_t) +
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001395 (sizeof(struct sctp_signed_cookie) -
Vlad Yasevich9834a2b2006-01-17 11:52:12 -08001396 sizeof(struct sctp_cookie));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
1398 fixed_size = headersize + sizeof(struct sctp_cookie);
1399
1400 /* Verify that the chunk looks like it even has a cookie.
1401 * There must be enough room for our cookie and our peer's
1402 * INIT chunk.
1403 */
1404 len = ntohs(chunk->chunk_hdr->length);
1405 if (len < fixed_size + sizeof(struct sctp_chunkhdr))
1406 goto malformed;
1407
1408 /* Verify that the cookie has been padded out. */
1409 if (bodysize % SCTP_COOKIE_MULTIPLE)
1410 goto malformed;
1411
1412 /* Process the cookie. */
1413 cookie = chunk->subh.cookie_hdr;
1414 bear_cookie = &cookie->c;
1415
1416 if (!sctp_sk(ep->base.sk)->hmac)
1417 goto no_hmac;
1418
1419 /* Check the signature. */
1420 keylen = SCTP_SECRET_SIZE;
1421 sg.page = virt_to_page(bear_cookie);
1422 sg.offset = (unsigned long)(bear_cookie) % PAGE_SIZE;
1423 sg.length = bodysize;
1424 key = (char *)ep->secret_key[ep->current_key];
Herbert Xu1b489e12006-08-20 15:07:14 +10001425 desc.tfm = sctp_sk(ep->base.sk)->hmac;
1426 desc.flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Al Viro8ca84482006-06-20 03:26:14 -07001428 memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
Herbert Xu1b489e12006-08-20 15:07:14 +10001429 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1430 crypto_hash_digest(&desc, &sg, bodysize, digest)) {
1431 *error = -SCTP_IERROR_NOMEM;
1432 goto fail;
1433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1436 /* Try the previous key. */
1437 key = (char *)ep->secret_key[ep->last_key];
Al Viro8ca84482006-06-20 03:26:14 -07001438 memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
Herbert Xu1b489e12006-08-20 15:07:14 +10001439 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1440 crypto_hash_digest(&desc, &sg, bodysize, digest)) {
1441 *error = -SCTP_IERROR_NOMEM;
1442 goto fail;
1443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1446 /* Yikes! Still bad signature! */
1447 *error = -SCTP_IERROR_BAD_SIG;
1448 goto fail;
1449 }
1450 }
1451
1452no_hmac:
1453 /* IG Section 2.35.2:
1454 * 3) Compare the port numbers and the verification tag contained
1455 * within the COOKIE ECHO chunk to the actual port numbers and the
1456 * verification tag within the SCTP common header of the received
1457 * packet. If these values do not match the packet MUST be silently
1458 * discarded,
1459 */
1460 if (ntohl(chunk->sctp_hdr->vtag) != bear_cookie->my_vtag) {
1461 *error = -SCTP_IERROR_BAD_TAG;
1462 goto fail;
1463 }
1464
Al Viro9b1dfad2006-11-20 17:09:17 -08001465 if (chunk->sctp_hdr->source != bear_cookie->peer_addr.v4.sin_port ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 ntohs(chunk->sctp_hdr->dest) != bear_cookie->my_port) {
1467 *error = -SCTP_IERROR_BAD_PORTS;
1468 goto fail;
1469 }
1470
1471 /* Check to see if the cookie is stale. If there is already
1472 * an association, there is no need to check cookie's expiration
1473 * for init collision case of lost COOKIE ACK.
Vlad Yasevichf236218b2006-09-29 17:10:03 -07001474 * If skb has been timestamped, then use the stamp, otherwise
1475 * use current time. This introduces a small possibility that
1476 * that a cookie may be considered expired, but his would only slow
1477 * down the new association establishment instead of every packet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 */
Vlad Yasevichf236218b2006-09-29 17:10:03 -07001479 if (sock_flag(ep->base.sk, SOCK_TIMESTAMP))
1480 skb_get_timestamp(skb, &tv);
1481 else
1482 do_gettimeofday(&tv);
1483
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001484 if (!asoc && tv_lt(bear_cookie->expiration, tv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 /*
1486 * Section 3.3.10.3 Stale Cookie Error (3)
1487 *
1488 * Cause of error
1489 * ---------------
1490 * Stale Cookie Error: Indicates the receipt of a valid State
1491 * Cookie that has expired.
1492 */
1493 len = ntohs(chunk->chunk_hdr->length);
1494 *errp = sctp_make_op_error_space(asoc, chunk, len);
1495 if (*errp) {
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001496 suseconds_t usecs = (tv.tv_sec -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 bear_cookie->expiration.tv_sec) * 1000000L +
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001498 tv.tv_usec - bear_cookie->expiration.tv_usec;
Al Viro34bcca22006-11-20 17:27:15 -08001499 __be32 n = htonl(usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001502 sizeof(n));
1503 sctp_addto_chunk(*errp, sizeof(n), &n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 *error = -SCTP_IERROR_STALE_COOKIE;
1505 } else
1506 *error = -SCTP_IERROR_NOMEM;
1507
1508 goto fail;
1509 }
1510
1511 /* Make a new base association. */
1512 scope = sctp_scope(sctp_source(chunk));
1513 retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
1514 if (!retval) {
1515 *error = -SCTP_IERROR_NOMEM;
1516 goto fail;
1517 }
1518
1519 /* Set up our peer's port number. */
1520 retval->peer.port = ntohs(chunk->sctp_hdr->source);
1521
1522 /* Populate the association from the cookie. */
1523 memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
1524
1525 if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
1526 GFP_ATOMIC) < 0) {
1527 *error = -SCTP_IERROR_NOMEM;
1528 goto fail;
1529 }
1530
1531 /* Also, add the destination address. */
1532 if (list_empty(&retval->base.bind_addr.address_list)) {
Al Viro16b0a032006-11-20 17:13:38 -08001533 sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest, 1,
Vlad Yasevich559cf712007-09-16 16:03:28 -07001534 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
1536
1537 retval->next_tsn = retval->c.initial_tsn;
1538 retval->ctsn_ack_point = retval->next_tsn - 1;
1539 retval->addip_serial = retval->c.initial_tsn;
1540 retval->adv_peer_ack_point = retval->ctsn_ack_point;
1541 retval->peer.prsctp_capable = retval->c.prsctp_capable;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001542 retval->peer.adaptation_ind = retval->c.adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 /* The INIT stuff will be done by the side effects. */
1545 return retval;
1546
1547fail:
1548 if (retval)
1549 sctp_association_free(retval);
1550
1551 return NULL;
1552
1553malformed:
1554 /* Yikes! The packet is either corrupt or deliberately
1555 * malformed.
1556 */
1557 *error = -SCTP_IERROR_MALFORMED;
1558 goto fail;
1559}
1560
1561/********************************************************************
1562 * 3rd Level Abstractions
1563 ********************************************************************/
1564
1565struct __sctp_missing {
Al Viro9f81bcd2006-11-20 17:26:34 -08001566 __be32 num_missing;
1567 __be16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568} __attribute__((packed));
1569
1570/*
1571 * Report a missing mandatory parameter.
1572 */
1573static int sctp_process_missing_param(const struct sctp_association *asoc,
1574 sctp_param_t paramtype,
1575 struct sctp_chunk *chunk,
1576 struct sctp_chunk **errp)
1577{
1578 struct __sctp_missing report;
1579 __u16 len;
1580
1581 len = WORD_ROUND(sizeof(report));
1582
1583 /* Make an ERROR chunk, preparing enough room for
1584 * returning multiple unknown parameters.
1585 */
1586 if (!*errp)
1587 *errp = sctp_make_op_error_space(asoc, chunk, len);
1588
1589 if (*errp) {
1590 report.num_missing = htonl(1);
1591 report.type = paramtype;
Vlad Yasevichebdfcad2007-01-15 19:12:31 -08001592 sctp_init_cause(*errp, SCTP_ERROR_MISS_PARAM,
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001593 sizeof(report));
1594 sctp_addto_chunk(*errp, sizeof(report), &report);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 }
1596
1597 /* Stop processing this chunk. */
1598 return 0;
1599}
1600
1601/* Report an Invalid Mandatory Parameter. */
1602static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
1603 struct sctp_chunk *chunk,
1604 struct sctp_chunk **errp)
1605{
1606 /* Invalid Mandatory Parameter Error has no payload. */
1607
1608 if (!*errp)
1609 *errp = sctp_make_op_error_space(asoc, chunk, 0);
1610
1611 if (*errp)
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001612 sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 /* Stop processing this chunk. */
1615 return 0;
1616}
1617
1618static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
1619 struct sctp_paramhdr *param,
1620 const struct sctp_chunk *chunk,
1621 struct sctp_chunk **errp)
1622{
1623 char error[] = "The following parameter had invalid length:";
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001624 size_t payload_len = WORD_ROUND(sizeof(error)) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 sizeof(sctp_paramhdr_t);
1626
1627
1628 /* Create an error chunk and fill it in with our payload. */
1629 if (!*errp)
1630 *errp = sctp_make_op_error_space(asoc, chunk, payload_len);
1631
1632 if (*errp) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001633 sctp_init_cause(*errp, SCTP_ERROR_PROTO_VIOLATION,
1634 sizeof(error) + sizeof(sctp_paramhdr_t));
1635 sctp_addto_chunk(*errp, sizeof(error), error);
1636 sctp_addto_param(*errp, sizeof(sctp_paramhdr_t), param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 }
1638
1639 return 0;
1640}
1641
1642
1643/* Do not attempt to handle the HOST_NAME parm. However, do
1644 * send back an indicator to the peer.
1645 */
1646static int sctp_process_hn_param(const struct sctp_association *asoc,
1647 union sctp_params param,
1648 struct sctp_chunk *chunk,
1649 struct sctp_chunk **errp)
1650{
1651 __u16 len = ntohs(param.p->length);
1652
1653 /* Make an ERROR chunk. */
1654 if (!*errp)
1655 *errp = sctp_make_op_error_space(asoc, chunk, len);
1656
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001657 if (*errp) {
1658 sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED, len);
1659 sctp_addto_chunk(*errp, len, param.v);
1660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 /* Stop processing this chunk. */
1663 return 0;
1664}
1665
1666/* RFC 3.2.1 & the Implementers Guide 2.2.
1667 *
1668 * The Parameter Types are encoded such that the
1669 * highest-order two bits specify the action that must be
1670 * taken if the processing endpoint does not recognize the
1671 * Parameter Type.
1672 *
1673 * 00 - Stop processing this SCTP chunk and discard it,
1674 * do not process any further chunks within it.
1675 *
1676 * 01 - Stop processing this SCTP chunk and discard it,
1677 * do not process any further chunks within it, and report
1678 * the unrecognized parameter in an 'Unrecognized
1679 * Parameter Type' (in either an ERROR or in the INIT ACK).
1680 *
1681 * 10 - Skip this parameter and continue processing.
1682 *
1683 * 11 - Skip this parameter and continue processing but
1684 * report the unrecognized parameter in an
1685 * 'Unrecognized Parameter Type' (in either an ERROR or in
1686 * the INIT ACK).
1687 *
1688 * Return value:
1689 * 0 - discard the chunk
1690 * 1 - continue with the chunk
1691 */
1692static int sctp_process_unk_param(const struct sctp_association *asoc,
1693 union sctp_params param,
1694 struct sctp_chunk *chunk,
1695 struct sctp_chunk **errp)
1696{
1697 int retval = 1;
1698
1699 switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
1700 case SCTP_PARAM_ACTION_DISCARD:
1701 retval = 0;
1702 break;
1703 case SCTP_PARAM_ACTION_DISCARD_ERR:
1704 retval = 0;
1705 /* Make an ERROR chunk, preparing enough room for
1706 * returning multiple unknown parameters.
1707 */
1708 if (NULL == *errp)
1709 *errp = sctp_make_op_error_space(asoc, chunk,
1710 ntohs(chunk->chunk_hdr->length));
1711
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001712 if (*errp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 WORD_ROUND(ntohs(param.p->length)));
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001715 sctp_addto_chunk(*errp,
1716 WORD_ROUND(ntohs(param.p->length)),
1717 param.v);
1718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 break;
1721 case SCTP_PARAM_ACTION_SKIP:
1722 break;
1723 case SCTP_PARAM_ACTION_SKIP_ERR:
1724 /* Make an ERROR chunk, preparing enough room for
1725 * returning multiple unknown parameters.
1726 */
1727 if (NULL == *errp)
1728 *errp = sctp_make_op_error_space(asoc, chunk,
1729 ntohs(chunk->chunk_hdr->length));
1730
1731 if (*errp) {
1732 sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 WORD_ROUND(ntohs(param.p->length)));
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001734 sctp_addto_chunk(*errp,
1735 WORD_ROUND(ntohs(param.p->length)),
1736 param.v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 } else {
1738 /* If there is no memory for generating the ERROR
1739 * report as specified, an ABORT will be triggered
1740 * to the peer and the association won't be
1741 * established.
1742 */
1743 retval = 0;
1744 }
1745
1746 break;
1747 default:
1748 break;
1749 }
1750
1751 return retval;
1752}
1753
1754/* Find unrecognized parameters in the chunk.
1755 * Return values:
1756 * 0 - discard the chunk
1757 * 1 - continue with the chunk
1758 */
1759static int sctp_verify_param(const struct sctp_association *asoc,
1760 union sctp_params param,
1761 sctp_cid_t cid,
1762 struct sctp_chunk *chunk,
1763 struct sctp_chunk **err_chunk)
1764{
1765 int retval = 1;
1766
1767 /* FIXME - This routine is not looking at each parameter per the
1768 * chunk type, i.e., unrecognized parameters should be further
1769 * identified based on the chunk id.
1770 */
1771
1772 switch (param.p->type) {
1773 case SCTP_PARAM_IPV4_ADDRESS:
1774 case SCTP_PARAM_IPV6_ADDRESS:
1775 case SCTP_PARAM_COOKIE_PRESERVATIVE:
1776 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
1777 case SCTP_PARAM_STATE_COOKIE:
1778 case SCTP_PARAM_HEARTBEAT_INFO:
1779 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
1780 case SCTP_PARAM_ECN_CAPABLE:
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001781 case SCTP_PARAM_ADAPTATION_LAYER_IND:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 break;
1783
1784 case SCTP_PARAM_HOST_NAME_ADDRESS:
1785 /* Tell the peer, we won't support this param. */
1786 return sctp_process_hn_param(asoc, param, chunk, err_chunk);
1787 case SCTP_PARAM_FWD_TSN_SUPPORT:
1788 if (sctp_prsctp_enable)
1789 break;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001790 /* Fall Through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 default:
1792 SCTP_DEBUG_PRINTK("Unrecognized param: %d for chunk %d.\n",
1793 ntohs(param.p->type), cid);
1794 return sctp_process_unk_param(asoc, param, chunk, err_chunk);
1795
1796 break;
1797 }
1798 return retval;
1799}
1800
1801/* Verify the INIT packet before we process it. */
1802int sctp_verify_init(const struct sctp_association *asoc,
1803 sctp_cid_t cid,
1804 sctp_init_chunk_t *peer_init,
1805 struct sctp_chunk *chunk,
1806 struct sctp_chunk **errp)
1807{
1808 union sctp_params param;
1809 int has_cookie = 0;
1810
1811 /* Verify stream values are non-zero. */
1812 if ((0 == peer_init->init_hdr.num_outbound_streams) ||
Vlad Yasevichd023f622007-01-15 19:15:45 -08001813 (0 == peer_init->init_hdr.num_inbound_streams) ||
1814 (0 == peer_init->init_hdr.init_tag) ||
1815 (SCTP_DEFAULT_MINWINDOW > ntohl(peer_init->init_hdr.a_rwnd))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817 sctp_process_inv_mandatory(asoc, chunk, errp);
1818 return 0;
1819 }
1820
1821 /* Check for missing mandatory parameters. */
1822 sctp_walk_params(param, peer_init, init_hdr.params) {
1823
1824 if (SCTP_PARAM_STATE_COOKIE == param.p->type)
1825 has_cookie = 1;
1826
1827 } /* for (loop through all parameters) */
1828
1829 /* There is a possibility that a parameter length was bad and
1830 * in that case we would have stoped walking the parameters.
1831 * The current param.p would point at the bad one.
1832 * Current consensus on the mailing list is to generate a PROTOCOL
1833 * VIOLATION error. We build the ERROR chunk here and let the normal
1834 * error handling code build and send the packet.
1835 */
Wei Yongjuncb243a12007-08-06 13:55:58 +08001836 if (param.v != (void*)chunk->chunk_end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
1838 return 0;
1839 }
1840
1841 /* The only missing mandatory param possible today is
1842 * the state cookie for an INIT-ACK chunk.
1843 */
1844 if ((SCTP_CID_INIT_ACK == cid) && !has_cookie) {
1845 sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
1846 chunk, errp);
1847 return 0;
1848 }
1849
1850 /* Find unrecognized parameters. */
1851
1852 sctp_walk_params(param, peer_init, init_hdr.params) {
1853
1854 if (!sctp_verify_param(asoc, param, cid, chunk, errp)) {
1855 if (SCTP_PARAM_HOST_NAME_ADDRESS == param.p->type)
1856 return 0;
1857 else
1858 return 1;
1859 }
1860
1861 } /* for (loop through all parameters) */
1862
1863 return 1;
1864}
1865
1866/* Unpack the parameters in an INIT packet into an association.
1867 * Returns 0 on failure, else success.
1868 * FIXME: This is an association method.
1869 */
1870int sctp_process_init(struct sctp_association *asoc, sctp_cid_t cid,
1871 const union sctp_addr *peer_addr,
Al Virodd0fc662005-10-07 07:46:04 +01001872 sctp_init_chunk_t *peer_init, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
1874 union sctp_params param;
1875 struct sctp_transport *transport;
1876 struct list_head *pos, *temp;
1877 char *cookie;
1878
1879 /* We must include the address that the INIT packet came from.
1880 * This is the only address that matters for an INIT packet.
1881 * When processing a COOKIE ECHO, we retrieve the from address
1882 * of the INIT from the cookie.
1883 */
1884
1885 /* This implementation defaults to making the first transport
1886 * added as the primary transport. The source address seems to
1887 * be a a better choice than any of the embedded addresses.
1888 */
Al Viro4bdf4b52006-11-20 17:10:20 -08001889 if (peer_addr) {
Al Viro6a1e5f32006-11-20 17:12:25 -08001890 if(!sctp_assoc_add_peer(asoc, peer_addr, gfp, SCTP_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 goto nomem;
Al Viro4bdf4b52006-11-20 17:10:20 -08001892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
1894 /* Process the initialization parameters. */
1895
1896 sctp_walk_params(param, peer_init, init_hdr.params) {
1897
1898 if (!sctp_process_param(asoc, param, peer_addr, gfp))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001899 goto clean_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 }
1901
Frank Filz3f7a87d2005-06-20 13:14:57 -07001902 /* Walk list of transports, removing transports in the UNKNOWN state. */
1903 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
1904 transport = list_entry(pos, struct sctp_transport, transports);
1905 if (transport->state == SCTP_UNKNOWN) {
1906 sctp_assoc_rm_peer(asoc, transport);
1907 }
1908 }
1909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 /* The fixed INIT headers are always in network byte
1911 * order.
1912 */
1913 asoc->peer.i.init_tag =
1914 ntohl(peer_init->init_hdr.init_tag);
1915 asoc->peer.i.a_rwnd =
1916 ntohl(peer_init->init_hdr.a_rwnd);
1917 asoc->peer.i.num_outbound_streams =
1918 ntohs(peer_init->init_hdr.num_outbound_streams);
1919 asoc->peer.i.num_inbound_streams =
1920 ntohs(peer_init->init_hdr.num_inbound_streams);
1921 asoc->peer.i.initial_tsn =
1922 ntohl(peer_init->init_hdr.initial_tsn);
1923
1924 /* Apply the upper bounds for output streams based on peer's
1925 * number of inbound streams.
1926 */
1927 if (asoc->c.sinit_num_ostreams >
1928 ntohs(peer_init->init_hdr.num_inbound_streams)) {
1929 asoc->c.sinit_num_ostreams =
1930 ntohs(peer_init->init_hdr.num_inbound_streams);
1931 }
1932
1933 if (asoc->c.sinit_max_instreams >
1934 ntohs(peer_init->init_hdr.num_outbound_streams)) {
1935 asoc->c.sinit_max_instreams =
1936 ntohs(peer_init->init_hdr.num_outbound_streams);
1937 }
1938
1939 /* Copy Initiation tag from INIT to VT_peer in cookie. */
1940 asoc->c.peer_vtag = asoc->peer.i.init_tag;
1941
1942 /* Peer Rwnd : Current calculated value of the peer's rwnd. */
1943 asoc->peer.rwnd = asoc->peer.i.a_rwnd;
1944
1945 /* Copy cookie in case we need to resend COOKIE-ECHO. */
1946 cookie = asoc->peer.cookie;
1947 if (cookie) {
Arnaldo Carvalho de Meloaf997d82006-11-21 01:20:33 -02001948 asoc->peer.cookie = kmemdup(cookie, asoc->peer.cookie_len, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 if (!asoc->peer.cookie)
1950 goto clean_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
1952
1953 /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
1954 * high (for example, implementations MAY use the size of the receiver
1955 * advertised window).
1956 */
1957 list_for_each(pos, &asoc->peer.transport_addr_list) {
1958 transport = list_entry(pos, struct sctp_transport, transports);
1959 transport->ssthresh = asoc->peer.i.a_rwnd;
1960 }
1961
1962 /* Set up the TSN tracking pieces. */
1963 sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_SIZE,
1964 asoc->peer.i.initial_tsn);
1965
1966 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
1967 *
1968 * The stream sequence number in all the streams shall start
1969 * from 0 when the association is established. Also, when the
1970 * stream sequence number reaches the value 65535 the next
1971 * stream sequence number shall be set to 0.
1972 */
1973
Frank Filz3f7a87d2005-06-20 13:14:57 -07001974 /* Allocate storage for the negotiated streams if it is not a temporary
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001975 * association.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 */
1977 if (!asoc->temp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 int error;
1979
1980 asoc->ssnmap = sctp_ssnmap_new(asoc->c.sinit_max_instreams,
1981 asoc->c.sinit_num_ostreams, gfp);
1982 if (!asoc->ssnmap)
1983 goto clean_up;
1984
Vlad Yasevich07d93962007-05-04 13:55:27 -07001985 error = sctp_assoc_set_id(asoc, gfp);
1986 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 goto clean_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 }
1989
1990 /* ADDIP Section 4.1 ASCONF Chunk Procedures
1991 *
1992 * When an endpoint has an ASCONF signaled change to be sent to the
1993 * remote endpoint it should do the following:
1994 * ...
1995 * A2) A serial number should be assigned to the Chunk. The serial
1996 * number should be a monotonically increasing number. All serial
1997 * numbers are defined to be initialized at the start of the
1998 * association to the same value as the Initial TSN.
1999 */
2000 asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
2001 return 1;
2002
2003clean_up:
2004 /* Release the transport structures. */
2005 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2006 transport = list_entry(pos, struct sctp_transport, transports);
2007 list_del_init(pos);
2008 sctp_transport_free(transport);
2009 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07002010
2011 asoc->peer.transport_count = 0;
2012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013nomem:
2014 return 0;
2015}
2016
2017
2018/* Update asoc with the option described in param.
2019 *
2020 * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
2021 *
2022 * asoc is the association to update.
2023 * param is the variable length parameter to use for update.
2024 * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
2025 * If the current packet is an INIT we want to minimize the amount of
2026 * work we do. In particular, we should not build transport
2027 * structures for the addresses.
2028 */
2029static int sctp_process_param(struct sctp_association *asoc,
2030 union sctp_params param,
2031 const union sctp_addr *peer_addr,
Al Virodd0fc662005-10-07 07:46:04 +01002032 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
2034 union sctp_addr addr;
2035 int i;
2036 __u16 sat;
2037 int retval = 1;
2038 sctp_scope_t scope;
2039 time_t stale;
2040 struct sctp_af *af;
2041
2042 /* We maintain all INIT parameters in network byte order all the
2043 * time. This allows us to not worry about whether the parameters
2044 * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
2045 */
2046 switch (param.p->type) {
2047 case SCTP_PARAM_IPV6_ADDRESS:
2048 if (PF_INET6 != asoc->base.sk->sk_family)
2049 break;
2050 /* Fall through. */
2051 case SCTP_PARAM_IPV4_ADDRESS:
2052 af = sctp_get_af_specific(param_type2af(param.p->type));
Al Virodd86d132006-11-20 17:11:13 -08002053 af->from_addr_param(&addr, param.addr, htons(asoc->peer.port), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 scope = sctp_scope(peer_addr);
Al Virodd86d132006-11-20 17:11:13 -08002055 if (sctp_in_scope(&addr, scope))
2056 if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_UNCONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 return 0;
2058 break;
2059
2060 case SCTP_PARAM_COOKIE_PRESERVATIVE:
2061 if (!sctp_cookie_preserve_enable)
2062 break;
2063
2064 stale = ntohl(param.life->lifespan_increment);
2065
2066 /* Suggested Cookie Life span increment's unit is msec,
2067 * (1/1000sec).
2068 */
2069 asoc->cookie_life.tv_sec += stale / 1000;
2070 asoc->cookie_life.tv_usec += (stale % 1000) * 1000;
2071 break;
2072
2073 case SCTP_PARAM_HOST_NAME_ADDRESS:
2074 SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
2075 break;
2076
2077 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2078 /* Turn off the default values first so we'll know which
2079 * ones are really set by the peer.
2080 */
2081 asoc->peer.ipv4_address = 0;
2082 asoc->peer.ipv6_address = 0;
2083
2084 /* Cycle through address types; avoid divide by 0. */
2085 sat = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2086 if (sat)
2087 sat /= sizeof(__u16);
2088
2089 for (i = 0; i < sat; ++i) {
2090 switch (param.sat->types[i]) {
2091 case SCTP_PARAM_IPV4_ADDRESS:
2092 asoc->peer.ipv4_address = 1;
2093 break;
2094
2095 case SCTP_PARAM_IPV6_ADDRESS:
2096 asoc->peer.ipv6_address = 1;
2097 break;
2098
2099 case SCTP_PARAM_HOST_NAME_ADDRESS:
2100 asoc->peer.hostname_address = 1;
2101 break;
2102
2103 default: /* Just ignore anything else. */
2104 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 }
2107 break;
2108
2109 case SCTP_PARAM_STATE_COOKIE:
2110 asoc->peer.cookie_len =
2111 ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2112 asoc->peer.cookie = param.cookie->body;
2113 break;
2114
2115 case SCTP_PARAM_HEARTBEAT_INFO:
2116 /* Would be odd to receive, but it causes no problems. */
2117 break;
2118
2119 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2120 /* Rejected during verify stage. */
2121 break;
2122
2123 case SCTP_PARAM_ECN_CAPABLE:
2124 asoc->peer.ecn_capable = 1;
2125 break;
2126
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002127 case SCTP_PARAM_ADAPTATION_LAYER_IND:
2128 asoc->peer.adaptation_ind = param.aind->adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 break;
2130
2131 case SCTP_PARAM_FWD_TSN_SUPPORT:
2132 if (sctp_prsctp_enable) {
2133 asoc->peer.prsctp_capable = 1;
2134 break;
2135 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002136 /* Fall Through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 default:
2138 /* Any unrecognized parameters should have been caught
2139 * and handled by sctp_verify_param() which should be
2140 * called prior to this routine. Simply log the error
2141 * here.
2142 */
2143 SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
2144 ntohs(param.p->type), asoc);
2145 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 return retval;
2149}
2150
2151/* Select a new verification tag. */
2152__u32 sctp_generate_tag(const struct sctp_endpoint *ep)
2153{
2154 /* I believe that this random number generator complies with RFC1750.
2155 * A tag of 0 is reserved for special cases (e.g. INIT).
2156 */
2157 __u32 x;
2158
2159 do {
2160 get_random_bytes(&x, sizeof(__u32));
2161 } while (x == 0);
2162
2163 return x;
2164}
2165
2166/* Select an initial TSN to send during startup. */
2167__u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
2168{
2169 __u32 retval;
2170
2171 get_random_bytes(&retval, sizeof(__u32));
2172 return retval;
2173}
2174
2175/*
2176 * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
2177 * 0 1 2 3
2178 * 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
2179 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2180 * | Type = 0xC1 | Chunk Flags | Chunk Length |
2181 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2182 * | Serial Number |
2183 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2184 * | Address Parameter |
2185 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2186 * | ASCONF Parameter #1 |
2187 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2188 * \ \
2189 * / .... /
2190 * \ \
2191 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2192 * | ASCONF Parameter #N |
2193 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2194 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002195 * Address Parameter and other parameter will not be wrapped in this function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 */
2197static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
2198 union sctp_addr *addr,
2199 int vparam_len)
2200{
2201 sctp_addiphdr_t asconf;
2202 struct sctp_chunk *retval;
2203 int length = sizeof(asconf) + vparam_len;
2204 union sctp_addr_param addrparam;
2205 int addrlen;
2206 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2207
2208 addrlen = af->to_addr_param(addr, &addrparam);
2209 if (!addrlen)
2210 return NULL;
2211 length += addrlen;
2212
2213 /* Create the chunk. */
2214 retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF, 0, length);
2215 if (!retval)
2216 return NULL;
2217
2218 asconf.serial = htonl(asoc->addip_serial++);
2219
2220 retval->subh.addip_hdr =
2221 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2222 retval->param_hdr.v =
2223 sctp_addto_chunk(retval, addrlen, &addrparam);
2224
2225 return retval;
2226}
2227
2228/* ADDIP
2229 * 3.2.1 Add IP Address
2230 * 0 1 2 3
2231 * 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
2232 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2233 * | Type = 0xC001 | Length = Variable |
2234 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2235 * | ASCONF-Request Correlation ID |
2236 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2237 * | Address Parameter |
2238 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2239 *
2240 * 3.2.2 Delete IP Address
2241 * 0 1 2 3
2242 * 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
2243 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2244 * | Type = 0xC002 | Length = Variable |
2245 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2246 * | ASCONF-Request Correlation ID |
2247 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2248 * | Address Parameter |
2249 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2250 *
2251 */
2252struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
2253 union sctp_addr *laddr,
2254 struct sockaddr *addrs,
2255 int addrcnt,
Al Virodbc16db2006-11-20 17:01:42 -08002256 __be16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
2258 sctp_addip_param_t param;
2259 struct sctp_chunk *retval;
2260 union sctp_addr_param addr_param;
2261 union sctp_addr *addr;
2262 void *addr_buf;
2263 struct sctp_af *af;
2264 int paramlen = sizeof(param);
2265 int addr_param_len = 0;
2266 int totallen = 0;
2267 int i;
2268
2269 /* Get total length of all the address parameters. */
2270 addr_buf = addrs;
2271 for (i = 0; i < addrcnt; i++) {
2272 addr = (union sctp_addr *)addr_buf;
2273 af = sctp_get_af_specific(addr->v4.sin_family);
2274 addr_param_len = af->to_addr_param(addr, &addr_param);
2275
2276 totallen += paramlen;
2277 totallen += addr_param_len;
2278
2279 addr_buf += af->sockaddr_len;
2280 }
2281
2282 /* Create an asconf chunk with the required length. */
2283 retval = sctp_make_asconf(asoc, laddr, totallen);
2284 if (!retval)
2285 return NULL;
2286
2287 /* Add the address parameters to the asconf chunk. */
2288 addr_buf = addrs;
2289 for (i = 0; i < addrcnt; i++) {
2290 addr = (union sctp_addr *)addr_buf;
2291 af = sctp_get_af_specific(addr->v4.sin_family);
2292 addr_param_len = af->to_addr_param(addr, &addr_param);
2293 param.param_hdr.type = flags;
2294 param.param_hdr.length = htons(paramlen + addr_param_len);
2295 param.crr_id = i;
2296
2297 sctp_addto_chunk(retval, paramlen, &param);
2298 sctp_addto_chunk(retval, addr_param_len, &addr_param);
2299
2300 addr_buf += af->sockaddr_len;
2301 }
2302 return retval;
2303}
2304
2305/* ADDIP
2306 * 3.2.4 Set Primary IP Address
2307 * 0 1 2 3
2308 * 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
2309 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2310 * | Type =0xC004 | Length = Variable |
2311 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2312 * | ASCONF-Request Correlation ID |
2313 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2314 * | Address Parameter |
2315 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2316 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002317 * Create an ASCONF chunk with Set Primary IP address parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 */
2319struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
2320 union sctp_addr *addr)
2321{
2322 sctp_addip_param_t param;
2323 struct sctp_chunk *retval;
2324 int len = sizeof(param);
2325 union sctp_addr_param addrparam;
2326 int addrlen;
2327 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2328
2329 addrlen = af->to_addr_param(addr, &addrparam);
2330 if (!addrlen)
2331 return NULL;
2332 len += addrlen;
2333
2334 /* Create the chunk and make asconf header. */
2335 retval = sctp_make_asconf(asoc, addr, len);
2336 if (!retval)
2337 return NULL;
2338
2339 param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
2340 param.param_hdr.length = htons(len);
2341 param.crr_id = 0;
2342
2343 sctp_addto_chunk(retval, sizeof(param), &param);
2344 sctp_addto_chunk(retval, addrlen, &addrparam);
2345
2346 return retval;
2347}
2348
2349/* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2350 * 0 1 2 3
2351 * 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
2352 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2353 * | Type = 0x80 | Chunk Flags | Chunk Length |
2354 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2355 * | Serial Number |
2356 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2357 * | ASCONF Parameter Response#1 |
2358 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2359 * \ \
2360 * / .... /
2361 * \ \
2362 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2363 * | ASCONF Parameter Response#N |
2364 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2365 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002366 * Create an ASCONF_ACK chunk with enough space for the parameter responses.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 */
2368static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
2369 __u32 serial, int vparam_len)
2370{
2371 sctp_addiphdr_t asconf;
2372 struct sctp_chunk *retval;
2373 int length = sizeof(asconf) + vparam_len;
2374
2375 /* Create the chunk. */
2376 retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF_ACK, 0, length);
2377 if (!retval)
2378 return NULL;
2379
2380 asconf.serial = htonl(serial);
2381
2382 retval->subh.addip_hdr =
2383 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2384
2385 return retval;
2386}
2387
2388/* Add response parameters to an ASCONF_ACK chunk. */
Al Viro9f81bcd2006-11-20 17:26:34 -08002389static void sctp_add_asconf_response(struct sctp_chunk *chunk, __be32 crr_id,
Al Virodbc16db2006-11-20 17:01:42 -08002390 __be16 err_code, sctp_addip_param_t *asconf_param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391{
2392 sctp_addip_param_t ack_param;
2393 sctp_errhdr_t err_param;
2394 int asconf_param_len = 0;
2395 int err_param_len = 0;
Al Virodbc16db2006-11-20 17:01:42 -08002396 __be16 response_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
2398 if (SCTP_ERROR_NO_ERROR == err_code) {
2399 response_type = SCTP_PARAM_SUCCESS_REPORT;
2400 } else {
2401 response_type = SCTP_PARAM_ERR_CAUSE;
2402 err_param_len = sizeof(err_param);
2403 if (asconf_param)
2404 asconf_param_len =
2405 ntohs(asconf_param->param_hdr.length);
2406 }
2407
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002408 /* Add Success Indication or Error Cause Indication parameter. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 ack_param.param_hdr.type = response_type;
2410 ack_param.param_hdr.length = htons(sizeof(ack_param) +
2411 err_param_len +
2412 asconf_param_len);
2413 ack_param.crr_id = crr_id;
2414 sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
2415
2416 if (SCTP_ERROR_NO_ERROR == err_code)
2417 return;
2418
2419 /* Add Error Cause parameter. */
2420 err_param.cause = err_code;
2421 err_param.length = htons(err_param_len + asconf_param_len);
2422 sctp_addto_chunk(chunk, err_param_len, &err_param);
2423
2424 /* Add the failed TLV copied from ASCONF chunk. */
2425 if (asconf_param)
2426 sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
2427}
2428
2429/* Process a asconf parameter. */
Al Virodbc16db2006-11-20 17:01:42 -08002430static __be16 sctp_process_asconf_param(struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 struct sctp_chunk *asconf,
2432 sctp_addip_param_t *asconf_param)
2433{
2434 struct sctp_transport *peer;
2435 struct sctp_af *af;
2436 union sctp_addr addr;
2437 struct list_head *pos;
2438 union sctp_addr_param *addr_param;
Al Viro5f242a12006-11-20 17:05:23 -08002439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 addr_param = (union sctp_addr_param *)
2441 ((void *)asconf_param + sizeof(sctp_addip_param_t));
2442
2443 af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2444 if (unlikely(!af))
2445 return SCTP_ERROR_INV_PARAM;
2446
Al Virodd86d132006-11-20 17:11:13 -08002447 af->from_addr_param(&addr, addr_param, htons(asoc->peer.port), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 switch (asconf_param->param_hdr.type) {
2449 case SCTP_PARAM_ADD_IP:
2450 /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002451 * request and does not have the local resources to add this
2452 * new address to the association, it MUST return an Error
2453 * Cause TLV set to the new error code 'Operation Refused
2454 * Due to Resource Shortage'.
2455 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Al Virodd86d132006-11-20 17:11:13 -08002457 peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC, SCTP_UNCONFIRMED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 if (!peer)
2459 return SCTP_ERROR_RSRC_LOW;
2460
2461 /* Start the heartbeat timer. */
2462 if (!mod_timer(&peer->hb_timer, sctp_transport_timeout(peer)))
2463 sctp_transport_hold(peer);
2464 break;
2465 case SCTP_PARAM_DEL_IP:
2466 /* ADDIP 4.3 D7) If a request is received to delete the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002467 * last remaining IP address of a peer endpoint, the receiver
2468 * MUST send an Error Cause TLV with the error cause set to the
2469 * new error code 'Request to Delete Last Remaining IP Address'.
2470 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 pos = asoc->peer.transport_addr_list.next;
2472 if (pos->next == &asoc->peer.transport_addr_list)
2473 return SCTP_ERROR_DEL_LAST_IP;
2474
2475 /* ADDIP 4.3 D8) If a request is received to delete an IP
2476 * address which is also the source address of the IP packet
2477 * which contained the ASCONF chunk, the receiver MUST reject
2478 * this request. To reject the request the receiver MUST send
2479 * an Error Cause TLV set to the new error code 'Request to
2480 * Delete Source IP Address'
2481 */
Al Viro6a1e5f32006-11-20 17:12:25 -08002482 if (sctp_cmp_addr_exact(sctp_source(asconf), &addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 return SCTP_ERROR_DEL_SRC_IP;
2484
Al Virodd86d132006-11-20 17:11:13 -08002485 sctp_assoc_del_peer(asoc, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 break;
2487 case SCTP_PARAM_SET_PRIMARY:
Al Virodd86d132006-11-20 17:11:13 -08002488 peer = sctp_assoc_lookup_paddr(asoc, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 if (!peer)
2490 return SCTP_ERROR_INV_PARAM;
2491
2492 sctp_assoc_set_primary(asoc, peer);
2493 break;
2494 default:
2495 return SCTP_ERROR_INV_PARAM;
2496 break;
2497 }
2498
2499 return SCTP_ERROR_NO_ERROR;
2500}
2501
Wei Yongjun6f4c6182007-09-19 17:19:52 +08002502/* Verify the ASCONF packet before we process it. */
2503int sctp_verify_asconf(const struct sctp_association *asoc,
2504 struct sctp_paramhdr *param_hdr, void *chunk_end,
2505 struct sctp_paramhdr **errp) {
2506 sctp_addip_param_t *asconf_param;
2507 union sctp_params param;
2508 int length, plen;
2509
2510 param.v = (sctp_paramhdr_t *) param_hdr;
2511 while (param.v <= chunk_end - sizeof(sctp_paramhdr_t)) {
2512 length = ntohs(param.p->length);
2513 *errp = param.p;
2514
2515 if (param.v > chunk_end - length ||
2516 length < sizeof(sctp_paramhdr_t))
2517 return 0;
2518
2519 switch (param.p->type) {
2520 case SCTP_PARAM_ADD_IP:
2521 case SCTP_PARAM_DEL_IP:
2522 case SCTP_PARAM_SET_PRIMARY:
2523 asconf_param = (sctp_addip_param_t *)param.v;
2524 plen = ntohs(asconf_param->param_hdr.length);
2525 if (plen < sizeof(sctp_addip_param_t) +
2526 sizeof(sctp_paramhdr_t))
2527 return 0;
2528 break;
2529 case SCTP_PARAM_SUCCESS_REPORT:
2530 case SCTP_PARAM_ADAPTATION_LAYER_IND:
2531 if (length != sizeof(sctp_addip_param_t))
2532 return 0;
2533
2534 break;
2535 default:
2536 break;
2537 }
2538
2539 param.v += WORD_ROUND(length);
2540 }
2541
2542 if (param.v != chunk_end)
2543 return 0;
2544
2545 return 1;
2546}
2547
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002548/* Process an incoming ASCONF chunk with the next expected serial no. and
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 * return an ASCONF_ACK chunk to be sent in response.
2550 */
2551struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
2552 struct sctp_chunk *asconf)
2553{
2554 sctp_addiphdr_t *hdr;
2555 union sctp_addr_param *addr_param;
2556 sctp_addip_param_t *asconf_param;
2557 struct sctp_chunk *asconf_ack;
2558
Al Virodbc16db2006-11-20 17:01:42 -08002559 __be16 err_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 int length = 0;
2561 int chunk_len = asconf->skb->len;
2562 __u32 serial;
2563 int all_param_pass = 1;
2564
2565 hdr = (sctp_addiphdr_t *)asconf->skb->data;
2566 serial = ntohl(hdr->serial);
2567
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002568 /* Skip the addiphdr and store a pointer to address parameter. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 length = sizeof(sctp_addiphdr_t);
2570 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
2571 chunk_len -= length;
2572
2573 /* Skip the address parameter and store a pointer to the first
2574 * asconf paramter.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002575 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 length = ntohs(addr_param->v4.param_hdr.length);
2577 asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
2578 chunk_len -= length;
2579
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002580 /* create an ASCONF_ACK chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 * Based on the definitions of parameters, we know that the size of
2582 * ASCONF_ACK parameters are less than or equal to the twice of ASCONF
2583 * paramters.
2584 */
2585 asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 2);
2586 if (!asconf_ack)
2587 goto done;
2588
2589 /* Process the TLVs contained within the ASCONF chunk. */
2590 while (chunk_len > 0) {
2591 err_code = sctp_process_asconf_param(asoc, asconf,
2592 asconf_param);
2593 /* ADDIP 4.1 A7)
2594 * If an error response is received for a TLV parameter,
2595 * all TLVs with no response before the failed TLV are
2596 * considered successful if not reported. All TLVs after
2597 * the failed response are considered unsuccessful unless
2598 * a specific success indication is present for the parameter.
2599 */
2600 if (SCTP_ERROR_NO_ERROR != err_code)
2601 all_param_pass = 0;
2602
2603 if (!all_param_pass)
2604 sctp_add_asconf_response(asconf_ack,
2605 asconf_param->crr_id, err_code,
2606 asconf_param);
2607
2608 /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
2609 * an IP address sends an 'Out of Resource' in its response, it
2610 * MUST also fail any subsequent add or delete requests bundled
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002611 * in the ASCONF.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 */
2613 if (SCTP_ERROR_RSRC_LOW == err_code)
2614 goto done;
2615
2616 /* Move to the next ASCONF param. */
2617 length = ntohs(asconf_param->param_hdr.length);
2618 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
2619 length);
2620 chunk_len -= length;
2621 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002622
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623done:
2624 asoc->peer.addip_serial++;
2625
2626 /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002627 * after freeing the reference to old asconf ack if any.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 */
2629 if (asconf_ack) {
2630 if (asoc->addip_last_asconf_ack)
2631 sctp_chunk_free(asoc->addip_last_asconf_ack);
2632
2633 sctp_chunk_hold(asconf_ack);
2634 asoc->addip_last_asconf_ack = asconf_ack;
2635 }
2636
2637 return asconf_ack;
2638}
2639
2640/* Process a asconf parameter that is successfully acked. */
2641static int sctp_asconf_param_success(struct sctp_association *asoc,
2642 sctp_addip_param_t *asconf_param)
2643{
2644 struct sctp_af *af;
2645 union sctp_addr addr;
2646 struct sctp_bind_addr *bp = &asoc->base.bind_addr;
2647 union sctp_addr_param *addr_param;
2648 struct list_head *pos;
2649 struct sctp_transport *transport;
Sridhar Samudraladc022a92006-07-21 14:49:25 -07002650 struct sctp_sockaddr_entry *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 int retval = 0;
2652
2653 addr_param = (union sctp_addr_param *)
2654 ((void *)asconf_param + sizeof(sctp_addip_param_t));
2655
2656 /* We have checked the packet before, so we do not check again. */
2657 af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
Al Virodd86d132006-11-20 17:11:13 -08002658 af->from_addr_param(&addr, addr_param, htons(bp->port), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
2660 switch (asconf_param->param_hdr.type) {
2661 case SCTP_PARAM_ADD_IP:
Vlad Yasevich559cf712007-09-16 16:03:28 -07002662 /* This is always done in BH context with a socket lock
2663 * held, so the list can not change.
2664 */
2665 list_for_each_entry(saddr, &bp->address_list, list) {
Al Virodd86d132006-11-20 17:11:13 -08002666 if (sctp_cmp_addr_exact(&saddr->a, &addr))
Sridhar Samudraladc022a92006-07-21 14:49:25 -07002667 saddr->use_as_src = 1;
2668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 break;
2670 case SCTP_PARAM_DEL_IP:
Vlad Yasevich559cf712007-09-16 16:03:28 -07002671 retval = sctp_del_bind_addr(bp, &addr, call_rcu_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 list_for_each(pos, &asoc->peer.transport_addr_list) {
2673 transport = list_entry(pos, struct sctp_transport,
2674 transports);
Sridhar Samudraladc022a92006-07-21 14:49:25 -07002675 dst_release(transport->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 sctp_transport_route(transport, NULL,
2677 sctp_sk(asoc->base.sk));
2678 }
2679 break;
2680 default:
2681 break;
2682 }
2683
2684 return retval;
2685}
2686
2687/* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
2688 * for the given asconf parameter. If there is no response for this parameter,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002689 * return the error code based on the third argument 'no_err'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 * ADDIP 4.1
2691 * A7) If an error response is received for a TLV parameter, all TLVs with no
2692 * response before the failed TLV are considered successful if not reported.
2693 * All TLVs after the failed response are considered unsuccessful unless a
2694 * specific success indication is present for the parameter.
2695 */
Al Virodbc16db2006-11-20 17:01:42 -08002696static __be16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 sctp_addip_param_t *asconf_param,
2698 int no_err)
2699{
2700 sctp_addip_param_t *asconf_ack_param;
2701 sctp_errhdr_t *err_param;
2702 int length;
2703 int asconf_ack_len = asconf_ack->skb->len;
Al Virodbc16db2006-11-20 17:01:42 -08002704 __be16 err_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
2706 if (no_err)
2707 err_code = SCTP_ERROR_NO_ERROR;
2708 else
2709 err_code = SCTP_ERROR_REQ_REFUSED;
2710
2711 /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
2712 * the first asconf_ack parameter.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002713 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 length = sizeof(sctp_addiphdr_t);
2715 asconf_ack_param = (sctp_addip_param_t *)(asconf_ack->skb->data +
2716 length);
2717 asconf_ack_len -= length;
2718
2719 while (asconf_ack_len > 0) {
2720 if (asconf_ack_param->crr_id == asconf_param->crr_id) {
2721 switch(asconf_ack_param->param_hdr.type) {
2722 case SCTP_PARAM_SUCCESS_REPORT:
2723 return SCTP_ERROR_NO_ERROR;
2724 case SCTP_PARAM_ERR_CAUSE:
2725 length = sizeof(sctp_addip_param_t);
2726 err_param = (sctp_errhdr_t *)
2727 ((void *)asconf_ack_param + length);
2728 asconf_ack_len -= length;
2729 if (asconf_ack_len > 0)
2730 return err_param->cause;
2731 else
2732 return SCTP_ERROR_INV_PARAM;
2733 break;
2734 default:
2735 return SCTP_ERROR_INV_PARAM;
2736 }
2737 }
2738
2739 length = ntohs(asconf_ack_param->param_hdr.length);
2740 asconf_ack_param = (sctp_addip_param_t *)
2741 ((void *)asconf_ack_param + length);
2742 asconf_ack_len -= length;
2743 }
2744
2745 return err_code;
2746}
2747
2748/* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
2749int sctp_process_asconf_ack(struct sctp_association *asoc,
2750 struct sctp_chunk *asconf_ack)
2751{
2752 struct sctp_chunk *asconf = asoc->addip_last_asconf;
2753 union sctp_addr_param *addr_param;
2754 sctp_addip_param_t *asconf_param;
2755 int length = 0;
2756 int asconf_len = asconf->skb->len;
2757 int all_param_pass = 0;
2758 int no_err = 1;
2759 int retval = 0;
Al Virodbc16db2006-11-20 17:01:42 -08002760 __be16 err_code = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
2762 /* Skip the chunkhdr and addiphdr from the last asconf sent and store
2763 * a pointer to address parameter.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002764 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 length = sizeof(sctp_addip_chunk_t);
2766 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
2767 asconf_len -= length;
2768
2769 /* Skip the address parameter in the last asconf sent and store a
2770 * pointer to the first asconf paramter.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002771 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 length = ntohs(addr_param->v4.param_hdr.length);
2773 asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
2774 asconf_len -= length;
2775
2776 /* ADDIP 4.1
2777 * A8) If there is no response(s) to specific TLV parameter(s), and no
2778 * failures are indicated, then all request(s) are considered
2779 * successful.
2780 */
2781 if (asconf_ack->skb->len == sizeof(sctp_addiphdr_t))
2782 all_param_pass = 1;
2783
2784 /* Process the TLVs contained in the last sent ASCONF chunk. */
2785 while (asconf_len > 0) {
2786 if (all_param_pass)
2787 err_code = SCTP_ERROR_NO_ERROR;
2788 else {
2789 err_code = sctp_get_asconf_response(asconf_ack,
2790 asconf_param,
2791 no_err);
2792 if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
2793 no_err = 0;
2794 }
2795
2796 switch (err_code) {
2797 case SCTP_ERROR_NO_ERROR:
2798 retval = sctp_asconf_param_success(asoc, asconf_param);
2799 break;
2800
2801 case SCTP_ERROR_RSRC_LOW:
2802 retval = 1;
2803 break;
2804
2805 case SCTP_ERROR_INV_PARAM:
2806 /* Disable sending this type of asconf parameter in
2807 * future.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002808 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 asoc->peer.addip_disabled_mask |=
2810 asconf_param->param_hdr.type;
2811 break;
2812
2813 case SCTP_ERROR_REQ_REFUSED:
2814 case SCTP_ERROR_DEL_LAST_IP:
2815 case SCTP_ERROR_DEL_SRC_IP:
2816 default:
2817 break;
2818 }
2819
2820 /* Skip the processed asconf parameter and move to the next
2821 * one.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002822 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 length = ntohs(asconf_param->param_hdr.length);
2824 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
2825 length);
2826 asconf_len -= length;
2827 }
2828
2829 /* Free the cached last sent asconf chunk. */
2830 sctp_chunk_free(asconf);
2831 asoc->addip_last_asconf = NULL;
2832
2833 /* Send the next asconf chunk from the addip chunk queue. */
David S. Miller79af02c2005-07-08 21:47:49 -07002834 if (!list_empty(&asoc->addip_chunk_list)) {
2835 struct list_head *entry = asoc->addip_chunk_list.next;
2836 asconf = list_entry(entry, struct sctp_chunk, list);
2837
2838 list_del_init(entry);
2839
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 /* Hold the chunk until an ASCONF_ACK is received. */
2841 sctp_chunk_hold(asconf);
2842 if (sctp_primitive_ASCONF(asoc, asconf))
2843 sctp_chunk_free(asconf);
2844 else
2845 asoc->addip_last_asconf = asconf;
2846 }
2847
2848 return retval;
2849}
2850
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002851/* Make a FWD TSN chunk. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
2853 __u32 new_cum_tsn, size_t nstreams,
2854 struct sctp_fwdtsn_skip *skiplist)
2855{
2856 struct sctp_chunk *retval = NULL;
2857 struct sctp_fwdtsn_chunk *ftsn_chunk;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002858 struct sctp_fwdtsn_hdr ftsn_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 struct sctp_fwdtsn_skip skip;
2860 size_t hint;
2861 int i;
2862
2863 hint = (nstreams + 1) * sizeof(__u32);
2864
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 retval = sctp_make_chunk(asoc, SCTP_CID_FWD_TSN, 0, hint);
2866
2867 if (!retval)
2868 return NULL;
2869
2870 ftsn_chunk = (struct sctp_fwdtsn_chunk *)retval->subh.fwdtsn_hdr;
2871
2872 ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
2873 retval->subh.fwdtsn_hdr =
2874 sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
2875
2876 for (i = 0; i < nstreams; i++) {
2877 skip.stream = skiplist[i].stream;
2878 skip.ssn = skiplist[i].ssn;
2879 sctp_addto_chunk(retval, sizeof(skip), &skip);
2880 }
2881
2882 return retval;
2883}