blob: 17cb400ecd6aa73421635af1835501b27a6f58a8 [file] [log] [blame]
Vlad Yasevich60c778b2008-01-11 09:57:09 -05001/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (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 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05007 * This file is part of the SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050013 * This SCTP implementation is free software;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * 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 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050019 * This SCTP implementation is distributed in the hope that it
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
21 * ************************
22 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 * See the GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with GNU CC; see the file COPYING. If not, write to
27 * the Free Software Foundation, 59 Temple Place - Suite 330,
28 * Boston, MA 02111-1307, USA.
29 *
30 * Please send any bug reports or fixes you make to the
31 * email address(es):
32 * lksctp developers <lksctp-developers@lists.sourceforge.net>
33 *
34 * Or submit a bug report through the following website:
35 * http://www.sf.net/projects/lksctp
36 *
37 * Written or modified by:
38 * La Monte H.P. Yarroll <piggy@acm.org>
39 * Karl Knutson <karl@athena.chicago.il.us>
40 * C. Robin <chris@hundredacre.ac.uk>
41 * Jon Grimm <jgrimm@us.ibm.com>
42 * Xingang Guo <xingang.guo@intel.com>
43 * Dajiang Zhang <dajiang.zhang@nokia.com>
44 * Sridhar Samudrala <sri@us.ibm.com>
45 * Daisy Chang <daisyc@us.ibm.com>
46 * Ardelle Fan <ardelle.fan@intel.com>
47 * Kevin Gao <kevin.gao@intel.com>
48 *
49 * Any bugs reported given to us we will try to fix... any fixes shared will
50 * be incorporated into the next SCTP release.
51 */
52
53#include <linux/types.h>
54#include <linux/kernel.h>
55#include <linux/ip.h>
56#include <linux/ipv6.h>
57#include <linux/net.h>
58#include <linux/inet.h>
Christian Borntraegerebc3bbc2007-10-23 12:46:32 +020059#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/crypto.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090061#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <net/sock.h>
63
64#include <linux/skbuff.h>
65#include <linux/random.h> /* for get_random_bytes */
66#include <net/sctp/sctp.h>
67#include <net/sctp/sm.h>
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069SCTP_STATIC
70struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
71 __u8 type, __u8 flags, int paylen);
72static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
73 const struct sctp_association *asoc,
74 const struct sctp_chunk *init_chunk,
75 int *cookie_len,
76 const __u8 *raw_addrs, int addrs_len);
77static int sctp_process_param(struct sctp_association *asoc,
78 union sctp_params param,
79 const union sctp_addr *peer_addr,
Al Virodd0fc662005-10-07 07:46:04 +010080 gfp_t gfp);
Vlad Yasevich8ee4be32007-11-29 08:50:35 -050081static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
82 const void *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/* What was the inbound interface for this chunk? */
85int sctp_chunk_iif(const struct sctp_chunk *chunk)
86{
87 struct sctp_af *af;
88 int iif = 0;
89
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070090 af = sctp_get_af_specific(ipver2af(ip_hdr(chunk->skb)->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 if (af)
92 iif = af->skb_iif(chunk->skb);
93
94 return iif;
95}
96
97/* RFC 2960 3.3.2 Initiation (INIT) (1)
98 *
99 * Note 2: The ECN capable field is reserved for future use of
100 * Explicit Congestion Notification.
101 */
102static const struct sctp_paramhdr ecap_param = {
103 SCTP_PARAM_ECN_CAPABLE,
Harvey Harrison09640e62009-02-01 00:45:17 -0800104 cpu_to_be16(sizeof(struct sctp_paramhdr)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105};
106static const struct sctp_paramhdr prsctp_param = {
107 SCTP_PARAM_FWD_TSN_SUPPORT,
Harvey Harrison09640e62009-02-01 00:45:17 -0800108 cpu_to_be16(sizeof(struct sctp_paramhdr)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109};
110
111/* A helper to initialize to initialize an op error inside a
112 * provided chunk, as most cause codes will be embedded inside an
113 * abort chunk.
114 */
Al Viro5bf2db02006-11-20 16:59:45 -0800115void sctp_init_cause(struct sctp_chunk *chunk, __be16 cause_code,
Wei Yongjun00f1c2d2007-08-21 15:50:01 +0800116 size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 sctp_errhdr_t err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 __u16 len;
120
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900121 /* Cause code constants are now defined in network order. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 err.cause = cause_code;
123 len = sizeof(sctp_errhdr_t) + paylen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 err.length = htons(len);
Vlad Yasevich4a1c0102007-01-09 14:35:51 -0800125 chunk->subh.err_hdr = sctp_addto_chunk(chunk, sizeof(sctp_errhdr_t), &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128/* 3.3.2 Initiation (INIT) (1)
129 *
130 * This chunk is used to initiate a SCTP association between two
131 * endpoints. The format of the INIT chunk is shown below:
132 *
133 * 0 1 2 3
134 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
135 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
136 * | Type = 1 | Chunk Flags | Chunk Length |
137 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
138 * | Initiate Tag |
139 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
140 * | Advertised Receiver Window Credit (a_rwnd) |
141 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
142 * | Number of Outbound Streams | Number of Inbound Streams |
143 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
144 * | Initial TSN |
145 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
146 * \ \
147 * / Optional/Variable-Length Parameters /
148 * \ \
149 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
150 *
151 *
152 * The INIT chunk contains the following parameters. Unless otherwise
153 * noted, each parameter MUST only be included once in the INIT chunk.
154 *
155 * Fixed Parameters Status
156 * ----------------------------------------------
157 * Initiate Tag Mandatory
158 * Advertised Receiver Window Credit Mandatory
159 * Number of Outbound Streams Mandatory
160 * Number of Inbound Streams Mandatory
161 * Initial TSN Mandatory
162 *
163 * Variable Parameters Status Type Value
164 * -------------------------------------------------------------
165 * IPv4 Address (Note 1) Optional 5
166 * IPv6 Address (Note 1) Optional 6
167 * Cookie Preservative Optional 9
168 * Reserved for ECN Capable (Note 2) Optional 32768 (0x8000)
169 * Host Name Address (Note 3) Optional 11
170 * Supported Address Types (Note 4) Optional 12
171 */
172struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
173 const struct sctp_bind_addr *bp,
Al Virodd0fc662005-10-07 07:46:04 +0100174 gfp_t gfp, int vparam_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 sctp_inithdr_t init;
177 union sctp_params addrs;
178 size_t chunksize;
179 struct sctp_chunk *retval = NULL;
180 int num_types, addrs_len = 0;
181 struct sctp_sock *sp;
182 sctp_supported_addrs_param_t sat;
Al Viro3dbe8652006-11-20 17:25:49 -0800183 __be16 types[2];
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800184 sctp_adaptation_ind_param_t aiparam;
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700185 sctp_supported_ext_param_t ext_param;
186 int num_ext = 0;
187 __u8 extensions[3];
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700188 sctp_paramhdr_t *auth_chunks = NULL,
189 *auth_hmacs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 /* RFC 2960 3.3.2 Initiation (INIT) (1)
192 *
193 * Note 1: The INIT chunks can contain multiple addresses that
194 * can be IPv4 and/or IPv6 in any combination.
195 */
196 retval = NULL;
197
198 /* Convert the provided bind address list to raw format. */
199 addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, gfp);
200
201 init.init_tag = htonl(asoc->c.my_vtag);
202 init.a_rwnd = htonl(asoc->rwnd);
203 init.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
204 init.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
205 init.initial_tsn = htonl(asoc->c.initial_tsn);
206
207 /* How many address types are needed? */
208 sp = sctp_sk(asoc->base.sk);
209 num_types = sp->pf->supported_addrs(sp, types);
210
211 chunksize = sizeof(init) + addrs_len + SCTP_SAT_LEN(num_types);
212 chunksize += sizeof(ecap_param);
Vlad Yasevich8ee4be32007-11-29 08:50:35 -0500213
Vlad Yasevich036b5792008-01-07 00:28:16 -0800214 if (sctp_prsctp_enable)
215 chunksize += sizeof(prsctp_param);
216
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700217 /* ADDIP: Section 4.2.7:
218 * An implementation supporting this extension [ADDIP] MUST list
219 * the ASCONF,the ASCONF-ACK, and the AUTH chunks in its INIT and
220 * INIT-ACK parameters.
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700221 */
222 if (sctp_addip_enable) {
223 extensions[num_ext] = SCTP_CID_ASCONF;
224 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
225 num_ext += 2;
226 }
227
malc6fc791e2009-03-12 09:49:20 +0000228 if (sp->adaptation_ind)
229 chunksize += sizeof(aiparam);
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 chunksize += vparam_len;
232
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700233 /* Account for AUTH related parameters */
234 if (sctp_auth_enable) {
235 /* Add random parameter length*/
236 chunksize += sizeof(asoc->c.auth_random);
237
238 /* Add HMACS parameter length if any were defined */
239 auth_hmacs = (sctp_paramhdr_t *)asoc->c.auth_hmacs;
240 if (auth_hmacs->length)
241 chunksize += ntohs(auth_hmacs->length);
242 else
243 auth_hmacs = NULL;
244
245 /* Add CHUNKS parameter length */
246 auth_chunks = (sctp_paramhdr_t *)asoc->c.auth_chunks;
247 if (auth_chunks->length)
248 chunksize += ntohs(auth_chunks->length);
249 else
Vlad Yasevich9baffaa2007-11-29 08:44:34 -0500250 auth_chunks = NULL;
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700251
252 extensions[num_ext] = SCTP_CID_AUTH;
253 num_ext += 1;
254 }
255
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700256 /* If we have any extensions to report, account for that */
257 if (num_ext)
258 chunksize += sizeof(sctp_supported_ext_param_t) + num_ext;
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 /* RFC 2960 3.3.2 Initiation (INIT) (1)
261 *
262 * Note 3: An INIT chunk MUST NOT contain more than one Host
263 * Name address parameter. Moreover, the sender of the INIT
264 * MUST NOT combine any other address types with the Host Name
265 * address in the INIT. The receiver of INIT MUST ignore any
266 * other address types if the Host Name address parameter is
267 * present in the received INIT chunk.
268 *
269 * PLEASE DO NOT FIXME [This version does not support Host Name.]
270 */
271
272 retval = sctp_make_chunk(asoc, SCTP_CID_INIT, 0, chunksize);
273 if (!retval)
274 goto nodata;
275
276 retval->subh.init_hdr =
277 sctp_addto_chunk(retval, sizeof(init), &init);
278 retval->param_hdr.v =
279 sctp_addto_chunk(retval, addrs_len, addrs.v);
280
281 /* RFC 2960 3.3.2 Initiation (INIT) (1)
282 *
283 * Note 4: This parameter, when present, specifies all the
284 * address types the sending endpoint can support. The absence
285 * of this parameter indicates that the sending endpoint can
286 * support any address type.
287 */
288 sat.param_hdr.type = SCTP_PARAM_SUPPORTED_ADDRESS_TYPES;
289 sat.param_hdr.length = htons(SCTP_SAT_LEN(num_types));
290 sctp_addto_chunk(retval, sizeof(sat), &sat);
291 sctp_addto_chunk(retval, num_types * sizeof(__u16), &types);
292
293 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700294
Joe Perches7aa1b542007-12-20 14:03:52 -0800295 /* Add the supported extensions parameter. Be nice and add this
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700296 * fist before addiding the parameters for the extensions themselves
297 */
298 if (num_ext) {
299 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
300 ext_param.param_hdr.length =
301 htons(sizeof(sctp_supported_ext_param_t) + num_ext);
302 sctp_addto_chunk(retval, sizeof(sctp_supported_ext_param_t),
303 &ext_param);
Vlad Yasevich8ee4be32007-11-29 08:50:35 -0500304 sctp_addto_param(retval, num_ext, extensions);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700305 }
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (sctp_prsctp_enable)
308 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700309
malc6fc791e2009-03-12 09:49:20 +0000310 if (sp->adaptation_ind) {
311 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
312 aiparam.param_hdr.length = htons(sizeof(aiparam));
313 aiparam.adaptation_ind = htonl(sp->adaptation_ind);
314 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
315 }
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700316
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700317 /* Add SCTP-AUTH chunks to the parameter list */
318 if (sctp_auth_enable) {
319 sctp_addto_chunk(retval, sizeof(asoc->c.auth_random),
320 asoc->c.auth_random);
321 if (auth_hmacs)
322 sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
323 auth_hmacs);
324 if (auth_chunks)
325 sctp_addto_chunk(retval, ntohs(auth_chunks->length),
326 auth_chunks);
327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328nodata:
Jesper Juhla51482b2005-11-08 09:41:34 -0800329 kfree(addrs.v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return retval;
331}
332
333struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
334 const struct sctp_chunk *chunk,
Al Virodd0fc662005-10-07 07:46:04 +0100335 gfp_t gfp, int unkparam_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 sctp_inithdr_t initack;
338 struct sctp_chunk *retval;
339 union sctp_params addrs;
malc6fc791e2009-03-12 09:49:20 +0000340 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 int addrs_len;
342 sctp_cookie_param_t *cookie;
343 int cookie_len;
344 size_t chunksize;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800345 sctp_adaptation_ind_param_t aiparam;
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700346 sctp_supported_ext_param_t ext_param;
347 int num_ext = 0;
348 __u8 extensions[3];
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700349 sctp_paramhdr_t *auth_chunks = NULL,
350 *auth_hmacs = NULL,
351 *auth_random = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 retval = NULL;
354
355 /* Note: there may be no addresses to embed. */
356 addrs = sctp_bind_addrs_to_raw(&asoc->base.bind_addr, &addrs_len, gfp);
357
358 initack.init_tag = htonl(asoc->c.my_vtag);
359 initack.a_rwnd = htonl(asoc->rwnd);
360 initack.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
361 initack.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
362 initack.initial_tsn = htonl(asoc->c.initial_tsn);
363
364 /* FIXME: We really ought to build the cookie right
365 * into the packet instead of allocating more fresh memory.
366 */
367 cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
368 addrs.v, addrs_len);
369 if (!cookie)
370 goto nomem_cookie;
371
372 /* Calculate the total size of allocation, include the reserved
373 * space for reporting unknown parameters if it is specified.
374 */
malc6fc791e2009-03-12 09:49:20 +0000375 sp = sctp_sk(asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;
377
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900378 /* Tell peer that we'll do ECN only if peer advertised such cap. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (asoc->peer.ecn_capable)
380 chunksize += sizeof(ecap_param);
381
Wei Yongjun5ffad5a2009-03-12 09:49:18 +0000382 if (asoc->peer.prsctp_capable)
Vlad Yasevich036b5792008-01-07 00:28:16 -0800383 chunksize += sizeof(prsctp_param);
384
Wei Yongjun5ffad5a2009-03-12 09:49:18 +0000385 if (asoc->peer.asconf_capable) {
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700386 extensions[num_ext] = SCTP_CID_ASCONF;
387 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
388 num_ext += 2;
389 }
390
malc6fc791e2009-03-12 09:49:20 +0000391 if (sp->adaptation_ind)
392 chunksize += sizeof(aiparam);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700394 if (asoc->peer.auth_capable) {
395 auth_random = (sctp_paramhdr_t *)asoc->c.auth_random;
396 chunksize += ntohs(auth_random->length);
397
398 auth_hmacs = (sctp_paramhdr_t *)asoc->c.auth_hmacs;
399 if (auth_hmacs->length)
400 chunksize += ntohs(auth_hmacs->length);
401 else
402 auth_hmacs = NULL;
403
404 auth_chunks = (sctp_paramhdr_t *)asoc->c.auth_chunks;
405 if (auth_chunks->length)
406 chunksize += ntohs(auth_chunks->length);
407 else
408 auth_chunks = NULL;
409
410 extensions[num_ext] = SCTP_CID_AUTH;
411 num_ext += 1;
412 }
413
Vlad Yasevich8ee4be32007-11-29 08:50:35 -0500414 if (num_ext)
415 chunksize += sizeof(sctp_supported_ext_param_t) + num_ext;
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* Now allocate and fill out the chunk. */
418 retval = sctp_make_chunk(asoc, SCTP_CID_INIT_ACK, 0, chunksize);
419 if (!retval)
420 goto nomem_chunk;
421
422 /* Per the advice in RFC 2960 6.4, send this reply to
423 * the source of the INIT packet.
424 */
425 retval->transport = chunk->transport;
426 retval->subh.init_hdr =
427 sctp_addto_chunk(retval, sizeof(initack), &initack);
428 retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
429 sctp_addto_chunk(retval, cookie_len, cookie);
430 if (asoc->peer.ecn_capable)
431 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700432 if (num_ext) {
433 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
434 ext_param.param_hdr.length =
435 htons(sizeof(sctp_supported_ext_param_t) + num_ext);
436 sctp_addto_chunk(retval, sizeof(sctp_supported_ext_param_t),
437 &ext_param);
Vlad Yasevich8ee4be32007-11-29 08:50:35 -0500438 sctp_addto_param(retval, num_ext, extensions);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (asoc->peer.prsctp_capable)
441 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
442
malc6fc791e2009-03-12 09:49:20 +0000443 if (sp->adaptation_ind) {
444 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
445 aiparam.param_hdr.length = htons(sizeof(aiparam));
446 aiparam.adaptation_ind = htonl(sp->adaptation_ind);
447 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700450 if (asoc->peer.auth_capable) {
451 sctp_addto_chunk(retval, ntohs(auth_random->length),
452 auth_random);
453 if (auth_hmacs)
454 sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
455 auth_hmacs);
456 if (auth_chunks)
457 sctp_addto_chunk(retval, ntohs(auth_chunks->length),
458 auth_chunks);
459 }
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 /* We need to remove the const qualifier at this point. */
462 retval->asoc = (struct sctp_association *) asoc;
463
464 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
465 *
466 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
467 * HEARTBEAT ACK, * etc.) to the same destination transport
468 * address from which it received the DATA or control chunk
469 * to which it is replying.
470 *
471 * [INIT ACK back to where the INIT came from.]
472 */
473 if (chunk)
474 retval->transport = chunk->transport;
475
476nomem_chunk:
477 kfree(cookie);
478nomem_cookie:
Jesper Juhla51482b2005-11-08 09:41:34 -0800479 kfree(addrs.v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return retval;
481}
482
483/* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
484 *
485 * This chunk is used only during the initialization of an association.
486 * It is sent by the initiator of an association to its peer to complete
487 * the initialization process. This chunk MUST precede any DATA chunk
488 * sent within the association, but MAY be bundled with one or more DATA
489 * chunks in the same packet.
490 *
491 * 0 1 2 3
492 * 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
493 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
494 * | Type = 10 |Chunk Flags | Length |
495 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
496 * / Cookie /
497 * \ \
498 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
499 *
500 * Chunk Flags: 8 bit
501 *
502 * Set to zero on transmit and ignored on receipt.
503 *
504 * Length: 16 bits (unsigned integer)
505 *
506 * Set to the size of the chunk in bytes, including the 4 bytes of
507 * the chunk header and the size of the Cookie.
508 *
509 * Cookie: variable size
510 *
511 * This field must contain the exact cookie received in the
512 * State Cookie parameter from the previous INIT ACK.
513 *
514 * An implementation SHOULD make the cookie as small as possible
515 * to insure interoperability.
516 */
517struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
518 const struct sctp_chunk *chunk)
519{
520 struct sctp_chunk *retval;
521 void *cookie;
522 int cookie_len;
523
524 cookie = asoc->peer.cookie;
525 cookie_len = asoc->peer.cookie_len;
526
527 /* Build a cookie echo chunk. */
528 retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ECHO, 0, cookie_len);
529 if (!retval)
530 goto nodata;
531 retval->subh.cookie_hdr =
532 sctp_addto_chunk(retval, cookie_len, cookie);
533
534 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
535 *
536 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
537 * HEARTBEAT ACK, * etc.) to the same destination transport
538 * address from which it * received the DATA or control chunk
539 * to which it is replying.
540 *
541 * [COOKIE ECHO back to where the INIT ACK came from.]
542 */
543 if (chunk)
544 retval->transport = chunk->transport;
545
546nodata:
547 return retval;
548}
549
550/* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
551 *
552 * This chunk is used only during the initialization of an
553 * association. It is used to acknowledge the receipt of a COOKIE
554 * ECHO chunk. This chunk MUST precede any DATA or SACK chunk sent
555 * within the association, but MAY be bundled with one or more DATA
556 * chunks or SACK chunk in the same SCTP packet.
557 *
558 * 0 1 2 3
559 * 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
560 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
561 * | Type = 11 |Chunk Flags | Length = 4 |
562 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
563 *
564 * Chunk Flags: 8 bits
565 *
566 * Set to zero on transmit and ignored on receipt.
567 */
568struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
569 const struct sctp_chunk *chunk)
570{
571 struct sctp_chunk *retval;
572
573 retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ACK, 0, 0);
574
575 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
576 *
577 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
578 * HEARTBEAT ACK, * etc.) to the same destination transport
579 * address from which it * received the DATA or control chunk
580 * to which it is replying.
581 *
582 * [COOKIE ACK back to where the COOKIE ECHO came from.]
583 */
584 if (retval && chunk)
585 retval->transport = chunk->transport;
586
587 return retval;
588}
589
590/*
591 * Appendix A: Explicit Congestion Notification:
592 * CWR:
593 *
594 * RFC 2481 details a specific bit for a sender to send in the header of
595 * its next outbound TCP segment to indicate to its peer that it has
596 * reduced its congestion window. This is termed the CWR bit. For
597 * SCTP the same indication is made by including the CWR chunk.
598 * This chunk contains one data element, i.e. the TSN number that
599 * was sent in the ECNE chunk. This element represents the lowest
600 * TSN number in the datagram that was originally marked with the
601 * CE bit.
602 *
603 * 0 1 2 3
604 * 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
605 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
606 * | Chunk Type=13 | Flags=00000000| Chunk Length = 8 |
607 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
608 * | Lowest TSN Number |
609 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
610 *
611 * Note: The CWR is considered a Control chunk.
612 */
613struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
614 const __u32 lowest_tsn,
615 const struct sctp_chunk *chunk)
616{
617 struct sctp_chunk *retval;
618 sctp_cwrhdr_t cwr;
619
620 cwr.lowest_tsn = htonl(lowest_tsn);
621 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_CWR, 0,
622 sizeof(sctp_cwrhdr_t));
623
624 if (!retval)
625 goto nodata;
626
627 retval->subh.ecn_cwr_hdr =
628 sctp_addto_chunk(retval, sizeof(cwr), &cwr);
629
630 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
631 *
632 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
633 * HEARTBEAT ACK, * etc.) to the same destination transport
634 * address from which it * received the DATA or control chunk
635 * to which it is replying.
636 *
637 * [Report a reduced congestion window back to where the ECNE
638 * came from.]
639 */
640 if (chunk)
641 retval->transport = chunk->transport;
642
643nodata:
644 return retval;
645}
646
647/* Make an ECNE chunk. This is a congestion experienced report. */
648struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
649 const __u32 lowest_tsn)
650{
651 struct sctp_chunk *retval;
652 sctp_ecnehdr_t ecne;
653
654 ecne.lowest_tsn = htonl(lowest_tsn);
655 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_ECNE, 0,
656 sizeof(sctp_ecnehdr_t));
657 if (!retval)
658 goto nodata;
659 retval->subh.ecne_hdr =
660 sctp_addto_chunk(retval, sizeof(ecne), &ecne);
661
662nodata:
663 return retval;
664}
665
666/* Make a DATA chunk for the given association from the provided
667 * parameters. However, do not populate the data payload.
668 */
669struct sctp_chunk *sctp_make_datafrag_empty(struct sctp_association *asoc,
670 const struct sctp_sndrcvinfo *sinfo,
671 int data_len, __u8 flags, __u16 ssn)
672{
673 struct sctp_chunk *retval;
674 struct sctp_datahdr dp;
675 int chunk_len;
676
677 /* We assign the TSN as LATE as possible, not here when
678 * creating the chunk.
679 */
680 dp.tsn = 0;
681 dp.stream = htons(sinfo->sinfo_stream);
682 dp.ppid = sinfo->sinfo_ppid;
683
684 /* Set the flags for an unordered send. */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -0700685 if (sinfo->sinfo_flags & SCTP_UNORDERED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 flags |= SCTP_DATA_UNORDERED;
687 dp.ssn = 0;
688 } else
689 dp.ssn = htons(ssn);
690
691 chunk_len = sizeof(dp) + data_len;
692 retval = sctp_make_chunk(asoc, SCTP_CID_DATA, flags, chunk_len);
693 if (!retval)
694 goto nodata;
695
696 retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
697 memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
698
699nodata:
700 return retval;
701}
702
703/* Create a selective ackowledgement (SACK) for the given
704 * association. This reports on which TSN's we've seen to date,
705 * including duplicates and gaps.
706 */
707struct sctp_chunk *sctp_make_sack(const struct sctp_association *asoc)
708{
709 struct sctp_chunk *retval;
710 struct sctp_sackhdr sack;
711 int len;
712 __u32 ctsn;
713 __u16 num_gabs, num_dup_tsns;
714 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
Vlad Yasevich02015182008-10-08 14:19:01 -0700715 struct sctp_gap_ack_block gabs[SCTP_MAX_GABS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Vlad Yasevich02015182008-10-08 14:19:01 -0700717 memset(gabs, 0, sizeof(gabs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 ctsn = sctp_tsnmap_get_ctsn(map);
719 SCTP_DEBUG_PRINTK("sackCTSNAck sent: 0x%x.\n", ctsn);
720
721 /* How much room is needed in the chunk? */
Vlad Yasevich02015182008-10-08 14:19:01 -0700722 num_gabs = sctp_tsnmap_num_gabs(map, gabs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 num_dup_tsns = sctp_tsnmap_num_dups(map);
724
725 /* Initialize the SACK header. */
726 sack.cum_tsn_ack = htonl(ctsn);
727 sack.a_rwnd = htonl(asoc->a_rwnd);
728 sack.num_gap_ack_blocks = htons(num_gabs);
729 sack.num_dup_tsns = htons(num_dup_tsns);
730
731 len = sizeof(sack)
732 + sizeof(struct sctp_gap_ack_block) * num_gabs
733 + sizeof(__u32) * num_dup_tsns;
734
735 /* Create the chunk. */
736 retval = sctp_make_chunk(asoc, SCTP_CID_SACK, 0, len);
737 if (!retval)
738 goto nodata;
739
740 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
741 *
742 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
743 * HEARTBEAT ACK, etc.) to the same destination transport
744 * address from which it received the DATA or control chunk to
745 * which it is replying. This rule should also be followed if
746 * the endpoint is bundling DATA chunks together with the
747 * reply chunk.
748 *
749 * However, when acknowledging multiple DATA chunks received
750 * in packets from different source addresses in a single
751 * SACK, the SACK chunk may be transmitted to one of the
752 * destination transport addresses from which the DATA or
753 * control chunks being acknowledged were received.
754 *
755 * [BUG: We do not implement the following paragraph.
756 * Perhaps we should remember the last transport we used for a
757 * SACK and avoid that (if possible) if we have seen any
758 * duplicates. --piggy]
759 *
760 * When a receiver of a duplicate DATA chunk sends a SACK to a
761 * multi- homed endpoint it MAY be beneficial to vary the
762 * destination address and not use the source address of the
763 * DATA chunk. The reason being that receiving a duplicate
764 * from a multi-homed endpoint might indicate that the return
765 * path (as specified in the source address of the DATA chunk)
766 * for the SACK is broken.
767 *
768 * [Send to the address from which we last received a DATA chunk.]
769 */
770 retval->transport = asoc->peer.last_data_from;
771
772 retval->subh.sack_hdr =
773 sctp_addto_chunk(retval, sizeof(sack), &sack);
774
775 /* Add the gap ack block information. */
776 if (num_gabs)
777 sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
Vlad Yasevich02015182008-10-08 14:19:01 -0700778 gabs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 /* Add the duplicate TSN information. */
781 if (num_dup_tsns)
782 sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
783 sctp_tsnmap_get_dups(map));
784
785nodata:
786 return retval;
787}
788
789/* Make a SHUTDOWN chunk. */
790struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
791 const struct sctp_chunk *chunk)
792{
793 struct sctp_chunk *retval;
794 sctp_shutdownhdr_t shut;
795 __u32 ctsn;
796
797 ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
798 shut.cum_tsn_ack = htonl(ctsn);
799
800 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN, 0,
801 sizeof(sctp_shutdownhdr_t));
802 if (!retval)
803 goto nodata;
804
805 retval->subh.shutdown_hdr =
806 sctp_addto_chunk(retval, sizeof(shut), &shut);
807
808 if (chunk)
809 retval->transport = chunk->transport;
810nodata:
811 return retval;
812}
813
814struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
815 const struct sctp_chunk *chunk)
816{
817 struct sctp_chunk *retval;
818
819 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0);
820
821 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
822 *
823 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
824 * HEARTBEAT ACK, * etc.) to the same destination transport
825 * address from which it * received the DATA or control chunk
826 * to which it is replying.
827 *
828 * [ACK back to where the SHUTDOWN came from.]
829 */
830 if (retval && chunk)
831 retval->transport = chunk->transport;
832
833 return retval;
834}
835
836struct sctp_chunk *sctp_make_shutdown_complete(
837 const struct sctp_association *asoc,
838 const struct sctp_chunk *chunk)
839{
840 struct sctp_chunk *retval;
841 __u8 flags = 0;
842
Jerome Forissier047a2422005-04-28 11:58:43 -0700843 /* Set the T-bit if we have no association (vtag will be
844 * reflected)
845 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
847
848 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags, 0);
849
850 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
851 *
852 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
853 * HEARTBEAT ACK, * etc.) to the same destination transport
854 * address from which it * received the DATA or control chunk
855 * to which it is replying.
856 *
857 * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
858 * came from.]
859 */
860 if (retval && chunk)
861 retval->transport = chunk->transport;
862
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900863 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
866/* Create an ABORT. Note that we set the T bit if we have no
Jerome Forissier047a2422005-04-28 11:58:43 -0700867 * association, except when responding to an INIT (sctpimpguide 2.41).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 */
869struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
870 const struct sctp_chunk *chunk,
871 const size_t hint)
872{
873 struct sctp_chunk *retval;
874 __u8 flags = 0;
875
Jerome Forissier047a2422005-04-28 11:58:43 -0700876 /* Set the T-bit if we have no association and 'chunk' is not
877 * an INIT (vtag will be reflected).
878 */
879 if (!asoc) {
880 if (chunk && chunk->chunk_hdr &&
881 chunk->chunk_hdr->type == SCTP_CID_INIT)
882 flags = 0;
883 else
884 flags = SCTP_CHUNK_FLAG_T;
885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 retval = sctp_make_chunk(asoc, SCTP_CID_ABORT, flags, hint);
888
889 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
890 *
891 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
892 * HEARTBEAT ACK, * etc.) to the same destination transport
893 * address from which it * received the DATA or control chunk
894 * to which it is replying.
895 *
896 * [ABORT back to where the offender came from.]
897 */
898 if (retval && chunk)
899 retval->transport = chunk->transport;
900
901 return retval;
902}
903
904/* Helper to create ABORT with a NO_USER_DATA error. */
905struct sctp_chunk *sctp_make_abort_no_data(
906 const struct sctp_association *asoc,
907 const struct sctp_chunk *chunk, __u32 tsn)
908{
909 struct sctp_chunk *retval;
Al Viro9f81bcd2006-11-20 17:26:34 -0800910 __be32 payload;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t)
913 + sizeof(tsn));
914
915 if (!retval)
916 goto no_mem;
917
918 /* Put the tsn back into network byte order. */
919 payload = htonl(tsn);
Wei Yongjun00f1c2d2007-08-21 15:50:01 +0800920 sctp_init_cause(retval, SCTP_ERROR_NO_DATA, sizeof(payload));
921 sctp_addto_chunk(retval, sizeof(payload), (const void *)&payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
924 *
925 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
926 * HEARTBEAT ACK, * etc.) to the same destination transport
927 * address from which it * received the DATA or control chunk
928 * to which it is replying.
929 *
930 * [ABORT back to where the offender came from.]
931 */
932 if (chunk)
933 retval->transport = chunk->transport;
934
935no_mem:
936 return retval;
937}
938
939/* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error. */
940struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
Sridhar Samudralac164a9b2006-08-22 11:50:39 -0700941 const struct msghdr *msg,
942 size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944 struct sctp_chunk *retval;
Sridhar Samudralac164a9b2006-08-22 11:50:39 -0700945 void *payload = NULL;
946 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Sridhar Samudralac164a9b2006-08-22 11:50:39 -0700948 retval = sctp_make_abort(asoc, NULL, sizeof(sctp_errhdr_t) + paylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (!retval)
950 goto err_chunk;
951
952 if (paylen) {
953 /* Put the msg_iov together into payload. */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -0700954 payload = kmalloc(paylen, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (!payload)
956 goto err_payload;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Sridhar Samudralac164a9b2006-08-22 11:50:39 -0700958 err = memcpy_fromiovec(payload, msg->msg_iov, paylen);
959 if (err < 0)
960 goto err_copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
962
Wei Yongjun00f1c2d2007-08-21 15:50:01 +0800963 sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, paylen);
964 sctp_addto_chunk(retval, paylen, payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 if (paylen)
967 kfree(payload);
968
969 return retval;
970
971err_copy:
972 kfree(payload);
973err_payload:
974 sctp_chunk_free(retval);
975 retval = NULL;
976err_chunk:
977 return retval;
978}
979
Adrian Bunk5c94bf82007-09-12 15:16:21 +0200980/* Append bytes to the end of a parameter. Will panic if chunk is not big
981 * enough.
982 */
983static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
984 const void *data)
985{
986 void *target;
987 int chunklen = ntohs(chunk->chunk_hdr->length);
988
989 target = skb_put(chunk->skb, len);
990
Vlad Yasevich6383cfb2009-11-23 15:53:56 -0500991 if (data)
992 memcpy(target, data, len);
993 else
994 memset(target, 0, len);
Adrian Bunk5c94bf82007-09-12 15:16:21 +0200995
996 /* Adjust the chunk length field. */
997 chunk->chunk_hdr->length = htons(chunklen + len);
998 chunk->chunk_end = skb_tail_pointer(chunk->skb);
999
1000 return target;
1001}
1002
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001003/* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004struct sctp_chunk *sctp_make_abort_violation(
1005 const struct sctp_association *asoc,
1006 const struct sctp_chunk *chunk,
1007 const __u8 *payload,
1008 const size_t paylen)
1009{
1010 struct sctp_chunk *retval;
1011 struct sctp_paramhdr phdr;
1012
1013 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t) + paylen
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001014 + sizeof(sctp_paramhdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (!retval)
1016 goto end;
1017
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001018 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, paylen
1019 + sizeof(sctp_paramhdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 phdr.type = htons(chunk->chunk_hdr->type);
1022 phdr.length = chunk->chunk_hdr->length;
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001023 sctp_addto_chunk(retval, paylen, payload);
1024 sctp_addto_param(retval, sizeof(sctp_paramhdr_t), &phdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026end:
1027 return retval;
1028}
1029
Wei Yongjunba016672008-09-30 05:32:24 -07001030struct sctp_chunk *sctp_make_violation_paramlen(
1031 const struct sctp_association *asoc,
1032 const struct sctp_chunk *chunk,
1033 struct sctp_paramhdr *param)
1034{
1035 struct sctp_chunk *retval;
1036 static const char error[] = "The following parameter had invalid length:";
1037 size_t payload_len = sizeof(error) + sizeof(sctp_errhdr_t) +
1038 sizeof(sctp_paramhdr_t);
1039
1040 retval = sctp_make_abort(asoc, chunk, payload_len);
1041 if (!retval)
1042 goto nodata;
1043
1044 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION,
1045 sizeof(error) + sizeof(sctp_paramhdr_t));
1046 sctp_addto_chunk(retval, sizeof(error), error);
1047 sctp_addto_param(retval, sizeof(sctp_paramhdr_t), param);
1048
1049nodata:
1050 return retval;
1051}
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053/* Make a HEARTBEAT chunk. */
1054struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
1055 const struct sctp_transport *transport,
1056 const void *payload, const size_t paylen)
1057{
1058 struct sctp_chunk *retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT,
1059 0, paylen);
1060
1061 if (!retval)
1062 goto nodata;
1063
1064 /* Cast away the 'const', as this is just telling the chunk
1065 * what transport it belongs to.
1066 */
1067 retval->transport = (struct sctp_transport *) transport;
1068 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
1069
1070nodata:
1071 return retval;
1072}
1073
1074struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
1075 const struct sctp_chunk *chunk,
1076 const void *payload, const size_t paylen)
1077{
1078 struct sctp_chunk *retval;
1079
1080 retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen);
1081 if (!retval)
1082 goto nodata;
1083
1084 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
1085
1086 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1087 *
1088 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1089 * HEARTBEAT ACK, * etc.) to the same destination transport
1090 * address from which it * received the DATA or control chunk
1091 * to which it is replying.
1092 *
1093 * [HBACK back to where the HEARTBEAT came from.]
1094 */
1095 if (chunk)
1096 retval->transport = chunk->transport;
1097
1098nodata:
1099 return retval;
1100}
1101
1102/* Create an Operation Error chunk with the specified space reserved.
1103 * This routine can be used for containing multiple causes in the chunk.
1104 */
1105static struct sctp_chunk *sctp_make_op_error_space(
1106 const struct sctp_association *asoc,
1107 const struct sctp_chunk *chunk,
1108 size_t size)
1109{
1110 struct sctp_chunk *retval;
1111
1112 retval = sctp_make_chunk(asoc, SCTP_CID_ERROR, 0,
1113 sizeof(sctp_errhdr_t) + size);
1114 if (!retval)
1115 goto nodata;
1116
1117 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1118 *
1119 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1120 * HEARTBEAT ACK, etc.) to the same destination transport
1121 * address from which it received the DATA or control chunk
1122 * to which it is replying.
1123 *
1124 */
1125 if (chunk)
1126 retval->transport = chunk->transport;
1127
1128nodata:
1129 return retval;
1130}
1131
1132/* Create an Operation Error chunk. */
1133struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
1134 const struct sctp_chunk *chunk,
Al Viro63706c52006-11-20 17:00:05 -08001135 __be16 cause_code, const void *payload,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001136 size_t paylen, size_t reserve_tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
1138 struct sctp_chunk *retval;
1139
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001140 retval = sctp_make_op_error_space(asoc, chunk, paylen + reserve_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (!retval)
1142 goto nodata;
1143
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001144 sctp_init_cause(retval, cause_code, paylen + reserve_tail);
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001145 sctp_addto_chunk(retval, paylen, payload);
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001146 if (reserve_tail)
1147 sctp_addto_param(retval, reserve_tail, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149nodata:
1150 return retval;
1151}
1152
Vlad Yasevich4cd57c82007-09-16 19:32:45 -07001153struct sctp_chunk *sctp_make_auth(const struct sctp_association *asoc)
1154{
1155 struct sctp_chunk *retval;
1156 struct sctp_hmac *hmac_desc;
1157 struct sctp_authhdr auth_hdr;
1158 __u8 *hmac;
1159
1160 /* Get the first hmac that the peer told us to use */
1161 hmac_desc = sctp_auth_asoc_get_hmac(asoc);
1162 if (unlikely(!hmac_desc))
1163 return NULL;
1164
1165 retval = sctp_make_chunk(asoc, SCTP_CID_AUTH, 0,
1166 hmac_desc->hmac_len + sizeof(sctp_authhdr_t));
1167 if (!retval)
1168 return NULL;
1169
1170 auth_hdr.hmac_id = htons(hmac_desc->hmac_id);
1171 auth_hdr.shkey_id = htons(asoc->active_key_id);
1172
1173 retval->subh.auth_hdr = sctp_addto_chunk(retval, sizeof(sctp_authhdr_t),
1174 &auth_hdr);
1175
1176 hmac = skb_put(retval->skb, hmac_desc->hmac_len);
1177 memset(hmac, 0, hmac_desc->hmac_len);
1178
1179 /* Adjust the chunk header to include the empty MAC */
1180 retval->chunk_hdr->length =
1181 htons(ntohs(retval->chunk_hdr->length) + hmac_desc->hmac_len);
1182 retval->chunk_end = skb_tail_pointer(retval->skb);
1183
1184 return retval;
1185}
1186
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188/********************************************************************
1189 * 2nd Level Abstractions
1190 ********************************************************************/
1191
1192/* Turn an skb into a chunk.
1193 * FIXME: Eventually move the structure directly inside the skb->cb[].
1194 */
1195struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
1196 const struct sctp_association *asoc,
1197 struct sock *sk)
1198{
1199 struct sctp_chunk *retval;
1200
Robert P. J. Dayc3762222007-02-10 01:45:03 -08001201 retval = kmem_cache_zalloc(sctp_chunk_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 if (!retval)
1204 goto nodata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 if (!sk) {
1207 SCTP_DEBUG_PRINTK("chunkifying skb %p w/o an sk\n", skb);
1208 }
1209
David S. Miller79af02c2005-07-08 21:47:49 -07001210 INIT_LIST_HEAD(&retval->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 retval->skb = skb;
1212 retval->asoc = (struct sctp_association *)asoc;
1213 retval->resent = 0;
1214 retval->has_tsn = 0;
1215 retval->has_ssn = 0;
1216 retval->rtt_in_progress = 0;
1217 retval->sent_at = 0;
1218 retval->singleton = 1;
1219 retval->end_of_packet = 0;
1220 retval->ecn_ce_done = 0;
1221 retval->pdiscard = 0;
1222
1223 /* sctpimpguide-05.txt Section 2.8.2
1224 * M1) Each time a new DATA chunk is transmitted
1225 * set the 'TSN.Missing.Report' count for that TSN to 0. The
1226 * 'TSN.Missing.Report' count will be used to determine missing chunks
1227 * and when to fast retransmit.
1228 */
1229 retval->tsn_missing_report = 0;
1230 retval->tsn_gap_acked = 0;
Neil Hormanc226ef92008-07-25 12:44:09 -04001231 retval->fast_retransmit = SCTP_CAN_FRTX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 /* If this is a fragmented message, track all fragments
1234 * of the message (for SEND_FAILED).
1235 */
1236 retval->msg = NULL;
1237
1238 /* Polish the bead hole. */
1239 INIT_LIST_HEAD(&retval->transmitted_list);
1240 INIT_LIST_HEAD(&retval->frag_list);
1241 SCTP_DBG_OBJCNT_INC(chunk);
1242 atomic_set(&retval->refcnt, 1);
1243
1244nodata:
1245 return retval;
1246}
1247
1248/* Set chunk->source and dest based on the IP header in chunk->skb. */
1249void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
1250 union sctp_addr *dest)
1251{
Al Virof235fca2006-11-20 17:09:01 -08001252 memcpy(&chunk->source, src, sizeof(union sctp_addr));
Al Viro16b0a032006-11-20 17:13:38 -08001253 memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254}
1255
1256/* Extract the source address from a chunk. */
1257const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
1258{
1259 /* If we have a known transport, use that. */
1260 if (chunk->transport) {
Al Viro6a1e5f32006-11-20 17:12:25 -08001261 return &chunk->transport->ipaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 } else {
1263 /* Otherwise, extract it from the IP header. */
Al Viro6a1e5f32006-11-20 17:12:25 -08001264 return &chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266}
1267
1268/* Create a new chunk, setting the type and flags headers from the
1269 * arguments, reserving enough space for a 'paylen' byte payload.
1270 */
1271SCTP_STATIC
1272struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
1273 __u8 type, __u8 flags, int paylen)
1274{
1275 struct sctp_chunk *retval;
1276 sctp_chunkhdr_t *chunk_hdr;
1277 struct sk_buff *skb;
1278 struct sock *sk;
1279
1280 /* No need to allocate LL here, as this is only a chunk. */
1281 skb = alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t) + paylen),
1282 GFP_ATOMIC);
1283 if (!skb)
1284 goto nodata;
1285
1286 /* Make room for the chunk header. */
1287 chunk_hdr = (sctp_chunkhdr_t *)skb_put(skb, sizeof(sctp_chunkhdr_t));
1288 chunk_hdr->type = type;
1289 chunk_hdr->flags = flags;
1290 chunk_hdr->length = htons(sizeof(sctp_chunkhdr_t));
1291
1292 sk = asoc ? asoc->base.sk : NULL;
1293 retval = sctp_chunkify(skb, asoc, sk);
1294 if (!retval) {
1295 kfree_skb(skb);
1296 goto nodata;
1297 }
1298
1299 retval->chunk_hdr = chunk_hdr;
1300 retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(struct sctp_chunkhdr);
1301
Vlad Yasevich4cd57c82007-09-16 19:32:45 -07001302 /* Determine if the chunk needs to be authenticated */
1303 if (sctp_auth_send_cid(type, asoc))
1304 retval->auth = 1;
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 /* Set the skb to the belonging sock for accounting. */
1307 skb->sk = sk;
1308
1309 return retval;
1310nodata:
1311 return NULL;
1312}
1313
1314
1315/* Release the memory occupied by a chunk. */
1316static void sctp_chunk_destroy(struct sctp_chunk *chunk)
1317{
Vlad Yasevicha08de642007-12-20 14:11:47 -08001318 BUG_ON(!list_empty(&chunk->list));
1319 list_del_init(&chunk->transmitted_list);
1320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 /* Free the chunk skb data and the SCTP_chunk stub itself. */
1322 dev_kfree_skb(chunk->skb);
1323
1324 SCTP_DBG_OBJCNT_DEC(chunk);
1325 kmem_cache_free(sctp_chunk_cachep, chunk);
1326}
1327
1328/* Possibly, free the chunk. */
1329void sctp_chunk_free(struct sctp_chunk *chunk)
1330{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 /* Release our reference on the message tracker. */
1332 if (chunk->msg)
1333 sctp_datamsg_put(chunk->msg);
1334
1335 sctp_chunk_put(chunk);
1336}
1337
1338/* Grab a reference to the chunk. */
1339void sctp_chunk_hold(struct sctp_chunk *ch)
1340{
1341 atomic_inc(&ch->refcnt);
1342}
1343
1344/* Release a reference to the chunk. */
1345void sctp_chunk_put(struct sctp_chunk *ch)
1346{
1347 if (atomic_dec_and_test(&ch->refcnt))
1348 sctp_chunk_destroy(ch);
1349}
1350
1351/* Append bytes to the end of a chunk. Will panic if chunk is not big
1352 * enough.
1353 */
1354void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
1355{
1356 void *target;
1357 void *padding;
1358 int chunklen = ntohs(chunk->chunk_hdr->length);
Wei Yongjun8d614ad2007-08-06 13:55:47 +08001359 int padlen = WORD_ROUND(chunklen) - chunklen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 padding = skb_put(chunk->skb, padlen);
1362 target = skb_put(chunk->skb, len);
1363
1364 memset(padding, 0, padlen);
1365 memcpy(target, data, len);
1366
1367 /* Adjust the chunk length field. */
1368 chunk->chunk_hdr->length = htons(chunklen + padlen + len);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001369 chunk->chunk_end = skb_tail_pointer(chunk->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 return target;
1372}
1373
1374/* Append bytes from user space to the end of a chunk. Will panic if
1375 * chunk is not big enough.
1376 * Returns a kernel err value.
1377 */
1378int sctp_user_addto_chunk(struct sctp_chunk *chunk, int off, int len,
1379 struct iovec *data)
1380{
1381 __u8 *target;
1382 int err = 0;
1383
1384 /* Make room in chunk for data. */
1385 target = skb_put(chunk->skb, len);
1386
1387 /* Copy data (whole iovec) into chunk */
1388 if ((err = memcpy_fromiovecend(target, data, off, len)))
1389 goto out;
1390
1391 /* Adjust the chunk length field. */
1392 chunk->chunk_hdr->length =
1393 htons(ntohs(chunk->chunk_hdr->length) + len);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001394 chunk->chunk_end = skb_tail_pointer(chunk->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396out:
1397 return err;
1398}
1399
1400/* Helper function to assign a TSN if needed. This assumes that both
1401 * the data_hdr and association have already been assigned.
1402 */
1403void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
1404{
Vlad Yasevichab3e5e72007-08-02 16:51:42 -04001405 struct sctp_datamsg *msg;
1406 struct sctp_chunk *lchunk;
1407 struct sctp_stream *stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 __u16 ssn;
1409 __u16 sid;
1410
1411 if (chunk->has_ssn)
1412 return;
1413
Vlad Yasevichab3e5e72007-08-02 16:51:42 -04001414 /* All fragments will be on the same stream */
1415 sid = ntohs(chunk->subh.data_hdr->stream);
1416 stream = &chunk->asoc->ssnmap->out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Vlad Yasevichab3e5e72007-08-02 16:51:42 -04001418 /* Now assign the sequence number to the entire message.
1419 * All fragments must have the same stream sequence number.
1420 */
1421 msg = chunk->msg;
1422 list_for_each_entry(lchunk, &msg->chunks, frag_list) {
1423 if (lchunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
1424 ssn = 0;
1425 } else {
1426 if (lchunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
1427 ssn = sctp_ssn_next(stream, sid);
1428 else
1429 ssn = sctp_ssn_peek(stream, sid);
1430 }
1431
1432 lchunk->subh.data_hdr->ssn = htons(ssn);
1433 lchunk->has_ssn = 1;
1434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435}
1436
1437/* Helper function to assign a TSN if needed. This assumes that both
1438 * the data_hdr and association have already been assigned.
1439 */
1440void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
1441{
1442 if (!chunk->has_tsn) {
1443 /* This is the last possible instant to
1444 * assign a TSN.
1445 */
1446 chunk->subh.data_hdr->tsn =
1447 htonl(sctp_association_get_next_tsn(chunk->asoc));
1448 chunk->has_tsn = 1;
1449 }
1450}
1451
1452/* Create a CLOSED association to use with an incoming packet. */
1453struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
Alexey Dobriyan3182cd842005-07-11 20:57:47 -07001454 struct sctp_chunk *chunk,
Al Virodd0fc662005-10-07 07:46:04 +01001455 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
1457 struct sctp_association *asoc;
1458 struct sk_buff *skb;
1459 sctp_scope_t scope;
1460 struct sctp_af *af;
1461
1462 /* Create the bare association. */
1463 scope = sctp_scope(sctp_source(chunk));
1464 asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
1465 if (!asoc)
1466 goto nodata;
1467 asoc->temp = 1;
1468 skb = chunk->skb;
1469 /* Create an entry for the source address of the packet. */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001470 af = sctp_get_af_specific(ipver2af(ip_hdr(skb)->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 if (unlikely(!af))
1472 goto fail;
Al Virod55c41b2006-11-20 17:09:40 -08001473 af->from_skb(&asoc->c.peer_addr, skb, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474nodata:
1475 return asoc;
1476
1477fail:
1478 sctp_association_free(asoc);
1479 return NULL;
1480}
1481
1482/* Build a cookie representing asoc.
1483 * This INCLUDES the param header needed to put the cookie in the INIT ACK.
1484 */
1485static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
1486 const struct sctp_association *asoc,
1487 const struct sctp_chunk *init_chunk,
1488 int *cookie_len,
1489 const __u8 *raw_addrs, int addrs_len)
1490{
1491 sctp_cookie_param_t *retval;
1492 struct sctp_signed_cookie *cookie;
1493 struct scatterlist sg;
1494 int headersize, bodysize;
1495 unsigned int keylen;
1496 char *key;
1497
Vlad Yasevich9834a2b2006-01-17 11:52:12 -08001498 /* Header size is static data prior to the actual cookie, including
1499 * any padding.
1500 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001501 headersize = sizeof(sctp_paramhdr_t) +
1502 (sizeof(struct sctp_signed_cookie) -
Vlad Yasevich9834a2b2006-01-17 11:52:12 -08001503 sizeof(struct sctp_cookie));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 bodysize = sizeof(struct sctp_cookie)
1505 + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
1506
1507 /* Pad out the cookie to a multiple to make the signature
1508 * functions simpler to write.
1509 */
1510 if (bodysize % SCTP_COOKIE_MULTIPLE)
1511 bodysize += SCTP_COOKIE_MULTIPLE
1512 - (bodysize % SCTP_COOKIE_MULTIPLE);
1513 *cookie_len = headersize + bodysize;
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 /* Clear this memory since we are sending this data structure
1516 * out on the network.
1517 */
Arnaldo Carvalho de Meloaf997d82006-11-21 01:20:33 -02001518 retval = kzalloc(*cookie_len, GFP_ATOMIC);
1519 if (!retval)
1520 goto nodata;
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 cookie = (struct sctp_signed_cookie *) retval->body;
1523
1524 /* Set up the parameter header. */
1525 retval->p.type = SCTP_PARAM_STATE_COOKIE;
1526 retval->p.length = htons(*cookie_len);
1527
1528 /* Copy the cookie part of the association itself. */
1529 cookie->c = asoc->c;
1530 /* Save the raw address list length in the cookie. */
1531 cookie->c.raw_addr_list_len = addrs_len;
1532
1533 /* Remember PR-SCTP capability. */
1534 cookie->c.prsctp_capable = asoc->peer.prsctp_capable;
1535
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001536 /* Save adaptation indication in the cookie. */
1537 cookie->c.adaptation_ind = asoc->peer.adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 /* Set an expiration time for the cookie. */
1540 do_gettimeofday(&cookie->c.expiration);
1541 TIMEVAL_ADD(asoc->cookie_life, cookie->c.expiration);
1542
1543 /* Copy the peer's init packet. */
1544 memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
1545 ntohs(init_chunk->chunk_hdr->length));
1546
1547 /* Copy the raw local address list of the association. */
1548 memcpy((__u8 *)&cookie->c.peer_init[0] +
1549 ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
1550
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001551 if (sctp_sk(ep->base.sk)->hmac) {
Herbert Xu1b489e12006-08-20 15:07:14 +10001552 struct hash_desc desc;
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 /* Sign the message. */
Herbert Xu68e3f5d2007-10-27 00:52:07 -07001555 sg_init_one(&sg, &cookie->c, bodysize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 keylen = SCTP_SECRET_SIZE;
1557 key = (char *)ep->secret_key[ep->current_key];
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001558 desc.tfm = sctp_sk(ep->base.sk)->hmac;
1559 desc.flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Herbert Xu1b489e12006-08-20 15:07:14 +10001561 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1562 crypto_hash_digest(&desc, &sg, bodysize, cookie->signature))
1563 goto free_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 }
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 return retval;
Herbert Xu1b489e12006-08-20 15:07:14 +10001567
1568free_cookie:
1569 kfree(retval);
1570nodata:
1571 *cookie_len = 0;
1572 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573}
1574
1575/* Unpack the cookie from COOKIE ECHO chunk, recreating the association. */
1576struct sctp_association *sctp_unpack_cookie(
1577 const struct sctp_endpoint *ep,
1578 const struct sctp_association *asoc,
Al Virodd0fc662005-10-07 07:46:04 +01001579 struct sctp_chunk *chunk, gfp_t gfp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 int *error, struct sctp_chunk **errp)
1581{
1582 struct sctp_association *retval = NULL;
1583 struct sctp_signed_cookie *cookie;
1584 struct sctp_cookie *bear_cookie;
1585 int headersize, bodysize, fixed_size;
Vlad Yasevich313e7b42006-01-17 11:55:57 -08001586 __u8 *digest = ep->digest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 struct scatterlist sg;
1588 unsigned int keylen, len;
1589 char *key;
1590 sctp_scope_t scope;
1591 struct sk_buff *skb = chunk->skb;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001592 struct timeval tv;
Herbert Xu1b489e12006-08-20 15:07:14 +10001593 struct hash_desc desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Vlad Yasevich9834a2b2006-01-17 11:52:12 -08001595 /* Header size is static data prior to the actual cookie, including
1596 * any padding.
1597 */
1598 headersize = sizeof(sctp_chunkhdr_t) +
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001599 (sizeof(struct sctp_signed_cookie) -
Vlad Yasevich9834a2b2006-01-17 11:52:12 -08001600 sizeof(struct sctp_cookie));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
1602 fixed_size = headersize + sizeof(struct sctp_cookie);
1603
1604 /* Verify that the chunk looks like it even has a cookie.
1605 * There must be enough room for our cookie and our peer's
1606 * INIT chunk.
1607 */
1608 len = ntohs(chunk->chunk_hdr->length);
1609 if (len < fixed_size + sizeof(struct sctp_chunkhdr))
1610 goto malformed;
1611
1612 /* Verify that the cookie has been padded out. */
1613 if (bodysize % SCTP_COOKIE_MULTIPLE)
1614 goto malformed;
1615
1616 /* Process the cookie. */
1617 cookie = chunk->subh.cookie_hdr;
1618 bear_cookie = &cookie->c;
1619
1620 if (!sctp_sk(ep->base.sk)->hmac)
1621 goto no_hmac;
1622
1623 /* Check the signature. */
1624 keylen = SCTP_SECRET_SIZE;
Herbert Xu68e3f5d2007-10-27 00:52:07 -07001625 sg_init_one(&sg, bear_cookie, bodysize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 key = (char *)ep->secret_key[ep->current_key];
Herbert Xu1b489e12006-08-20 15:07:14 +10001627 desc.tfm = sctp_sk(ep->base.sk)->hmac;
1628 desc.flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Al Viro8ca84482006-06-20 03:26:14 -07001630 memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
Herbert Xu1b489e12006-08-20 15:07:14 +10001631 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1632 crypto_hash_digest(&desc, &sg, bodysize, digest)) {
1633 *error = -SCTP_IERROR_NOMEM;
1634 goto fail;
1635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1638 /* Try the previous key. */
1639 key = (char *)ep->secret_key[ep->last_key];
Al Viro8ca84482006-06-20 03:26:14 -07001640 memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
Herbert Xu1b489e12006-08-20 15:07:14 +10001641 if (crypto_hash_setkey(desc.tfm, key, keylen) ||
1642 crypto_hash_digest(&desc, &sg, bodysize, digest)) {
1643 *error = -SCTP_IERROR_NOMEM;
1644 goto fail;
1645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1648 /* Yikes! Still bad signature! */
1649 *error = -SCTP_IERROR_BAD_SIG;
1650 goto fail;
1651 }
1652 }
1653
1654no_hmac:
1655 /* IG Section 2.35.2:
1656 * 3) Compare the port numbers and the verification tag contained
1657 * within the COOKIE ECHO chunk to the actual port numbers and the
1658 * verification tag within the SCTP common header of the received
1659 * packet. If these values do not match the packet MUST be silently
1660 * discarded,
1661 */
1662 if (ntohl(chunk->sctp_hdr->vtag) != bear_cookie->my_vtag) {
1663 *error = -SCTP_IERROR_BAD_TAG;
1664 goto fail;
1665 }
1666
Al Viro9b1dfad2006-11-20 17:09:17 -08001667 if (chunk->sctp_hdr->source != bear_cookie->peer_addr.v4.sin_port ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 ntohs(chunk->sctp_hdr->dest) != bear_cookie->my_port) {
1669 *error = -SCTP_IERROR_BAD_PORTS;
1670 goto fail;
1671 }
1672
1673 /* Check to see if the cookie is stale. If there is already
1674 * an association, there is no need to check cookie's expiration
1675 * for init collision case of lost COOKIE ACK.
Vlad Yasevichf236218b2006-09-29 17:10:03 -07001676 * If skb has been timestamped, then use the stamp, otherwise
1677 * use current time. This introduces a small possibility that
1678 * that a cookie may be considered expired, but his would only slow
1679 * down the new association establishment instead of every packet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 */
Vlad Yasevichf236218b2006-09-29 17:10:03 -07001681 if (sock_flag(ep->base.sk, SOCK_TIMESTAMP))
1682 skb_get_timestamp(skb, &tv);
1683 else
1684 do_gettimeofday(&tv);
1685
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001686 if (!asoc && tv_lt(bear_cookie->expiration, tv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 /*
1688 * Section 3.3.10.3 Stale Cookie Error (3)
1689 *
1690 * Cause of error
1691 * ---------------
1692 * Stale Cookie Error: Indicates the receipt of a valid State
1693 * Cookie that has expired.
1694 */
1695 len = ntohs(chunk->chunk_hdr->length);
1696 *errp = sctp_make_op_error_space(asoc, chunk, len);
1697 if (*errp) {
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001698 suseconds_t usecs = (tv.tv_sec -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 bear_cookie->expiration.tv_sec) * 1000000L +
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001700 tv.tv_usec - bear_cookie->expiration.tv_usec;
Al Viro34bcca22006-11-20 17:27:15 -08001701 __be32 n = htonl(usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001704 sizeof(n));
1705 sctp_addto_chunk(*errp, sizeof(n), &n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 *error = -SCTP_IERROR_STALE_COOKIE;
1707 } else
1708 *error = -SCTP_IERROR_NOMEM;
1709
1710 goto fail;
1711 }
1712
1713 /* Make a new base association. */
1714 scope = sctp_scope(sctp_source(chunk));
1715 retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
1716 if (!retval) {
1717 *error = -SCTP_IERROR_NOMEM;
1718 goto fail;
1719 }
1720
1721 /* Set up our peer's port number. */
1722 retval->peer.port = ntohs(chunk->sctp_hdr->source);
1723
1724 /* Populate the association from the cookie. */
1725 memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
1726
1727 if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
1728 GFP_ATOMIC) < 0) {
1729 *error = -SCTP_IERROR_NOMEM;
1730 goto fail;
1731 }
1732
1733 /* Also, add the destination address. */
1734 if (list_empty(&retval->base.bind_addr.address_list)) {
Vlad Yasevichf57d96b2007-12-20 14:12:24 -08001735 sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest,
1736 SCTP_ADDR_SRC, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
1738
1739 retval->next_tsn = retval->c.initial_tsn;
1740 retval->ctsn_ack_point = retval->next_tsn - 1;
1741 retval->addip_serial = retval->c.initial_tsn;
1742 retval->adv_peer_ack_point = retval->ctsn_ack_point;
1743 retval->peer.prsctp_capable = retval->c.prsctp_capable;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001744 retval->peer.adaptation_ind = retval->c.adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746 /* The INIT stuff will be done by the side effects. */
1747 return retval;
1748
1749fail:
1750 if (retval)
1751 sctp_association_free(retval);
1752
1753 return NULL;
1754
1755malformed:
1756 /* Yikes! The packet is either corrupt or deliberately
1757 * malformed.
1758 */
1759 *error = -SCTP_IERROR_MALFORMED;
1760 goto fail;
1761}
1762
1763/********************************************************************
1764 * 3rd Level Abstractions
1765 ********************************************************************/
1766
1767struct __sctp_missing {
Al Viro9f81bcd2006-11-20 17:26:34 -08001768 __be32 num_missing;
1769 __be16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770} __attribute__((packed));
1771
1772/*
1773 * Report a missing mandatory parameter.
1774 */
1775static int sctp_process_missing_param(const struct sctp_association *asoc,
1776 sctp_param_t paramtype,
1777 struct sctp_chunk *chunk,
1778 struct sctp_chunk **errp)
1779{
1780 struct __sctp_missing report;
1781 __u16 len;
1782
1783 len = WORD_ROUND(sizeof(report));
1784
1785 /* Make an ERROR chunk, preparing enough room for
1786 * returning multiple unknown parameters.
1787 */
1788 if (!*errp)
1789 *errp = sctp_make_op_error_space(asoc, chunk, len);
1790
1791 if (*errp) {
1792 report.num_missing = htonl(1);
1793 report.type = paramtype;
Vlad Yasevichebdfcad2007-01-15 19:12:31 -08001794 sctp_init_cause(*errp, SCTP_ERROR_MISS_PARAM,
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001795 sizeof(report));
1796 sctp_addto_chunk(*errp, sizeof(report), &report);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 }
1798
1799 /* Stop processing this chunk. */
1800 return 0;
1801}
1802
1803/* Report an Invalid Mandatory Parameter. */
1804static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
1805 struct sctp_chunk *chunk,
1806 struct sctp_chunk **errp)
1807{
1808 /* Invalid Mandatory Parameter Error has no payload. */
1809
1810 if (!*errp)
1811 *errp = sctp_make_op_error_space(asoc, chunk, 0);
1812
1813 if (*errp)
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001814 sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 /* Stop processing this chunk. */
1817 return 0;
1818}
1819
1820static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
1821 struct sctp_paramhdr *param,
1822 const struct sctp_chunk *chunk,
1823 struct sctp_chunk **errp)
1824{
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001825 /* This is a fatal error. Any accumulated non-fatal errors are
1826 * not reported.
1827 */
1828 if (*errp)
1829 sctp_chunk_free(*errp);
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 /* Create an error chunk and fill it in with our payload. */
Wei Yongjunba016672008-09-30 05:32:24 -07001832 *errp = sctp_make_violation_paramlen(asoc, chunk, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
1834 return 0;
1835}
1836
1837
1838/* Do not attempt to handle the HOST_NAME parm. However, do
1839 * send back an indicator to the peer.
1840 */
1841static int sctp_process_hn_param(const struct sctp_association *asoc,
1842 union sctp_params param,
1843 struct sctp_chunk *chunk,
1844 struct sctp_chunk **errp)
1845{
1846 __u16 len = ntohs(param.p->length);
1847
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001848 /* Processing of the HOST_NAME parameter will generate an
1849 * ABORT. If we've accumulated any non-fatal errors, they
1850 * would be unrecognized parameters and we should not include
1851 * them in the ABORT.
1852 */
1853 if (*errp)
1854 sctp_chunk_free(*errp);
1855
1856 *errp = sctp_make_op_error_space(asoc, chunk, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001858 if (*errp) {
1859 sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED, len);
1860 sctp_addto_chunk(*errp, len, param.v);
1861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 /* Stop processing this chunk. */
1864 return 0;
1865}
1866
Vlad Yasevichd6701192007-12-20 14:13:31 -08001867static int sctp_verify_ext_param(union sctp_params param)
1868{
1869 __u16 num_ext = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
1870 int have_auth = 0;
1871 int have_asconf = 0;
1872 int i;
1873
1874 for (i = 0; i < num_ext; i++) {
1875 switch (param.ext->chunks[i]) {
1876 case SCTP_CID_AUTH:
1877 have_auth = 1;
1878 break;
1879 case SCTP_CID_ASCONF:
1880 case SCTP_CID_ASCONF_ACK:
1881 have_asconf = 1;
1882 break;
1883 }
1884 }
1885
1886 /* ADD-IP Security: The draft requires us to ABORT or ignore the
1887 * INIT/INIT-ACK if ADD-IP is listed, but AUTH is not. Do this
1888 * only if ADD-IP is turned on and we are not backward-compatible
1889 * mode.
1890 */
1891 if (sctp_addip_noauth)
1892 return 1;
1893
1894 if (sctp_addip_enable && !have_auth && have_asconf)
1895 return 0;
1896
1897 return 1;
1898}
1899
Vlad Yasevich131a47e2007-09-16 15:53:56 -07001900static void sctp_process_ext_param(struct sctp_association *asoc,
1901 union sctp_params param)
1902{
1903 __u16 num_ext = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
1904 int i;
1905
1906 for (i = 0; i < num_ext; i++) {
1907 switch (param.ext->chunks[i]) {
1908 case SCTP_CID_FWD_TSN:
1909 if (sctp_prsctp_enable &&
1910 !asoc->peer.prsctp_capable)
1911 asoc->peer.prsctp_capable = 1;
1912 break;
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07001913 case SCTP_CID_AUTH:
1914 /* if the peer reports AUTH, assume that he
1915 * supports AUTH.
1916 */
Vlad Yasevich0ef46e22008-09-18 16:27:38 -07001917 if (sctp_auth_enable)
1918 asoc->peer.auth_capable = 1;
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07001919 break;
Vlad Yasevich131a47e2007-09-16 15:53:56 -07001920 case SCTP_CID_ASCONF:
1921 case SCTP_CID_ASCONF_ACK:
Vlad Yasevich0ef46e22008-09-18 16:27:38 -07001922 if (sctp_addip_enable)
1923 asoc->peer.asconf_capable = 1;
Vlad Yasevich6b2f9cb2007-09-16 19:35:39 -07001924 break;
Vlad Yasevich131a47e2007-09-16 15:53:56 -07001925 default:
1926 break;
1927 }
1928 }
1929}
1930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931/* RFC 3.2.1 & the Implementers Guide 2.2.
1932 *
1933 * The Parameter Types are encoded such that the
1934 * highest-order two bits specify the action that must be
1935 * taken if the processing endpoint does not recognize the
1936 * Parameter Type.
1937 *
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001938 * 00 - Stop processing this parameter; do not process any further
1939 * parameters within this chunk
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 *
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001941 * 01 - Stop processing this parameter, do not process any further
1942 * parameters within this chunk, and report the unrecognized
1943 * parameter in an 'Unrecognized Parameter' ERROR chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 *
1945 * 10 - Skip this parameter and continue processing.
1946 *
1947 * 11 - Skip this parameter and continue processing but
1948 * report the unrecognized parameter in an
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001949 * 'Unrecognized Parameter' ERROR chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 *
1951 * Return value:
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001952 * SCTP_IERROR_NO_ERROR - continue with the chunk
1953 * SCTP_IERROR_ERROR - stop and report an error.
1954 * SCTP_IERROR_NOMEME - out of memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 */
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001956static sctp_ierror_t sctp_process_unk_param(const struct sctp_association *asoc,
1957 union sctp_params param,
1958 struct sctp_chunk *chunk,
1959 struct sctp_chunk **errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001961 int retval = SCTP_IERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
1964 case SCTP_PARAM_ACTION_DISCARD:
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001965 retval = SCTP_IERROR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 break;
1967 case SCTP_PARAM_ACTION_SKIP:
1968 break;
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001969 case SCTP_PARAM_ACTION_DISCARD_ERR:
1970 retval = SCTP_IERROR_ERROR;
1971 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 case SCTP_PARAM_ACTION_SKIP_ERR:
1973 /* Make an ERROR chunk, preparing enough room for
1974 * returning multiple unknown parameters.
1975 */
1976 if (NULL == *errp)
1977 *errp = sctp_make_op_error_space(asoc, chunk,
1978 ntohs(chunk->chunk_hdr->length));
1979
1980 if (*errp) {
1981 sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 WORD_ROUND(ntohs(param.p->length)));
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001983 sctp_addto_chunk(*errp,
1984 WORD_ROUND(ntohs(param.p->length)),
1985 param.v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 } else {
1987 /* If there is no memory for generating the ERROR
1988 * report as specified, an ABORT will be triggered
1989 * to the peer and the association won't be
1990 * established.
1991 */
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001992 retval = SCTP_IERROR_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 break;
1995 default:
1996 break;
1997 }
1998
1999 return retval;
2000}
2001
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002002/* Verify variable length parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 * Return values:
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002004 * SCTP_IERROR_ABORT - trigger an ABORT
2005 * SCTP_IERROR_NOMEM - out of memory (abort)
2006 * SCTP_IERROR_ERROR - stop processing, trigger an ERROR
2007 * SCTP_IERROR_NO_ERROR - continue with the chunk
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 */
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002009static sctp_ierror_t sctp_verify_param(const struct sctp_association *asoc,
2010 union sctp_params param,
2011 sctp_cid_t cid,
2012 struct sctp_chunk *chunk,
2013 struct sctp_chunk **err_chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
Wei Yongjun72da7b32008-04-12 18:39:19 -07002015 struct sctp_hmac_algo_param *hmacs;
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002016 int retval = SCTP_IERROR_NO_ERROR;
Wei Yongjun72da7b32008-04-12 18:39:19 -07002017 __u16 n_elt, id = 0;
2018 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020 /* FIXME - This routine is not looking at each parameter per the
2021 * chunk type, i.e., unrecognized parameters should be further
2022 * identified based on the chunk id.
2023 */
2024
2025 switch (param.p->type) {
2026 case SCTP_PARAM_IPV4_ADDRESS:
2027 case SCTP_PARAM_IPV6_ADDRESS:
2028 case SCTP_PARAM_COOKIE_PRESERVATIVE:
2029 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2030 case SCTP_PARAM_STATE_COOKIE:
2031 case SCTP_PARAM_HEARTBEAT_INFO:
2032 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2033 case SCTP_PARAM_ECN_CAPABLE:
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002034 case SCTP_PARAM_ADAPTATION_LAYER_IND:
Vlad Yasevichd6701192007-12-20 14:13:31 -08002035 break;
2036
Vlad Yasevich131a47e2007-09-16 15:53:56 -07002037 case SCTP_PARAM_SUPPORTED_EXT:
Vlad Yasevichd6701192007-12-20 14:13:31 -08002038 if (!sctp_verify_ext_param(param))
2039 return SCTP_IERROR_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 break;
2041
Vlad Yasevichd6de3092007-12-20 14:10:00 -08002042 case SCTP_PARAM_SET_PRIMARY:
2043 if (sctp_addip_enable)
2044 break;
2045 goto fallthrough;
2046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 case SCTP_PARAM_HOST_NAME_ADDRESS:
2048 /* Tell the peer, we won't support this param. */
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002049 sctp_process_hn_param(asoc, param, chunk, err_chunk);
2050 retval = SCTP_IERROR_ABORT;
2051 break;
Vlad Yasevich131a47e2007-09-16 15:53:56 -07002052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 case SCTP_PARAM_FWD_TSN_SUPPORT:
2054 if (sctp_prsctp_enable)
2055 break;
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002056 goto fallthrough;
2057
2058 case SCTP_PARAM_RANDOM:
2059 if (!sctp_auth_enable)
2060 goto fallthrough;
2061
2062 /* SCTP-AUTH: Secion 6.1
2063 * If the random number is not 32 byte long the association
2064 * MUST be aborted. The ABORT chunk SHOULD contain the error
2065 * cause 'Protocol Violation'.
2066 */
2067 if (SCTP_AUTH_RANDOM_LENGTH !=
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002068 ntohs(param.p->length) - sizeof(sctp_paramhdr_t)) {
2069 sctp_process_inv_paramlength(asoc, param.p,
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002070 chunk, err_chunk);
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002071 retval = SCTP_IERROR_ABORT;
2072 }
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002073 break;
2074
2075 case SCTP_PARAM_CHUNKS:
2076 if (!sctp_auth_enable)
2077 goto fallthrough;
2078
2079 /* SCTP-AUTH: Section 3.2
2080 * The CHUNKS parameter MUST be included once in the INIT or
2081 * INIT-ACK chunk if the sender wants to receive authenticated
2082 * chunks. Its maximum length is 260 bytes.
2083 */
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002084 if (260 < ntohs(param.p->length)) {
2085 sctp_process_inv_paramlength(asoc, param.p,
2086 chunk, err_chunk);
2087 retval = SCTP_IERROR_ABORT;
2088 }
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002089 break;
2090
2091 case SCTP_PARAM_HMAC_ALGO:
Wei Yongjun72da7b32008-04-12 18:39:19 -07002092 if (!sctp_auth_enable)
2093 goto fallthrough;
2094
2095 hmacs = (struct sctp_hmac_algo_param *)param.p;
2096 n_elt = (ntohs(param.p->length) - sizeof(sctp_paramhdr_t)) >> 1;
2097
2098 /* SCTP-AUTH: Section 6.1
2099 * The HMAC algorithm based on SHA-1 MUST be supported and
2100 * included in the HMAC-ALGO parameter.
2101 */
2102 for (i = 0; i < n_elt; i++) {
2103 id = ntohs(hmacs->hmac_ids[i]);
2104
2105 if (id == SCTP_AUTH_HMAC_ID_SHA1)
2106 break;
2107 }
2108
2109 if (id != SCTP_AUTH_HMAC_ID_SHA1) {
2110 sctp_process_inv_paramlength(asoc, param.p, chunk,
2111 err_chunk);
2112 retval = SCTP_IERROR_ABORT;
2113 }
2114 break;
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002115fallthrough:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 default:
2117 SCTP_DEBUG_PRINTK("Unrecognized param: %d for chunk %d.\n",
2118 ntohs(param.p->type), cid);
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002119 retval = sctp_process_unk_param(asoc, param, chunk, err_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 break;
2121 }
2122 return retval;
2123}
2124
2125/* Verify the INIT packet before we process it. */
2126int sctp_verify_init(const struct sctp_association *asoc,
2127 sctp_cid_t cid,
2128 sctp_init_chunk_t *peer_init,
2129 struct sctp_chunk *chunk,
2130 struct sctp_chunk **errp)
2131{
2132 union sctp_params param;
2133 int has_cookie = 0;
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002134 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 /* Verify stream values are non-zero. */
2137 if ((0 == peer_init->init_hdr.num_outbound_streams) ||
Vlad Yasevichd023f622007-01-15 19:15:45 -08002138 (0 == peer_init->init_hdr.num_inbound_streams) ||
2139 (0 == peer_init->init_hdr.init_tag) ||
2140 (SCTP_DEFAULT_MINWINDOW > ntohl(peer_init->init_hdr.a_rwnd))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002142 return sctp_process_inv_mandatory(asoc, chunk, errp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 }
2144
2145 /* Check for missing mandatory parameters. */
2146 sctp_walk_params(param, peer_init, init_hdr.params) {
2147
2148 if (SCTP_PARAM_STATE_COOKIE == param.p->type)
2149 has_cookie = 1;
2150
2151 } /* for (loop through all parameters) */
2152
2153 /* There is a possibility that a parameter length was bad and
2154 * in that case we would have stoped walking the parameters.
2155 * The current param.p would point at the bad one.
2156 * Current consensus on the mailing list is to generate a PROTOCOL
2157 * VIOLATION error. We build the ERROR chunk here and let the normal
2158 * error handling code build and send the packet.
2159 */
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002160 if (param.v != (void*)chunk->chunk_end)
2161 return sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
2163 /* The only missing mandatory param possible today is
2164 * the state cookie for an INIT-ACK chunk.
2165 */
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002166 if ((SCTP_CID_INIT_ACK == cid) && !has_cookie)
2167 return sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
2168 chunk, errp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002170 /* Verify all the variable length parameters */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 sctp_walk_params(param, peer_init, init_hdr.params) {
2172
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002173 result = sctp_verify_param(asoc, param, cid, chunk, errp);
2174 switch (result) {
2175 case SCTP_IERROR_ABORT:
2176 case SCTP_IERROR_NOMEM:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 return 0;
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002178 case SCTP_IERROR_ERROR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 return 1;
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002180 case SCTP_IERROR_NO_ERROR:
2181 default:
2182 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 }
2184
2185 } /* for (loop through all parameters) */
2186
2187 return 1;
2188}
2189
2190/* Unpack the parameters in an INIT packet into an association.
2191 * Returns 0 on failure, else success.
2192 * FIXME: This is an association method.
2193 */
2194int sctp_process_init(struct sctp_association *asoc, sctp_cid_t cid,
2195 const union sctp_addr *peer_addr,
Al Virodd0fc662005-10-07 07:46:04 +01002196 sctp_init_chunk_t *peer_init, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
2198 union sctp_params param;
2199 struct sctp_transport *transport;
2200 struct list_head *pos, *temp;
2201 char *cookie;
2202
2203 /* We must include the address that the INIT packet came from.
2204 * This is the only address that matters for an INIT packet.
2205 * When processing a COOKIE ECHO, we retrieve the from address
2206 * of the INIT from the cookie.
2207 */
2208
2209 /* This implementation defaults to making the first transport
2210 * added as the primary transport. The source address seems to
2211 * be a a better choice than any of the embedded addresses.
2212 */
Al Viro4bdf4b52006-11-20 17:10:20 -08002213 if (peer_addr) {
Al Viro6a1e5f32006-11-20 17:12:25 -08002214 if(!sctp_assoc_add_peer(asoc, peer_addr, gfp, SCTP_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 goto nomem;
Al Viro4bdf4b52006-11-20 17:10:20 -08002216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
2218 /* Process the initialization parameters. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 sctp_walk_params(param, peer_init, init_hdr.params) {
2220
2221 if (!sctp_process_param(asoc, param, peer_addr, gfp))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002222 goto clean_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 }
2224
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002225 /* AUTH: After processing the parameters, make sure that we
2226 * have all the required info to potentially do authentications.
2227 */
2228 if (asoc->peer.auth_capable && (!asoc->peer.peer_random ||
2229 !asoc->peer.peer_hmacs))
2230 asoc->peer.auth_capable = 0;
2231
Vlad Yasevichd6701192007-12-20 14:13:31 -08002232 /* In a non-backward compatible mode, if the peer claims
2233 * support for ADD-IP but not AUTH, the ADD-IP spec states
2234 * that we MUST ABORT the association. Section 6. The section
2235 * also give us an option to silently ignore the packet, which
2236 * is what we'll do here.
Vlad Yasevich6b2f9cb2007-09-16 19:35:39 -07002237 */
Vlad Yasevich73d9c4f2007-10-24 17:24:26 -04002238 if (!sctp_addip_noauth &&
2239 (asoc->peer.asconf_capable && !asoc->peer.auth_capable)) {
Vlad Yasevich6b2f9cb2007-09-16 19:35:39 -07002240 asoc->peer.addip_disabled_mask |= (SCTP_PARAM_ADD_IP |
2241 SCTP_PARAM_DEL_IP |
2242 SCTP_PARAM_SET_PRIMARY);
Vlad Yasevich88799fe2007-10-24 17:24:23 -04002243 asoc->peer.asconf_capable = 0;
Vlad Yasevichd6701192007-12-20 14:13:31 -08002244 goto clean_up;
Vlad Yasevich6b2f9cb2007-09-16 19:35:39 -07002245 }
2246
Frank Filz3f7a87d2005-06-20 13:14:57 -07002247 /* Walk list of transports, removing transports in the UNKNOWN state. */
2248 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2249 transport = list_entry(pos, struct sctp_transport, transports);
2250 if (transport->state == SCTP_UNKNOWN) {
2251 sctp_assoc_rm_peer(asoc, transport);
2252 }
2253 }
2254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 /* The fixed INIT headers are always in network byte
2256 * order.
2257 */
2258 asoc->peer.i.init_tag =
2259 ntohl(peer_init->init_hdr.init_tag);
2260 asoc->peer.i.a_rwnd =
2261 ntohl(peer_init->init_hdr.a_rwnd);
2262 asoc->peer.i.num_outbound_streams =
2263 ntohs(peer_init->init_hdr.num_outbound_streams);
2264 asoc->peer.i.num_inbound_streams =
2265 ntohs(peer_init->init_hdr.num_inbound_streams);
2266 asoc->peer.i.initial_tsn =
2267 ntohl(peer_init->init_hdr.initial_tsn);
2268
2269 /* Apply the upper bounds for output streams based on peer's
2270 * number of inbound streams.
2271 */
2272 if (asoc->c.sinit_num_ostreams >
2273 ntohs(peer_init->init_hdr.num_inbound_streams)) {
2274 asoc->c.sinit_num_ostreams =
2275 ntohs(peer_init->init_hdr.num_inbound_streams);
2276 }
2277
2278 if (asoc->c.sinit_max_instreams >
2279 ntohs(peer_init->init_hdr.num_outbound_streams)) {
2280 asoc->c.sinit_max_instreams =
2281 ntohs(peer_init->init_hdr.num_outbound_streams);
2282 }
2283
2284 /* Copy Initiation tag from INIT to VT_peer in cookie. */
2285 asoc->c.peer_vtag = asoc->peer.i.init_tag;
2286
2287 /* Peer Rwnd : Current calculated value of the peer's rwnd. */
2288 asoc->peer.rwnd = asoc->peer.i.a_rwnd;
2289
2290 /* Copy cookie in case we need to resend COOKIE-ECHO. */
2291 cookie = asoc->peer.cookie;
2292 if (cookie) {
Arnaldo Carvalho de Meloaf997d82006-11-21 01:20:33 -02002293 asoc->peer.cookie = kmemdup(cookie, asoc->peer.cookie_len, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 if (!asoc->peer.cookie)
2295 goto clean_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 }
2297
2298 /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
2299 * high (for example, implementations MAY use the size of the receiver
2300 * advertised window).
2301 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002302 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
2303 transports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 transport->ssthresh = asoc->peer.i.a_rwnd;
2305 }
2306
2307 /* Set up the TSN tracking pieces. */
Vlad Yasevich8e1ee182008-10-08 14:18:39 -07002308 if (!sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
2309 asoc->peer.i.initial_tsn, gfp))
2310 goto clean_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
2312 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
2313 *
2314 * The stream sequence number in all the streams shall start
2315 * from 0 when the association is established. Also, when the
2316 * stream sequence number reaches the value 65535 the next
2317 * stream sequence number shall be set to 0.
2318 */
2319
Frank Filz3f7a87d2005-06-20 13:14:57 -07002320 /* Allocate storage for the negotiated streams if it is not a temporary
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002321 * association.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 */
2323 if (!asoc->temp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 int error;
2325
2326 asoc->ssnmap = sctp_ssnmap_new(asoc->c.sinit_max_instreams,
2327 asoc->c.sinit_num_ostreams, gfp);
2328 if (!asoc->ssnmap)
2329 goto clean_up;
2330
Vlad Yasevich07d93962007-05-04 13:55:27 -07002331 error = sctp_assoc_set_id(asoc, gfp);
2332 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 goto clean_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 }
2335
2336 /* ADDIP Section 4.1 ASCONF Chunk Procedures
2337 *
2338 * When an endpoint has an ASCONF signaled change to be sent to the
2339 * remote endpoint it should do the following:
2340 * ...
2341 * A2) A serial number should be assigned to the Chunk. The serial
2342 * number should be a monotonically increasing number. All serial
2343 * numbers are defined to be initialized at the start of the
2344 * association to the same value as the Initial TSN.
2345 */
2346 asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
2347 return 1;
2348
2349clean_up:
2350 /* Release the transport structures. */
2351 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2352 transport = list_entry(pos, struct sctp_transport, transports);
Vlad Yasevichadd52372008-09-18 16:28:27 -07002353 if (transport->state != SCTP_ACTIVE)
2354 sctp_assoc_rm_peer(asoc, transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07002356
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357nomem:
2358 return 0;
2359}
2360
2361
2362/* Update asoc with the option described in param.
2363 *
2364 * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
2365 *
2366 * asoc is the association to update.
2367 * param is the variable length parameter to use for update.
2368 * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
2369 * If the current packet is an INIT we want to minimize the amount of
2370 * work we do. In particular, we should not build transport
2371 * structures for the addresses.
2372 */
2373static int sctp_process_param(struct sctp_association *asoc,
2374 union sctp_params param,
2375 const union sctp_addr *peer_addr,
Al Virodd0fc662005-10-07 07:46:04 +01002376 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377{
2378 union sctp_addr addr;
2379 int i;
2380 __u16 sat;
2381 int retval = 1;
2382 sctp_scope_t scope;
2383 time_t stale;
2384 struct sctp_af *af;
Vlad Yasevichd6de3092007-12-20 14:10:00 -08002385 union sctp_addr_param *addr_param;
2386 struct sctp_transport *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
2388 /* We maintain all INIT parameters in network byte order all the
2389 * time. This allows us to not worry about whether the parameters
2390 * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
2391 */
2392 switch (param.p->type) {
2393 case SCTP_PARAM_IPV6_ADDRESS:
2394 if (PF_INET6 != asoc->base.sk->sk_family)
2395 break;
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07002396 goto do_addr_param;
2397
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 case SCTP_PARAM_IPV4_ADDRESS:
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07002399 /* v4 addresses are not allowed on v6-only socket */
2400 if (ipv6_only_sock(asoc->base.sk))
2401 break;
2402do_addr_param:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 af = sctp_get_af_specific(param_type2af(param.p->type));
Al Virodd86d132006-11-20 17:11:13 -08002404 af->from_addr_param(&addr, param.addr, htons(asoc->peer.port), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 scope = sctp_scope(peer_addr);
Al Virodd86d132006-11-20 17:11:13 -08002406 if (sctp_in_scope(&addr, scope))
2407 if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_UNCONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 return 0;
2409 break;
2410
2411 case SCTP_PARAM_COOKIE_PRESERVATIVE:
2412 if (!sctp_cookie_preserve_enable)
2413 break;
2414
2415 stale = ntohl(param.life->lifespan_increment);
2416
2417 /* Suggested Cookie Life span increment's unit is msec,
2418 * (1/1000sec).
2419 */
2420 asoc->cookie_life.tv_sec += stale / 1000;
2421 asoc->cookie_life.tv_usec += (stale % 1000) * 1000;
2422 break;
2423
2424 case SCTP_PARAM_HOST_NAME_ADDRESS:
2425 SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
2426 break;
2427
2428 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2429 /* Turn off the default values first so we'll know which
2430 * ones are really set by the peer.
2431 */
2432 asoc->peer.ipv4_address = 0;
2433 asoc->peer.ipv6_address = 0;
2434
Gui Jianfeng140ee962008-03-05 13:43:32 -08002435 /* Assume that peer supports the address family
2436 * by which it sends a packet.
2437 */
2438 if (peer_addr->sa.sa_family == AF_INET6)
2439 asoc->peer.ipv6_address = 1;
2440 else if (peer_addr->sa.sa_family == AF_INET)
2441 asoc->peer.ipv4_address = 1;
2442
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 /* Cycle through address types; avoid divide by 0. */
2444 sat = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2445 if (sat)
2446 sat /= sizeof(__u16);
2447
2448 for (i = 0; i < sat; ++i) {
2449 switch (param.sat->types[i]) {
2450 case SCTP_PARAM_IPV4_ADDRESS:
2451 asoc->peer.ipv4_address = 1;
2452 break;
2453
2454 case SCTP_PARAM_IPV6_ADDRESS:
Wei Yongjun6e40a912008-05-09 15:11:17 -07002455 if (PF_INET6 == asoc->base.sk->sk_family)
2456 asoc->peer.ipv6_address = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 break;
2458
2459 case SCTP_PARAM_HOST_NAME_ADDRESS:
2460 asoc->peer.hostname_address = 1;
2461 break;
2462
2463 default: /* Just ignore anything else. */
2464 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 }
2467 break;
2468
2469 case SCTP_PARAM_STATE_COOKIE:
2470 asoc->peer.cookie_len =
2471 ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2472 asoc->peer.cookie = param.cookie->body;
2473 break;
2474
2475 case SCTP_PARAM_HEARTBEAT_INFO:
2476 /* Would be odd to receive, but it causes no problems. */
2477 break;
2478
2479 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2480 /* Rejected during verify stage. */
2481 break;
2482
2483 case SCTP_PARAM_ECN_CAPABLE:
2484 asoc->peer.ecn_capable = 1;
2485 break;
2486
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002487 case SCTP_PARAM_ADAPTATION_LAYER_IND:
Vlad Yaseviche69c4e02008-09-15 16:29:49 -04002488 asoc->peer.adaptation_ind = ntohl(param.aind->adaptation_ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 break;
2490
Vlad Yasevichd6de3092007-12-20 14:10:00 -08002491 case SCTP_PARAM_SET_PRIMARY:
Vlad Yasevich0ef46e22008-09-18 16:27:38 -07002492 if (!sctp_addip_enable)
2493 goto fall_through;
2494
Vlad Yasevichd6de3092007-12-20 14:10:00 -08002495 addr_param = param.v + sizeof(sctp_addip_param_t);
2496
2497 af = sctp_get_af_specific(param_type2af(param.p->type));
2498 af->from_addr_param(&addr, addr_param,
2499 htons(asoc->peer.port), 0);
2500
2501 /* if the address is invalid, we can't process it.
2502 * XXX: see spec for what to do.
2503 */
2504 if (!af->addr_valid(&addr, NULL, NULL))
2505 break;
2506
2507 t = sctp_assoc_lookup_paddr(asoc, &addr);
2508 if (!t)
2509 break;
2510
2511 sctp_assoc_set_primary(asoc, t);
2512 break;
2513
Vlad Yasevich131a47e2007-09-16 15:53:56 -07002514 case SCTP_PARAM_SUPPORTED_EXT:
2515 sctp_process_ext_param(asoc, param);
2516 break;
2517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 case SCTP_PARAM_FWD_TSN_SUPPORT:
2519 if (sctp_prsctp_enable) {
2520 asoc->peer.prsctp_capable = 1;
2521 break;
2522 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002523 /* Fall Through */
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002524 goto fall_through;
2525
2526 case SCTP_PARAM_RANDOM:
2527 if (!sctp_auth_enable)
2528 goto fall_through;
2529
2530 /* Save peer's random parameter */
2531 asoc->peer.peer_random = kmemdup(param.p,
2532 ntohs(param.p->length), gfp);
2533 if (!asoc->peer.peer_random) {
2534 retval = 0;
2535 break;
2536 }
2537 break;
2538
2539 case SCTP_PARAM_HMAC_ALGO:
2540 if (!sctp_auth_enable)
2541 goto fall_through;
2542
2543 /* Save peer's HMAC list */
2544 asoc->peer.peer_hmacs = kmemdup(param.p,
2545 ntohs(param.p->length), gfp);
2546 if (!asoc->peer.peer_hmacs) {
2547 retval = 0;
2548 break;
2549 }
2550
2551 /* Set the default HMAC the peer requested*/
2552 sctp_auth_asoc_set_default_hmac(asoc, param.hmac_algo);
2553 break;
2554
2555 case SCTP_PARAM_CHUNKS:
2556 if (!sctp_auth_enable)
2557 goto fall_through;
2558
2559 asoc->peer.peer_chunks = kmemdup(param.p,
2560 ntohs(param.p->length), gfp);
2561 if (!asoc->peer.peer_chunks)
2562 retval = 0;
2563 break;
2564fall_through:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 default:
2566 /* Any unrecognized parameters should have been caught
2567 * and handled by sctp_verify_param() which should be
2568 * called prior to this routine. Simply log the error
2569 * here.
2570 */
2571 SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
2572 ntohs(param.p->type), asoc);
2573 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
2576 return retval;
2577}
2578
2579/* Select a new verification tag. */
2580__u32 sctp_generate_tag(const struct sctp_endpoint *ep)
2581{
2582 /* I believe that this random number generator complies with RFC1750.
2583 * A tag of 0 is reserved for special cases (e.g. INIT).
2584 */
2585 __u32 x;
2586
2587 do {
2588 get_random_bytes(&x, sizeof(__u32));
2589 } while (x == 0);
2590
2591 return x;
2592}
2593
2594/* Select an initial TSN to send during startup. */
2595__u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
2596{
2597 __u32 retval;
2598
2599 get_random_bytes(&retval, sizeof(__u32));
2600 return retval;
2601}
2602
2603/*
2604 * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
2605 * 0 1 2 3
2606 * 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
2607 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2608 * | Type = 0xC1 | Chunk Flags | Chunk Length |
2609 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2610 * | Serial Number |
2611 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2612 * | Address Parameter |
2613 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2614 * | ASCONF Parameter #1 |
2615 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2616 * \ \
2617 * / .... /
2618 * \ \
2619 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2620 * | ASCONF Parameter #N |
2621 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2622 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002623 * Address Parameter and other parameter will not be wrapped in this function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 */
2625static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
2626 union sctp_addr *addr,
2627 int vparam_len)
2628{
2629 sctp_addiphdr_t asconf;
2630 struct sctp_chunk *retval;
2631 int length = sizeof(asconf) + vparam_len;
2632 union sctp_addr_param addrparam;
2633 int addrlen;
2634 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2635
2636 addrlen = af->to_addr_param(addr, &addrparam);
2637 if (!addrlen)
2638 return NULL;
2639 length += addrlen;
2640
2641 /* Create the chunk. */
2642 retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF, 0, length);
2643 if (!retval)
2644 return NULL;
2645
2646 asconf.serial = htonl(asoc->addip_serial++);
2647
2648 retval->subh.addip_hdr =
2649 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2650 retval->param_hdr.v =
2651 sctp_addto_chunk(retval, addrlen, &addrparam);
2652
2653 return retval;
2654}
2655
2656/* ADDIP
2657 * 3.2.1 Add IP Address
2658 * 0 1 2 3
2659 * 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
2660 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2661 * | Type = 0xC001 | Length = Variable |
2662 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2663 * | ASCONF-Request Correlation ID |
2664 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2665 * | Address Parameter |
2666 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2667 *
2668 * 3.2.2 Delete IP Address
2669 * 0 1 2 3
2670 * 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
2671 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2672 * | Type = 0xC002 | Length = Variable |
2673 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2674 * | ASCONF-Request Correlation ID |
2675 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2676 * | Address Parameter |
2677 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2678 *
2679 */
2680struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
2681 union sctp_addr *laddr,
2682 struct sockaddr *addrs,
2683 int addrcnt,
Al Virodbc16db2006-11-20 17:01:42 -08002684 __be16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685{
2686 sctp_addip_param_t param;
2687 struct sctp_chunk *retval;
2688 union sctp_addr_param addr_param;
2689 union sctp_addr *addr;
2690 void *addr_buf;
2691 struct sctp_af *af;
2692 int paramlen = sizeof(param);
2693 int addr_param_len = 0;
2694 int totallen = 0;
2695 int i;
2696
2697 /* Get total length of all the address parameters. */
2698 addr_buf = addrs;
2699 for (i = 0; i < addrcnt; i++) {
2700 addr = (union sctp_addr *)addr_buf;
2701 af = sctp_get_af_specific(addr->v4.sin_family);
2702 addr_param_len = af->to_addr_param(addr, &addr_param);
2703
2704 totallen += paramlen;
2705 totallen += addr_param_len;
2706
2707 addr_buf += af->sockaddr_len;
2708 }
2709
2710 /* Create an asconf chunk with the required length. */
2711 retval = sctp_make_asconf(asoc, laddr, totallen);
2712 if (!retval)
2713 return NULL;
2714
2715 /* Add the address parameters to the asconf chunk. */
2716 addr_buf = addrs;
2717 for (i = 0; i < addrcnt; i++) {
2718 addr = (union sctp_addr *)addr_buf;
2719 af = sctp_get_af_specific(addr->v4.sin_family);
2720 addr_param_len = af->to_addr_param(addr, &addr_param);
2721 param.param_hdr.type = flags;
2722 param.param_hdr.length = htons(paramlen + addr_param_len);
2723 param.crr_id = i;
2724
2725 sctp_addto_chunk(retval, paramlen, &param);
2726 sctp_addto_chunk(retval, addr_param_len, &addr_param);
2727
2728 addr_buf += af->sockaddr_len;
2729 }
2730 return retval;
2731}
2732
2733/* ADDIP
2734 * 3.2.4 Set Primary IP Address
2735 * 0 1 2 3
2736 * 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
2737 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2738 * | Type =0xC004 | Length = Variable |
2739 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2740 * | ASCONF-Request Correlation ID |
2741 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2742 * | Address Parameter |
2743 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2744 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002745 * Create an ASCONF chunk with Set Primary IP address parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 */
2747struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
2748 union sctp_addr *addr)
2749{
2750 sctp_addip_param_t param;
2751 struct sctp_chunk *retval;
2752 int len = sizeof(param);
2753 union sctp_addr_param addrparam;
2754 int addrlen;
2755 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2756
2757 addrlen = af->to_addr_param(addr, &addrparam);
2758 if (!addrlen)
2759 return NULL;
2760 len += addrlen;
2761
2762 /* Create the chunk and make asconf header. */
2763 retval = sctp_make_asconf(asoc, addr, len);
2764 if (!retval)
2765 return NULL;
2766
2767 param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
2768 param.param_hdr.length = htons(len);
2769 param.crr_id = 0;
2770
2771 sctp_addto_chunk(retval, sizeof(param), &param);
2772 sctp_addto_chunk(retval, addrlen, &addrparam);
2773
2774 return retval;
2775}
2776
2777/* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2778 * 0 1 2 3
2779 * 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
2780 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2781 * | Type = 0x80 | Chunk Flags | Chunk Length |
2782 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2783 * | Serial Number |
2784 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2785 * | ASCONF Parameter Response#1 |
2786 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2787 * \ \
2788 * / .... /
2789 * \ \
2790 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2791 * | ASCONF Parameter Response#N |
2792 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2793 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002794 * Create an ASCONF_ACK chunk with enough space for the parameter responses.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 */
2796static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
2797 __u32 serial, int vparam_len)
2798{
2799 sctp_addiphdr_t asconf;
2800 struct sctp_chunk *retval;
2801 int length = sizeof(asconf) + vparam_len;
2802
2803 /* Create the chunk. */
2804 retval = sctp_make_chunk(asoc, SCTP_CID_ASCONF_ACK, 0, length);
2805 if (!retval)
2806 return NULL;
2807
2808 asconf.serial = htonl(serial);
2809
2810 retval->subh.addip_hdr =
2811 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2812
2813 return retval;
2814}
2815
2816/* Add response parameters to an ASCONF_ACK chunk. */
Al Viro9f81bcd2006-11-20 17:26:34 -08002817static void sctp_add_asconf_response(struct sctp_chunk *chunk, __be32 crr_id,
Al Virodbc16db2006-11-20 17:01:42 -08002818 __be16 err_code, sctp_addip_param_t *asconf_param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819{
2820 sctp_addip_param_t ack_param;
2821 sctp_errhdr_t err_param;
2822 int asconf_param_len = 0;
2823 int err_param_len = 0;
Al Virodbc16db2006-11-20 17:01:42 -08002824 __be16 response_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
2826 if (SCTP_ERROR_NO_ERROR == err_code) {
2827 response_type = SCTP_PARAM_SUCCESS_REPORT;
2828 } else {
2829 response_type = SCTP_PARAM_ERR_CAUSE;
2830 err_param_len = sizeof(err_param);
2831 if (asconf_param)
2832 asconf_param_len =
2833 ntohs(asconf_param->param_hdr.length);
2834 }
2835
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002836 /* Add Success Indication or Error Cause Indication parameter. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 ack_param.param_hdr.type = response_type;
2838 ack_param.param_hdr.length = htons(sizeof(ack_param) +
2839 err_param_len +
2840 asconf_param_len);
2841 ack_param.crr_id = crr_id;
2842 sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
2843
2844 if (SCTP_ERROR_NO_ERROR == err_code)
2845 return;
2846
2847 /* Add Error Cause parameter. */
2848 err_param.cause = err_code;
2849 err_param.length = htons(err_param_len + asconf_param_len);
2850 sctp_addto_chunk(chunk, err_param_len, &err_param);
2851
2852 /* Add the failed TLV copied from ASCONF chunk. */
2853 if (asconf_param)
2854 sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
2855}
2856
2857/* Process a asconf parameter. */
Al Virodbc16db2006-11-20 17:01:42 -08002858static __be16 sctp_process_asconf_param(struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 struct sctp_chunk *asconf,
2860 sctp_addip_param_t *asconf_param)
2861{
2862 struct sctp_transport *peer;
2863 struct sctp_af *af;
2864 union sctp_addr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 union sctp_addr_param *addr_param;
Al Viro5f242a12006-11-20 17:05:23 -08002866
Patrick McHardyc1cc6782008-05-13 23:25:00 -07002867 addr_param = (union sctp_addr_param *)
2868 ((void *)asconf_param + sizeof(sctp_addip_param_t));
2869
Wei Yongjun44e65c12009-06-16 14:48:24 +08002870 if (asconf_param->param_hdr.type != SCTP_PARAM_ADD_IP &&
2871 asconf_param->param_hdr.type != SCTP_PARAM_DEL_IP &&
2872 asconf_param->param_hdr.type != SCTP_PARAM_SET_PRIMARY)
2873 return SCTP_ERROR_UNKNOWN_PARAM;
2874
Wei Yongjunc4492582008-05-09 15:11:53 -07002875 switch (addr_param->v4.param_hdr.type) {
2876 case SCTP_PARAM_IPV6_ADDRESS:
2877 if (!asoc->peer.ipv6_address)
Wei Yongjun945e5ab2009-04-16 14:21:02 +08002878 return SCTP_ERROR_DNS_FAILED;
Wei Yongjunc4492582008-05-09 15:11:53 -07002879 break;
2880 case SCTP_PARAM_IPV4_ADDRESS:
2881 if (!asoc->peer.ipv4_address)
Wei Yongjun945e5ab2009-04-16 14:21:02 +08002882 return SCTP_ERROR_DNS_FAILED;
Wei Yongjunc4492582008-05-09 15:11:53 -07002883 break;
2884 default:
Wei Yongjun945e5ab2009-04-16 14:21:02 +08002885 return SCTP_ERROR_DNS_FAILED;
Wei Yongjunc4492582008-05-09 15:11:53 -07002886 }
2887
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
2889 if (unlikely(!af))
Wei Yongjun945e5ab2009-04-16 14:21:02 +08002890 return SCTP_ERROR_DNS_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
Al Virodd86d132006-11-20 17:11:13 -08002892 af->from_addr_param(&addr, addr_param, htons(asoc->peer.port), 0);
Vlad Yasevich42e30bf2007-12-20 14:08:56 -08002893
2894 /* ADDIP 4.2.1 This parameter MUST NOT contain a broadcast
2895 * or multicast address.
2896 * (note: wildcard is permitted and requires special handling so
2897 * make sure we check for that)
2898 */
2899 if (!af->is_any(&addr) && !af->addr_valid(&addr, NULL, asconf->skb))
Wei Yongjun945e5ab2009-04-16 14:21:02 +08002900 return SCTP_ERROR_DNS_FAILED;
Vlad Yasevich42e30bf2007-12-20 14:08:56 -08002901
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 switch (asconf_param->param_hdr.type) {
2903 case SCTP_PARAM_ADD_IP:
Vlad Yasevich42e30bf2007-12-20 14:08:56 -08002904 /* Section 4.2.1:
2905 * If the address 0.0.0.0 or ::0 is provided, the source
2906 * address of the packet MUST be added.
2907 */
2908 if (af->is_any(&addr))
2909 memcpy(&addr, &asconf->source, sizeof(addr));
2910
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002912 * request and does not have the local resources to add this
2913 * new address to the association, it MUST return an Error
2914 * Cause TLV set to the new error code 'Operation Refused
2915 * Due to Resource Shortage'.
2916 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
Al Virodd86d132006-11-20 17:11:13 -08002918 peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC, SCTP_UNCONFIRMED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 if (!peer)
2920 return SCTP_ERROR_RSRC_LOW;
2921
2922 /* Start the heartbeat timer. */
2923 if (!mod_timer(&peer->hb_timer, sctp_transport_timeout(peer)))
2924 sctp_transport_hold(peer);
2925 break;
2926 case SCTP_PARAM_DEL_IP:
2927 /* ADDIP 4.3 D7) If a request is received to delete the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002928 * last remaining IP address of a peer endpoint, the receiver
2929 * MUST send an Error Cause TLV with the error cause set to the
2930 * new error code 'Request to Delete Last Remaining IP Address'.
2931 */
Vlad Yasevich42e30bf2007-12-20 14:08:56 -08002932 if (asoc->peer.transport_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 return SCTP_ERROR_DEL_LAST_IP;
2934
2935 /* ADDIP 4.3 D8) If a request is received to delete an IP
2936 * address which is also the source address of the IP packet
2937 * which contained the ASCONF chunk, the receiver MUST reject
2938 * this request. To reject the request the receiver MUST send
2939 * an Error Cause TLV set to the new error code 'Request to
2940 * Delete Source IP Address'
2941 */
Al Viro6a1e5f32006-11-20 17:12:25 -08002942 if (sctp_cmp_addr_exact(sctp_source(asconf), &addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 return SCTP_ERROR_DEL_SRC_IP;
2944
Vlad Yasevich42e30bf2007-12-20 14:08:56 -08002945 /* Section 4.2.2
2946 * If the address 0.0.0.0 or ::0 is provided, all
2947 * addresses of the peer except the source address of the
2948 * packet MUST be deleted.
2949 */
2950 if (af->is_any(&addr)) {
2951 sctp_assoc_set_primary(asoc, asconf->transport);
2952 sctp_assoc_del_nonprimary_peers(asoc,
2953 asconf->transport);
2954 } else
2955 sctp_assoc_del_peer(asoc, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 break;
2957 case SCTP_PARAM_SET_PRIMARY:
Vlad Yasevich42e30bf2007-12-20 14:08:56 -08002958 /* ADDIP Section 4.2.4
2959 * If the address 0.0.0.0 or ::0 is provided, the receiver
2960 * MAY mark the source address of the packet as its
2961 * primary.
2962 */
2963 if (af->is_any(&addr))
2964 memcpy(&addr.v4, sctp_source(asconf), sizeof(addr));
2965
Al Virodd86d132006-11-20 17:11:13 -08002966 peer = sctp_assoc_lookup_paddr(asoc, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 if (!peer)
Wei Yongjun945e5ab2009-04-16 14:21:02 +08002968 return SCTP_ERROR_DNS_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
2970 sctp_assoc_set_primary(asoc, peer);
2971 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 }
2973
2974 return SCTP_ERROR_NO_ERROR;
2975}
2976
Wei Yongjun6f4c6182007-09-19 17:19:52 +08002977/* Verify the ASCONF packet before we process it. */
2978int sctp_verify_asconf(const struct sctp_association *asoc,
2979 struct sctp_paramhdr *param_hdr, void *chunk_end,
2980 struct sctp_paramhdr **errp) {
2981 sctp_addip_param_t *asconf_param;
2982 union sctp_params param;
2983 int length, plen;
2984
2985 param.v = (sctp_paramhdr_t *) param_hdr;
2986 while (param.v <= chunk_end - sizeof(sctp_paramhdr_t)) {
2987 length = ntohs(param.p->length);
2988 *errp = param.p;
2989
2990 if (param.v > chunk_end - length ||
2991 length < sizeof(sctp_paramhdr_t))
2992 return 0;
2993
2994 switch (param.p->type) {
2995 case SCTP_PARAM_ADD_IP:
2996 case SCTP_PARAM_DEL_IP:
2997 case SCTP_PARAM_SET_PRIMARY:
2998 asconf_param = (sctp_addip_param_t *)param.v;
2999 plen = ntohs(asconf_param->param_hdr.length);
3000 if (plen < sizeof(sctp_addip_param_t) +
3001 sizeof(sctp_paramhdr_t))
3002 return 0;
3003 break;
3004 case SCTP_PARAM_SUCCESS_REPORT:
3005 case SCTP_PARAM_ADAPTATION_LAYER_IND:
3006 if (length != sizeof(sctp_addip_param_t))
3007 return 0;
3008
3009 break;
3010 default:
3011 break;
3012 }
3013
3014 param.v += WORD_ROUND(length);
3015 }
3016
3017 if (param.v != chunk_end)
3018 return 0;
3019
3020 return 1;
3021}
3022
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003023/* Process an incoming ASCONF chunk with the next expected serial no. and
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 * return an ASCONF_ACK chunk to be sent in response.
3025 */
3026struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
3027 struct sctp_chunk *asconf)
3028{
3029 sctp_addiphdr_t *hdr;
3030 union sctp_addr_param *addr_param;
3031 sctp_addip_param_t *asconf_param;
3032 struct sctp_chunk *asconf_ack;
3033
Al Virodbc16db2006-11-20 17:01:42 -08003034 __be16 err_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 int length = 0;
Wei Yongjunf3830cc2007-10-15 11:51:03 +09003036 int chunk_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 __u32 serial;
3038 int all_param_pass = 1;
3039
Wei Yongjunf3830cc2007-10-15 11:51:03 +09003040 chunk_len = ntohs(asconf->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 hdr = (sctp_addiphdr_t *)asconf->skb->data;
3042 serial = ntohl(hdr->serial);
3043
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003044 /* Skip the addiphdr and store a pointer to address parameter. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 length = sizeof(sctp_addiphdr_t);
3046 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
3047 chunk_len -= length;
3048
3049 /* Skip the address parameter and store a pointer to the first
Joe Perches7aa1b542007-12-20 14:03:52 -08003050 * asconf parameter.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003051 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 length = ntohs(addr_param->v4.param_hdr.length);
3053 asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
3054 chunk_len -= length;
3055
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003056 /* create an ASCONF_ACK chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 * Based on the definitions of parameters, we know that the size of
3058 * ASCONF_ACK parameters are less than or equal to the twice of ASCONF
Joe Perches7aa1b542007-12-20 14:03:52 -08003059 * parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 */
3061 asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 2);
3062 if (!asconf_ack)
3063 goto done;
3064
3065 /* Process the TLVs contained within the ASCONF chunk. */
3066 while (chunk_len > 0) {
3067 err_code = sctp_process_asconf_param(asoc, asconf,
3068 asconf_param);
3069 /* ADDIP 4.1 A7)
3070 * If an error response is received for a TLV parameter,
3071 * all TLVs with no response before the failed TLV are
3072 * considered successful if not reported. All TLVs after
3073 * the failed response are considered unsuccessful unless
3074 * a specific success indication is present for the parameter.
3075 */
3076 if (SCTP_ERROR_NO_ERROR != err_code)
3077 all_param_pass = 0;
3078
3079 if (!all_param_pass)
3080 sctp_add_asconf_response(asconf_ack,
3081 asconf_param->crr_id, err_code,
3082 asconf_param);
3083
3084 /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
3085 * an IP address sends an 'Out of Resource' in its response, it
3086 * MUST also fail any subsequent add or delete requests bundled
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003087 * in the ASCONF.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 */
3089 if (SCTP_ERROR_RSRC_LOW == err_code)
3090 goto done;
3091
3092 /* Move to the next ASCONF param. */
3093 length = ntohs(asconf_param->param_hdr.length);
3094 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
3095 length);
3096 chunk_len -= length;
3097 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003098
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099done:
3100 asoc->peer.addip_serial++;
3101
3102 /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003103 * after freeing the reference to old asconf ack if any.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 */
3105 if (asconf_ack) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 sctp_chunk_hold(asconf_ack);
Vlad Yasevicha08de642007-12-20 14:11:47 -08003107 list_add_tail(&asconf_ack->transmitted_list,
3108 &asoc->asconf_ack_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 }
3110
3111 return asconf_ack;
3112}
3113
3114/* Process a asconf parameter that is successfully acked. */
Wei Yongjun425e0f62009-06-16 14:47:30 +08003115static void sctp_asconf_param_success(struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 sctp_addip_param_t *asconf_param)
3117{
3118 struct sctp_af *af;
3119 union sctp_addr addr;
3120 struct sctp_bind_addr *bp = &asoc->base.bind_addr;
3121 union sctp_addr_param *addr_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 struct sctp_transport *transport;
Sridhar Samudraladc022a92006-07-21 14:49:25 -07003123 struct sctp_sockaddr_entry *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
3125 addr_param = (union sctp_addr_param *)
3126 ((void *)asconf_param + sizeof(sctp_addip_param_t));
3127
3128 /* We have checked the packet before, so we do not check again. */
3129 af = sctp_get_af_specific(param_type2af(addr_param->v4.param_hdr.type));
Al Virodd86d132006-11-20 17:11:13 -08003130 af->from_addr_param(&addr, addr_param, htons(bp->port), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131
3132 switch (asconf_param->param_hdr.type) {
3133 case SCTP_PARAM_ADD_IP:
Vlad Yasevich559cf712007-09-16 16:03:28 -07003134 /* This is always done in BH context with a socket lock
3135 * held, so the list can not change.
3136 */
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -04003137 local_bh_disable();
Vlad Yasevich559cf712007-09-16 16:03:28 -07003138 list_for_each_entry(saddr, &bp->address_list, list) {
Al Virodd86d132006-11-20 17:11:13 -08003139 if (sctp_cmp_addr_exact(&saddr->a, &addr))
Vlad Yasevichf57d96b2007-12-20 14:12:24 -08003140 saddr->state = SCTP_ADDR_SRC;
Sridhar Samudraladc022a92006-07-21 14:49:25 -07003141 }
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -04003142 local_bh_enable();
Wei Yongjun3cd97492009-06-16 10:07:23 +08003143 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
3144 transports) {
3145 if (transport->state == SCTP_ACTIVE)
3146 continue;
3147 dst_release(transport->dst);
3148 sctp_transport_route(transport, NULL,
3149 sctp_sk(asoc->base.sk));
3150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 break;
3152 case SCTP_PARAM_DEL_IP:
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -04003153 local_bh_disable();
Wei Yongjun425e0f62009-06-16 14:47:30 +08003154 sctp_del_bind_addr(bp, &addr);
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -04003155 local_bh_enable();
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07003156 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
3157 transports) {
Sridhar Samudraladc022a92006-07-21 14:49:25 -07003158 dst_release(transport->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 sctp_transport_route(transport, NULL,
3160 sctp_sk(asoc->base.sk));
3161 }
3162 break;
3163 default:
3164 break;
3165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166}
3167
3168/* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
3169 * for the given asconf parameter. If there is no response for this parameter,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003170 * return the error code based on the third argument 'no_err'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 * ADDIP 4.1
3172 * A7) If an error response is received for a TLV parameter, all TLVs with no
3173 * response before the failed TLV are considered successful if not reported.
3174 * All TLVs after the failed response are considered unsuccessful unless a
3175 * specific success indication is present for the parameter.
3176 */
Al Virodbc16db2006-11-20 17:01:42 -08003177static __be16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 sctp_addip_param_t *asconf_param,
3179 int no_err)
3180{
3181 sctp_addip_param_t *asconf_ack_param;
3182 sctp_errhdr_t *err_param;
3183 int length;
Wei Yongjunf3830cc2007-10-15 11:51:03 +09003184 int asconf_ack_len;
Al Virodbc16db2006-11-20 17:01:42 -08003185 __be16 err_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
3187 if (no_err)
3188 err_code = SCTP_ERROR_NO_ERROR;
3189 else
3190 err_code = SCTP_ERROR_REQ_REFUSED;
3191
Wei Yongjunf3830cc2007-10-15 11:51:03 +09003192 asconf_ack_len = ntohs(asconf_ack->chunk_hdr->length) -
3193 sizeof(sctp_chunkhdr_t);
3194
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
3196 * the first asconf_ack parameter.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003197 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 length = sizeof(sctp_addiphdr_t);
3199 asconf_ack_param = (sctp_addip_param_t *)(asconf_ack->skb->data +
3200 length);
3201 asconf_ack_len -= length;
3202
3203 while (asconf_ack_len > 0) {
3204 if (asconf_ack_param->crr_id == asconf_param->crr_id) {
3205 switch(asconf_ack_param->param_hdr.type) {
3206 case SCTP_PARAM_SUCCESS_REPORT:
3207 return SCTP_ERROR_NO_ERROR;
3208 case SCTP_PARAM_ERR_CAUSE:
3209 length = sizeof(sctp_addip_param_t);
3210 err_param = (sctp_errhdr_t *)
3211 ((void *)asconf_ack_param + length);
3212 asconf_ack_len -= length;
3213 if (asconf_ack_len > 0)
3214 return err_param->cause;
3215 else
3216 return SCTP_ERROR_INV_PARAM;
3217 break;
3218 default:
3219 return SCTP_ERROR_INV_PARAM;
3220 }
3221 }
3222
3223 length = ntohs(asconf_ack_param->param_hdr.length);
3224 asconf_ack_param = (sctp_addip_param_t *)
3225 ((void *)asconf_ack_param + length);
3226 asconf_ack_len -= length;
3227 }
3228
3229 return err_code;
3230}
3231
3232/* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
3233int sctp_process_asconf_ack(struct sctp_association *asoc,
3234 struct sctp_chunk *asconf_ack)
3235{
3236 struct sctp_chunk *asconf = asoc->addip_last_asconf;
3237 union sctp_addr_param *addr_param;
3238 sctp_addip_param_t *asconf_param;
3239 int length = 0;
3240 int asconf_len = asconf->skb->len;
3241 int all_param_pass = 0;
3242 int no_err = 1;
3243 int retval = 0;
Al Virodbc16db2006-11-20 17:01:42 -08003244 __be16 err_code = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
3246 /* Skip the chunkhdr and addiphdr from the last asconf sent and store
3247 * a pointer to address parameter.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003248 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 length = sizeof(sctp_addip_chunk_t);
3250 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
3251 asconf_len -= length;
3252
3253 /* Skip the address parameter in the last asconf sent and store a
Joe Perches7aa1b542007-12-20 14:03:52 -08003254 * pointer to the first asconf parameter.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003255 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 length = ntohs(addr_param->v4.param_hdr.length);
3257 asconf_param = (sctp_addip_param_t *)((void *)addr_param + length);
3258 asconf_len -= length;
3259
3260 /* ADDIP 4.1
3261 * A8) If there is no response(s) to specific TLV parameter(s), and no
3262 * failures are indicated, then all request(s) are considered
3263 * successful.
3264 */
3265 if (asconf_ack->skb->len == sizeof(sctp_addiphdr_t))
3266 all_param_pass = 1;
3267
3268 /* Process the TLVs contained in the last sent ASCONF chunk. */
3269 while (asconf_len > 0) {
3270 if (all_param_pass)
3271 err_code = SCTP_ERROR_NO_ERROR;
3272 else {
3273 err_code = sctp_get_asconf_response(asconf_ack,
3274 asconf_param,
3275 no_err);
3276 if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
3277 no_err = 0;
3278 }
3279
3280 switch (err_code) {
3281 case SCTP_ERROR_NO_ERROR:
Wei Yongjun425e0f62009-06-16 14:47:30 +08003282 sctp_asconf_param_success(asoc, asconf_param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 break;
3284
3285 case SCTP_ERROR_RSRC_LOW:
3286 retval = 1;
3287 break;
3288
Wei Yongjuna987f762009-04-07 15:44:29 +08003289 case SCTP_ERROR_UNKNOWN_PARAM:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 /* Disable sending this type of asconf parameter in
3291 * future.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003292 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 asoc->peer.addip_disabled_mask |=
3294 asconf_param->param_hdr.type;
3295 break;
3296
3297 case SCTP_ERROR_REQ_REFUSED:
3298 case SCTP_ERROR_DEL_LAST_IP:
3299 case SCTP_ERROR_DEL_SRC_IP:
3300 default:
3301 break;
3302 }
3303
3304 /* Skip the processed asconf parameter and move to the next
3305 * one.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003306 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 length = ntohs(asconf_param->param_hdr.length);
3308 asconf_param = (sctp_addip_param_t *)((void *)asconf_param +
3309 length);
3310 asconf_len -= length;
3311 }
3312
3313 /* Free the cached last sent asconf chunk. */
Vlad Yasevich5f9646c2008-02-05 14:23:44 -05003314 list_del_init(&asconf->transmitted_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 sctp_chunk_free(asconf);
3316 asoc->addip_last_asconf = NULL;
3317
3318 /* Send the next asconf chunk from the addip chunk queue. */
David S. Miller79af02c2005-07-08 21:47:49 -07003319 if (!list_empty(&asoc->addip_chunk_list)) {
3320 struct list_head *entry = asoc->addip_chunk_list.next;
3321 asconf = list_entry(entry, struct sctp_chunk, list);
3322
3323 list_del_init(entry);
3324
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 /* Hold the chunk until an ASCONF_ACK is received. */
3326 sctp_chunk_hold(asconf);
3327 if (sctp_primitive_ASCONF(asoc, asconf))
3328 sctp_chunk_free(asconf);
3329 else
3330 asoc->addip_last_asconf = asconf;
3331 }
3332
3333 return retval;
3334}
3335
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003336/* Make a FWD TSN chunk. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
3338 __u32 new_cum_tsn, size_t nstreams,
3339 struct sctp_fwdtsn_skip *skiplist)
3340{
3341 struct sctp_chunk *retval = NULL;
3342 struct sctp_fwdtsn_chunk *ftsn_chunk;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003343 struct sctp_fwdtsn_hdr ftsn_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 struct sctp_fwdtsn_skip skip;
3345 size_t hint;
3346 int i;
3347
3348 hint = (nstreams + 1) * sizeof(__u32);
3349
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 retval = sctp_make_chunk(asoc, SCTP_CID_FWD_TSN, 0, hint);
3351
3352 if (!retval)
3353 return NULL;
3354
3355 ftsn_chunk = (struct sctp_fwdtsn_chunk *)retval->subh.fwdtsn_hdr;
3356
3357 ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
3358 retval->subh.fwdtsn_hdr =
3359 sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
3360
3361 for (i = 0; i < nstreams; i++) {
3362 skip.stream = skiplist[i].stream;
3363 skip.ssn = skiplist[i].ssn;
3364 sctp_addto_chunk(retval, sizeof(skip), &skip);
3365 }
3366
3367 return retval;
3368}