blob: ac6aaa0465293ccf103113d090bf4014a125b8b9 [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,
Xin Longbfc6f822017-08-05 20:00:04 +080083 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 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,
Xin Longbfc6f822017-08-05 20:00:04 +080089 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 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,
Xin Longbfc6f822017-08-05 20:00:04 +080095 const union sctp_subtype type,
Vlad Yasevichece25df2007-09-07 16:30:54 -040096 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,
Xin Longbfc6f822017-08-05 20:00:04 +0800119 const union sctp_subtype type,
Adrian Bunk52c1da32005-06-23 22:05:33 -0700120 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,
Xin Longbfc6f822017-08-05 20:00:04 +0800127 const union sctp_subtype 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,
Xin Longbfc6f822017-08-05 20:00:04 +0800135 const union sctp_subtype type,
Wei Yongjunaecedea2007-08-02 16:57:44 +0800136 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,
Xin Longbfc6f822017-08-05 20:00:04 +0800143 const union sctp_subtype type,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400144 void *arg,
145 sctp_cmd_seq_t *commands);
146
Xin Long4785c7a2017-08-05 19:59:58 +0800147static enum sctp_ierror sctp_sf_authenticate(
148 struct net *net,
149 const struct sctp_endpoint *ep,
150 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +0800151 const union sctp_subtype type,
Xin Long4785c7a2017-08-05 19:59:58 +0800152 struct sctp_chunk *chunk);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700153
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000154static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
155 const struct sctp_endpoint *ep,
Vlad Yasevich75205f42007-12-20 14:12:59 -0800156 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +0800157 const union sctp_subtype type,
Vlad Yasevich75205f42007-12-20 14:12:59 -0800158 void *arg,
159 sctp_cmd_seq_t *commands);
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/* Small helper function that checks if the chunk length
162 * is of the appropriate length. The 'required_length' argument
163 * is set to be the size of a specific chunk we are testing.
Marcelo Ricardo Leitner509e7a312016-12-28 09:26:35 -0200164 * Return Values: true = Valid length
165 * false = Invalid length
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 *
167 */
Marcelo Ricardo Leitner509e7a312016-12-28 09:26:35 -0200168static inline bool
169sctp_chunk_length_valid(struct sctp_chunk *chunk, __u16 required_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
172
Daniel Borkmann26b87c72014-10-09 22:55:33 +0200173 /* Previously already marked? */
174 if (unlikely(chunk->pdiscard))
Marcelo Ricardo Leitner509e7a312016-12-28 09:26:35 -0200175 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (unlikely(chunk_length < required_length))
Marcelo Ricardo Leitner509e7a312016-12-28 09:26:35 -0200177 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Marcelo Ricardo Leitner509e7a312016-12-28 09:26:35 -0200179 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182/**********************************************************
183 * These are the state functions for handling chunk events.
184 **********************************************************/
185
186/*
187 * Process the final SHUTDOWN COMPLETE.
188 *
189 * Section: 4 (C) (diagram), 9.2
190 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
191 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
192 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
193 * should stop the T2-shutdown timer and remove all knowledge of the
194 * association (and thus the association enters the CLOSED state).
195 *
Jerome Forissier047a2422005-04-28 11:58:43 -0700196 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 * C) Rules for packet carrying SHUTDOWN COMPLETE:
198 * ...
Jerome Forissier047a2422005-04-28 11:58:43 -0700199 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
200 * if the Verification Tag field of the packet matches its own tag and
201 * the T bit is not set
202 * OR
203 * it is set to its peer's tag and the T bit is set in the Chunk
204 * Flags.
205 * Otherwise, the receiver MUST silently discard the packet
206 * and take no further action. An endpoint MUST ignore the
207 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 *
209 * Inputs
210 * (endpoint, asoc, chunk)
211 *
212 * Outputs
213 * (asoc, reply_msg, msg_up, timers, counters)
214 *
215 * The return value is the disposition of the chunk.
216 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000217sctp_disposition_t sctp_sf_do_4_C(struct net *net,
218 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +0800220 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 void *arg,
222 sctp_cmd_seq_t *commands)
223{
224 struct sctp_chunk *chunk = arg;
225 struct sctp_ulpevent *ev;
226
Vlad Yasevichece25df2007-09-07 16:30:54 -0400227 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000228 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -0400229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 /* RFC 2960 6.10 Bundling
231 *
232 * An endpoint MUST NOT bundle INIT, INIT ACK or
233 * SHUTDOWN COMPLETE with any other chunks.
234 */
235 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000236 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Vlad Yasevichece25df2007-09-07 16:30:54 -0400238 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
Xin Long922dbc52017-06-30 11:52:13 +0800239 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000240 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400241 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 /* RFC 2960 10.2 SCTP-to-ULP
244 *
245 * H) SHUTDOWN COMPLETE notification
246 *
247 * When SCTP completes the shutdown procedures (section 9.2) this
248 * notification is passed to the upper layer.
249 */
250 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700251 0, 0, 0, NULL, GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700252 if (ev)
253 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900254 SCTP_ULPEVENT(ev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
257 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
258 * not the chunk should be discarded. If the endpoint is in
259 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
260 * T2-shutdown timer and remove all knowledge of the
261 * association (and thus the association enters the CLOSED
262 * state).
263 */
264 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
265 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
266
267 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
268 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
269
270 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
271 SCTP_STATE(SCTP_STATE_CLOSED));
272
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000273 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
274 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
277
278 return SCTP_DISPOSITION_DELETE_TCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281/*
282 * Respond to a normal INIT chunk.
283 * We are the side that is being asked for an association.
284 *
285 * Section: 5.1 Normal Establishment of an Association, B
286 * B) "Z" shall respond immediately with an INIT ACK chunk. The
287 * destination IP address of the INIT ACK MUST be set to the source
288 * IP address of the INIT to which this INIT ACK is responding. In
289 * the response, besides filling in other parameters, "Z" must set the
290 * Verification Tag field to Tag_A, and also provide its own
291 * Verification Tag (Tag_Z) in the Initiate Tag field.
292 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900293 * Verification Tag: Must be 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 *
295 * Inputs
296 * (endpoint, asoc, chunk)
297 *
298 * Outputs
299 * (asoc, reply_msg, msg_up, timers, counters)
300 *
301 * The return value is the disposition of the chunk.
302 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000303sctp_disposition_t sctp_sf_do_5_1B_init(struct net *net,
304 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +0800306 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 void *arg,
308 sctp_cmd_seq_t *commands)
309{
Xin Long62e6b7e2017-07-23 09:34:28 +0800310 struct sctp_chunk *chunk = arg, *repl, *err_chunk;
311 struct sctp_unrecognized_param *unk_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 struct sctp_association *new_asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 struct sctp_packet *packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 int len;
315
316 /* 6.10 Bundling
317 * An endpoint MUST NOT bundle INIT, INIT ACK or
318 * SHUTDOWN COMPLETE with any other chunks.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900319 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 * IG Section 2.11.2
321 * Furthermore, we require that the receiver of an INIT chunk MUST
322 * enforce these rules by silently discarding an arriving packet
323 * with an INIT chunk that is bundled with other chunks.
324 */
325 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000326 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 /* If the packet is an OOTB packet which is temporarily on the
329 * control endpoint, respond with an ABORT.
330 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +0000331 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000332 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000333 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900337 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 */
339 if (chunk->sctp_hdr->vtag != 0)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000340 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 /* Make sure that the INIT chunk has a valid length.
343 * Normally, this would cause an ABORT with a Protocol Violation
344 * error, but since we don't have an association, we'll
345 * just discard the packet.
346 */
Xin Long01a992b2017-06-30 11:52:22 +0800347 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000348 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Vlad Yasevichbec96402009-07-30 18:08:28 -0400350 /* If the INIT is coming toward a closing socket, we'll send back
351 * and ABORT. Essentially, this catches the race of INIT being
352 * backloged to the socket at the same time as the user isses close().
353 * Since the socket and all its associations are going away, we
354 * can treat this OOTB
355 */
356 if (sctp_sstate(ep->base.sk, CLOSING))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000357 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Vlad Yasevichbec96402009-07-30 18:08:28 -0400358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 /* Verify the INIT chunk before processing it. */
360 err_chunk = NULL;
Vlad Yasevichb14878c2014-04-17 17:26:50 +0200361 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Xin Long01a992b2017-06-30 11:52:22 +0800362 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 &err_chunk)) {
364 /* This chunk contains fatal error. It is to be discarded.
365 * Send an ABORT, with causes if there is any.
366 */
367 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000368 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 (__u8 *)(err_chunk->chunk_hdr) +
Xin Long922dbc52017-06-30 11:52:13 +0800370 sizeof(struct sctp_chunkhdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 ntohs(err_chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +0800372 sizeof(struct sctp_chunkhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 sctp_chunk_free(err_chunk);
375
376 if (packet) {
377 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
378 SCTP_PACKET(packet));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000379 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return SCTP_DISPOSITION_CONSUME;
381 } else {
382 return SCTP_DISPOSITION_NOMEM;
383 }
384 } else {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000385 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 commands);
387 }
388 }
389
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900390 /* Grab the INIT header. */
Xin Long4ae70c02017-06-30 11:52:21 +0800391 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 /* Tag the variable length parameters. */
Xin Long4ae70c02017-06-30 11:52:21 +0800394 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
397 if (!new_asoc)
398 goto nomem;
399
Vlad Yasevich409b95a2009-11-10 08:57:34 +0000400 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
401 sctp_scope(sctp_source(chunk)),
402 GFP_ATOMIC) < 0)
403 goto nomem_init;
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 /* The call, sctp_process_init(), can fail on memory allocation. */
Wei Yongjunde6becdc02011-04-19 21:30:51 +0000406 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Xin Long01a992b2017-06-30 11:52:22 +0800407 (struct sctp_init_chunk *)chunk->chunk_hdr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 GFP_ATOMIC))
409 goto nomem_init;
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
412
413 /* If there are errors need to be reported for unknown parameters,
414 * make sure to reserve enough room in the INIT ACK for them.
415 */
416 len = 0;
417 if (err_chunk)
418 len = ntohs(err_chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +0800419 sizeof(struct sctp_chunkhdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
422 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700423 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 /* If there are errors need to be reported for unknown parameters,
426 * include them in the outgoing INIT ACK as "Unrecognized parameter"
427 * parameter.
428 */
429 if (err_chunk) {
430 /* Get the "Unrecognized parameter" parameter(s) out of the
431 * ERROR chunk generated by sctp_verify_init(). Since the
432 * error cause code for "unknown parameter" and the
433 * "Unrecognized parameter" type is the same, we can
434 * construct the parameters in INIT ACK by copying the
435 * ERROR causes over.
436 */
Xin Long62e6b7e2017-07-23 09:34:28 +0800437 unk_param = (struct sctp_unrecognized_param *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 ((__u8 *)(err_chunk->chunk_hdr) +
Xin Long922dbc52017-06-30 11:52:13 +0800439 sizeof(struct sctp_chunkhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 /* Replace the cause code with the "Unrecognized parameter"
441 * parameter type.
442 */
443 sctp_addto_chunk(repl, len, unk_param);
444 sctp_chunk_free(err_chunk);
445 }
446
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700447 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
450
451 /*
452 * Note: After sending out INIT ACK with the State Cookie parameter,
453 * "Z" MUST NOT allocate any resources, nor keep any states for the
454 * new association. Otherwise, "Z" will be vulnerable to resource
455 * attacks.
456 */
457 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
458
459 return SCTP_DISPOSITION_DELETE_TCB;
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461nomem_init:
462 sctp_association_free(new_asoc);
463nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700464 if (err_chunk)
465 sctp_chunk_free(err_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return SCTP_DISPOSITION_NOMEM;
467}
468
469/*
470 * Respond to a normal INIT ACK chunk.
471 * We are the side that is initiating the association.
472 *
473 * Section: 5.1 Normal Establishment of an Association, C
474 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
475 * timer and leave COOKIE-WAIT state. "A" shall then send the State
476 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
477 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
478 *
479 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
480 * DATA chunks, but it MUST be the first chunk in the packet and
481 * until the COOKIE ACK is returned the sender MUST NOT send any
482 * other packets to the peer.
483 *
484 * Verification Tag: 3.3.3
485 * If the value of the Initiate Tag in a received INIT ACK chunk is
486 * found to be 0, the receiver MUST treat it as an error and close the
487 * association by transmitting an ABORT.
488 *
489 * Inputs
490 * (endpoint, asoc, chunk)
491 *
492 * Outputs
493 * (asoc, reply_msg, msg_up, timers, counters)
494 *
495 * The return value is the disposition of the chunk.
496 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000497sctp_disposition_t sctp_sf_do_5_1C_ack(struct net *net,
498 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +0800500 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 void *arg,
502 sctp_cmd_seq_t *commands)
503{
504 struct sctp_chunk *chunk = arg;
Xin Long01a992b2017-06-30 11:52:22 +0800505 struct sctp_init_chunk *initchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 struct sctp_chunk *err_chunk;
507 struct sctp_packet *packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000510 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 /* 6.10 Bundling
513 * An endpoint MUST NOT bundle INIT, INIT ACK or
514 * SHUTDOWN COMPLETE with any other chunks.
515 */
516 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000517 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Vlad Yasevichece25df2007-09-07 16:30:54 -0400519 /* Make sure that the INIT-ACK chunk has a valid length */
Xin Longcb1844c2017-07-23 09:34:26 +0800520 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_initack_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000521 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400522 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 /* Grab the INIT header. */
Xin Long4ae70c02017-06-30 11:52:21 +0800524 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* Verify the INIT chunk before processing it. */
527 err_chunk = NULL;
Vlad Yasevichb14878c2014-04-17 17:26:50 +0200528 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Xin Long01a992b2017-06-30 11:52:22 +0800529 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 &err_chunk)) {
531
Xin Long2a493212017-08-03 15:42:13 +0800532 enum sctp_error error = SCTP_ERROR_NO_RESOURCE;
Vlad Yasevich853f4b52008-01-20 06:10:46 -0800533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /* This chunk contains fatal error. It is to be discarded.
Vlad Yasevichd6701192007-12-20 14:13:31 -0800535 * Send an ABORT, with causes. If there are no causes,
536 * then there wasn't enough memory. Just terminate
537 * the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 */
539 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000540 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 (__u8 *)(err_chunk->chunk_hdr) +
Xin Long922dbc52017-06-30 11:52:13 +0800542 sizeof(struct sctp_chunkhdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 ntohs(err_chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +0800544 sizeof(struct sctp_chunkhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 sctp_chunk_free(err_chunk);
547
548 if (packet) {
549 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
550 SCTP_PACKET(packet));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000551 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700552 error = SCTP_ERROR_INV_PARAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700555
556 /* SCTP-AUTH, Section 6.3:
557 * It should be noted that if the receiver wants to tear
558 * down an association in an authenticated way only, the
559 * handling of malformed packets should not result in
560 * tearing down the association.
561 *
562 * This means that if we only want to abort associations
563 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300564 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700565 * was malformed.
566 */
567 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000568 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700569
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000570 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
571 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700572 asoc, chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574
575 /* Tag the variable length parameters. Note that we never
576 * convert the parameters in an INIT chunk.
577 */
Xin Long4ae70c02017-06-30 11:52:21 +0800578 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Xin Long01a992b2017-06-30 11:52:22 +0800580 initchunk = (struct sctp_init_chunk *)chunk->chunk_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
583 SCTP_PEER_INIT(initchunk));
584
Frank Filz3f7a87d2005-06-20 13:14:57 -0700585 /* Reset init error count upon receipt of INIT-ACK. */
586 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 /* 5.1 C) "A" shall stop the T1-init timer and leave
589 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
590 * timer, and enter the COOKIE-ECHOED state.
591 */
592 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
593 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
594 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
595 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
596 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
597 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
598
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700599 /* SCTP-AUTH: genereate the assocition shared keys so that
600 * we can potentially signe the COOKIE-ECHO.
601 */
602 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /* 5.1 C) "A" shall then send the State Cookie received in the
605 * INIT ACK chunk in a COOKIE ECHO chunk, ...
606 */
607 /* If there is any errors to report, send the ERROR chunk generated
608 * for unknown parameters as well.
609 */
610 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
611 SCTP_CHUNK(err_chunk));
612
613 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
616/*
617 * Respond to a normal COOKIE ECHO chunk.
618 * We are the side that is being asked for an association.
619 *
620 * Section: 5.1 Normal Establishment of an Association, D
621 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
622 * with a COOKIE ACK chunk after building a TCB and moving to
623 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
624 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
625 * chunk MUST be the first chunk in the packet.
626 *
627 * IMPLEMENTATION NOTE: An implementation may choose to send the
628 * Communication Up notification to the SCTP user upon reception
629 * of a valid COOKIE ECHO chunk.
630 *
631 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
632 * D) Rules for packet carrying a COOKIE ECHO
633 *
634 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
635 * Initial Tag received in the INIT ACK.
636 *
637 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
638 *
639 * Inputs
640 * (endpoint, asoc, chunk)
641 *
642 * Outputs
643 * (asoc, reply_msg, msg_up, timers, counters)
644 *
645 * The return value is the disposition of the chunk.
646 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000647sctp_disposition_t sctp_sf_do_5_1D_ce(struct net *net,
648 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +0800650 const union sctp_subtype type, void *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 sctp_cmd_seq_t *commands)
652{
653 struct sctp_chunk *chunk = arg;
654 struct sctp_association *new_asoc;
Xin Long01a992b2017-06-30 11:52:22 +0800655 struct sctp_init_chunk *peer_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 struct sctp_chunk *repl;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700657 struct sctp_ulpevent *ev, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 int error = 0;
659 struct sctp_chunk *err_chk_p;
Vlad Yasevich609ee462007-08-31 03:10:59 +0900660 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 /* If the packet is an OOTB packet which is temporarily on the
663 * control endpoint, respond with an ABORT.
664 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +0000665 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000666 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000667 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 /* Make sure that the COOKIE_ECHO chunk has a valid length.
671 * In this case, we check that we have enough for at least a
672 * chunk header. More detailed verification is done
673 * in sctp_unpack_cookie().
674 */
Xin Long922dbc52017-06-30 11:52:13 +0800675 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000676 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Vlad Yasevich609ee462007-08-31 03:10:59 +0900678 /* If the endpoint is not listening or if the number of associations
679 * on the TCP-style socket exceed the max backlog, respond with an
680 * ABORT.
681 */
682 sk = ep->base.sk;
683 if (!sctp_sstate(sk, LISTENING) ||
684 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000685 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Vlad Yasevich609ee462007-08-31 03:10:59 +0900686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 /* "Decode" the chunk. We have no optional parameters so we
688 * are in good shape.
689 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900690 chunk->subh.cookie_hdr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -0700692 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +0800693 sizeof(struct sctp_chunkhdr)))
Sridhar Samudrala62b08082006-05-05 17:04:43 -0700694 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
697 * "Z" will reply with a COOKIE ACK chunk after building a TCB
698 * and moving to the ESTABLISHED state.
699 */
700 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
701 &err_chk_p);
702
703 /* FIXME:
704 * If the re-build failed, what is the proper error path
705 * from here?
706 *
707 * [We should abort the association. --piggy]
708 */
709 if (!new_asoc) {
710 /* FIXME: Several errors are possible. A bad cookie should
711 * be silently discarded, but think about logging it too.
712 */
713 switch (error) {
714 case -SCTP_IERROR_NOMEM:
715 goto nomem;
716
717 case -SCTP_IERROR_STALE_COOKIE:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000718 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 err_chk_p);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000720 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 case -SCTP_IERROR_BAD_SIG:
723 default:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000724 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700729 /* Delay state machine commands until later.
730 *
731 * Re-build the bind address for the association is done in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 * the sctp_unpack_cookie() already.
733 */
734 /* This is a brand-new association, so these are not yet side
735 * effects--it is safe to run them here.
736 */
737 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
738
Wei Yongjunde6becdc02011-04-19 21:30:51 +0000739 if (!sctp_process_init(new_asoc, chunk,
Al Viro6a1e5f32006-11-20 17:12:25 -0800740 &chunk->subh.cookie_hdr->c.peer_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 peer_init, GFP_ATOMIC))
742 goto nomem_init;
743
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700744 /* SCTP-AUTH: Now that we've populate required fields in
745 * sctp_process_init, set up the assocaition shared keys as
746 * necessary so that we can potentially authenticate the ACK
747 */
748 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
749 if (error)
750 goto nomem_init;
751
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700752 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
753 * is supposed to be authenticated and we have to do delayed
754 * authentication. We've just recreated the association using
755 * the information in the cookie and now it's much easier to
756 * do the authentication.
757 */
758 if (chunk->auth_chunk) {
759 struct sctp_chunk auth;
Xin Long4785c7a2017-08-05 19:59:58 +0800760 enum sctp_ierror ret;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700761
Daniel Borkmannec0223e2014-03-03 17:23:04 +0100762 /* Make sure that we and the peer are AUTH capable */
763 if (!net->sctp.auth_enable || !new_asoc->peer.auth_capable) {
Daniel Borkmannec0223e2014-03-03 17:23:04 +0100764 sctp_association_free(new_asoc);
765 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
766 }
767
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700768 /* set-up our fake chunk so that we can process it */
769 auth.skb = chunk->auth_chunk;
770 auth.asoc = chunk->asoc;
771 auth.sctp_hdr = chunk->sctp_hdr;
Xin Long922dbc52017-06-30 11:52:13 +0800772 auth.chunk_hdr = (struct sctp_chunkhdr *)
773 skb_push(chunk->auth_chunk,
774 sizeof(struct sctp_chunkhdr));
775 skb_pull(chunk->auth_chunk, sizeof(struct sctp_chunkhdr));
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700776 auth.transport = chunk->transport;
777
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000778 ret = sctp_sf_authenticate(net, ep, new_asoc, type, &auth);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700779 if (ret != SCTP_IERROR_NO_ERROR) {
780 sctp_association_free(new_asoc);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000781 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700782 }
783 }
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 repl = sctp_make_cookie_ack(new_asoc, chunk);
786 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700787 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 /* RFC 2960 5.1 Normal Establishment of an Association
790 *
791 * D) IMPLEMENTATION NOTE: An implementation may choose to
792 * send the Communication Up notification to the SCTP user
793 * upon reception of a valid COOKIE ECHO chunk.
794 */
795 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
796 new_asoc->c.sinit_num_ostreams,
797 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700798 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (!ev)
800 goto nomem_ev;
801
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900802 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800803 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800805 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800807 if (new_asoc->peer.adaptation_ind) {
808 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700810 if (!ai_ev)
811 goto nomem_aiev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700814 /* Add all the state machine commands now since we've created
815 * everything. This way we don't introduce memory corruptions
816 * during side-effect processing and correclty count established
817 * associations.
818 */
819 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
820 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
821 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000822 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
823 SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700824 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
825
Neil Horman9f70f462013-12-10 06:48:15 -0500826 if (new_asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700827 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
828 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
829
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700830 /* This will send the COOKIE ACK */
831 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
832
833 /* Queue the ASSOC_CHANGE event */
834 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
835
836 /* Send up the Adaptation Layer Indication event */
837 if (ai_ev)
838 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
839 SCTP_ULPEVENT(ai_ev));
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return SCTP_DISPOSITION_CONSUME;
842
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700843nomem_aiev:
844 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845nomem_ev:
846 sctp_chunk_free(repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847nomem_init:
848 sctp_association_free(new_asoc);
849nomem:
850 return SCTP_DISPOSITION_NOMEM;
851}
852
853/*
854 * Respond to a normal COOKIE ACK chunk.
Marcelo Ricardo Leitnerb52effd2015-07-17 13:50:21 -0300855 * We are the side that is asking for an association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 *
857 * RFC 2960 5.1 Normal Establishment of an Association
858 *
859 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
860 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
861 * timer. It may also notify its ULP about the successful
862 * establishment of the association with a Communication Up
863 * notification (see Section 10).
864 *
865 * Verification Tag:
866 * Inputs
867 * (endpoint, asoc, chunk)
868 *
869 * Outputs
870 * (asoc, reply_msg, msg_up, timers, counters)
871 *
872 * The return value is the disposition of the chunk.
873 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000874sctp_disposition_t sctp_sf_do_5_1E_ca(struct net *net,
875 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +0800877 const union sctp_subtype type, void *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 sctp_cmd_seq_t *commands)
879{
880 struct sctp_chunk *chunk = arg;
881 struct sctp_ulpevent *ev;
882
883 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000884 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 /* Verify that the chunk length for the COOKIE-ACK is OK.
887 * If we don't do this, any bundled chunks may be junked.
888 */
Xin Long922dbc52017-06-30 11:52:13 +0800889 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000890 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 commands);
892
893 /* Reset init error count upon receipt of COOKIE-ACK,
894 * to avoid problems with the managemement of this
895 * counter in stale cookie situations when a transition back
896 * from the COOKIE-ECHOED state to the COOKIE-WAIT
897 * state is performed.
898 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700899 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 /* RFC 2960 5.1 Normal Establishment of an Association
902 *
903 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
904 * from the COOKIE-ECHOED state to the ESTABLISHED state,
905 * stopping the T1-cookie timer.
906 */
907 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
908 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
909 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
910 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000911 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
912 SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
Neil Horman9f70f462013-12-10 06:48:15 -0500914 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
916 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 /* It may also notify its ULP about the successful
919 * establishment of the association with a Communication Up
920 * notification (see Section 10).
921 */
922 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
923 0, asoc->c.sinit_num_ostreams,
924 asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700925 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 if (!ev)
928 goto nomem;
929
930 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
931
932 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800933 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800935 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800937 if (asoc->peer.adaptation_ind) {
938 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (!ev)
940 goto nomem;
941
942 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
943 SCTP_ULPEVENT(ev));
944 }
945
946 return SCTP_DISPOSITION_CONSUME;
947nomem:
948 return SCTP_DISPOSITION_NOMEM;
949}
950
951/* Generate and sendout a heartbeat packet. */
952static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
953 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +0800954 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 void *arg,
956 sctp_cmd_seq_t *commands)
957{
958 struct sctp_transport *transport = (struct sctp_transport *) arg;
959 struct sctp_chunk *reply;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 /* Send a heartbeat to our peer. */
Wei Yongjun92c73af2011-04-19 21:31:47 +0000962 reply = sctp_make_heartbeat(asoc, transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (!reply)
964 return SCTP_DISPOSITION_NOMEM;
965
966 /* Set rto_pending indicating that an RTT measurement
967 * is started with this heartbeat chunk.
968 */
969 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
970 SCTP_TRANSPORT(transport));
971
972 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
973 return SCTP_DISPOSITION_CONSUME;
974}
975
976/* Generate a HEARTBEAT packet on the given transport. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000977sctp_disposition_t sctp_sf_sendbeat_8_3(struct net *net,
978 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +0800980 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 void *arg,
982 sctp_cmd_seq_t *commands)
983{
984 struct sctp_transport *transport = (struct sctp_transport *) arg;
985
Vlad Yasevichb9f84782009-08-26 09:36:25 -0400986 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700987 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
988 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
990 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -0800991 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000992 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
993 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return SCTP_DISPOSITION_DELETE_TCB;
995 }
996
997 /* Section 3.3.5.
998 * The Sender-specific Heartbeat Info field should normally include
999 * information about the sender's current time when this HEARTBEAT
1000 * chunk is sent and the destination transport address to which this
1001 * HEARTBEAT is sent (see Section 8.3).
1002 */
1003
Frank Filz52ccb8e2005-12-22 11:36:46 -08001004 if (transport->param_flags & SPP_HB_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (SCTP_DISPOSITION_NOMEM ==
1006 sctp_sf_heartbeat(ep, asoc, type, arg,
1007 commands))
1008 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevich245cba72009-11-23 15:53:58 -05001009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 /* Set transport error counter and association error counter
1011 * when sending heartbeat.
1012 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00001013 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 SCTP_TRANSPORT(transport));
1015 }
Vlad Yasevich245cba72009-11-23 15:53:58 -05001016 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1017 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1019 SCTP_TRANSPORT(transport));
1020
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001021 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
Xin Long7b9438d2017-01-18 00:44:43 +08001024/* resend asoc strreset_chunk. */
1025sctp_disposition_t sctp_sf_send_reconf(struct net *net,
1026 const struct sctp_endpoint *ep,
1027 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08001028 const union sctp_subtype type, void *arg,
Xin Long7b9438d2017-01-18 00:44:43 +08001029 sctp_cmd_seq_t *commands)
1030{
1031 struct sctp_transport *transport = arg;
1032
1033 if (asoc->overall_error_count >= asoc->max_retrans) {
1034 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
1035 SCTP_ERROR(ETIMEDOUT));
1036 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
1037 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
1038 SCTP_PERR(SCTP_ERROR_NO_ERROR));
1039 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
1040 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
1041 return SCTP_DISPOSITION_DELETE_TCB;
1042 }
1043
1044 sctp_chunk_hold(asoc->strreset_chunk);
1045 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1046 SCTP_CHUNK(asoc->strreset_chunk));
1047 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
1048
1049 return SCTP_DISPOSITION_CONSUME;
1050}
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052/*
1053 * Process an heartbeat request.
1054 *
1055 * Section: 8.3 Path Heartbeat
1056 * The receiver of the HEARTBEAT should immediately respond with a
1057 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1058 * from the received HEARTBEAT chunk.
1059 *
1060 * Verification Tag: 8.5 Verification Tag [Normal verification]
1061 * When receiving an SCTP packet, the endpoint MUST ensure that the
1062 * value in the Verification Tag field of the received SCTP packet
1063 * matches its own Tag. If the received Verification Tag value does not
1064 * match the receiver's own tag value, the receiver shall silently
1065 * discard the packet and shall not process it any further except for
1066 * those cases listed in Section 8.5.1 below.
1067 *
1068 * Inputs
1069 * (endpoint, asoc, chunk)
1070 *
1071 * Outputs
1072 * (asoc, reply_msg, msg_up, timers, counters)
1073 *
1074 * The return value is the disposition of the chunk.
1075 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001076sctp_disposition_t sctp_sf_beat_8_3(struct net *net,
1077 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08001079 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 void *arg,
1081 sctp_cmd_seq_t *commands)
1082{
Xin Long3c918702017-06-30 11:52:16 +08001083 struct sctp_paramhdr *param_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 struct sctp_chunk *chunk = arg;
1085 struct sctp_chunk *reply;
1086 size_t paylen = 0;
1087
1088 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001089 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 /* Make sure that the HEARTBEAT chunk has a valid length. */
Xin Long38c00f72017-07-23 09:34:35 +08001092 if (!sctp_chunk_length_valid(chunk,
1093 sizeof(struct sctp_heartbeat_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001094 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 commands);
1096
1097 /* 8.3 The receiver of the HEARTBEAT should immediately
1098 * respond with a HEARTBEAT ACK that contains the Heartbeat
1099 * Information field copied from the received HEARTBEAT chunk.
1100 */
Xin Long4d2dcdf2017-07-23 09:34:34 +08001101 chunk->subh.hb_hdr = (struct sctp_heartbeathdr *)chunk->skb->data;
Xin Long3c918702017-06-30 11:52:16 +08001102 param_hdr = (struct sctp_paramhdr *)chunk->subh.hb_hdr;
Xin Long922dbc52017-06-30 11:52:13 +08001103 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(struct sctp_chunkhdr);
Thomas Graf06a31e22012-11-30 02:16:27 +00001104
1105 if (ntohs(param_hdr->length) > paylen)
1106 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
1107 param_hdr, commands);
1108
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001109 if (!pskb_pull(chunk->skb, paylen))
1110 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Thomas Graf06a31e22012-11-30 02:16:27 +00001112 reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (!reply)
1114 goto nomem;
1115
1116 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1117 return SCTP_DISPOSITION_CONSUME;
1118
1119nomem:
1120 return SCTP_DISPOSITION_NOMEM;
1121}
1122
1123/*
1124 * Process the returning HEARTBEAT ACK.
1125 *
1126 * Section: 8.3 Path Heartbeat
1127 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1128 * should clear the error counter of the destination transport
1129 * address to which the HEARTBEAT was sent, and mark the destination
1130 * transport address as active if it is not so marked. The endpoint may
1131 * optionally report to the upper layer when an inactive destination
1132 * address is marked as active due to the reception of the latest
1133 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1134 * clear the association overall error count as well (as defined
1135 * in section 8.1).
1136 *
1137 * The receiver of the HEARTBEAT ACK should also perform an RTT
1138 * measurement for that destination transport address using the time
1139 * value carried in the HEARTBEAT ACK chunk.
1140 *
1141 * Verification Tag: 8.5 Verification Tag [Normal verification]
1142 *
1143 * Inputs
1144 * (endpoint, asoc, chunk)
1145 *
1146 * Outputs
1147 * (asoc, reply_msg, msg_up, timers, counters)
1148 *
1149 * The return value is the disposition of the chunk.
1150 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001151sctp_disposition_t sctp_sf_backbeat_8_3(struct net *net,
1152 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08001154 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 void *arg,
1156 sctp_cmd_seq_t *commands)
1157{
1158 struct sctp_chunk *chunk = arg;
1159 union sctp_addr from_addr;
1160 struct sctp_transport *link;
1161 sctp_sender_hb_info_t *hbinfo;
1162 unsigned long max_interval;
1163
1164 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001165 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
Xin Long922dbc52017-06-30 11:52:13 +08001168 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr) +
Wei Yongjundadb50c2009-08-22 11:27:37 +08001169 sizeof(sctp_sender_hb_info_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001170 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 commands);
1172
1173 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
Vladislav Yasevicha6012662006-05-19 14:25:53 -07001174 /* Make sure that the length of the parameter is what we expect */
1175 if (ntohs(hbinfo->param_hdr.length) !=
1176 sizeof(sctp_sender_hb_info_t)) {
1177 return SCTP_DISPOSITION_DISCARD;
1178 }
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 from_addr = hbinfo->daddr;
Al Viro63de08f2006-11-20 17:07:25 -08001181 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 /* This should never happen, but lets log it if so. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001184 if (unlikely(!link)) {
1185 if (from_addr.sa.sa_family == AF_INET6) {
Joe Perchese87cc472012-05-13 21:56:26 +00001186 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1187 __func__,
1188 asoc,
1189 &from_addr.v6.sin6_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001190 } else {
Joe Perchese87cc472012-05-13 21:56:26 +00001191 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1192 __func__,
1193 asoc,
1194 &from_addr.v4.sin_addr.s_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return SCTP_DISPOSITION_DISCARD;
1197 }
1198
Sridhar Samudralaad8fec12006-07-21 14:48:50 -07001199 /* Validate the 64-bit random nonce. */
1200 if (hbinfo->hb_nonce != link->hb_nonce)
1201 return SCTP_DISPOSITION_DISCARD;
1202
Frank Filz52ccb8e2005-12-22 11:36:46 -08001203 max_interval = link->hbinterval + link->rto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 /* Check if the timestamp looks valid. */
1206 if (time_after(hbinfo->sent_at, jiffies) ||
1207 time_after(jiffies, hbinfo->sent_at + max_interval)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001208 pr_debug("%s: HEARTBEAT ACK with invalid timestamp received "
1209 "for transport:%p\n", __func__, link);
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return SCTP_DISPOSITION_DISCARD;
1212 }
1213
1214 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1215 * the HEARTBEAT should clear the error counter of the
1216 * destination transport address to which the HEARTBEAT was
1217 * sent and mark the destination transport address as active if
1218 * it is not so marked.
1219 */
1220 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1221
1222 return SCTP_DISPOSITION_CONSUME;
1223}
1224
1225/* Helper function to send out an abort for the restart
1226 * condition.
1227 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001228static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 struct sctp_chunk *init,
1230 sctp_cmd_seq_t *commands)
1231{
1232 int len;
1233 struct sctp_packet *pkt;
1234 union sctp_addr_param *addrparm;
1235 struct sctp_errhdr *errhdr;
1236 struct sctp_endpoint *ep;
Xin Longd8238d92017-08-03 15:42:11 +08001237 char buffer[sizeof(*errhdr) + sizeof(*addrparm)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1239
1240 /* Build the error on the stack. We are way to malloc crazy
1241 * throughout the code today.
1242 */
1243 errhdr = (struct sctp_errhdr *)buffer;
1244 addrparm = (union sctp_addr_param *)errhdr->variable;
1245
1246 /* Copy into a parm format. */
1247 len = af->to_addr_param(ssa, addrparm);
Xin Longd8238d92017-08-03 15:42:11 +08001248 len += sizeof(*errhdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 errhdr->cause = SCTP_ERROR_RESTART;
1251 errhdr->length = htons(len);
1252
1253 /* Assign to the control socket. */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001254 ep = sctp_sk(net->sctp.ctl_sock)->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 /* Association is NULL since this may be a restart attack and we
1257 * want to send back the attacker's vtag.
1258 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001259 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 if (!pkt)
1262 goto out;
1263 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1264
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00001265 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 /* Discard the rest of the inbound packet. */
1268 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1269
1270out:
1271 /* Even if there is no memory, treat as a failure so
1272 * the packet will get dropped.
1273 */
1274 return 0;
1275}
1276
Joe Perches123031c2010-09-08 11:04:21 +00001277static bool list_has_sctp_addr(const struct list_head *list,
1278 union sctp_addr *ipaddr)
1279{
1280 struct sctp_transport *addr;
1281
1282 list_for_each_entry(addr, list, transports) {
1283 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1284 return true;
1285 }
1286
1287 return false;
1288}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289/* A restart is occurring, check to make sure no new addresses
1290 * are being added as we may be under a takeover attack.
1291 */
1292static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1293 const struct sctp_association *asoc,
1294 struct sctp_chunk *init,
1295 sctp_cmd_seq_t *commands)
1296{
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001297 struct net *net = sock_net(new_asoc->base.sk);
Joe Perches123031c2010-09-08 11:04:21 +00001298 struct sctp_transport *new_addr;
1299 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Joe Perches123031c2010-09-08 11:04:21 +00001301 /* Implementor's Guide - Section 5.2.2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 * ...
1303 * Before responding the endpoint MUST check to see if the
1304 * unexpected INIT adds new addresses to the association. If new
1305 * addresses are added to the association, the endpoint MUST respond
1306 * with an ABORT..
1307 */
1308
1309 /* Search through all current addresses and make sure
1310 * we aren't adding any new ones.
1311 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001312 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
Joe Perches123031c2010-09-08 11:04:21 +00001313 transports) {
1314 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1315 &new_addr->ipaddr)) {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001316 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
Joe Perches123031c2010-09-08 11:04:21 +00001317 commands);
1318 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 break;
Joe Perches123031c2010-09-08 11:04:21 +00001320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
1322
1323 /* Return success if all addresses were found. */
Joe Perches123031c2010-09-08 11:04:21 +00001324 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
1327/* Populate the verification/tie tags based on overlapping INIT
1328 * scenario.
1329 *
1330 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1331 */
1332static void sctp_tietags_populate(struct sctp_association *new_asoc,
1333 const struct sctp_association *asoc)
1334{
1335 switch (asoc->state) {
1336
1337 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1338
1339 case SCTP_STATE_COOKIE_WAIT:
1340 new_asoc->c.my_vtag = asoc->c.my_vtag;
1341 new_asoc->c.my_ttag = asoc->c.my_vtag;
1342 new_asoc->c.peer_ttag = 0;
1343 break;
1344
1345 case SCTP_STATE_COOKIE_ECHOED:
1346 new_asoc->c.my_vtag = asoc->c.my_vtag;
1347 new_asoc->c.my_ttag = asoc->c.my_vtag;
1348 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1349 break;
1350
1351 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1352 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1353 */
1354 default:
1355 new_asoc->c.my_ttag = asoc->c.my_vtag;
1356 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1357 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 /* Other parameters for the endpoint SHOULD be copied from the
1361 * existing parameters of the association (e.g. number of
1362 * outbound streams) into the INIT ACK and cookie.
1363 */
1364 new_asoc->rwnd = asoc->rwnd;
1365 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1366 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1367 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1368}
1369
1370/*
1371 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1372 * handling action.
1373 *
1374 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1375 *
1376 * Returns value representing action to be taken. These action values
1377 * correspond to Action/Description values in RFC 2960, Table 2.
1378 */
1379static char sctp_tietags_compare(struct sctp_association *new_asoc,
1380 const struct sctp_association *asoc)
1381{
1382 /* In this case, the peer may have restarted. */
1383 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1384 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1385 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1386 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1387 return 'A';
1388
1389 /* Collision case B. */
1390 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1391 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1392 (0 == asoc->c.peer_vtag))) {
1393 return 'B';
1394 }
1395
1396 /* Collision case D. */
1397 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1398 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1399 return 'D';
1400
1401 /* Collision case C. */
1402 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1403 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1404 (0 == new_asoc->c.my_ttag) &&
1405 (0 == new_asoc->c.peer_ttag))
1406 return 'C';
1407
1408 /* No match to any of the special cases; discard this packet. */
1409 return 'E';
1410}
1411
1412/* Common helper routine for both duplicate and simulataneous INIT
1413 * chunk handling.
1414 */
1415static sctp_disposition_t sctp_sf_do_unexpected_init(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001416 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 const struct sctp_endpoint *ep,
1418 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08001419 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 void *arg, sctp_cmd_seq_t *commands)
1421{
Xin Long62e6b7e2017-07-23 09:34:28 +08001422 struct sctp_chunk *chunk = arg, *repl, *err_chunk;
1423 struct sctp_unrecognized_param *unk_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 struct sctp_association *new_asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 struct sctp_packet *packet;
Xin Long62e6b7e2017-07-23 09:34:28 +08001426 sctp_disposition_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 int len;
1428
1429 /* 6.10 Bundling
1430 * An endpoint MUST NOT bundle INIT, INIT ACK or
1431 * SHUTDOWN COMPLETE with any other chunks.
1432 *
1433 * IG Section 2.11.2
1434 * Furthermore, we require that the receiver of an INIT chunk MUST
1435 * enforce these rules by silently discarding an arriving packet
1436 * with an INIT chunk that is bundled with other chunks.
1437 */
1438 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001439 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001442 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 */
1444 if (chunk->sctp_hdr->vtag != 0)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001445 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 /* Make sure that the INIT chunk has a valid length.
1448 * In this case, we generate a protocol violation since we have
1449 * an association established.
1450 */
Xin Long01a992b2017-06-30 11:52:22 +08001451 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001452 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 commands);
1454 /* Grab the INIT header. */
Xin Long4ae70c02017-06-30 11:52:21 +08001455 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 /* Tag the variable length parameters. */
Xin Long4ae70c02017-06-30 11:52:21 +08001458 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 /* Verify the INIT chunk before processing it. */
1461 err_chunk = NULL;
Vlad Yasevichb14878c2014-04-17 17:26:50 +02001462 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Xin Long01a992b2017-06-30 11:52:22 +08001463 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 &err_chunk)) {
1465 /* This chunk contains fatal error. It is to be discarded.
1466 * Send an ABORT, with causes if there is any.
1467 */
1468 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001469 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 (__u8 *)(err_chunk->chunk_hdr) +
Xin Long922dbc52017-06-30 11:52:13 +08001471 sizeof(struct sctp_chunkhdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 ntohs(err_chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +08001473 sizeof(struct sctp_chunkhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 if (packet) {
1476 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1477 SCTP_PACKET(packet));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001478 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 retval = SCTP_DISPOSITION_CONSUME;
1480 } else {
1481 retval = SCTP_DISPOSITION_NOMEM;
1482 }
1483 goto cleanup;
1484 } else {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001485 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 commands);
1487 }
1488 }
1489
1490 /*
1491 * Other parameters for the endpoint SHOULD be copied from the
1492 * existing parameters of the association (e.g. number of
1493 * outbound streams) into the INIT ACK and cookie.
1494 * FIXME: We are copying parameters from the endpoint not the
1495 * association.
1496 */
1497 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1498 if (!new_asoc)
1499 goto nomem;
1500
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001501 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1502 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1503 goto nomem;
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 /* In the outbound INIT ACK the endpoint MUST copy its current
1506 * Verification Tag and Peers Verification tag into a reserved
1507 * place (local tie-tag and per tie-tag) within the state cookie.
1508 */
Wei Yongjunde6becdc02011-04-19 21:30:51 +00001509 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Xin Long01a992b2017-06-30 11:52:22 +08001510 (struct sctp_init_chunk *)chunk->chunk_hdr,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001511 GFP_ATOMIC))
1512 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 /* Make sure no new addresses are being added during the
1515 * restart. Do not do this check for COOKIE-WAIT state,
1516 * since there are no peer addresses to check against.
1517 * Upon return an ABORT will have been sent if needed.
1518 */
1519 if (!sctp_state(asoc, COOKIE_WAIT)) {
1520 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1521 commands)) {
1522 retval = SCTP_DISPOSITION_CONSUME;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001523 goto nomem_retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 }
1525 }
1526
1527 sctp_tietags_populate(new_asoc, asoc);
1528
1529 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1530
1531 /* If there are errors need to be reported for unknown parameters,
1532 * make sure to reserve enough room in the INIT ACK for them.
1533 */
1534 len = 0;
1535 if (err_chunk) {
1536 len = ntohs(err_chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +08001537 sizeof(struct sctp_chunkhdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1541 if (!repl)
1542 goto nomem;
1543
1544 /* If there are errors need to be reported for unknown parameters,
1545 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1546 * parameter.
1547 */
1548 if (err_chunk) {
1549 /* Get the "Unrecognized parameter" parameter(s) out of the
1550 * ERROR chunk generated by sctp_verify_init(). Since the
1551 * error cause code for "unknown parameter" and the
1552 * "Unrecognized parameter" type is the same, we can
1553 * construct the parameters in INIT ACK by copying the
1554 * ERROR causes over.
1555 */
Xin Long62e6b7e2017-07-23 09:34:28 +08001556 unk_param = (struct sctp_unrecognized_param *)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 ((__u8 *)(err_chunk->chunk_hdr) +
Xin Long922dbc52017-06-30 11:52:13 +08001558 sizeof(struct sctp_chunkhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 /* Replace the cause code with the "Unrecognized parameter"
1560 * parameter type.
1561 */
1562 sctp_addto_chunk(repl, len, unk_param);
1563 }
1564
1565 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1566 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1567
1568 /*
1569 * Note: After sending out INIT ACK with the State Cookie parameter,
1570 * "Z" MUST NOT allocate any resources for this new association.
1571 * Otherwise, "Z" will be vulnerable to resource attacks.
1572 */
1573 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1574 retval = SCTP_DISPOSITION_CONSUME;
1575
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001576 return retval;
1577
1578nomem:
1579 retval = SCTP_DISPOSITION_NOMEM;
1580nomem_retval:
1581 if (new_asoc)
1582 sctp_association_free(new_asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583cleanup:
1584 if (err_chunk)
1585 sctp_chunk_free(err_chunk);
1586 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
1588
1589/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001590 * Handle simultaneous INIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 * This means we started an INIT and then we got an INIT request from
1592 * our peer.
1593 *
1594 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1595 * This usually indicates an initialization collision, i.e., each
1596 * endpoint is attempting, at about the same time, to establish an
1597 * association with the other endpoint.
1598 *
1599 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1600 * endpoint MUST respond with an INIT ACK using the same parameters it
1601 * sent in its original INIT chunk (including its Verification Tag,
1602 * unchanged). These original parameters are combined with those from the
1603 * newly received INIT chunk. The endpoint shall also generate a State
1604 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1605 * INIT to calculate the State Cookie.
1606 *
1607 * After that, the endpoint MUST NOT change its state, the T1-init
1608 * timer shall be left running and the corresponding TCB MUST NOT be
1609 * destroyed. The normal procedures for handling State Cookies when
1610 * a TCB exists will resolve the duplicate INITs to a single association.
1611 *
1612 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1613 * its Tie-Tags with the Tag information of itself and its peer (see
1614 * section 5.2.2 for a description of the Tie-Tags).
1615 *
1616 * Verification Tag: Not explicit, but an INIT can not have a valid
1617 * verification tag, so we skip the check.
1618 *
1619 * Inputs
1620 * (endpoint, asoc, chunk)
1621 *
1622 * Outputs
1623 * (asoc, reply_msg, msg_up, timers, counters)
1624 *
1625 * The return value is the disposition of the chunk.
1626 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001627sctp_disposition_t sctp_sf_do_5_2_1_siminit(struct net *net,
1628 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08001630 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 void *arg,
1632 sctp_cmd_seq_t *commands)
1633{
1634 /* Call helper to do the real work for both simulataneous and
1635 * duplicate INIT chunk handling.
1636 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001637 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
1640/*
1641 * Handle duplicated INIT messages. These are usually delayed
1642 * restransmissions.
1643 *
1644 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1645 * COOKIE-ECHOED and COOKIE-WAIT
1646 *
1647 * Unless otherwise stated, upon reception of an unexpected INIT for
1648 * this association, the endpoint shall generate an INIT ACK with a
1649 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1650 * current Verification Tag and peer's Verification Tag into a reserved
1651 * place within the state cookie. We shall refer to these locations as
1652 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1653 * containing this INIT ACK MUST carry a Verification Tag value equal to
1654 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1655 * MUST contain a new Initiation Tag (randomly generated see Section
1656 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1657 * existing parameters of the association (e.g. number of outbound
1658 * streams) into the INIT ACK and cookie.
1659 *
1660 * After sending out the INIT ACK, the endpoint shall take no further
1661 * actions, i.e., the existing association, including its current state,
1662 * and the corresponding TCB MUST NOT be changed.
1663 *
1664 * Note: Only when a TCB exists and the association is not in a COOKIE-
1665 * WAIT state are the Tie-Tags populated. For a normal association INIT
1666 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1667 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1668 * State Cookie are populated as specified in section 5.2.1.
1669 *
1670 * Verification Tag: Not specified, but an INIT has no way of knowing
1671 * what the verification tag could be, so we ignore it.
1672 *
1673 * Inputs
1674 * (endpoint, asoc, chunk)
1675 *
1676 * Outputs
1677 * (asoc, reply_msg, msg_up, timers, counters)
1678 *
1679 * The return value is the disposition of the chunk.
1680 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001681sctp_disposition_t sctp_sf_do_5_2_2_dupinit(struct net *net,
1682 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08001684 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 void *arg,
1686 sctp_cmd_seq_t *commands)
1687{
1688 /* Call helper to do the real work for both simulataneous and
1689 * duplicate INIT chunk handling.
1690 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001691 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692}
1693
1694
Vlad Yasevich610ab732007-01-15 19:18:30 -08001695/*
1696 * Unexpected INIT-ACK handler.
1697 *
1698 * Section 5.2.3
1699 * If an INIT ACK received by an endpoint in any state other than the
1700 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1701 * An unexpected INIT ACK usually indicates the processing of an old or
1702 * duplicated INIT chunk.
1703*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001704sctp_disposition_t sctp_sf_do_5_2_3_initack(struct net *net,
1705 const struct sctp_endpoint *ep,
Vlad Yasevich610ab732007-01-15 19:18:30 -08001706 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08001707 const union sctp_subtype type,
Vlad Yasevich610ab732007-01-15 19:18:30 -08001708 void *arg, sctp_cmd_seq_t *commands)
1709{
1710 /* Per the above section, we'll discard the chunk if we have an
1711 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1712 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001713 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001714 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001715 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001716 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001717}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719/* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1720 *
1721 * Section 5.2.4
1722 * A) In this case, the peer may have restarted.
1723 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001724static sctp_disposition_t sctp_sf_do_dupcook_a(struct net *net,
1725 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 const struct sctp_association *asoc,
1727 struct sctp_chunk *chunk,
1728 sctp_cmd_seq_t *commands,
1729 struct sctp_association *new_asoc)
1730{
Xin Long01a992b2017-06-30 11:52:22 +08001731 struct sctp_init_chunk *peer_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 struct sctp_ulpevent *ev;
1733 struct sctp_chunk *repl;
1734 struct sctp_chunk *err;
1735 sctp_disposition_t disposition;
1736
1737 /* new_asoc is a brand-new association, so these are not yet
1738 * side effects--it is safe to run them here.
1739 */
1740 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1741
Wei Yongjunde6becdc02011-04-19 21:30:51 +00001742 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 GFP_ATOMIC))
1744 goto nomem;
1745
1746 /* Make sure no new addresses are being added during the
1747 * restart. Though this is a pretty complicated attack
1748 * since you'd have to get inside the cookie.
1749 */
1750 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1751 return SCTP_DISPOSITION_CONSUME;
1752 }
1753
1754 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1755 * the peer has restarted (Action A), it MUST NOT setup a new
1756 * association but instead resend the SHUTDOWN ACK and send an ERROR
1757 * chunk with a "Cookie Received while Shutting Down" error cause to
1758 * its peer.
1759 */
1760 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001761 disposition = sctp_sf_do_9_2_reshutack(net, ep, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1763 chunk, commands);
1764 if (SCTP_DISPOSITION_NOMEM == disposition)
1765 goto nomem;
1766
1767 err = sctp_make_op_error(asoc, chunk,
1768 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001769 NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 if (err)
1771 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1772 SCTP_CHUNK(err));
1773
1774 return SCTP_DISPOSITION_CONSUME;
1775 }
1776
Wei Yongjuna000c012011-05-29 23:23:36 +00001777 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1778 * data. Consider the optional choice of resending of this data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 */
Wei Yongjuna000c012011-05-29 23:23:36 +00001780 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1781 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1782 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1784
Wei Yongjuna000c012011-05-29 23:23:36 +00001785 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1786 * and ASCONF-ACK cache.
1787 */
1788 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1789 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1790 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 repl = sctp_make_cookie_ack(new_asoc, chunk);
1793 if (!repl)
1794 goto nomem;
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 /* Report association restart to upper layer. */
1797 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1798 new_asoc->c.sinit_num_ostreams,
1799 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07001800 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (!ev)
1802 goto nomem_ev;
1803
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001804 /* Update the content of current association. */
1805 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
Vlad Yasevichbdf6fa52014-10-03 18:16:20 -04001807 if (sctp_state(asoc, SHUTDOWN_PENDING) &&
1808 (sctp_sstate(asoc->base.sk, CLOSING) ||
1809 sock_flag(asoc->base.sk, SOCK_DEAD))) {
1810 /* if were currently in SHUTDOWN_PENDING, but the socket
1811 * has been closed by user, don't transition to ESTABLISHED.
1812 * Instead trigger SHUTDOWN bundled with COOKIE_ACK.
1813 */
1814 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1815 return sctp_sf_do_9_2_start_shutdown(net, ep, asoc,
1816 SCTP_ST_CHUNK(0), NULL,
1817 commands);
1818 } else {
1819 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1820 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1821 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 return SCTP_DISPOSITION_CONSUME;
1824
1825nomem_ev:
1826 sctp_chunk_free(repl);
1827nomem:
1828 return SCTP_DISPOSITION_NOMEM;
1829}
1830
1831/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1832 *
1833 * Section 5.2.4
1834 * B) In this case, both sides may be attempting to start an association
1835 * at about the same time but the peer endpoint started its INIT
1836 * after responding to the local endpoint's INIT
1837 */
1838/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001839static sctp_disposition_t sctp_sf_do_dupcook_b(struct net *net,
1840 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 const struct sctp_association *asoc,
1842 struct sctp_chunk *chunk,
1843 sctp_cmd_seq_t *commands,
1844 struct sctp_association *new_asoc)
1845{
Xin Long01a992b2017-06-30 11:52:22 +08001846 struct sctp_init_chunk *peer_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 struct sctp_chunk *repl;
1848
1849 /* new_asoc is a brand-new association, so these are not yet
1850 * side effects--it is safe to run them here.
1851 */
1852 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
Wei Yongjunde6becdc02011-04-19 21:30:51 +00001853 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 GFP_ATOMIC))
1855 goto nomem;
1856
1857 /* Update the content of current association. */
1858 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1859 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1860 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001861 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1863
1864 repl = sctp_make_cookie_ack(new_asoc, chunk);
1865 if (!repl)
1866 goto nomem;
1867
1868 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
1870 /* RFC 2960 5.1 Normal Establishment of an Association
1871 *
1872 * D) IMPLEMENTATION NOTE: An implementation may choose to
1873 * send the Communication Up notification to the SCTP user
1874 * upon reception of a valid COOKIE ECHO chunk.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001875 *
1876 * Sadly, this needs to be implemented as a side-effect, because
1877 * we are not guaranteed to have set the association id of the real
1878 * association and so these notifications need to be delayed until
1879 * the association id is allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Vlad Yasevich07d93962007-05-04 13:55:27 -07001882 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
1884 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001885 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001887 * peers requested adaptation layer.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001888 *
1889 * This also needs to be done as a side effect for the same reason as
1890 * above.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 */
Vlad Yasevich07d93962007-05-04 13:55:27 -07001892 if (asoc->peer.adaptation_ind)
1893 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
1895 return SCTP_DISPOSITION_CONSUME;
1896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897nomem:
1898 return SCTP_DISPOSITION_NOMEM;
1899}
1900
1901/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1902 *
1903 * Section 5.2.4
1904 * C) In this case, the local endpoint's cookie has arrived late.
1905 * Before it arrived, the local endpoint sent an INIT and received an
1906 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1907 * but a new tag of its own.
1908 */
1909/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001910static sctp_disposition_t sctp_sf_do_dupcook_c(struct net *net,
1911 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 const struct sctp_association *asoc,
1913 struct sctp_chunk *chunk,
1914 sctp_cmd_seq_t *commands,
1915 struct sctp_association *new_asoc)
1916{
1917 /* The cookie should be silently discarded.
1918 * The endpoint SHOULD NOT change states and should leave
1919 * any timers running.
1920 */
1921 return SCTP_DISPOSITION_DISCARD;
1922}
1923
1924/* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1925 *
1926 * Section 5.2.4
1927 *
1928 * D) When both local and remote tags match the endpoint should always
1929 * enter the ESTABLISHED state, if it has not already done so.
1930 */
1931/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001932static sctp_disposition_t sctp_sf_do_dupcook_d(struct net *net,
1933 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 const struct sctp_association *asoc,
1935 struct sctp_chunk *chunk,
1936 sctp_cmd_seq_t *commands,
1937 struct sctp_association *new_asoc)
1938{
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001939 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 struct sctp_chunk *repl;
1941
1942 /* Clarification from Implementor's Guide:
1943 * D) When both local and remote tags match the endpoint should
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001944 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1945 * It should stop any cookie timer that may be running and send
1946 * a COOKIE ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 */
1948
1949 /* Don't accidentally move back into established state. */
1950 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1951 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1952 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1953 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1954 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001955 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1957 SCTP_NULL());
1958
1959 /* RFC 2960 5.1 Normal Establishment of an Association
1960 *
1961 * D) IMPLEMENTATION NOTE: An implementation may choose
1962 * to send the Communication Up notification to the
1963 * SCTP user upon reception of a valid COOKIE
1964 * ECHO chunk.
1965 */
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001966 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 SCTP_COMM_UP, 0,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001968 asoc->c.sinit_num_ostreams,
1969 asoc->c.sinit_max_instreams,
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09001970 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 if (!ev)
1972 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
1974 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001975 * When a peer sends a Adaptation Layer Indication parameter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 * SCTP delivers this notification to inform the application
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001977 * that of the peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001979 if (asoc->peer.adaptation_ind) {
1980 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001982 if (!ai_ev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 goto nomem;
1984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 }
1986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
1988 repl = sctp_make_cookie_ack(new_asoc, chunk);
1989 if (!repl)
1990 goto nomem;
1991
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07001992 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1993
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001994 if (ev)
1995 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1996 SCTP_ULPEVENT(ev));
1997 if (ai_ev)
1998 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1999 SCTP_ULPEVENT(ai_ev));
2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 return SCTP_DISPOSITION_CONSUME;
2002
2003nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07002004 if (ai_ev)
2005 sctp_ulpevent_free(ai_ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 if (ev)
2007 sctp_ulpevent_free(ev);
2008 return SCTP_DISPOSITION_NOMEM;
2009}
2010
2011/*
2012 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
2013 * chunk was retransmitted and then delayed in the network.
2014 *
2015 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
2016 *
2017 * Verification Tag: None. Do cookie validation.
2018 *
2019 * Inputs
2020 * (endpoint, asoc, chunk)
2021 *
2022 * Outputs
2023 * (asoc, reply_msg, msg_up, timers, counters)
2024 *
2025 * The return value is the disposition of the chunk.
2026 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002027sctp_disposition_t sctp_sf_do_5_2_4_dupcook(struct net *net,
2028 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002030 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 void *arg,
2032 sctp_cmd_seq_t *commands)
2033{
2034 sctp_disposition_t retval;
2035 struct sctp_chunk *chunk = arg;
2036 struct sctp_association *new_asoc;
2037 int error = 0;
2038 char action;
2039 struct sctp_chunk *err_chk_p;
2040
2041 /* Make sure that the chunk has a valid length from the protocol
2042 * perspective. In this case check to make sure we have at least
2043 * enough for the chunk header. Cookie length verification is
2044 * done later.
2045 */
Xin Long922dbc52017-06-30 11:52:13 +08002046 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002047 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 commands);
2049
2050 /* "Decode" the chunk. We have no optional parameters so we
2051 * are in good shape.
2052 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002053 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -07002054 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +08002055 sizeof(struct sctp_chunkhdr)))
Sridhar Samudrala62b08082006-05-05 17:04:43 -07002056 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
2058 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2059 * of a duplicate COOKIE ECHO match the Verification Tags of the
2060 * current association, consider the State Cookie valid even if
2061 * the lifespan is exceeded.
2062 */
2063 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2064 &err_chk_p);
2065
2066 /* FIXME:
2067 * If the re-build failed, what is the proper error path
2068 * from here?
2069 *
2070 * [We should abort the association. --piggy]
2071 */
2072 if (!new_asoc) {
2073 /* FIXME: Several errors are possible. A bad cookie should
2074 * be silently discarded, but think about logging it too.
2075 */
2076 switch (error) {
2077 case -SCTP_IERROR_NOMEM:
2078 goto nomem;
2079
2080 case -SCTP_IERROR_STALE_COOKIE:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002081 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 err_chk_p);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002083 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 case -SCTP_IERROR_BAD_SIG:
2085 default:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002086 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 }
2089
Xin Long7e062972017-05-23 13:28:55 +08002090 /* Set temp so that it won't be added into hashtable */
2091 new_asoc->temp = 1;
2092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 /* Compare the tie_tag in cookie with the verification tag of
2094 * current association.
2095 */
2096 action = sctp_tietags_compare(new_asoc, asoc);
2097
2098 switch (action) {
2099 case 'A': /* Association restart. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002100 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 new_asoc);
2102 break;
2103
2104 case 'B': /* Collision case B. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002105 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 new_asoc);
2107 break;
2108
2109 case 'C': /* Collision case C. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002110 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 new_asoc);
2112 break;
2113
2114 case 'D': /* Collision case D. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002115 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 new_asoc);
2117 break;
2118
2119 default: /* Discard packet for all others. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002120 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 /* Delete the tempory new association. */
Vlad Yasevichf2815632013-03-12 15:53:23 +00002125 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2127
Max Matveevd5ccd492011-08-29 21:02:24 +00002128 /* Restore association pointer to provide SCTP command interpeter
2129 * with a valid context in case it needs to manipulate
2130 * the queues */
2131 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2132 SCTP_ASOC((struct sctp_association *)asoc));
2133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 return retval;
2135
2136nomem:
2137 return SCTP_DISPOSITION_NOMEM;
2138}
2139
2140/*
2141 * Process an ABORT. (SHUTDOWN-PENDING state)
2142 *
2143 * See sctp_sf_do_9_1_abort().
2144 */
2145sctp_disposition_t sctp_sf_shutdown_pending_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002146 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 const struct sctp_endpoint *ep,
2148 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002149 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 void *arg,
2151 sctp_cmd_seq_t *commands)
2152{
2153 struct sctp_chunk *chunk = arg;
2154
2155 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002156 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
2158 /* Make sure that the ABORT chunk has a valid length.
2159 * Since this is an ABORT chunk, we have to discard it
2160 * because of the following text:
2161 * RFC 2960, Section 3.3.7
2162 * If an endpoint receives an ABORT with a format error or for an
2163 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002164 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 * as we do not know its true length. So, to be safe, discard the
2166 * packet.
2167 */
Xin Long441ae652017-07-23 09:34:36 +08002168 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002169 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Vlad Yasevich75205f42007-12-20 14:12:59 -08002171 /* ADD-IP: Special case for ABORT chunks
2172 * F4) One special consideration is that ABORT Chunks arriving
2173 * destined to the IP address being deleted MUST be
2174 * ignored (see Section 5.3.1 for further details).
2175 */
2176 if (SCTP_ADDR_DEL ==
2177 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002178 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002179
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002180 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
2183/*
2184 * Process an ABORT. (SHUTDOWN-SENT state)
2185 *
2186 * See sctp_sf_do_9_1_abort().
2187 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002188sctp_disposition_t sctp_sf_shutdown_sent_abort(struct net *net,
2189 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002191 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 void *arg,
2193 sctp_cmd_seq_t *commands)
2194{
2195 struct sctp_chunk *chunk = arg;
2196
2197 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002198 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
2200 /* Make sure that the ABORT chunk has a valid length.
2201 * Since this is an ABORT chunk, we have to discard it
2202 * because of the following text:
2203 * RFC 2960, Section 3.3.7
2204 * If an endpoint receives an ABORT with a format error or for an
2205 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002206 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 * as we do not know its true length. So, to be safe, discard the
2208 * packet.
2209 */
Xin Long441ae652017-07-23 09:34:36 +08002210 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002211 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Vlad Yasevich75205f42007-12-20 14:12:59 -08002213 /* ADD-IP: Special case for ABORT chunks
2214 * F4) One special consideration is that ABORT Chunks arriving
2215 * destined to the IP address being deleted MUST be
2216 * ignored (see Section 5.3.1 for further details).
2217 */
2218 if (SCTP_ADDR_DEL ==
2219 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002220 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 /* Stop the T2-shutdown timer. */
2223 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2224 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2225
2226 /* Stop the T5-shutdown guard timer. */
2227 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2228 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2229
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002230 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231}
2232
2233/*
2234 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2235 *
2236 * See sctp_sf_do_9_1_abort().
2237 */
2238sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002239 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 const struct sctp_endpoint *ep,
2241 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002242 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 void *arg,
2244 sctp_cmd_seq_t *commands)
2245{
2246 /* The same T2 timer, so we should be able to use
2247 * common function with the SHUTDOWN-SENT state.
2248 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002249 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250}
2251
2252/*
2253 * Handle an Error received in COOKIE_ECHOED state.
2254 *
2255 * Only handle the error type of stale COOKIE Error, the other errors will
2256 * be ignored.
2257 *
2258 * Inputs
2259 * (endpoint, asoc, chunk)
2260 *
2261 * Outputs
2262 * (asoc, reply_msg, msg_up, timers, counters)
2263 *
2264 * The return value is the disposition of the chunk.
2265 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002266sctp_disposition_t sctp_sf_cookie_echoed_err(struct net *net,
2267 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002269 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 void *arg,
2271 sctp_cmd_seq_t *commands)
2272{
2273 struct sctp_chunk *chunk = arg;
Xin Longd8238d92017-08-03 15:42:11 +08002274 struct sctp_errhdr *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
2276 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002277 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
2279 /* Make sure that the ERROR chunk has a valid length.
2280 * The parameter walking depends on this as well.
2281 */
Xin Long87caeba2017-08-03 15:42:12 +08002282 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_operr_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002283 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 commands);
2285
2286 /* Process the error here */
2287 /* FUTURE FIXME: When PR-SCTP related and other optional
2288 * parms are emitted, this will have to change to handle multiple
2289 * errors.
2290 */
2291 sctp_walk_errors(err, chunk->chunk_hdr) {
2292 if (SCTP_ERROR_STALE_COOKIE == err->cause)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002293 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 arg, commands);
2295 }
2296
2297 /* It is possible to have malformed error causes, and that
2298 * will cause us to end the walk early. However, since
2299 * we are discarding the packet, there should be no adverse
2300 * affects.
2301 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002302 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303}
2304
2305/*
2306 * Handle a Stale COOKIE Error
2307 *
2308 * Section: 5.2.6 Handle Stale COOKIE Error
2309 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2310 * one of the following three alternatives.
2311 * ...
2312 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2313 * Preservative parameter requesting an extension to the lifetime of
2314 * the State Cookie. When calculating the time extension, an
2315 * implementation SHOULD use the RTT information measured based on the
2316 * previous COOKIE ECHO / ERROR exchange, and should add no more
2317 * than 1 second beyond the measured RTT, due to long State Cookie
2318 * lifetimes making the endpoint more subject to a replay attack.
2319 *
2320 * Verification Tag: Not explicit, but safe to ignore.
2321 *
2322 * Inputs
2323 * (endpoint, asoc, chunk)
2324 *
2325 * Outputs
2326 * (asoc, reply_msg, msg_up, timers, counters)
2327 *
2328 * The return value is the disposition of the chunk.
2329 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002330static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
2331 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002333 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 void *arg,
2335 sctp_cmd_seq_t *commands)
2336{
Frank Filz3f7a87d2005-06-20 13:14:57 -07002337 int attempts = asoc->init_err_counter + 1;
Xin Long365ddb62017-07-17 11:29:51 +08002338 struct sctp_chunk *chunk = arg, *reply;
2339 struct sctp_cookie_preserve_param bht;
2340 struct sctp_bind_addr *bp;
Xin Longd8238d92017-08-03 15:42:11 +08002341 struct sctp_errhdr *err;
Xin Long365ddb62017-07-17 11:29:51 +08002342 u32 stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Vlad Yasevich81845c22006-01-30 15:59:54 -08002344 if (attempts > asoc->max_init_attempts) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002345 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2346 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002348 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 return SCTP_DISPOSITION_DELETE_TCB;
2350 }
2351
Xin Longd8238d92017-08-03 15:42:11 +08002352 err = (struct sctp_errhdr *)(chunk->skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
2354 /* When calculating the time extension, an implementation
2355 * SHOULD use the RTT information measured based on the
2356 * previous COOKIE ECHO / ERROR exchange, and should add no
2357 * more than 1 second beyond the measured RTT, due to long
2358 * State Cookie lifetimes making the endpoint more subject to
2359 * a replay attack.
2360 * Measure of Staleness's unit is usec. (1/1000000 sec)
2361 * Suggested Cookie Life-span Increment's unit is msec.
2362 * (1/1000 sec)
2363 * In general, if you use the suggested cookie life, the value
2364 * found in the field of measure of staleness should be doubled
2365 * to give ample time to retransmit the new cookie and thus
2366 * yield a higher probability of success on the reattempt.
2367 */
Xin Longd8238d92017-08-03 15:42:11 +08002368 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(*err)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 stale = (stale * 2) / 1000;
2370
2371 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2372 bht.param_hdr.length = htons(sizeof(bht));
2373 bht.lifespan_increment = htonl(stale);
2374
2375 /* Build that new INIT chunk. */
2376 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2377 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2378 if (!reply)
2379 goto nomem;
2380
2381 sctp_addto_chunk(reply, sizeof(bht), &bht);
2382
2383 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2384 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2385
2386 /* Stop pending T3-rtx and heartbeat timers */
2387 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2388 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2389
2390 /* Delete non-primary peer ip addresses since we are transitioning
2391 * back to the COOKIE-WAIT state
2392 */
2393 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2394
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002395 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2396 * resend
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 */
Vlad Yasevichb6157d82007-10-24 15:59:16 -04002398 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 SCTP_TRANSPORT(asoc->peer.primary_path));
2400
2401 /* Cast away the const modifier, as we want to just
2402 * rerun it through as a sideffect.
2403 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07002404 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
2406 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2407 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2408 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2409 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2410 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2411 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2412
2413 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2414
2415 return SCTP_DISPOSITION_CONSUME;
2416
2417nomem:
2418 return SCTP_DISPOSITION_NOMEM;
2419}
2420
2421/*
2422 * Process an ABORT.
2423 *
2424 * Section: 9.1
2425 * After checking the Verification Tag, the receiving endpoint shall
2426 * remove the association from its record, and shall report the
2427 * termination to its upper layer.
2428 *
2429 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2430 * B) Rules for packet carrying ABORT:
2431 *
2432 * - The endpoint shall always fill in the Verification Tag field of the
2433 * outbound packet with the destination endpoint's tag value if it
2434 * is known.
2435 *
2436 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2437 * MUST follow the procedure described in Section 8.4.
2438 *
2439 * - The receiver MUST accept the packet if the Verification Tag
2440 * matches either its own tag, OR the tag of its peer. Otherwise, the
2441 * receiver MUST silently discard the packet and take no further
2442 * action.
2443 *
2444 * Inputs
2445 * (endpoint, asoc, chunk)
2446 *
2447 * Outputs
2448 * (asoc, reply_msg, msg_up, timers, counters)
2449 *
2450 * The return value is the disposition of the chunk.
2451 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002452sctp_disposition_t sctp_sf_do_9_1_abort(struct net *net,
2453 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002455 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 void *arg,
2457 sctp_cmd_seq_t *commands)
2458{
2459 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
2461 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002462 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
2464 /* Make sure that the ABORT chunk has a valid length.
2465 * Since this is an ABORT chunk, we have to discard it
2466 * because of the following text:
2467 * RFC 2960, Section 3.3.7
2468 * If an endpoint receives an ABORT with a format error or for an
2469 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002470 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 * as we do not know its true length. So, to be safe, discard the
2472 * packet.
2473 */
Xin Long441ae652017-07-23 09:34:36 +08002474 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002475 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Vlad Yasevich75205f42007-12-20 14:12:59 -08002477 /* ADD-IP: Special case for ABORT chunks
2478 * F4) One special consideration is that ABORT Chunks arriving
2479 * destined to the IP address being deleted MUST be
2480 * ignored (see Section 5.3.1 for further details).
2481 */
2482 if (SCTP_ADDR_DEL ==
2483 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002484 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002485
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002486 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002487}
2488
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002489static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
2490 const struct sctp_endpoint *ep,
Vlad Yasevich75205f42007-12-20 14:12:59 -08002491 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002492 const union sctp_subtype type,
Vlad Yasevich75205f42007-12-20 14:12:59 -08002493 void *arg,
2494 sctp_cmd_seq_t *commands)
2495{
2496 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002497 unsigned int len;
Vlad Yasevich75205f42007-12-20 14:12:59 -08002498 __be16 error = SCTP_ERROR_NO_ERROR;
2499
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 /* See if we have an error cause code in the chunk. */
2501 len = ntohs(chunk->chunk_hdr->length);
Shan Wei96ca4682011-04-19 21:26:26 +00002502 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr)) {
Xin Longd8238d92017-08-03 15:42:11 +08002503 struct sctp_errhdr *err;
Shan Wei96ca4682011-04-19 21:26:26 +00002504
Shan Wei96ca4682011-04-19 21:26:26 +00002505 sctp_walk_errors(err, chunk->chunk_hdr);
2506 if ((void *)err != (void *)chunk->chunk_end)
Xin Longd8238d92017-08-03 15:42:11 +08002507 return sctp_sf_pdiscard(net, ep, asoc, type, arg,
2508 commands);
Shan Wei96ca4682011-04-19 21:26:26 +00002509
Xin Longd8238d92017-08-03 15:42:11 +08002510 error = ((struct sctp_errhdr *)chunk->skb->data)->cause;
Shan Wei96ca4682011-04-19 21:26:26 +00002511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002513 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002514 /* ASSOC_FAILED will DELETE_TCB. */
Al Viro5be291f2006-11-20 17:01:06 -08002515 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00002516 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2517 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
2519 return SCTP_DISPOSITION_ABORT;
2520}
2521
2522/*
2523 * Process an ABORT. (COOKIE-WAIT state)
2524 *
2525 * See sctp_sf_do_9_1_abort() above.
2526 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002527sctp_disposition_t sctp_sf_cookie_wait_abort(struct net *net,
2528 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002530 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 void *arg,
2532 sctp_cmd_seq_t *commands)
2533{
2534 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002535 unsigned int len;
Al Virof94c0192006-11-20 17:00:25 -08002536 __be16 error = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
2538 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002539 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
2541 /* Make sure that the ABORT chunk has a valid length.
2542 * Since this is an ABORT chunk, we have to discard it
2543 * because of the following text:
2544 * RFC 2960, Section 3.3.7
2545 * If an endpoint receives an ABORT with a format error or for an
2546 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002547 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 * as we do not know its true length. So, to be safe, discard the
2549 * packet.
2550 */
Xin Long441ae652017-07-23 09:34:36 +08002551 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002552 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
2554 /* See if we have an error cause code in the chunk. */
2555 len = ntohs(chunk->chunk_hdr->length);
2556 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
Xin Longd8238d92017-08-03 15:42:11 +08002557 error = ((struct sctp_errhdr *)chunk->skb->data)->cause;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002559 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002560 chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561}
2562
2563/*
2564 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2565 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002566sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(struct net *net,
2567 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002569 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 void *arg,
2571 sctp_cmd_seq_t *commands)
2572{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002573 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002574 ENOPROTOOPT, asoc,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002575 (struct sctp_transport *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576}
2577
2578/*
2579 * Process an ABORT. (COOKIE-ECHOED state)
2580 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002581sctp_disposition_t sctp_sf_cookie_echoed_abort(struct net *net,
2582 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002584 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 void *arg,
2586 sctp_cmd_seq_t *commands)
2587{
2588 /* There is a single T1 timer, so we should be able to use
2589 * common function with the COOKIE-WAIT state.
2590 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002591 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592}
2593
2594/*
2595 * Stop T1 timer and abort association with "INIT failed".
2596 *
2597 * This is common code called by several sctp_sf_*_abort() functions above.
2598 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002599static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
2600 sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -08002601 __be16 error, int sk_err,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002602 const struct sctp_association *asoc,
2603 struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
Daniel Borkmannbb333812013-06-28 19:49:40 +02002605 pr_debug("%s: ABORT received (INIT)\n", __func__);
2606
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2608 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002609 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2611 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002612 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 /* CMD_INIT_FAILED will DELETE_TCB. */
2614 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002615 SCTP_PERR(error));
Daniel Borkmannbb333812013-06-28 19:49:40 +02002616
Frank Filz3f7a87d2005-06-20 13:14:57 -07002617 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618}
2619
2620/*
2621 * sctp_sf_do_9_2_shut
2622 *
2623 * Section: 9.2
2624 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2625 * - enter the SHUTDOWN-RECEIVED state,
2626 *
2627 * - stop accepting new data from its SCTP user
2628 *
2629 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2630 * that all its outstanding DATA chunks have been received by the
2631 * SHUTDOWN sender.
2632 *
2633 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2634 * send a SHUTDOWN in response to a ULP request. And should discard
2635 * subsequent SHUTDOWN chunks.
2636 *
2637 * If there are still outstanding DATA chunks left, the SHUTDOWN
2638 * receiver shall continue to follow normal data transmission
2639 * procedures defined in Section 6 until all outstanding DATA chunks
2640 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2641 * new data from its SCTP user.
2642 *
2643 * Verification Tag: 8.5 Verification Tag [Normal verification]
2644 *
2645 * Inputs
2646 * (endpoint, asoc, chunk)
2647 *
2648 * Outputs
2649 * (asoc, reply_msg, msg_up, timers, counters)
2650 *
2651 * The return value is the disposition of the chunk.
2652 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002653sctp_disposition_t sctp_sf_do_9_2_shutdown(struct net *net,
2654 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002656 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 void *arg,
2658 sctp_cmd_seq_t *commands)
2659{
2660 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 sctp_disposition_t disposition;
Xin Longe61e4052017-08-03 15:42:09 +08002662 struct sctp_shutdownhdr *sdh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 struct sctp_ulpevent *ev;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002664 __u32 ctsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
2666 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002667 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
2669 /* Make sure that the SHUTDOWN chunk has a valid length. */
Xin Longac23e682017-08-03 15:42:10 +08002670 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002671 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 commands);
2673
2674 /* Convert the elaborate header. */
Xin Longe61e4052017-08-03 15:42:09 +08002675 sdh = (struct sctp_shutdownhdr *)chunk->skb->data;
2676 skb_pull(chunk->skb, sizeof(*sdh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 chunk->subh.shutdown_hdr = sdh;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002678 ctsn = ntohl(sdh->cum_tsn_ack);
2679
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002680 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02002681 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2682 asoc->ctsn_ack_point);
2683
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002684 return SCTP_DISPOSITION_DISCARD;
2685 }
2686
Wei Yongjundf10eec2008-10-23 01:00:21 -07002687 /* If Cumulative TSN Ack beyond the max tsn currently
2688 * send, terminating the association and respond to the
2689 * sender with an ABORT.
2690 */
2691 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002692 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002694 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2695 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2696 * inform the application that it should cease sending data.
2697 */
2698 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2699 if (!ev) {
2700 disposition = SCTP_DISPOSITION_NOMEM;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002701 goto out;
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002702 }
2703 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2704
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2706 * - enter the SHUTDOWN-RECEIVED state,
2707 * - stop accepting new data from its SCTP user
2708 *
2709 * [This is implicit in the new state.]
2710 */
2711 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2712 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2713 disposition = SCTP_DISPOSITION_CONSUME;
2714
2715 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002716 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 arg, commands);
2718 }
2719
2720 if (SCTP_DISPOSITION_NOMEM == disposition)
2721 goto out;
2722
2723 /* - verify, by checking the Cumulative TSN Ack field of the
2724 * chunk, that all its outstanding DATA chunks have been
2725 * received by the SHUTDOWN sender.
2726 */
2727 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
Al Viro2178eda2006-11-20 17:26:53 -08002728 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730out:
2731 return disposition;
2732}
2733
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002734/*
2735 * sctp_sf_do_9_2_shut_ctsn
2736 *
2737 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2738 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2739 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2740 * MUST be processed.
2741 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002742sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(struct net *net,
2743 const struct sctp_endpoint *ep,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002744 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002745 const union sctp_subtype type,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002746 void *arg,
2747 sctp_cmd_seq_t *commands)
2748{
2749 struct sctp_chunk *chunk = arg;
Xin Longe61e4052017-08-03 15:42:09 +08002750 struct sctp_shutdownhdr *sdh;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002751 __u32 ctsn;
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002752
2753 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002754 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002755
2756 /* Make sure that the SHUTDOWN chunk has a valid length. */
Xin Longac23e682017-08-03 15:42:10 +08002757 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002758 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002759 commands);
2760
Xin Longe61e4052017-08-03 15:42:09 +08002761 sdh = (struct sctp_shutdownhdr *)chunk->skb->data;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002762 ctsn = ntohl(sdh->cum_tsn_ack);
2763
2764 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02002765 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2766 asoc->ctsn_ack_point);
2767
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002768 return SCTP_DISPOSITION_DISCARD;
2769 }
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002770
2771 /* If Cumulative TSN Ack beyond the max tsn currently
2772 * send, terminating the association and respond to the
2773 * sender with an ABORT.
2774 */
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002775 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002776 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002777
2778 /* verify, by checking the Cumulative TSN Ack field of the
2779 * chunk, that all its outstanding DATA chunks have been
2780 * received by the SHUTDOWN sender.
2781 */
2782 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2783 SCTP_BE32(sdh->cum_tsn_ack));
2784
2785 return SCTP_DISPOSITION_CONSUME;
2786}
2787
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788/* RFC 2960 9.2
2789 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2790 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2791 * transport addresses (either in the IP addresses or in the INIT chunk)
2792 * that belong to this association, it should discard the INIT chunk and
2793 * retransmit the SHUTDOWN ACK chunk.
2794 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002795sctp_disposition_t sctp_sf_do_9_2_reshutack(struct net *net,
2796 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002798 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 void *arg,
2800 sctp_cmd_seq_t *commands)
2801{
2802 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2803 struct sctp_chunk *reply;
2804
Vlad Yasevichece25df2007-09-07 16:30:54 -04002805 /* Make sure that the chunk has a valid length */
Xin Long922dbc52017-06-30 11:52:13 +08002806 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002807 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04002808 commands);
2809
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 /* Since we are not going to really process this INIT, there
2811 * is no point in verifying chunk boundries. Just generate
2812 * the SHUTDOWN ACK.
2813 */
2814 reply = sctp_make_shutdown_ack(asoc, chunk);
2815 if (NULL == reply)
2816 goto nomem;
2817
2818 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2819 * the T2-SHUTDOWN timer.
2820 */
2821 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2822
2823 /* and restart the T2-shutdown timer. */
2824 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2825 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2826
2827 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2828
2829 return SCTP_DISPOSITION_CONSUME;
2830nomem:
2831 return SCTP_DISPOSITION_NOMEM;
2832}
2833
2834/*
2835 * sctp_sf_do_ecn_cwr
2836 *
2837 * Section: Appendix A: Explicit Congestion Notification
2838 *
2839 * CWR:
2840 *
2841 * RFC 2481 details a specific bit for a sender to send in the header of
2842 * its next outbound TCP segment to indicate to its peer that it has
2843 * reduced its congestion window. This is termed the CWR bit. For
2844 * SCTP the same indication is made by including the CWR chunk.
2845 * This chunk contains one data element, i.e. the TSN number that
2846 * was sent in the ECNE chunk. This element represents the lowest
2847 * TSN number in the datagram that was originally marked with the
2848 * CE bit.
2849 *
2850 * Verification Tag: 8.5 Verification Tag [Normal verification]
2851 * Inputs
2852 * (endpoint, asoc, chunk)
2853 *
2854 * Outputs
2855 * (asoc, reply_msg, msg_up, timers, counters)
2856 *
2857 * The return value is the disposition of the chunk.
2858 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002859sctp_disposition_t sctp_sf_do_ecn_cwr(struct net *net,
2860 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002862 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 void *arg,
2864 sctp_cmd_seq_t *commands)
2865{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 struct sctp_chunk *chunk = arg;
Xin Long65f77102017-08-03 15:42:16 +08002867 struct sctp_cwrhdr *cwr;
Al Viro34bcca22006-11-20 17:27:15 -08002868 u32 lowest_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
2870 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002871 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
Xin Longb515fd22017-08-03 15:42:15 +08002873 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002874 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002876
Xin Long65f77102017-08-03 15:42:16 +08002877 cwr = (struct sctp_cwrhdr *)chunk->skb->data;
2878 skb_pull(chunk->skb, sizeof(*cwr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
Al Viro34bcca22006-11-20 17:27:15 -08002880 lowest_tsn = ntohl(cwr->lowest_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
2882 /* Does this CWR ack the last sent congestion notification? */
Al Viro34bcca22006-11-20 17:27:15 -08002883 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 /* Stop sending ECNE. */
2885 sctp_add_cmd_sf(commands,
2886 SCTP_CMD_ECN_CWR,
Al Viro34bcca22006-11-20 17:27:15 -08002887 SCTP_U32(lowest_tsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 }
2889 return SCTP_DISPOSITION_CONSUME;
2890}
2891
2892/*
2893 * sctp_sf_do_ecne
2894 *
2895 * Section: Appendix A: Explicit Congestion Notification
2896 *
2897 * ECN-Echo
2898 *
2899 * RFC 2481 details a specific bit for a receiver to send back in its
2900 * TCP acknowledgements to notify the sender of the Congestion
2901 * Experienced (CE) bit having arrived from the network. For SCTP this
2902 * same indication is made by including the ECNE chunk. This chunk
2903 * contains one data element, i.e. the lowest TSN associated with the IP
2904 * datagram marked with the CE bit.....
2905 *
2906 * Verification Tag: 8.5 Verification Tag [Normal verification]
2907 * Inputs
2908 * (endpoint, asoc, chunk)
2909 *
2910 * Outputs
2911 * (asoc, reply_msg, msg_up, timers, counters)
2912 *
2913 * The return value is the disposition of the chunk.
2914 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002915sctp_disposition_t sctp_sf_do_ecne(struct net *net,
2916 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002918 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 void *arg,
2920 sctp_cmd_seq_t *commands)
2921{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 struct sctp_chunk *chunk = arg;
Xin Long1fb6d832017-08-03 15:42:14 +08002923 struct sctp_ecnehdr *ecne;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
2925 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002926 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Xin Longb515fd22017-08-03 15:42:15 +08002928 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002929 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 commands);
2931
Xin Long1fb6d832017-08-03 15:42:14 +08002932 ecne = (struct sctp_ecnehdr *)chunk->skb->data;
2933 skb_pull(chunk->skb, sizeof(*ecne));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
2935 /* If this is a newer ECNE than the last CWR packet we sent out */
2936 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2937 SCTP_U32(ntohl(ecne->lowest_tsn)));
2938
2939 return SCTP_DISPOSITION_CONSUME;
2940}
2941
2942/*
2943 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2944 *
2945 * The SCTP endpoint MUST always acknowledge the reception of each valid
2946 * DATA chunk.
2947 *
2948 * The guidelines on delayed acknowledgement algorithm specified in
2949 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2950 * acknowledgement SHOULD be generated for at least every second packet
2951 * (not every second DATA chunk) received, and SHOULD be generated within
2952 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2953 * situations it may be beneficial for an SCTP transmitter to be more
2954 * conservative than the algorithms detailed in this document allow.
2955 * However, an SCTP transmitter MUST NOT be more aggressive than the
2956 * following algorithms allow.
2957 *
2958 * A SCTP receiver MUST NOT generate more than one SACK for every
2959 * incoming packet, other than to update the offered window as the
2960 * receiving application consumes new data.
2961 *
2962 * Verification Tag: 8.5 Verification Tag [Normal verification]
2963 *
2964 * Inputs
2965 * (endpoint, asoc, chunk)
2966 *
2967 * Outputs
2968 * (asoc, reply_msg, msg_up, timers, counters)
2969 *
2970 * The return value is the disposition of the chunk.
2971 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002972sctp_disposition_t sctp_sf_eat_data_6_2(struct net *net,
2973 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002975 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 void *arg,
2977 sctp_cmd_seq_t *commands)
2978{
2979 struct sctp_chunk *chunk = arg;
Wei Yongjun6dc76942009-11-23 15:53:53 -05002980 sctp_arg_t force = SCTP_NOFORCE();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 int error;
2982
2983 if (!sctp_vtag_verify(chunk, asoc)) {
2984 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2985 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002986 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
Xin Long9f8d3142017-06-30 11:52:20 +08002989 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_data_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002990 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 commands);
2992
wangweidongcb3f8372013-12-23 12:16:50 +08002993 error = sctp_eat_data(asoc, chunk, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 switch (error) {
2995 case SCTP_IERROR_NO_ERROR:
2996 break;
2997 case SCTP_IERROR_HIGH_TSN:
2998 case SCTP_IERROR_BAD_STREAM:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002999 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 goto discard_noforce;
3001 case SCTP_IERROR_DUP_TSN:
3002 case SCTP_IERROR_IGNORE_TSN:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003003 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 goto discard_force;
3005 case SCTP_IERROR_NO_DATA:
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02003006 return SCTP_DISPOSITION_ABORT;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003007 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003008 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Xin Long3583df12017-06-30 11:52:19 +08003009 (u8 *)chunk->subh.data_hdr,
3010 sizeof(struct sctp_datahdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 default:
3012 BUG();
3013 }
3014
Wei Yongjun6dc76942009-11-23 15:53:53 -05003015 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
3016 force = SCTP_FORCE();
3017
Neil Horman9f70f462013-12-10 06:48:15 -05003018 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3020 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3021 }
3022
3023 /* If this is the last chunk in a packet, we need to count it
3024 * toward sack generation. Note that we need to SACK every
3025 * OTHER packet containing data chunks, EVEN IF WE DISCARD
3026 * THEM. We elect to NOT generate SACK's if the chunk fails
3027 * the verification tag test.
3028 *
3029 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3030 *
3031 * The SCTP endpoint MUST always acknowledge the reception of
3032 * each valid DATA chunk.
3033 *
3034 * The guidelines on delayed acknowledgement algorithm
3035 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
3036 * Specifically, an acknowledgement SHOULD be generated for at
3037 * least every second packet (not every second DATA chunk)
3038 * received, and SHOULD be generated within 200 ms of the
3039 * arrival of any unacknowledged DATA chunk. In some
3040 * situations it may be beneficial for an SCTP transmitter to
3041 * be more conservative than the algorithms detailed in this
3042 * document allow. However, an SCTP transmitter MUST NOT be
3043 * more aggressive than the following algorithms allow.
3044 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08003045 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003046 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 return SCTP_DISPOSITION_CONSUME;
3049
3050discard_force:
3051 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3052 *
3053 * When a packet arrives with duplicate DATA chunk(s) and with
3054 * no new DATA chunk(s), the endpoint MUST immediately send a
3055 * SACK with no delay. If a packet arrives with duplicate
3056 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3057 * MAY immediately send a SACK. Normally receipt of duplicate
3058 * DATA chunks will occur when the original SACK chunk was lost
3059 * and the peer's RTO has expired. The duplicate TSN number(s)
3060 * SHOULD be reported in the SACK as duplicate.
3061 */
3062 /* In our case, we split the MAY SACK advice up whether or not
3063 * the last chunk is a duplicate.'
3064 */
3065 if (chunk->end_of_packet)
3066 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3067 return SCTP_DISPOSITION_DISCARD;
3068
3069discard_noforce:
Frank Filz52ccb8e2005-12-22 11:36:46 -08003070 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003071 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 return SCTP_DISPOSITION_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074}
3075
3076/*
3077 * sctp_sf_eat_data_fast_4_4
3078 *
3079 * Section: 4 (4)
3080 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3081 * DATA chunks without delay.
3082 *
3083 * Verification Tag: 8.5 Verification Tag [Normal verification]
3084 * Inputs
3085 * (endpoint, asoc, chunk)
3086 *
3087 * Outputs
3088 * (asoc, reply_msg, msg_up, timers, counters)
3089 *
3090 * The return value is the disposition of the chunk.
3091 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003092sctp_disposition_t sctp_sf_eat_data_fast_4_4(struct net *net,
3093 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08003095 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 void *arg,
3097 sctp_cmd_seq_t *commands)
3098{
3099 struct sctp_chunk *chunk = arg;
3100 int error;
3101
3102 if (!sctp_vtag_verify(chunk, asoc)) {
3103 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3104 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003105 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 }
3107
Xin Long9f8d3142017-06-30 11:52:20 +08003108 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_data_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003109 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 commands);
3111
wangweidongcb3f8372013-12-23 12:16:50 +08003112 error = sctp_eat_data(asoc, chunk, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 switch (error) {
3114 case SCTP_IERROR_NO_ERROR:
3115 case SCTP_IERROR_HIGH_TSN:
3116 case SCTP_IERROR_DUP_TSN:
3117 case SCTP_IERROR_IGNORE_TSN:
3118 case SCTP_IERROR_BAD_STREAM:
3119 break;
3120 case SCTP_IERROR_NO_DATA:
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02003121 return SCTP_DISPOSITION_ABORT;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003122 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003123 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Xin Long3583df12017-06-30 11:52:19 +08003124 (u8 *)chunk->subh.data_hdr,
3125 sizeof(struct sctp_datahdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 default:
3127 BUG();
3128 }
3129
3130 /* Go a head and force a SACK, since we are shutting down. */
3131
3132 /* Implementor's Guide.
3133 *
3134 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3135 * respond to each received packet containing one or more DATA chunk(s)
3136 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3137 */
3138 if (chunk->end_of_packet) {
3139 /* We must delay the chunk creation since the cumulative
3140 * TSN has not been updated yet.
3141 */
3142 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3143 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3144 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3145 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3146 }
3147
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 return SCTP_DISPOSITION_CONSUME;
3149}
3150
3151/*
3152 * Section: 6.2 Processing a Received SACK
3153 * D) Any time a SACK arrives, the endpoint performs the following:
3154 *
3155 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3156 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3157 * increasing, a SACK whose Cumulative TSN Ack is less than the
3158 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3159 *
3160 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3161 * of bytes still outstanding after processing the Cumulative TSN Ack
3162 * and the Gap Ack Blocks.
3163 *
3164 * iii) If the SACK is missing a TSN that was previously
3165 * acknowledged via a Gap Ack Block (e.g., the data receiver
3166 * reneged on the data), then mark the corresponding DATA chunk
3167 * as available for retransmit: Mark it as missing for fast
3168 * retransmit as described in Section 7.2.4 and if no retransmit
3169 * timer is running for the destination address to which the DATA
3170 * chunk was originally transmitted, then T3-rtx is started for
3171 * that destination address.
3172 *
3173 * Verification Tag: 8.5 Verification Tag [Normal verification]
3174 *
3175 * Inputs
3176 * (endpoint, asoc, chunk)
3177 *
3178 * Outputs
3179 * (asoc, reply_msg, msg_up, timers, counters)
3180 *
3181 * The return value is the disposition of the chunk.
3182 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003183sctp_disposition_t sctp_sf_eat_sack_6_2(struct net *net,
3184 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08003186 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 void *arg,
3188 sctp_cmd_seq_t *commands)
3189{
3190 struct sctp_chunk *chunk = arg;
Xin Long78731082017-07-23 09:34:32 +08003191 struct sctp_sackhdr *sackh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 __u32 ctsn;
3193
3194 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003195 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
3197 /* Make sure that the SACK chunk has a valid length. */
Xin Longd4d6c612017-07-23 09:34:33 +08003198 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_sack_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003199 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 commands);
3201
3202 /* Pull the SACK chunk from the data buffer */
3203 sackh = sctp_sm_pull_sack(chunk);
3204 /* Was this a bogus SACK? */
3205 if (!sackh)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003206 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 chunk->subh.sack_hdr = sackh;
3208 ctsn = ntohl(sackh->cum_tsn_ack);
3209
3210 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3211 * Ack Point, then drop the SACK. Since Cumulative TSN
3212 * Ack is monotonically increasing, a SACK whose
3213 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3214 * Point indicates an out-of-order SACK.
3215 */
3216 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02003217 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
3218 asoc->ctsn_ack_point);
3219
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 return SCTP_DISPOSITION_DISCARD;
3221 }
3222
Wei Yongjunaecedea2007-08-02 16:57:44 +08003223 /* If Cumulative TSN Ack beyond the max tsn currently
3224 * send, terminating the association and respond to the
3225 * sender with an ABORT.
3226 */
3227 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003228 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjunaecedea2007-08-02 16:57:44 +08003229
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 /* Return this SACK for further processing. */
Nicolas Dichteledfee032012-10-03 05:43:22 +00003231 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
3233 /* Note: We do the rest of the work on the PROCESS_SACK
3234 * sideeffect.
3235 */
3236 return SCTP_DISPOSITION_CONSUME;
3237}
3238
3239/*
3240 * Generate an ABORT in response to a packet.
3241 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003242 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003244 * 8) The receiver should respond to the sender of the OOTB packet with
3245 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3246 * MUST fill in the Verification Tag field of the outbound packet
3247 * with the value found in the Verification Tag field of the OOTB
3248 * packet and set the T-bit in the Chunk Flags to indicate that the
3249 * Verification Tag is reflected. After sending this ABORT, the
3250 * receiver of the OOTB packet shall discard the OOTB packet and take
3251 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 *
3253 * Verification Tag:
3254 *
3255 * The return value is the disposition of the chunk.
3256*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003257static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
3258 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08003260 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 void *arg,
3262 sctp_cmd_seq_t *commands)
3263{
3264 struct sctp_packet *packet = NULL;
3265 struct sctp_chunk *chunk = arg;
3266 struct sctp_chunk *abort;
3267
Eric W. Biederman2ce95502012-08-06 08:43:06 +00003268 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Marcelo Ricardo Leitnereab59072016-12-28 09:26:31 -02003269 if (!packet)
3270 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
Marcelo Ricardo Leitnereab59072016-12-28 09:26:31 -02003272 /* Make an ABORT. The T bit will be set if the asoc
3273 * is NULL.
3274 */
3275 abort = sctp_make_abort(asoc, chunk, 0);
3276 if (!abort) {
3277 sctp_ootb_pkt_free(packet);
3278 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 }
3280
Marcelo Ricardo Leitnereab59072016-12-28 09:26:31 -02003281 /* Reflect vtag if T-Bit is set */
3282 if (sctp_test_T_bit(abort))
3283 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3284
3285 /* Set the skb to the belonging sock for accounting. */
3286 abort->skb->sk = ep->base.sk;
3287
3288 sctp_packet_append_chunk(packet, abort);
3289
3290 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3291 SCTP_PACKET(packet));
3292
3293 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3294
3295 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3296 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297}
3298
3299/*
3300 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3301 * event as ULP notification for each cause included in the chunk.
3302 *
3303 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3304 *
3305 * The return value is the disposition of the chunk.
3306*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003307sctp_disposition_t sctp_sf_operr_notify(struct net *net,
3308 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08003310 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 void *arg,
3312 sctp_cmd_seq_t *commands)
3313{
3314 struct sctp_chunk *chunk = arg;
Xin Longd8238d92017-08-03 15:42:11 +08003315 struct sctp_errhdr *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
3317 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003318 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319
3320 /* Make sure that the ERROR chunk has a valid length. */
Xin Long87caeba2017-08-03 15:42:12 +08003321 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_operr_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003322 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 commands);
Shan Wei8a00be12011-04-19 21:25:40 +00003324 sctp_walk_errors(err, chunk->chunk_hdr);
3325 if ((void *)err != (void *)chunk->chunk_end)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003326 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Shan Wei8a00be12011-04-19 21:25:40 +00003327 (void *)err, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
Wei Yongjun3df267872009-03-02 06:46:51 +00003329 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3330 SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333}
3334
3335/*
3336 * Process an inbound SHUTDOWN ACK.
3337 *
3338 * From Section 9.2:
3339 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3340 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3341 * peer, and remove all record of the association.
3342 *
3343 * The return value is the disposition.
3344 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003345sctp_disposition_t sctp_sf_do_9_2_final(struct net *net,
3346 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08003348 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 void *arg,
3350 sctp_cmd_seq_t *commands)
3351{
3352 struct sctp_chunk *chunk = arg;
3353 struct sctp_chunk *reply;
3354 struct sctp_ulpevent *ev;
3355
3356 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003357 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
3359 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
Xin Long922dbc52017-06-30 11:52:13 +08003360 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003361 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 /* 10.2 H) SHUTDOWN COMPLETE notification
3364 *
3365 * When SCTP completes the shutdown procedures (section 9.2) this
3366 * notification is passed to the upper layer.
3367 */
3368 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07003369 0, 0, 0, NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 if (!ev)
3371 goto nomem;
3372
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003373 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3374 reply = sctp_make_shutdown_complete(asoc, chunk);
3375 if (!reply)
3376 goto nomem_chunk;
3377
3378 /* Do all the commands now (after allocation), so that we
3379 * have consistent state if memory allocation failes
3380 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3382
3383 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3384 * stop the T2-shutdown timer,
3385 */
3386 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3387 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3388
3389 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3390 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3391
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3393 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003394 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3395 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3397
3398 /* ...and remove all record of the association. */
3399 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3400 return SCTP_DISPOSITION_DELETE_TCB;
3401
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003402nomem_chunk:
3403 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404nomem:
3405 return SCTP_DISPOSITION_NOMEM;
3406}
3407
3408/*
Jerome Forissier047a2422005-04-28 11:58:43 -07003409 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3410 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3412 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3413 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3414 * packet must fill in the Verification Tag field of the outbound
3415 * packet with the Verification Tag received in the SHUTDOWN ACK and
Jerome Forissier047a2422005-04-28 11:58:43 -07003416 * set the T-bit in the Chunk Flags to indicate that the Verification
3417 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 *
3419 * 8) The receiver should respond to the sender of the OOTB packet with
3420 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3421 * MUST fill in the Verification Tag field of the outbound packet
3422 * with the value found in the Verification Tag field of the OOTB
Jerome Forissier047a2422005-04-28 11:58:43 -07003423 * packet and set the T-bit in the Chunk Flags to indicate that the
3424 * Verification Tag is reflected. After sending this ABORT, the
3425 * receiver of the OOTB packet shall discard the OOTB packet and take
3426 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003428sctp_disposition_t sctp_sf_ootb(struct net *net,
3429 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08003431 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 void *arg,
3433 sctp_cmd_seq_t *commands)
3434{
3435 struct sctp_chunk *chunk = arg;
3436 struct sk_buff *skb = chunk->skb;
Xin Long922dbc52017-06-30 11:52:13 +08003437 struct sctp_chunkhdr *ch;
Xin Longd8238d92017-08-03 15:42:11 +08003438 struct sctp_errhdr *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 __u8 *ch_end;
3440 int ootb_shut_ack = 0;
Shan Wei85c5ed42011-04-19 21:30:01 +00003441 int ootb_cookie_ack = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003443 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
Xin Long922dbc52017-06-30 11:52:13 +08003445 ch = (struct sctp_chunkhdr *)chunk->chunk_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 do {
Vlad Yasevichece25df2007-09-07 16:30:54 -04003447 /* Report violation if the chunk is less then minimal */
Xin Long922dbc52017-06-30 11:52:13 +08003448 if (ntohs(ch->length) < sizeof(*ch))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003449 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003450 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
Marcelo Ricardo Leitnerbf911e92016-10-25 14:27:39 -02003452 /* Report violation if chunk len overflows */
3453 ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
3454 if (ch_end > skb_tail_pointer(skb))
3455 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3456 commands);
3457
Vlad Yasevichece25df2007-09-07 16:30:54 -04003458 /* Now that we know we at least have a chunk header,
3459 * do things that are type appropriate.
3460 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3462 ootb_shut_ack = 1;
3463
3464 /* RFC 2960, Section 3.3.7
3465 * Moreover, under any circumstances, an endpoint that
3466 * receives an ABORT MUST NOT respond to that ABORT by
3467 * sending an ABORT of its own.
3468 */
3469 if (SCTP_CID_ABORT == ch->type)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003470 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003471
Shan Wei85c5ed42011-04-19 21:30:01 +00003472 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3473 * or a COOKIE ACK the SCTP Packet should be silently
3474 * discarded.
3475 */
3476
3477 if (SCTP_CID_COOKIE_ACK == ch->type)
3478 ootb_cookie_ack = 1;
3479
3480 if (SCTP_CID_ERROR == ch->type) {
3481 sctp_walk_errors(err, ch) {
3482 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3483 ootb_cookie_ack = 1;
3484 break;
3485 }
3486 }
3487 }
3488
Xin Long922dbc52017-06-30 11:52:13 +08003489 ch = (struct sctp_chunkhdr *)ch_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003490 } while (ch_end < skb_tail_pointer(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
3492 if (ootb_shut_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003493 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
Shan Wei85c5ed42011-04-19 21:30:01 +00003494 else if (ootb_cookie_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003495 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003497 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498}
3499
3500/*
3501 * Handle an "Out of the blue" SHUTDOWN ACK.
3502 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003503 * Section: 8.4 5, sctpimpguide 2.41.
3504 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
Jerome Forissier047a2422005-04-28 11:58:43 -07003506 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3507 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3508 * packet must fill in the Verification Tag field of the outbound
3509 * packet with the Verification Tag received in the SHUTDOWN ACK and
3510 * set the T-bit in the Chunk Flags to indicate that the Verification
3511 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 *
3513 * Inputs
3514 * (endpoint, asoc, type, arg, commands)
3515 *
3516 * Outputs
3517 * (sctp_disposition_t)
3518 *
3519 * The return value is the disposition of the chunk.
3520 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003521static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
3522 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08003524 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 void *arg,
3526 sctp_cmd_seq_t *commands)
3527{
3528 struct sctp_packet *packet = NULL;
3529 struct sctp_chunk *chunk = arg;
3530 struct sctp_chunk *shut;
3531
Eric W. Biederman2ce95502012-08-06 08:43:06 +00003532 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Marcelo Ricardo Leitner1ff01562016-12-28 09:26:32 -02003533 if (!packet)
3534 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
Marcelo Ricardo Leitner1ff01562016-12-28 09:26:32 -02003536 /* Make an SHUTDOWN_COMPLETE.
3537 * The T bit will be set if the asoc is NULL.
3538 */
3539 shut = sctp_make_shutdown_complete(asoc, chunk);
3540 if (!shut) {
3541 sctp_ootb_pkt_free(packet);
3542 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 }
3544
Marcelo Ricardo Leitner1ff01562016-12-28 09:26:32 -02003545 /* Reflect vtag if T-Bit is set */
3546 if (sctp_test_T_bit(shut))
3547 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3548
3549 /* Set the skb to the belonging sock for accounting. */
3550 shut->skb->sk = ep->base.sk;
3551
3552 sctp_packet_append_chunk(packet, shut);
3553
3554 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3555 SCTP_PACKET(packet));
3556
3557 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3558
3559 /* If the chunk length is invalid, we don't want to process
3560 * the reset of the packet.
3561 */
Xin Long922dbc52017-06-30 11:52:13 +08003562 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Marcelo Ricardo Leitner1ff01562016-12-28 09:26:32 -02003563 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3564
3565 /* We need to discard the rest of the packet to prevent
3566 * potential bomming attacks from additional bundled chunks.
3567 * This is documented in SCTP Threats ID.
3568 */
3569 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570}
3571
3572/*
3573 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3574 *
3575 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3576 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3577 * procedures in section 8.4 SHOULD be followed, in other words it
3578 * should be treated as an Out Of The Blue packet.
3579 * [This means that we do NOT check the Verification Tag on these
3580 * chunks. --piggy ]
3581 *
3582 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003583sctp_disposition_t sctp_sf_do_8_5_1_E_sa(struct net *net,
3584 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08003586 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 void *arg,
3588 sctp_cmd_seq_t *commands)
3589{
Vlad Yasevichece25df2007-09-07 16:30:54 -04003590 struct sctp_chunk *chunk = arg;
3591
3592 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
Xin Long922dbc52017-06-30 11:52:13 +08003593 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003594 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003595 commands);
3596
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 /* Although we do have an association in this case, it corresponds
3598 * to a restarted association. So the packet is treated as an OOTB
3599 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3600 * called with a NULL association.
3601 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003602 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Wei Yongjun8190f892008-09-08 12:13:55 +08003603
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003604 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605}
3606
3607/* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003608sctp_disposition_t sctp_sf_do_asconf(struct net *net,
3609 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08003611 const union sctp_subtype type, void *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 sctp_cmd_seq_t *commands)
3613{
3614 struct sctp_chunk *chunk = arg;
3615 struct sctp_chunk *asconf_ack = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003616 struct sctp_paramhdr *err_param = NULL;
Xin Long65205cc2017-08-03 15:42:19 +08003617 struct sctp_addiphdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 __u32 serial;
3619
3620 if (!sctp_vtag_verify(chunk, asoc)) {
3621 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3622 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003623 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 }
3625
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003626 /* ADD-IP: Section 4.1.1
3627 * This chunk MUST be sent in an authenticated way by using
3628 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3629 * is received unauthenticated it MUST be silently discarded as
3630 * described in [I-D.ietf-tsvwg-sctp-auth].
3631 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003632 if (!net->sctp.addip_noauth && !chunk->auth)
Xin Long68d75462017-08-03 15:42:20 +08003633 return sctp_sf_discard_chunk(net, ep, asoc, type, arg,
3634 commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003635
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
Xin Long68d75462017-08-03 15:42:20 +08003637 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_addip_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003638 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 commands);
3640
Xin Long65205cc2017-08-03 15:42:19 +08003641 hdr = (struct sctp_addiphdr *)chunk->skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 serial = ntohl(hdr->serial);
3643
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003644 /* Verify the ASCONF chunk before processing it. */
Daniel Borkmann9de79222014-10-09 22:55:31 +02003645 if (!sctp_verify_asconf(asoc, chunk, true, &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003646 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003647 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003648
Vlad Yasevicha08de642007-12-20 14:11:47 -08003649 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 * the endpoint stored in a new association variable
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003651 * 'Peer-Serial-Number'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 */
3653 if (serial == asoc->peer.addip_serial + 1) {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003654 /* If this is the first instance of ASCONF in the packet,
3655 * we can clean our old ASCONF-ACKs.
3656 */
3657 if (!chunk->has_asconf)
3658 sctp_assoc_clean_asconf_ack_cache(asoc);
3659
3660 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3661 * expected, process the ASCONF as described below and after
3662 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3663 * the response packet and cache a copy of it (in the event it
3664 * later needs to be retransmitted).
3665 *
3666 * Essentially, do V1-V5.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 */
3668 asconf_ack = sctp_process_asconf((struct sctp_association *)
3669 asoc, chunk);
3670 if (!asconf_ack)
3671 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevicha08de642007-12-20 14:11:47 -08003672 } else if (serial < asoc->peer.addip_serial + 1) {
3673 /* ADDIP 5.2 E2)
3674 * If the value found in the Sequence Number is less than the
3675 * ('Peer- Sequence-Number' + 1), simply skip to the next
3676 * ASCONF, and include in the outbound response packet
3677 * any previously cached ASCONF-ACK response that was
3678 * sent and saved that matches the Sequence Number of the
3679 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3680 * Chunk exists. This will occur when an older ASCONF
3681 * arrives out of order. In such a case, the receiver
3682 * should skip the ASCONF Chunk and not include ASCONF-ACK
3683 * Chunk for that chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003685 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3686 if (!asconf_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 return SCTP_DISPOSITION_DISCARD;
Vlad Yasevich31b02e12009-09-04 18:21:00 -04003688
3689 /* Reset the transport so that we select the correct one
3690 * this time around. This is to make sure that we don't
3691 * accidentally use a stale transport that's been removed.
3692 */
3693 asconf_ack->transport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 } else {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003695 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 * it must be either a stale packet or from an attacker.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003697 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 return SCTP_DISPOSITION_DISCARD;
3699 }
3700
Vlad Yasevicha08de642007-12-20 14:11:47 -08003701 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3702 * containing the ASCONF-ACK Chunks MUST be the source address of
3703 * the SCTP packet that held the ASCONF Chunks.
3704 *
3705 * To do this properly, we'll set the destination address of the chunk
3706 * and at the transmit time, will try look up the transport to use.
3707 * Since ASCONFs may be bundled, the correct transport may not be
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +02003708 * created until we process the entire packet, thus this workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003710 asconf_ack->dest = chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
Michio Honda6af29cc2011-06-16 17:14:34 +09003712 if (asoc->new_transport) {
wangweidongf7010e62013-12-23 12:16:52 +08003713 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport, commands);
Michio Honda6af29cc2011-06-16 17:14:34 +09003714 ((struct sctp_association *)asoc)->new_transport = NULL;
3715 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003716
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 return SCTP_DISPOSITION_CONSUME;
3718}
3719
3720/*
3721 * ADDIP Section 4.3 General rules for address manipulation
3722 * When building TLV parameters for the ASCONF Chunk that will add or
3723 * delete IP addresses the D0 to D13 rules should be applied:
3724 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003725sctp_disposition_t sctp_sf_do_asconf_ack(struct net *net,
3726 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08003728 const union sctp_subtype type,
3729 void *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 sctp_cmd_seq_t *commands)
3731{
3732 struct sctp_chunk *asconf_ack = arg;
3733 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3734 struct sctp_chunk *abort;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003735 struct sctp_paramhdr *err_param = NULL;
Xin Long65205cc2017-08-03 15:42:19 +08003736 struct sctp_addiphdr *addip_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 __u32 sent_serial, rcvd_serial;
3738
3739 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3740 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3741 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003742 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 }
3744
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003745 /* ADD-IP, Section 4.1.2:
3746 * This chunk MUST be sent in an authenticated way by using
3747 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3748 * is received unauthenticated it MUST be silently discarded as
3749 * described in [I-D.ietf-tsvwg-sctp-auth].
3750 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003751 if (!net->sctp.addip_noauth && !asconf_ack->auth)
Xin Long68d75462017-08-03 15:42:20 +08003752 return sctp_sf_discard_chunk(net, ep, asoc, type, arg,
3753 commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003754
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 /* Make sure that the ADDIP chunk has a valid length. */
Xin Long68d75462017-08-03 15:42:20 +08003756 if (!sctp_chunk_length_valid(asconf_ack,
3757 sizeof(struct sctp_addip_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003758 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 commands);
3760
Xin Long65205cc2017-08-03 15:42:19 +08003761 addip_hdr = (struct sctp_addiphdr *)asconf_ack->skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 rcvd_serial = ntohl(addip_hdr->serial);
3763
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003764 /* Verify the ASCONF-ACK chunk before processing it. */
Daniel Borkmann9de79222014-10-09 22:55:31 +02003765 if (!sctp_verify_asconf(asoc, asconf_ack, false, &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003766 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003767 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003768
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 if (last_asconf) {
Xin Long65205cc2017-08-03 15:42:19 +08003770 addip_hdr = (struct sctp_addiphdr *)last_asconf->subh.addip_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 sent_serial = ntohl(addip_hdr->serial);
3772 } else {
3773 sent_serial = asoc->addip_serial - 1;
3774 }
3775
3776 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3777 * equal to the next serial number to be used but no ASCONF chunk is
3778 * outstanding the endpoint MUST ABORT the association. Note that a
3779 * sequence number is greater than if it is no more than 2^^31-1
3780 * larger than the current sequence number (using serial arithmetic).
3781 */
3782 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3783 !(asoc->addip_last_asconf)) {
3784 abort = sctp_make_abort(asoc, asconf_ack,
Xin Longd8238d92017-08-03 15:42:11 +08003785 sizeof(struct sctp_errhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003787 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3789 SCTP_CHUNK(abort));
3790 }
3791 /* We are going to ABORT, so we might as well stop
3792 * processing the rest of the chunks in the packet.
3793 */
3794 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3795 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
wangweidongcb3f8372013-12-23 12:16:50 +08003796 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003797 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003798 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003800 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003801 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3802 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 return SCTP_DISPOSITION_ABORT;
3804 }
3805
3806 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3807 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3808 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3809
3810 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
Vlad Yasevichc0786692010-04-28 08:47:22 +00003811 asconf_ack)) {
3812 /* Successfully processed ASCONF_ACK. We can
3813 * release the next asconf if we have one.
3814 */
3815 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3816 SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 return SCTP_DISPOSITION_CONSUME;
Vlad Yasevichc0786692010-04-28 08:47:22 +00003818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819
3820 abort = sctp_make_abort(asoc, asconf_ack,
Xin Longd8238d92017-08-03 15:42:11 +08003821 sizeof(struct sctp_errhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003823 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3825 SCTP_CHUNK(abort));
3826 }
3827 /* We are going to ABORT, so we might as well stop
3828 * processing the rest of the chunks in the packet.
3829 */
wangweidongcb3f8372013-12-23 12:16:50 +08003830 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003831 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003832 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003834 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003835 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3836 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 return SCTP_DISPOSITION_ABORT;
3838 }
3839
3840 return SCTP_DISPOSITION_DISCARD;
3841}
3842
Xin Long2040d3d2017-02-17 12:45:42 +08003843/* RE-CONFIG Section 5.2 Upon reception of an RECONF Chunk. */
3844sctp_disposition_t sctp_sf_do_reconf(struct net *net,
3845 const struct sctp_endpoint *ep,
3846 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08003847 const union sctp_subtype type, void *arg,
Xin Long2040d3d2017-02-17 12:45:42 +08003848 sctp_cmd_seq_t *commands)
3849{
3850 struct sctp_paramhdr *err_param = NULL;
3851 struct sctp_chunk *chunk = arg;
3852 struct sctp_reconf_chunk *hdr;
3853 union sctp_params param;
3854
3855 if (!sctp_vtag_verify(chunk, asoc)) {
3856 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3857 SCTP_NULL());
3858 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3859 }
3860
3861 /* Make sure that the RECONF chunk has a valid length. */
3862 if (!sctp_chunk_length_valid(chunk, sizeof(*hdr)))
3863 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3864 commands);
3865
3866 if (!sctp_verify_reconf(asoc, chunk, &err_param))
3867 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3868 (void *)err_param, commands);
3869
3870 hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
3871 sctp_walk_params(param, hdr, params) {
3872 struct sctp_chunk *reply = NULL;
3873 struct sctp_ulpevent *ev = NULL;
3874
3875 if (param.p->type == SCTP_PARAM_RESET_OUT_REQUEST)
3876 reply = sctp_process_strreset_outreq(
3877 (struct sctp_association *)asoc, param, &ev);
3878 else if (param.p->type == SCTP_PARAM_RESET_IN_REQUEST)
3879 reply = sctp_process_strreset_inreq(
3880 (struct sctp_association *)asoc, param, &ev);
Xin Long692787c2017-03-10 12:11:07 +08003881 else if (param.p->type == SCTP_PARAM_RESET_TSN_REQUEST)
3882 reply = sctp_process_strreset_tsnreq(
3883 (struct sctp_association *)asoc, param, &ev);
Xin Long50a41592017-03-10 12:11:09 +08003884 else if (param.p->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS)
3885 reply = sctp_process_strreset_addstrm_out(
3886 (struct sctp_association *)asoc, param, &ev);
Xin Longc5c4ebb2017-03-10 12:11:10 +08003887 else if (param.p->type == SCTP_PARAM_RESET_ADD_IN_STREAMS)
3888 reply = sctp_process_strreset_addstrm_in(
3889 (struct sctp_association *)asoc, param, &ev);
Xin Long11ae76e2017-03-10 12:11:11 +08003890 else if (param.p->type == SCTP_PARAM_RESET_RESPONSE)
3891 reply = sctp_process_strreset_resp(
3892 (struct sctp_association *)asoc, param, &ev);
Xin Long2040d3d2017-02-17 12:45:42 +08003893
3894 if (ev)
3895 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
3896 SCTP_ULPEVENT(ev));
3897
3898 if (reply)
3899 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3900 SCTP_CHUNK(reply));
3901 }
3902
3903 return SCTP_DISPOSITION_CONSUME;
3904}
3905
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906/*
3907 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3908 *
3909 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3910 * its cumulative TSN point to the value carried in the FORWARD TSN
3911 * chunk, and then MUST further advance its cumulative TSN point locally
3912 * if possible.
3913 * After the above processing, the data receiver MUST stop reporting any
3914 * missing TSNs earlier than or equal to the new cumulative TSN point.
3915 *
3916 * Verification Tag: 8.5 Verification Tag [Normal verification]
3917 *
3918 * The return value is the disposition of the chunk.
3919 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003920sctp_disposition_t sctp_sf_eat_fwd_tsn(struct net *net,
3921 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08003923 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 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
Xin Longd15c9ed2017-02-03 17:37:06 +08003939 if (!asoc->peer.prsctp_capable)
3940 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
3941
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 /* Make sure that the FORWARD_TSN chunk has valid length. */
3943 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003944 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 commands);
3946
3947 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3948 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3949 len = ntohs(chunk->chunk_hdr->length);
3950 len -= sizeof(struct sctp_chunkhdr);
3951 skb_pull(chunk->skb, len);
3952
3953 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02003954 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955
3956 /* The TSN is too high--silently discard the chunk and count on it
3957 * getting retransmitted later.
3958 */
3959 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3960 goto discard_noforce;
3961
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003962 /* Silently discard the chunk if stream-id is not valid */
3963 sctp_walk_fwdtsn(skip, chunk) {
Xin Longcee360a2017-05-31 16:36:31 +08003964 if (ntohs(skip->stream) >= asoc->stream.incnt)
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003965 goto discard_noforce;
3966 }
3967
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3969 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003970 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003972
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 /* Count this as receiving DATA. */
Neil Horman9f70f462013-12-10 06:48:15 -05003974 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3976 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3977 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003978
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 /* FIXME: For now send a SACK, but DATA processing may
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003980 * send another.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 */
3982 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983
3984 return SCTP_DISPOSITION_CONSUME;
3985
3986discard_noforce:
3987 return SCTP_DISPOSITION_DISCARD;
3988}
3989
3990sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003991 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 const struct sctp_endpoint *ep,
3993 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08003994 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 void *arg,
3996 sctp_cmd_seq_t *commands)
3997{
3998 struct sctp_chunk *chunk = arg;
3999 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08004000 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 __u16 len;
4002 __u32 tsn;
4003
4004 if (!sctp_vtag_verify(chunk, asoc)) {
4005 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4006 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004007 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 }
4009
Xin Longd15c9ed2017-02-03 17:37:06 +08004010 if (!asoc->peer.prsctp_capable)
4011 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4012
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 /* Make sure that the FORWARD_TSN chunk has a valid length. */
4014 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004015 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 commands);
4017
4018 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
4019 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
4020 len = ntohs(chunk->chunk_hdr->length);
4021 len -= sizeof(struct sctp_chunkhdr);
4022 skb_pull(chunk->skb, len);
4023
4024 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02004025 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026
4027 /* The TSN is too high--silently discard the chunk and count on it
4028 * getting retransmitted later.
4029 */
4030 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
4031 goto gen_shutdown;
4032
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08004033 /* Silently discard the chunk if stream-id is not valid */
4034 sctp_walk_fwdtsn(skip, chunk) {
Xin Longcee360a2017-05-31 16:36:31 +08004035 if (ntohs(skip->stream) >= asoc->stream.incnt)
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08004036 goto gen_shutdown;
4037 }
4038
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
4040 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004041 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004043
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 /* Go a head and force a SACK, since we are shutting down. */
4045gen_shutdown:
4046 /* Implementor's Guide.
4047 *
4048 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
4049 * respond to each received packet containing one or more DATA chunk(s)
4050 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
4051 */
4052 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
4053 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
4054 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4055 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4056
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004057 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058}
4059
4060/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004061 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004062 *
4063 * The receiver MUST use the HMAC algorithm indicated in the HMAC
4064 * Identifier field. If this algorithm was not specified by the
4065 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
4066 * during association setup, the AUTH chunk and all chunks after it MUST
4067 * be discarded and an ERROR chunk SHOULD be sent with the error cause
4068 * defined in Section 4.1.
4069 *
4070 * If an endpoint with no shared key receives a Shared Key Identifier
4071 * other than 0, it MUST silently discard all authenticated chunks. If
4072 * the endpoint has at least one endpoint pair shared key for the peer,
4073 * it MUST use the key specified by the Shared Key Identifier if a
4074 * key has been configured for that Shared Key Identifier. If no
4075 * endpoint pair shared key has been configured for that Shared Key
4076 * Identifier, all authenticated chunks MUST be silently discarded.
4077 *
4078 * Verification Tag: 8.5 Verification Tag [Normal verification]
4079 *
4080 * The return value is the disposition of the chunk.
4081 */
Xin Long4785c7a2017-08-05 19:59:58 +08004082static enum sctp_ierror sctp_sf_authenticate(
4083 struct net *net,
4084 const struct sctp_endpoint *ep,
4085 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08004086 const union sctp_subtype type,
Xin Long4785c7a2017-08-05 19:59:58 +08004087 struct sctp_chunk *chunk)
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004088{
4089 struct sctp_authhdr *auth_hdr;
4090 struct sctp_hmac *hmac;
4091 unsigned int sig_len;
4092 __u16 key_id;
4093 __u8 *save_digest;
4094 __u8 *digest;
4095
4096 /* Pull in the auth header, so we can do some more verification */
4097 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4098 chunk->subh.auth_hdr = auth_hdr;
Xin Longbb96dec2017-08-03 15:42:22 +08004099 skb_pull(chunk->skb, sizeof(*auth_hdr));
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004100
Masanari Iida02582e92012-08-22 19:11:26 +09004101 /* Make sure that we support the HMAC algorithm from the auth
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004102 * chunk.
4103 */
4104 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4105 return SCTP_IERROR_AUTH_BAD_HMAC;
4106
4107 /* Make sure that the provided shared key identifier has been
4108 * configured
4109 */
4110 key_id = ntohs(auth_hdr->shkey_id);
4111 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
4112 return SCTP_IERROR_AUTH_BAD_KEYID;
4113
4114
4115 /* Make sure that the length of the signature matches what
4116 * we expect.
4117 */
Xin Longbb96dec2017-08-03 15:42:22 +08004118 sig_len = ntohs(chunk->chunk_hdr->length) -
4119 sizeof(struct sctp_auth_chunk);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004120 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4121 if (sig_len != hmac->hmac_len)
4122 return SCTP_IERROR_PROTO_VIOLATION;
4123
4124 /* Now that we've done validation checks, we can compute and
4125 * verify the hmac. The steps involved are:
4126 * 1. Save the digest from the chunk.
4127 * 2. Zero out the digest in the chunk.
4128 * 3. Compute the new digest
4129 * 4. Compare saved and new digests.
4130 */
4131 digest = auth_hdr->hmac;
4132 skb_pull(chunk->skb, sig_len);
4133
4134 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4135 if (!save_digest)
4136 goto nomem;
4137
4138 memset(digest, 0, sig_len);
4139
4140 sctp_auth_calculate_hmac(asoc, chunk->skb,
Xin Longbb96dec2017-08-03 15:42:22 +08004141 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4142 GFP_ATOMIC);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004143
4144 /* Discard the packet if the digests do not match */
4145 if (memcmp(save_digest, digest, sig_len)) {
4146 kfree(save_digest);
4147 return SCTP_IERROR_BAD_SIG;
4148 }
4149
4150 kfree(save_digest);
4151 chunk->auth = 1;
4152
4153 return SCTP_IERROR_NO_ERROR;
4154nomem:
4155 return SCTP_IERROR_NOMEM;
4156}
4157
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004158sctp_disposition_t sctp_sf_eat_auth(struct net *net,
4159 const struct sctp_endpoint *ep,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004160 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08004161 const union sctp_subtype type,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004162 void *arg,
4163 sctp_cmd_seq_t *commands)
4164{
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004165 struct sctp_chunk *chunk = arg;
Xin Long4785c7a2017-08-05 19:59:58 +08004166 struct sctp_authhdr *auth_hdr;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004167 struct sctp_chunk *err_chunk;
Xin Long4785c7a2017-08-05 19:59:58 +08004168 enum sctp_ierror error;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004169
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004170 /* Make sure that the peer has AUTH capable */
4171 if (!asoc->peer.auth_capable)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004172 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004173
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004174 if (!sctp_vtag_verify(chunk, asoc)) {
4175 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4176 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004177 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004178 }
4179
4180 /* Make sure that the AUTH chunk has valid length. */
4181 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004182 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004183 commands);
4184
4185 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004186 error = sctp_sf_authenticate(net, ep, asoc, type, chunk);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004187 switch (error) {
Joe Perches7fd71b12011-07-01 09:43:11 +00004188 case SCTP_IERROR_AUTH_BAD_HMAC:
4189 /* Generate the ERROR chunk and discard the rest
4190 * of the packet
4191 */
4192 err_chunk = sctp_make_op_error(asoc, chunk,
4193 SCTP_ERROR_UNSUP_HMAC,
4194 &auth_hdr->hmac_id,
4195 sizeof(__u16), 0);
4196 if (err_chunk) {
4197 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4198 SCTP_CHUNK(err_chunk));
4199 }
4200 /* Fall Through */
4201 case SCTP_IERROR_AUTH_BAD_KEYID:
4202 case SCTP_IERROR_BAD_SIG:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004203 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Joe Perches7fd71b12011-07-01 09:43:11 +00004204
4205 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004206 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Joe Perches7fd71b12011-07-01 09:43:11 +00004207 commands);
4208
4209 case SCTP_IERROR_NOMEM:
4210 return SCTP_DISPOSITION_NOMEM;
4211
4212 default: /* Prevent gcc warnings */
4213 break;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004214 }
4215
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004216 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4217 struct sctp_ulpevent *ev;
4218
4219 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4220 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4221
4222 if (!ev)
4223 return -ENOMEM;
4224
4225 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4226 SCTP_ULPEVENT(ev));
4227 }
4228
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004229 return SCTP_DISPOSITION_CONSUME;
4230}
4231
4232/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 * Process an unknown chunk.
4234 *
4235 * Section: 3.2. Also, 2.1 in the implementor's guide.
4236 *
4237 * Chunk Types are encoded such that the highest-order two bits specify
4238 * the action that must be taken if the processing endpoint does not
4239 * recognize the Chunk Type.
4240 *
4241 * 00 - Stop processing this SCTP packet and discard it, do not process
4242 * any further chunks within it.
4243 *
4244 * 01 - Stop processing this SCTP packet and discard it, do not process
4245 * any further chunks within it, and report the unrecognized
4246 * chunk in an 'Unrecognized Chunk Type'.
4247 *
4248 * 10 - Skip this chunk and continue processing.
4249 *
4250 * 11 - Skip this chunk and continue processing, but report in an ERROR
4251 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4252 *
4253 * The return value is the disposition of the chunk.
4254 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004255sctp_disposition_t sctp_sf_unk_chunk(struct net *net,
4256 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08004258 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 void *arg,
4260 sctp_cmd_seq_t *commands)
4261{
4262 struct sctp_chunk *unk_chunk = arg;
4263 struct sctp_chunk *err_chunk;
Xin Long922dbc52017-06-30 11:52:13 +08004264 struct sctp_chunkhdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265
Daniel Borkmannbb333812013-06-28 19:49:40 +02004266 pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267
4268 if (!sctp_vtag_verify(unk_chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004269 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270
4271 /* Make sure that the chunk has a valid length.
4272 * Since we don't know the chunk type, we use a general
4273 * chunkhdr structure to make a comparison.
4274 */
Xin Long922dbc52017-06-30 11:52:13 +08004275 if (!sctp_chunk_length_valid(unk_chunk, sizeof(*hdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004276 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 commands);
4278
4279 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4280 case SCTP_CID_ACTION_DISCARD:
4281 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004282 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 case SCTP_CID_ACTION_DISCARD_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 /* Generate an ERROR chunk as response. */
4285 hdr = unk_chunk->chunk_hdr;
4286 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4287 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -03004288 SCTP_PAD4(ntohs(hdr->length)),
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004289 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 if (err_chunk) {
4291 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4292 SCTP_CHUNK(err_chunk));
4293 }
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07004294
4295 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004296 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 case SCTP_CID_ACTION_SKIP:
4299 /* Skip the chunk. */
4300 return SCTP_DISPOSITION_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 case SCTP_CID_ACTION_SKIP_ERR:
4302 /* Generate an ERROR chunk as response. */
4303 hdr = unk_chunk->chunk_hdr;
4304 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4305 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -03004306 SCTP_PAD4(ntohs(hdr->length)),
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004307 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 if (err_chunk) {
4309 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4310 SCTP_CHUNK(err_chunk));
4311 }
4312 /* Skip the chunk. */
4313 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 default:
4315 break;
4316 }
4317
4318 return SCTP_DISPOSITION_DISCARD;
4319}
4320
4321/*
4322 * Discard the chunk.
4323 *
4324 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4325 * [Too numerous to mention...]
4326 * Verification Tag: No verification needed.
4327 * Inputs
4328 * (endpoint, asoc, chunk)
4329 *
4330 * Outputs
4331 * (asoc, reply_msg, msg_up, timers, counters)
4332 *
4333 * The return value is the disposition of the chunk.
4334 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004335sctp_disposition_t sctp_sf_discard_chunk(struct net *net,
4336 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08004338 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 void *arg,
4340 sctp_cmd_seq_t *commands)
4341{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004342 struct sctp_chunk *chunk = arg;
4343
4344 /* Make sure that the chunk has a valid length.
4345 * Since we don't know the chunk type, we use a general
4346 * chunkhdr structure to make a comparison.
4347 */
Xin Long922dbc52017-06-30 11:52:13 +08004348 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004349 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004350 commands);
4351
Daniel Borkmannbb333812013-06-28 19:49:40 +02004352 pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
4353
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 return SCTP_DISPOSITION_DISCARD;
4355}
4356
4357/*
4358 * Discard the whole packet.
4359 *
4360 * Section: 8.4 2)
4361 *
4362 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4363 * silently discard the OOTB packet and take no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 *
4365 * Verification Tag: No verification necessary
4366 *
4367 * Inputs
4368 * (endpoint, asoc, chunk)
4369 *
4370 * Outputs
4371 * (asoc, reply_msg, msg_up, timers, counters)
4372 *
4373 * The return value is the disposition of the chunk.
4374 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004375sctp_disposition_t sctp_sf_pdiscard(struct net *net,
4376 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08004378 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 void *arg,
4380 sctp_cmd_seq_t *commands)
4381{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004382 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4384
4385 return SCTP_DISPOSITION_CONSUME;
4386}
4387
4388
4389/*
4390 * The other end is violating protocol.
4391 *
4392 * Section: Not specified
4393 * Verification Tag: Not specified
4394 * Inputs
4395 * (endpoint, asoc, chunk)
4396 *
4397 * Outputs
4398 * (asoc, reply_msg, msg_up, timers, counters)
4399 *
4400 * We simply tag the chunk as a violation. The state machine will log
4401 * the violation and continue.
4402 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004403sctp_disposition_t sctp_sf_violation(struct net *net,
4404 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08004406 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 void *arg,
4408 sctp_cmd_seq_t *commands)
4409{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004410 struct sctp_chunk *chunk = arg;
4411
4412 /* Make sure that the chunk has a valid length. */
Xin Long922dbc52017-06-30 11:52:13 +08004413 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004414 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004415 commands);
4416
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 return SCTP_DISPOSITION_VIOLATION;
4418}
4419
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420/*
Wei Yongjunaecedea2007-08-02 16:57:44 +08004421 * Common function to handle a protocol violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 */
Wei Yongjunaecedea2007-08-02 16:57:44 +08004423static sctp_disposition_t sctp_sf_abort_violation(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004424 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004425 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 void *arg,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004428 sctp_cmd_seq_t *commands,
4429 const __u8 *payload,
4430 const size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004432 struct sctp_packet *packet = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 struct sctp_chunk *chunk = arg;
4434 struct sctp_chunk *abort = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004436 /* SCTP-AUTH, Section 6.3:
4437 * It should be noted that if the receiver wants to tear
4438 * down an association in an authenticated way only, the
4439 * handling of malformed packets should not result in
4440 * tearing down the association.
4441 *
4442 * This means that if we only want to abort associations
4443 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004444 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004445 * was malformed.
4446 */
4447 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4448 goto discard;
4449
Jesper Juhl9abed242007-11-11 23:57:49 +01004450 /* Make the abort chunk. */
4451 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4452 if (!abort)
4453 goto nomem;
4454
Vlad Yasevichece25df2007-09-07 16:30:54 -04004455 if (asoc) {
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004456 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4457 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4458 !asoc->peer.i.init_tag) {
Xin Longcb1844c2017-07-23 09:34:26 +08004459 struct sctp_initack_chunk *initack;
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004460
Xin Longcb1844c2017-07-23 09:34:26 +08004461 initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
4462 if (!sctp_chunk_length_valid(chunk, sizeof(*initack)))
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004463 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4464 else {
4465 unsigned int inittag;
4466
4467 inittag = ntohl(initack->init_hdr.init_tag);
4468 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4469 SCTP_U32(inittag));
4470 }
4471 }
4472
Vlad Yasevichece25df2007-09-07 16:30:54 -04004473 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004474 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
Vlad Yasevichece25df2007-09-07 16:30:54 -04004476 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4477 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4478 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4479 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4480 SCTP_ERROR(ECONNREFUSED));
4481 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4482 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4483 } else {
4484 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4485 SCTP_ERROR(ECONNABORTED));
4486 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4487 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004488 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 } else {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00004491 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004492
4493 if (!packet)
4494 goto nomem_pkt;
4495
4496 if (sctp_test_T_bit(abort))
4497 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4498
4499 abort->skb->sk = ep->base.sk;
4500
4501 sctp_packet_append_chunk(packet, abort);
4502
4503 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4504 SCTP_PACKET(packet));
4505
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004506 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 }
4508
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004509 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004510
Vlad Yasevich56eb82b2008-10-09 14:33:01 -07004511discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004512 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 return SCTP_DISPOSITION_ABORT;
4514
Vlad Yasevichece25df2007-09-07 16:30:54 -04004515nomem_pkt:
4516 sctp_chunk_free(abort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517nomem:
4518 return SCTP_DISPOSITION_NOMEM;
4519}
4520
Wei Yongjunaecedea2007-08-02 16:57:44 +08004521/*
4522 * Handle a protocol violation when the chunk length is invalid.
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004523 * "Invalid" length is identified as smaller than the minimal length a
Wei Yongjunaecedea2007-08-02 16:57:44 +08004524 * given chunk can be. For example, a SACK chunk has invalid length
Xin Longd4d6c612017-07-23 09:34:33 +08004525 * if its length is set to be smaller than the size of struct sctp_sack_chunk.
Wei Yongjunaecedea2007-08-02 16:57:44 +08004526 *
4527 * We inform the other end by sending an ABORT with a Protocol Violation
4528 * error code.
4529 *
4530 * Section: Not specified
4531 * Verification Tag: Nothing to do
4532 * Inputs
4533 * (endpoint, asoc, chunk)
4534 *
4535 * Outputs
4536 * (reply_msg, msg_up, counters)
4537 *
4538 * Generate an ABORT chunk and terminate the association.
4539 */
4540static sctp_disposition_t sctp_sf_violation_chunklen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004541 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004542 const struct sctp_endpoint *ep,
4543 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08004544 const union sctp_subtype type,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004545 void *arg,
4546 sctp_cmd_seq_t *commands)
4547{
wangweidongcb3f8372013-12-23 12:16:50 +08004548 static const char err_str[] = "The following chunk had invalid length:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004549
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004550 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004551 sizeof(err_str));
4552}
4553
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004554/*
4555 * Handle a protocol violation when the parameter length is invalid.
Shan Wei33c7cfd2011-04-18 19:11:01 +00004556 * If the length is smaller than the minimum length of a given parameter,
4557 * or accumulated length in multi parameters exceeds the end of the chunk,
4558 * the length is considered as invalid.
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004559 */
4560static sctp_disposition_t sctp_sf_violation_paramlen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004561 struct net *net,
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004562 const struct sctp_endpoint *ep,
4563 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08004564 const union sctp_subtype type,
Wei Yongjunba016672008-09-30 05:32:24 -07004565 void *arg, void *ext,
4566 sctp_cmd_seq_t *commands)
4567{
4568 struct sctp_chunk *chunk = arg;
4569 struct sctp_paramhdr *param = ext;
4570 struct sctp_chunk *abort = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004571
Wei Yongjunba016672008-09-30 05:32:24 -07004572 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4573 goto discard;
4574
4575 /* Make the abort chunk. */
4576 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4577 if (!abort)
4578 goto nomem;
4579
4580 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004581 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Wei Yongjunba016672008-09-30 05:32:24 -07004582
4583 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4584 SCTP_ERROR(ECONNABORTED));
4585 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4586 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004587 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4588 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Wei Yongjunba016672008-09-30 05:32:24 -07004589
4590discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004591 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Wei Yongjunba016672008-09-30 05:32:24 -07004592 return SCTP_DISPOSITION_ABORT;
4593nomem:
4594 return SCTP_DISPOSITION_NOMEM;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004595}
4596
Wei Yongjunaecedea2007-08-02 16:57:44 +08004597/* Handle a protocol violation when the peer trying to advance the
4598 * cumulative tsn ack to a point beyond the max tsn currently sent.
4599 *
4600 * We inform the other end by sending an ABORT with a Protocol Violation
4601 * error code.
4602 */
4603static sctp_disposition_t sctp_sf_violation_ctsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004604 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004605 const struct sctp_endpoint *ep,
4606 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08004607 const union sctp_subtype type,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004608 void *arg,
4609 sctp_cmd_seq_t *commands)
4610{
wangweidongcb3f8372013-12-23 12:16:50 +08004611 static const char err_str[] = "The cumulative tsn ack beyond the max tsn currently sent:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004612
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004613 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004614 sizeof(err_str));
4615}
4616
Vlad Yasevichece25df2007-09-07 16:30:54 -04004617/* Handle protocol violation of an invalid chunk bundling. For example,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004618 * when we have an association and we receive bundled INIT-ACK, or
Vlad Yasevichece25df2007-09-07 16:30:54 -04004619 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004620 * statement from the specs. Additionally, there might be an attacker
Vlad Yasevichece25df2007-09-07 16:30:54 -04004621 * on the path and we may not want to continue this communication.
4622 */
4623static sctp_disposition_t sctp_sf_violation_chunk(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004624 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004625 const struct sctp_endpoint *ep,
4626 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08004627 const union sctp_subtype type,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004628 void *arg,
4629 sctp_cmd_seq_t *commands)
4630{
wangweidongcb3f8372013-12-23 12:16:50 +08004631 static const char err_str[] = "The following chunk violates protocol:";
Vlad Yasevichece25df2007-09-07 16:30:54 -04004632
4633 if (!asoc)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004634 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004635
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004636 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004637 sizeof(err_str));
4638}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639/***************************************************************************
4640 * These are the state functions for handling primitive (Section 10) events.
4641 ***************************************************************************/
4642/*
4643 * sctp_sf_do_prm_asoc
4644 *
4645 * Section: 10.1 ULP-to-SCTP
4646 * B) Associate
4647 *
4648 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4649 * outbound stream count)
4650 * -> association id [,destination transport addr list] [,outbound stream
4651 * count]
4652 *
4653 * This primitive allows the upper layer to initiate an association to a
4654 * specific peer endpoint.
4655 *
4656 * The peer endpoint shall be specified by one of the transport addresses
4657 * which defines the endpoint (see Section 1.4). If the local SCTP
4658 * instance has not been initialized, the ASSOCIATE is considered an
4659 * error.
4660 * [This is not relevant for the kernel implementation since we do all
4661 * initialization at boot time. It we hadn't initialized we wouldn't
4662 * get anywhere near this code.]
4663 *
4664 * An association id, which is a local handle to the SCTP association,
4665 * will be returned on successful establishment of the association. If
4666 * SCTP is not able to open an SCTP association with the peer endpoint,
4667 * an error is returned.
4668 * [In the kernel implementation, the struct sctp_association needs to
4669 * be created BEFORE causing this primitive to run.]
4670 *
4671 * Other association parameters may be returned, including the
4672 * complete destination transport addresses of the peer as well as the
4673 * outbound stream count of the local endpoint. One of the transport
4674 * address from the returned destination addresses will be selected by
4675 * the local endpoint as default primary path for sending SCTP packets
4676 * to this peer. The returned "destination transport addr list" can
4677 * be used by the ULP to change the default primary path or to force
4678 * sending a packet to a specific transport address. [All of this
4679 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4680 * function.]
4681 *
4682 * Mandatory attributes:
4683 *
4684 * o local SCTP instance name - obtained from the INITIALIZE operation.
4685 * [This is the argument asoc.]
4686 * o destination transport addr - specified as one of the transport
4687 * addresses of the peer endpoint with which the association is to be
4688 * established.
4689 * [This is asoc->peer.active_path.]
4690 * o outbound stream count - the number of outbound streams the ULP
4691 * would like to open towards this peer endpoint.
4692 * [BUG: This is not currently implemented.]
4693 * Optional attributes:
4694 *
4695 * None.
4696 *
4697 * The return value is a disposition.
4698 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004699sctp_disposition_t sctp_sf_do_prm_asoc(struct net *net,
4700 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08004702 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 void *arg,
4704 sctp_cmd_seq_t *commands)
4705{
4706 struct sctp_chunk *repl;
wangweidong26ac8e52013-12-23 12:16:51 +08004707 struct sctp_association *my_asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
4709 /* The comment below says that we enter COOKIE-WAIT AFTER
4710 * sending the INIT, but that doesn't actually work in our
4711 * implementation...
4712 */
4713 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4714 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4715
4716 /* RFC 2960 5.1 Normal Establishment of an Association
4717 *
4718 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4719 * must provide its Verification Tag (Tag_A) in the Initiate
4720 * Tag field. Tag_A SHOULD be a random number in the range of
4721 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4722 */
4723
4724 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4725 if (!repl)
4726 goto nomem;
4727
Daniel Borkmann405426f2013-06-14 18:24:05 +02004728 /* Choose transport for INIT. */
4729 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4730 SCTP_CHUNK(repl));
4731
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 /* Cast away the const modifier, as we want to just
4733 * rerun it through as a sideffect.
4734 */
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004735 my_asoc = (struct sctp_association *)asoc;
4736 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737
4738 /* After sending the INIT, "A" starts the T1-init timer and
4739 * enters the COOKIE-WAIT state.
4740 */
4741 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4742 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4743 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4744 return SCTP_DISPOSITION_CONSUME;
4745
4746nomem:
4747 return SCTP_DISPOSITION_NOMEM;
4748}
4749
4750/*
4751 * Process the SEND primitive.
4752 *
4753 * Section: 10.1 ULP-to-SCTP
4754 * E) Send
4755 *
4756 * Format: SEND(association id, buffer address, byte count [,context]
4757 * [,stream id] [,life time] [,destination transport address]
4758 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4759 * -> result
4760 *
4761 * This is the main method to send user data via SCTP.
4762 *
4763 * Mandatory attributes:
4764 *
4765 * o association id - local handle to the SCTP association
4766 *
4767 * o buffer address - the location where the user message to be
4768 * transmitted is stored;
4769 *
4770 * o byte count - The size of the user data in number of bytes;
4771 *
4772 * Optional attributes:
4773 *
4774 * o context - an optional 32 bit integer that will be carried in the
4775 * sending failure notification to the ULP if the transportation of
4776 * this User Message fails.
4777 *
4778 * o stream id - to indicate which stream to send the data on. If not
4779 * specified, stream 0 will be used.
4780 *
4781 * o life time - specifies the life time of the user data. The user data
4782 * will not be sent by SCTP after the life time expires. This
4783 * parameter can be used to avoid efforts to transmit stale
4784 * user messages. SCTP notifies the ULP if the data cannot be
4785 * initiated to transport (i.e. sent to the destination via SCTP's
4786 * send primitive) within the life time variable. However, the
4787 * user data will be transmitted if SCTP has attempted to transmit a
4788 * chunk before the life time expired.
4789 *
4790 * o destination transport address - specified as one of the destination
4791 * transport addresses of the peer endpoint to which this packet
4792 * should be sent. Whenever possible, SCTP should use this destination
4793 * transport address for sending the packets, instead of the current
4794 * primary path.
4795 *
4796 * o unorder flag - this flag, if present, indicates that the user
4797 * would like the data delivered in an unordered fashion to the peer
4798 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4799 * message).
4800 *
4801 * o no-bundle flag - instructs SCTP not to bundle this user data with
4802 * other outbound DATA chunks. SCTP MAY still bundle even when
4803 * this flag is present, when faced with network congestion.
4804 *
4805 * o payload protocol-id - A 32 bit unsigned integer that is to be
4806 * passed to the peer indicating the type of payload protocol data
4807 * being transmitted. This value is passed as opaque data by SCTP.
4808 *
4809 * The return value is the disposition.
4810 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004811sctp_disposition_t sctp_sf_do_prm_send(struct net *net,
4812 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08004814 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 void *arg,
4816 sctp_cmd_seq_t *commands)
4817{
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004818 struct sctp_datamsg *msg = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004820 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 return SCTP_DISPOSITION_CONSUME;
4822}
4823
4824/*
4825 * Process the SHUTDOWN primitive.
4826 *
4827 * Section: 10.1:
4828 * C) Shutdown
4829 *
4830 * Format: SHUTDOWN(association id)
4831 * -> result
4832 *
4833 * Gracefully closes an association. Any locally queued user data
4834 * will be delivered to the peer. The association will be terminated only
4835 * after the peer acknowledges all the SCTP packets sent. A success code
4836 * will be returned on successful termination of the association. If
4837 * attempting to terminate the association results in a failure, an error
4838 * code shall be returned.
4839 *
4840 * Mandatory attributes:
4841 *
4842 * o association id - local handle to the SCTP association
4843 *
4844 * Optional attributes:
4845 *
4846 * None.
4847 *
4848 * The return value is the disposition.
4849 */
4850sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004851 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 const struct sctp_endpoint *ep,
4853 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08004854 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 void *arg,
4856 sctp_cmd_seq_t *commands)
4857{
4858 int disposition;
4859
4860 /* From 9.2 Shutdown of an Association
4861 * Upon receipt of the SHUTDOWN primitive from its upper
4862 * layer, the endpoint enters SHUTDOWN-PENDING state and
4863 * remains there until all outstanding data has been
4864 * acknowledged by its peer. The endpoint accepts no new data
4865 * from its upper layer, but retransmits data to the far end
4866 * if necessary to fill gaps.
4867 */
4868 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4869 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4870
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 disposition = SCTP_DISPOSITION_CONSUME;
4872 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004873 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 arg, commands);
4875 }
4876 return disposition;
4877}
4878
4879/*
4880 * Process the ABORT primitive.
4881 *
4882 * Section: 10.1:
4883 * C) Abort
4884 *
4885 * Format: Abort(association id [, cause code])
4886 * -> result
4887 *
4888 * Ungracefully closes an association. Any locally queued user data
4889 * will be discarded and an ABORT chunk is sent to the peer. A success code
4890 * will be returned on successful abortion of the association. If
4891 * attempting to abort the association results in a failure, an error
4892 * code shall be returned.
4893 *
4894 * Mandatory attributes:
4895 *
4896 * o association id - local handle to the SCTP association
4897 *
4898 * Optional attributes:
4899 *
4900 * o cause code - reason of the abort to be passed to the peer
4901 *
4902 * None.
4903 *
4904 * The return value is the disposition.
4905 */
4906sctp_disposition_t sctp_sf_do_9_1_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004907 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 const struct sctp_endpoint *ep,
4909 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08004910 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 void *arg,
4912 sctp_cmd_seq_t *commands)
4913{
4914 /* From 9.1 Abort of an Association
4915 * Upon receipt of the ABORT primitive from its upper
4916 * layer, the endpoint enters CLOSED state and
4917 * discard all outstanding data has been
4918 * acknowledged by its peer. The endpoint accepts no new data
4919 * from its upper layer, but retransmits data to the far end
4920 * if necessary to fill gaps.
4921 */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004922 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923
Xin Long068d8bd2015-12-29 17:49:25 +08004924 if (abort)
4925 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926
4927 /* Even if we can't send the ABORT due to low memory delete the
4928 * TCB. This is a departure from our typical NOMEM handling.
4929 */
4930
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004931 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4932 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 /* Delete the established association. */
4934 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08004935 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004937 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4938 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02004940 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941}
4942
4943/* We tried an illegal operation on an association which is closed. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004944sctp_disposition_t sctp_sf_error_closed(struct net *net,
4945 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08004947 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 void *arg,
4949 sctp_cmd_seq_t *commands)
4950{
4951 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4952 return SCTP_DISPOSITION_CONSUME;
4953}
4954
4955/* We tried an illegal operation on an association which is shutting
4956 * down.
4957 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004958sctp_disposition_t sctp_sf_error_shutdown(struct net *net,
4959 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08004961 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 void *arg,
4963 sctp_cmd_seq_t *commands)
4964{
4965 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4966 SCTP_ERROR(-ESHUTDOWN));
4967 return SCTP_DISPOSITION_CONSUME;
4968}
4969
4970/*
4971 * sctp_cookie_wait_prm_shutdown
4972 *
4973 * Section: 4 Note: 2
4974 * Verification Tag:
4975 * Inputs
4976 * (endpoint, asoc)
4977 *
4978 * The RFC does not explicitly address this issue, but is the route through the
4979 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4980 *
4981 * Outputs
4982 * (timers)
4983 */
4984sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004985 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 const struct sctp_endpoint *ep,
4987 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08004988 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 void *arg,
4990 sctp_cmd_seq_t *commands)
4991{
4992 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4993 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4994
4995 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4996 SCTP_STATE(SCTP_STATE_CLOSED));
4997
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004998 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999
5000 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
5001
5002 return SCTP_DISPOSITION_DELETE_TCB;
5003}
5004
5005/*
5006 * sctp_cookie_echoed_prm_shutdown
5007 *
5008 * Section: 4 Note: 2
5009 * Verification Tag:
5010 * Inputs
5011 * (endpoint, asoc)
5012 *
5013 * The RFC does not explcitly address this issue, but is the route through the
5014 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
5015 *
5016 * Outputs
5017 * (timers)
5018 */
5019sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005020 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 const struct sctp_endpoint *ep,
5022 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08005023 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 void *arg, 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_shutdown(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030}
5031
5032/*
5033 * sctp_sf_cookie_wait_prm_abort
5034 *
5035 * Section: 4 Note: 2
5036 * Verification Tag:
5037 * Inputs
5038 * (endpoint, asoc)
5039 *
5040 * The RFC does not explicitly address this issue, but is the route through the
5041 * state table when someone issues an abort while in COOKIE_WAIT state.
5042 *
5043 * Outputs
5044 * (timers)
5045 */
5046sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005047 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 const struct sctp_endpoint *ep,
5049 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08005050 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 void *arg,
5052 sctp_cmd_seq_t *commands)
5053{
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07005054 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055
5056 /* Stop T1-init timer */
5057 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5058 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059
Xin Long068d8bd2015-12-29 17:49:25 +08005060 if (abort)
5061 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062
5063 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5064 SCTP_STATE(SCTP_STATE_CLOSED));
5065
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005066 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067
5068 /* Even if we can't send the ABORT due to low memory delete the
5069 * TCB. This is a departure from our typical NOMEM handling.
5070 */
5071
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005072 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5073 SCTP_ERROR(ECONNREFUSED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 /* Delete the established association. */
5075 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005076 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02005078 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079}
5080
5081/*
5082 * sctp_sf_cookie_echoed_prm_abort
5083 *
5084 * Section: 4 Note: 3
5085 * Verification Tag:
5086 * Inputs
5087 * (endpoint, asoc)
5088 *
5089 * The RFC does not explcitly address this issue, but is the route through the
5090 * state table when someone issues an abort while in COOKIE_ECHOED state.
5091 *
5092 * Outputs
5093 * (timers)
5094 */
5095sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005096 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 const struct sctp_endpoint *ep,
5098 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08005099 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 void *arg,
5101 sctp_cmd_seq_t *commands)
5102{
5103 /* There is a single T1 timer, so we should be able to use
5104 * common function with the COOKIE-WAIT state.
5105 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005106 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107}
5108
5109/*
5110 * sctp_sf_shutdown_pending_prm_abort
5111 *
5112 * Inputs
5113 * (endpoint, asoc)
5114 *
5115 * The RFC does not explicitly address this issue, but is the route through the
5116 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5117 *
5118 * Outputs
5119 * (timers)
5120 */
5121sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005122 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 const struct sctp_endpoint *ep,
5124 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08005125 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 void *arg,
5127 sctp_cmd_seq_t *commands)
5128{
5129 /* Stop the T5-shutdown guard timer. */
5130 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5131 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5132
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005133 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134}
5135
5136/*
5137 * sctp_sf_shutdown_sent_prm_abort
5138 *
5139 * Inputs
5140 * (endpoint, asoc)
5141 *
5142 * The RFC does not explicitly address this issue, but is the route through the
5143 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5144 *
5145 * Outputs
5146 * (timers)
5147 */
5148sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005149 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 const struct sctp_endpoint *ep,
5151 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08005152 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 void *arg,
5154 sctp_cmd_seq_t *commands)
5155{
5156 /* Stop the T2-shutdown timer. */
5157 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5158 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5159
5160 /* Stop the T5-shutdown guard timer. */
5161 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5162 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5163
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005164 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165}
5166
5167/*
5168 * sctp_sf_cookie_echoed_prm_abort
5169 *
5170 * Inputs
5171 * (endpoint, asoc)
5172 *
5173 * The RFC does not explcitly address this issue, but is the route through the
5174 * state table when someone issues an abort while in COOKIE_ECHOED state.
5175 *
5176 * Outputs
5177 * (timers)
5178 */
5179sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005180 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 const struct sctp_endpoint *ep,
5182 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08005183 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 void *arg,
5185 sctp_cmd_seq_t *commands)
5186{
5187 /* The same T2 timer, so we should be able to use
5188 * common function with the SHUTDOWN-SENT state.
5189 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005190 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191}
5192
5193/*
5194 * Process the REQUESTHEARTBEAT primitive
5195 *
5196 * 10.1 ULP-to-SCTP
5197 * J) Request Heartbeat
5198 *
5199 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5200 *
5201 * -> result
5202 *
5203 * Instructs the local endpoint to perform a HeartBeat on the specified
5204 * destination transport address of the given association. The returned
5205 * result should indicate whether the transmission of the HEARTBEAT
5206 * chunk to the destination address is successful.
5207 *
5208 * Mandatory attributes:
5209 *
5210 * o association id - local handle to the SCTP association
5211 *
5212 * o destination transport address - the transport address of the
5213 * association on which a heartbeat should be issued.
5214 */
5215sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005216 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 const struct sctp_endpoint *ep,
5218 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08005219 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 void *arg,
5221 sctp_cmd_seq_t *commands)
5222{
Vlad Yasevichfb785252007-03-19 17:02:03 -07005223 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5224 (struct sctp_transport *)arg, commands))
5225 return SCTP_DISPOSITION_NOMEM;
5226
5227 /*
5228 * RFC 2960 (bis), section 8.3
5229 *
5230 * D) Request an on-demand HEARTBEAT on a specific destination
5231 * transport address of a given association.
5232 *
5233 * The endpoint should increment the respective error counter of
5234 * the destination transport address each time a HEARTBEAT is sent
5235 * to that address and not acknowledged within one RTO.
5236 *
5237 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00005238 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Vlad Yasevichfb785252007-03-19 17:02:03 -07005239 SCTP_TRANSPORT(arg));
5240 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241}
5242
5243/*
5244 * ADDIP Section 4.1 ASCONF Chunk Procedures
5245 * When an endpoint has an ASCONF signaled change to be sent to the
5246 * remote endpoint it should do A1 to A9
5247 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005248sctp_disposition_t sctp_sf_do_prm_asconf(struct net *net,
5249 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08005251 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 void *arg,
5253 sctp_cmd_seq_t *commands)
5254{
5255 struct sctp_chunk *chunk = arg;
5256
5257 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5258 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5259 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5260 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5261 return SCTP_DISPOSITION_CONSUME;
5262}
5263
Xin Long7a090b02017-01-18 00:44:44 +08005264/* RE-CONFIG Section 5.1 RECONF Chunk Procedures */
5265sctp_disposition_t sctp_sf_do_prm_reconf(struct net *net,
5266 const struct sctp_endpoint *ep,
5267 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08005268 const union sctp_subtype type,
Xin Long7a090b02017-01-18 00:44:44 +08005269 void *arg, sctp_cmd_seq_t *commands)
5270{
5271 struct sctp_chunk *chunk = arg;
5272
5273 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5274 return SCTP_DISPOSITION_CONSUME;
5275}
5276
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277/*
5278 * Ignore the primitive event
5279 *
5280 * The return value is the disposition of the primitive.
5281 */
5282sctp_disposition_t sctp_sf_ignore_primitive(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005283 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 const struct sctp_endpoint *ep,
5285 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08005286 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 void *arg,
5288 sctp_cmd_seq_t *commands)
5289{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005290 pr_debug("%s: primitive type:%d is ignored\n", __func__,
5291 type.primitive);
5292
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 return SCTP_DISPOSITION_DISCARD;
5294}
5295
5296/***************************************************************************
5297 * These are the state functions for the OTHER events.
5298 ***************************************************************************/
5299
5300/*
Wei Yongjune1cdd552011-04-17 17:29:03 +00005301 * When the SCTP stack has no more user data to send or retransmit, this
5302 * notification is given to the user. Also, at the time when a user app
5303 * subscribes to this event, if there is no data to be sent or
5304 * retransmit, the stack will immediately send up this notification.
5305 */
5306sctp_disposition_t sctp_sf_do_no_pending_tsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005307 struct net *net,
Wei Yongjune1cdd552011-04-17 17:29:03 +00005308 const struct sctp_endpoint *ep,
5309 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08005310 const union sctp_subtype type,
Wei Yongjune1cdd552011-04-17 17:29:03 +00005311 void *arg,
5312 sctp_cmd_seq_t *commands)
5313{
5314 struct sctp_ulpevent *event;
5315
5316 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5317 if (!event)
5318 return SCTP_DISPOSITION_NOMEM;
5319
5320 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5321
5322 return SCTP_DISPOSITION_CONSUME;
5323}
5324
5325/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 * Start the shutdown negotiation.
5327 *
5328 * From Section 9.2:
5329 * Once all its outstanding data has been acknowledged, the endpoint
5330 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5331 * TSN Ack field the last sequential TSN it has received from the peer.
5332 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5333 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5334 * with the updated last sequential TSN received from its peer.
5335 *
5336 * The return value is the disposition.
5337 */
5338sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005339 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 const struct sctp_endpoint *ep,
5341 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08005342 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 void *arg,
5344 sctp_cmd_seq_t *commands)
5345{
5346 struct sctp_chunk *reply;
5347
5348 /* Once all its outstanding data has been acknowledged, the
5349 * endpoint shall send a SHUTDOWN chunk to its peer including
5350 * in the Cumulative TSN Ack field the last sequential TSN it
5351 * has received from the peer.
5352 */
5353 reply = sctp_make_shutdown(asoc, NULL);
5354 if (!reply)
5355 goto nomem;
5356
5357 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5358 * T2-shutdown timer.
5359 */
5360 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5361
5362 /* It shall then start the T2-shutdown timer */
5363 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5364 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5365
Wei Yongjun536428a2008-09-05 08:55:26 +08005366 /* RFC 4960 Section 9.2
5367 * The sender of the SHUTDOWN MAY also start an overall guard timer
5368 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5369 */
Thomas Graff8d96052011-07-07 00:28:35 +00005370 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
Wei Yongjun536428a2008-09-05 08:55:26 +08005371 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5372
Neil Horman9f70f462013-12-10 06:48:15 -05005373 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5375 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5376
5377 /* and enter the SHUTDOWN-SENT state. */
5378 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5379 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5380
5381 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5382 *
5383 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005384 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 */
5386 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5387
5388 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5389
5390 return SCTP_DISPOSITION_CONSUME;
5391
5392nomem:
5393 return SCTP_DISPOSITION_NOMEM;
5394}
5395
5396/*
5397 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5398 *
5399 * From Section 9.2:
5400 *
5401 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5402 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5403 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5404 * endpoint must re-send the SHUTDOWN ACK.
5405 *
5406 * The return value is the disposition.
5407 */
5408sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005409 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 const struct sctp_endpoint *ep,
5411 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08005412 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 void *arg,
5414 sctp_cmd_seq_t *commands)
5415{
5416 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5417 struct sctp_chunk *reply;
5418
5419 /* There are 2 ways of getting here:
5420 * 1) called in response to a SHUTDOWN chunk
5421 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5422 *
5423 * For the case (2), the arg parameter is set to NULL. We need
5424 * to check that we have a chunk before accessing it's fields.
5425 */
5426 if (chunk) {
5427 if (!sctp_vtag_verify(chunk, asoc))
Xin Longac23e682017-08-03 15:42:10 +08005428 return sctp_sf_pdiscard(net, ep, asoc, type, arg,
5429 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430
5431 /* Make sure that the SHUTDOWN chunk has a valid length. */
Xin Longac23e682017-08-03 15:42:10 +08005432 if (!sctp_chunk_length_valid(
5433 chunk, sizeof(struct sctp_shutdown_chunk)))
5434 return sctp_sf_violation_chunklen(net, ep, asoc, type,
5435 arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 }
5437
5438 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5439 * shall send a SHUTDOWN ACK ...
5440 */
5441 reply = sctp_make_shutdown_ack(asoc, chunk);
5442 if (!reply)
5443 goto nomem;
5444
5445 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5446 * the T2-shutdown timer.
5447 */
5448 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5449
5450 /* and start/restart a T2-shutdown timer of its own, */
5451 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5452 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5453
Neil Horman9f70f462013-12-10 06:48:15 -05005454 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5456 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5457
5458 /* Enter the SHUTDOWN-ACK-SENT state. */
5459 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5460 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5461
5462 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5463 *
5464 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005465 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 */
5467 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5468
5469 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5470
5471 return SCTP_DISPOSITION_CONSUME;
5472
5473nomem:
5474 return SCTP_DISPOSITION_NOMEM;
5475}
5476
5477/*
5478 * Ignore the event defined as other
5479 *
5480 * The return value is the disposition of the event.
5481 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005482sctp_disposition_t sctp_sf_ignore_other(struct net *net,
5483 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08005485 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 void *arg,
5487 sctp_cmd_seq_t *commands)
5488{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005489 pr_debug("%s: the event other type:%d is ignored\n",
5490 __func__, type.other);
5491
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 return SCTP_DISPOSITION_DISCARD;
5493}
5494
5495/************************************************************
5496 * These are the state functions for handling timeout events.
5497 ************************************************************/
5498
5499/*
5500 * RTX Timeout
5501 *
5502 * Section: 6.3.3 Handle T3-rtx Expiration
5503 *
5504 * Whenever the retransmission timer T3-rtx expires for a destination
5505 * address, do the following:
5506 * [See below]
5507 *
5508 * The return value is the disposition of the chunk.
5509 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005510sctp_disposition_t sctp_sf_do_6_3_3_rtx(struct net *net,
5511 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08005513 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 void *arg,
5515 sctp_cmd_seq_t *commands)
5516{
5517 struct sctp_transport *transport = arg;
5518
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005519 SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005520
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 if (asoc->overall_error_count >= asoc->max_retrans) {
lucien8a0d19c2015-12-05 15:35:36 +08005522 if (asoc->peer.zero_window_announced &&
5523 asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
Thomas Graff8d96052011-07-07 00:28:35 +00005524 /*
5525 * We are here likely because the receiver had its rwnd
5526 * closed for a while and we have not been able to
5527 * transmit the locally queued data within the maximum
5528 * retransmission attempts limit. Start the T5
5529 * shutdown guard timer to give the receiver one last
5530 * chance and some additional time to recover before
5531 * aborting.
5532 */
5533 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5534 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5535 } else {
5536 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5537 SCTP_ERROR(ETIMEDOUT));
5538 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5539 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5540 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005541 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5542 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Thomas Graff8d96052011-07-07 00:28:35 +00005543 return SCTP_DISPOSITION_DELETE_TCB;
5544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 }
5546
5547 /* E1) For the destination address for which the timer
5548 * expires, adjust its ssthresh with rules defined in Section
5549 * 7.2.3 and set the cwnd <- MTU.
5550 */
5551
5552 /* E2) For the destination address for which the timer
5553 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5554 * maximum value discussed in rule C7 above (RTO.max) may be
5555 * used to provide an upper bound to this doubling operation.
5556 */
5557
5558 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5559 * outstanding DATA chunks for the address for which the
5560 * T3-rtx has expired will fit into a single packet, subject
5561 * to the MTU constraint for the path corresponding to the
5562 * destination transport address to which the retransmission
5563 * is being sent (this may be different from the address for
5564 * which the timer expires [see Section 6.4]). Call this
5565 * value K. Bundle and retransmit those K DATA chunks in a
5566 * single packet to the destination endpoint.
5567 *
5568 * Note: Any DATA chunks that were sent to the address for
5569 * which the T3-rtx timer expired but did not fit in one MTU
5570 * (rule E3 above), should be marked for retransmission and
5571 * sent as soon as cwnd allows (normally when a SACK arrives).
5572 */
5573
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 /* Do some failure management (Section 8.2). */
5575 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5576
Vlad Yasevich1845a572007-02-21 02:06:19 -08005577 /* NB: Rules E4 and F1 are implicit in R1. */
5578 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5579
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 return SCTP_DISPOSITION_CONSUME;
5581}
5582
5583/*
5584 * Generate delayed SACK on timeout
5585 *
5586 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5587 *
5588 * The guidelines on delayed acknowledgement algorithm specified in
5589 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5590 * acknowledgement SHOULD be generated for at least every second packet
5591 * (not every second DATA chunk) received, and SHOULD be generated
5592 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5593 * some situations it may be beneficial for an SCTP transmitter to be
5594 * more conservative than the algorithms detailed in this document
5595 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5596 * the following algorithms allow.
5597 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005598sctp_disposition_t sctp_sf_do_6_2_sack(struct net *net,
5599 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08005601 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 void *arg,
5603 sctp_cmd_seq_t *commands)
5604{
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005605 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5607 return SCTP_DISPOSITION_CONSUME;
5608}
5609
5610/*
Frank Filz3f7a87d2005-06-20 13:14:57 -07005611 * sctp_sf_t1_init_timer_expire
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 *
5613 * Section: 4 Note: 2
5614 * Verification Tag:
5615 * Inputs
5616 * (endpoint, asoc)
5617 *
5618 * RFC 2960 Section 4 Notes
5619 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5620 * and re-start the T1-init timer without changing state. This MUST
5621 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5622 * endpoint MUST abort the initialization process and report the
5623 * error to SCTP user.
5624 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07005625 * Outputs
5626 * (timers, events)
5627 *
5628 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005629sctp_disposition_t sctp_sf_t1_init_timer_expire(struct net *net,
5630 const struct sctp_endpoint *ep,
Frank Filz3f7a87d2005-06-20 13:14:57 -07005631 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08005632 const union sctp_subtype type,
Frank Filz3f7a87d2005-06-20 13:14:57 -07005633 void *arg,
5634 sctp_cmd_seq_t *commands)
5635{
5636 struct sctp_chunk *repl = NULL;
5637 struct sctp_bind_addr *bp;
5638 int attempts = asoc->init_err_counter + 1;
5639
Daniel Borkmannbb333812013-06-28 19:49:40 +02005640 pr_debug("%s: timer T1 expired (INIT)\n", __func__);
5641
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005642 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005643
Vlad Yasevich81845c22006-01-30 15:59:54 -08005644 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005645 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5646 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5647 if (!repl)
5648 return SCTP_DISPOSITION_NOMEM;
5649
5650 /* Choose transport for INIT. */
5651 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5652 SCTP_CHUNK(repl));
5653
5654 /* Issue a sideeffect to do the needed accounting. */
5655 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5656 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5657
5658 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5659 } else {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005660 pr_debug("%s: giving up on INIT, attempts:%d "
5661 "max_init_attempts:%d\n", __func__, attempts,
5662 asoc->max_init_attempts);
5663
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005664 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5665 SCTP_ERROR(ETIMEDOUT));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005666 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005667 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005668 return SCTP_DISPOSITION_DELETE_TCB;
5669 }
5670
5671 return SCTP_DISPOSITION_CONSUME;
5672}
5673
5674/*
5675 * sctp_sf_t1_cookie_timer_expire
5676 *
5677 * Section: 4 Note: 2
5678 * Verification Tag:
5679 * Inputs
5680 * (endpoint, asoc)
5681 *
5682 * RFC 2960 Section 4 Notes
5683 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 * COOKIE ECHO and re-start the T1-cookie timer without changing
5685 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5686 * After that, the endpoint MUST abort the initialization process and
5687 * report the error to SCTP user.
5688 *
5689 * Outputs
5690 * (timers, events)
5691 *
5692 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005693sctp_disposition_t sctp_sf_t1_cookie_timer_expire(struct net *net,
5694 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08005696 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 void *arg,
5698 sctp_cmd_seq_t *commands)
5699{
Frank Filz3f7a87d2005-06-20 13:14:57 -07005700 struct sctp_chunk *repl = NULL;
5701 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702
Daniel Borkmannbb333812013-06-28 19:49:40 +02005703 pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
5704
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005705 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706
Vlad Yasevich81845c22006-01-30 15:59:54 -08005707 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005708 repl = sctp_make_cookie_echo(asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 if (!repl)
Frank Filz3f7a87d2005-06-20 13:14:57 -07005710 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711
Vlad Yasevich96cd0d32008-09-08 14:00:26 -04005712 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5713 SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 /* Issue a sideeffect to do the needed accounting. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005715 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5716 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5717
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5719 } else {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005720 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5721 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005723 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 return SCTP_DISPOSITION_DELETE_TCB;
5725 }
5726
5727 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728}
5729
5730/* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5731 * with the updated last sequential TSN received from its peer.
5732 *
5733 * An endpoint should limit the number of retransmissions of the
5734 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5735 * If this threshold is exceeded the endpoint should destroy the TCB and
5736 * MUST report the peer endpoint unreachable to the upper layer (and
5737 * thus the association enters the CLOSED state). The reception of any
5738 * packet from its peer (i.e. as the peer sends all of its queued DATA
5739 * chunks) should clear the endpoint's retransmission count and restart
5740 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5741 * all of its queued DATA chunks that have not yet been sent.
5742 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005743sctp_disposition_t sctp_sf_t2_timer_expire(struct net *net,
5744 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08005746 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 void *arg,
5748 sctp_cmd_seq_t *commands)
5749{
5750 struct sctp_chunk *reply = NULL;
5751
Daniel Borkmannbb333812013-06-28 19:49:40 +02005752 pr_debug("%s: timer T2 expired\n", __func__);
5753
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005754 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005755
Neil Horman58fbbed2008-02-29 11:40:56 -08005756 ((struct sctp_association *)asoc)->shutdown_retries++;
5757
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005759 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5760 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5762 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005763 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005764 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5765 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 return SCTP_DISPOSITION_DELETE_TCB;
5767 }
5768
5769 switch (asoc->state) {
5770 case SCTP_STATE_SHUTDOWN_SENT:
5771 reply = sctp_make_shutdown(asoc, NULL);
5772 break;
5773
5774 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5775 reply = sctp_make_shutdown_ack(asoc, NULL);
5776 break;
5777
5778 default:
5779 BUG();
5780 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782
5783 if (!reply)
5784 goto nomem;
5785
Wei Yongjun6345b192009-04-26 23:13:35 +08005786 /* Do some failure management (Section 8.2).
5787 * If we remove the transport an SHUTDOWN was last sent to, don't
5788 * do failure management.
5789 */
5790 if (asoc->shutdown_last_sent_to)
5791 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5792 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793
5794 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5795 * the T2-shutdown timer.
5796 */
5797 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5798
5799 /* Restart the T2-shutdown timer. */
5800 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5801 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5802 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5803 return SCTP_DISPOSITION_CONSUME;
5804
5805nomem:
5806 return SCTP_DISPOSITION_NOMEM;
5807}
5808
5809/*
5810 * ADDIP Section 4.1 ASCONF CHunk Procedures
5811 * If the T4 RTO timer expires the endpoint should do B1 to B5
5812 */
5813sctp_disposition_t sctp_sf_t4_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005814 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815 const struct sctp_endpoint *ep,
5816 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08005817 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818 void *arg,
5819 sctp_cmd_seq_t *commands)
5820{
5821 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5822 struct sctp_transport *transport = chunk->transport;
5823
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005824 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005825
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5827 * detection on the appropriate destination address as defined in
5828 * RFC2960 [5] section 8.1 and 8.2.
5829 */
Wei Yongjun10a43ce2009-04-26 23:14:42 +08005830 if (transport)
5831 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5832 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833
5834 /* Reconfig T4 timer and transport. */
5835 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5836
5837 /* ADDIP 4.1 B2) Increment the association error counters and perform
5838 * endpoint failure detection on the association as defined in
5839 * RFC2960 [5] section 8.1 and 8.2.
5840 * association error counter is incremented in SCTP_CMD_STRIKE.
5841 */
5842 if (asoc->overall_error_count >= asoc->max_retrans) {
5843 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5844 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005845 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5846 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005848 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005849 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5850 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 return SCTP_DISPOSITION_ABORT;
5852 }
5853
5854 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5855 * the ASCONF chunk was sent by doubling the RTO timer value.
5856 * This is done in SCTP_CMD_STRIKE.
5857 */
5858
5859 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5860 * choose an alternate destination address (please refer to RFC2960
5861 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005862 * chunk, it MUST be the same (including its serial number) as the last
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 * ASCONF sent.
5864 */
5865 sctp_chunk_hold(asoc->addip_last_asconf);
5866 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5867 SCTP_CHUNK(asoc->addip_last_asconf));
5868
5869 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5870 * destination is selected, then the RTO used will be that of the new
5871 * destination address.
5872 */
5873 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5874 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5875
5876 return SCTP_DISPOSITION_CONSUME;
5877}
5878
5879/* sctpimpguide-05 Section 2.12.2
5880 * The sender of the SHUTDOWN MAY also start an overall guard timer
5881 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5882 * At the expiration of this timer the sender SHOULD abort the association
5883 * by sending an ABORT chunk.
5884 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005885sctp_disposition_t sctp_sf_t5_timer_expire(struct net *net,
5886 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08005888 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 void *arg,
5890 sctp_cmd_seq_t *commands)
5891{
5892 struct sctp_chunk *reply = NULL;
5893
Daniel Borkmannbb333812013-06-28 19:49:40 +02005894 pr_debug("%s: timer T5 expired\n", __func__);
5895
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005896 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897
5898 reply = sctp_make_abort(asoc, NULL, 0);
5899 if (!reply)
5900 goto nomem;
5901
5902 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005903 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5904 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005906 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005908 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5909 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevicha1080a82008-10-09 14:33:26 -07005910
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 return SCTP_DISPOSITION_DELETE_TCB;
5912nomem:
5913 return SCTP_DISPOSITION_NOMEM;
5914}
5915
5916/* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5917 * the association is automatically closed by starting the shutdown process.
5918 * The work that needs to be done is same as when SHUTDOWN is initiated by
5919 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5920 */
5921sctp_disposition_t sctp_sf_autoclose_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005922 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 const struct sctp_endpoint *ep,
5924 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08005925 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926 void *arg,
5927 sctp_cmd_seq_t *commands)
5928{
5929 int disposition;
5930
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005931 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005932
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 /* From 9.2 Shutdown of an Association
5934 * Upon receipt of the SHUTDOWN primitive from its upper
5935 * layer, the endpoint enters SHUTDOWN-PENDING state and
5936 * remains there until all outstanding data has been
5937 * acknowledged by its peer. The endpoint accepts no new data
5938 * from its upper layer, but retransmits data to the far end
5939 * if necessary to fill gaps.
5940 */
5941 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5942 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5943
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 disposition = SCTP_DISPOSITION_CONSUME;
5945 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005946 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 arg, commands);
5948 }
5949 return disposition;
5950}
5951
5952/*****************************************************************************
5953 * These are sa state functions which could apply to all types of events.
5954 ****************************************************************************/
5955
5956/*
5957 * This table entry is not implemented.
5958 *
5959 * Inputs
5960 * (endpoint, asoc, chunk)
5961 *
5962 * The return value is the disposition of the chunk.
5963 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005964sctp_disposition_t sctp_sf_not_impl(struct net *net,
5965 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08005967 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 void *arg,
5969 sctp_cmd_seq_t *commands)
5970{
5971 return SCTP_DISPOSITION_NOT_IMPL;
5972}
5973
5974/*
5975 * This table entry represents a bug.
5976 *
5977 * Inputs
5978 * (endpoint, asoc, chunk)
5979 *
5980 * The return value is the disposition of the chunk.
5981 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005982sctp_disposition_t sctp_sf_bug(struct net *net,
5983 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08005985 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 void *arg,
5987 sctp_cmd_seq_t *commands)
5988{
5989 return SCTP_DISPOSITION_BUG;
5990}
5991
5992/*
5993 * This table entry represents the firing of a timer in the wrong state.
5994 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5995 * when the association is in the wrong state. This event should
5996 * be ignored, so as to prevent any rearming of the timer.
5997 *
5998 * Inputs
5999 * (endpoint, asoc, chunk)
6000 *
6001 * The return value is the disposition of the chunk.
6002 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006003sctp_disposition_t sctp_sf_timer_ignore(struct net *net,
6004 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08006006 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007 void *arg,
6008 sctp_cmd_seq_t *commands)
6009{
Daniel Borkmannbb333812013-06-28 19:49:40 +02006010 pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
6011
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 return SCTP_DISPOSITION_CONSUME;
6013}
6014
6015/********************************************************************
6016 * 2nd Level Abstractions
6017 ********************************************************************/
6018
6019/* Pull the SACK chunk based on the SACK header. */
6020static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
6021{
6022 struct sctp_sackhdr *sack;
6023 unsigned int len;
6024 __u16 num_blocks;
6025 __u16 num_dup_tsns;
6026
6027 /* Protect ourselves from reading too far into
6028 * the skb from a bogus sender.
6029 */
6030 sack = (struct sctp_sackhdr *) chunk->skb->data;
6031
6032 num_blocks = ntohs(sack->num_gap_ack_blocks);
6033 num_dup_tsns = ntohs(sack->num_dup_tsns);
6034 len = sizeof(struct sctp_sackhdr);
6035 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
6036 if (len > chunk->skb->len)
6037 return NULL;
6038
6039 skb_pull(chunk->skb, len);
6040
6041 return sack;
6042}
6043
6044/* Create an ABORT packet to be sent as a response, with the specified
6045 * error causes.
6046 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006047static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
6048 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 const struct sctp_association *asoc,
6050 struct sctp_chunk *chunk,
6051 const void *payload,
6052 size_t paylen)
6053{
6054 struct sctp_packet *packet;
6055 struct sctp_chunk *abort;
6056
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006057 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058
6059 if (packet) {
6060 /* Make an ABORT.
6061 * The T bit will be set if the asoc is NULL.
6062 */
6063 abort = sctp_make_abort(asoc, chunk, paylen);
6064 if (!abort) {
6065 sctp_ootb_pkt_free(packet);
6066 return NULL;
6067 }
Jerome Forissier047a2422005-04-28 11:58:43 -07006068
6069 /* Reflect vtag if T-Bit is set */
6070 if (sctp_test_T_bit(abort))
6071 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
6072
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 /* Add specified error causes, i.e., payload, to the
6074 * end of the chunk.
6075 */
6076 sctp_addto_chunk(abort, paylen, payload);
6077
6078 /* Set the skb to the belonging sock for accounting. */
6079 abort->skb->sk = ep->base.sk;
6080
6081 sctp_packet_append_chunk(packet, abort);
6082
6083 }
6084
6085 return packet;
6086}
6087
6088/* Allocate a packet for responding in the OOTB conditions. */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006089static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
6090 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 const struct sctp_chunk *chunk)
6092{
6093 struct sctp_packet *packet;
6094 struct sctp_transport *transport;
6095 __u16 sport;
6096 __u16 dport;
6097 __u32 vtag;
6098
6099 /* Get the source and destination port from the inbound packet. */
6100 sport = ntohs(chunk->sctp_hdr->dest);
6101 dport = ntohs(chunk->sctp_hdr->source);
6102
6103 /* The V-tag is going to be the same as the inbound packet if no
6104 * association exists, otherwise, use the peer's vtag.
6105 */
6106 if (asoc) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08006107 /* Special case the INIT-ACK as there is no peer's vtag
6108 * yet.
6109 */
wangweidongcb3f8372013-12-23 12:16:50 +08006110 switch (chunk->chunk_hdr->type) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08006111 case SCTP_CID_INIT_ACK:
6112 {
Xin Longcb1844c2017-07-23 09:34:26 +08006113 struct sctp_initack_chunk *initack;
Wei Yongjun02c4e122007-08-31 10:03:58 +08006114
Xin Longcb1844c2017-07-23 09:34:26 +08006115 initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
Wei Yongjun02c4e122007-08-31 10:03:58 +08006116 vtag = ntohl(initack->init_hdr.init_tag);
6117 break;
6118 }
6119 default:
6120 vtag = asoc->peer.i.init_tag;
6121 break;
6122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123 } else {
6124 /* Special case the INIT and stale COOKIE_ECHO as there is no
6125 * vtag yet.
6126 */
wangweidongcb3f8372013-12-23 12:16:50 +08006127 switch (chunk->chunk_hdr->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 case SCTP_CID_INIT:
6129 {
Xin Long01a992b2017-06-30 11:52:22 +08006130 struct sctp_init_chunk *init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131
Xin Long01a992b2017-06-30 11:52:22 +08006132 init = (struct sctp_init_chunk *)chunk->chunk_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 vtag = ntohl(init->init_hdr.init_tag);
6134 break;
6135 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006136 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 vtag = ntohl(chunk->sctp_hdr->vtag);
6138 break;
6139 }
6140 }
6141
6142 /* Make a transport for the bucket, Eliza... */
Eric W. Biederman89bf3452012-08-07 07:26:14 +00006143 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144 if (!transport)
6145 goto nomem;
6146
6147 /* Cache a route for the transport with the chunk's destination as
6148 * the source address.
6149 */
Al Viro16b0a032006-11-20 17:13:38 -08006150 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006151 sctp_sk(net->sctp.ctl_sock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152
Marcelo Ricardo Leitner66b91d22016-12-28 09:26:34 -02006153 packet = &transport->packet;
6154 sctp_packet_init(packet, transport, sport, dport);
6155 sctp_packet_config(packet, vtag, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156
6157 return packet;
6158
6159nomem:
6160 return NULL;
6161}
6162
6163/* Free the packet allocated earlier for responding in the OOTB condition. */
6164void sctp_ootb_pkt_free(struct sctp_packet *packet)
6165{
6166 sctp_transport_free(packet->transport);
6167}
6168
6169/* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006170static void sctp_send_stale_cookie_err(struct net *net,
6171 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172 const struct sctp_association *asoc,
6173 const struct sctp_chunk *chunk,
6174 sctp_cmd_seq_t *commands,
6175 struct sctp_chunk *err_chunk)
6176{
6177 struct sctp_packet *packet;
6178
6179 if (err_chunk) {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006180 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181 if (packet) {
6182 struct sctp_signed_cookie *cookie;
6183
6184 /* Override the OOTB vtag from the cookie. */
6185 cookie = chunk->subh.cookie_hdr;
6186 packet->vtag = cookie->c.peer_vtag;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006187
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 /* Set the skb to the belonging sock for accounting. */
6189 err_chunk->skb->sk = ep->base.sk;
6190 sctp_packet_append_chunk(packet, err_chunk);
6191 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6192 SCTP_PACKET(packet));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006193 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194 } else
6195 sctp_chunk_free (err_chunk);
6196 }
6197}
6198
6199
6200/* Process a data chunk */
6201static int sctp_eat_data(const struct sctp_association *asoc,
6202 struct sctp_chunk *chunk,
6203 sctp_cmd_seq_t *commands)
6204{
Xin Long3583df12017-06-30 11:52:19 +08006205 struct sctp_datahdr *data_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206 struct sctp_chunk *err;
6207 size_t datalen;
6208 sctp_verb_t deliver;
6209 int tmp;
6210 __u32 tsn;
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006211 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
Neil Horman049b3ff2005-11-11 16:08:24 -08006212 struct sock *sk = asoc->base.sk;
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006213 struct net *net = sock_net(sk);
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006214 u16 ssn;
6215 u16 sid;
6216 u8 ordered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217
Xin Long3583df12017-06-30 11:52:19 +08006218 data_hdr = (struct sctp_datahdr *)chunk->skb->data;
6219 chunk->subh.data_hdr = data_hdr;
6220 skb_pull(chunk->skb, sizeof(*data_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221
6222 tsn = ntohl(data_hdr->tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02006223 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224
6225 /* ASSERT: Now skb->data is really the user data. */
6226
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227 /* Process ECN based congestion.
6228 *
6229 * Since the chunk structure is reused for all chunks within
6230 * a packet, we use ecn_ce_done to track if we've already
6231 * done CE processing for this packet.
6232 *
6233 * We need to do ECN processing even if we plan to discard the
6234 * chunk later.
6235 */
6236
Marcelo Ricardo Leitner2d47fd12016-07-13 15:09:00 -03006237 if (asoc->peer.ecn_capable && !chunk->ecn_ce_done) {
Marcelo Ricardo Leitnere7487c82016-07-13 15:08:58 -03006238 struct sctp_af *af = SCTP_INPUT_CB(chunk->skb)->af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 chunk->ecn_ce_done = 1;
6240
Marcelo Ricardo Leitner2d47fd12016-07-13 15:09:00 -03006241 if (af->is_ce(sctp_gso_headskb(chunk->skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242 /* Do real work as sideffect. */
6243 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6244 SCTP_U32(tsn));
6245 }
6246 }
6247
6248 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6249 if (tmp < 0) {
6250 /* The TSN is too high--silently discard the chunk and
6251 * count on it getting retransmitted later.
6252 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006253 if (chunk->asoc)
6254 chunk->asoc->stats.outofseqtsns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255 return SCTP_IERROR_HIGH_TSN;
6256 } else if (tmp > 0) {
6257 /* This is a duplicate. Record it. */
6258 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6259 return SCTP_IERROR_DUP_TSN;
6260 }
6261
6262 /* This is a new TSN. */
6263
6264 /* Discard if there is no room in the receive window.
6265 * Actually, allow a little bit of overflow (up to a MTU).
6266 */
6267 datalen = ntohs(chunk->chunk_hdr->length);
Xin Long9f8d3142017-06-30 11:52:20 +08006268 datalen -= sizeof(struct sctp_data_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269
6270 deliver = SCTP_CMD_CHUNK_ULP;
6271
6272 /* Think about partial delivery. */
6273 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6274
6275 /* Even if we don't accept this chunk there is
6276 * memory pressure.
6277 */
6278 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6279 }
6280
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006281 /* Spill over rwnd a little bit. Note: While allowed, this spill over
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282 * seems a bit troublesome in that frag_point varies based on
6283 * PMTU. In cases, such as loopback, this might be a rather
6284 * large spill over.
6285 */
Daniel Borkmann362d5202014-04-14 21:45:17 +02006286 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
Neil Horman4d93df02007-08-15 16:07:44 -07006287 (datalen > asoc->rwnd + asoc->frag_point))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288
6289 /* If this is the next TSN, consider reneging to make
6290 * room. Note: Playing nice with a confused sender. A
6291 * malicious sender can still eat up all our buffer
6292 * space and in the future we may want to detect and
6293 * do more drastic reneging.
6294 */
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006295 if (sctp_tsnmap_has_gap(map) &&
6296 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006297 pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298 deliver = SCTP_CMD_RENEGE;
6299 } else {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006300 pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
6301 __func__, tsn, datalen, asoc->rwnd);
6302
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303 return SCTP_IERROR_IGNORE_TSN;
6304 }
6305 }
6306
6307 /*
Neil Horman4d93df02007-08-15 16:07:44 -07006308 * Also try to renege to limit our memory usage in the event that
6309 * we are under memory pressure
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006310 * If we can't renege, don't worry about it, the sk_rmem_schedule
Neil Horman4d93df02007-08-15 16:07:44 -07006311 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6312 * memory usage too much
6313 */
6314 if (*sk->sk_prot_creator->memory_pressure) {
6315 if (sctp_tsnmap_has_gap(map) &&
wangweidongf7010e62013-12-23 12:16:52 +08006316 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006317 pr_debug("%s: under pressure, reneging for tsn:%u\n",
6318 __func__, tsn);
Neil Horman4d93df02007-08-15 16:07:44 -07006319 deliver = SCTP_CMD_RENEGE;
6320 }
6321 }
6322
6323 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324 * Section 3.3.10.9 No User Data (9)
6325 *
6326 * Cause of error
6327 * ---------------
6328 * No User Data: This error cause is returned to the originator of a
6329 * DATA chunk if a received DATA chunk has no user data.
6330 */
6331 if (unlikely(0 == datalen)) {
6332 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6333 if (err) {
6334 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6335 SCTP_CHUNK(err));
6336 }
6337 /* We are going to ABORT, so we might as well stop
6338 * processing the rest of the chunks in the packet.
6339 */
wangweidongcb3f8372013-12-23 12:16:50 +08006340 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07006341 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6342 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08006344 SCTP_PERR(SCTP_ERROR_NO_DATA));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006345 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6346 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347 return SCTP_IERROR_NO_DATA;
6348 }
6349
Sridhar Samudrala9faa7302006-07-21 14:49:07 -07006350 chunk->data_accepted = 1;
6351
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6353 * if we renege and the chunk arrives again.
6354 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006355 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006356 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006357 if (chunk->asoc)
6358 chunk->asoc->stats.iuodchunks++;
6359 } else {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006360 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006361 if (chunk->asoc)
6362 chunk->asoc->stats.iodchunks++;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006363 ordered = 1;
6364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365
6366 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6367 *
6368 * If an endpoint receive a DATA chunk with an invalid stream
6369 * identifier, it shall acknowledge the reception of the DATA chunk
6370 * following the normal procedure, immediately send an ERROR chunk
6371 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6372 * and discard the DATA chunk.
6373 */
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006374 sid = ntohs(data_hdr->stream);
Xin Longcee360a2017-05-31 16:36:31 +08006375 if (sid >= asoc->stream.incnt) {
Vlad Yasevich3888e9e2008-07-08 02:28:39 -07006376 /* Mark tsn as received even though we drop it */
6377 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6378
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6380 &data_hdr->stream,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05006381 sizeof(data_hdr->stream),
6382 sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383 if (err)
6384 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6385 SCTP_CHUNK(err));
6386 return SCTP_IERROR_BAD_STREAM;
6387 }
6388
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006389 /* Check to see if the SSN is possible for this TSN.
6390 * The biggest gap we can record is 4K wide. Since SSNs wrap
6391 * at an unsigned short, there is no way that an SSN can
6392 * wrap and for a valid TSN. We can simply check if the current
6393 * SSN is smaller then the next expected one. If it is, it wrapped
6394 * and is invalid.
6395 */
6396 ssn = ntohs(data_hdr->ssn);
Xin Longcee360a2017-05-31 16:36:31 +08006397 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->stream, in, sid)))
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006398 return SCTP_IERROR_PROTO_VIOLATION;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006399
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400 /* Send the data up to the user. Note: Schedule the
6401 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6402 * chunk needs the updated rwnd.
6403 */
6404 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6405
6406 return SCTP_IERROR_NO_ERROR;
6407}