blob: e518eb64ccf3578f7892da050c160a56cf3cc833 [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
Herbert Xu5821c762016-01-24 21:20:12 +080048#include <crypto/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/types.h>
50#include <linux/kernel.h>
51#include <linux/ip.h>
52#include <linux/ipv6.h>
53#include <linux/net.h>
54#include <linux/inet.h>
Christian Borntraegerebc3bbc2007-10-23 12:46:32 +020055#include <linux/scatterlist.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,
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -030065 __u8 type, __u8 flags, int paylen,
66 gfp_t gfp);
Vlad Yasevich072017b2013-08-09 22:05:36 -040067static struct sctp_chunk *sctp_make_data(const struct sctp_association *asoc,
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -030068 __u8 flags, int paylen, gfp_t gfp);
Vlad Yasevich072017b2013-08-09 22:05:36 -040069static struct sctp_chunk *_sctp_make_chunk(const struct sctp_association *asoc,
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -030070 __u8 type, __u8 flags, int paylen,
71 gfp_t gfp);
Xin Longf48ef4c2017-07-23 09:34:27 +080072static struct sctp_cookie_param *sctp_pack_cookie(
73 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 const struct sctp_association *asoc,
75 const struct sctp_chunk *init_chunk,
76 int *cookie_len,
77 const __u8 *raw_addrs, int addrs_len);
78static int sctp_process_param(struct sctp_association *asoc,
79 union sctp_params param,
80 const union sctp_addr *peer_addr,
Al Virodd0fc662005-10-07 07:46:04 +010081 gfp_t gfp);
Vlad Yasevich8ee4be32007-11-29 08:50:35 -050082static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
83 const void *data);
stephen hemminger6daaf0d2014-01-09 22:31:11 -080084static void *sctp_addto_chunk_fixed(struct sctp_chunk *, int len,
85 const void *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Vlad Yasevich072017b2013-08-09 22:05:36 -040087/* Control chunk destructor */
88static void sctp_control_release_owner(struct sk_buff *skb)
89{
Xin Long1b1e0bc2018-03-14 19:05:30 +080090 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
91
Xin Longec2e5062018-03-14 19:05:33 +080092 if (chunk->shkey) {
93 struct sctp_shared_key *shkey = chunk->shkey;
94 struct sctp_association *asoc = chunk->asoc;
95
96 /* refcnt == 2 and !list_empty mean after this release, it's
97 * not being used anywhere, and it's time to notify userland
98 * that this shkey can be freed if it's been deactivated.
99 */
100 if (shkey->deactivated && !list_empty(&shkey->key_list) &&
101 refcount_read(&shkey->refcnt) == 2) {
102 struct sctp_ulpevent *ev;
103
104 ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id,
105 SCTP_AUTH_FREE_KEY,
106 GFP_KERNEL);
107 if (ev)
108 asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
109 }
Xin Long1b1e0bc2018-03-14 19:05:30 +0800110 sctp_auth_shkey_release(chunk->shkey);
Xin Longec2e5062018-03-14 19:05:33 +0800111 }
Vlad Yasevich072017b2013-08-09 22:05:36 -0400112}
113
114static void sctp_control_set_owner_w(struct sctp_chunk *chunk)
115{
116 struct sctp_association *asoc = chunk->asoc;
117 struct sk_buff *skb = chunk->skb;
118
119 /* TODO: properly account for control chunks.
120 * To do it right we'll need:
121 * 1) endpoint if association isn't known.
122 * 2) proper memory accounting.
123 *
124 * For now don't do anything for now.
125 */
Xin Long1b1e0bc2018-03-14 19:05:30 +0800126 if (chunk->auth) {
127 chunk->shkey = asoc->shkey;
128 sctp_auth_shkey_hold(chunk->shkey);
129 }
Vlad Yasevich072017b2013-08-09 22:05:36 -0400130 skb->sk = asoc ? asoc->base.sk : NULL;
Xin Long1b1e0bc2018-03-14 19:05:30 +0800131 skb_shinfo(skb)->destructor_arg = chunk;
Vlad Yasevich072017b2013-08-09 22:05:36 -0400132 skb->destructor = sctp_control_release_owner;
133}
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/* What was the inbound interface for this chunk? */
136int sctp_chunk_iif(const struct sctp_chunk *chunk)
137{
Marcelo Ricardo Leitnere7487c82016-07-13 15:08:58 -0300138 struct sk_buff *skb = chunk->skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Marcelo Ricardo Leitnere7487c82016-07-13 15:08:58 -0300140 return SCTP_INPUT_CB(skb)->af->skb_iif(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143/* RFC 2960 3.3.2 Initiation (INIT) (1)
144 *
145 * Note 2: The ECN capable field is reserved for future use of
146 * Explicit Congestion Notification.
147 */
148static const struct sctp_paramhdr ecap_param = {
149 SCTP_PARAM_ECN_CAPABLE,
Harvey Harrison09640e62009-02-01 00:45:17 -0800150 cpu_to_be16(sizeof(struct sctp_paramhdr)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152static const struct sctp_paramhdr prsctp_param = {
153 SCTP_PARAM_FWD_TSN_SUPPORT,
Harvey Harrison09640e62009-02-01 00:45:17 -0800154 cpu_to_be16(sizeof(struct sctp_paramhdr)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155};
156
Neil Horman5fa782c2010-04-28 10:30:59 +0000157/* A helper to initialize an op error inside a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 * provided chunk, as most cause codes will be embedded inside an
159 * abort chunk.
160 */
Marcelo Ricardo Leitner6d3e8aa2018-04-29 12:56:31 -0300161int sctp_init_cause(struct sctp_chunk *chunk, __be16 cause_code,
162 size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Xin Longd8238d92017-08-03 15:42:11 +0800164 struct sctp_errhdr err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 __u16 len;
166
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900167 /* Cause code constants are now defined in network order. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 err.cause = cause_code;
Xin Longd8238d92017-08-03 15:42:11 +0800169 len = sizeof(err) + paylen;
Marcelo Ricardo Leitner6d3e8aa2018-04-29 12:56:31 -0300170 err.length = htons(len);
171
172 if (skb_tailroom(chunk->skb) < len)
173 return -ENOSPC;
174
Xin Longd8238d92017-08-03 15:42:11 +0800175 chunk->subh.err_hdr = sctp_addto_chunk(chunk, sizeof(err), &err);
Marcelo Ricardo Leitner6d3e8aa2018-04-29 12:56:31 -0300176
177 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Neil Horman5fa782c2010-04-28 10:30:59 +0000180/* A helper to initialize an op error inside a
181 * provided chunk, as most cause codes will be embedded inside an
182 * abort chunk. Differs from sctp_init_cause in that it won't oops
183 * if there isn't enough space in the op error chunk
184 */
Ioan Orghicidb28aaf2012-07-13 07:16:37 +0000185static int sctp_init_cause_fixed(struct sctp_chunk *chunk, __be16 cause_code,
Xin Long327c0da2017-08-11 10:23:58 +0800186 size_t paylen)
Neil Horman5fa782c2010-04-28 10:30:59 +0000187{
Xin Longd8238d92017-08-03 15:42:11 +0800188 struct sctp_errhdr err;
Neil Horman5fa782c2010-04-28 10:30:59 +0000189 __u16 len;
190
191 /* Cause code constants are now defined in network order. */
192 err.cause = cause_code;
Xin Longd8238d92017-08-03 15:42:11 +0800193 len = sizeof(err) + paylen;
Neil Horman5fa782c2010-04-28 10:30:59 +0000194 err.length = htons(len);
195
Wei Yongjun2e3219b2010-05-17 22:51:58 -0700196 if (skb_tailroom(chunk->skb) < len)
Neil Horman5fa782c2010-04-28 10:30:59 +0000197 return -ENOSPC;
Xin Longd8238d92017-08-03 15:42:11 +0800198
199 chunk->subh.err_hdr = sctp_addto_chunk_fixed(chunk, sizeof(err), &err);
200
Neil Horman5fa782c2010-04-28 10:30:59 +0000201 return 0;
202}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/* 3.3.2 Initiation (INIT) (1)
204 *
205 * This chunk is used to initiate a SCTP association between two
206 * endpoints. The format of the INIT chunk is shown below:
207 *
208 * 0 1 2 3
209 * 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
210 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
211 * | Type = 1 | Chunk Flags | Chunk Length |
212 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
213 * | Initiate Tag |
214 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
215 * | Advertised Receiver Window Credit (a_rwnd) |
216 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
217 * | Number of Outbound Streams | Number of Inbound Streams |
218 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
219 * | Initial TSN |
220 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
221 * \ \
222 * / Optional/Variable-Length Parameters /
223 * \ \
224 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
225 *
226 *
227 * The INIT chunk contains the following parameters. Unless otherwise
228 * noted, each parameter MUST only be included once in the INIT chunk.
229 *
230 * Fixed Parameters Status
231 * ----------------------------------------------
232 * Initiate Tag Mandatory
233 * Advertised Receiver Window Credit Mandatory
234 * Number of Outbound Streams Mandatory
235 * Number of Inbound Streams Mandatory
236 * Initial TSN Mandatory
237 *
238 * Variable Parameters Status Type Value
239 * -------------------------------------------------------------
240 * IPv4 Address (Note 1) Optional 5
241 * IPv6 Address (Note 1) Optional 6
242 * Cookie Preservative Optional 9
243 * Reserved for ECN Capable (Note 2) Optional 32768 (0x8000)
244 * Host Name Address (Note 3) Optional 11
245 * Supported Address Types (Note 4) Optional 12
246 */
247struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
Xin Long327c0da2017-08-11 10:23:58 +0800248 const struct sctp_bind_addr *bp,
249 gfp_t gfp, int vparam_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000251 struct net *net = sock_net(asoc->base.sk);
Xin Long327c0da2017-08-11 10:23:58 +0800252 struct sctp_supported_ext_param ext_param;
253 struct sctp_adaptation_ind_param aiparam;
254 struct sctp_paramhdr *auth_chunks = NULL;
255 struct sctp_paramhdr *auth_hmacs = NULL;
256 struct sctp_supported_addrs_param sat;
Vlad Yasevichb14878c2014-04-17 17:26:50 +0200257 struct sctp_endpoint *ep = asoc->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 struct sctp_chunk *retval = NULL;
259 int num_types, addrs_len = 0;
Xin Long327c0da2017-08-11 10:23:58 +0800260 struct sctp_inithdr init;
261 union sctp_params addrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 struct sctp_sock *sp;
Xin Long96b120b2017-12-08 21:03:59 +0800263 __u8 extensions[5];
Xin Long327c0da2017-08-11 10:23:58 +0800264 size_t chunksize;
265 __be16 types[2];
266 int num_ext = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 /* RFC 2960 3.3.2 Initiation (INIT) (1)
269 *
270 * Note 1: The INIT chunks can contain multiple addresses that
271 * can be IPv4 and/or IPv6 in any combination.
272 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 /* Convert the provided bind address list to raw format. */
275 addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, gfp);
276
277 init.init_tag = htonl(asoc->c.my_vtag);
278 init.a_rwnd = htonl(asoc->rwnd);
279 init.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
280 init.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
281 init.initial_tsn = htonl(asoc->c.initial_tsn);
282
283 /* How many address types are needed? */
284 sp = sctp_sk(asoc->base.sk);
285 num_types = sp->pf->supported_addrs(sp, types);
286
Wei Yongjuna8170c32010-04-28 08:47:21 +0000287 chunksize = sizeof(init) + addrs_len;
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -0300288 chunksize += SCTP_PAD4(SCTP_SAT_LEN(num_types));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 chunksize += sizeof(ecap_param);
Vlad Yasevich8ee4be32007-11-29 08:50:35 -0500290
Xin Long28aa4c22016-07-09 19:47:40 +0800291 if (asoc->prsctp_enable)
Vlad Yasevich036b5792008-01-07 00:28:16 -0800292 chunksize += sizeof(prsctp_param);
293
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700294 /* ADDIP: Section 4.2.7:
295 * An implementation supporting this extension [ADDIP] MUST list
296 * the ASCONF,the ASCONF-ACK, and the AUTH chunks in its INIT and
297 * INIT-ACK parameters.
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700298 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +0000299 if (net->sctp.addip_enable) {
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700300 extensions[num_ext] = SCTP_CID_ASCONF;
301 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
302 num_ext += 2;
303 }
304
Xin Longc28445c2017-01-18 00:44:45 +0800305 if (asoc->reconf_enable) {
306 extensions[num_ext] = SCTP_CID_RECONF;
307 num_ext += 1;
308 }
309
malc6fc791e2009-03-12 09:49:20 +0000310 if (sp->adaptation_ind)
311 chunksize += sizeof(aiparam);
312
Xin Long96b120b2017-12-08 21:03:59 +0800313 if (sp->strm_interleave) {
314 extensions[num_ext] = SCTP_CID_I_DATA;
315 num_ext += 1;
316 }
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 chunksize += vparam_len;
319
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700320 /* Account for AUTH related parameters */
Vlad Yasevichb14878c2014-04-17 17:26:50 +0200321 if (ep->auth_enable) {
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700322 /* Add random parameter length*/
323 chunksize += sizeof(asoc->c.auth_random);
324
325 /* Add HMACS parameter length if any were defined */
Xin Long3c918702017-06-30 11:52:16 +0800326 auth_hmacs = (struct sctp_paramhdr *)asoc->c.auth_hmacs;
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700327 if (auth_hmacs->length)
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -0300328 chunksize += SCTP_PAD4(ntohs(auth_hmacs->length));
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700329 else
330 auth_hmacs = NULL;
331
332 /* Add CHUNKS parameter length */
Xin Long3c918702017-06-30 11:52:16 +0800333 auth_chunks = (struct sctp_paramhdr *)asoc->c.auth_chunks;
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700334 if (auth_chunks->length)
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -0300335 chunksize += SCTP_PAD4(ntohs(auth_chunks->length));
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700336 else
Vlad Yasevich9baffaa2007-11-29 08:44:34 -0500337 auth_chunks = NULL;
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700338
339 extensions[num_ext] = SCTP_CID_AUTH;
340 num_ext += 1;
341 }
342
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700343 /* If we have any extensions to report, account for that */
344 if (num_ext)
Xin Long15328d92017-07-17 11:29:56 +0800345 chunksize += SCTP_PAD4(sizeof(ext_param) + num_ext);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 /* RFC 2960 3.3.2 Initiation (INIT) (1)
348 *
349 * Note 3: An INIT chunk MUST NOT contain more than one Host
350 * Name address parameter. Moreover, the sender of the INIT
351 * MUST NOT combine any other address types with the Host Name
352 * address in the INIT. The receiver of INIT MUST ignore any
353 * other address types if the Host Name address parameter is
354 * present in the received INIT chunk.
355 *
356 * PLEASE DO NOT FIXME [This version does not support Host Name.]
357 */
358
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -0300359 retval = sctp_make_control(asoc, SCTP_CID_INIT, 0, chunksize, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (!retval)
361 goto nodata;
362
363 retval->subh.init_hdr =
364 sctp_addto_chunk(retval, sizeof(init), &init);
365 retval->param_hdr.v =
366 sctp_addto_chunk(retval, addrs_len, addrs.v);
367
368 /* RFC 2960 3.3.2 Initiation (INIT) (1)
369 *
370 * Note 4: This parameter, when present, specifies all the
371 * address types the sending endpoint can support. The absence
372 * of this parameter indicates that the sending endpoint can
373 * support any address type.
374 */
375 sat.param_hdr.type = SCTP_PARAM_SUPPORTED_ADDRESS_TYPES;
376 sat.param_hdr.length = htons(SCTP_SAT_LEN(num_types));
377 sctp_addto_chunk(retval, sizeof(sat), &sat);
378 sctp_addto_chunk(retval, num_types * sizeof(__u16), &types);
379
380 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700381
Joe Perches7aa1b542007-12-20 14:03:52 -0800382 /* Add the supported extensions parameter. Be nice and add this
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700383 * fist before addiding the parameters for the extensions themselves
384 */
385 if (num_ext) {
386 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
Xin Long15328d92017-07-17 11:29:56 +0800387 ext_param.param_hdr.length = htons(sizeof(ext_param) + num_ext);
388 sctp_addto_chunk(retval, sizeof(ext_param), &ext_param);
Vlad Yasevich8ee4be32007-11-29 08:50:35 -0500389 sctp_addto_param(retval, num_ext, extensions);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700390 }
391
Xin Long28aa4c22016-07-09 19:47:40 +0800392 if (asoc->prsctp_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700394
malc6fc791e2009-03-12 09:49:20 +0000395 if (sp->adaptation_ind) {
396 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
397 aiparam.param_hdr.length = htons(sizeof(aiparam));
398 aiparam.adaptation_ind = htonl(sp->adaptation_ind);
399 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
400 }
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700401
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700402 /* Add SCTP-AUTH chunks to the parameter list */
Vlad Yasevichb14878c2014-04-17 17:26:50 +0200403 if (ep->auth_enable) {
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700404 sctp_addto_chunk(retval, sizeof(asoc->c.auth_random),
405 asoc->c.auth_random);
406 if (auth_hmacs)
407 sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
408 auth_hmacs);
409 if (auth_chunks)
410 sctp_addto_chunk(retval, ntohs(auth_chunks->length),
411 auth_chunks);
412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413nodata:
Jesper Juhla51482b2005-11-08 09:41:34 -0800414 kfree(addrs.v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return retval;
416}
417
418struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
Xin Long327c0da2017-08-11 10:23:58 +0800419 const struct sctp_chunk *chunk,
420 gfp_t gfp, int unkparam_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Xin Long327c0da2017-08-11 10:23:58 +0800422 struct sctp_supported_ext_param ext_param;
423 struct sctp_adaptation_ind_param aiparam;
424 struct sctp_paramhdr *auth_chunks = NULL;
425 struct sctp_paramhdr *auth_random = NULL;
426 struct sctp_paramhdr *auth_hmacs = NULL;
427 struct sctp_chunk *retval = NULL;
428 struct sctp_cookie_param *cookie;
Xin Long4ae70c02017-06-30 11:52:21 +0800429 struct sctp_inithdr initack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 union sctp_params addrs;
malc6fc791e2009-03-12 09:49:20 +0000431 struct sctp_sock *sp;
Xin Long96b120b2017-12-08 21:03:59 +0800432 __u8 extensions[5];
Xin Long327c0da2017-08-11 10:23:58 +0800433 size_t chunksize;
434 int num_ext = 0;
435 int cookie_len;
436 int addrs_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 /* Note: there may be no addresses to embed. */
439 addrs = sctp_bind_addrs_to_raw(&asoc->base.bind_addr, &addrs_len, gfp);
440
441 initack.init_tag = htonl(asoc->c.my_vtag);
442 initack.a_rwnd = htonl(asoc->rwnd);
443 initack.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
444 initack.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
445 initack.initial_tsn = htonl(asoc->c.initial_tsn);
446
447 /* FIXME: We really ought to build the cookie right
448 * into the packet instead of allocating more fresh memory.
449 */
450 cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
451 addrs.v, addrs_len);
452 if (!cookie)
453 goto nomem_cookie;
454
455 /* Calculate the total size of allocation, include the reserved
456 * space for reporting unknown parameters if it is specified.
457 */
malc6fc791e2009-03-12 09:49:20 +0000458 sp = sctp_sk(asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;
460
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900461 /* Tell peer that we'll do ECN only if peer advertised such cap. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (asoc->peer.ecn_capable)
463 chunksize += sizeof(ecap_param);
464
Wei Yongjun5ffad5a2009-03-12 09:49:18 +0000465 if (asoc->peer.prsctp_capable)
Vlad Yasevich036b5792008-01-07 00:28:16 -0800466 chunksize += sizeof(prsctp_param);
467
Wei Yongjun5ffad5a2009-03-12 09:49:18 +0000468 if (asoc->peer.asconf_capable) {
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700469 extensions[num_ext] = SCTP_CID_ASCONF;
470 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
471 num_ext += 2;
472 }
473
Xin Longc28445c2017-01-18 00:44:45 +0800474 if (asoc->peer.reconf_capable) {
475 extensions[num_ext] = SCTP_CID_RECONF;
476 num_ext += 1;
477 }
478
malc6fc791e2009-03-12 09:49:20 +0000479 if (sp->adaptation_ind)
480 chunksize += sizeof(aiparam);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Xin Long96b120b2017-12-08 21:03:59 +0800482 if (asoc->intl_enable) {
483 extensions[num_ext] = SCTP_CID_I_DATA;
484 num_ext += 1;
485 }
486
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700487 if (asoc->peer.auth_capable) {
Xin Long3c918702017-06-30 11:52:16 +0800488 auth_random = (struct sctp_paramhdr *)asoc->c.auth_random;
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700489 chunksize += ntohs(auth_random->length);
490
Xin Long3c918702017-06-30 11:52:16 +0800491 auth_hmacs = (struct sctp_paramhdr *)asoc->c.auth_hmacs;
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700492 if (auth_hmacs->length)
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -0300493 chunksize += SCTP_PAD4(ntohs(auth_hmacs->length));
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700494 else
495 auth_hmacs = NULL;
496
Xin Long3c918702017-06-30 11:52:16 +0800497 auth_chunks = (struct sctp_paramhdr *)asoc->c.auth_chunks;
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700498 if (auth_chunks->length)
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -0300499 chunksize += SCTP_PAD4(ntohs(auth_chunks->length));
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700500 else
501 auth_chunks = NULL;
502
503 extensions[num_ext] = SCTP_CID_AUTH;
504 num_ext += 1;
505 }
506
Vlad Yasevich8ee4be32007-11-29 08:50:35 -0500507 if (num_ext)
Xin Long15328d92017-07-17 11:29:56 +0800508 chunksize += SCTP_PAD4(sizeof(ext_param) + num_ext);
Vlad Yasevich8ee4be32007-11-29 08:50:35 -0500509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 /* Now allocate and fill out the chunk. */
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -0300511 retval = sctp_make_control(asoc, SCTP_CID_INIT_ACK, 0, chunksize, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (!retval)
513 goto nomem_chunk;
514
Dan Carpenterb99a4d52010-04-30 22:41:09 -0400515 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
516 *
517 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
518 * HEARTBEAT ACK, * etc.) to the same destination transport
519 * address from which it received the DATA or control chunk
520 * to which it is replying.
521 *
522 * [INIT ACK back to where the INIT came from.]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 */
524 retval->transport = chunk->transport;
Dan Carpenterb99a4d52010-04-30 22:41:09 -0400525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 retval->subh.init_hdr =
527 sctp_addto_chunk(retval, sizeof(initack), &initack);
528 retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
529 sctp_addto_chunk(retval, cookie_len, cookie);
530 if (asoc->peer.ecn_capable)
531 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700532 if (num_ext) {
533 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
Xin Long15328d92017-07-17 11:29:56 +0800534 ext_param.param_hdr.length = htons(sizeof(ext_param) + num_ext);
535 sctp_addto_chunk(retval, sizeof(ext_param), &ext_param);
Vlad Yasevich8ee4be32007-11-29 08:50:35 -0500536 sctp_addto_param(retval, num_ext, extensions);
Vlad Yasevich131a47e2007-09-16 15:53:56 -0700537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (asoc->peer.prsctp_capable)
539 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
540
malc6fc791e2009-03-12 09:49:20 +0000541 if (sp->adaptation_ind) {
542 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
543 aiparam.param_hdr.length = htons(sizeof(aiparam));
544 aiparam.adaptation_ind = htonl(sp->adaptation_ind);
545 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700548 if (asoc->peer.auth_capable) {
549 sctp_addto_chunk(retval, ntohs(auth_random->length),
550 auth_random);
551 if (auth_hmacs)
552 sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
553 auth_hmacs);
554 if (auth_chunks)
555 sctp_addto_chunk(retval, ntohs(auth_chunks->length),
556 auth_chunks);
557 }
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 /* We need to remove the const qualifier at this point. */
560 retval->asoc = (struct sctp_association *) asoc;
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562nomem_chunk:
563 kfree(cookie);
564nomem_cookie:
Jesper Juhla51482b2005-11-08 09:41:34 -0800565 kfree(addrs.v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return retval;
567}
568
569/* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
570 *
571 * This chunk is used only during the initialization of an association.
572 * It is sent by the initiator of an association to its peer to complete
573 * the initialization process. This chunk MUST precede any DATA chunk
574 * sent within the association, but MAY be bundled with one or more DATA
575 * chunks in the same packet.
576 *
577 * 0 1 2 3
578 * 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
579 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
580 * | Type = 10 |Chunk Flags | Length |
581 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
582 * / Cookie /
583 * \ \
584 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
585 *
586 * Chunk Flags: 8 bit
587 *
588 * Set to zero on transmit and ignored on receipt.
589 *
590 * Length: 16 bits (unsigned integer)
591 *
592 * Set to the size of the chunk in bytes, including the 4 bytes of
593 * the chunk header and the size of the Cookie.
594 *
595 * Cookie: variable size
596 *
597 * This field must contain the exact cookie received in the
598 * State Cookie parameter from the previous INIT ACK.
599 *
600 * An implementation SHOULD make the cookie as small as possible
601 * to insure interoperability.
602 */
603struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
Xin Long327c0da2017-08-11 10:23:58 +0800604 const struct sctp_chunk *chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 struct sctp_chunk *retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 int cookie_len;
Xin Long327c0da2017-08-11 10:23:58 +0800608 void *cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 cookie = asoc->peer.cookie;
611 cookie_len = asoc->peer.cookie_len;
612
613 /* Build a cookie echo chunk. */
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -0300614 retval = sctp_make_control(asoc, SCTP_CID_COOKIE_ECHO, 0,
615 cookie_len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (!retval)
617 goto nodata;
618 retval->subh.cookie_hdr =
619 sctp_addto_chunk(retval, cookie_len, cookie);
620
621 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
622 *
623 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
624 * HEARTBEAT ACK, * etc.) to the same destination transport
625 * address from which it * received the DATA or control chunk
626 * to which it is replying.
627 *
628 * [COOKIE ECHO back to where the INIT ACK came from.]
629 */
630 if (chunk)
631 retval->transport = chunk->transport;
632
633nodata:
634 return retval;
635}
636
637/* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
638 *
639 * This chunk is used only during the initialization of an
640 * association. It is used to acknowledge the receipt of a COOKIE
641 * ECHO chunk. This chunk MUST precede any DATA or SACK chunk sent
642 * within the association, but MAY be bundled with one or more DATA
643 * chunks or SACK chunk in the same SCTP packet.
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 * | Type = 11 |Chunk Flags | Length = 4 |
649 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
650 *
651 * Chunk Flags: 8 bits
652 *
653 * Set to zero on transmit and ignored on receipt.
654 */
655struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
Xin Long327c0da2017-08-11 10:23:58 +0800656 const struct sctp_chunk *chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 struct sctp_chunk *retval;
659
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -0300660 retval = sctp_make_control(asoc, SCTP_CID_COOKIE_ACK, 0, 0, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
663 *
664 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
665 * HEARTBEAT ACK, * etc.) to the same destination transport
666 * address from which it * received the DATA or control chunk
667 * to which it is replying.
668 *
669 * [COOKIE ACK back to where the COOKIE ECHO came from.]
670 */
671 if (retval && chunk)
672 retval->transport = chunk->transport;
673
674 return retval;
675}
676
677/*
678 * Appendix A: Explicit Congestion Notification:
679 * CWR:
680 *
681 * RFC 2481 details a specific bit for a sender to send in the header of
682 * its next outbound TCP segment to indicate to its peer that it has
683 * reduced its congestion window. This is termed the CWR bit. For
684 * SCTP the same indication is made by including the CWR chunk.
685 * This chunk contains one data element, i.e. the TSN number that
686 * was sent in the ECNE chunk. This element represents the lowest
687 * TSN number in the datagram that was originally marked with the
688 * CE bit.
689 *
690 * 0 1 2 3
691 * 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
692 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
693 * | Chunk Type=13 | Flags=00000000| Chunk Length = 8 |
694 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
695 * | Lowest TSN Number |
696 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
697 *
698 * Note: The CWR is considered a Control chunk.
699 */
700struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
Xin Long327c0da2017-08-11 10:23:58 +0800701 const __u32 lowest_tsn,
702 const struct sctp_chunk *chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 struct sctp_chunk *retval;
Xin Long65f77102017-08-03 15:42:16 +0800705 struct sctp_cwrhdr cwr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 cwr.lowest_tsn = htonl(lowest_tsn);
Vlad Yasevich072017b2013-08-09 22:05:36 -0400708 retval = sctp_make_control(asoc, SCTP_CID_ECN_CWR, 0,
Xin Long65f77102017-08-03 15:42:16 +0800709 sizeof(cwr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 if (!retval)
712 goto nodata;
713
714 retval->subh.ecn_cwr_hdr =
715 sctp_addto_chunk(retval, sizeof(cwr), &cwr);
716
717 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
718 *
719 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
720 * HEARTBEAT ACK, * etc.) to the same destination transport
721 * address from which it * received the DATA or control chunk
722 * to which it is replying.
723 *
724 * [Report a reduced congestion window back to where the ECNE
725 * came from.]
726 */
727 if (chunk)
728 retval->transport = chunk->transport;
729
730nodata:
731 return retval;
732}
733
734/* Make an ECNE chunk. This is a congestion experienced report. */
735struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
Xin Long327c0da2017-08-11 10:23:58 +0800736 const __u32 lowest_tsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
738 struct sctp_chunk *retval;
Xin Long1fb6d832017-08-03 15:42:14 +0800739 struct sctp_ecnehdr ecne;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 ecne.lowest_tsn = htonl(lowest_tsn);
Vlad Yasevich072017b2013-08-09 22:05:36 -0400742 retval = sctp_make_control(asoc, SCTP_CID_ECN_ECNE, 0,
Xin Long1fb6d832017-08-03 15:42:14 +0800743 sizeof(ecne), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (!retval)
745 goto nodata;
746 retval->subh.ecne_hdr =
747 sctp_addto_chunk(retval, sizeof(ecne), &ecne);
748
749nodata:
750 return retval;
751}
752
753/* Make a DATA chunk for the given association from the provided
754 * parameters. However, do not populate the data payload.
755 */
Xin Long0c3f6f62017-12-08 21:04:01 +0800756struct sctp_chunk *sctp_make_datafrag_empty(const struct sctp_association *asoc,
Xin Long327c0da2017-08-11 10:23:58 +0800757 const struct sctp_sndrcvinfo *sinfo,
Xin Long0c3f6f62017-12-08 21:04:01 +0800758 int len, __u8 flags, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
760 struct sctp_chunk *retval;
761 struct sctp_datahdr dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 /* We assign the TSN as LATE as possible, not here when
764 * creating the chunk.
765 */
Xin Long0c3f6f62017-12-08 21:04:01 +0800766 memset(&dp, 0, sizeof(dp));
767 dp.ppid = sinfo->sinfo_ppid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 dp.stream = htons(sinfo->sinfo_stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 /* Set the flags for an unordered send. */
Xin Long0c3f6f62017-12-08 21:04:01 +0800771 if (sinfo->sinfo_flags & SCTP_UNORDERED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 flags |= SCTP_DATA_UNORDERED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Xin Long0c3f6f62017-12-08 21:04:01 +0800774 retval = sctp_make_data(asoc, flags, sizeof(dp) + len, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (!retval)
Xin Long0c3f6f62017-12-08 21:04:01 +0800776 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
779 memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return retval;
782}
783
784/* Create a selective ackowledgement (SACK) for the given
785 * association. This reports on which TSN's we've seen to date,
786 * including duplicates and gaps.
787 */
Marcelo Ricardo Leitner47b3ba52018-04-24 18:17:34 -0300788struct sctp_chunk *sctp_make_sack(struct sctp_association *asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Xin Long327c0da2017-08-11 10:23:58 +0800790 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
Xin Long327c0da2017-08-11 10:23:58 +0800791 struct sctp_gap_ack_block gabs[SCTP_MAX_GABS];
792 __u16 num_gabs, num_dup_tsns;
793 struct sctp_transport *trans;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 struct sctp_chunk *retval;
795 struct sctp_sackhdr sack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 __u32 ctsn;
Xin Long327c0da2017-08-11 10:23:58 +0800797 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Vlad Yasevich02015182008-10-08 14:19:01 -0700799 memset(gabs, 0, sizeof(gabs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 ctsn = sctp_tsnmap_get_ctsn(map);
Daniel Borkmannbb333812013-06-28 19:49:40 +0200801
802 pr_debug("%s: sackCTSNAck sent:0x%x\n", __func__, ctsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 /* How much room is needed in the chunk? */
Vlad Yasevich02015182008-10-08 14:19:01 -0700805 num_gabs = sctp_tsnmap_num_gabs(map, gabs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 num_dup_tsns = sctp_tsnmap_num_dups(map);
807
808 /* Initialize the SACK header. */
809 sack.cum_tsn_ack = htonl(ctsn);
810 sack.a_rwnd = htonl(asoc->a_rwnd);
811 sack.num_gap_ack_blocks = htons(num_gabs);
812 sack.num_dup_tsns = htons(num_dup_tsns);
813
814 len = sizeof(sack)
815 + sizeof(struct sctp_gap_ack_block) * num_gabs
816 + sizeof(__u32) * num_dup_tsns;
817
818 /* Create the chunk. */
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -0300819 retval = sctp_make_control(asoc, SCTP_CID_SACK, 0, len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (!retval)
821 goto nodata;
822
823 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
824 *
825 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
826 * HEARTBEAT ACK, etc.) to the same destination transport
827 * address from which it received the DATA or control chunk to
828 * which it is replying. This rule should also be followed if
829 * the endpoint is bundling DATA chunks together with the
830 * reply chunk.
831 *
832 * However, when acknowledging multiple DATA chunks received
833 * in packets from different source addresses in a single
834 * SACK, the SACK chunk may be transmitted to one of the
835 * destination transport addresses from which the DATA or
836 * control chunks being acknowledged were received.
837 *
838 * [BUG: We do not implement the following paragraph.
839 * Perhaps we should remember the last transport we used for a
840 * SACK and avoid that (if possible) if we have seen any
841 * duplicates. --piggy]
842 *
843 * When a receiver of a duplicate DATA chunk sends a SACK to a
844 * multi- homed endpoint it MAY be beneficial to vary the
845 * destination address and not use the source address of the
846 * DATA chunk. The reason being that receiving a duplicate
847 * from a multi-homed endpoint might indicate that the return
848 * path (as specified in the source address of the DATA chunk)
849 * for the SACK is broken.
850 *
851 * [Send to the address from which we last received a DATA chunk.]
852 */
853 retval->transport = asoc->peer.last_data_from;
854
855 retval->subh.sack_hdr =
856 sctp_addto_chunk(retval, sizeof(sack), &sack);
857
858 /* Add the gap ack block information. */
859 if (num_gabs)
860 sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
Vlad Yasevich02015182008-10-08 14:19:01 -0700861 gabs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 /* Add the duplicate TSN information. */
Michele Baldessari196d6752012-12-01 04:49:42 +0000864 if (num_dup_tsns) {
Marcelo Ricardo Leitner47b3ba52018-04-24 18:17:34 -0300865 asoc->stats.idupchunks += num_dup_tsns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
867 sctp_tsnmap_get_dups(map));
Michele Baldessari196d6752012-12-01 04:49:42 +0000868 }
Neil Horman42448542012-06-30 03:04:26 +0000869 /* Once we have a sack generated, check to see what our sack
870 * generation is, if its 0, reset the transports to 0, and reset
871 * the association generation to 1
872 *
873 * The idea is that zero is never used as a valid generation for the
874 * association so no transport will match after a wrap event like this,
875 * Until the next sack
876 */
Marcelo Ricardo Leitner47b3ba52018-04-24 18:17:34 -0300877 if (++asoc->peer.sack_generation == 0) {
Neil Horman42448542012-06-30 03:04:26 +0000878 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
879 transports)
880 trans->sack_generation = 0;
Marcelo Ricardo Leitner47b3ba52018-04-24 18:17:34 -0300881 asoc->peer.sack_generation = 1;
Neil Horman42448542012-06-30 03:04:26 +0000882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883nodata:
884 return retval;
885}
886
887/* Make a SHUTDOWN chunk. */
888struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
889 const struct sctp_chunk *chunk)
890{
Xin Longe61e4052017-08-03 15:42:09 +0800891 struct sctp_shutdownhdr shut;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 struct sctp_chunk *retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 __u32 ctsn;
894
895 ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
896 shut.cum_tsn_ack = htonl(ctsn);
897
Vlad Yasevich072017b2013-08-09 22:05:36 -0400898 retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN, 0,
Xin Longe61e4052017-08-03 15:42:09 +0800899 sizeof(shut), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (!retval)
901 goto nodata;
902
903 retval->subh.shutdown_hdr =
904 sctp_addto_chunk(retval, sizeof(shut), &shut);
905
906 if (chunk)
907 retval->transport = chunk->transport;
908nodata:
909 return retval;
910}
911
912struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
Xin Long327c0da2017-08-11 10:23:58 +0800913 const struct sctp_chunk *chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
915 struct sctp_chunk *retval;
916
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -0300917 retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0,
918 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
921 *
922 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
923 * HEARTBEAT ACK, * etc.) to the same destination transport
924 * address from which it * received the DATA or control chunk
925 * to which it is replying.
926 *
927 * [ACK back to where the SHUTDOWN came from.]
928 */
929 if (retval && chunk)
930 retval->transport = chunk->transport;
931
932 return retval;
933}
934
935struct sctp_chunk *sctp_make_shutdown_complete(
Xin Long327c0da2017-08-11 10:23:58 +0800936 const struct sctp_association *asoc,
937 const struct sctp_chunk *chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 struct sctp_chunk *retval;
940 __u8 flags = 0;
941
Jerome Forissier047a2422005-04-28 11:58:43 -0700942 /* Set the T-bit if we have no association (vtag will be
943 * reflected)
944 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
946
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -0300947 retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags,
948 0, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
951 *
952 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
953 * HEARTBEAT ACK, * etc.) to the same destination transport
954 * address from which it * received the DATA or control chunk
955 * to which it is replying.
956 *
957 * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
958 * came from.]
959 */
960 if (retval && chunk)
961 retval->transport = chunk->transport;
962
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900963 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
966/* Create an ABORT. Note that we set the T bit if we have no
Jerome Forissier047a2422005-04-28 11:58:43 -0700967 * association, except when responding to an INIT (sctpimpguide 2.41).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 */
969struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
Xin Long327c0da2017-08-11 10:23:58 +0800970 const struct sctp_chunk *chunk,
971 const size_t hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
973 struct sctp_chunk *retval;
974 __u8 flags = 0;
975
Jerome Forissier047a2422005-04-28 11:58:43 -0700976 /* Set the T-bit if we have no association and 'chunk' is not
977 * an INIT (vtag will be reflected).
978 */
979 if (!asoc) {
980 if (chunk && chunk->chunk_hdr &&
981 chunk->chunk_hdr->type == SCTP_CID_INIT)
982 flags = 0;
983 else
984 flags = SCTP_CHUNK_FLAG_T;
985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -0300987 retval = sctp_make_control(asoc, SCTP_CID_ABORT, flags, hint,
988 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
991 *
992 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
993 * HEARTBEAT ACK, * etc.) to the same destination transport
994 * address from which it * received the DATA or control chunk
995 * to which it is replying.
996 *
997 * [ABORT back to where the offender came from.]
998 */
999 if (retval && chunk)
1000 retval->transport = chunk->transport;
1001
1002 return retval;
1003}
1004
1005/* Helper to create ABORT with a NO_USER_DATA error. */
1006struct sctp_chunk *sctp_make_abort_no_data(
Xin Long327c0da2017-08-11 10:23:58 +08001007 const struct sctp_association *asoc,
1008 const struct sctp_chunk *chunk,
1009 __u32 tsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
1011 struct sctp_chunk *retval;
Al Viro9f81bcd2006-11-20 17:26:34 -08001012 __be32 payload;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Xin Longd8238d92017-08-03 15:42:11 +08001014 retval = sctp_make_abort(asoc, chunk,
1015 sizeof(struct sctp_errhdr) + sizeof(tsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 if (!retval)
1018 goto no_mem;
1019
1020 /* Put the tsn back into network byte order. */
1021 payload = htonl(tsn);
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001022 sctp_init_cause(retval, SCTP_ERROR_NO_DATA, sizeof(payload));
1023 sctp_addto_chunk(retval, sizeof(payload), (const void *)&payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1026 *
1027 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1028 * HEARTBEAT ACK, * etc.) to the same destination transport
1029 * address from which it * received the DATA or control chunk
1030 * to which it is replying.
1031 *
1032 * [ABORT back to where the offender came from.]
1033 */
1034 if (chunk)
1035 retval->transport = chunk->transport;
1036
1037no_mem:
1038 return retval;
1039}
1040
1041/* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error. */
1042struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
Al Viro6ce8e9c2014-04-06 21:25:44 -04001043 struct msghdr *msg,
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001044 size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
1046 struct sctp_chunk *retval;
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001047 void *payload = NULL;
1048 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Xin Longd8238d92017-08-03 15:42:11 +08001050 retval = sctp_make_abort(asoc, NULL,
1051 sizeof(struct sctp_errhdr) + paylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (!retval)
1053 goto err_chunk;
1054
1055 if (paylen) {
1056 /* Put the msg_iov together into payload. */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001057 payload = kmalloc(paylen, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (!payload)
1059 goto err_payload;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Al Viro6ce8e9c2014-04-06 21:25:44 -04001061 err = memcpy_from_msg(payload, msg, paylen);
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07001062 if (err < 0)
1063 goto err_copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001066 sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, paylen);
1067 sctp_addto_chunk(retval, paylen, payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 if (paylen)
1070 kfree(payload);
1071
1072 return retval;
1073
1074err_copy:
1075 kfree(payload);
1076err_payload:
1077 sctp_chunk_free(retval);
1078 retval = NULL;
1079err_chunk:
1080 return retval;
1081}
1082
Adrian Bunk5c94bf82007-09-12 15:16:21 +02001083/* Append bytes to the end of a parameter. Will panic if chunk is not big
1084 * enough.
1085 */
1086static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
1087 const void *data)
1088{
Adrian Bunk5c94bf82007-09-12 15:16:21 +02001089 int chunklen = ntohs(chunk->chunk_hdr->length);
Xin Long327c0da2017-08-11 10:23:58 +08001090 void *target;
Adrian Bunk5c94bf82007-09-12 15:16:21 +02001091
1092 target = skb_put(chunk->skb, len);
1093
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001094 if (data)
1095 memcpy(target, data, len);
1096 else
1097 memset(target, 0, len);
Adrian Bunk5c94bf82007-09-12 15:16:21 +02001098
1099 /* Adjust the chunk length field. */
1100 chunk->chunk_hdr->length = htons(chunklen + len);
1101 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1102
1103 return target;
1104}
1105
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001106/* Make an ABORT chunk with a PROTOCOL VIOLATION cause code. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107struct sctp_chunk *sctp_make_abort_violation(
Xin Long327c0da2017-08-11 10:23:58 +08001108 const struct sctp_association *asoc,
1109 const struct sctp_chunk *chunk,
1110 const __u8 *payload,
1111 const size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
1113 struct sctp_chunk *retval;
1114 struct sctp_paramhdr phdr;
1115
Xin Longd8238d92017-08-03 15:42:11 +08001116 retval = sctp_make_abort(asoc, chunk, sizeof(struct sctp_errhdr) +
1117 paylen + sizeof(phdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (!retval)
1119 goto end;
1120
Xin Long3c918702017-06-30 11:52:16 +08001121 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, paylen +
1122 sizeof(phdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 phdr.type = htons(chunk->chunk_hdr->type);
1125 phdr.length = chunk->chunk_hdr->length;
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001126 sctp_addto_chunk(retval, paylen, payload);
Xin Long3c918702017-06-30 11:52:16 +08001127 sctp_addto_param(retval, sizeof(phdr), &phdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129end:
1130 return retval;
1131}
1132
Wei Yongjunba016672008-09-30 05:32:24 -07001133struct sctp_chunk *sctp_make_violation_paramlen(
Xin Long327c0da2017-08-11 10:23:58 +08001134 const struct sctp_association *asoc,
1135 const struct sctp_chunk *chunk,
1136 struct sctp_paramhdr *param)
Wei Yongjunba016672008-09-30 05:32:24 -07001137{
Wei Yongjunba016672008-09-30 05:32:24 -07001138 static const char error[] = "The following parameter had invalid length:";
Xin Longd8238d92017-08-03 15:42:11 +08001139 size_t payload_len = sizeof(error) + sizeof(struct sctp_errhdr) +
Xin Long3c918702017-06-30 11:52:16 +08001140 sizeof(*param);
Xin Long327c0da2017-08-11 10:23:58 +08001141 struct sctp_chunk *retval;
Wei Yongjunba016672008-09-30 05:32:24 -07001142
1143 retval = sctp_make_abort(asoc, chunk, payload_len);
1144 if (!retval)
1145 goto nodata;
1146
1147 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION,
Xin Long3c918702017-06-30 11:52:16 +08001148 sizeof(error) + sizeof(*param));
Wei Yongjunba016672008-09-30 05:32:24 -07001149 sctp_addto_chunk(retval, sizeof(error), error);
Xin Long3c918702017-06-30 11:52:16 +08001150 sctp_addto_param(retval, sizeof(*param), param);
Wei Yongjunba016672008-09-30 05:32:24 -07001151
1152nodata:
1153 return retval;
1154}
1155
Neil Hormande4594a2012-11-20 10:14:30 +00001156struct sctp_chunk *sctp_make_violation_max_retrans(
Xin Long327c0da2017-08-11 10:23:58 +08001157 const struct sctp_association *asoc,
1158 const struct sctp_chunk *chunk)
Neil Hormande4594a2012-11-20 10:14:30 +00001159{
Neil Hormande4594a2012-11-20 10:14:30 +00001160 static const char error[] = "Association exceeded its max_retans count";
Xin Longd8238d92017-08-03 15:42:11 +08001161 size_t payload_len = sizeof(error) + sizeof(struct sctp_errhdr);
Xin Long327c0da2017-08-11 10:23:58 +08001162 struct sctp_chunk *retval;
Neil Hormande4594a2012-11-20 10:14:30 +00001163
1164 retval = sctp_make_abort(asoc, chunk, payload_len);
1165 if (!retval)
1166 goto nodata;
1167
1168 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, sizeof(error));
1169 sctp_addto_chunk(retval, sizeof(error), error);
1170
1171nodata:
1172 return retval;
1173}
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175/* Make a HEARTBEAT chunk. */
1176struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
Xin Longedf903f2017-08-11 10:23:46 +08001177 const struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Xin Longedf903f2017-08-11 10:23:46 +08001179 struct sctp_sender_hb_info hbinfo;
Wei Yongjun92c73af2011-04-19 21:31:47 +00001180 struct sctp_chunk *retval;
Wei Yongjun92c73af2011-04-19 21:31:47 +00001181
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -03001182 retval = sctp_make_control(asoc, SCTP_CID_HEARTBEAT, 0,
1183 sizeof(hbinfo), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 if (!retval)
1186 goto nodata;
1187
Wei Yongjun92c73af2011-04-19 21:31:47 +00001188 hbinfo.param_hdr.type = SCTP_PARAM_HEARTBEAT_INFO;
Xin Longedf903f2017-08-11 10:23:46 +08001189 hbinfo.param_hdr.length = htons(sizeof(hbinfo));
Wei Yongjun92c73af2011-04-19 21:31:47 +00001190 hbinfo.daddr = transport->ipaddr;
1191 hbinfo.sent_at = jiffies;
1192 hbinfo.hb_nonce = transport->hb_nonce;
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 /* Cast away the 'const', as this is just telling the chunk
1195 * what transport it belongs to.
1196 */
1197 retval->transport = (struct sctp_transport *) transport;
Wei Yongjun92c73af2011-04-19 21:31:47 +00001198 retval->subh.hbs_hdr = sctp_addto_chunk(retval, sizeof(hbinfo),
1199 &hbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201nodata:
1202 return retval;
1203}
1204
1205struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
Xin Long327c0da2017-08-11 10:23:58 +08001206 const struct sctp_chunk *chunk,
1207 const void *payload,
1208 const size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
1210 struct sctp_chunk *retval;
1211
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -03001212 retval = sctp_make_control(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen,
1213 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (!retval)
1215 goto nodata;
1216
1217 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
1218
1219 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1220 *
1221 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1222 * HEARTBEAT ACK, * etc.) to the same destination transport
1223 * address from which it * received the DATA or control chunk
1224 * to which it is replying.
1225 *
1226 * [HBACK back to where the HEARTBEAT came from.]
1227 */
1228 if (chunk)
1229 retval->transport = chunk->transport;
1230
1231nodata:
1232 return retval;
1233}
1234
1235/* Create an Operation Error chunk with the specified space reserved.
1236 * This routine can be used for containing multiple causes in the chunk.
1237 */
1238static struct sctp_chunk *sctp_make_op_error_space(
Xin Long327c0da2017-08-11 10:23:58 +08001239 const struct sctp_association *asoc,
1240 const struct sctp_chunk *chunk,
1241 size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
1243 struct sctp_chunk *retval;
1244
Vlad Yasevich072017b2013-08-09 22:05:36 -04001245 retval = sctp_make_control(asoc, SCTP_CID_ERROR, 0,
Xin Longd8238d92017-08-03 15:42:11 +08001246 sizeof(struct sctp_errhdr) + size,
1247 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if (!retval)
1249 goto nodata;
1250
1251 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
1252 *
1253 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
1254 * HEARTBEAT ACK, etc.) to the same destination transport
1255 * address from which it received the DATA or control chunk
1256 * to which it is replying.
1257 *
1258 */
1259 if (chunk)
1260 retval->transport = chunk->transport;
1261
1262nodata:
1263 return retval;
1264}
1265
Neil Horman5fa782c2010-04-28 10:30:59 +00001266/* Create an Operation Error chunk of a fixed size,
1267 * specifically, max(asoc->pathmtu, SCTP_DEFAULT_MAXSEGMENT)
1268 * This is a helper function to allocate an error chunk for
1269 * for those invalid parameter codes in which we may not want
Jorrit Schippersd82603c2012-12-27 17:33:02 +01001270 * to report all the errors, if the incoming chunk is large
Neil Horman5fa782c2010-04-28 10:30:59 +00001271 */
1272static inline struct sctp_chunk *sctp_make_op_error_fixed(
Xin Long327c0da2017-08-11 10:23:58 +08001273 const struct sctp_association *asoc,
1274 const struct sctp_chunk *chunk)
Neil Horman5fa782c2010-04-28 10:30:59 +00001275{
1276 size_t size = asoc ? asoc->pathmtu : 0;
1277
1278 if (!size)
1279 size = SCTP_DEFAULT_MAXSEGMENT;
1280
1281 return sctp_make_op_error_space(asoc, chunk, size);
1282}
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284/* Create an Operation Error chunk. */
1285struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
Xin Long327c0da2017-08-11 10:23:58 +08001286 const struct sctp_chunk *chunk,
1287 __be16 cause_code, const void *payload,
1288 size_t paylen, size_t reserve_tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
1290 struct sctp_chunk *retval;
1291
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001292 retval = sctp_make_op_error_space(asoc, chunk, paylen + reserve_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (!retval)
1294 goto nodata;
1295
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001296 sctp_init_cause(retval, cause_code, paylen + reserve_tail);
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001297 sctp_addto_chunk(retval, paylen, payload);
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001298 if (reserve_tail)
1299 sctp_addto_param(retval, reserve_tail, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301nodata:
1302 return retval;
1303}
1304
Xin Long1b1e0bc2018-03-14 19:05:30 +08001305struct sctp_chunk *sctp_make_auth(const struct sctp_association *asoc,
1306 __u16 key_id)
Vlad Yasevich4cd57c82007-09-16 19:32:45 -07001307{
Vlad Yasevich4cd57c82007-09-16 19:32:45 -07001308 struct sctp_authhdr auth_hdr;
Xin Long327c0da2017-08-11 10:23:58 +08001309 struct sctp_hmac *hmac_desc;
1310 struct sctp_chunk *retval;
Vlad Yasevich4cd57c82007-09-16 19:32:45 -07001311
1312 /* Get the first hmac that the peer told us to use */
1313 hmac_desc = sctp_auth_asoc_get_hmac(asoc);
1314 if (unlikely(!hmac_desc))
1315 return NULL;
1316
Vlad Yasevich072017b2013-08-09 22:05:36 -04001317 retval = sctp_make_control(asoc, SCTP_CID_AUTH, 0,
Xin Long96f7ef42017-08-03 15:42:21 +08001318 hmac_desc->hmac_len + sizeof(auth_hdr),
1319 GFP_ATOMIC);
Vlad Yasevich4cd57c82007-09-16 19:32:45 -07001320 if (!retval)
1321 return NULL;
1322
1323 auth_hdr.hmac_id = htons(hmac_desc->hmac_id);
Xin Long1b1e0bc2018-03-14 19:05:30 +08001324 auth_hdr.shkey_id = htons(key_id);
Vlad Yasevich4cd57c82007-09-16 19:32:45 -07001325
Xin Long96f7ef42017-08-03 15:42:21 +08001326 retval->subh.auth_hdr = sctp_addto_chunk(retval, sizeof(auth_hdr),
1327 &auth_hdr);
Vlad Yasevich4cd57c82007-09-16 19:32:45 -07001328
Marcelo Ricardo Leitner594831a2018-01-11 14:22:07 -02001329 skb_put_zero(retval->skb, hmac_desc->hmac_len);
Vlad Yasevich4cd57c82007-09-16 19:32:45 -07001330
1331 /* Adjust the chunk header to include the empty MAC */
1332 retval->chunk_hdr->length =
1333 htons(ntohs(retval->chunk_hdr->length) + hmac_desc->hmac_len);
1334 retval->chunk_end = skb_tail_pointer(retval->skb);
1335
1336 return retval;
1337}
1338
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340/********************************************************************
1341 * 2nd Level Abstractions
1342 ********************************************************************/
1343
1344/* Turn an skb into a chunk.
1345 * FIXME: Eventually move the structure directly inside the skb->cb[].
wangweidong3dc0a542013-10-26 16:06:31 +08001346 *
1347 * sctpimpguide-05.txt Section 2.8.2
1348 * M1) Each time a new DATA chunk is transmitted
1349 * set the 'TSN.Missing.Report' count for that TSN to 0. The
1350 * 'TSN.Missing.Report' count will be used to determine missing chunks
1351 * and when to fast retransmit.
1352 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 */
1354struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
Xin Long327c0da2017-08-11 10:23:58 +08001355 const struct sctp_association *asoc,
1356 struct sock *sk, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
1358 struct sctp_chunk *retval;
1359
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -03001360 retval = kmem_cache_zalloc(sctp_chunk_cachep, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 if (!retval)
1363 goto nodata;
Daniel Borkmannbb333812013-06-28 19:49:40 +02001364 if (!sk)
1365 pr_debug("%s: chunkifying skb:%p w/o an sk\n", __func__, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
David S. Miller79af02c2005-07-08 21:47:49 -07001367 INIT_LIST_HEAD(&retval->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 retval->skb = skb;
1369 retval->asoc = (struct sctp_association *)asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 retval->singleton = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Neil Hormanc226ef92008-07-25 12:44:09 -04001372 retval->fast_retransmit = SCTP_CAN_FRTX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 /* Polish the bead hole. */
1375 INIT_LIST_HEAD(&retval->transmitted_list);
1376 INIT_LIST_HEAD(&retval->frag_list);
1377 SCTP_DBG_OBJCNT_INC(chunk);
Reshetova, Elenae7f02792017-07-04 15:53:26 +03001378 refcount_set(&retval->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380nodata:
1381 return retval;
1382}
1383
1384/* Set chunk->source and dest based on the IP header in chunk->skb. */
1385void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
1386 union sctp_addr *dest)
1387{
Al Virof235fca2006-11-20 17:09:01 -08001388 memcpy(&chunk->source, src, sizeof(union sctp_addr));
Al Viro16b0a032006-11-20 17:13:38 -08001389 memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
1391
1392/* Extract the source address from a chunk. */
1393const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
1394{
1395 /* If we have a known transport, use that. */
1396 if (chunk->transport) {
Al Viro6a1e5f32006-11-20 17:12:25 -08001397 return &chunk->transport->ipaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 } else {
1399 /* Otherwise, extract it from the IP header. */
Al Viro6a1e5f32006-11-20 17:12:25 -08001400 return &chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
1402}
1403
1404/* Create a new chunk, setting the type and flags headers from the
1405 * arguments, reserving enough space for a 'paylen' byte payload.
1406 */
Vlad Yasevich072017b2013-08-09 22:05:36 -04001407static struct sctp_chunk *_sctp_make_chunk(const struct sctp_association *asoc,
Xin Long327c0da2017-08-11 10:23:58 +08001408 __u8 type, __u8 flags, int paylen,
1409 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Xin Long922dbc52017-06-30 11:52:13 +08001411 struct sctp_chunkhdr *chunk_hdr;
Xin Long327c0da2017-08-11 10:23:58 +08001412 struct sctp_chunk *retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 struct sk_buff *skb;
1414 struct sock *sk;
Alexey Kodanev07f2c7a2018-02-09 17:35:23 +03001415 int chunklen;
1416
1417 chunklen = SCTP_PAD4(sizeof(*chunk_hdr) + paylen);
1418 if (chunklen > SCTP_MAX_CHUNK_LEN)
1419 goto nodata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 /* No need to allocate LL here, as this is only a chunk. */
Alexey Kodanev07f2c7a2018-02-09 17:35:23 +03001422 skb = alloc_skb(chunklen, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (!skb)
1424 goto nodata;
1425
1426 /* Make room for the chunk header. */
Xin Long922dbc52017-06-30 11:52:13 +08001427 chunk_hdr = (struct sctp_chunkhdr *)skb_put(skb, sizeof(*chunk_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 chunk_hdr->type = type;
1429 chunk_hdr->flags = flags;
Xin Long922dbc52017-06-30 11:52:13 +08001430 chunk_hdr->length = htons(sizeof(*chunk_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 sk = asoc ? asoc->base.sk : NULL;
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -03001433 retval = sctp_chunkify(skb, asoc, sk, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if (!retval) {
1435 kfree_skb(skb);
1436 goto nodata;
1437 }
1438
1439 retval->chunk_hdr = chunk_hdr;
Xin Long922dbc52017-06-30 11:52:13 +08001440 retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(*chunk_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Vlad Yasevich4cd57c82007-09-16 19:32:45 -07001442 /* Determine if the chunk needs to be authenticated */
1443 if (sctp_auth_send_cid(type, asoc))
1444 retval->auth = 1;
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return retval;
1447nodata:
1448 return NULL;
1449}
1450
Vlad Yasevich072017b2013-08-09 22:05:36 -04001451static struct sctp_chunk *sctp_make_data(const struct sctp_association *asoc,
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -03001452 __u8 flags, int paylen, gfp_t gfp)
Vlad Yasevich072017b2013-08-09 22:05:36 -04001453{
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -03001454 return _sctp_make_chunk(asoc, SCTP_CID_DATA, flags, paylen, gfp);
Vlad Yasevich072017b2013-08-09 22:05:36 -04001455}
1456
Xin Longad05a7a2017-12-08 21:04:00 +08001457struct sctp_chunk *sctp_make_idata(const struct sctp_association *asoc,
1458 __u8 flags, int paylen, gfp_t gfp)
1459{
1460 return _sctp_make_chunk(asoc, SCTP_CID_I_DATA, flags, paylen, gfp);
1461}
1462
Vlad Yasevich072017b2013-08-09 22:05:36 -04001463static struct sctp_chunk *sctp_make_control(const struct sctp_association *asoc,
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -03001464 __u8 type, __u8 flags, int paylen,
1465 gfp_t gfp)
Vlad Yasevich072017b2013-08-09 22:05:36 -04001466{
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -03001467 struct sctp_chunk *chunk;
Vlad Yasevich072017b2013-08-09 22:05:36 -04001468
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -03001469 chunk = _sctp_make_chunk(asoc, type, flags, paylen, gfp);
Vlad Yasevich072017b2013-08-09 22:05:36 -04001470 if (chunk)
1471 sctp_control_set_owner_w(chunk);
1472
1473 return chunk;
1474}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476/* Release the memory occupied by a chunk. */
1477static void sctp_chunk_destroy(struct sctp_chunk *chunk)
1478{
Vlad Yasevicha08de642007-12-20 14:11:47 -08001479 BUG_ON(!list_empty(&chunk->list));
1480 list_del_init(&chunk->transmitted_list);
1481
Daniel Borkmannc4856582014-03-04 16:35:51 +01001482 consume_skb(chunk->skb);
1483 consume_skb(chunk->auth_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 SCTP_DBG_OBJCNT_DEC(chunk);
1486 kmem_cache_free(sctp_chunk_cachep, chunk);
1487}
1488
1489/* Possibly, free the chunk. */
1490void sctp_chunk_free(struct sctp_chunk *chunk)
1491{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 /* Release our reference on the message tracker. */
1493 if (chunk->msg)
1494 sctp_datamsg_put(chunk->msg);
1495
1496 sctp_chunk_put(chunk);
1497}
1498
1499/* Grab a reference to the chunk. */
1500void sctp_chunk_hold(struct sctp_chunk *ch)
1501{
Reshetova, Elenae7f02792017-07-04 15:53:26 +03001502 refcount_inc(&ch->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503}
1504
1505/* Release a reference to the chunk. */
1506void sctp_chunk_put(struct sctp_chunk *ch)
1507{
Reshetova, Elenae7f02792017-07-04 15:53:26 +03001508 if (refcount_dec_and_test(&ch->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 sctp_chunk_destroy(ch);
1510}
1511
1512/* Append bytes to the end of a chunk. Will panic if chunk is not big
1513 * enough.
1514 */
1515void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
1516{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 int chunklen = ntohs(chunk->chunk_hdr->length);
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -03001518 int padlen = SCTP_PAD4(chunklen) - chunklen;
Xin Long327c0da2017-08-11 10:23:58 +08001519 void *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
yuan linyub952f4d2017-06-18 22:52:04 +08001521 skb_put_zero(chunk->skb, padlen);
Johannes Berg59ae1d12017-06-16 14:29:20 +02001522 target = skb_put_data(chunk->skb, data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 /* Adjust the chunk length field. */
1525 chunk->chunk_hdr->length = htons(chunklen + padlen + len);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001526 chunk->chunk_end = skb_tail_pointer(chunk->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528 return target;
1529}
1530
Neil Horman5fa782c2010-04-28 10:30:59 +00001531/* Append bytes to the end of a chunk. Returns NULL if there isn't sufficient
1532 * space in the chunk
1533 */
stephen hemminger6daaf0d2014-01-09 22:31:11 -08001534static void *sctp_addto_chunk_fixed(struct sctp_chunk *chunk,
1535 int len, const void *data)
Neil Horman5fa782c2010-04-28 10:30:59 +00001536{
Wei Yongjun2e3219b2010-05-17 22:51:58 -07001537 if (skb_tailroom(chunk->skb) >= len)
Neil Horman5fa782c2010-04-28 10:30:59 +00001538 return sctp_addto_chunk(chunk, len, data);
1539 else
1540 return NULL;
1541}
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543/* Append bytes from user space to the end of a chunk. Will panic if
1544 * chunk is not big enough.
1545 * Returns a kernel err value.
1546 */
Al Viroe0eb0932014-11-15 01:11:23 -05001547int sctp_user_addto_chunk(struct sctp_chunk *chunk, int len,
1548 struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
Al Viroe0eb0932014-11-15 01:11:23 -05001550 void *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552 /* Make room in chunk for data. */
1553 target = skb_put(chunk->skb, len);
1554
1555 /* Copy data (whole iovec) into chunk */
Al Viro3b6d4db2017-02-17 23:17:41 -05001556 if (!copy_from_iter_full(target, len, from))
Al Viroe0eb0932014-11-15 01:11:23 -05001557 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 /* Adjust the chunk length field. */
1560 chunk->chunk_hdr->length =
1561 htons(ntohs(chunk->chunk_hdr->length) + len);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001562 chunk->chunk_end = skb_tail_pointer(chunk->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Al Viroe0eb0932014-11-15 01:11:23 -05001564 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
1567/* Helper function to assign a TSN if needed. This assumes that both
1568 * the data_hdr and association have already been assigned.
1569 */
1570void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
1571{
Vlad Yasevichab3e5e72007-08-02 16:51:42 -04001572 struct sctp_stream *stream;
Xin Long327c0da2017-08-11 10:23:58 +08001573 struct sctp_chunk *lchunk;
1574 struct sctp_datamsg *msg;
1575 __u16 ssn, sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 if (chunk->has_ssn)
1578 return;
1579
Vlad Yasevichab3e5e72007-08-02 16:51:42 -04001580 /* All fragments will be on the same stream */
1581 sid = ntohs(chunk->subh.data_hdr->stream);
Xin Longcee360a2017-05-31 16:36:31 +08001582 stream = &chunk->asoc->stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Vlad Yasevichab3e5e72007-08-02 16:51:42 -04001584 /* Now assign the sequence number to the entire message.
1585 * All fragments must have the same stream sequence number.
1586 */
1587 msg = chunk->msg;
1588 list_for_each_entry(lchunk, &msg->chunks, frag_list) {
1589 if (lchunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
1590 ssn = 0;
1591 } else {
1592 if (lchunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
Xin Longa8386312017-01-06 22:18:33 +08001593 ssn = sctp_ssn_next(stream, out, sid);
Vlad Yasevichab3e5e72007-08-02 16:51:42 -04001594 else
Xin Longa8386312017-01-06 22:18:33 +08001595 ssn = sctp_ssn_peek(stream, out, sid);
Vlad Yasevichab3e5e72007-08-02 16:51:42 -04001596 }
1597
1598 lchunk->subh.data_hdr->ssn = htons(ssn);
1599 lchunk->has_ssn = 1;
1600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601}
1602
1603/* Helper function to assign a TSN if needed. This assumes that both
1604 * the data_hdr and association have already been assigned.
1605 */
1606void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
1607{
1608 if (!chunk->has_tsn) {
1609 /* This is the last possible instant to
1610 * assign a TSN.
1611 */
1612 chunk->subh.data_hdr->tsn =
1613 htonl(sctp_association_get_next_tsn(chunk->asoc));
1614 chunk->has_tsn = 1;
1615 }
1616}
1617
1618/* Create a CLOSED association to use with an incoming packet. */
1619struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
Xin Long327c0da2017-08-11 10:23:58 +08001620 struct sctp_chunk *chunk,
1621 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
1623 struct sctp_association *asoc;
Xin Long1c662012017-08-05 19:59:54 +08001624 enum sctp_scope scope;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
1627 /* Create the bare association. */
1628 scope = sctp_scope(sctp_source(chunk));
1629 asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
1630 if (!asoc)
1631 goto nodata;
1632 asoc->temp = 1;
1633 skb = chunk->skb;
1634 /* Create an entry for the source address of the packet. */
Marcelo Ricardo Leitnere7487c82016-07-13 15:08:58 -03001635 SCTP_INPUT_CB(skb)->af->from_skb(&asoc->c.peer_addr, skb, 1);
1636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637nodata:
1638 return asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
1641/* Build a cookie representing asoc.
1642 * This INCLUDES the param header needed to put the cookie in the INIT ACK.
1643 */
Xin Longf48ef4c2017-07-23 09:34:27 +08001644static struct sctp_cookie_param *sctp_pack_cookie(
1645 const struct sctp_endpoint *ep,
1646 const struct sctp_association *asoc,
1647 const struct sctp_chunk *init_chunk,
Xin Long327c0da2017-08-11 10:23:58 +08001648 int *cookie_len, const __u8 *raw_addrs,
1649 int addrs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 struct sctp_signed_cookie *cookie;
Xin Longf48ef4c2017-07-23 09:34:27 +08001652 struct sctp_cookie_param *retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 int headersize, bodysize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Vlad Yasevich9834a2b2006-01-17 11:52:12 -08001655 /* Header size is static data prior to the actual cookie, including
1656 * any padding.
1657 */
Xin Long3c918702017-06-30 11:52:16 +08001658 headersize = sizeof(struct sctp_paramhdr) +
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001659 (sizeof(struct sctp_signed_cookie) -
Vlad Yasevich9834a2b2006-01-17 11:52:12 -08001660 sizeof(struct sctp_cookie));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 bodysize = sizeof(struct sctp_cookie)
1662 + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
1663
1664 /* Pad out the cookie to a multiple to make the signature
1665 * functions simpler to write.
1666 */
1667 if (bodysize % SCTP_COOKIE_MULTIPLE)
1668 bodysize += SCTP_COOKIE_MULTIPLE
1669 - (bodysize % SCTP_COOKIE_MULTIPLE);
1670 *cookie_len = headersize + bodysize;
1671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 /* Clear this memory since we are sending this data structure
1673 * out on the network.
1674 */
Arnaldo Carvalho de Meloaf997d82006-11-21 01:20:33 -02001675 retval = kzalloc(*cookie_len, GFP_ATOMIC);
1676 if (!retval)
1677 goto nodata;
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 cookie = (struct sctp_signed_cookie *) retval->body;
1680
1681 /* Set up the parameter header. */
1682 retval->p.type = SCTP_PARAM_STATE_COOKIE;
1683 retval->p.length = htons(*cookie_len);
1684
1685 /* Copy the cookie part of the association itself. */
1686 cookie->c = asoc->c;
1687 /* Save the raw address list length in the cookie. */
1688 cookie->c.raw_addr_list_len = addrs_len;
1689
1690 /* Remember PR-SCTP capability. */
1691 cookie->c.prsctp_capable = asoc->peer.prsctp_capable;
1692
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001693 /* Save adaptation indication in the cookie. */
1694 cookie->c.adaptation_ind = asoc->peer.adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696 /* Set an expiration time for the cookie. */
Daniel Borkmann52db8822013-06-25 18:17:27 +02001697 cookie->c.expiration = ktime_add(asoc->cookie_life,
Marcelo Ricardo Leitnercb5e1732015-12-04 15:14:03 -02001698 ktime_get_real());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
1700 /* Copy the peer's init packet. */
1701 memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
1702 ntohs(init_chunk->chunk_hdr->length));
1703
1704 /* Copy the raw local address list of the association. */
1705 memcpy((__u8 *)&cookie->c.peer_init[0] +
1706 ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
1707
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001708 if (sctp_sk(ep->base.sk)->hmac) {
Herbert Xu5821c762016-01-24 21:20:12 +08001709 SHASH_DESC_ON_STACK(desc, sctp_sk(ep->base.sk)->hmac);
1710 int err;
Herbert Xu1b489e12006-08-20 15:07:14 +10001711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 /* Sign the message. */
Herbert Xu5821c762016-01-24 21:20:12 +08001713 desc->tfm = sctp_sk(ep->base.sk)->hmac;
1714 desc->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Herbert Xu5821c762016-01-24 21:20:12 +08001716 err = crypto_shash_setkey(desc->tfm, ep->secret_key,
1717 sizeof(ep->secret_key)) ?:
1718 crypto_shash_digest(desc, (u8 *)&cookie->c, bodysize,
1719 cookie->signature);
1720 shash_desc_zero(desc);
1721 if (err)
Herbert Xu1b489e12006-08-20 15:07:14 +10001722 goto free_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 }
1724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 return retval;
Herbert Xu1b489e12006-08-20 15:07:14 +10001726
1727free_cookie:
1728 kfree(retval);
1729nodata:
1730 *cookie_len = 0;
1731 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732}
1733
1734/* Unpack the cookie from COOKIE ECHO chunk, recreating the association. */
1735struct sctp_association *sctp_unpack_cookie(
Xin Long327c0da2017-08-11 10:23:58 +08001736 const struct sctp_endpoint *ep,
1737 const struct sctp_association *asoc,
1738 struct sctp_chunk *chunk, gfp_t gfp,
1739 int *error, struct sctp_chunk **errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
1741 struct sctp_association *retval = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 int headersize, bodysize, fixed_size;
Xin Long327c0da2017-08-11 10:23:58 +08001743 struct sctp_signed_cookie *cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 struct sk_buff *skb = chunk->skb;
Xin Long327c0da2017-08-11 10:23:58 +08001745 struct sctp_cookie *bear_cookie;
1746 __u8 *digest = ep->digest;
1747 enum sctp_scope scope;
1748 unsigned int len;
Daniel Borkmann52db8822013-06-25 18:17:27 +02001749 ktime_t kt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Vlad Yasevich9834a2b2006-01-17 11:52:12 -08001751 /* Header size is static data prior to the actual cookie, including
1752 * any padding.
1753 */
Xin Long922dbc52017-06-30 11:52:13 +08001754 headersize = sizeof(struct sctp_chunkhdr) +
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001755 (sizeof(struct sctp_signed_cookie) -
Vlad Yasevich9834a2b2006-01-17 11:52:12 -08001756 sizeof(struct sctp_cookie));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
1758 fixed_size = headersize + sizeof(struct sctp_cookie);
1759
1760 /* Verify that the chunk looks like it even has a cookie.
1761 * There must be enough room for our cookie and our peer's
1762 * INIT chunk.
1763 */
1764 len = ntohs(chunk->chunk_hdr->length);
1765 if (len < fixed_size + sizeof(struct sctp_chunkhdr))
1766 goto malformed;
1767
1768 /* Verify that the cookie has been padded out. */
1769 if (bodysize % SCTP_COOKIE_MULTIPLE)
1770 goto malformed;
1771
1772 /* Process the cookie. */
1773 cookie = chunk->subh.cookie_hdr;
1774 bear_cookie = &cookie->c;
1775
1776 if (!sctp_sk(ep->base.sk)->hmac)
1777 goto no_hmac;
1778
1779 /* Check the signature. */
Herbert Xu5821c762016-01-24 21:20:12 +08001780 {
1781 SHASH_DESC_ON_STACK(desc, sctp_sk(ep->base.sk)->hmac);
1782 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Herbert Xu5821c762016-01-24 21:20:12 +08001784 desc->tfm = sctp_sk(ep->base.sk)->hmac;
1785 desc->flags = 0;
1786
1787 err = crypto_shash_setkey(desc->tfm, ep->secret_key,
1788 sizeof(ep->secret_key)) ?:
1789 crypto_shash_digest(desc, (u8 *)bear_cookie, bodysize,
1790 digest);
1791 shash_desc_zero(desc);
1792
1793 if (err) {
1794 *error = -SCTP_IERROR_NOMEM;
1795 goto fail;
1796 }
Herbert Xu1b489e12006-08-20 15:07:14 +10001797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
1799 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
Daniel Borkmann570617e2013-02-12 05:15:33 +00001800 *error = -SCTP_IERROR_BAD_SIG;
1801 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 }
1803
1804no_hmac:
1805 /* IG Section 2.35.2:
1806 * 3) Compare the port numbers and the verification tag contained
1807 * within the COOKIE ECHO chunk to the actual port numbers and the
1808 * verification tag within the SCTP common header of the received
1809 * packet. If these values do not match the packet MUST be silently
1810 * discarded,
1811 */
1812 if (ntohl(chunk->sctp_hdr->vtag) != bear_cookie->my_vtag) {
1813 *error = -SCTP_IERROR_BAD_TAG;
1814 goto fail;
1815 }
1816
Al Viro9b1dfad2006-11-20 17:09:17 -08001817 if (chunk->sctp_hdr->source != bear_cookie->peer_addr.v4.sin_port ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 ntohs(chunk->sctp_hdr->dest) != bear_cookie->my_port) {
1819 *error = -SCTP_IERROR_BAD_PORTS;
1820 goto fail;
1821 }
1822
1823 /* Check to see if the cookie is stale. If there is already
1824 * an association, there is no need to check cookie's expiration
1825 * for init collision case of lost COOKIE ACK.
Vlad Yasevichf236218b2006-09-29 17:10:03 -07001826 * If skb has been timestamped, then use the stamp, otherwise
1827 * use current time. This introduces a small possibility that
1828 * that a cookie may be considered expired, but his would only slow
1829 * down the new association establishment instead of every packet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 */
Vlad Yasevichf236218b2006-09-29 17:10:03 -07001831 if (sock_flag(ep->base.sk, SOCK_TIMESTAMP))
Daniel Borkmann52db8822013-06-25 18:17:27 +02001832 kt = skb_get_ktime(skb);
Vlad Yasevichf236218b2006-09-29 17:10:03 -07001833 else
Marcelo Ricardo Leitnercb5e1732015-12-04 15:14:03 -02001834 kt = ktime_get_real();
Vlad Yasevichf236218b2006-09-29 17:10:03 -07001835
Daniel Borkmann67cb9362014-06-11 18:19:28 +02001836 if (!asoc && ktime_before(bear_cookie->expiration, kt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 /*
1838 * Section 3.3.10.3 Stale Cookie Error (3)
1839 *
1840 * Cause of error
1841 * ---------------
1842 * Stale Cookie Error: Indicates the receipt of a valid State
1843 * Cookie that has expired.
1844 */
1845 len = ntohs(chunk->chunk_hdr->length);
1846 *errp = sctp_make_op_error_space(asoc, chunk, len);
1847 if (*errp) {
Daniel Borkmann52db8822013-06-25 18:17:27 +02001848 suseconds_t usecs = ktime_to_us(ktime_sub(kt, bear_cookie->expiration));
Al Viro34bcca22006-11-20 17:27:15 -08001849 __be32 n = htonl(usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001852 sizeof(n));
1853 sctp_addto_chunk(*errp, sizeof(n), &n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 *error = -SCTP_IERROR_STALE_COOKIE;
1855 } else
1856 *error = -SCTP_IERROR_NOMEM;
1857
1858 goto fail;
1859 }
1860
1861 /* Make a new base association. */
1862 scope = sctp_scope(sctp_source(chunk));
1863 retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
1864 if (!retval) {
1865 *error = -SCTP_IERROR_NOMEM;
1866 goto fail;
1867 }
1868
1869 /* Set up our peer's port number. */
1870 retval->peer.port = ntohs(chunk->sctp_hdr->source);
1871
1872 /* Populate the association from the cookie. */
1873 memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
1874
1875 if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
1876 GFP_ATOMIC) < 0) {
1877 *error = -SCTP_IERROR_NOMEM;
1878 goto fail;
1879 }
1880
1881 /* Also, add the destination address. */
1882 if (list_empty(&retval->base.bind_addr.address_list)) {
Vlad Yasevichf57d96b2007-12-20 14:12:24 -08001883 sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest,
Marcelo Ricardo Leitner133800d2016-03-08 10:34:28 -03001884 sizeof(chunk->dest), SCTP_ADDR_SRC,
1885 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 }
1887
1888 retval->next_tsn = retval->c.initial_tsn;
1889 retval->ctsn_ack_point = retval->next_tsn - 1;
1890 retval->addip_serial = retval->c.initial_tsn;
Xin Longcc16f002017-01-18 00:44:42 +08001891 retval->strreset_outseq = retval->c.initial_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 retval->adv_peer_ack_point = retval->ctsn_ack_point;
1893 retval->peer.prsctp_capable = retval->c.prsctp_capable;
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001894 retval->peer.adaptation_ind = retval->c.adaptation_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
1896 /* The INIT stuff will be done by the side effects. */
1897 return retval;
1898
1899fail:
1900 if (retval)
1901 sctp_association_free(retval);
1902
1903 return NULL;
1904
1905malformed:
1906 /* Yikes! The packet is either corrupt or deliberately
1907 * malformed.
1908 */
1909 *error = -SCTP_IERROR_MALFORMED;
1910 goto fail;
1911}
1912
1913/********************************************************************
1914 * 3rd Level Abstractions
1915 ********************************************************************/
1916
1917struct __sctp_missing {
Al Viro9f81bcd2006-11-20 17:26:34 -08001918 __be32 num_missing;
1919 __be16 type;
Eric Dumazetbc105022010-06-03 03:21:52 -07001920} __packed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922/*
1923 * Report a missing mandatory parameter.
1924 */
1925static int sctp_process_missing_param(const struct sctp_association *asoc,
Xin Long34b4e292017-06-30 11:52:17 +08001926 enum sctp_param paramtype,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 struct sctp_chunk *chunk,
1928 struct sctp_chunk **errp)
1929{
1930 struct __sctp_missing report;
1931 __u16 len;
1932
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -03001933 len = SCTP_PAD4(sizeof(report));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 /* Make an ERROR chunk, preparing enough room for
1936 * returning multiple unknown parameters.
1937 */
1938 if (!*errp)
1939 *errp = sctp_make_op_error_space(asoc, chunk, len);
1940
1941 if (*errp) {
1942 report.num_missing = htonl(1);
1943 report.type = paramtype;
Vlad Yasevichebdfcad2007-01-15 19:12:31 -08001944 sctp_init_cause(*errp, SCTP_ERROR_MISS_PARAM,
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001945 sizeof(report));
1946 sctp_addto_chunk(*errp, sizeof(report), &report);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 }
1948
1949 /* Stop processing this chunk. */
1950 return 0;
1951}
1952
1953/* Report an Invalid Mandatory Parameter. */
1954static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
1955 struct sctp_chunk *chunk,
1956 struct sctp_chunk **errp)
1957{
1958 /* Invalid Mandatory Parameter Error has no payload. */
1959
1960 if (!*errp)
1961 *errp = sctp_make_op_error_space(asoc, chunk, 0);
1962
1963 if (*errp)
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08001964 sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 /* Stop processing this chunk. */
1967 return 0;
1968}
1969
1970static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
1971 struct sctp_paramhdr *param,
1972 const struct sctp_chunk *chunk,
1973 struct sctp_chunk **errp)
1974{
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001975 /* This is a fatal error. Any accumulated non-fatal errors are
1976 * not reported.
1977 */
1978 if (*errp)
1979 sctp_chunk_free(*errp);
1980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 /* Create an error chunk and fill it in with our payload. */
Wei Yongjunba016672008-09-30 05:32:24 -07001982 *errp = sctp_make_violation_paramlen(asoc, chunk, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
1984 return 0;
1985}
1986
1987
1988/* Do not attempt to handle the HOST_NAME parm. However, do
1989 * send back an indicator to the peer.
1990 */
1991static int sctp_process_hn_param(const struct sctp_association *asoc,
1992 union sctp_params param,
1993 struct sctp_chunk *chunk,
1994 struct sctp_chunk **errp)
1995{
1996 __u16 len = ntohs(param.p->length);
1997
Vlad Yasevich7ab90802007-11-09 11:43:41 -05001998 /* Processing of the HOST_NAME parameter will generate an
1999 * ABORT. If we've accumulated any non-fatal errors, they
2000 * would be unrecognized parameters and we should not include
2001 * them in the ABORT.
2002 */
2003 if (*errp)
2004 sctp_chunk_free(*errp);
2005
2006 *errp = sctp_make_op_error_space(asoc, chunk, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08002008 if (*errp) {
2009 sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED, len);
2010 sctp_addto_chunk(*errp, len, param.v);
2011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 /* Stop processing this chunk. */
2014 return 0;
2015}
2016
Eric W. Biedermanf53b5b02012-08-07 07:29:08 +00002017static int sctp_verify_ext_param(struct net *net, union sctp_params param)
Vlad Yasevichd6701192007-12-20 14:13:31 -08002018{
Xin Long3c918702017-06-30 11:52:16 +08002019 __u16 num_ext = ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
Vlad Yasevichd6701192007-12-20 14:13:31 -08002020 int have_asconf = 0;
Xin Long327c0da2017-08-11 10:23:58 +08002021 int have_auth = 0;
Vlad Yasevichd6701192007-12-20 14:13:31 -08002022 int i;
2023
2024 for (i = 0; i < num_ext; i++) {
2025 switch (param.ext->chunks[i]) {
wangweidongf7010e62013-12-23 12:16:52 +08002026 case SCTP_CID_AUTH:
2027 have_auth = 1;
2028 break;
2029 case SCTP_CID_ASCONF:
2030 case SCTP_CID_ASCONF_ACK:
2031 have_asconf = 1;
2032 break;
Vlad Yasevichd6701192007-12-20 14:13:31 -08002033 }
2034 }
2035
2036 /* ADD-IP Security: The draft requires us to ABORT or ignore the
2037 * INIT/INIT-ACK if ADD-IP is listed, but AUTH is not. Do this
2038 * only if ADD-IP is turned on and we are not backward-compatible
2039 * mode.
2040 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00002041 if (net->sctp.addip_noauth)
Vlad Yasevichd6701192007-12-20 14:13:31 -08002042 return 1;
2043
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00002044 if (net->sctp.addip_enable && !have_auth && have_asconf)
Vlad Yasevichd6701192007-12-20 14:13:31 -08002045 return 0;
2046
2047 return 1;
2048}
2049
Vlad Yasevich131a47e2007-09-16 15:53:56 -07002050static void sctp_process_ext_param(struct sctp_association *asoc,
Xin Long327c0da2017-08-11 10:23:58 +08002051 union sctp_params param)
Vlad Yasevich131a47e2007-09-16 15:53:56 -07002052{
Xin Long3c918702017-06-30 11:52:16 +08002053 __u16 num_ext = ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
Xin Long327c0da2017-08-11 10:23:58 +08002054 struct net *net = sock_net(asoc->base.sk);
Vlad Yasevich131a47e2007-09-16 15:53:56 -07002055 int i;
2056
2057 for (i = 0; i < num_ext; i++) {
2058 switch (param.ext->chunks[i]) {
Xin Longc28445c2017-01-18 00:44:45 +08002059 case SCTP_CID_RECONF:
2060 if (asoc->reconf_enable &&
2061 !asoc->peer.reconf_capable)
2062 asoc->peer.reconf_capable = 1;
2063 break;
wangweidongf7010e62013-12-23 12:16:52 +08002064 case SCTP_CID_FWD_TSN:
Xin Long28aa4c22016-07-09 19:47:40 +08002065 if (asoc->prsctp_enable && !asoc->peer.prsctp_capable)
2066 asoc->peer.prsctp_capable = 1;
wangweidongf7010e62013-12-23 12:16:52 +08002067 break;
2068 case SCTP_CID_AUTH:
2069 /* if the peer reports AUTH, assume that he
2070 * supports AUTH.
2071 */
Vlad Yasevichb14878c2014-04-17 17:26:50 +02002072 if (asoc->ep->auth_enable)
wangweidongf7010e62013-12-23 12:16:52 +08002073 asoc->peer.auth_capable = 1;
2074 break;
2075 case SCTP_CID_ASCONF:
2076 case SCTP_CID_ASCONF_ACK:
2077 if (net->sctp.addip_enable)
2078 asoc->peer.asconf_capable = 1;
2079 break;
Xin Long96b120b2017-12-08 21:03:59 +08002080 case SCTP_CID_I_DATA:
2081 if (sctp_sk(asoc->base.sk)->strm_interleave)
2082 asoc->intl_enable = 1;
2083 break;
wangweidongf7010e62013-12-23 12:16:52 +08002084 default:
2085 break;
Vlad Yasevich131a47e2007-09-16 15:53:56 -07002086 }
2087 }
2088}
2089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090/* RFC 3.2.1 & the Implementers Guide 2.2.
2091 *
2092 * The Parameter Types are encoded such that the
2093 * highest-order two bits specify the action that must be
2094 * taken if the processing endpoint does not recognize the
2095 * Parameter Type.
2096 *
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002097 * 00 - Stop processing this parameter; do not process any further
2098 * parameters within this chunk
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 *
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002100 * 01 - Stop processing this parameter, do not process any further
2101 * parameters within this chunk, and report the unrecognized
2102 * parameter in an 'Unrecognized Parameter' ERROR chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 *
2104 * 10 - Skip this parameter and continue processing.
2105 *
2106 * 11 - Skip this parameter and continue processing but
2107 * report the unrecognized parameter in an
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002108 * 'Unrecognized Parameter' ERROR chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 *
2110 * Return value:
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002111 * SCTP_IERROR_NO_ERROR - continue with the chunk
2112 * SCTP_IERROR_ERROR - stop and report an error.
2113 * SCTP_IERROR_NOMEME - out of memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 */
Xin Long4785c7a2017-08-05 19:59:58 +08002115static enum sctp_ierror sctp_process_unk_param(
2116 const struct sctp_association *asoc,
2117 union sctp_params param,
2118 struct sctp_chunk *chunk,
2119 struct sctp_chunk **errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002121 int retval = SCTP_IERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
2123 switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
2124 case SCTP_PARAM_ACTION_DISCARD:
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002125 retval = SCTP_IERROR_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 break;
2127 case SCTP_PARAM_ACTION_SKIP:
2128 break;
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002129 case SCTP_PARAM_ACTION_DISCARD_ERR:
2130 retval = SCTP_IERROR_ERROR;
2131 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 case SCTP_PARAM_ACTION_SKIP_ERR:
2133 /* Make an ERROR chunk, preparing enough room for
2134 * returning multiple unknown parameters.
2135 */
2136 if (NULL == *errp)
Neil Horman5fa782c2010-04-28 10:30:59 +00002137 *errp = sctp_make_op_error_fixed(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
2139 if (*errp) {
Jiri Bohac2205a6e2011-02-17 13:12:08 +00002140 if (!sctp_init_cause_fixed(*errp, SCTP_ERROR_UNKNOWN_PARAM,
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -03002141 SCTP_PAD4(ntohs(param.p->length))))
Jiri Bohac2205a6e2011-02-17 13:12:08 +00002142 sctp_addto_chunk_fixed(*errp,
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -03002143 SCTP_PAD4(ntohs(param.p->length)),
Jiri Bohac2205a6e2011-02-17 13:12:08 +00002144 param.v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 } else {
2146 /* If there is no memory for generating the ERROR
2147 * report as specified, an ABORT will be triggered
2148 * to the peer and the association won't be
2149 * established.
2150 */
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002151 retval = SCTP_IERROR_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 break;
2154 default:
2155 break;
2156 }
2157
2158 return retval;
2159}
2160
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002161/* Verify variable length parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 * Return values:
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002163 * SCTP_IERROR_ABORT - trigger an ABORT
2164 * SCTP_IERROR_NOMEM - out of memory (abort)
2165 * SCTP_IERROR_ERROR - stop processing, trigger an ERROR
2166 * SCTP_IERROR_NO_ERROR - continue with the chunk
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 */
Xin Long4785c7a2017-08-05 19:59:58 +08002168static enum sctp_ierror sctp_verify_param(struct net *net,
2169 const struct sctp_endpoint *ep,
2170 const struct sctp_association *asoc,
2171 union sctp_params param,
2172 enum sctp_cid cid,
2173 struct sctp_chunk *chunk,
2174 struct sctp_chunk **err_chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
Wei Yongjun72da7b32008-04-12 18:39:19 -07002176 struct sctp_hmac_algo_param *hmacs;
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002177 int retval = SCTP_IERROR_NO_ERROR;
Wei Yongjun72da7b32008-04-12 18:39:19 -07002178 __u16 n_elt, id = 0;
2179 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 /* FIXME - This routine is not looking at each parameter per the
2182 * chunk type, i.e., unrecognized parameters should be further
2183 * identified based on the chunk id.
2184 */
2185
2186 switch (param.p->type) {
2187 case SCTP_PARAM_IPV4_ADDRESS:
2188 case SCTP_PARAM_IPV6_ADDRESS:
2189 case SCTP_PARAM_COOKIE_PRESERVATIVE:
2190 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2191 case SCTP_PARAM_STATE_COOKIE:
2192 case SCTP_PARAM_HEARTBEAT_INFO:
2193 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2194 case SCTP_PARAM_ECN_CAPABLE:
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002195 case SCTP_PARAM_ADAPTATION_LAYER_IND:
Vlad Yasevichd6701192007-12-20 14:13:31 -08002196 break;
2197
Vlad Yasevich131a47e2007-09-16 15:53:56 -07002198 case SCTP_PARAM_SUPPORTED_EXT:
Eric W. Biedermanf53b5b02012-08-07 07:29:08 +00002199 if (!sctp_verify_ext_param(net, param))
Vlad Yasevichd6701192007-12-20 14:13:31 -08002200 return SCTP_IERROR_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 break;
2202
Vlad Yasevichd6de3092007-12-20 14:10:00 -08002203 case SCTP_PARAM_SET_PRIMARY:
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00002204 if (net->sctp.addip_enable)
Vlad Yasevichd6de3092007-12-20 14:10:00 -08002205 break;
2206 goto fallthrough;
2207
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 case SCTP_PARAM_HOST_NAME_ADDRESS:
2209 /* Tell the peer, we won't support this param. */
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002210 sctp_process_hn_param(asoc, param, chunk, err_chunk);
2211 retval = SCTP_IERROR_ABORT;
2212 break;
Vlad Yasevich131a47e2007-09-16 15:53:56 -07002213
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 case SCTP_PARAM_FWD_TSN_SUPPORT:
Xin Long28aa4c22016-07-09 19:47:40 +08002215 if (ep->prsctp_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 break;
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002217 goto fallthrough;
2218
2219 case SCTP_PARAM_RANDOM:
Vlad Yasevichb14878c2014-04-17 17:26:50 +02002220 if (!ep->auth_enable)
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002221 goto fallthrough;
2222
2223 /* SCTP-AUTH: Secion 6.1
2224 * If the random number is not 32 byte long the association
2225 * MUST be aborted. The ABORT chunk SHOULD contain the error
2226 * cause 'Protocol Violation'.
2227 */
2228 if (SCTP_AUTH_RANDOM_LENGTH !=
Xin Long3c918702017-06-30 11:52:16 +08002229 ntohs(param.p->length) - sizeof(struct sctp_paramhdr)) {
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002230 sctp_process_inv_paramlength(asoc, param.p,
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002231 chunk, err_chunk);
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002232 retval = SCTP_IERROR_ABORT;
2233 }
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002234 break;
2235
2236 case SCTP_PARAM_CHUNKS:
Vlad Yasevichb14878c2014-04-17 17:26:50 +02002237 if (!ep->auth_enable)
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002238 goto fallthrough;
2239
2240 /* SCTP-AUTH: Section 3.2
2241 * The CHUNKS parameter MUST be included once in the INIT or
2242 * INIT-ACK chunk if the sender wants to receive authenticated
2243 * chunks. Its maximum length is 260 bytes.
2244 */
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002245 if (260 < ntohs(param.p->length)) {
2246 sctp_process_inv_paramlength(asoc, param.p,
2247 chunk, err_chunk);
2248 retval = SCTP_IERROR_ABORT;
2249 }
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002250 break;
2251
2252 case SCTP_PARAM_HMAC_ALGO:
Vlad Yasevichb14878c2014-04-17 17:26:50 +02002253 if (!ep->auth_enable)
Wei Yongjun72da7b32008-04-12 18:39:19 -07002254 goto fallthrough;
2255
2256 hmacs = (struct sctp_hmac_algo_param *)param.p;
Xin Long3c918702017-06-30 11:52:16 +08002257 n_elt = (ntohs(param.p->length) -
2258 sizeof(struct sctp_paramhdr)) >> 1;
Wei Yongjun72da7b32008-04-12 18:39:19 -07002259
2260 /* SCTP-AUTH: Section 6.1
2261 * The HMAC algorithm based on SHA-1 MUST be supported and
2262 * included in the HMAC-ALGO parameter.
2263 */
2264 for (i = 0; i < n_elt; i++) {
2265 id = ntohs(hmacs->hmac_ids[i]);
2266
2267 if (id == SCTP_AUTH_HMAC_ID_SHA1)
2268 break;
2269 }
2270
2271 if (id != SCTP_AUTH_HMAC_ID_SHA1) {
2272 sctp_process_inv_paramlength(asoc, param.p, chunk,
2273 err_chunk);
2274 retval = SCTP_IERROR_ABORT;
2275 }
2276 break;
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002277fallthrough:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 default:
Daniel Borkmannbb333812013-06-28 19:49:40 +02002279 pr_debug("%s: unrecognized param:%d for chunk:%d\n",
2280 __func__, ntohs(param.p->type), cid);
2281
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002282 retval = sctp_process_unk_param(asoc, param, chunk, err_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 break;
2284 }
2285 return retval;
2286}
2287
2288/* Verify the INIT packet before we process it. */
Vlad Yasevichb14878c2014-04-17 17:26:50 +02002289int sctp_verify_init(struct net *net, const struct sctp_endpoint *ep,
Xin Long6d85e682017-06-30 11:52:14 +08002290 const struct sctp_association *asoc, enum sctp_cid cid,
Xin Long01a992b2017-06-30 11:52:22 +08002291 struct sctp_init_chunk *peer_init,
2292 struct sctp_chunk *chunk, struct sctp_chunk **errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293{
2294 union sctp_params param;
Daniel Borkmann7613f5f2013-08-27 16:53:52 +02002295 bool has_cookie = false;
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002296 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Daniel Borkmann7613f5f2013-08-27 16:53:52 +02002298 /* Check for missing mandatory parameters. Note: Initial TSN is
2299 * also mandatory, but is not checked here since the valid range
2300 * is 0..2**32-1. RFC4960, section 3.3.3.
2301 */
2302 if (peer_init->init_hdr.num_outbound_streams == 0 ||
2303 peer_init->init_hdr.num_inbound_streams == 0 ||
2304 peer_init->init_hdr.init_tag == 0 ||
2305 ntohl(peer_init->init_hdr.a_rwnd) < SCTP_DEFAULT_MINWINDOW)
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002306 return sctp_process_inv_mandatory(asoc, chunk, errp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 sctp_walk_params(param, peer_init, init_hdr.params) {
Daniel Borkmann7613f5f2013-08-27 16:53:52 +02002309 if (param.p->type == SCTP_PARAM_STATE_COOKIE)
2310 has_cookie = true;
2311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313 /* There is a possibility that a parameter length was bad and
2314 * in that case we would have stoped walking the parameters.
2315 * The current param.p would point at the bad one.
2316 * Current consensus on the mailing list is to generate a PROTOCOL
2317 * VIOLATION error. We build the ERROR chunk here and let the normal
2318 * error handling code build and send the packet.
2319 */
wangweidong26ac8e52013-12-23 12:16:51 +08002320 if (param.v != (void *)chunk->chunk_end)
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002321 return sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 /* The only missing mandatory param possible today is
2324 * the state cookie for an INIT-ACK chunk.
2325 */
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002326 if ((SCTP_CID_INIT_ACK == cid) && !has_cookie)
2327 return sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
2328 chunk, errp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002330 /* Verify all the variable length parameters */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 sctp_walk_params(param, peer_init, init_hdr.params) {
Vlad Yasevichb14878c2014-04-17 17:26:50 +02002332 result = sctp_verify_param(net, ep, asoc, param, cid,
2333 chunk, errp);
Vlad Yasevich7ab90802007-11-09 11:43:41 -05002334 switch (result) {
wangweidongf7010e62013-12-23 12:16:52 +08002335 case SCTP_IERROR_ABORT:
2336 case SCTP_IERROR_NOMEM:
2337 return 0;
2338 case SCTP_IERROR_ERROR:
2339 return 1;
2340 case SCTP_IERROR_NO_ERROR:
2341 default:
2342 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 }
2344
2345 } /* for (loop through all parameters) */
2346
2347 return 1;
2348}
2349
2350/* Unpack the parameters in an INIT packet into an association.
2351 * Returns 0 on failure, else success.
2352 * FIXME: This is an association method.
2353 */
Wei Yongjunde6becdc02011-04-19 21:30:51 +00002354int sctp_process_init(struct sctp_association *asoc, struct sctp_chunk *chunk,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 const union sctp_addr *peer_addr,
Xin Long01a992b2017-06-30 11:52:22 +08002356 struct sctp_init_chunk *peer_init, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00002358 struct net *net = sock_net(asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 struct sctp_transport *transport;
2360 struct list_head *pos, *temp;
Xin Long327c0da2017-08-11 10:23:58 +08002361 union sctp_params param;
Wei Yongjunde6becdc02011-04-19 21:30:51 +00002362 union sctp_addr addr;
Xin Long327c0da2017-08-11 10:23:58 +08002363 struct sctp_af *af;
Wei Yongjunde6becdc02011-04-19 21:30:51 +00002364 int src_match = 0;
Xin Long327c0da2017-08-11 10:23:58 +08002365 char *cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
2367 /* We must include the address that the INIT packet came from.
2368 * This is the only address that matters for an INIT packet.
2369 * When processing a COOKIE ECHO, we retrieve the from address
2370 * of the INIT from the cookie.
2371 */
2372
2373 /* This implementation defaults to making the first transport
2374 * added as the primary transport. The source address seems to
2375 * be a a better choice than any of the embedded addresses.
2376 */
wangweidongcb3f8372013-12-23 12:16:50 +08002377 if (!sctp_assoc_add_peer(asoc, peer_addr, gfp, SCTP_ACTIVE))
Wei Yongjunde6becdc02011-04-19 21:30:51 +00002378 goto nomem;
2379
2380 if (sctp_cmp_addr_exact(sctp_source(chunk), peer_addr))
2381 src_match = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
2383 /* Process the initialization parameters. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 sctp_walk_params(param, peer_init, init_hdr.params) {
Wei Yongjunde6becdc02011-04-19 21:30:51 +00002385 if (!src_match && (param.p->type == SCTP_PARAM_IPV4_ADDRESS ||
2386 param.p->type == SCTP_PARAM_IPV6_ADDRESS)) {
2387 af = sctp_get_af_specific(param_type2af(param.p->type));
2388 af->from_addr_param(&addr, param.addr,
2389 chunk->sctp_hdr->source, 0);
2390 if (sctp_cmp_addr_exact(sctp_source(chunk), &addr))
2391 src_match = 1;
2392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
2394 if (!sctp_process_param(asoc, param, peer_addr, gfp))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002395 goto clean_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 }
2397
Wei Yongjunde6becdc02011-04-19 21:30:51 +00002398 /* source address of chunk may not match any valid address */
2399 if (!src_match)
2400 goto clean_up;
2401
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002402 /* AUTH: After processing the parameters, make sure that we
2403 * have all the required info to potentially do authentications.
2404 */
2405 if (asoc->peer.auth_capable && (!asoc->peer.peer_random ||
2406 !asoc->peer.peer_hmacs))
2407 asoc->peer.auth_capable = 0;
2408
Vlad Yasevichd6701192007-12-20 14:13:31 -08002409 /* In a non-backward compatible mode, if the peer claims
2410 * support for ADD-IP but not AUTH, the ADD-IP spec states
2411 * that we MUST ABORT the association. Section 6. The section
2412 * also give us an option to silently ignore the packet, which
2413 * is what we'll do here.
Vlad Yasevich6b2f9cb2007-09-16 19:35:39 -07002414 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00002415 if (!net->sctp.addip_noauth &&
Vlad Yasevich73d9c4f2007-10-24 17:24:26 -04002416 (asoc->peer.asconf_capable && !asoc->peer.auth_capable)) {
Vlad Yasevich6b2f9cb2007-09-16 19:35:39 -07002417 asoc->peer.addip_disabled_mask |= (SCTP_PARAM_ADD_IP |
2418 SCTP_PARAM_DEL_IP |
2419 SCTP_PARAM_SET_PRIMARY);
Vlad Yasevich88799fe2007-10-24 17:24:23 -04002420 asoc->peer.asconf_capable = 0;
Vlad Yasevichd6701192007-12-20 14:13:31 -08002421 goto clean_up;
Vlad Yasevich6b2f9cb2007-09-16 19:35:39 -07002422 }
2423
Frank Filz3f7a87d2005-06-20 13:14:57 -07002424 /* Walk list of transports, removing transports in the UNKNOWN state. */
2425 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2426 transport = list_entry(pos, struct sctp_transport, transports);
2427 if (transport->state == SCTP_UNKNOWN) {
2428 sctp_assoc_rm_peer(asoc, transport);
2429 }
2430 }
2431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 /* The fixed INIT headers are always in network byte
2433 * order.
2434 */
2435 asoc->peer.i.init_tag =
2436 ntohl(peer_init->init_hdr.init_tag);
2437 asoc->peer.i.a_rwnd =
2438 ntohl(peer_init->init_hdr.a_rwnd);
2439 asoc->peer.i.num_outbound_streams =
2440 ntohs(peer_init->init_hdr.num_outbound_streams);
2441 asoc->peer.i.num_inbound_streams =
2442 ntohs(peer_init->init_hdr.num_inbound_streams);
2443 asoc->peer.i.initial_tsn =
2444 ntohl(peer_init->init_hdr.initial_tsn);
2445
Xin Longcc16f002017-01-18 00:44:42 +08002446 asoc->strreset_inseq = asoc->peer.i.initial_tsn;
2447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 /* Apply the upper bounds for output streams based on peer's
2449 * number of inbound streams.
2450 */
2451 if (asoc->c.sinit_num_ostreams >
2452 ntohs(peer_init->init_hdr.num_inbound_streams)) {
2453 asoc->c.sinit_num_ostreams =
2454 ntohs(peer_init->init_hdr.num_inbound_streams);
2455 }
2456
2457 if (asoc->c.sinit_max_instreams >
2458 ntohs(peer_init->init_hdr.num_outbound_streams)) {
2459 asoc->c.sinit_max_instreams =
2460 ntohs(peer_init->init_hdr.num_outbound_streams);
2461 }
2462
2463 /* Copy Initiation tag from INIT to VT_peer in cookie. */
2464 asoc->c.peer_vtag = asoc->peer.i.init_tag;
2465
2466 /* Peer Rwnd : Current calculated value of the peer's rwnd. */
2467 asoc->peer.rwnd = asoc->peer.i.a_rwnd;
2468
2469 /* Copy cookie in case we need to resend COOKIE-ECHO. */
2470 cookie = asoc->peer.cookie;
2471 if (cookie) {
Arnaldo Carvalho de Meloaf997d82006-11-21 01:20:33 -02002472 asoc->peer.cookie = kmemdup(cookie, asoc->peer.cookie_len, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 if (!asoc->peer.cookie)
2474 goto clean_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
2476
2477 /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
2478 * high (for example, implementations MAY use the size of the receiver
2479 * advertised window).
2480 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07002481 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
2482 transports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 transport->ssthresh = asoc->peer.i.a_rwnd;
2484 }
2485
2486 /* Set up the TSN tracking pieces. */
Vlad Yasevich8e1ee182008-10-08 14:18:39 -07002487 if (!sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
2488 asoc->peer.i.initial_tsn, gfp))
2489 goto clean_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
2491 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
2492 *
2493 * The stream sequence number in all the streams shall start
2494 * from 0 when the association is established. Also, when the
2495 * stream sequence number reaches the value 65535 the next
2496 * stream sequence number shall be set to 0.
2497 */
2498
Xin Longff356412017-05-31 16:36:32 +08002499 if (sctp_stream_init(&asoc->stream, asoc->c.sinit_num_ostreams,
2500 asoc->c.sinit_max_instreams, gfp))
Xin Long7e062972017-05-23 13:28:55 +08002501 goto clean_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
Xin Long7e062972017-05-23 13:28:55 +08002503 if (!asoc->temp && sctp_assoc_set_id(asoc, gfp))
2504 goto clean_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505
2506 /* ADDIP Section 4.1 ASCONF Chunk Procedures
2507 *
2508 * When an endpoint has an ASCONF signaled change to be sent to the
2509 * remote endpoint it should do the following:
2510 * ...
2511 * A2) A serial number should be assigned to the Chunk. The serial
2512 * number should be a monotonically increasing number. All serial
2513 * numbers are defined to be initialized at the start of the
2514 * association to the same value as the Initial TSN.
2515 */
2516 asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
2517 return 1;
2518
2519clean_up:
2520 /* Release the transport structures. */
2521 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2522 transport = list_entry(pos, struct sctp_transport, transports);
Vlad Yasevichadd52372008-09-18 16:28:27 -07002523 if (transport->state != SCTP_ACTIVE)
2524 sctp_assoc_rm_peer(asoc, transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 }
Frank Filz3f7a87d2005-06-20 13:14:57 -07002526
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527nomem:
2528 return 0;
2529}
2530
2531
2532/* Update asoc with the option described in param.
2533 *
2534 * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
2535 *
2536 * asoc is the association to update.
2537 * param is the variable length parameter to use for update.
2538 * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
2539 * If the current packet is an INIT we want to minimize the amount of
2540 * work we do. In particular, we should not build transport
2541 * structures for the addresses.
2542 */
2543static int sctp_process_param(struct sctp_association *asoc,
2544 union sctp_params param,
2545 const union sctp_addr *peer_addr,
Al Virodd0fc662005-10-07 07:46:04 +01002546 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547{
Eric W. Biedermane7ff4a72012-08-07 07:27:02 +00002548 struct net *net = sock_net(asoc->base.sk);
Xin Long327c0da2017-08-11 10:23:58 +08002549 struct sctp_endpoint *ep = asoc->ep;
Vlad Yasevichd6de3092007-12-20 14:10:00 -08002550 union sctp_addr_param *addr_param;
2551 struct sctp_transport *t;
Xin Long327c0da2017-08-11 10:23:58 +08002552 enum sctp_scope scope;
2553 union sctp_addr addr;
2554 struct sctp_af *af;
2555 int retval = 1, i;
2556 u32 stale;
2557 __u16 sat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
2559 /* We maintain all INIT parameters in network byte order all the
2560 * time. This allows us to not worry about whether the parameters
2561 * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
2562 */
2563 switch (param.p->type) {
2564 case SCTP_PARAM_IPV6_ADDRESS:
2565 if (PF_INET6 != asoc->base.sk->sk_family)
2566 break;
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07002567 goto do_addr_param;
2568
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 case SCTP_PARAM_IPV4_ADDRESS:
Vlad Yasevich7dab83d2008-07-18 23:05:40 -07002570 /* v4 addresses are not allowed on v6-only socket */
2571 if (ipv6_only_sock(asoc->base.sk))
2572 break;
2573do_addr_param:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 af = sctp_get_af_specific(param_type2af(param.p->type));
Al Virodd86d132006-11-20 17:11:13 -08002575 af->from_addr_param(&addr, param.addr, htons(asoc->peer.port), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 scope = sctp_scope(peer_addr);
Eric W. Biedermane7ff4a72012-08-07 07:27:02 +00002577 if (sctp_in_scope(net, &addr, scope))
Al Virodd86d132006-11-20 17:11:13 -08002578 if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_UNCONFIRMED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 return 0;
2580 break;
2581
2582 case SCTP_PARAM_COOKIE_PRESERVATIVE:
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00002583 if (!net->sctp.cookie_preserve_enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 break;
2585
2586 stale = ntohl(param.life->lifespan_increment);
2587
2588 /* Suggested Cookie Life span increment's unit is msec,
2589 * (1/1000sec).
2590 */
Daniel Borkmann52db8822013-06-25 18:17:27 +02002591 asoc->cookie_life = ktime_add_ms(asoc->cookie_life, stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 break;
2593
2594 case SCTP_PARAM_HOST_NAME_ADDRESS:
Daniel Borkmannbb333812013-06-28 19:49:40 +02002595 pr_debug("%s: unimplemented SCTP_HOST_NAME_ADDRESS\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 break;
2597
2598 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2599 /* Turn off the default values first so we'll know which
2600 * ones are really set by the peer.
2601 */
2602 asoc->peer.ipv4_address = 0;
2603 asoc->peer.ipv6_address = 0;
2604
Gui Jianfeng140ee962008-03-05 13:43:32 -08002605 /* Assume that peer supports the address family
2606 * by which it sends a packet.
2607 */
2608 if (peer_addr->sa.sa_family == AF_INET6)
2609 asoc->peer.ipv6_address = 1;
2610 else if (peer_addr->sa.sa_family == AF_INET)
2611 asoc->peer.ipv4_address = 1;
2612
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 /* Cycle through address types; avoid divide by 0. */
Xin Long3c918702017-06-30 11:52:16 +08002614 sat = ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 if (sat)
2616 sat /= sizeof(__u16);
2617
2618 for (i = 0; i < sat; ++i) {
2619 switch (param.sat->types[i]) {
2620 case SCTP_PARAM_IPV4_ADDRESS:
2621 asoc->peer.ipv4_address = 1;
2622 break;
2623
2624 case SCTP_PARAM_IPV6_ADDRESS:
Wei Yongjun6e40a912008-05-09 15:11:17 -07002625 if (PF_INET6 == asoc->base.sk->sk_family)
2626 asoc->peer.ipv6_address = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 break;
2628
2629 case SCTP_PARAM_HOST_NAME_ADDRESS:
2630 asoc->peer.hostname_address = 1;
2631 break;
2632
2633 default: /* Just ignore anything else. */
2634 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 }
2637 break;
2638
2639 case SCTP_PARAM_STATE_COOKIE:
2640 asoc->peer.cookie_len =
Xin Long3c918702017-06-30 11:52:16 +08002641 ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 asoc->peer.cookie = param.cookie->body;
2643 break;
2644
2645 case SCTP_PARAM_HEARTBEAT_INFO:
2646 /* Would be odd to receive, but it causes no problems. */
2647 break;
2648
2649 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2650 /* Rejected during verify stage. */
2651 break;
2652
2653 case SCTP_PARAM_ECN_CAPABLE:
2654 asoc->peer.ecn_capable = 1;
2655 break;
2656
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002657 case SCTP_PARAM_ADAPTATION_LAYER_IND:
Vlad Yaseviche69c4e02008-09-15 16:29:49 -04002658 asoc->peer.adaptation_ind = ntohl(param.aind->adaptation_ind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 break;
2660
Vlad Yasevichd6de3092007-12-20 14:10:00 -08002661 case SCTP_PARAM_SET_PRIMARY:
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00002662 if (!net->sctp.addip_enable)
Vlad Yasevich0ef46e22008-09-18 16:27:38 -07002663 goto fall_through;
2664
Xin Long8b32f232017-08-03 15:42:18 +08002665 addr_param = param.v + sizeof(struct sctp_addip_param);
Vlad Yasevichd6de3092007-12-20 14:10:00 -08002666
Saran Maruti Ramanaracfbf6542015-01-29 11:05:58 +01002667 af = sctp_get_af_specific(param_type2af(addr_param->p.type));
Daniel Borkmanne40607c2014-11-10 17:54:26 +01002668 if (af == NULL)
2669 break;
2670
Vlad Yasevichd6de3092007-12-20 14:10:00 -08002671 af->from_addr_param(&addr, addr_param,
2672 htons(asoc->peer.port), 0);
2673
2674 /* if the address is invalid, we can't process it.
2675 * XXX: see spec for what to do.
2676 */
2677 if (!af->addr_valid(&addr, NULL, NULL))
2678 break;
2679
2680 t = sctp_assoc_lookup_paddr(asoc, &addr);
2681 if (!t)
2682 break;
2683
2684 sctp_assoc_set_primary(asoc, t);
2685 break;
2686
Vlad Yasevich131a47e2007-09-16 15:53:56 -07002687 case SCTP_PARAM_SUPPORTED_EXT:
2688 sctp_process_ext_param(asoc, param);
2689 break;
2690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 case SCTP_PARAM_FWD_TSN_SUPPORT:
Xin Long28aa4c22016-07-09 19:47:40 +08002692 if (asoc->prsctp_enable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 asoc->peer.prsctp_capable = 1;
2694 break;
2695 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002696 /* Fall Through */
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002697 goto fall_through;
2698
2699 case SCTP_PARAM_RANDOM:
Vlad Yasevichb14878c2014-04-17 17:26:50 +02002700 if (!ep->auth_enable)
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002701 goto fall_through;
2702
2703 /* Save peer's random parameter */
2704 asoc->peer.peer_random = kmemdup(param.p,
2705 ntohs(param.p->length), gfp);
2706 if (!asoc->peer.peer_random) {
2707 retval = 0;
2708 break;
2709 }
2710 break;
2711
2712 case SCTP_PARAM_HMAC_ALGO:
Vlad Yasevichb14878c2014-04-17 17:26:50 +02002713 if (!ep->auth_enable)
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002714 goto fall_through;
2715
2716 /* Save peer's HMAC list */
2717 asoc->peer.peer_hmacs = kmemdup(param.p,
2718 ntohs(param.p->length), gfp);
2719 if (!asoc->peer.peer_hmacs) {
2720 retval = 0;
2721 break;
2722 }
2723
2724 /* Set the default HMAC the peer requested*/
2725 sctp_auth_asoc_set_default_hmac(asoc, param.hmac_algo);
2726 break;
2727
2728 case SCTP_PARAM_CHUNKS:
Vlad Yasevichb14878c2014-04-17 17:26:50 +02002729 if (!ep->auth_enable)
Vlad Yasevich730fc3d2007-09-16 19:32:11 -07002730 goto fall_through;
2731
2732 asoc->peer.peer_chunks = kmemdup(param.p,
2733 ntohs(param.p->length), gfp);
2734 if (!asoc->peer.peer_chunks)
2735 retval = 0;
2736 break;
2737fall_through:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 default:
2739 /* Any unrecognized parameters should have been caught
2740 * and handled by sctp_verify_param() which should be
2741 * called prior to this routine. Simply log the error
2742 * here.
2743 */
Daniel Borkmannbb333812013-06-28 19:49:40 +02002744 pr_debug("%s: ignoring param:%d for association:%p.\n",
2745 __func__, ntohs(param.p->type), asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
2749 return retval;
2750}
2751
2752/* Select a new verification tag. */
2753__u32 sctp_generate_tag(const struct sctp_endpoint *ep)
2754{
2755 /* I believe that this random number generator complies with RFC1750.
2756 * A tag of 0 is reserved for special cases (e.g. INIT).
2757 */
2758 __u32 x;
2759
2760 do {
2761 get_random_bytes(&x, sizeof(__u32));
2762 } while (x == 0);
2763
2764 return x;
2765}
2766
2767/* Select an initial TSN to send during startup. */
2768__u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
2769{
2770 __u32 retval;
2771
2772 get_random_bytes(&retval, sizeof(__u32));
2773 return retval;
2774}
2775
2776/*
2777 * ADDIP 3.1.1 Address Configuration Change Chunk (ASCONF)
2778 * 0 1 2 3
2779 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
2780 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2781 * | Type = 0xC1 | Chunk Flags | Chunk Length |
2782 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2783 * | Serial Number |
2784 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2785 * | Address Parameter |
2786 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2787 * | ASCONF Parameter #1 |
2788 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2789 * \ \
2790 * / .... /
2791 * \ \
2792 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2793 * | ASCONF Parameter #N |
2794 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2795 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002796 * Address Parameter and other parameter will not be wrapped in this function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 */
2798static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
2799 union sctp_addr *addr,
2800 int vparam_len)
2801{
Xin Long65205cc2017-08-03 15:42:19 +08002802 struct sctp_addiphdr asconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 struct sctp_chunk *retval;
2804 int length = sizeof(asconf) + vparam_len;
2805 union sctp_addr_param addrparam;
2806 int addrlen;
2807 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2808
2809 addrlen = af->to_addr_param(addr, &addrparam);
2810 if (!addrlen)
2811 return NULL;
2812 length += addrlen;
2813
2814 /* Create the chunk. */
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -03002815 retval = sctp_make_control(asoc, SCTP_CID_ASCONF, 0, length,
2816 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 if (!retval)
2818 return NULL;
2819
2820 asconf.serial = htonl(asoc->addip_serial++);
2821
2822 retval->subh.addip_hdr =
2823 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2824 retval->param_hdr.v =
2825 sctp_addto_chunk(retval, addrlen, &addrparam);
2826
2827 return retval;
2828}
2829
2830/* ADDIP
2831 * 3.2.1 Add IP Address
2832 * 0 1 2 3
2833 * 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
2834 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2835 * | Type = 0xC001 | Length = Variable |
2836 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2837 * | ASCONF-Request Correlation ID |
2838 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2839 * | Address Parameter |
2840 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2841 *
2842 * 3.2.2 Delete IP Address
2843 * 0 1 2 3
2844 * 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
2845 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2846 * | Type = 0xC002 | Length = Variable |
2847 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2848 * | ASCONF-Request Correlation ID |
2849 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2850 * | Address Parameter |
2851 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2852 *
2853 */
2854struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
Xin Long327c0da2017-08-11 10:23:58 +08002855 union sctp_addr *laddr,
2856 struct sockaddr *addrs,
2857 int addrcnt, __be16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858{
Xin Long327c0da2017-08-11 10:23:58 +08002859 union sctp_addr_param addr_param;
Xin Long8b32f232017-08-03 15:42:18 +08002860 struct sctp_addip_param param;
Xin Long327c0da2017-08-11 10:23:58 +08002861 int paramlen = sizeof(param);
2862 struct sctp_chunk *retval;
2863 int addr_param_len = 0;
2864 union sctp_addr *addr;
2865 int totallen = 0, i;
2866 int del_pickup = 0;
2867 struct sctp_af *af;
2868 void *addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
2870 /* Get total length of all the address parameters. */
2871 addr_buf = addrs;
2872 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +00002873 addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 af = sctp_get_af_specific(addr->v4.sin_family);
2875 addr_param_len = af->to_addr_param(addr, &addr_param);
2876
2877 totallen += paramlen;
2878 totallen += addr_param_len;
2879
2880 addr_buf += af->sockaddr_len;
Michio Honda8a07eb02011-04-26 20:19:36 +09002881 if (asoc->asconf_addr_del_pending && !del_pickup) {
2882 /* reuse the parameter length from the same scope one */
2883 totallen += paramlen;
2884 totallen += addr_param_len;
2885 del_pickup = 1;
Daniel Borkmannbb333812013-06-28 19:49:40 +02002886
2887 pr_debug("%s: picked same-scope del_pending addr, "
2888 "totallen for all addresses is %d\n",
2889 __func__, totallen);
Michio Honda8a07eb02011-04-26 20:19:36 +09002890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 }
2892
2893 /* Create an asconf chunk with the required length. */
2894 retval = sctp_make_asconf(asoc, laddr, totallen);
2895 if (!retval)
2896 return NULL;
2897
2898 /* Add the address parameters to the asconf chunk. */
2899 addr_buf = addrs;
2900 for (i = 0; i < addrcnt; i++) {
Joe Perchesea110732011-06-13 16:21:26 +00002901 addr = addr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 af = sctp_get_af_specific(addr->v4.sin_family);
2903 addr_param_len = af->to_addr_param(addr, &addr_param);
2904 param.param_hdr.type = flags;
2905 param.param_hdr.length = htons(paramlen + addr_param_len);
Xin Long978aa042017-10-28 19:43:57 +08002906 param.crr_id = htonl(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
2908 sctp_addto_chunk(retval, paramlen, &param);
2909 sctp_addto_chunk(retval, addr_param_len, &addr_param);
2910
2911 addr_buf += af->sockaddr_len;
2912 }
Michio Honda8a07eb02011-04-26 20:19:36 +09002913 if (flags == SCTP_PARAM_ADD_IP && del_pickup) {
2914 addr = asoc->asconf_addr_del_pending;
2915 af = sctp_get_af_specific(addr->v4.sin_family);
2916 addr_param_len = af->to_addr_param(addr, &addr_param);
2917 param.param_hdr.type = SCTP_PARAM_DEL_IP;
2918 param.param_hdr.length = htons(paramlen + addr_param_len);
Xin Long978aa042017-10-28 19:43:57 +08002919 param.crr_id = htonl(i);
Michio Honda8a07eb02011-04-26 20:19:36 +09002920
2921 sctp_addto_chunk(retval, paramlen, &param);
2922 sctp_addto_chunk(retval, addr_param_len, &addr_param);
2923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 return retval;
2925}
2926
2927/* ADDIP
2928 * 3.2.4 Set Primary IP Address
2929 * 0 1 2 3
2930 * 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
2931 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2932 * | Type =0xC004 | Length = Variable |
2933 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2934 * | ASCONF-Request Correlation ID |
2935 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2936 * | Address Parameter |
2937 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2938 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002939 * Create an ASCONF chunk with Set Primary IP address parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 */
2941struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
2942 union sctp_addr *addr)
2943{
Xin Long327c0da2017-08-11 10:23:58 +08002944 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2945 union sctp_addr_param addrparam;
Xin Long8b32f232017-08-03 15:42:18 +08002946 struct sctp_addip_param param;
Xin Long327c0da2017-08-11 10:23:58 +08002947 struct sctp_chunk *retval;
2948 int len = sizeof(param);
2949 int addrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
2951 addrlen = af->to_addr_param(addr, &addrparam);
2952 if (!addrlen)
2953 return NULL;
2954 len += addrlen;
2955
2956 /* Create the chunk and make asconf header. */
2957 retval = sctp_make_asconf(asoc, addr, len);
2958 if (!retval)
2959 return NULL;
2960
2961 param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
2962 param.param_hdr.length = htons(len);
2963 param.crr_id = 0;
2964
2965 sctp_addto_chunk(retval, sizeof(param), &param);
2966 sctp_addto_chunk(retval, addrlen, &addrparam);
2967
2968 return retval;
2969}
2970
2971/* ADDIP 3.1.2 Address Configuration Acknowledgement Chunk (ASCONF-ACK)
2972 * 0 1 2 3
2973 * 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
2974 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2975 * | Type = 0x80 | Chunk Flags | Chunk Length |
2976 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2977 * | Serial Number |
2978 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2979 * | ASCONF Parameter Response#1 |
2980 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2981 * \ \
2982 * / .... /
2983 * \ \
2984 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2985 * | ASCONF Parameter Response#N |
2986 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2987 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002988 * Create an ASCONF_ACK chunk with enough space for the parameter responses.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 */
2990static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
2991 __u32 serial, int vparam_len)
2992{
Xin Long327c0da2017-08-11 10:23:58 +08002993 struct sctp_addiphdr asconf;
2994 struct sctp_chunk *retval;
2995 int length = sizeof(asconf) + vparam_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
2997 /* Create the chunk. */
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -03002998 retval = sctp_make_control(asoc, SCTP_CID_ASCONF_ACK, 0, length,
2999 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 if (!retval)
3001 return NULL;
3002
3003 asconf.serial = htonl(serial);
3004
3005 retval->subh.addip_hdr =
3006 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
3007
3008 return retval;
3009}
3010
3011/* Add response parameters to an ASCONF_ACK chunk. */
Al Viro9f81bcd2006-11-20 17:26:34 -08003012static void sctp_add_asconf_response(struct sctp_chunk *chunk, __be32 crr_id,
Xin Long8b32f232017-08-03 15:42:18 +08003013 __be16 err_code,
3014 struct sctp_addip_param *asconf_param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015{
Xin Long8b32f232017-08-03 15:42:18 +08003016 struct sctp_addip_param ack_param;
Xin Long327c0da2017-08-11 10:23:58 +08003017 struct sctp_errhdr err_param;
3018 int asconf_param_len = 0;
3019 int err_param_len = 0;
3020 __be16 response_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
3022 if (SCTP_ERROR_NO_ERROR == err_code) {
3023 response_type = SCTP_PARAM_SUCCESS_REPORT;
3024 } else {
3025 response_type = SCTP_PARAM_ERR_CAUSE;
3026 err_param_len = sizeof(err_param);
3027 if (asconf_param)
3028 asconf_param_len =
3029 ntohs(asconf_param->param_hdr.length);
3030 }
3031
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003032 /* Add Success Indication or Error Cause Indication parameter. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 ack_param.param_hdr.type = response_type;
3034 ack_param.param_hdr.length = htons(sizeof(ack_param) +
3035 err_param_len +
3036 asconf_param_len);
3037 ack_param.crr_id = crr_id;
3038 sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
3039
3040 if (SCTP_ERROR_NO_ERROR == err_code)
3041 return;
3042
3043 /* Add Error Cause parameter. */
3044 err_param.cause = err_code;
3045 err_param.length = htons(err_param_len + asconf_param_len);
3046 sctp_addto_chunk(chunk, err_param_len, &err_param);
3047
3048 /* Add the failed TLV copied from ASCONF chunk. */
3049 if (asconf_param)
3050 sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
3051}
3052
3053/* Process a asconf parameter. */
Al Virodbc16db2006-11-20 17:01:42 -08003054static __be16 sctp_process_asconf_param(struct sctp_association *asoc,
Xin Long8b32f232017-08-03 15:42:18 +08003055 struct sctp_chunk *asconf,
3056 struct sctp_addip_param *asconf_param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 union sctp_addr_param *addr_param;
Xin Long327c0da2017-08-11 10:23:58 +08003059 struct sctp_transport *peer;
3060 union sctp_addr addr;
3061 struct sctp_af *af;
Al Viro5f242a12006-11-20 17:05:23 -08003062
Xin Long8b32f232017-08-03 15:42:18 +08003063 addr_param = (void *)asconf_param + sizeof(*asconf_param);
Patrick McHardyc1cc6782008-05-13 23:25:00 -07003064
Wei Yongjun44e65c12009-06-16 14:48:24 +08003065 if (asconf_param->param_hdr.type != SCTP_PARAM_ADD_IP &&
3066 asconf_param->param_hdr.type != SCTP_PARAM_DEL_IP &&
3067 asconf_param->param_hdr.type != SCTP_PARAM_SET_PRIMARY)
3068 return SCTP_ERROR_UNKNOWN_PARAM;
3069
Shan Wei6a435732011-04-18 19:11:47 +00003070 switch (addr_param->p.type) {
Wei Yongjunc4492582008-05-09 15:11:53 -07003071 case SCTP_PARAM_IPV6_ADDRESS:
3072 if (!asoc->peer.ipv6_address)
Wei Yongjun945e5ab2009-04-16 14:21:02 +08003073 return SCTP_ERROR_DNS_FAILED;
Wei Yongjunc4492582008-05-09 15:11:53 -07003074 break;
3075 case SCTP_PARAM_IPV4_ADDRESS:
3076 if (!asoc->peer.ipv4_address)
Wei Yongjun945e5ab2009-04-16 14:21:02 +08003077 return SCTP_ERROR_DNS_FAILED;
Wei Yongjunc4492582008-05-09 15:11:53 -07003078 break;
3079 default:
Wei Yongjun945e5ab2009-04-16 14:21:02 +08003080 return SCTP_ERROR_DNS_FAILED;
Wei Yongjunc4492582008-05-09 15:11:53 -07003081 }
3082
Shan Wei6a435732011-04-18 19:11:47 +00003083 af = sctp_get_af_specific(param_type2af(addr_param->p.type));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 if (unlikely(!af))
Wei Yongjun945e5ab2009-04-16 14:21:02 +08003085 return SCTP_ERROR_DNS_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
Al Virodd86d132006-11-20 17:11:13 -08003087 af->from_addr_param(&addr, addr_param, htons(asoc->peer.port), 0);
Vlad Yasevich42e30bf2007-12-20 14:08:56 -08003088
3089 /* ADDIP 4.2.1 This parameter MUST NOT contain a broadcast
3090 * or multicast address.
3091 * (note: wildcard is permitted and requires special handling so
3092 * make sure we check for that)
3093 */
3094 if (!af->is_any(&addr) && !af->addr_valid(&addr, NULL, asconf->skb))
Wei Yongjun945e5ab2009-04-16 14:21:02 +08003095 return SCTP_ERROR_DNS_FAILED;
Vlad Yasevich42e30bf2007-12-20 14:08:56 -08003096
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 switch (asconf_param->param_hdr.type) {
3098 case SCTP_PARAM_ADD_IP:
Vlad Yasevich42e30bf2007-12-20 14:08:56 -08003099 /* Section 4.2.1:
3100 * If the address 0.0.0.0 or ::0 is provided, the source
3101 * address of the packet MUST be added.
3102 */
3103 if (af->is_any(&addr))
3104 memcpy(&addr, &asconf->source, sizeof(addr));
3105
Richard Haines2277c7c2018-02-13 20:56:24 +00003106 if (security_sctp_bind_connect(asoc->ep->base.sk,
3107 SCTP_PARAM_ADD_IP,
3108 (struct sockaddr *)&addr,
3109 af->sockaddr_len))
3110 return SCTP_ERROR_REQ_REFUSED;
3111
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 /* ADDIP 4.3 D9) If an endpoint receives an ADD IP address
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003113 * request and does not have the local resources to add this
3114 * new address to the association, it MUST return an Error
3115 * Cause TLV set to the new error code 'Operation Refused
3116 * Due to Resource Shortage'.
3117 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
Al Virodd86d132006-11-20 17:11:13 -08003119 peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC, SCTP_UNCONFIRMED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 if (!peer)
3121 return SCTP_ERROR_RSRC_LOW;
3122
3123 /* Start the heartbeat timer. */
Marcelo Ricardo Leitnerba6f5e32016-04-06 15:15:19 -03003124 sctp_transport_reset_hb_timer(peer);
Michio Honda6af29cc2011-06-16 17:14:34 +09003125 asoc->new_transport = peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 break;
3127 case SCTP_PARAM_DEL_IP:
3128 /* ADDIP 4.3 D7) If a request is received to delete the
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003129 * last remaining IP address of a peer endpoint, the receiver
3130 * MUST send an Error Cause TLV with the error cause set to the
3131 * new error code 'Request to Delete Last Remaining IP Address'.
3132 */
Vlad Yasevich42e30bf2007-12-20 14:08:56 -08003133 if (asoc->peer.transport_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 return SCTP_ERROR_DEL_LAST_IP;
3135
3136 /* ADDIP 4.3 D8) If a request is received to delete an IP
3137 * address which is also the source address of the IP packet
3138 * which contained the ASCONF chunk, the receiver MUST reject
3139 * this request. To reject the request the receiver MUST send
3140 * an Error Cause TLV set to the new error code 'Request to
3141 * Delete Source IP Address'
3142 */
Michio Hondab1364102011-04-26 17:37:02 +09003143 if (sctp_cmp_addr_exact(&asconf->source, &addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 return SCTP_ERROR_DEL_SRC_IP;
3145
Vlad Yasevich42e30bf2007-12-20 14:08:56 -08003146 /* Section 4.2.2
3147 * If the address 0.0.0.0 or ::0 is provided, all
3148 * addresses of the peer except the source address of the
3149 * packet MUST be deleted.
3150 */
3151 if (af->is_any(&addr)) {
3152 sctp_assoc_set_primary(asoc, asconf->transport);
3153 sctp_assoc_del_nonprimary_peers(asoc,
3154 asconf->transport);
lucien7c5a9462015-08-28 17:45:58 +08003155 return SCTP_ERROR_NO_ERROR;
3156 }
3157
3158 /* If the address is not part of the association, the
3159 * ASCONF-ACK with Error Cause Indication Parameter
3160 * which including cause of Unresolvable Address should
3161 * be sent.
3162 */
3163 peer = sctp_assoc_lookup_paddr(asoc, &addr);
3164 if (!peer)
3165 return SCTP_ERROR_DNS_FAILED;
3166
3167 sctp_assoc_rm_peer(asoc, peer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 break;
3169 case SCTP_PARAM_SET_PRIMARY:
Vlad Yasevich42e30bf2007-12-20 14:08:56 -08003170 /* ADDIP Section 4.2.4
3171 * If the address 0.0.0.0 or ::0 is provided, the receiver
3172 * MAY mark the source address of the packet as its
3173 * primary.
3174 */
3175 if (af->is_any(&addr))
3176 memcpy(&addr.v4, sctp_source(asconf), sizeof(addr));
3177
Richard Haines2277c7c2018-02-13 20:56:24 +00003178 if (security_sctp_bind_connect(asoc->ep->base.sk,
3179 SCTP_PARAM_SET_PRIMARY,
3180 (struct sockaddr *)&addr,
3181 af->sockaddr_len))
3182 return SCTP_ERROR_REQ_REFUSED;
3183
Al Virodd86d132006-11-20 17:11:13 -08003184 peer = sctp_assoc_lookup_paddr(asoc, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 if (!peer)
Wei Yongjun945e5ab2009-04-16 14:21:02 +08003186 return SCTP_ERROR_DNS_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
3188 sctp_assoc_set_primary(asoc, peer);
3189 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 }
3191
3192 return SCTP_ERROR_NO_ERROR;
3193}
3194
Daniel Borkmann9de79222014-10-09 22:55:31 +02003195/* Verify the ASCONF packet before we process it. */
3196bool sctp_verify_asconf(const struct sctp_association *asoc,
3197 struct sctp_chunk *chunk, bool addr_param_needed,
3198 struct sctp_paramhdr **errp)
3199{
Xin Long68d75462017-08-03 15:42:20 +08003200 struct sctp_addip_chunk *addip;
Daniel Borkmann9de79222014-10-09 22:55:31 +02003201 bool addr_param_seen = false;
Xin Long327c0da2017-08-11 10:23:58 +08003202 union sctp_params param;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003203
Xin Long68d75462017-08-03 15:42:20 +08003204 addip = (struct sctp_addip_chunk *)chunk->chunk_hdr;
Daniel Borkmann9de79222014-10-09 22:55:31 +02003205 sctp_walk_params(param, addip, addip_hdr.params) {
3206 size_t length = ntohs(param.p->length);
3207
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003208 *errp = param.p;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003209 switch (param.p->type) {
Daniel Borkmann9de79222014-10-09 22:55:31 +02003210 case SCTP_PARAM_ERR_CAUSE:
3211 break;
3212 case SCTP_PARAM_IPV4_ADDRESS:
Xin Longa38905e2017-07-17 11:29:49 +08003213 if (length != sizeof(struct sctp_ipv4addr_param))
Daniel Borkmann9de79222014-10-09 22:55:31 +02003214 return false;
lucience7b4cc2015-08-27 16:26:34 +08003215 /* ensure there is only one addr param and it's in the
3216 * beginning of addip_hdr params, or we reject it.
3217 */
3218 if (param.v != addip->addip_hdr.params)
3219 return false;
Daniel Borkmann9de79222014-10-09 22:55:31 +02003220 addr_param_seen = true;
3221 break;
3222 case SCTP_PARAM_IPV6_ADDRESS:
Xin Long00987cc2017-07-17 11:29:50 +08003223 if (length != sizeof(struct sctp_ipv6addr_param))
Daniel Borkmann9de79222014-10-09 22:55:31 +02003224 return false;
lucience7b4cc2015-08-27 16:26:34 +08003225 if (param.v != addip->addip_hdr.params)
3226 return false;
Daniel Borkmann9de79222014-10-09 22:55:31 +02003227 addr_param_seen = true;
3228 break;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003229 case SCTP_PARAM_ADD_IP:
3230 case SCTP_PARAM_DEL_IP:
3231 case SCTP_PARAM_SET_PRIMARY:
Daniel Borkmann9de79222014-10-09 22:55:31 +02003232 /* In ASCONF chunks, these need to be first. */
3233 if (addr_param_needed && !addr_param_seen)
3234 return false;
3235 length = ntohs(param.addip->param_hdr.length);
Xin Long8b32f232017-08-03 15:42:18 +08003236 if (length < sizeof(struct sctp_addip_param) +
Xin Long3c918702017-06-30 11:52:16 +08003237 sizeof(**errp))
Daniel Borkmann9de79222014-10-09 22:55:31 +02003238 return false;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003239 break;
3240 case SCTP_PARAM_SUCCESS_REPORT:
3241 case SCTP_PARAM_ADAPTATION_LAYER_IND:
Xin Long8b32f232017-08-03 15:42:18 +08003242 if (length != sizeof(struct sctp_addip_param))
Daniel Borkmann9de79222014-10-09 22:55:31 +02003243 return false;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003244 break;
3245 default:
Daniel Borkmann9de79222014-10-09 22:55:31 +02003246 /* This is unkown to us, reject! */
3247 return false;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003248 }
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003249 }
3250
Daniel Borkmann9de79222014-10-09 22:55:31 +02003251 /* Remaining sanity checks. */
3252 if (addr_param_needed && !addr_param_seen)
3253 return false;
3254 if (!addr_param_needed && addr_param_seen)
3255 return false;
3256 if (param.v != chunk->chunk_end)
3257 return false;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003258
Daniel Borkmann9de79222014-10-09 22:55:31 +02003259 return true;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003260}
3261
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003262/* Process an incoming ASCONF chunk with the next expected serial no. and
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 * return an ASCONF_ACK chunk to be sent in response.
3264 */
3265struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
3266 struct sctp_chunk *asconf)
3267{
Xin Long327c0da2017-08-11 10:23:58 +08003268 union sctp_addr_param *addr_param;
Xin Long68d75462017-08-03 15:42:20 +08003269 struct sctp_addip_chunk *addip;
Xin Long327c0da2017-08-11 10:23:58 +08003270 struct sctp_chunk *asconf_ack;
Daniel Borkmann9de79222014-10-09 22:55:31 +02003271 bool all_param_pass = true;
Xin Long327c0da2017-08-11 10:23:58 +08003272 struct sctp_addiphdr *hdr;
3273 int length = 0, chunk_len;
Daniel Borkmann9de79222014-10-09 22:55:31 +02003274 union sctp_params param;
Xin Long327c0da2017-08-11 10:23:58 +08003275 __be16 err_code;
3276 __u32 serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
Xin Long68d75462017-08-03 15:42:20 +08003278 addip = (struct sctp_addip_chunk *)asconf->chunk_hdr;
Xin Long922dbc52017-06-30 11:52:13 +08003279 chunk_len = ntohs(asconf->chunk_hdr->length) -
3280 sizeof(struct sctp_chunkhdr);
Xin Long65205cc2017-08-03 15:42:19 +08003281 hdr = (struct sctp_addiphdr *)asconf->skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 serial = ntohl(hdr->serial);
3283
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003284 /* Skip the addiphdr and store a pointer to address parameter. */
Xin Long65205cc2017-08-03 15:42:19 +08003285 length = sizeof(*hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
3287 chunk_len -= length;
3288
3289 /* Skip the address parameter and store a pointer to the first
Joe Perches7aa1b542007-12-20 14:03:52 -08003290 * asconf parameter.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003291 */
Shan Wei6a435732011-04-18 19:11:47 +00003292 length = ntohs(addr_param->p.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 chunk_len -= length;
3294
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003295 /* create an ASCONF_ACK chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 * Based on the definitions of parameters, we know that the size of
Wei Yongjun2cab86b2011-03-31 23:42:55 +00003297 * ASCONF_ACK parameters are less than or equal to the fourfold of ASCONF
Joe Perches7aa1b542007-12-20 14:03:52 -08003298 * parameters.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 */
Wei Yongjun2cab86b2011-03-31 23:42:55 +00003300 asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 if (!asconf_ack)
3302 goto done;
3303
3304 /* Process the TLVs contained within the ASCONF chunk. */
Daniel Borkmann9de79222014-10-09 22:55:31 +02003305 sctp_walk_params(param, addip, addip_hdr.params) {
3306 /* Skip preceeding address parameters. */
3307 if (param.p->type == SCTP_PARAM_IPV4_ADDRESS ||
3308 param.p->type == SCTP_PARAM_IPV6_ADDRESS)
3309 continue;
3310
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 err_code = sctp_process_asconf_param(asoc, asconf,
Daniel Borkmann9de79222014-10-09 22:55:31 +02003312 param.addip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 /* ADDIP 4.1 A7)
3314 * If an error response is received for a TLV parameter,
3315 * all TLVs with no response before the failed TLV are
3316 * considered successful if not reported. All TLVs after
3317 * the failed response are considered unsuccessful unless
3318 * a specific success indication is present for the parameter.
3319 */
Daniel Borkmann9de79222014-10-09 22:55:31 +02003320 if (err_code != SCTP_ERROR_NO_ERROR)
3321 all_param_pass = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 if (!all_param_pass)
Daniel Borkmann9de79222014-10-09 22:55:31 +02003323 sctp_add_asconf_response(asconf_ack, param.addip->crr_id,
3324 err_code, param.addip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325
3326 /* ADDIP 4.3 D11) When an endpoint receiving an ASCONF to add
3327 * an IP address sends an 'Out of Resource' in its response, it
3328 * MUST also fail any subsequent add or delete requests bundled
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003329 * in the ASCONF.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 */
Daniel Borkmann9de79222014-10-09 22:55:31 +02003331 if (err_code == SCTP_ERROR_RSRC_LOW)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334done:
3335 asoc->peer.addip_serial++;
3336
3337 /* If we are sending a new ASCONF_ACK hold a reference to it in assoc
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003338 * after freeing the reference to old asconf ack if any.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 */
3340 if (asconf_ack) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 sctp_chunk_hold(asconf_ack);
Vlad Yasevicha08de642007-12-20 14:11:47 -08003342 list_add_tail(&asconf_ack->transmitted_list,
3343 &asoc->asconf_ack_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 }
3345
3346 return asconf_ack;
3347}
3348
3349/* Process a asconf parameter that is successfully acked. */
Wei Yongjun425e0f62009-06-16 14:47:30 +08003350static void sctp_asconf_param_success(struct sctp_association *asoc,
Xin Long8b32f232017-08-03 15:42:18 +08003351 struct sctp_addip_param *asconf_param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 struct sctp_bind_addr *bp = &asoc->base.bind_addr;
3354 union sctp_addr_param *addr_param;
Sridhar Samudraladc022a92006-07-21 14:49:25 -07003355 struct sctp_sockaddr_entry *saddr;
Xin Long327c0da2017-08-11 10:23:58 +08003356 struct sctp_transport *transport;
3357 union sctp_addr addr;
3358 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
Xin Long8b32f232017-08-03 15:42:18 +08003360 addr_param = (void *)asconf_param + sizeof(*asconf_param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361
3362 /* We have checked the packet before, so we do not check again. */
Shan Wei6a435732011-04-18 19:11:47 +00003363 af = sctp_get_af_specific(param_type2af(addr_param->p.type));
Al Virodd86d132006-11-20 17:11:13 -08003364 af->from_addr_param(&addr, addr_param, htons(bp->port), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365
3366 switch (asconf_param->param_hdr.type) {
3367 case SCTP_PARAM_ADD_IP:
Vlad Yasevich559cf712007-09-16 16:03:28 -07003368 /* This is always done in BH context with a socket lock
3369 * held, so the list can not change.
3370 */
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -04003371 local_bh_disable();
Vlad Yasevich559cf712007-09-16 16:03:28 -07003372 list_for_each_entry(saddr, &bp->address_list, list) {
Al Virodd86d132006-11-20 17:11:13 -08003373 if (sctp_cmp_addr_exact(&saddr->a, &addr))
Vlad Yasevichf57d96b2007-12-20 14:12:24 -08003374 saddr->state = SCTP_ADDR_SRC;
Sridhar Samudraladc022a92006-07-21 14:49:25 -07003375 }
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -04003376 local_bh_enable();
Wei Yongjun3cd97492009-06-16 10:07:23 +08003377 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
3378 transports) {
Julian Anastasovc86a7732017-02-06 23:14:13 +02003379 sctp_transport_dst_release(transport);
Wei Yongjun3cd97492009-06-16 10:07:23 +08003380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 break;
3382 case SCTP_PARAM_DEL_IP:
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -04003383 local_bh_disable();
Wei Yongjun425e0f62009-06-16 14:47:30 +08003384 sctp_del_bind_addr(bp, &addr);
Michio Honda8a07eb02011-04-26 20:19:36 +09003385 if (asoc->asconf_addr_del_pending != NULL &&
3386 sctp_cmp_addr_exact(asoc->asconf_addr_del_pending, &addr)) {
3387 kfree(asoc->asconf_addr_del_pending);
3388 asoc->asconf_addr_del_pending = NULL;
3389 }
Vlad Yasevich0ed90fb2007-10-24 16:10:00 -04003390 local_bh_enable();
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07003391 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
3392 transports) {
Julian Anastasovc86a7732017-02-06 23:14:13 +02003393 sctp_transport_dst_release(transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 }
3395 break;
3396 default:
3397 break;
3398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399}
3400
3401/* Get the corresponding ASCONF response error code from the ASCONF_ACK chunk
3402 * for the given asconf parameter. If there is no response for this parameter,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003403 * return the error code based on the third argument 'no_err'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 * ADDIP 4.1
3405 * A7) If an error response is received for a TLV parameter, all TLVs with no
3406 * response before the failed TLV are considered successful if not reported.
3407 * All TLVs after the failed response are considered unsuccessful unless a
3408 * specific success indication is present for the parameter.
3409 */
Al Virodbc16db2006-11-20 17:01:42 -08003410static __be16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
Xin Long8b32f232017-08-03 15:42:18 +08003411 struct sctp_addip_param *asconf_param,
3412 int no_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413{
Xin Long8b32f232017-08-03 15:42:18 +08003414 struct sctp_addip_param *asconf_ack_param;
Xin Long327c0da2017-08-11 10:23:58 +08003415 struct sctp_errhdr *err_param;
3416 int asconf_ack_len;
3417 __be16 err_code;
3418 int length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419
3420 if (no_err)
3421 err_code = SCTP_ERROR_NO_ERROR;
3422 else
3423 err_code = SCTP_ERROR_REQ_REFUSED;
3424
Wei Yongjunf3830cc2007-10-15 11:51:03 +09003425 asconf_ack_len = ntohs(asconf_ack->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +08003426 sizeof(struct sctp_chunkhdr);
Wei Yongjunf3830cc2007-10-15 11:51:03 +09003427
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 /* Skip the addiphdr from the asconf_ack chunk and store a pointer to
3429 * the first asconf_ack parameter.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003430 */
Xin Long65205cc2017-08-03 15:42:19 +08003431 length = sizeof(struct sctp_addiphdr);
Xin Long8b32f232017-08-03 15:42:18 +08003432 asconf_ack_param = (struct sctp_addip_param *)(asconf_ack->skb->data +
3433 length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 asconf_ack_len -= length;
3435
3436 while (asconf_ack_len > 0) {
3437 if (asconf_ack_param->crr_id == asconf_param->crr_id) {
wangweidongcb3f8372013-12-23 12:16:50 +08003438 switch (asconf_ack_param->param_hdr.type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 case SCTP_PARAM_SUCCESS_REPORT:
3440 return SCTP_ERROR_NO_ERROR;
3441 case SCTP_PARAM_ERR_CAUSE:
Xin Long8b32f232017-08-03 15:42:18 +08003442 length = sizeof(*asconf_ack_param);
Joe Perchesea110732011-06-13 16:21:26 +00003443 err_param = (void *)asconf_ack_param + length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 asconf_ack_len -= length;
3445 if (asconf_ack_len > 0)
3446 return err_param->cause;
3447 else
3448 return SCTP_ERROR_INV_PARAM;
3449 break;
3450 default:
3451 return SCTP_ERROR_INV_PARAM;
3452 }
3453 }
3454
3455 length = ntohs(asconf_ack_param->param_hdr.length);
Joe Perchesea110732011-06-13 16:21:26 +00003456 asconf_ack_param = (void *)asconf_ack_param + length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 asconf_ack_len -= length;
3458 }
3459
3460 return err_code;
3461}
3462
3463/* Process an incoming ASCONF_ACK chunk against the cached last ASCONF chunk. */
3464int sctp_process_asconf_ack(struct sctp_association *asoc,
3465 struct sctp_chunk *asconf_ack)
3466{
Xin Long327c0da2017-08-11 10:23:58 +08003467 struct sctp_chunk *asconf = asoc->addip_last_asconf;
3468 struct sctp_addip_param *asconf_param;
3469 __be16 err_code = SCTP_ERROR_NO_ERROR;
3470 union sctp_addr_param *addr_param;
3471 int asconf_len = asconf->skb->len;
3472 int all_param_pass = 0;
3473 int length = 0;
3474 int no_err = 1;
3475 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
3477 /* Skip the chunkhdr and addiphdr from the last asconf sent and store
3478 * a pointer to address parameter.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003479 */
Xin Long68d75462017-08-03 15:42:20 +08003480 length = sizeof(struct sctp_addip_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
3482 asconf_len -= length;
3483
3484 /* Skip the address parameter in the last asconf sent and store a
Joe Perches7aa1b542007-12-20 14:03:52 -08003485 * pointer to the first asconf parameter.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003486 */
Shan Wei6a435732011-04-18 19:11:47 +00003487 length = ntohs(addr_param->p.length);
Joe Perchesea110732011-06-13 16:21:26 +00003488 asconf_param = (void *)addr_param + length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 asconf_len -= length;
3490
3491 /* ADDIP 4.1
3492 * A8) If there is no response(s) to specific TLV parameter(s), and no
3493 * failures are indicated, then all request(s) are considered
3494 * successful.
3495 */
Xin Long65205cc2017-08-03 15:42:19 +08003496 if (asconf_ack->skb->len == sizeof(struct sctp_addiphdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 all_param_pass = 1;
3498
3499 /* Process the TLVs contained in the last sent ASCONF chunk. */
3500 while (asconf_len > 0) {
3501 if (all_param_pass)
3502 err_code = SCTP_ERROR_NO_ERROR;
3503 else {
3504 err_code = sctp_get_asconf_response(asconf_ack,
3505 asconf_param,
3506 no_err);
3507 if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
3508 no_err = 0;
3509 }
3510
3511 switch (err_code) {
3512 case SCTP_ERROR_NO_ERROR:
Wei Yongjun425e0f62009-06-16 14:47:30 +08003513 sctp_asconf_param_success(asoc, asconf_param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 break;
3515
3516 case SCTP_ERROR_RSRC_LOW:
3517 retval = 1;
3518 break;
3519
Wei Yongjuna987f762009-04-07 15:44:29 +08003520 case SCTP_ERROR_UNKNOWN_PARAM:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 /* Disable sending this type of asconf parameter in
3522 * future.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003523 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 asoc->peer.addip_disabled_mask |=
3525 asconf_param->param_hdr.type;
3526 break;
3527
3528 case SCTP_ERROR_REQ_REFUSED:
3529 case SCTP_ERROR_DEL_LAST_IP:
3530 case SCTP_ERROR_DEL_SRC_IP:
3531 default:
3532 break;
3533 }
3534
3535 /* Skip the processed asconf parameter and move to the next
3536 * one.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003537 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 length = ntohs(asconf_param->param_hdr.length);
Joe Perchesea110732011-06-13 16:21:26 +00003539 asconf_param = (void *)asconf_param + length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 asconf_len -= length;
3541 }
3542
Michio Hondaddc4bbe2011-06-17 11:03:23 +09003543 if (no_err && asoc->src_out_of_asoc_ok) {
Michio Honda8a07eb02011-04-26 20:19:36 +09003544 asoc->src_out_of_asoc_ok = 0;
Michio Hondaddc4bbe2011-06-17 11:03:23 +09003545 sctp_transport_immediate_rtx(asoc->peer.primary_path);
3546 }
Michio Honda8a07eb02011-04-26 20:19:36 +09003547
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 /* Free the cached last sent asconf chunk. */
Vlad Yasevich5f9646c2008-02-05 14:23:44 -05003549 list_del_init(&asconf->transmitted_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 sctp_chunk_free(asconf);
3551 asoc->addip_last_asconf = NULL;
3552
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 return retval;
3554}
3555
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003556/* Make a FWD TSN chunk. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
3558 __u32 new_cum_tsn, size_t nstreams,
3559 struct sctp_fwdtsn_skip *skiplist)
3560{
3561 struct sctp_chunk *retval = NULL;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003562 struct sctp_fwdtsn_hdr ftsn_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 struct sctp_fwdtsn_skip skip;
3564 size_t hint;
3565 int i;
3566
3567 hint = (nstreams + 1) * sizeof(__u32);
3568
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -03003569 retval = sctp_make_control(asoc, SCTP_CID_FWD_TSN, 0, hint, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570
3571 if (!retval)
3572 return NULL;
3573
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
3575 retval->subh.fwdtsn_hdr =
3576 sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
3577
3578 for (i = 0; i < nstreams; i++) {
3579 skip.stream = skiplist[i].stream;
3580 skip.ssn = skiplist[i].ssn;
3581 sctp_addto_chunk(retval, sizeof(skip), &skip);
3582 }
3583
3584 return retval;
3585}
Xin Longcc16f002017-01-18 00:44:42 +08003586
Xin Long2d07a492017-12-15 00:41:25 +08003587struct sctp_chunk *sctp_make_ifwdtsn(const struct sctp_association *asoc,
3588 __u32 new_cum_tsn, size_t nstreams,
3589 struct sctp_ifwdtsn_skip *skiplist)
3590{
3591 struct sctp_chunk *retval = NULL;
3592 struct sctp_ifwdtsn_hdr ftsn_hdr;
3593 size_t hint;
3594
3595 hint = (nstreams + 1) * sizeof(__u32);
3596
3597 retval = sctp_make_control(asoc, SCTP_CID_I_FWD_TSN, 0, hint,
3598 GFP_ATOMIC);
3599 if (!retval)
3600 return NULL;
3601
3602 ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
3603 retval->subh.ifwdtsn_hdr =
3604 sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
3605
3606 sctp_addto_chunk(retval, nstreams * sizeof(skiplist[0]), skiplist);
3607
3608 return retval;
3609}
3610
Xin Longcc16f002017-01-18 00:44:42 +08003611/* RE-CONFIG 3.1 (RE-CONFIG chunk)
3612 * 0 1 2 3
3613 * 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
3614 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3615 * | Type = 130 | Chunk Flags | Chunk Length |
3616 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3617 * \ \
3618 * / Re-configuration Parameter /
3619 * \ \
3620 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3621 * \ \
3622 * / Re-configuration Parameter (optional) /
3623 * \ \
3624 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3625 */
Xin Long327c0da2017-08-11 10:23:58 +08003626static struct sctp_chunk *sctp_make_reconf(const struct sctp_association *asoc,
3627 int length)
Xin Longcc16f002017-01-18 00:44:42 +08003628{
3629 struct sctp_reconf_chunk *reconf;
3630 struct sctp_chunk *retval;
3631
3632 retval = sctp_make_control(asoc, SCTP_CID_RECONF, 0, length,
3633 GFP_ATOMIC);
3634 if (!retval)
3635 return NULL;
3636
3637 reconf = (struct sctp_reconf_chunk *)retval->chunk_hdr;
3638 retval->param_hdr.v = reconf->params;
3639
3640 return retval;
3641}
3642
3643/* RE-CONFIG 4.1 (STREAM OUT RESET)
3644 * 0 1 2 3
3645 * 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
3646 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3647 * | Parameter Type = 13 | Parameter Length = 16 + 2 * N |
3648 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3649 * | Re-configuration Request Sequence Number |
3650 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3651 * | Re-configuration Response Sequence Number |
3652 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3653 * | Sender's Last Assigned TSN |
3654 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3655 * | Stream Number 1 (optional) | Stream Number 2 (optional) |
3656 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3657 * / ...... /
3658 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3659 * | Stream Number N-1 (optional) | Stream Number N (optional) |
3660 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3661 *
3662 * RE-CONFIG 4.2 (STREAM IN RESET)
3663 * 0 1 2 3
3664 * 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
3665 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3666 * | Parameter Type = 14 | Parameter Length = 8 + 2 * N |
3667 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3668 * | Re-configuration Request Sequence Number |
3669 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3670 * | Stream Number 1 (optional) | Stream Number 2 (optional) |
3671 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3672 * / ...... /
3673 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3674 * | Stream Number N-1 (optional) | Stream Number N (optional) |
3675 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3676 */
3677struct sctp_chunk *sctp_make_strreset_req(
Xin Long327c0da2017-08-11 10:23:58 +08003678 const struct sctp_association *asoc,
Xin Long1da4fc92017-10-28 19:43:54 +08003679 __u16 stream_num, __be16 *stream_list,
Xin Long327c0da2017-08-11 10:23:58 +08003680 bool out, bool in)
Xin Longcc16f002017-01-18 00:44:42 +08003681{
Xin Long423852f2017-11-15 17:00:11 +08003682 __u16 stream_len = stream_num * sizeof(__u16);
Xin Longcc16f002017-01-18 00:44:42 +08003683 struct sctp_strreset_outreq outreq;
Xin Longcc16f002017-01-18 00:44:42 +08003684 struct sctp_strreset_inreq inreq;
3685 struct sctp_chunk *retval;
Xin Long16e1a912017-02-17 12:45:40 +08003686 __u16 outlen, inlen;
Xin Longcc16f002017-01-18 00:44:42 +08003687
3688 outlen = (sizeof(outreq) + stream_len) * out;
3689 inlen = (sizeof(inreq) + stream_len) * in;
3690
3691 retval = sctp_make_reconf(asoc, outlen + inlen);
3692 if (!retval)
3693 return NULL;
3694
Xin Longcc16f002017-01-18 00:44:42 +08003695 if (outlen) {
3696 outreq.param_hdr.type = SCTP_PARAM_RESET_OUT_REQUEST;
3697 outreq.param_hdr.length = htons(outlen);
3698 outreq.request_seq = htonl(asoc->strreset_outseq);
3699 outreq.response_seq = htonl(asoc->strreset_inseq - 1);
3700 outreq.send_reset_at_tsn = htonl(asoc->next_tsn - 1);
3701
3702 sctp_addto_chunk(retval, sizeof(outreq), &outreq);
3703
3704 if (stream_len)
3705 sctp_addto_chunk(retval, stream_len, stream_list);
3706 }
3707
3708 if (inlen) {
3709 inreq.param_hdr.type = SCTP_PARAM_RESET_IN_REQUEST;
3710 inreq.param_hdr.length = htons(inlen);
3711 inreq.request_seq = htonl(asoc->strreset_outseq + out);
3712
3713 sctp_addto_chunk(retval, sizeof(inreq), &inreq);
3714
3715 if (stream_len)
3716 sctp_addto_chunk(retval, stream_len, stream_list);
3717 }
3718
Xin Longcc16f002017-01-18 00:44:42 +08003719 return retval;
3720}
Xin Longc56480a2017-02-09 01:18:17 +08003721
3722/* RE-CONFIG 4.3 (SSN/TSN RESET ALL)
3723 * 0 1 2 3
3724 * 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
3725 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3726 * | Parameter Type = 15 | Parameter Length = 8 |
3727 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3728 * | Re-configuration Request Sequence Number |
3729 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3730 */
3731struct sctp_chunk *sctp_make_strreset_tsnreq(
Xin Long327c0da2017-08-11 10:23:58 +08003732 const struct sctp_association *asoc)
Xin Longc56480a2017-02-09 01:18:17 +08003733{
3734 struct sctp_strreset_tsnreq tsnreq;
3735 __u16 length = sizeof(tsnreq);
3736 struct sctp_chunk *retval;
3737
3738 retval = sctp_make_reconf(asoc, length);
3739 if (!retval)
3740 return NULL;
3741
3742 tsnreq.param_hdr.type = SCTP_PARAM_RESET_TSN_REQUEST;
3743 tsnreq.param_hdr.length = htons(length);
3744 tsnreq.request_seq = htonl(asoc->strreset_outseq);
3745
3746 sctp_addto_chunk(retval, sizeof(tsnreq), &tsnreq);
3747
3748 return retval;
3749}
Xin Long78098112017-02-09 01:18:19 +08003750
3751/* RE-CONFIG 4.5/4.6 (ADD STREAM)
3752 * 0 1 2 3
3753 * 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
3754 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3755 * | Parameter Type = 17 | Parameter Length = 12 |
3756 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3757 * | Re-configuration Request Sequence Number |
3758 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3759 * | Number of new streams | Reserved |
3760 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3761 */
3762struct sctp_chunk *sctp_make_strreset_addstrm(
Xin Long327c0da2017-08-11 10:23:58 +08003763 const struct sctp_association *asoc,
3764 __u16 out, __u16 in)
Xin Long78098112017-02-09 01:18:19 +08003765{
3766 struct sctp_strreset_addstrm addstrm;
3767 __u16 size = sizeof(addstrm);
3768 struct sctp_chunk *retval;
3769
3770 retval = sctp_make_reconf(asoc, (!!out + !!in) * size);
3771 if (!retval)
3772 return NULL;
3773
3774 if (out) {
3775 addstrm.param_hdr.type = SCTP_PARAM_RESET_ADD_OUT_STREAMS;
3776 addstrm.param_hdr.length = htons(size);
3777 addstrm.number_of_streams = htons(out);
3778 addstrm.request_seq = htonl(asoc->strreset_outseq);
3779 addstrm.reserved = 0;
3780
3781 sctp_addto_chunk(retval, size, &addstrm);
3782 }
3783
3784 if (in) {
3785 addstrm.param_hdr.type = SCTP_PARAM_RESET_ADD_IN_STREAMS;
3786 addstrm.param_hdr.length = htons(size);
3787 addstrm.number_of_streams = htons(in);
3788 addstrm.request_seq = htonl(asoc->strreset_outseq + !!out);
3789 addstrm.reserved = 0;
3790
3791 sctp_addto_chunk(retval, size, &addstrm);
3792 }
3793
3794 return retval;
3795}
Xin Longbd4b9f82017-02-17 12:45:37 +08003796
3797/* RE-CONFIG 4.4 (RESP)
3798 * 0 1 2 3
3799 * 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
3800 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3801 * | Parameter Type = 16 | Parameter Length |
3802 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3803 * | Re-configuration Response Sequence Number |
3804 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3805 * | Result |
3806 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3807 */
Xin Long327c0da2017-08-11 10:23:58 +08003808struct sctp_chunk *sctp_make_strreset_resp(const struct sctp_association *asoc,
3809 __u32 result, __u32 sn)
Xin Longbd4b9f82017-02-17 12:45:37 +08003810{
3811 struct sctp_strreset_resp resp;
3812 __u16 length = sizeof(resp);
3813 struct sctp_chunk *retval;
3814
3815 retval = sctp_make_reconf(asoc, length);
3816 if (!retval)
3817 return NULL;
3818
3819 resp.param_hdr.type = SCTP_PARAM_RESET_RESPONSE;
3820 resp.param_hdr.length = htons(length);
3821 resp.response_seq = htonl(sn);
3822 resp.result = htonl(result);
3823
3824 sctp_addto_chunk(retval, sizeof(resp), &resp);
3825
3826 return retval;
3827}
3828
3829/* RE-CONFIG 4.4 OPTIONAL (TSNRESP)
3830 * 0 1 2 3
3831 * 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
3832 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3833 * | Parameter Type = 16 | Parameter Length |
3834 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3835 * | Re-configuration Response Sequence Number |
3836 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3837 * | Result |
3838 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3839 * | Sender's Next TSN (optional) |
3840 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3841 * | Receiver's Next TSN (optional) |
3842 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3843 */
Xin Long327c0da2017-08-11 10:23:58 +08003844struct sctp_chunk *sctp_make_strreset_tsnresp(struct sctp_association *asoc,
3845 __u32 result, __u32 sn,
3846 __u32 sender_tsn,
3847 __u32 receiver_tsn)
Xin Longbd4b9f82017-02-17 12:45:37 +08003848{
3849 struct sctp_strreset_resptsn tsnresp;
3850 __u16 length = sizeof(tsnresp);
3851 struct sctp_chunk *retval;
3852
3853 retval = sctp_make_reconf(asoc, length);
3854 if (!retval)
3855 return NULL;
3856
3857 tsnresp.param_hdr.type = SCTP_PARAM_RESET_RESPONSE;
3858 tsnresp.param_hdr.length = htons(length);
3859
3860 tsnresp.response_seq = htonl(sn);
3861 tsnresp.result = htonl(result);
3862 tsnresp.senders_next_tsn = htonl(sender_tsn);
3863 tsnresp.receivers_next_tsn = htonl(receiver_tsn);
3864
3865 sctp_addto_chunk(retval, sizeof(tsnresp), &tsnresp);
3866
3867 return retval;
3868}
Xin Longea625042017-02-17 12:45:41 +08003869
3870bool sctp_verify_reconf(const struct sctp_association *asoc,
3871 struct sctp_chunk *chunk,
3872 struct sctp_paramhdr **errp)
3873{
3874 struct sctp_reconf_chunk *hdr;
3875 union sctp_params param;
Xin Long1da4fc92017-10-28 19:43:54 +08003876 __be16 last = 0;
3877 __u16 cnt = 0;
Xin Longea625042017-02-17 12:45:41 +08003878
3879 hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
3880 sctp_walk_params(param, hdr, params) {
3881 __u16 length = ntohs(param.p->length);
3882
3883 *errp = param.p;
3884 if (cnt++ > 2)
3885 return false;
3886 switch (param.p->type) {
3887 case SCTP_PARAM_RESET_OUT_REQUEST:
3888 if (length < sizeof(struct sctp_strreset_outreq) ||
3889 (last && last != SCTP_PARAM_RESET_RESPONSE &&
3890 last != SCTP_PARAM_RESET_IN_REQUEST))
3891 return false;
3892 break;
3893 case SCTP_PARAM_RESET_IN_REQUEST:
3894 if (length < sizeof(struct sctp_strreset_inreq) ||
3895 (last && last != SCTP_PARAM_RESET_OUT_REQUEST))
3896 return false;
3897 break;
3898 case SCTP_PARAM_RESET_RESPONSE:
3899 if ((length != sizeof(struct sctp_strreset_resp) &&
3900 length != sizeof(struct sctp_strreset_resptsn)) ||
3901 (last && last != SCTP_PARAM_RESET_RESPONSE &&
3902 last != SCTP_PARAM_RESET_OUT_REQUEST))
3903 return false;
3904 break;
3905 case SCTP_PARAM_RESET_TSN_REQUEST:
3906 if (length !=
3907 sizeof(struct sctp_strreset_tsnreq) || last)
3908 return false;
3909 break;
3910 case SCTP_PARAM_RESET_ADD_IN_STREAMS:
3911 if (length != sizeof(struct sctp_strreset_addstrm) ||
3912 (last && last != SCTP_PARAM_RESET_ADD_OUT_STREAMS))
3913 return false;
3914 break;
3915 case SCTP_PARAM_RESET_ADD_OUT_STREAMS:
3916 if (length != sizeof(struct sctp_strreset_addstrm) ||
3917 (last && last != SCTP_PARAM_RESET_ADD_IN_STREAMS))
3918 return false;
3919 break;
3920 default:
3921 return false;
3922 }
3923
3924 last = param.p->type;
3925 }
3926
3927 return true;
3928}