blob: 32ac90b22654470930560a75032821d8c97dfd6e [file] [log] [blame]
Vlad Yasevich60c778b2008-01-11 09:57:09 -05001/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2002 Intel Corp.
6 * Copyright (c) 2002 Nokia Corp.
7 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05008 * This is part of the SCTP Linux Kernel Implementation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * These are the state functions for the state machine.
11 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050012 * This SCTP implementation is free software;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * you can redistribute it and/or modify it under the terms of
14 * the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
16 * any later version.
17 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050018 * This SCTP implementation is distributed in the hope that it
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
20 * ************************
21 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 * See the GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
Jeff Kirsher4b2f13a2013-12-06 06:28:48 -080025 * along with GNU CC; see the file COPYING. If not, see
26 * <http://www.gnu.org/licenses/>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 *
28 * Please send any bug reports or fixes you make to the
29 * email address(es):
Daniel Borkmann91705c62013-07-23 14:51:47 +020030 * lksctp developers <linux-sctp@vger.kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 * Written or modified by:
33 * La Monte H.P. Yarroll <piggy@acm.org>
34 * Karl Knutson <karl@athena.chicago.il.us>
35 * Mathew Kotowsky <kotowsky@sctp.org>
36 * Sridhar Samudrala <samudrala@us.ibm.com>
37 * Jon Grimm <jgrimm@us.ibm.com>
38 * Hui Huang <hui.huang@nokia.com>
39 * Dajiang Zhang <dajiang.zhang@nokia.com>
40 * Daisy Chang <daisyc@us.ibm.com>
41 * Ardelle Fan <ardelle.fan@intel.com>
42 * Ryan Layer <rmlayer@us.ibm.com>
43 * Kevin Gao <kevin.gao@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 */
45
Joe Perches145ce502010-08-24 13:21:08 +000046#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/types.h>
49#include <linux/kernel.h>
50#include <linux/ip.h>
51#include <linux/ipv6.h>
52#include <linux/net.h>
53#include <linux/inet.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/sock.h>
56#include <net/inet_ecn.h>
57#include <linux/skbuff.h>
58#include <net/sctp/sctp.h>
59#include <net/sctp/sm.h>
60#include <net/sctp/structs.h>
61
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000062static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
63 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 const struct sctp_association *asoc,
65 struct sctp_chunk *chunk,
66 const void *payload,
67 size_t paylen);
68static int sctp_eat_data(const struct sctp_association *asoc,
69 struct sctp_chunk *chunk,
70 sctp_cmd_seq_t *commands);
Eric W. Biederman2ce95502012-08-06 08:43:06 +000071static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
72 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 const struct sctp_chunk *chunk);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000074static void sctp_send_stale_cookie_err(struct net *net,
75 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 const struct sctp_association *asoc,
77 const struct sctp_chunk *chunk,
78 sctp_cmd_seq_t *commands,
79 struct sctp_chunk *err_chunk);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000080static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
81 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 const struct sctp_association *asoc,
83 const sctp_subtype_t type,
84 void *arg,
85 sctp_cmd_seq_t *commands);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000086static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
87 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 const struct sctp_association *asoc,
89 const sctp_subtype_t type,
90 void *arg,
91 sctp_cmd_seq_t *commands);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000092static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
93 const struct sctp_endpoint *ep,
Vlad Yasevichece25df2007-09-07 16:30:54 -040094 const struct sctp_association *asoc,
95 const sctp_subtype_t type,
96 void *arg,
97 sctp_cmd_seq_t *commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
99
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000100static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
101 sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -0800102 __be16 error, int sk_err,
Adrian Bunk52c1da32005-06-23 22:05:33 -0700103 const struct sctp_association *asoc,
104 struct sctp_transport *transport);
105
Wei Yongjunaecedea2007-08-02 16:57:44 +0800106static sctp_disposition_t sctp_sf_abort_violation(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000107 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400108 const struct sctp_endpoint *ep,
Wei Yongjunaecedea2007-08-02 16:57:44 +0800109 const struct sctp_association *asoc,
110 void *arg,
111 sctp_cmd_seq_t *commands,
112 const __u8 *payload,
113 const size_t paylen);
114
Adrian Bunk52c1da32005-06-23 22:05:33 -0700115static sctp_disposition_t sctp_sf_violation_chunklen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000116 struct net *net,
Adrian Bunk52c1da32005-06-23 22:05:33 -0700117 const struct sctp_endpoint *ep,
118 const struct sctp_association *asoc,
119 const sctp_subtype_t type,
120 void *arg,
121 sctp_cmd_seq_t *commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Wei Yongjun6f4c6182007-09-19 17:19:52 +0800123static sctp_disposition_t sctp_sf_violation_paramlen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000124 struct net *net,
Wei Yongjun6f4c6182007-09-19 17:19:52 +0800125 const struct sctp_endpoint *ep,
126 const struct sctp_association *asoc,
127 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -0700128 void *arg, void *ext,
Wei Yongjun6f4c6182007-09-19 17:19:52 +0800129 sctp_cmd_seq_t *commands);
130
Wei Yongjunaecedea2007-08-02 16:57:44 +0800131static sctp_disposition_t sctp_sf_violation_ctsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000132 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +0800133 const struct sctp_endpoint *ep,
134 const struct sctp_association *asoc,
135 const sctp_subtype_t type,
136 void *arg,
137 sctp_cmd_seq_t *commands);
138
Vlad Yasevichece25df2007-09-07 16:30:54 -0400139static sctp_disposition_t sctp_sf_violation_chunk(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000140 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400141 const struct sctp_endpoint *ep,
142 const struct sctp_association *asoc,
143 const sctp_subtype_t type,
144 void *arg,
145 sctp_cmd_seq_t *commands);
146
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000147static sctp_ierror_t sctp_sf_authenticate(struct net *net,
148 const struct sctp_endpoint *ep,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700149 const struct sctp_association *asoc,
150 const sctp_subtype_t type,
151 struct sctp_chunk *chunk);
152
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000153static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
154 const struct sctp_endpoint *ep,
Vlad Yasevich75205f42007-12-20 14:12:59 -0800155 const struct sctp_association *asoc,
156 const sctp_subtype_t type,
157 void *arg,
158 sctp_cmd_seq_t *commands);
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/* Small helper function that checks if the chunk length
161 * is of the appropriate length. The 'required_length' argument
162 * is set to be the size of a specific chunk we are testing.
Marcelo Ricardo Leitner509e7a312016-12-28 09:26:35 -0200163 * Return Values: true = Valid length
164 * false = Invalid length
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 *
166 */
Marcelo Ricardo Leitner509e7a312016-12-28 09:26:35 -0200167static inline bool
168sctp_chunk_length_valid(struct sctp_chunk *chunk, __u16 required_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
170 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
171
Daniel Borkmann26b87c72014-10-09 22:55:33 +0200172 /* Previously already marked? */
173 if (unlikely(chunk->pdiscard))
Marcelo Ricardo Leitner509e7a312016-12-28 09:26:35 -0200174 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (unlikely(chunk_length < required_length))
Marcelo Ricardo Leitner509e7a312016-12-28 09:26:35 -0200176 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Marcelo Ricardo Leitner509e7a312016-12-28 09:26:35 -0200178 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181/**********************************************************
182 * These are the state functions for handling chunk events.
183 **********************************************************/
184
185/*
186 * Process the final SHUTDOWN COMPLETE.
187 *
188 * Section: 4 (C) (diagram), 9.2
189 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
190 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
191 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
192 * should stop the T2-shutdown timer and remove all knowledge of the
193 * association (and thus the association enters the CLOSED state).
194 *
Jerome Forissier047a2422005-04-28 11:58:43 -0700195 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * C) Rules for packet carrying SHUTDOWN COMPLETE:
197 * ...
Jerome Forissier047a2422005-04-28 11:58:43 -0700198 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
199 * if the Verification Tag field of the packet matches its own tag and
200 * the T bit is not set
201 * OR
202 * it is set to its peer's tag and the T bit is set in the Chunk
203 * Flags.
204 * Otherwise, the receiver MUST silently discard the packet
205 * and take no further action. An endpoint MUST ignore the
206 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 *
208 * Inputs
209 * (endpoint, asoc, chunk)
210 *
211 * Outputs
212 * (asoc, reply_msg, msg_up, timers, counters)
213 *
214 * The return value is the disposition of the chunk.
215 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000216sctp_disposition_t sctp_sf_do_4_C(struct net *net,
217 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 const struct sctp_association *asoc,
219 const sctp_subtype_t type,
220 void *arg,
221 sctp_cmd_seq_t *commands)
222{
223 struct sctp_chunk *chunk = arg;
224 struct sctp_ulpevent *ev;
225
Vlad Yasevichece25df2007-09-07 16:30:54 -0400226 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000227 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -0400228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /* RFC 2960 6.10 Bundling
230 *
231 * An endpoint MUST NOT bundle INIT, INIT ACK or
232 * SHUTDOWN COMPLETE with any other chunks.
233 */
234 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000235 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Vlad Yasevichece25df2007-09-07 16:30:54 -0400237 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
Xin Long922dbc52017-06-30 11:52:13 +0800238 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000239 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400240 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 /* RFC 2960 10.2 SCTP-to-ULP
243 *
244 * H) SHUTDOWN COMPLETE notification
245 *
246 * When SCTP completes the shutdown procedures (section 9.2) this
247 * notification is passed to the upper layer.
248 */
249 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700250 0, 0, 0, NULL, GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700251 if (ev)
252 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900253 SCTP_ULPEVENT(ev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
256 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
257 * not the chunk should be discarded. If the endpoint is in
258 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
259 * T2-shutdown timer and remove all knowledge of the
260 * association (and thus the association enters the CLOSED
261 * state).
262 */
263 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
264 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
265
266 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
267 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
268
269 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
270 SCTP_STATE(SCTP_STATE_CLOSED));
271
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000272 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
273 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
276
277 return SCTP_DISPOSITION_DELETE_TCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280/*
281 * Respond to a normal INIT chunk.
282 * We are the side that is being asked for an association.
283 *
284 * Section: 5.1 Normal Establishment of an Association, B
285 * B) "Z" shall respond immediately with an INIT ACK chunk. The
286 * destination IP address of the INIT ACK MUST be set to the source
287 * IP address of the INIT to which this INIT ACK is responding. In
288 * the response, besides filling in other parameters, "Z" must set the
289 * Verification Tag field to Tag_A, and also provide its own
290 * Verification Tag (Tag_Z) in the Initiate Tag field.
291 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900292 * Verification Tag: Must be 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 *
294 * Inputs
295 * (endpoint, asoc, chunk)
296 *
297 * Outputs
298 * (asoc, reply_msg, msg_up, timers, counters)
299 *
300 * The return value is the disposition of the chunk.
301 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000302sctp_disposition_t sctp_sf_do_5_1B_init(struct net *net,
303 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 const struct sctp_association *asoc,
305 const sctp_subtype_t type,
306 void *arg,
307 sctp_cmd_seq_t *commands)
308{
Xin Long62e6b7e2017-07-23 09:34:28 +0800309 struct sctp_chunk *chunk = arg, *repl, *err_chunk;
310 struct sctp_unrecognized_param *unk_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 struct sctp_association *new_asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 struct sctp_packet *packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 int len;
314
315 /* 6.10 Bundling
316 * An endpoint MUST NOT bundle INIT, INIT ACK or
317 * SHUTDOWN COMPLETE with any other chunks.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900318 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 * IG Section 2.11.2
320 * Furthermore, we require that the receiver of an INIT chunk MUST
321 * enforce these rules by silently discarding an arriving packet
322 * with an INIT chunk that is bundled with other chunks.
323 */
324 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000325 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 /* If the packet is an OOTB packet which is temporarily on the
328 * control endpoint, respond with an ABORT.
329 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +0000330 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000331 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000332 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900336 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 */
338 if (chunk->sctp_hdr->vtag != 0)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000339 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 /* Make sure that the INIT chunk has a valid length.
342 * Normally, this would cause an ABORT with a Protocol Violation
343 * error, but since we don't have an association, we'll
344 * just discard the packet.
345 */
Xin Long01a992b2017-06-30 11:52:22 +0800346 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000347 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Vlad Yasevichbec96402009-07-30 18:08:28 -0400349 /* If the INIT is coming toward a closing socket, we'll send back
350 * and ABORT. Essentially, this catches the race of INIT being
351 * backloged to the socket at the same time as the user isses close().
352 * Since the socket and all its associations are going away, we
353 * can treat this OOTB
354 */
355 if (sctp_sstate(ep->base.sk, CLOSING))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000356 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Vlad Yasevichbec96402009-07-30 18:08:28 -0400357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 /* Verify the INIT chunk before processing it. */
359 err_chunk = NULL;
Vlad Yasevichb14878c2014-04-17 17:26:50 +0200360 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Xin Long01a992b2017-06-30 11:52:22 +0800361 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 &err_chunk)) {
363 /* This chunk contains fatal error. It is to be discarded.
364 * Send an ABORT, with causes if there is any.
365 */
366 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000367 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 (__u8 *)(err_chunk->chunk_hdr) +
Xin Long922dbc52017-06-30 11:52:13 +0800369 sizeof(struct sctp_chunkhdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 ntohs(err_chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +0800371 sizeof(struct sctp_chunkhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 sctp_chunk_free(err_chunk);
374
375 if (packet) {
376 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
377 SCTP_PACKET(packet));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000378 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return SCTP_DISPOSITION_CONSUME;
380 } else {
381 return SCTP_DISPOSITION_NOMEM;
382 }
383 } else {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000384 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 commands);
386 }
387 }
388
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900389 /* Grab the INIT header. */
Xin Long4ae70c02017-06-30 11:52:21 +0800390 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 /* Tag the variable length parameters. */
Xin Long4ae70c02017-06-30 11:52:21 +0800393 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
396 if (!new_asoc)
397 goto nomem;
398
Vlad Yasevich409b95a2009-11-10 08:57:34 +0000399 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
400 sctp_scope(sctp_source(chunk)),
401 GFP_ATOMIC) < 0)
402 goto nomem_init;
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 /* The call, sctp_process_init(), can fail on memory allocation. */
Wei Yongjunde6becdc02011-04-19 21:30:51 +0000405 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Xin Long01a992b2017-06-30 11:52:22 +0800406 (struct sctp_init_chunk *)chunk->chunk_hdr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 GFP_ATOMIC))
408 goto nomem_init;
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
411
412 /* If there are errors need to be reported for unknown parameters,
413 * make sure to reserve enough room in the INIT ACK for them.
414 */
415 len = 0;
416 if (err_chunk)
417 len = ntohs(err_chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +0800418 sizeof(struct sctp_chunkhdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
421 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700422 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 /* If there are errors need to be reported for unknown parameters,
425 * include them in the outgoing INIT ACK as "Unrecognized parameter"
426 * parameter.
427 */
428 if (err_chunk) {
429 /* Get the "Unrecognized parameter" parameter(s) out of the
430 * ERROR chunk generated by sctp_verify_init(). Since the
431 * error cause code for "unknown parameter" and the
432 * "Unrecognized parameter" type is the same, we can
433 * construct the parameters in INIT ACK by copying the
434 * ERROR causes over.
435 */
Xin Long62e6b7e2017-07-23 09:34:28 +0800436 unk_param = (struct sctp_unrecognized_param *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 ((__u8 *)(err_chunk->chunk_hdr) +
Xin Long922dbc52017-06-30 11:52:13 +0800438 sizeof(struct sctp_chunkhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 /* Replace the cause code with the "Unrecognized parameter"
440 * parameter type.
441 */
442 sctp_addto_chunk(repl, len, unk_param);
443 sctp_chunk_free(err_chunk);
444 }
445
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700446 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
449
450 /*
451 * Note: After sending out INIT ACK with the State Cookie parameter,
452 * "Z" MUST NOT allocate any resources, nor keep any states for the
453 * new association. Otherwise, "Z" will be vulnerable to resource
454 * attacks.
455 */
456 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
457
458 return SCTP_DISPOSITION_DELETE_TCB;
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460nomem_init:
461 sctp_association_free(new_asoc);
462nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700463 if (err_chunk)
464 sctp_chunk_free(err_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return SCTP_DISPOSITION_NOMEM;
466}
467
468/*
469 * Respond to a normal INIT ACK chunk.
470 * We are the side that is initiating the association.
471 *
472 * Section: 5.1 Normal Establishment of an Association, C
473 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
474 * timer and leave COOKIE-WAIT state. "A" shall then send the State
475 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
476 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
477 *
478 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
479 * DATA chunks, but it MUST be the first chunk in the packet and
480 * until the COOKIE ACK is returned the sender MUST NOT send any
481 * other packets to the peer.
482 *
483 * Verification Tag: 3.3.3
484 * If the value of the Initiate Tag in a received INIT ACK chunk is
485 * found to be 0, the receiver MUST treat it as an error and close the
486 * association by transmitting an ABORT.
487 *
488 * Inputs
489 * (endpoint, asoc, chunk)
490 *
491 * Outputs
492 * (asoc, reply_msg, msg_up, timers, counters)
493 *
494 * The return value is the disposition of the chunk.
495 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000496sctp_disposition_t sctp_sf_do_5_1C_ack(struct net *net,
497 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 const struct sctp_association *asoc,
499 const sctp_subtype_t type,
500 void *arg,
501 sctp_cmd_seq_t *commands)
502{
503 struct sctp_chunk *chunk = arg;
Xin Long01a992b2017-06-30 11:52:22 +0800504 struct sctp_init_chunk *initchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 struct sctp_chunk *err_chunk;
506 struct sctp_packet *packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000509 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 /* 6.10 Bundling
512 * An endpoint MUST NOT bundle INIT, INIT ACK or
513 * SHUTDOWN COMPLETE with any other chunks.
514 */
515 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000516 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Vlad Yasevichece25df2007-09-07 16:30:54 -0400518 /* Make sure that the INIT-ACK chunk has a valid length */
Xin Longcb1844c2017-07-23 09:34:26 +0800519 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_initack_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000520 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400521 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /* Grab the INIT header. */
Xin Long4ae70c02017-06-30 11:52:21 +0800523 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 /* Verify the INIT chunk before processing it. */
526 err_chunk = NULL;
Vlad Yasevichb14878c2014-04-17 17:26:50 +0200527 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Xin Long01a992b2017-06-30 11:52:22 +0800528 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 &err_chunk)) {
530
Vlad Yasevich853f4b52008-01-20 06:10:46 -0800531 sctp_error_t error = SCTP_ERROR_NO_RESOURCE;
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /* This chunk contains fatal error. It is to be discarded.
Vlad Yasevichd6701192007-12-20 14:13:31 -0800534 * Send an ABORT, with causes. If there are no causes,
535 * then there wasn't enough memory. Just terminate
536 * the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 */
538 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000539 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 (__u8 *)(err_chunk->chunk_hdr) +
Xin Long922dbc52017-06-30 11:52:13 +0800541 sizeof(struct sctp_chunkhdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 ntohs(err_chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +0800543 sizeof(struct sctp_chunkhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 sctp_chunk_free(err_chunk);
546
547 if (packet) {
548 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
549 SCTP_PACKET(packet));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000550 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700551 error = SCTP_ERROR_INV_PARAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700554
555 /* SCTP-AUTH, Section 6.3:
556 * It should be noted that if the receiver wants to tear
557 * down an association in an authenticated way only, the
558 * handling of malformed packets should not result in
559 * tearing down the association.
560 *
561 * This means that if we only want to abort associations
562 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300563 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700564 * was malformed.
565 */
566 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000567 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700568
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000569 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
570 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700571 asoc, chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
573
574 /* Tag the variable length parameters. Note that we never
575 * convert the parameters in an INIT chunk.
576 */
Xin Long4ae70c02017-06-30 11:52:21 +0800577 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Xin Long01a992b2017-06-30 11:52:22 +0800579 initchunk = (struct sctp_init_chunk *)chunk->chunk_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
582 SCTP_PEER_INIT(initchunk));
583
Frank Filz3f7a87d2005-06-20 13:14:57 -0700584 /* Reset init error count upon receipt of INIT-ACK. */
585 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /* 5.1 C) "A" shall stop the T1-init timer and leave
588 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
589 * timer, and enter the COOKIE-ECHOED state.
590 */
591 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
592 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
593 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
594 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
595 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
596 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
597
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700598 /* SCTP-AUTH: genereate the assocition shared keys so that
599 * we can potentially signe the COOKIE-ECHO.
600 */
601 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 /* 5.1 C) "A" shall then send the State Cookie received in the
604 * INIT ACK chunk in a COOKIE ECHO chunk, ...
605 */
606 /* If there is any errors to report, send the ERROR chunk generated
607 * for unknown parameters as well.
608 */
609 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
610 SCTP_CHUNK(err_chunk));
611
612 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
615/*
616 * Respond to a normal COOKIE ECHO chunk.
617 * We are the side that is being asked for an association.
618 *
619 * Section: 5.1 Normal Establishment of an Association, D
620 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
621 * with a COOKIE ACK chunk after building a TCB and moving to
622 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
623 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
624 * chunk MUST be the first chunk in the packet.
625 *
626 * IMPLEMENTATION NOTE: An implementation may choose to send the
627 * Communication Up notification to the SCTP user upon reception
628 * of a valid COOKIE ECHO chunk.
629 *
630 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
631 * D) Rules for packet carrying a COOKIE ECHO
632 *
633 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
634 * Initial Tag received in the INIT ACK.
635 *
636 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
637 *
638 * Inputs
639 * (endpoint, asoc, chunk)
640 *
641 * Outputs
642 * (asoc, reply_msg, msg_up, timers, counters)
643 *
644 * The return value is the disposition of the chunk.
645 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000646sctp_disposition_t sctp_sf_do_5_1D_ce(struct net *net,
647 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 const struct sctp_association *asoc,
649 const sctp_subtype_t type, void *arg,
650 sctp_cmd_seq_t *commands)
651{
652 struct sctp_chunk *chunk = arg;
653 struct sctp_association *new_asoc;
Xin Long01a992b2017-06-30 11:52:22 +0800654 struct sctp_init_chunk *peer_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 struct sctp_chunk *repl;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700656 struct sctp_ulpevent *ev, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 int error = 0;
658 struct sctp_chunk *err_chk_p;
Vlad Yasevich609ee462007-08-31 03:10:59 +0900659 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 /* If the packet is an OOTB packet which is temporarily on the
662 * control endpoint, respond with an ABORT.
663 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +0000664 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000665 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000666 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 /* Make sure that the COOKIE_ECHO chunk has a valid length.
670 * In this case, we check that we have enough for at least a
671 * chunk header. More detailed verification is done
672 * in sctp_unpack_cookie().
673 */
Xin Long922dbc52017-06-30 11:52:13 +0800674 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000675 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Vlad Yasevich609ee462007-08-31 03:10:59 +0900677 /* If the endpoint is not listening or if the number of associations
678 * on the TCP-style socket exceed the max backlog, respond with an
679 * ABORT.
680 */
681 sk = ep->base.sk;
682 if (!sctp_sstate(sk, LISTENING) ||
683 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000684 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Vlad Yasevich609ee462007-08-31 03:10:59 +0900685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 /* "Decode" the chunk. We have no optional parameters so we
687 * are in good shape.
688 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900689 chunk->subh.cookie_hdr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -0700691 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +0800692 sizeof(struct sctp_chunkhdr)))
Sridhar Samudrala62b08082006-05-05 17:04:43 -0700693 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
696 * "Z" will reply with a COOKIE ACK chunk after building a TCB
697 * and moving to the ESTABLISHED state.
698 */
699 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
700 &err_chk_p);
701
702 /* FIXME:
703 * If the re-build failed, what is the proper error path
704 * from here?
705 *
706 * [We should abort the association. --piggy]
707 */
708 if (!new_asoc) {
709 /* FIXME: Several errors are possible. A bad cookie should
710 * be silently discarded, but think about logging it too.
711 */
712 switch (error) {
713 case -SCTP_IERROR_NOMEM:
714 goto nomem;
715
716 case -SCTP_IERROR_STALE_COOKIE:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000717 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 err_chk_p);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000719 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 case -SCTP_IERROR_BAD_SIG:
722 default:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000723 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700728 /* Delay state machine commands until later.
729 *
730 * Re-build the bind address for the association is done in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 * the sctp_unpack_cookie() already.
732 */
733 /* This is a brand-new association, so these are not yet side
734 * effects--it is safe to run them here.
735 */
736 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
737
Wei Yongjunde6becdc02011-04-19 21:30:51 +0000738 if (!sctp_process_init(new_asoc, chunk,
Al Viro6a1e5f32006-11-20 17:12:25 -0800739 &chunk->subh.cookie_hdr->c.peer_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 peer_init, GFP_ATOMIC))
741 goto nomem_init;
742
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700743 /* SCTP-AUTH: Now that we've populate required fields in
744 * sctp_process_init, set up the assocaition shared keys as
745 * necessary so that we can potentially authenticate the ACK
746 */
747 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
748 if (error)
749 goto nomem_init;
750
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700751 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
752 * is supposed to be authenticated and we have to do delayed
753 * authentication. We've just recreated the association using
754 * the information in the cookie and now it's much easier to
755 * do the authentication.
756 */
757 if (chunk->auth_chunk) {
758 struct sctp_chunk auth;
759 sctp_ierror_t ret;
760
Daniel Borkmannec0223e2014-03-03 17:23:04 +0100761 /* Make sure that we and the peer are AUTH capable */
762 if (!net->sctp.auth_enable || !new_asoc->peer.auth_capable) {
Daniel Borkmannec0223e2014-03-03 17:23:04 +0100763 sctp_association_free(new_asoc);
764 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
765 }
766
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700767 /* set-up our fake chunk so that we can process it */
768 auth.skb = chunk->auth_chunk;
769 auth.asoc = chunk->asoc;
770 auth.sctp_hdr = chunk->sctp_hdr;
Xin Long922dbc52017-06-30 11:52:13 +0800771 auth.chunk_hdr = (struct sctp_chunkhdr *)
772 skb_push(chunk->auth_chunk,
773 sizeof(struct sctp_chunkhdr));
774 skb_pull(chunk->auth_chunk, sizeof(struct sctp_chunkhdr));
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700775 auth.transport = chunk->transport;
776
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000777 ret = sctp_sf_authenticate(net, ep, new_asoc, type, &auth);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700778 if (ret != SCTP_IERROR_NO_ERROR) {
779 sctp_association_free(new_asoc);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000780 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700781 }
782 }
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 repl = sctp_make_cookie_ack(new_asoc, chunk);
785 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700786 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 /* RFC 2960 5.1 Normal Establishment of an Association
789 *
790 * D) IMPLEMENTATION NOTE: An implementation may choose to
791 * send the Communication Up notification to the SCTP user
792 * upon reception of a valid COOKIE ECHO chunk.
793 */
794 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
795 new_asoc->c.sinit_num_ostreams,
796 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700797 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (!ev)
799 goto nomem_ev;
800
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900801 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800802 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800804 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800806 if (new_asoc->peer.adaptation_ind) {
807 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700809 if (!ai_ev)
810 goto nomem_aiev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700813 /* Add all the state machine commands now since we've created
814 * everything. This way we don't introduce memory corruptions
815 * during side-effect processing and correclty count established
816 * associations.
817 */
818 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
819 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
820 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000821 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
822 SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700823 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
824
Neil Horman9f70f462013-12-10 06:48:15 -0500825 if (new_asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700826 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
827 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
828
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700829 /* This will send the COOKIE ACK */
830 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
831
832 /* Queue the ASSOC_CHANGE event */
833 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
834
835 /* Send up the Adaptation Layer Indication event */
836 if (ai_ev)
837 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
838 SCTP_ULPEVENT(ai_ev));
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return SCTP_DISPOSITION_CONSUME;
841
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700842nomem_aiev:
843 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844nomem_ev:
845 sctp_chunk_free(repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846nomem_init:
847 sctp_association_free(new_asoc);
848nomem:
849 return SCTP_DISPOSITION_NOMEM;
850}
851
852/*
853 * Respond to a normal COOKIE ACK chunk.
Marcelo Ricardo Leitnerb52effd2015-07-17 13:50:21 -0300854 * We are the side that is asking for an association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 *
856 * RFC 2960 5.1 Normal Establishment of an Association
857 *
858 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
859 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
860 * timer. It may also notify its ULP about the successful
861 * establishment of the association with a Communication Up
862 * notification (see Section 10).
863 *
864 * Verification Tag:
865 * Inputs
866 * (endpoint, asoc, chunk)
867 *
868 * Outputs
869 * (asoc, reply_msg, msg_up, timers, counters)
870 *
871 * The return value is the disposition of the chunk.
872 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000873sctp_disposition_t sctp_sf_do_5_1E_ca(struct net *net,
874 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 const struct sctp_association *asoc,
876 const sctp_subtype_t type, void *arg,
877 sctp_cmd_seq_t *commands)
878{
879 struct sctp_chunk *chunk = arg;
880 struct sctp_ulpevent *ev;
881
882 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000883 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 /* Verify that the chunk length for the COOKIE-ACK is OK.
886 * If we don't do this, any bundled chunks may be junked.
887 */
Xin Long922dbc52017-06-30 11:52:13 +0800888 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000889 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 commands);
891
892 /* Reset init error count upon receipt of COOKIE-ACK,
893 * to avoid problems with the managemement of this
894 * counter in stale cookie situations when a transition back
895 * from the COOKIE-ECHOED state to the COOKIE-WAIT
896 * state is performed.
897 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700898 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 /* RFC 2960 5.1 Normal Establishment of an Association
901 *
902 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
903 * from the COOKIE-ECHOED state to the ESTABLISHED state,
904 * stopping the T1-cookie timer.
905 */
906 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
907 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
908 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
909 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000910 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
911 SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
Neil Horman9f70f462013-12-10 06:48:15 -0500913 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
915 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 /* It may also notify its ULP about the successful
918 * establishment of the association with a Communication Up
919 * notification (see Section 10).
920 */
921 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
922 0, asoc->c.sinit_num_ostreams,
923 asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700924 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 if (!ev)
927 goto nomem;
928
929 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
930
931 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800932 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800934 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800936 if (asoc->peer.adaptation_ind) {
937 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (!ev)
939 goto nomem;
940
941 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
942 SCTP_ULPEVENT(ev));
943 }
944
945 return SCTP_DISPOSITION_CONSUME;
946nomem:
947 return SCTP_DISPOSITION_NOMEM;
948}
949
950/* Generate and sendout a heartbeat packet. */
951static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
952 const struct sctp_association *asoc,
953 const sctp_subtype_t type,
954 void *arg,
955 sctp_cmd_seq_t *commands)
956{
957 struct sctp_transport *transport = (struct sctp_transport *) arg;
958 struct sctp_chunk *reply;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 /* Send a heartbeat to our peer. */
Wei Yongjun92c73af2011-04-19 21:31:47 +0000961 reply = sctp_make_heartbeat(asoc, transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (!reply)
963 return SCTP_DISPOSITION_NOMEM;
964
965 /* Set rto_pending indicating that an RTT measurement
966 * is started with this heartbeat chunk.
967 */
968 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
969 SCTP_TRANSPORT(transport));
970
971 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
972 return SCTP_DISPOSITION_CONSUME;
973}
974
975/* Generate a HEARTBEAT packet on the given transport. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000976sctp_disposition_t sctp_sf_sendbeat_8_3(struct net *net,
977 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 const struct sctp_association *asoc,
979 const sctp_subtype_t type,
980 void *arg,
981 sctp_cmd_seq_t *commands)
982{
983 struct sctp_transport *transport = (struct sctp_transport *) arg;
984
Vlad Yasevichb9f84782009-08-26 09:36:25 -0400985 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700986 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
987 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
989 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -0800990 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000991 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
992 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 return SCTP_DISPOSITION_DELETE_TCB;
994 }
995
996 /* Section 3.3.5.
997 * The Sender-specific Heartbeat Info field should normally include
998 * information about the sender's current time when this HEARTBEAT
999 * chunk is sent and the destination transport address to which this
1000 * HEARTBEAT is sent (see Section 8.3).
1001 */
1002
Frank Filz52ccb8e2005-12-22 11:36:46 -08001003 if (transport->param_flags & SPP_HB_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (SCTP_DISPOSITION_NOMEM ==
1005 sctp_sf_heartbeat(ep, asoc, type, arg,
1006 commands))
1007 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevich245cba72009-11-23 15:53:58 -05001008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 /* Set transport error counter and association error counter
1010 * when sending heartbeat.
1011 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00001012 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 SCTP_TRANSPORT(transport));
1014 }
Vlad Yasevich245cba72009-11-23 15:53:58 -05001015 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1016 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1018 SCTP_TRANSPORT(transport));
1019
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001020 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
1022
Xin Long7b9438d2017-01-18 00:44:43 +08001023/* resend asoc strreset_chunk. */
1024sctp_disposition_t sctp_sf_send_reconf(struct net *net,
1025 const struct sctp_endpoint *ep,
1026 const struct sctp_association *asoc,
1027 const sctp_subtype_t type, void *arg,
1028 sctp_cmd_seq_t *commands)
1029{
1030 struct sctp_transport *transport = arg;
1031
1032 if (asoc->overall_error_count >= asoc->max_retrans) {
1033 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
1034 SCTP_ERROR(ETIMEDOUT));
1035 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
1036 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
1037 SCTP_PERR(SCTP_ERROR_NO_ERROR));
1038 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
1039 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
1040 return SCTP_DISPOSITION_DELETE_TCB;
1041 }
1042
1043 sctp_chunk_hold(asoc->strreset_chunk);
1044 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1045 SCTP_CHUNK(asoc->strreset_chunk));
1046 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
1047
1048 return SCTP_DISPOSITION_CONSUME;
1049}
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051/*
1052 * Process an heartbeat request.
1053 *
1054 * Section: 8.3 Path Heartbeat
1055 * The receiver of the HEARTBEAT should immediately respond with a
1056 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1057 * from the received HEARTBEAT chunk.
1058 *
1059 * Verification Tag: 8.5 Verification Tag [Normal verification]
1060 * When receiving an SCTP packet, the endpoint MUST ensure that the
1061 * value in the Verification Tag field of the received SCTP packet
1062 * matches its own Tag. If the received Verification Tag value does not
1063 * match the receiver's own tag value, the receiver shall silently
1064 * discard the packet and shall not process it any further except for
1065 * those cases listed in Section 8.5.1 below.
1066 *
1067 * Inputs
1068 * (endpoint, asoc, chunk)
1069 *
1070 * Outputs
1071 * (asoc, reply_msg, msg_up, timers, counters)
1072 *
1073 * The return value is the disposition of the chunk.
1074 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001075sctp_disposition_t sctp_sf_beat_8_3(struct net *net,
1076 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 const struct sctp_association *asoc,
1078 const sctp_subtype_t type,
1079 void *arg,
1080 sctp_cmd_seq_t *commands)
1081{
Xin Long3c918702017-06-30 11:52:16 +08001082 struct sctp_paramhdr *param_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 struct sctp_chunk *chunk = arg;
1084 struct sctp_chunk *reply;
1085 size_t paylen = 0;
1086
1087 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001088 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 /* Make sure that the HEARTBEAT chunk has a valid length. */
1091 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001092 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 commands);
1094
1095 /* 8.3 The receiver of the HEARTBEAT should immediately
1096 * respond with a HEARTBEAT ACK that contains the Heartbeat
1097 * Information field copied from the received HEARTBEAT chunk.
1098 */
Xin Long4d2dcdf2017-07-23 09:34:34 +08001099 chunk->subh.hb_hdr = (struct sctp_heartbeathdr *)chunk->skb->data;
Xin Long3c918702017-06-30 11:52:16 +08001100 param_hdr = (struct sctp_paramhdr *)chunk->subh.hb_hdr;
Xin Long922dbc52017-06-30 11:52:13 +08001101 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(struct sctp_chunkhdr);
Thomas Graf06a31e22012-11-30 02:16:27 +00001102
1103 if (ntohs(param_hdr->length) > paylen)
1104 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
1105 param_hdr, commands);
1106
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001107 if (!pskb_pull(chunk->skb, paylen))
1108 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Thomas Graf06a31e22012-11-30 02:16:27 +00001110 reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (!reply)
1112 goto nomem;
1113
1114 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1115 return SCTP_DISPOSITION_CONSUME;
1116
1117nomem:
1118 return SCTP_DISPOSITION_NOMEM;
1119}
1120
1121/*
1122 * Process the returning HEARTBEAT ACK.
1123 *
1124 * Section: 8.3 Path Heartbeat
1125 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1126 * should clear the error counter of the destination transport
1127 * address to which the HEARTBEAT was sent, and mark the destination
1128 * transport address as active if it is not so marked. The endpoint may
1129 * optionally report to the upper layer when an inactive destination
1130 * address is marked as active due to the reception of the latest
1131 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1132 * clear the association overall error count as well (as defined
1133 * in section 8.1).
1134 *
1135 * The receiver of the HEARTBEAT ACK should also perform an RTT
1136 * measurement for that destination transport address using the time
1137 * value carried in the HEARTBEAT ACK chunk.
1138 *
1139 * Verification Tag: 8.5 Verification Tag [Normal verification]
1140 *
1141 * Inputs
1142 * (endpoint, asoc, chunk)
1143 *
1144 * Outputs
1145 * (asoc, reply_msg, msg_up, timers, counters)
1146 *
1147 * The return value is the disposition of the chunk.
1148 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001149sctp_disposition_t sctp_sf_backbeat_8_3(struct net *net,
1150 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 const struct sctp_association *asoc,
1152 const sctp_subtype_t type,
1153 void *arg,
1154 sctp_cmd_seq_t *commands)
1155{
1156 struct sctp_chunk *chunk = arg;
1157 union sctp_addr from_addr;
1158 struct sctp_transport *link;
1159 sctp_sender_hb_info_t *hbinfo;
1160 unsigned long max_interval;
1161
1162 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001163 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
Xin Long922dbc52017-06-30 11:52:13 +08001166 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr) +
Wei Yongjundadb50c2009-08-22 11:27:37 +08001167 sizeof(sctp_sender_hb_info_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001168 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 commands);
1170
1171 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
Vladislav Yasevicha6012662006-05-19 14:25:53 -07001172 /* Make sure that the length of the parameter is what we expect */
1173 if (ntohs(hbinfo->param_hdr.length) !=
1174 sizeof(sctp_sender_hb_info_t)) {
1175 return SCTP_DISPOSITION_DISCARD;
1176 }
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 from_addr = hbinfo->daddr;
Al Viro63de08f2006-11-20 17:07:25 -08001179 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 /* This should never happen, but lets log it if so. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001182 if (unlikely(!link)) {
1183 if (from_addr.sa.sa_family == AF_INET6) {
Joe Perchese87cc472012-05-13 21:56:26 +00001184 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1185 __func__,
1186 asoc,
1187 &from_addr.v6.sin6_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001188 } else {
Joe Perchese87cc472012-05-13 21:56:26 +00001189 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1190 __func__,
1191 asoc,
1192 &from_addr.v4.sin_addr.s_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return SCTP_DISPOSITION_DISCARD;
1195 }
1196
Sridhar Samudralaad8fec12006-07-21 14:48:50 -07001197 /* Validate the 64-bit random nonce. */
1198 if (hbinfo->hb_nonce != link->hb_nonce)
1199 return SCTP_DISPOSITION_DISCARD;
1200
Frank Filz52ccb8e2005-12-22 11:36:46 -08001201 max_interval = link->hbinterval + link->rto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 /* Check if the timestamp looks valid. */
1204 if (time_after(hbinfo->sent_at, jiffies) ||
1205 time_after(jiffies, hbinfo->sent_at + max_interval)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001206 pr_debug("%s: HEARTBEAT ACK with invalid timestamp received "
1207 "for transport:%p\n", __func__, link);
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return SCTP_DISPOSITION_DISCARD;
1210 }
1211
1212 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1213 * the HEARTBEAT should clear the error counter of the
1214 * destination transport address to which the HEARTBEAT was
1215 * sent and mark the destination transport address as active if
1216 * it is not so marked.
1217 */
1218 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1219
1220 return SCTP_DISPOSITION_CONSUME;
1221}
1222
1223/* Helper function to send out an abort for the restart
1224 * condition.
1225 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001226static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 struct sctp_chunk *init,
1228 sctp_cmd_seq_t *commands)
1229{
1230 int len;
1231 struct sctp_packet *pkt;
1232 union sctp_addr_param *addrparm;
1233 struct sctp_errhdr *errhdr;
1234 struct sctp_endpoint *ep;
1235 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1236 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1237
1238 /* Build the error on the stack. We are way to malloc crazy
1239 * throughout the code today.
1240 */
1241 errhdr = (struct sctp_errhdr *)buffer;
1242 addrparm = (union sctp_addr_param *)errhdr->variable;
1243
1244 /* Copy into a parm format. */
1245 len = af->to_addr_param(ssa, addrparm);
1246 len += sizeof(sctp_errhdr_t);
1247
1248 errhdr->cause = SCTP_ERROR_RESTART;
1249 errhdr->length = htons(len);
1250
1251 /* Assign to the control socket. */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001252 ep = sctp_sk(net->sctp.ctl_sock)->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 /* Association is NULL since this may be a restart attack and we
1255 * want to send back the attacker's vtag.
1256 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001257 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 if (!pkt)
1260 goto out;
1261 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1262
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00001263 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 /* Discard the rest of the inbound packet. */
1266 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1267
1268out:
1269 /* Even if there is no memory, treat as a failure so
1270 * the packet will get dropped.
1271 */
1272 return 0;
1273}
1274
Joe Perches123031c2010-09-08 11:04:21 +00001275static bool list_has_sctp_addr(const struct list_head *list,
1276 union sctp_addr *ipaddr)
1277{
1278 struct sctp_transport *addr;
1279
1280 list_for_each_entry(addr, list, transports) {
1281 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1282 return true;
1283 }
1284
1285 return false;
1286}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287/* A restart is occurring, check to make sure no new addresses
1288 * are being added as we may be under a takeover attack.
1289 */
1290static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1291 const struct sctp_association *asoc,
1292 struct sctp_chunk *init,
1293 sctp_cmd_seq_t *commands)
1294{
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001295 struct net *net = sock_net(new_asoc->base.sk);
Joe Perches123031c2010-09-08 11:04:21 +00001296 struct sctp_transport *new_addr;
1297 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Joe Perches123031c2010-09-08 11:04:21 +00001299 /* Implementor's Guide - Section 5.2.2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 * ...
1301 * Before responding the endpoint MUST check to see if the
1302 * unexpected INIT adds new addresses to the association. If new
1303 * addresses are added to the association, the endpoint MUST respond
1304 * with an ABORT..
1305 */
1306
1307 /* Search through all current addresses and make sure
1308 * we aren't adding any new ones.
1309 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001310 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
Joe Perches123031c2010-09-08 11:04:21 +00001311 transports) {
1312 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1313 &new_addr->ipaddr)) {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001314 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
Joe Perches123031c2010-09-08 11:04:21 +00001315 commands);
1316 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 break;
Joe Perches123031c2010-09-08 11:04:21 +00001318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320
1321 /* Return success if all addresses were found. */
Joe Perches123031c2010-09-08 11:04:21 +00001322 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324
1325/* Populate the verification/tie tags based on overlapping INIT
1326 * scenario.
1327 *
1328 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1329 */
1330static void sctp_tietags_populate(struct sctp_association *new_asoc,
1331 const struct sctp_association *asoc)
1332{
1333 switch (asoc->state) {
1334
1335 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1336
1337 case SCTP_STATE_COOKIE_WAIT:
1338 new_asoc->c.my_vtag = asoc->c.my_vtag;
1339 new_asoc->c.my_ttag = asoc->c.my_vtag;
1340 new_asoc->c.peer_ttag = 0;
1341 break;
1342
1343 case SCTP_STATE_COOKIE_ECHOED:
1344 new_asoc->c.my_vtag = asoc->c.my_vtag;
1345 new_asoc->c.my_ttag = asoc->c.my_vtag;
1346 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1347 break;
1348
1349 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1350 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1351 */
1352 default:
1353 new_asoc->c.my_ttag = asoc->c.my_vtag;
1354 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1355 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 /* Other parameters for the endpoint SHOULD be copied from the
1359 * existing parameters of the association (e.g. number of
1360 * outbound streams) into the INIT ACK and cookie.
1361 */
1362 new_asoc->rwnd = asoc->rwnd;
1363 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1364 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1365 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1366}
1367
1368/*
1369 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1370 * handling action.
1371 *
1372 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1373 *
1374 * Returns value representing action to be taken. These action values
1375 * correspond to Action/Description values in RFC 2960, Table 2.
1376 */
1377static char sctp_tietags_compare(struct sctp_association *new_asoc,
1378 const struct sctp_association *asoc)
1379{
1380 /* In this case, the peer may have restarted. */
1381 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1382 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1383 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1384 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1385 return 'A';
1386
1387 /* Collision case B. */
1388 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1389 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1390 (0 == asoc->c.peer_vtag))) {
1391 return 'B';
1392 }
1393
1394 /* Collision case D. */
1395 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1396 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1397 return 'D';
1398
1399 /* Collision case C. */
1400 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1401 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1402 (0 == new_asoc->c.my_ttag) &&
1403 (0 == new_asoc->c.peer_ttag))
1404 return 'C';
1405
1406 /* No match to any of the special cases; discard this packet. */
1407 return 'E';
1408}
1409
1410/* Common helper routine for both duplicate and simulataneous INIT
1411 * chunk handling.
1412 */
1413static sctp_disposition_t sctp_sf_do_unexpected_init(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001414 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 const struct sctp_endpoint *ep,
1416 const struct sctp_association *asoc,
1417 const sctp_subtype_t type,
1418 void *arg, sctp_cmd_seq_t *commands)
1419{
Xin Long62e6b7e2017-07-23 09:34:28 +08001420 struct sctp_chunk *chunk = arg, *repl, *err_chunk;
1421 struct sctp_unrecognized_param *unk_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 struct sctp_association *new_asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 struct sctp_packet *packet;
Xin Long62e6b7e2017-07-23 09:34:28 +08001424 sctp_disposition_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 int len;
1426
1427 /* 6.10 Bundling
1428 * An endpoint MUST NOT bundle INIT, INIT ACK or
1429 * SHUTDOWN COMPLETE with any other chunks.
1430 *
1431 * IG Section 2.11.2
1432 * Furthermore, we require that the receiver of an INIT chunk MUST
1433 * enforce these rules by silently discarding an arriving packet
1434 * with an INIT chunk that is bundled with other chunks.
1435 */
1436 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001437 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001440 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 */
1442 if (chunk->sctp_hdr->vtag != 0)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001443 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 /* Make sure that the INIT chunk has a valid length.
1446 * In this case, we generate a protocol violation since we have
1447 * an association established.
1448 */
Xin Long01a992b2017-06-30 11:52:22 +08001449 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001450 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 commands);
1452 /* Grab the INIT header. */
Xin Long4ae70c02017-06-30 11:52:21 +08001453 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 /* Tag the variable length parameters. */
Xin Long4ae70c02017-06-30 11:52:21 +08001456 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 /* Verify the INIT chunk before processing it. */
1459 err_chunk = NULL;
Vlad Yasevichb14878c2014-04-17 17:26:50 +02001460 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Xin Long01a992b2017-06-30 11:52:22 +08001461 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 &err_chunk)) {
1463 /* This chunk contains fatal error. It is to be discarded.
1464 * Send an ABORT, with causes if there is any.
1465 */
1466 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001467 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 (__u8 *)(err_chunk->chunk_hdr) +
Xin Long922dbc52017-06-30 11:52:13 +08001469 sizeof(struct sctp_chunkhdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 ntohs(err_chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +08001471 sizeof(struct sctp_chunkhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 if (packet) {
1474 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1475 SCTP_PACKET(packet));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001476 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 retval = SCTP_DISPOSITION_CONSUME;
1478 } else {
1479 retval = SCTP_DISPOSITION_NOMEM;
1480 }
1481 goto cleanup;
1482 } else {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001483 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 commands);
1485 }
1486 }
1487
1488 /*
1489 * Other parameters for the endpoint SHOULD be copied from the
1490 * existing parameters of the association (e.g. number of
1491 * outbound streams) into the INIT ACK and cookie.
1492 * FIXME: We are copying parameters from the endpoint not the
1493 * association.
1494 */
1495 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1496 if (!new_asoc)
1497 goto nomem;
1498
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001499 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1500 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1501 goto nomem;
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 /* In the outbound INIT ACK the endpoint MUST copy its current
1504 * Verification Tag and Peers Verification tag into a reserved
1505 * place (local tie-tag and per tie-tag) within the state cookie.
1506 */
Wei Yongjunde6becdc02011-04-19 21:30:51 +00001507 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Xin Long01a992b2017-06-30 11:52:22 +08001508 (struct sctp_init_chunk *)chunk->chunk_hdr,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001509 GFP_ATOMIC))
1510 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 /* Make sure no new addresses are being added during the
1513 * restart. Do not do this check for COOKIE-WAIT state,
1514 * since there are no peer addresses to check against.
1515 * Upon return an ABORT will have been sent if needed.
1516 */
1517 if (!sctp_state(asoc, COOKIE_WAIT)) {
1518 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1519 commands)) {
1520 retval = SCTP_DISPOSITION_CONSUME;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001521 goto nomem_retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
1523 }
1524
1525 sctp_tietags_populate(new_asoc, asoc);
1526
1527 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1528
1529 /* If there are errors need to be reported for unknown parameters,
1530 * make sure to reserve enough room in the INIT ACK for them.
1531 */
1532 len = 0;
1533 if (err_chunk) {
1534 len = ntohs(err_chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +08001535 sizeof(struct sctp_chunkhdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 }
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1539 if (!repl)
1540 goto nomem;
1541
1542 /* If there are errors need to be reported for unknown parameters,
1543 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1544 * parameter.
1545 */
1546 if (err_chunk) {
1547 /* Get the "Unrecognized parameter" parameter(s) out of the
1548 * ERROR chunk generated by sctp_verify_init(). Since the
1549 * error cause code for "unknown parameter" and the
1550 * "Unrecognized parameter" type is the same, we can
1551 * construct the parameters in INIT ACK by copying the
1552 * ERROR causes over.
1553 */
Xin Long62e6b7e2017-07-23 09:34:28 +08001554 unk_param = (struct sctp_unrecognized_param *)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 ((__u8 *)(err_chunk->chunk_hdr) +
Xin Long922dbc52017-06-30 11:52:13 +08001556 sizeof(struct sctp_chunkhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 /* Replace the cause code with the "Unrecognized parameter"
1558 * parameter type.
1559 */
1560 sctp_addto_chunk(repl, len, unk_param);
1561 }
1562
1563 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1564 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1565
1566 /*
1567 * Note: After sending out INIT ACK with the State Cookie parameter,
1568 * "Z" MUST NOT allocate any resources for this new association.
1569 * Otherwise, "Z" will be vulnerable to resource attacks.
1570 */
1571 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1572 retval = SCTP_DISPOSITION_CONSUME;
1573
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001574 return retval;
1575
1576nomem:
1577 retval = SCTP_DISPOSITION_NOMEM;
1578nomem_retval:
1579 if (new_asoc)
1580 sctp_association_free(new_asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581cleanup:
1582 if (err_chunk)
1583 sctp_chunk_free(err_chunk);
1584 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585}
1586
1587/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001588 * Handle simultaneous INIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 * This means we started an INIT and then we got an INIT request from
1590 * our peer.
1591 *
1592 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1593 * This usually indicates an initialization collision, i.e., each
1594 * endpoint is attempting, at about the same time, to establish an
1595 * association with the other endpoint.
1596 *
1597 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1598 * endpoint MUST respond with an INIT ACK using the same parameters it
1599 * sent in its original INIT chunk (including its Verification Tag,
1600 * unchanged). These original parameters are combined with those from the
1601 * newly received INIT chunk. The endpoint shall also generate a State
1602 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1603 * INIT to calculate the State Cookie.
1604 *
1605 * After that, the endpoint MUST NOT change its state, the T1-init
1606 * timer shall be left running and the corresponding TCB MUST NOT be
1607 * destroyed. The normal procedures for handling State Cookies when
1608 * a TCB exists will resolve the duplicate INITs to a single association.
1609 *
1610 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1611 * its Tie-Tags with the Tag information of itself and its peer (see
1612 * section 5.2.2 for a description of the Tie-Tags).
1613 *
1614 * Verification Tag: Not explicit, but an INIT can not have a valid
1615 * verification tag, so we skip the check.
1616 *
1617 * Inputs
1618 * (endpoint, asoc, chunk)
1619 *
1620 * Outputs
1621 * (asoc, reply_msg, msg_up, timers, counters)
1622 *
1623 * The return value is the disposition of the chunk.
1624 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001625sctp_disposition_t sctp_sf_do_5_2_1_siminit(struct net *net,
1626 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 const struct sctp_association *asoc,
1628 const sctp_subtype_t type,
1629 void *arg,
1630 sctp_cmd_seq_t *commands)
1631{
1632 /* Call helper to do the real work for both simulataneous and
1633 * duplicate INIT chunk handling.
1634 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001635 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636}
1637
1638/*
1639 * Handle duplicated INIT messages. These are usually delayed
1640 * restransmissions.
1641 *
1642 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1643 * COOKIE-ECHOED and COOKIE-WAIT
1644 *
1645 * Unless otherwise stated, upon reception of an unexpected INIT for
1646 * this association, the endpoint shall generate an INIT ACK with a
1647 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1648 * current Verification Tag and peer's Verification Tag into a reserved
1649 * place within the state cookie. We shall refer to these locations as
1650 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1651 * containing this INIT ACK MUST carry a Verification Tag value equal to
1652 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1653 * MUST contain a new Initiation Tag (randomly generated see Section
1654 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1655 * existing parameters of the association (e.g. number of outbound
1656 * streams) into the INIT ACK and cookie.
1657 *
1658 * After sending out the INIT ACK, the endpoint shall take no further
1659 * actions, i.e., the existing association, including its current state,
1660 * and the corresponding TCB MUST NOT be changed.
1661 *
1662 * Note: Only when a TCB exists and the association is not in a COOKIE-
1663 * WAIT state are the Tie-Tags populated. For a normal association INIT
1664 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1665 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1666 * State Cookie are populated as specified in section 5.2.1.
1667 *
1668 * Verification Tag: Not specified, but an INIT has no way of knowing
1669 * what the verification tag could be, so we ignore it.
1670 *
1671 * Inputs
1672 * (endpoint, asoc, chunk)
1673 *
1674 * Outputs
1675 * (asoc, reply_msg, msg_up, timers, counters)
1676 *
1677 * The return value is the disposition of the chunk.
1678 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001679sctp_disposition_t sctp_sf_do_5_2_2_dupinit(struct net *net,
1680 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 const struct sctp_association *asoc,
1682 const sctp_subtype_t type,
1683 void *arg,
1684 sctp_cmd_seq_t *commands)
1685{
1686 /* Call helper to do the real work for both simulataneous and
1687 * duplicate INIT chunk handling.
1688 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001689 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690}
1691
1692
Vlad Yasevich610ab732007-01-15 19:18:30 -08001693/*
1694 * Unexpected INIT-ACK handler.
1695 *
1696 * Section 5.2.3
1697 * If an INIT ACK received by an endpoint in any state other than the
1698 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1699 * An unexpected INIT ACK usually indicates the processing of an old or
1700 * duplicated INIT chunk.
1701*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001702sctp_disposition_t sctp_sf_do_5_2_3_initack(struct net *net,
1703 const struct sctp_endpoint *ep,
Vlad Yasevich610ab732007-01-15 19:18:30 -08001704 const struct sctp_association *asoc,
1705 const sctp_subtype_t type,
1706 void *arg, sctp_cmd_seq_t *commands)
1707{
1708 /* Per the above section, we'll discard the chunk if we have an
1709 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1710 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001711 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001712 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001713 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001714 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001715}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717/* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1718 *
1719 * Section 5.2.4
1720 * A) In this case, the peer may have restarted.
1721 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001722static sctp_disposition_t sctp_sf_do_dupcook_a(struct net *net,
1723 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 const struct sctp_association *asoc,
1725 struct sctp_chunk *chunk,
1726 sctp_cmd_seq_t *commands,
1727 struct sctp_association *new_asoc)
1728{
Xin Long01a992b2017-06-30 11:52:22 +08001729 struct sctp_init_chunk *peer_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 struct sctp_ulpevent *ev;
1731 struct sctp_chunk *repl;
1732 struct sctp_chunk *err;
1733 sctp_disposition_t disposition;
1734
1735 /* new_asoc is a brand-new association, so these are not yet
1736 * side effects--it is safe to run them here.
1737 */
1738 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1739
Wei Yongjunde6becdc02011-04-19 21:30:51 +00001740 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 GFP_ATOMIC))
1742 goto nomem;
1743
1744 /* Make sure no new addresses are being added during the
1745 * restart. Though this is a pretty complicated attack
1746 * since you'd have to get inside the cookie.
1747 */
1748 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1749 return SCTP_DISPOSITION_CONSUME;
1750 }
1751
1752 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1753 * the peer has restarted (Action A), it MUST NOT setup a new
1754 * association but instead resend the SHUTDOWN ACK and send an ERROR
1755 * chunk with a "Cookie Received while Shutting Down" error cause to
1756 * its peer.
1757 */
1758 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001759 disposition = sctp_sf_do_9_2_reshutack(net, ep, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1761 chunk, commands);
1762 if (SCTP_DISPOSITION_NOMEM == disposition)
1763 goto nomem;
1764
1765 err = sctp_make_op_error(asoc, chunk,
1766 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001767 NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 if (err)
1769 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1770 SCTP_CHUNK(err));
1771
1772 return SCTP_DISPOSITION_CONSUME;
1773 }
1774
Wei Yongjuna000c012011-05-29 23:23:36 +00001775 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1776 * data. Consider the optional choice of resending of this data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 */
Wei Yongjuna000c012011-05-29 23:23:36 +00001778 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1779 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1780 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1782
Wei Yongjuna000c012011-05-29 23:23:36 +00001783 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1784 * and ASCONF-ACK cache.
1785 */
1786 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1787 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1788 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 repl = sctp_make_cookie_ack(new_asoc, chunk);
1791 if (!repl)
1792 goto nomem;
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 /* Report association restart to upper layer. */
1795 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1796 new_asoc->c.sinit_num_ostreams,
1797 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07001798 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 if (!ev)
1800 goto nomem_ev;
1801
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001802 /* Update the content of current association. */
1803 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
Vlad Yasevichbdf6fa52014-10-03 18:16:20 -04001805 if (sctp_state(asoc, SHUTDOWN_PENDING) &&
1806 (sctp_sstate(asoc->base.sk, CLOSING) ||
1807 sock_flag(asoc->base.sk, SOCK_DEAD))) {
1808 /* if were currently in SHUTDOWN_PENDING, but the socket
1809 * has been closed by user, don't transition to ESTABLISHED.
1810 * Instead trigger SHUTDOWN bundled with COOKIE_ACK.
1811 */
1812 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1813 return sctp_sf_do_9_2_start_shutdown(net, ep, asoc,
1814 SCTP_ST_CHUNK(0), NULL,
1815 commands);
1816 } else {
1817 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1818 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1819 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 return SCTP_DISPOSITION_CONSUME;
1822
1823nomem_ev:
1824 sctp_chunk_free(repl);
1825nomem:
1826 return SCTP_DISPOSITION_NOMEM;
1827}
1828
1829/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1830 *
1831 * Section 5.2.4
1832 * B) In this case, both sides may be attempting to start an association
1833 * at about the same time but the peer endpoint started its INIT
1834 * after responding to the local endpoint's INIT
1835 */
1836/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001837static sctp_disposition_t sctp_sf_do_dupcook_b(struct net *net,
1838 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 const struct sctp_association *asoc,
1840 struct sctp_chunk *chunk,
1841 sctp_cmd_seq_t *commands,
1842 struct sctp_association *new_asoc)
1843{
Xin Long01a992b2017-06-30 11:52:22 +08001844 struct sctp_init_chunk *peer_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 struct sctp_chunk *repl;
1846
1847 /* new_asoc is a brand-new association, so these are not yet
1848 * side effects--it is safe to run them here.
1849 */
1850 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
Wei Yongjunde6becdc02011-04-19 21:30:51 +00001851 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 GFP_ATOMIC))
1853 goto nomem;
1854
1855 /* Update the content of current association. */
1856 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1857 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1858 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001859 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1861
1862 repl = sctp_make_cookie_ack(new_asoc, chunk);
1863 if (!repl)
1864 goto nomem;
1865
1866 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868 /* RFC 2960 5.1 Normal Establishment of an Association
1869 *
1870 * D) IMPLEMENTATION NOTE: An implementation may choose to
1871 * send the Communication Up notification to the SCTP user
1872 * upon reception of a valid COOKIE ECHO chunk.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001873 *
1874 * Sadly, this needs to be implemented as a side-effect, because
1875 * we are not guaranteed to have set the association id of the real
1876 * association and so these notifications need to be delayed until
1877 * the association id is allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Vlad Yasevich07d93962007-05-04 13:55:27 -07001880 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
1882 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001883 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001885 * peers requested adaptation layer.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001886 *
1887 * This also needs to be done as a side effect for the same reason as
1888 * above.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 */
Vlad Yasevich07d93962007-05-04 13:55:27 -07001890 if (asoc->peer.adaptation_ind)
1891 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893 return SCTP_DISPOSITION_CONSUME;
1894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895nomem:
1896 return SCTP_DISPOSITION_NOMEM;
1897}
1898
1899/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1900 *
1901 * Section 5.2.4
1902 * C) In this case, the local endpoint's cookie has arrived late.
1903 * Before it arrived, the local endpoint sent an INIT and received an
1904 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1905 * but a new tag of its own.
1906 */
1907/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001908static sctp_disposition_t sctp_sf_do_dupcook_c(struct net *net,
1909 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 const struct sctp_association *asoc,
1911 struct sctp_chunk *chunk,
1912 sctp_cmd_seq_t *commands,
1913 struct sctp_association *new_asoc)
1914{
1915 /* The cookie should be silently discarded.
1916 * The endpoint SHOULD NOT change states and should leave
1917 * any timers running.
1918 */
1919 return SCTP_DISPOSITION_DISCARD;
1920}
1921
1922/* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1923 *
1924 * Section 5.2.4
1925 *
1926 * D) When both local and remote tags match the endpoint should always
1927 * enter the ESTABLISHED state, if it has not already done so.
1928 */
1929/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001930static sctp_disposition_t sctp_sf_do_dupcook_d(struct net *net,
1931 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 const struct sctp_association *asoc,
1933 struct sctp_chunk *chunk,
1934 sctp_cmd_seq_t *commands,
1935 struct sctp_association *new_asoc)
1936{
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001937 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 struct sctp_chunk *repl;
1939
1940 /* Clarification from Implementor's Guide:
1941 * D) When both local and remote tags match the endpoint should
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001942 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1943 * It should stop any cookie timer that may be running and send
1944 * a COOKIE ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 */
1946
1947 /* Don't accidentally move back into established state. */
1948 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1949 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1950 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1951 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1952 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001953 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1955 SCTP_NULL());
1956
1957 /* RFC 2960 5.1 Normal Establishment of an Association
1958 *
1959 * D) IMPLEMENTATION NOTE: An implementation may choose
1960 * to send the Communication Up notification to the
1961 * SCTP user upon reception of a valid COOKIE
1962 * ECHO chunk.
1963 */
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001964 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 SCTP_COMM_UP, 0,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001966 asoc->c.sinit_num_ostreams,
1967 asoc->c.sinit_max_instreams,
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09001968 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 if (!ev)
1970 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
1972 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001973 * When a peer sends a Adaptation Layer Indication parameter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 * SCTP delivers this notification to inform the application
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001975 * that of the peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001977 if (asoc->peer.adaptation_ind) {
1978 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001980 if (!ai_ev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 goto nomem;
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
1984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 repl = sctp_make_cookie_ack(new_asoc, chunk);
1987 if (!repl)
1988 goto nomem;
1989
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07001990 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1991
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001992 if (ev)
1993 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1994 SCTP_ULPEVENT(ev));
1995 if (ai_ev)
1996 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1997 SCTP_ULPEVENT(ai_ev));
1998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 return SCTP_DISPOSITION_CONSUME;
2000
2001nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07002002 if (ai_ev)
2003 sctp_ulpevent_free(ai_ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 if (ev)
2005 sctp_ulpevent_free(ev);
2006 return SCTP_DISPOSITION_NOMEM;
2007}
2008
2009/*
2010 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
2011 * chunk was retransmitted and then delayed in the network.
2012 *
2013 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
2014 *
2015 * Verification Tag: None. Do cookie validation.
2016 *
2017 * Inputs
2018 * (endpoint, asoc, chunk)
2019 *
2020 * Outputs
2021 * (asoc, reply_msg, msg_up, timers, counters)
2022 *
2023 * The return value is the disposition of the chunk.
2024 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002025sctp_disposition_t sctp_sf_do_5_2_4_dupcook(struct net *net,
2026 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 const struct sctp_association *asoc,
2028 const sctp_subtype_t type,
2029 void *arg,
2030 sctp_cmd_seq_t *commands)
2031{
2032 sctp_disposition_t retval;
2033 struct sctp_chunk *chunk = arg;
2034 struct sctp_association *new_asoc;
2035 int error = 0;
2036 char action;
2037 struct sctp_chunk *err_chk_p;
2038
2039 /* Make sure that the chunk has a valid length from the protocol
2040 * perspective. In this case check to make sure we have at least
2041 * enough for the chunk header. Cookie length verification is
2042 * done later.
2043 */
Xin Long922dbc52017-06-30 11:52:13 +08002044 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002045 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 commands);
2047
2048 /* "Decode" the chunk. We have no optional parameters so we
2049 * are in good shape.
2050 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002051 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -07002052 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +08002053 sizeof(struct sctp_chunkhdr)))
Sridhar Samudrala62b08082006-05-05 17:04:43 -07002054 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
2056 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2057 * of a duplicate COOKIE ECHO match the Verification Tags of the
2058 * current association, consider the State Cookie valid even if
2059 * the lifespan is exceeded.
2060 */
2061 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2062 &err_chk_p);
2063
2064 /* FIXME:
2065 * If the re-build failed, what is the proper error path
2066 * from here?
2067 *
2068 * [We should abort the association. --piggy]
2069 */
2070 if (!new_asoc) {
2071 /* FIXME: Several errors are possible. A bad cookie should
2072 * be silently discarded, but think about logging it too.
2073 */
2074 switch (error) {
2075 case -SCTP_IERROR_NOMEM:
2076 goto nomem;
2077
2078 case -SCTP_IERROR_STALE_COOKIE:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002079 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 err_chk_p);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002081 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 case -SCTP_IERROR_BAD_SIG:
2083 default:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002084 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
2087
Xin Long7e062972017-05-23 13:28:55 +08002088 /* Set temp so that it won't be added into hashtable */
2089 new_asoc->temp = 1;
2090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 /* Compare the tie_tag in cookie with the verification tag of
2092 * current association.
2093 */
2094 action = sctp_tietags_compare(new_asoc, asoc);
2095
2096 switch (action) {
2097 case 'A': /* Association restart. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002098 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 new_asoc);
2100 break;
2101
2102 case 'B': /* Collision case B. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002103 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 new_asoc);
2105 break;
2106
2107 case 'C': /* Collision case C. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002108 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 new_asoc);
2110 break;
2111
2112 case 'D': /* Collision case D. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002113 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 new_asoc);
2115 break;
2116
2117 default: /* Discard packet for all others. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002118 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122 /* Delete the tempory new association. */
Vlad Yasevichf2815632013-03-12 15:53:23 +00002123 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2125
Max Matveevd5ccd492011-08-29 21:02:24 +00002126 /* Restore association pointer to provide SCTP command interpeter
2127 * with a valid context in case it needs to manipulate
2128 * the queues */
2129 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2130 SCTP_ASOC((struct sctp_association *)asoc));
2131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 return retval;
2133
2134nomem:
2135 return SCTP_DISPOSITION_NOMEM;
2136}
2137
2138/*
2139 * Process an ABORT. (SHUTDOWN-PENDING state)
2140 *
2141 * See sctp_sf_do_9_1_abort().
2142 */
2143sctp_disposition_t sctp_sf_shutdown_pending_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002144 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 const struct sctp_endpoint *ep,
2146 const struct sctp_association *asoc,
2147 const sctp_subtype_t type,
2148 void *arg,
2149 sctp_cmd_seq_t *commands)
2150{
2151 struct sctp_chunk *chunk = arg;
2152
2153 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002154 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 /* Make sure that the ABORT chunk has a valid length.
2157 * Since this is an ABORT chunk, we have to discard it
2158 * because of the following text:
2159 * RFC 2960, Section 3.3.7
2160 * If an endpoint receives an ABORT with a format error or for an
2161 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002162 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 * as we do not know its true length. So, to be safe, discard the
2164 * packet.
2165 */
2166 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002167 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Vlad Yasevich75205f42007-12-20 14:12:59 -08002169 /* ADD-IP: Special case for ABORT chunks
2170 * F4) One special consideration is that ABORT Chunks arriving
2171 * destined to the IP address being deleted MUST be
2172 * ignored (see Section 5.3.1 for further details).
2173 */
2174 if (SCTP_ADDR_DEL ==
2175 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002176 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002177
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002178 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
2180
2181/*
2182 * Process an ABORT. (SHUTDOWN-SENT state)
2183 *
2184 * See sctp_sf_do_9_1_abort().
2185 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002186sctp_disposition_t sctp_sf_shutdown_sent_abort(struct net *net,
2187 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 const struct sctp_association *asoc,
2189 const sctp_subtype_t type,
2190 void *arg,
2191 sctp_cmd_seq_t *commands)
2192{
2193 struct sctp_chunk *chunk = arg;
2194
2195 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002196 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198 /* Make sure that the ABORT chunk has a valid length.
2199 * Since this is an ABORT chunk, we have to discard it
2200 * because of the following text:
2201 * RFC 2960, Section 3.3.7
2202 * If an endpoint receives an ABORT with a format error or for an
2203 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002204 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 * as we do not know its true length. So, to be safe, discard the
2206 * packet.
2207 */
2208 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002209 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Vlad Yasevich75205f42007-12-20 14:12:59 -08002211 /* ADD-IP: Special case for ABORT chunks
2212 * F4) One special consideration is that ABORT Chunks arriving
2213 * destined to the IP address being deleted MUST be
2214 * ignored (see Section 5.3.1 for further details).
2215 */
2216 if (SCTP_ADDR_DEL ==
2217 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002218 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 /* Stop the T2-shutdown timer. */
2221 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2222 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2223
2224 /* Stop the T5-shutdown guard timer. */
2225 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2226 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2227
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002228 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229}
2230
2231/*
2232 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2233 *
2234 * See sctp_sf_do_9_1_abort().
2235 */
2236sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002237 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 const struct sctp_endpoint *ep,
2239 const struct sctp_association *asoc,
2240 const sctp_subtype_t type,
2241 void *arg,
2242 sctp_cmd_seq_t *commands)
2243{
2244 /* The same T2 timer, so we should be able to use
2245 * common function with the SHUTDOWN-SENT state.
2246 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002247 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248}
2249
2250/*
2251 * Handle an Error received in COOKIE_ECHOED state.
2252 *
2253 * Only handle the error type of stale COOKIE Error, the other errors will
2254 * be ignored.
2255 *
2256 * Inputs
2257 * (endpoint, asoc, chunk)
2258 *
2259 * Outputs
2260 * (asoc, reply_msg, msg_up, timers, counters)
2261 *
2262 * The return value is the disposition of the chunk.
2263 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002264sctp_disposition_t sctp_sf_cookie_echoed_err(struct net *net,
2265 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 const struct sctp_association *asoc,
2267 const sctp_subtype_t type,
2268 void *arg,
2269 sctp_cmd_seq_t *commands)
2270{
2271 struct sctp_chunk *chunk = arg;
2272 sctp_errhdr_t *err;
2273
2274 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002275 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
2277 /* Make sure that the ERROR chunk has a valid length.
2278 * The parameter walking depends on this as well.
2279 */
2280 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002281 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 commands);
2283
2284 /* Process the error here */
2285 /* FUTURE FIXME: When PR-SCTP related and other optional
2286 * parms are emitted, this will have to change to handle multiple
2287 * errors.
2288 */
2289 sctp_walk_errors(err, chunk->chunk_hdr) {
2290 if (SCTP_ERROR_STALE_COOKIE == err->cause)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002291 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 arg, commands);
2293 }
2294
2295 /* It is possible to have malformed error causes, and that
2296 * will cause us to end the walk early. However, since
2297 * we are discarding the packet, there should be no adverse
2298 * affects.
2299 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002300 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301}
2302
2303/*
2304 * Handle a Stale COOKIE Error
2305 *
2306 * Section: 5.2.6 Handle Stale COOKIE Error
2307 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2308 * one of the following three alternatives.
2309 * ...
2310 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2311 * Preservative parameter requesting an extension to the lifetime of
2312 * the State Cookie. When calculating the time extension, an
2313 * implementation SHOULD use the RTT information measured based on the
2314 * previous COOKIE ECHO / ERROR exchange, and should add no more
2315 * than 1 second beyond the measured RTT, due to long State Cookie
2316 * lifetimes making the endpoint more subject to a replay attack.
2317 *
2318 * Verification Tag: Not explicit, but safe to ignore.
2319 *
2320 * Inputs
2321 * (endpoint, asoc, chunk)
2322 *
2323 * Outputs
2324 * (asoc, reply_msg, msg_up, timers, counters)
2325 *
2326 * The return value is the disposition of the chunk.
2327 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002328static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
2329 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 const struct sctp_association *asoc,
2331 const sctp_subtype_t type,
2332 void *arg,
2333 sctp_cmd_seq_t *commands)
2334{
Frank Filz3f7a87d2005-06-20 13:14:57 -07002335 int attempts = asoc->init_err_counter + 1;
Xin Long365ddb62017-07-17 11:29:51 +08002336 struct sctp_chunk *chunk = arg, *reply;
2337 struct sctp_cookie_preserve_param bht;
2338 struct sctp_bind_addr *bp;
2339 sctp_errhdr_t *err;
2340 u32 stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Vlad Yasevich81845c22006-01-30 15:59:54 -08002342 if (attempts > asoc->max_init_attempts) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002343 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2344 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002346 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 return SCTP_DISPOSITION_DELETE_TCB;
2348 }
2349
2350 err = (sctp_errhdr_t *)(chunk->skb->data);
2351
2352 /* When calculating the time extension, an implementation
2353 * SHOULD use the RTT information measured based on the
2354 * previous COOKIE ECHO / ERROR exchange, and should add no
2355 * more than 1 second beyond the measured RTT, due to long
2356 * State Cookie lifetimes making the endpoint more subject to
2357 * a replay attack.
2358 * Measure of Staleness's unit is usec. (1/1000000 sec)
2359 * Suggested Cookie Life-span Increment's unit is msec.
2360 * (1/1000 sec)
2361 * In general, if you use the suggested cookie life, the value
2362 * found in the field of measure of staleness should be doubled
2363 * to give ample time to retransmit the new cookie and thus
2364 * yield a higher probability of success on the reattempt.
2365 */
Al Viro34bcca22006-11-20 17:27:15 -08002366 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 stale = (stale * 2) / 1000;
2368
2369 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2370 bht.param_hdr.length = htons(sizeof(bht));
2371 bht.lifespan_increment = htonl(stale);
2372
2373 /* Build that new INIT chunk. */
2374 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2375 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2376 if (!reply)
2377 goto nomem;
2378
2379 sctp_addto_chunk(reply, sizeof(bht), &bht);
2380
2381 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2382 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2383
2384 /* Stop pending T3-rtx and heartbeat timers */
2385 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2386 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2387
2388 /* Delete non-primary peer ip addresses since we are transitioning
2389 * back to the COOKIE-WAIT state
2390 */
2391 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2392
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002393 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2394 * resend
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 */
Vlad Yasevichb6157d82007-10-24 15:59:16 -04002396 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 SCTP_TRANSPORT(asoc->peer.primary_path));
2398
2399 /* Cast away the const modifier, as we want to just
2400 * rerun it through as a sideffect.
2401 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07002402 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2405 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2406 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2407 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2408 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2409 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2410
2411 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2412
2413 return SCTP_DISPOSITION_CONSUME;
2414
2415nomem:
2416 return SCTP_DISPOSITION_NOMEM;
2417}
2418
2419/*
2420 * Process an ABORT.
2421 *
2422 * Section: 9.1
2423 * After checking the Verification Tag, the receiving endpoint shall
2424 * remove the association from its record, and shall report the
2425 * termination to its upper layer.
2426 *
2427 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2428 * B) Rules for packet carrying ABORT:
2429 *
2430 * - The endpoint shall always fill in the Verification Tag field of the
2431 * outbound packet with the destination endpoint's tag value if it
2432 * is known.
2433 *
2434 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2435 * MUST follow the procedure described in Section 8.4.
2436 *
2437 * - The receiver MUST accept the packet if the Verification Tag
2438 * matches either its own tag, OR the tag of its peer. Otherwise, the
2439 * receiver MUST silently discard the packet and take no further
2440 * action.
2441 *
2442 * Inputs
2443 * (endpoint, asoc, chunk)
2444 *
2445 * Outputs
2446 * (asoc, reply_msg, msg_up, timers, counters)
2447 *
2448 * The return value is the disposition of the chunk.
2449 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002450sctp_disposition_t sctp_sf_do_9_1_abort(struct net *net,
2451 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 const struct sctp_association *asoc,
2453 const sctp_subtype_t type,
2454 void *arg,
2455 sctp_cmd_seq_t *commands)
2456{
2457 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
2459 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002460 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
2462 /* Make sure that the ABORT chunk has a valid length.
2463 * Since this is an ABORT chunk, we have to discard it
2464 * because of the following text:
2465 * RFC 2960, Section 3.3.7
2466 * If an endpoint receives an ABORT with a format error or for an
2467 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002468 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 * as we do not know its true length. So, to be safe, discard the
2470 * packet.
2471 */
2472 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002473 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Vlad Yasevich75205f42007-12-20 14:12:59 -08002475 /* ADD-IP: Special case for ABORT chunks
2476 * F4) One special consideration is that ABORT Chunks arriving
2477 * destined to the IP address being deleted MUST be
2478 * ignored (see Section 5.3.1 for further details).
2479 */
2480 if (SCTP_ADDR_DEL ==
2481 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002482 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002483
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002484 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002485}
2486
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002487static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
2488 const struct sctp_endpoint *ep,
Vlad Yasevich75205f42007-12-20 14:12:59 -08002489 const struct sctp_association *asoc,
2490 const sctp_subtype_t type,
2491 void *arg,
2492 sctp_cmd_seq_t *commands)
2493{
2494 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002495 unsigned int len;
Vlad Yasevich75205f42007-12-20 14:12:59 -08002496 __be16 error = SCTP_ERROR_NO_ERROR;
2497
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 /* See if we have an error cause code in the chunk. */
2499 len = ntohs(chunk->chunk_hdr->length);
Shan Wei96ca4682011-04-19 21:26:26 +00002500 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr)) {
2501
2502 sctp_errhdr_t *err;
2503 sctp_walk_errors(err, chunk->chunk_hdr);
2504 if ((void *)err != (void *)chunk->chunk_end)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002505 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Shan Wei96ca4682011-04-19 21:26:26 +00002506
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
Shan Wei96ca4682011-04-19 21:26:26 +00002508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002510 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002511 /* ASSOC_FAILED will DELETE_TCB. */
Al Viro5be291f2006-11-20 17:01:06 -08002512 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00002513 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2514 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
2516 return SCTP_DISPOSITION_ABORT;
2517}
2518
2519/*
2520 * Process an ABORT. (COOKIE-WAIT state)
2521 *
2522 * See sctp_sf_do_9_1_abort() above.
2523 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002524sctp_disposition_t sctp_sf_cookie_wait_abort(struct net *net,
2525 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 const struct sctp_association *asoc,
2527 const sctp_subtype_t type,
2528 void *arg,
2529 sctp_cmd_seq_t *commands)
2530{
2531 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002532 unsigned int len;
Al Virof94c0192006-11-20 17:00:25 -08002533 __be16 error = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
2535 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002536 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
2538 /* Make sure that the ABORT chunk has a valid length.
2539 * Since this is an ABORT chunk, we have to discard it
2540 * because of the following text:
2541 * RFC 2960, Section 3.3.7
2542 * If an endpoint receives an ABORT with a format error or for an
2543 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002544 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 * as we do not know its true length. So, to be safe, discard the
2546 * packet.
2547 */
2548 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002549 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
2551 /* See if we have an error cause code in the chunk. */
2552 len = ntohs(chunk->chunk_hdr->length);
2553 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2554 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2555
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002556 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002557 chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558}
2559
2560/*
2561 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2562 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002563sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(struct net *net,
2564 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 const struct sctp_association *asoc,
2566 const sctp_subtype_t type,
2567 void *arg,
2568 sctp_cmd_seq_t *commands)
2569{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002570 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002571 ENOPROTOOPT, asoc,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002572 (struct sctp_transport *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573}
2574
2575/*
2576 * Process an ABORT. (COOKIE-ECHOED state)
2577 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002578sctp_disposition_t sctp_sf_cookie_echoed_abort(struct net *net,
2579 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 const struct sctp_association *asoc,
2581 const sctp_subtype_t type,
2582 void *arg,
2583 sctp_cmd_seq_t *commands)
2584{
2585 /* There is a single T1 timer, so we should be able to use
2586 * common function with the COOKIE-WAIT state.
2587 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002588 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589}
2590
2591/*
2592 * Stop T1 timer and abort association with "INIT failed".
2593 *
2594 * This is common code called by several sctp_sf_*_abort() functions above.
2595 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002596static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
2597 sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -08002598 __be16 error, int sk_err,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002599 const struct sctp_association *asoc,
2600 struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
Daniel Borkmannbb333812013-06-28 19:49:40 +02002602 pr_debug("%s: ABORT received (INIT)\n", __func__);
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2605 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002606 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2608 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002609 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 /* CMD_INIT_FAILED will DELETE_TCB. */
2611 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002612 SCTP_PERR(error));
Daniel Borkmannbb333812013-06-28 19:49:40 +02002613
Frank Filz3f7a87d2005-06-20 13:14:57 -07002614 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615}
2616
2617/*
2618 * sctp_sf_do_9_2_shut
2619 *
2620 * Section: 9.2
2621 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2622 * - enter the SHUTDOWN-RECEIVED state,
2623 *
2624 * - stop accepting new data from its SCTP user
2625 *
2626 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2627 * that all its outstanding DATA chunks have been received by the
2628 * SHUTDOWN sender.
2629 *
2630 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2631 * send a SHUTDOWN in response to a ULP request. And should discard
2632 * subsequent SHUTDOWN chunks.
2633 *
2634 * If there are still outstanding DATA chunks left, the SHUTDOWN
2635 * receiver shall continue to follow normal data transmission
2636 * procedures defined in Section 6 until all outstanding DATA chunks
2637 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2638 * new data from its SCTP user.
2639 *
2640 * Verification Tag: 8.5 Verification Tag [Normal verification]
2641 *
2642 * Inputs
2643 * (endpoint, asoc, chunk)
2644 *
2645 * Outputs
2646 * (asoc, reply_msg, msg_up, timers, counters)
2647 *
2648 * The return value is the disposition of the chunk.
2649 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002650sctp_disposition_t sctp_sf_do_9_2_shutdown(struct net *net,
2651 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 const struct sctp_association *asoc,
2653 const sctp_subtype_t type,
2654 void *arg,
2655 sctp_cmd_seq_t *commands)
2656{
2657 struct sctp_chunk *chunk = arg;
2658 sctp_shutdownhdr_t *sdh;
2659 sctp_disposition_t disposition;
2660 struct sctp_ulpevent *ev;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002661 __u32 ctsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
2663 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002664 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
2666 /* Make sure that the SHUTDOWN chunk has a valid length. */
2667 if (!sctp_chunk_length_valid(chunk,
2668 sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002669 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 commands);
2671
2672 /* Convert the elaborate header. */
2673 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2674 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2675 chunk->subh.shutdown_hdr = sdh;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002676 ctsn = ntohl(sdh->cum_tsn_ack);
2677
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002678 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02002679 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2680 asoc->ctsn_ack_point);
2681
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002682 return SCTP_DISPOSITION_DISCARD;
2683 }
2684
Wei Yongjundf10eec2008-10-23 01:00:21 -07002685 /* If Cumulative TSN Ack beyond the max tsn currently
2686 * send, terminating the association and respond to the
2687 * sender with an ABORT.
2688 */
2689 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002690 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002692 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2693 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2694 * inform the application that it should cease sending data.
2695 */
2696 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2697 if (!ev) {
2698 disposition = SCTP_DISPOSITION_NOMEM;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002699 goto out;
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002700 }
2701 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2702
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2704 * - enter the SHUTDOWN-RECEIVED state,
2705 * - stop accepting new data from its SCTP user
2706 *
2707 * [This is implicit in the new state.]
2708 */
2709 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2710 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2711 disposition = SCTP_DISPOSITION_CONSUME;
2712
2713 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002714 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 arg, commands);
2716 }
2717
2718 if (SCTP_DISPOSITION_NOMEM == disposition)
2719 goto out;
2720
2721 /* - verify, by checking the Cumulative TSN Ack field of the
2722 * chunk, that all its outstanding DATA chunks have been
2723 * received by the SHUTDOWN sender.
2724 */
2725 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
Al Viro2178eda2006-11-20 17:26:53 -08002726 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728out:
2729 return disposition;
2730}
2731
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002732/*
2733 * sctp_sf_do_9_2_shut_ctsn
2734 *
2735 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2736 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2737 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2738 * MUST be processed.
2739 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002740sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(struct net *net,
2741 const struct sctp_endpoint *ep,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002742 const struct sctp_association *asoc,
2743 const sctp_subtype_t type,
2744 void *arg,
2745 sctp_cmd_seq_t *commands)
2746{
2747 struct sctp_chunk *chunk = arg;
2748 sctp_shutdownhdr_t *sdh;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002749 __u32 ctsn;
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002750
2751 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002752 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002753
2754 /* Make sure that the SHUTDOWN chunk has a valid length. */
2755 if (!sctp_chunk_length_valid(chunk,
2756 sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002757 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002758 commands);
2759
2760 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002761 ctsn = ntohl(sdh->cum_tsn_ack);
2762
2763 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02002764 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2765 asoc->ctsn_ack_point);
2766
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002767 return SCTP_DISPOSITION_DISCARD;
2768 }
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002769
2770 /* If Cumulative TSN Ack beyond the max tsn currently
2771 * send, terminating the association and respond to the
2772 * sender with an ABORT.
2773 */
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002774 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002775 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002776
2777 /* verify, by checking the Cumulative TSN Ack field of the
2778 * chunk, that all its outstanding DATA chunks have been
2779 * received by the SHUTDOWN sender.
2780 */
2781 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2782 SCTP_BE32(sdh->cum_tsn_ack));
2783
2784 return SCTP_DISPOSITION_CONSUME;
2785}
2786
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787/* RFC 2960 9.2
2788 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2789 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2790 * transport addresses (either in the IP addresses or in the INIT chunk)
2791 * that belong to this association, it should discard the INIT chunk and
2792 * retransmit the SHUTDOWN ACK chunk.
2793 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002794sctp_disposition_t sctp_sf_do_9_2_reshutack(struct net *net,
2795 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 const struct sctp_association *asoc,
2797 const sctp_subtype_t type,
2798 void *arg,
2799 sctp_cmd_seq_t *commands)
2800{
2801 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2802 struct sctp_chunk *reply;
2803
Vlad Yasevichece25df2007-09-07 16:30:54 -04002804 /* Make sure that the chunk has a valid length */
Xin Long922dbc52017-06-30 11:52:13 +08002805 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002806 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04002807 commands);
2808
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 /* Since we are not going to really process this INIT, there
2810 * is no point in verifying chunk boundries. Just generate
2811 * the SHUTDOWN ACK.
2812 */
2813 reply = sctp_make_shutdown_ack(asoc, chunk);
2814 if (NULL == reply)
2815 goto nomem;
2816
2817 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2818 * the T2-SHUTDOWN timer.
2819 */
2820 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2821
2822 /* and restart the T2-shutdown timer. */
2823 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2824 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2825
2826 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2827
2828 return SCTP_DISPOSITION_CONSUME;
2829nomem:
2830 return SCTP_DISPOSITION_NOMEM;
2831}
2832
2833/*
2834 * sctp_sf_do_ecn_cwr
2835 *
2836 * Section: Appendix A: Explicit Congestion Notification
2837 *
2838 * CWR:
2839 *
2840 * RFC 2481 details a specific bit for a sender to send in the header of
2841 * its next outbound TCP segment to indicate to its peer that it has
2842 * reduced its congestion window. This is termed the CWR bit. For
2843 * SCTP the same indication is made by including the CWR chunk.
2844 * This chunk contains one data element, i.e. the TSN number that
2845 * was sent in the ECNE chunk. This element represents the lowest
2846 * TSN number in the datagram that was originally marked with the
2847 * CE bit.
2848 *
2849 * Verification Tag: 8.5 Verification Tag [Normal verification]
2850 * Inputs
2851 * (endpoint, asoc, chunk)
2852 *
2853 * Outputs
2854 * (asoc, reply_msg, msg_up, timers, counters)
2855 *
2856 * The return value is the disposition of the chunk.
2857 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002858sctp_disposition_t sctp_sf_do_ecn_cwr(struct net *net,
2859 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 const struct sctp_association *asoc,
2861 const sctp_subtype_t type,
2862 void *arg,
2863 sctp_cmd_seq_t *commands)
2864{
2865 sctp_cwrhdr_t *cwr;
2866 struct sctp_chunk *chunk = arg;
Al Viro34bcca22006-11-20 17:27:15 -08002867 u32 lowest_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
2869 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002870 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
2872 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002873 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002875
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2877 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2878
Al Viro34bcca22006-11-20 17:27:15 -08002879 lowest_tsn = ntohl(cwr->lowest_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
2881 /* Does this CWR ack the last sent congestion notification? */
Al Viro34bcca22006-11-20 17:27:15 -08002882 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 /* Stop sending ECNE. */
2884 sctp_add_cmd_sf(commands,
2885 SCTP_CMD_ECN_CWR,
Al Viro34bcca22006-11-20 17:27:15 -08002886 SCTP_U32(lowest_tsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 }
2888 return SCTP_DISPOSITION_CONSUME;
2889}
2890
2891/*
2892 * sctp_sf_do_ecne
2893 *
2894 * Section: Appendix A: Explicit Congestion Notification
2895 *
2896 * ECN-Echo
2897 *
2898 * RFC 2481 details a specific bit for a receiver to send back in its
2899 * TCP acknowledgements to notify the sender of the Congestion
2900 * Experienced (CE) bit having arrived from the network. For SCTP this
2901 * same indication is made by including the ECNE chunk. This chunk
2902 * contains one data element, i.e. the lowest TSN associated with the IP
2903 * datagram marked with the CE bit.....
2904 *
2905 * Verification Tag: 8.5 Verification Tag [Normal verification]
2906 * Inputs
2907 * (endpoint, asoc, chunk)
2908 *
2909 * Outputs
2910 * (asoc, reply_msg, msg_up, timers, counters)
2911 *
2912 * The return value is the disposition of the chunk.
2913 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002914sctp_disposition_t sctp_sf_do_ecne(struct net *net,
2915 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 const struct sctp_association *asoc,
2917 const sctp_subtype_t type,
2918 void *arg,
2919 sctp_cmd_seq_t *commands)
2920{
2921 sctp_ecnehdr_t *ecne;
2922 struct sctp_chunk *chunk = arg;
2923
2924 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002925 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
2927 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002928 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 commands);
2930
2931 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2932 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2933
2934 /* If this is a newer ECNE than the last CWR packet we sent out */
2935 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2936 SCTP_U32(ntohl(ecne->lowest_tsn)));
2937
2938 return SCTP_DISPOSITION_CONSUME;
2939}
2940
2941/*
2942 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2943 *
2944 * The SCTP endpoint MUST always acknowledge the reception of each valid
2945 * DATA chunk.
2946 *
2947 * The guidelines on delayed acknowledgement algorithm specified in
2948 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2949 * acknowledgement SHOULD be generated for at least every second packet
2950 * (not every second DATA chunk) received, and SHOULD be generated within
2951 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2952 * situations it may be beneficial for an SCTP transmitter to be more
2953 * conservative than the algorithms detailed in this document allow.
2954 * However, an SCTP transmitter MUST NOT be more aggressive than the
2955 * following algorithms allow.
2956 *
2957 * A SCTP receiver MUST NOT generate more than one SACK for every
2958 * incoming packet, other than to update the offered window as the
2959 * receiving application consumes new data.
2960 *
2961 * Verification Tag: 8.5 Verification Tag [Normal verification]
2962 *
2963 * Inputs
2964 * (endpoint, asoc, chunk)
2965 *
2966 * Outputs
2967 * (asoc, reply_msg, msg_up, timers, counters)
2968 *
2969 * The return value is the disposition of the chunk.
2970 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002971sctp_disposition_t sctp_sf_eat_data_6_2(struct net *net,
2972 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 const struct sctp_association *asoc,
2974 const sctp_subtype_t type,
2975 void *arg,
2976 sctp_cmd_seq_t *commands)
2977{
2978 struct sctp_chunk *chunk = arg;
Wei Yongjun6dc76942009-11-23 15:53:53 -05002979 sctp_arg_t force = SCTP_NOFORCE();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 int error;
2981
2982 if (!sctp_vtag_verify(chunk, asoc)) {
2983 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2984 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002985 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
Xin Long9f8d3142017-06-30 11:52:20 +08002988 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_data_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002989 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 commands);
2991
wangweidongcb3f8372013-12-23 12:16:50 +08002992 error = sctp_eat_data(asoc, chunk, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 switch (error) {
2994 case SCTP_IERROR_NO_ERROR:
2995 break;
2996 case SCTP_IERROR_HIGH_TSN:
2997 case SCTP_IERROR_BAD_STREAM:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002998 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 goto discard_noforce;
3000 case SCTP_IERROR_DUP_TSN:
3001 case SCTP_IERROR_IGNORE_TSN:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003002 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 goto discard_force;
3004 case SCTP_IERROR_NO_DATA:
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02003005 return SCTP_DISPOSITION_ABORT;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003006 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003007 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Xin Long3583df12017-06-30 11:52:19 +08003008 (u8 *)chunk->subh.data_hdr,
3009 sizeof(struct sctp_datahdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 default:
3011 BUG();
3012 }
3013
Wei Yongjun6dc76942009-11-23 15:53:53 -05003014 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
3015 force = SCTP_FORCE();
3016
Neil Horman9f70f462013-12-10 06:48:15 -05003017 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3019 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3020 }
3021
3022 /* If this is the last chunk in a packet, we need to count it
3023 * toward sack generation. Note that we need to SACK every
3024 * OTHER packet containing data chunks, EVEN IF WE DISCARD
3025 * THEM. We elect to NOT generate SACK's if the chunk fails
3026 * the verification tag test.
3027 *
3028 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3029 *
3030 * The SCTP endpoint MUST always acknowledge the reception of
3031 * each valid DATA chunk.
3032 *
3033 * The guidelines on delayed acknowledgement algorithm
3034 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
3035 * Specifically, an acknowledgement SHOULD be generated for at
3036 * least every second packet (not every second DATA chunk)
3037 * received, and SHOULD be generated within 200 ms of the
3038 * arrival of any unacknowledged DATA chunk. In some
3039 * situations it may be beneficial for an SCTP transmitter to
3040 * be more conservative than the algorithms detailed in this
3041 * document allow. However, an SCTP transmitter MUST NOT be
3042 * more aggressive than the following algorithms allow.
3043 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08003044 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003045 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 return SCTP_DISPOSITION_CONSUME;
3048
3049discard_force:
3050 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3051 *
3052 * When a packet arrives with duplicate DATA chunk(s) and with
3053 * no new DATA chunk(s), the endpoint MUST immediately send a
3054 * SACK with no delay. If a packet arrives with duplicate
3055 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3056 * MAY immediately send a SACK. Normally receipt of duplicate
3057 * DATA chunks will occur when the original SACK chunk was lost
3058 * and the peer's RTO has expired. The duplicate TSN number(s)
3059 * SHOULD be reported in the SACK as duplicate.
3060 */
3061 /* In our case, we split the MAY SACK advice up whether or not
3062 * the last chunk is a duplicate.'
3063 */
3064 if (chunk->end_of_packet)
3065 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3066 return SCTP_DISPOSITION_DISCARD;
3067
3068discard_noforce:
Frank Filz52ccb8e2005-12-22 11:36:46 -08003069 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003070 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 return SCTP_DISPOSITION_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073}
3074
3075/*
3076 * sctp_sf_eat_data_fast_4_4
3077 *
3078 * Section: 4 (4)
3079 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3080 * DATA chunks without delay.
3081 *
3082 * Verification Tag: 8.5 Verification Tag [Normal verification]
3083 * Inputs
3084 * (endpoint, asoc, chunk)
3085 *
3086 * Outputs
3087 * (asoc, reply_msg, msg_up, timers, counters)
3088 *
3089 * The return value is the disposition of the chunk.
3090 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003091sctp_disposition_t sctp_sf_eat_data_fast_4_4(struct net *net,
3092 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 const struct sctp_association *asoc,
3094 const sctp_subtype_t type,
3095 void *arg,
3096 sctp_cmd_seq_t *commands)
3097{
3098 struct sctp_chunk *chunk = arg;
3099 int error;
3100
3101 if (!sctp_vtag_verify(chunk, asoc)) {
3102 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3103 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003104 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 }
3106
Xin Long9f8d3142017-06-30 11:52:20 +08003107 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_data_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003108 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 commands);
3110
wangweidongcb3f8372013-12-23 12:16:50 +08003111 error = sctp_eat_data(asoc, chunk, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 switch (error) {
3113 case SCTP_IERROR_NO_ERROR:
3114 case SCTP_IERROR_HIGH_TSN:
3115 case SCTP_IERROR_DUP_TSN:
3116 case SCTP_IERROR_IGNORE_TSN:
3117 case SCTP_IERROR_BAD_STREAM:
3118 break;
3119 case SCTP_IERROR_NO_DATA:
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02003120 return SCTP_DISPOSITION_ABORT;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003121 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003122 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Xin Long3583df12017-06-30 11:52:19 +08003123 (u8 *)chunk->subh.data_hdr,
3124 sizeof(struct sctp_datahdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 default:
3126 BUG();
3127 }
3128
3129 /* Go a head and force a SACK, since we are shutting down. */
3130
3131 /* Implementor's Guide.
3132 *
3133 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3134 * respond to each received packet containing one or more DATA chunk(s)
3135 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3136 */
3137 if (chunk->end_of_packet) {
3138 /* We must delay the chunk creation since the cumulative
3139 * TSN has not been updated yet.
3140 */
3141 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3142 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3143 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3144 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3145 }
3146
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 return SCTP_DISPOSITION_CONSUME;
3148}
3149
3150/*
3151 * Section: 6.2 Processing a Received SACK
3152 * D) Any time a SACK arrives, the endpoint performs the following:
3153 *
3154 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3155 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3156 * increasing, a SACK whose Cumulative TSN Ack is less than the
3157 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3158 *
3159 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3160 * of bytes still outstanding after processing the Cumulative TSN Ack
3161 * and the Gap Ack Blocks.
3162 *
3163 * iii) If the SACK is missing a TSN that was previously
3164 * acknowledged via a Gap Ack Block (e.g., the data receiver
3165 * reneged on the data), then mark the corresponding DATA chunk
3166 * as available for retransmit: Mark it as missing for fast
3167 * retransmit as described in Section 7.2.4 and if no retransmit
3168 * timer is running for the destination address to which the DATA
3169 * chunk was originally transmitted, then T3-rtx is started for
3170 * that destination address.
3171 *
3172 * Verification Tag: 8.5 Verification Tag [Normal verification]
3173 *
3174 * Inputs
3175 * (endpoint, asoc, chunk)
3176 *
3177 * Outputs
3178 * (asoc, reply_msg, msg_up, timers, counters)
3179 *
3180 * The return value is the disposition of the chunk.
3181 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003182sctp_disposition_t sctp_sf_eat_sack_6_2(struct net *net,
3183 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 const struct sctp_association *asoc,
3185 const sctp_subtype_t type,
3186 void *arg,
3187 sctp_cmd_seq_t *commands)
3188{
3189 struct sctp_chunk *chunk = arg;
Xin Long78731082017-07-23 09:34:32 +08003190 struct sctp_sackhdr *sackh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 __u32 ctsn;
3192
3193 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003194 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
3196 /* Make sure that the SACK chunk has a valid length. */
Xin Longd4d6c612017-07-23 09:34:33 +08003197 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_sack_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003198 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 commands);
3200
3201 /* Pull the SACK chunk from the data buffer */
3202 sackh = sctp_sm_pull_sack(chunk);
3203 /* Was this a bogus SACK? */
3204 if (!sackh)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003205 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 chunk->subh.sack_hdr = sackh;
3207 ctsn = ntohl(sackh->cum_tsn_ack);
3208
3209 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3210 * Ack Point, then drop the SACK. Since Cumulative TSN
3211 * Ack is monotonically increasing, a SACK whose
3212 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3213 * Point indicates an out-of-order SACK.
3214 */
3215 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02003216 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
3217 asoc->ctsn_ack_point);
3218
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 return SCTP_DISPOSITION_DISCARD;
3220 }
3221
Wei Yongjunaecedea2007-08-02 16:57:44 +08003222 /* If Cumulative TSN Ack beyond the max tsn currently
3223 * send, terminating the association and respond to the
3224 * sender with an ABORT.
3225 */
3226 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003227 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjunaecedea2007-08-02 16:57:44 +08003228
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 /* Return this SACK for further processing. */
Nicolas Dichteledfee032012-10-03 05:43:22 +00003230 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
3232 /* Note: We do the rest of the work on the PROCESS_SACK
3233 * sideeffect.
3234 */
3235 return SCTP_DISPOSITION_CONSUME;
3236}
3237
3238/*
3239 * Generate an ABORT in response to a packet.
3240 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003241 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003243 * 8) The receiver should respond to the sender of the OOTB packet with
3244 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3245 * MUST fill in the Verification Tag field of the outbound packet
3246 * with the value found in the Verification Tag field of the OOTB
3247 * packet and set the T-bit in the Chunk Flags to indicate that the
3248 * Verification Tag is reflected. After sending this ABORT, the
3249 * receiver of the OOTB packet shall discard the OOTB packet and take
3250 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 *
3252 * Verification Tag:
3253 *
3254 * The return value is the disposition of the chunk.
3255*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003256static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
3257 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 const struct sctp_association *asoc,
3259 const sctp_subtype_t type,
3260 void *arg,
3261 sctp_cmd_seq_t *commands)
3262{
3263 struct sctp_packet *packet = NULL;
3264 struct sctp_chunk *chunk = arg;
3265 struct sctp_chunk *abort;
3266
Eric W. Biederman2ce95502012-08-06 08:43:06 +00003267 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Marcelo Ricardo Leitnereab59072016-12-28 09:26:31 -02003268 if (!packet)
3269 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
Marcelo Ricardo Leitnereab59072016-12-28 09:26:31 -02003271 /* Make an ABORT. The T bit will be set if the asoc
3272 * is NULL.
3273 */
3274 abort = sctp_make_abort(asoc, chunk, 0);
3275 if (!abort) {
3276 sctp_ootb_pkt_free(packet);
3277 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 }
3279
Marcelo Ricardo Leitnereab59072016-12-28 09:26:31 -02003280 /* Reflect vtag if T-Bit is set */
3281 if (sctp_test_T_bit(abort))
3282 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3283
3284 /* Set the skb to the belonging sock for accounting. */
3285 abort->skb->sk = ep->base.sk;
3286
3287 sctp_packet_append_chunk(packet, abort);
3288
3289 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3290 SCTP_PACKET(packet));
3291
3292 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3293
3294 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3295 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296}
3297
3298/*
3299 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3300 * event as ULP notification for each cause included in the chunk.
3301 *
3302 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3303 *
3304 * The return value is the disposition of the chunk.
3305*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003306sctp_disposition_t sctp_sf_operr_notify(struct net *net,
3307 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 const struct sctp_association *asoc,
3309 const sctp_subtype_t type,
3310 void *arg,
3311 sctp_cmd_seq_t *commands)
3312{
3313 struct sctp_chunk *chunk = arg;
Shan Wei8a00be12011-04-19 21:25:40 +00003314 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
3316 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003317 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
3319 /* Make sure that the ERROR chunk has a valid length. */
3320 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003321 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 commands);
Shan Wei8a00be12011-04-19 21:25:40 +00003323 sctp_walk_errors(err, chunk->chunk_hdr);
3324 if ((void *)err != (void *)chunk->chunk_end)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003325 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Shan Wei8a00be12011-04-19 21:25:40 +00003326 (void *)err, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327
Wei Yongjun3df267872009-03-02 06:46:51 +00003328 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3329 SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332}
3333
3334/*
3335 * Process an inbound SHUTDOWN ACK.
3336 *
3337 * From Section 9.2:
3338 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3339 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3340 * peer, and remove all record of the association.
3341 *
3342 * The return value is the disposition.
3343 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003344sctp_disposition_t sctp_sf_do_9_2_final(struct net *net,
3345 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 const struct sctp_association *asoc,
3347 const sctp_subtype_t type,
3348 void *arg,
3349 sctp_cmd_seq_t *commands)
3350{
3351 struct sctp_chunk *chunk = arg;
3352 struct sctp_chunk *reply;
3353 struct sctp_ulpevent *ev;
3354
3355 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003356 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
3358 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
Xin Long922dbc52017-06-30 11:52:13 +08003359 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003360 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 /* 10.2 H) SHUTDOWN COMPLETE notification
3363 *
3364 * When SCTP completes the shutdown procedures (section 9.2) this
3365 * notification is passed to the upper layer.
3366 */
3367 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07003368 0, 0, 0, NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 if (!ev)
3370 goto nomem;
3371
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003372 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3373 reply = sctp_make_shutdown_complete(asoc, chunk);
3374 if (!reply)
3375 goto nomem_chunk;
3376
3377 /* Do all the commands now (after allocation), so that we
3378 * have consistent state if memory allocation failes
3379 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3381
3382 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3383 * stop the T2-shutdown timer,
3384 */
3385 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3386 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3387
3388 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3389 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3390
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3392 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003393 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3394 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3396
3397 /* ...and remove all record of the association. */
3398 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3399 return SCTP_DISPOSITION_DELETE_TCB;
3400
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003401nomem_chunk:
3402 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403nomem:
3404 return SCTP_DISPOSITION_NOMEM;
3405}
3406
3407/*
Jerome Forissier047a2422005-04-28 11:58:43 -07003408 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3409 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3411 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3412 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3413 * packet must fill in the Verification Tag field of the outbound
3414 * packet with the Verification Tag received in the SHUTDOWN ACK and
Jerome Forissier047a2422005-04-28 11:58:43 -07003415 * set the T-bit in the Chunk Flags to indicate that the Verification
3416 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 *
3418 * 8) The receiver should respond to the sender of the OOTB packet with
3419 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3420 * MUST fill in the Verification Tag field of the outbound packet
3421 * with the value found in the Verification Tag field of the OOTB
Jerome Forissier047a2422005-04-28 11:58:43 -07003422 * packet and set the T-bit in the Chunk Flags to indicate that the
3423 * Verification Tag is reflected. After sending this ABORT, the
3424 * receiver of the OOTB packet shall discard the OOTB packet and take
3425 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003427sctp_disposition_t sctp_sf_ootb(struct net *net,
3428 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 const struct sctp_association *asoc,
3430 const sctp_subtype_t type,
3431 void *arg,
3432 sctp_cmd_seq_t *commands)
3433{
3434 struct sctp_chunk *chunk = arg;
3435 struct sk_buff *skb = chunk->skb;
Xin Long922dbc52017-06-30 11:52:13 +08003436 struct sctp_chunkhdr *ch;
Shan Wei85c5ed42011-04-19 21:30:01 +00003437 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 __u8 *ch_end;
3439 int ootb_shut_ack = 0;
Shan Wei85c5ed42011-04-19 21:30:01 +00003440 int ootb_cookie_ack = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003442 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443
Xin Long922dbc52017-06-30 11:52:13 +08003444 ch = (struct sctp_chunkhdr *)chunk->chunk_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 do {
Vlad Yasevichece25df2007-09-07 16:30:54 -04003446 /* Report violation if the chunk is less then minimal */
Xin Long922dbc52017-06-30 11:52:13 +08003447 if (ntohs(ch->length) < sizeof(*ch))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003448 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003449 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
Marcelo Ricardo Leitnerbf911e92016-10-25 14:27:39 -02003451 /* Report violation if chunk len overflows */
3452 ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
3453 if (ch_end > skb_tail_pointer(skb))
3454 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3455 commands);
3456
Vlad Yasevichece25df2007-09-07 16:30:54 -04003457 /* Now that we know we at least have a chunk header,
3458 * do things that are type appropriate.
3459 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3461 ootb_shut_ack = 1;
3462
3463 /* RFC 2960, Section 3.3.7
3464 * Moreover, under any circumstances, an endpoint that
3465 * receives an ABORT MUST NOT respond to that ABORT by
3466 * sending an ABORT of its own.
3467 */
3468 if (SCTP_CID_ABORT == ch->type)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003469 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003470
Shan Wei85c5ed42011-04-19 21:30:01 +00003471 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3472 * or a COOKIE ACK the SCTP Packet should be silently
3473 * discarded.
3474 */
3475
3476 if (SCTP_CID_COOKIE_ACK == ch->type)
3477 ootb_cookie_ack = 1;
3478
3479 if (SCTP_CID_ERROR == ch->type) {
3480 sctp_walk_errors(err, ch) {
3481 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3482 ootb_cookie_ack = 1;
3483 break;
3484 }
3485 }
3486 }
3487
Xin Long922dbc52017-06-30 11:52:13 +08003488 ch = (struct sctp_chunkhdr *)ch_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003489 } while (ch_end < skb_tail_pointer(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490
3491 if (ootb_shut_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003492 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
Shan Wei85c5ed42011-04-19 21:30:01 +00003493 else if (ootb_cookie_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003494 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003496 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497}
3498
3499/*
3500 * Handle an "Out of the blue" SHUTDOWN ACK.
3501 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003502 * Section: 8.4 5, sctpimpguide 2.41.
3503 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
Jerome Forissier047a2422005-04-28 11:58:43 -07003505 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3506 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3507 * packet must fill in the Verification Tag field of the outbound
3508 * packet with the Verification Tag received in the SHUTDOWN ACK and
3509 * set the T-bit in the Chunk Flags to indicate that the Verification
3510 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 *
3512 * Inputs
3513 * (endpoint, asoc, type, arg, commands)
3514 *
3515 * Outputs
3516 * (sctp_disposition_t)
3517 *
3518 * The return value is the disposition of the chunk.
3519 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003520static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
3521 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 const struct sctp_association *asoc,
3523 const sctp_subtype_t type,
3524 void *arg,
3525 sctp_cmd_seq_t *commands)
3526{
3527 struct sctp_packet *packet = NULL;
3528 struct sctp_chunk *chunk = arg;
3529 struct sctp_chunk *shut;
3530
Eric W. Biederman2ce95502012-08-06 08:43:06 +00003531 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Marcelo Ricardo Leitner1ff01562016-12-28 09:26:32 -02003532 if (!packet)
3533 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534
Marcelo Ricardo Leitner1ff01562016-12-28 09:26:32 -02003535 /* Make an SHUTDOWN_COMPLETE.
3536 * The T bit will be set if the asoc is NULL.
3537 */
3538 shut = sctp_make_shutdown_complete(asoc, chunk);
3539 if (!shut) {
3540 sctp_ootb_pkt_free(packet);
3541 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 }
3543
Marcelo Ricardo Leitner1ff01562016-12-28 09:26:32 -02003544 /* Reflect vtag if T-Bit is set */
3545 if (sctp_test_T_bit(shut))
3546 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3547
3548 /* Set the skb to the belonging sock for accounting. */
3549 shut->skb->sk = ep->base.sk;
3550
3551 sctp_packet_append_chunk(packet, shut);
3552
3553 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3554 SCTP_PACKET(packet));
3555
3556 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3557
3558 /* If the chunk length is invalid, we don't want to process
3559 * the reset of the packet.
3560 */
Xin Long922dbc52017-06-30 11:52:13 +08003561 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Marcelo Ricardo Leitner1ff01562016-12-28 09:26:32 -02003562 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3563
3564 /* We need to discard the rest of the packet to prevent
3565 * potential bomming attacks from additional bundled chunks.
3566 * This is documented in SCTP Threats ID.
3567 */
3568 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569}
3570
3571/*
3572 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3573 *
3574 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3575 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3576 * procedures in section 8.4 SHOULD be followed, in other words it
3577 * should be treated as an Out Of The Blue packet.
3578 * [This means that we do NOT check the Verification Tag on these
3579 * chunks. --piggy ]
3580 *
3581 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003582sctp_disposition_t sctp_sf_do_8_5_1_E_sa(struct net *net,
3583 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 const struct sctp_association *asoc,
3585 const sctp_subtype_t type,
3586 void *arg,
3587 sctp_cmd_seq_t *commands)
3588{
Vlad Yasevichece25df2007-09-07 16:30:54 -04003589 struct sctp_chunk *chunk = arg;
3590
3591 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
Xin Long922dbc52017-06-30 11:52:13 +08003592 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003593 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003594 commands);
3595
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 /* Although we do have an association in this case, it corresponds
3597 * to a restarted association. So the packet is treated as an OOTB
3598 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3599 * called with a NULL association.
3600 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003601 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Wei Yongjun8190f892008-09-08 12:13:55 +08003602
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003603 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604}
3605
3606/* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003607sctp_disposition_t sctp_sf_do_asconf(struct net *net,
3608 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 const struct sctp_association *asoc,
3610 const sctp_subtype_t type, void *arg,
3611 sctp_cmd_seq_t *commands)
3612{
3613 struct sctp_chunk *chunk = arg;
3614 struct sctp_chunk *asconf_ack = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003615 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 sctp_addiphdr_t *hdr;
3617 __u32 serial;
3618
3619 if (!sctp_vtag_verify(chunk, asoc)) {
3620 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3621 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003622 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 }
3624
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003625 /* ADD-IP: Section 4.1.1
3626 * This chunk MUST be sent in an authenticated way by using
3627 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3628 * is received unauthenticated it MUST be silently discarded as
3629 * described in [I-D.ietf-tsvwg-sctp-auth].
3630 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003631 if (!net->sctp.addip_noauth && !chunk->auth)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003632 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003633
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3635 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003636 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 commands);
3638
3639 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3640 serial = ntohl(hdr->serial);
3641
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003642 /* Verify the ASCONF chunk before processing it. */
Daniel Borkmann9de79222014-10-09 22:55:31 +02003643 if (!sctp_verify_asconf(asoc, chunk, true, &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003644 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003645 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003646
Vlad Yasevicha08de642007-12-20 14:11:47 -08003647 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 * the endpoint stored in a new association variable
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003649 * 'Peer-Serial-Number'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 */
3651 if (serial == asoc->peer.addip_serial + 1) {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003652 /* If this is the first instance of ASCONF in the packet,
3653 * we can clean our old ASCONF-ACKs.
3654 */
3655 if (!chunk->has_asconf)
3656 sctp_assoc_clean_asconf_ack_cache(asoc);
3657
3658 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3659 * expected, process the ASCONF as described below and after
3660 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3661 * the response packet and cache a copy of it (in the event it
3662 * later needs to be retransmitted).
3663 *
3664 * Essentially, do V1-V5.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 */
3666 asconf_ack = sctp_process_asconf((struct sctp_association *)
3667 asoc, chunk);
3668 if (!asconf_ack)
3669 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevicha08de642007-12-20 14:11:47 -08003670 } else if (serial < asoc->peer.addip_serial + 1) {
3671 /* ADDIP 5.2 E2)
3672 * If the value found in the Sequence Number is less than the
3673 * ('Peer- Sequence-Number' + 1), simply skip to the next
3674 * ASCONF, and include in the outbound response packet
3675 * any previously cached ASCONF-ACK response that was
3676 * sent and saved that matches the Sequence Number of the
3677 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3678 * Chunk exists. This will occur when an older ASCONF
3679 * arrives out of order. In such a case, the receiver
3680 * should skip the ASCONF Chunk and not include ASCONF-ACK
3681 * Chunk for that chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003683 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3684 if (!asconf_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 return SCTP_DISPOSITION_DISCARD;
Vlad Yasevich31b02e12009-09-04 18:21:00 -04003686
3687 /* Reset the transport so that we select the correct one
3688 * this time around. This is to make sure that we don't
3689 * accidentally use a stale transport that's been removed.
3690 */
3691 asconf_ack->transport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 } else {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003693 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 * it must be either a stale packet or from an attacker.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003695 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 return SCTP_DISPOSITION_DISCARD;
3697 }
3698
Vlad Yasevicha08de642007-12-20 14:11:47 -08003699 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3700 * containing the ASCONF-ACK Chunks MUST be the source address of
3701 * the SCTP packet that held the ASCONF Chunks.
3702 *
3703 * To do this properly, we'll set the destination address of the chunk
3704 * and at the transmit time, will try look up the transport to use.
3705 * Since ASCONFs may be bundled, the correct transport may not be
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +02003706 * created until we process the entire packet, thus this workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003708 asconf_ack->dest = chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
Michio Honda6af29cc2011-06-16 17:14:34 +09003710 if (asoc->new_transport) {
wangweidongf7010e62013-12-23 12:16:52 +08003711 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport, commands);
Michio Honda6af29cc2011-06-16 17:14:34 +09003712 ((struct sctp_association *)asoc)->new_transport = NULL;
3713 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003714
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 return SCTP_DISPOSITION_CONSUME;
3716}
3717
3718/*
3719 * ADDIP Section 4.3 General rules for address manipulation
3720 * When building TLV parameters for the ASCONF Chunk that will add or
3721 * delete IP addresses the D0 to D13 rules should be applied:
3722 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003723sctp_disposition_t sctp_sf_do_asconf_ack(struct net *net,
3724 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 const struct sctp_association *asoc,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003726 const sctp_subtype_t type, void *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 sctp_cmd_seq_t *commands)
3728{
3729 struct sctp_chunk *asconf_ack = arg;
3730 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3731 struct sctp_chunk *abort;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003732 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 sctp_addiphdr_t *addip_hdr;
3734 __u32 sent_serial, rcvd_serial;
3735
3736 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3737 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3738 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003739 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 }
3741
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003742 /* ADD-IP, Section 4.1.2:
3743 * This chunk MUST be sent in an authenticated way by using
3744 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3745 * is received unauthenticated it MUST be silently discarded as
3746 * described in [I-D.ietf-tsvwg-sctp-auth].
3747 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003748 if (!net->sctp.addip_noauth && !asconf_ack->auth)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003749 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003750
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 /* Make sure that the ADDIP chunk has a valid length. */
3752 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003753 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 commands);
3755
3756 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3757 rcvd_serial = ntohl(addip_hdr->serial);
3758
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003759 /* Verify the ASCONF-ACK chunk before processing it. */
Daniel Borkmann9de79222014-10-09 22:55:31 +02003760 if (!sctp_verify_asconf(asoc, asconf_ack, false, &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003761 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003762 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003763
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 if (last_asconf) {
3765 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3766 sent_serial = ntohl(addip_hdr->serial);
3767 } else {
3768 sent_serial = asoc->addip_serial - 1;
3769 }
3770
3771 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3772 * equal to the next serial number to be used but no ASCONF chunk is
3773 * outstanding the endpoint MUST ABORT the association. Note that a
3774 * sequence number is greater than if it is no more than 2^^31-1
3775 * larger than the current sequence number (using serial arithmetic).
3776 */
3777 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3778 !(asoc->addip_last_asconf)) {
3779 abort = sctp_make_abort(asoc, asconf_ack,
3780 sizeof(sctp_errhdr_t));
3781 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003782 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3784 SCTP_CHUNK(abort));
3785 }
3786 /* We are going to ABORT, so we might as well stop
3787 * processing the rest of the chunks in the packet.
3788 */
3789 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3790 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
wangweidongcb3f8372013-12-23 12:16:50 +08003791 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003792 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003793 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003795 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003796 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3797 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 return SCTP_DISPOSITION_ABORT;
3799 }
3800
3801 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3802 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3803 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3804
3805 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
Vlad Yasevichc0786692010-04-28 08:47:22 +00003806 asconf_ack)) {
3807 /* Successfully processed ASCONF_ACK. We can
3808 * release the next asconf if we have one.
3809 */
3810 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3811 SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 return SCTP_DISPOSITION_CONSUME;
Vlad Yasevichc0786692010-04-28 08:47:22 +00003813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
3815 abort = sctp_make_abort(asoc, asconf_ack,
3816 sizeof(sctp_errhdr_t));
3817 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003818 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3820 SCTP_CHUNK(abort));
3821 }
3822 /* We are going to ABORT, so we might as well stop
3823 * processing the rest of the chunks in the packet.
3824 */
wangweidongcb3f8372013-12-23 12:16:50 +08003825 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003826 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003827 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003829 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003830 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3831 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 return SCTP_DISPOSITION_ABORT;
3833 }
3834
3835 return SCTP_DISPOSITION_DISCARD;
3836}
3837
Xin Long2040d3d2017-02-17 12:45:42 +08003838/* RE-CONFIG Section 5.2 Upon reception of an RECONF Chunk. */
3839sctp_disposition_t sctp_sf_do_reconf(struct net *net,
3840 const struct sctp_endpoint *ep,
3841 const struct sctp_association *asoc,
3842 const sctp_subtype_t type, void *arg,
3843 sctp_cmd_seq_t *commands)
3844{
3845 struct sctp_paramhdr *err_param = NULL;
3846 struct sctp_chunk *chunk = arg;
3847 struct sctp_reconf_chunk *hdr;
3848 union sctp_params param;
3849
3850 if (!sctp_vtag_verify(chunk, asoc)) {
3851 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3852 SCTP_NULL());
3853 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3854 }
3855
3856 /* Make sure that the RECONF chunk has a valid length. */
3857 if (!sctp_chunk_length_valid(chunk, sizeof(*hdr)))
3858 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3859 commands);
3860
3861 if (!sctp_verify_reconf(asoc, chunk, &err_param))
3862 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3863 (void *)err_param, commands);
3864
3865 hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
3866 sctp_walk_params(param, hdr, params) {
3867 struct sctp_chunk *reply = NULL;
3868 struct sctp_ulpevent *ev = NULL;
3869
3870 if (param.p->type == SCTP_PARAM_RESET_OUT_REQUEST)
3871 reply = sctp_process_strreset_outreq(
3872 (struct sctp_association *)asoc, param, &ev);
3873 else if (param.p->type == SCTP_PARAM_RESET_IN_REQUEST)
3874 reply = sctp_process_strreset_inreq(
3875 (struct sctp_association *)asoc, param, &ev);
Xin Long692787c2017-03-10 12:11:07 +08003876 else if (param.p->type == SCTP_PARAM_RESET_TSN_REQUEST)
3877 reply = sctp_process_strreset_tsnreq(
3878 (struct sctp_association *)asoc, param, &ev);
Xin Long50a41592017-03-10 12:11:09 +08003879 else if (param.p->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS)
3880 reply = sctp_process_strreset_addstrm_out(
3881 (struct sctp_association *)asoc, param, &ev);
Xin Longc5c4ebb2017-03-10 12:11:10 +08003882 else if (param.p->type == SCTP_PARAM_RESET_ADD_IN_STREAMS)
3883 reply = sctp_process_strreset_addstrm_in(
3884 (struct sctp_association *)asoc, param, &ev);
Xin Long11ae76e2017-03-10 12:11:11 +08003885 else if (param.p->type == SCTP_PARAM_RESET_RESPONSE)
3886 reply = sctp_process_strreset_resp(
3887 (struct sctp_association *)asoc, param, &ev);
Xin Long2040d3d2017-02-17 12:45:42 +08003888
3889 if (ev)
3890 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
3891 SCTP_ULPEVENT(ev));
3892
3893 if (reply)
3894 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3895 SCTP_CHUNK(reply));
3896 }
3897
3898 return SCTP_DISPOSITION_CONSUME;
3899}
3900
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901/*
3902 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3903 *
3904 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3905 * its cumulative TSN point to the value carried in the FORWARD TSN
3906 * chunk, and then MUST further advance its cumulative TSN point locally
3907 * if possible.
3908 * After the above processing, the data receiver MUST stop reporting any
3909 * missing TSNs earlier than or equal to the new cumulative TSN point.
3910 *
3911 * Verification Tag: 8.5 Verification Tag [Normal verification]
3912 *
3913 * The return value is the disposition of the chunk.
3914 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003915sctp_disposition_t sctp_sf_eat_fwd_tsn(struct net *net,
3916 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 const struct sctp_association *asoc,
3918 const sctp_subtype_t type,
3919 void *arg,
3920 sctp_cmd_seq_t *commands)
3921{
3922 struct sctp_chunk *chunk = arg;
3923 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003924 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 __u16 len;
3926 __u32 tsn;
3927
3928 if (!sctp_vtag_verify(chunk, asoc)) {
3929 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3930 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003931 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 }
3933
Xin Longd15c9ed2017-02-03 17:37:06 +08003934 if (!asoc->peer.prsctp_capable)
3935 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
3936
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 /* Make sure that the FORWARD_TSN chunk has valid length. */
3938 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003939 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 commands);
3941
3942 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3943 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3944 len = ntohs(chunk->chunk_hdr->length);
3945 len -= sizeof(struct sctp_chunkhdr);
3946 skb_pull(chunk->skb, len);
3947
3948 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02003949 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950
3951 /* The TSN is too high--silently discard the chunk and count on it
3952 * getting retransmitted later.
3953 */
3954 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3955 goto discard_noforce;
3956
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003957 /* Silently discard the chunk if stream-id is not valid */
3958 sctp_walk_fwdtsn(skip, chunk) {
Xin Longcee360a2017-05-31 16:36:31 +08003959 if (ntohs(skip->stream) >= asoc->stream.incnt)
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003960 goto discard_noforce;
3961 }
3962
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3964 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003965 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003967
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 /* Count this as receiving DATA. */
Neil Horman9f70f462013-12-10 06:48:15 -05003969 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3971 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3972 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003973
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 /* FIXME: For now send a SACK, but DATA processing may
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003975 * send another.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 */
3977 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978
3979 return SCTP_DISPOSITION_CONSUME;
3980
3981discard_noforce:
3982 return SCTP_DISPOSITION_DISCARD;
3983}
3984
3985sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003986 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 const struct sctp_endpoint *ep,
3988 const struct sctp_association *asoc,
3989 const sctp_subtype_t type,
3990 void *arg,
3991 sctp_cmd_seq_t *commands)
3992{
3993 struct sctp_chunk *chunk = arg;
3994 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003995 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 __u16 len;
3997 __u32 tsn;
3998
3999 if (!sctp_vtag_verify(chunk, asoc)) {
4000 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4001 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004002 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 }
4004
Xin Longd15c9ed2017-02-03 17:37:06 +08004005 if (!asoc->peer.prsctp_capable)
4006 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4007
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 /* Make sure that the FORWARD_TSN chunk has a valid length. */
4009 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004010 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 commands);
4012
4013 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
4014 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
4015 len = ntohs(chunk->chunk_hdr->length);
4016 len -= sizeof(struct sctp_chunkhdr);
4017 skb_pull(chunk->skb, len);
4018
4019 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02004020 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021
4022 /* The TSN is too high--silently discard the chunk and count on it
4023 * getting retransmitted later.
4024 */
4025 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
4026 goto gen_shutdown;
4027
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08004028 /* Silently discard the chunk if stream-id is not valid */
4029 sctp_walk_fwdtsn(skip, chunk) {
Xin Longcee360a2017-05-31 16:36:31 +08004030 if (ntohs(skip->stream) >= asoc->stream.incnt)
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08004031 goto gen_shutdown;
4032 }
4033
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
4035 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004036 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004038
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 /* Go a head and force a SACK, since we are shutting down. */
4040gen_shutdown:
4041 /* Implementor's Guide.
4042 *
4043 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
4044 * respond to each received packet containing one or more DATA chunk(s)
4045 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
4046 */
4047 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
4048 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
4049 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4050 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4051
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004052 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053}
4054
4055/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004056 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004057 *
4058 * The receiver MUST use the HMAC algorithm indicated in the HMAC
4059 * Identifier field. If this algorithm was not specified by the
4060 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
4061 * during association setup, the AUTH chunk and all chunks after it MUST
4062 * be discarded and an ERROR chunk SHOULD be sent with the error cause
4063 * defined in Section 4.1.
4064 *
4065 * If an endpoint with no shared key receives a Shared Key Identifier
4066 * other than 0, it MUST silently discard all authenticated chunks. If
4067 * the endpoint has at least one endpoint pair shared key for the peer,
4068 * it MUST use the key specified by the Shared Key Identifier if a
4069 * key has been configured for that Shared Key Identifier. If no
4070 * endpoint pair shared key has been configured for that Shared Key
4071 * Identifier, all authenticated chunks MUST be silently discarded.
4072 *
4073 * Verification Tag: 8.5 Verification Tag [Normal verification]
4074 *
4075 * The return value is the disposition of the chunk.
4076 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004077static sctp_ierror_t sctp_sf_authenticate(struct net *net,
4078 const struct sctp_endpoint *ep,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004079 const struct sctp_association *asoc,
4080 const sctp_subtype_t type,
4081 struct sctp_chunk *chunk)
4082{
4083 struct sctp_authhdr *auth_hdr;
4084 struct sctp_hmac *hmac;
4085 unsigned int sig_len;
4086 __u16 key_id;
4087 __u8 *save_digest;
4088 __u8 *digest;
4089
4090 /* Pull in the auth header, so we can do some more verification */
4091 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4092 chunk->subh.auth_hdr = auth_hdr;
4093 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
4094
Masanari Iida02582e92012-08-22 19:11:26 +09004095 /* Make sure that we support the HMAC algorithm from the auth
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004096 * chunk.
4097 */
4098 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4099 return SCTP_IERROR_AUTH_BAD_HMAC;
4100
4101 /* Make sure that the provided shared key identifier has been
4102 * configured
4103 */
4104 key_id = ntohs(auth_hdr->shkey_id);
4105 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
4106 return SCTP_IERROR_AUTH_BAD_KEYID;
4107
4108
4109 /* Make sure that the length of the signature matches what
4110 * we expect.
4111 */
4112 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
4113 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4114 if (sig_len != hmac->hmac_len)
4115 return SCTP_IERROR_PROTO_VIOLATION;
4116
4117 /* Now that we've done validation checks, we can compute and
4118 * verify the hmac. The steps involved are:
4119 * 1. Save the digest from the chunk.
4120 * 2. Zero out the digest in the chunk.
4121 * 3. Compute the new digest
4122 * 4. Compare saved and new digests.
4123 */
4124 digest = auth_hdr->hmac;
4125 skb_pull(chunk->skb, sig_len);
4126
4127 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4128 if (!save_digest)
4129 goto nomem;
4130
4131 memset(digest, 0, sig_len);
4132
4133 sctp_auth_calculate_hmac(asoc, chunk->skb,
4134 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4135 GFP_ATOMIC);
4136
4137 /* Discard the packet if the digests do not match */
4138 if (memcmp(save_digest, digest, sig_len)) {
4139 kfree(save_digest);
4140 return SCTP_IERROR_BAD_SIG;
4141 }
4142
4143 kfree(save_digest);
4144 chunk->auth = 1;
4145
4146 return SCTP_IERROR_NO_ERROR;
4147nomem:
4148 return SCTP_IERROR_NOMEM;
4149}
4150
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004151sctp_disposition_t sctp_sf_eat_auth(struct net *net,
4152 const struct sctp_endpoint *ep,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004153 const struct sctp_association *asoc,
4154 const sctp_subtype_t type,
4155 void *arg,
4156 sctp_cmd_seq_t *commands)
4157{
4158 struct sctp_authhdr *auth_hdr;
4159 struct sctp_chunk *chunk = arg;
4160 struct sctp_chunk *err_chunk;
4161 sctp_ierror_t error;
4162
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004163 /* Make sure that the peer has AUTH capable */
4164 if (!asoc->peer.auth_capable)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004165 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004166
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004167 if (!sctp_vtag_verify(chunk, asoc)) {
4168 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4169 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004170 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004171 }
4172
4173 /* Make sure that the AUTH chunk has valid length. */
4174 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004175 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004176 commands);
4177
4178 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004179 error = sctp_sf_authenticate(net, ep, asoc, type, chunk);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004180 switch (error) {
Joe Perches7fd71b12011-07-01 09:43:11 +00004181 case SCTP_IERROR_AUTH_BAD_HMAC:
4182 /* Generate the ERROR chunk and discard the rest
4183 * of the packet
4184 */
4185 err_chunk = sctp_make_op_error(asoc, chunk,
4186 SCTP_ERROR_UNSUP_HMAC,
4187 &auth_hdr->hmac_id,
4188 sizeof(__u16), 0);
4189 if (err_chunk) {
4190 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4191 SCTP_CHUNK(err_chunk));
4192 }
4193 /* Fall Through */
4194 case SCTP_IERROR_AUTH_BAD_KEYID:
4195 case SCTP_IERROR_BAD_SIG:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004196 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Joe Perches7fd71b12011-07-01 09:43:11 +00004197
4198 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004199 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Joe Perches7fd71b12011-07-01 09:43:11 +00004200 commands);
4201
4202 case SCTP_IERROR_NOMEM:
4203 return SCTP_DISPOSITION_NOMEM;
4204
4205 default: /* Prevent gcc warnings */
4206 break;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004207 }
4208
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004209 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4210 struct sctp_ulpevent *ev;
4211
4212 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4213 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4214
4215 if (!ev)
4216 return -ENOMEM;
4217
4218 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4219 SCTP_ULPEVENT(ev));
4220 }
4221
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004222 return SCTP_DISPOSITION_CONSUME;
4223}
4224
4225/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 * Process an unknown chunk.
4227 *
4228 * Section: 3.2. Also, 2.1 in the implementor's guide.
4229 *
4230 * Chunk Types are encoded such that the highest-order two bits specify
4231 * the action that must be taken if the processing endpoint does not
4232 * recognize the Chunk Type.
4233 *
4234 * 00 - Stop processing this SCTP packet and discard it, do not process
4235 * any further chunks within it.
4236 *
4237 * 01 - Stop processing this SCTP packet and discard it, do not process
4238 * any further chunks within it, and report the unrecognized
4239 * chunk in an 'Unrecognized Chunk Type'.
4240 *
4241 * 10 - Skip this chunk and continue processing.
4242 *
4243 * 11 - Skip this chunk and continue processing, but report in an ERROR
4244 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4245 *
4246 * The return value is the disposition of the chunk.
4247 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004248sctp_disposition_t sctp_sf_unk_chunk(struct net *net,
4249 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 const struct sctp_association *asoc,
4251 const sctp_subtype_t type,
4252 void *arg,
4253 sctp_cmd_seq_t *commands)
4254{
4255 struct sctp_chunk *unk_chunk = arg;
4256 struct sctp_chunk *err_chunk;
Xin Long922dbc52017-06-30 11:52:13 +08004257 struct sctp_chunkhdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258
Daniel Borkmannbb333812013-06-28 19:49:40 +02004259 pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260
4261 if (!sctp_vtag_verify(unk_chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004262 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263
4264 /* Make sure that the chunk has a valid length.
4265 * Since we don't know the chunk type, we use a general
4266 * chunkhdr structure to make a comparison.
4267 */
Xin Long922dbc52017-06-30 11:52:13 +08004268 if (!sctp_chunk_length_valid(unk_chunk, sizeof(*hdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004269 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 commands);
4271
4272 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4273 case SCTP_CID_ACTION_DISCARD:
4274 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004275 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 case SCTP_CID_ACTION_DISCARD_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 /* Generate an ERROR chunk as response. */
4278 hdr = unk_chunk->chunk_hdr;
4279 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4280 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -03004281 SCTP_PAD4(ntohs(hdr->length)),
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004282 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 if (err_chunk) {
4284 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4285 SCTP_CHUNK(err_chunk));
4286 }
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07004287
4288 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004289 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 case SCTP_CID_ACTION_SKIP:
4292 /* Skip the chunk. */
4293 return SCTP_DISPOSITION_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 case SCTP_CID_ACTION_SKIP_ERR:
4295 /* Generate an ERROR chunk as response. */
4296 hdr = unk_chunk->chunk_hdr;
4297 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4298 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -03004299 SCTP_PAD4(ntohs(hdr->length)),
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004300 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 if (err_chunk) {
4302 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4303 SCTP_CHUNK(err_chunk));
4304 }
4305 /* Skip the chunk. */
4306 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 default:
4308 break;
4309 }
4310
4311 return SCTP_DISPOSITION_DISCARD;
4312}
4313
4314/*
4315 * Discard the chunk.
4316 *
4317 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4318 * [Too numerous to mention...]
4319 * Verification Tag: No verification needed.
4320 * Inputs
4321 * (endpoint, asoc, chunk)
4322 *
4323 * Outputs
4324 * (asoc, reply_msg, msg_up, timers, counters)
4325 *
4326 * The return value is the disposition of the chunk.
4327 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004328sctp_disposition_t sctp_sf_discard_chunk(struct net *net,
4329 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 const struct sctp_association *asoc,
4331 const sctp_subtype_t type,
4332 void *arg,
4333 sctp_cmd_seq_t *commands)
4334{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004335 struct sctp_chunk *chunk = arg;
4336
4337 /* Make sure that the chunk has a valid length.
4338 * Since we don't know the chunk type, we use a general
4339 * chunkhdr structure to make a comparison.
4340 */
Xin Long922dbc52017-06-30 11:52:13 +08004341 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004342 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004343 commands);
4344
Daniel Borkmannbb333812013-06-28 19:49:40 +02004345 pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
4346
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 return SCTP_DISPOSITION_DISCARD;
4348}
4349
4350/*
4351 * Discard the whole packet.
4352 *
4353 * Section: 8.4 2)
4354 *
4355 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4356 * silently discard the OOTB packet and take no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 *
4358 * Verification Tag: No verification necessary
4359 *
4360 * Inputs
4361 * (endpoint, asoc, chunk)
4362 *
4363 * Outputs
4364 * (asoc, reply_msg, msg_up, timers, counters)
4365 *
4366 * The return value is the disposition of the chunk.
4367 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004368sctp_disposition_t sctp_sf_pdiscard(struct net *net,
4369 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 const struct sctp_association *asoc,
4371 const sctp_subtype_t type,
4372 void *arg,
4373 sctp_cmd_seq_t *commands)
4374{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004375 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4377
4378 return SCTP_DISPOSITION_CONSUME;
4379}
4380
4381
4382/*
4383 * The other end is violating protocol.
4384 *
4385 * Section: Not specified
4386 * Verification Tag: Not specified
4387 * Inputs
4388 * (endpoint, asoc, chunk)
4389 *
4390 * Outputs
4391 * (asoc, reply_msg, msg_up, timers, counters)
4392 *
4393 * We simply tag the chunk as a violation. The state machine will log
4394 * the violation and continue.
4395 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004396sctp_disposition_t sctp_sf_violation(struct net *net,
4397 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 const struct sctp_association *asoc,
4399 const sctp_subtype_t type,
4400 void *arg,
4401 sctp_cmd_seq_t *commands)
4402{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004403 struct sctp_chunk *chunk = arg;
4404
4405 /* Make sure that the chunk has a valid length. */
Xin Long922dbc52017-06-30 11:52:13 +08004406 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004407 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004408 commands);
4409
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 return SCTP_DISPOSITION_VIOLATION;
4411}
4412
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413/*
Wei Yongjunaecedea2007-08-02 16:57:44 +08004414 * Common function to handle a protocol violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 */
Wei Yongjunaecedea2007-08-02 16:57:44 +08004416static sctp_disposition_t sctp_sf_abort_violation(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004417 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004418 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 void *arg,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004421 sctp_cmd_seq_t *commands,
4422 const __u8 *payload,
4423 const size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004425 struct sctp_packet *packet = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 struct sctp_chunk *chunk = arg;
4427 struct sctp_chunk *abort = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004429 /* SCTP-AUTH, Section 6.3:
4430 * It should be noted that if the receiver wants to tear
4431 * down an association in an authenticated way only, the
4432 * handling of malformed packets should not result in
4433 * tearing down the association.
4434 *
4435 * This means that if we only want to abort associations
4436 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004437 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004438 * was malformed.
4439 */
4440 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4441 goto discard;
4442
Jesper Juhl9abed242007-11-11 23:57:49 +01004443 /* Make the abort chunk. */
4444 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4445 if (!abort)
4446 goto nomem;
4447
Vlad Yasevichece25df2007-09-07 16:30:54 -04004448 if (asoc) {
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004449 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4450 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4451 !asoc->peer.i.init_tag) {
Xin Longcb1844c2017-07-23 09:34:26 +08004452 struct sctp_initack_chunk *initack;
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004453
Xin Longcb1844c2017-07-23 09:34:26 +08004454 initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
4455 if (!sctp_chunk_length_valid(chunk, sizeof(*initack)))
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004456 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4457 else {
4458 unsigned int inittag;
4459
4460 inittag = ntohl(initack->init_hdr.init_tag);
4461 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4462 SCTP_U32(inittag));
4463 }
4464 }
4465
Vlad Yasevichece25df2007-09-07 16:30:54 -04004466 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004467 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468
Vlad Yasevichece25df2007-09-07 16:30:54 -04004469 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4470 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4471 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4472 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4473 SCTP_ERROR(ECONNREFUSED));
4474 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4475 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4476 } else {
4477 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4478 SCTP_ERROR(ECONNABORTED));
4479 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4480 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004481 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 } else {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00004484 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004485
4486 if (!packet)
4487 goto nomem_pkt;
4488
4489 if (sctp_test_T_bit(abort))
4490 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4491
4492 abort->skb->sk = ep->base.sk;
4493
4494 sctp_packet_append_chunk(packet, abort);
4495
4496 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4497 SCTP_PACKET(packet));
4498
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004499 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 }
4501
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004502 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004503
Vlad Yasevich56eb82b2008-10-09 14:33:01 -07004504discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004505 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 return SCTP_DISPOSITION_ABORT;
4507
Vlad Yasevichece25df2007-09-07 16:30:54 -04004508nomem_pkt:
4509 sctp_chunk_free(abort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510nomem:
4511 return SCTP_DISPOSITION_NOMEM;
4512}
4513
Wei Yongjunaecedea2007-08-02 16:57:44 +08004514/*
4515 * Handle a protocol violation when the chunk length is invalid.
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004516 * "Invalid" length is identified as smaller than the minimal length a
Wei Yongjunaecedea2007-08-02 16:57:44 +08004517 * given chunk can be. For example, a SACK chunk has invalid length
Xin Longd4d6c612017-07-23 09:34:33 +08004518 * if its length is set to be smaller than the size of struct sctp_sack_chunk.
Wei Yongjunaecedea2007-08-02 16:57:44 +08004519 *
4520 * We inform the other end by sending an ABORT with a Protocol Violation
4521 * error code.
4522 *
4523 * Section: Not specified
4524 * Verification Tag: Nothing to do
4525 * Inputs
4526 * (endpoint, asoc, chunk)
4527 *
4528 * Outputs
4529 * (reply_msg, msg_up, counters)
4530 *
4531 * Generate an ABORT chunk and terminate the association.
4532 */
4533static sctp_disposition_t sctp_sf_violation_chunklen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004534 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004535 const struct sctp_endpoint *ep,
4536 const struct sctp_association *asoc,
4537 const sctp_subtype_t type,
4538 void *arg,
4539 sctp_cmd_seq_t *commands)
4540{
wangweidongcb3f8372013-12-23 12:16:50 +08004541 static const char err_str[] = "The following chunk had invalid length:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004542
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004543 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004544 sizeof(err_str));
4545}
4546
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004547/*
4548 * Handle a protocol violation when the parameter length is invalid.
Shan Wei33c7cfd2011-04-18 19:11:01 +00004549 * If the length is smaller than the minimum length of a given parameter,
4550 * or accumulated length in multi parameters exceeds the end of the chunk,
4551 * the length is considered as invalid.
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004552 */
4553static sctp_disposition_t sctp_sf_violation_paramlen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004554 struct net *net,
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004555 const struct sctp_endpoint *ep,
4556 const struct sctp_association *asoc,
4557 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -07004558 void *arg, void *ext,
4559 sctp_cmd_seq_t *commands)
4560{
4561 struct sctp_chunk *chunk = arg;
4562 struct sctp_paramhdr *param = ext;
4563 struct sctp_chunk *abort = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004564
Wei Yongjunba016672008-09-30 05:32:24 -07004565 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4566 goto discard;
4567
4568 /* Make the abort chunk. */
4569 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4570 if (!abort)
4571 goto nomem;
4572
4573 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004574 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Wei Yongjunba016672008-09-30 05:32:24 -07004575
4576 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4577 SCTP_ERROR(ECONNABORTED));
4578 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4579 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004580 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4581 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Wei Yongjunba016672008-09-30 05:32:24 -07004582
4583discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004584 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Wei Yongjunba016672008-09-30 05:32:24 -07004585 return SCTP_DISPOSITION_ABORT;
4586nomem:
4587 return SCTP_DISPOSITION_NOMEM;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004588}
4589
Wei Yongjunaecedea2007-08-02 16:57:44 +08004590/* Handle a protocol violation when the peer trying to advance the
4591 * cumulative tsn ack to a point beyond the max tsn currently sent.
4592 *
4593 * We inform the other end by sending an ABORT with a Protocol Violation
4594 * error code.
4595 */
4596static sctp_disposition_t sctp_sf_violation_ctsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004597 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004598 const struct sctp_endpoint *ep,
4599 const struct sctp_association *asoc,
4600 const sctp_subtype_t type,
4601 void *arg,
4602 sctp_cmd_seq_t *commands)
4603{
wangweidongcb3f8372013-12-23 12:16:50 +08004604 static const char err_str[] = "The cumulative tsn ack beyond the max tsn currently sent:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004605
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004606 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004607 sizeof(err_str));
4608}
4609
Vlad Yasevichece25df2007-09-07 16:30:54 -04004610/* Handle protocol violation of an invalid chunk bundling. For example,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004611 * when we have an association and we receive bundled INIT-ACK, or
Vlad Yasevichece25df2007-09-07 16:30:54 -04004612 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004613 * statement from the specs. Additionally, there might be an attacker
Vlad Yasevichece25df2007-09-07 16:30:54 -04004614 * on the path and we may not want to continue this communication.
4615 */
4616static sctp_disposition_t sctp_sf_violation_chunk(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004617 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004618 const struct sctp_endpoint *ep,
4619 const struct sctp_association *asoc,
4620 const sctp_subtype_t type,
4621 void *arg,
4622 sctp_cmd_seq_t *commands)
4623{
wangweidongcb3f8372013-12-23 12:16:50 +08004624 static const char err_str[] = "The following chunk violates protocol:";
Vlad Yasevichece25df2007-09-07 16:30:54 -04004625
4626 if (!asoc)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004627 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004628
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004629 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004630 sizeof(err_str));
4631}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632/***************************************************************************
4633 * These are the state functions for handling primitive (Section 10) events.
4634 ***************************************************************************/
4635/*
4636 * sctp_sf_do_prm_asoc
4637 *
4638 * Section: 10.1 ULP-to-SCTP
4639 * B) Associate
4640 *
4641 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4642 * outbound stream count)
4643 * -> association id [,destination transport addr list] [,outbound stream
4644 * count]
4645 *
4646 * This primitive allows the upper layer to initiate an association to a
4647 * specific peer endpoint.
4648 *
4649 * The peer endpoint shall be specified by one of the transport addresses
4650 * which defines the endpoint (see Section 1.4). If the local SCTP
4651 * instance has not been initialized, the ASSOCIATE is considered an
4652 * error.
4653 * [This is not relevant for the kernel implementation since we do all
4654 * initialization at boot time. It we hadn't initialized we wouldn't
4655 * get anywhere near this code.]
4656 *
4657 * An association id, which is a local handle to the SCTP association,
4658 * will be returned on successful establishment of the association. If
4659 * SCTP is not able to open an SCTP association with the peer endpoint,
4660 * an error is returned.
4661 * [In the kernel implementation, the struct sctp_association needs to
4662 * be created BEFORE causing this primitive to run.]
4663 *
4664 * Other association parameters may be returned, including the
4665 * complete destination transport addresses of the peer as well as the
4666 * outbound stream count of the local endpoint. One of the transport
4667 * address from the returned destination addresses will be selected by
4668 * the local endpoint as default primary path for sending SCTP packets
4669 * to this peer. The returned "destination transport addr list" can
4670 * be used by the ULP to change the default primary path or to force
4671 * sending a packet to a specific transport address. [All of this
4672 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4673 * function.]
4674 *
4675 * Mandatory attributes:
4676 *
4677 * o local SCTP instance name - obtained from the INITIALIZE operation.
4678 * [This is the argument asoc.]
4679 * o destination transport addr - specified as one of the transport
4680 * addresses of the peer endpoint with which the association is to be
4681 * established.
4682 * [This is asoc->peer.active_path.]
4683 * o outbound stream count - the number of outbound streams the ULP
4684 * would like to open towards this peer endpoint.
4685 * [BUG: This is not currently implemented.]
4686 * Optional attributes:
4687 *
4688 * None.
4689 *
4690 * The return value is a disposition.
4691 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004692sctp_disposition_t sctp_sf_do_prm_asoc(struct net *net,
4693 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 const struct sctp_association *asoc,
4695 const sctp_subtype_t type,
4696 void *arg,
4697 sctp_cmd_seq_t *commands)
4698{
4699 struct sctp_chunk *repl;
wangweidong26ac8e52013-12-23 12:16:51 +08004700 struct sctp_association *my_asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701
4702 /* The comment below says that we enter COOKIE-WAIT AFTER
4703 * sending the INIT, but that doesn't actually work in our
4704 * implementation...
4705 */
4706 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4707 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4708
4709 /* RFC 2960 5.1 Normal Establishment of an Association
4710 *
4711 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4712 * must provide its Verification Tag (Tag_A) in the Initiate
4713 * Tag field. Tag_A SHOULD be a random number in the range of
4714 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4715 */
4716
4717 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4718 if (!repl)
4719 goto nomem;
4720
Daniel Borkmann405426f2013-06-14 18:24:05 +02004721 /* Choose transport for INIT. */
4722 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4723 SCTP_CHUNK(repl));
4724
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 /* Cast away the const modifier, as we want to just
4726 * rerun it through as a sideffect.
4727 */
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004728 my_asoc = (struct sctp_association *)asoc;
4729 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730
4731 /* After sending the INIT, "A" starts the T1-init timer and
4732 * enters the COOKIE-WAIT state.
4733 */
4734 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4735 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4736 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4737 return SCTP_DISPOSITION_CONSUME;
4738
4739nomem:
4740 return SCTP_DISPOSITION_NOMEM;
4741}
4742
4743/*
4744 * Process the SEND primitive.
4745 *
4746 * Section: 10.1 ULP-to-SCTP
4747 * E) Send
4748 *
4749 * Format: SEND(association id, buffer address, byte count [,context]
4750 * [,stream id] [,life time] [,destination transport address]
4751 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4752 * -> result
4753 *
4754 * This is the main method to send user data via SCTP.
4755 *
4756 * Mandatory attributes:
4757 *
4758 * o association id - local handle to the SCTP association
4759 *
4760 * o buffer address - the location where the user message to be
4761 * transmitted is stored;
4762 *
4763 * o byte count - The size of the user data in number of bytes;
4764 *
4765 * Optional attributes:
4766 *
4767 * o context - an optional 32 bit integer that will be carried in the
4768 * sending failure notification to the ULP if the transportation of
4769 * this User Message fails.
4770 *
4771 * o stream id - to indicate which stream to send the data on. If not
4772 * specified, stream 0 will be used.
4773 *
4774 * o life time - specifies the life time of the user data. The user data
4775 * will not be sent by SCTP after the life time expires. This
4776 * parameter can be used to avoid efforts to transmit stale
4777 * user messages. SCTP notifies the ULP if the data cannot be
4778 * initiated to transport (i.e. sent to the destination via SCTP's
4779 * send primitive) within the life time variable. However, the
4780 * user data will be transmitted if SCTP has attempted to transmit a
4781 * chunk before the life time expired.
4782 *
4783 * o destination transport address - specified as one of the destination
4784 * transport addresses of the peer endpoint to which this packet
4785 * should be sent. Whenever possible, SCTP should use this destination
4786 * transport address for sending the packets, instead of the current
4787 * primary path.
4788 *
4789 * o unorder flag - this flag, if present, indicates that the user
4790 * would like the data delivered in an unordered fashion to the peer
4791 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4792 * message).
4793 *
4794 * o no-bundle flag - instructs SCTP not to bundle this user data with
4795 * other outbound DATA chunks. SCTP MAY still bundle even when
4796 * this flag is present, when faced with network congestion.
4797 *
4798 * o payload protocol-id - A 32 bit unsigned integer that is to be
4799 * passed to the peer indicating the type of payload protocol data
4800 * being transmitted. This value is passed as opaque data by SCTP.
4801 *
4802 * The return value is the disposition.
4803 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004804sctp_disposition_t sctp_sf_do_prm_send(struct net *net,
4805 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 const struct sctp_association *asoc,
4807 const sctp_subtype_t type,
4808 void *arg,
4809 sctp_cmd_seq_t *commands)
4810{
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004811 struct sctp_datamsg *msg = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004813 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 return SCTP_DISPOSITION_CONSUME;
4815}
4816
4817/*
4818 * Process the SHUTDOWN primitive.
4819 *
4820 * Section: 10.1:
4821 * C) Shutdown
4822 *
4823 * Format: SHUTDOWN(association id)
4824 * -> result
4825 *
4826 * Gracefully closes an association. Any locally queued user data
4827 * will be delivered to the peer. The association will be terminated only
4828 * after the peer acknowledges all the SCTP packets sent. A success code
4829 * will be returned on successful termination of the association. If
4830 * attempting to terminate the association results in a failure, an error
4831 * code shall be returned.
4832 *
4833 * Mandatory attributes:
4834 *
4835 * o association id - local handle to the SCTP association
4836 *
4837 * Optional attributes:
4838 *
4839 * None.
4840 *
4841 * The return value is the disposition.
4842 */
4843sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004844 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 const struct sctp_endpoint *ep,
4846 const struct sctp_association *asoc,
4847 const sctp_subtype_t type,
4848 void *arg,
4849 sctp_cmd_seq_t *commands)
4850{
4851 int disposition;
4852
4853 /* From 9.2 Shutdown of an Association
4854 * Upon receipt of the SHUTDOWN primitive from its upper
4855 * layer, the endpoint enters SHUTDOWN-PENDING state and
4856 * remains there until all outstanding data has been
4857 * acknowledged by its peer. The endpoint accepts no new data
4858 * from its upper layer, but retransmits data to the far end
4859 * if necessary to fill gaps.
4860 */
4861 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4862 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4863
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 disposition = SCTP_DISPOSITION_CONSUME;
4865 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004866 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 arg, commands);
4868 }
4869 return disposition;
4870}
4871
4872/*
4873 * Process the ABORT primitive.
4874 *
4875 * Section: 10.1:
4876 * C) Abort
4877 *
4878 * Format: Abort(association id [, cause code])
4879 * -> result
4880 *
4881 * Ungracefully closes an association. Any locally queued user data
4882 * will be discarded and an ABORT chunk is sent to the peer. A success code
4883 * will be returned on successful abortion of the association. If
4884 * attempting to abort the association results in a failure, an error
4885 * code shall be returned.
4886 *
4887 * Mandatory attributes:
4888 *
4889 * o association id - local handle to the SCTP association
4890 *
4891 * Optional attributes:
4892 *
4893 * o cause code - reason of the abort to be passed to the peer
4894 *
4895 * None.
4896 *
4897 * The return value is the disposition.
4898 */
4899sctp_disposition_t sctp_sf_do_9_1_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004900 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 const struct sctp_endpoint *ep,
4902 const struct sctp_association *asoc,
4903 const sctp_subtype_t type,
4904 void *arg,
4905 sctp_cmd_seq_t *commands)
4906{
4907 /* From 9.1 Abort of an Association
4908 * Upon receipt of the ABORT primitive from its upper
4909 * layer, the endpoint enters CLOSED state and
4910 * discard all outstanding data has been
4911 * acknowledged by its peer. The endpoint accepts no new data
4912 * from its upper layer, but retransmits data to the far end
4913 * if necessary to fill gaps.
4914 */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004915 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916
Xin Long068d8bd2015-12-29 17:49:25 +08004917 if (abort)
4918 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919
4920 /* Even if we can't send the ABORT due to low memory delete the
4921 * TCB. This is a departure from our typical NOMEM handling.
4922 */
4923
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004924 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4925 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 /* Delete the established association. */
4927 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08004928 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004930 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4931 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02004933 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934}
4935
4936/* We tried an illegal operation on an association which is closed. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004937sctp_disposition_t sctp_sf_error_closed(struct net *net,
4938 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 const struct sctp_association *asoc,
4940 const sctp_subtype_t type,
4941 void *arg,
4942 sctp_cmd_seq_t *commands)
4943{
4944 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4945 return SCTP_DISPOSITION_CONSUME;
4946}
4947
4948/* We tried an illegal operation on an association which is shutting
4949 * down.
4950 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004951sctp_disposition_t sctp_sf_error_shutdown(struct net *net,
4952 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 const struct sctp_association *asoc,
4954 const sctp_subtype_t type,
4955 void *arg,
4956 sctp_cmd_seq_t *commands)
4957{
4958 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4959 SCTP_ERROR(-ESHUTDOWN));
4960 return SCTP_DISPOSITION_CONSUME;
4961}
4962
4963/*
4964 * sctp_cookie_wait_prm_shutdown
4965 *
4966 * Section: 4 Note: 2
4967 * Verification Tag:
4968 * Inputs
4969 * (endpoint, asoc)
4970 *
4971 * The RFC does not explicitly address this issue, but is the route through the
4972 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4973 *
4974 * Outputs
4975 * (timers)
4976 */
4977sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004978 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 const struct sctp_endpoint *ep,
4980 const struct sctp_association *asoc,
4981 const sctp_subtype_t type,
4982 void *arg,
4983 sctp_cmd_seq_t *commands)
4984{
4985 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4986 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4987
4988 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4989 SCTP_STATE(SCTP_STATE_CLOSED));
4990
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004991 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992
4993 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4994
4995 return SCTP_DISPOSITION_DELETE_TCB;
4996}
4997
4998/*
4999 * sctp_cookie_echoed_prm_shutdown
5000 *
5001 * Section: 4 Note: 2
5002 * Verification Tag:
5003 * Inputs
5004 * (endpoint, asoc)
5005 *
5006 * The RFC does not explcitly address this issue, but is the route through the
5007 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
5008 *
5009 * Outputs
5010 * (timers)
5011 */
5012sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005013 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 const struct sctp_endpoint *ep,
5015 const struct sctp_association *asoc,
5016 const sctp_subtype_t type,
5017 void *arg, sctp_cmd_seq_t *commands)
5018{
5019 /* There is a single T1 timer, so we should be able to use
5020 * common function with the COOKIE-WAIT state.
5021 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005022 return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023}
5024
5025/*
5026 * sctp_sf_cookie_wait_prm_abort
5027 *
5028 * Section: 4 Note: 2
5029 * Verification Tag:
5030 * Inputs
5031 * (endpoint, asoc)
5032 *
5033 * The RFC does not explicitly address this issue, but is the route through the
5034 * state table when someone issues an abort while in COOKIE_WAIT state.
5035 *
5036 * Outputs
5037 * (timers)
5038 */
5039sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005040 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 const struct sctp_endpoint *ep,
5042 const struct sctp_association *asoc,
5043 const sctp_subtype_t type,
5044 void *arg,
5045 sctp_cmd_seq_t *commands)
5046{
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07005047 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048
5049 /* Stop T1-init timer */
5050 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5051 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052
Xin Long068d8bd2015-12-29 17:49:25 +08005053 if (abort)
5054 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055
5056 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5057 SCTP_STATE(SCTP_STATE_CLOSED));
5058
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005059 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060
5061 /* Even if we can't send the ABORT due to low memory delete the
5062 * TCB. This is a departure from our typical NOMEM handling.
5063 */
5064
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005065 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5066 SCTP_ERROR(ECONNREFUSED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 /* Delete the established association. */
5068 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005069 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02005071 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072}
5073
5074/*
5075 * sctp_sf_cookie_echoed_prm_abort
5076 *
5077 * Section: 4 Note: 3
5078 * Verification Tag:
5079 * Inputs
5080 * (endpoint, asoc)
5081 *
5082 * The RFC does not explcitly address this issue, but is the route through the
5083 * state table when someone issues an abort while in COOKIE_ECHOED state.
5084 *
5085 * Outputs
5086 * (timers)
5087 */
5088sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005089 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 const struct sctp_endpoint *ep,
5091 const struct sctp_association *asoc,
5092 const sctp_subtype_t type,
5093 void *arg,
5094 sctp_cmd_seq_t *commands)
5095{
5096 /* There is a single T1 timer, so we should be able to use
5097 * common function with the COOKIE-WAIT state.
5098 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005099 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100}
5101
5102/*
5103 * sctp_sf_shutdown_pending_prm_abort
5104 *
5105 * Inputs
5106 * (endpoint, asoc)
5107 *
5108 * The RFC does not explicitly address this issue, but is the route through the
5109 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5110 *
5111 * Outputs
5112 * (timers)
5113 */
5114sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005115 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 const struct sctp_endpoint *ep,
5117 const struct sctp_association *asoc,
5118 const sctp_subtype_t type,
5119 void *arg,
5120 sctp_cmd_seq_t *commands)
5121{
5122 /* Stop the T5-shutdown guard timer. */
5123 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5124 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5125
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005126 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127}
5128
5129/*
5130 * sctp_sf_shutdown_sent_prm_abort
5131 *
5132 * Inputs
5133 * (endpoint, asoc)
5134 *
5135 * The RFC does not explicitly address this issue, but is the route through the
5136 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5137 *
5138 * Outputs
5139 * (timers)
5140 */
5141sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005142 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 const struct sctp_endpoint *ep,
5144 const struct sctp_association *asoc,
5145 const sctp_subtype_t type,
5146 void *arg,
5147 sctp_cmd_seq_t *commands)
5148{
5149 /* Stop the T2-shutdown timer. */
5150 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5151 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5152
5153 /* Stop the T5-shutdown guard timer. */
5154 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5155 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5156
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005157 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158}
5159
5160/*
5161 * sctp_sf_cookie_echoed_prm_abort
5162 *
5163 * Inputs
5164 * (endpoint, asoc)
5165 *
5166 * The RFC does not explcitly address this issue, but is the route through the
5167 * state table when someone issues an abort while in COOKIE_ECHOED state.
5168 *
5169 * Outputs
5170 * (timers)
5171 */
5172sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005173 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 const struct sctp_endpoint *ep,
5175 const struct sctp_association *asoc,
5176 const sctp_subtype_t type,
5177 void *arg,
5178 sctp_cmd_seq_t *commands)
5179{
5180 /* The same T2 timer, so we should be able to use
5181 * common function with the SHUTDOWN-SENT state.
5182 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005183 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184}
5185
5186/*
5187 * Process the REQUESTHEARTBEAT primitive
5188 *
5189 * 10.1 ULP-to-SCTP
5190 * J) Request Heartbeat
5191 *
5192 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5193 *
5194 * -> result
5195 *
5196 * Instructs the local endpoint to perform a HeartBeat on the specified
5197 * destination transport address of the given association. The returned
5198 * result should indicate whether the transmission of the HEARTBEAT
5199 * chunk to the destination address is successful.
5200 *
5201 * Mandatory attributes:
5202 *
5203 * o association id - local handle to the SCTP association
5204 *
5205 * o destination transport address - the transport address of the
5206 * association on which a heartbeat should be issued.
5207 */
5208sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005209 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 const struct sctp_endpoint *ep,
5211 const struct sctp_association *asoc,
5212 const sctp_subtype_t type,
5213 void *arg,
5214 sctp_cmd_seq_t *commands)
5215{
Vlad Yasevichfb785252007-03-19 17:02:03 -07005216 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5217 (struct sctp_transport *)arg, commands))
5218 return SCTP_DISPOSITION_NOMEM;
5219
5220 /*
5221 * RFC 2960 (bis), section 8.3
5222 *
5223 * D) Request an on-demand HEARTBEAT on a specific destination
5224 * transport address of a given association.
5225 *
5226 * The endpoint should increment the respective error counter of
5227 * the destination transport address each time a HEARTBEAT is sent
5228 * to that address and not acknowledged within one RTO.
5229 *
5230 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00005231 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Vlad Yasevichfb785252007-03-19 17:02:03 -07005232 SCTP_TRANSPORT(arg));
5233 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234}
5235
5236/*
5237 * ADDIP Section 4.1 ASCONF Chunk Procedures
5238 * When an endpoint has an ASCONF signaled change to be sent to the
5239 * remote endpoint it should do A1 to A9
5240 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005241sctp_disposition_t sctp_sf_do_prm_asconf(struct net *net,
5242 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 const struct sctp_association *asoc,
5244 const sctp_subtype_t type,
5245 void *arg,
5246 sctp_cmd_seq_t *commands)
5247{
5248 struct sctp_chunk *chunk = arg;
5249
5250 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5251 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5252 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5253 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5254 return SCTP_DISPOSITION_CONSUME;
5255}
5256
Xin Long7a090b02017-01-18 00:44:44 +08005257/* RE-CONFIG Section 5.1 RECONF Chunk Procedures */
5258sctp_disposition_t sctp_sf_do_prm_reconf(struct net *net,
5259 const struct sctp_endpoint *ep,
5260 const struct sctp_association *asoc,
5261 const sctp_subtype_t type,
5262 void *arg, sctp_cmd_seq_t *commands)
5263{
5264 struct sctp_chunk *chunk = arg;
5265
5266 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5267 return SCTP_DISPOSITION_CONSUME;
5268}
5269
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270/*
5271 * Ignore the primitive event
5272 *
5273 * The return value is the disposition of the primitive.
5274 */
5275sctp_disposition_t sctp_sf_ignore_primitive(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005276 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 const struct sctp_endpoint *ep,
5278 const struct sctp_association *asoc,
5279 const sctp_subtype_t type,
5280 void *arg,
5281 sctp_cmd_seq_t *commands)
5282{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005283 pr_debug("%s: primitive type:%d is ignored\n", __func__,
5284 type.primitive);
5285
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 return SCTP_DISPOSITION_DISCARD;
5287}
5288
5289/***************************************************************************
5290 * These are the state functions for the OTHER events.
5291 ***************************************************************************/
5292
5293/*
Wei Yongjune1cdd552011-04-17 17:29:03 +00005294 * When the SCTP stack has no more user data to send or retransmit, this
5295 * notification is given to the user. Also, at the time when a user app
5296 * subscribes to this event, if there is no data to be sent or
5297 * retransmit, the stack will immediately send up this notification.
5298 */
5299sctp_disposition_t sctp_sf_do_no_pending_tsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005300 struct net *net,
Wei Yongjune1cdd552011-04-17 17:29:03 +00005301 const struct sctp_endpoint *ep,
5302 const struct sctp_association *asoc,
5303 const sctp_subtype_t type,
5304 void *arg,
5305 sctp_cmd_seq_t *commands)
5306{
5307 struct sctp_ulpevent *event;
5308
5309 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5310 if (!event)
5311 return SCTP_DISPOSITION_NOMEM;
5312
5313 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5314
5315 return SCTP_DISPOSITION_CONSUME;
5316}
5317
5318/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 * Start the shutdown negotiation.
5320 *
5321 * From Section 9.2:
5322 * Once all its outstanding data has been acknowledged, the endpoint
5323 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5324 * TSN Ack field the last sequential TSN it has received from the peer.
5325 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5326 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5327 * with the updated last sequential TSN received from its peer.
5328 *
5329 * The return value is the disposition.
5330 */
5331sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005332 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 const struct sctp_endpoint *ep,
5334 const struct sctp_association *asoc,
5335 const sctp_subtype_t type,
5336 void *arg,
5337 sctp_cmd_seq_t *commands)
5338{
5339 struct sctp_chunk *reply;
5340
5341 /* Once all its outstanding data has been acknowledged, the
5342 * endpoint shall send a SHUTDOWN chunk to its peer including
5343 * in the Cumulative TSN Ack field the last sequential TSN it
5344 * has received from the peer.
5345 */
5346 reply = sctp_make_shutdown(asoc, NULL);
5347 if (!reply)
5348 goto nomem;
5349
5350 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5351 * T2-shutdown timer.
5352 */
5353 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5354
5355 /* It shall then start the T2-shutdown timer */
5356 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5357 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5358
Wei Yongjun536428a2008-09-05 08:55:26 +08005359 /* RFC 4960 Section 9.2
5360 * The sender of the SHUTDOWN MAY also start an overall guard timer
5361 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5362 */
Thomas Graff8d96052011-07-07 00:28:35 +00005363 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
Wei Yongjun536428a2008-09-05 08:55:26 +08005364 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5365
Neil Horman9f70f462013-12-10 06:48:15 -05005366 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5368 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5369
5370 /* and enter the SHUTDOWN-SENT state. */
5371 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5372 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5373
5374 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5375 *
5376 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005377 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 */
5379 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5380
5381 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5382
5383 return SCTP_DISPOSITION_CONSUME;
5384
5385nomem:
5386 return SCTP_DISPOSITION_NOMEM;
5387}
5388
5389/*
5390 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5391 *
5392 * From Section 9.2:
5393 *
5394 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5395 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5396 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5397 * endpoint must re-send the SHUTDOWN ACK.
5398 *
5399 * The return value is the disposition.
5400 */
5401sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005402 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 const struct sctp_endpoint *ep,
5404 const struct sctp_association *asoc,
5405 const sctp_subtype_t type,
5406 void *arg,
5407 sctp_cmd_seq_t *commands)
5408{
5409 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5410 struct sctp_chunk *reply;
5411
5412 /* There are 2 ways of getting here:
5413 * 1) called in response to a SHUTDOWN chunk
5414 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5415 *
5416 * For the case (2), the arg parameter is set to NULL. We need
5417 * to check that we have a chunk before accessing it's fields.
5418 */
5419 if (chunk) {
5420 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005421 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422
5423 /* Make sure that the SHUTDOWN chunk has a valid length. */
5424 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005425 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 commands);
5427 }
5428
5429 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5430 * shall send a SHUTDOWN ACK ...
5431 */
5432 reply = sctp_make_shutdown_ack(asoc, chunk);
5433 if (!reply)
5434 goto nomem;
5435
5436 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5437 * the T2-shutdown timer.
5438 */
5439 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5440
5441 /* and start/restart a T2-shutdown timer of its own, */
5442 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5443 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5444
Neil Horman9f70f462013-12-10 06:48:15 -05005445 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5447 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5448
5449 /* Enter the SHUTDOWN-ACK-SENT state. */
5450 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5451 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5452
5453 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5454 *
5455 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005456 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 */
5458 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5459
5460 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5461
5462 return SCTP_DISPOSITION_CONSUME;
5463
5464nomem:
5465 return SCTP_DISPOSITION_NOMEM;
5466}
5467
5468/*
5469 * Ignore the event defined as other
5470 *
5471 * The return value is the disposition of the event.
5472 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005473sctp_disposition_t sctp_sf_ignore_other(struct net *net,
5474 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 const struct sctp_association *asoc,
5476 const sctp_subtype_t type,
5477 void *arg,
5478 sctp_cmd_seq_t *commands)
5479{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005480 pr_debug("%s: the event other type:%d is ignored\n",
5481 __func__, type.other);
5482
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 return SCTP_DISPOSITION_DISCARD;
5484}
5485
5486/************************************************************
5487 * These are the state functions for handling timeout events.
5488 ************************************************************/
5489
5490/*
5491 * RTX Timeout
5492 *
5493 * Section: 6.3.3 Handle T3-rtx Expiration
5494 *
5495 * Whenever the retransmission timer T3-rtx expires for a destination
5496 * address, do the following:
5497 * [See below]
5498 *
5499 * The return value is the disposition of the chunk.
5500 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005501sctp_disposition_t sctp_sf_do_6_3_3_rtx(struct net *net,
5502 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 const struct sctp_association *asoc,
5504 const sctp_subtype_t type,
5505 void *arg,
5506 sctp_cmd_seq_t *commands)
5507{
5508 struct sctp_transport *transport = arg;
5509
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005510 SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005511
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 if (asoc->overall_error_count >= asoc->max_retrans) {
lucien8a0d19c2015-12-05 15:35:36 +08005513 if (asoc->peer.zero_window_announced &&
5514 asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
Thomas Graff8d96052011-07-07 00:28:35 +00005515 /*
5516 * We are here likely because the receiver had its rwnd
5517 * closed for a while and we have not been able to
5518 * transmit the locally queued data within the maximum
5519 * retransmission attempts limit. Start the T5
5520 * shutdown guard timer to give the receiver one last
5521 * chance and some additional time to recover before
5522 * aborting.
5523 */
5524 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5525 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5526 } else {
5527 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5528 SCTP_ERROR(ETIMEDOUT));
5529 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5530 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5531 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005532 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5533 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Thomas Graff8d96052011-07-07 00:28:35 +00005534 return SCTP_DISPOSITION_DELETE_TCB;
5535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 }
5537
5538 /* E1) For the destination address for which the timer
5539 * expires, adjust its ssthresh with rules defined in Section
5540 * 7.2.3 and set the cwnd <- MTU.
5541 */
5542
5543 /* E2) For the destination address for which the timer
5544 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5545 * maximum value discussed in rule C7 above (RTO.max) may be
5546 * used to provide an upper bound to this doubling operation.
5547 */
5548
5549 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5550 * outstanding DATA chunks for the address for which the
5551 * T3-rtx has expired will fit into a single packet, subject
5552 * to the MTU constraint for the path corresponding to the
5553 * destination transport address to which the retransmission
5554 * is being sent (this may be different from the address for
5555 * which the timer expires [see Section 6.4]). Call this
5556 * value K. Bundle and retransmit those K DATA chunks in a
5557 * single packet to the destination endpoint.
5558 *
5559 * Note: Any DATA chunks that were sent to the address for
5560 * which the T3-rtx timer expired but did not fit in one MTU
5561 * (rule E3 above), should be marked for retransmission and
5562 * sent as soon as cwnd allows (normally when a SACK arrives).
5563 */
5564
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 /* Do some failure management (Section 8.2). */
5566 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5567
Vlad Yasevich1845a572007-02-21 02:06:19 -08005568 /* NB: Rules E4 and F1 are implicit in R1. */
5569 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5570
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 return SCTP_DISPOSITION_CONSUME;
5572}
5573
5574/*
5575 * Generate delayed SACK on timeout
5576 *
5577 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5578 *
5579 * The guidelines on delayed acknowledgement algorithm specified in
5580 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5581 * acknowledgement SHOULD be generated for at least every second packet
5582 * (not every second DATA chunk) received, and SHOULD be generated
5583 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5584 * some situations it may be beneficial for an SCTP transmitter to be
5585 * more conservative than the algorithms detailed in this document
5586 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5587 * the following algorithms allow.
5588 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005589sctp_disposition_t sctp_sf_do_6_2_sack(struct net *net,
5590 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 const struct sctp_association *asoc,
5592 const sctp_subtype_t type,
5593 void *arg,
5594 sctp_cmd_seq_t *commands)
5595{
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005596 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5598 return SCTP_DISPOSITION_CONSUME;
5599}
5600
5601/*
Frank Filz3f7a87d2005-06-20 13:14:57 -07005602 * sctp_sf_t1_init_timer_expire
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 *
5604 * Section: 4 Note: 2
5605 * Verification Tag:
5606 * Inputs
5607 * (endpoint, asoc)
5608 *
5609 * RFC 2960 Section 4 Notes
5610 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5611 * and re-start the T1-init timer without changing state. This MUST
5612 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5613 * endpoint MUST abort the initialization process and report the
5614 * error to SCTP user.
5615 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07005616 * Outputs
5617 * (timers, events)
5618 *
5619 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005620sctp_disposition_t sctp_sf_t1_init_timer_expire(struct net *net,
5621 const struct sctp_endpoint *ep,
Frank Filz3f7a87d2005-06-20 13:14:57 -07005622 const struct sctp_association *asoc,
5623 const sctp_subtype_t type,
5624 void *arg,
5625 sctp_cmd_seq_t *commands)
5626{
5627 struct sctp_chunk *repl = NULL;
5628 struct sctp_bind_addr *bp;
5629 int attempts = asoc->init_err_counter + 1;
5630
Daniel Borkmannbb333812013-06-28 19:49:40 +02005631 pr_debug("%s: timer T1 expired (INIT)\n", __func__);
5632
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005633 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005634
Vlad Yasevich81845c22006-01-30 15:59:54 -08005635 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005636 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5637 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5638 if (!repl)
5639 return SCTP_DISPOSITION_NOMEM;
5640
5641 /* Choose transport for INIT. */
5642 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5643 SCTP_CHUNK(repl));
5644
5645 /* Issue a sideeffect to do the needed accounting. */
5646 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5647 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5648
5649 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5650 } else {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005651 pr_debug("%s: giving up on INIT, attempts:%d "
5652 "max_init_attempts:%d\n", __func__, attempts,
5653 asoc->max_init_attempts);
5654
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005655 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5656 SCTP_ERROR(ETIMEDOUT));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005657 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005658 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005659 return SCTP_DISPOSITION_DELETE_TCB;
5660 }
5661
5662 return SCTP_DISPOSITION_CONSUME;
5663}
5664
5665/*
5666 * sctp_sf_t1_cookie_timer_expire
5667 *
5668 * Section: 4 Note: 2
5669 * Verification Tag:
5670 * Inputs
5671 * (endpoint, asoc)
5672 *
5673 * RFC 2960 Section 4 Notes
5674 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 * COOKIE ECHO and re-start the T1-cookie timer without changing
5676 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5677 * After that, the endpoint MUST abort the initialization process and
5678 * report the error to SCTP user.
5679 *
5680 * Outputs
5681 * (timers, events)
5682 *
5683 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005684sctp_disposition_t sctp_sf_t1_cookie_timer_expire(struct net *net,
5685 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 const struct sctp_association *asoc,
5687 const sctp_subtype_t type,
5688 void *arg,
5689 sctp_cmd_seq_t *commands)
5690{
Frank Filz3f7a87d2005-06-20 13:14:57 -07005691 struct sctp_chunk *repl = NULL;
5692 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693
Daniel Borkmannbb333812013-06-28 19:49:40 +02005694 pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
5695
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005696 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697
Vlad Yasevich81845c22006-01-30 15:59:54 -08005698 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005699 repl = sctp_make_cookie_echo(asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 if (!repl)
Frank Filz3f7a87d2005-06-20 13:14:57 -07005701 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702
Vlad Yasevich96cd0d32008-09-08 14:00:26 -04005703 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5704 SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 /* Issue a sideeffect to do the needed accounting. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005706 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5707 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5708
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5710 } else {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005711 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5712 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005714 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 return SCTP_DISPOSITION_DELETE_TCB;
5716 }
5717
5718 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719}
5720
5721/* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5722 * with the updated last sequential TSN received from its peer.
5723 *
5724 * An endpoint should limit the number of retransmissions of the
5725 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5726 * If this threshold is exceeded the endpoint should destroy the TCB and
5727 * MUST report the peer endpoint unreachable to the upper layer (and
5728 * thus the association enters the CLOSED state). The reception of any
5729 * packet from its peer (i.e. as the peer sends all of its queued DATA
5730 * chunks) should clear the endpoint's retransmission count and restart
5731 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5732 * all of its queued DATA chunks that have not yet been sent.
5733 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005734sctp_disposition_t sctp_sf_t2_timer_expire(struct net *net,
5735 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 const struct sctp_association *asoc,
5737 const sctp_subtype_t type,
5738 void *arg,
5739 sctp_cmd_seq_t *commands)
5740{
5741 struct sctp_chunk *reply = NULL;
5742
Daniel Borkmannbb333812013-06-28 19:49:40 +02005743 pr_debug("%s: timer T2 expired\n", __func__);
5744
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005745 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005746
Neil Horman58fbbed2008-02-29 11:40:56 -08005747 ((struct sctp_association *)asoc)->shutdown_retries++;
5748
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005750 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5751 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5753 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005754 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005755 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5756 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 return SCTP_DISPOSITION_DELETE_TCB;
5758 }
5759
5760 switch (asoc->state) {
5761 case SCTP_STATE_SHUTDOWN_SENT:
5762 reply = sctp_make_shutdown(asoc, NULL);
5763 break;
5764
5765 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5766 reply = sctp_make_shutdown_ack(asoc, NULL);
5767 break;
5768
5769 default:
5770 BUG();
5771 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773
5774 if (!reply)
5775 goto nomem;
5776
Wei Yongjun6345b192009-04-26 23:13:35 +08005777 /* Do some failure management (Section 8.2).
5778 * If we remove the transport an SHUTDOWN was last sent to, don't
5779 * do failure management.
5780 */
5781 if (asoc->shutdown_last_sent_to)
5782 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5783 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784
5785 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5786 * the T2-shutdown timer.
5787 */
5788 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5789
5790 /* Restart the T2-shutdown timer. */
5791 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5792 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5793 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5794 return SCTP_DISPOSITION_CONSUME;
5795
5796nomem:
5797 return SCTP_DISPOSITION_NOMEM;
5798}
5799
5800/*
5801 * ADDIP Section 4.1 ASCONF CHunk Procedures
5802 * If the T4 RTO timer expires the endpoint should do B1 to B5
5803 */
5804sctp_disposition_t sctp_sf_t4_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005805 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 const struct sctp_endpoint *ep,
5807 const struct sctp_association *asoc,
5808 const sctp_subtype_t type,
5809 void *arg,
5810 sctp_cmd_seq_t *commands)
5811{
5812 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5813 struct sctp_transport *transport = chunk->transport;
5814
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005815 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005816
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5818 * detection on the appropriate destination address as defined in
5819 * RFC2960 [5] section 8.1 and 8.2.
5820 */
Wei Yongjun10a43ce2009-04-26 23:14:42 +08005821 if (transport)
5822 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5823 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824
5825 /* Reconfig T4 timer and transport. */
5826 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5827
5828 /* ADDIP 4.1 B2) Increment the association error counters and perform
5829 * endpoint failure detection on the association as defined in
5830 * RFC2960 [5] section 8.1 and 8.2.
5831 * association error counter is incremented in SCTP_CMD_STRIKE.
5832 */
5833 if (asoc->overall_error_count >= asoc->max_retrans) {
5834 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5835 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005836 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5837 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005839 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005840 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5841 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 return SCTP_DISPOSITION_ABORT;
5843 }
5844
5845 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5846 * the ASCONF chunk was sent by doubling the RTO timer value.
5847 * This is done in SCTP_CMD_STRIKE.
5848 */
5849
5850 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5851 * choose an alternate destination address (please refer to RFC2960
5852 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005853 * chunk, it MUST be the same (including its serial number) as the last
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 * ASCONF sent.
5855 */
5856 sctp_chunk_hold(asoc->addip_last_asconf);
5857 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5858 SCTP_CHUNK(asoc->addip_last_asconf));
5859
5860 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5861 * destination is selected, then the RTO used will be that of the new
5862 * destination address.
5863 */
5864 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5865 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5866
5867 return SCTP_DISPOSITION_CONSUME;
5868}
5869
5870/* sctpimpguide-05 Section 2.12.2
5871 * The sender of the SHUTDOWN MAY also start an overall guard timer
5872 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5873 * At the expiration of this timer the sender SHOULD abort the association
5874 * by sending an ABORT chunk.
5875 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005876sctp_disposition_t sctp_sf_t5_timer_expire(struct net *net,
5877 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 const struct sctp_association *asoc,
5879 const sctp_subtype_t type,
5880 void *arg,
5881 sctp_cmd_seq_t *commands)
5882{
5883 struct sctp_chunk *reply = NULL;
5884
Daniel Borkmannbb333812013-06-28 19:49:40 +02005885 pr_debug("%s: timer T5 expired\n", __func__);
5886
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005887 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888
5889 reply = sctp_make_abort(asoc, NULL, 0);
5890 if (!reply)
5891 goto nomem;
5892
5893 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005894 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5895 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005897 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005899 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5900 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevicha1080a82008-10-09 14:33:26 -07005901
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902 return SCTP_DISPOSITION_DELETE_TCB;
5903nomem:
5904 return SCTP_DISPOSITION_NOMEM;
5905}
5906
5907/* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5908 * the association is automatically closed by starting the shutdown process.
5909 * The work that needs to be done is same as when SHUTDOWN is initiated by
5910 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5911 */
5912sctp_disposition_t sctp_sf_autoclose_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005913 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 const struct sctp_endpoint *ep,
5915 const struct sctp_association *asoc,
5916 const sctp_subtype_t type,
5917 void *arg,
5918 sctp_cmd_seq_t *commands)
5919{
5920 int disposition;
5921
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005922 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005923
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 /* From 9.2 Shutdown of an Association
5925 * Upon receipt of the SHUTDOWN primitive from its upper
5926 * layer, the endpoint enters SHUTDOWN-PENDING state and
5927 * remains there until all outstanding data has been
5928 * acknowledged by its peer. The endpoint accepts no new data
5929 * from its upper layer, but retransmits data to the far end
5930 * if necessary to fill gaps.
5931 */
5932 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5933 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5934
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 disposition = SCTP_DISPOSITION_CONSUME;
5936 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005937 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938 arg, commands);
5939 }
5940 return disposition;
5941}
5942
5943/*****************************************************************************
5944 * These are sa state functions which could apply to all types of events.
5945 ****************************************************************************/
5946
5947/*
5948 * This table entry is not implemented.
5949 *
5950 * Inputs
5951 * (endpoint, asoc, chunk)
5952 *
5953 * The return value is the disposition of the chunk.
5954 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005955sctp_disposition_t sctp_sf_not_impl(struct net *net,
5956 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 const struct sctp_association *asoc,
5958 const sctp_subtype_t type,
5959 void *arg,
5960 sctp_cmd_seq_t *commands)
5961{
5962 return SCTP_DISPOSITION_NOT_IMPL;
5963}
5964
5965/*
5966 * This table entry represents a bug.
5967 *
5968 * Inputs
5969 * (endpoint, asoc, chunk)
5970 *
5971 * The return value is the disposition of the chunk.
5972 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005973sctp_disposition_t sctp_sf_bug(struct net *net,
5974 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 const struct sctp_association *asoc,
5976 const sctp_subtype_t type,
5977 void *arg,
5978 sctp_cmd_seq_t *commands)
5979{
5980 return SCTP_DISPOSITION_BUG;
5981}
5982
5983/*
5984 * This table entry represents the firing of a timer in the wrong state.
5985 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5986 * when the association is in the wrong state. This event should
5987 * be ignored, so as to prevent any rearming of the timer.
5988 *
5989 * Inputs
5990 * (endpoint, asoc, chunk)
5991 *
5992 * The return value is the disposition of the chunk.
5993 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005994sctp_disposition_t sctp_sf_timer_ignore(struct net *net,
5995 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 const struct sctp_association *asoc,
5997 const sctp_subtype_t type,
5998 void *arg,
5999 sctp_cmd_seq_t *commands)
6000{
Daniel Borkmannbb333812013-06-28 19:49:40 +02006001 pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
6002
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003 return SCTP_DISPOSITION_CONSUME;
6004}
6005
6006/********************************************************************
6007 * 2nd Level Abstractions
6008 ********************************************************************/
6009
6010/* Pull the SACK chunk based on the SACK header. */
6011static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
6012{
6013 struct sctp_sackhdr *sack;
6014 unsigned int len;
6015 __u16 num_blocks;
6016 __u16 num_dup_tsns;
6017
6018 /* Protect ourselves from reading too far into
6019 * the skb from a bogus sender.
6020 */
6021 sack = (struct sctp_sackhdr *) chunk->skb->data;
6022
6023 num_blocks = ntohs(sack->num_gap_ack_blocks);
6024 num_dup_tsns = ntohs(sack->num_dup_tsns);
6025 len = sizeof(struct sctp_sackhdr);
6026 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
6027 if (len > chunk->skb->len)
6028 return NULL;
6029
6030 skb_pull(chunk->skb, len);
6031
6032 return sack;
6033}
6034
6035/* Create an ABORT packet to be sent as a response, with the specified
6036 * error causes.
6037 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006038static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
6039 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 const struct sctp_association *asoc,
6041 struct sctp_chunk *chunk,
6042 const void *payload,
6043 size_t paylen)
6044{
6045 struct sctp_packet *packet;
6046 struct sctp_chunk *abort;
6047
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006048 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049
6050 if (packet) {
6051 /* Make an ABORT.
6052 * The T bit will be set if the asoc is NULL.
6053 */
6054 abort = sctp_make_abort(asoc, chunk, paylen);
6055 if (!abort) {
6056 sctp_ootb_pkt_free(packet);
6057 return NULL;
6058 }
Jerome Forissier047a2422005-04-28 11:58:43 -07006059
6060 /* Reflect vtag if T-Bit is set */
6061 if (sctp_test_T_bit(abort))
6062 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
6063
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 /* Add specified error causes, i.e., payload, to the
6065 * end of the chunk.
6066 */
6067 sctp_addto_chunk(abort, paylen, payload);
6068
6069 /* Set the skb to the belonging sock for accounting. */
6070 abort->skb->sk = ep->base.sk;
6071
6072 sctp_packet_append_chunk(packet, abort);
6073
6074 }
6075
6076 return packet;
6077}
6078
6079/* Allocate a packet for responding in the OOTB conditions. */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006080static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
6081 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 const struct sctp_chunk *chunk)
6083{
6084 struct sctp_packet *packet;
6085 struct sctp_transport *transport;
6086 __u16 sport;
6087 __u16 dport;
6088 __u32 vtag;
6089
6090 /* Get the source and destination port from the inbound packet. */
6091 sport = ntohs(chunk->sctp_hdr->dest);
6092 dport = ntohs(chunk->sctp_hdr->source);
6093
6094 /* The V-tag is going to be the same as the inbound packet if no
6095 * association exists, otherwise, use the peer's vtag.
6096 */
6097 if (asoc) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08006098 /* Special case the INIT-ACK as there is no peer's vtag
6099 * yet.
6100 */
wangweidongcb3f8372013-12-23 12:16:50 +08006101 switch (chunk->chunk_hdr->type) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08006102 case SCTP_CID_INIT_ACK:
6103 {
Xin Longcb1844c2017-07-23 09:34:26 +08006104 struct sctp_initack_chunk *initack;
Wei Yongjun02c4e122007-08-31 10:03:58 +08006105
Xin Longcb1844c2017-07-23 09:34:26 +08006106 initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
Wei Yongjun02c4e122007-08-31 10:03:58 +08006107 vtag = ntohl(initack->init_hdr.init_tag);
6108 break;
6109 }
6110 default:
6111 vtag = asoc->peer.i.init_tag;
6112 break;
6113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114 } else {
6115 /* Special case the INIT and stale COOKIE_ECHO as there is no
6116 * vtag yet.
6117 */
wangweidongcb3f8372013-12-23 12:16:50 +08006118 switch (chunk->chunk_hdr->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119 case SCTP_CID_INIT:
6120 {
Xin Long01a992b2017-06-30 11:52:22 +08006121 struct sctp_init_chunk *init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122
Xin Long01a992b2017-06-30 11:52:22 +08006123 init = (struct sctp_init_chunk *)chunk->chunk_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124 vtag = ntohl(init->init_hdr.init_tag);
6125 break;
6126 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006127 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 vtag = ntohl(chunk->sctp_hdr->vtag);
6129 break;
6130 }
6131 }
6132
6133 /* Make a transport for the bucket, Eliza... */
Eric W. Biederman89bf3452012-08-07 07:26:14 +00006134 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 if (!transport)
6136 goto nomem;
6137
6138 /* Cache a route for the transport with the chunk's destination as
6139 * the source address.
6140 */
Al Viro16b0a032006-11-20 17:13:38 -08006141 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006142 sctp_sk(net->sctp.ctl_sock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143
Marcelo Ricardo Leitner66b91d22016-12-28 09:26:34 -02006144 packet = &transport->packet;
6145 sctp_packet_init(packet, transport, sport, dport);
6146 sctp_packet_config(packet, vtag, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147
6148 return packet;
6149
6150nomem:
6151 return NULL;
6152}
6153
6154/* Free the packet allocated earlier for responding in the OOTB condition. */
6155void sctp_ootb_pkt_free(struct sctp_packet *packet)
6156{
6157 sctp_transport_free(packet->transport);
6158}
6159
6160/* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006161static void sctp_send_stale_cookie_err(struct net *net,
6162 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 const struct sctp_association *asoc,
6164 const struct sctp_chunk *chunk,
6165 sctp_cmd_seq_t *commands,
6166 struct sctp_chunk *err_chunk)
6167{
6168 struct sctp_packet *packet;
6169
6170 if (err_chunk) {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006171 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172 if (packet) {
6173 struct sctp_signed_cookie *cookie;
6174
6175 /* Override the OOTB vtag from the cookie. */
6176 cookie = chunk->subh.cookie_hdr;
6177 packet->vtag = cookie->c.peer_vtag;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006178
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 /* Set the skb to the belonging sock for accounting. */
6180 err_chunk->skb->sk = ep->base.sk;
6181 sctp_packet_append_chunk(packet, err_chunk);
6182 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6183 SCTP_PACKET(packet));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006184 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185 } else
6186 sctp_chunk_free (err_chunk);
6187 }
6188}
6189
6190
6191/* Process a data chunk */
6192static int sctp_eat_data(const struct sctp_association *asoc,
6193 struct sctp_chunk *chunk,
6194 sctp_cmd_seq_t *commands)
6195{
Xin Long3583df12017-06-30 11:52:19 +08006196 struct sctp_datahdr *data_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 struct sctp_chunk *err;
6198 size_t datalen;
6199 sctp_verb_t deliver;
6200 int tmp;
6201 __u32 tsn;
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006202 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
Neil Horman049b3ff2005-11-11 16:08:24 -08006203 struct sock *sk = asoc->base.sk;
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006204 struct net *net = sock_net(sk);
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006205 u16 ssn;
6206 u16 sid;
6207 u8 ordered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208
Xin Long3583df12017-06-30 11:52:19 +08006209 data_hdr = (struct sctp_datahdr *)chunk->skb->data;
6210 chunk->subh.data_hdr = data_hdr;
6211 skb_pull(chunk->skb, sizeof(*data_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212
6213 tsn = ntohl(data_hdr->tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02006214 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215
6216 /* ASSERT: Now skb->data is really the user data. */
6217
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 /* Process ECN based congestion.
6219 *
6220 * Since the chunk structure is reused for all chunks within
6221 * a packet, we use ecn_ce_done to track if we've already
6222 * done CE processing for this packet.
6223 *
6224 * We need to do ECN processing even if we plan to discard the
6225 * chunk later.
6226 */
6227
Marcelo Ricardo Leitner2d47fd12016-07-13 15:09:00 -03006228 if (asoc->peer.ecn_capable && !chunk->ecn_ce_done) {
Marcelo Ricardo Leitnere7487c82016-07-13 15:08:58 -03006229 struct sctp_af *af = SCTP_INPUT_CB(chunk->skb)->af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230 chunk->ecn_ce_done = 1;
6231
Marcelo Ricardo Leitner2d47fd12016-07-13 15:09:00 -03006232 if (af->is_ce(sctp_gso_headskb(chunk->skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233 /* Do real work as sideffect. */
6234 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6235 SCTP_U32(tsn));
6236 }
6237 }
6238
6239 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6240 if (tmp < 0) {
6241 /* The TSN is too high--silently discard the chunk and
6242 * count on it getting retransmitted later.
6243 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006244 if (chunk->asoc)
6245 chunk->asoc->stats.outofseqtsns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 return SCTP_IERROR_HIGH_TSN;
6247 } else if (tmp > 0) {
6248 /* This is a duplicate. Record it. */
6249 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6250 return SCTP_IERROR_DUP_TSN;
6251 }
6252
6253 /* This is a new TSN. */
6254
6255 /* Discard if there is no room in the receive window.
6256 * Actually, allow a little bit of overflow (up to a MTU).
6257 */
6258 datalen = ntohs(chunk->chunk_hdr->length);
Xin Long9f8d3142017-06-30 11:52:20 +08006259 datalen -= sizeof(struct sctp_data_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260
6261 deliver = SCTP_CMD_CHUNK_ULP;
6262
6263 /* Think about partial delivery. */
6264 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6265
6266 /* Even if we don't accept this chunk there is
6267 * memory pressure.
6268 */
6269 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6270 }
6271
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006272 /* Spill over rwnd a little bit. Note: While allowed, this spill over
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 * seems a bit troublesome in that frag_point varies based on
6274 * PMTU. In cases, such as loopback, this might be a rather
6275 * large spill over.
6276 */
Daniel Borkmann362d5202014-04-14 21:45:17 +02006277 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
Neil Horman4d93df02007-08-15 16:07:44 -07006278 (datalen > asoc->rwnd + asoc->frag_point))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279
6280 /* If this is the next TSN, consider reneging to make
6281 * room. Note: Playing nice with a confused sender. A
6282 * malicious sender can still eat up all our buffer
6283 * space and in the future we may want to detect and
6284 * do more drastic reneging.
6285 */
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006286 if (sctp_tsnmap_has_gap(map) &&
6287 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006288 pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289 deliver = SCTP_CMD_RENEGE;
6290 } else {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006291 pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
6292 __func__, tsn, datalen, asoc->rwnd);
6293
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294 return SCTP_IERROR_IGNORE_TSN;
6295 }
6296 }
6297
6298 /*
Neil Horman4d93df02007-08-15 16:07:44 -07006299 * Also try to renege to limit our memory usage in the event that
6300 * we are under memory pressure
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006301 * If we can't renege, don't worry about it, the sk_rmem_schedule
Neil Horman4d93df02007-08-15 16:07:44 -07006302 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6303 * memory usage too much
6304 */
6305 if (*sk->sk_prot_creator->memory_pressure) {
6306 if (sctp_tsnmap_has_gap(map) &&
wangweidongf7010e62013-12-23 12:16:52 +08006307 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006308 pr_debug("%s: under pressure, reneging for tsn:%u\n",
6309 __func__, tsn);
Neil Horman4d93df02007-08-15 16:07:44 -07006310 deliver = SCTP_CMD_RENEGE;
6311 }
6312 }
6313
6314 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315 * Section 3.3.10.9 No User Data (9)
6316 *
6317 * Cause of error
6318 * ---------------
6319 * No User Data: This error cause is returned to the originator of a
6320 * DATA chunk if a received DATA chunk has no user data.
6321 */
6322 if (unlikely(0 == datalen)) {
6323 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6324 if (err) {
6325 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6326 SCTP_CHUNK(err));
6327 }
6328 /* We are going to ABORT, so we might as well stop
6329 * processing the rest of the chunks in the packet.
6330 */
wangweidongcb3f8372013-12-23 12:16:50 +08006331 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07006332 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6333 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08006335 SCTP_PERR(SCTP_ERROR_NO_DATA));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006336 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6337 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338 return SCTP_IERROR_NO_DATA;
6339 }
6340
Sridhar Samudrala9faa7302006-07-21 14:49:07 -07006341 chunk->data_accepted = 1;
6342
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6344 * if we renege and the chunk arrives again.
6345 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006346 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006347 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006348 if (chunk->asoc)
6349 chunk->asoc->stats.iuodchunks++;
6350 } else {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006351 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006352 if (chunk->asoc)
6353 chunk->asoc->stats.iodchunks++;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006354 ordered = 1;
6355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356
6357 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6358 *
6359 * If an endpoint receive a DATA chunk with an invalid stream
6360 * identifier, it shall acknowledge the reception of the DATA chunk
6361 * following the normal procedure, immediately send an ERROR chunk
6362 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6363 * and discard the DATA chunk.
6364 */
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006365 sid = ntohs(data_hdr->stream);
Xin Longcee360a2017-05-31 16:36:31 +08006366 if (sid >= asoc->stream.incnt) {
Vlad Yasevich3888e9e2008-07-08 02:28:39 -07006367 /* Mark tsn as received even though we drop it */
6368 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6369
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6371 &data_hdr->stream,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05006372 sizeof(data_hdr->stream),
6373 sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374 if (err)
6375 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6376 SCTP_CHUNK(err));
6377 return SCTP_IERROR_BAD_STREAM;
6378 }
6379
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006380 /* Check to see if the SSN is possible for this TSN.
6381 * The biggest gap we can record is 4K wide. Since SSNs wrap
6382 * at an unsigned short, there is no way that an SSN can
6383 * wrap and for a valid TSN. We can simply check if the current
6384 * SSN is smaller then the next expected one. If it is, it wrapped
6385 * and is invalid.
6386 */
6387 ssn = ntohs(data_hdr->ssn);
Xin Longcee360a2017-05-31 16:36:31 +08006388 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->stream, in, sid)))
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006389 return SCTP_IERROR_PROTO_VIOLATION;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006390
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391 /* Send the data up to the user. Note: Schedule the
6392 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6393 * chunk needs the updated rwnd.
6394 */
6395 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6396
6397 return SCTP_IERROR_NO_ERROR;
6398}