blob: e13c83f9a6eea8efbfb185fcaf668d418abae5b0 [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 Leitner509e7a32016-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 Leitner509e7a32016-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 Leitner509e7a32016-12-28 09:26:35 -0200174 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (unlikely(chunk_length < required_length))
Marcelo Ricardo Leitner509e7a32016-12-28 09:26:35 -0200176 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Marcelo Ricardo Leitner509e7a32016-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
Xin Long2a493212017-08-03 15:42:13 +0800531 enum sctp_error error = SCTP_ERROR_NO_RESOURCE;
Vlad Yasevich853f4b52008-01-20 06:10:46 -0800532
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. */
Xin Long38c00f72017-07-23 09:34:35 +08001091 if (!sctp_chunk_length_valid(chunk,
1092 sizeof(struct sctp_heartbeat_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001093 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 commands);
1095
1096 /* 8.3 The receiver of the HEARTBEAT should immediately
1097 * respond with a HEARTBEAT ACK that contains the Heartbeat
1098 * Information field copied from the received HEARTBEAT chunk.
1099 */
Xin Long4d2dcdf2017-07-23 09:34:34 +08001100 chunk->subh.hb_hdr = (struct sctp_heartbeathdr *)chunk->skb->data;
Xin Long3c918702017-06-30 11:52:16 +08001101 param_hdr = (struct sctp_paramhdr *)chunk->subh.hb_hdr;
Xin Long922dbc52017-06-30 11:52:13 +08001102 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(struct sctp_chunkhdr);
Thomas Graf06a31e22012-11-30 02:16:27 +00001103
1104 if (ntohs(param_hdr->length) > paylen)
1105 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
1106 param_hdr, commands);
1107
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001108 if (!pskb_pull(chunk->skb, paylen))
1109 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Thomas Graf06a31e22012-11-30 02:16:27 +00001111 reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (!reply)
1113 goto nomem;
1114
1115 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1116 return SCTP_DISPOSITION_CONSUME;
1117
1118nomem:
1119 return SCTP_DISPOSITION_NOMEM;
1120}
1121
1122/*
1123 * Process the returning HEARTBEAT ACK.
1124 *
1125 * Section: 8.3 Path Heartbeat
1126 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1127 * should clear the error counter of the destination transport
1128 * address to which the HEARTBEAT was sent, and mark the destination
1129 * transport address as active if it is not so marked. The endpoint may
1130 * optionally report to the upper layer when an inactive destination
1131 * address is marked as active due to the reception of the latest
1132 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1133 * clear the association overall error count as well (as defined
1134 * in section 8.1).
1135 *
1136 * The receiver of the HEARTBEAT ACK should also perform an RTT
1137 * measurement for that destination transport address using the time
1138 * value carried in the HEARTBEAT ACK chunk.
1139 *
1140 * Verification Tag: 8.5 Verification Tag [Normal verification]
1141 *
1142 * Inputs
1143 * (endpoint, asoc, chunk)
1144 *
1145 * Outputs
1146 * (asoc, reply_msg, msg_up, timers, counters)
1147 *
1148 * The return value is the disposition of the chunk.
1149 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001150sctp_disposition_t sctp_sf_backbeat_8_3(struct net *net,
1151 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 const struct sctp_association *asoc,
1153 const sctp_subtype_t type,
1154 void *arg,
1155 sctp_cmd_seq_t *commands)
1156{
1157 struct sctp_chunk *chunk = arg;
1158 union sctp_addr from_addr;
1159 struct sctp_transport *link;
1160 sctp_sender_hb_info_t *hbinfo;
1161 unsigned long max_interval;
1162
1163 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001164 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
Xin Long922dbc52017-06-30 11:52:13 +08001167 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr) +
Wei Yongjundadb50c2009-08-22 11:27:37 +08001168 sizeof(sctp_sender_hb_info_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001169 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 commands);
1171
1172 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
Vladislav Yasevicha6012662006-05-19 14:25:53 -07001173 /* Make sure that the length of the parameter is what we expect */
1174 if (ntohs(hbinfo->param_hdr.length) !=
1175 sizeof(sctp_sender_hb_info_t)) {
1176 return SCTP_DISPOSITION_DISCARD;
1177 }
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 from_addr = hbinfo->daddr;
Al Viro63de08f2006-11-20 17:07:25 -08001180 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 /* This should never happen, but lets log it if so. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001183 if (unlikely(!link)) {
1184 if (from_addr.sa.sa_family == AF_INET6) {
Joe Perchese87cc472012-05-13 21:56:26 +00001185 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1186 __func__,
1187 asoc,
1188 &from_addr.v6.sin6_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001189 } else {
Joe Perchese87cc472012-05-13 21:56:26 +00001190 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1191 __func__,
1192 asoc,
1193 &from_addr.v4.sin_addr.s_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 return SCTP_DISPOSITION_DISCARD;
1196 }
1197
Sridhar Samudralaad8fec12006-07-21 14:48:50 -07001198 /* Validate the 64-bit random nonce. */
1199 if (hbinfo->hb_nonce != link->hb_nonce)
1200 return SCTP_DISPOSITION_DISCARD;
1201
Frank Filz52ccb8e2005-12-22 11:36:46 -08001202 max_interval = link->hbinterval + link->rto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 /* Check if the timestamp looks valid. */
1205 if (time_after(hbinfo->sent_at, jiffies) ||
1206 time_after(jiffies, hbinfo->sent_at + max_interval)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001207 pr_debug("%s: HEARTBEAT ACK with invalid timestamp received "
1208 "for transport:%p\n", __func__, link);
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return SCTP_DISPOSITION_DISCARD;
1211 }
1212
1213 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1214 * the HEARTBEAT should clear the error counter of the
1215 * destination transport address to which the HEARTBEAT was
1216 * sent and mark the destination transport address as active if
1217 * it is not so marked.
1218 */
1219 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1220
1221 return SCTP_DISPOSITION_CONSUME;
1222}
1223
1224/* Helper function to send out an abort for the restart
1225 * condition.
1226 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001227static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 struct sctp_chunk *init,
1229 sctp_cmd_seq_t *commands)
1230{
1231 int len;
1232 struct sctp_packet *pkt;
1233 union sctp_addr_param *addrparm;
1234 struct sctp_errhdr *errhdr;
1235 struct sctp_endpoint *ep;
Xin Longd8238d92017-08-03 15:42:11 +08001236 char buffer[sizeof(*errhdr) + sizeof(*addrparm)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1238
1239 /* Build the error on the stack. We are way to malloc crazy
1240 * throughout the code today.
1241 */
1242 errhdr = (struct sctp_errhdr *)buffer;
1243 addrparm = (union sctp_addr_param *)errhdr->variable;
1244
1245 /* Copy into a parm format. */
1246 len = af->to_addr_param(ssa, addrparm);
Xin Longd8238d92017-08-03 15:42:11 +08001247 len += sizeof(*errhdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 errhdr->cause = SCTP_ERROR_RESTART;
1250 errhdr->length = htons(len);
1251
1252 /* Assign to the control socket. */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001253 ep = sctp_sk(net->sctp.ctl_sock)->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 /* Association is NULL since this may be a restart attack and we
1256 * want to send back the attacker's vtag.
1257 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001258 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 if (!pkt)
1261 goto out;
1262 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1263
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00001264 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 /* Discard the rest of the inbound packet. */
1267 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1268
1269out:
1270 /* Even if there is no memory, treat as a failure so
1271 * the packet will get dropped.
1272 */
1273 return 0;
1274}
1275
Joe Perches123031c2010-09-08 11:04:21 +00001276static bool list_has_sctp_addr(const struct list_head *list,
1277 union sctp_addr *ipaddr)
1278{
1279 struct sctp_transport *addr;
1280
1281 list_for_each_entry(addr, list, transports) {
1282 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1283 return true;
1284 }
1285
1286 return false;
1287}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288/* A restart is occurring, check to make sure no new addresses
1289 * are being added as we may be under a takeover attack.
1290 */
1291static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1292 const struct sctp_association *asoc,
1293 struct sctp_chunk *init,
1294 sctp_cmd_seq_t *commands)
1295{
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001296 struct net *net = sock_net(new_asoc->base.sk);
Joe Perches123031c2010-09-08 11:04:21 +00001297 struct sctp_transport *new_addr;
1298 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Joe Perches123031c2010-09-08 11:04:21 +00001300 /* Implementor's Guide - Section 5.2.2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 * ...
1302 * Before responding the endpoint MUST check to see if the
1303 * unexpected INIT adds new addresses to the association. If new
1304 * addresses are added to the association, the endpoint MUST respond
1305 * with an ABORT..
1306 */
1307
1308 /* Search through all current addresses and make sure
1309 * we aren't adding any new ones.
1310 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001311 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
Joe Perches123031c2010-09-08 11:04:21 +00001312 transports) {
1313 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1314 &new_addr->ipaddr)) {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001315 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
Joe Perches123031c2010-09-08 11:04:21 +00001316 commands);
1317 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 break;
Joe Perches123031c2010-09-08 11:04:21 +00001319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 }
1321
1322 /* Return success if all addresses were found. */
Joe Perches123031c2010-09-08 11:04:21 +00001323 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324}
1325
1326/* Populate the verification/tie tags based on overlapping INIT
1327 * scenario.
1328 *
1329 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1330 */
1331static void sctp_tietags_populate(struct sctp_association *new_asoc,
1332 const struct sctp_association *asoc)
1333{
1334 switch (asoc->state) {
1335
1336 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1337
1338 case SCTP_STATE_COOKIE_WAIT:
1339 new_asoc->c.my_vtag = asoc->c.my_vtag;
1340 new_asoc->c.my_ttag = asoc->c.my_vtag;
1341 new_asoc->c.peer_ttag = 0;
1342 break;
1343
1344 case SCTP_STATE_COOKIE_ECHOED:
1345 new_asoc->c.my_vtag = asoc->c.my_vtag;
1346 new_asoc->c.my_ttag = asoc->c.my_vtag;
1347 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1348 break;
1349
1350 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1351 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1352 */
1353 default:
1354 new_asoc->c.my_ttag = asoc->c.my_vtag;
1355 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1356 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 /* Other parameters for the endpoint SHOULD be copied from the
1360 * existing parameters of the association (e.g. number of
1361 * outbound streams) into the INIT ACK and cookie.
1362 */
1363 new_asoc->rwnd = asoc->rwnd;
1364 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1365 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1366 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1367}
1368
1369/*
1370 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1371 * handling action.
1372 *
1373 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1374 *
1375 * Returns value representing action to be taken. These action values
1376 * correspond to Action/Description values in RFC 2960, Table 2.
1377 */
1378static char sctp_tietags_compare(struct sctp_association *new_asoc,
1379 const struct sctp_association *asoc)
1380{
1381 /* In this case, the peer may have restarted. */
1382 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1383 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1384 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1385 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1386 return 'A';
1387
1388 /* Collision case B. */
1389 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1390 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1391 (0 == asoc->c.peer_vtag))) {
1392 return 'B';
1393 }
1394
1395 /* Collision case D. */
1396 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1397 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1398 return 'D';
1399
1400 /* Collision case C. */
1401 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1402 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1403 (0 == new_asoc->c.my_ttag) &&
1404 (0 == new_asoc->c.peer_ttag))
1405 return 'C';
1406
1407 /* No match to any of the special cases; discard this packet. */
1408 return 'E';
1409}
1410
1411/* Common helper routine for both duplicate and simulataneous INIT
1412 * chunk handling.
1413 */
1414static sctp_disposition_t sctp_sf_do_unexpected_init(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001415 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 const struct sctp_endpoint *ep,
1417 const struct sctp_association *asoc,
1418 const sctp_subtype_t type,
1419 void *arg, sctp_cmd_seq_t *commands)
1420{
Xin Long62e6b7e2017-07-23 09:34:28 +08001421 struct sctp_chunk *chunk = arg, *repl, *err_chunk;
1422 struct sctp_unrecognized_param *unk_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 struct sctp_association *new_asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 struct sctp_packet *packet;
Xin Long62e6b7e2017-07-23 09:34:28 +08001425 sctp_disposition_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 int len;
1427
1428 /* 6.10 Bundling
1429 * An endpoint MUST NOT bundle INIT, INIT ACK or
1430 * SHUTDOWN COMPLETE with any other chunks.
1431 *
1432 * IG Section 2.11.2
1433 * Furthermore, we require that the receiver of an INIT chunk MUST
1434 * enforce these rules by silently discarding an arriving packet
1435 * with an INIT chunk that is bundled with other chunks.
1436 */
1437 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001438 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001441 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 */
1443 if (chunk->sctp_hdr->vtag != 0)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001444 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 /* Make sure that the INIT chunk has a valid length.
1447 * In this case, we generate a protocol violation since we have
1448 * an association established.
1449 */
Xin Long01a992b2017-06-30 11:52:22 +08001450 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001451 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 commands);
1453 /* Grab the INIT header. */
Xin Long4ae70c02017-06-30 11:52:21 +08001454 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 /* Tag the variable length parameters. */
Xin Long4ae70c02017-06-30 11:52:21 +08001457 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 /* Verify the INIT chunk before processing it. */
1460 err_chunk = NULL;
Vlad Yasevichb14878c2014-04-17 17:26:50 +02001461 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Xin Long01a992b2017-06-30 11:52:22 +08001462 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 &err_chunk)) {
1464 /* This chunk contains fatal error. It is to be discarded.
1465 * Send an ABORT, with causes if there is any.
1466 */
1467 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001468 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 (__u8 *)(err_chunk->chunk_hdr) +
Xin Long922dbc52017-06-30 11:52:13 +08001470 sizeof(struct sctp_chunkhdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 ntohs(err_chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +08001472 sizeof(struct sctp_chunkhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 if (packet) {
1475 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1476 SCTP_PACKET(packet));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001477 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 retval = SCTP_DISPOSITION_CONSUME;
1479 } else {
1480 retval = SCTP_DISPOSITION_NOMEM;
1481 }
1482 goto cleanup;
1483 } else {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001484 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 commands);
1486 }
1487 }
1488
1489 /*
1490 * Other parameters for the endpoint SHOULD be copied from the
1491 * existing parameters of the association (e.g. number of
1492 * outbound streams) into the INIT ACK and cookie.
1493 * FIXME: We are copying parameters from the endpoint not the
1494 * association.
1495 */
1496 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1497 if (!new_asoc)
1498 goto nomem;
1499
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001500 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1501 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1502 goto nomem;
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 /* In the outbound INIT ACK the endpoint MUST copy its current
1505 * Verification Tag and Peers Verification tag into a reserved
1506 * place (local tie-tag and per tie-tag) within the state cookie.
1507 */
Wei Yongjunde6becdc02011-04-19 21:30:51 +00001508 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Xin Long01a992b2017-06-30 11:52:22 +08001509 (struct sctp_init_chunk *)chunk->chunk_hdr,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001510 GFP_ATOMIC))
1511 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 /* Make sure no new addresses are being added during the
1514 * restart. Do not do this check for COOKIE-WAIT state,
1515 * since there are no peer addresses to check against.
1516 * Upon return an ABORT will have been sent if needed.
1517 */
1518 if (!sctp_state(asoc, COOKIE_WAIT)) {
1519 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1520 commands)) {
1521 retval = SCTP_DISPOSITION_CONSUME;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001522 goto nomem_retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
1524 }
1525
1526 sctp_tietags_populate(new_asoc, asoc);
1527
1528 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1529
1530 /* If there are errors need to be reported for unknown parameters,
1531 * make sure to reserve enough room in the INIT ACK for them.
1532 */
1533 len = 0;
1534 if (err_chunk) {
1535 len = ntohs(err_chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +08001536 sizeof(struct sctp_chunkhdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1540 if (!repl)
1541 goto nomem;
1542
1543 /* If there are errors need to be reported for unknown parameters,
1544 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1545 * parameter.
1546 */
1547 if (err_chunk) {
1548 /* Get the "Unrecognized parameter" parameter(s) out of the
1549 * ERROR chunk generated by sctp_verify_init(). Since the
1550 * error cause code for "unknown parameter" and the
1551 * "Unrecognized parameter" type is the same, we can
1552 * construct the parameters in INIT ACK by copying the
1553 * ERROR causes over.
1554 */
Xin Long62e6b7e2017-07-23 09:34:28 +08001555 unk_param = (struct sctp_unrecognized_param *)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 ((__u8 *)(err_chunk->chunk_hdr) +
Xin Long922dbc52017-06-30 11:52:13 +08001557 sizeof(struct sctp_chunkhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 /* Replace the cause code with the "Unrecognized parameter"
1559 * parameter type.
1560 */
1561 sctp_addto_chunk(repl, len, unk_param);
1562 }
1563
1564 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1565 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1566
1567 /*
1568 * Note: After sending out INIT ACK with the State Cookie parameter,
1569 * "Z" MUST NOT allocate any resources for this new association.
1570 * Otherwise, "Z" will be vulnerable to resource attacks.
1571 */
1572 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1573 retval = SCTP_DISPOSITION_CONSUME;
1574
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001575 return retval;
1576
1577nomem:
1578 retval = SCTP_DISPOSITION_NOMEM;
1579nomem_retval:
1580 if (new_asoc)
1581 sctp_association_free(new_asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582cleanup:
1583 if (err_chunk)
1584 sctp_chunk_free(err_chunk);
1585 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586}
1587
1588/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001589 * Handle simultaneous INIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 * This means we started an INIT and then we got an INIT request from
1591 * our peer.
1592 *
1593 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1594 * This usually indicates an initialization collision, i.e., each
1595 * endpoint is attempting, at about the same time, to establish an
1596 * association with the other endpoint.
1597 *
1598 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1599 * endpoint MUST respond with an INIT ACK using the same parameters it
1600 * sent in its original INIT chunk (including its Verification Tag,
1601 * unchanged). These original parameters are combined with those from the
1602 * newly received INIT chunk. The endpoint shall also generate a State
1603 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1604 * INIT to calculate the State Cookie.
1605 *
1606 * After that, the endpoint MUST NOT change its state, the T1-init
1607 * timer shall be left running and the corresponding TCB MUST NOT be
1608 * destroyed. The normal procedures for handling State Cookies when
1609 * a TCB exists will resolve the duplicate INITs to a single association.
1610 *
1611 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1612 * its Tie-Tags with the Tag information of itself and its peer (see
1613 * section 5.2.2 for a description of the Tie-Tags).
1614 *
1615 * Verification Tag: Not explicit, but an INIT can not have a valid
1616 * verification tag, so we skip the check.
1617 *
1618 * Inputs
1619 * (endpoint, asoc, chunk)
1620 *
1621 * Outputs
1622 * (asoc, reply_msg, msg_up, timers, counters)
1623 *
1624 * The return value is the disposition of the chunk.
1625 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001626sctp_disposition_t sctp_sf_do_5_2_1_siminit(struct net *net,
1627 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 const struct sctp_association *asoc,
1629 const sctp_subtype_t type,
1630 void *arg,
1631 sctp_cmd_seq_t *commands)
1632{
1633 /* Call helper to do the real work for both simulataneous and
1634 * duplicate INIT chunk handling.
1635 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001636 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637}
1638
1639/*
1640 * Handle duplicated INIT messages. These are usually delayed
1641 * restransmissions.
1642 *
1643 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1644 * COOKIE-ECHOED and COOKIE-WAIT
1645 *
1646 * Unless otherwise stated, upon reception of an unexpected INIT for
1647 * this association, the endpoint shall generate an INIT ACK with a
1648 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1649 * current Verification Tag and peer's Verification Tag into a reserved
1650 * place within the state cookie. We shall refer to these locations as
1651 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1652 * containing this INIT ACK MUST carry a Verification Tag value equal to
1653 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1654 * MUST contain a new Initiation Tag (randomly generated see Section
1655 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1656 * existing parameters of the association (e.g. number of outbound
1657 * streams) into the INIT ACK and cookie.
1658 *
1659 * After sending out the INIT ACK, the endpoint shall take no further
1660 * actions, i.e., the existing association, including its current state,
1661 * and the corresponding TCB MUST NOT be changed.
1662 *
1663 * Note: Only when a TCB exists and the association is not in a COOKIE-
1664 * WAIT state are the Tie-Tags populated. For a normal association INIT
1665 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1666 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1667 * State Cookie are populated as specified in section 5.2.1.
1668 *
1669 * Verification Tag: Not specified, but an INIT has no way of knowing
1670 * what the verification tag could be, so we ignore it.
1671 *
1672 * Inputs
1673 * (endpoint, asoc, chunk)
1674 *
1675 * Outputs
1676 * (asoc, reply_msg, msg_up, timers, counters)
1677 *
1678 * The return value is the disposition of the chunk.
1679 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001680sctp_disposition_t sctp_sf_do_5_2_2_dupinit(struct net *net,
1681 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 const struct sctp_association *asoc,
1683 const sctp_subtype_t type,
1684 void *arg,
1685 sctp_cmd_seq_t *commands)
1686{
1687 /* Call helper to do the real work for both simulataneous and
1688 * duplicate INIT chunk handling.
1689 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001690 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691}
1692
1693
Vlad Yasevich610ab732007-01-15 19:18:30 -08001694/*
1695 * Unexpected INIT-ACK handler.
1696 *
1697 * Section 5.2.3
1698 * If an INIT ACK received by an endpoint in any state other than the
1699 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1700 * An unexpected INIT ACK usually indicates the processing of an old or
1701 * duplicated INIT chunk.
1702*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001703sctp_disposition_t sctp_sf_do_5_2_3_initack(struct net *net,
1704 const struct sctp_endpoint *ep,
Vlad Yasevich610ab732007-01-15 19:18:30 -08001705 const struct sctp_association *asoc,
1706 const sctp_subtype_t type,
1707 void *arg, sctp_cmd_seq_t *commands)
1708{
1709 /* Per the above section, we'll discard the chunk if we have an
1710 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1711 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001712 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001713 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001714 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001715 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001716}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718/* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1719 *
1720 * Section 5.2.4
1721 * A) In this case, the peer may have restarted.
1722 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001723static sctp_disposition_t sctp_sf_do_dupcook_a(struct net *net,
1724 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 const struct sctp_association *asoc,
1726 struct sctp_chunk *chunk,
1727 sctp_cmd_seq_t *commands,
1728 struct sctp_association *new_asoc)
1729{
Xin Long01a992b2017-06-30 11:52:22 +08001730 struct sctp_init_chunk *peer_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 struct sctp_ulpevent *ev;
1732 struct sctp_chunk *repl;
1733 struct sctp_chunk *err;
1734 sctp_disposition_t disposition;
1735
1736 /* new_asoc is a brand-new association, so these are not yet
1737 * side effects--it is safe to run them here.
1738 */
1739 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1740
Wei Yongjunde6becdc02011-04-19 21:30:51 +00001741 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 GFP_ATOMIC))
1743 goto nomem;
1744
1745 /* Make sure no new addresses are being added during the
1746 * restart. Though this is a pretty complicated attack
1747 * since you'd have to get inside the cookie.
1748 */
1749 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1750 return SCTP_DISPOSITION_CONSUME;
1751 }
1752
1753 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1754 * the peer has restarted (Action A), it MUST NOT setup a new
1755 * association but instead resend the SHUTDOWN ACK and send an ERROR
1756 * chunk with a "Cookie Received while Shutting Down" error cause to
1757 * its peer.
1758 */
1759 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001760 disposition = sctp_sf_do_9_2_reshutack(net, ep, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1762 chunk, commands);
1763 if (SCTP_DISPOSITION_NOMEM == disposition)
1764 goto nomem;
1765
1766 err = sctp_make_op_error(asoc, chunk,
1767 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001768 NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 if (err)
1770 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1771 SCTP_CHUNK(err));
1772
1773 return SCTP_DISPOSITION_CONSUME;
1774 }
1775
Wei Yongjuna000c012011-05-29 23:23:36 +00001776 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1777 * data. Consider the optional choice of resending of this data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 */
Wei Yongjuna000c012011-05-29 23:23:36 +00001779 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1780 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1781 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1783
Wei Yongjuna000c012011-05-29 23:23:36 +00001784 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1785 * and ASCONF-ACK cache.
1786 */
1787 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1788 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1789 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 repl = sctp_make_cookie_ack(new_asoc, chunk);
1792 if (!repl)
1793 goto nomem;
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 /* Report association restart to upper layer. */
1796 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1797 new_asoc->c.sinit_num_ostreams,
1798 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07001799 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 if (!ev)
1801 goto nomem_ev;
1802
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001803 /* Update the content of current association. */
1804 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
Vlad Yasevichbdf6fa52014-10-03 18:16:20 -04001806 if (sctp_state(asoc, SHUTDOWN_PENDING) &&
1807 (sctp_sstate(asoc->base.sk, CLOSING) ||
1808 sock_flag(asoc->base.sk, SOCK_DEAD))) {
1809 /* if were currently in SHUTDOWN_PENDING, but the socket
1810 * has been closed by user, don't transition to ESTABLISHED.
1811 * Instead trigger SHUTDOWN bundled with COOKIE_ACK.
1812 */
1813 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1814 return sctp_sf_do_9_2_start_shutdown(net, ep, asoc,
1815 SCTP_ST_CHUNK(0), NULL,
1816 commands);
1817 } else {
1818 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1819 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1820 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 return SCTP_DISPOSITION_CONSUME;
1823
1824nomem_ev:
1825 sctp_chunk_free(repl);
1826nomem:
1827 return SCTP_DISPOSITION_NOMEM;
1828}
1829
1830/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1831 *
1832 * Section 5.2.4
1833 * B) In this case, both sides may be attempting to start an association
1834 * at about the same time but the peer endpoint started its INIT
1835 * after responding to the local endpoint's INIT
1836 */
1837/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001838static sctp_disposition_t sctp_sf_do_dupcook_b(struct net *net,
1839 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 const struct sctp_association *asoc,
1841 struct sctp_chunk *chunk,
1842 sctp_cmd_seq_t *commands,
1843 struct sctp_association *new_asoc)
1844{
Xin Long01a992b2017-06-30 11:52:22 +08001845 struct sctp_init_chunk *peer_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 struct sctp_chunk *repl;
1847
1848 /* new_asoc is a brand-new association, so these are not yet
1849 * side effects--it is safe to run them here.
1850 */
1851 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
Wei Yongjunde6becdc02011-04-19 21:30:51 +00001852 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 GFP_ATOMIC))
1854 goto nomem;
1855
1856 /* Update the content of current association. */
1857 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1858 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1859 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001860 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1862
1863 repl = sctp_make_cookie_ack(new_asoc, chunk);
1864 if (!repl)
1865 goto nomem;
1866
1867 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 /* RFC 2960 5.1 Normal Establishment of an Association
1870 *
1871 * D) IMPLEMENTATION NOTE: An implementation may choose to
1872 * send the Communication Up notification to the SCTP user
1873 * upon reception of a valid COOKIE ECHO chunk.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001874 *
1875 * Sadly, this needs to be implemented as a side-effect, because
1876 * we are not guaranteed to have set the association id of the real
1877 * association and so these notifications need to be delayed until
1878 * the association id is allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Vlad Yasevich07d93962007-05-04 13:55:27 -07001881 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
1883 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001884 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001886 * peers requested adaptation layer.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001887 *
1888 * This also needs to be done as a side effect for the same reason as
1889 * above.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 */
Vlad Yasevich07d93962007-05-04 13:55:27 -07001891 if (asoc->peer.adaptation_ind)
1892 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
1894 return SCTP_DISPOSITION_CONSUME;
1895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896nomem:
1897 return SCTP_DISPOSITION_NOMEM;
1898}
1899
1900/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1901 *
1902 * Section 5.2.4
1903 * C) In this case, the local endpoint's cookie has arrived late.
1904 * Before it arrived, the local endpoint sent an INIT and received an
1905 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1906 * but a new tag of its own.
1907 */
1908/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001909static sctp_disposition_t sctp_sf_do_dupcook_c(struct net *net,
1910 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 const struct sctp_association *asoc,
1912 struct sctp_chunk *chunk,
1913 sctp_cmd_seq_t *commands,
1914 struct sctp_association *new_asoc)
1915{
1916 /* The cookie should be silently discarded.
1917 * The endpoint SHOULD NOT change states and should leave
1918 * any timers running.
1919 */
1920 return SCTP_DISPOSITION_DISCARD;
1921}
1922
1923/* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1924 *
1925 * Section 5.2.4
1926 *
1927 * D) When both local and remote tags match the endpoint should always
1928 * enter the ESTABLISHED state, if it has not already done so.
1929 */
1930/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001931static sctp_disposition_t sctp_sf_do_dupcook_d(struct net *net,
1932 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 const struct sctp_association *asoc,
1934 struct sctp_chunk *chunk,
1935 sctp_cmd_seq_t *commands,
1936 struct sctp_association *new_asoc)
1937{
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001938 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 struct sctp_chunk *repl;
1940
1941 /* Clarification from Implementor's Guide:
1942 * D) When both local and remote tags match the endpoint should
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001943 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1944 * It should stop any cookie timer that may be running and send
1945 * a COOKIE ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 */
1947
1948 /* Don't accidentally move back into established state. */
1949 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1950 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1951 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1952 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1953 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001954 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1956 SCTP_NULL());
1957
1958 /* RFC 2960 5.1 Normal Establishment of an Association
1959 *
1960 * D) IMPLEMENTATION NOTE: An implementation may choose
1961 * to send the Communication Up notification to the
1962 * SCTP user upon reception of a valid COOKIE
1963 * ECHO chunk.
1964 */
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001965 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 SCTP_COMM_UP, 0,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001967 asoc->c.sinit_num_ostreams,
1968 asoc->c.sinit_max_instreams,
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09001969 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (!ev)
1971 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001974 * When a peer sends a Adaptation Layer Indication parameter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 * SCTP delivers this notification to inform the application
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001976 * that of the peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001978 if (asoc->peer.adaptation_ind) {
1979 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001981 if (!ai_ev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 goto nomem;
1983
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 }
1985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987 repl = sctp_make_cookie_ack(new_asoc, chunk);
1988 if (!repl)
1989 goto nomem;
1990
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07001991 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1992
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001993 if (ev)
1994 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1995 SCTP_ULPEVENT(ev));
1996 if (ai_ev)
1997 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1998 SCTP_ULPEVENT(ai_ev));
1999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 return SCTP_DISPOSITION_CONSUME;
2001
2002nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07002003 if (ai_ev)
2004 sctp_ulpevent_free(ai_ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (ev)
2006 sctp_ulpevent_free(ev);
2007 return SCTP_DISPOSITION_NOMEM;
2008}
2009
2010/*
2011 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
2012 * chunk was retransmitted and then delayed in the network.
2013 *
2014 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
2015 *
2016 * Verification Tag: None. Do cookie validation.
2017 *
2018 * Inputs
2019 * (endpoint, asoc, chunk)
2020 *
2021 * Outputs
2022 * (asoc, reply_msg, msg_up, timers, counters)
2023 *
2024 * The return value is the disposition of the chunk.
2025 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002026sctp_disposition_t sctp_sf_do_5_2_4_dupcook(struct net *net,
2027 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 const struct sctp_association *asoc,
2029 const sctp_subtype_t type,
2030 void *arg,
2031 sctp_cmd_seq_t *commands)
2032{
2033 sctp_disposition_t retval;
2034 struct sctp_chunk *chunk = arg;
2035 struct sctp_association *new_asoc;
2036 int error = 0;
2037 char action;
2038 struct sctp_chunk *err_chk_p;
2039
2040 /* Make sure that the chunk has a valid length from the protocol
2041 * perspective. In this case check to make sure we have at least
2042 * enough for the chunk header. Cookie length verification is
2043 * done later.
2044 */
Xin Long922dbc52017-06-30 11:52:13 +08002045 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002046 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 commands);
2048
2049 /* "Decode" the chunk. We have no optional parameters so we
2050 * are in good shape.
2051 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002052 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -07002053 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +08002054 sizeof(struct sctp_chunkhdr)))
Sridhar Samudrala62b08082006-05-05 17:04:43 -07002055 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
2057 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2058 * of a duplicate COOKIE ECHO match the Verification Tags of the
2059 * current association, consider the State Cookie valid even if
2060 * the lifespan is exceeded.
2061 */
2062 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2063 &err_chk_p);
2064
2065 /* FIXME:
2066 * If the re-build failed, what is the proper error path
2067 * from here?
2068 *
2069 * [We should abort the association. --piggy]
2070 */
2071 if (!new_asoc) {
2072 /* FIXME: Several errors are possible. A bad cookie should
2073 * be silently discarded, but think about logging it too.
2074 */
2075 switch (error) {
2076 case -SCTP_IERROR_NOMEM:
2077 goto nomem;
2078
2079 case -SCTP_IERROR_STALE_COOKIE:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002080 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 err_chk_p);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002082 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 case -SCTP_IERROR_BAD_SIG:
2084 default:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002085 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 }
2088
Xin Long7e062972017-05-23 13:28:55 +08002089 /* Set temp so that it won't be added into hashtable */
2090 new_asoc->temp = 1;
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 /* Compare the tie_tag in cookie with the verification tag of
2093 * current association.
2094 */
2095 action = sctp_tietags_compare(new_asoc, asoc);
2096
2097 switch (action) {
2098 case 'A': /* Association restart. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002099 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 new_asoc);
2101 break;
2102
2103 case 'B': /* Collision case B. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002104 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 new_asoc);
2106 break;
2107
2108 case 'C': /* Collision case C. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002109 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 new_asoc);
2111 break;
2112
2113 case 'D': /* Collision case D. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002114 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 new_asoc);
2116 break;
2117
2118 default: /* Discard packet for all others. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002119 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
2123 /* Delete the tempory new association. */
Vlad Yasevichf2815632013-03-12 15:53:23 +00002124 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2126
Max Matveevd5ccd492011-08-29 21:02:24 +00002127 /* Restore association pointer to provide SCTP command interpeter
2128 * with a valid context in case it needs to manipulate
2129 * the queues */
2130 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2131 SCTP_ASOC((struct sctp_association *)asoc));
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 return retval;
2134
2135nomem:
2136 return SCTP_DISPOSITION_NOMEM;
2137}
2138
2139/*
2140 * Process an ABORT. (SHUTDOWN-PENDING state)
2141 *
2142 * See sctp_sf_do_9_1_abort().
2143 */
2144sctp_disposition_t sctp_sf_shutdown_pending_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002145 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 const struct sctp_endpoint *ep,
2147 const struct sctp_association *asoc,
2148 const sctp_subtype_t type,
2149 void *arg,
2150 sctp_cmd_seq_t *commands)
2151{
2152 struct sctp_chunk *chunk = arg;
2153
2154 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002155 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157 /* Make sure that the ABORT chunk has a valid length.
2158 * Since this is an ABORT chunk, we have to discard it
2159 * because of the following text:
2160 * RFC 2960, Section 3.3.7
2161 * If an endpoint receives an ABORT with a format error or for an
2162 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002163 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 * as we do not know its true length. So, to be safe, discard the
2165 * packet.
2166 */
Xin Long441ae652017-07-23 09:34:36 +08002167 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002168 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Vlad Yasevich75205f42007-12-20 14:12:59 -08002170 /* ADD-IP: Special case for ABORT chunks
2171 * F4) One special consideration is that ABORT Chunks arriving
2172 * destined to the IP address being deleted MUST be
2173 * ignored (see Section 5.3.1 for further details).
2174 */
2175 if (SCTP_ADDR_DEL ==
2176 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002177 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002178
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002179 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
2182/*
2183 * Process an ABORT. (SHUTDOWN-SENT state)
2184 *
2185 * See sctp_sf_do_9_1_abort().
2186 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002187sctp_disposition_t sctp_sf_shutdown_sent_abort(struct net *net,
2188 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 const struct sctp_association *asoc,
2190 const sctp_subtype_t type,
2191 void *arg,
2192 sctp_cmd_seq_t *commands)
2193{
2194 struct sctp_chunk *chunk = arg;
2195
2196 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002197 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
2199 /* Make sure that the ABORT chunk has a valid length.
2200 * Since this is an ABORT chunk, we have to discard it
2201 * because of the following text:
2202 * RFC 2960, Section 3.3.7
2203 * If an endpoint receives an ABORT with a format error or for an
2204 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002205 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 * as we do not know its true length. So, to be safe, discard the
2207 * packet.
2208 */
Xin Long441ae652017-07-23 09:34:36 +08002209 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002210 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Vlad Yasevich75205f42007-12-20 14:12:59 -08002212 /* ADD-IP: Special case for ABORT chunks
2213 * F4) One special consideration is that ABORT Chunks arriving
2214 * destined to the IP address being deleted MUST be
2215 * ignored (see Section 5.3.1 for further details).
2216 */
2217 if (SCTP_ADDR_DEL ==
2218 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002219 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002220
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 /* Stop the T2-shutdown timer. */
2222 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2223 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2224
2225 /* Stop the T5-shutdown guard timer. */
2226 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2227 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2228
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002229 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230}
2231
2232/*
2233 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2234 *
2235 * See sctp_sf_do_9_1_abort().
2236 */
2237sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002238 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 const struct sctp_endpoint *ep,
2240 const struct sctp_association *asoc,
2241 const sctp_subtype_t type,
2242 void *arg,
2243 sctp_cmd_seq_t *commands)
2244{
2245 /* The same T2 timer, so we should be able to use
2246 * common function with the SHUTDOWN-SENT state.
2247 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002248 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249}
2250
2251/*
2252 * Handle an Error received in COOKIE_ECHOED state.
2253 *
2254 * Only handle the error type of stale COOKIE Error, the other errors will
2255 * be ignored.
2256 *
2257 * Inputs
2258 * (endpoint, asoc, chunk)
2259 *
2260 * Outputs
2261 * (asoc, reply_msg, msg_up, timers, counters)
2262 *
2263 * The return value is the disposition of the chunk.
2264 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002265sctp_disposition_t sctp_sf_cookie_echoed_err(struct net *net,
2266 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 const struct sctp_association *asoc,
2268 const sctp_subtype_t type,
2269 void *arg,
2270 sctp_cmd_seq_t *commands)
2271{
2272 struct sctp_chunk *chunk = arg;
Xin Longd8238d92017-08-03 15:42:11 +08002273 struct sctp_errhdr *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
2275 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002276 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 /* Make sure that the ERROR chunk has a valid length.
2279 * The parameter walking depends on this as well.
2280 */
Xin Long87caeba2017-08-03 15:42:12 +08002281 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_operr_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002282 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 commands);
2284
2285 /* Process the error here */
2286 /* FUTURE FIXME: When PR-SCTP related and other optional
2287 * parms are emitted, this will have to change to handle multiple
2288 * errors.
2289 */
2290 sctp_walk_errors(err, chunk->chunk_hdr) {
2291 if (SCTP_ERROR_STALE_COOKIE == err->cause)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002292 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 arg, commands);
2294 }
2295
2296 /* It is possible to have malformed error causes, and that
2297 * will cause us to end the walk early. However, since
2298 * we are discarding the packet, there should be no adverse
2299 * affects.
2300 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002301 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302}
2303
2304/*
2305 * Handle a Stale COOKIE Error
2306 *
2307 * Section: 5.2.6 Handle Stale COOKIE Error
2308 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2309 * one of the following three alternatives.
2310 * ...
2311 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2312 * Preservative parameter requesting an extension to the lifetime of
2313 * the State Cookie. When calculating the time extension, an
2314 * implementation SHOULD use the RTT information measured based on the
2315 * previous COOKIE ECHO / ERROR exchange, and should add no more
2316 * than 1 second beyond the measured RTT, due to long State Cookie
2317 * lifetimes making the endpoint more subject to a replay attack.
2318 *
2319 * Verification Tag: Not explicit, but safe to ignore.
2320 *
2321 * Inputs
2322 * (endpoint, asoc, chunk)
2323 *
2324 * Outputs
2325 * (asoc, reply_msg, msg_up, timers, counters)
2326 *
2327 * The return value is the disposition of the chunk.
2328 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002329static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
2330 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 const struct sctp_association *asoc,
2332 const sctp_subtype_t type,
2333 void *arg,
2334 sctp_cmd_seq_t *commands)
2335{
Frank Filz3f7a87d2005-06-20 13:14:57 -07002336 int attempts = asoc->init_err_counter + 1;
Xin Long365ddb62017-07-17 11:29:51 +08002337 struct sctp_chunk *chunk = arg, *reply;
2338 struct sctp_cookie_preserve_param bht;
2339 struct sctp_bind_addr *bp;
Xin Longd8238d92017-08-03 15:42:11 +08002340 struct sctp_errhdr *err;
Xin Long365ddb62017-07-17 11:29:51 +08002341 u32 stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Vlad Yasevich81845c22006-01-30 15:59:54 -08002343 if (attempts > asoc->max_init_attempts) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002344 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2345 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002347 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return SCTP_DISPOSITION_DELETE_TCB;
2349 }
2350
Xin Longd8238d92017-08-03 15:42:11 +08002351 err = (struct sctp_errhdr *)(chunk->skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
2353 /* When calculating the time extension, an implementation
2354 * SHOULD use the RTT information measured based on the
2355 * previous COOKIE ECHO / ERROR exchange, and should add no
2356 * more than 1 second beyond the measured RTT, due to long
2357 * State Cookie lifetimes making the endpoint more subject to
2358 * a replay attack.
2359 * Measure of Staleness's unit is usec. (1/1000000 sec)
2360 * Suggested Cookie Life-span Increment's unit is msec.
2361 * (1/1000 sec)
2362 * In general, if you use the suggested cookie life, the value
2363 * found in the field of measure of staleness should be doubled
2364 * to give ample time to retransmit the new cookie and thus
2365 * yield a higher probability of success on the reattempt.
2366 */
Xin Longd8238d92017-08-03 15:42:11 +08002367 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(*err)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 stale = (stale * 2) / 1000;
2369
2370 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2371 bht.param_hdr.length = htons(sizeof(bht));
2372 bht.lifespan_increment = htonl(stale);
2373
2374 /* Build that new INIT chunk. */
2375 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2376 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2377 if (!reply)
2378 goto nomem;
2379
2380 sctp_addto_chunk(reply, sizeof(bht), &bht);
2381
2382 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2383 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2384
2385 /* Stop pending T3-rtx and heartbeat timers */
2386 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2387 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2388
2389 /* Delete non-primary peer ip addresses since we are transitioning
2390 * back to the COOKIE-WAIT state
2391 */
2392 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2393
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002394 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2395 * resend
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 */
Vlad Yasevichb6157d82007-10-24 15:59:16 -04002397 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 SCTP_TRANSPORT(asoc->peer.primary_path));
2399
2400 /* Cast away the const modifier, as we want to just
2401 * rerun it through as a sideffect.
2402 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07002403 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2406 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2407 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2408 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2409 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2410 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2411
2412 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2413
2414 return SCTP_DISPOSITION_CONSUME;
2415
2416nomem:
2417 return SCTP_DISPOSITION_NOMEM;
2418}
2419
2420/*
2421 * Process an ABORT.
2422 *
2423 * Section: 9.1
2424 * After checking the Verification Tag, the receiving endpoint shall
2425 * remove the association from its record, and shall report the
2426 * termination to its upper layer.
2427 *
2428 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2429 * B) Rules for packet carrying ABORT:
2430 *
2431 * - The endpoint shall always fill in the Verification Tag field of the
2432 * outbound packet with the destination endpoint's tag value if it
2433 * is known.
2434 *
2435 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2436 * MUST follow the procedure described in Section 8.4.
2437 *
2438 * - The receiver MUST accept the packet if the Verification Tag
2439 * matches either its own tag, OR the tag of its peer. Otherwise, the
2440 * receiver MUST silently discard the packet and take no further
2441 * action.
2442 *
2443 * Inputs
2444 * (endpoint, asoc, chunk)
2445 *
2446 * Outputs
2447 * (asoc, reply_msg, msg_up, timers, counters)
2448 *
2449 * The return value is the disposition of the chunk.
2450 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002451sctp_disposition_t sctp_sf_do_9_1_abort(struct net *net,
2452 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 const struct sctp_association *asoc,
2454 const sctp_subtype_t type,
2455 void *arg,
2456 sctp_cmd_seq_t *commands)
2457{
2458 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002461 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
2463 /* Make sure that the ABORT chunk has a valid length.
2464 * Since this is an ABORT chunk, we have to discard it
2465 * because of the following text:
2466 * RFC 2960, Section 3.3.7
2467 * If an endpoint receives an ABORT with a format error or for an
2468 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002469 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 * as we do not know its true length. So, to be safe, discard the
2471 * packet.
2472 */
Xin Long441ae652017-07-23 09:34:36 +08002473 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002474 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Vlad Yasevich75205f42007-12-20 14:12:59 -08002476 /* ADD-IP: Special case for ABORT chunks
2477 * F4) One special consideration is that ABORT Chunks arriving
2478 * destined to the IP address being deleted MUST be
2479 * ignored (see Section 5.3.1 for further details).
2480 */
2481 if (SCTP_ADDR_DEL ==
2482 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002483 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002484
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002485 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002486}
2487
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002488static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
2489 const struct sctp_endpoint *ep,
Vlad Yasevich75205f42007-12-20 14:12:59 -08002490 const struct sctp_association *asoc,
2491 const sctp_subtype_t type,
2492 void *arg,
2493 sctp_cmd_seq_t *commands)
2494{
2495 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002496 unsigned int len;
Vlad Yasevich75205f42007-12-20 14:12:59 -08002497 __be16 error = SCTP_ERROR_NO_ERROR;
2498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 /* See if we have an error cause code in the chunk. */
2500 len = ntohs(chunk->chunk_hdr->length);
Shan Wei96ca4682011-04-19 21:26:26 +00002501 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr)) {
Xin Longd8238d92017-08-03 15:42:11 +08002502 struct sctp_errhdr *err;
Shan Wei96ca4682011-04-19 21:26:26 +00002503
Shan Wei96ca4682011-04-19 21:26:26 +00002504 sctp_walk_errors(err, chunk->chunk_hdr);
2505 if ((void *)err != (void *)chunk->chunk_end)
Xin Longd8238d92017-08-03 15:42:11 +08002506 return sctp_sf_pdiscard(net, ep, asoc, type, arg,
2507 commands);
Shan Wei96ca4682011-04-19 21:26:26 +00002508
Xin Longd8238d92017-08-03 15:42:11 +08002509 error = ((struct sctp_errhdr *)chunk->skb->data)->cause;
Shan Wei96ca4682011-04-19 21:26:26 +00002510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002512 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002513 /* ASSOC_FAILED will DELETE_TCB. */
Al Viro5be291f2006-11-20 17:01:06 -08002514 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00002515 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2516 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
2518 return SCTP_DISPOSITION_ABORT;
2519}
2520
2521/*
2522 * Process an ABORT. (COOKIE-WAIT state)
2523 *
2524 * See sctp_sf_do_9_1_abort() above.
2525 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002526sctp_disposition_t sctp_sf_cookie_wait_abort(struct net *net,
2527 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 const struct sctp_association *asoc,
2529 const sctp_subtype_t type,
2530 void *arg,
2531 sctp_cmd_seq_t *commands)
2532{
2533 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002534 unsigned int len;
Al Virof94c0192006-11-20 17:00:25 -08002535 __be16 error = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
2537 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002538 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
2540 /* Make sure that the ABORT chunk has a valid length.
2541 * Since this is an ABORT chunk, we have to discard it
2542 * because of the following text:
2543 * RFC 2960, Section 3.3.7
2544 * If an endpoint receives an ABORT with a format error or for an
2545 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002546 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 * as we do not know its true length. So, to be safe, discard the
2548 * packet.
2549 */
Xin Long441ae652017-07-23 09:34:36 +08002550 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002551 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
2553 /* See if we have an error cause code in the chunk. */
2554 len = ntohs(chunk->chunk_hdr->length);
2555 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
Xin Longd8238d92017-08-03 15:42:11 +08002556 error = ((struct sctp_errhdr *)chunk->skb->data)->cause;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002558 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002559 chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560}
2561
2562/*
2563 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2564 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002565sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(struct net *net,
2566 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 const struct sctp_association *asoc,
2568 const sctp_subtype_t type,
2569 void *arg,
2570 sctp_cmd_seq_t *commands)
2571{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002572 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002573 ENOPROTOOPT, asoc,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002574 (struct sctp_transport *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575}
2576
2577/*
2578 * Process an ABORT. (COOKIE-ECHOED state)
2579 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002580sctp_disposition_t sctp_sf_cookie_echoed_abort(struct net *net,
2581 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 const struct sctp_association *asoc,
2583 const sctp_subtype_t type,
2584 void *arg,
2585 sctp_cmd_seq_t *commands)
2586{
2587 /* There is a single T1 timer, so we should be able to use
2588 * common function with the COOKIE-WAIT state.
2589 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002590 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591}
2592
2593/*
2594 * Stop T1 timer and abort association with "INIT failed".
2595 *
2596 * This is common code called by several sctp_sf_*_abort() functions above.
2597 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002598static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
2599 sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -08002600 __be16 error, int sk_err,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002601 const struct sctp_association *asoc,
2602 struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
Daniel Borkmannbb333812013-06-28 19:49:40 +02002604 pr_debug("%s: ABORT received (INIT)\n", __func__);
2605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2607 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002608 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2610 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002611 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 /* CMD_INIT_FAILED will DELETE_TCB. */
2613 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002614 SCTP_PERR(error));
Daniel Borkmannbb333812013-06-28 19:49:40 +02002615
Frank Filz3f7a87d2005-06-20 13:14:57 -07002616 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617}
2618
2619/*
2620 * sctp_sf_do_9_2_shut
2621 *
2622 * Section: 9.2
2623 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2624 * - enter the SHUTDOWN-RECEIVED state,
2625 *
2626 * - stop accepting new data from its SCTP user
2627 *
2628 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2629 * that all its outstanding DATA chunks have been received by the
2630 * SHUTDOWN sender.
2631 *
2632 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2633 * send a SHUTDOWN in response to a ULP request. And should discard
2634 * subsequent SHUTDOWN chunks.
2635 *
2636 * If there are still outstanding DATA chunks left, the SHUTDOWN
2637 * receiver shall continue to follow normal data transmission
2638 * procedures defined in Section 6 until all outstanding DATA chunks
2639 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2640 * new data from its SCTP user.
2641 *
2642 * Verification Tag: 8.5 Verification Tag [Normal verification]
2643 *
2644 * Inputs
2645 * (endpoint, asoc, chunk)
2646 *
2647 * Outputs
2648 * (asoc, reply_msg, msg_up, timers, counters)
2649 *
2650 * The return value is the disposition of the chunk.
2651 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002652sctp_disposition_t sctp_sf_do_9_2_shutdown(struct net *net,
2653 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 const struct sctp_association *asoc,
2655 const sctp_subtype_t type,
2656 void *arg,
2657 sctp_cmd_seq_t *commands)
2658{
2659 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 sctp_disposition_t disposition;
Xin Longe61e4052017-08-03 15:42:09 +08002661 struct sctp_shutdownhdr *sdh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 struct sctp_ulpevent *ev;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002663 __u32 ctsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
2665 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002666 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
2668 /* Make sure that the SHUTDOWN chunk has a valid length. */
Xin Longac23e682017-08-03 15:42:10 +08002669 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002670 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 commands);
2672
2673 /* Convert the elaborate header. */
Xin Longe61e4052017-08-03 15:42:09 +08002674 sdh = (struct sctp_shutdownhdr *)chunk->skb->data;
2675 skb_pull(chunk->skb, sizeof(*sdh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 chunk->subh.shutdown_hdr = sdh;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002677 ctsn = ntohl(sdh->cum_tsn_ack);
2678
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002679 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02002680 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2681 asoc->ctsn_ack_point);
2682
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002683 return SCTP_DISPOSITION_DISCARD;
2684 }
2685
Wei Yongjundf10eec2008-10-23 01:00:21 -07002686 /* If Cumulative TSN Ack beyond the max tsn currently
2687 * send, terminating the association and respond to the
2688 * sender with an ABORT.
2689 */
2690 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002691 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002693 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2694 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2695 * inform the application that it should cease sending data.
2696 */
2697 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2698 if (!ev) {
2699 disposition = SCTP_DISPOSITION_NOMEM;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002700 goto out;
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002701 }
2702 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2703
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2705 * - enter the SHUTDOWN-RECEIVED state,
2706 * - stop accepting new data from its SCTP user
2707 *
2708 * [This is implicit in the new state.]
2709 */
2710 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2711 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2712 disposition = SCTP_DISPOSITION_CONSUME;
2713
2714 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002715 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 arg, commands);
2717 }
2718
2719 if (SCTP_DISPOSITION_NOMEM == disposition)
2720 goto out;
2721
2722 /* - verify, by checking the Cumulative TSN Ack field of the
2723 * chunk, that all its outstanding DATA chunks have been
2724 * received by the SHUTDOWN sender.
2725 */
2726 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
Al Viro2178eda2006-11-20 17:26:53 -08002727 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729out:
2730 return disposition;
2731}
2732
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002733/*
2734 * sctp_sf_do_9_2_shut_ctsn
2735 *
2736 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2737 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2738 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2739 * MUST be processed.
2740 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002741sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(struct net *net,
2742 const struct sctp_endpoint *ep,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002743 const struct sctp_association *asoc,
2744 const sctp_subtype_t type,
2745 void *arg,
2746 sctp_cmd_seq_t *commands)
2747{
2748 struct sctp_chunk *chunk = arg;
Xin Longe61e4052017-08-03 15:42:09 +08002749 struct sctp_shutdownhdr *sdh;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002750 __u32 ctsn;
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002751
2752 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002753 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002754
2755 /* Make sure that the SHUTDOWN chunk has a valid length. */
Xin Longac23e682017-08-03 15:42:10 +08002756 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
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
Xin Longe61e4052017-08-03 15:42:09 +08002760 sdh = (struct sctp_shutdownhdr *)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{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 struct sctp_chunk *chunk = arg;
Xin Long65f77102017-08-03 15:42:16 +08002866 struct sctp_cwrhdr *cwr;
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
Xin Longb515fd22017-08-03 15:42:15 +08002872 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
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
Xin Long65f77102017-08-03 15:42:16 +08002876 cwr = (struct sctp_cwrhdr *)chunk->skb->data;
2877 skb_pull(chunk->skb, sizeof(*cwr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
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{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 struct sctp_chunk *chunk = arg;
Xin Long1fb6d832017-08-03 15:42:14 +08002922 struct sctp_ecnehdr *ecne;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
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
Xin Longb515fd22017-08-03 15:42:15 +08002927 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
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
Xin Long1fb6d832017-08-03 15:42:14 +08002931 ecne = (struct sctp_ecnehdr *)chunk->skb->data;
2932 skb_pull(chunk->skb, sizeof(*ecne));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
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;
Xin Longd8238d92017-08-03 15:42:11 +08003314 struct sctp_errhdr *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. */
Xin Long87caeba2017-08-03 15:42:12 +08003320 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_operr_chunk)))
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;
Xin Longd8238d92017-08-03 15:42:11 +08003437 struct sctp_errhdr *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,
Xin Longd8238d92017-08-03 15:42:11 +08003780 sizeof(struct sctp_errhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 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,
Xin Longd8238d92017-08-03 15:42:11 +08003816 sizeof(struct sctp_errhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 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))
Xin Longac23e682017-08-03 15:42:10 +08005421 return sctp_sf_pdiscard(net, ep, asoc, type, arg,
5422 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423
5424 /* Make sure that the SHUTDOWN chunk has a valid length. */
Xin Longac23e682017-08-03 15:42:10 +08005425 if (!sctp_chunk_length_valid(
5426 chunk, sizeof(struct sctp_shutdown_chunk)))
5427 return sctp_sf_violation_chunklen(net, ep, asoc, type,
5428 arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 }
5430
5431 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5432 * shall send a SHUTDOWN ACK ...
5433 */
5434 reply = sctp_make_shutdown_ack(asoc, chunk);
5435 if (!reply)
5436 goto nomem;
5437
5438 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5439 * the T2-shutdown timer.
5440 */
5441 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5442
5443 /* and start/restart a T2-shutdown timer of its own, */
5444 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5445 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5446
Neil Horman9f70f462013-12-10 06:48:15 -05005447 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5449 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5450
5451 /* Enter the SHUTDOWN-ACK-SENT state. */
5452 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5453 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5454
5455 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5456 *
5457 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005458 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 */
5460 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5461
5462 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5463
5464 return SCTP_DISPOSITION_CONSUME;
5465
5466nomem:
5467 return SCTP_DISPOSITION_NOMEM;
5468}
5469
5470/*
5471 * Ignore the event defined as other
5472 *
5473 * The return value is the disposition of the event.
5474 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005475sctp_disposition_t sctp_sf_ignore_other(struct net *net,
5476 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 const struct sctp_association *asoc,
5478 const sctp_subtype_t type,
5479 void *arg,
5480 sctp_cmd_seq_t *commands)
5481{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005482 pr_debug("%s: the event other type:%d is ignored\n",
5483 __func__, type.other);
5484
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 return SCTP_DISPOSITION_DISCARD;
5486}
5487
5488/************************************************************
5489 * These are the state functions for handling timeout events.
5490 ************************************************************/
5491
5492/*
5493 * RTX Timeout
5494 *
5495 * Section: 6.3.3 Handle T3-rtx Expiration
5496 *
5497 * Whenever the retransmission timer T3-rtx expires for a destination
5498 * address, do the following:
5499 * [See below]
5500 *
5501 * The return value is the disposition of the chunk.
5502 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005503sctp_disposition_t sctp_sf_do_6_3_3_rtx(struct net *net,
5504 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 const struct sctp_association *asoc,
5506 const sctp_subtype_t type,
5507 void *arg,
5508 sctp_cmd_seq_t *commands)
5509{
5510 struct sctp_transport *transport = arg;
5511
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005512 SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005513
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 if (asoc->overall_error_count >= asoc->max_retrans) {
lucien8a0d19c2015-12-05 15:35:36 +08005515 if (asoc->peer.zero_window_announced &&
5516 asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
Thomas Graff8d96052011-07-07 00:28:35 +00005517 /*
5518 * We are here likely because the receiver had its rwnd
5519 * closed for a while and we have not been able to
5520 * transmit the locally queued data within the maximum
5521 * retransmission attempts limit. Start the T5
5522 * shutdown guard timer to give the receiver one last
5523 * chance and some additional time to recover before
5524 * aborting.
5525 */
5526 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5527 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5528 } else {
5529 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5530 SCTP_ERROR(ETIMEDOUT));
5531 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5532 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5533 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005534 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5535 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Thomas Graff8d96052011-07-07 00:28:35 +00005536 return SCTP_DISPOSITION_DELETE_TCB;
5537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 }
5539
5540 /* E1) For the destination address for which the timer
5541 * expires, adjust its ssthresh with rules defined in Section
5542 * 7.2.3 and set the cwnd <- MTU.
5543 */
5544
5545 /* E2) For the destination address for which the timer
5546 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5547 * maximum value discussed in rule C7 above (RTO.max) may be
5548 * used to provide an upper bound to this doubling operation.
5549 */
5550
5551 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5552 * outstanding DATA chunks for the address for which the
5553 * T3-rtx has expired will fit into a single packet, subject
5554 * to the MTU constraint for the path corresponding to the
5555 * destination transport address to which the retransmission
5556 * is being sent (this may be different from the address for
5557 * which the timer expires [see Section 6.4]). Call this
5558 * value K. Bundle and retransmit those K DATA chunks in a
5559 * single packet to the destination endpoint.
5560 *
5561 * Note: Any DATA chunks that were sent to the address for
5562 * which the T3-rtx timer expired but did not fit in one MTU
5563 * (rule E3 above), should be marked for retransmission and
5564 * sent as soon as cwnd allows (normally when a SACK arrives).
5565 */
5566
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 /* Do some failure management (Section 8.2). */
5568 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5569
Vlad Yasevich1845a572007-02-21 02:06:19 -08005570 /* NB: Rules E4 and F1 are implicit in R1. */
5571 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5572
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 return SCTP_DISPOSITION_CONSUME;
5574}
5575
5576/*
5577 * Generate delayed SACK on timeout
5578 *
5579 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5580 *
5581 * The guidelines on delayed acknowledgement algorithm specified in
5582 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5583 * acknowledgement SHOULD be generated for at least every second packet
5584 * (not every second DATA chunk) received, and SHOULD be generated
5585 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5586 * some situations it may be beneficial for an SCTP transmitter to be
5587 * more conservative than the algorithms detailed in this document
5588 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5589 * the following algorithms allow.
5590 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005591sctp_disposition_t sctp_sf_do_6_2_sack(struct net *net,
5592 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 const struct sctp_association *asoc,
5594 const sctp_subtype_t type,
5595 void *arg,
5596 sctp_cmd_seq_t *commands)
5597{
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005598 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5600 return SCTP_DISPOSITION_CONSUME;
5601}
5602
5603/*
Frank Filz3f7a87d2005-06-20 13:14:57 -07005604 * sctp_sf_t1_init_timer_expire
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 *
5606 * Section: 4 Note: 2
5607 * Verification Tag:
5608 * Inputs
5609 * (endpoint, asoc)
5610 *
5611 * RFC 2960 Section 4 Notes
5612 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5613 * and re-start the T1-init timer without changing state. This MUST
5614 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5615 * endpoint MUST abort the initialization process and report the
5616 * error to SCTP user.
5617 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07005618 * Outputs
5619 * (timers, events)
5620 *
5621 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005622sctp_disposition_t sctp_sf_t1_init_timer_expire(struct net *net,
5623 const struct sctp_endpoint *ep,
Frank Filz3f7a87d2005-06-20 13:14:57 -07005624 const struct sctp_association *asoc,
5625 const sctp_subtype_t type,
5626 void *arg,
5627 sctp_cmd_seq_t *commands)
5628{
5629 struct sctp_chunk *repl = NULL;
5630 struct sctp_bind_addr *bp;
5631 int attempts = asoc->init_err_counter + 1;
5632
Daniel Borkmannbb333812013-06-28 19:49:40 +02005633 pr_debug("%s: timer T1 expired (INIT)\n", __func__);
5634
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005635 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005636
Vlad Yasevich81845c22006-01-30 15:59:54 -08005637 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005638 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5639 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5640 if (!repl)
5641 return SCTP_DISPOSITION_NOMEM;
5642
5643 /* Choose transport for INIT. */
5644 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5645 SCTP_CHUNK(repl));
5646
5647 /* Issue a sideeffect to do the needed accounting. */
5648 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5649 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5650
5651 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5652 } else {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005653 pr_debug("%s: giving up on INIT, attempts:%d "
5654 "max_init_attempts:%d\n", __func__, attempts,
5655 asoc->max_init_attempts);
5656
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005657 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5658 SCTP_ERROR(ETIMEDOUT));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005659 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005660 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005661 return SCTP_DISPOSITION_DELETE_TCB;
5662 }
5663
5664 return SCTP_DISPOSITION_CONSUME;
5665}
5666
5667/*
5668 * sctp_sf_t1_cookie_timer_expire
5669 *
5670 * Section: 4 Note: 2
5671 * Verification Tag:
5672 * Inputs
5673 * (endpoint, asoc)
5674 *
5675 * RFC 2960 Section 4 Notes
5676 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 * COOKIE ECHO and re-start the T1-cookie timer without changing
5678 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5679 * After that, the endpoint MUST abort the initialization process and
5680 * report the error to SCTP user.
5681 *
5682 * Outputs
5683 * (timers, events)
5684 *
5685 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005686sctp_disposition_t sctp_sf_t1_cookie_timer_expire(struct net *net,
5687 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 const struct sctp_association *asoc,
5689 const sctp_subtype_t type,
5690 void *arg,
5691 sctp_cmd_seq_t *commands)
5692{
Frank Filz3f7a87d2005-06-20 13:14:57 -07005693 struct sctp_chunk *repl = NULL;
5694 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695
Daniel Borkmannbb333812013-06-28 19:49:40 +02005696 pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
5697
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005698 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699
Vlad Yasevich81845c22006-01-30 15:59:54 -08005700 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005701 repl = sctp_make_cookie_echo(asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 if (!repl)
Frank Filz3f7a87d2005-06-20 13:14:57 -07005703 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704
Vlad Yasevich96cd0d32008-09-08 14:00:26 -04005705 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5706 SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 /* Issue a sideeffect to do the needed accounting. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005708 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5709 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5710
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5712 } else {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005713 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5714 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005716 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 return SCTP_DISPOSITION_DELETE_TCB;
5718 }
5719
5720 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721}
5722
5723/* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5724 * with the updated last sequential TSN received from its peer.
5725 *
5726 * An endpoint should limit the number of retransmissions of the
5727 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5728 * If this threshold is exceeded the endpoint should destroy the TCB and
5729 * MUST report the peer endpoint unreachable to the upper layer (and
5730 * thus the association enters the CLOSED state). The reception of any
5731 * packet from its peer (i.e. as the peer sends all of its queued DATA
5732 * chunks) should clear the endpoint's retransmission count and restart
5733 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5734 * all of its queued DATA chunks that have not yet been sent.
5735 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005736sctp_disposition_t sctp_sf_t2_timer_expire(struct net *net,
5737 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 const struct sctp_association *asoc,
5739 const sctp_subtype_t type,
5740 void *arg,
5741 sctp_cmd_seq_t *commands)
5742{
5743 struct sctp_chunk *reply = NULL;
5744
Daniel Borkmannbb333812013-06-28 19:49:40 +02005745 pr_debug("%s: timer T2 expired\n", __func__);
5746
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005747 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005748
Neil Horman58fbbed2008-02-29 11:40:56 -08005749 ((struct sctp_association *)asoc)->shutdown_retries++;
5750
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005752 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5753 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5755 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005756 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005757 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5758 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 return SCTP_DISPOSITION_DELETE_TCB;
5760 }
5761
5762 switch (asoc->state) {
5763 case SCTP_STATE_SHUTDOWN_SENT:
5764 reply = sctp_make_shutdown(asoc, NULL);
5765 break;
5766
5767 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5768 reply = sctp_make_shutdown_ack(asoc, NULL);
5769 break;
5770
5771 default:
5772 BUG();
5773 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775
5776 if (!reply)
5777 goto nomem;
5778
Wei Yongjun6345b192009-04-26 23:13:35 +08005779 /* Do some failure management (Section 8.2).
5780 * If we remove the transport an SHUTDOWN was last sent to, don't
5781 * do failure management.
5782 */
5783 if (asoc->shutdown_last_sent_to)
5784 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5785 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786
5787 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5788 * the T2-shutdown timer.
5789 */
5790 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5791
5792 /* Restart the T2-shutdown timer. */
5793 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5794 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5795 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5796 return SCTP_DISPOSITION_CONSUME;
5797
5798nomem:
5799 return SCTP_DISPOSITION_NOMEM;
5800}
5801
5802/*
5803 * ADDIP Section 4.1 ASCONF CHunk Procedures
5804 * If the T4 RTO timer expires the endpoint should do B1 to B5
5805 */
5806sctp_disposition_t sctp_sf_t4_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005807 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 const struct sctp_endpoint *ep,
5809 const struct sctp_association *asoc,
5810 const sctp_subtype_t type,
5811 void *arg,
5812 sctp_cmd_seq_t *commands)
5813{
5814 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5815 struct sctp_transport *transport = chunk->transport;
5816
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005817 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005818
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5820 * detection on the appropriate destination address as defined in
5821 * RFC2960 [5] section 8.1 and 8.2.
5822 */
Wei Yongjun10a43ce2009-04-26 23:14:42 +08005823 if (transport)
5824 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5825 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826
5827 /* Reconfig T4 timer and transport. */
5828 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5829
5830 /* ADDIP 4.1 B2) Increment the association error counters and perform
5831 * endpoint failure detection on the association as defined in
5832 * RFC2960 [5] section 8.1 and 8.2.
5833 * association error counter is incremented in SCTP_CMD_STRIKE.
5834 */
5835 if (asoc->overall_error_count >= asoc->max_retrans) {
5836 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5837 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005838 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5839 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005841 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005842 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5843 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844 return SCTP_DISPOSITION_ABORT;
5845 }
5846
5847 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5848 * the ASCONF chunk was sent by doubling the RTO timer value.
5849 * This is done in SCTP_CMD_STRIKE.
5850 */
5851
5852 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5853 * choose an alternate destination address (please refer to RFC2960
5854 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005855 * chunk, it MUST be the same (including its serial number) as the last
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 * ASCONF sent.
5857 */
5858 sctp_chunk_hold(asoc->addip_last_asconf);
5859 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5860 SCTP_CHUNK(asoc->addip_last_asconf));
5861
5862 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5863 * destination is selected, then the RTO used will be that of the new
5864 * destination address.
5865 */
5866 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5867 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5868
5869 return SCTP_DISPOSITION_CONSUME;
5870}
5871
5872/* sctpimpguide-05 Section 2.12.2
5873 * The sender of the SHUTDOWN MAY also start an overall guard timer
5874 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5875 * At the expiration of this timer the sender SHOULD abort the association
5876 * by sending an ABORT chunk.
5877 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005878sctp_disposition_t sctp_sf_t5_timer_expire(struct net *net,
5879 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 const struct sctp_association *asoc,
5881 const sctp_subtype_t type,
5882 void *arg,
5883 sctp_cmd_seq_t *commands)
5884{
5885 struct sctp_chunk *reply = NULL;
5886
Daniel Borkmannbb333812013-06-28 19:49:40 +02005887 pr_debug("%s: timer T5 expired\n", __func__);
5888
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005889 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890
5891 reply = sctp_make_abort(asoc, NULL, 0);
5892 if (!reply)
5893 goto nomem;
5894
5895 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005896 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5897 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005899 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005901 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5902 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevicha1080a82008-10-09 14:33:26 -07005903
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 return SCTP_DISPOSITION_DELETE_TCB;
5905nomem:
5906 return SCTP_DISPOSITION_NOMEM;
5907}
5908
5909/* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5910 * the association is automatically closed by starting the shutdown process.
5911 * The work that needs to be done is same as when SHUTDOWN is initiated by
5912 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5913 */
5914sctp_disposition_t sctp_sf_autoclose_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005915 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916 const struct sctp_endpoint *ep,
5917 const struct sctp_association *asoc,
5918 const sctp_subtype_t type,
5919 void *arg,
5920 sctp_cmd_seq_t *commands)
5921{
5922 int disposition;
5923
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005924 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005925
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926 /* From 9.2 Shutdown of an Association
5927 * Upon receipt of the SHUTDOWN primitive from its upper
5928 * layer, the endpoint enters SHUTDOWN-PENDING state and
5929 * remains there until all outstanding data has been
5930 * acknowledged by its peer. The endpoint accepts no new data
5931 * from its upper layer, but retransmits data to the far end
5932 * if necessary to fill gaps.
5933 */
5934 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5935 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5936
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 disposition = SCTP_DISPOSITION_CONSUME;
5938 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005939 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940 arg, commands);
5941 }
5942 return disposition;
5943}
5944
5945/*****************************************************************************
5946 * These are sa state functions which could apply to all types of events.
5947 ****************************************************************************/
5948
5949/*
5950 * This table entry is not implemented.
5951 *
5952 * Inputs
5953 * (endpoint, asoc, chunk)
5954 *
5955 * The return value is the disposition of the chunk.
5956 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005957sctp_disposition_t sctp_sf_not_impl(struct net *net,
5958 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959 const struct sctp_association *asoc,
5960 const sctp_subtype_t type,
5961 void *arg,
5962 sctp_cmd_seq_t *commands)
5963{
5964 return SCTP_DISPOSITION_NOT_IMPL;
5965}
5966
5967/*
5968 * This table entry represents a bug.
5969 *
5970 * Inputs
5971 * (endpoint, asoc, chunk)
5972 *
5973 * The return value is the disposition of the chunk.
5974 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005975sctp_disposition_t sctp_sf_bug(struct net *net,
5976 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 const struct sctp_association *asoc,
5978 const sctp_subtype_t type,
5979 void *arg,
5980 sctp_cmd_seq_t *commands)
5981{
5982 return SCTP_DISPOSITION_BUG;
5983}
5984
5985/*
5986 * This table entry represents the firing of a timer in the wrong state.
5987 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5988 * when the association is in the wrong state. This event should
5989 * be ignored, so as to prevent any rearming of the timer.
5990 *
5991 * Inputs
5992 * (endpoint, asoc, chunk)
5993 *
5994 * The return value is the disposition of the chunk.
5995 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005996sctp_disposition_t sctp_sf_timer_ignore(struct net *net,
5997 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 const struct sctp_association *asoc,
5999 const sctp_subtype_t type,
6000 void *arg,
6001 sctp_cmd_seq_t *commands)
6002{
Daniel Borkmannbb333812013-06-28 19:49:40 +02006003 pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
6004
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005 return SCTP_DISPOSITION_CONSUME;
6006}
6007
6008/********************************************************************
6009 * 2nd Level Abstractions
6010 ********************************************************************/
6011
6012/* Pull the SACK chunk based on the SACK header. */
6013static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
6014{
6015 struct sctp_sackhdr *sack;
6016 unsigned int len;
6017 __u16 num_blocks;
6018 __u16 num_dup_tsns;
6019
6020 /* Protect ourselves from reading too far into
6021 * the skb from a bogus sender.
6022 */
6023 sack = (struct sctp_sackhdr *) chunk->skb->data;
6024
6025 num_blocks = ntohs(sack->num_gap_ack_blocks);
6026 num_dup_tsns = ntohs(sack->num_dup_tsns);
6027 len = sizeof(struct sctp_sackhdr);
6028 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
6029 if (len > chunk->skb->len)
6030 return NULL;
6031
6032 skb_pull(chunk->skb, len);
6033
6034 return sack;
6035}
6036
6037/* Create an ABORT packet to be sent as a response, with the specified
6038 * error causes.
6039 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006040static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
6041 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042 const struct sctp_association *asoc,
6043 struct sctp_chunk *chunk,
6044 const void *payload,
6045 size_t paylen)
6046{
6047 struct sctp_packet *packet;
6048 struct sctp_chunk *abort;
6049
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006050 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051
6052 if (packet) {
6053 /* Make an ABORT.
6054 * The T bit will be set if the asoc is NULL.
6055 */
6056 abort = sctp_make_abort(asoc, chunk, paylen);
6057 if (!abort) {
6058 sctp_ootb_pkt_free(packet);
6059 return NULL;
6060 }
Jerome Forissier047a2422005-04-28 11:58:43 -07006061
6062 /* Reflect vtag if T-Bit is set */
6063 if (sctp_test_T_bit(abort))
6064 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
6065
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 /* Add specified error causes, i.e., payload, to the
6067 * end of the chunk.
6068 */
6069 sctp_addto_chunk(abort, paylen, payload);
6070
6071 /* Set the skb to the belonging sock for accounting. */
6072 abort->skb->sk = ep->base.sk;
6073
6074 sctp_packet_append_chunk(packet, abort);
6075
6076 }
6077
6078 return packet;
6079}
6080
6081/* Allocate a packet for responding in the OOTB conditions. */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006082static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
6083 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 const struct sctp_chunk *chunk)
6085{
6086 struct sctp_packet *packet;
6087 struct sctp_transport *transport;
6088 __u16 sport;
6089 __u16 dport;
6090 __u32 vtag;
6091
6092 /* Get the source and destination port from the inbound packet. */
6093 sport = ntohs(chunk->sctp_hdr->dest);
6094 dport = ntohs(chunk->sctp_hdr->source);
6095
6096 /* The V-tag is going to be the same as the inbound packet if no
6097 * association exists, otherwise, use the peer's vtag.
6098 */
6099 if (asoc) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08006100 /* Special case the INIT-ACK as there is no peer's vtag
6101 * yet.
6102 */
wangweidongcb3f8372013-12-23 12:16:50 +08006103 switch (chunk->chunk_hdr->type) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08006104 case SCTP_CID_INIT_ACK:
6105 {
Xin Longcb1844c2017-07-23 09:34:26 +08006106 struct sctp_initack_chunk *initack;
Wei Yongjun02c4e122007-08-31 10:03:58 +08006107
Xin Longcb1844c2017-07-23 09:34:26 +08006108 initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
Wei Yongjun02c4e122007-08-31 10:03:58 +08006109 vtag = ntohl(initack->init_hdr.init_tag);
6110 break;
6111 }
6112 default:
6113 vtag = asoc->peer.i.init_tag;
6114 break;
6115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 } else {
6117 /* Special case the INIT and stale COOKIE_ECHO as there is no
6118 * vtag yet.
6119 */
wangweidongcb3f8372013-12-23 12:16:50 +08006120 switch (chunk->chunk_hdr->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 case SCTP_CID_INIT:
6122 {
Xin Long01a992b2017-06-30 11:52:22 +08006123 struct sctp_init_chunk *init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124
Xin Long01a992b2017-06-30 11:52:22 +08006125 init = (struct sctp_init_chunk *)chunk->chunk_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126 vtag = ntohl(init->init_hdr.init_tag);
6127 break;
6128 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006129 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130 vtag = ntohl(chunk->sctp_hdr->vtag);
6131 break;
6132 }
6133 }
6134
6135 /* Make a transport for the bucket, Eliza... */
Eric W. Biederman89bf3452012-08-07 07:26:14 +00006136 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 if (!transport)
6138 goto nomem;
6139
6140 /* Cache a route for the transport with the chunk's destination as
6141 * the source address.
6142 */
Al Viro16b0a032006-11-20 17:13:38 -08006143 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006144 sctp_sk(net->sctp.ctl_sock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145
Marcelo Ricardo Leitner66b91d22016-12-28 09:26:34 -02006146 packet = &transport->packet;
6147 sctp_packet_init(packet, transport, sport, dport);
6148 sctp_packet_config(packet, vtag, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149
6150 return packet;
6151
6152nomem:
6153 return NULL;
6154}
6155
6156/* Free the packet allocated earlier for responding in the OOTB condition. */
6157void sctp_ootb_pkt_free(struct sctp_packet *packet)
6158{
6159 sctp_transport_free(packet->transport);
6160}
6161
6162/* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006163static void sctp_send_stale_cookie_err(struct net *net,
6164 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 const struct sctp_association *asoc,
6166 const struct sctp_chunk *chunk,
6167 sctp_cmd_seq_t *commands,
6168 struct sctp_chunk *err_chunk)
6169{
6170 struct sctp_packet *packet;
6171
6172 if (err_chunk) {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006173 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 if (packet) {
6175 struct sctp_signed_cookie *cookie;
6176
6177 /* Override the OOTB vtag from the cookie. */
6178 cookie = chunk->subh.cookie_hdr;
6179 packet->vtag = cookie->c.peer_vtag;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006180
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181 /* Set the skb to the belonging sock for accounting. */
6182 err_chunk->skb->sk = ep->base.sk;
6183 sctp_packet_append_chunk(packet, err_chunk);
6184 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6185 SCTP_PACKET(packet));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006186 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187 } else
6188 sctp_chunk_free (err_chunk);
6189 }
6190}
6191
6192
6193/* Process a data chunk */
6194static int sctp_eat_data(const struct sctp_association *asoc,
6195 struct sctp_chunk *chunk,
6196 sctp_cmd_seq_t *commands)
6197{
Xin Long3583df12017-06-30 11:52:19 +08006198 struct sctp_datahdr *data_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 struct sctp_chunk *err;
6200 size_t datalen;
6201 sctp_verb_t deliver;
6202 int tmp;
6203 __u32 tsn;
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006204 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
Neil Horman049b3ff2005-11-11 16:08:24 -08006205 struct sock *sk = asoc->base.sk;
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006206 struct net *net = sock_net(sk);
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006207 u16 ssn;
6208 u16 sid;
6209 u8 ordered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210
Xin Long3583df12017-06-30 11:52:19 +08006211 data_hdr = (struct sctp_datahdr *)chunk->skb->data;
6212 chunk->subh.data_hdr = data_hdr;
6213 skb_pull(chunk->skb, sizeof(*data_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214
6215 tsn = ntohl(data_hdr->tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02006216 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217
6218 /* ASSERT: Now skb->data is really the user data. */
6219
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 /* Process ECN based congestion.
6221 *
6222 * Since the chunk structure is reused for all chunks within
6223 * a packet, we use ecn_ce_done to track if we've already
6224 * done CE processing for this packet.
6225 *
6226 * We need to do ECN processing even if we plan to discard the
6227 * chunk later.
6228 */
6229
Marcelo Ricardo Leitner2d47fd12016-07-13 15:09:00 -03006230 if (asoc->peer.ecn_capable && !chunk->ecn_ce_done) {
Marcelo Ricardo Leitnere7487c82016-07-13 15:08:58 -03006231 struct sctp_af *af = SCTP_INPUT_CB(chunk->skb)->af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 chunk->ecn_ce_done = 1;
6233
Marcelo Ricardo Leitner2d47fd12016-07-13 15:09:00 -03006234 if (af->is_ce(sctp_gso_headskb(chunk->skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235 /* Do real work as sideffect. */
6236 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6237 SCTP_U32(tsn));
6238 }
6239 }
6240
6241 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6242 if (tmp < 0) {
6243 /* The TSN is too high--silently discard the chunk and
6244 * count on it getting retransmitted later.
6245 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006246 if (chunk->asoc)
6247 chunk->asoc->stats.outofseqtsns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248 return SCTP_IERROR_HIGH_TSN;
6249 } else if (tmp > 0) {
6250 /* This is a duplicate. Record it. */
6251 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6252 return SCTP_IERROR_DUP_TSN;
6253 }
6254
6255 /* This is a new TSN. */
6256
6257 /* Discard if there is no room in the receive window.
6258 * Actually, allow a little bit of overflow (up to a MTU).
6259 */
6260 datalen = ntohs(chunk->chunk_hdr->length);
Xin Long9f8d3142017-06-30 11:52:20 +08006261 datalen -= sizeof(struct sctp_data_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262
6263 deliver = SCTP_CMD_CHUNK_ULP;
6264
6265 /* Think about partial delivery. */
6266 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6267
6268 /* Even if we don't accept this chunk there is
6269 * memory pressure.
6270 */
6271 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6272 }
6273
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006274 /* Spill over rwnd a little bit. Note: While allowed, this spill over
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275 * seems a bit troublesome in that frag_point varies based on
6276 * PMTU. In cases, such as loopback, this might be a rather
6277 * large spill over.
6278 */
Daniel Borkmann362d5202014-04-14 21:45:17 +02006279 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
Neil Horman4d93df02007-08-15 16:07:44 -07006280 (datalen > asoc->rwnd + asoc->frag_point))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281
6282 /* If this is the next TSN, consider reneging to make
6283 * room. Note: Playing nice with a confused sender. A
6284 * malicious sender can still eat up all our buffer
6285 * space and in the future we may want to detect and
6286 * do more drastic reneging.
6287 */
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006288 if (sctp_tsnmap_has_gap(map) &&
6289 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006290 pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 deliver = SCTP_CMD_RENEGE;
6292 } else {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006293 pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
6294 __func__, tsn, datalen, asoc->rwnd);
6295
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 return SCTP_IERROR_IGNORE_TSN;
6297 }
6298 }
6299
6300 /*
Neil Horman4d93df02007-08-15 16:07:44 -07006301 * Also try to renege to limit our memory usage in the event that
6302 * we are under memory pressure
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006303 * If we can't renege, don't worry about it, the sk_rmem_schedule
Neil Horman4d93df02007-08-15 16:07:44 -07006304 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6305 * memory usage too much
6306 */
6307 if (*sk->sk_prot_creator->memory_pressure) {
6308 if (sctp_tsnmap_has_gap(map) &&
wangweidongf7010e62013-12-23 12:16:52 +08006309 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006310 pr_debug("%s: under pressure, reneging for tsn:%u\n",
6311 __func__, tsn);
Neil Horman4d93df02007-08-15 16:07:44 -07006312 deliver = SCTP_CMD_RENEGE;
6313 }
6314 }
6315
6316 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 * Section 3.3.10.9 No User Data (9)
6318 *
6319 * Cause of error
6320 * ---------------
6321 * No User Data: This error cause is returned to the originator of a
6322 * DATA chunk if a received DATA chunk has no user data.
6323 */
6324 if (unlikely(0 == datalen)) {
6325 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6326 if (err) {
6327 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6328 SCTP_CHUNK(err));
6329 }
6330 /* We are going to ABORT, so we might as well stop
6331 * processing the rest of the chunks in the packet.
6332 */
wangweidongcb3f8372013-12-23 12:16:50 +08006333 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07006334 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6335 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08006337 SCTP_PERR(SCTP_ERROR_NO_DATA));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006338 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6339 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340 return SCTP_IERROR_NO_DATA;
6341 }
6342
Sridhar Samudrala9faa7302006-07-21 14:49:07 -07006343 chunk->data_accepted = 1;
6344
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6346 * if we renege and the chunk arrives again.
6347 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006348 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006349 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006350 if (chunk->asoc)
6351 chunk->asoc->stats.iuodchunks++;
6352 } else {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006353 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006354 if (chunk->asoc)
6355 chunk->asoc->stats.iodchunks++;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006356 ordered = 1;
6357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358
6359 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6360 *
6361 * If an endpoint receive a DATA chunk with an invalid stream
6362 * identifier, it shall acknowledge the reception of the DATA chunk
6363 * following the normal procedure, immediately send an ERROR chunk
6364 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6365 * and discard the DATA chunk.
6366 */
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006367 sid = ntohs(data_hdr->stream);
Xin Longcee360a2017-05-31 16:36:31 +08006368 if (sid >= asoc->stream.incnt) {
Vlad Yasevich3888e9e2008-07-08 02:28:39 -07006369 /* Mark tsn as received even though we drop it */
6370 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6371
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6373 &data_hdr->stream,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05006374 sizeof(data_hdr->stream),
6375 sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376 if (err)
6377 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6378 SCTP_CHUNK(err));
6379 return SCTP_IERROR_BAD_STREAM;
6380 }
6381
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006382 /* Check to see if the SSN is possible for this TSN.
6383 * The biggest gap we can record is 4K wide. Since SSNs wrap
6384 * at an unsigned short, there is no way that an SSN can
6385 * wrap and for a valid TSN. We can simply check if the current
6386 * SSN is smaller then the next expected one. If it is, it wrapped
6387 * and is invalid.
6388 */
6389 ssn = ntohs(data_hdr->ssn);
Xin Longcee360a2017-05-31 16:36:31 +08006390 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->stream, in, sid)))
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006391 return SCTP_IERROR_PROTO_VIOLATION;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006392
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393 /* Send the data up to the user. Note: Schedule the
6394 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6395 * chunk needs the updated rwnd.
6396 */
6397 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6398
6399 return SCTP_IERROR_NO_ERROR;
6400}