blob: 1a3c75347f483dc1bd6d075d7c1257f757633332 [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 * Copyright (c) 2002 Nokia Corp.
7 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05008 * This is part of the SCTP Linux Kernel Implementation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * These are the state functions for the state machine.
11 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050012 * This SCTP implementation is free software;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * you can redistribute it and/or modify it under the terms of
14 * the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
16 * any later version.
17 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050018 * This SCTP implementation is distributed in the hope that it
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
20 * ************************
21 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 * See the GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
Jeff Kirsher4b2f13a2013-12-06 06:28:48 -080025 * along with GNU CC; see the file COPYING. If not, see
26 * <http://www.gnu.org/licenses/>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 *
28 * Please send any bug reports or fixes you make to the
29 * email address(es):
Daniel Borkmann91705c62013-07-23 14:51:47 +020030 * lksctp developers <linux-sctp@vger.kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 * Written or modified by:
33 * La Monte H.P. Yarroll <piggy@acm.org>
34 * Karl Knutson <karl@athena.chicago.il.us>
35 * Mathew Kotowsky <kotowsky@sctp.org>
36 * Sridhar Samudrala <samudrala@us.ibm.com>
37 * Jon Grimm <jgrimm@us.ibm.com>
38 * Hui Huang <hui.huang@nokia.com>
39 * Dajiang Zhang <dajiang.zhang@nokia.com>
40 * Daisy Chang <daisyc@us.ibm.com>
41 * Ardelle Fan <ardelle.fan@intel.com>
42 * Ryan Layer <rmlayer@us.ibm.com>
43 * Kevin Gao <kevin.gao@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 */
45
Joe Perches145ce502010-08-24 13:21:08 +000046#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/types.h>
49#include <linux/kernel.h>
50#include <linux/ip.h>
51#include <linux/ipv6.h>
52#include <linux/net.h>
53#include <linux/inet.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/sock.h>
56#include <net/inet_ecn.h>
57#include <linux/skbuff.h>
58#include <net/sctp/sctp.h>
59#include <net/sctp/sm.h>
60#include <net/sctp/structs.h>
61
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000062static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
63 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 const struct sctp_association *asoc,
65 struct sctp_chunk *chunk,
66 const void *payload,
67 size_t paylen);
68static int sctp_eat_data(const struct sctp_association *asoc,
69 struct sctp_chunk *chunk,
70 sctp_cmd_seq_t *commands);
Eric W. Biederman2ce95502012-08-06 08:43:06 +000071static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
72 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 const struct sctp_chunk *chunk);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000074static void sctp_send_stale_cookie_err(struct net *net,
75 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 const struct sctp_association *asoc,
77 const struct sctp_chunk *chunk,
78 sctp_cmd_seq_t *commands,
79 struct sctp_chunk *err_chunk);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000080static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
81 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 const struct sctp_association *asoc,
83 const sctp_subtype_t type,
84 void *arg,
85 sctp_cmd_seq_t *commands);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000086static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
87 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 const struct sctp_association *asoc,
89 const sctp_subtype_t type,
90 void *arg,
91 sctp_cmd_seq_t *commands);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000092static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
93 const struct sctp_endpoint *ep,
Vlad Yasevichece25df2007-09-07 16:30:54 -040094 const struct sctp_association *asoc,
95 const sctp_subtype_t type,
96 void *arg,
97 sctp_cmd_seq_t *commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
99
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000100static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
101 sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -0800102 __be16 error, int sk_err,
Adrian Bunk52c1da32005-06-23 22:05:33 -0700103 const struct sctp_association *asoc,
104 struct sctp_transport *transport);
105
Wei Yongjunaecedea2007-08-02 16:57:44 +0800106static sctp_disposition_t sctp_sf_abort_violation(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000107 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400108 const struct sctp_endpoint *ep,
Wei Yongjunaecedea2007-08-02 16:57:44 +0800109 const struct sctp_association *asoc,
110 void *arg,
111 sctp_cmd_seq_t *commands,
112 const __u8 *payload,
113 const size_t paylen);
114
Adrian Bunk52c1da32005-06-23 22:05:33 -0700115static sctp_disposition_t sctp_sf_violation_chunklen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000116 struct net *net,
Adrian Bunk52c1da32005-06-23 22:05:33 -0700117 const struct sctp_endpoint *ep,
118 const struct sctp_association *asoc,
119 const sctp_subtype_t type,
120 void *arg,
121 sctp_cmd_seq_t *commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Wei Yongjun6f4c6182007-09-19 17:19:52 +0800123static sctp_disposition_t sctp_sf_violation_paramlen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000124 struct net *net,
Wei Yongjun6f4c6182007-09-19 17:19:52 +0800125 const struct sctp_endpoint *ep,
126 const struct sctp_association *asoc,
127 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -0700128 void *arg, void *ext,
Wei Yongjun6f4c6182007-09-19 17:19:52 +0800129 sctp_cmd_seq_t *commands);
130
Wei Yongjunaecedea2007-08-02 16:57:44 +0800131static sctp_disposition_t sctp_sf_violation_ctsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000132 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +0800133 const struct sctp_endpoint *ep,
134 const struct sctp_association *asoc,
135 const sctp_subtype_t type,
136 void *arg,
137 sctp_cmd_seq_t *commands);
138
Vlad Yasevichece25df2007-09-07 16:30:54 -0400139static sctp_disposition_t sctp_sf_violation_chunk(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000140 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400141 const struct sctp_endpoint *ep,
142 const struct sctp_association *asoc,
143 const sctp_subtype_t type,
144 void *arg,
145 sctp_cmd_seq_t *commands);
146
Xin Long0e67ad52018-05-05 14:59:47 +0800147static sctp_ierror_t sctp_sf_authenticate(
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700148 const struct sctp_association *asoc,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700149 struct sctp_chunk *chunk);
150
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000151static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
152 const struct sctp_endpoint *ep,
Vlad Yasevich75205f42007-12-20 14:12:59 -0800153 const struct sctp_association *asoc,
154 const sctp_subtype_t type,
155 void *arg,
156 sctp_cmd_seq_t *commands);
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/* Small helper function that checks if the chunk length
159 * is of the appropriate length. The 'required_length' argument
160 * is set to be the size of a specific chunk we are testing.
161 * Return Values: 1 = Valid length
162 * 0 = Invalid length
163 *
164 */
165static inline int
166sctp_chunk_length_valid(struct sctp_chunk *chunk,
167 __u16 required_length)
168{
169 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
170
Daniel Borkmann26b87c72014-10-09 22:55:33 +0200171 /* Previously already marked? */
172 if (unlikely(chunk->pdiscard))
173 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (unlikely(chunk_length < required_length))
175 return 0;
176
177 return 1;
178}
179
Xin Long548e23f2020-02-18 12:07:53 +0800180/* Check for format error in an ABORT chunk */
181static inline bool sctp_err_chunk_valid(struct sctp_chunk *chunk)
182{
183 struct sctp_errhdr *err;
184
185 sctp_walk_errors(err, chunk->chunk_hdr);
186
187 return (void *)err == (void *)chunk->chunk_end;
188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/**********************************************************
191 * These are the state functions for handling chunk events.
192 **********************************************************/
193
194/*
195 * Process the final SHUTDOWN COMPLETE.
196 *
197 * Section: 4 (C) (diagram), 9.2
198 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
199 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
200 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
201 * should stop the T2-shutdown timer and remove all knowledge of the
202 * association (and thus the association enters the CLOSED state).
203 *
Jerome Forissier047a2422005-04-28 11:58:43 -0700204 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * C) Rules for packet carrying SHUTDOWN COMPLETE:
206 * ...
Jerome Forissier047a2422005-04-28 11:58:43 -0700207 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
208 * if the Verification Tag field of the packet matches its own tag and
209 * the T bit is not set
210 * OR
211 * it is set to its peer's tag and the T bit is set in the Chunk
212 * Flags.
213 * Otherwise, the receiver MUST silently discard the packet
214 * and take no further action. An endpoint MUST ignore the
215 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 *
217 * Inputs
218 * (endpoint, asoc, chunk)
219 *
220 * Outputs
221 * (asoc, reply_msg, msg_up, timers, counters)
222 *
223 * The return value is the disposition of the chunk.
224 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000225sctp_disposition_t sctp_sf_do_4_C(struct net *net,
226 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 const struct sctp_association *asoc,
228 const sctp_subtype_t type,
229 void *arg,
230 sctp_cmd_seq_t *commands)
231{
232 struct sctp_chunk *chunk = arg;
233 struct sctp_ulpevent *ev;
234
Vlad Yasevichece25df2007-09-07 16:30:54 -0400235 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000236 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -0400237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 /* RFC 2960 6.10 Bundling
239 *
240 * An endpoint MUST NOT bundle INIT, INIT ACK or
241 * SHUTDOWN COMPLETE with any other chunks.
242 */
243 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000244 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Vlad Yasevichece25df2007-09-07 16:30:54 -0400246 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
247 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000248 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400249 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 /* RFC 2960 10.2 SCTP-to-ULP
252 *
253 * H) SHUTDOWN COMPLETE notification
254 *
255 * When SCTP completes the shutdown procedures (section 9.2) this
256 * notification is passed to the upper layer.
257 */
258 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700259 0, 0, 0, NULL, GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700260 if (ev)
261 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900262 SCTP_ULPEVENT(ev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
265 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
266 * not the chunk should be discarded. If the endpoint is in
267 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
268 * T2-shutdown timer and remove all knowledge of the
269 * association (and thus the association enters the CLOSED
270 * state).
271 */
272 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
273 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
274
275 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
276 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
277
278 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
279 SCTP_STATE(SCTP_STATE_CLOSED));
280
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000281 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
282 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
285
286 return SCTP_DISPOSITION_DELETE_TCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289/*
290 * Respond to a normal INIT chunk.
291 * We are the side that is being asked for an association.
292 *
293 * Section: 5.1 Normal Establishment of an Association, B
294 * B) "Z" shall respond immediately with an INIT ACK chunk. The
295 * destination IP address of the INIT ACK MUST be set to the source
296 * IP address of the INIT to which this INIT ACK is responding. In
297 * the response, besides filling in other parameters, "Z" must set the
298 * Verification Tag field to Tag_A, and also provide its own
299 * Verification Tag (Tag_Z) in the Initiate Tag field.
300 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900301 * Verification Tag: Must be 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 *
303 * Inputs
304 * (endpoint, asoc, chunk)
305 *
306 * Outputs
307 * (asoc, reply_msg, msg_up, timers, counters)
308 *
309 * The return value is the disposition of the chunk.
310 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000311sctp_disposition_t sctp_sf_do_5_1B_init(struct net *net,
312 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 const struct sctp_association *asoc,
314 const sctp_subtype_t type,
315 void *arg,
316 sctp_cmd_seq_t *commands)
317{
318 struct sctp_chunk *chunk = arg;
319 struct sctp_chunk *repl;
320 struct sctp_association *new_asoc;
321 struct sctp_chunk *err_chunk;
322 struct sctp_packet *packet;
323 sctp_unrecognized_param_t *unk_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 int len;
325
326 /* 6.10 Bundling
327 * An endpoint MUST NOT bundle INIT, INIT ACK or
328 * SHUTDOWN COMPLETE with any other chunks.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900329 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 * IG Section 2.11.2
331 * Furthermore, we require that the receiver of an INIT chunk MUST
332 * enforce these rules by silently discarding an arriving packet
333 * with an INIT chunk that is bundled with other chunks.
334 */
335 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000336 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 /* If the packet is an OOTB packet which is temporarily on the
339 * control endpoint, respond with an ABORT.
340 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +0000341 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000342 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000343 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900347 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 */
349 if (chunk->sctp_hdr->vtag != 0)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000350 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 /* Make sure that the INIT chunk has a valid length.
353 * Normally, this would cause an ABORT with a Protocol Violation
354 * error, but since we don't have an association, we'll
355 * just discard the packet.
356 */
357 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000358 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Vlad Yasevichbec96402009-07-30 18:08:28 -0400360 /* If the INIT is coming toward a closing socket, we'll send back
361 * and ABORT. Essentially, this catches the race of INIT being
362 * backloged to the socket at the same time as the user isses close().
363 * Since the socket and all its associations are going away, we
364 * can treat this OOTB
365 */
366 if (sctp_sstate(ep->base.sk, CLOSING))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000367 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Vlad Yasevichbec96402009-07-30 18:08:28 -0400368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 /* Verify the INIT chunk before processing it. */
370 err_chunk = NULL;
Vlad Yasevichb14878c2014-04-17 17:26:50 +0200371 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
373 &err_chunk)) {
374 /* This chunk contains fatal error. It is to be discarded.
375 * Send an ABORT, with causes if there is any.
376 */
377 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000378 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 (__u8 *)(err_chunk->chunk_hdr) +
380 sizeof(sctp_chunkhdr_t),
381 ntohs(err_chunk->chunk_hdr->length) -
382 sizeof(sctp_chunkhdr_t));
383
384 sctp_chunk_free(err_chunk);
385
386 if (packet) {
387 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
388 SCTP_PACKET(packet));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000389 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return SCTP_DISPOSITION_CONSUME;
391 } else {
392 return SCTP_DISPOSITION_NOMEM;
393 }
394 } else {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000395 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 commands);
397 }
398 }
399
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900400 /* Grab the INIT header. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 chunk->subh.init_hdr = (sctp_inithdr_t *)chunk->skb->data;
402
403 /* Tag the variable length parameters. */
404 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
405
406 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
407 if (!new_asoc)
408 goto nomem;
409
Vlad Yasevich409b95a2009-11-10 08:57:34 +0000410 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
411 sctp_scope(sctp_source(chunk)),
412 GFP_ATOMIC) < 0)
413 goto nomem_init;
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /* The call, sctp_process_init(), can fail on memory allocation. */
Wei Yongjunde6becd2011-04-19 21:30:51 +0000416 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 (sctp_init_chunk_t *)chunk->chunk_hdr,
418 GFP_ATOMIC))
419 goto nomem_init;
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
422
423 /* If there are errors need to be reported for unknown parameters,
424 * make sure to reserve enough room in the INIT ACK for them.
425 */
426 len = 0;
427 if (err_chunk)
428 len = ntohs(err_chunk->chunk_hdr->length) -
429 sizeof(sctp_chunkhdr_t);
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
432 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700433 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 /* If there are errors need to be reported for unknown parameters,
436 * include them in the outgoing INIT ACK as "Unrecognized parameter"
437 * parameter.
438 */
439 if (err_chunk) {
440 /* Get the "Unrecognized parameter" parameter(s) out of the
441 * ERROR chunk generated by sctp_verify_init(). Since the
442 * error cause code for "unknown parameter" and the
443 * "Unrecognized parameter" type is the same, we can
444 * construct the parameters in INIT ACK by copying the
445 * ERROR causes over.
446 */
447 unk_param = (sctp_unrecognized_param_t *)
448 ((__u8 *)(err_chunk->chunk_hdr) +
449 sizeof(sctp_chunkhdr_t));
450 /* Replace the cause code with the "Unrecognized parameter"
451 * parameter type.
452 */
453 sctp_addto_chunk(repl, len, unk_param);
454 sctp_chunk_free(err_chunk);
455 }
456
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700457 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
460
461 /*
462 * Note: After sending out INIT ACK with the State Cookie parameter,
463 * "Z" MUST NOT allocate any resources, nor keep any states for the
464 * new association. Otherwise, "Z" will be vulnerable to resource
465 * attacks.
466 */
467 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
468
469 return SCTP_DISPOSITION_DELETE_TCB;
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471nomem_init:
472 sctp_association_free(new_asoc);
473nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700474 if (err_chunk)
475 sctp_chunk_free(err_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return SCTP_DISPOSITION_NOMEM;
477}
478
479/*
480 * Respond to a normal INIT ACK chunk.
481 * We are the side that is initiating the association.
482 *
483 * Section: 5.1 Normal Establishment of an Association, C
484 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
485 * timer and leave COOKIE-WAIT state. "A" shall then send the State
486 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
487 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
488 *
489 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
490 * DATA chunks, but it MUST be the first chunk in the packet and
491 * until the COOKIE ACK is returned the sender MUST NOT send any
492 * other packets to the peer.
493 *
494 * Verification Tag: 3.3.3
495 * If the value of the Initiate Tag in a received INIT ACK chunk is
496 * found to be 0, the receiver MUST treat it as an error and close the
497 * association by transmitting an ABORT.
498 *
499 * Inputs
500 * (endpoint, asoc, chunk)
501 *
502 * Outputs
503 * (asoc, reply_msg, msg_up, timers, counters)
504 *
505 * The return value is the disposition of the chunk.
506 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000507sctp_disposition_t sctp_sf_do_5_1C_ack(struct net *net,
508 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 const struct sctp_association *asoc,
510 const sctp_subtype_t type,
511 void *arg,
512 sctp_cmd_seq_t *commands)
513{
514 struct sctp_chunk *chunk = arg;
515 sctp_init_chunk_t *initchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 struct sctp_chunk *err_chunk;
517 struct sctp_packet *packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000520 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /* 6.10 Bundling
523 * An endpoint MUST NOT bundle INIT, INIT ACK or
524 * SHUTDOWN COMPLETE with any other chunks.
525 */
526 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000527 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Vlad Yasevichece25df2007-09-07 16:30:54 -0400529 /* Make sure that the INIT-ACK chunk has a valid length */
530 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_initack_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000531 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400532 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /* Grab the INIT header. */
534 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 /* Verify the INIT chunk before processing it. */
537 err_chunk = NULL;
Vlad Yasevichb14878c2014-04-17 17:26:50 +0200538 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
540 &err_chunk)) {
541
Vlad Yasevich853f4b52008-01-20 06:10:46 -0800542 sctp_error_t error = SCTP_ERROR_NO_RESOURCE;
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 /* This chunk contains fatal error. It is to be discarded.
Vlad Yasevichd6701192007-12-20 14:13:31 -0800545 * Send an ABORT, with causes. If there are no causes,
546 * then there wasn't enough memory. Just terminate
547 * the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 */
549 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000550 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 (__u8 *)(err_chunk->chunk_hdr) +
552 sizeof(sctp_chunkhdr_t),
553 ntohs(err_chunk->chunk_hdr->length) -
554 sizeof(sctp_chunkhdr_t));
555
556 sctp_chunk_free(err_chunk);
557
558 if (packet) {
559 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
560 SCTP_PACKET(packet));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000561 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700562 error = SCTP_ERROR_INV_PARAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700565
566 /* SCTP-AUTH, Section 6.3:
567 * It should be noted that if the receiver wants to tear
568 * down an association in an authenticated way only, the
569 * handling of malformed packets should not result in
570 * tearing down the association.
571 *
572 * This means that if we only want to abort associations
573 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300574 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700575 * was malformed.
576 */
577 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000578 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700579
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000580 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
581 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700582 asoc, chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584
585 /* Tag the variable length parameters. Note that we never
586 * convert the parameters in an INIT chunk.
587 */
588 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
589
590 initchunk = (sctp_init_chunk_t *) chunk->chunk_hdr;
591
592 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
593 SCTP_PEER_INIT(initchunk));
594
Frank Filz3f7a87d2005-06-20 13:14:57 -0700595 /* Reset init error count upon receipt of INIT-ACK. */
596 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 /* 5.1 C) "A" shall stop the T1-init timer and leave
599 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
600 * timer, and enter the COOKIE-ECHOED state.
601 */
602 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
603 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
604 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
605 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
606 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
607 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
608
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700609 /* SCTP-AUTH: genereate the assocition shared keys so that
610 * we can potentially signe the COOKIE-ECHO.
611 */
612 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /* 5.1 C) "A" shall then send the State Cookie received in the
615 * INIT ACK chunk in a COOKIE ECHO chunk, ...
616 */
617 /* If there is any errors to report, send the ERROR chunk generated
618 * for unknown parameters as well.
619 */
620 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
621 SCTP_CHUNK(err_chunk));
622
623 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Xin Long0e67ad52018-05-05 14:59:47 +0800626static bool sctp_auth_chunk_verify(struct net *net, struct sctp_chunk *chunk,
627 const struct sctp_association *asoc)
628{
629 struct sctp_chunk auth;
630
631 if (!chunk->auth_chunk)
632 return true;
633
634 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
635 * is supposed to be authenticated and we have to do delayed
636 * authentication. We've just recreated the association using
637 * the information in the cookie and now it's much easier to
638 * do the authentication.
639 */
640
641 /* Make sure that we and the peer are AUTH capable */
642 if (!net->sctp.auth_enable || !asoc->peer.auth_capable)
643 return false;
644
645 /* set-up our fake chunk so that we can process it */
646 auth.skb = chunk->auth_chunk;
647 auth.asoc = chunk->asoc;
648 auth.sctp_hdr = chunk->sctp_hdr;
649 auth.chunk_hdr = (struct sctp_chunkhdr *)
650 skb_push(chunk->auth_chunk,
651 sizeof(struct sctp_chunkhdr));
652 skb_pull(chunk->auth_chunk, sizeof(struct sctp_chunkhdr));
653 auth.transport = chunk->transport;
654
655 return sctp_sf_authenticate(asoc, &auth) == SCTP_IERROR_NO_ERROR;
656}
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658/*
659 * Respond to a normal COOKIE ECHO chunk.
660 * We are the side that is being asked for an association.
661 *
662 * Section: 5.1 Normal Establishment of an Association, D
663 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
664 * with a COOKIE ACK chunk after building a TCB and moving to
665 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
666 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
667 * chunk MUST be the first chunk in the packet.
668 *
669 * IMPLEMENTATION NOTE: An implementation may choose to send the
670 * Communication Up notification to the SCTP user upon reception
671 * of a valid COOKIE ECHO chunk.
672 *
673 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
674 * D) Rules for packet carrying a COOKIE ECHO
675 *
676 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
677 * Initial Tag received in the INIT ACK.
678 *
679 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
680 *
681 * Inputs
682 * (endpoint, asoc, chunk)
683 *
684 * Outputs
685 * (asoc, reply_msg, msg_up, timers, counters)
686 *
687 * The return value is the disposition of the chunk.
688 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000689sctp_disposition_t sctp_sf_do_5_1D_ce(struct net *net,
690 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 const struct sctp_association *asoc,
692 const sctp_subtype_t type, void *arg,
693 sctp_cmd_seq_t *commands)
694{
695 struct sctp_chunk *chunk = arg;
696 struct sctp_association *new_asoc;
697 sctp_init_chunk_t *peer_init;
698 struct sctp_chunk *repl;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700699 struct sctp_ulpevent *ev, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 int error = 0;
701 struct sctp_chunk *err_chk_p;
Vlad Yasevich609ee462007-08-31 03:10:59 +0900702 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 /* If the packet is an OOTB packet which is temporarily on the
705 * control endpoint, respond with an ABORT.
706 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +0000707 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000708 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000709 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 /* Make sure that the COOKIE_ECHO chunk has a valid length.
713 * In this case, we check that we have enough for at least a
714 * chunk header. More detailed verification is done
715 * in sctp_unpack_cookie().
716 */
717 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000718 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Vlad Yasevich609ee462007-08-31 03:10:59 +0900720 /* If the endpoint is not listening or if the number of associations
721 * on the TCP-style socket exceed the max backlog, respond with an
722 * ABORT.
723 */
724 sk = ep->base.sk;
725 if (!sctp_sstate(sk, LISTENING) ||
726 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000727 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Vlad Yasevich609ee462007-08-31 03:10:59 +0900728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /* "Decode" the chunk. We have no optional parameters so we
730 * are in good shape.
731 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900732 chunk->subh.cookie_hdr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -0700734 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
735 sizeof(sctp_chunkhdr_t)))
736 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
739 * "Z" will reply with a COOKIE ACK chunk after building a TCB
740 * and moving to the ESTABLISHED state.
741 */
742 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
743 &err_chk_p);
744
745 /* FIXME:
746 * If the re-build failed, what is the proper error path
747 * from here?
748 *
749 * [We should abort the association. --piggy]
750 */
751 if (!new_asoc) {
752 /* FIXME: Several errors are possible. A bad cookie should
753 * be silently discarded, but think about logging it too.
754 */
755 switch (error) {
756 case -SCTP_IERROR_NOMEM:
757 goto nomem;
758
759 case -SCTP_IERROR_STALE_COOKIE:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000760 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 err_chk_p);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000762 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 case -SCTP_IERROR_BAD_SIG:
765 default:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000766 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700771 /* Delay state machine commands until later.
772 *
773 * Re-build the bind address for the association is done in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 * the sctp_unpack_cookie() already.
775 */
776 /* This is a brand-new association, so these are not yet side
777 * effects--it is safe to run them here.
778 */
779 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
780
Wei Yongjunde6becd2011-04-19 21:30:51 +0000781 if (!sctp_process_init(new_asoc, chunk,
Al Viro6a1e5f32006-11-20 17:12:25 -0800782 &chunk->subh.cookie_hdr->c.peer_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 peer_init, GFP_ATOMIC))
784 goto nomem_init;
785
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700786 /* SCTP-AUTH: Now that we've populate required fields in
787 * sctp_process_init, set up the assocaition shared keys as
788 * necessary so that we can potentially authenticate the ACK
789 */
790 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
791 if (error)
792 goto nomem_init;
793
Xin Long0e67ad52018-05-05 14:59:47 +0800794 if (!sctp_auth_chunk_verify(net, chunk, new_asoc)) {
795 sctp_association_free(new_asoc);
796 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700797 }
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 repl = sctp_make_cookie_ack(new_asoc, chunk);
800 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700801 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 /* RFC 2960 5.1 Normal Establishment of an Association
804 *
805 * D) IMPLEMENTATION NOTE: An implementation may choose to
806 * send the Communication Up notification to the SCTP user
807 * upon reception of a valid COOKIE ECHO chunk.
808 */
809 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
810 new_asoc->c.sinit_num_ostreams,
811 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700812 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (!ev)
814 goto nomem_ev;
815
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900816 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800817 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800819 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800821 if (new_asoc->peer.adaptation_ind) {
822 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700824 if (!ai_ev)
825 goto nomem_aiev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
827
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700828 /* Add all the state machine commands now since we've created
829 * everything. This way we don't introduce memory corruptions
830 * during side-effect processing and correclty count established
831 * associations.
832 */
833 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
834 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
835 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000836 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
837 SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700838 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
839
Neil Horman9f70f462013-12-10 06:48:15 -0500840 if (new_asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700841 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
842 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
843
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700844 /* This will send the COOKIE ACK */
845 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
846
847 /* Queue the ASSOC_CHANGE event */
848 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
849
850 /* Send up the Adaptation Layer Indication event */
851 if (ai_ev)
852 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
853 SCTP_ULPEVENT(ai_ev));
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return SCTP_DISPOSITION_CONSUME;
856
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700857nomem_aiev:
858 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859nomem_ev:
860 sctp_chunk_free(repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861nomem_init:
862 sctp_association_free(new_asoc);
863nomem:
864 return SCTP_DISPOSITION_NOMEM;
865}
866
867/*
868 * Respond to a normal COOKIE ACK chunk.
Marcelo Ricardo Leitnerb52effd2015-07-17 13:50:21 -0300869 * We are the side that is asking for an association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 *
871 * RFC 2960 5.1 Normal Establishment of an Association
872 *
873 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
874 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
875 * timer. It may also notify its ULP about the successful
876 * establishment of the association with a Communication Up
877 * notification (see Section 10).
878 *
879 * Verification Tag:
880 * Inputs
881 * (endpoint, asoc, chunk)
882 *
883 * Outputs
884 * (asoc, reply_msg, msg_up, timers, counters)
885 *
886 * The return value is the disposition of the chunk.
887 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000888sctp_disposition_t sctp_sf_do_5_1E_ca(struct net *net,
889 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 const struct sctp_association *asoc,
891 const sctp_subtype_t type, void *arg,
892 sctp_cmd_seq_t *commands)
893{
894 struct sctp_chunk *chunk = arg;
895 struct sctp_ulpevent *ev;
896
897 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000898 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 /* Verify that the chunk length for the COOKIE-ACK is OK.
901 * If we don't do this, any bundled chunks may be junked.
902 */
903 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000904 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 commands);
906
907 /* Reset init error count upon receipt of COOKIE-ACK,
908 * to avoid problems with the managemement of this
909 * counter in stale cookie situations when a transition back
910 * from the COOKIE-ECHOED state to the COOKIE-WAIT
911 * state is performed.
912 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700913 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 /* RFC 2960 5.1 Normal Establishment of an Association
916 *
917 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
918 * from the COOKIE-ECHOED state to the ESTABLISHED state,
919 * stopping the T1-cookie timer.
920 */
921 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
922 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
923 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
924 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000925 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
926 SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
Neil Horman9f70f462013-12-10 06:48:15 -0500928 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
930 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 /* It may also notify its ULP about the successful
933 * establishment of the association with a Communication Up
934 * notification (see Section 10).
935 */
936 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
937 0, asoc->c.sinit_num_ostreams,
938 asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700939 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 if (!ev)
942 goto nomem;
943
944 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
945
946 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800947 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800949 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800951 if (asoc->peer.adaptation_ind) {
952 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (!ev)
954 goto nomem;
955
956 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
957 SCTP_ULPEVENT(ev));
958 }
959
960 return SCTP_DISPOSITION_CONSUME;
961nomem:
962 return SCTP_DISPOSITION_NOMEM;
963}
964
965/* Generate and sendout a heartbeat packet. */
966static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
967 const struct sctp_association *asoc,
968 const sctp_subtype_t type,
969 void *arg,
970 sctp_cmd_seq_t *commands)
971{
972 struct sctp_transport *transport = (struct sctp_transport *) arg;
973 struct sctp_chunk *reply;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 /* Send a heartbeat to our peer. */
Wei Yongjun92c73af2011-04-19 21:31:47 +0000976 reply = sctp_make_heartbeat(asoc, transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (!reply)
978 return SCTP_DISPOSITION_NOMEM;
979
980 /* Set rto_pending indicating that an RTT measurement
981 * is started with this heartbeat chunk.
982 */
983 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
984 SCTP_TRANSPORT(transport));
985
986 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
987 return SCTP_DISPOSITION_CONSUME;
988}
989
990/* Generate a HEARTBEAT packet on the given transport. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000991sctp_disposition_t sctp_sf_sendbeat_8_3(struct net *net,
992 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 const struct sctp_association *asoc,
994 const sctp_subtype_t type,
995 void *arg,
996 sctp_cmd_seq_t *commands)
997{
998 struct sctp_transport *transport = (struct sctp_transport *) arg;
999
Vlad Yasevichb9f84782009-08-26 09:36:25 -04001000 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07001001 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
1002 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
1004 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08001005 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00001006 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
1007 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return SCTP_DISPOSITION_DELETE_TCB;
1009 }
1010
1011 /* Section 3.3.5.
1012 * The Sender-specific Heartbeat Info field should normally include
1013 * information about the sender's current time when this HEARTBEAT
1014 * chunk is sent and the destination transport address to which this
1015 * HEARTBEAT is sent (see Section 8.3).
1016 */
1017
Frank Filz52ccb8e2005-12-22 11:36:46 -08001018 if (transport->param_flags & SPP_HB_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (SCTP_DISPOSITION_NOMEM ==
1020 sctp_sf_heartbeat(ep, asoc, type, arg,
1021 commands))
1022 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevich245cba72009-11-23 15:53:58 -05001023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 /* Set transport error counter and association error counter
1025 * when sending heartbeat.
1026 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00001027 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 SCTP_TRANSPORT(transport));
1029 }
Vlad Yasevich245cba72009-11-23 15:53:58 -05001030 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1031 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1033 SCTP_TRANSPORT(transport));
1034
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001035 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
1038/*
1039 * Process an heartbeat request.
1040 *
1041 * Section: 8.3 Path Heartbeat
1042 * The receiver of the HEARTBEAT should immediately respond with a
1043 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1044 * from the received HEARTBEAT chunk.
1045 *
1046 * Verification Tag: 8.5 Verification Tag [Normal verification]
1047 * When receiving an SCTP packet, the endpoint MUST ensure that the
1048 * value in the Verification Tag field of the received SCTP packet
1049 * matches its own Tag. If the received Verification Tag value does not
1050 * match the receiver's own tag value, the receiver shall silently
1051 * discard the packet and shall not process it any further except for
1052 * those cases listed in Section 8.5.1 below.
1053 *
1054 * Inputs
1055 * (endpoint, asoc, chunk)
1056 *
1057 * Outputs
1058 * (asoc, reply_msg, msg_up, timers, counters)
1059 *
1060 * The return value is the disposition of the chunk.
1061 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001062sctp_disposition_t sctp_sf_beat_8_3(struct net *net,
1063 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 const struct sctp_association *asoc,
1065 const sctp_subtype_t type,
1066 void *arg,
1067 sctp_cmd_seq_t *commands)
1068{
Thomas Graf06a31e22012-11-30 02:16:27 +00001069 sctp_paramhdr_t *param_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 struct sctp_chunk *chunk = arg;
1071 struct sctp_chunk *reply;
1072 size_t paylen = 0;
1073
1074 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001075 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 /* Make sure that the HEARTBEAT chunk has a valid length. */
1078 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001079 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 commands);
1081
1082 /* 8.3 The receiver of the HEARTBEAT should immediately
1083 * respond with a HEARTBEAT ACK that contains the Heartbeat
1084 * Information field copied from the received HEARTBEAT chunk.
1085 */
1086 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
Thomas Graf06a31e22012-11-30 02:16:27 +00001087 param_hdr = (sctp_paramhdr_t *) chunk->subh.hb_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
Thomas Graf06a31e22012-11-30 02:16:27 +00001089
1090 if (ntohs(param_hdr->length) > paylen)
1091 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
1092 param_hdr, commands);
1093
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001094 if (!pskb_pull(chunk->skb, paylen))
1095 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Thomas Graf06a31e22012-11-30 02:16:27 +00001097 reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (!reply)
1099 goto nomem;
1100
1101 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1102 return SCTP_DISPOSITION_CONSUME;
1103
1104nomem:
1105 return SCTP_DISPOSITION_NOMEM;
1106}
1107
1108/*
1109 * Process the returning HEARTBEAT ACK.
1110 *
1111 * Section: 8.3 Path Heartbeat
1112 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1113 * should clear the error counter of the destination transport
1114 * address to which the HEARTBEAT was sent, and mark the destination
1115 * transport address as active if it is not so marked. The endpoint may
1116 * optionally report to the upper layer when an inactive destination
1117 * address is marked as active due to the reception of the latest
1118 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1119 * clear the association overall error count as well (as defined
1120 * in section 8.1).
1121 *
1122 * The receiver of the HEARTBEAT ACK should also perform an RTT
1123 * measurement for that destination transport address using the time
1124 * value carried in the HEARTBEAT ACK chunk.
1125 *
1126 * Verification Tag: 8.5 Verification Tag [Normal verification]
1127 *
1128 * Inputs
1129 * (endpoint, asoc, chunk)
1130 *
1131 * Outputs
1132 * (asoc, reply_msg, msg_up, timers, counters)
1133 *
1134 * The return value is the disposition of the chunk.
1135 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001136sctp_disposition_t sctp_sf_backbeat_8_3(struct net *net,
1137 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 const struct sctp_association *asoc,
1139 const sctp_subtype_t type,
1140 void *arg,
1141 sctp_cmd_seq_t *commands)
1142{
1143 struct sctp_chunk *chunk = arg;
1144 union sctp_addr from_addr;
1145 struct sctp_transport *link;
1146 sctp_sender_hb_info_t *hbinfo;
1147 unsigned long max_interval;
1148
1149 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001150 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
Wei Yongjundadb50c2009-08-22 11:27:37 +08001153 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t) +
1154 sizeof(sctp_sender_hb_info_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001155 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 commands);
1157
1158 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
Vladislav Yasevicha6012662006-05-19 14:25:53 -07001159 /* Make sure that the length of the parameter is what we expect */
1160 if (ntohs(hbinfo->param_hdr.length) !=
1161 sizeof(sctp_sender_hb_info_t)) {
1162 return SCTP_DISPOSITION_DISCARD;
1163 }
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 from_addr = hbinfo->daddr;
Al Viro63de08f2006-11-20 17:07:25 -08001166 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 /* This should never happen, but lets log it if so. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001169 if (unlikely(!link)) {
1170 if (from_addr.sa.sa_family == AF_INET6) {
Joe Perchese87cc472012-05-13 21:56:26 +00001171 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1172 __func__,
1173 asoc,
1174 &from_addr.v6.sin6_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001175 } else {
Joe Perchese87cc472012-05-13 21:56:26 +00001176 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1177 __func__,
1178 asoc,
1179 &from_addr.v4.sin_addr.s_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 return SCTP_DISPOSITION_DISCARD;
1182 }
1183
Sridhar Samudralaad8fec12006-07-21 14:48:50 -07001184 /* Validate the 64-bit random nonce. */
1185 if (hbinfo->hb_nonce != link->hb_nonce)
1186 return SCTP_DISPOSITION_DISCARD;
1187
Frank Filz52ccb8e2005-12-22 11:36:46 -08001188 max_interval = link->hbinterval + link->rto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 /* Check if the timestamp looks valid. */
1191 if (time_after(hbinfo->sent_at, jiffies) ||
1192 time_after(jiffies, hbinfo->sent_at + max_interval)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001193 pr_debug("%s: HEARTBEAT ACK with invalid timestamp received "
1194 "for transport:%p\n", __func__, link);
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return SCTP_DISPOSITION_DISCARD;
1197 }
1198
1199 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1200 * the HEARTBEAT should clear the error counter of the
1201 * destination transport address to which the HEARTBEAT was
1202 * sent and mark the destination transport address as active if
1203 * it is not so marked.
1204 */
1205 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1206
1207 return SCTP_DISPOSITION_CONSUME;
1208}
1209
1210/* Helper function to send out an abort for the restart
1211 * condition.
1212 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001213static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 struct sctp_chunk *init,
1215 sctp_cmd_seq_t *commands)
1216{
1217 int len;
1218 struct sctp_packet *pkt;
1219 union sctp_addr_param *addrparm;
1220 struct sctp_errhdr *errhdr;
1221 struct sctp_endpoint *ep;
1222 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1223 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1224
1225 /* Build the error on the stack. We are way to malloc crazy
1226 * throughout the code today.
1227 */
1228 errhdr = (struct sctp_errhdr *)buffer;
1229 addrparm = (union sctp_addr_param *)errhdr->variable;
1230
1231 /* Copy into a parm format. */
1232 len = af->to_addr_param(ssa, addrparm);
1233 len += sizeof(sctp_errhdr_t);
1234
1235 errhdr->cause = SCTP_ERROR_RESTART;
1236 errhdr->length = htons(len);
1237
1238 /* Assign to the control socket. */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001239 ep = sctp_sk(net->sctp.ctl_sock)->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 /* Association is NULL since this may be a restart attack and we
1242 * want to send back the attacker's vtag.
1243 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001244 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 if (!pkt)
1247 goto out;
1248 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1249
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00001250 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 /* Discard the rest of the inbound packet. */
1253 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1254
1255out:
1256 /* Even if there is no memory, treat as a failure so
1257 * the packet will get dropped.
1258 */
1259 return 0;
1260}
1261
Joe Perches123031c2010-09-08 11:04:21 +00001262static bool list_has_sctp_addr(const struct list_head *list,
1263 union sctp_addr *ipaddr)
1264{
1265 struct sctp_transport *addr;
1266
1267 list_for_each_entry(addr, list, transports) {
1268 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1269 return true;
1270 }
1271
1272 return false;
1273}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274/* A restart is occurring, check to make sure no new addresses
1275 * are being added as we may be under a takeover attack.
1276 */
1277static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1278 const struct sctp_association *asoc,
1279 struct sctp_chunk *init,
1280 sctp_cmd_seq_t *commands)
1281{
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001282 struct net *net = sock_net(new_asoc->base.sk);
Joe Perches123031c2010-09-08 11:04:21 +00001283 struct sctp_transport *new_addr;
1284 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Joe Perches123031c2010-09-08 11:04:21 +00001286 /* Implementor's Guide - Section 5.2.2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 * ...
1288 * Before responding the endpoint MUST check to see if the
1289 * unexpected INIT adds new addresses to the association. If new
1290 * addresses are added to the association, the endpoint MUST respond
1291 * with an ABORT..
1292 */
1293
1294 /* Search through all current addresses and make sure
1295 * we aren't adding any new ones.
1296 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001297 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
Joe Perches123031c2010-09-08 11:04:21 +00001298 transports) {
1299 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1300 &new_addr->ipaddr)) {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001301 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
Joe Perches123031c2010-09-08 11:04:21 +00001302 commands);
1303 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 break;
Joe Perches123031c2010-09-08 11:04:21 +00001305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307
1308 /* Return success if all addresses were found. */
Joe Perches123031c2010-09-08 11:04:21 +00001309 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
1311
1312/* Populate the verification/tie tags based on overlapping INIT
1313 * scenario.
1314 *
1315 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1316 */
1317static void sctp_tietags_populate(struct sctp_association *new_asoc,
1318 const struct sctp_association *asoc)
1319{
1320 switch (asoc->state) {
1321
1322 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1323
1324 case SCTP_STATE_COOKIE_WAIT:
1325 new_asoc->c.my_vtag = asoc->c.my_vtag;
1326 new_asoc->c.my_ttag = asoc->c.my_vtag;
1327 new_asoc->c.peer_ttag = 0;
1328 break;
1329
1330 case SCTP_STATE_COOKIE_ECHOED:
1331 new_asoc->c.my_vtag = asoc->c.my_vtag;
1332 new_asoc->c.my_ttag = asoc->c.my_vtag;
1333 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1334 break;
1335
1336 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1337 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1338 */
1339 default:
1340 new_asoc->c.my_ttag = asoc->c.my_vtag;
1341 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1342 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 /* Other parameters for the endpoint SHOULD be copied from the
1346 * existing parameters of the association (e.g. number of
1347 * outbound streams) into the INIT ACK and cookie.
1348 */
1349 new_asoc->rwnd = asoc->rwnd;
1350 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1351 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1352 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1353}
1354
1355/*
1356 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1357 * handling action.
1358 *
1359 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1360 *
1361 * Returns value representing action to be taken. These action values
1362 * correspond to Action/Description values in RFC 2960, Table 2.
1363 */
1364static char sctp_tietags_compare(struct sctp_association *new_asoc,
1365 const struct sctp_association *asoc)
1366{
1367 /* In this case, the peer may have restarted. */
1368 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1369 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1370 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1371 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1372 return 'A';
1373
1374 /* Collision case B. */
1375 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1376 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1377 (0 == asoc->c.peer_vtag))) {
1378 return 'B';
1379 }
1380
1381 /* Collision case D. */
1382 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1383 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1384 return 'D';
1385
1386 /* Collision case C. */
1387 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1388 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1389 (0 == new_asoc->c.my_ttag) &&
1390 (0 == new_asoc->c.peer_ttag))
1391 return 'C';
1392
1393 /* No match to any of the special cases; discard this packet. */
1394 return 'E';
1395}
1396
1397/* Common helper routine for both duplicate and simulataneous INIT
1398 * chunk handling.
1399 */
1400static sctp_disposition_t sctp_sf_do_unexpected_init(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001401 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 const struct sctp_endpoint *ep,
1403 const struct sctp_association *asoc,
1404 const sctp_subtype_t type,
1405 void *arg, sctp_cmd_seq_t *commands)
1406{
1407 sctp_disposition_t retval;
1408 struct sctp_chunk *chunk = arg;
1409 struct sctp_chunk *repl;
1410 struct sctp_association *new_asoc;
1411 struct sctp_chunk *err_chunk;
1412 struct sctp_packet *packet;
1413 sctp_unrecognized_param_t *unk_param;
1414 int len;
1415
1416 /* 6.10 Bundling
1417 * An endpoint MUST NOT bundle INIT, INIT ACK or
1418 * SHUTDOWN COMPLETE with any other chunks.
1419 *
1420 * IG Section 2.11.2
1421 * Furthermore, we require that the receiver of an INIT chunk MUST
1422 * enforce these rules by silently discarding an arriving packet
1423 * with an INIT chunk that is bundled with other chunks.
1424 */
1425 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001426 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001429 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 */
1431 if (chunk->sctp_hdr->vtag != 0)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001432 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 /* Make sure that the INIT chunk has a valid length.
1435 * In this case, we generate a protocol violation since we have
1436 * an association established.
1437 */
1438 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001439 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 commands);
1441 /* Grab the INIT header. */
1442 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1443
1444 /* Tag the variable length parameters. */
1445 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1446
1447 /* Verify the INIT chunk before processing it. */
1448 err_chunk = NULL;
Vlad Yasevichb14878c2014-04-17 17:26:50 +02001449 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1451 &err_chunk)) {
1452 /* This chunk contains fatal error. It is to be discarded.
1453 * Send an ABORT, with causes if there is any.
1454 */
1455 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001456 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 (__u8 *)(err_chunk->chunk_hdr) +
1458 sizeof(sctp_chunkhdr_t),
1459 ntohs(err_chunk->chunk_hdr->length) -
1460 sizeof(sctp_chunkhdr_t));
1461
1462 if (packet) {
1463 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1464 SCTP_PACKET(packet));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001465 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 retval = SCTP_DISPOSITION_CONSUME;
1467 } else {
1468 retval = SCTP_DISPOSITION_NOMEM;
1469 }
1470 goto cleanup;
1471 } else {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001472 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 commands);
1474 }
1475 }
1476
1477 /*
1478 * Other parameters for the endpoint SHOULD be copied from the
1479 * existing parameters of the association (e.g. number of
1480 * outbound streams) into the INIT ACK and cookie.
1481 * FIXME: We are copying parameters from the endpoint not the
1482 * association.
1483 */
1484 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1485 if (!new_asoc)
1486 goto nomem;
1487
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001488 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1489 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1490 goto nomem;
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 /* In the outbound INIT ACK the endpoint MUST copy its current
1493 * Verification Tag and Peers Verification tag into a reserved
1494 * place (local tie-tag and per tie-tag) within the state cookie.
1495 */
Wei Yongjunde6becd2011-04-19 21:30:51 +00001496 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 (sctp_init_chunk_t *)chunk->chunk_hdr,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001498 GFP_ATOMIC))
1499 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 /* Make sure no new addresses are being added during the
1502 * restart. Do not do this check for COOKIE-WAIT state,
1503 * since there are no peer addresses to check against.
1504 * Upon return an ABORT will have been sent if needed.
1505 */
1506 if (!sctp_state(asoc, COOKIE_WAIT)) {
1507 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1508 commands)) {
1509 retval = SCTP_DISPOSITION_CONSUME;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001510 goto nomem_retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 }
1512 }
1513
1514 sctp_tietags_populate(new_asoc, asoc);
1515
1516 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1517
1518 /* If there are errors need to be reported for unknown parameters,
1519 * make sure to reserve enough room in the INIT ACK for them.
1520 */
1521 len = 0;
1522 if (err_chunk) {
1523 len = ntohs(err_chunk->chunk_hdr->length) -
1524 sizeof(sctp_chunkhdr_t);
1525 }
1526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1528 if (!repl)
1529 goto nomem;
1530
1531 /* If there are errors need to be reported for unknown parameters,
1532 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1533 * parameter.
1534 */
1535 if (err_chunk) {
1536 /* Get the "Unrecognized parameter" parameter(s) out of the
1537 * ERROR chunk generated by sctp_verify_init(). Since the
1538 * error cause code for "unknown parameter" and the
1539 * "Unrecognized parameter" type is the same, we can
1540 * construct the parameters in INIT ACK by copying the
1541 * ERROR causes over.
1542 */
1543 unk_param = (sctp_unrecognized_param_t *)
1544 ((__u8 *)(err_chunk->chunk_hdr) +
1545 sizeof(sctp_chunkhdr_t));
1546 /* Replace the cause code with the "Unrecognized parameter"
1547 * parameter type.
1548 */
1549 sctp_addto_chunk(repl, len, unk_param);
1550 }
1551
1552 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1553 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1554
1555 /*
1556 * Note: After sending out INIT ACK with the State Cookie parameter,
1557 * "Z" MUST NOT allocate any resources for this new association.
1558 * Otherwise, "Z" will be vulnerable to resource attacks.
1559 */
1560 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1561 retval = SCTP_DISPOSITION_CONSUME;
1562
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001563 return retval;
1564
1565nomem:
1566 retval = SCTP_DISPOSITION_NOMEM;
1567nomem_retval:
1568 if (new_asoc)
1569 sctp_association_free(new_asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570cleanup:
1571 if (err_chunk)
1572 sctp_chunk_free(err_chunk);
1573 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
1575
1576/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001577 * Handle simultaneous INIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 * This means we started an INIT and then we got an INIT request from
1579 * our peer.
1580 *
1581 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1582 * This usually indicates an initialization collision, i.e., each
1583 * endpoint is attempting, at about the same time, to establish an
1584 * association with the other endpoint.
1585 *
1586 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1587 * endpoint MUST respond with an INIT ACK using the same parameters it
1588 * sent in its original INIT chunk (including its Verification Tag,
1589 * unchanged). These original parameters are combined with those from the
1590 * newly received INIT chunk. The endpoint shall also generate a State
1591 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1592 * INIT to calculate the State Cookie.
1593 *
1594 * After that, the endpoint MUST NOT change its state, the T1-init
1595 * timer shall be left running and the corresponding TCB MUST NOT be
1596 * destroyed. The normal procedures for handling State Cookies when
1597 * a TCB exists will resolve the duplicate INITs to a single association.
1598 *
1599 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1600 * its Tie-Tags with the Tag information of itself and its peer (see
1601 * section 5.2.2 for a description of the Tie-Tags).
1602 *
1603 * Verification Tag: Not explicit, but an INIT can not have a valid
1604 * verification tag, so we skip the check.
1605 *
1606 * Inputs
1607 * (endpoint, asoc, chunk)
1608 *
1609 * Outputs
1610 * (asoc, reply_msg, msg_up, timers, counters)
1611 *
1612 * The return value is the disposition of the chunk.
1613 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001614sctp_disposition_t sctp_sf_do_5_2_1_siminit(struct net *net,
1615 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 const struct sctp_association *asoc,
1617 const sctp_subtype_t type,
1618 void *arg,
1619 sctp_cmd_seq_t *commands)
1620{
1621 /* Call helper to do the real work for both simulataneous and
1622 * duplicate INIT chunk handling.
1623 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001624 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
1626
1627/*
1628 * Handle duplicated INIT messages. These are usually delayed
1629 * restransmissions.
1630 *
1631 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1632 * COOKIE-ECHOED and COOKIE-WAIT
1633 *
1634 * Unless otherwise stated, upon reception of an unexpected INIT for
1635 * this association, the endpoint shall generate an INIT ACK with a
1636 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1637 * current Verification Tag and peer's Verification Tag into a reserved
1638 * place within the state cookie. We shall refer to these locations as
1639 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1640 * containing this INIT ACK MUST carry a Verification Tag value equal to
1641 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1642 * MUST contain a new Initiation Tag (randomly generated see Section
1643 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1644 * existing parameters of the association (e.g. number of outbound
1645 * streams) into the INIT ACK and cookie.
1646 *
1647 * After sending out the INIT ACK, the endpoint shall take no further
1648 * actions, i.e., the existing association, including its current state,
1649 * and the corresponding TCB MUST NOT be changed.
1650 *
1651 * Note: Only when a TCB exists and the association is not in a COOKIE-
1652 * WAIT state are the Tie-Tags populated. For a normal association INIT
1653 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1654 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1655 * State Cookie are populated as specified in section 5.2.1.
1656 *
1657 * Verification Tag: Not specified, but an INIT has no way of knowing
1658 * what the verification tag could be, so we ignore it.
1659 *
1660 * Inputs
1661 * (endpoint, asoc, chunk)
1662 *
1663 * Outputs
1664 * (asoc, reply_msg, msg_up, timers, counters)
1665 *
1666 * The return value is the disposition of the chunk.
1667 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001668sctp_disposition_t sctp_sf_do_5_2_2_dupinit(struct net *net,
1669 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 const struct sctp_association *asoc,
1671 const sctp_subtype_t type,
1672 void *arg,
1673 sctp_cmd_seq_t *commands)
1674{
1675 /* Call helper to do the real work for both simulataneous and
1676 * duplicate INIT chunk handling.
1677 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001678 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
1680
1681
Vlad Yasevich610ab732007-01-15 19:18:30 -08001682/*
1683 * Unexpected INIT-ACK handler.
1684 *
1685 * Section 5.2.3
1686 * If an INIT ACK received by an endpoint in any state other than the
1687 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1688 * An unexpected INIT ACK usually indicates the processing of an old or
1689 * duplicated INIT chunk.
1690*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001691sctp_disposition_t sctp_sf_do_5_2_3_initack(struct net *net,
1692 const struct sctp_endpoint *ep,
Vlad Yasevich610ab732007-01-15 19:18:30 -08001693 const struct sctp_association *asoc,
1694 const sctp_subtype_t type,
1695 void *arg, sctp_cmd_seq_t *commands)
1696{
1697 /* Per the above section, we'll discard the chunk if we have an
1698 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1699 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001700 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001701 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001702 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001703 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001704}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706/* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1707 *
1708 * Section 5.2.4
1709 * A) In this case, the peer may have restarted.
1710 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001711static sctp_disposition_t sctp_sf_do_dupcook_a(struct net *net,
1712 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 const struct sctp_association *asoc,
1714 struct sctp_chunk *chunk,
1715 sctp_cmd_seq_t *commands,
1716 struct sctp_association *new_asoc)
1717{
1718 sctp_init_chunk_t *peer_init;
1719 struct sctp_ulpevent *ev;
1720 struct sctp_chunk *repl;
1721 struct sctp_chunk *err;
1722 sctp_disposition_t disposition;
1723
1724 /* new_asoc is a brand-new association, so these are not yet
1725 * side effects--it is safe to run them here.
1726 */
1727 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1728
Wei Yongjunde6becd2011-04-19 21:30:51 +00001729 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 GFP_ATOMIC))
1731 goto nomem;
1732
Xin Long0e67ad52018-05-05 14:59:47 +08001733 if (!sctp_auth_chunk_verify(net, chunk, new_asoc))
1734 return SCTP_DISPOSITION_DISCARD;
1735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 /* Make sure no new addresses are being added during the
1737 * restart. Though this is a pretty complicated attack
1738 * since you'd have to get inside the cookie.
1739 */
Xin Long0e67ad52018-05-05 14:59:47 +08001740 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
1743 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1744 * the peer has restarted (Action A), it MUST NOT setup a new
1745 * association but instead resend the SHUTDOWN ACK and send an ERROR
1746 * chunk with a "Cookie Received while Shutting Down" error cause to
1747 * its peer.
1748 */
1749 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001750 disposition = sctp_sf_do_9_2_reshutack(net, ep, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1752 chunk, commands);
1753 if (SCTP_DISPOSITION_NOMEM == disposition)
1754 goto nomem;
1755
1756 err = sctp_make_op_error(asoc, chunk,
1757 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001758 NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 if (err)
1760 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1761 SCTP_CHUNK(err));
1762
1763 return SCTP_DISPOSITION_CONSUME;
1764 }
1765
Wei Yongjuna000c012011-05-29 23:23:36 +00001766 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1767 * data. Consider the optional choice of resending of this data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 */
Wei Yongjuna000c012011-05-29 23:23:36 +00001769 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1770 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1771 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1773
Wei Yongjuna000c012011-05-29 23:23:36 +00001774 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1775 * and ASCONF-ACK cache.
1776 */
1777 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1778 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1779 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 repl = sctp_make_cookie_ack(new_asoc, chunk);
1782 if (!repl)
1783 goto nomem;
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 /* Report association restart to upper layer. */
1786 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1787 new_asoc->c.sinit_num_ostreams,
1788 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07001789 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if (!ev)
1791 goto nomem_ev;
1792
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001793 /* Update the content of current association. */
1794 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
Vlad Yasevichbdf6fa52014-10-03 18:16:20 -04001796 if (sctp_state(asoc, SHUTDOWN_PENDING) &&
1797 (sctp_sstate(asoc->base.sk, CLOSING) ||
1798 sock_flag(asoc->base.sk, SOCK_DEAD))) {
1799 /* if were currently in SHUTDOWN_PENDING, but the socket
1800 * has been closed by user, don't transition to ESTABLISHED.
1801 * Instead trigger SHUTDOWN bundled with COOKIE_ACK.
1802 */
1803 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1804 return sctp_sf_do_9_2_start_shutdown(net, ep, asoc,
1805 SCTP_ST_CHUNK(0), NULL,
1806 commands);
1807 } else {
1808 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1809 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1810 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return SCTP_DISPOSITION_CONSUME;
1813
1814nomem_ev:
1815 sctp_chunk_free(repl);
1816nomem:
1817 return SCTP_DISPOSITION_NOMEM;
1818}
1819
1820/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1821 *
1822 * Section 5.2.4
1823 * B) In this case, both sides may be attempting to start an association
1824 * at about the same time but the peer endpoint started its INIT
1825 * after responding to the local endpoint's INIT
1826 */
1827/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001828static sctp_disposition_t sctp_sf_do_dupcook_b(struct net *net,
1829 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 const struct sctp_association *asoc,
1831 struct sctp_chunk *chunk,
1832 sctp_cmd_seq_t *commands,
1833 struct sctp_association *new_asoc)
1834{
1835 sctp_init_chunk_t *peer_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 struct sctp_chunk *repl;
1837
1838 /* new_asoc is a brand-new association, so these are not yet
1839 * side effects--it is safe to run them here.
1840 */
1841 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
Wei Yongjunde6becd2011-04-19 21:30:51 +00001842 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 GFP_ATOMIC))
1844 goto nomem;
1845
Xin Long0e67ad52018-05-05 14:59:47 +08001846 if (!sctp_auth_chunk_verify(net, chunk, new_asoc))
1847 return SCTP_DISPOSITION_DISCARD;
1848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 /* Update the content of current association. */
1850 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1851 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1852 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001853 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1855
1856 repl = sctp_make_cookie_ack(new_asoc, chunk);
1857 if (!repl)
1858 goto nomem;
1859
1860 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862 /* RFC 2960 5.1 Normal Establishment of an Association
1863 *
1864 * D) IMPLEMENTATION NOTE: An implementation may choose to
1865 * send the Communication Up notification to the SCTP user
1866 * upon reception of a valid COOKIE ECHO chunk.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001867 *
1868 * Sadly, this needs to be implemented as a side-effect, because
1869 * we are not guaranteed to have set the association id of the real
1870 * association and so these notifications need to be delayed until
1871 * the association id is allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
Vlad Yasevich07d93962007-05-04 13:55:27 -07001874 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001877 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001879 * peers requested adaptation layer.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001880 *
1881 * This also needs to be done as a side effect for the same reason as
1882 * above.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 */
Vlad Yasevich07d93962007-05-04 13:55:27 -07001884 if (asoc->peer.adaptation_ind)
1885 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 return SCTP_DISPOSITION_CONSUME;
1888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889nomem:
1890 return SCTP_DISPOSITION_NOMEM;
1891}
1892
1893/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1894 *
1895 * Section 5.2.4
1896 * C) In this case, the local endpoint's cookie has arrived late.
1897 * Before it arrived, the local endpoint sent an INIT and received an
1898 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1899 * but a new tag of its own.
1900 */
1901/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001902static sctp_disposition_t sctp_sf_do_dupcook_c(struct net *net,
1903 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 const struct sctp_association *asoc,
1905 struct sctp_chunk *chunk,
1906 sctp_cmd_seq_t *commands,
1907 struct sctp_association *new_asoc)
1908{
1909 /* The cookie should be silently discarded.
1910 * The endpoint SHOULD NOT change states and should leave
1911 * any timers running.
1912 */
1913 return SCTP_DISPOSITION_DISCARD;
1914}
1915
1916/* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1917 *
1918 * Section 5.2.4
1919 *
1920 * D) When both local and remote tags match the endpoint should always
1921 * enter the ESTABLISHED state, if it has not already done so.
1922 */
1923/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001924static sctp_disposition_t sctp_sf_do_dupcook_d(struct net *net,
1925 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 const struct sctp_association *asoc,
1927 struct sctp_chunk *chunk,
1928 sctp_cmd_seq_t *commands,
1929 struct sctp_association *new_asoc)
1930{
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001931 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 struct sctp_chunk *repl;
1933
1934 /* Clarification from Implementor's Guide:
1935 * D) When both local and remote tags match the endpoint should
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001936 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1937 * It should stop any cookie timer that may be running and send
1938 * a COOKIE ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 */
1940
Xin Long0e67ad52018-05-05 14:59:47 +08001941 if (!sctp_auth_chunk_verify(net, chunk, asoc))
1942 return SCTP_DISPOSITION_DISCARD;
1943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 /* Don't accidentally move back into established state. */
1945 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1946 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1947 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1948 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1949 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001950 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1952 SCTP_NULL());
1953
1954 /* RFC 2960 5.1 Normal Establishment of an Association
1955 *
1956 * D) IMPLEMENTATION NOTE: An implementation may choose
1957 * to send the Communication Up notification to the
1958 * SCTP user upon reception of a valid COOKIE
1959 * ECHO chunk.
1960 */
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001961 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 SCTP_COMM_UP, 0,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001963 asoc->c.sinit_num_ostreams,
1964 asoc->c.sinit_max_instreams,
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09001965 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 if (!ev)
1967 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001970 * When a peer sends a Adaptation Layer Indication parameter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 * SCTP delivers this notification to inform the application
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001972 * that of the peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001974 if (asoc->peer.adaptation_ind) {
1975 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001977 if (!ai_ev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 goto nomem;
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
1981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Xin Longc832ac42018-05-02 13:39:46 +08001983 repl = sctp_make_cookie_ack(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 if (!repl)
1985 goto nomem;
1986
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07001987 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1988
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001989 if (ev)
1990 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1991 SCTP_ULPEVENT(ev));
1992 if (ai_ev)
1993 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1994 SCTP_ULPEVENT(ai_ev));
1995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 return SCTP_DISPOSITION_CONSUME;
1997
1998nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001999 if (ai_ev)
2000 sctp_ulpevent_free(ai_ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 if (ev)
2002 sctp_ulpevent_free(ev);
2003 return SCTP_DISPOSITION_NOMEM;
2004}
2005
2006/*
2007 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
2008 * chunk was retransmitted and then delayed in the network.
2009 *
2010 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
2011 *
2012 * Verification Tag: None. Do cookie validation.
2013 *
2014 * Inputs
2015 * (endpoint, asoc, chunk)
2016 *
2017 * Outputs
2018 * (asoc, reply_msg, msg_up, timers, counters)
2019 *
2020 * The return value is the disposition of the chunk.
2021 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002022sctp_disposition_t sctp_sf_do_5_2_4_dupcook(struct net *net,
2023 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 const struct sctp_association *asoc,
2025 const sctp_subtype_t type,
2026 void *arg,
2027 sctp_cmd_seq_t *commands)
2028{
2029 sctp_disposition_t retval;
2030 struct sctp_chunk *chunk = arg;
2031 struct sctp_association *new_asoc;
2032 int error = 0;
2033 char action;
2034 struct sctp_chunk *err_chk_p;
2035
2036 /* Make sure that the chunk has a valid length from the protocol
2037 * perspective. In this case check to make sure we have at least
2038 * enough for the chunk header. Cookie length verification is
2039 * done later.
2040 */
2041 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002042 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 commands);
2044
2045 /* "Decode" the chunk. We have no optional parameters so we
2046 * are in good shape.
2047 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002048 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -07002049 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
2050 sizeof(sctp_chunkhdr_t)))
2051 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2054 * of a duplicate COOKIE ECHO match the Verification Tags of the
2055 * current association, consider the State Cookie valid even if
2056 * the lifespan is exceeded.
2057 */
2058 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2059 &err_chk_p);
2060
2061 /* FIXME:
2062 * If the re-build failed, what is the proper error path
2063 * from here?
2064 *
2065 * [We should abort the association. --piggy]
2066 */
2067 if (!new_asoc) {
2068 /* FIXME: Several errors are possible. A bad cookie should
2069 * be silently discarded, but think about logging it too.
2070 */
2071 switch (error) {
2072 case -SCTP_IERROR_NOMEM:
2073 goto nomem;
2074
2075 case -SCTP_IERROR_STALE_COOKIE:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002076 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 err_chk_p);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002078 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 case -SCTP_IERROR_BAD_SIG:
2080 default:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002081 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 }
2084
2085 /* Compare the tie_tag in cookie with the verification tag of
2086 * current association.
2087 */
2088 action = sctp_tietags_compare(new_asoc, asoc);
2089
2090 switch (action) {
2091 case 'A': /* Association restart. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002092 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 new_asoc);
2094 break;
2095
2096 case 'B': /* Collision case B. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002097 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 new_asoc);
2099 break;
2100
2101 case 'C': /* Collision case C. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002102 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 new_asoc);
2104 break;
2105
2106 case 'D': /* Collision case D. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002107 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 new_asoc);
2109 break;
2110
2111 default: /* Discard packet for all others. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002112 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116 /* Delete the tempory new association. */
Vlad Yasevichf2815632013-03-12 15:53:23 +00002117 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2119
Max Matveevd5ccd492011-08-29 21:02:24 +00002120 /* Restore association pointer to provide SCTP command interpeter
2121 * with a valid context in case it needs to manipulate
2122 * the queues */
2123 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2124 SCTP_ASOC((struct sctp_association *)asoc));
2125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 return retval;
2127
2128nomem:
2129 return SCTP_DISPOSITION_NOMEM;
2130}
2131
2132/*
2133 * Process an ABORT. (SHUTDOWN-PENDING state)
2134 *
2135 * See sctp_sf_do_9_1_abort().
2136 */
2137sctp_disposition_t sctp_sf_shutdown_pending_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002138 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 const struct sctp_endpoint *ep,
2140 const struct sctp_association *asoc,
2141 const sctp_subtype_t type,
2142 void *arg,
2143 sctp_cmd_seq_t *commands)
2144{
2145 struct sctp_chunk *chunk = arg;
2146
2147 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002148 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 /* Make sure that the ABORT chunk has a valid length.
2151 * Since this is an ABORT chunk, we have to discard it
2152 * because of the following text:
2153 * RFC 2960, Section 3.3.7
2154 * If an endpoint receives an ABORT with a format error or for an
2155 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002156 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 * as we do not know its true length. So, to be safe, discard the
2158 * packet.
2159 */
2160 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002161 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Vlad Yasevich75205f42007-12-20 14:12:59 -08002163 /* ADD-IP: Special case for ABORT chunks
2164 * F4) One special consideration is that ABORT Chunks arriving
2165 * destined to the IP address being deleted MUST be
2166 * ignored (see Section 5.3.1 for further details).
2167 */
2168 if (SCTP_ADDR_DEL ==
2169 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002170 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002171
Xin Long548e23f2020-02-18 12:07:53 +08002172 if (!sctp_err_chunk_valid(chunk))
2173 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2174
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002175 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176}
2177
2178/*
2179 * Process an ABORT. (SHUTDOWN-SENT state)
2180 *
2181 * See sctp_sf_do_9_1_abort().
2182 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002183sctp_disposition_t sctp_sf_shutdown_sent_abort(struct net *net,
2184 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 const struct sctp_association *asoc,
2186 const sctp_subtype_t type,
2187 void *arg,
2188 sctp_cmd_seq_t *commands)
2189{
2190 struct sctp_chunk *chunk = arg;
2191
2192 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002193 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
2195 /* Make sure that the ABORT chunk has a valid length.
2196 * Since this is an ABORT chunk, we have to discard it
2197 * because of the following text:
2198 * RFC 2960, Section 3.3.7
2199 * If an endpoint receives an ABORT with a format error or for an
2200 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002201 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 * as we do not know its true length. So, to be safe, discard the
2203 * packet.
2204 */
2205 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002206 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Vlad Yasevich75205f42007-12-20 14:12:59 -08002208 /* ADD-IP: Special case for ABORT chunks
2209 * F4) One special consideration is that ABORT Chunks arriving
2210 * destined to the IP address being deleted MUST be
2211 * ignored (see Section 5.3.1 for further details).
2212 */
2213 if (SCTP_ADDR_DEL ==
2214 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002215 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002216
Xin Long548e23f2020-02-18 12:07:53 +08002217 if (!sctp_err_chunk_valid(chunk))
2218 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 /* Stop the T2-shutdown timer. */
2221 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2222 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2223
2224 /* Stop the T5-shutdown guard timer. */
2225 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2226 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2227
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002228 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229}
2230
2231/*
2232 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2233 *
2234 * See sctp_sf_do_9_1_abort().
2235 */
2236sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002237 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 const struct sctp_endpoint *ep,
2239 const struct sctp_association *asoc,
2240 const sctp_subtype_t type,
2241 void *arg,
2242 sctp_cmd_seq_t *commands)
2243{
2244 /* The same T2 timer, so we should be able to use
2245 * common function with the SHUTDOWN-SENT state.
2246 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002247 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248}
2249
2250/*
2251 * Handle an Error received in COOKIE_ECHOED state.
2252 *
2253 * Only handle the error type of stale COOKIE Error, the other errors will
2254 * be ignored.
2255 *
2256 * Inputs
2257 * (endpoint, asoc, chunk)
2258 *
2259 * Outputs
2260 * (asoc, reply_msg, msg_up, timers, counters)
2261 *
2262 * The return value is the disposition of the chunk.
2263 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002264sctp_disposition_t sctp_sf_cookie_echoed_err(struct net *net,
2265 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 const struct sctp_association *asoc,
2267 const sctp_subtype_t type,
2268 void *arg,
2269 sctp_cmd_seq_t *commands)
2270{
2271 struct sctp_chunk *chunk = arg;
2272 sctp_errhdr_t *err;
2273
2274 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002275 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
2277 /* Make sure that the ERROR chunk has a valid length.
2278 * The parameter walking depends on this as well.
2279 */
2280 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002281 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 commands);
2283
2284 /* Process the error here */
2285 /* FUTURE FIXME: When PR-SCTP related and other optional
2286 * parms are emitted, this will have to change to handle multiple
2287 * errors.
2288 */
2289 sctp_walk_errors(err, chunk->chunk_hdr) {
2290 if (SCTP_ERROR_STALE_COOKIE == err->cause)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002291 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 arg, commands);
2293 }
2294
2295 /* It is possible to have malformed error causes, and that
2296 * will cause us to end the walk early. However, since
2297 * we are discarding the packet, there should be no adverse
2298 * affects.
2299 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002300 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301}
2302
2303/*
2304 * Handle a Stale COOKIE Error
2305 *
2306 * Section: 5.2.6 Handle Stale COOKIE Error
2307 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2308 * one of the following three alternatives.
2309 * ...
2310 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2311 * Preservative parameter requesting an extension to the lifetime of
2312 * the State Cookie. When calculating the time extension, an
2313 * implementation SHOULD use the RTT information measured based on the
2314 * previous COOKIE ECHO / ERROR exchange, and should add no more
2315 * than 1 second beyond the measured RTT, due to long State Cookie
2316 * lifetimes making the endpoint more subject to a replay attack.
2317 *
2318 * Verification Tag: Not explicit, but safe to ignore.
2319 *
2320 * Inputs
2321 * (endpoint, asoc, chunk)
2322 *
2323 * Outputs
2324 * (asoc, reply_msg, msg_up, timers, counters)
2325 *
2326 * The return value is the disposition of the chunk.
2327 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002328static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
2329 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 const struct sctp_association *asoc,
2331 const sctp_subtype_t type,
2332 void *arg,
2333 sctp_cmd_seq_t *commands)
2334{
2335 struct sctp_chunk *chunk = arg;
Arnd Bergmann3ef0a252015-09-30 13:26:40 +02002336 u32 stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 sctp_cookie_preserve_param_t bht;
2338 sctp_errhdr_t *err;
2339 struct sctp_chunk *reply;
2340 struct sctp_bind_addr *bp;
Frank Filz3f7a87d2005-06-20 13:14:57 -07002341 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Vlad Yasevich81845c22006-01-30 15:59:54 -08002343 if (attempts > asoc->max_init_attempts) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002344 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2345 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002347 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return SCTP_DISPOSITION_DELETE_TCB;
2349 }
2350
2351 err = (sctp_errhdr_t *)(chunk->skb->data);
2352
2353 /* When calculating the time extension, an implementation
2354 * SHOULD use the RTT information measured based on the
2355 * previous COOKIE ECHO / ERROR exchange, and should add no
2356 * more than 1 second beyond the measured RTT, due to long
2357 * State Cookie lifetimes making the endpoint more subject to
2358 * a replay attack.
2359 * Measure of Staleness's unit is usec. (1/1000000 sec)
2360 * Suggested Cookie Life-span Increment's unit is msec.
2361 * (1/1000 sec)
2362 * In general, if you use the suggested cookie life, the value
2363 * found in the field of measure of staleness should be doubled
2364 * to give ample time to retransmit the new cookie and thus
2365 * yield a higher probability of success on the reattempt.
2366 */
Al Viro34bcca22006-11-20 17:27:15 -08002367 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 stale = (stale * 2) / 1000;
2369
2370 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2371 bht.param_hdr.length = htons(sizeof(bht));
2372 bht.lifespan_increment = htonl(stale);
2373
2374 /* Build that new INIT chunk. */
2375 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2376 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2377 if (!reply)
2378 goto nomem;
2379
2380 sctp_addto_chunk(reply, sizeof(bht), &bht);
2381
2382 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2383 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2384
2385 /* Stop pending T3-rtx and heartbeat timers */
2386 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2387 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2388
2389 /* Delete non-primary peer ip addresses since we are transitioning
2390 * back to the COOKIE-WAIT state
2391 */
2392 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2393
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002394 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2395 * resend
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 */
Vlad Yasevichb6157d82007-10-24 15:59:16 -04002397 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 SCTP_TRANSPORT(asoc->peer.primary_path));
2399
2400 /* Cast away the const modifier, as we want to just
2401 * rerun it through as a sideffect.
2402 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07002403 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2406 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2407 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2408 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2409 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2410 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2411
2412 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2413
2414 return SCTP_DISPOSITION_CONSUME;
2415
2416nomem:
2417 return SCTP_DISPOSITION_NOMEM;
2418}
2419
2420/*
2421 * Process an ABORT.
2422 *
2423 * Section: 9.1
2424 * After checking the Verification Tag, the receiving endpoint shall
2425 * remove the association from its record, and shall report the
2426 * termination to its upper layer.
2427 *
2428 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2429 * B) Rules for packet carrying ABORT:
2430 *
2431 * - The endpoint shall always fill in the Verification Tag field of the
2432 * outbound packet with the destination endpoint's tag value if it
2433 * is known.
2434 *
2435 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2436 * MUST follow the procedure described in Section 8.4.
2437 *
2438 * - The receiver MUST accept the packet if the Verification Tag
2439 * matches either its own tag, OR the tag of its peer. Otherwise, the
2440 * receiver MUST silently discard the packet and take no further
2441 * action.
2442 *
2443 * Inputs
2444 * (endpoint, asoc, chunk)
2445 *
2446 * Outputs
2447 * (asoc, reply_msg, msg_up, timers, counters)
2448 *
2449 * The return value is the disposition of the chunk.
2450 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002451sctp_disposition_t sctp_sf_do_9_1_abort(struct net *net,
2452 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 const struct sctp_association *asoc,
2454 const sctp_subtype_t type,
2455 void *arg,
2456 sctp_cmd_seq_t *commands)
2457{
2458 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002461 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
2463 /* Make sure that the ABORT chunk has a valid length.
2464 * Since this is an ABORT chunk, we have to discard it
2465 * because of the following text:
2466 * RFC 2960, Section 3.3.7
2467 * If an endpoint receives an ABORT with a format error or for an
2468 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002469 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 * as we do not know its true length. So, to be safe, discard the
2471 * packet.
2472 */
2473 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002474 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Vlad Yasevich75205f42007-12-20 14:12:59 -08002476 /* ADD-IP: Special case for ABORT chunks
2477 * F4) One special consideration is that ABORT Chunks arriving
2478 * destined to the IP address being deleted MUST be
2479 * ignored (see Section 5.3.1 for further details).
2480 */
2481 if (SCTP_ADDR_DEL ==
2482 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002483 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002484
Xin Long548e23f2020-02-18 12:07:53 +08002485 if (!sctp_err_chunk_valid(chunk))
2486 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2487
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002488 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002489}
2490
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002491static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
2492 const struct sctp_endpoint *ep,
Vlad Yasevich75205f42007-12-20 14:12:59 -08002493 const struct sctp_association *asoc,
2494 const sctp_subtype_t type,
2495 void *arg,
2496 sctp_cmd_seq_t *commands)
2497{
2498 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002499 unsigned int len;
Vlad Yasevich75205f42007-12-20 14:12:59 -08002500 __be16 error = SCTP_ERROR_NO_ERROR;
2501
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 /* See if we have an error cause code in the chunk. */
2503 len = ntohs(chunk->chunk_hdr->length);
Shan Wei96ca4682011-04-19 21:26:26 +00002504
Xin Long548e23f2020-02-18 12:07:53 +08002505 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2507
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002508 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002509 /* ASSOC_FAILED will DELETE_TCB. */
Al Viro5be291f2006-11-20 17:01:06 -08002510 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00002511 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2512 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
2514 return SCTP_DISPOSITION_ABORT;
2515}
2516
2517/*
2518 * Process an ABORT. (COOKIE-WAIT state)
2519 *
2520 * See sctp_sf_do_9_1_abort() above.
2521 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002522sctp_disposition_t sctp_sf_cookie_wait_abort(struct net *net,
2523 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 const struct sctp_association *asoc,
2525 const sctp_subtype_t type,
2526 void *arg,
2527 sctp_cmd_seq_t *commands)
2528{
2529 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002530 unsigned int len;
Al Virof94c0192006-11-20 17:00:25 -08002531 __be16 error = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
2533 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002534 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
2536 /* Make sure that the ABORT chunk has a valid length.
2537 * Since this is an ABORT chunk, we have to discard it
2538 * because of the following text:
2539 * RFC 2960, Section 3.3.7
2540 * If an endpoint receives an ABORT with a format error or for an
2541 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002542 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 * as we do not know its true length. So, to be safe, discard the
2544 * packet.
2545 */
2546 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002547 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
2549 /* See if we have an error cause code in the chunk. */
2550 len = ntohs(chunk->chunk_hdr->length);
2551 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2552 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2553
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002554 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002555 chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556}
2557
2558/*
2559 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2560 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002561sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(struct net *net,
2562 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 const struct sctp_association *asoc,
2564 const sctp_subtype_t type,
2565 void *arg,
2566 sctp_cmd_seq_t *commands)
2567{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002568 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002569 ENOPROTOOPT, asoc,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002570 (struct sctp_transport *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571}
2572
2573/*
2574 * Process an ABORT. (COOKIE-ECHOED state)
2575 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002576sctp_disposition_t sctp_sf_cookie_echoed_abort(struct net *net,
2577 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 const struct sctp_association *asoc,
2579 const sctp_subtype_t type,
2580 void *arg,
2581 sctp_cmd_seq_t *commands)
2582{
2583 /* There is a single T1 timer, so we should be able to use
2584 * common function with the COOKIE-WAIT state.
2585 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002586 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587}
2588
2589/*
2590 * Stop T1 timer and abort association with "INIT failed".
2591 *
2592 * This is common code called by several sctp_sf_*_abort() functions above.
2593 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002594static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
2595 sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -08002596 __be16 error, int sk_err,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002597 const struct sctp_association *asoc,
2598 struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599{
Daniel Borkmannbb333812013-06-28 19:49:40 +02002600 pr_debug("%s: ABORT received (INIT)\n", __func__);
2601
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2603 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002604 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2606 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002607 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 /* CMD_INIT_FAILED will DELETE_TCB. */
2609 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002610 SCTP_PERR(error));
Daniel Borkmannbb333812013-06-28 19:49:40 +02002611
Frank Filz3f7a87d2005-06-20 13:14:57 -07002612 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613}
2614
2615/*
2616 * sctp_sf_do_9_2_shut
2617 *
2618 * Section: 9.2
2619 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2620 * - enter the SHUTDOWN-RECEIVED state,
2621 *
2622 * - stop accepting new data from its SCTP user
2623 *
2624 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2625 * that all its outstanding DATA chunks have been received by the
2626 * SHUTDOWN sender.
2627 *
2628 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2629 * send a SHUTDOWN in response to a ULP request. And should discard
2630 * subsequent SHUTDOWN chunks.
2631 *
2632 * If there are still outstanding DATA chunks left, the SHUTDOWN
2633 * receiver shall continue to follow normal data transmission
2634 * procedures defined in Section 6 until all outstanding DATA chunks
2635 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2636 * new data from its SCTP user.
2637 *
2638 * Verification Tag: 8.5 Verification Tag [Normal verification]
2639 *
2640 * Inputs
2641 * (endpoint, asoc, chunk)
2642 *
2643 * Outputs
2644 * (asoc, reply_msg, msg_up, timers, counters)
2645 *
2646 * The return value is the disposition of the chunk.
2647 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002648sctp_disposition_t sctp_sf_do_9_2_shutdown(struct net *net,
2649 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 const struct sctp_association *asoc,
2651 const sctp_subtype_t type,
2652 void *arg,
2653 sctp_cmd_seq_t *commands)
2654{
2655 struct sctp_chunk *chunk = arg;
2656 sctp_shutdownhdr_t *sdh;
2657 sctp_disposition_t disposition;
2658 struct sctp_ulpevent *ev;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002659 __u32 ctsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
2661 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002662 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
2664 /* Make sure that the SHUTDOWN chunk has a valid length. */
2665 if (!sctp_chunk_length_valid(chunk,
2666 sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002667 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 commands);
2669
2670 /* Convert the elaborate header. */
2671 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2672 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2673 chunk->subh.shutdown_hdr = sdh;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002674 ctsn = ntohl(sdh->cum_tsn_ack);
2675
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002676 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02002677 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2678 asoc->ctsn_ack_point);
2679
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002680 return SCTP_DISPOSITION_DISCARD;
2681 }
2682
Wei Yongjundf10eec2008-10-23 01:00:21 -07002683 /* If Cumulative TSN Ack beyond the max tsn currently
2684 * send, terminating the association and respond to the
2685 * sender with an ABORT.
2686 */
2687 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002688 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002690 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2691 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2692 * inform the application that it should cease sending data.
2693 */
2694 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2695 if (!ev) {
2696 disposition = SCTP_DISPOSITION_NOMEM;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002697 goto out;
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002698 }
2699 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2700
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2702 * - enter the SHUTDOWN-RECEIVED state,
2703 * - stop accepting new data from its SCTP user
2704 *
2705 * [This is implicit in the new state.]
2706 */
2707 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2708 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2709 disposition = SCTP_DISPOSITION_CONSUME;
2710
2711 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002712 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 arg, commands);
2714 }
2715
2716 if (SCTP_DISPOSITION_NOMEM == disposition)
2717 goto out;
2718
2719 /* - verify, by checking the Cumulative TSN Ack field of the
2720 * chunk, that all its outstanding DATA chunks have been
2721 * received by the SHUTDOWN sender.
2722 */
2723 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
Al Viro2178eda2006-11-20 17:26:53 -08002724 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726out:
2727 return disposition;
2728}
2729
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002730/*
2731 * sctp_sf_do_9_2_shut_ctsn
2732 *
2733 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2734 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2735 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2736 * MUST be processed.
2737 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002738sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(struct net *net,
2739 const struct sctp_endpoint *ep,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002740 const struct sctp_association *asoc,
2741 const sctp_subtype_t type,
2742 void *arg,
2743 sctp_cmd_seq_t *commands)
2744{
2745 struct sctp_chunk *chunk = arg;
2746 sctp_shutdownhdr_t *sdh;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002747 __u32 ctsn;
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002748
2749 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002750 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002751
2752 /* Make sure that the SHUTDOWN chunk has a valid length. */
2753 if (!sctp_chunk_length_valid(chunk,
2754 sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002755 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002756 commands);
2757
2758 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002759 ctsn = ntohl(sdh->cum_tsn_ack);
2760
2761 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02002762 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2763 asoc->ctsn_ack_point);
2764
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002765 return SCTP_DISPOSITION_DISCARD;
2766 }
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002767
2768 /* If Cumulative TSN Ack beyond the max tsn currently
2769 * send, terminating the association and respond to the
2770 * sender with an ABORT.
2771 */
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002772 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002773 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002774
2775 /* verify, by checking the Cumulative TSN Ack field of the
2776 * chunk, that all its outstanding DATA chunks have been
2777 * received by the SHUTDOWN sender.
2778 */
2779 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2780 SCTP_BE32(sdh->cum_tsn_ack));
2781
2782 return SCTP_DISPOSITION_CONSUME;
2783}
2784
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785/* RFC 2960 9.2
2786 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2787 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2788 * transport addresses (either in the IP addresses or in the INIT chunk)
2789 * that belong to this association, it should discard the INIT chunk and
2790 * retransmit the SHUTDOWN ACK chunk.
2791 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002792sctp_disposition_t sctp_sf_do_9_2_reshutack(struct net *net,
2793 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 const struct sctp_association *asoc,
2795 const sctp_subtype_t type,
2796 void *arg,
2797 sctp_cmd_seq_t *commands)
2798{
2799 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2800 struct sctp_chunk *reply;
2801
Vlad Yasevichece25df2007-09-07 16:30:54 -04002802 /* Make sure that the chunk has a valid length */
2803 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002804 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04002805 commands);
2806
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 /* Since we are not going to really process this INIT, there
2808 * is no point in verifying chunk boundries. Just generate
2809 * the SHUTDOWN ACK.
2810 */
2811 reply = sctp_make_shutdown_ack(asoc, chunk);
2812 if (NULL == reply)
2813 goto nomem;
2814
2815 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2816 * the T2-SHUTDOWN timer.
2817 */
2818 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2819
2820 /* and restart the T2-shutdown timer. */
2821 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2822 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2823
2824 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2825
2826 return SCTP_DISPOSITION_CONSUME;
2827nomem:
2828 return SCTP_DISPOSITION_NOMEM;
2829}
2830
2831/*
2832 * sctp_sf_do_ecn_cwr
2833 *
2834 * Section: Appendix A: Explicit Congestion Notification
2835 *
2836 * CWR:
2837 *
2838 * RFC 2481 details a specific bit for a sender to send in the header of
2839 * its next outbound TCP segment to indicate to its peer that it has
2840 * reduced its congestion window. This is termed the CWR bit. For
2841 * SCTP the same indication is made by including the CWR chunk.
2842 * This chunk contains one data element, i.e. the TSN number that
2843 * was sent in the ECNE chunk. This element represents the lowest
2844 * TSN number in the datagram that was originally marked with the
2845 * CE bit.
2846 *
2847 * Verification Tag: 8.5 Verification Tag [Normal verification]
2848 * Inputs
2849 * (endpoint, asoc, chunk)
2850 *
2851 * Outputs
2852 * (asoc, reply_msg, msg_up, timers, counters)
2853 *
2854 * The return value is the disposition of the chunk.
2855 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002856sctp_disposition_t sctp_sf_do_ecn_cwr(struct net *net,
2857 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 const struct sctp_association *asoc,
2859 const sctp_subtype_t type,
2860 void *arg,
2861 sctp_cmd_seq_t *commands)
2862{
2863 sctp_cwrhdr_t *cwr;
2864 struct sctp_chunk *chunk = arg;
Al Viro34bcca22006-11-20 17:27:15 -08002865 u32 lowest_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
2867 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002868 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
2870 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002871 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2875 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2876
Al Viro34bcca22006-11-20 17:27:15 -08002877 lowest_tsn = ntohl(cwr->lowest_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
2879 /* Does this CWR ack the last sent congestion notification? */
Al Viro34bcca22006-11-20 17:27:15 -08002880 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 /* Stop sending ECNE. */
2882 sctp_add_cmd_sf(commands,
2883 SCTP_CMD_ECN_CWR,
Al Viro34bcca22006-11-20 17:27:15 -08002884 SCTP_U32(lowest_tsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 }
2886 return SCTP_DISPOSITION_CONSUME;
2887}
2888
2889/*
2890 * sctp_sf_do_ecne
2891 *
2892 * Section: Appendix A: Explicit Congestion Notification
2893 *
2894 * ECN-Echo
2895 *
2896 * RFC 2481 details a specific bit for a receiver to send back in its
2897 * TCP acknowledgements to notify the sender of the Congestion
2898 * Experienced (CE) bit having arrived from the network. For SCTP this
2899 * same indication is made by including the ECNE chunk. This chunk
2900 * contains one data element, i.e. the lowest TSN associated with the IP
2901 * datagram marked with the CE bit.....
2902 *
2903 * Verification Tag: 8.5 Verification Tag [Normal verification]
2904 * Inputs
2905 * (endpoint, asoc, chunk)
2906 *
2907 * Outputs
2908 * (asoc, reply_msg, msg_up, timers, counters)
2909 *
2910 * The return value is the disposition of the chunk.
2911 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002912sctp_disposition_t sctp_sf_do_ecne(struct net *net,
2913 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 const struct sctp_association *asoc,
2915 const sctp_subtype_t type,
2916 void *arg,
2917 sctp_cmd_seq_t *commands)
2918{
2919 sctp_ecnehdr_t *ecne;
2920 struct sctp_chunk *chunk = arg;
2921
2922 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002923 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
2925 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002926 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 commands);
2928
2929 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2930 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2931
2932 /* If this is a newer ECNE than the last CWR packet we sent out */
2933 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2934 SCTP_U32(ntohl(ecne->lowest_tsn)));
2935
2936 return SCTP_DISPOSITION_CONSUME;
2937}
2938
2939/*
2940 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2941 *
2942 * The SCTP endpoint MUST always acknowledge the reception of each valid
2943 * DATA chunk.
2944 *
2945 * The guidelines on delayed acknowledgement algorithm specified in
2946 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2947 * acknowledgement SHOULD be generated for at least every second packet
2948 * (not every second DATA chunk) received, and SHOULD be generated within
2949 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2950 * situations it may be beneficial for an SCTP transmitter to be more
2951 * conservative than the algorithms detailed in this document allow.
2952 * However, an SCTP transmitter MUST NOT be more aggressive than the
2953 * following algorithms allow.
2954 *
2955 * A SCTP receiver MUST NOT generate more than one SACK for every
2956 * incoming packet, other than to update the offered window as the
2957 * receiving application consumes new data.
2958 *
2959 * Verification Tag: 8.5 Verification Tag [Normal verification]
2960 *
2961 * Inputs
2962 * (endpoint, asoc, chunk)
2963 *
2964 * Outputs
2965 * (asoc, reply_msg, msg_up, timers, counters)
2966 *
2967 * The return value is the disposition of the chunk.
2968 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002969sctp_disposition_t sctp_sf_eat_data_6_2(struct net *net,
2970 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 const struct sctp_association *asoc,
2972 const sctp_subtype_t type,
2973 void *arg,
2974 sctp_cmd_seq_t *commands)
2975{
2976 struct sctp_chunk *chunk = arg;
Wei Yongjun6dc76942009-11-23 15:53:53 -05002977 sctp_arg_t force = SCTP_NOFORCE();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 int error;
2979
2980 if (!sctp_vtag_verify(chunk, asoc)) {
2981 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2982 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002983 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
2986 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002987 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 commands);
2989
wangweidongcb3f8372013-12-23 12:16:50 +08002990 error = sctp_eat_data(asoc, chunk, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 switch (error) {
2992 case SCTP_IERROR_NO_ERROR:
2993 break;
2994 case SCTP_IERROR_HIGH_TSN:
2995 case SCTP_IERROR_BAD_STREAM:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002996 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 goto discard_noforce;
2998 case SCTP_IERROR_DUP_TSN:
2999 case SCTP_IERROR_IGNORE_TSN:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003000 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 goto discard_force;
3002 case SCTP_IERROR_NO_DATA:
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02003003 return SCTP_DISPOSITION_ABORT;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003004 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003005 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003006 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 default:
3008 BUG();
3009 }
3010
Wei Yongjun6dc76942009-11-23 15:53:53 -05003011 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
3012 force = SCTP_FORCE();
3013
Neil Horman9f70f462013-12-10 06:48:15 -05003014 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3016 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3017 }
3018
3019 /* If this is the last chunk in a packet, we need to count it
3020 * toward sack generation. Note that we need to SACK every
3021 * OTHER packet containing data chunks, EVEN IF WE DISCARD
3022 * THEM. We elect to NOT generate SACK's if the chunk fails
3023 * the verification tag test.
3024 *
3025 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3026 *
3027 * The SCTP endpoint MUST always acknowledge the reception of
3028 * each valid DATA chunk.
3029 *
3030 * The guidelines on delayed acknowledgement algorithm
3031 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
3032 * Specifically, an acknowledgement SHOULD be generated for at
3033 * least every second packet (not every second DATA chunk)
3034 * received, and SHOULD be generated within 200 ms of the
3035 * arrival of any unacknowledged DATA chunk. In some
3036 * situations it may be beneficial for an SCTP transmitter to
3037 * be more conservative than the algorithms detailed in this
3038 * document allow. However, an SCTP transmitter MUST NOT be
3039 * more aggressive than the following algorithms allow.
3040 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08003041 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003042 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 return SCTP_DISPOSITION_CONSUME;
3045
3046discard_force:
3047 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3048 *
3049 * When a packet arrives with duplicate DATA chunk(s) and with
3050 * no new DATA chunk(s), the endpoint MUST immediately send a
3051 * SACK with no delay. If a packet arrives with duplicate
3052 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3053 * MAY immediately send a SACK. Normally receipt of duplicate
3054 * DATA chunks will occur when the original SACK chunk was lost
3055 * and the peer's RTO has expired. The duplicate TSN number(s)
3056 * SHOULD be reported in the SACK as duplicate.
3057 */
3058 /* In our case, we split the MAY SACK advice up whether or not
3059 * the last chunk is a duplicate.'
3060 */
3061 if (chunk->end_of_packet)
3062 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3063 return SCTP_DISPOSITION_DISCARD;
3064
3065discard_noforce:
Frank Filz52ccb8e2005-12-22 11:36:46 -08003066 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003067 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 return SCTP_DISPOSITION_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070}
3071
3072/*
3073 * sctp_sf_eat_data_fast_4_4
3074 *
3075 * Section: 4 (4)
3076 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3077 * DATA chunks without delay.
3078 *
3079 * Verification Tag: 8.5 Verification Tag [Normal verification]
3080 * Inputs
3081 * (endpoint, asoc, chunk)
3082 *
3083 * Outputs
3084 * (asoc, reply_msg, msg_up, timers, counters)
3085 *
3086 * The return value is the disposition of the chunk.
3087 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003088sctp_disposition_t sctp_sf_eat_data_fast_4_4(struct net *net,
3089 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 const struct sctp_association *asoc,
3091 const sctp_subtype_t type,
3092 void *arg,
3093 sctp_cmd_seq_t *commands)
3094{
3095 struct sctp_chunk *chunk = arg;
3096 int error;
3097
3098 if (!sctp_vtag_verify(chunk, asoc)) {
3099 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3100 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003101 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 }
3103
3104 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003105 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 commands);
3107
wangweidongcb3f8372013-12-23 12:16:50 +08003108 error = sctp_eat_data(asoc, chunk, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 switch (error) {
3110 case SCTP_IERROR_NO_ERROR:
3111 case SCTP_IERROR_HIGH_TSN:
3112 case SCTP_IERROR_DUP_TSN:
3113 case SCTP_IERROR_IGNORE_TSN:
3114 case SCTP_IERROR_BAD_STREAM:
3115 break;
3116 case SCTP_IERROR_NO_DATA:
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02003117 return SCTP_DISPOSITION_ABORT;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003118 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003119 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003120 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 default:
3122 BUG();
3123 }
3124
3125 /* Go a head and force a SACK, since we are shutting down. */
3126
3127 /* Implementor's Guide.
3128 *
3129 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3130 * respond to each received packet containing one or more DATA chunk(s)
3131 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3132 */
3133 if (chunk->end_of_packet) {
3134 /* We must delay the chunk creation since the cumulative
3135 * TSN has not been updated yet.
3136 */
3137 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3138 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3139 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3140 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3141 }
3142
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 return SCTP_DISPOSITION_CONSUME;
3144}
3145
3146/*
3147 * Section: 6.2 Processing a Received SACK
3148 * D) Any time a SACK arrives, the endpoint performs the following:
3149 *
3150 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3151 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3152 * increasing, a SACK whose Cumulative TSN Ack is less than the
3153 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3154 *
3155 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3156 * of bytes still outstanding after processing the Cumulative TSN Ack
3157 * and the Gap Ack Blocks.
3158 *
3159 * iii) If the SACK is missing a TSN that was previously
3160 * acknowledged via a Gap Ack Block (e.g., the data receiver
3161 * reneged on the data), then mark the corresponding DATA chunk
3162 * as available for retransmit: Mark it as missing for fast
3163 * retransmit as described in Section 7.2.4 and if no retransmit
3164 * timer is running for the destination address to which the DATA
3165 * chunk was originally transmitted, then T3-rtx is started for
3166 * that destination address.
3167 *
3168 * Verification Tag: 8.5 Verification Tag [Normal verification]
3169 *
3170 * Inputs
3171 * (endpoint, asoc, chunk)
3172 *
3173 * Outputs
3174 * (asoc, reply_msg, msg_up, timers, counters)
3175 *
3176 * The return value is the disposition of the chunk.
3177 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003178sctp_disposition_t sctp_sf_eat_sack_6_2(struct net *net,
3179 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 const struct sctp_association *asoc,
3181 const sctp_subtype_t type,
3182 void *arg,
3183 sctp_cmd_seq_t *commands)
3184{
3185 struct sctp_chunk *chunk = arg;
3186 sctp_sackhdr_t *sackh;
3187 __u32 ctsn;
3188
3189 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003190 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
3192 /* Make sure that the SACK chunk has a valid length. */
3193 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003194 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 commands);
3196
3197 /* Pull the SACK chunk from the data buffer */
3198 sackh = sctp_sm_pull_sack(chunk);
3199 /* Was this a bogus SACK? */
3200 if (!sackh)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003201 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 chunk->subh.sack_hdr = sackh;
3203 ctsn = ntohl(sackh->cum_tsn_ack);
3204
3205 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3206 * Ack Point, then drop the SACK. Since Cumulative TSN
3207 * Ack is monotonically increasing, a SACK whose
3208 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3209 * Point indicates an out-of-order SACK.
3210 */
3211 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02003212 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
3213 asoc->ctsn_ack_point);
3214
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 return SCTP_DISPOSITION_DISCARD;
3216 }
3217
Wei Yongjunaecedea2007-08-02 16:57:44 +08003218 /* If Cumulative TSN Ack beyond the max tsn currently
3219 * send, terminating the association and respond to the
3220 * sender with an ABORT.
3221 */
3222 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003223 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjunaecedea2007-08-02 16:57:44 +08003224
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 /* Return this SACK for further processing. */
Nicolas Dichteledfee032012-10-03 05:43:22 +00003226 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
3228 /* Note: We do the rest of the work on the PROCESS_SACK
3229 * sideeffect.
3230 */
3231 return SCTP_DISPOSITION_CONSUME;
3232}
3233
3234/*
3235 * Generate an ABORT in response to a packet.
3236 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003237 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003239 * 8) The receiver should respond to the sender of the OOTB packet with
3240 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3241 * MUST fill in the Verification Tag field of the outbound packet
3242 * with the value found in the Verification Tag field of the OOTB
3243 * packet and set the T-bit in the Chunk Flags to indicate that the
3244 * Verification Tag is reflected. After sending this ABORT, the
3245 * receiver of the OOTB packet shall discard the OOTB packet and take
3246 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 *
3248 * Verification Tag:
3249 *
3250 * The return value is the disposition of the chunk.
3251*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003252static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
3253 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 const struct sctp_association *asoc,
3255 const sctp_subtype_t type,
3256 void *arg,
3257 sctp_cmd_seq_t *commands)
3258{
3259 struct sctp_packet *packet = NULL;
3260 struct sctp_chunk *chunk = arg;
3261 struct sctp_chunk *abort;
3262
Eric W. Biederman2ce95502012-08-06 08:43:06 +00003263 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
3265 if (packet) {
3266 /* Make an ABORT. The T bit will be set if the asoc
3267 * is NULL.
3268 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003269 abort = sctp_make_abort(asoc, chunk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 if (!abort) {
3271 sctp_ootb_pkt_free(packet);
3272 return SCTP_DISPOSITION_NOMEM;
3273 }
3274
Jerome Forissier047a2422005-04-28 11:58:43 -07003275 /* Reflect vtag if T-Bit is set */
3276 if (sctp_test_T_bit(abort))
3277 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3278
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 /* Set the skb to the belonging sock for accounting. */
3280 abort->skb->sk = ep->base.sk;
3281
3282 sctp_packet_append_chunk(packet, abort);
3283
3284 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3285 SCTP_PACKET(packet));
3286
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003287 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003289 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 return SCTP_DISPOSITION_CONSUME;
3291 }
3292
3293 return SCTP_DISPOSITION_NOMEM;
3294}
3295
3296/*
3297 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3298 * event as ULP notification for each cause included in the chunk.
3299 *
3300 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3301 *
3302 * The return value is the disposition of the chunk.
3303*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003304sctp_disposition_t sctp_sf_operr_notify(struct net *net,
3305 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 const struct sctp_association *asoc,
3307 const sctp_subtype_t type,
3308 void *arg,
3309 sctp_cmd_seq_t *commands)
3310{
3311 struct sctp_chunk *chunk = arg;
Shan Wei8a00be12011-04-19 21:25:40 +00003312 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313
3314 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003315 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
3317 /* Make sure that the ERROR chunk has a valid length. */
3318 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003319 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 commands);
Shan Wei8a00be12011-04-19 21:25:40 +00003321 sctp_walk_errors(err, chunk->chunk_hdr);
3322 if ((void *)err != (void *)chunk->chunk_end)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003323 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Shan Wei8a00be12011-04-19 21:25:40 +00003324 (void *)err, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325
Wei Yongjun3df26782009-03-02 06:46:51 +00003326 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3327 SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330}
3331
3332/*
3333 * Process an inbound SHUTDOWN ACK.
3334 *
3335 * From Section 9.2:
3336 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3337 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3338 * peer, and remove all record of the association.
3339 *
3340 * The return value is the disposition.
3341 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003342sctp_disposition_t sctp_sf_do_9_2_final(struct net *net,
3343 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 const struct sctp_association *asoc,
3345 const sctp_subtype_t type,
3346 void *arg,
3347 sctp_cmd_seq_t *commands)
3348{
3349 struct sctp_chunk *chunk = arg;
3350 struct sctp_chunk *reply;
3351 struct sctp_ulpevent *ev;
3352
3353 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003354 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
3356 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3357 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003358 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 /* 10.2 H) SHUTDOWN COMPLETE notification
3361 *
3362 * When SCTP completes the shutdown procedures (section 9.2) this
3363 * notification is passed to the upper layer.
3364 */
3365 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07003366 0, 0, 0, NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 if (!ev)
3368 goto nomem;
3369
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003370 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3371 reply = sctp_make_shutdown_complete(asoc, chunk);
3372 if (!reply)
3373 goto nomem_chunk;
3374
3375 /* Do all the commands now (after allocation), so that we
3376 * have consistent state if memory allocation failes
3377 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3379
3380 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3381 * stop the T2-shutdown timer,
3382 */
3383 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3384 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3385
3386 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3387 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3388
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3390 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003391 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3392 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3394
3395 /* ...and remove all record of the association. */
3396 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3397 return SCTP_DISPOSITION_DELETE_TCB;
3398
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003399nomem_chunk:
3400 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401nomem:
3402 return SCTP_DISPOSITION_NOMEM;
3403}
3404
3405/*
Jerome Forissier047a2422005-04-28 11:58:43 -07003406 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3407 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3409 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3410 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3411 * packet must fill in the Verification Tag field of the outbound
3412 * packet with the Verification Tag received in the SHUTDOWN ACK and
Jerome Forissier047a2422005-04-28 11:58:43 -07003413 * set the T-bit in the Chunk Flags to indicate that the Verification
3414 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 *
3416 * 8) The receiver should respond to the sender of the OOTB packet with
3417 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3418 * MUST fill in the Verification Tag field of the outbound packet
3419 * with the value found in the Verification Tag field of the OOTB
Jerome Forissier047a2422005-04-28 11:58:43 -07003420 * packet and set the T-bit in the Chunk Flags to indicate that the
3421 * Verification Tag is reflected. After sending this ABORT, the
3422 * receiver of the OOTB packet shall discard the OOTB packet and take
3423 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003425sctp_disposition_t sctp_sf_ootb(struct net *net,
3426 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 const struct sctp_association *asoc,
3428 const sctp_subtype_t type,
3429 void *arg,
3430 sctp_cmd_seq_t *commands)
3431{
3432 struct sctp_chunk *chunk = arg;
3433 struct sk_buff *skb = chunk->skb;
3434 sctp_chunkhdr_t *ch;
Shan Wei85c5ed42011-04-19 21:30:01 +00003435 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 __u8 *ch_end;
3437 int ootb_shut_ack = 0;
Shan Wei85c5ed42011-04-19 21:30:01 +00003438 int ootb_cookie_ack = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003440 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
3442 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3443 do {
Vlad Yasevichece25df2007-09-07 16:30:54 -04003444 /* Report violation if the chunk is less then minimal */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003446 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003447 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
Marcelo Ricardo Leitnerbf911e92016-10-25 14:27:39 -02003449 /* Report violation if chunk len overflows */
3450 ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
3451 if (ch_end > skb_tail_pointer(skb))
3452 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3453 commands);
3454
Vlad Yasevichece25df2007-09-07 16:30:54 -04003455 /* Now that we know we at least have a chunk header,
3456 * do things that are type appropriate.
3457 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3459 ootb_shut_ack = 1;
3460
3461 /* RFC 2960, Section 3.3.7
3462 * Moreover, under any circumstances, an endpoint that
3463 * receives an ABORT MUST NOT respond to that ABORT by
3464 * sending an ABORT of its own.
3465 */
3466 if (SCTP_CID_ABORT == ch->type)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003467 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003468
Shan Wei85c5ed42011-04-19 21:30:01 +00003469 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3470 * or a COOKIE ACK the SCTP Packet should be silently
3471 * discarded.
3472 */
3473
3474 if (SCTP_CID_COOKIE_ACK == ch->type)
3475 ootb_cookie_ack = 1;
3476
3477 if (SCTP_CID_ERROR == ch->type) {
3478 sctp_walk_errors(err, ch) {
3479 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3480 ootb_cookie_ack = 1;
3481 break;
3482 }
3483 }
3484 }
3485
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 ch = (sctp_chunkhdr_t *) ch_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003487 } while (ch_end < skb_tail_pointer(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
3489 if (ootb_shut_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003490 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
Shan Wei85c5ed42011-04-19 21:30:01 +00003491 else if (ootb_cookie_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003492 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003494 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495}
3496
3497/*
3498 * Handle an "Out of the blue" SHUTDOWN ACK.
3499 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003500 * Section: 8.4 5, sctpimpguide 2.41.
3501 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
Jerome Forissier047a2422005-04-28 11:58:43 -07003503 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3504 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3505 * packet must fill in the Verification Tag field of the outbound
3506 * packet with the Verification Tag received in the SHUTDOWN ACK and
3507 * set the T-bit in the Chunk Flags to indicate that the Verification
3508 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 *
3510 * Inputs
3511 * (endpoint, asoc, type, arg, commands)
3512 *
3513 * Outputs
3514 * (sctp_disposition_t)
3515 *
3516 * The return value is the disposition of the chunk.
3517 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003518static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
3519 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 const struct sctp_association *asoc,
3521 const sctp_subtype_t type,
3522 void *arg,
3523 sctp_cmd_seq_t *commands)
3524{
3525 struct sctp_packet *packet = NULL;
3526 struct sctp_chunk *chunk = arg;
3527 struct sctp_chunk *shut;
3528
Eric W. Biederman2ce95502012-08-06 08:43:06 +00003529 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530
3531 if (packet) {
3532 /* Make an SHUTDOWN_COMPLETE.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003533 * The T bit will be set if the asoc is NULL.
3534 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 shut = sctp_make_shutdown_complete(asoc, chunk);
3536 if (!shut) {
3537 sctp_ootb_pkt_free(packet);
3538 return SCTP_DISPOSITION_NOMEM;
3539 }
3540
Jerome Forissier047a2422005-04-28 11:58:43 -07003541 /* Reflect vtag if T-Bit is set */
3542 if (sctp_test_T_bit(shut))
3543 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3544
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 /* Set the skb to the belonging sock for accounting. */
3546 shut->skb->sk = ep->base.sk;
3547
3548 sctp_packet_append_chunk(packet, shut);
3549
3550 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3551 SCTP_PACKET(packet));
3552
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003553 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554
3555 /* If the chunk length is invalid, we don't want to process
3556 * the reset of the packet.
3557 */
3558 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003559 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003561 /* We need to discard the rest of the packet to prevent
3562 * potential bomming attacks from additional bundled chunks.
3563 * This is documented in SCTP Threats ID.
3564 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003565 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 }
3567
3568 return SCTP_DISPOSITION_NOMEM;
3569}
3570
3571/*
3572 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3573 *
3574 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3575 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3576 * procedures in section 8.4 SHOULD be followed, in other words it
3577 * should be treated as an Out Of The Blue packet.
3578 * [This means that we do NOT check the Verification Tag on these
3579 * chunks. --piggy ]
3580 *
3581 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003582sctp_disposition_t sctp_sf_do_8_5_1_E_sa(struct net *net,
3583 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 const struct sctp_association *asoc,
3585 const sctp_subtype_t type,
3586 void *arg,
3587 sctp_cmd_seq_t *commands)
3588{
Vlad Yasevichece25df2007-09-07 16:30:54 -04003589 struct sctp_chunk *chunk = arg;
3590
3591 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3592 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003593 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003594 commands);
3595
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 /* Although we do have an association in this case, it corresponds
3597 * to a restarted association. So the packet is treated as an OOTB
3598 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3599 * called with a NULL association.
3600 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003601 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Wei Yongjun8190f892008-09-08 12:13:55 +08003602
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003603 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604}
3605
3606/* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003607sctp_disposition_t sctp_sf_do_asconf(struct net *net,
3608 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 const struct sctp_association *asoc,
3610 const sctp_subtype_t type, void *arg,
3611 sctp_cmd_seq_t *commands)
3612{
3613 struct sctp_chunk *chunk = arg;
3614 struct sctp_chunk *asconf_ack = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003615 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 sctp_addiphdr_t *hdr;
3617 __u32 serial;
3618
3619 if (!sctp_vtag_verify(chunk, asoc)) {
3620 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3621 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003622 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 }
3624
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003625 /* ADD-IP: Section 4.1.1
3626 * This chunk MUST be sent in an authenticated way by using
3627 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3628 * is received unauthenticated it MUST be silently discarded as
3629 * described in [I-D.ietf-tsvwg-sctp-auth].
3630 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003631 if (!net->sctp.addip_noauth && !chunk->auth)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003632 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003633
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3635 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003636 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 commands);
3638
3639 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3640 serial = ntohl(hdr->serial);
3641
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003642 /* Verify the ASCONF chunk before processing it. */
Daniel Borkmann9de79222014-10-09 22:55:31 +02003643 if (!sctp_verify_asconf(asoc, chunk, true, &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003644 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003645 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003646
Vlad Yasevicha08de642007-12-20 14:11:47 -08003647 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 * the endpoint stored in a new association variable
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003649 * 'Peer-Serial-Number'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 */
3651 if (serial == asoc->peer.addip_serial + 1) {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003652 /* If this is the first instance of ASCONF in the packet,
3653 * we can clean our old ASCONF-ACKs.
3654 */
3655 if (!chunk->has_asconf)
3656 sctp_assoc_clean_asconf_ack_cache(asoc);
3657
3658 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3659 * expected, process the ASCONF as described below and after
3660 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3661 * the response packet and cache a copy of it (in the event it
3662 * later needs to be retransmitted).
3663 *
3664 * Essentially, do V1-V5.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 */
3666 asconf_ack = sctp_process_asconf((struct sctp_association *)
3667 asoc, chunk);
3668 if (!asconf_ack)
3669 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevicha08de642007-12-20 14:11:47 -08003670 } else if (serial < asoc->peer.addip_serial + 1) {
3671 /* ADDIP 5.2 E2)
3672 * If the value found in the Sequence Number is less than the
3673 * ('Peer- Sequence-Number' + 1), simply skip to the next
3674 * ASCONF, and include in the outbound response packet
3675 * any previously cached ASCONF-ACK response that was
3676 * sent and saved that matches the Sequence Number of the
3677 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3678 * Chunk exists. This will occur when an older ASCONF
3679 * arrives out of order. In such a case, the receiver
3680 * should skip the ASCONF Chunk and not include ASCONF-ACK
3681 * Chunk for that chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003683 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3684 if (!asconf_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 return SCTP_DISPOSITION_DISCARD;
Vlad Yasevich31b02e12009-09-04 18:21:00 -04003686
3687 /* Reset the transport so that we select the correct one
3688 * this time around. This is to make sure that we don't
3689 * accidentally use a stale transport that's been removed.
3690 */
3691 asconf_ack->transport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 } else {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003693 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 * it must be either a stale packet or from an attacker.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003695 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 return SCTP_DISPOSITION_DISCARD;
3697 }
3698
Vlad Yasevicha08de642007-12-20 14:11:47 -08003699 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3700 * containing the ASCONF-ACK Chunks MUST be the source address of
3701 * the SCTP packet that held the ASCONF Chunks.
3702 *
3703 * To do this properly, we'll set the destination address of the chunk
3704 * and at the transmit time, will try look up the transport to use.
3705 * Since ASCONFs may be bundled, the correct transport may not be
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +02003706 * created until we process the entire packet, thus this workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003708 asconf_ack->dest = chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
Michio Honda6af29cc2011-06-16 17:14:34 +09003710 if (asoc->new_transport) {
wangweidongf7010e62013-12-23 12:16:52 +08003711 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport, commands);
Michio Honda6af29cc2011-06-16 17:14:34 +09003712 ((struct sctp_association *)asoc)->new_transport = NULL;
3713 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003714
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 return SCTP_DISPOSITION_CONSUME;
3716}
3717
3718/*
3719 * ADDIP Section 4.3 General rules for address manipulation
3720 * When building TLV parameters for the ASCONF Chunk that will add or
3721 * delete IP addresses the D0 to D13 rules should be applied:
3722 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003723sctp_disposition_t sctp_sf_do_asconf_ack(struct net *net,
3724 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 const struct sctp_association *asoc,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003726 const sctp_subtype_t type, void *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 sctp_cmd_seq_t *commands)
3728{
3729 struct sctp_chunk *asconf_ack = arg;
3730 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3731 struct sctp_chunk *abort;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003732 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 sctp_addiphdr_t *addip_hdr;
3734 __u32 sent_serial, rcvd_serial;
3735
3736 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3737 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3738 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003739 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 }
3741
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003742 /* ADD-IP, Section 4.1.2:
3743 * This chunk MUST be sent in an authenticated way by using
3744 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3745 * is received unauthenticated it MUST be silently discarded as
3746 * described in [I-D.ietf-tsvwg-sctp-auth].
3747 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003748 if (!net->sctp.addip_noauth && !asconf_ack->auth)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003749 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003750
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 /* Make sure that the ADDIP chunk has a valid length. */
3752 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003753 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 commands);
3755
3756 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3757 rcvd_serial = ntohl(addip_hdr->serial);
3758
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003759 /* Verify the ASCONF-ACK chunk before processing it. */
Daniel Borkmann9de79222014-10-09 22:55:31 +02003760 if (!sctp_verify_asconf(asoc, asconf_ack, false, &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003761 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003762 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003763
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 if (last_asconf) {
3765 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3766 sent_serial = ntohl(addip_hdr->serial);
3767 } else {
3768 sent_serial = asoc->addip_serial - 1;
3769 }
3770
3771 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3772 * equal to the next serial number to be used but no ASCONF chunk is
3773 * outstanding the endpoint MUST ABORT the association. Note that a
3774 * sequence number is greater than if it is no more than 2^^31-1
3775 * larger than the current sequence number (using serial arithmetic).
3776 */
3777 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3778 !(asoc->addip_last_asconf)) {
3779 abort = sctp_make_abort(asoc, asconf_ack,
3780 sizeof(sctp_errhdr_t));
3781 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003782 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3784 SCTP_CHUNK(abort));
3785 }
3786 /* We are going to ABORT, so we might as well stop
3787 * processing the rest of the chunks in the packet.
3788 */
3789 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3790 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
wangweidongcb3f8372013-12-23 12:16:50 +08003791 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003792 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003793 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003795 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003796 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3797 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 return SCTP_DISPOSITION_ABORT;
3799 }
3800
3801 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3802 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3803 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3804
3805 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
Vlad Yasevichc0786692010-04-28 08:47:22 +00003806 asconf_ack)) {
3807 /* Successfully processed ASCONF_ACK. We can
3808 * release the next asconf if we have one.
3809 */
3810 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3811 SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 return SCTP_DISPOSITION_CONSUME;
Vlad Yasevichc0786692010-04-28 08:47:22 +00003813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
3815 abort = sctp_make_abort(asoc, asconf_ack,
3816 sizeof(sctp_errhdr_t));
3817 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003818 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3820 SCTP_CHUNK(abort));
3821 }
3822 /* We are going to ABORT, so we might as well stop
3823 * processing the rest of the chunks in the packet.
3824 */
wangweidongcb3f8372013-12-23 12:16:50 +08003825 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003826 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003827 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003829 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003830 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3831 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 return SCTP_DISPOSITION_ABORT;
3833 }
3834
3835 return SCTP_DISPOSITION_DISCARD;
3836}
3837
3838/*
3839 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3840 *
3841 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3842 * its cumulative TSN point to the value carried in the FORWARD TSN
3843 * chunk, and then MUST further advance its cumulative TSN point locally
3844 * if possible.
3845 * After the above processing, the data receiver MUST stop reporting any
3846 * missing TSNs earlier than or equal to the new cumulative TSN point.
3847 *
3848 * Verification Tag: 8.5 Verification Tag [Normal verification]
3849 *
3850 * The return value is the disposition of the chunk.
3851 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003852sctp_disposition_t sctp_sf_eat_fwd_tsn(struct net *net,
3853 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 const struct sctp_association *asoc,
3855 const sctp_subtype_t type,
3856 void *arg,
3857 sctp_cmd_seq_t *commands)
3858{
3859 struct sctp_chunk *chunk = arg;
3860 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003861 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 __u16 len;
3863 __u32 tsn;
3864
3865 if (!sctp_vtag_verify(chunk, asoc)) {
3866 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3867 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003868 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 }
3870
3871 /* Make sure that the FORWARD_TSN chunk has valid length. */
3872 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003873 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 commands);
3875
3876 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3877 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3878 len = ntohs(chunk->chunk_hdr->length);
3879 len -= sizeof(struct sctp_chunkhdr);
3880 skb_pull(chunk->skb, len);
3881
3882 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02003883 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
3885 /* The TSN is too high--silently discard the chunk and count on it
3886 * getting retransmitted later.
3887 */
3888 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3889 goto discard_noforce;
3890
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003891 /* Silently discard the chunk if stream-id is not valid */
3892 sctp_walk_fwdtsn(skip, chunk) {
3893 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3894 goto discard_noforce;
3895 }
3896
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3898 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003899 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003901
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 /* Count this as receiving DATA. */
Neil Horman9f70f462013-12-10 06:48:15 -05003903 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3905 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3906 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003907
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 /* FIXME: For now send a SACK, but DATA processing may
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003909 * send another.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 */
3911 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912
3913 return SCTP_DISPOSITION_CONSUME;
3914
3915discard_noforce:
3916 return SCTP_DISPOSITION_DISCARD;
3917}
3918
3919sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003920 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 const struct sctp_endpoint *ep,
3922 const struct sctp_association *asoc,
3923 const sctp_subtype_t type,
3924 void *arg,
3925 sctp_cmd_seq_t *commands)
3926{
3927 struct sctp_chunk *chunk = arg;
3928 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003929 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 __u16 len;
3931 __u32 tsn;
3932
3933 if (!sctp_vtag_verify(chunk, asoc)) {
3934 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3935 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003936 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 }
3938
3939 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3940 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003941 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 commands);
3943
3944 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3945 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3946 len = ntohs(chunk->chunk_hdr->length);
3947 len -= sizeof(struct sctp_chunkhdr);
3948 skb_pull(chunk->skb, len);
3949
3950 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02003951 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952
3953 /* The TSN is too high--silently discard the chunk and count on it
3954 * getting retransmitted later.
3955 */
3956 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3957 goto gen_shutdown;
3958
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003959 /* Silently discard the chunk if stream-id is not valid */
3960 sctp_walk_fwdtsn(skip, chunk) {
3961 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3962 goto gen_shutdown;
3963 }
3964
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3966 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003967 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003969
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 /* Go a head and force a SACK, since we are shutting down. */
3971gen_shutdown:
3972 /* Implementor's Guide.
3973 *
3974 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3975 * respond to each received packet containing one or more DATA chunk(s)
3976 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3977 */
3978 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3979 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3980 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3981 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3982
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003983 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984}
3985
3986/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003987 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003988 *
3989 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3990 * Identifier field. If this algorithm was not specified by the
3991 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3992 * during association setup, the AUTH chunk and all chunks after it MUST
3993 * be discarded and an ERROR chunk SHOULD be sent with the error cause
3994 * defined in Section 4.1.
3995 *
3996 * If an endpoint with no shared key receives a Shared Key Identifier
3997 * other than 0, it MUST silently discard all authenticated chunks. If
3998 * the endpoint has at least one endpoint pair shared key for the peer,
3999 * it MUST use the key specified by the Shared Key Identifier if a
4000 * key has been configured for that Shared Key Identifier. If no
4001 * endpoint pair shared key has been configured for that Shared Key
4002 * Identifier, all authenticated chunks MUST be silently discarded.
4003 *
4004 * Verification Tag: 8.5 Verification Tag [Normal verification]
4005 *
4006 * The return value is the disposition of the chunk.
4007 */
Xin Long0e67ad52018-05-05 14:59:47 +08004008static sctp_ierror_t sctp_sf_authenticate(
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004009 const struct sctp_association *asoc,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004010 struct sctp_chunk *chunk)
4011{
4012 struct sctp_authhdr *auth_hdr;
4013 struct sctp_hmac *hmac;
4014 unsigned int sig_len;
4015 __u16 key_id;
4016 __u8 *save_digest;
4017 __u8 *digest;
4018
4019 /* Pull in the auth header, so we can do some more verification */
4020 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4021 chunk->subh.auth_hdr = auth_hdr;
4022 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
4023
Masanari Iida02582e92012-08-22 19:11:26 +09004024 /* Make sure that we support the HMAC algorithm from the auth
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004025 * chunk.
4026 */
4027 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4028 return SCTP_IERROR_AUTH_BAD_HMAC;
4029
4030 /* Make sure that the provided shared key identifier has been
4031 * configured
4032 */
4033 key_id = ntohs(auth_hdr->shkey_id);
4034 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
4035 return SCTP_IERROR_AUTH_BAD_KEYID;
4036
4037
4038 /* Make sure that the length of the signature matches what
4039 * we expect.
4040 */
4041 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
4042 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4043 if (sig_len != hmac->hmac_len)
4044 return SCTP_IERROR_PROTO_VIOLATION;
4045
4046 /* Now that we've done validation checks, we can compute and
4047 * verify the hmac. The steps involved are:
4048 * 1. Save the digest from the chunk.
4049 * 2. Zero out the digest in the chunk.
4050 * 3. Compute the new digest
4051 * 4. Compare saved and new digests.
4052 */
4053 digest = auth_hdr->hmac;
4054 skb_pull(chunk->skb, sig_len);
4055
4056 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4057 if (!save_digest)
4058 goto nomem;
4059
4060 memset(digest, 0, sig_len);
4061
4062 sctp_auth_calculate_hmac(asoc, chunk->skb,
4063 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4064 GFP_ATOMIC);
4065
4066 /* Discard the packet if the digests do not match */
4067 if (memcmp(save_digest, digest, sig_len)) {
4068 kfree(save_digest);
4069 return SCTP_IERROR_BAD_SIG;
4070 }
4071
4072 kfree(save_digest);
4073 chunk->auth = 1;
4074
4075 return SCTP_IERROR_NO_ERROR;
4076nomem:
4077 return SCTP_IERROR_NOMEM;
4078}
4079
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004080sctp_disposition_t sctp_sf_eat_auth(struct net *net,
4081 const struct sctp_endpoint *ep,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004082 const struct sctp_association *asoc,
4083 const sctp_subtype_t type,
4084 void *arg,
4085 sctp_cmd_seq_t *commands)
4086{
4087 struct sctp_authhdr *auth_hdr;
4088 struct sctp_chunk *chunk = arg;
4089 struct sctp_chunk *err_chunk;
4090 sctp_ierror_t error;
4091
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004092 /* Make sure that the peer has AUTH capable */
4093 if (!asoc->peer.auth_capable)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004094 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004095
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004096 if (!sctp_vtag_verify(chunk, asoc)) {
4097 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4098 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004099 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004100 }
4101
4102 /* Make sure that the AUTH chunk has valid length. */
4103 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004104 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004105 commands);
4106
4107 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
Xin Long0e67ad52018-05-05 14:59:47 +08004108 error = sctp_sf_authenticate(asoc, chunk);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004109 switch (error) {
Joe Perches7fd71b12011-07-01 09:43:11 +00004110 case SCTP_IERROR_AUTH_BAD_HMAC:
4111 /* Generate the ERROR chunk and discard the rest
4112 * of the packet
4113 */
4114 err_chunk = sctp_make_op_error(asoc, chunk,
4115 SCTP_ERROR_UNSUP_HMAC,
4116 &auth_hdr->hmac_id,
4117 sizeof(__u16), 0);
4118 if (err_chunk) {
4119 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4120 SCTP_CHUNK(err_chunk));
4121 }
4122 /* Fall Through */
4123 case SCTP_IERROR_AUTH_BAD_KEYID:
4124 case SCTP_IERROR_BAD_SIG:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004125 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Joe Perches7fd71b12011-07-01 09:43:11 +00004126
4127 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004128 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Joe Perches7fd71b12011-07-01 09:43:11 +00004129 commands);
4130
4131 case SCTP_IERROR_NOMEM:
4132 return SCTP_DISPOSITION_NOMEM;
4133
4134 default: /* Prevent gcc warnings */
4135 break;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004136 }
4137
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004138 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4139 struct sctp_ulpevent *ev;
4140
4141 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4142 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4143
4144 if (!ev)
4145 return -ENOMEM;
4146
4147 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4148 SCTP_ULPEVENT(ev));
4149 }
4150
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004151 return SCTP_DISPOSITION_CONSUME;
4152}
4153
4154/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 * Process an unknown chunk.
4156 *
4157 * Section: 3.2. Also, 2.1 in the implementor's guide.
4158 *
4159 * Chunk Types are encoded such that the highest-order two bits specify
4160 * the action that must be taken if the processing endpoint does not
4161 * recognize the Chunk Type.
4162 *
4163 * 00 - Stop processing this SCTP packet and discard it, do not process
4164 * any further chunks within it.
4165 *
4166 * 01 - Stop processing this SCTP packet and discard it, do not process
4167 * any further chunks within it, and report the unrecognized
4168 * chunk in an 'Unrecognized Chunk Type'.
4169 *
4170 * 10 - Skip this chunk and continue processing.
4171 *
4172 * 11 - Skip this chunk and continue processing, but report in an ERROR
4173 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4174 *
4175 * The return value is the disposition of the chunk.
4176 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004177sctp_disposition_t sctp_sf_unk_chunk(struct net *net,
4178 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 const struct sctp_association *asoc,
4180 const sctp_subtype_t type,
4181 void *arg,
4182 sctp_cmd_seq_t *commands)
4183{
4184 struct sctp_chunk *unk_chunk = arg;
4185 struct sctp_chunk *err_chunk;
4186 sctp_chunkhdr_t *hdr;
4187
Daniel Borkmannbb333812013-06-28 19:49:40 +02004188 pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189
4190 if (!sctp_vtag_verify(unk_chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004191 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192
4193 /* Make sure that the chunk has a valid length.
4194 * Since we don't know the chunk type, we use a general
4195 * chunkhdr structure to make a comparison.
4196 */
4197 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004198 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 commands);
4200
4201 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4202 case SCTP_CID_ACTION_DISCARD:
4203 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004204 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 case SCTP_CID_ACTION_DISCARD_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 /* Generate an ERROR chunk as response. */
4207 hdr = unk_chunk->chunk_hdr;
4208 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4209 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -03004210 SCTP_PAD4(ntohs(hdr->length)),
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004211 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 if (err_chunk) {
4213 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4214 SCTP_CHUNK(err_chunk));
4215 }
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07004216
4217 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004218 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 case SCTP_CID_ACTION_SKIP:
4221 /* Skip the chunk. */
4222 return SCTP_DISPOSITION_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 case SCTP_CID_ACTION_SKIP_ERR:
4224 /* Generate an ERROR chunk as response. */
4225 hdr = unk_chunk->chunk_hdr;
4226 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4227 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -03004228 SCTP_PAD4(ntohs(hdr->length)),
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004229 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 if (err_chunk) {
4231 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4232 SCTP_CHUNK(err_chunk));
4233 }
4234 /* Skip the chunk. */
4235 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 default:
4237 break;
4238 }
4239
4240 return SCTP_DISPOSITION_DISCARD;
4241}
4242
4243/*
4244 * Discard the chunk.
4245 *
4246 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4247 * [Too numerous to mention...]
4248 * Verification Tag: No verification needed.
4249 * Inputs
4250 * (endpoint, asoc, chunk)
4251 *
4252 * Outputs
4253 * (asoc, reply_msg, msg_up, timers, counters)
4254 *
4255 * The return value is the disposition of the chunk.
4256 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004257sctp_disposition_t sctp_sf_discard_chunk(struct net *net,
4258 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 const struct sctp_association *asoc,
4260 const sctp_subtype_t type,
4261 void *arg,
4262 sctp_cmd_seq_t *commands)
4263{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004264 struct sctp_chunk *chunk = arg;
4265
4266 /* Make sure that the chunk has a valid length.
4267 * Since we don't know the chunk type, we use a general
4268 * chunkhdr structure to make a comparison.
4269 */
4270 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004271 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004272 commands);
4273
Daniel Borkmannbb333812013-06-28 19:49:40 +02004274 pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
4275
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 return SCTP_DISPOSITION_DISCARD;
4277}
4278
4279/*
4280 * Discard the whole packet.
4281 *
4282 * Section: 8.4 2)
4283 *
4284 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4285 * silently discard the OOTB packet and take no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 *
4287 * Verification Tag: No verification necessary
4288 *
4289 * Inputs
4290 * (endpoint, asoc, chunk)
4291 *
4292 * Outputs
4293 * (asoc, reply_msg, msg_up, timers, counters)
4294 *
4295 * The return value is the disposition of the chunk.
4296 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004297sctp_disposition_t sctp_sf_pdiscard(struct net *net,
4298 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 const struct sctp_association *asoc,
4300 const sctp_subtype_t type,
4301 void *arg,
4302 sctp_cmd_seq_t *commands)
4303{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004304 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4306
4307 return SCTP_DISPOSITION_CONSUME;
4308}
4309
4310
4311/*
4312 * The other end is violating protocol.
4313 *
4314 * Section: Not specified
4315 * Verification Tag: Not specified
4316 * Inputs
4317 * (endpoint, asoc, chunk)
4318 *
4319 * Outputs
4320 * (asoc, reply_msg, msg_up, timers, counters)
4321 *
4322 * We simply tag the chunk as a violation. The state machine will log
4323 * the violation and continue.
4324 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004325sctp_disposition_t sctp_sf_violation(struct net *net,
4326 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 const struct sctp_association *asoc,
4328 const sctp_subtype_t type,
4329 void *arg,
4330 sctp_cmd_seq_t *commands)
4331{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004332 struct sctp_chunk *chunk = arg;
4333
4334 /* Make sure that the chunk has a valid length. */
4335 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004336 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004337 commands);
4338
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 return SCTP_DISPOSITION_VIOLATION;
4340}
4341
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342/*
Wei Yongjunaecedea2007-08-02 16:57:44 +08004343 * Common function to handle a protocol violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 */
Wei Yongjunaecedea2007-08-02 16:57:44 +08004345static sctp_disposition_t sctp_sf_abort_violation(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004346 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004347 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 void *arg,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004350 sctp_cmd_seq_t *commands,
4351 const __u8 *payload,
4352 const size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004354 struct sctp_packet *packet = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 struct sctp_chunk *chunk = arg;
4356 struct sctp_chunk *abort = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004358 /* SCTP-AUTH, Section 6.3:
4359 * It should be noted that if the receiver wants to tear
4360 * down an association in an authenticated way only, the
4361 * handling of malformed packets should not result in
4362 * tearing down the association.
4363 *
4364 * This means that if we only want to abort associations
4365 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004366 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004367 * was malformed.
4368 */
4369 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4370 goto discard;
4371
Jesper Juhl9abed242007-11-11 23:57:49 +01004372 /* Make the abort chunk. */
4373 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4374 if (!abort)
4375 goto nomem;
4376
Vlad Yasevichece25df2007-09-07 16:30:54 -04004377 if (asoc) {
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004378 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4379 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4380 !asoc->peer.i.init_tag) {
4381 sctp_initack_chunk_t *initack;
4382
4383 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4384 if (!sctp_chunk_length_valid(chunk,
4385 sizeof(sctp_initack_chunk_t)))
4386 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4387 else {
4388 unsigned int inittag;
4389
4390 inittag = ntohl(initack->init_hdr.init_tag);
4391 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4392 SCTP_U32(inittag));
4393 }
4394 }
4395
Vlad Yasevichece25df2007-09-07 16:30:54 -04004396 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004397 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398
Vlad Yasevichece25df2007-09-07 16:30:54 -04004399 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4400 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4401 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4402 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4403 SCTP_ERROR(ECONNREFUSED));
4404 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4405 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4406 } else {
4407 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4408 SCTP_ERROR(ECONNABORTED));
4409 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4410 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004411 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 } else {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00004414 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004415
4416 if (!packet)
4417 goto nomem_pkt;
4418
4419 if (sctp_test_T_bit(abort))
4420 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4421
4422 abort->skb->sk = ep->base.sk;
4423
4424 sctp_packet_append_chunk(packet, abort);
4425
4426 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4427 SCTP_PACKET(packet));
4428
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004429 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 }
4431
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004432 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004433
Vlad Yasevich56eb82b2008-10-09 14:33:01 -07004434discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004435 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 return SCTP_DISPOSITION_ABORT;
4437
Vlad Yasevichece25df2007-09-07 16:30:54 -04004438nomem_pkt:
4439 sctp_chunk_free(abort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440nomem:
4441 return SCTP_DISPOSITION_NOMEM;
4442}
4443
Wei Yongjunaecedea2007-08-02 16:57:44 +08004444/*
4445 * Handle a protocol violation when the chunk length is invalid.
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004446 * "Invalid" length is identified as smaller than the minimal length a
Wei Yongjunaecedea2007-08-02 16:57:44 +08004447 * given chunk can be. For example, a SACK chunk has invalid length
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004448 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
Wei Yongjunaecedea2007-08-02 16:57:44 +08004449 *
4450 * We inform the other end by sending an ABORT with a Protocol Violation
4451 * error code.
4452 *
4453 * Section: Not specified
4454 * Verification Tag: Nothing to do
4455 * Inputs
4456 * (endpoint, asoc, chunk)
4457 *
4458 * Outputs
4459 * (reply_msg, msg_up, counters)
4460 *
4461 * Generate an ABORT chunk and terminate the association.
4462 */
4463static sctp_disposition_t sctp_sf_violation_chunklen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004464 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004465 const struct sctp_endpoint *ep,
4466 const struct sctp_association *asoc,
4467 const sctp_subtype_t type,
4468 void *arg,
4469 sctp_cmd_seq_t *commands)
4470{
wangweidongcb3f8372013-12-23 12:16:50 +08004471 static const char err_str[] = "The following chunk had invalid length:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004472
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004473 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004474 sizeof(err_str));
4475}
4476
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004477/*
4478 * Handle a protocol violation when the parameter length is invalid.
Shan Wei33c7cfd2011-04-18 19:11:01 +00004479 * If the length is smaller than the minimum length of a given parameter,
4480 * or accumulated length in multi parameters exceeds the end of the chunk,
4481 * the length is considered as invalid.
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004482 */
4483static sctp_disposition_t sctp_sf_violation_paramlen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004484 struct net *net,
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004485 const struct sctp_endpoint *ep,
4486 const struct sctp_association *asoc,
4487 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -07004488 void *arg, void *ext,
4489 sctp_cmd_seq_t *commands)
4490{
4491 struct sctp_chunk *chunk = arg;
4492 struct sctp_paramhdr *param = ext;
4493 struct sctp_chunk *abort = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004494
Wei Yongjunba016672008-09-30 05:32:24 -07004495 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4496 goto discard;
4497
4498 /* Make the abort chunk. */
4499 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4500 if (!abort)
4501 goto nomem;
4502
4503 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004504 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Wei Yongjunba016672008-09-30 05:32:24 -07004505
4506 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4507 SCTP_ERROR(ECONNABORTED));
4508 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4509 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004510 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4511 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Wei Yongjunba016672008-09-30 05:32:24 -07004512
4513discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004514 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Wei Yongjunba016672008-09-30 05:32:24 -07004515 return SCTP_DISPOSITION_ABORT;
4516nomem:
4517 return SCTP_DISPOSITION_NOMEM;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004518}
4519
Wei Yongjunaecedea2007-08-02 16:57:44 +08004520/* Handle a protocol violation when the peer trying to advance the
4521 * cumulative tsn ack to a point beyond the max tsn currently sent.
4522 *
4523 * We inform the other end by sending an ABORT with a Protocol Violation
4524 * error code.
4525 */
4526static sctp_disposition_t sctp_sf_violation_ctsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004527 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004528 const struct sctp_endpoint *ep,
4529 const struct sctp_association *asoc,
4530 const sctp_subtype_t type,
4531 void *arg,
4532 sctp_cmd_seq_t *commands)
4533{
wangweidongcb3f8372013-12-23 12:16:50 +08004534 static const char err_str[] = "The cumulative tsn ack beyond the max tsn currently sent:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004535
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004536 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004537 sizeof(err_str));
4538}
4539
Vlad Yasevichece25df2007-09-07 16:30:54 -04004540/* Handle protocol violation of an invalid chunk bundling. For example,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004541 * when we have an association and we receive bundled INIT-ACK, or
Vlad Yasevichece25df2007-09-07 16:30:54 -04004542 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004543 * statement from the specs. Additionally, there might be an attacker
Vlad Yasevichece25df2007-09-07 16:30:54 -04004544 * on the path and we may not want to continue this communication.
4545 */
4546static sctp_disposition_t sctp_sf_violation_chunk(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004547 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004548 const struct sctp_endpoint *ep,
4549 const struct sctp_association *asoc,
4550 const sctp_subtype_t type,
4551 void *arg,
4552 sctp_cmd_seq_t *commands)
4553{
wangweidongcb3f8372013-12-23 12:16:50 +08004554 static const char err_str[] = "The following chunk violates protocol:";
Vlad Yasevichece25df2007-09-07 16:30:54 -04004555
4556 if (!asoc)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004557 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004558
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004559 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004560 sizeof(err_str));
4561}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562/***************************************************************************
4563 * These are the state functions for handling primitive (Section 10) events.
4564 ***************************************************************************/
4565/*
4566 * sctp_sf_do_prm_asoc
4567 *
4568 * Section: 10.1 ULP-to-SCTP
4569 * B) Associate
4570 *
4571 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4572 * outbound stream count)
4573 * -> association id [,destination transport addr list] [,outbound stream
4574 * count]
4575 *
4576 * This primitive allows the upper layer to initiate an association to a
4577 * specific peer endpoint.
4578 *
4579 * The peer endpoint shall be specified by one of the transport addresses
4580 * which defines the endpoint (see Section 1.4). If the local SCTP
4581 * instance has not been initialized, the ASSOCIATE is considered an
4582 * error.
4583 * [This is not relevant for the kernel implementation since we do all
4584 * initialization at boot time. It we hadn't initialized we wouldn't
4585 * get anywhere near this code.]
4586 *
4587 * An association id, which is a local handle to the SCTP association,
4588 * will be returned on successful establishment of the association. If
4589 * SCTP is not able to open an SCTP association with the peer endpoint,
4590 * an error is returned.
4591 * [In the kernel implementation, the struct sctp_association needs to
4592 * be created BEFORE causing this primitive to run.]
4593 *
4594 * Other association parameters may be returned, including the
4595 * complete destination transport addresses of the peer as well as the
4596 * outbound stream count of the local endpoint. One of the transport
4597 * address from the returned destination addresses will be selected by
4598 * the local endpoint as default primary path for sending SCTP packets
4599 * to this peer. The returned "destination transport addr list" can
4600 * be used by the ULP to change the default primary path or to force
4601 * sending a packet to a specific transport address. [All of this
4602 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4603 * function.]
4604 *
4605 * Mandatory attributes:
4606 *
4607 * o local SCTP instance name - obtained from the INITIALIZE operation.
4608 * [This is the argument asoc.]
4609 * o destination transport addr - specified as one of the transport
4610 * addresses of the peer endpoint with which the association is to be
4611 * established.
4612 * [This is asoc->peer.active_path.]
4613 * o outbound stream count - the number of outbound streams the ULP
4614 * would like to open towards this peer endpoint.
4615 * [BUG: This is not currently implemented.]
4616 * Optional attributes:
4617 *
4618 * None.
4619 *
4620 * The return value is a disposition.
4621 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004622sctp_disposition_t sctp_sf_do_prm_asoc(struct net *net,
4623 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 const struct sctp_association *asoc,
4625 const sctp_subtype_t type,
4626 void *arg,
4627 sctp_cmd_seq_t *commands)
4628{
4629 struct sctp_chunk *repl;
wangweidong26ac8e52013-12-23 12:16:51 +08004630 struct sctp_association *my_asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631
4632 /* The comment below says that we enter COOKIE-WAIT AFTER
4633 * sending the INIT, but that doesn't actually work in our
4634 * implementation...
4635 */
4636 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4637 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4638
4639 /* RFC 2960 5.1 Normal Establishment of an Association
4640 *
4641 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4642 * must provide its Verification Tag (Tag_A) in the Initiate
4643 * Tag field. Tag_A SHOULD be a random number in the range of
4644 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4645 */
4646
4647 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4648 if (!repl)
4649 goto nomem;
4650
Daniel Borkmann405426f2013-06-14 18:24:05 +02004651 /* Choose transport for INIT. */
4652 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4653 SCTP_CHUNK(repl));
4654
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 /* Cast away the const modifier, as we want to just
4656 * rerun it through as a sideffect.
4657 */
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004658 my_asoc = (struct sctp_association *)asoc;
4659 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660
4661 /* After sending the INIT, "A" starts the T1-init timer and
4662 * enters the COOKIE-WAIT state.
4663 */
4664 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4665 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4666 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4667 return SCTP_DISPOSITION_CONSUME;
4668
4669nomem:
4670 return SCTP_DISPOSITION_NOMEM;
4671}
4672
4673/*
4674 * Process the SEND primitive.
4675 *
4676 * Section: 10.1 ULP-to-SCTP
4677 * E) Send
4678 *
4679 * Format: SEND(association id, buffer address, byte count [,context]
4680 * [,stream id] [,life time] [,destination transport address]
4681 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4682 * -> result
4683 *
4684 * This is the main method to send user data via SCTP.
4685 *
4686 * Mandatory attributes:
4687 *
4688 * o association id - local handle to the SCTP association
4689 *
4690 * o buffer address - the location where the user message to be
4691 * transmitted is stored;
4692 *
4693 * o byte count - The size of the user data in number of bytes;
4694 *
4695 * Optional attributes:
4696 *
4697 * o context - an optional 32 bit integer that will be carried in the
4698 * sending failure notification to the ULP if the transportation of
4699 * this User Message fails.
4700 *
4701 * o stream id - to indicate which stream to send the data on. If not
4702 * specified, stream 0 will be used.
4703 *
4704 * o life time - specifies the life time of the user data. The user data
4705 * will not be sent by SCTP after the life time expires. This
4706 * parameter can be used to avoid efforts to transmit stale
4707 * user messages. SCTP notifies the ULP if the data cannot be
4708 * initiated to transport (i.e. sent to the destination via SCTP's
4709 * send primitive) within the life time variable. However, the
4710 * user data will be transmitted if SCTP has attempted to transmit a
4711 * chunk before the life time expired.
4712 *
4713 * o destination transport address - specified as one of the destination
4714 * transport addresses of the peer endpoint to which this packet
4715 * should be sent. Whenever possible, SCTP should use this destination
4716 * transport address for sending the packets, instead of the current
4717 * primary path.
4718 *
4719 * o unorder flag - this flag, if present, indicates that the user
4720 * would like the data delivered in an unordered fashion to the peer
4721 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4722 * message).
4723 *
4724 * o no-bundle flag - instructs SCTP not to bundle this user data with
4725 * other outbound DATA chunks. SCTP MAY still bundle even when
4726 * this flag is present, when faced with network congestion.
4727 *
4728 * o payload protocol-id - A 32 bit unsigned integer that is to be
4729 * passed to the peer indicating the type of payload protocol data
4730 * being transmitted. This value is passed as opaque data by SCTP.
4731 *
4732 * The return value is the disposition.
4733 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004734sctp_disposition_t sctp_sf_do_prm_send(struct net *net,
4735 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 const struct sctp_association *asoc,
4737 const sctp_subtype_t type,
4738 void *arg,
4739 sctp_cmd_seq_t *commands)
4740{
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004741 struct sctp_datamsg *msg = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004743 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 return SCTP_DISPOSITION_CONSUME;
4745}
4746
4747/*
4748 * Process the SHUTDOWN primitive.
4749 *
4750 * Section: 10.1:
4751 * C) Shutdown
4752 *
4753 * Format: SHUTDOWN(association id)
4754 * -> result
4755 *
4756 * Gracefully closes an association. Any locally queued user data
4757 * will be delivered to the peer. The association will be terminated only
4758 * after the peer acknowledges all the SCTP packets sent. A success code
4759 * will be returned on successful termination of the association. If
4760 * attempting to terminate the association results in a failure, an error
4761 * code shall be returned.
4762 *
4763 * Mandatory attributes:
4764 *
4765 * o association id - local handle to the SCTP association
4766 *
4767 * Optional attributes:
4768 *
4769 * None.
4770 *
4771 * The return value is the disposition.
4772 */
4773sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004774 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 const struct sctp_endpoint *ep,
4776 const struct sctp_association *asoc,
4777 const sctp_subtype_t type,
4778 void *arg,
4779 sctp_cmd_seq_t *commands)
4780{
4781 int disposition;
4782
4783 /* From 9.2 Shutdown of an Association
4784 * Upon receipt of the SHUTDOWN primitive from its upper
4785 * layer, the endpoint enters SHUTDOWN-PENDING state and
4786 * remains there until all outstanding data has been
4787 * acknowledged by its peer. The endpoint accepts no new data
4788 * from its upper layer, but retransmits data to the far end
4789 * if necessary to fill gaps.
4790 */
4791 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4792 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4793
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 disposition = SCTP_DISPOSITION_CONSUME;
4795 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004796 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 arg, commands);
4798 }
4799 return disposition;
4800}
4801
4802/*
4803 * Process the ABORT primitive.
4804 *
4805 * Section: 10.1:
4806 * C) Abort
4807 *
4808 * Format: Abort(association id [, cause code])
4809 * -> result
4810 *
4811 * Ungracefully closes an association. Any locally queued user data
4812 * will be discarded and an ABORT chunk is sent to the peer. A success code
4813 * will be returned on successful abortion of the association. If
4814 * attempting to abort the association results in a failure, an error
4815 * code shall be returned.
4816 *
4817 * Mandatory attributes:
4818 *
4819 * o association id - local handle to the SCTP association
4820 *
4821 * Optional attributes:
4822 *
4823 * o cause code - reason of the abort to be passed to the peer
4824 *
4825 * None.
4826 *
4827 * The return value is the disposition.
4828 */
4829sctp_disposition_t sctp_sf_do_9_1_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004830 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 const struct sctp_endpoint *ep,
4832 const struct sctp_association *asoc,
4833 const sctp_subtype_t type,
4834 void *arg,
4835 sctp_cmd_seq_t *commands)
4836{
4837 /* From 9.1 Abort of an Association
4838 * Upon receipt of the ABORT primitive from its upper
4839 * layer, the endpoint enters CLOSED state and
4840 * discard all outstanding data has been
4841 * acknowledged by its peer. The endpoint accepts no new data
4842 * from its upper layer, but retransmits data to the far end
4843 * if necessary to fill gaps.
4844 */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004845 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846
Xin Long068d8bd2015-12-29 17:49:25 +08004847 if (abort)
4848 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849
4850 /* Even if we can't send the ABORT due to low memory delete the
4851 * TCB. This is a departure from our typical NOMEM handling.
4852 */
4853
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004854 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4855 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 /* Delete the established association. */
4857 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08004858 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004860 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4861 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02004863 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864}
4865
4866/* We tried an illegal operation on an association which is closed. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004867sctp_disposition_t sctp_sf_error_closed(struct net *net,
4868 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 const struct sctp_association *asoc,
4870 const sctp_subtype_t type,
4871 void *arg,
4872 sctp_cmd_seq_t *commands)
4873{
4874 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4875 return SCTP_DISPOSITION_CONSUME;
4876}
4877
4878/* We tried an illegal operation on an association which is shutting
4879 * down.
4880 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004881sctp_disposition_t sctp_sf_error_shutdown(struct net *net,
4882 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 const struct sctp_association *asoc,
4884 const sctp_subtype_t type,
4885 void *arg,
4886 sctp_cmd_seq_t *commands)
4887{
4888 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4889 SCTP_ERROR(-ESHUTDOWN));
4890 return SCTP_DISPOSITION_CONSUME;
4891}
4892
4893/*
4894 * sctp_cookie_wait_prm_shutdown
4895 *
4896 * Section: 4 Note: 2
4897 * Verification Tag:
4898 * Inputs
4899 * (endpoint, asoc)
4900 *
4901 * The RFC does not explicitly address this issue, but is the route through the
4902 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4903 *
4904 * Outputs
4905 * (timers)
4906 */
4907sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004908 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 const struct sctp_endpoint *ep,
4910 const struct sctp_association *asoc,
4911 const sctp_subtype_t type,
4912 void *arg,
4913 sctp_cmd_seq_t *commands)
4914{
4915 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4916 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4917
4918 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4919 SCTP_STATE(SCTP_STATE_CLOSED));
4920
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004921 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922
4923 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4924
4925 return SCTP_DISPOSITION_DELETE_TCB;
4926}
4927
4928/*
4929 * sctp_cookie_echoed_prm_shutdown
4930 *
4931 * Section: 4 Note: 2
4932 * Verification Tag:
4933 * Inputs
4934 * (endpoint, asoc)
4935 *
4936 * The RFC does not explcitly address this issue, but is the route through the
4937 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4938 *
4939 * Outputs
4940 * (timers)
4941 */
4942sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004943 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 const struct sctp_endpoint *ep,
4945 const struct sctp_association *asoc,
4946 const sctp_subtype_t type,
4947 void *arg, sctp_cmd_seq_t *commands)
4948{
4949 /* There is a single T1 timer, so we should be able to use
4950 * common function with the COOKIE-WAIT state.
4951 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004952 return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953}
4954
4955/*
4956 * sctp_sf_cookie_wait_prm_abort
4957 *
4958 * Section: 4 Note: 2
4959 * Verification Tag:
4960 * Inputs
4961 * (endpoint, asoc)
4962 *
4963 * The RFC does not explicitly address this issue, but is the route through the
4964 * state table when someone issues an abort while in COOKIE_WAIT state.
4965 *
4966 * Outputs
4967 * (timers)
4968 */
4969sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004970 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 const struct sctp_endpoint *ep,
4972 const struct sctp_association *asoc,
4973 const sctp_subtype_t type,
4974 void *arg,
4975 sctp_cmd_seq_t *commands)
4976{
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004977 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978
4979 /* Stop T1-init timer */
4980 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4981 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982
Xin Long068d8bd2015-12-29 17:49:25 +08004983 if (abort)
4984 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985
4986 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4987 SCTP_STATE(SCTP_STATE_CLOSED));
4988
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004989 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990
4991 /* Even if we can't send the ABORT due to low memory delete the
4992 * TCB. This is a departure from our typical NOMEM handling.
4993 */
4994
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004995 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4996 SCTP_ERROR(ECONNREFUSED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 /* Delete the established association. */
4998 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08004999 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02005001 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002}
5003
5004/*
5005 * sctp_sf_cookie_echoed_prm_abort
5006 *
5007 * Section: 4 Note: 3
5008 * Verification Tag:
5009 * Inputs
5010 * (endpoint, asoc)
5011 *
5012 * The RFC does not explcitly address this issue, but is the route through the
5013 * state table when someone issues an abort while in COOKIE_ECHOED state.
5014 *
5015 * Outputs
5016 * (timers)
5017 */
5018sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005019 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 const struct sctp_endpoint *ep,
5021 const struct sctp_association *asoc,
5022 const sctp_subtype_t type,
5023 void *arg,
5024 sctp_cmd_seq_t *commands)
5025{
5026 /* There is a single T1 timer, so we should be able to use
5027 * common function with the COOKIE-WAIT state.
5028 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005029 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030}
5031
5032/*
5033 * sctp_sf_shutdown_pending_prm_abort
5034 *
5035 * Inputs
5036 * (endpoint, asoc)
5037 *
5038 * The RFC does not explicitly address this issue, but is the route through the
5039 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5040 *
5041 * Outputs
5042 * (timers)
5043 */
5044sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005045 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 const struct sctp_endpoint *ep,
5047 const struct sctp_association *asoc,
5048 const sctp_subtype_t type,
5049 void *arg,
5050 sctp_cmd_seq_t *commands)
5051{
5052 /* Stop the T5-shutdown guard timer. */
5053 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5054 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5055
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005056 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057}
5058
5059/*
5060 * sctp_sf_shutdown_sent_prm_abort
5061 *
5062 * Inputs
5063 * (endpoint, asoc)
5064 *
5065 * The RFC does not explicitly address this issue, but is the route through the
5066 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5067 *
5068 * Outputs
5069 * (timers)
5070 */
5071sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005072 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 const struct sctp_endpoint *ep,
5074 const struct sctp_association *asoc,
5075 const sctp_subtype_t type,
5076 void *arg,
5077 sctp_cmd_seq_t *commands)
5078{
5079 /* Stop the T2-shutdown timer. */
5080 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5081 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5082
5083 /* Stop the T5-shutdown guard timer. */
5084 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5085 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5086
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005087 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088}
5089
5090/*
5091 * sctp_sf_cookie_echoed_prm_abort
5092 *
5093 * Inputs
5094 * (endpoint, asoc)
5095 *
5096 * The RFC does not explcitly address this issue, but is the route through the
5097 * state table when someone issues an abort while in COOKIE_ECHOED state.
5098 *
5099 * Outputs
5100 * (timers)
5101 */
5102sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005103 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 const struct sctp_endpoint *ep,
5105 const struct sctp_association *asoc,
5106 const sctp_subtype_t type,
5107 void *arg,
5108 sctp_cmd_seq_t *commands)
5109{
5110 /* The same T2 timer, so we should be able to use
5111 * common function with the SHUTDOWN-SENT state.
5112 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005113 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114}
5115
5116/*
5117 * Process the REQUESTHEARTBEAT primitive
5118 *
5119 * 10.1 ULP-to-SCTP
5120 * J) Request Heartbeat
5121 *
5122 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5123 *
5124 * -> result
5125 *
5126 * Instructs the local endpoint to perform a HeartBeat on the specified
5127 * destination transport address of the given association. The returned
5128 * result should indicate whether the transmission of the HEARTBEAT
5129 * chunk to the destination address is successful.
5130 *
5131 * Mandatory attributes:
5132 *
5133 * o association id - local handle to the SCTP association
5134 *
5135 * o destination transport address - the transport address of the
5136 * association on which a heartbeat should be issued.
5137 */
5138sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005139 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 const struct sctp_endpoint *ep,
5141 const struct sctp_association *asoc,
5142 const sctp_subtype_t type,
5143 void *arg,
5144 sctp_cmd_seq_t *commands)
5145{
Vlad Yasevichfb785252007-03-19 17:02:03 -07005146 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5147 (struct sctp_transport *)arg, commands))
5148 return SCTP_DISPOSITION_NOMEM;
5149
5150 /*
5151 * RFC 2960 (bis), section 8.3
5152 *
5153 * D) Request an on-demand HEARTBEAT on a specific destination
5154 * transport address of a given association.
5155 *
5156 * The endpoint should increment the respective error counter of
5157 * the destination transport address each time a HEARTBEAT is sent
5158 * to that address and not acknowledged within one RTO.
5159 *
5160 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00005161 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Vlad Yasevichfb785252007-03-19 17:02:03 -07005162 SCTP_TRANSPORT(arg));
5163 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164}
5165
5166/*
5167 * ADDIP Section 4.1 ASCONF Chunk Procedures
5168 * When an endpoint has an ASCONF signaled change to be sent to the
5169 * remote endpoint it should do A1 to A9
5170 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005171sctp_disposition_t sctp_sf_do_prm_asconf(struct net *net,
5172 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 const struct sctp_association *asoc,
5174 const sctp_subtype_t type,
5175 void *arg,
5176 sctp_cmd_seq_t *commands)
5177{
5178 struct sctp_chunk *chunk = arg;
5179
5180 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5181 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5182 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5183 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5184 return SCTP_DISPOSITION_CONSUME;
5185}
5186
5187/*
5188 * Ignore the primitive event
5189 *
5190 * The return value is the disposition of the primitive.
5191 */
5192sctp_disposition_t sctp_sf_ignore_primitive(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005193 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 const struct sctp_endpoint *ep,
5195 const struct sctp_association *asoc,
5196 const sctp_subtype_t type,
5197 void *arg,
5198 sctp_cmd_seq_t *commands)
5199{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005200 pr_debug("%s: primitive type:%d is ignored\n", __func__,
5201 type.primitive);
5202
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 return SCTP_DISPOSITION_DISCARD;
5204}
5205
5206/***************************************************************************
5207 * These are the state functions for the OTHER events.
5208 ***************************************************************************/
5209
5210/*
Wei Yongjune1cdd552011-04-17 17:29:03 +00005211 * When the SCTP stack has no more user data to send or retransmit, this
5212 * notification is given to the user. Also, at the time when a user app
5213 * subscribes to this event, if there is no data to be sent or
5214 * retransmit, the stack will immediately send up this notification.
5215 */
5216sctp_disposition_t sctp_sf_do_no_pending_tsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005217 struct net *net,
Wei Yongjune1cdd552011-04-17 17:29:03 +00005218 const struct sctp_endpoint *ep,
5219 const struct sctp_association *asoc,
5220 const sctp_subtype_t type,
5221 void *arg,
5222 sctp_cmd_seq_t *commands)
5223{
5224 struct sctp_ulpevent *event;
5225
5226 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5227 if (!event)
5228 return SCTP_DISPOSITION_NOMEM;
5229
5230 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5231
5232 return SCTP_DISPOSITION_CONSUME;
5233}
5234
5235/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 * Start the shutdown negotiation.
5237 *
5238 * From Section 9.2:
5239 * Once all its outstanding data has been acknowledged, the endpoint
5240 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5241 * TSN Ack field the last sequential TSN it has received from the peer.
5242 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5243 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5244 * with the updated last sequential TSN received from its peer.
5245 *
5246 * The return value is the disposition.
5247 */
5248sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005249 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 const struct sctp_endpoint *ep,
5251 const struct sctp_association *asoc,
5252 const sctp_subtype_t type,
5253 void *arg,
5254 sctp_cmd_seq_t *commands)
5255{
5256 struct sctp_chunk *reply;
5257
5258 /* Once all its outstanding data has been acknowledged, the
5259 * endpoint shall send a SHUTDOWN chunk to its peer including
5260 * in the Cumulative TSN Ack field the last sequential TSN it
5261 * has received from the peer.
5262 */
5263 reply = sctp_make_shutdown(asoc, NULL);
5264 if (!reply)
5265 goto nomem;
5266
5267 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5268 * T2-shutdown timer.
5269 */
5270 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5271
5272 /* It shall then start the T2-shutdown timer */
5273 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5274 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5275
Wei Yongjun536428a2008-09-05 08:55:26 +08005276 /* RFC 4960 Section 9.2
5277 * The sender of the SHUTDOWN MAY also start an overall guard timer
5278 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5279 */
Thomas Graff8d96052011-07-07 00:28:35 +00005280 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
Wei Yongjun536428a2008-09-05 08:55:26 +08005281 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5282
Neil Horman9f70f462013-12-10 06:48:15 -05005283 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5285 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5286
5287 /* and enter the SHUTDOWN-SENT state. */
5288 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5289 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5290
5291 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5292 *
5293 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005294 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 */
5296 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5297
5298 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5299
5300 return SCTP_DISPOSITION_CONSUME;
5301
5302nomem:
5303 return SCTP_DISPOSITION_NOMEM;
5304}
5305
5306/*
5307 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5308 *
5309 * From Section 9.2:
5310 *
5311 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5312 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5313 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5314 * endpoint must re-send the SHUTDOWN ACK.
5315 *
5316 * The return value is the disposition.
5317 */
5318sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005319 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 const struct sctp_endpoint *ep,
5321 const struct sctp_association *asoc,
5322 const sctp_subtype_t type,
5323 void *arg,
5324 sctp_cmd_seq_t *commands)
5325{
5326 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5327 struct sctp_chunk *reply;
5328
5329 /* There are 2 ways of getting here:
5330 * 1) called in response to a SHUTDOWN chunk
5331 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5332 *
5333 * For the case (2), the arg parameter is set to NULL. We need
5334 * to check that we have a chunk before accessing it's fields.
5335 */
5336 if (chunk) {
5337 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005338 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339
5340 /* Make sure that the SHUTDOWN chunk has a valid length. */
5341 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005342 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 commands);
5344 }
5345
5346 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5347 * shall send a SHUTDOWN ACK ...
5348 */
5349 reply = sctp_make_shutdown_ack(asoc, chunk);
5350 if (!reply)
5351 goto nomem;
5352
5353 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5354 * the T2-shutdown timer.
5355 */
5356 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5357
5358 /* and start/restart a T2-shutdown timer of its own, */
5359 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5360 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5361
Neil Horman9f70f462013-12-10 06:48:15 -05005362 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5364 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5365
5366 /* Enter the SHUTDOWN-ACK-SENT state. */
5367 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5368 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5369
5370 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5371 *
5372 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005373 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 */
5375 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5376
5377 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5378
5379 return SCTP_DISPOSITION_CONSUME;
5380
5381nomem:
5382 return SCTP_DISPOSITION_NOMEM;
5383}
5384
5385/*
5386 * Ignore the event defined as other
5387 *
5388 * The return value is the disposition of the event.
5389 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005390sctp_disposition_t sctp_sf_ignore_other(struct net *net,
5391 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 const struct sctp_association *asoc,
5393 const sctp_subtype_t type,
5394 void *arg,
5395 sctp_cmd_seq_t *commands)
5396{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005397 pr_debug("%s: the event other type:%d is ignored\n",
5398 __func__, type.other);
5399
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 return SCTP_DISPOSITION_DISCARD;
5401}
5402
5403/************************************************************
5404 * These are the state functions for handling timeout events.
5405 ************************************************************/
5406
5407/*
5408 * RTX Timeout
5409 *
5410 * Section: 6.3.3 Handle T3-rtx Expiration
5411 *
5412 * Whenever the retransmission timer T3-rtx expires for a destination
5413 * address, do the following:
5414 * [See below]
5415 *
5416 * The return value is the disposition of the chunk.
5417 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005418sctp_disposition_t sctp_sf_do_6_3_3_rtx(struct net *net,
5419 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 const struct sctp_association *asoc,
5421 const sctp_subtype_t type,
5422 void *arg,
5423 sctp_cmd_seq_t *commands)
5424{
5425 struct sctp_transport *transport = arg;
5426
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005427 SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005428
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 if (asoc->overall_error_count >= asoc->max_retrans) {
lucien8a0d19c2015-12-05 15:35:36 +08005430 if (asoc->peer.zero_window_announced &&
5431 asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
Thomas Graff8d96052011-07-07 00:28:35 +00005432 /*
5433 * We are here likely because the receiver had its rwnd
5434 * closed for a while and we have not been able to
5435 * transmit the locally queued data within the maximum
5436 * retransmission attempts limit. Start the T5
5437 * shutdown guard timer to give the receiver one last
5438 * chance and some additional time to recover before
5439 * aborting.
5440 */
5441 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5442 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5443 } else {
5444 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5445 SCTP_ERROR(ETIMEDOUT));
5446 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5447 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5448 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005449 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5450 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Thomas Graff8d96052011-07-07 00:28:35 +00005451 return SCTP_DISPOSITION_DELETE_TCB;
5452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 }
5454
5455 /* E1) For the destination address for which the timer
5456 * expires, adjust its ssthresh with rules defined in Section
5457 * 7.2.3 and set the cwnd <- MTU.
5458 */
5459
5460 /* E2) For the destination address for which the timer
5461 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5462 * maximum value discussed in rule C7 above (RTO.max) may be
5463 * used to provide an upper bound to this doubling operation.
5464 */
5465
5466 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5467 * outstanding DATA chunks for the address for which the
5468 * T3-rtx has expired will fit into a single packet, subject
5469 * to the MTU constraint for the path corresponding to the
5470 * destination transport address to which the retransmission
5471 * is being sent (this may be different from the address for
5472 * which the timer expires [see Section 6.4]). Call this
5473 * value K. Bundle and retransmit those K DATA chunks in a
5474 * single packet to the destination endpoint.
5475 *
5476 * Note: Any DATA chunks that were sent to the address for
5477 * which the T3-rtx timer expired but did not fit in one MTU
5478 * (rule E3 above), should be marked for retransmission and
5479 * sent as soon as cwnd allows (normally when a SACK arrives).
5480 */
5481
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 /* Do some failure management (Section 8.2). */
5483 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5484
Vlad Yasevich1845a572007-02-21 02:06:19 -08005485 /* NB: Rules E4 and F1 are implicit in R1. */
5486 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5487
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 return SCTP_DISPOSITION_CONSUME;
5489}
5490
5491/*
5492 * Generate delayed SACK on timeout
5493 *
5494 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5495 *
5496 * The guidelines on delayed acknowledgement algorithm specified in
5497 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5498 * acknowledgement SHOULD be generated for at least every second packet
5499 * (not every second DATA chunk) received, and SHOULD be generated
5500 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5501 * some situations it may be beneficial for an SCTP transmitter to be
5502 * more conservative than the algorithms detailed in this document
5503 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5504 * the following algorithms allow.
5505 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005506sctp_disposition_t sctp_sf_do_6_2_sack(struct net *net,
5507 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 const struct sctp_association *asoc,
5509 const sctp_subtype_t type,
5510 void *arg,
5511 sctp_cmd_seq_t *commands)
5512{
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005513 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5515 return SCTP_DISPOSITION_CONSUME;
5516}
5517
5518/*
Frank Filz3f7a87d2005-06-20 13:14:57 -07005519 * sctp_sf_t1_init_timer_expire
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 *
5521 * Section: 4 Note: 2
5522 * Verification Tag:
5523 * Inputs
5524 * (endpoint, asoc)
5525 *
5526 * RFC 2960 Section 4 Notes
5527 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5528 * and re-start the T1-init timer without changing state. This MUST
5529 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5530 * endpoint MUST abort the initialization process and report the
5531 * error to SCTP user.
5532 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07005533 * Outputs
5534 * (timers, events)
5535 *
5536 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005537sctp_disposition_t sctp_sf_t1_init_timer_expire(struct net *net,
5538 const struct sctp_endpoint *ep,
Frank Filz3f7a87d2005-06-20 13:14:57 -07005539 const struct sctp_association *asoc,
5540 const sctp_subtype_t type,
5541 void *arg,
5542 sctp_cmd_seq_t *commands)
5543{
5544 struct sctp_chunk *repl = NULL;
5545 struct sctp_bind_addr *bp;
5546 int attempts = asoc->init_err_counter + 1;
5547
Daniel Borkmannbb333812013-06-28 19:49:40 +02005548 pr_debug("%s: timer T1 expired (INIT)\n", __func__);
5549
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005550 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005551
Vlad Yasevich81845c22006-01-30 15:59:54 -08005552 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005553 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5554 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5555 if (!repl)
5556 return SCTP_DISPOSITION_NOMEM;
5557
5558 /* Choose transport for INIT. */
5559 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5560 SCTP_CHUNK(repl));
5561
5562 /* Issue a sideeffect to do the needed accounting. */
5563 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5564 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5565
5566 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5567 } else {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005568 pr_debug("%s: giving up on INIT, attempts:%d "
5569 "max_init_attempts:%d\n", __func__, attempts,
5570 asoc->max_init_attempts);
5571
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005572 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5573 SCTP_ERROR(ETIMEDOUT));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005574 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005575 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005576 return SCTP_DISPOSITION_DELETE_TCB;
5577 }
5578
5579 return SCTP_DISPOSITION_CONSUME;
5580}
5581
5582/*
5583 * sctp_sf_t1_cookie_timer_expire
5584 *
5585 * Section: 4 Note: 2
5586 * Verification Tag:
5587 * Inputs
5588 * (endpoint, asoc)
5589 *
5590 * RFC 2960 Section 4 Notes
5591 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 * COOKIE ECHO and re-start the T1-cookie timer without changing
5593 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5594 * After that, the endpoint MUST abort the initialization process and
5595 * report the error to SCTP user.
5596 *
5597 * Outputs
5598 * (timers, events)
5599 *
5600 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005601sctp_disposition_t sctp_sf_t1_cookie_timer_expire(struct net *net,
5602 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 const struct sctp_association *asoc,
5604 const sctp_subtype_t type,
5605 void *arg,
5606 sctp_cmd_seq_t *commands)
5607{
Frank Filz3f7a87d2005-06-20 13:14:57 -07005608 struct sctp_chunk *repl = NULL;
5609 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610
Daniel Borkmannbb333812013-06-28 19:49:40 +02005611 pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
5612
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005613 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614
Vlad Yasevich81845c22006-01-30 15:59:54 -08005615 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005616 repl = sctp_make_cookie_echo(asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 if (!repl)
Frank Filz3f7a87d2005-06-20 13:14:57 -07005618 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619
Vlad Yasevich96cd0d32008-09-08 14:00:26 -04005620 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5621 SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 /* Issue a sideeffect to do the needed accounting. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005623 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5624 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5625
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5627 } else {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005628 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5629 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005631 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 return SCTP_DISPOSITION_DELETE_TCB;
5633 }
5634
5635 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636}
5637
5638/* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5639 * with the updated last sequential TSN received from its peer.
5640 *
5641 * An endpoint should limit the number of retransmissions of the
5642 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5643 * If this threshold is exceeded the endpoint should destroy the TCB and
5644 * MUST report the peer endpoint unreachable to the upper layer (and
5645 * thus the association enters the CLOSED state). The reception of any
5646 * packet from its peer (i.e. as the peer sends all of its queued DATA
5647 * chunks) should clear the endpoint's retransmission count and restart
5648 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5649 * all of its queued DATA chunks that have not yet been sent.
5650 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005651sctp_disposition_t sctp_sf_t2_timer_expire(struct net *net,
5652 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 const struct sctp_association *asoc,
5654 const sctp_subtype_t type,
5655 void *arg,
5656 sctp_cmd_seq_t *commands)
5657{
5658 struct sctp_chunk *reply = NULL;
5659
Daniel Borkmannbb333812013-06-28 19:49:40 +02005660 pr_debug("%s: timer T2 expired\n", __func__);
5661
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005662 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005663
Neil Horman58fbbed2008-02-29 11:40:56 -08005664 ((struct sctp_association *)asoc)->shutdown_retries++;
5665
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005667 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5668 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5670 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005671 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005672 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5673 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 return SCTP_DISPOSITION_DELETE_TCB;
5675 }
5676
5677 switch (asoc->state) {
5678 case SCTP_STATE_SHUTDOWN_SENT:
5679 reply = sctp_make_shutdown(asoc, NULL);
5680 break;
5681
5682 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5683 reply = sctp_make_shutdown_ack(asoc, NULL);
5684 break;
5685
5686 default:
5687 BUG();
5688 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690
5691 if (!reply)
5692 goto nomem;
5693
Wei Yongjun6345b192009-04-26 23:13:35 +08005694 /* Do some failure management (Section 8.2).
5695 * If we remove the transport an SHUTDOWN was last sent to, don't
5696 * do failure management.
5697 */
5698 if (asoc->shutdown_last_sent_to)
5699 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5700 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701
5702 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5703 * the T2-shutdown timer.
5704 */
5705 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5706
5707 /* Restart the T2-shutdown timer. */
5708 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5709 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5710 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5711 return SCTP_DISPOSITION_CONSUME;
5712
5713nomem:
5714 return SCTP_DISPOSITION_NOMEM;
5715}
5716
5717/*
5718 * ADDIP Section 4.1 ASCONF CHunk Procedures
5719 * If the T4 RTO timer expires the endpoint should do B1 to B5
5720 */
5721sctp_disposition_t sctp_sf_t4_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005722 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 const struct sctp_endpoint *ep,
5724 const struct sctp_association *asoc,
5725 const sctp_subtype_t type,
5726 void *arg,
5727 sctp_cmd_seq_t *commands)
5728{
5729 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5730 struct sctp_transport *transport = chunk->transport;
5731
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005732 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005733
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5735 * detection on the appropriate destination address as defined in
5736 * RFC2960 [5] section 8.1 and 8.2.
5737 */
Wei Yongjun10a43ce2009-04-26 23:14:42 +08005738 if (transport)
5739 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5740 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741
5742 /* Reconfig T4 timer and transport. */
5743 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5744
5745 /* ADDIP 4.1 B2) Increment the association error counters and perform
5746 * endpoint failure detection on the association as defined in
5747 * RFC2960 [5] section 8.1 and 8.2.
5748 * association error counter is incremented in SCTP_CMD_STRIKE.
5749 */
5750 if (asoc->overall_error_count >= asoc->max_retrans) {
5751 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5752 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005753 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5754 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005756 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005757 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5758 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 return SCTP_DISPOSITION_ABORT;
5760 }
5761
5762 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5763 * the ASCONF chunk was sent by doubling the RTO timer value.
5764 * This is done in SCTP_CMD_STRIKE.
5765 */
5766
5767 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5768 * choose an alternate destination address (please refer to RFC2960
5769 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005770 * chunk, it MUST be the same (including its serial number) as the last
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 * ASCONF sent.
5772 */
5773 sctp_chunk_hold(asoc->addip_last_asconf);
5774 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5775 SCTP_CHUNK(asoc->addip_last_asconf));
5776
5777 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5778 * destination is selected, then the RTO used will be that of the new
5779 * destination address.
5780 */
5781 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5782 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5783
5784 return SCTP_DISPOSITION_CONSUME;
5785}
5786
5787/* sctpimpguide-05 Section 2.12.2
5788 * The sender of the SHUTDOWN MAY also start an overall guard timer
5789 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5790 * At the expiration of this timer the sender SHOULD abort the association
5791 * by sending an ABORT chunk.
5792 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005793sctp_disposition_t sctp_sf_t5_timer_expire(struct net *net,
5794 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795 const struct sctp_association *asoc,
5796 const sctp_subtype_t type,
5797 void *arg,
5798 sctp_cmd_seq_t *commands)
5799{
5800 struct sctp_chunk *reply = NULL;
5801
Daniel Borkmannbb333812013-06-28 19:49:40 +02005802 pr_debug("%s: timer T5 expired\n", __func__);
5803
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005804 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805
5806 reply = sctp_make_abort(asoc, NULL, 0);
5807 if (!reply)
5808 goto nomem;
5809
5810 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005811 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5812 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005814 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005816 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5817 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevicha1080a82008-10-09 14:33:26 -07005818
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 return SCTP_DISPOSITION_DELETE_TCB;
5820nomem:
5821 return SCTP_DISPOSITION_NOMEM;
5822}
5823
5824/* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5825 * the association is automatically closed by starting the shutdown process.
5826 * The work that needs to be done is same as when SHUTDOWN is initiated by
5827 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5828 */
5829sctp_disposition_t sctp_sf_autoclose_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005830 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 const struct sctp_endpoint *ep,
5832 const struct sctp_association *asoc,
5833 const sctp_subtype_t type,
5834 void *arg,
5835 sctp_cmd_seq_t *commands)
5836{
5837 int disposition;
5838
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005839 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005840
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 /* From 9.2 Shutdown of an Association
5842 * Upon receipt of the SHUTDOWN primitive from its upper
5843 * layer, the endpoint enters SHUTDOWN-PENDING state and
5844 * remains there until all outstanding data has been
5845 * acknowledged by its peer. The endpoint accepts no new data
5846 * from its upper layer, but retransmits data to the far end
5847 * if necessary to fill gaps.
5848 */
5849 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5850 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5851
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 disposition = SCTP_DISPOSITION_CONSUME;
5853 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005854 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 arg, commands);
5856 }
5857 return disposition;
5858}
5859
5860/*****************************************************************************
5861 * These are sa state functions which could apply to all types of events.
5862 ****************************************************************************/
5863
5864/*
5865 * This table entry is not implemented.
5866 *
5867 * Inputs
5868 * (endpoint, asoc, chunk)
5869 *
5870 * The return value is the disposition of the chunk.
5871 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005872sctp_disposition_t sctp_sf_not_impl(struct net *net,
5873 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 const struct sctp_association *asoc,
5875 const sctp_subtype_t type,
5876 void *arg,
5877 sctp_cmd_seq_t *commands)
5878{
5879 return SCTP_DISPOSITION_NOT_IMPL;
5880}
5881
5882/*
5883 * This table entry represents a bug.
5884 *
5885 * Inputs
5886 * (endpoint, asoc, chunk)
5887 *
5888 * The return value is the disposition of the chunk.
5889 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005890sctp_disposition_t sctp_sf_bug(struct net *net,
5891 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 const struct sctp_association *asoc,
5893 const sctp_subtype_t type,
5894 void *arg,
5895 sctp_cmd_seq_t *commands)
5896{
5897 return SCTP_DISPOSITION_BUG;
5898}
5899
5900/*
5901 * This table entry represents the firing of a timer in the wrong state.
5902 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5903 * when the association is in the wrong state. This event should
5904 * be ignored, so as to prevent any rearming of the timer.
5905 *
5906 * Inputs
5907 * (endpoint, asoc, chunk)
5908 *
5909 * The return value is the disposition of the chunk.
5910 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005911sctp_disposition_t sctp_sf_timer_ignore(struct net *net,
5912 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 const struct sctp_association *asoc,
5914 const sctp_subtype_t type,
5915 void *arg,
5916 sctp_cmd_seq_t *commands)
5917{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005918 pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
5919
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 return SCTP_DISPOSITION_CONSUME;
5921}
5922
5923/********************************************************************
5924 * 2nd Level Abstractions
5925 ********************************************************************/
5926
5927/* Pull the SACK chunk based on the SACK header. */
5928static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5929{
5930 struct sctp_sackhdr *sack;
5931 unsigned int len;
5932 __u16 num_blocks;
5933 __u16 num_dup_tsns;
5934
5935 /* Protect ourselves from reading too far into
5936 * the skb from a bogus sender.
5937 */
5938 sack = (struct sctp_sackhdr *) chunk->skb->data;
5939
5940 num_blocks = ntohs(sack->num_gap_ack_blocks);
5941 num_dup_tsns = ntohs(sack->num_dup_tsns);
5942 len = sizeof(struct sctp_sackhdr);
5943 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5944 if (len > chunk->skb->len)
5945 return NULL;
5946
5947 skb_pull(chunk->skb, len);
5948
5949 return sack;
5950}
5951
5952/* Create an ABORT packet to be sent as a response, with the specified
5953 * error causes.
5954 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005955static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
5956 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 const struct sctp_association *asoc,
5958 struct sctp_chunk *chunk,
5959 const void *payload,
5960 size_t paylen)
5961{
5962 struct sctp_packet *packet;
5963 struct sctp_chunk *abort;
5964
Eric W. Biederman2ce95502012-08-06 08:43:06 +00005965 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966
5967 if (packet) {
5968 /* Make an ABORT.
5969 * The T bit will be set if the asoc is NULL.
5970 */
5971 abort = sctp_make_abort(asoc, chunk, paylen);
5972 if (!abort) {
5973 sctp_ootb_pkt_free(packet);
5974 return NULL;
5975 }
Jerome Forissier047a2422005-04-28 11:58:43 -07005976
5977 /* Reflect vtag if T-Bit is set */
5978 if (sctp_test_T_bit(abort))
5979 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5980
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 /* Add specified error causes, i.e., payload, to the
5982 * end of the chunk.
5983 */
5984 sctp_addto_chunk(abort, paylen, payload);
5985
5986 /* Set the skb to the belonging sock for accounting. */
5987 abort->skb->sk = ep->base.sk;
5988
5989 sctp_packet_append_chunk(packet, abort);
5990
5991 }
5992
5993 return packet;
5994}
5995
5996/* Allocate a packet for responding in the OOTB conditions. */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00005997static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
5998 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999 const struct sctp_chunk *chunk)
6000{
6001 struct sctp_packet *packet;
6002 struct sctp_transport *transport;
6003 __u16 sport;
6004 __u16 dport;
6005 __u32 vtag;
6006
6007 /* Get the source and destination port from the inbound packet. */
6008 sport = ntohs(chunk->sctp_hdr->dest);
6009 dport = ntohs(chunk->sctp_hdr->source);
6010
6011 /* The V-tag is going to be the same as the inbound packet if no
6012 * association exists, otherwise, use the peer's vtag.
6013 */
6014 if (asoc) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08006015 /* Special case the INIT-ACK as there is no peer's vtag
6016 * yet.
6017 */
wangweidongcb3f8372013-12-23 12:16:50 +08006018 switch (chunk->chunk_hdr->type) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08006019 case SCTP_CID_INIT_ACK:
6020 {
6021 sctp_initack_chunk_t *initack;
6022
6023 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
6024 vtag = ntohl(initack->init_hdr.init_tag);
6025 break;
6026 }
6027 default:
6028 vtag = asoc->peer.i.init_tag;
6029 break;
6030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 } else {
6032 /* Special case the INIT and stale COOKIE_ECHO as there is no
6033 * vtag yet.
6034 */
wangweidongcb3f8372013-12-23 12:16:50 +08006035 switch (chunk->chunk_hdr->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 case SCTP_CID_INIT:
6037 {
6038 sctp_init_chunk_t *init;
6039
6040 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
6041 vtag = ntohl(init->init_hdr.init_tag);
6042 break;
6043 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006044 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 vtag = ntohl(chunk->sctp_hdr->vtag);
6046 break;
6047 }
6048 }
6049
6050 /* Make a transport for the bucket, Eliza... */
Eric W. Biederman89bf3452012-08-07 07:26:14 +00006051 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 if (!transport)
6053 goto nomem;
6054
6055 /* Cache a route for the transport with the chunk's destination as
6056 * the source address.
6057 */
Al Viro16b0a032006-11-20 17:13:38 -08006058 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006059 sctp_sk(net->sctp.ctl_sock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060
6061 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
6062 packet = sctp_packet_config(packet, vtag, 0);
6063
6064 return packet;
6065
6066nomem:
6067 return NULL;
6068}
6069
6070/* Free the packet allocated earlier for responding in the OOTB condition. */
6071void sctp_ootb_pkt_free(struct sctp_packet *packet)
6072{
6073 sctp_transport_free(packet->transport);
6074}
6075
6076/* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006077static void sctp_send_stale_cookie_err(struct net *net,
6078 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 const struct sctp_association *asoc,
6080 const struct sctp_chunk *chunk,
6081 sctp_cmd_seq_t *commands,
6082 struct sctp_chunk *err_chunk)
6083{
6084 struct sctp_packet *packet;
6085
6086 if (err_chunk) {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006087 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 if (packet) {
6089 struct sctp_signed_cookie *cookie;
6090
6091 /* Override the OOTB vtag from the cookie. */
6092 cookie = chunk->subh.cookie_hdr;
6093 packet->vtag = cookie->c.peer_vtag;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006094
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 /* Set the skb to the belonging sock for accounting. */
6096 err_chunk->skb->sk = ep->base.sk;
6097 sctp_packet_append_chunk(packet, err_chunk);
6098 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6099 SCTP_PACKET(packet));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006100 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 } else
6102 sctp_chunk_free (err_chunk);
6103 }
6104}
6105
6106
6107/* Process a data chunk */
6108static int sctp_eat_data(const struct sctp_association *asoc,
6109 struct sctp_chunk *chunk,
6110 sctp_cmd_seq_t *commands)
6111{
6112 sctp_datahdr_t *data_hdr;
6113 struct sctp_chunk *err;
6114 size_t datalen;
6115 sctp_verb_t deliver;
6116 int tmp;
6117 __u32 tsn;
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006118 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
Neil Horman049b3ff2005-11-11 16:08:24 -08006119 struct sock *sk = asoc->base.sk;
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006120 struct net *net = sock_net(sk);
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006121 u16 ssn;
6122 u16 sid;
6123 u8 ordered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124
6125 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
6126 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
6127
6128 tsn = ntohl(data_hdr->tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02006129 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130
6131 /* ASSERT: Now skb->data is really the user data. */
6132
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 /* Process ECN based congestion.
6134 *
6135 * Since the chunk structure is reused for all chunks within
6136 * a packet, we use ecn_ce_done to track if we've already
6137 * done CE processing for this packet.
6138 *
6139 * We need to do ECN processing even if we plan to discard the
6140 * chunk later.
6141 */
6142
Marcelo Ricardo Leitner2d47fd12016-07-13 15:09:00 -03006143 if (asoc->peer.ecn_capable && !chunk->ecn_ce_done) {
Marcelo Ricardo Leitnere7487c82016-07-13 15:08:58 -03006144 struct sctp_af *af = SCTP_INPUT_CB(chunk->skb)->af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145 chunk->ecn_ce_done = 1;
6146
Marcelo Ricardo Leitner2d47fd12016-07-13 15:09:00 -03006147 if (af->is_ce(sctp_gso_headskb(chunk->skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148 /* Do real work as sideffect. */
6149 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6150 SCTP_U32(tsn));
6151 }
6152 }
6153
6154 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6155 if (tmp < 0) {
6156 /* The TSN is too high--silently discard the chunk and
6157 * count on it getting retransmitted later.
6158 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006159 if (chunk->asoc)
6160 chunk->asoc->stats.outofseqtsns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 return SCTP_IERROR_HIGH_TSN;
6162 } else if (tmp > 0) {
6163 /* This is a duplicate. Record it. */
6164 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6165 return SCTP_IERROR_DUP_TSN;
6166 }
6167
6168 /* This is a new TSN. */
6169
6170 /* Discard if there is no room in the receive window.
6171 * Actually, allow a little bit of overflow (up to a MTU).
6172 */
6173 datalen = ntohs(chunk->chunk_hdr->length);
6174 datalen -= sizeof(sctp_data_chunk_t);
6175
6176 deliver = SCTP_CMD_CHUNK_ULP;
6177
6178 /* Think about partial delivery. */
6179 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6180
6181 /* Even if we don't accept this chunk there is
6182 * memory pressure.
6183 */
6184 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6185 }
6186
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006187 /* Spill over rwnd a little bit. Note: While allowed, this spill over
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 * seems a bit troublesome in that frag_point varies based on
6189 * PMTU. In cases, such as loopback, this might be a rather
6190 * large spill over.
6191 */
Daniel Borkmann362d5202014-04-14 21:45:17 +02006192 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
Neil Horman4d93df02007-08-15 16:07:44 -07006193 (datalen > asoc->rwnd + asoc->frag_point))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194
6195 /* If this is the next TSN, consider reneging to make
6196 * room. Note: Playing nice with a confused sender. A
6197 * malicious sender can still eat up all our buffer
6198 * space and in the future we may want to detect and
6199 * do more drastic reneging.
6200 */
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006201 if (sctp_tsnmap_has_gap(map) &&
6202 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006203 pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 deliver = SCTP_CMD_RENEGE;
6205 } else {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006206 pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
6207 __func__, tsn, datalen, asoc->rwnd);
6208
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209 return SCTP_IERROR_IGNORE_TSN;
6210 }
6211 }
6212
6213 /*
Neil Horman4d93df02007-08-15 16:07:44 -07006214 * Also try to renege to limit our memory usage in the event that
6215 * we are under memory pressure
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006216 * If we can't renege, don't worry about it, the sk_rmem_schedule
Neil Horman4d93df02007-08-15 16:07:44 -07006217 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6218 * memory usage too much
6219 */
6220 if (*sk->sk_prot_creator->memory_pressure) {
6221 if (sctp_tsnmap_has_gap(map) &&
wangweidongf7010e62013-12-23 12:16:52 +08006222 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006223 pr_debug("%s: under pressure, reneging for tsn:%u\n",
6224 __func__, tsn);
Neil Horman4d93df02007-08-15 16:07:44 -07006225 deliver = SCTP_CMD_RENEGE;
6226 }
6227 }
6228
6229 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230 * Section 3.3.10.9 No User Data (9)
6231 *
6232 * Cause of error
6233 * ---------------
6234 * No User Data: This error cause is returned to the originator of a
6235 * DATA chunk if a received DATA chunk has no user data.
6236 */
6237 if (unlikely(0 == datalen)) {
6238 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6239 if (err) {
6240 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6241 SCTP_CHUNK(err));
6242 }
6243 /* We are going to ABORT, so we might as well stop
6244 * processing the rest of the chunks in the packet.
6245 */
wangweidongcb3f8372013-12-23 12:16:50 +08006246 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07006247 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6248 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08006250 SCTP_PERR(SCTP_ERROR_NO_DATA));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006251 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6252 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253 return SCTP_IERROR_NO_DATA;
6254 }
6255
Sridhar Samudrala9faa7302006-07-21 14:49:07 -07006256 chunk->data_accepted = 1;
6257
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6259 * if we renege and the chunk arrives again.
6260 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006261 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006262 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006263 if (chunk->asoc)
6264 chunk->asoc->stats.iuodchunks++;
6265 } else {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006266 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006267 if (chunk->asoc)
6268 chunk->asoc->stats.iodchunks++;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006269 ordered = 1;
6270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271
6272 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6273 *
6274 * If an endpoint receive a DATA chunk with an invalid stream
6275 * identifier, it shall acknowledge the reception of the DATA chunk
6276 * following the normal procedure, immediately send an ERROR chunk
6277 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6278 * and discard the DATA chunk.
6279 */
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006280 sid = ntohs(data_hdr->stream);
6281 if (sid >= asoc->c.sinit_max_instreams) {
Vlad Yasevich3888e9e2008-07-08 02:28:39 -07006282 /* Mark tsn as received even though we drop it */
6283 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6284
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6286 &data_hdr->stream,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05006287 sizeof(data_hdr->stream),
6288 sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289 if (err)
6290 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6291 SCTP_CHUNK(err));
6292 return SCTP_IERROR_BAD_STREAM;
6293 }
6294
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006295 /* Check to see if the SSN is possible for this TSN.
6296 * The biggest gap we can record is 4K wide. Since SSNs wrap
6297 * at an unsigned short, there is no way that an SSN can
6298 * wrap and for a valid TSN. We can simply check if the current
6299 * SSN is smaller then the next expected one. If it is, it wrapped
6300 * and is invalid.
6301 */
6302 ssn = ntohs(data_hdr->ssn);
6303 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->ssnmap->in, sid))) {
6304 return SCTP_IERROR_PROTO_VIOLATION;
6305 }
6306
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307 /* Send the data up to the user. Note: Schedule the
6308 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6309 * chunk needs the updated rwnd.
6310 */
6311 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6312
6313 return SCTP_IERROR_NO_ERROR;
6314}