blob: e5f7cdb42a850c66c758c81b7f0ce999724158a9 [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
Jeff Kirsher4b2f13a2013-12-06 06:28:48 -080026 * along with GNU CC; see the file COPYING. If not, see
27 * <http://www.gnu.org/licenses/>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 *
29 * Please send any bug reports or fixes you make to the
30 * email address(es):
Daniel Borkmann91705c62013-07-23 14:51:47 +020031 * lksctp developers <linux-sctp@vger.kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 * Written or modified by:
34 * La Monte H.P. Yarroll <piggy@acm.org>
35 * Karl Knutson <karl@athena.chicago.il.us>
36 * C. Robin <chris@hundredacre.ac.uk>
37 * Jon Grimm <jgrimm@us.ibm.com>
38 * Xingang Guo <xingang.guo@intel.com>
39 * Dajiang Zhang <dajiang.zhang@nokia.com>
40 * Sridhar Samudrala <sri@us.ibm.com>
41 * Daisy Chang <daisyc@us.ibm.com>
42 * Ardelle Fan <ardelle.fan@intel.com>
43 * Kevin Gao <kevin.gao@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 */
45
Joe Perches145ce502010-08-24 13:21:08 +000046#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/types.h>
49#include <linux/kernel.h>
50#include <linux/ip.h>
51#include <linux/ipv6.h>
52#include <linux/net.h>
53#include <linux/inet.h>
Christian Borntraegerebc3bbc2007-10-23 12:46:32 +020054#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/crypto.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090056#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <net/sock.h>
58
59#include <linux/skbuff.h>
60#include <linux/random.h> /* for get_random_bytes */
61#include <net/sctp/sctp.h>
62#include <net/sctp/sm.h>
63
Vlad Yasevich072017b2013-08-09 22:05:36 -040064static struct sctp_chunk *sctp_make_control(const struct sctp_association *asoc,
65 __u8 type, __u8 flags, int paylen);
66static struct sctp_chunk *sctp_make_data(const struct sctp_association *asoc,
67 __u8 flags, int paylen);
68static struct sctp_chunk *_sctp_make_chunk(const struct sctp_association *asoc,
69 __u8 type, __u8 flags, int paylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
71 const struct sctp_association *asoc,
72 const struct sctp_chunk *init_chunk,
73 int *cookie_len,
74 const __u8 *raw_addrs, int addrs_len);
75static int sctp_process_param(struct sctp_association *asoc,
76 union sctp_params param,
77 const union sctp_addr *peer_addr,
Al Virodd0fc662005-10-07 07:46:04 +010078 gfp_t gfp);
Vlad Yasevich8ee4be32007-11-29 08:50:35 -050079static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
80 const void *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Vlad Yasevich072017b2013-08-09 22:05:36 -040082/* Control chunk destructor */
83static void sctp_control_release_owner(struct sk_buff *skb)
84{
85 /*TODO: do memory release */
86}
87
88static void sctp_control_set_owner_w(struct sctp_chunk *chunk)
89{
90 struct sctp_association *asoc = chunk->asoc;
91 struct sk_buff *skb = chunk->skb;
92
93 /* TODO: properly account for control chunks.
94 * To do it right we'll need:
95 * 1) endpoint if association isn't known.
96 * 2) proper memory accounting.
97 *
98 * For now don't do anything for now.
99 */
100 skb->sk = asoc ? asoc->base.sk : NULL;
101 skb->destructor = sctp_control_release_owner;
102}
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/* What was the inbound interface for this chunk? */
105int sctp_chunk_iif(const struct sctp_chunk *chunk)
106{
107 struct sctp_af *af;
108 int iif = 0;
109
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700110 af = sctp_get_af_specific(ipver2af(ip_hdr(chunk->skb)->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (af)
112 iif = af->skb_iif(chunk->skb);
113
114 return iif;
115}
116
117/* RFC 2960 3.3.2 Initiation (INIT) (1)
118 *
119 * Note 2: The ECN capable field is reserved for future use of
120 * Explicit Congestion Notification.
121 */
122static const struct sctp_paramhdr ecap_param = {
123 SCTP_PARAM_ECN_CAPABLE,
Harvey Harrison09640e632009-02-01 00:45:17 -0800124 cpu_to_be16(sizeof(struct sctp_paramhdr)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126static const struct sctp_paramhdr prsctp_param = {
127 SCTP_PARAM_FWD_TSN_SUPPORT,
Harvey Harrison09640e632009-02-01 00:45:17 -0800128 cpu_to_be16(sizeof(struct sctp_paramhdr)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129};
130
Neil Horman5fa782c2010-04-28 10:30:59 +0000131/* A helper to initialize an op error inside a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 * provided chunk, as most cause codes will be embedded inside an
133 * abort chunk.
134 */
Al Viro5bf2db02006-11-20 16:59:45 -0800135void sctp_init_cause(struct sctp_chunk *chunk, __be16 cause_code,
Wei Yongjun00f1c2d2007-08-21 15:50:01 +0800136 size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 sctp_errhdr_t err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 __u16 len;
140
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900141 /* Cause code constants are now defined in network order. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 err.cause = cause_code;
143 len = sizeof(sctp_errhdr_t) + paylen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 err.length = htons(len);
Vlad Yasevich4a1c0102007-01-09 14:35:51 -0800145 chunk->subh.err_hdr = sctp_addto_chunk(chunk, sizeof(sctp_errhdr_t), &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Neil Horman5fa782c2010-04-28 10:30:59 +0000148/* A helper to initialize an op error inside a
149 * provided chunk, as most cause codes will be embedded inside an
150 * abort chunk. Differs from sctp_init_cause in that it won't oops
151 * if there isn't enough space in the op error chunk
152 */
Ioan Orghicidb28aaf2012-07-13 07:16:37 +0000153static int sctp_init_cause_fixed(struct sctp_chunk *chunk, __be16 cause_code,
Neil Horman5fa782c2010-04-28 10:30:59 +0000154 size_t paylen)
155{
156 sctp_errhdr_t err;
157 __u16 len;
158
159 /* Cause code constants are now defined in network order. */
160 err.cause = cause_code;
161 len = sizeof(sctp_errhdr_t) + paylen;
162 err.length = htons(len);
163
Wei Yongjun2e3219b2010-05-17 22:51:58 -0700164 if (skb_tailroom(chunk->skb) < len)
Neil Horman5fa782c2010-04-28 10:30:59 +0000165 return -ENOSPC;
166 chunk->subh.err_hdr = sctp_addto_chunk_fixed(chunk,
167 sizeof(sctp_errhdr_t),
168 &err);
169 return 0;
170}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/* 3.3.2 Initiation (INIT) (1)
172 *
173 * This chunk is used to initiate a SCTP association between two
174 * endpoints. The format of the INIT chunk is shown below:
175 *
176 * 0 1 2 3
177 * 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
178 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
179 * | Type = 1 | Chunk Flags | Chunk Length |
180 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
181 * | Initiate Tag |
182 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
183 * | Advertised Receiver Window Credit (a_rwnd) |
184 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
185 * | Number of Outbound Streams | Number of Inbound Streams |
186 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
187 * | Initial TSN |
188 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
189 * \ \
190 * / Optional/Variable-Length Parameters /
191 * \ \
192 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
193 *
194 *
195 * The INIT chunk contains the following parameters. Unless otherwise
196 * noted, each parameter MUST only be included once in the INIT chunk.
197 *
198 * Fixed Parameters Status
199 * ----------------------------------------------
200 * Initiate Tag Mandatory
201 * Advertised Receiver Window Credit Mandatory
202 * Number of Outbound Streams Mandatory
203 * Number of Inbound Streams Mandatory
204 * Initial TSN Mandatory
205 *
206 * Variable Parameters Status Type Value
207 * -------------------------------------------------------------
208 * IPv4 Address (Note 1) Optional 5
209 * IPv6 Address (Note 1) Optional 6
210 * Cookie Preservative Optional 9
211 * Reserved for ECN Capable (Note 2) Optional 32768 (0x8000)
212 * Host Name Address (Note 3) Optional 11
213 * Supported Address Types (Note 4) Optional 12
214 */
215struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
216 const struct sctp_bind_addr *bp,
Al Virodd0fc662005-10-07 07:46:04 +0100217 gfp_t gfp, int vparam_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000219 struct net *net = sock_net(asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 sctp_inithdr_t init;
221 union sctp_params addrs;
222 size_t chunksize;
223 struct sctp_chunk *retval = NULL;
224 int num_types, addrs_len = 0;
225 struct sctp_sock *sp;
226 sctp_supported_addrs_param_t sat;
Al Viro3dbe8652006-11-20 17:25:49 -0800227 __be16 types[2];
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800228 sctp_adaptation_ind_param_t aiparam;
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700229 sctp_supported_ext_param_t ext_param;
230 int num_ext = 0;
231 __u8 extensions[3];
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700232 sctp_paramhdr_t *auth_chunks = NULL,
233 *auth_hmacs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 /* RFC 2960 3.3.2 Initiation (INIT) (1)
236 *
237 * Note 1: The INIT chunks can contain multiple addresses that
238 * can be IPv4 and/or IPv6 in any combination.
239 */
240 retval = NULL;
241
242 /* Convert the provided bind address list to raw format. */
243 addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, gfp);
244
245 init.init_tag = htonl(asoc->c.my_vtag);
246 init.a_rwnd = htonl(asoc->rwnd);
247 init.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
248 init.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
249 init.initial_tsn = htonl(asoc->c.initial_tsn);
250
251 /* How many address types are needed? */
252 sp = sctp_sk(asoc->base.sk);
253 num_types = sp->pf->supported_addrs(sp, types);
254
Wei Yongjuna8170c32010-04-28 08:47:21 +0000255 chunksize = sizeof(init) + addrs_len;
256 chunksize += WORD_ROUND(SCTP_SAT_LEN(num_types));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 chunksize += sizeof(ecap_param);
Vlad Yasevich8ee4be32007-11-29 08:50:35 -0500258
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000259 if (net->sctp.prsctp_enable)
Vlad Yasevich036b5792008-01-07 00:28:16 -0800260 chunksize += sizeof(prsctp_param);
261
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700262 /* ADDIP: Section 4.2.7:
263 * An implementation supporting this extension [ADDIP] MUST list
264 * the ASCONF,the ASCONF-ACK, and the AUTH chunks in its INIT and
265 * INIT-ACK parameters.
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700266 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000267 if (net->sctp.addip_enable) {
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700268 extensions[num_ext] = SCTP_CID_ASCONF;
269 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
270 num_ext += 2;
271 }
272
malc6fc791e2009-03-12 09:49:20 +0000273 if (sp->adaptation_ind)
274 chunksize += sizeof(aiparam);
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 chunksize += vparam_len;
277
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700278 /* Account for AUTH related parameters */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000279 if (net->sctp.auth_enable) {
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700280 /* Add random parameter length*/
281 chunksize += sizeof(asoc->c.auth_random);
282
283 /* Add HMACS parameter length if any were defined */
284 auth_hmacs = (sctp_paramhdr_t *)asoc->c.auth_hmacs;
285 if (auth_hmacs->length)
Wei Yongjuna8170c32010-04-28 08:47:21 +0000286 chunksize += WORD_ROUND(ntohs(auth_hmacs->length));
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700287 else
288 auth_hmacs = NULL;
289
290 /* Add CHUNKS parameter length */
291 auth_chunks = (sctp_paramhdr_t *)asoc->c.auth_chunks;
292 if (auth_chunks->length)
Wei Yongjuna8170c32010-04-28 08:47:21 +0000293 chunksize += WORD_ROUND(ntohs(auth_chunks->length));
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700294 else
Vlad Yasevich9baffaa2007-11-29 08:44:34 -0500295 auth_chunks = NULL;
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700296
297 extensions[num_ext] = SCTP_CID_AUTH;
298 num_ext += 1;
299 }
300
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700301 /* If we have any extensions to report, account for that */
302 if (num_ext)
Wei Yongjuna8170c32010-04-28 08:47:21 +0000303 chunksize += WORD_ROUND(sizeof(sctp_supported_ext_param_t) +
304 num_ext);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 /* RFC 2960 3.3.2 Initiation (INIT) (1)
307 *
308 * Note 3: An INIT chunk MUST NOT contain more than one Host
309 * Name address parameter. Moreover, the sender of the INIT
310 * MUST NOT combine any other address types with the Host Name
311 * address in the INIT. The receiver of INIT MUST ignore any
312 * other address types if the Host Name address parameter is
313 * present in the received INIT chunk.
314 *
315 * PLEASE DO NOT FIXME [This version does not support Host Name.]
316 */
317
Vlad Yasevich072017b2013-08-09 22:05:36 -0400318 retval = sctp_make_control(asoc, SCTP_CID_INIT, 0, chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (!retval)
320 goto nodata;
321
322 retval->subh.init_hdr =
323 sctp_addto_chunk(retval, sizeof(init), &init);
324 retval->param_hdr.v =
325 sctp_addto_chunk(retval, addrs_len, addrs.v);
326
327 /* RFC 2960 3.3.2 Initiation (INIT) (1)
328 *
329 * Note 4: This parameter, when present, specifies all the
330 * address types the sending endpoint can support. The absence
331 * of this parameter indicates that the sending endpoint can
332 * support any address type.
333 */
334 sat.param_hdr.type = SCTP_PARAM_SUPPORTED_ADDRESS_TYPES;
335 sat.param_hdr.length = htons(SCTP_SAT_LEN(num_types));
336 sctp_addto_chunk(retval, sizeof(sat), &sat);
337 sctp_addto_chunk(retval, num_types * sizeof(__u16), &types);
338
339 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700340
Joe Perches7aa1b542007-12-20 14:03:52 -0800341 /* Add the supported extensions parameter. Be nice and add this
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700342 * fist before addiding the parameters for the extensions themselves
343 */
344 if (num_ext) {
345 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
346 ext_param.param_hdr.length =
347 htons(sizeof(sctp_supported_ext_param_t) + num_ext);
348 sctp_addto_chunk(retval, sizeof(sctp_supported_ext_param_t),
349 &ext_param);
Vlad Yasevich8ee4be32007-11-29 08:50:35 -0500350 sctp_addto_param(retval, num_ext, extensions);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700351 }
352
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000353 if (net->sctp.prsctp_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700355
malc6fc791e2009-03-12 09:49:20 +0000356 if (sp->adaptation_ind) {
357 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
358 aiparam.param_hdr.length = htons(sizeof(aiparam));
359 aiparam.adaptation_ind = htonl(sp->adaptation_ind);
360 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
361 }
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700362
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700363 /* Add SCTP-AUTH chunks to the parameter list */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000364 if (net->sctp.auth_enable) {
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700365 sctp_addto_chunk(retval, sizeof(asoc->c.auth_random),
366 asoc->c.auth_random);
367 if (auth_hmacs)
368 sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
369 auth_hmacs);
370 if (auth_chunks)
371 sctp_addto_chunk(retval, ntohs(auth_chunks->length),
372 auth_chunks);
373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374nodata:
Jesper Juhla51482b2005-11-08 09:41:34 -0800375 kfree(addrs.v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return retval;
377}
378
379struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
380 const struct sctp_chunk *chunk,
Al Virodd0fc662005-10-07 07:46:04 +0100381 gfp_t gfp, int unkparam_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 sctp_inithdr_t initack;
384 struct sctp_chunk *retval;
385 union sctp_params addrs;
malc6fc791e2009-03-12 09:49:20 +0000386 struct sctp_sock *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 int addrs_len;
388 sctp_cookie_param_t *cookie;
389 int cookie_len;
390 size_t chunksize;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800391 sctp_adaptation_ind_param_t aiparam;
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700392 sctp_supported_ext_param_t ext_param;
393 int num_ext = 0;
394 __u8 extensions[3];
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700395 sctp_paramhdr_t *auth_chunks = NULL,
396 *auth_hmacs = NULL,
397 *auth_random = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 retval = NULL;
400
401 /* Note: there may be no addresses to embed. */
402 addrs = sctp_bind_addrs_to_raw(&asoc->base.bind_addr, &addrs_len, gfp);
403
404 initack.init_tag = htonl(asoc->c.my_vtag);
405 initack.a_rwnd = htonl(asoc->rwnd);
406 initack.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
407 initack.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
408 initack.initial_tsn = htonl(asoc->c.initial_tsn);
409
410 /* FIXME: We really ought to build the cookie right
411 * into the packet instead of allocating more fresh memory.
412 */
413 cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
414 addrs.v, addrs_len);
415 if (!cookie)
416 goto nomem_cookie;
417
418 /* Calculate the total size of allocation, include the reserved
419 * space for reporting unknown parameters if it is specified.
420 */
malc6fc791e2009-03-12 09:49:20 +0000421 sp = sctp_sk(asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;
423
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900424 /* Tell peer that we'll do ECN only if peer advertised such cap. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (asoc->peer.ecn_capable)
426 chunksize += sizeof(ecap_param);
427
Wei Yongjun5ffad5a2009-03-12 09:49:18 +0000428 if (asoc->peer.prsctp_capable)
Vlad Yasevich036b5792008-01-07 00:28:16 -0800429 chunksize += sizeof(prsctp_param);
430
Wei Yongjun5ffad5a2009-03-12 09:49:18 +0000431 if (asoc->peer.asconf_capable) {
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700432 extensions[num_ext] = SCTP_CID_ASCONF;
433 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
434 num_ext += 2;
435 }
436
malc6fc791e2009-03-12 09:49:20 +0000437 if (sp->adaptation_ind)
438 chunksize += sizeof(aiparam);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700440 if (asoc->peer.auth_capable) {
441 auth_random = (sctp_paramhdr_t *)asoc->c.auth_random;
442 chunksize += ntohs(auth_random->length);
443
444 auth_hmacs = (sctp_paramhdr_t *)asoc->c.auth_hmacs;
445 if (auth_hmacs->length)
Wei Yongjuna8170c32010-04-28 08:47:21 +0000446 chunksize += WORD_ROUND(ntohs(auth_hmacs->length));
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700447 else
448 auth_hmacs = NULL;
449
450 auth_chunks = (sctp_paramhdr_t *)asoc->c.auth_chunks;
451 if (auth_chunks->length)
Wei Yongjuna8170c32010-04-28 08:47:21 +0000452 chunksize += WORD_ROUND(ntohs(auth_chunks->length));
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700453 else
454 auth_chunks = NULL;
455
456 extensions[num_ext] = SCTP_CID_AUTH;
457 num_ext += 1;
458 }
459
Vlad Yasevich8ee4be32007-11-29 08:50:35 -0500460 if (num_ext)
Wei Yongjuna8170c32010-04-28 08:47:21 +0000461 chunksize += WORD_ROUND(sizeof(sctp_supported_ext_param_t) +
462 num_ext);
Vlad Yasevich8ee4be32007-11-29 08:50:35 -0500463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /* Now allocate and fill out the chunk. */
Vlad Yasevich072017b2013-08-09 22:05:36 -0400465 retval = sctp_make_control(asoc, SCTP_CID_INIT_ACK, 0, chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (!retval)
467 goto nomem_chunk;
468
Dan Carpenterb99a4d52010-04-30 22:41:09 -0400469 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
470 *
471 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
472 * HEARTBEAT ACK, * etc.) to the same destination transport
473 * address from which it received the DATA or control chunk
474 * to which it is replying.
475 *
476 * [INIT ACK back to where the INIT came from.]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 */
478 retval->transport = chunk->transport;
Dan Carpenterb99a4d52010-04-30 22:41:09 -0400479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 retval->subh.init_hdr =
481 sctp_addto_chunk(retval, sizeof(initack), &initack);
482 retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
483 sctp_addto_chunk(retval, cookie_len, cookie);
484 if (asoc->peer.ecn_capable)
485 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700486 if (num_ext) {
487 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
488 ext_param.param_hdr.length =
489 htons(sizeof(sctp_supported_ext_param_t) + num_ext);
490 sctp_addto_chunk(retval, sizeof(sctp_supported_ext_param_t),
491 &ext_param);
Vlad Yasevich8ee4be32007-11-29 08:50:35 -0500492 sctp_addto_param(retval, num_ext, extensions);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (asoc->peer.prsctp_capable)
495 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
496
malc6fc791e2009-03-12 09:49:20 +0000497 if (sp->adaptation_ind) {
498 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
499 aiparam.param_hdr.length = htons(sizeof(aiparam));
500 aiparam.adaptation_ind = htonl(sp->adaptation_ind);
501 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700504 if (asoc->peer.auth_capable) {
505 sctp_addto_chunk(retval, ntohs(auth_random->length),
506 auth_random);
507 if (auth_hmacs)
508 sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
509 auth_hmacs);
510 if (auth_chunks)
511 sctp_addto_chunk(retval, ntohs(auth_chunks->length),
512 auth_chunks);
513 }
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /* We need to remove the const qualifier at this point. */
516 retval->asoc = (struct sctp_association *) asoc;
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518nomem_chunk:
519 kfree(cookie);
520nomem_cookie:
Jesper Juhla51482b2005-11-08 09:41:34 -0800521 kfree(addrs.v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return retval;
523}
524
525/* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
526 *
527 * This chunk is used only during the initialization of an association.
528 * It is sent by the initiator of an association to its peer to complete
529 * the initialization process. This chunk MUST precede any DATA chunk
530 * sent within the association, but MAY be bundled with one or more DATA
531 * chunks in the same packet.
532 *
533 * 0 1 2 3
534 * 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
535 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
536 * | Type = 10 |Chunk Flags | Length |
537 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
538 * / Cookie /
539 * \ \
540 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
541 *
542 * Chunk Flags: 8 bit
543 *
544 * Set to zero on transmit and ignored on receipt.
545 *
546 * Length: 16 bits (unsigned integer)
547 *
548 * Set to the size of the chunk in bytes, including the 4 bytes of
549 * the chunk header and the size of the Cookie.
550 *
551 * Cookie: variable size
552 *
553 * This field must contain the exact cookie received in the
554 * State Cookie parameter from the previous INIT ACK.
555 *
556 * An implementation SHOULD make the cookie as small as possible
557 * to insure interoperability.
558 */
559struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
560 const struct sctp_chunk *chunk)
561{
562 struct sctp_chunk *retval;
563 void *cookie;
564 int cookie_len;
565
566 cookie = asoc->peer.cookie;
567 cookie_len = asoc->peer.cookie_len;
568
569 /* Build a cookie echo chunk. */
Vlad Yasevich072017b2013-08-09 22:05:36 -0400570 retval = sctp_make_control(asoc, SCTP_CID_COOKIE_ECHO, 0, cookie_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (!retval)
572 goto nodata;
573 retval->subh.cookie_hdr =
574 sctp_addto_chunk(retval, cookie_len, cookie);
575
576 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
577 *
578 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
579 * HEARTBEAT ACK, * etc.) to the same destination transport
580 * address from which it * received the DATA or control chunk
581 * to which it is replying.
582 *
583 * [COOKIE ECHO back to where the INIT ACK came from.]
584 */
585 if (chunk)
586 retval->transport = chunk->transport;
587
588nodata:
589 return retval;
590}
591
592/* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
593 *
594 * This chunk is used only during the initialization of an
595 * association. It is used to acknowledge the receipt of a COOKIE
596 * ECHO chunk. This chunk MUST precede any DATA or SACK chunk sent
597 * within the association, but MAY be bundled with one or more DATA
598 * chunks or SACK chunk in the same SCTP packet.
599 *
600 * 0 1 2 3
601 * 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
602 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
603 * | Type = 11 |Chunk Flags | Length = 4 |
604 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
605 *
606 * Chunk Flags: 8 bits
607 *
608 * Set to zero on transmit and ignored on receipt.
609 */
610struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
611 const struct sctp_chunk *chunk)
612{
613 struct sctp_chunk *retval;
614
Vlad Yasevich072017b2013-08-09 22:05:36 -0400615 retval = sctp_make_control(asoc, SCTP_CID_COOKIE_ACK, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
618 *
619 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
620 * HEARTBEAT ACK, * etc.) to the same destination transport
621 * address from which it * received the DATA or control chunk
622 * to which it is replying.
623 *
624 * [COOKIE ACK back to where the COOKIE ECHO came from.]
625 */
626 if (retval && chunk)
627 retval->transport = chunk->transport;
628
629 return retval;
630}
631
632/*
633 * Appendix A: Explicit Congestion Notification:
634 * CWR:
635 *
636 * RFC 2481 details a specific bit for a sender to send in the header of
637 * its next outbound TCP segment to indicate to its peer that it has
638 * reduced its congestion window. This is termed the CWR bit. For
639 * SCTP the same indication is made by including the CWR chunk.
640 * This chunk contains one data element, i.e. the TSN number that
641 * was sent in the ECNE chunk. This element represents the lowest
642 * TSN number in the datagram that was originally marked with the
643 * CE bit.
644 *
645 * 0 1 2 3
646 * 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
647 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
648 * | Chunk Type=13 | Flags=00000000| Chunk Length = 8 |
649 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
650 * | Lowest TSN Number |
651 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
652 *
653 * Note: The CWR is considered a Control chunk.
654 */
655struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
656 const __u32 lowest_tsn,
657 const struct sctp_chunk *chunk)
658{
659 struct sctp_chunk *retval;
660 sctp_cwrhdr_t cwr;
661
662 cwr.lowest_tsn = htonl(lowest_tsn);
Vlad Yasevich072017b2013-08-09 22:05:36 -0400663 retval = sctp_make_control(asoc, SCTP_CID_ECN_CWR, 0,
664 sizeof(sctp_cwrhdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 if (!retval)
667 goto nodata;
668
669 retval->subh.ecn_cwr_hdr =
670 sctp_addto_chunk(retval, sizeof(cwr), &cwr);
671
672 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
673 *
674 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
675 * HEARTBEAT ACK, * etc.) to the same destination transport
676 * address from which it * received the DATA or control chunk
677 * to which it is replying.
678 *
679 * [Report a reduced congestion window back to where the ECNE
680 * came from.]
681 */
682 if (chunk)
683 retval->transport = chunk->transport;
684
685nodata:
686 return retval;
687}
688
689/* Make an ECNE chunk. This is a congestion experienced report. */
690struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
691 const __u32 lowest_tsn)
692{
693 struct sctp_chunk *retval;
694 sctp_ecnehdr_t ecne;
695
696 ecne.lowest_tsn = htonl(lowest_tsn);
Vlad Yasevich072017b2013-08-09 22:05:36 -0400697 retval = sctp_make_control(asoc, SCTP_CID_ECN_ECNE, 0,
698 sizeof(sctp_ecnehdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (!retval)
700 goto nodata;
701 retval->subh.ecne_hdr =
702 sctp_addto_chunk(retval, sizeof(ecne), &ecne);
703
704nodata:
705 return retval;
706}
707
708/* Make a DATA chunk for the given association from the provided
709 * parameters. However, do not populate the data payload.
710 */
711struct sctp_chunk *sctp_make_datafrag_empty(struct sctp_association *asoc,
712 const struct sctp_sndrcvinfo *sinfo,
713 int data_len, __u8 flags, __u16 ssn)
714{
715 struct sctp_chunk *retval;
716 struct sctp_datahdr dp;
717 int chunk_len;
718
719 /* We assign the TSN as LATE as possible, not here when
720 * creating the chunk.
721 */
722 dp.tsn = 0;
723 dp.stream = htons(sinfo->sinfo_stream);
724 dp.ppid = sinfo->sinfo_ppid;
725
726 /* Set the flags for an unordered send. */
Ivan Skytte Jorgenseneaa5c542005-10-28 15:10:00 -0700727 if (sinfo->sinfo_flags & SCTP_UNORDERED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 flags |= SCTP_DATA_UNORDERED;
729 dp.ssn = 0;
730 } else
731 dp.ssn = htons(ssn);
732
733 chunk_len = sizeof(dp) + data_len;
Vlad Yasevich072017b2013-08-09 22:05:36 -0400734 retval = sctp_make_data(asoc, flags, chunk_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (!retval)
736 goto nodata;
737
738 retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
739 memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
740
741nodata:
742 return retval;
743}
744
745/* Create a selective ackowledgement (SACK) for the given
746 * association. This reports on which TSN's we've seen to date,
747 * including duplicates and gaps.
748 */
749struct sctp_chunk *sctp_make_sack(const struct sctp_association *asoc)
750{
751 struct sctp_chunk *retval;
752 struct sctp_sackhdr sack;
753 int len;
754 __u32 ctsn;
755 __u16 num_gabs, num_dup_tsns;
Neil Horman42448542012-06-30 03:04:26 +0000756 struct sctp_association *aptr = (struct sctp_association *)asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
Vlad Yasevich02015182008-10-08 14:19:01 -0700758 struct sctp_gap_ack_block gabs[SCTP_MAX_GABS];
Neil Horman42448542012-06-30 03:04:26 +0000759 struct sctp_transport *trans;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Vlad Yasevich02015182008-10-08 14:19:01 -0700761 memset(gabs, 0, sizeof(gabs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 ctsn = sctp_tsnmap_get_ctsn(map);
Daniel Borkmannbb333812013-06-28 19:49:40 +0200763
764 pr_debug("%s: sackCTSNAck sent:0x%x\n", __func__, ctsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 /* How much room is needed in the chunk? */
Vlad Yasevich02015182008-10-08 14:19:01 -0700767 num_gabs = sctp_tsnmap_num_gabs(map, gabs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 num_dup_tsns = sctp_tsnmap_num_dups(map);
769
770 /* Initialize the SACK header. */
771 sack.cum_tsn_ack = htonl(ctsn);
772 sack.a_rwnd = htonl(asoc->a_rwnd);
773 sack.num_gap_ack_blocks = htons(num_gabs);
774 sack.num_dup_tsns = htons(num_dup_tsns);
775
776 len = sizeof(sack)
777 + sizeof(struct sctp_gap_ack_block) * num_gabs
778 + sizeof(__u32) * num_dup_tsns;
779
780 /* Create the chunk. */
Vlad Yasevich072017b2013-08-09 22:05:36 -0400781 retval = sctp_make_control(asoc, SCTP_CID_SACK, 0, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (!retval)
783 goto nodata;
784
785 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
786 *
787 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
788 * HEARTBEAT ACK, etc.) to the same destination transport
789 * address from which it received the DATA or control chunk to
790 * which it is replying. This rule should also be followed if
791 * the endpoint is bundling DATA chunks together with the
792 * reply chunk.
793 *
794 * However, when acknowledging multiple DATA chunks received
795 * in packets from different source addresses in a single
796 * SACK, the SACK chunk may be transmitted to one of the
797 * destination transport addresses from which the DATA or
798 * control chunks being acknowledged were received.
799 *
800 * [BUG: We do not implement the following paragraph.
801 * Perhaps we should remember the last transport we used for a
802 * SACK and avoid that (if possible) if we have seen any
803 * duplicates. --piggy]
804 *
805 * When a receiver of a duplicate DATA chunk sends a SACK to a
806 * multi- homed endpoint it MAY be beneficial to vary the
807 * destination address and not use the source address of the
808 * DATA chunk. The reason being that receiving a duplicate
809 * from a multi-homed endpoint might indicate that the return
810 * path (as specified in the source address of the DATA chunk)
811 * for the SACK is broken.
812 *
813 * [Send to the address from which we last received a DATA chunk.]
814 */
815 retval->transport = asoc->peer.last_data_from;
816
817 retval->subh.sack_hdr =
818 sctp_addto_chunk(retval, sizeof(sack), &sack);
819
820 /* Add the gap ack block information. */
821 if (num_gabs)
822 sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
Vlad Yasevich02015182008-10-08 14:19:01 -0700823 gabs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 /* Add the duplicate TSN information. */
Michele Baldessari196d6752012-12-01 04:49:42 +0000826 if (num_dup_tsns) {
827 aptr->stats.idupchunks += num_dup_tsns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
829 sctp_tsnmap_get_dups(map));
Michele Baldessari196d6752012-12-01 04:49:42 +0000830 }
Neil Horman42448542012-06-30 03:04:26 +0000831 /* Once we have a sack generated, check to see what our sack
832 * generation is, if its 0, reset the transports to 0, and reset
833 * the association generation to 1
834 *
835 * The idea is that zero is never used as a valid generation for the
836 * association so no transport will match after a wrap event like this,
837 * Until the next sack
838 */
839 if (++aptr->peer.sack_generation == 0) {
840 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
841 transports)
842 trans->sack_generation = 0;
843 aptr->peer.sack_generation = 1;
844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845nodata:
846 return retval;
847}
848
849/* Make a SHUTDOWN chunk. */
850struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
851 const struct sctp_chunk *chunk)
852{
853 struct sctp_chunk *retval;
854 sctp_shutdownhdr_t shut;
855 __u32 ctsn;
856
857 ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
858 shut.cum_tsn_ack = htonl(ctsn);
859
Vlad Yasevich072017b2013-08-09 22:05:36 -0400860 retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN, 0,
861 sizeof(sctp_shutdownhdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (!retval)
863 goto nodata;
864
865 retval->subh.shutdown_hdr =
866 sctp_addto_chunk(retval, sizeof(shut), &shut);
867
868 if (chunk)
869 retval->transport = chunk->transport;
870nodata:
871 return retval;
872}
873
874struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
875 const struct sctp_chunk *chunk)
876{
877 struct sctp_chunk *retval;
878
Vlad Yasevich072017b2013-08-09 22:05:36 -0400879 retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
882 *
883 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
884 * HEARTBEAT ACK, * etc.) to the same destination transport
885 * address from which it * received the DATA or control chunk
886 * to which it is replying.
887 *
888 * [ACK back to where the SHUTDOWN came from.]
889 */
890 if (retval && chunk)
891 retval->transport = chunk->transport;
892
893 return retval;
894}
895
896struct sctp_chunk *sctp_make_shutdown_complete(
897 const struct sctp_association *asoc,
898 const struct sctp_chunk *chunk)
899{
900 struct sctp_chunk *retval;
901 __u8 flags = 0;
902
Jerome Forissier047a2422005-04-28 11:58:43 -0700903 /* Set the T-bit if we have no association (vtag will be
904 * reflected)
905 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
907
Vlad Yasevich072017b2013-08-09 22:05:36 -0400908 retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
911 *
912 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
913 * HEARTBEAT ACK, * etc.) to the same destination transport
914 * address from which it * received the DATA or control chunk
915 * to which it is replying.
916 *
917 * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
918 * came from.]
919 */
920 if (retval && chunk)
921 retval->transport = chunk->transport;
922
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900923 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
926/* Create an ABORT. Note that we set the T bit if we have no
Jerome Forissier047a2422005-04-28 11:58:43 -0700927 * association, except when responding to an INIT (sctpimpguide 2.41).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 */
929struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
930 const struct sctp_chunk *chunk,
931 const size_t hint)
932{
933 struct sctp_chunk *retval;
934 __u8 flags = 0;
935
Jerome Forissier047a2422005-04-28 11:58:43 -0700936 /* Set the T-bit if we have no association and 'chunk' is not
937 * an INIT (vtag will be reflected).
938 */
939 if (!asoc) {
940 if (chunk && chunk->chunk_hdr &&
941 chunk->chunk_hdr->type == SCTP_CID_INIT)
942 flags = 0;
943 else
944 flags = SCTP_CHUNK_FLAG_T;
945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Vlad Yasevich072017b2013-08-09 22:05:36 -0400947 retval = sctp_make_control(asoc, SCTP_CID_ABORT, flags, hint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
950 *
951 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
952 * HEARTBEAT ACK, * etc.) to the same destination transport
953 * address from which it * received the DATA or control chunk
954 * to which it is replying.
955 *
956 * [ABORT back to where the offender came from.]
957 */
958 if (retval && chunk)
959 retval->transport = chunk->transport;
960
961 return retval;
962}
963
964/* Helper to create ABORT with a NO_USER_DATA error. */
965struct sctp_chunk *sctp_make_abort_no_data(
966 const struct sctp_association *asoc,
967 const struct sctp_chunk *chunk, __u32 tsn)
968{
969 struct sctp_chunk *retval;
Al Viro9f81bcd2006-11-20 17:26:34 -0800970 __be32 payload;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t)
973 + sizeof(tsn));
974
975 if (!retval)
976 goto no_mem;
977
978 /* Put the tsn back into network byte order. */
979 payload = htonl(tsn);
Wei Yongjun00f1c2d2007-08-21 15:50:01 +0800980 sctp_init_cause(retval, SCTP_ERROR_NO_DATA, sizeof(payload));
981 sctp_addto_chunk(retval, sizeof(payload), (const void *)&payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
984 *
985 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
986 * HEARTBEAT ACK, * etc.) to the same destination transport
987 * address from which it * received the DATA or control chunk
988 * to which it is replying.
989 *
990 * [ABORT back to where the offender came from.]
991 */
992 if (chunk)
993 retval->transport = chunk->transport;
994
995no_mem:
996 return retval;
997}
998
999/* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error. */
1000struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001001 const struct msghdr *msg,
1002 size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
1004 struct sctp_chunk *retval;
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001005 void *payload = NULL;
1006 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001008 retval = sctp_make_abort(asoc, NULL, sizeof(sctp_errhdr_t) + paylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (!retval)
1010 goto err_chunk;
1011
1012 if (paylen) {
1013 /* Put the msg_iov together into payload. */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001014 payload = kmalloc(paylen, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (!payload)
1016 goto err_payload;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001018 err = memcpy_fromiovec(payload, msg->msg_iov, paylen);
1019 if (err < 0)
1020 goto err_copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
1022
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001023 sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, paylen);
1024 sctp_addto_chunk(retval, paylen, payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 if (paylen)
1027 kfree(payload);
1028
1029 return retval;
1030
1031err_copy:
1032 kfree(payload);
1033err_payload:
1034 sctp_chunk_free(retval);
1035 retval = NULL;
1036err_chunk:
1037 return retval;
1038}
1039
Adrian Bunk5c94bf82007-09-12 15:16:21 +02001040/* Append bytes to the end of a parameter. Will panic if chunk is not big
1041 * enough.
1042 */
1043static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
1044 const void *data)
1045{
1046 void *target;
1047 int chunklen = ntohs(chunk->chunk_hdr->length);
1048
1049 target = skb_put(chunk->skb, len);
1050
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001051 if (data)
1052 memcpy(target, data, len);
1053 else
1054 memset(target, 0, len);
Adrian Bunk5c94bf82007-09-12 15:16:21 +02001055
1056 /* Adjust the chunk length field. */
1057 chunk->chunk_hdr->length = htons(chunklen + len);
1058 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1059
1060 return target;
1061}
1062
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001063/* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064struct sctp_chunk *sctp_make_abort_violation(
1065 const struct sctp_association *asoc,
1066 const struct sctp_chunk *chunk,
1067 const __u8 *payload,
1068 const size_t paylen)
1069{
1070 struct sctp_chunk *retval;
1071 struct sctp_paramhdr phdr;
1072
1073 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t) + paylen
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001074 + sizeof(sctp_paramhdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (!retval)
1076 goto end;
1077
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001078 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, paylen
1079 + sizeof(sctp_paramhdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 phdr.type = htons(chunk->chunk_hdr->type);
1082 phdr.length = chunk->chunk_hdr->length;
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001083 sctp_addto_chunk(retval, paylen, payload);
1084 sctp_addto_param(retval, sizeof(sctp_paramhdr_t), &phdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086end:
1087 return retval;
1088}
1089
Wei Yongjunba016672008-09-30 05:32:24 -07001090struct sctp_chunk *sctp_make_violation_paramlen(
1091 const struct sctp_association *asoc,
1092 const struct sctp_chunk *chunk,
1093 struct sctp_paramhdr *param)
1094{
1095 struct sctp_chunk *retval;
1096 static const char error[] = "The following parameter had invalid length:";
1097 size_t payload_len = sizeof(error) + sizeof(sctp_errhdr_t) +
1098 sizeof(sctp_paramhdr_t);
1099
1100 retval = sctp_make_abort(asoc, chunk, payload_len);
1101 if (!retval)
1102 goto nodata;
1103
1104 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION,
1105 sizeof(error) + sizeof(sctp_paramhdr_t));
1106 sctp_addto_chunk(retval, sizeof(error), error);
1107 sctp_addto_param(retval, sizeof(sctp_paramhdr_t), param);
1108
1109nodata:
1110 return retval;
1111}
1112
Neil Hormande4594a2012-11-20 10:14:30 +00001113struct sctp_chunk *sctp_make_violation_max_retrans(
1114 const struct sctp_association *asoc,
1115 const struct sctp_chunk *chunk)
1116{
1117 struct sctp_chunk *retval;
1118 static const char error[] = "Association exceeded its max_retans count";
1119 size_t payload_len = sizeof(error) + sizeof(sctp_errhdr_t);
1120
1121 retval = sctp_make_abort(asoc, chunk, payload_len);
1122 if (!retval)
1123 goto nodata;
1124
1125 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, sizeof(error));
1126 sctp_addto_chunk(retval, sizeof(error), error);
1127
1128nodata:
1129 return retval;
1130}
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132/* Make a HEARTBEAT chunk. */
1133struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
Wei Yongjun92c73af2011-04-19 21:31:47 +00001134 const struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Wei Yongjun92c73af2011-04-19 21:31:47 +00001136 struct sctp_chunk *retval;
1137 sctp_sender_hb_info_t hbinfo;
1138
Vlad Yasevich072017b2013-08-09 22:05:36 -04001139 retval = sctp_make_control(asoc, SCTP_CID_HEARTBEAT, 0, sizeof(hbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 if (!retval)
1142 goto nodata;
1143
Wei Yongjun92c73af2011-04-19 21:31:47 +00001144 hbinfo.param_hdr.type = SCTP_PARAM_HEARTBEAT_INFO;
1145 hbinfo.param_hdr.length = htons(sizeof(sctp_sender_hb_info_t));
1146 hbinfo.daddr = transport->ipaddr;
1147 hbinfo.sent_at = jiffies;
1148 hbinfo.hb_nonce = transport->hb_nonce;
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 /* Cast away the 'const', as this is just telling the chunk
1151 * what transport it belongs to.
1152 */
1153 retval->transport = (struct sctp_transport *) transport;
Wei Yongjun92c73af2011-04-19 21:31:47 +00001154 retval->subh.hbs_hdr = sctp_addto_chunk(retval, sizeof(hbinfo),
1155 &hbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157nodata:
1158 return retval;
1159}
1160
1161struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
1162 const struct sctp_chunk *chunk,
1163 const void *payload, const size_t paylen)
1164{
1165 struct sctp_chunk *retval;
1166
Vlad Yasevich072017b2013-08-09 22:05:36 -04001167 retval = sctp_make_control(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (!retval)
1169 goto nodata;
1170
1171 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
1172
1173 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1174 *
1175 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1176 * HEARTBEAT ACK, * etc.) to the same destination transport
1177 * address from which it * received the DATA or control chunk
1178 * to which it is replying.
1179 *
1180 * [HBACK back to where the HEARTBEAT came from.]
1181 */
1182 if (chunk)
1183 retval->transport = chunk->transport;
1184
1185nodata:
1186 return retval;
1187}
1188
1189/* Create an Operation Error chunk with the specified space reserved.
1190 * This routine can be used for containing multiple causes in the chunk.
1191 */
1192static struct sctp_chunk *sctp_make_op_error_space(
1193 const struct sctp_association *asoc,
1194 const struct sctp_chunk *chunk,
1195 size_t size)
1196{
1197 struct sctp_chunk *retval;
1198
Vlad Yasevich072017b2013-08-09 22:05:36 -04001199 retval = sctp_make_control(asoc, SCTP_CID_ERROR, 0,
1200 sizeof(sctp_errhdr_t) + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (!retval)
1202 goto nodata;
1203
1204 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1205 *
1206 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1207 * HEARTBEAT ACK, etc.) to the same destination transport
1208 * address from which it received the DATA or control chunk
1209 * to which it is replying.
1210 *
1211 */
1212 if (chunk)
1213 retval->transport = chunk->transport;
1214
1215nodata:
1216 return retval;
1217}
1218
Neil Horman5fa782c2010-04-28 10:30:59 +00001219/* Create an Operation Error chunk of a fixed size,
1220 * specifically, max(asoc->pathmtu, SCTP_DEFAULT_MAXSEGMENT)
1221 * This is a helper function to allocate an error chunk for
1222 * for those invalid parameter codes in which we may not want
Jorrit Schippersd82603c2012-12-27 17:33:02 +01001223 * to report all the errors, if the incoming chunk is large
Neil Horman5fa782c2010-04-28 10:30:59 +00001224 */
1225static inline struct sctp_chunk *sctp_make_op_error_fixed(
1226 const struct sctp_association *asoc,
1227 const struct sctp_chunk *chunk)
1228{
1229 size_t size = asoc ? asoc->pathmtu : 0;
1230
1231 if (!size)
1232 size = SCTP_DEFAULT_MAXSEGMENT;
1233
1234 return sctp_make_op_error_space(asoc, chunk, size);
1235}
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237/* Create an Operation Error chunk. */
1238struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
1239 const struct sctp_chunk *chunk,
Al Viro63706c52006-11-20 17:00:05 -08001240 __be16 cause_code, const void *payload,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001241 size_t paylen, size_t reserve_tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
1243 struct sctp_chunk *retval;
1244
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001245 retval = sctp_make_op_error_space(asoc, chunk, paylen + reserve_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (!retval)
1247 goto nodata;
1248
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001249 sctp_init_cause(retval, cause_code, paylen + reserve_tail);
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001250 sctp_addto_chunk(retval, paylen, payload);
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001251 if (reserve_tail)
1252 sctp_addto_param(retval, reserve_tail, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254nodata:
1255 return retval;
1256}
1257
Vlad Yasevich4cd57c82007-09-16 19:32:45 -07001258struct sctp_chunk *sctp_make_auth(const struct sctp_association *asoc)
1259{
1260 struct sctp_chunk *retval;
1261 struct sctp_hmac *hmac_desc;
1262 struct sctp_authhdr auth_hdr;
1263 __u8 *hmac;
1264
1265 /* Get the first hmac that the peer told us to use */
1266 hmac_desc = sctp_auth_asoc_get_hmac(asoc);
1267 if (unlikely(!hmac_desc))
1268 return NULL;
1269
Vlad Yasevich072017b2013-08-09 22:05:36 -04001270 retval = sctp_make_control(asoc, SCTP_CID_AUTH, 0,
Vlad Yasevich4cd57c82007-09-16 19:32:45 -07001271 hmac_desc->hmac_len + sizeof(sctp_authhdr_t));
1272 if (!retval)
1273 return NULL;
1274
1275 auth_hdr.hmac_id = htons(hmac_desc->hmac_id);
1276 auth_hdr.shkey_id = htons(asoc->active_key_id);
1277
1278 retval->subh.auth_hdr = sctp_addto_chunk(retval, sizeof(sctp_authhdr_t),
1279 &auth_hdr);
1280
1281 hmac = skb_put(retval->skb, hmac_desc->hmac_len);
1282 memset(hmac, 0, hmac_desc->hmac_len);
1283
1284 /* Adjust the chunk header to include the empty MAC */
1285 retval->chunk_hdr->length =
1286 htons(ntohs(retval->chunk_hdr->length) + hmac_desc->hmac_len);
1287 retval->chunk_end = skb_tail_pointer(retval->skb);
1288
1289 return retval;
1290}
1291
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293/********************************************************************
1294 * 2nd Level Abstractions
1295 ********************************************************************/
1296
1297/* Turn an skb into a chunk.
1298 * FIXME: Eventually move the structure directly inside the skb->cb[].
wangweidong3dc0a542013-10-26 16:06:31 +08001299 *
1300 * sctpimpguide-05.txt Section 2.8.2
1301 * M1) Each time a new DATA chunk is transmitted
1302 * set the 'TSN.Missing.Report' count for that TSN to 0. The
1303 * 'TSN.Missing.Report' count will be used to determine missing chunks
1304 * and when to fast retransmit.
1305 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 */
1307struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
1308 const struct sctp_association *asoc,
1309 struct sock *sk)
1310{
1311 struct sctp_chunk *retval;
1312
Robert P. J. Dayc3762222007-02-10 01:45:03 -08001313 retval = kmem_cache_zalloc(sctp_chunk_cachep, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 if (!retval)
1316 goto nodata;
Daniel Borkmannbb333812013-06-28 19:49:40 +02001317 if (!sk)
1318 pr_debug("%s: chunkifying skb:%p w/o an sk\n", __func__, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
David S. Miller79af02c2005-07-08 21:47:49 -07001320 INIT_LIST_HEAD(&retval->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 retval->skb = skb;
1322 retval->asoc = (struct sctp_association *)asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 retval->singleton = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Neil Hormanc226ef92008-07-25 12:44:09 -04001325 retval->fast_retransmit = SCTP_CAN_FRTX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 /* Polish the bead hole. */
1328 INIT_LIST_HEAD(&retval->transmitted_list);
1329 INIT_LIST_HEAD(&retval->frag_list);
1330 SCTP_DBG_OBJCNT_INC(chunk);
1331 atomic_set(&retval->refcnt, 1);
1332
1333nodata:
1334 return retval;
1335}
1336
1337/* Set chunk->source and dest based on the IP header in chunk->skb. */
1338void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
1339 union sctp_addr *dest)
1340{
Al Virof235fca2006-11-20 17:09:01 -08001341 memcpy(&chunk->source, src, sizeof(union sctp_addr));
Al Viro16b0a032006-11-20 17:13:38 -08001342 memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
1345/* Extract the source address from a chunk. */
1346const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
1347{
1348 /* If we have a known transport, use that. */
1349 if (chunk->transport) {
Al Viro6a1e5f32006-11-20 17:12:25 -08001350 return &chunk->transport->ipaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 } else {
1352 /* Otherwise, extract it from the IP header. */
Al Viro6a1e5f32006-11-20 17:12:25 -08001353 return &chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355}
1356
1357/* Create a new chunk, setting the type and flags headers from the
1358 * arguments, reserving enough space for a 'paylen' byte payload.
1359 */
Vlad Yasevich072017b2013-08-09 22:05:36 -04001360static struct sctp_chunk *_sctp_make_chunk(const struct sctp_association *asoc,
1361 __u8 type, __u8 flags, int paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
1363 struct sctp_chunk *retval;
1364 sctp_chunkhdr_t *chunk_hdr;
1365 struct sk_buff *skb;
1366 struct sock *sk;
1367
1368 /* No need to allocate LL here, as this is only a chunk. */
1369 skb = alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t) + paylen),
1370 GFP_ATOMIC);
1371 if (!skb)
1372 goto nodata;
1373
1374 /* Make room for the chunk header. */
1375 chunk_hdr = (sctp_chunkhdr_t *)skb_put(skb, sizeof(sctp_chunkhdr_t));
1376 chunk_hdr->type = type;
1377 chunk_hdr->flags = flags;
1378 chunk_hdr->length = htons(sizeof(sctp_chunkhdr_t));
1379
1380 sk = asoc ? asoc->base.sk : NULL;
1381 retval = sctp_chunkify(skb, asoc, sk);
1382 if (!retval) {
1383 kfree_skb(skb);
1384 goto nodata;
1385 }
1386
1387 retval->chunk_hdr = chunk_hdr;
1388 retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(struct sctp_chunkhdr);
1389
Vlad Yasevich4cd57c82007-09-16 19:32:45 -07001390 /* Determine if the chunk needs to be authenticated */
1391 if (sctp_auth_send_cid(type, asoc))
1392 retval->auth = 1;
1393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return retval;
1395nodata:
1396 return NULL;
1397}
1398
Vlad Yasevich072017b2013-08-09 22:05:36 -04001399static struct sctp_chunk *sctp_make_data(const struct sctp_association *asoc,
1400 __u8 flags, int paylen)
1401{
1402 return _sctp_make_chunk(asoc, SCTP_CID_DATA, flags, paylen);
1403}
1404
1405static struct sctp_chunk *sctp_make_control(const struct sctp_association *asoc,
1406 __u8 type, __u8 flags, int paylen)
1407{
1408 struct sctp_chunk *chunk = _sctp_make_chunk(asoc, type, flags, paylen);
1409
1410 if (chunk)
1411 sctp_control_set_owner_w(chunk);
1412
1413 return chunk;
1414}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416/* Release the memory occupied by a chunk. */
1417static void sctp_chunk_destroy(struct sctp_chunk *chunk)
1418{
Vlad Yasevicha08de642007-12-20 14:11:47 -08001419 BUG_ON(!list_empty(&chunk->list));
1420 list_del_init(&chunk->transmitted_list);
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 /* Free the chunk skb data and the SCTP_chunk stub itself. */
1423 dev_kfree_skb(chunk->skb);
1424
1425 SCTP_DBG_OBJCNT_DEC(chunk);
1426 kmem_cache_free(sctp_chunk_cachep, chunk);
1427}
1428
1429/* Possibly, free the chunk. */
1430void sctp_chunk_free(struct sctp_chunk *chunk)
1431{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 /* Release our reference on the message tracker. */
1433 if (chunk->msg)
1434 sctp_datamsg_put(chunk->msg);
1435
1436 sctp_chunk_put(chunk);
1437}
1438
1439/* Grab a reference to the chunk. */
1440void sctp_chunk_hold(struct sctp_chunk *ch)
1441{
1442 atomic_inc(&ch->refcnt);
1443}
1444
1445/* Release a reference to the chunk. */
1446void sctp_chunk_put(struct sctp_chunk *ch)
1447{
1448 if (atomic_dec_and_test(&ch->refcnt))
1449 sctp_chunk_destroy(ch);
1450}
1451
1452/* Append bytes to the end of a chunk. Will panic if chunk is not big
1453 * enough.
1454 */
1455void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
1456{
1457 void *target;
1458 void *padding;
1459 int chunklen = ntohs(chunk->chunk_hdr->length);
Wei Yongjun8d614ad2007-08-06 13:55:47 +08001460 int padlen = WORD_ROUND(chunklen) - chunklen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 padding = skb_put(chunk->skb, padlen);
1463 target = skb_put(chunk->skb, len);
1464
1465 memset(padding, 0, padlen);
1466 memcpy(target, data, len);
1467
1468 /* Adjust the chunk length field. */
1469 chunk->chunk_hdr->length = htons(chunklen + padlen + len);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001470 chunk->chunk_end = skb_tail_pointer(chunk->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 return target;
1473}
1474
Neil Horman5fa782c2010-04-28 10:30:59 +00001475/* Append bytes to the end of a chunk. Returns NULL if there isn't sufficient
1476 * space in the chunk
1477 */
1478void *sctp_addto_chunk_fixed(struct sctp_chunk *chunk,
1479 int len, const void *data)
1480{
Wei Yongjun2e3219b2010-05-17 22:51:58 -07001481 if (skb_tailroom(chunk->skb) >= len)
Neil Horman5fa782c2010-04-28 10:30:59 +00001482 return sctp_addto_chunk(chunk, len, data);
1483 else
1484 return NULL;
1485}
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487/* Append bytes from user space to the end of a chunk. Will panic if
1488 * chunk is not big enough.
1489 * Returns a kernel err value.
1490 */
1491int sctp_user_addto_chunk(struct sctp_chunk *chunk, int off, int len,
1492 struct iovec *data)
1493{
1494 __u8 *target;
1495 int err = 0;
1496
1497 /* Make room in chunk for data. */
1498 target = skb_put(chunk->skb, len);
1499
1500 /* Copy data (whole iovec) into chunk */
1501 if ((err = memcpy_fromiovecend(target, data, off, len)))
1502 goto out;
1503
1504 /* Adjust the chunk length field. */
1505 chunk->chunk_hdr->length =
1506 htons(ntohs(chunk->chunk_hdr->length) + len);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001507 chunk->chunk_end = skb_tail_pointer(chunk->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509out:
1510 return err;
1511}
1512
1513/* Helper function to assign a TSN if needed. This assumes that both
1514 * the data_hdr and association have already been assigned.
1515 */
1516void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
1517{
Vlad Yasevichab3e5e72007-08-02 16:51:42 -04001518 struct sctp_datamsg *msg;
1519 struct sctp_chunk *lchunk;
1520 struct sctp_stream *stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 __u16 ssn;
1522 __u16 sid;
1523
1524 if (chunk->has_ssn)
1525 return;
1526
Vlad Yasevichab3e5e72007-08-02 16:51:42 -04001527 /* All fragments will be on the same stream */
1528 sid = ntohs(chunk->subh.data_hdr->stream);
1529 stream = &chunk->asoc->ssnmap->out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Vlad Yasevichab3e5e72007-08-02 16:51:42 -04001531 /* Now assign the sequence number to the entire message.
1532 * All fragments must have the same stream sequence number.
1533 */
1534 msg = chunk->msg;
1535 list_for_each_entry(lchunk, &msg->chunks, frag_list) {
1536 if (lchunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
1537 ssn = 0;
1538 } else {
1539 if (lchunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
1540 ssn = sctp_ssn_next(stream, sid);
1541 else
1542 ssn = sctp_ssn_peek(stream, sid);
1543 }
1544
1545 lchunk->subh.data_hdr->ssn = htons(ssn);
1546 lchunk->has_ssn = 1;
1547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
1550/* Helper function to assign a TSN if needed. This assumes that both
1551 * the data_hdr and association have already been assigned.
1552 */
1553void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
1554{
1555 if (!chunk->has_tsn) {
1556 /* This is the last possible instant to
1557 * assign a TSN.
1558 */
1559 chunk->subh.data_hdr->tsn =
1560 htonl(sctp_association_get_next_tsn(chunk->asoc));
1561 chunk->has_tsn = 1;
1562 }
1563}
1564
1565/* Create a CLOSED association to use with an incoming packet. */
1566struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
Alexey Dobriyan3182cd82005-07-11 20:57:47 -07001567 struct sctp_chunk *chunk,
Al Virodd0fc662005-10-07 07:46:04 +01001568 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
1570 struct sctp_association *asoc;
1571 struct sk_buff *skb;
1572 sctp_scope_t scope;
1573 struct sctp_af *af;
1574
1575 /* Create the bare association. */
1576 scope = sctp_scope(sctp_source(chunk));
1577 asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
1578 if (!asoc)
1579 goto nodata;
1580 asoc->temp = 1;
1581 skb = chunk->skb;
1582 /* Create an entry for the source address of the packet. */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001583 af = sctp_get_af_specific(ipver2af(ip_hdr(skb)->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 if (unlikely(!af))
1585 goto fail;
Al Virod55c41b2006-11-20 17:09:40 -08001586 af->from_skb(&asoc->c.peer_addr, skb, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587nodata:
1588 return asoc;
1589
1590fail:
1591 sctp_association_free(asoc);
1592 return NULL;
1593}
1594
1595/* Build a cookie representing asoc.
1596 * This INCLUDES the param header needed to put the cookie in the INIT ACK.
1597 */
1598static sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
1599 const struct sctp_association *asoc,
1600 const struct sctp_chunk *init_chunk,
1601 int *cookie_len,
1602 const __u8 *raw_addrs, int addrs_len)
1603{
1604 sctp_cookie_param_t *retval;
1605 struct sctp_signed_cookie *cookie;
1606 struct scatterlist sg;
1607 int headersize, bodysize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Vlad Yasevich9834a2b2006-01-17 11:52:12 -08001609 /* Header size is static data prior to the actual cookie, including
1610 * any padding.
1611 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001612 headersize = sizeof(sctp_paramhdr_t) +
1613 (sizeof(struct sctp_signed_cookie) -
Vlad Yasevich9834a2b2006-01-17 11:52:12 -08001614 sizeof(struct sctp_cookie));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 bodysize = sizeof(struct sctp_cookie)
1616 + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
1617
1618 /* Pad out the cookie to a multiple to make the signature
1619 * functions simpler to write.
1620 */
1621 if (bodysize % SCTP_COOKIE_MULTIPLE)
1622 bodysize += SCTP_COOKIE_MULTIPLE
1623 - (bodysize % SCTP_COOKIE_MULTIPLE);
1624 *cookie_len = headersize + bodysize;
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 /* Clear this memory since we are sending this data structure
1627 * out on the network.
1628 */
Arnaldo Carvalho de Meloaf997d82006-11-21 01:20:33 -02001629 retval = kzalloc(*cookie_len, GFP_ATOMIC);
1630 if (!retval)
1631 goto nodata;
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 cookie = (struct sctp_signed_cookie *) retval->body;
1634
1635 /* Set up the parameter header. */
1636 retval->p.type = SCTP_PARAM_STATE_COOKIE;
1637 retval->p.length = htons(*cookie_len);
1638
1639 /* Copy the cookie part of the association itself. */
1640 cookie->c = asoc->c;
1641 /* Save the raw address list length in the cookie. */
1642 cookie->c.raw_addr_list_len = addrs_len;
1643
1644 /* Remember PR-SCTP capability. */
1645 cookie->c.prsctp_capable = asoc->peer.prsctp_capable;
1646
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001647 /* Save adaptation indication in the cookie. */
1648 cookie->c.adaptation_ind = asoc->peer.adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 /* Set an expiration time for the cookie. */
Daniel Borkmann52db8822013-06-25 18:17:27 +02001651 cookie->c.expiration = ktime_add(asoc->cookie_life,
1652 ktime_get());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
1654 /* Copy the peer's init packet. */
1655 memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
1656 ntohs(init_chunk->chunk_hdr->length));
1657
1658 /* Copy the raw local address list of the association. */
1659 memcpy((__u8 *)&cookie->c.peer_init[0] +
1660 ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
1661
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001662 if (sctp_sk(ep->base.sk)->hmac) {
Herbert Xu1b489e12006-08-20 15:07:14 +10001663 struct hash_desc desc;
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 /* Sign the message. */
Herbert Xu68e3f5d2007-10-27 00:52:07 -07001666 sg_init_one(&sg, &cookie->c, bodysize);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001667 desc.tfm = sctp_sk(ep->base.sk)->hmac;
1668 desc.flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Daniel Borkmann570617e2013-02-12 05:15:33 +00001670 if (crypto_hash_setkey(desc.tfm, ep->secret_key,
1671 sizeof(ep->secret_key)) ||
Herbert Xu1b489e12006-08-20 15:07:14 +10001672 crypto_hash_digest(&desc, &sg, bodysize, cookie->signature))
1673 goto free_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 }
1675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 return retval;
Herbert Xu1b489e12006-08-20 15:07:14 +10001677
1678free_cookie:
1679 kfree(retval);
1680nodata:
1681 *cookie_len = 0;
1682 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683}
1684
1685/* Unpack the cookie from COOKIE ECHO chunk, recreating the association. */
1686struct sctp_association *sctp_unpack_cookie(
1687 const struct sctp_endpoint *ep,
1688 const struct sctp_association *asoc,
Al Virodd0fc662005-10-07 07:46:04 +01001689 struct sctp_chunk *chunk, gfp_t gfp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 int *error, struct sctp_chunk **errp)
1691{
1692 struct sctp_association *retval = NULL;
1693 struct sctp_signed_cookie *cookie;
1694 struct sctp_cookie *bear_cookie;
1695 int headersize, bodysize, fixed_size;
Vlad Yasevich313e7b42006-01-17 11:55:57 -08001696 __u8 *digest = ep->digest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 struct scatterlist sg;
Daniel Borkmann570617e2013-02-12 05:15:33 +00001698 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 sctp_scope_t scope;
1700 struct sk_buff *skb = chunk->skb;
Daniel Borkmann52db8822013-06-25 18:17:27 +02001701 ktime_t kt;
Herbert Xu1b489e12006-08-20 15:07:14 +10001702 struct hash_desc desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Vlad Yasevich9834a2b2006-01-17 11:52:12 -08001704 /* Header size is static data prior to the actual cookie, including
1705 * any padding.
1706 */
1707 headersize = sizeof(sctp_chunkhdr_t) +
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001708 (sizeof(struct sctp_signed_cookie) -
Vlad Yasevich9834a2b2006-01-17 11:52:12 -08001709 sizeof(struct sctp_cookie));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
1711 fixed_size = headersize + sizeof(struct sctp_cookie);
1712
1713 /* Verify that the chunk looks like it even has a cookie.
1714 * There must be enough room for our cookie and our peer's
1715 * INIT chunk.
1716 */
1717 len = ntohs(chunk->chunk_hdr->length);
1718 if (len < fixed_size + sizeof(struct sctp_chunkhdr))
1719 goto malformed;
1720
1721 /* Verify that the cookie has been padded out. */
1722 if (bodysize % SCTP_COOKIE_MULTIPLE)
1723 goto malformed;
1724
1725 /* Process the cookie. */
1726 cookie = chunk->subh.cookie_hdr;
1727 bear_cookie = &cookie->c;
1728
1729 if (!sctp_sk(ep->base.sk)->hmac)
1730 goto no_hmac;
1731
1732 /* Check the signature. */
Herbert Xu68e3f5d2007-10-27 00:52:07 -07001733 sg_init_one(&sg, bear_cookie, bodysize);
Herbert Xu1b489e12006-08-20 15:07:14 +10001734 desc.tfm = sctp_sk(ep->base.sk)->hmac;
1735 desc.flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Al Viro8ca84482006-06-20 03:26:14 -07001737 memset(digest, 0x00, SCTP_SIGNATURE_SIZE);
Daniel Borkmann570617e2013-02-12 05:15:33 +00001738 if (crypto_hash_setkey(desc.tfm, ep->secret_key,
1739 sizeof(ep->secret_key)) ||
Herbert Xu1b489e12006-08-20 15:07:14 +10001740 crypto_hash_digest(&desc, &sg, bodysize, digest)) {
1741 *error = -SCTP_IERROR_NOMEM;
1742 goto fail;
1743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
Daniel Borkmann570617e2013-02-12 05:15:33 +00001746 *error = -SCTP_IERROR_BAD_SIG;
1747 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
1749
1750no_hmac:
1751 /* IG Section 2.35.2:
1752 * 3) Compare the port numbers and the verification tag contained
1753 * within the COOKIE ECHO chunk to the actual port numbers and the
1754 * verification tag within the SCTP common header of the received
1755 * packet. If these values do not match the packet MUST be silently
1756 * discarded,
1757 */
1758 if (ntohl(chunk->sctp_hdr->vtag) != bear_cookie->my_vtag) {
1759 *error = -SCTP_IERROR_BAD_TAG;
1760 goto fail;
1761 }
1762
Al Viro9b1dfad2006-11-20 17:09:17 -08001763 if (chunk->sctp_hdr->source != bear_cookie->peer_addr.v4.sin_port ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 ntohs(chunk->sctp_hdr->dest) != bear_cookie->my_port) {
1765 *error = -SCTP_IERROR_BAD_PORTS;
1766 goto fail;
1767 }
1768
1769 /* Check to see if the cookie is stale. If there is already
1770 * an association, there is no need to check cookie's expiration
1771 * for init collision case of lost COOKIE ACK.
Vlad Yasevichf236218b2006-09-29 17:10:03 -07001772 * If skb has been timestamped, then use the stamp, otherwise
1773 * use current time. This introduces a small possibility that
1774 * that a cookie may be considered expired, but his would only slow
1775 * down the new association establishment instead of every packet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 */
Vlad Yasevichf236218b2006-09-29 17:10:03 -07001777 if (sock_flag(ep->base.sk, SOCK_TIMESTAMP))
Daniel Borkmann52db8822013-06-25 18:17:27 +02001778 kt = skb_get_ktime(skb);
Vlad Yasevichf236218b2006-09-29 17:10:03 -07001779 else
Daniel Borkmann52db8822013-06-25 18:17:27 +02001780 kt = ktime_get();
Vlad Yasevichf236218b2006-09-29 17:10:03 -07001781
Daniel Borkmann52db8822013-06-25 18:17:27 +02001782 if (!asoc && ktime_compare(bear_cookie->expiration, kt) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 /*
1784 * Section 3.3.10.3 Stale Cookie Error (3)
1785 *
1786 * Cause of error
1787 * ---------------
1788 * Stale Cookie Error: Indicates the receipt of a valid State
1789 * Cookie that has expired.
1790 */
1791 len = ntohs(chunk->chunk_hdr->length);
1792 *errp = sctp_make_op_error_space(asoc, chunk, len);
1793 if (*errp) {
Daniel Borkmann52db8822013-06-25 18:17:27 +02001794 suseconds_t usecs = ktime_to_us(ktime_sub(kt, bear_cookie->expiration));
Al Viro34bcca22006-11-20 17:27:15 -08001795 __be32 n = htonl(usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001798 sizeof(n));
1799 sctp_addto_chunk(*errp, sizeof(n), &n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 *error = -SCTP_IERROR_STALE_COOKIE;
1801 } else
1802 *error = -SCTP_IERROR_NOMEM;
1803
1804 goto fail;
1805 }
1806
1807 /* Make a new base association. */
1808 scope = sctp_scope(sctp_source(chunk));
1809 retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
1810 if (!retval) {
1811 *error = -SCTP_IERROR_NOMEM;
1812 goto fail;
1813 }
1814
1815 /* Set up our peer's port number. */
1816 retval->peer.port = ntohs(chunk->sctp_hdr->source);
1817
1818 /* Populate the association from the cookie. */
1819 memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
1820
1821 if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
1822 GFP_ATOMIC) < 0) {
1823 *error = -SCTP_IERROR_NOMEM;
1824 goto fail;
1825 }
1826
1827 /* Also, add the destination address. */
1828 if (list_empty(&retval->base.bind_addr.address_list)) {
Vlad Yasevichf57d96b2007-12-20 14:12:24 -08001829 sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest,
1830 SCTP_ADDR_SRC, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 }
1832
1833 retval->next_tsn = retval->c.initial_tsn;
1834 retval->ctsn_ack_point = retval->next_tsn - 1;
1835 retval->addip_serial = retval->c.initial_tsn;
1836 retval->adv_peer_ack_point = retval->ctsn_ack_point;
1837 retval->peer.prsctp_capable = retval->c.prsctp_capable;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001838 retval->peer.adaptation_ind = retval->c.adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 /* The INIT stuff will be done by the side effects. */
1841 return retval;
1842
1843fail:
1844 if (retval)
1845 sctp_association_free(retval);
1846
1847 return NULL;
1848
1849malformed:
1850 /* Yikes! The packet is either corrupt or deliberately
1851 * malformed.
1852 */
1853 *error = -SCTP_IERROR_MALFORMED;
1854 goto fail;
1855}
1856
1857/********************************************************************
1858 * 3rd Level Abstractions
1859 ********************************************************************/
1860
1861struct __sctp_missing {
Al Viro9f81bcd2006-11-20 17:26:34 -08001862 __be32 num_missing;
1863 __be16 type;
Eric Dumazetbc105022010-06-03 03:21:52 -07001864} __packed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
1866/*
1867 * Report a missing mandatory parameter.
1868 */
1869static int sctp_process_missing_param(const struct sctp_association *asoc,
1870 sctp_param_t paramtype,
1871 struct sctp_chunk *chunk,
1872 struct sctp_chunk **errp)
1873{
1874 struct __sctp_missing report;
1875 __u16 len;
1876
1877 len = WORD_ROUND(sizeof(report));
1878
1879 /* Make an ERROR chunk, preparing enough room for
1880 * returning multiple unknown parameters.
1881 */
1882 if (!*errp)
1883 *errp = sctp_make_op_error_space(asoc, chunk, len);
1884
1885 if (*errp) {
1886 report.num_missing = htonl(1);
1887 report.type = paramtype;
Vlad Yasevichebdfcad2007-01-15 19:12:31 -08001888 sctp_init_cause(*errp, SCTP_ERROR_MISS_PARAM,
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001889 sizeof(report));
1890 sctp_addto_chunk(*errp, sizeof(report), &report);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 }
1892
1893 /* Stop processing this chunk. */
1894 return 0;
1895}
1896
1897/* Report an Invalid Mandatory Parameter. */
1898static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
1899 struct sctp_chunk *chunk,
1900 struct sctp_chunk **errp)
1901{
1902 /* Invalid Mandatory Parameter Error has no payload. */
1903
1904 if (!*errp)
1905 *errp = sctp_make_op_error_space(asoc, chunk, 0);
1906
1907 if (*errp)
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001908 sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 /* Stop processing this chunk. */
1911 return 0;
1912}
1913
1914static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
1915 struct sctp_paramhdr *param,
1916 const struct sctp_chunk *chunk,
1917 struct sctp_chunk **errp)
1918{
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001919 /* This is a fatal error. Any accumulated non-fatal errors are
1920 * not reported.
1921 */
1922 if (*errp)
1923 sctp_chunk_free(*errp);
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 /* Create an error chunk and fill it in with our payload. */
Wei Yongjunba016672008-09-30 05:32:24 -07001926 *errp = sctp_make_violation_paramlen(asoc, chunk, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
1928 return 0;
1929}
1930
1931
1932/* Do not attempt to handle the HOST_NAME parm. However, do
1933 * send back an indicator to the peer.
1934 */
1935static int sctp_process_hn_param(const struct sctp_association *asoc,
1936 union sctp_params param,
1937 struct sctp_chunk *chunk,
1938 struct sctp_chunk **errp)
1939{
1940 __u16 len = ntohs(param.p->length);
1941
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001942 /* Processing of the HOST_NAME parameter will generate an
1943 * ABORT. If we've accumulated any non-fatal errors, they
1944 * would be unrecognized parameters and we should not include
1945 * them in the ABORT.
1946 */
1947 if (*errp)
1948 sctp_chunk_free(*errp);
1949
1950 *errp = sctp_make_op_error_space(asoc, chunk, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001952 if (*errp) {
1953 sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED, len);
1954 sctp_addto_chunk(*errp, len, param.v);
1955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
1957 /* Stop processing this chunk. */
1958 return 0;
1959}
1960
Eric W. Biedermanf53b5b02012-08-07 07:29:08 +00001961static int sctp_verify_ext_param(struct net *net, union sctp_params param)
Vlad Yasevichd6701192007-12-20 14:13:31 -08001962{
1963 __u16 num_ext = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
1964 int have_auth = 0;
1965 int have_asconf = 0;
1966 int i;
1967
1968 for (i = 0; i < num_ext; i++) {
1969 switch (param.ext->chunks[i]) {
wangweidongf7010e62013-12-23 12:16:52 +08001970 case SCTP_CID_AUTH:
1971 have_auth = 1;
1972 break;
1973 case SCTP_CID_ASCONF:
1974 case SCTP_CID_ASCONF_ACK:
1975 have_asconf = 1;
1976 break;
Vlad Yasevichd6701192007-12-20 14:13:31 -08001977 }
1978 }
1979
1980 /* ADD-IP Security: The draft requires us to ABORT or ignore the
1981 * INIT/INIT-ACK if ADD-IP is listed, but AUTH is not. Do this
1982 * only if ADD-IP is turned on and we are not backward-compatible
1983 * mode.
1984 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00001985 if (net->sctp.addip_noauth)
Vlad Yasevichd6701192007-12-20 14:13:31 -08001986 return 1;
1987
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00001988 if (net->sctp.addip_enable && !have_auth && have_asconf)
Vlad Yasevichd6701192007-12-20 14:13:31 -08001989 return 0;
1990
1991 return 1;
1992}
1993
Vlad Yasevich131a47e2007-09-16 15:53:56 -07001994static void sctp_process_ext_param(struct sctp_association *asoc,
1995 union sctp_params param)
1996{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00001997 struct net *net = sock_net(asoc->base.sk);
Vlad Yasevich131a47e2007-09-16 15:53:56 -07001998 __u16 num_ext = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
1999 int i;
2000
2001 for (i = 0; i < num_ext; i++) {
2002 switch (param.ext->chunks[i]) {
wangweidongf7010e62013-12-23 12:16:52 +08002003 case SCTP_CID_FWD_TSN:
2004 if (net->sctp.prsctp_enable && !asoc->peer.prsctp_capable)
Vlad Yasevich131a47e2007-09-16 15:53:56 -07002005 asoc->peer.prsctp_capable = 1;
wangweidongf7010e62013-12-23 12:16:52 +08002006 break;
2007 case SCTP_CID_AUTH:
2008 /* if the peer reports AUTH, assume that he
2009 * supports AUTH.
2010 */
2011 if (net->sctp.auth_enable)
2012 asoc->peer.auth_capable = 1;
2013 break;
2014 case SCTP_CID_ASCONF:
2015 case SCTP_CID_ASCONF_ACK:
2016 if (net->sctp.addip_enable)
2017 asoc->peer.asconf_capable = 1;
2018 break;
2019 default:
2020 break;
Vlad Yasevich131a47e2007-09-16 15:53:56 -07002021 }
2022 }
2023}
2024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025/* RFC 3.2.1 & the Implementers Guide 2.2.
2026 *
2027 * The Parameter Types are encoded such that the
2028 * highest-order two bits specify the action that must be
2029 * taken if the processing endpoint does not recognize the
2030 * Parameter Type.
2031 *
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002032 * 00 - Stop processing this parameter; do not process any further
2033 * parameters within this chunk
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 *
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002035 * 01 - Stop processing this parameter, do not process any further
2036 * parameters within this chunk, and report the unrecognized
2037 * parameter in an 'Unrecognized Parameter' ERROR chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 *
2039 * 10 - Skip this parameter and continue processing.
2040 *
2041 * 11 - Skip this parameter and continue processing but
2042 * report the unrecognized parameter in an
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002043 * 'Unrecognized Parameter' ERROR chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 *
2045 * Return value:
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002046 * SCTP_IERROR_NO_ERROR - continue with the chunk
2047 * SCTP_IERROR_ERROR - stop and report an error.
2048 * SCTP_IERROR_NOMEME - out of memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 */
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002050static sctp_ierror_t sctp_process_unk_param(const struct sctp_association *asoc,
2051 union sctp_params param,
2052 struct sctp_chunk *chunk,
2053 struct sctp_chunk **errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002055 int retval = SCTP_IERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
2057 switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
2058 case SCTP_PARAM_ACTION_DISCARD:
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002059 retval = SCTP_IERROR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 break;
2061 case SCTP_PARAM_ACTION_SKIP:
2062 break;
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002063 case SCTP_PARAM_ACTION_DISCARD_ERR:
2064 retval = SCTP_IERROR_ERROR;
2065 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 case SCTP_PARAM_ACTION_SKIP_ERR:
2067 /* Make an ERROR chunk, preparing enough room for
2068 * returning multiple unknown parameters.
2069 */
2070 if (NULL == *errp)
Neil Horman5fa782c2010-04-28 10:30:59 +00002071 *errp = sctp_make_op_error_fixed(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 if (*errp) {
Jiri Bohac2205a6e2011-02-17 13:12:08 +00002074 if (!sctp_init_cause_fixed(*errp, SCTP_ERROR_UNKNOWN_PARAM,
2075 WORD_ROUND(ntohs(param.p->length))))
2076 sctp_addto_chunk_fixed(*errp,
2077 WORD_ROUND(ntohs(param.p->length)),
2078 param.v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 } else {
2080 /* If there is no memory for generating the ERROR
2081 * report as specified, an ABORT will be triggered
2082 * to the peer and the association won't be
2083 * established.
2084 */
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002085 retval = SCTP_IERROR_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 break;
2088 default:
2089 break;
2090 }
2091
2092 return retval;
2093}
2094
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002095/* Verify variable length parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 * Return values:
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002097 * SCTP_IERROR_ABORT - trigger an ABORT
2098 * SCTP_IERROR_NOMEM - out of memory (abort)
2099 * SCTP_IERROR_ERROR - stop processing, trigger an ERROR
2100 * SCTP_IERROR_NO_ERROR - continue with the chunk
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 */
Eric W. Biedermanf53b5b02012-08-07 07:29:08 +00002102static sctp_ierror_t sctp_verify_param(struct net *net,
2103 const struct sctp_association *asoc,
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002104 union sctp_params param,
2105 sctp_cid_t cid,
2106 struct sctp_chunk *chunk,
2107 struct sctp_chunk **err_chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
Wei Yongjun72da7b32008-04-12 18:39:19 -07002109 struct sctp_hmac_algo_param *hmacs;
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002110 int retval = SCTP_IERROR_NO_ERROR;
Wei Yongjun72da7b32008-04-12 18:39:19 -07002111 __u16 n_elt, id = 0;
2112 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
2114 /* FIXME - This routine is not looking at each parameter per the
2115 * chunk type, i.e., unrecognized parameters should be further
2116 * identified based on the chunk id.
2117 */
2118
2119 switch (param.p->type) {
2120 case SCTP_PARAM_IPV4_ADDRESS:
2121 case SCTP_PARAM_IPV6_ADDRESS:
2122 case SCTP_PARAM_COOKIE_PRESERVATIVE:
2123 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2124 case SCTP_PARAM_STATE_COOKIE:
2125 case SCTP_PARAM_HEARTBEAT_INFO:
2126 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2127 case SCTP_PARAM_ECN_CAPABLE:
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002128 case SCTP_PARAM_ADAPTATION_LAYER_IND:
Vlad Yasevichd6701192007-12-20 14:13:31 -08002129 break;
2130
Vlad Yasevich131a47e2007-09-16 15:53:56 -07002131 case SCTP_PARAM_SUPPORTED_EXT:
Eric W. Biedermanf53b5b02012-08-07 07:29:08 +00002132 if (!sctp_verify_ext_param(net, param))
Vlad Yasevichd6701192007-12-20 14:13:31 -08002133 return SCTP_IERROR_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 break;
2135
Vlad Yasevichd6de3092007-12-20 14:10:00 -08002136 case SCTP_PARAM_SET_PRIMARY:
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00002137 if (net->sctp.addip_enable)
Vlad Yasevichd6de3092007-12-20 14:10:00 -08002138 break;
2139 goto fallthrough;
2140
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 case SCTP_PARAM_HOST_NAME_ADDRESS:
2142 /* Tell the peer, we won't support this param. */
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002143 sctp_process_hn_param(asoc, param, chunk, err_chunk);
2144 retval = SCTP_IERROR_ABORT;
2145 break;
Vlad Yasevich131a47e2007-09-16 15:53:56 -07002146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 case SCTP_PARAM_FWD_TSN_SUPPORT:
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00002148 if (net->sctp.prsctp_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 break;
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002150 goto fallthrough;
2151
2152 case SCTP_PARAM_RANDOM:
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00002153 if (!net->sctp.auth_enable)
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002154 goto fallthrough;
2155
2156 /* SCTP-AUTH: Secion 6.1
2157 * If the random number is not 32 byte long the association
2158 * MUST be aborted. The ABORT chunk SHOULD contain the error
2159 * cause 'Protocol Violation'.
2160 */
2161 if (SCTP_AUTH_RANDOM_LENGTH !=
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002162 ntohs(param.p->length) - sizeof(sctp_paramhdr_t)) {
2163 sctp_process_inv_paramlength(asoc, param.p,
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002164 chunk, err_chunk);
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002165 retval = SCTP_IERROR_ABORT;
2166 }
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002167 break;
2168
2169 case SCTP_PARAM_CHUNKS:
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00002170 if (!net->sctp.auth_enable)
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002171 goto fallthrough;
2172
2173 /* SCTP-AUTH: Section 3.2
2174 * The CHUNKS parameter MUST be included once in the INIT or
2175 * INIT-ACK chunk if the sender wants to receive authenticated
2176 * chunks. Its maximum length is 260 bytes.
2177 */
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002178 if (260 < ntohs(param.p->length)) {
2179 sctp_process_inv_paramlength(asoc, param.p,
2180 chunk, err_chunk);
2181 retval = SCTP_IERROR_ABORT;
2182 }
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002183 break;
2184
2185 case SCTP_PARAM_HMAC_ALGO:
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00002186 if (!net->sctp.auth_enable)
Wei Yongjun72da7b32008-04-12 18:39:19 -07002187 goto fallthrough;
2188
2189 hmacs = (struct sctp_hmac_algo_param *)param.p;
2190 n_elt = (ntohs(param.p->length) - sizeof(sctp_paramhdr_t)) >> 1;
2191
2192 /* SCTP-AUTH: Section 6.1
2193 * The HMAC algorithm based on SHA-1 MUST be supported and
2194 * included in the HMAC-ALGO parameter.
2195 */
2196 for (i = 0; i < n_elt; i++) {
2197 id = ntohs(hmacs->hmac_ids[i]);
2198
2199 if (id == SCTP_AUTH_HMAC_ID_SHA1)
2200 break;
2201 }
2202
2203 if (id != SCTP_AUTH_HMAC_ID_SHA1) {
2204 sctp_process_inv_paramlength(asoc, param.p, chunk,
2205 err_chunk);
2206 retval = SCTP_IERROR_ABORT;
2207 }
2208 break;
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002209fallthrough:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 default:
Daniel Borkmannbb333812013-06-28 19:49:40 +02002211 pr_debug("%s: unrecognized param:%d for chunk:%d\n",
2212 __func__, ntohs(param.p->type), cid);
2213
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002214 retval = sctp_process_unk_param(asoc, param, chunk, err_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 break;
2216 }
2217 return retval;
2218}
2219
2220/* Verify the INIT packet before we process it. */
Eric W. Biedermanf53b5b02012-08-07 07:29:08 +00002221int sctp_verify_init(struct net *net, const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 sctp_cid_t cid,
2223 sctp_init_chunk_t *peer_init,
2224 struct sctp_chunk *chunk,
2225 struct sctp_chunk **errp)
2226{
2227 union sctp_params param;
Daniel Borkmann7613f5f2013-08-27 16:53:52 +02002228 bool has_cookie = false;
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002229 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
Daniel Borkmann7613f5f2013-08-27 16:53:52 +02002231 /* Check for missing mandatory parameters. Note: Initial TSN is
2232 * also mandatory, but is not checked here since the valid range
2233 * is 0..2**32-1. RFC4960, section 3.3.3.
2234 */
2235 if (peer_init->init_hdr.num_outbound_streams == 0 ||
2236 peer_init->init_hdr.num_inbound_streams == 0 ||
2237 peer_init->init_hdr.init_tag == 0 ||
2238 ntohl(peer_init->init_hdr.a_rwnd) < SCTP_DEFAULT_MINWINDOW)
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002239 return sctp_process_inv_mandatory(asoc, chunk, errp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 sctp_walk_params(param, peer_init, init_hdr.params) {
Daniel Borkmann7613f5f2013-08-27 16:53:52 +02002242 if (param.p->type == SCTP_PARAM_STATE_COOKIE)
2243 has_cookie = true;
2244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
2246 /* There is a possibility that a parameter length was bad and
2247 * in that case we would have stoped walking the parameters.
2248 * The current param.p would point at the bad one.
2249 * Current consensus on the mailing list is to generate a PROTOCOL
2250 * VIOLATION error. We build the ERROR chunk here and let the normal
2251 * error handling code build and send the packet.
2252 */
wangweidong26ac8e52013-12-23 12:16:51 +08002253 if (param.v != (void *)chunk->chunk_end)
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002254 return sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256 /* The only missing mandatory param possible today is
2257 * the state cookie for an INIT-ACK chunk.
2258 */
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002259 if ((SCTP_CID_INIT_ACK == cid) && !has_cookie)
2260 return sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
2261 chunk, errp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002263 /* Verify all the variable length parameters */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 sctp_walk_params(param, peer_init, init_hdr.params) {
2265
Eric W. Biedermanf53b5b02012-08-07 07:29:08 +00002266 result = sctp_verify_param(net, asoc, param, cid, chunk, errp);
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002267 switch (result) {
wangweidongf7010e62013-12-23 12:16:52 +08002268 case SCTP_IERROR_ABORT:
2269 case SCTP_IERROR_NOMEM:
2270 return 0;
2271 case SCTP_IERROR_ERROR:
2272 return 1;
2273 case SCTP_IERROR_NO_ERROR:
2274 default:
2275 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 }
2277
2278 } /* for (loop through all parameters) */
2279
2280 return 1;
2281}
2282
2283/* Unpack the parameters in an INIT packet into an association.
2284 * Returns 0 on failure, else success.
2285 * FIXME: This is an association method.
2286 */
Wei Yongjunde6becdc02011-04-19 21:30:51 +00002287int sctp_process_init(struct sctp_association *asoc, struct sctp_chunk *chunk,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 const union sctp_addr *peer_addr,
Al Virodd0fc662005-10-07 07:46:04 +01002289 sctp_init_chunk_t *peer_init, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00002291 struct net *net = sock_net(asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 union sctp_params param;
2293 struct sctp_transport *transport;
2294 struct list_head *pos, *temp;
Wei Yongjunde6becdc02011-04-19 21:30:51 +00002295 struct sctp_af *af;
2296 union sctp_addr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 char *cookie;
Wei Yongjunde6becdc02011-04-19 21:30:51 +00002298 int src_match = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300 /* We must include the address that the INIT packet came from.
2301 * This is the only address that matters for an INIT packet.
2302 * When processing a COOKIE ECHO, we retrieve the from address
2303 * of the INIT from the cookie.
2304 */
2305
2306 /* This implementation defaults to making the first transport
2307 * added as the primary transport. The source address seems to
2308 * be a a better choice than any of the embedded addresses.
2309 */
wangweidongcb3f8372013-12-23 12:16:50 +08002310 if (!sctp_assoc_add_peer(asoc, peer_addr, gfp, SCTP_ACTIVE))
Wei Yongjunde6becdc02011-04-19 21:30:51 +00002311 goto nomem;
2312
2313 if (sctp_cmp_addr_exact(sctp_source(chunk), peer_addr))
2314 src_match = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 /* Process the initialization parameters. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 sctp_walk_params(param, peer_init, init_hdr.params) {
Wei Yongjunde6becdc02011-04-19 21:30:51 +00002318 if (!src_match && (param.p->type == SCTP_PARAM_IPV4_ADDRESS ||
2319 param.p->type == SCTP_PARAM_IPV6_ADDRESS)) {
2320 af = sctp_get_af_specific(param_type2af(param.p->type));
2321 af->from_addr_param(&addr, param.addr,
2322 chunk->sctp_hdr->source, 0);
2323 if (sctp_cmp_addr_exact(sctp_source(chunk), &addr))
2324 src_match = 1;
2325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 if (!sctp_process_param(asoc, param, peer_addr, gfp))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002328 goto clean_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 }
2330
Wei Yongjunde6becdc02011-04-19 21:30:51 +00002331 /* source address of chunk may not match any valid address */
2332 if (!src_match)
2333 goto clean_up;
2334
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002335 /* AUTH: After processing the parameters, make sure that we
2336 * have all the required info to potentially do authentications.
2337 */
2338 if (asoc->peer.auth_capable && (!asoc->peer.peer_random ||
2339 !asoc->peer.peer_hmacs))
2340 asoc->peer.auth_capable = 0;
2341
Vlad Yasevichd6701192007-12-20 14:13:31 -08002342 /* In a non-backward compatible mode, if the peer claims
2343 * support for ADD-IP but not AUTH, the ADD-IP spec states
2344 * that we MUST ABORT the association. Section 6. The section
2345 * also give us an option to silently ignore the packet, which
2346 * is what we'll do here.
Vlad Yasevich6b2f9cb2007-09-16 19:35:39 -07002347 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00002348 if (!net->sctp.addip_noauth &&
Vlad Yasevich73d9c4f2007-10-24 17:24:26 -04002349 (asoc->peer.asconf_capable && !asoc->peer.auth_capable)) {
Vlad Yasevich6b2f9cb2007-09-16 19:35:39 -07002350 asoc->peer.addip_disabled_mask |= (SCTP_PARAM_ADD_IP |
2351 SCTP_PARAM_DEL_IP |
2352 SCTP_PARAM_SET_PRIMARY);
Vlad Yasevich88799fe2007-10-24 17:24:23 -04002353 asoc->peer.asconf_capable = 0;
Vlad Yasevichd6701192007-12-20 14:13:31 -08002354 goto clean_up;
Vlad Yasevich6b2f9cb2007-09-16 19:35:39 -07002355 }
2356
Frank Filz3f7a87d2005-06-20 13:14:57 -07002357 /* Walk list of transports, removing transports in the UNKNOWN state. */
2358 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2359 transport = list_entry(pos, struct sctp_transport, transports);
2360 if (transport->state == SCTP_UNKNOWN) {
2361 sctp_assoc_rm_peer(asoc, transport);
2362 }
2363 }
2364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 /* The fixed INIT headers are always in network byte
2366 * order.
2367 */
2368 asoc->peer.i.init_tag =
2369 ntohl(peer_init->init_hdr.init_tag);
2370 asoc->peer.i.a_rwnd =
2371 ntohl(peer_init->init_hdr.a_rwnd);
2372 asoc->peer.i.num_outbound_streams =
2373 ntohs(peer_init->init_hdr.num_outbound_streams);
2374 asoc->peer.i.num_inbound_streams =
2375 ntohs(peer_init->init_hdr.num_inbound_streams);
2376 asoc->peer.i.initial_tsn =
2377 ntohl(peer_init->init_hdr.initial_tsn);
2378
2379 /* Apply the upper bounds for output streams based on peer's
2380 * number of inbound streams.
2381 */
2382 if (asoc->c.sinit_num_ostreams >
2383 ntohs(peer_init->init_hdr.num_inbound_streams)) {
2384 asoc->c.sinit_num_ostreams =
2385 ntohs(peer_init->init_hdr.num_inbound_streams);
2386 }
2387
2388 if (asoc->c.sinit_max_instreams >
2389 ntohs(peer_init->init_hdr.num_outbound_streams)) {
2390 asoc->c.sinit_max_instreams =
2391 ntohs(peer_init->init_hdr.num_outbound_streams);
2392 }
2393
2394 /* Copy Initiation tag from INIT to VT_peer in cookie. */
2395 asoc->c.peer_vtag = asoc->peer.i.init_tag;
2396
2397 /* Peer Rwnd : Current calculated value of the peer's rwnd. */
2398 asoc->peer.rwnd = asoc->peer.i.a_rwnd;
2399
2400 /* Copy cookie in case we need to resend COOKIE-ECHO. */
2401 cookie = asoc->peer.cookie;
2402 if (cookie) {
Arnaldo Carvalho de Meloaf997d82006-11-21 01:20:33 -02002403 asoc->peer.cookie = kmemdup(cookie, asoc->peer.cookie_len, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 if (!asoc->peer.cookie)
2405 goto clean_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 }
2407
2408 /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
2409 * high (for example, implementations MAY use the size of the receiver
2410 * advertised window).
2411 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002412 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
2413 transports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 transport->ssthresh = asoc->peer.i.a_rwnd;
2415 }
2416
2417 /* Set up the TSN tracking pieces. */
Vlad Yasevich8e1ee182008-10-08 14:18:39 -07002418 if (!sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
2419 asoc->peer.i.initial_tsn, gfp))
2420 goto clean_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
2422 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
2423 *
2424 * The stream sequence number in all the streams shall start
2425 * from 0 when the association is established. Also, when the
2426 * stream sequence number reaches the value 65535 the next
2427 * stream sequence number shall be set to 0.
2428 */
2429
Frank Filz3f7a87d2005-06-20 13:14:57 -07002430 /* Allocate storage for the negotiated streams if it is not a temporary
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002431 * association.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 */
2433 if (!asoc->temp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 int error;
2435
2436 asoc->ssnmap = sctp_ssnmap_new(asoc->c.sinit_max_instreams,
2437 asoc->c.sinit_num_ostreams, gfp);
2438 if (!asoc->ssnmap)
2439 goto clean_up;
2440
Vlad Yasevich07d93962007-05-04 13:55:27 -07002441 error = sctp_assoc_set_id(asoc, gfp);
2442 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 goto clean_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
2445
2446 /* ADDIP Section 4.1 ASCONF Chunk Procedures
2447 *
2448 * When an endpoint has an ASCONF signaled change to be sent to the
2449 * remote endpoint it should do the following:
2450 * ...
2451 * A2) A serial number should be assigned to the Chunk. The serial
2452 * number should be a monotonically increasing number. All serial
2453 * numbers are defined to be initialized at the start of the
2454 * association to the same value as the Initial TSN.
2455 */
2456 asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
2457 return 1;
2458
2459clean_up:
2460 /* Release the transport structures. */
2461 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2462 transport = list_entry(pos, struct sctp_transport, transports);
Vlad Yasevichadd52372008-09-18 16:28:27 -07002463 if (transport->state != SCTP_ACTIVE)
2464 sctp_assoc_rm_peer(asoc, transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07002466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467nomem:
2468 return 0;
2469}
2470
2471
2472/* Update asoc with the option described in param.
2473 *
2474 * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
2475 *
2476 * asoc is the association to update.
2477 * param is the variable length parameter to use for update.
2478 * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
2479 * If the current packet is an INIT we want to minimize the amount of
2480 * work we do. In particular, we should not build transport
2481 * structures for the addresses.
2482 */
2483static int sctp_process_param(struct sctp_association *asoc,
2484 union sctp_params param,
2485 const union sctp_addr *peer_addr,
Al Virodd0fc662005-10-07 07:46:04 +01002486 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487{
Eric W. Biedermane7ff4a72012-08-07 07:27:02 +00002488 struct net *net = sock_net(asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 union sctp_addr addr;
2490 int i;
2491 __u16 sat;
2492 int retval = 1;
2493 sctp_scope_t scope;
2494 time_t stale;
2495 struct sctp_af *af;
Vlad Yasevichd6de3092007-12-20 14:10:00 -08002496 union sctp_addr_param *addr_param;
2497 struct sctp_transport *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
2499 /* We maintain all INIT parameters in network byte order all the
2500 * time. This allows us to not worry about whether the parameters
2501 * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
2502 */
2503 switch (param.p->type) {
2504 case SCTP_PARAM_IPV6_ADDRESS:
2505 if (PF_INET6 != asoc->base.sk->sk_family)
2506 break;
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07002507 goto do_addr_param;
2508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 case SCTP_PARAM_IPV4_ADDRESS:
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07002510 /* v4 addresses are not allowed on v6-only socket */
2511 if (ipv6_only_sock(asoc->base.sk))
2512 break;
2513do_addr_param:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 af = sctp_get_af_specific(param_type2af(param.p->type));
Al Virodd86d132006-11-20 17:11:13 -08002515 af->from_addr_param(&addr, param.addr, htons(asoc->peer.port), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 scope = sctp_scope(peer_addr);
Eric W. Biedermane7ff4a72012-08-07 07:27:02 +00002517 if (sctp_in_scope(net, &addr, scope))
Al Virodd86d132006-11-20 17:11:13 -08002518 if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_UNCONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 return 0;
2520 break;
2521
2522 case SCTP_PARAM_COOKIE_PRESERVATIVE:
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00002523 if (!net->sctp.cookie_preserve_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 break;
2525
2526 stale = ntohl(param.life->lifespan_increment);
2527
2528 /* Suggested Cookie Life span increment's unit is msec,
2529 * (1/1000sec).
2530 */
Daniel Borkmann52db8822013-06-25 18:17:27 +02002531 asoc->cookie_life = ktime_add_ms(asoc->cookie_life, stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 break;
2533
2534 case SCTP_PARAM_HOST_NAME_ADDRESS:
Daniel Borkmannbb333812013-06-28 19:49:40 +02002535 pr_debug("%s: unimplemented SCTP_HOST_NAME_ADDRESS\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 break;
2537
2538 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2539 /* Turn off the default values first so we'll know which
2540 * ones are really set by the peer.
2541 */
2542 asoc->peer.ipv4_address = 0;
2543 asoc->peer.ipv6_address = 0;
2544
Gui Jianfeng140ee962008-03-05 13:43:32 -08002545 /* Assume that peer supports the address family
2546 * by which it sends a packet.
2547 */
2548 if (peer_addr->sa.sa_family == AF_INET6)
2549 asoc->peer.ipv6_address = 1;
2550 else if (peer_addr->sa.sa_family == AF_INET)
2551 asoc->peer.ipv4_address = 1;
2552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 /* Cycle through address types; avoid divide by 0. */
2554 sat = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2555 if (sat)
2556 sat /= sizeof(__u16);
2557
2558 for (i = 0; i < sat; ++i) {
2559 switch (param.sat->types[i]) {
2560 case SCTP_PARAM_IPV4_ADDRESS:
2561 asoc->peer.ipv4_address = 1;
2562 break;
2563
2564 case SCTP_PARAM_IPV6_ADDRESS:
Wei Yongjun6e40a912008-05-09 15:11:17 -07002565 if (PF_INET6 == asoc->base.sk->sk_family)
2566 asoc->peer.ipv6_address = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 break;
2568
2569 case SCTP_PARAM_HOST_NAME_ADDRESS:
2570 asoc->peer.hostname_address = 1;
2571 break;
2572
2573 default: /* Just ignore anything else. */
2574 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 }
2577 break;
2578
2579 case SCTP_PARAM_STATE_COOKIE:
2580 asoc->peer.cookie_len =
2581 ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
2582 asoc->peer.cookie = param.cookie->body;
2583 break;
2584
2585 case SCTP_PARAM_HEARTBEAT_INFO:
2586 /* Would be odd to receive, but it causes no problems. */
2587 break;
2588
2589 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2590 /* Rejected during verify stage. */
2591 break;
2592
2593 case SCTP_PARAM_ECN_CAPABLE:
2594 asoc->peer.ecn_capable = 1;
2595 break;
2596
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002597 case SCTP_PARAM_ADAPTATION_LAYER_IND:
Vlad Yaseviche69c4e02008-09-15 16:29:49 -04002598 asoc->peer.adaptation_ind = ntohl(param.aind->adaptation_ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 break;
2600
Vlad Yasevichd6de3092007-12-20 14:10:00 -08002601 case SCTP_PARAM_SET_PRIMARY:
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00002602 if (!net->sctp.addip_enable)
Vlad Yasevich0ef46e22008-09-18 16:27:38 -07002603 goto fall_through;
2604
Vlad Yasevichd6de3092007-12-20 14:10:00 -08002605 addr_param = param.v + sizeof(sctp_addip_param_t);
2606
2607 af = sctp_get_af_specific(param_type2af(param.p->type));
2608 af->from_addr_param(&addr, addr_param,
2609 htons(asoc->peer.port), 0);
2610
2611 /* if the address is invalid, we can't process it.
2612 * XXX: see spec for what to do.
2613 */
2614 if (!af->addr_valid(&addr, NULL, NULL))
2615 break;
2616
2617 t = sctp_assoc_lookup_paddr(asoc, &addr);
2618 if (!t)
2619 break;
2620
2621 sctp_assoc_set_primary(asoc, t);
2622 break;
2623
Vlad Yasevich131a47e2007-09-16 15:53:56 -07002624 case SCTP_PARAM_SUPPORTED_EXT:
2625 sctp_process_ext_param(asoc, param);
2626 break;
2627
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 case SCTP_PARAM_FWD_TSN_SUPPORT:
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00002629 if (net->sctp.prsctp_enable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 asoc->peer.prsctp_capable = 1;
2631 break;
2632 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002633 /* Fall Through */
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002634 goto fall_through;
2635
2636 case SCTP_PARAM_RANDOM:
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00002637 if (!net->sctp.auth_enable)
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002638 goto fall_through;
2639
2640 /* Save peer's random parameter */
2641 asoc->peer.peer_random = kmemdup(param.p,
2642 ntohs(param.p->length), gfp);
2643 if (!asoc->peer.peer_random) {
2644 retval = 0;
2645 break;
2646 }
2647 break;
2648
2649 case SCTP_PARAM_HMAC_ALGO:
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00002650 if (!net->sctp.auth_enable)
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002651 goto fall_through;
2652
2653 /* Save peer's HMAC list */
2654 asoc->peer.peer_hmacs = kmemdup(param.p,
2655 ntohs(param.p->length), gfp);
2656 if (!asoc->peer.peer_hmacs) {
2657 retval = 0;
2658 break;
2659 }
2660
2661 /* Set the default HMAC the peer requested*/
2662 sctp_auth_asoc_set_default_hmac(asoc, param.hmac_algo);
2663 break;
2664
2665 case SCTP_PARAM_CHUNKS:
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00002666 if (!net->sctp.auth_enable)
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002667 goto fall_through;
2668
2669 asoc->peer.peer_chunks = kmemdup(param.p,
2670 ntohs(param.p->length), gfp);
2671 if (!asoc->peer.peer_chunks)
2672 retval = 0;
2673 break;
2674fall_through:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 default:
2676 /* Any unrecognized parameters should have been caught
2677 * and handled by sctp_verify_param() which should be
2678 * called prior to this routine. Simply log the error
2679 * here.
2680 */
Daniel Borkmannbb333812013-06-28 19:49:40 +02002681 pr_debug("%s: ignoring param:%d for association:%p.\n",
2682 __func__, ntohs(param.p->type), asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
2686 return retval;
2687}
2688
2689/* Select a new verification tag. */
2690__u32 sctp_generate_tag(const struct sctp_endpoint *ep)
2691{
2692 /* I believe that this random number generator complies with RFC1750.
2693 * A tag of 0 is reserved for special cases (e.g. INIT).
2694 */
2695 __u32 x;
2696
2697 do {
2698 get_random_bytes(&x, sizeof(__u32));
2699 } while (x == 0);
2700
2701 return x;
2702}
2703
2704/* Select an initial TSN to send during startup. */
2705__u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
2706{
2707 __u32 retval;
2708
2709 get_random_bytes(&retval, sizeof(__u32));
2710 return retval;
2711}
2712
2713/*
2714 * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
2715 * 0 1 2 3
2716 * 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
2717 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2718 * | Type = 0xC1 | Chunk Flags | Chunk Length |
2719 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2720 * | Serial Number |
2721 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2722 * | Address Parameter |
2723 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2724 * | ASCONF Parameter #1 |
2725 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2726 * \ \
2727 * / .... /
2728 * \ \
2729 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2730 * | ASCONF Parameter #N |
2731 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2732 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002733 * Address Parameter and other parameter will not be wrapped in this function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 */
2735static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
2736 union sctp_addr *addr,
2737 int vparam_len)
2738{
2739 sctp_addiphdr_t asconf;
2740 struct sctp_chunk *retval;
2741 int length = sizeof(asconf) + vparam_len;
2742 union sctp_addr_param addrparam;
2743 int addrlen;
2744 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2745
2746 addrlen = af->to_addr_param(addr, &addrparam);
2747 if (!addrlen)
2748 return NULL;
2749 length += addrlen;
2750
2751 /* Create the chunk. */
Vlad Yasevich072017b2013-08-09 22:05:36 -04002752 retval = sctp_make_control(asoc, SCTP_CID_ASCONF, 0, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 if (!retval)
2754 return NULL;
2755
2756 asconf.serial = htonl(asoc->addip_serial++);
2757
2758 retval->subh.addip_hdr =
2759 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2760 retval->param_hdr.v =
2761 sctp_addto_chunk(retval, addrlen, &addrparam);
2762
2763 return retval;
2764}
2765
2766/* ADDIP
2767 * 3.2.1 Add IP Address
2768 * 0 1 2 3
2769 * 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
2770 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2771 * | Type = 0xC001 | Length = Variable |
2772 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2773 * | ASCONF-Request Correlation ID |
2774 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2775 * | Address Parameter |
2776 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2777 *
2778 * 3.2.2 Delete IP Address
2779 * 0 1 2 3
2780 * 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
2781 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2782 * | Type = 0xC002 | Length = Variable |
2783 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2784 * | ASCONF-Request Correlation ID |
2785 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2786 * | Address Parameter |
2787 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2788 *
2789 */
2790struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
2791 union sctp_addr *laddr,
2792 struct sockaddr *addrs,
2793 int addrcnt,
Al Virodbc16db2006-11-20 17:01:42 -08002794 __be16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795{
2796 sctp_addip_param_t param;
2797 struct sctp_chunk *retval;
2798 union sctp_addr_param addr_param;
2799 union sctp_addr *addr;
2800 void *addr_buf;
2801 struct sctp_af *af;
2802 int paramlen = sizeof(param);
2803 int addr_param_len = 0;
2804 int totallen = 0;
2805 int i;
Michio Honda8a07eb02011-04-26 20:19:36 +09002806 int del_pickup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
2808 /* Get total length of all the address parameters. */
2809 addr_buf = addrs;
2810 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +00002811 addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 af = sctp_get_af_specific(addr->v4.sin_family);
2813 addr_param_len = af->to_addr_param(addr, &addr_param);
2814
2815 totallen += paramlen;
2816 totallen += addr_param_len;
2817
2818 addr_buf += af->sockaddr_len;
Michio Honda8a07eb02011-04-26 20:19:36 +09002819 if (asoc->asconf_addr_del_pending && !del_pickup) {
2820 /* reuse the parameter length from the same scope one */
2821 totallen += paramlen;
2822 totallen += addr_param_len;
2823 del_pickup = 1;
Daniel Borkmannbb333812013-06-28 19:49:40 +02002824
2825 pr_debug("%s: picked same-scope del_pending addr, "
2826 "totallen for all addresses is %d\n",
2827 __func__, totallen);
Michio Honda8a07eb02011-04-26 20:19:36 +09002828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 }
2830
2831 /* Create an asconf chunk with the required length. */
2832 retval = sctp_make_asconf(asoc, laddr, totallen);
2833 if (!retval)
2834 return NULL;
2835
2836 /* Add the address parameters to the asconf chunk. */
2837 addr_buf = addrs;
2838 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +00002839 addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 af = sctp_get_af_specific(addr->v4.sin_family);
2841 addr_param_len = af->to_addr_param(addr, &addr_param);
2842 param.param_hdr.type = flags;
2843 param.param_hdr.length = htons(paramlen + addr_param_len);
2844 param.crr_id = i;
2845
2846 sctp_addto_chunk(retval, paramlen, &param);
2847 sctp_addto_chunk(retval, addr_param_len, &addr_param);
2848
2849 addr_buf += af->sockaddr_len;
2850 }
Michio Honda8a07eb02011-04-26 20:19:36 +09002851 if (flags == SCTP_PARAM_ADD_IP && del_pickup) {
2852 addr = asoc->asconf_addr_del_pending;
2853 af = sctp_get_af_specific(addr->v4.sin_family);
2854 addr_param_len = af->to_addr_param(addr, &addr_param);
2855 param.param_hdr.type = SCTP_PARAM_DEL_IP;
2856 param.param_hdr.length = htons(paramlen + addr_param_len);
2857 param.crr_id = i;
2858
2859 sctp_addto_chunk(retval, paramlen, &param);
2860 sctp_addto_chunk(retval, addr_param_len, &addr_param);
2861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 return retval;
2863}
2864
2865/* ADDIP
2866 * 3.2.4 Set Primary IP Address
2867 * 0 1 2 3
2868 * 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
2869 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2870 * | Type =0xC004 | Length = Variable |
2871 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2872 * | ASCONF-Request Correlation ID |
2873 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2874 * | Address Parameter |
2875 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2876 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002877 * Create an ASCONF chunk with Set Primary IP address parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 */
2879struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
2880 union sctp_addr *addr)
2881{
2882 sctp_addip_param_t param;
2883 struct sctp_chunk *retval;
2884 int len = sizeof(param);
2885 union sctp_addr_param addrparam;
2886 int addrlen;
2887 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2888
2889 addrlen = af->to_addr_param(addr, &addrparam);
2890 if (!addrlen)
2891 return NULL;
2892 len += addrlen;
2893
2894 /* Create the chunk and make asconf header. */
2895 retval = sctp_make_asconf(asoc, addr, len);
2896 if (!retval)
2897 return NULL;
2898
2899 param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
2900 param.param_hdr.length = htons(len);
2901 param.crr_id = 0;
2902
2903 sctp_addto_chunk(retval, sizeof(param), &param);
2904 sctp_addto_chunk(retval, addrlen, &addrparam);
2905
2906 return retval;
2907}
2908
2909/* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2910 * 0 1 2 3
2911 * 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
2912 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2913 * | Type = 0x80 | Chunk Flags | Chunk Length |
2914 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2915 * | Serial Number |
2916 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2917 * | ASCONF Parameter Response#1 |
2918 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2919 * \ \
2920 * / .... /
2921 * \ \
2922 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2923 * | ASCONF Parameter Response#N |
2924 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2925 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002926 * Create an ASCONF_ACK chunk with enough space for the parameter responses.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 */
2928static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
2929 __u32 serial, int vparam_len)
2930{
2931 sctp_addiphdr_t asconf;
2932 struct sctp_chunk *retval;
2933 int length = sizeof(asconf) + vparam_len;
2934
2935 /* Create the chunk. */
Vlad Yasevich072017b2013-08-09 22:05:36 -04002936 retval = sctp_make_control(asoc, SCTP_CID_ASCONF_ACK, 0, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 if (!retval)
2938 return NULL;
2939
2940 asconf.serial = htonl(serial);
2941
2942 retval->subh.addip_hdr =
2943 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2944
2945 return retval;
2946}
2947
2948/* Add response parameters to an ASCONF_ACK chunk. */
Al Viro9f81bcd2006-11-20 17:26:34 -08002949static void sctp_add_asconf_response(struct sctp_chunk *chunk, __be32 crr_id,
Al Virodbc16db2006-11-20 17:01:42 -08002950 __be16 err_code, sctp_addip_param_t *asconf_param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951{
2952 sctp_addip_param_t ack_param;
2953 sctp_errhdr_t err_param;
2954 int asconf_param_len = 0;
2955 int err_param_len = 0;
Al Virodbc16db2006-11-20 17:01:42 -08002956 __be16 response_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
2958 if (SCTP_ERROR_NO_ERROR == err_code) {
2959 response_type = SCTP_PARAM_SUCCESS_REPORT;
2960 } else {
2961 response_type = SCTP_PARAM_ERR_CAUSE;
2962 err_param_len = sizeof(err_param);
2963 if (asconf_param)
2964 asconf_param_len =
2965 ntohs(asconf_param->param_hdr.length);
2966 }
2967
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002968 /* Add Success Indication or Error Cause Indication parameter. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 ack_param.param_hdr.type = response_type;
2970 ack_param.param_hdr.length = htons(sizeof(ack_param) +
2971 err_param_len +
2972 asconf_param_len);
2973 ack_param.crr_id = crr_id;
2974 sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
2975
2976 if (SCTP_ERROR_NO_ERROR == err_code)
2977 return;
2978
2979 /* Add Error Cause parameter. */
2980 err_param.cause = err_code;
2981 err_param.length = htons(err_param_len + asconf_param_len);
2982 sctp_addto_chunk(chunk, err_param_len, &err_param);
2983
2984 /* Add the failed TLV copied from ASCONF chunk. */
2985 if (asconf_param)
2986 sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
2987}
2988
2989/* Process a asconf parameter. */
Al Virodbc16db2006-11-20 17:01:42 -08002990static __be16 sctp_process_asconf_param(struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 struct sctp_chunk *asconf,
2992 sctp_addip_param_t *asconf_param)
2993{
2994 struct sctp_transport *peer;
2995 struct sctp_af *af;
2996 union sctp_addr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 union sctp_addr_param *addr_param;
Al Viro5f242a132006-11-20 17:05:23 -08002998
Joe Perchesea110732011-06-13 16:21:26 +00002999 addr_param = (void *)asconf_param + sizeof(sctp_addip_param_t);
Patrick McHardyc1cc6782008-05-13 23:25:00 -07003000
Wei Yongjun44e65c12009-06-16 14:48:24 +08003001 if (asconf_param->param_hdr.type != SCTP_PARAM_ADD_IP &&
3002 asconf_param->param_hdr.type != SCTP_PARAM_DEL_IP &&
3003 asconf_param->param_hdr.type != SCTP_PARAM_SET_PRIMARY)
3004 return SCTP_ERROR_UNKNOWN_PARAM;
3005
Shan Wei6a435732011-04-18 19:11:47 +00003006 switch (addr_param->p.type) {
Wei Yongjunc4492582008-05-09 15:11:53 -07003007 case SCTP_PARAM_IPV6_ADDRESS:
3008 if (!asoc->peer.ipv6_address)
Wei Yongjun945e5ab2009-04-16 14:21:02 +08003009 return SCTP_ERROR_DNS_FAILED;
Wei Yongjunc4492582008-05-09 15:11:53 -07003010 break;
3011 case SCTP_PARAM_IPV4_ADDRESS:
3012 if (!asoc->peer.ipv4_address)
Wei Yongjun945e5ab2009-04-16 14:21:02 +08003013 return SCTP_ERROR_DNS_FAILED;
Wei Yongjunc4492582008-05-09 15:11:53 -07003014 break;
3015 default:
Wei Yongjun945e5ab2009-04-16 14:21:02 +08003016 return SCTP_ERROR_DNS_FAILED;
Wei Yongjunc4492582008-05-09 15:11:53 -07003017 }
3018
Shan Wei6a435732011-04-18 19:11:47 +00003019 af = sctp_get_af_specific(param_type2af(addr_param->p.type));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 if (unlikely(!af))
Wei Yongjun945e5ab2009-04-16 14:21:02 +08003021 return SCTP_ERROR_DNS_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
Al Virodd86d132006-11-20 17:11:13 -08003023 af->from_addr_param(&addr, addr_param, htons(asoc->peer.port), 0);
Vlad Yasevich42e30bf2007-12-20 14:08:56 -08003024
3025 /* ADDIP 4.2.1 This parameter MUST NOT contain a broadcast
3026 * or multicast address.
3027 * (note: wildcard is permitted and requires special handling so
3028 * make sure we check for that)
3029 */
3030 if (!af->is_any(&addr) && !af->addr_valid(&addr, NULL, asconf->skb))
Wei Yongjun945e5ab2009-04-16 14:21:02 +08003031 return SCTP_ERROR_DNS_FAILED;
Vlad Yasevich42e30bf2007-12-20 14:08:56 -08003032
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 switch (asconf_param->param_hdr.type) {
3034 case SCTP_PARAM_ADD_IP:
Vlad Yasevich42e30bf2007-12-20 14:08:56 -08003035 /* Section 4.2.1:
3036 * If the address 0.0.0.0 or ::0 is provided, the source
3037 * address of the packet MUST be added.
3038 */
3039 if (af->is_any(&addr))
3040 memcpy(&addr, &asconf->source, sizeof(addr));
3041
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003043 * request and does not have the local resources to add this
3044 * new address to the association, it MUST return an Error
3045 * Cause TLV set to the new error code 'Operation Refused
3046 * Due to Resource Shortage'.
3047 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
Al Virodd86d132006-11-20 17:11:13 -08003049 peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC, SCTP_UNCONFIRMED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 if (!peer)
3051 return SCTP_ERROR_RSRC_LOW;
3052
3053 /* Start the heartbeat timer. */
3054 if (!mod_timer(&peer->hb_timer, sctp_transport_timeout(peer)))
3055 sctp_transport_hold(peer);
Michio Honda6af29cc2011-06-16 17:14:34 +09003056 asoc->new_transport = peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 break;
3058 case SCTP_PARAM_DEL_IP:
3059 /* ADDIP 4.3 D7) If a request is received to delete the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003060 * last remaining IP address of a peer endpoint, the receiver
3061 * MUST send an Error Cause TLV with the error cause set to the
3062 * new error code 'Request to Delete Last Remaining IP Address'.
3063 */
Vlad Yasevich42e30bf2007-12-20 14:08:56 -08003064 if (asoc->peer.transport_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 return SCTP_ERROR_DEL_LAST_IP;
3066
3067 /* ADDIP 4.3 D8) If a request is received to delete an IP
3068 * address which is also the source address of the IP packet
3069 * which contained the ASCONF chunk, the receiver MUST reject
3070 * this request. To reject the request the receiver MUST send
3071 * an Error Cause TLV set to the new error code 'Request to
3072 * Delete Source IP Address'
3073 */
Michio Hondab1364102011-04-26 17:37:02 +09003074 if (sctp_cmp_addr_exact(&asconf->source, &addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 return SCTP_ERROR_DEL_SRC_IP;
3076
Vlad Yasevich42e30bf2007-12-20 14:08:56 -08003077 /* Section 4.2.2
3078 * If the address 0.0.0.0 or ::0 is provided, all
3079 * addresses of the peer except the source address of the
3080 * packet MUST be deleted.
3081 */
3082 if (af->is_any(&addr)) {
3083 sctp_assoc_set_primary(asoc, asconf->transport);
3084 sctp_assoc_del_nonprimary_peers(asoc,
3085 asconf->transport);
3086 } else
3087 sctp_assoc_del_peer(asoc, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 break;
3089 case SCTP_PARAM_SET_PRIMARY:
Vlad Yasevich42e30bf2007-12-20 14:08:56 -08003090 /* ADDIP Section 4.2.4
3091 * If the address 0.0.0.0 or ::0 is provided, the receiver
3092 * MAY mark the source address of the packet as its
3093 * primary.
3094 */
3095 if (af->is_any(&addr))
3096 memcpy(&addr.v4, sctp_source(asconf), sizeof(addr));
3097
Al Virodd86d132006-11-20 17:11:13 -08003098 peer = sctp_assoc_lookup_paddr(asoc, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 if (!peer)
Wei Yongjun945e5ab2009-04-16 14:21:02 +08003100 return SCTP_ERROR_DNS_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
3102 sctp_assoc_set_primary(asoc, peer);
3103 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 }
3105
3106 return SCTP_ERROR_NO_ERROR;
3107}
3108
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003109/* Verify the ASCONF packet before we process it. */
3110int sctp_verify_asconf(const struct sctp_association *asoc,
3111 struct sctp_paramhdr *param_hdr, void *chunk_end,
3112 struct sctp_paramhdr **errp) {
3113 sctp_addip_param_t *asconf_param;
3114 union sctp_params param;
3115 int length, plen;
3116
3117 param.v = (sctp_paramhdr_t *) param_hdr;
3118 while (param.v <= chunk_end - sizeof(sctp_paramhdr_t)) {
3119 length = ntohs(param.p->length);
3120 *errp = param.p;
3121
3122 if (param.v > chunk_end - length ||
3123 length < sizeof(sctp_paramhdr_t))
3124 return 0;
3125
3126 switch (param.p->type) {
3127 case SCTP_PARAM_ADD_IP:
3128 case SCTP_PARAM_DEL_IP:
3129 case SCTP_PARAM_SET_PRIMARY:
3130 asconf_param = (sctp_addip_param_t *)param.v;
3131 plen = ntohs(asconf_param->param_hdr.length);
3132 if (plen < sizeof(sctp_addip_param_t) +
3133 sizeof(sctp_paramhdr_t))
3134 return 0;
3135 break;
3136 case SCTP_PARAM_SUCCESS_REPORT:
3137 case SCTP_PARAM_ADAPTATION_LAYER_IND:
3138 if (length != sizeof(sctp_addip_param_t))
3139 return 0;
3140
3141 break;
3142 default:
3143 break;
3144 }
3145
3146 param.v += WORD_ROUND(length);
3147 }
3148
3149 if (param.v != chunk_end)
3150 return 0;
3151
3152 return 1;
3153}
3154
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003155/* Process an incoming ASCONF chunk with the next expected serial no. and
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 * return an ASCONF_ACK chunk to be sent in response.
3157 */
3158struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
3159 struct sctp_chunk *asconf)
3160{
3161 sctp_addiphdr_t *hdr;
3162 union sctp_addr_param *addr_param;
3163 sctp_addip_param_t *asconf_param;
3164 struct sctp_chunk *asconf_ack;
3165
Al Virodbc16db2006-11-20 17:01:42 -08003166 __be16 err_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 int length = 0;
Wei Yongjunf3830cc2007-10-15 11:51:03 +09003168 int chunk_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 __u32 serial;
3170 int all_param_pass = 1;
3171
Wei Yongjunf3830cc2007-10-15 11:51:03 +09003172 chunk_len = ntohs(asconf->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 hdr = (sctp_addiphdr_t *)asconf->skb->data;
3174 serial = ntohl(hdr->serial);
3175
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003176 /* Skip the addiphdr and store a pointer to address parameter. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 length = sizeof(sctp_addiphdr_t);
3178 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
3179 chunk_len -= length;
3180
3181 /* Skip the address parameter and store a pointer to the first
Joe Perches7aa1b542007-12-20 14:03:52 -08003182 * asconf parameter.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003183 */
Shan Wei6a435732011-04-18 19:11:47 +00003184 length = ntohs(addr_param->p.length);
Joe Perchesea110732011-06-13 16:21:26 +00003185 asconf_param = (void *)addr_param + length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 chunk_len -= length;
3187
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003188 /* create an ASCONF_ACK chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 * Based on the definitions of parameters, we know that the size of
Wei Yongjun2cab86b2011-03-31 23:42:55 +00003190 * ASCONF_ACK parameters are less than or equal to the fourfold of ASCONF
Joe Perches7aa1b542007-12-20 14:03:52 -08003191 * parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 */
Wei Yongjun2cab86b2011-03-31 23:42:55 +00003193 asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 if (!asconf_ack)
3195 goto done;
3196
3197 /* Process the TLVs contained within the ASCONF chunk. */
3198 while (chunk_len > 0) {
3199 err_code = sctp_process_asconf_param(asoc, asconf,
3200 asconf_param);
3201 /* ADDIP 4.1 A7)
3202 * If an error response is received for a TLV parameter,
3203 * all TLVs with no response before the failed TLV are
3204 * considered successful if not reported. All TLVs after
3205 * the failed response are considered unsuccessful unless
3206 * a specific success indication is present for the parameter.
3207 */
3208 if (SCTP_ERROR_NO_ERROR != err_code)
3209 all_param_pass = 0;
3210
3211 if (!all_param_pass)
3212 sctp_add_asconf_response(asconf_ack,
3213 asconf_param->crr_id, err_code,
3214 asconf_param);
3215
3216 /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
3217 * an IP address sends an 'Out of Resource' in its response, it
3218 * MUST also fail any subsequent add or delete requests bundled
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003219 * in the ASCONF.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 */
3221 if (SCTP_ERROR_RSRC_LOW == err_code)
3222 goto done;
3223
3224 /* Move to the next ASCONF param. */
3225 length = ntohs(asconf_param->param_hdr.length);
Joe Perchesea110732011-06-13 16:21:26 +00003226 asconf_param = (void *)asconf_param + length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 chunk_len -= length;
3228 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003229
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230done:
3231 asoc->peer.addip_serial++;
3232
3233 /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003234 * after freeing the reference to old asconf ack if any.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 */
3236 if (asconf_ack) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 sctp_chunk_hold(asconf_ack);
Vlad Yasevicha08de642007-12-20 14:11:47 -08003238 list_add_tail(&asconf_ack->transmitted_list,
3239 &asoc->asconf_ack_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 }
3241
3242 return asconf_ack;
3243}
3244
3245/* Process a asconf parameter that is successfully acked. */
Wei Yongjun425e0f62009-06-16 14:47:30 +08003246static void sctp_asconf_param_success(struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 sctp_addip_param_t *asconf_param)
3248{
3249 struct sctp_af *af;
3250 union sctp_addr addr;
3251 struct sctp_bind_addr *bp = &asoc->base.bind_addr;
3252 union sctp_addr_param *addr_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 struct sctp_transport *transport;
Sridhar Samudraladc022a92006-07-21 14:49:25 -07003254 struct sctp_sockaddr_entry *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255
Joe Perchesea110732011-06-13 16:21:26 +00003256 addr_param = (void *)asconf_param + sizeof(sctp_addip_param_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
3258 /* We have checked the packet before, so we do not check again. */
Shan Wei6a435732011-04-18 19:11:47 +00003259 af = sctp_get_af_specific(param_type2af(addr_param->p.type));
Al Virodd86d132006-11-20 17:11:13 -08003260 af->from_addr_param(&addr, addr_param, htons(bp->port), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
3262 switch (asconf_param->param_hdr.type) {
3263 case SCTP_PARAM_ADD_IP:
Vlad Yasevich559cf712007-09-16 16:03:28 -07003264 /* This is always done in BH context with a socket lock
3265 * held, so the list can not change.
3266 */
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -04003267 local_bh_disable();
Vlad Yasevich559cf712007-09-16 16:03:28 -07003268 list_for_each_entry(saddr, &bp->address_list, list) {
Al Virodd86d132006-11-20 17:11:13 -08003269 if (sctp_cmp_addr_exact(&saddr->a, &addr))
Vlad Yasevichf57d96b2007-12-20 14:12:24 -08003270 saddr->state = SCTP_ADDR_SRC;
Sridhar Samudraladc022a92006-07-21 14:49:25 -07003271 }
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -04003272 local_bh_enable();
Wei Yongjun3cd97492009-06-16 10:07:23 +08003273 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
3274 transports) {
Wei Yongjun3cd97492009-06-16 10:07:23 +08003275 dst_release(transport->dst);
Vlad Yasevichc6ef0062011-04-18 19:15:22 +00003276 transport->dst = NULL;
Wei Yongjun3cd97492009-06-16 10:07:23 +08003277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 break;
3279 case SCTP_PARAM_DEL_IP:
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -04003280 local_bh_disable();
Wei Yongjun425e0f62009-06-16 14:47:30 +08003281 sctp_del_bind_addr(bp, &addr);
Michio Honda8a07eb02011-04-26 20:19:36 +09003282 if (asoc->asconf_addr_del_pending != NULL &&
3283 sctp_cmp_addr_exact(asoc->asconf_addr_del_pending, &addr)) {
3284 kfree(asoc->asconf_addr_del_pending);
3285 asoc->asconf_addr_del_pending = NULL;
3286 }
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -04003287 local_bh_enable();
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07003288 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
3289 transports) {
Sridhar Samudraladc022a92006-07-21 14:49:25 -07003290 dst_release(transport->dst);
Vlad Yasevichc6ef0062011-04-18 19:15:22 +00003291 transport->dst = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 }
3293 break;
3294 default:
3295 break;
3296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297}
3298
3299/* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
3300 * for the given asconf parameter. If there is no response for this parameter,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003301 * return the error code based on the third argument 'no_err'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 * ADDIP 4.1
3303 * A7) If an error response is received for a TLV parameter, all TLVs with no
3304 * response before the failed TLV are considered successful if not reported.
3305 * All TLVs after the failed response are considered unsuccessful unless a
3306 * specific success indication is present for the parameter.
3307 */
Al Virodbc16db2006-11-20 17:01:42 -08003308static __be16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 sctp_addip_param_t *asconf_param,
3310 int no_err)
3311{
3312 sctp_addip_param_t *asconf_ack_param;
3313 sctp_errhdr_t *err_param;
3314 int length;
Wei Yongjunf3830cc2007-10-15 11:51:03 +09003315 int asconf_ack_len;
Al Virodbc16db2006-11-20 17:01:42 -08003316 __be16 err_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
3318 if (no_err)
3319 err_code = SCTP_ERROR_NO_ERROR;
3320 else
3321 err_code = SCTP_ERROR_REQ_REFUSED;
3322
Wei Yongjunf3830cc2007-10-15 11:51:03 +09003323 asconf_ack_len = ntohs(asconf_ack->chunk_hdr->length) -
3324 sizeof(sctp_chunkhdr_t);
3325
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
3327 * the first asconf_ack parameter.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003328 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 length = sizeof(sctp_addiphdr_t);
3330 asconf_ack_param = (sctp_addip_param_t *)(asconf_ack->skb->data +
3331 length);
3332 asconf_ack_len -= length;
3333
3334 while (asconf_ack_len > 0) {
3335 if (asconf_ack_param->crr_id == asconf_param->crr_id) {
wangweidongcb3f8372013-12-23 12:16:50 +08003336 switch (asconf_ack_param->param_hdr.type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 case SCTP_PARAM_SUCCESS_REPORT:
3338 return SCTP_ERROR_NO_ERROR;
3339 case SCTP_PARAM_ERR_CAUSE:
3340 length = sizeof(sctp_addip_param_t);
Joe Perchesea110732011-06-13 16:21:26 +00003341 err_param = (void *)asconf_ack_param + length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 asconf_ack_len -= length;
3343 if (asconf_ack_len > 0)
3344 return err_param->cause;
3345 else
3346 return SCTP_ERROR_INV_PARAM;
3347 break;
3348 default:
3349 return SCTP_ERROR_INV_PARAM;
3350 }
3351 }
3352
3353 length = ntohs(asconf_ack_param->param_hdr.length);
Joe Perchesea110732011-06-13 16:21:26 +00003354 asconf_ack_param = (void *)asconf_ack_param + length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 asconf_ack_len -= length;
3356 }
3357
3358 return err_code;
3359}
3360
3361/* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
3362int sctp_process_asconf_ack(struct sctp_association *asoc,
3363 struct sctp_chunk *asconf_ack)
3364{
3365 struct sctp_chunk *asconf = asoc->addip_last_asconf;
3366 union sctp_addr_param *addr_param;
3367 sctp_addip_param_t *asconf_param;
3368 int length = 0;
3369 int asconf_len = asconf->skb->len;
3370 int all_param_pass = 0;
3371 int no_err = 1;
3372 int retval = 0;
Al Virodbc16db2006-11-20 17:01:42 -08003373 __be16 err_code = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374
3375 /* Skip the chunkhdr and addiphdr from the last asconf sent and store
3376 * a pointer to address parameter.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003377 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 length = sizeof(sctp_addip_chunk_t);
3379 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
3380 asconf_len -= length;
3381
3382 /* Skip the address parameter in the last asconf sent and store a
Joe Perches7aa1b542007-12-20 14:03:52 -08003383 * pointer to the first asconf parameter.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003384 */
Shan Wei6a435732011-04-18 19:11:47 +00003385 length = ntohs(addr_param->p.length);
Joe Perchesea110732011-06-13 16:21:26 +00003386 asconf_param = (void *)addr_param + length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 asconf_len -= length;
3388
3389 /* ADDIP 4.1
3390 * A8) If there is no response(s) to specific TLV parameter(s), and no
3391 * failures are indicated, then all request(s) are considered
3392 * successful.
3393 */
3394 if (asconf_ack->skb->len == sizeof(sctp_addiphdr_t))
3395 all_param_pass = 1;
3396
3397 /* Process the TLVs contained in the last sent ASCONF chunk. */
3398 while (asconf_len > 0) {
3399 if (all_param_pass)
3400 err_code = SCTP_ERROR_NO_ERROR;
3401 else {
3402 err_code = sctp_get_asconf_response(asconf_ack,
3403 asconf_param,
3404 no_err);
3405 if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
3406 no_err = 0;
3407 }
3408
3409 switch (err_code) {
3410 case SCTP_ERROR_NO_ERROR:
Wei Yongjun425e0f62009-06-16 14:47:30 +08003411 sctp_asconf_param_success(asoc, asconf_param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 break;
3413
3414 case SCTP_ERROR_RSRC_LOW:
3415 retval = 1;
3416 break;
3417
Wei Yongjuna987f762009-04-07 15:44:29 +08003418 case SCTP_ERROR_UNKNOWN_PARAM:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 /* Disable sending this type of asconf parameter in
3420 * future.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003421 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 asoc->peer.addip_disabled_mask |=
3423 asconf_param->param_hdr.type;
3424 break;
3425
3426 case SCTP_ERROR_REQ_REFUSED:
3427 case SCTP_ERROR_DEL_LAST_IP:
3428 case SCTP_ERROR_DEL_SRC_IP:
3429 default:
3430 break;
3431 }
3432
3433 /* Skip the processed asconf parameter and move to the next
3434 * one.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003435 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 length = ntohs(asconf_param->param_hdr.length);
Joe Perchesea110732011-06-13 16:21:26 +00003437 asconf_param = (void *)asconf_param + length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 asconf_len -= length;
3439 }
3440
Michio Hondaddc4bbe2011-06-17 11:03:23 +09003441 if (no_err && asoc->src_out_of_asoc_ok) {
Michio Honda8a07eb02011-04-26 20:19:36 +09003442 asoc->src_out_of_asoc_ok = 0;
Michio Hondaddc4bbe2011-06-17 11:03:23 +09003443 sctp_transport_immediate_rtx(asoc->peer.primary_path);
3444 }
Michio Honda8a07eb02011-04-26 20:19:36 +09003445
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 /* Free the cached last sent asconf chunk. */
Vlad Yasevich5f9646c2008-02-05 14:23:44 -05003447 list_del_init(&asconf->transmitted_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 sctp_chunk_free(asconf);
3449 asoc->addip_last_asconf = NULL;
3450
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 return retval;
3452}
3453
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003454/* Make a FWD TSN chunk. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
3456 __u32 new_cum_tsn, size_t nstreams,
3457 struct sctp_fwdtsn_skip *skiplist)
3458{
3459 struct sctp_chunk *retval = NULL;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003460 struct sctp_fwdtsn_hdr ftsn_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 struct sctp_fwdtsn_skip skip;
3462 size_t hint;
3463 int i;
3464
3465 hint = (nstreams + 1) * sizeof(__u32);
3466
Vlad Yasevich072017b2013-08-09 22:05:36 -04003467 retval = sctp_make_control(asoc, SCTP_CID_FWD_TSN, 0, hint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
3469 if (!retval)
3470 return NULL;
3471
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
3473 retval->subh.fwdtsn_hdr =
3474 sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
3475
3476 for (i = 0; i < nstreams; i++) {
3477 skip.stream = skiplist[i].stream;
3478 skip.ssn = skiplist[i].ssn;
3479 sctp_addto_chunk(retval, sizeof(skip), &skip);
3480 }
3481
3482 return retval;
3483}