blob: cc56a67dbb4ddf633d2277ca36e8620bf5479fb2 [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
Masami Hiramatsu103d7502017-12-29 11:46:51 +090062#define CREATE_TRACE_POINTS
63#include <trace/events/sctp.h>
64
Xin Long172a1592017-08-11 10:23:57 +080065static struct sctp_packet *sctp_abort_pkt_new(
66 struct net *net,
67 const struct sctp_endpoint *ep,
68 const struct sctp_association *asoc,
69 struct sctp_chunk *chunk,
70 const void *payload, size_t paylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static int sctp_eat_data(const struct sctp_association *asoc,
72 struct sctp_chunk *chunk,
Xin Longa85bbeb2017-08-11 10:23:52 +080073 struct sctp_cmd_seq *commands);
Xin Long172a1592017-08-11 10:23:57 +080074static struct sctp_packet *sctp_ootb_pkt_new(
75 struct net *net,
76 const struct sctp_association *asoc,
77 const struct sctp_chunk *chunk);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000078static void sctp_send_stale_cookie_err(struct net *net,
79 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 const struct sctp_association *asoc,
81 const struct sctp_chunk *chunk,
Xin Longa85bbeb2017-08-11 10:23:52 +080082 struct sctp_cmd_seq *commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 struct sctp_chunk *err_chunk);
Xin Long172a1592017-08-11 10:23:57 +080084static enum sctp_disposition sctp_sf_do_5_2_6_stale(
85 struct net *net,
86 const struct sctp_endpoint *ep,
87 const struct sctp_association *asoc,
88 const union sctp_subtype type,
89 void *arg,
90 struct sctp_cmd_seq *commands);
91static enum sctp_disposition sctp_sf_shut_8_4_5(
92 struct net *net,
93 const struct sctp_endpoint *ep,
94 const struct sctp_association *asoc,
95 const union sctp_subtype type,
96 void *arg,
97 struct sctp_cmd_seq *commands);
98static enum sctp_disposition sctp_sf_tabort_8_4_8(
99 struct net *net,
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000100 const struct sctp_endpoint *ep,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400101 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +0800102 const union sctp_subtype type,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400103 void *arg,
Xin Longa85bbeb2017-08-11 10:23:52 +0800104 struct sctp_cmd_seq *commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
106
Xin Long172a1592017-08-11 10:23:57 +0800107static enum sctp_disposition sctp_stop_t1_and_abort(
108 struct net *net,
109 struct sctp_cmd_seq *commands,
110 __be16 error, int sk_err,
111 const struct sctp_association *asoc,
112 struct sctp_transport *transport);
Adrian Bunk52c1da32005-06-23 22:05:33 -0700113
Xin Long172a1592017-08-11 10:23:57 +0800114static enum sctp_disposition sctp_sf_abort_violation(
115 struct net *net,
116 const struct sctp_endpoint *ep,
117 const struct sctp_association *asoc,
118 void *arg,
119 struct sctp_cmd_seq *commands,
120 const __u8 *payload,
121 const size_t paylen);
Wei Yongjunaecedea2007-08-02 16:57:44 +0800122
Xin Long172a1592017-08-11 10:23:57 +0800123static enum sctp_disposition sctp_sf_violation_chunklen(
124 struct net *net,
125 const struct sctp_endpoint *ep,
126 const struct sctp_association *asoc,
127 const union sctp_subtype type,
128 void *arg,
129 struct sctp_cmd_seq *commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Xin Long172a1592017-08-11 10:23:57 +0800131static enum sctp_disposition sctp_sf_violation_paramlen(
132 struct net *net,
133 const struct sctp_endpoint *ep,
134 const struct sctp_association *asoc,
135 const union sctp_subtype type,
136 void *arg, void *ext,
137 struct sctp_cmd_seq *commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +0800138
Xin Long172a1592017-08-11 10:23:57 +0800139static enum sctp_disposition sctp_sf_violation_ctsn(
140 struct net *net,
141 const struct sctp_endpoint *ep,
142 const struct sctp_association *asoc,
143 const union sctp_subtype type,
144 void *arg,
145 struct sctp_cmd_seq *commands);
Wei Yongjunaecedea2007-08-02 16:57:44 +0800146
Xin Long172a1592017-08-11 10:23:57 +0800147static enum sctp_disposition sctp_sf_violation_chunk(
148 struct net *net,
149 const struct sctp_endpoint *ep,
150 const struct sctp_association *asoc,
151 const union sctp_subtype type,
152 void *arg,
153 struct sctp_cmd_seq *commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -0400154
Xin Long4785c7a2017-08-05 19:59:58 +0800155static enum sctp_ierror sctp_sf_authenticate(
Xin Long172a1592017-08-11 10:23:57 +0800156 struct net *net,
157 const struct sctp_endpoint *ep,
158 const struct sctp_association *asoc,
159 const union sctp_subtype type,
160 struct sctp_chunk *chunk);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700161
Xin Long172a1592017-08-11 10:23:57 +0800162static enum sctp_disposition __sctp_sf_do_9_1_abort(
163 struct net *net,
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000164 const struct sctp_endpoint *ep,
Vlad Yasevich75205f42007-12-20 14:12:59 -0800165 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +0800166 const union sctp_subtype type,
Vlad Yasevich75205f42007-12-20 14:12:59 -0800167 void *arg,
Xin Longa85bbeb2017-08-11 10:23:52 +0800168 struct sctp_cmd_seq *commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -0800169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/* Small helper function that checks if the chunk length
171 * is of the appropriate length. The 'required_length' argument
172 * is set to be the size of a specific chunk we are testing.
Marcelo Ricardo Leitner509e7a32016-12-28 09:26:35 -0200173 * Return Values: true = Valid length
174 * false = Invalid length
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 *
176 */
Xin Long172a1592017-08-11 10:23:57 +0800177static inline bool sctp_chunk_length_valid(struct sctp_chunk *chunk,
178 __u16 required_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
181
Daniel Borkmann26b87c72014-10-09 22:55:33 +0200182 /* Previously already marked? */
183 if (unlikely(chunk->pdiscard))
Marcelo Ricardo Leitner509e7a32016-12-28 09:26:35 -0200184 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (unlikely(chunk_length < required_length))
Marcelo Ricardo Leitner509e7a32016-12-28 09:26:35 -0200186 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Marcelo Ricardo Leitner509e7a32016-12-28 09:26:35 -0200188 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191/**********************************************************
192 * These are the state functions for handling chunk events.
193 **********************************************************/
194
195/*
196 * Process the final SHUTDOWN COMPLETE.
197 *
198 * Section: 4 (C) (diagram), 9.2
199 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
200 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
201 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
202 * should stop the T2-shutdown timer and remove all knowledge of the
203 * association (and thus the association enters the CLOSED state).
204 *
Jerome Forissier047a2422005-04-28 11:58:43 -0700205 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 * C) Rules for packet carrying SHUTDOWN COMPLETE:
207 * ...
Jerome Forissier047a2422005-04-28 11:58:43 -0700208 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
209 * if the Verification Tag field of the packet matches its own tag and
210 * the T bit is not set
211 * OR
212 * it is set to its peer's tag and the T bit is set in the Chunk
213 * Flags.
214 * Otherwise, the receiver MUST silently discard the packet
215 * and take no further action. An endpoint MUST ignore the
216 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 *
218 * Inputs
219 * (endpoint, asoc, chunk)
220 *
221 * Outputs
222 * (asoc, reply_msg, msg_up, timers, counters)
223 *
224 * The return value is the disposition of the chunk.
225 */
Xin Long172a1592017-08-11 10:23:57 +0800226enum sctp_disposition sctp_sf_do_4_C(struct net *net,
227 const struct sctp_endpoint *ep,
228 const struct sctp_association *asoc,
229 const union sctp_subtype type,
230 void *arg, struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 struct sctp_chunk *chunk = arg;
233 struct sctp_ulpevent *ev;
234
Vlad Yasevichece25df2007-09-07 16:30:54 -0400235 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000236 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -0400237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 /* RFC 2960 6.10 Bundling
239 *
240 * An endpoint MUST NOT bundle INIT, INIT ACK or
241 * SHUTDOWN COMPLETE with any other chunks.
242 */
243 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000244 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Vlad Yasevichece25df2007-09-07 16:30:54 -0400246 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
Xin Long922dbc52017-06-30 11:52:13 +0800247 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000248 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400249 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 /* RFC 2960 10.2 SCTP-to-ULP
252 *
253 * H) SHUTDOWN COMPLETE notification
254 *
255 * When SCTP completes the shutdown procedures (section 9.2) this
256 * notification is passed to the upper layer.
257 */
258 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700259 0, 0, 0, NULL, GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700260 if (ev)
261 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900262 SCTP_ULPEVENT(ev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
265 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
266 * not the chunk should be discarded. If the endpoint is in
267 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
268 * T2-shutdown timer and remove all knowledge of the
269 * association (and thus the association enters the CLOSED
270 * state).
271 */
272 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
273 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
274
275 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
276 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
277
278 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
279 SCTP_STATE(SCTP_STATE_CLOSED));
280
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000281 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
282 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
285
286 return SCTP_DISPOSITION_DELETE_TCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289/*
290 * Respond to a normal INIT chunk.
291 * We are the side that is being asked for an association.
292 *
293 * Section: 5.1 Normal Establishment of an Association, B
294 * B) "Z" shall respond immediately with an INIT ACK chunk. The
295 * destination IP address of the INIT ACK MUST be set to the source
296 * IP address of the INIT to which this INIT ACK is responding. In
297 * the response, besides filling in other parameters, "Z" must set the
298 * Verification Tag field to Tag_A, and also provide its own
299 * Verification Tag (Tag_Z) in the Initiate Tag field.
300 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900301 * Verification Tag: Must be 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 *
303 * Inputs
304 * (endpoint, asoc, chunk)
305 *
306 * Outputs
307 * (asoc, reply_msg, msg_up, timers, counters)
308 *
309 * The return value is the disposition of the chunk.
310 */
Xin Long172a1592017-08-11 10:23:57 +0800311enum sctp_disposition sctp_sf_do_5_1B_init(struct net *net,
312 const struct sctp_endpoint *ep,
313 const struct sctp_association *asoc,
314 const union sctp_subtype type,
315 void *arg,
316 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Xin Long62e6b7e2017-07-23 09:34:28 +0800318 struct sctp_chunk *chunk = arg, *repl, *err_chunk;
319 struct sctp_unrecognized_param *unk_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 struct sctp_association *new_asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 struct sctp_packet *packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 int len;
323
324 /* 6.10 Bundling
325 * An endpoint MUST NOT bundle INIT, INIT ACK or
326 * SHUTDOWN COMPLETE with any other chunks.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900327 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 * IG Section 2.11.2
329 * Furthermore, we require that the receiver of an INIT chunk MUST
330 * enforce these rules by silently discarding an arriving packet
331 * with an INIT chunk that is bundled with other chunks.
332 */
333 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000334 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 /* If the packet is an OOTB packet which is temporarily on the
337 * control endpoint, respond with an ABORT.
338 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +0000339 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000340 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000341 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900345 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 */
347 if (chunk->sctp_hdr->vtag != 0)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000348 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 /* Make sure that the INIT chunk has a valid length.
351 * Normally, this would cause an ABORT with a Protocol Violation
352 * error, but since we don't have an association, we'll
353 * just discard the packet.
354 */
Xin Long01a992b2017-06-30 11:52:22 +0800355 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000356 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Vlad Yasevichbec96402009-07-30 18:08:28 -0400358 /* If the INIT is coming toward a closing socket, we'll send back
359 * and ABORT. Essentially, this catches the race of INIT being
360 * backloged to the socket at the same time as the user isses close().
361 * Since the socket and all its associations are going away, we
362 * can treat this OOTB
363 */
364 if (sctp_sstate(ep->base.sk, CLOSING))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000365 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Vlad Yasevichbec96402009-07-30 18:08:28 -0400366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 /* Verify the INIT chunk before processing it. */
368 err_chunk = NULL;
Vlad Yasevichb14878c2014-04-17 17:26:50 +0200369 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Xin Long01a992b2017-06-30 11:52:22 +0800370 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 &err_chunk)) {
372 /* This chunk contains fatal error. It is to be discarded.
373 * Send an ABORT, with causes if there is any.
374 */
375 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000376 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 (__u8 *)(err_chunk->chunk_hdr) +
Xin Long922dbc52017-06-30 11:52:13 +0800378 sizeof(struct sctp_chunkhdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 ntohs(err_chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +0800380 sizeof(struct sctp_chunkhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 sctp_chunk_free(err_chunk);
383
384 if (packet) {
385 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
386 SCTP_PACKET(packet));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000387 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return SCTP_DISPOSITION_CONSUME;
389 } else {
390 return SCTP_DISPOSITION_NOMEM;
391 }
392 } else {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000393 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 commands);
395 }
396 }
397
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900398 /* Grab the INIT header. */
Xin Long4ae70c02017-06-30 11:52:21 +0800399 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 /* Tag the variable length parameters. */
Xin Long4ae70c02017-06-30 11:52:21 +0800402 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
405 if (!new_asoc)
406 goto nomem;
407
Vlad Yasevich409b95a2009-11-10 08:57:34 +0000408 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
409 sctp_scope(sctp_source(chunk)),
410 GFP_ATOMIC) < 0)
411 goto nomem_init;
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 /* The call, sctp_process_init(), can fail on memory allocation. */
Wei Yongjunde6becdc02011-04-19 21:30:51 +0000414 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Xin Long01a992b2017-06-30 11:52:22 +0800415 (struct sctp_init_chunk *)chunk->chunk_hdr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 GFP_ATOMIC))
417 goto nomem_init;
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
420
421 /* If there are errors need to be reported for unknown parameters,
422 * make sure to reserve enough room in the INIT ACK for them.
423 */
424 len = 0;
425 if (err_chunk)
426 len = ntohs(err_chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +0800427 sizeof(struct sctp_chunkhdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
430 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700431 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 /* If there are errors need to be reported for unknown parameters,
434 * include them in the outgoing INIT ACK as "Unrecognized parameter"
435 * parameter.
436 */
437 if (err_chunk) {
438 /* Get the "Unrecognized parameter" parameter(s) out of the
439 * ERROR chunk generated by sctp_verify_init(). Since the
440 * error cause code for "unknown parameter" and the
441 * "Unrecognized parameter" type is the same, we can
442 * construct the parameters in INIT ACK by copying the
443 * ERROR causes over.
444 */
Xin Long62e6b7e2017-07-23 09:34:28 +0800445 unk_param = (struct sctp_unrecognized_param *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 ((__u8 *)(err_chunk->chunk_hdr) +
Xin Long922dbc52017-06-30 11:52:13 +0800447 sizeof(struct sctp_chunkhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 /* Replace the cause code with the "Unrecognized parameter"
449 * parameter type.
450 */
451 sctp_addto_chunk(repl, len, unk_param);
452 sctp_chunk_free(err_chunk);
453 }
454
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700455 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
458
459 /*
460 * Note: After sending out INIT ACK with the State Cookie parameter,
461 * "Z" MUST NOT allocate any resources, nor keep any states for the
462 * new association. Otherwise, "Z" will be vulnerable to resource
463 * attacks.
464 */
465 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
466
467 return SCTP_DISPOSITION_DELETE_TCB;
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469nomem_init:
470 sctp_association_free(new_asoc);
471nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700472 if (err_chunk)
473 sctp_chunk_free(err_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return SCTP_DISPOSITION_NOMEM;
475}
476
477/*
478 * Respond to a normal INIT ACK chunk.
479 * We are the side that is initiating the association.
480 *
481 * Section: 5.1 Normal Establishment of an Association, C
482 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
483 * timer and leave COOKIE-WAIT state. "A" shall then send the State
484 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
485 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
486 *
487 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
488 * DATA chunks, but it MUST be the first chunk in the packet and
489 * until the COOKIE ACK is returned the sender MUST NOT send any
490 * other packets to the peer.
491 *
492 * Verification Tag: 3.3.3
493 * If the value of the Initiate Tag in a received INIT ACK chunk is
494 * found to be 0, the receiver MUST treat it as an error and close the
495 * association by transmitting an ABORT.
496 *
497 * Inputs
498 * (endpoint, asoc, chunk)
499 *
500 * Outputs
501 * (asoc, reply_msg, msg_up, timers, counters)
502 *
503 * The return value is the disposition of the chunk.
504 */
Xin Long172a1592017-08-11 10:23:57 +0800505enum sctp_disposition sctp_sf_do_5_1C_ack(struct net *net,
506 const struct sctp_endpoint *ep,
507 const struct sctp_association *asoc,
508 const union sctp_subtype type,
509 void *arg,
510 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Xin Long01a992b2017-06-30 11:52:22 +0800512 struct sctp_init_chunk *initchunk;
Xin Long172a1592017-08-11 10:23:57 +0800513 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 struct sctp_chunk *err_chunk;
515 struct sctp_packet *packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000518 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /* 6.10 Bundling
521 * An endpoint MUST NOT bundle INIT, INIT ACK or
522 * SHUTDOWN COMPLETE with any other chunks.
523 */
524 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000525 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Vlad Yasevichece25df2007-09-07 16:30:54 -0400527 /* Make sure that the INIT-ACK chunk has a valid length */
Xin Longcb1844c2017-07-23 09:34:26 +0800528 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_initack_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000529 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400530 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 /* Grab the INIT header. */
Xin Long4ae70c02017-06-30 11:52:21 +0800532 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /* Verify the INIT chunk before processing it. */
535 err_chunk = NULL;
Vlad Yasevichb14878c2014-04-17 17:26:50 +0200536 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Xin Long01a992b2017-06-30 11:52:22 +0800537 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 &err_chunk)) {
539
Xin Long2a493212017-08-03 15:42:13 +0800540 enum sctp_error error = SCTP_ERROR_NO_RESOURCE;
Vlad Yasevich853f4b52008-01-20 06:10:46 -0800541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 /* This chunk contains fatal error. It is to be discarded.
Vlad Yasevichd6701192007-12-20 14:13:31 -0800543 * Send an ABORT, with causes. If there are no causes,
544 * then there wasn't enough memory. Just terminate
545 * the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 */
547 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000548 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 (__u8 *)(err_chunk->chunk_hdr) +
Xin Long922dbc52017-06-30 11:52:13 +0800550 sizeof(struct sctp_chunkhdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 ntohs(err_chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +0800552 sizeof(struct sctp_chunkhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 sctp_chunk_free(err_chunk);
555
556 if (packet) {
557 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
558 SCTP_PACKET(packet));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000559 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700560 error = SCTP_ERROR_INV_PARAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700563
564 /* SCTP-AUTH, Section 6.3:
565 * It should be noted that if the receiver wants to tear
566 * down an association in an authenticated way only, the
567 * handling of malformed packets should not result in
568 * tearing down the association.
569 *
570 * This means that if we only want to abort associations
571 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300572 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700573 * was malformed.
574 */
575 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000576 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700577
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000578 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
579 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700580 asoc, chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
582
583 /* Tag the variable length parameters. Note that we never
584 * convert the parameters in an INIT chunk.
585 */
Xin Long4ae70c02017-06-30 11:52:21 +0800586 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Xin Long01a992b2017-06-30 11:52:22 +0800588 initchunk = (struct sctp_init_chunk *)chunk->chunk_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
591 SCTP_PEER_INIT(initchunk));
592
Frank Filz3f7a87d2005-06-20 13:14:57 -0700593 /* Reset init error count upon receipt of INIT-ACK. */
594 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /* 5.1 C) "A" shall stop the T1-init timer and leave
597 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
598 * timer, and enter the COOKIE-ECHOED state.
599 */
600 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
601 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
602 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
603 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
604 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
605 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
606
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700607 /* SCTP-AUTH: genereate the assocition shared keys so that
608 * we can potentially signe the COOKIE-ECHO.
609 */
610 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /* 5.1 C) "A" shall then send the State Cookie received in the
613 * INIT ACK chunk in a COOKIE ECHO chunk, ...
614 */
615 /* If there is any errors to report, send the ERROR chunk generated
616 * for unknown parameters as well.
617 */
618 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
619 SCTP_CHUNK(err_chunk));
620
621 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
624/*
625 * Respond to a normal COOKIE ECHO chunk.
626 * We are the side that is being asked for an association.
627 *
628 * Section: 5.1 Normal Establishment of an Association, D
629 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
630 * with a COOKIE ACK chunk after building a TCB and moving to
631 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
632 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
633 * chunk MUST be the first chunk in the packet.
634 *
635 * IMPLEMENTATION NOTE: An implementation may choose to send the
636 * Communication Up notification to the SCTP user upon reception
637 * of a valid COOKIE ECHO chunk.
638 *
639 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
640 * D) Rules for packet carrying a COOKIE ECHO
641 *
642 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
643 * Initial Tag received in the INIT ACK.
644 *
645 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
646 *
647 * Inputs
648 * (endpoint, asoc, chunk)
649 *
650 * Outputs
651 * (asoc, reply_msg, msg_up, timers, counters)
652 *
653 * The return value is the disposition of the chunk.
654 */
Xin Long172a1592017-08-11 10:23:57 +0800655enum sctp_disposition sctp_sf_do_5_1D_ce(struct net *net,
656 const struct sctp_endpoint *ep,
657 const struct sctp_association *asoc,
658 const union sctp_subtype type,
659 void *arg,
660 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Xin Long30f6ebf2018-03-14 19:05:34 +0800662 struct sctp_ulpevent *ev, *ai_ev = NULL, *auth_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 struct sctp_association *new_asoc;
Xin Long01a992b2017-06-30 11:52:22 +0800664 struct sctp_init_chunk *peer_init;
Xin Long172a1592017-08-11 10:23:57 +0800665 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 struct sctp_chunk *err_chk_p;
Xin Long172a1592017-08-11 10:23:57 +0800667 struct sctp_chunk *repl;
Vlad Yasevich609ee462007-08-31 03:10:59 +0900668 struct sock *sk;
Xin Long172a1592017-08-11 10:23:57 +0800669 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 /* If the packet is an OOTB packet which is temporarily on the
672 * control endpoint, respond with an ABORT.
673 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +0000674 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000675 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000676 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 /* Make sure that the COOKIE_ECHO chunk has a valid length.
680 * In this case, we check that we have enough for at least a
681 * chunk header. More detailed verification is done
682 * in sctp_unpack_cookie().
683 */
Xin Long922dbc52017-06-30 11:52:13 +0800684 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000685 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Vlad Yasevich609ee462007-08-31 03:10:59 +0900687 /* If the endpoint is not listening or if the number of associations
688 * on the TCP-style socket exceed the max backlog, respond with an
689 * ABORT.
690 */
691 sk = ep->base.sk;
692 if (!sctp_sstate(sk, LISTENING) ||
693 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000694 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Vlad Yasevich609ee462007-08-31 03:10:59 +0900695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /* "Decode" the chunk. We have no optional parameters so we
697 * are in good shape.
698 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900699 chunk->subh.cookie_hdr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -0700701 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +0800702 sizeof(struct sctp_chunkhdr)))
Sridhar Samudrala62b08082006-05-05 17:04:43 -0700703 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
706 * "Z" will reply with a COOKIE ACK chunk after building a TCB
707 * and moving to the ESTABLISHED state.
708 */
709 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
710 &err_chk_p);
711
712 /* FIXME:
713 * If the re-build failed, what is the proper error path
714 * from here?
715 *
716 * [We should abort the association. --piggy]
717 */
718 if (!new_asoc) {
719 /* FIXME: Several errors are possible. A bad cookie should
720 * be silently discarded, but think about logging it too.
721 */
722 switch (error) {
723 case -SCTP_IERROR_NOMEM:
724 goto nomem;
725
726 case -SCTP_IERROR_STALE_COOKIE:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000727 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 err_chk_p);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000729 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 case -SCTP_IERROR_BAD_SIG:
732 default:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000733 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700738 /* Delay state machine commands until later.
739 *
740 * Re-build the bind address for the association is done in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 * the sctp_unpack_cookie() already.
742 */
743 /* This is a brand-new association, so these are not yet side
744 * effects--it is safe to run them here.
745 */
746 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
747
Wei Yongjunde6becdc02011-04-19 21:30:51 +0000748 if (!sctp_process_init(new_asoc, chunk,
Al Viro6a1e5f32006-11-20 17:12:25 -0800749 &chunk->subh.cookie_hdr->c.peer_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 peer_init, GFP_ATOMIC))
751 goto nomem_init;
752
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700753 /* SCTP-AUTH: Now that we've populate required fields in
754 * sctp_process_init, set up the assocaition shared keys as
755 * necessary so that we can potentially authenticate the ACK
756 */
757 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
758 if (error)
759 goto nomem_init;
760
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700761 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
762 * is supposed to be authenticated and we have to do delayed
763 * authentication. We've just recreated the association using
764 * the information in the cookie and now it's much easier to
765 * do the authentication.
766 */
767 if (chunk->auth_chunk) {
768 struct sctp_chunk auth;
Xin Long4785c7a2017-08-05 19:59:58 +0800769 enum sctp_ierror ret;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700770
Daniel Borkmannec0223e2014-03-03 17:23:04 +0100771 /* Make sure that we and the peer are AUTH capable */
772 if (!net->sctp.auth_enable || !new_asoc->peer.auth_capable) {
Daniel Borkmannec0223e2014-03-03 17:23:04 +0100773 sctp_association_free(new_asoc);
774 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
775 }
776
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700777 /* set-up our fake chunk so that we can process it */
778 auth.skb = chunk->auth_chunk;
779 auth.asoc = chunk->asoc;
780 auth.sctp_hdr = chunk->sctp_hdr;
Xin Long922dbc52017-06-30 11:52:13 +0800781 auth.chunk_hdr = (struct sctp_chunkhdr *)
782 skb_push(chunk->auth_chunk,
783 sizeof(struct sctp_chunkhdr));
784 skb_pull(chunk->auth_chunk, sizeof(struct sctp_chunkhdr));
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700785 auth.transport = chunk->transport;
786
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000787 ret = sctp_sf_authenticate(net, ep, new_asoc, type, &auth);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700788 if (ret != SCTP_IERROR_NO_ERROR) {
789 sctp_association_free(new_asoc);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000790 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700791 }
792 }
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 repl = sctp_make_cookie_ack(new_asoc, chunk);
795 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700796 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 /* RFC 2960 5.1 Normal Establishment of an Association
799 *
800 * D) IMPLEMENTATION NOTE: An implementation may choose to
801 * send the Communication Up notification to the SCTP user
802 * upon reception of a valid COOKIE ECHO chunk.
803 */
804 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
805 new_asoc->c.sinit_num_ostreams,
806 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700807 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (!ev)
809 goto nomem_ev;
810
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900811 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800812 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800814 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800816 if (new_asoc->peer.adaptation_ind) {
817 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700819 if (!ai_ev)
820 goto nomem_aiev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
822
Xin Long30f6ebf2018-03-14 19:05:34 +0800823 if (!new_asoc->peer.auth_capable) {
824 auth_ev = sctp_ulpevent_make_authkey(new_asoc, 0,
825 SCTP_AUTH_NO_AUTH,
826 GFP_ATOMIC);
827 if (!auth_ev)
828 goto nomem_authev;
829 }
830
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700831 /* Add all the state machine commands now since we've created
832 * everything. This way we don't introduce memory corruptions
833 * during side-effect processing and correclty count established
834 * associations.
835 */
836 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
837 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
838 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000839 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
840 SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700841 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
842
Neil Horman9f70f462013-12-10 06:48:15 -0500843 if (new_asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700844 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
845 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
846
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700847 /* This will send the COOKIE ACK */
848 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
849
850 /* Queue the ASSOC_CHANGE event */
851 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
852
853 /* Send up the Adaptation Layer Indication event */
854 if (ai_ev)
855 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
856 SCTP_ULPEVENT(ai_ev));
857
Xin Long30f6ebf2018-03-14 19:05:34 +0800858 if (auth_ev)
859 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
860 SCTP_ULPEVENT(auth_ev));
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return SCTP_DISPOSITION_CONSUME;
863
Xin Long30f6ebf2018-03-14 19:05:34 +0800864nomem_authev:
865 sctp_ulpevent_free(ai_ev);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700866nomem_aiev:
867 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868nomem_ev:
869 sctp_chunk_free(repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870nomem_init:
871 sctp_association_free(new_asoc);
872nomem:
873 return SCTP_DISPOSITION_NOMEM;
874}
875
876/*
877 * Respond to a normal COOKIE ACK chunk.
Marcelo Ricardo Leitnerb52effd2015-07-17 13:50:21 -0300878 * We are the side that is asking for an association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 *
880 * RFC 2960 5.1 Normal Establishment of an Association
881 *
882 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
883 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
884 * timer. It may also notify its ULP about the successful
885 * establishment of the association with a Communication Up
886 * notification (see Section 10).
887 *
888 * Verification Tag:
889 * Inputs
890 * (endpoint, asoc, chunk)
891 *
892 * Outputs
893 * (asoc, reply_msg, msg_up, timers, counters)
894 *
895 * The return value is the disposition of the chunk.
896 */
Xin Long172a1592017-08-11 10:23:57 +0800897enum sctp_disposition sctp_sf_do_5_1E_ca(struct net *net,
898 const struct sctp_endpoint *ep,
899 const struct sctp_association *asoc,
900 const union sctp_subtype type,
901 void *arg,
902 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
904 struct sctp_chunk *chunk = arg;
905 struct sctp_ulpevent *ev;
906
907 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000908 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 /* Verify that the chunk length for the COOKIE-ACK is OK.
911 * If we don't do this, any bundled chunks may be junked.
912 */
Xin Long922dbc52017-06-30 11:52:13 +0800913 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000914 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 commands);
916
917 /* Reset init error count upon receipt of COOKIE-ACK,
918 * to avoid problems with the managemement of this
919 * counter in stale cookie situations when a transition back
920 * from the COOKIE-ECHOED state to the COOKIE-WAIT
921 * state is performed.
922 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700923 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 /* RFC 2960 5.1 Normal Establishment of an Association
926 *
927 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
928 * from the COOKIE-ECHOED state to the ESTABLISHED state,
929 * stopping the T1-cookie timer.
930 */
931 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
932 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
933 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
934 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000935 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
936 SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
Neil Horman9f70f462013-12-10 06:48:15 -0500938 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
940 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 /* It may also notify its ULP about the successful
943 * establishment of the association with a Communication Up
944 * notification (see Section 10).
945 */
946 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
947 0, asoc->c.sinit_num_ostreams,
948 asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700949 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 if (!ev)
952 goto nomem;
953
954 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
955
956 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800957 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800959 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800961 if (asoc->peer.adaptation_ind) {
962 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (!ev)
964 goto nomem;
965
966 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
967 SCTP_ULPEVENT(ev));
968 }
969
Xin Long30f6ebf2018-03-14 19:05:34 +0800970 if (!asoc->peer.auth_capable) {
971 ev = sctp_ulpevent_make_authkey(asoc, 0, SCTP_AUTH_NO_AUTH,
972 GFP_ATOMIC);
973 if (!ev)
974 goto nomem;
975 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
976 SCTP_ULPEVENT(ev));
977 }
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return SCTP_DISPOSITION_CONSUME;
980nomem:
981 return SCTP_DISPOSITION_NOMEM;
982}
983
984/* Generate and sendout a heartbeat packet. */
Xin Long172a1592017-08-11 10:23:57 +0800985static enum sctp_disposition sctp_sf_heartbeat(
986 const struct sctp_endpoint *ep,
987 const struct sctp_association *asoc,
988 const union sctp_subtype type,
989 void *arg,
990 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
992 struct sctp_transport *transport = (struct sctp_transport *) arg;
993 struct sctp_chunk *reply;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 /* Send a heartbeat to our peer. */
Wei Yongjun92c73af2011-04-19 21:31:47 +0000996 reply = sctp_make_heartbeat(asoc, transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (!reply)
998 return SCTP_DISPOSITION_NOMEM;
999
1000 /* Set rto_pending indicating that an RTT measurement
1001 * is started with this heartbeat chunk.
1002 */
1003 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
1004 SCTP_TRANSPORT(transport));
1005
1006 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1007 return SCTP_DISPOSITION_CONSUME;
1008}
1009
1010/* Generate a HEARTBEAT packet on the given transport. */
Xin Long172a1592017-08-11 10:23:57 +08001011enum sctp_disposition sctp_sf_sendbeat_8_3(struct net *net,
1012 const struct sctp_endpoint *ep,
1013 const struct sctp_association *asoc,
1014 const union sctp_subtype type,
1015 void *arg,
1016 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
1018 struct sctp_transport *transport = (struct sctp_transport *) arg;
1019
Vlad Yasevichb9f84782009-08-26 09:36:25 -04001020 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07001021 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
1022 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
1024 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08001025 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00001026 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
1027 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return SCTP_DISPOSITION_DELETE_TCB;
1029 }
1030
1031 /* Section 3.3.5.
1032 * The Sender-specific Heartbeat Info field should normally include
1033 * information about the sender's current time when this HEARTBEAT
1034 * chunk is sent and the destination transport address to which this
1035 * HEARTBEAT is sent (see Section 8.3).
1036 */
1037
Frank Filz52ccb8e2005-12-22 11:36:46 -08001038 if (transport->param_flags & SPP_HB_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 if (SCTP_DISPOSITION_NOMEM ==
1040 sctp_sf_heartbeat(ep, asoc, type, arg,
1041 commands))
1042 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevich245cba72009-11-23 15:53:58 -05001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 /* Set transport error counter and association error counter
1045 * when sending heartbeat.
1046 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00001047 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 SCTP_TRANSPORT(transport));
1049 }
Vlad Yasevich245cba72009-11-23 15:53:58 -05001050 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1051 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1053 SCTP_TRANSPORT(transport));
1054
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001055 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
Xin Long7b9438d2017-01-18 00:44:43 +08001058/* resend asoc strreset_chunk. */
Xin Long172a1592017-08-11 10:23:57 +08001059enum sctp_disposition sctp_sf_send_reconf(struct net *net,
1060 const struct sctp_endpoint *ep,
1061 const struct sctp_association *asoc,
1062 const union sctp_subtype type,
1063 void *arg,
1064 struct sctp_cmd_seq *commands)
Xin Long7b9438d2017-01-18 00:44:43 +08001065{
1066 struct sctp_transport *transport = arg;
1067
1068 if (asoc->overall_error_count >= asoc->max_retrans) {
1069 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
1070 SCTP_ERROR(ETIMEDOUT));
1071 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
1072 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
1073 SCTP_PERR(SCTP_ERROR_NO_ERROR));
1074 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
1075 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
1076 return SCTP_DISPOSITION_DELETE_TCB;
1077 }
1078
1079 sctp_chunk_hold(asoc->strreset_chunk);
1080 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1081 SCTP_CHUNK(asoc->strreset_chunk));
1082 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
1083
1084 return SCTP_DISPOSITION_CONSUME;
1085}
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087/*
1088 * Process an heartbeat request.
1089 *
1090 * Section: 8.3 Path Heartbeat
1091 * The receiver of the HEARTBEAT should immediately respond with a
1092 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1093 * from the received HEARTBEAT chunk.
1094 *
1095 * Verification Tag: 8.5 Verification Tag [Normal verification]
1096 * When receiving an SCTP packet, the endpoint MUST ensure that the
1097 * value in the Verification Tag field of the received SCTP packet
1098 * matches its own Tag. If the received Verification Tag value does not
1099 * match the receiver's own tag value, the receiver shall silently
1100 * discard the packet and shall not process it any further except for
1101 * those cases listed in Section 8.5.1 below.
1102 *
1103 * Inputs
1104 * (endpoint, asoc, chunk)
1105 *
1106 * Outputs
1107 * (asoc, reply_msg, msg_up, timers, counters)
1108 *
1109 * The return value is the disposition of the chunk.
1110 */
Xin Long172a1592017-08-11 10:23:57 +08001111enum sctp_disposition sctp_sf_beat_8_3(struct net *net,
1112 const struct sctp_endpoint *ep,
1113 const struct sctp_association *asoc,
1114 const union sctp_subtype type,
1115 void *arg, struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Xin Long3c918702017-06-30 11:52:16 +08001117 struct sctp_paramhdr *param_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 struct sctp_chunk *chunk = arg;
1119 struct sctp_chunk *reply;
1120 size_t paylen = 0;
1121
1122 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001123 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 /* Make sure that the HEARTBEAT chunk has a valid length. */
Xin Long38c00f72017-07-23 09:34:35 +08001126 if (!sctp_chunk_length_valid(chunk,
1127 sizeof(struct sctp_heartbeat_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001128 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 commands);
1130
1131 /* 8.3 The receiver of the HEARTBEAT should immediately
1132 * respond with a HEARTBEAT ACK that contains the Heartbeat
1133 * Information field copied from the received HEARTBEAT chunk.
1134 */
Xin Long4d2dcdf2017-07-23 09:34:34 +08001135 chunk->subh.hb_hdr = (struct sctp_heartbeathdr *)chunk->skb->data;
Xin Long3c918702017-06-30 11:52:16 +08001136 param_hdr = (struct sctp_paramhdr *)chunk->subh.hb_hdr;
Xin Long922dbc52017-06-30 11:52:13 +08001137 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(struct sctp_chunkhdr);
Thomas Graf06a31e22012-11-30 02:16:27 +00001138
1139 if (ntohs(param_hdr->length) > paylen)
1140 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
1141 param_hdr, commands);
1142
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001143 if (!pskb_pull(chunk->skb, paylen))
1144 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Thomas Graf06a31e22012-11-30 02:16:27 +00001146 reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (!reply)
1148 goto nomem;
1149
1150 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1151 return SCTP_DISPOSITION_CONSUME;
1152
1153nomem:
1154 return SCTP_DISPOSITION_NOMEM;
1155}
1156
1157/*
1158 * Process the returning HEARTBEAT ACK.
1159 *
1160 * Section: 8.3 Path Heartbeat
1161 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1162 * should clear the error counter of the destination transport
1163 * address to which the HEARTBEAT was sent, and mark the destination
1164 * transport address as active if it is not so marked. The endpoint may
1165 * optionally report to the upper layer when an inactive destination
1166 * address is marked as active due to the reception of the latest
1167 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1168 * clear the association overall error count as well (as defined
1169 * in section 8.1).
1170 *
1171 * The receiver of the HEARTBEAT ACK should also perform an RTT
1172 * measurement for that destination transport address using the time
1173 * value carried in the HEARTBEAT ACK chunk.
1174 *
1175 * Verification Tag: 8.5 Verification Tag [Normal verification]
1176 *
1177 * Inputs
1178 * (endpoint, asoc, chunk)
1179 *
1180 * Outputs
1181 * (asoc, reply_msg, msg_up, timers, counters)
1182 *
1183 * The return value is the disposition of the chunk.
1184 */
Xin Long172a1592017-08-11 10:23:57 +08001185enum sctp_disposition sctp_sf_backbeat_8_3(struct net *net,
1186 const struct sctp_endpoint *ep,
1187 const struct sctp_association *asoc,
1188 const union sctp_subtype type,
1189 void *arg,
1190 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
Xin Longedf903f2017-08-11 10:23:46 +08001192 struct sctp_sender_hb_info *hbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 struct sctp_transport *link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 unsigned long max_interval;
Xin Longedf903f2017-08-11 10:23:46 +08001196 union sctp_addr from_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001199 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
Xin Long922dbc52017-06-30 11:52:13 +08001202 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr) +
Xin Longedf903f2017-08-11 10:23:46 +08001203 sizeof(*hbinfo)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001204 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 commands);
1206
Xin Longedf903f2017-08-11 10:23:46 +08001207 hbinfo = (struct sctp_sender_hb_info *)chunk->skb->data;
Vladislav Yasevicha6012662006-05-19 14:25:53 -07001208 /* Make sure that the length of the parameter is what we expect */
Xin Longedf903f2017-08-11 10:23:46 +08001209 if (ntohs(hbinfo->param_hdr.length) != sizeof(*hbinfo))
Vladislav Yasevicha6012662006-05-19 14:25:53 -07001210 return SCTP_DISPOSITION_DISCARD;
Vladislav Yasevicha6012662006-05-19 14:25:53 -07001211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 from_addr = hbinfo->daddr;
Al Viro63de08f2006-11-20 17:07:25 -08001213 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 /* This should never happen, but lets log it if so. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001216 if (unlikely(!link)) {
1217 if (from_addr.sa.sa_family == AF_INET6) {
Joe Perchese87cc472012-05-13 21:56:26 +00001218 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1219 __func__,
1220 asoc,
1221 &from_addr.v6.sin6_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001222 } else {
Joe Perchese87cc472012-05-13 21:56:26 +00001223 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1224 __func__,
1225 asoc,
1226 &from_addr.v4.sin_addr.s_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 return SCTP_DISPOSITION_DISCARD;
1229 }
1230
Sridhar Samudralaad8fec12006-07-21 14:48:50 -07001231 /* Validate the 64-bit random nonce. */
1232 if (hbinfo->hb_nonce != link->hb_nonce)
1233 return SCTP_DISPOSITION_DISCARD;
1234
Frank Filz52ccb8e2005-12-22 11:36:46 -08001235 max_interval = link->hbinterval + link->rto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 /* Check if the timestamp looks valid. */
1238 if (time_after(hbinfo->sent_at, jiffies) ||
1239 time_after(jiffies, hbinfo->sent_at + max_interval)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001240 pr_debug("%s: HEARTBEAT ACK with invalid timestamp received "
1241 "for transport:%p\n", __func__, link);
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return SCTP_DISPOSITION_DISCARD;
1244 }
1245
1246 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1247 * the HEARTBEAT should clear the error counter of the
1248 * destination transport address to which the HEARTBEAT was
1249 * sent and mark the destination transport address as active if
1250 * it is not so marked.
1251 */
1252 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1253
1254 return SCTP_DISPOSITION_CONSUME;
1255}
1256
1257/* Helper function to send out an abort for the restart
1258 * condition.
1259 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001260static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 struct sctp_chunk *init,
Xin Longa85bbeb2017-08-11 10:23:52 +08001262 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
Xin Long172a1592017-08-11 10:23:57 +08001264 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 union sctp_addr_param *addrparm;
1266 struct sctp_errhdr *errhdr;
Xin Longd8238d92017-08-03 15:42:11 +08001267 char buffer[sizeof(*errhdr) + sizeof(*addrparm)];
Xin Long172a1592017-08-11 10:23:57 +08001268 struct sctp_endpoint *ep;
1269 struct sctp_packet *pkt;
1270 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 /* Build the error on the stack. We are way to malloc crazy
1273 * throughout the code today.
1274 */
1275 errhdr = (struct sctp_errhdr *)buffer;
1276 addrparm = (union sctp_addr_param *)errhdr->variable;
1277
1278 /* Copy into a parm format. */
1279 len = af->to_addr_param(ssa, addrparm);
Xin Longd8238d92017-08-03 15:42:11 +08001280 len += sizeof(*errhdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 errhdr->cause = SCTP_ERROR_RESTART;
1283 errhdr->length = htons(len);
1284
1285 /* Assign to the control socket. */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001286 ep = sctp_sk(net->sctp.ctl_sock)->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 /* Association is NULL since this may be a restart attack and we
1289 * want to send back the attacker's vtag.
1290 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001291 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 if (!pkt)
1294 goto out;
1295 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1296
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00001297 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 /* Discard the rest of the inbound packet. */
1300 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1301
1302out:
1303 /* Even if there is no memory, treat as a failure so
1304 * the packet will get dropped.
1305 */
1306 return 0;
1307}
1308
Joe Perches123031c2010-09-08 11:04:21 +00001309static bool list_has_sctp_addr(const struct list_head *list,
1310 union sctp_addr *ipaddr)
1311{
1312 struct sctp_transport *addr;
1313
1314 list_for_each_entry(addr, list, transports) {
1315 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1316 return true;
1317 }
1318
1319 return false;
1320}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321/* A restart is occurring, check to make sure no new addresses
1322 * are being added as we may be under a takeover attack.
1323 */
1324static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1325 const struct sctp_association *asoc,
1326 struct sctp_chunk *init,
Xin Longa85bbeb2017-08-11 10:23:52 +08001327 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001329 struct net *net = sock_net(new_asoc->base.sk);
Joe Perches123031c2010-09-08 11:04:21 +00001330 struct sctp_transport *new_addr;
1331 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Joe Perches123031c2010-09-08 11:04:21 +00001333 /* Implementor's Guide - Section 5.2.2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 * ...
1335 * Before responding the endpoint MUST check to see if the
1336 * unexpected INIT adds new addresses to the association. If new
1337 * addresses are added to the association, the endpoint MUST respond
1338 * with an ABORT..
1339 */
1340
1341 /* Search through all current addresses and make sure
1342 * we aren't adding any new ones.
1343 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001344 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
Joe Perches123031c2010-09-08 11:04:21 +00001345 transports) {
1346 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1347 &new_addr->ipaddr)) {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001348 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
Joe Perches123031c2010-09-08 11:04:21 +00001349 commands);
1350 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 break;
Joe Perches123031c2010-09-08 11:04:21 +00001352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
1354
1355 /* Return success if all addresses were found. */
Joe Perches123031c2010-09-08 11:04:21 +00001356 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358
1359/* Populate the verification/tie tags based on overlapping INIT
1360 * scenario.
1361 *
1362 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1363 */
1364static void sctp_tietags_populate(struct sctp_association *new_asoc,
1365 const struct sctp_association *asoc)
1366{
1367 switch (asoc->state) {
1368
1369 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1370
1371 case SCTP_STATE_COOKIE_WAIT:
1372 new_asoc->c.my_vtag = asoc->c.my_vtag;
1373 new_asoc->c.my_ttag = asoc->c.my_vtag;
1374 new_asoc->c.peer_ttag = 0;
1375 break;
1376
1377 case SCTP_STATE_COOKIE_ECHOED:
1378 new_asoc->c.my_vtag = asoc->c.my_vtag;
1379 new_asoc->c.my_ttag = asoc->c.my_vtag;
1380 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1381 break;
1382
1383 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1384 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1385 */
1386 default:
1387 new_asoc->c.my_ttag = asoc->c.my_vtag;
1388 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1389 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 /* Other parameters for the endpoint SHOULD be copied from the
1393 * existing parameters of the association (e.g. number of
1394 * outbound streams) into the INIT ACK and cookie.
1395 */
1396 new_asoc->rwnd = asoc->rwnd;
1397 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1398 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1399 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1400}
1401
1402/*
1403 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1404 * handling action.
1405 *
1406 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1407 *
1408 * Returns value representing action to be taken. These action values
1409 * correspond to Action/Description values in RFC 2960, Table 2.
1410 */
1411static char sctp_tietags_compare(struct sctp_association *new_asoc,
1412 const struct sctp_association *asoc)
1413{
1414 /* In this case, the peer may have restarted. */
1415 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1416 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1417 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1418 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1419 return 'A';
1420
1421 /* Collision case B. */
1422 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1423 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1424 (0 == asoc->c.peer_vtag))) {
1425 return 'B';
1426 }
1427
1428 /* Collision case D. */
1429 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1430 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1431 return 'D';
1432
1433 /* Collision case C. */
1434 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1435 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1436 (0 == new_asoc->c.my_ttag) &&
1437 (0 == new_asoc->c.peer_ttag))
1438 return 'C';
1439
1440 /* No match to any of the special cases; discard this packet. */
1441 return 'E';
1442}
1443
1444/* Common helper routine for both duplicate and simulataneous INIT
1445 * chunk handling.
1446 */
Xin Long172a1592017-08-11 10:23:57 +08001447static enum sctp_disposition sctp_sf_do_unexpected_init(
1448 struct net *net,
1449 const struct sctp_endpoint *ep,
1450 const struct sctp_association *asoc,
1451 const union sctp_subtype type,
1452 void *arg,
1453 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
Xin Long62e6b7e2017-07-23 09:34:28 +08001455 struct sctp_chunk *chunk = arg, *repl, *err_chunk;
1456 struct sctp_unrecognized_param *unk_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 struct sctp_association *new_asoc;
Xin Long172a1592017-08-11 10:23:57 +08001458 enum sctp_disposition retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 struct sctp_packet *packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 int len;
1461
1462 /* 6.10 Bundling
1463 * An endpoint MUST NOT bundle INIT, INIT ACK or
1464 * SHUTDOWN COMPLETE with any other chunks.
1465 *
1466 * IG Section 2.11.2
1467 * Furthermore, we require that the receiver of an INIT chunk MUST
1468 * enforce these rules by silently discarding an arriving packet
1469 * with an INIT chunk that is bundled with other chunks.
1470 */
1471 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001472 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001475 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 */
1477 if (chunk->sctp_hdr->vtag != 0)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001478 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 /* Make sure that the INIT chunk has a valid length.
1481 * In this case, we generate a protocol violation since we have
1482 * an association established.
1483 */
Xin Long01a992b2017-06-30 11:52:22 +08001484 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001485 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 commands);
1487 /* Grab the INIT header. */
Xin Long4ae70c02017-06-30 11:52:21 +08001488 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 /* Tag the variable length parameters. */
Xin Long4ae70c02017-06-30 11:52:21 +08001491 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 /* Verify the INIT chunk before processing it. */
1494 err_chunk = NULL;
Vlad Yasevichb14878c2014-04-17 17:26:50 +02001495 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Xin Long01a992b2017-06-30 11:52:22 +08001496 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 &err_chunk)) {
1498 /* This chunk contains fatal error. It is to be discarded.
1499 * Send an ABORT, with causes if there is any.
1500 */
1501 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001502 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 (__u8 *)(err_chunk->chunk_hdr) +
Xin Long922dbc52017-06-30 11:52:13 +08001504 sizeof(struct sctp_chunkhdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 ntohs(err_chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +08001506 sizeof(struct sctp_chunkhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508 if (packet) {
1509 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1510 SCTP_PACKET(packet));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001511 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 retval = SCTP_DISPOSITION_CONSUME;
1513 } else {
1514 retval = SCTP_DISPOSITION_NOMEM;
1515 }
1516 goto cleanup;
1517 } else {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001518 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 commands);
1520 }
1521 }
1522
1523 /*
1524 * Other parameters for the endpoint SHOULD be copied from the
1525 * existing parameters of the association (e.g. number of
1526 * outbound streams) into the INIT ACK and cookie.
1527 * FIXME: We are copying parameters from the endpoint not the
1528 * association.
1529 */
1530 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1531 if (!new_asoc)
1532 goto nomem;
1533
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001534 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1535 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1536 goto nomem;
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 /* In the outbound INIT ACK the endpoint MUST copy its current
1539 * Verification Tag and Peers Verification tag into a reserved
1540 * place (local tie-tag and per tie-tag) within the state cookie.
1541 */
Wei Yongjunde6becdc02011-04-19 21:30:51 +00001542 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Xin Long01a992b2017-06-30 11:52:22 +08001543 (struct sctp_init_chunk *)chunk->chunk_hdr,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001544 GFP_ATOMIC))
1545 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547 /* Make sure no new addresses are being added during the
1548 * restart. Do not do this check for COOKIE-WAIT state,
1549 * since there are no peer addresses to check against.
1550 * Upon return an ABORT will have been sent if needed.
1551 */
1552 if (!sctp_state(asoc, COOKIE_WAIT)) {
1553 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1554 commands)) {
1555 retval = SCTP_DISPOSITION_CONSUME;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001556 goto nomem_retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
1558 }
1559
1560 sctp_tietags_populate(new_asoc, asoc);
1561
1562 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1563
1564 /* If there are errors need to be reported for unknown parameters,
1565 * make sure to reserve enough room in the INIT ACK for them.
1566 */
1567 len = 0;
1568 if (err_chunk) {
1569 len = ntohs(err_chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +08001570 sizeof(struct sctp_chunkhdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1574 if (!repl)
1575 goto nomem;
1576
1577 /* If there are errors need to be reported for unknown parameters,
1578 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1579 * parameter.
1580 */
1581 if (err_chunk) {
1582 /* Get the "Unrecognized parameter" parameter(s) out of the
1583 * ERROR chunk generated by sctp_verify_init(). Since the
1584 * error cause code for "unknown parameter" and the
1585 * "Unrecognized parameter" type is the same, we can
1586 * construct the parameters in INIT ACK by copying the
1587 * ERROR causes over.
1588 */
Xin Long62e6b7e2017-07-23 09:34:28 +08001589 unk_param = (struct sctp_unrecognized_param *)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 ((__u8 *)(err_chunk->chunk_hdr) +
Xin Long922dbc52017-06-30 11:52:13 +08001591 sizeof(struct sctp_chunkhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 /* Replace the cause code with the "Unrecognized parameter"
1593 * parameter type.
1594 */
1595 sctp_addto_chunk(repl, len, unk_param);
1596 }
1597
1598 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1599 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1600
1601 /*
1602 * Note: After sending out INIT ACK with the State Cookie parameter,
1603 * "Z" MUST NOT allocate any resources for this new association.
1604 * Otherwise, "Z" will be vulnerable to resource attacks.
1605 */
1606 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1607 retval = SCTP_DISPOSITION_CONSUME;
1608
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001609 return retval;
1610
1611nomem:
1612 retval = SCTP_DISPOSITION_NOMEM;
1613nomem_retval:
1614 if (new_asoc)
1615 sctp_association_free(new_asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616cleanup:
1617 if (err_chunk)
1618 sctp_chunk_free(err_chunk);
1619 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620}
1621
1622/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001623 * Handle simultaneous INIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 * This means we started an INIT and then we got an INIT request from
1625 * our peer.
1626 *
1627 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1628 * This usually indicates an initialization collision, i.e., each
1629 * endpoint is attempting, at about the same time, to establish an
1630 * association with the other endpoint.
1631 *
1632 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1633 * endpoint MUST respond with an INIT ACK using the same parameters it
1634 * sent in its original INIT chunk (including its Verification Tag,
1635 * unchanged). These original parameters are combined with those from the
1636 * newly received INIT chunk. The endpoint shall also generate a State
1637 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1638 * INIT to calculate the State Cookie.
1639 *
1640 * After that, the endpoint MUST NOT change its state, the T1-init
1641 * timer shall be left running and the corresponding TCB MUST NOT be
1642 * destroyed. The normal procedures for handling State Cookies when
1643 * a TCB exists will resolve the duplicate INITs to a single association.
1644 *
1645 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1646 * its Tie-Tags with the Tag information of itself and its peer (see
1647 * section 5.2.2 for a description of the Tie-Tags).
1648 *
1649 * Verification Tag: Not explicit, but an INIT can not have a valid
1650 * verification tag, so we skip the check.
1651 *
1652 * Inputs
1653 * (endpoint, asoc, chunk)
1654 *
1655 * Outputs
1656 * (asoc, reply_msg, msg_up, timers, counters)
1657 *
1658 * The return value is the disposition of the chunk.
1659 */
Xin Long172a1592017-08-11 10:23:57 +08001660enum sctp_disposition sctp_sf_do_5_2_1_siminit(
1661 struct net *net,
1662 const struct sctp_endpoint *ep,
1663 const struct sctp_association *asoc,
1664 const union sctp_subtype type,
1665 void *arg,
1666 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
1668 /* Call helper to do the real work for both simulataneous and
1669 * duplicate INIT chunk handling.
1670 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001671 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672}
1673
1674/*
1675 * Handle duplicated INIT messages. These are usually delayed
1676 * restransmissions.
1677 *
1678 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1679 * COOKIE-ECHOED and COOKIE-WAIT
1680 *
1681 * Unless otherwise stated, upon reception of an unexpected INIT for
1682 * this association, the endpoint shall generate an INIT ACK with a
1683 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1684 * current Verification Tag and peer's Verification Tag into a reserved
1685 * place within the state cookie. We shall refer to these locations as
1686 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1687 * containing this INIT ACK MUST carry a Verification Tag value equal to
1688 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1689 * MUST contain a new Initiation Tag (randomly generated see Section
1690 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1691 * existing parameters of the association (e.g. number of outbound
1692 * streams) into the INIT ACK and cookie.
1693 *
1694 * After sending out the INIT ACK, the endpoint shall take no further
1695 * actions, i.e., the existing association, including its current state,
1696 * and the corresponding TCB MUST NOT be changed.
1697 *
1698 * Note: Only when a TCB exists and the association is not in a COOKIE-
1699 * WAIT state are the Tie-Tags populated. For a normal association INIT
1700 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1701 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1702 * State Cookie are populated as specified in section 5.2.1.
1703 *
1704 * Verification Tag: Not specified, but an INIT has no way of knowing
1705 * what the verification tag could be, so we ignore it.
1706 *
1707 * Inputs
1708 * (endpoint, asoc, chunk)
1709 *
1710 * Outputs
1711 * (asoc, reply_msg, msg_up, timers, counters)
1712 *
1713 * The return value is the disposition of the chunk.
1714 */
Xin Long172a1592017-08-11 10:23:57 +08001715enum sctp_disposition sctp_sf_do_5_2_2_dupinit(
1716 struct net *net,
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001717 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08001719 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 void *arg,
Xin Longa85bbeb2017-08-11 10:23:52 +08001721 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
1723 /* Call helper to do the real work for both simulataneous and
1724 * duplicate INIT chunk handling.
1725 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001726 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
1728
1729
Vlad Yasevich610ab732007-01-15 19:18:30 -08001730/*
1731 * Unexpected INIT-ACK handler.
1732 *
1733 * Section 5.2.3
1734 * If an INIT ACK received by an endpoint in any state other than the
1735 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1736 * An unexpected INIT ACK usually indicates the processing of an old or
1737 * duplicated INIT chunk.
1738*/
Xin Long172a1592017-08-11 10:23:57 +08001739enum sctp_disposition sctp_sf_do_5_2_3_initack(
1740 struct net *net,
1741 const struct sctp_endpoint *ep,
1742 const struct sctp_association *asoc,
1743 const union sctp_subtype type,
1744 void *arg,
1745 struct sctp_cmd_seq *commands)
Vlad Yasevich610ab732007-01-15 19:18:30 -08001746{
1747 /* Per the above section, we'll discard the chunk if we have an
1748 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1749 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001750 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001751 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001752 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001753 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001754}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756/* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1757 *
1758 * Section 5.2.4
1759 * A) In this case, the peer may have restarted.
1760 */
Xin Long172a1592017-08-11 10:23:57 +08001761static enum sctp_disposition sctp_sf_do_dupcook_a(
1762 struct net *net,
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001763 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 const struct sctp_association *asoc,
1765 struct sctp_chunk *chunk,
Xin Longa85bbeb2017-08-11 10:23:52 +08001766 struct sctp_cmd_seq *commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 struct sctp_association *new_asoc)
1768{
Xin Long01a992b2017-06-30 11:52:22 +08001769 struct sctp_init_chunk *peer_init;
Xin Long172a1592017-08-11 10:23:57 +08001770 enum sctp_disposition disposition;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 struct sctp_ulpevent *ev;
1772 struct sctp_chunk *repl;
1773 struct sctp_chunk *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
1775 /* new_asoc is a brand-new association, so these are not yet
1776 * side effects--it is safe to run them here.
1777 */
1778 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1779
Wei Yongjunde6becdc02011-04-19 21:30:51 +00001780 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 GFP_ATOMIC))
1782 goto nomem;
1783
1784 /* Make sure no new addresses are being added during the
1785 * restart. Though this is a pretty complicated attack
1786 * since you'd have to get inside the cookie.
1787 */
1788 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1789 return SCTP_DISPOSITION_CONSUME;
1790 }
1791
1792 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1793 * the peer has restarted (Action A), it MUST NOT setup a new
1794 * association but instead resend the SHUTDOWN ACK and send an ERROR
1795 * chunk with a "Cookie Received while Shutting Down" error cause to
1796 * its peer.
1797 */
1798 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001799 disposition = sctp_sf_do_9_2_reshutack(net, ep, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1801 chunk, commands);
1802 if (SCTP_DISPOSITION_NOMEM == disposition)
1803 goto nomem;
1804
1805 err = sctp_make_op_error(asoc, chunk,
1806 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001807 NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 if (err)
1809 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1810 SCTP_CHUNK(err));
1811
1812 return SCTP_DISPOSITION_CONSUME;
1813 }
1814
Wei Yongjuna000c012011-05-29 23:23:36 +00001815 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1816 * data. Consider the optional choice of resending of this data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 */
Wei Yongjuna000c012011-05-29 23:23:36 +00001818 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1819 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1820 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1822
Wei Yongjuna000c012011-05-29 23:23:36 +00001823 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1824 * and ASCONF-ACK cache.
1825 */
1826 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1827 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1828 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 repl = sctp_make_cookie_ack(new_asoc, chunk);
1831 if (!repl)
1832 goto nomem;
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 /* Report association restart to upper layer. */
1835 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1836 new_asoc->c.sinit_num_ostreams,
1837 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07001838 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 if (!ev)
1840 goto nomem_ev;
1841
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001842 /* Update the content of current association. */
1843 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
Vlad Yasevichbdf6fa52014-10-03 18:16:20 -04001845 if (sctp_state(asoc, SHUTDOWN_PENDING) &&
1846 (sctp_sstate(asoc->base.sk, CLOSING) ||
1847 sock_flag(asoc->base.sk, SOCK_DEAD))) {
1848 /* if were currently in SHUTDOWN_PENDING, but the socket
1849 * has been closed by user, don't transition to ESTABLISHED.
1850 * Instead trigger SHUTDOWN bundled with COOKIE_ACK.
1851 */
1852 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1853 return sctp_sf_do_9_2_start_shutdown(net, ep, asoc,
1854 SCTP_ST_CHUNK(0), NULL,
1855 commands);
1856 } else {
1857 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1858 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1859 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 return SCTP_DISPOSITION_CONSUME;
1862
1863nomem_ev:
1864 sctp_chunk_free(repl);
1865nomem:
1866 return SCTP_DISPOSITION_NOMEM;
1867}
1868
1869/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1870 *
1871 * Section 5.2.4
1872 * B) In this case, both sides may be attempting to start an association
1873 * at about the same time but the peer endpoint started its INIT
1874 * after responding to the local endpoint's INIT
1875 */
1876/* This case represents an initialization collision. */
Xin Long172a1592017-08-11 10:23:57 +08001877static enum sctp_disposition sctp_sf_do_dupcook_b(
1878 struct net *net,
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001879 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 const struct sctp_association *asoc,
1881 struct sctp_chunk *chunk,
Xin Longa85bbeb2017-08-11 10:23:52 +08001882 struct sctp_cmd_seq *commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 struct sctp_association *new_asoc)
1884{
Xin Long01a992b2017-06-30 11:52:22 +08001885 struct sctp_init_chunk *peer_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 struct sctp_chunk *repl;
1887
1888 /* new_asoc is a brand-new association, so these are not yet
1889 * side effects--it is safe to run them here.
1890 */
1891 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
Wei Yongjunde6becdc02011-04-19 21:30:51 +00001892 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 GFP_ATOMIC))
1894 goto nomem;
1895
1896 /* Update the content of current association. */
1897 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1898 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1899 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001900 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1902
1903 repl = sctp_make_cookie_ack(new_asoc, chunk);
1904 if (!repl)
1905 goto nomem;
1906
1907 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 /* RFC 2960 5.1 Normal Establishment of an Association
1910 *
1911 * D) IMPLEMENTATION NOTE: An implementation may choose to
1912 * send the Communication Up notification to the SCTP user
1913 * upon reception of a valid COOKIE ECHO chunk.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001914 *
1915 * Sadly, this needs to be implemented as a side-effect, because
1916 * we are not guaranteed to have set the association id of the real
1917 * association and so these notifications need to be delayed until
1918 * the association id is allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Vlad Yasevich07d93962007-05-04 13:55:27 -07001921 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001924 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001926 * peers requested adaptation layer.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001927 *
1928 * This also needs to be done as a side effect for the same reason as
1929 * above.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 */
Vlad Yasevich07d93962007-05-04 13:55:27 -07001931 if (asoc->peer.adaptation_ind)
1932 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Xin Long30f6ebf2018-03-14 19:05:34 +08001934 if (!asoc->peer.auth_capable)
1935 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_NO_AUTH, SCTP_NULL());
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 return SCTP_DISPOSITION_CONSUME;
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939nomem:
1940 return SCTP_DISPOSITION_NOMEM;
1941}
1942
1943/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1944 *
1945 * Section 5.2.4
1946 * C) In this case, the local endpoint's cookie has arrived late.
1947 * Before it arrived, the local endpoint sent an INIT and received an
1948 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1949 * but a new tag of its own.
1950 */
1951/* This case represents an initialization collision. */
Xin Long172a1592017-08-11 10:23:57 +08001952static enum sctp_disposition sctp_sf_do_dupcook_c(
1953 struct net *net,
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001954 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 const struct sctp_association *asoc,
1956 struct sctp_chunk *chunk,
Xin Longa85bbeb2017-08-11 10:23:52 +08001957 struct sctp_cmd_seq *commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 struct sctp_association *new_asoc)
1959{
1960 /* The cookie should be silently discarded.
1961 * The endpoint SHOULD NOT change states and should leave
1962 * any timers running.
1963 */
1964 return SCTP_DISPOSITION_DISCARD;
1965}
1966
1967/* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1968 *
1969 * Section 5.2.4
1970 *
1971 * D) When both local and remote tags match the endpoint should always
1972 * enter the ESTABLISHED state, if it has not already done so.
1973 */
1974/* This case represents an initialization collision. */
Xin Long172a1592017-08-11 10:23:57 +08001975static enum sctp_disposition sctp_sf_do_dupcook_d(
1976 struct net *net,
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001977 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 const struct sctp_association *asoc,
1979 struct sctp_chunk *chunk,
Xin Longa85bbeb2017-08-11 10:23:52 +08001980 struct sctp_cmd_seq *commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 struct sctp_association *new_asoc)
1982{
Xin Long30f6ebf2018-03-14 19:05:34 +08001983 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL, *auth_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 struct sctp_chunk *repl;
1985
1986 /* Clarification from Implementor's Guide:
1987 * D) When both local and remote tags match the endpoint should
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001988 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1989 * It should stop any cookie timer that may be running and send
1990 * a COOKIE ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 */
1992
1993 /* Don't accidentally move back into established state. */
1994 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1995 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1996 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1997 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1998 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001999 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
2001 SCTP_NULL());
2002
2003 /* RFC 2960 5.1 Normal Establishment of an Association
2004 *
2005 * D) IMPLEMENTATION NOTE: An implementation may choose
2006 * to send the Communication Up notification to the
2007 * SCTP user upon reception of a valid COOKIE
2008 * ECHO chunk.
2009 */
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07002010 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 SCTP_COMM_UP, 0,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07002012 asoc->c.sinit_num_ostreams,
2013 asoc->c.sinit_max_instreams,
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09002014 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (!ev)
2016 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
2018 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002019 * When a peer sends a Adaptation Layer Indication parameter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 * SCTP delivers this notification to inform the application
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002021 * that of the peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002023 if (asoc->peer.adaptation_ind) {
2024 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07002026 if (!ai_ev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 goto nomem;
2028
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 }
Xin Long30f6ebf2018-03-14 19:05:34 +08002030
2031 if (!asoc->peer.auth_capable) {
2032 auth_ev = sctp_ulpevent_make_authkey(asoc, 0,
2033 SCTP_AUTH_NO_AUTH,
2034 GFP_ATOMIC);
2035 if (!auth_ev)
2036 goto nomem;
2037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040 repl = sctp_make_cookie_ack(new_asoc, chunk);
2041 if (!repl)
2042 goto nomem;
2043
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07002044 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
2045
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07002046 if (ev)
2047 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2048 SCTP_ULPEVENT(ev));
2049 if (ai_ev)
2050 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2051 SCTP_ULPEVENT(ai_ev));
Xin Long30f6ebf2018-03-14 19:05:34 +08002052 if (auth_ev)
2053 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2054 SCTP_ULPEVENT(auth_ev));
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07002055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 return SCTP_DISPOSITION_CONSUME;
2057
2058nomem:
Xin Long30f6ebf2018-03-14 19:05:34 +08002059 if (auth_ev)
2060 sctp_ulpevent_free(auth_ev);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07002061 if (ai_ev)
2062 sctp_ulpevent_free(ai_ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 if (ev)
2064 sctp_ulpevent_free(ev);
2065 return SCTP_DISPOSITION_NOMEM;
2066}
2067
2068/*
2069 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
2070 * chunk was retransmitted and then delayed in the network.
2071 *
2072 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
2073 *
2074 * Verification Tag: None. Do cookie validation.
2075 *
2076 * Inputs
2077 * (endpoint, asoc, chunk)
2078 *
2079 * Outputs
2080 * (asoc, reply_msg, msg_up, timers, counters)
2081 *
2082 * The return value is the disposition of the chunk.
2083 */
Xin Long172a1592017-08-11 10:23:57 +08002084enum sctp_disposition sctp_sf_do_5_2_4_dupcook(
2085 struct net *net,
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002086 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002088 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 void *arg,
Xin Longa85bbeb2017-08-11 10:23:52 +08002090 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 struct sctp_association *new_asoc;
Xin Long172a1592017-08-11 10:23:57 +08002093 struct sctp_chunk *chunk = arg;
2094 enum sctp_disposition retval;
2095 struct sctp_chunk *err_chk_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 int error = 0;
2097 char action;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099 /* Make sure that the chunk has a valid length from the protocol
2100 * perspective. In this case check to make sure we have at least
2101 * enough for the chunk header. Cookie length verification is
2102 * done later.
2103 */
Xin Long922dbc52017-06-30 11:52:13 +08002104 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002105 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 commands);
2107
2108 /* "Decode" the chunk. We have no optional parameters so we
2109 * are in good shape.
2110 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002111 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -07002112 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +08002113 sizeof(struct sctp_chunkhdr)))
Sridhar Samudrala62b08082006-05-05 17:04:43 -07002114 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2117 * of a duplicate COOKIE ECHO match the Verification Tags of the
2118 * current association, consider the State Cookie valid even if
2119 * the lifespan is exceeded.
2120 */
2121 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2122 &err_chk_p);
2123
2124 /* FIXME:
2125 * If the re-build failed, what is the proper error path
2126 * from here?
2127 *
2128 * [We should abort the association. --piggy]
2129 */
2130 if (!new_asoc) {
2131 /* FIXME: Several errors are possible. A bad cookie should
2132 * be silently discarded, but think about logging it too.
2133 */
2134 switch (error) {
2135 case -SCTP_IERROR_NOMEM:
2136 goto nomem;
2137
2138 case -SCTP_IERROR_STALE_COOKIE:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002139 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 err_chk_p);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002141 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 case -SCTP_IERROR_BAD_SIG:
2143 default:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002144 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 }
2147
Xin Long7e062972017-05-23 13:28:55 +08002148 /* Set temp so that it won't be added into hashtable */
2149 new_asoc->temp = 1;
2150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 /* Compare the tie_tag in cookie with the verification tag of
2152 * current association.
2153 */
2154 action = sctp_tietags_compare(new_asoc, asoc);
2155
2156 switch (action) {
2157 case 'A': /* Association restart. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002158 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 new_asoc);
2160 break;
2161
2162 case 'B': /* Collision case B. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002163 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 new_asoc);
2165 break;
2166
2167 case 'C': /* Collision case C. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002168 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 new_asoc);
2170 break;
2171
2172 case 'D': /* Collision case D. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002173 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 new_asoc);
2175 break;
2176
2177 default: /* Discard packet for all others. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002178 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 /* Delete the tempory new association. */
Vlad Yasevichf2815632013-03-12 15:53:23 +00002183 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2185
Max Matveevd5ccd492011-08-29 21:02:24 +00002186 /* Restore association pointer to provide SCTP command interpeter
2187 * with a valid context in case it needs to manipulate
2188 * the queues */
2189 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2190 SCTP_ASOC((struct sctp_association *)asoc));
2191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 return retval;
2193
2194nomem:
2195 return SCTP_DISPOSITION_NOMEM;
2196}
2197
2198/*
2199 * Process an ABORT. (SHUTDOWN-PENDING state)
2200 *
2201 * See sctp_sf_do_9_1_abort().
2202 */
Xin Long172a1592017-08-11 10:23:57 +08002203enum sctp_disposition sctp_sf_shutdown_pending_abort(
2204 struct net *net,
2205 const struct sctp_endpoint *ep,
2206 const struct sctp_association *asoc,
2207 const union sctp_subtype type,
2208 void *arg,
2209 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210{
2211 struct sctp_chunk *chunk = arg;
2212
2213 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002214 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
2216 /* Make sure that the ABORT chunk has a valid length.
2217 * Since this is an ABORT chunk, we have to discard it
2218 * because of the following text:
2219 * RFC 2960, Section 3.3.7
2220 * If an endpoint receives an ABORT with a format error or for an
2221 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002222 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 * as we do not know its true length. So, to be safe, discard the
2224 * packet.
2225 */
Xin Long441ae652017-07-23 09:34:36 +08002226 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002227 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Vlad Yasevich75205f42007-12-20 14:12:59 -08002229 /* ADD-IP: Special case for ABORT chunks
2230 * F4) One special consideration is that ABORT Chunks arriving
2231 * destined to the IP address being deleted MUST be
2232 * ignored (see Section 5.3.1 for further details).
2233 */
2234 if (SCTP_ADDR_DEL ==
2235 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002236 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002237
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002238 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239}
2240
2241/*
2242 * Process an ABORT. (SHUTDOWN-SENT state)
2243 *
2244 * See sctp_sf_do_9_1_abort().
2245 */
Xin Long172a1592017-08-11 10:23:57 +08002246enum sctp_disposition sctp_sf_shutdown_sent_abort(
2247 struct net *net,
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002248 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002250 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 void *arg,
Xin Longa85bbeb2017-08-11 10:23:52 +08002252 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
2254 struct sctp_chunk *chunk = arg;
2255
2256 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002257 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
2259 /* Make sure that the ABORT chunk has a valid length.
2260 * Since this is an ABORT chunk, we have to discard it
2261 * because of the following text:
2262 * RFC 2960, Section 3.3.7
2263 * If an endpoint receives an ABORT with a format error or for an
2264 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002265 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 * as we do not know its true length. So, to be safe, discard the
2267 * packet.
2268 */
Xin Long441ae652017-07-23 09:34:36 +08002269 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002270 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Vlad Yasevich75205f42007-12-20 14:12:59 -08002272 /* ADD-IP: Special case for ABORT chunks
2273 * F4) One special consideration is that ABORT Chunks arriving
2274 * destined to the IP address being deleted MUST be
2275 * ignored (see Section 5.3.1 for further details).
2276 */
2277 if (SCTP_ADDR_DEL ==
2278 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002279 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 /* Stop the T2-shutdown timer. */
2282 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2283 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2284
2285 /* Stop the T5-shutdown guard timer. */
2286 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2287 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2288
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002289 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290}
2291
2292/*
2293 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2294 *
2295 * See sctp_sf_do_9_1_abort().
2296 */
Xin Long172a1592017-08-11 10:23:57 +08002297enum sctp_disposition sctp_sf_shutdown_ack_sent_abort(
2298 struct net *net,
2299 const struct sctp_endpoint *ep,
2300 const struct sctp_association *asoc,
2301 const union sctp_subtype type,
2302 void *arg,
2303 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
2305 /* The same T2 timer, so we should be able to use
2306 * common function with the SHUTDOWN-SENT state.
2307 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002308 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309}
2310
2311/*
2312 * Handle an Error received in COOKIE_ECHOED state.
2313 *
2314 * Only handle the error type of stale COOKIE Error, the other errors will
2315 * be ignored.
2316 *
2317 * Inputs
2318 * (endpoint, asoc, chunk)
2319 *
2320 * Outputs
2321 * (asoc, reply_msg, msg_up, timers, counters)
2322 *
2323 * The return value is the disposition of the chunk.
2324 */
Xin Long172a1592017-08-11 10:23:57 +08002325enum sctp_disposition sctp_sf_cookie_echoed_err(
2326 struct net *net,
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002327 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002329 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 void *arg,
Xin Longa85bbeb2017-08-11 10:23:52 +08002331 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
2333 struct sctp_chunk *chunk = arg;
Xin Longd8238d92017-08-03 15:42:11 +08002334 struct sctp_errhdr *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
2336 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002337 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
2339 /* Make sure that the ERROR chunk has a valid length.
2340 * The parameter walking depends on this as well.
2341 */
Xin Long87caeba2017-08-03 15:42:12 +08002342 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_operr_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002343 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 commands);
2345
2346 /* Process the error here */
2347 /* FUTURE FIXME: When PR-SCTP related and other optional
2348 * parms are emitted, this will have to change to handle multiple
2349 * errors.
2350 */
2351 sctp_walk_errors(err, chunk->chunk_hdr) {
2352 if (SCTP_ERROR_STALE_COOKIE == err->cause)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002353 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 arg, commands);
2355 }
2356
2357 /* It is possible to have malformed error causes, and that
2358 * will cause us to end the walk early. However, since
2359 * we are discarding the packet, there should be no adverse
2360 * affects.
2361 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002362 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363}
2364
2365/*
2366 * Handle a Stale COOKIE Error
2367 *
2368 * Section: 5.2.6 Handle Stale COOKIE Error
2369 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2370 * one of the following three alternatives.
2371 * ...
2372 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2373 * Preservative parameter requesting an extension to the lifetime of
2374 * the State Cookie. When calculating the time extension, an
2375 * implementation SHOULD use the RTT information measured based on the
2376 * previous COOKIE ECHO / ERROR exchange, and should add no more
2377 * than 1 second beyond the measured RTT, due to long State Cookie
2378 * lifetimes making the endpoint more subject to a replay attack.
2379 *
2380 * Verification Tag: Not explicit, but safe to ignore.
2381 *
2382 * Inputs
2383 * (endpoint, asoc, chunk)
2384 *
2385 * Outputs
2386 * (asoc, reply_msg, msg_up, timers, counters)
2387 *
2388 * The return value is the disposition of the chunk.
2389 */
Xin Long172a1592017-08-11 10:23:57 +08002390static enum sctp_disposition sctp_sf_do_5_2_6_stale(
2391 struct net *net,
2392 const struct sctp_endpoint *ep,
2393 const struct sctp_association *asoc,
2394 const union sctp_subtype type,
2395 void *arg,
2396 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397{
Frank Filz3f7a87d2005-06-20 13:14:57 -07002398 int attempts = asoc->init_err_counter + 1;
Xin Long365ddb62017-07-17 11:29:51 +08002399 struct sctp_chunk *chunk = arg, *reply;
2400 struct sctp_cookie_preserve_param bht;
2401 struct sctp_bind_addr *bp;
Xin Longd8238d92017-08-03 15:42:11 +08002402 struct sctp_errhdr *err;
Xin Long365ddb62017-07-17 11:29:51 +08002403 u32 stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
Vlad Yasevich81845c22006-01-30 15:59:54 -08002405 if (attempts > asoc->max_init_attempts) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002406 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2407 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002409 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 return SCTP_DISPOSITION_DELETE_TCB;
2411 }
2412
Xin Longd8238d92017-08-03 15:42:11 +08002413 err = (struct sctp_errhdr *)(chunk->skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
2415 /* When calculating the time extension, an implementation
2416 * SHOULD use the RTT information measured based on the
2417 * previous COOKIE ECHO / ERROR exchange, and should add no
2418 * more than 1 second beyond the measured RTT, due to long
2419 * State Cookie lifetimes making the endpoint more subject to
2420 * a replay attack.
2421 * Measure of Staleness's unit is usec. (1/1000000 sec)
2422 * Suggested Cookie Life-span Increment's unit is msec.
2423 * (1/1000 sec)
2424 * In general, if you use the suggested cookie life, the value
2425 * found in the field of measure of staleness should be doubled
2426 * to give ample time to retransmit the new cookie and thus
2427 * yield a higher probability of success on the reattempt.
2428 */
Xin Longd8238d92017-08-03 15:42:11 +08002429 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(*err)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 stale = (stale * 2) / 1000;
2431
2432 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2433 bht.param_hdr.length = htons(sizeof(bht));
2434 bht.lifespan_increment = htonl(stale);
2435
2436 /* Build that new INIT chunk. */
2437 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2438 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2439 if (!reply)
2440 goto nomem;
2441
2442 sctp_addto_chunk(reply, sizeof(bht), &bht);
2443
2444 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2445 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2446
2447 /* Stop pending T3-rtx and heartbeat timers */
2448 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2449 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2450
2451 /* Delete non-primary peer ip addresses since we are transitioning
2452 * back to the COOKIE-WAIT state
2453 */
2454 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2455
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002456 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2457 * resend
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 */
Vlad Yasevichb6157d82007-10-24 15:59:16 -04002459 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 SCTP_TRANSPORT(asoc->peer.primary_path));
2461
2462 /* Cast away the const modifier, as we want to just
2463 * rerun it through as a sideffect.
2464 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07002465 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
2467 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2468 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2469 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2470 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2471 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2472 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2473
2474 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2475
2476 return SCTP_DISPOSITION_CONSUME;
2477
2478nomem:
2479 return SCTP_DISPOSITION_NOMEM;
2480}
2481
2482/*
2483 * Process an ABORT.
2484 *
2485 * Section: 9.1
2486 * After checking the Verification Tag, the receiving endpoint shall
2487 * remove the association from its record, and shall report the
2488 * termination to its upper layer.
2489 *
2490 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2491 * B) Rules for packet carrying ABORT:
2492 *
2493 * - The endpoint shall always fill in the Verification Tag field of the
2494 * outbound packet with the destination endpoint's tag value if it
2495 * is known.
2496 *
2497 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2498 * MUST follow the procedure described in Section 8.4.
2499 *
2500 * - The receiver MUST accept the packet if the Verification Tag
2501 * matches either its own tag, OR the tag of its peer. Otherwise, the
2502 * receiver MUST silently discard the packet and take no further
2503 * action.
2504 *
2505 * Inputs
2506 * (endpoint, asoc, chunk)
2507 *
2508 * Outputs
2509 * (asoc, reply_msg, msg_up, timers, counters)
2510 *
2511 * The return value is the disposition of the chunk.
2512 */
Xin Long172a1592017-08-11 10:23:57 +08002513enum sctp_disposition sctp_sf_do_9_1_abort(
2514 struct net *net,
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002515 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002517 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 void *arg,
Xin Longa85bbeb2017-08-11 10:23:52 +08002519 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
2521 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
2523 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002524 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
2526 /* Make sure that the ABORT chunk has a valid length.
2527 * Since this is an ABORT chunk, we have to discard it
2528 * because of the following text:
2529 * RFC 2960, Section 3.3.7
2530 * If an endpoint receives an ABORT with a format error or for an
2531 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002532 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 * as we do not know its true length. So, to be safe, discard the
2534 * packet.
2535 */
Xin Long441ae652017-07-23 09:34:36 +08002536 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002537 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Vlad Yasevich75205f42007-12-20 14:12:59 -08002539 /* ADD-IP: Special case for ABORT chunks
2540 * F4) One special consideration is that ABORT Chunks arriving
2541 * destined to the IP address being deleted MUST be
2542 * ignored (see Section 5.3.1 for further details).
2543 */
2544 if (SCTP_ADDR_DEL ==
2545 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002546 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002547
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002548 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002549}
2550
Xin Long172a1592017-08-11 10:23:57 +08002551static enum sctp_disposition __sctp_sf_do_9_1_abort(
2552 struct net *net,
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002553 const struct sctp_endpoint *ep,
Vlad Yasevich75205f42007-12-20 14:12:59 -08002554 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002555 const union sctp_subtype type,
Vlad Yasevich75205f42007-12-20 14:12:59 -08002556 void *arg,
Xin Longa85bbeb2017-08-11 10:23:52 +08002557 struct sctp_cmd_seq *commands)
Vlad Yasevich75205f42007-12-20 14:12:59 -08002558{
Xin Long172a1592017-08-11 10:23:57 +08002559 __be16 error = SCTP_ERROR_NO_ERROR;
Vlad Yasevich75205f42007-12-20 14:12:59 -08002560 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002561 unsigned int len;
Vlad Yasevich75205f42007-12-20 14:12:59 -08002562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 /* See if we have an error cause code in the chunk. */
2564 len = ntohs(chunk->chunk_hdr->length);
Shan Wei96ca4682011-04-19 21:26:26 +00002565 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr)) {
Xin Longd8238d92017-08-03 15:42:11 +08002566 struct sctp_errhdr *err;
Shan Wei96ca4682011-04-19 21:26:26 +00002567
Shan Wei96ca4682011-04-19 21:26:26 +00002568 sctp_walk_errors(err, chunk->chunk_hdr);
2569 if ((void *)err != (void *)chunk->chunk_end)
Xin Longd8238d92017-08-03 15:42:11 +08002570 return sctp_sf_pdiscard(net, ep, asoc, type, arg,
2571 commands);
Shan Wei96ca4682011-04-19 21:26:26 +00002572
Xin Longd8238d92017-08-03 15:42:11 +08002573 error = ((struct sctp_errhdr *)chunk->skb->data)->cause;
Shan Wei96ca4682011-04-19 21:26:26 +00002574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002576 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002577 /* ASSOC_FAILED will DELETE_TCB. */
Al Viro5be291f2006-11-20 17:01:06 -08002578 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00002579 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2580 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
2582 return SCTP_DISPOSITION_ABORT;
2583}
2584
2585/*
2586 * Process an ABORT. (COOKIE-WAIT state)
2587 *
2588 * See sctp_sf_do_9_1_abort() above.
2589 */
Xin Long172a1592017-08-11 10:23:57 +08002590enum sctp_disposition sctp_sf_cookie_wait_abort(
2591 struct net *net,
2592 const struct sctp_endpoint *ep,
2593 const struct sctp_association *asoc,
2594 const union sctp_subtype type,
2595 void *arg,
2596 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597{
Xin Long172a1592017-08-11 10:23:57 +08002598 __be16 error = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002600 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
2602 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002603 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
2605 /* Make sure that the ABORT chunk has a valid length.
2606 * Since this is an ABORT chunk, we have to discard it
2607 * because of the following text:
2608 * RFC 2960, Section 3.3.7
2609 * If an endpoint receives an ABORT with a format error or for an
2610 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002611 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 * as we do not know its true length. So, to be safe, discard the
2613 * packet.
2614 */
Xin Long441ae652017-07-23 09:34:36 +08002615 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002616 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
2618 /* See if we have an error cause code in the chunk. */
2619 len = ntohs(chunk->chunk_hdr->length);
2620 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
Xin Longd8238d92017-08-03 15:42:11 +08002621 error = ((struct sctp_errhdr *)chunk->skb->data)->cause;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002623 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002624 chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625}
2626
2627/*
2628 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2629 */
Xin Long172a1592017-08-11 10:23:57 +08002630enum sctp_disposition sctp_sf_cookie_wait_icmp_abort(
2631 struct net *net,
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002632 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002634 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 void *arg,
Xin Longa85bbeb2017-08-11 10:23:52 +08002636 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002638 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002639 ENOPROTOOPT, asoc,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002640 (struct sctp_transport *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641}
2642
2643/*
2644 * Process an ABORT. (COOKIE-ECHOED state)
2645 */
Xin Long172a1592017-08-11 10:23:57 +08002646enum sctp_disposition sctp_sf_cookie_echoed_abort(
2647 struct net *net,
2648 const struct sctp_endpoint *ep,
2649 const struct sctp_association *asoc,
2650 const union sctp_subtype type,
2651 void *arg,
2652 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653{
2654 /* There is a single T1 timer, so we should be able to use
2655 * common function with the COOKIE-WAIT state.
2656 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002657 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658}
2659
2660/*
2661 * Stop T1 timer and abort association with "INIT failed".
2662 *
2663 * This is common code called by several sctp_sf_*_abort() functions above.
2664 */
Xin Long172a1592017-08-11 10:23:57 +08002665static enum sctp_disposition sctp_stop_t1_and_abort(
2666 struct net *net,
2667 struct sctp_cmd_seq *commands,
2668 __be16 error, int sk_err,
2669 const struct sctp_association *asoc,
2670 struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671{
Daniel Borkmannbb333812013-06-28 19:49:40 +02002672 pr_debug("%s: ABORT received (INIT)\n", __func__);
2673
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2675 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002676 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2678 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002679 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 /* CMD_INIT_FAILED will DELETE_TCB. */
2681 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002682 SCTP_PERR(error));
Daniel Borkmannbb333812013-06-28 19:49:40 +02002683
Frank Filz3f7a87d2005-06-20 13:14:57 -07002684 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685}
2686
2687/*
2688 * sctp_sf_do_9_2_shut
2689 *
2690 * Section: 9.2
2691 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2692 * - enter the SHUTDOWN-RECEIVED state,
2693 *
2694 * - stop accepting new data from its SCTP user
2695 *
2696 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2697 * that all its outstanding DATA chunks have been received by the
2698 * SHUTDOWN sender.
2699 *
2700 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2701 * send a SHUTDOWN in response to a ULP request. And should discard
2702 * subsequent SHUTDOWN chunks.
2703 *
2704 * If there are still outstanding DATA chunks left, the SHUTDOWN
2705 * receiver shall continue to follow normal data transmission
2706 * procedures defined in Section 6 until all outstanding DATA chunks
2707 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2708 * new data from its SCTP user.
2709 *
2710 * Verification Tag: 8.5 Verification Tag [Normal verification]
2711 *
2712 * Inputs
2713 * (endpoint, asoc, chunk)
2714 *
2715 * Outputs
2716 * (asoc, reply_msg, msg_up, timers, counters)
2717 *
2718 * The return value is the disposition of the chunk.
2719 */
Xin Long172a1592017-08-11 10:23:57 +08002720enum sctp_disposition sctp_sf_do_9_2_shutdown(
2721 struct net *net,
2722 const struct sctp_endpoint *ep,
2723 const struct sctp_association *asoc,
2724 const union sctp_subtype type,
2725 void *arg,
2726 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727{
Xin Long172a1592017-08-11 10:23:57 +08002728 enum sctp_disposition disposition;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 struct sctp_chunk *chunk = arg;
Xin Longe61e4052017-08-03 15:42:09 +08002730 struct sctp_shutdownhdr *sdh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 struct sctp_ulpevent *ev;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002732 __u32 ctsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
2734 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002735 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
2737 /* Make sure that the SHUTDOWN chunk has a valid length. */
Xin Longac23e682017-08-03 15:42:10 +08002738 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002739 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 commands);
2741
2742 /* Convert the elaborate header. */
Xin Longe61e4052017-08-03 15:42:09 +08002743 sdh = (struct sctp_shutdownhdr *)chunk->skb->data;
2744 skb_pull(chunk->skb, sizeof(*sdh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 chunk->subh.shutdown_hdr = sdh;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002746 ctsn = ntohl(sdh->cum_tsn_ack);
2747
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002748 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02002749 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2750 asoc->ctsn_ack_point);
2751
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002752 return SCTP_DISPOSITION_DISCARD;
2753 }
2754
Wei Yongjundf10eec2008-10-23 01:00:21 -07002755 /* If Cumulative TSN Ack beyond the max tsn currently
2756 * send, terminating the association and respond to the
2757 * sender with an ABORT.
2758 */
2759 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002760 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002762 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2763 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2764 * inform the application that it should cease sending data.
2765 */
2766 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2767 if (!ev) {
2768 disposition = SCTP_DISPOSITION_NOMEM;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002769 goto out;
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002770 }
2771 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2772
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2774 * - enter the SHUTDOWN-RECEIVED state,
2775 * - stop accepting new data from its SCTP user
2776 *
2777 * [This is implicit in the new state.]
2778 */
2779 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2780 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2781 disposition = SCTP_DISPOSITION_CONSUME;
2782
2783 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002784 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 arg, commands);
2786 }
2787
2788 if (SCTP_DISPOSITION_NOMEM == disposition)
2789 goto out;
2790
2791 /* - verify, by checking the Cumulative TSN Ack field of the
2792 * chunk, that all its outstanding DATA chunks have been
2793 * received by the SHUTDOWN sender.
2794 */
2795 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
Al Viro2178eda2006-11-20 17:26:53 -08002796 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798out:
2799 return disposition;
2800}
2801
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002802/*
2803 * sctp_sf_do_9_2_shut_ctsn
2804 *
2805 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2806 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2807 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2808 * MUST be processed.
2809 */
Xin Long172a1592017-08-11 10:23:57 +08002810enum sctp_disposition sctp_sf_do_9_2_shut_ctsn(
2811 struct net *net,
2812 const struct sctp_endpoint *ep,
2813 const struct sctp_association *asoc,
2814 const union sctp_subtype type,
2815 void *arg,
2816 struct sctp_cmd_seq *commands)
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002817{
2818 struct sctp_chunk *chunk = arg;
Xin Longe61e4052017-08-03 15:42:09 +08002819 struct sctp_shutdownhdr *sdh;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002820 __u32 ctsn;
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002821
2822 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002823 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002824
2825 /* Make sure that the SHUTDOWN chunk has a valid length. */
Xin Longac23e682017-08-03 15:42:10 +08002826 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002827 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002828 commands);
2829
Xin Longe61e4052017-08-03 15:42:09 +08002830 sdh = (struct sctp_shutdownhdr *)chunk->skb->data;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002831 ctsn = ntohl(sdh->cum_tsn_ack);
2832
2833 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02002834 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2835 asoc->ctsn_ack_point);
2836
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002837 return SCTP_DISPOSITION_DISCARD;
2838 }
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002839
2840 /* If Cumulative TSN Ack beyond the max tsn currently
2841 * send, terminating the association and respond to the
2842 * sender with an ABORT.
2843 */
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002844 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002845 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002846
2847 /* verify, by checking the Cumulative TSN Ack field of the
2848 * chunk, that all its outstanding DATA chunks have been
2849 * received by the SHUTDOWN sender.
2850 */
2851 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2852 SCTP_BE32(sdh->cum_tsn_ack));
2853
2854 return SCTP_DISPOSITION_CONSUME;
2855}
2856
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857/* RFC 2960 9.2
2858 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2859 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2860 * transport addresses (either in the IP addresses or in the INIT chunk)
2861 * that belong to this association, it should discard the INIT chunk and
2862 * retransmit the SHUTDOWN ACK chunk.
2863 */
Xin Long172a1592017-08-11 10:23:57 +08002864enum sctp_disposition sctp_sf_do_9_2_reshutack(
2865 struct net *net,
2866 const struct sctp_endpoint *ep,
2867 const struct sctp_association *asoc,
2868 const union sctp_subtype type,
2869 void *arg,
2870 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871{
Xin Long172a1592017-08-11 10:23:57 +08002872 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 struct sctp_chunk *reply;
2874
Vlad Yasevichece25df2007-09-07 16:30:54 -04002875 /* Make sure that the chunk has a valid length */
Xin Long922dbc52017-06-30 11:52:13 +08002876 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002877 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04002878 commands);
2879
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 /* Since we are not going to really process this INIT, there
2881 * is no point in verifying chunk boundries. Just generate
2882 * the SHUTDOWN ACK.
2883 */
2884 reply = sctp_make_shutdown_ack(asoc, chunk);
2885 if (NULL == reply)
2886 goto nomem;
2887
2888 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2889 * the T2-SHUTDOWN timer.
2890 */
2891 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2892
2893 /* and restart the T2-shutdown timer. */
2894 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2895 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2896
2897 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2898
2899 return SCTP_DISPOSITION_CONSUME;
2900nomem:
2901 return SCTP_DISPOSITION_NOMEM;
2902}
2903
2904/*
2905 * sctp_sf_do_ecn_cwr
2906 *
2907 * Section: Appendix A: Explicit Congestion Notification
2908 *
2909 * CWR:
2910 *
2911 * RFC 2481 details a specific bit for a sender to send in the header of
2912 * its next outbound TCP segment to indicate to its peer that it has
2913 * reduced its congestion window. This is termed the CWR bit. For
2914 * SCTP the same indication is made by including the CWR chunk.
2915 * This chunk contains one data element, i.e. the TSN number that
2916 * was sent in the ECNE chunk. This element represents the lowest
2917 * TSN number in the datagram that was originally marked with the
2918 * CE bit.
2919 *
2920 * Verification Tag: 8.5 Verification Tag [Normal verification]
2921 * Inputs
2922 * (endpoint, asoc, chunk)
2923 *
2924 * Outputs
2925 * (asoc, reply_msg, msg_up, timers, counters)
2926 *
2927 * The return value is the disposition of the chunk.
2928 */
Xin Long172a1592017-08-11 10:23:57 +08002929enum sctp_disposition sctp_sf_do_ecn_cwr(struct net *net,
2930 const struct sctp_endpoint *ep,
2931 const struct sctp_association *asoc,
2932 const union sctp_subtype type,
2933 void *arg,
2934 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 struct sctp_chunk *chunk = arg;
Xin Long65f77102017-08-03 15:42:16 +08002937 struct sctp_cwrhdr *cwr;
Al Viro34bcca22006-11-20 17:27:15 -08002938 u32 lowest_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
2940 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002941 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
Xin Longb515fd22017-08-03 15:42:15 +08002943 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002944 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002946
Xin Long65f77102017-08-03 15:42:16 +08002947 cwr = (struct sctp_cwrhdr *)chunk->skb->data;
2948 skb_pull(chunk->skb, sizeof(*cwr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
Al Viro34bcca22006-11-20 17:27:15 -08002950 lowest_tsn = ntohl(cwr->lowest_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
2952 /* Does this CWR ack the last sent congestion notification? */
Al Viro34bcca22006-11-20 17:27:15 -08002953 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 /* Stop sending ECNE. */
2955 sctp_add_cmd_sf(commands,
2956 SCTP_CMD_ECN_CWR,
Al Viro34bcca22006-11-20 17:27:15 -08002957 SCTP_U32(lowest_tsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 }
2959 return SCTP_DISPOSITION_CONSUME;
2960}
2961
2962/*
2963 * sctp_sf_do_ecne
2964 *
2965 * Section: Appendix A: Explicit Congestion Notification
2966 *
2967 * ECN-Echo
2968 *
2969 * RFC 2481 details a specific bit for a receiver to send back in its
2970 * TCP acknowledgements to notify the sender of the Congestion
2971 * Experienced (CE) bit having arrived from the network. For SCTP this
2972 * same indication is made by including the ECNE chunk. This chunk
2973 * contains one data element, i.e. the lowest TSN associated with the IP
2974 * datagram marked with the CE bit.....
2975 *
2976 * Verification Tag: 8.5 Verification Tag [Normal verification]
2977 * Inputs
2978 * (endpoint, asoc, chunk)
2979 *
2980 * Outputs
2981 * (asoc, reply_msg, msg_up, timers, counters)
2982 *
2983 * The return value is the disposition of the chunk.
2984 */
Xin Long172a1592017-08-11 10:23:57 +08002985enum sctp_disposition sctp_sf_do_ecne(struct net *net,
2986 const struct sctp_endpoint *ep,
2987 const struct sctp_association *asoc,
2988 const union sctp_subtype type,
2989 void *arg, struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 struct sctp_chunk *chunk = arg;
Xin Long1fb6d832017-08-03 15:42:14 +08002992 struct sctp_ecnehdr *ecne;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
2994 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002995 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Xin Longb515fd22017-08-03 15:42:15 +08002997 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002998 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 commands);
3000
Xin Long1fb6d832017-08-03 15:42:14 +08003001 ecne = (struct sctp_ecnehdr *)chunk->skb->data;
3002 skb_pull(chunk->skb, sizeof(*ecne));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
3004 /* If this is a newer ECNE than the last CWR packet we sent out */
3005 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
3006 SCTP_U32(ntohl(ecne->lowest_tsn)));
3007
3008 return SCTP_DISPOSITION_CONSUME;
3009}
3010
3011/*
3012 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
3013 *
3014 * The SCTP endpoint MUST always acknowledge the reception of each valid
3015 * DATA chunk.
3016 *
3017 * The guidelines on delayed acknowledgement algorithm specified in
3018 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
3019 * acknowledgement SHOULD be generated for at least every second packet
3020 * (not every second DATA chunk) received, and SHOULD be generated within
3021 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
3022 * situations it may be beneficial for an SCTP transmitter to be more
3023 * conservative than the algorithms detailed in this document allow.
3024 * However, an SCTP transmitter MUST NOT be more aggressive than the
3025 * following algorithms allow.
3026 *
3027 * A SCTP receiver MUST NOT generate more than one SACK for every
3028 * incoming packet, other than to update the offered window as the
3029 * receiving application consumes new data.
3030 *
3031 * Verification Tag: 8.5 Verification Tag [Normal verification]
3032 *
3033 * Inputs
3034 * (endpoint, asoc, chunk)
3035 *
3036 * Outputs
3037 * (asoc, reply_msg, msg_up, timers, counters)
3038 *
3039 * The return value is the disposition of the chunk.
3040 */
Xin Long172a1592017-08-11 10:23:57 +08003041enum sctp_disposition sctp_sf_eat_data_6_2(struct net *net,
3042 const struct sctp_endpoint *ep,
3043 const struct sctp_association *asoc,
3044 const union sctp_subtype type,
3045 void *arg,
3046 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047{
Xin Longc488b772017-08-11 10:23:53 +08003048 union sctp_arg force = SCTP_NOFORCE();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 struct sctp_chunk *chunk = arg;
3050 int error;
3051
3052 if (!sctp_vtag_verify(chunk, asoc)) {
3053 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3054 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003055 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
Xin Long9d4ceaf2017-12-08 21:04:03 +08003058 if (!sctp_chunk_length_valid(chunk, sctp_datachk_len(&asoc->stream)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003059 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 commands);
3061
wangweidongcb3f8372013-12-23 12:16:50 +08003062 error = sctp_eat_data(asoc, chunk, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 switch (error) {
3064 case SCTP_IERROR_NO_ERROR:
3065 break;
3066 case SCTP_IERROR_HIGH_TSN:
3067 case SCTP_IERROR_BAD_STREAM:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003068 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 goto discard_noforce;
3070 case SCTP_IERROR_DUP_TSN:
3071 case SCTP_IERROR_IGNORE_TSN:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003072 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 goto discard_force;
3074 case SCTP_IERROR_NO_DATA:
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02003075 return SCTP_DISPOSITION_ABORT;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003076 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003077 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Xin Long3583df12017-06-30 11:52:19 +08003078 (u8 *)chunk->subh.data_hdr,
Xin Long9d4ceaf2017-12-08 21:04:03 +08003079 sctp_datahdr_len(&asoc->stream));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 default:
3081 BUG();
3082 }
3083
Wei Yongjun6dc76942009-11-23 15:53:53 -05003084 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
3085 force = SCTP_FORCE();
3086
Neil Horman9f70f462013-12-10 06:48:15 -05003087 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3089 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3090 }
3091
3092 /* If this is the last chunk in a packet, we need to count it
3093 * toward sack generation. Note that we need to SACK every
3094 * OTHER packet containing data chunks, EVEN IF WE DISCARD
3095 * THEM. We elect to NOT generate SACK's if the chunk fails
3096 * the verification tag test.
3097 *
3098 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3099 *
3100 * The SCTP endpoint MUST always acknowledge the reception of
3101 * each valid DATA chunk.
3102 *
3103 * The guidelines on delayed acknowledgement algorithm
3104 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
3105 * Specifically, an acknowledgement SHOULD be generated for at
3106 * least every second packet (not every second DATA chunk)
3107 * received, and SHOULD be generated within 200 ms of the
3108 * arrival of any unacknowledged DATA chunk. In some
3109 * situations it may be beneficial for an SCTP transmitter to
3110 * be more conservative than the algorithms detailed in this
3111 * document allow. However, an SCTP transmitter MUST NOT be
3112 * more aggressive than the following algorithms allow.
3113 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08003114 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003115 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 return SCTP_DISPOSITION_CONSUME;
3118
3119discard_force:
3120 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3121 *
3122 * When a packet arrives with duplicate DATA chunk(s) and with
3123 * no new DATA chunk(s), the endpoint MUST immediately send a
3124 * SACK with no delay. If a packet arrives with duplicate
3125 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3126 * MAY immediately send a SACK. Normally receipt of duplicate
3127 * DATA chunks will occur when the original SACK chunk was lost
3128 * and the peer's RTO has expired. The duplicate TSN number(s)
3129 * SHOULD be reported in the SACK as duplicate.
3130 */
3131 /* In our case, we split the MAY SACK advice up whether or not
3132 * the last chunk is a duplicate.'
3133 */
3134 if (chunk->end_of_packet)
3135 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3136 return SCTP_DISPOSITION_DISCARD;
3137
3138discard_noforce:
Frank Filz52ccb8e2005-12-22 11:36:46 -08003139 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003140 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 return SCTP_DISPOSITION_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143}
3144
3145/*
3146 * sctp_sf_eat_data_fast_4_4
3147 *
3148 * Section: 4 (4)
3149 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3150 * DATA chunks without delay.
3151 *
3152 * Verification Tag: 8.5 Verification Tag [Normal verification]
3153 * Inputs
3154 * (endpoint, asoc, chunk)
3155 *
3156 * Outputs
3157 * (asoc, reply_msg, msg_up, timers, counters)
3158 *
3159 * The return value is the disposition of the chunk.
3160 */
Xin Long172a1592017-08-11 10:23:57 +08003161enum sctp_disposition sctp_sf_eat_data_fast_4_4(
3162 struct net *net,
3163 const struct sctp_endpoint *ep,
3164 const struct sctp_association *asoc,
3165 const union sctp_subtype type,
3166 void *arg,
3167 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168{
3169 struct sctp_chunk *chunk = arg;
3170 int error;
3171
3172 if (!sctp_vtag_verify(chunk, asoc)) {
3173 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3174 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003175 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 }
3177
Xin Long9d4ceaf2017-12-08 21:04:03 +08003178 if (!sctp_chunk_length_valid(chunk, sctp_datachk_len(&asoc->stream)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003179 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 commands);
3181
wangweidongcb3f8372013-12-23 12:16:50 +08003182 error = sctp_eat_data(asoc, chunk, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 switch (error) {
3184 case SCTP_IERROR_NO_ERROR:
3185 case SCTP_IERROR_HIGH_TSN:
3186 case SCTP_IERROR_DUP_TSN:
3187 case SCTP_IERROR_IGNORE_TSN:
3188 case SCTP_IERROR_BAD_STREAM:
3189 break;
3190 case SCTP_IERROR_NO_DATA:
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02003191 return SCTP_DISPOSITION_ABORT;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003192 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003193 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Xin Long3583df12017-06-30 11:52:19 +08003194 (u8 *)chunk->subh.data_hdr,
Xin Long9d4ceaf2017-12-08 21:04:03 +08003195 sctp_datahdr_len(&asoc->stream));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 default:
3197 BUG();
3198 }
3199
3200 /* Go a head and force a SACK, since we are shutting down. */
3201
3202 /* Implementor's Guide.
3203 *
3204 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3205 * respond to each received packet containing one or more DATA chunk(s)
3206 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3207 */
3208 if (chunk->end_of_packet) {
3209 /* We must delay the chunk creation since the cumulative
3210 * TSN has not been updated yet.
3211 */
3212 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3213 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3214 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3215 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3216 }
3217
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 return SCTP_DISPOSITION_CONSUME;
3219}
3220
3221/*
3222 * Section: 6.2 Processing a Received SACK
3223 * D) Any time a SACK arrives, the endpoint performs the following:
3224 *
3225 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3226 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3227 * increasing, a SACK whose Cumulative TSN Ack is less than the
3228 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3229 *
3230 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3231 * of bytes still outstanding after processing the Cumulative TSN Ack
3232 * and the Gap Ack Blocks.
3233 *
3234 * iii) If the SACK is missing a TSN that was previously
3235 * acknowledged via a Gap Ack Block (e.g., the data receiver
3236 * reneged on the data), then mark the corresponding DATA chunk
3237 * as available for retransmit: Mark it as missing for fast
3238 * retransmit as described in Section 7.2.4 and if no retransmit
3239 * timer is running for the destination address to which the DATA
3240 * chunk was originally transmitted, then T3-rtx is started for
3241 * that destination address.
3242 *
3243 * Verification Tag: 8.5 Verification Tag [Normal verification]
3244 *
3245 * Inputs
3246 * (endpoint, asoc, chunk)
3247 *
3248 * Outputs
3249 * (asoc, reply_msg, msg_up, timers, counters)
3250 *
3251 * The return value is the disposition of the chunk.
3252 */
Xin Long172a1592017-08-11 10:23:57 +08003253enum sctp_disposition sctp_sf_eat_sack_6_2(struct net *net,
3254 const struct sctp_endpoint *ep,
3255 const struct sctp_association *asoc,
3256 const union sctp_subtype type,
3257 void *arg,
3258 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259{
3260 struct sctp_chunk *chunk = arg;
Xin Long78731082017-07-23 09:34:32 +08003261 struct sctp_sackhdr *sackh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 __u32 ctsn;
3263
Masami Hiramatsu103d7502017-12-29 11:46:51 +09003264 trace_sctp_probe(ep, asoc, chunk);
3265
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003267 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
3269 /* Make sure that the SACK chunk has a valid length. */
Xin Longd4d6c612017-07-23 09:34:33 +08003270 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_sack_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003271 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 commands);
3273
3274 /* Pull the SACK chunk from the data buffer */
3275 sackh = sctp_sm_pull_sack(chunk);
3276 /* Was this a bogus SACK? */
3277 if (!sackh)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003278 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 chunk->subh.sack_hdr = sackh;
3280 ctsn = ntohl(sackh->cum_tsn_ack);
3281
3282 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3283 * Ack Point, then drop the SACK. Since Cumulative TSN
3284 * Ack is monotonically increasing, a SACK whose
3285 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3286 * Point indicates an out-of-order SACK.
3287 */
3288 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02003289 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
3290 asoc->ctsn_ack_point);
3291
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 return SCTP_DISPOSITION_DISCARD;
3293 }
3294
Wei Yongjunaecedea2007-08-02 16:57:44 +08003295 /* If Cumulative TSN Ack beyond the max tsn currently
3296 * send, terminating the association and respond to the
3297 * sender with an ABORT.
3298 */
3299 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003300 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjunaecedea2007-08-02 16:57:44 +08003301
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 /* Return this SACK for further processing. */
Nicolas Dichteledfee032012-10-03 05:43:22 +00003303 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
3305 /* Note: We do the rest of the work on the PROCESS_SACK
3306 * sideeffect.
3307 */
3308 return SCTP_DISPOSITION_CONSUME;
3309}
3310
3311/*
3312 * Generate an ABORT in response to a packet.
3313 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003314 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003316 * 8) The receiver should respond to the sender of the OOTB packet with
3317 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3318 * MUST fill in the Verification Tag field of the outbound packet
3319 * with the value found in the Verification Tag field of the OOTB
3320 * packet and set the T-bit in the Chunk Flags to indicate that the
3321 * Verification Tag is reflected. After sending this ABORT, the
3322 * receiver of the OOTB packet shall discard the OOTB packet and take
3323 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 *
3325 * Verification Tag:
3326 *
3327 * The return value is the disposition of the chunk.
3328*/
Xin Long172a1592017-08-11 10:23:57 +08003329static enum sctp_disposition sctp_sf_tabort_8_4_8(
3330 struct net *net,
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003331 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08003333 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 void *arg,
Xin Longa85bbeb2017-08-11 10:23:52 +08003335 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336{
3337 struct sctp_packet *packet = NULL;
3338 struct sctp_chunk *chunk = arg;
3339 struct sctp_chunk *abort;
3340
Eric W. Biederman2ce95502012-08-06 08:43:06 +00003341 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Marcelo Ricardo Leitnereab59072016-12-28 09:26:31 -02003342 if (!packet)
3343 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344
Marcelo Ricardo Leitnereab59072016-12-28 09:26:31 -02003345 /* Make an ABORT. The T bit will be set if the asoc
3346 * is NULL.
3347 */
3348 abort = sctp_make_abort(asoc, chunk, 0);
3349 if (!abort) {
3350 sctp_ootb_pkt_free(packet);
3351 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 }
3353
Marcelo Ricardo Leitnereab59072016-12-28 09:26:31 -02003354 /* Reflect vtag if T-Bit is set */
3355 if (sctp_test_T_bit(abort))
3356 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3357
3358 /* Set the skb to the belonging sock for accounting. */
3359 abort->skb->sk = ep->base.sk;
3360
3361 sctp_packet_append_chunk(packet, abort);
3362
3363 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3364 SCTP_PACKET(packet));
3365
3366 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3367
3368 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3369 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370}
3371
3372/*
3373 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3374 * event as ULP notification for each cause included in the chunk.
3375 *
3376 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3377 *
3378 * The return value is the disposition of the chunk.
3379*/
Xin Long172a1592017-08-11 10:23:57 +08003380enum sctp_disposition sctp_sf_operr_notify(struct net *net,
3381 const struct sctp_endpoint *ep,
3382 const struct sctp_association *asoc,
3383 const union sctp_subtype type,
3384 void *arg,
3385 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386{
3387 struct sctp_chunk *chunk = arg;
Xin Longd8238d92017-08-03 15:42:11 +08003388 struct sctp_errhdr *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
3390 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003391 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
3393 /* Make sure that the ERROR chunk has a valid length. */
Xin Long87caeba2017-08-03 15:42:12 +08003394 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_operr_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003395 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 commands);
Shan Wei8a00be12011-04-19 21:25:40 +00003397 sctp_walk_errors(err, chunk->chunk_hdr);
3398 if ((void *)err != (void *)chunk->chunk_end)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003399 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Shan Wei8a00be12011-04-19 21:25:40 +00003400 (void *)err, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401
Wei Yongjun3df267872009-03-02 06:46:51 +00003402 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3403 SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406}
3407
3408/*
3409 * Process an inbound SHUTDOWN ACK.
3410 *
3411 * From Section 9.2:
3412 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3413 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3414 * peer, and remove all record of the association.
3415 *
3416 * The return value is the disposition.
3417 */
Xin Long172a1592017-08-11 10:23:57 +08003418enum sctp_disposition sctp_sf_do_9_2_final(struct net *net,
3419 const struct sctp_endpoint *ep,
3420 const struct sctp_association *asoc,
3421 const union sctp_subtype type,
3422 void *arg,
3423 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424{
3425 struct sctp_chunk *chunk = arg;
3426 struct sctp_chunk *reply;
3427 struct sctp_ulpevent *ev;
3428
3429 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003430 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431
3432 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
Xin Long922dbc52017-06-30 11:52:13 +08003433 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003434 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 /* 10.2 H) SHUTDOWN COMPLETE notification
3437 *
3438 * When SCTP completes the shutdown procedures (section 9.2) this
3439 * notification is passed to the upper layer.
3440 */
3441 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07003442 0, 0, 0, NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 if (!ev)
3444 goto nomem;
3445
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003446 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3447 reply = sctp_make_shutdown_complete(asoc, chunk);
3448 if (!reply)
3449 goto nomem_chunk;
3450
3451 /* Do all the commands now (after allocation), so that we
3452 * have consistent state if memory allocation failes
3453 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3455
3456 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3457 * stop the T2-shutdown timer,
3458 */
3459 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3460 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3461
3462 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3463 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3464
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3466 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003467 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3468 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3470
3471 /* ...and remove all record of the association. */
3472 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3473 return SCTP_DISPOSITION_DELETE_TCB;
3474
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003475nomem_chunk:
3476 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477nomem:
3478 return SCTP_DISPOSITION_NOMEM;
3479}
3480
3481/*
Jerome Forissier047a2422005-04-28 11:58:43 -07003482 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3483 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3485 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3486 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3487 * packet must fill in the Verification Tag field of the outbound
3488 * packet with the Verification Tag received in the SHUTDOWN ACK and
Jerome Forissier047a2422005-04-28 11:58:43 -07003489 * set the T-bit in the Chunk Flags to indicate that the Verification
3490 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 *
3492 * 8) The receiver should respond to the sender of the OOTB packet with
3493 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3494 * MUST fill in the Verification Tag field of the outbound packet
3495 * with the value found in the Verification Tag field of the OOTB
Jerome Forissier047a2422005-04-28 11:58:43 -07003496 * packet and set the T-bit in the Chunk Flags to indicate that the
3497 * Verification Tag is reflected. After sending this ABORT, the
3498 * receiver of the OOTB packet shall discard the OOTB packet and take
3499 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 */
Xin Long172a1592017-08-11 10:23:57 +08003501enum sctp_disposition sctp_sf_ootb(struct net *net,
3502 const struct sctp_endpoint *ep,
3503 const struct sctp_association *asoc,
3504 const union sctp_subtype type,
3505 void *arg, struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506{
3507 struct sctp_chunk *chunk = arg;
3508 struct sk_buff *skb = chunk->skb;
Xin Long922dbc52017-06-30 11:52:13 +08003509 struct sctp_chunkhdr *ch;
Xin Longd8238d92017-08-03 15:42:11 +08003510 struct sctp_errhdr *err;
Shan Wei85c5ed42011-04-19 21:30:01 +00003511 int ootb_cookie_ack = 0;
Xin Long172a1592017-08-11 10:23:57 +08003512 int ootb_shut_ack = 0;
3513 __u8 *ch_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003515 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
Xin Long922dbc52017-06-30 11:52:13 +08003517 ch = (struct sctp_chunkhdr *)chunk->chunk_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 do {
Vlad Yasevichece25df2007-09-07 16:30:54 -04003519 /* Report violation if the chunk is less then minimal */
Xin Long922dbc52017-06-30 11:52:13 +08003520 if (ntohs(ch->length) < sizeof(*ch))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003521 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003522 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523
Marcelo Ricardo Leitnerbf911e92016-10-25 14:27:39 -02003524 /* Report violation if chunk len overflows */
3525 ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
3526 if (ch_end > skb_tail_pointer(skb))
3527 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3528 commands);
3529
Vlad Yasevichece25df2007-09-07 16:30:54 -04003530 /* Now that we know we at least have a chunk header,
3531 * do things that are type appropriate.
3532 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3534 ootb_shut_ack = 1;
3535
3536 /* RFC 2960, Section 3.3.7
3537 * Moreover, under any circumstances, an endpoint that
3538 * receives an ABORT MUST NOT respond to that ABORT by
3539 * sending an ABORT of its own.
3540 */
3541 if (SCTP_CID_ABORT == ch->type)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003542 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003543
Shan Wei85c5ed42011-04-19 21:30:01 +00003544 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3545 * or a COOKIE ACK the SCTP Packet should be silently
3546 * discarded.
3547 */
3548
3549 if (SCTP_CID_COOKIE_ACK == ch->type)
3550 ootb_cookie_ack = 1;
3551
3552 if (SCTP_CID_ERROR == ch->type) {
3553 sctp_walk_errors(err, ch) {
3554 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3555 ootb_cookie_ack = 1;
3556 break;
3557 }
3558 }
3559 }
3560
Xin Long922dbc52017-06-30 11:52:13 +08003561 ch = (struct sctp_chunkhdr *)ch_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003562 } while (ch_end < skb_tail_pointer(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
3564 if (ootb_shut_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003565 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
Shan Wei85c5ed42011-04-19 21:30:01 +00003566 else if (ootb_cookie_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003567 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003569 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570}
3571
3572/*
3573 * Handle an "Out of the blue" SHUTDOWN ACK.
3574 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003575 * Section: 8.4 5, sctpimpguide 2.41.
3576 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
Jerome Forissier047a2422005-04-28 11:58:43 -07003578 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3579 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3580 * packet must fill in the Verification Tag field of the outbound
3581 * packet with the Verification Tag received in the SHUTDOWN ACK and
3582 * set the T-bit in the Chunk Flags to indicate that the Verification
3583 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 *
3585 * Inputs
3586 * (endpoint, asoc, type, arg, commands)
3587 *
3588 * Outputs
Xin Long172a1592017-08-11 10:23:57 +08003589 * (enum sctp_disposition)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 *
3591 * The return value is the disposition of the chunk.
3592 */
Xin Long172a1592017-08-11 10:23:57 +08003593static enum sctp_disposition sctp_sf_shut_8_4_5(
3594 struct net *net,
3595 const struct sctp_endpoint *ep,
3596 const struct sctp_association *asoc,
3597 const union sctp_subtype type,
3598 void *arg,
3599 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600{
3601 struct sctp_packet *packet = NULL;
3602 struct sctp_chunk *chunk = arg;
3603 struct sctp_chunk *shut;
3604
Eric W. Biederman2ce95502012-08-06 08:43:06 +00003605 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Marcelo Ricardo Leitner1ff01562016-12-28 09:26:32 -02003606 if (!packet)
3607 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608
Marcelo Ricardo Leitner1ff01562016-12-28 09:26:32 -02003609 /* Make an SHUTDOWN_COMPLETE.
3610 * The T bit will be set if the asoc is NULL.
3611 */
3612 shut = sctp_make_shutdown_complete(asoc, chunk);
3613 if (!shut) {
3614 sctp_ootb_pkt_free(packet);
3615 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 }
3617
Marcelo Ricardo Leitner1ff01562016-12-28 09:26:32 -02003618 /* Reflect vtag if T-Bit is set */
3619 if (sctp_test_T_bit(shut))
3620 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3621
3622 /* Set the skb to the belonging sock for accounting. */
3623 shut->skb->sk = ep->base.sk;
3624
3625 sctp_packet_append_chunk(packet, shut);
3626
3627 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3628 SCTP_PACKET(packet));
3629
3630 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3631
3632 /* If the chunk length is invalid, we don't want to process
3633 * the reset of the packet.
3634 */
Xin Long922dbc52017-06-30 11:52:13 +08003635 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Marcelo Ricardo Leitner1ff01562016-12-28 09:26:32 -02003636 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3637
3638 /* We need to discard the rest of the packet to prevent
3639 * potential bomming attacks from additional bundled chunks.
3640 * This is documented in SCTP Threats ID.
3641 */
3642 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643}
3644
3645/*
3646 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3647 *
3648 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3649 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3650 * procedures in section 8.4 SHOULD be followed, in other words it
3651 * should be treated as an Out Of The Blue packet.
3652 * [This means that we do NOT check the Verification Tag on these
3653 * chunks. --piggy ]
3654 *
3655 */
Xin Long172a1592017-08-11 10:23:57 +08003656enum sctp_disposition sctp_sf_do_8_5_1_E_sa(struct net *net,
3657 const struct sctp_endpoint *ep,
3658 const struct sctp_association *asoc,
3659 const union sctp_subtype type,
3660 void *arg,
3661 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662{
Vlad Yasevichece25df2007-09-07 16:30:54 -04003663 struct sctp_chunk *chunk = arg;
3664
3665 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
Xin Long922dbc52017-06-30 11:52:13 +08003666 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003667 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003668 commands);
3669
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 /* Although we do have an association in this case, it corresponds
3671 * to a restarted association. So the packet is treated as an OOTB
3672 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3673 * called with a NULL association.
3674 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003675 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Wei Yongjun8190f892008-09-08 12:13:55 +08003676
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003677 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678}
3679
3680/* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
Xin Long172a1592017-08-11 10:23:57 +08003681enum sctp_disposition sctp_sf_do_asconf(struct net *net,
3682 const struct sctp_endpoint *ep,
3683 const struct sctp_association *asoc,
3684 const union sctp_subtype type,
3685 void *arg,
3686 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687{
Xin Long172a1592017-08-11 10:23:57 +08003688 struct sctp_paramhdr *err_param = NULL;
3689 struct sctp_chunk *asconf_ack = NULL;
3690 struct sctp_chunk *chunk = arg;
3691 struct sctp_addiphdr *hdr;
3692 __u32 serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693
3694 if (!sctp_vtag_verify(chunk, asoc)) {
3695 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3696 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003697 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 }
3699
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003700 /* ADD-IP: Section 4.1.1
3701 * This chunk MUST be sent in an authenticated way by using
3702 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3703 * is received unauthenticated it MUST be silently discarded as
3704 * described in [I-D.ietf-tsvwg-sctp-auth].
3705 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003706 if (!net->sctp.addip_noauth && !chunk->auth)
Xin Long68d75462017-08-03 15:42:20 +08003707 return sctp_sf_discard_chunk(net, ep, asoc, type, arg,
3708 commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003709
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
Xin Long68d75462017-08-03 15:42:20 +08003711 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_addip_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003712 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 commands);
3714
Xin Long65205cc2017-08-03 15:42:19 +08003715 hdr = (struct sctp_addiphdr *)chunk->skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 serial = ntohl(hdr->serial);
3717
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003718 /* Verify the ASCONF chunk before processing it. */
Daniel Borkmann9de79222014-10-09 22:55:31 +02003719 if (!sctp_verify_asconf(asoc, chunk, true, &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003720 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003721 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003722
Vlad Yasevicha08de642007-12-20 14:11:47 -08003723 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 * the endpoint stored in a new association variable
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003725 * 'Peer-Serial-Number'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 */
3727 if (serial == asoc->peer.addip_serial + 1) {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003728 /* If this is the first instance of ASCONF in the packet,
3729 * we can clean our old ASCONF-ACKs.
3730 */
3731 if (!chunk->has_asconf)
3732 sctp_assoc_clean_asconf_ack_cache(asoc);
3733
3734 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3735 * expected, process the ASCONF as described below and after
3736 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3737 * the response packet and cache a copy of it (in the event it
3738 * later needs to be retransmitted).
3739 *
3740 * Essentially, do V1-V5.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 */
3742 asconf_ack = sctp_process_asconf((struct sctp_association *)
3743 asoc, chunk);
3744 if (!asconf_ack)
3745 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevicha08de642007-12-20 14:11:47 -08003746 } else if (serial < asoc->peer.addip_serial + 1) {
3747 /* ADDIP 5.2 E2)
3748 * If the value found in the Sequence Number is less than the
3749 * ('Peer- Sequence-Number' + 1), simply skip to the next
3750 * ASCONF, and include in the outbound response packet
3751 * any previously cached ASCONF-ACK response that was
3752 * sent and saved that matches the Sequence Number of the
3753 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3754 * Chunk exists. This will occur when an older ASCONF
3755 * arrives out of order. In such a case, the receiver
3756 * should skip the ASCONF Chunk and not include ASCONF-ACK
3757 * Chunk for that chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003759 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3760 if (!asconf_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 return SCTP_DISPOSITION_DISCARD;
Vlad Yasevich31b02e12009-09-04 18:21:00 -04003762
3763 /* Reset the transport so that we select the correct one
3764 * this time around. This is to make sure that we don't
3765 * accidentally use a stale transport that's been removed.
3766 */
3767 asconf_ack->transport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 } else {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003769 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 * it must be either a stale packet or from an attacker.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003771 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 return SCTP_DISPOSITION_DISCARD;
3773 }
3774
Vlad Yasevicha08de642007-12-20 14:11:47 -08003775 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3776 * containing the ASCONF-ACK Chunks MUST be the source address of
3777 * the SCTP packet that held the ASCONF Chunks.
3778 *
3779 * To do this properly, we'll set the destination address of the chunk
3780 * and at the transmit time, will try look up the transport to use.
3781 * Since ASCONFs may be bundled, the correct transport may not be
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +02003782 * created until we process the entire packet, thus this workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003784 asconf_ack->dest = chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
Michio Honda6af29cc2011-06-16 17:14:34 +09003786 if (asoc->new_transport) {
wangweidongf7010e62013-12-23 12:16:52 +08003787 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport, commands);
Michio Honda6af29cc2011-06-16 17:14:34 +09003788 ((struct sctp_association *)asoc)->new_transport = NULL;
3789 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003790
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 return SCTP_DISPOSITION_CONSUME;
3792}
3793
3794/*
3795 * ADDIP Section 4.3 General rules for address manipulation
3796 * When building TLV parameters for the ASCONF Chunk that will add or
3797 * delete IP addresses the D0 to D13 rules should be applied:
3798 */
Xin Long172a1592017-08-11 10:23:57 +08003799enum sctp_disposition sctp_sf_do_asconf_ack(struct net *net,
3800 const struct sctp_endpoint *ep,
3801 const struct sctp_association *asoc,
3802 const union sctp_subtype type,
3803 void *arg,
3804 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805{
Xin Long172a1592017-08-11 10:23:57 +08003806 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3807 struct sctp_paramhdr *err_param = NULL;
3808 struct sctp_chunk *asconf_ack = arg;
3809 struct sctp_addiphdr *addip_hdr;
3810 __u32 sent_serial, rcvd_serial;
3811 struct sctp_chunk *abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812
3813 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3814 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3815 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003816 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 }
3818
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003819 /* ADD-IP, Section 4.1.2:
3820 * This chunk MUST be sent in an authenticated way by using
3821 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3822 * is received unauthenticated it MUST be silently discarded as
3823 * described in [I-D.ietf-tsvwg-sctp-auth].
3824 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003825 if (!net->sctp.addip_noauth && !asconf_ack->auth)
Xin Long68d75462017-08-03 15:42:20 +08003826 return sctp_sf_discard_chunk(net, ep, asoc, type, arg,
3827 commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003828
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 /* Make sure that the ADDIP chunk has a valid length. */
Xin Long68d75462017-08-03 15:42:20 +08003830 if (!sctp_chunk_length_valid(asconf_ack,
3831 sizeof(struct sctp_addip_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003832 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 commands);
3834
Xin Long65205cc2017-08-03 15:42:19 +08003835 addip_hdr = (struct sctp_addiphdr *)asconf_ack->skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 rcvd_serial = ntohl(addip_hdr->serial);
3837
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003838 /* Verify the ASCONF-ACK chunk before processing it. */
Daniel Borkmann9de79222014-10-09 22:55:31 +02003839 if (!sctp_verify_asconf(asoc, asconf_ack, false, &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003840 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003841 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003842
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 if (last_asconf) {
Xin Long65205cc2017-08-03 15:42:19 +08003844 addip_hdr = (struct sctp_addiphdr *)last_asconf->subh.addip_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 sent_serial = ntohl(addip_hdr->serial);
3846 } else {
3847 sent_serial = asoc->addip_serial - 1;
3848 }
3849
3850 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3851 * equal to the next serial number to be used but no ASCONF chunk is
3852 * outstanding the endpoint MUST ABORT the association. Note that a
3853 * sequence number is greater than if it is no more than 2^^31-1
3854 * larger than the current sequence number (using serial arithmetic).
3855 */
3856 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3857 !(asoc->addip_last_asconf)) {
3858 abort = sctp_make_abort(asoc, asconf_ack,
Xin Longd8238d92017-08-03 15:42:11 +08003859 sizeof(struct sctp_errhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003861 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3863 SCTP_CHUNK(abort));
3864 }
3865 /* We are going to ABORT, so we might as well stop
3866 * processing the rest of the chunks in the packet.
3867 */
3868 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3869 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
wangweidongcb3f8372013-12-23 12:16:50 +08003870 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003871 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003872 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003874 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003875 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3876 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 return SCTP_DISPOSITION_ABORT;
3878 }
3879
3880 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3881 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3882 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3883
3884 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
Vlad Yasevichc0786692010-04-28 08:47:22 +00003885 asconf_ack)) {
3886 /* Successfully processed ASCONF_ACK. We can
3887 * release the next asconf if we have one.
3888 */
3889 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3890 SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 return SCTP_DISPOSITION_CONSUME;
Vlad Yasevichc0786692010-04-28 08:47:22 +00003892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893
3894 abort = sctp_make_abort(asoc, asconf_ack,
Xin Longd8238d92017-08-03 15:42:11 +08003895 sizeof(struct sctp_errhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003897 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3899 SCTP_CHUNK(abort));
3900 }
3901 /* We are going to ABORT, so we might as well stop
3902 * processing the rest of the chunks in the packet.
3903 */
wangweidongcb3f8372013-12-23 12:16:50 +08003904 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003905 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003906 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003908 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003909 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3910 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 return SCTP_DISPOSITION_ABORT;
3912 }
3913
3914 return SCTP_DISPOSITION_DISCARD;
3915}
3916
Xin Long2040d3d2017-02-17 12:45:42 +08003917/* RE-CONFIG Section 5.2 Upon reception of an RECONF Chunk. */
Xin Long172a1592017-08-11 10:23:57 +08003918enum sctp_disposition sctp_sf_do_reconf(struct net *net,
3919 const struct sctp_endpoint *ep,
3920 const struct sctp_association *asoc,
3921 const union sctp_subtype type,
3922 void *arg,
3923 struct sctp_cmd_seq *commands)
Xin Long2040d3d2017-02-17 12:45:42 +08003924{
3925 struct sctp_paramhdr *err_param = NULL;
3926 struct sctp_chunk *chunk = arg;
3927 struct sctp_reconf_chunk *hdr;
3928 union sctp_params param;
3929
3930 if (!sctp_vtag_verify(chunk, asoc)) {
3931 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3932 SCTP_NULL());
3933 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3934 }
3935
3936 /* Make sure that the RECONF chunk has a valid length. */
3937 if (!sctp_chunk_length_valid(chunk, sizeof(*hdr)))
3938 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3939 commands);
3940
3941 if (!sctp_verify_reconf(asoc, chunk, &err_param))
3942 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3943 (void *)err_param, commands);
3944
3945 hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
3946 sctp_walk_params(param, hdr, params) {
3947 struct sctp_chunk *reply = NULL;
3948 struct sctp_ulpevent *ev = NULL;
3949
3950 if (param.p->type == SCTP_PARAM_RESET_OUT_REQUEST)
3951 reply = sctp_process_strreset_outreq(
3952 (struct sctp_association *)asoc, param, &ev);
3953 else if (param.p->type == SCTP_PARAM_RESET_IN_REQUEST)
3954 reply = sctp_process_strreset_inreq(
3955 (struct sctp_association *)asoc, param, &ev);
Xin Long692787c2017-03-10 12:11:07 +08003956 else if (param.p->type == SCTP_PARAM_RESET_TSN_REQUEST)
3957 reply = sctp_process_strreset_tsnreq(
3958 (struct sctp_association *)asoc, param, &ev);
Xin Long50a41592017-03-10 12:11:09 +08003959 else if (param.p->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS)
3960 reply = sctp_process_strreset_addstrm_out(
3961 (struct sctp_association *)asoc, param, &ev);
Xin Longc5c4ebb2017-03-10 12:11:10 +08003962 else if (param.p->type == SCTP_PARAM_RESET_ADD_IN_STREAMS)
3963 reply = sctp_process_strreset_addstrm_in(
3964 (struct sctp_association *)asoc, param, &ev);
Xin Long11ae76e2017-03-10 12:11:11 +08003965 else if (param.p->type == SCTP_PARAM_RESET_RESPONSE)
3966 reply = sctp_process_strreset_resp(
3967 (struct sctp_association *)asoc, param, &ev);
Xin Long2040d3d2017-02-17 12:45:42 +08003968
3969 if (ev)
3970 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
3971 SCTP_ULPEVENT(ev));
3972
3973 if (reply)
3974 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3975 SCTP_CHUNK(reply));
3976 }
3977
3978 return SCTP_DISPOSITION_CONSUME;
3979}
3980
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981/*
3982 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3983 *
3984 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3985 * its cumulative TSN point to the value carried in the FORWARD TSN
3986 * chunk, and then MUST further advance its cumulative TSN point locally
3987 * if possible.
3988 * After the above processing, the data receiver MUST stop reporting any
3989 * missing TSNs earlier than or equal to the new cumulative TSN point.
3990 *
3991 * Verification Tag: 8.5 Verification Tag [Normal verification]
3992 *
3993 * The return value is the disposition of the chunk.
3994 */
Xin Long172a1592017-08-11 10:23:57 +08003995enum sctp_disposition sctp_sf_eat_fwd_tsn(struct net *net,
3996 const struct sctp_endpoint *ep,
3997 const struct sctp_association *asoc,
3998 const union sctp_subtype type,
3999 void *arg,
4000 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Xin Long172a1592017-08-11 10:23:57 +08004003 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 __u16 len;
4005 __u32 tsn;
4006
4007 if (!sctp_vtag_verify(chunk, asoc)) {
4008 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4009 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004010 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 }
4012
Xin Longd15c9ed2017-02-03 17:37:06 +08004013 if (!asoc->peer.prsctp_capable)
4014 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4015
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 /* Make sure that the FORWARD_TSN chunk has valid length. */
Xin Long0fc2ea92017-12-15 00:41:27 +08004017 if (!sctp_chunk_length_valid(chunk, sctp_ftsnchk_len(&asoc->stream)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004018 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 commands);
4020
4021 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
4022 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
4023 len = ntohs(chunk->chunk_hdr->length);
4024 len -= sizeof(struct sctp_chunkhdr);
4025 skb_pull(chunk->skb, len);
4026
4027 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02004028 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
4030 /* The TSN is too high--silently discard the chunk and count on it
4031 * getting retransmitted later.
4032 */
4033 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
4034 goto discard_noforce;
4035
Xin Long0fc2ea92017-12-15 00:41:27 +08004036 if (!asoc->stream.si->validate_ftsn(chunk))
4037 goto discard_noforce;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08004038
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
Xin Long0fc2ea92017-12-15 00:41:27 +08004040 if (len > sctp_ftsnhdr_len(&asoc->stream))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004041 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004043
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 /* Count this as receiving DATA. */
Neil Horman9f70f462013-12-10 06:48:15 -05004045 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4047 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
4048 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004049
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 /* FIXME: For now send a SACK, but DATA processing may
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004051 * send another.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 */
4053 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054
4055 return SCTP_DISPOSITION_CONSUME;
4056
4057discard_noforce:
4058 return SCTP_DISPOSITION_DISCARD;
4059}
4060
Xin Long172a1592017-08-11 10:23:57 +08004061enum sctp_disposition sctp_sf_eat_fwd_tsn_fast(
4062 struct net *net,
4063 const struct sctp_endpoint *ep,
4064 const struct sctp_association *asoc,
4065 const union sctp_subtype type,
4066 void *arg,
4067 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Xin Long172a1592017-08-11 10:23:57 +08004070 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 __u16 len;
4072 __u32 tsn;
4073
4074 if (!sctp_vtag_verify(chunk, asoc)) {
4075 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4076 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004077 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 }
4079
Xin Longd15c9ed2017-02-03 17:37:06 +08004080 if (!asoc->peer.prsctp_capable)
4081 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4082
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 /* Make sure that the FORWARD_TSN chunk has a valid length. */
Xin Long0fc2ea92017-12-15 00:41:27 +08004084 if (!sctp_chunk_length_valid(chunk, sctp_ftsnchk_len(&asoc->stream)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004085 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 commands);
4087
4088 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
4089 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
4090 len = ntohs(chunk->chunk_hdr->length);
4091 len -= sizeof(struct sctp_chunkhdr);
4092 skb_pull(chunk->skb, len);
4093
4094 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02004095 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096
4097 /* The TSN is too high--silently discard the chunk and count on it
4098 * getting retransmitted later.
4099 */
4100 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
4101 goto gen_shutdown;
4102
Xin Long0fc2ea92017-12-15 00:41:27 +08004103 if (!asoc->stream.si->validate_ftsn(chunk))
4104 goto gen_shutdown;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08004105
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
Xin Long0fc2ea92017-12-15 00:41:27 +08004107 if (len > sctp_ftsnhdr_len(&asoc->stream))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004108 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004110
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 /* Go a head and force a SACK, since we are shutting down. */
4112gen_shutdown:
4113 /* Implementor's Guide.
4114 *
4115 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
4116 * respond to each received packet containing one or more DATA chunk(s)
4117 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
4118 */
4119 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
4120 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
4121 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4122 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4123
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004124 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125}
4126
4127/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004128 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004129 *
4130 * The receiver MUST use the HMAC algorithm indicated in the HMAC
4131 * Identifier field. If this algorithm was not specified by the
4132 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
4133 * during association setup, the AUTH chunk and all chunks after it MUST
4134 * be discarded and an ERROR chunk SHOULD be sent with the error cause
4135 * defined in Section 4.1.
4136 *
4137 * If an endpoint with no shared key receives a Shared Key Identifier
4138 * other than 0, it MUST silently discard all authenticated chunks. If
4139 * the endpoint has at least one endpoint pair shared key for the peer,
4140 * it MUST use the key specified by the Shared Key Identifier if a
4141 * key has been configured for that Shared Key Identifier. If no
4142 * endpoint pair shared key has been configured for that Shared Key
4143 * Identifier, all authenticated chunks MUST be silently discarded.
4144 *
4145 * Verification Tag: 8.5 Verification Tag [Normal verification]
4146 *
4147 * The return value is the disposition of the chunk.
4148 */
Xin Long4785c7a2017-08-05 19:59:58 +08004149static enum sctp_ierror sctp_sf_authenticate(
Xin Long172a1592017-08-11 10:23:57 +08004150 struct net *net,
4151 const struct sctp_endpoint *ep,
4152 const struct sctp_association *asoc,
4153 const union sctp_subtype type,
4154 struct sctp_chunk *chunk)
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004155{
Xin Long1b1e0bc2018-03-14 19:05:30 +08004156 struct sctp_shared_key *sh_key = NULL;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004157 struct sctp_authhdr *auth_hdr;
Xin Long172a1592017-08-11 10:23:57 +08004158 __u8 *save_digest, *digest;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004159 struct sctp_hmac *hmac;
4160 unsigned int sig_len;
4161 __u16 key_id;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004162
4163 /* Pull in the auth header, so we can do some more verification */
4164 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4165 chunk->subh.auth_hdr = auth_hdr;
Xin Longbb96dec2017-08-03 15:42:22 +08004166 skb_pull(chunk->skb, sizeof(*auth_hdr));
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004167
Masanari Iida02582e92012-08-22 19:11:26 +09004168 /* Make sure that we support the HMAC algorithm from the auth
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004169 * chunk.
4170 */
4171 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4172 return SCTP_IERROR_AUTH_BAD_HMAC;
4173
4174 /* Make sure that the provided shared key identifier has been
4175 * configured
4176 */
4177 key_id = ntohs(auth_hdr->shkey_id);
Xin Long1b1e0bc2018-03-14 19:05:30 +08004178 if (key_id != asoc->active_key_id) {
4179 sh_key = sctp_auth_get_shkey(asoc, key_id);
4180 if (!sh_key)
4181 return SCTP_IERROR_AUTH_BAD_KEYID;
4182 }
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004183
4184 /* Make sure that the length of the signature matches what
4185 * we expect.
4186 */
Xin Longbb96dec2017-08-03 15:42:22 +08004187 sig_len = ntohs(chunk->chunk_hdr->length) -
4188 sizeof(struct sctp_auth_chunk);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004189 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4190 if (sig_len != hmac->hmac_len)
4191 return SCTP_IERROR_PROTO_VIOLATION;
4192
4193 /* Now that we've done validation checks, we can compute and
4194 * verify the hmac. The steps involved are:
4195 * 1. Save the digest from the chunk.
4196 * 2. Zero out the digest in the chunk.
4197 * 3. Compute the new digest
4198 * 4. Compare saved and new digests.
4199 */
4200 digest = auth_hdr->hmac;
4201 skb_pull(chunk->skb, sig_len);
4202
4203 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4204 if (!save_digest)
4205 goto nomem;
4206
4207 memset(digest, 0, sig_len);
4208
4209 sctp_auth_calculate_hmac(asoc, chunk->skb,
Xin Longbb96dec2017-08-03 15:42:22 +08004210 (struct sctp_auth_chunk *)chunk->chunk_hdr,
Xin Long1b1e0bc2018-03-14 19:05:30 +08004211 sh_key, GFP_ATOMIC);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004212
4213 /* Discard the packet if the digests do not match */
4214 if (memcmp(save_digest, digest, sig_len)) {
4215 kfree(save_digest);
4216 return SCTP_IERROR_BAD_SIG;
4217 }
4218
4219 kfree(save_digest);
4220 chunk->auth = 1;
4221
4222 return SCTP_IERROR_NO_ERROR;
4223nomem:
4224 return SCTP_IERROR_NOMEM;
4225}
4226
Xin Long172a1592017-08-11 10:23:57 +08004227enum sctp_disposition sctp_sf_eat_auth(struct net *net,
4228 const struct sctp_endpoint *ep,
4229 const struct sctp_association *asoc,
4230 const union sctp_subtype type,
4231 void *arg, struct sctp_cmd_seq *commands)
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004232{
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004233 struct sctp_chunk *chunk = arg;
Xin Long4785c7a2017-08-05 19:59:58 +08004234 struct sctp_authhdr *auth_hdr;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004235 struct sctp_chunk *err_chunk;
Xin Long4785c7a2017-08-05 19:59:58 +08004236 enum sctp_ierror error;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004237
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004238 /* Make sure that the peer has AUTH capable */
4239 if (!asoc->peer.auth_capable)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004240 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004241
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004242 if (!sctp_vtag_verify(chunk, asoc)) {
4243 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4244 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004245 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004246 }
4247
4248 /* Make sure that the AUTH chunk has valid length. */
4249 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004250 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004251 commands);
4252
4253 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004254 error = sctp_sf_authenticate(net, ep, asoc, type, chunk);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004255 switch (error) {
Joe Perches7fd71b12011-07-01 09:43:11 +00004256 case SCTP_IERROR_AUTH_BAD_HMAC:
4257 /* Generate the ERROR chunk and discard the rest
4258 * of the packet
4259 */
4260 err_chunk = sctp_make_op_error(asoc, chunk,
4261 SCTP_ERROR_UNSUP_HMAC,
4262 &auth_hdr->hmac_id,
4263 sizeof(__u16), 0);
4264 if (err_chunk) {
4265 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4266 SCTP_CHUNK(err_chunk));
4267 }
4268 /* Fall Through */
4269 case SCTP_IERROR_AUTH_BAD_KEYID:
4270 case SCTP_IERROR_BAD_SIG:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004271 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Joe Perches7fd71b12011-07-01 09:43:11 +00004272
4273 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004274 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Joe Perches7fd71b12011-07-01 09:43:11 +00004275 commands);
4276
4277 case SCTP_IERROR_NOMEM:
4278 return SCTP_DISPOSITION_NOMEM;
4279
4280 default: /* Prevent gcc warnings */
4281 break;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004282 }
4283
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004284 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4285 struct sctp_ulpevent *ev;
4286
4287 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
Xin Longec2e5062018-03-14 19:05:33 +08004288 SCTP_AUTH_NEW_KEY, GFP_ATOMIC);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004289
4290 if (!ev)
4291 return -ENOMEM;
4292
4293 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4294 SCTP_ULPEVENT(ev));
4295 }
4296
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004297 return SCTP_DISPOSITION_CONSUME;
4298}
4299
4300/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 * Process an unknown chunk.
4302 *
4303 * Section: 3.2. Also, 2.1 in the implementor's guide.
4304 *
4305 * Chunk Types are encoded such that the highest-order two bits specify
4306 * the action that must be taken if the processing endpoint does not
4307 * recognize the Chunk Type.
4308 *
4309 * 00 - Stop processing this SCTP packet and discard it, do not process
4310 * any further chunks within it.
4311 *
4312 * 01 - Stop processing this SCTP packet and discard it, do not process
4313 * any further chunks within it, and report the unrecognized
4314 * chunk in an 'Unrecognized Chunk Type'.
4315 *
4316 * 10 - Skip this chunk and continue processing.
4317 *
4318 * 11 - Skip this chunk and continue processing, but report in an ERROR
4319 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4320 *
4321 * The return value is the disposition of the chunk.
4322 */
Xin Long172a1592017-08-11 10:23:57 +08004323enum sctp_disposition sctp_sf_unk_chunk(struct net *net,
4324 const struct sctp_endpoint *ep,
4325 const struct sctp_association *asoc,
4326 const union sctp_subtype type,
4327 void *arg,
4328 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329{
4330 struct sctp_chunk *unk_chunk = arg;
4331 struct sctp_chunk *err_chunk;
Xin Long922dbc52017-06-30 11:52:13 +08004332 struct sctp_chunkhdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333
Daniel Borkmannbb333812013-06-28 19:49:40 +02004334 pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335
4336 if (!sctp_vtag_verify(unk_chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004337 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338
4339 /* Make sure that the chunk has a valid length.
4340 * Since we don't know the chunk type, we use a general
4341 * chunkhdr structure to make a comparison.
4342 */
Xin Long922dbc52017-06-30 11:52:13 +08004343 if (!sctp_chunk_length_valid(unk_chunk, sizeof(*hdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004344 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 commands);
4346
4347 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4348 case SCTP_CID_ACTION_DISCARD:
4349 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004350 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 case SCTP_CID_ACTION_DISCARD_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 /* Generate an ERROR chunk as response. */
4353 hdr = unk_chunk->chunk_hdr;
4354 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4355 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -03004356 SCTP_PAD4(ntohs(hdr->length)),
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004357 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 if (err_chunk) {
4359 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4360 SCTP_CHUNK(err_chunk));
4361 }
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07004362
4363 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004364 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 case SCTP_CID_ACTION_SKIP:
4367 /* Skip the chunk. */
4368 return SCTP_DISPOSITION_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 case SCTP_CID_ACTION_SKIP_ERR:
4370 /* Generate an ERROR chunk as response. */
4371 hdr = unk_chunk->chunk_hdr;
4372 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4373 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -03004374 SCTP_PAD4(ntohs(hdr->length)),
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004375 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 if (err_chunk) {
4377 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4378 SCTP_CHUNK(err_chunk));
4379 }
4380 /* Skip the chunk. */
4381 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 default:
4383 break;
4384 }
4385
4386 return SCTP_DISPOSITION_DISCARD;
4387}
4388
4389/*
4390 * Discard the chunk.
4391 *
4392 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4393 * [Too numerous to mention...]
4394 * Verification Tag: No verification needed.
4395 * Inputs
4396 * (endpoint, asoc, chunk)
4397 *
4398 * Outputs
4399 * (asoc, reply_msg, msg_up, timers, counters)
4400 *
4401 * The return value is the disposition of the chunk.
4402 */
Xin Long172a1592017-08-11 10:23:57 +08004403enum sctp_disposition sctp_sf_discard_chunk(struct net *net,
4404 const struct sctp_endpoint *ep,
4405 const struct sctp_association *asoc,
4406 const union sctp_subtype type,
4407 void *arg,
4408 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004410 struct sctp_chunk *chunk = arg;
4411
4412 /* Make sure that the chunk has a valid length.
4413 * Since we don't know the chunk type, we use a general
4414 * chunkhdr structure to make a comparison.
4415 */
Xin Long922dbc52017-06-30 11:52:13 +08004416 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004417 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004418 commands);
4419
Daniel Borkmannbb333812013-06-28 19:49:40 +02004420 pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
4421
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 return SCTP_DISPOSITION_DISCARD;
4423}
4424
4425/*
4426 * Discard the whole packet.
4427 *
4428 * Section: 8.4 2)
4429 *
4430 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4431 * silently discard the OOTB packet and take no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 *
4433 * Verification Tag: No verification necessary
4434 *
4435 * Inputs
4436 * (endpoint, asoc, chunk)
4437 *
4438 * Outputs
4439 * (asoc, reply_msg, msg_up, timers, counters)
4440 *
4441 * The return value is the disposition of the chunk.
4442 */
Xin Long172a1592017-08-11 10:23:57 +08004443enum sctp_disposition sctp_sf_pdiscard(struct net *net,
4444 const struct sctp_endpoint *ep,
4445 const struct sctp_association *asoc,
4446 const union sctp_subtype type,
4447 void *arg, struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004449 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4451
4452 return SCTP_DISPOSITION_CONSUME;
4453}
4454
4455
4456/*
4457 * The other end is violating protocol.
4458 *
4459 * Section: Not specified
4460 * Verification Tag: Not specified
4461 * Inputs
4462 * (endpoint, asoc, chunk)
4463 *
4464 * Outputs
4465 * (asoc, reply_msg, msg_up, timers, counters)
4466 *
4467 * We simply tag the chunk as a violation. The state machine will log
4468 * the violation and continue.
4469 */
Xin Long172a1592017-08-11 10:23:57 +08004470enum sctp_disposition sctp_sf_violation(struct net *net,
4471 const struct sctp_endpoint *ep,
4472 const struct sctp_association *asoc,
4473 const union sctp_subtype type,
4474 void *arg,
4475 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004477 struct sctp_chunk *chunk = arg;
4478
4479 /* Make sure that the chunk has a valid length. */
Xin Long922dbc52017-06-30 11:52:13 +08004480 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004481 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004482 commands);
4483
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 return SCTP_DISPOSITION_VIOLATION;
4485}
4486
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487/*
Wei Yongjunaecedea2007-08-02 16:57:44 +08004488 * Common function to handle a protocol violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 */
Xin Long172a1592017-08-11 10:23:57 +08004490static enum sctp_disposition sctp_sf_abort_violation(
4491 struct net *net,
4492 const struct sctp_endpoint *ep,
4493 const struct sctp_association *asoc,
4494 void *arg,
4495 struct sctp_cmd_seq *commands,
4496 const __u8 *payload,
4497 const size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004499 struct sctp_packet *packet = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 struct sctp_chunk *chunk = arg;
4501 struct sctp_chunk *abort = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004503 /* SCTP-AUTH, Section 6.3:
4504 * It should be noted that if the receiver wants to tear
4505 * down an association in an authenticated way only, the
4506 * handling of malformed packets should not result in
4507 * tearing down the association.
4508 *
4509 * This means that if we only want to abort associations
4510 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004511 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004512 * was malformed.
4513 */
4514 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4515 goto discard;
4516
Jesper Juhl9abed242007-11-11 23:57:49 +01004517 /* Make the abort chunk. */
4518 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4519 if (!abort)
4520 goto nomem;
4521
Vlad Yasevichece25df2007-09-07 16:30:54 -04004522 if (asoc) {
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004523 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4524 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4525 !asoc->peer.i.init_tag) {
Xin Longcb1844c2017-07-23 09:34:26 +08004526 struct sctp_initack_chunk *initack;
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004527
Xin Longcb1844c2017-07-23 09:34:26 +08004528 initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
4529 if (!sctp_chunk_length_valid(chunk, sizeof(*initack)))
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004530 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4531 else {
4532 unsigned int inittag;
4533
4534 inittag = ntohl(initack->init_hdr.init_tag);
4535 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4536 SCTP_U32(inittag));
4537 }
4538 }
4539
Vlad Yasevichece25df2007-09-07 16:30:54 -04004540 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004541 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542
Vlad Yasevichece25df2007-09-07 16:30:54 -04004543 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4544 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4545 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4546 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4547 SCTP_ERROR(ECONNREFUSED));
4548 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4549 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4550 } else {
4551 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4552 SCTP_ERROR(ECONNABORTED));
4553 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4554 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004555 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 } else {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00004558 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004559
4560 if (!packet)
4561 goto nomem_pkt;
4562
4563 if (sctp_test_T_bit(abort))
4564 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4565
4566 abort->skb->sk = ep->base.sk;
4567
4568 sctp_packet_append_chunk(packet, abort);
4569
4570 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4571 SCTP_PACKET(packet));
4572
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004573 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 }
4575
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004576 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004577
Vlad Yasevich56eb82b2008-10-09 14:33:01 -07004578discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004579 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 return SCTP_DISPOSITION_ABORT;
4581
Vlad Yasevichece25df2007-09-07 16:30:54 -04004582nomem_pkt:
4583 sctp_chunk_free(abort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584nomem:
4585 return SCTP_DISPOSITION_NOMEM;
4586}
4587
Wei Yongjunaecedea2007-08-02 16:57:44 +08004588/*
4589 * Handle a protocol violation when the chunk length is invalid.
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004590 * "Invalid" length is identified as smaller than the minimal length a
Wei Yongjunaecedea2007-08-02 16:57:44 +08004591 * given chunk can be. For example, a SACK chunk has invalid length
Xin Longd4d6c612017-07-23 09:34:33 +08004592 * if its length is set to be smaller than the size of struct sctp_sack_chunk.
Wei Yongjunaecedea2007-08-02 16:57:44 +08004593 *
4594 * We inform the other end by sending an ABORT with a Protocol Violation
4595 * error code.
4596 *
4597 * Section: Not specified
4598 * Verification Tag: Nothing to do
4599 * Inputs
4600 * (endpoint, asoc, chunk)
4601 *
4602 * Outputs
4603 * (reply_msg, msg_up, counters)
4604 *
4605 * Generate an ABORT chunk and terminate the association.
4606 */
Xin Long172a1592017-08-11 10:23:57 +08004607static enum sctp_disposition sctp_sf_violation_chunklen(
4608 struct net *net,
4609 const struct sctp_endpoint *ep,
4610 const struct sctp_association *asoc,
4611 const union sctp_subtype type,
4612 void *arg,
4613 struct sctp_cmd_seq *commands)
Wei Yongjunaecedea2007-08-02 16:57:44 +08004614{
wangweidongcb3f8372013-12-23 12:16:50 +08004615 static const char err_str[] = "The following chunk had invalid length:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004616
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004617 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Xin Long172a1592017-08-11 10:23:57 +08004618 sizeof(err_str));
Wei Yongjunaecedea2007-08-02 16:57:44 +08004619}
4620
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004621/*
4622 * Handle a protocol violation when the parameter length is invalid.
Shan Wei33c7cfd2011-04-18 19:11:01 +00004623 * If the length is smaller than the minimum length of a given parameter,
4624 * or accumulated length in multi parameters exceeds the end of the chunk,
4625 * the length is considered as invalid.
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004626 */
Xin Long172a1592017-08-11 10:23:57 +08004627static enum sctp_disposition sctp_sf_violation_paramlen(
4628 struct net *net,
4629 const struct sctp_endpoint *ep,
4630 const struct sctp_association *asoc,
4631 const union sctp_subtype type,
4632 void *arg, void *ext,
4633 struct sctp_cmd_seq *commands)
Wei Yongjunba016672008-09-30 05:32:24 -07004634{
Wei Yongjunba016672008-09-30 05:32:24 -07004635 struct sctp_paramhdr *param = ext;
4636 struct sctp_chunk *abort = NULL;
Xin Long172a1592017-08-11 10:23:57 +08004637 struct sctp_chunk *chunk = arg;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004638
Wei Yongjunba016672008-09-30 05:32:24 -07004639 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4640 goto discard;
4641
4642 /* Make the abort chunk. */
4643 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4644 if (!abort)
4645 goto nomem;
4646
4647 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004648 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Wei Yongjunba016672008-09-30 05:32:24 -07004649
4650 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4651 SCTP_ERROR(ECONNABORTED));
4652 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4653 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004654 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4655 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Wei Yongjunba016672008-09-30 05:32:24 -07004656
4657discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004658 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Wei Yongjunba016672008-09-30 05:32:24 -07004659 return SCTP_DISPOSITION_ABORT;
4660nomem:
4661 return SCTP_DISPOSITION_NOMEM;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004662}
4663
Wei Yongjunaecedea2007-08-02 16:57:44 +08004664/* Handle a protocol violation when the peer trying to advance the
4665 * cumulative tsn ack to a point beyond the max tsn currently sent.
4666 *
4667 * We inform the other end by sending an ABORT with a Protocol Violation
4668 * error code.
4669 */
Xin Long172a1592017-08-11 10:23:57 +08004670static enum sctp_disposition sctp_sf_violation_ctsn(
4671 struct net *net,
4672 const struct sctp_endpoint *ep,
4673 const struct sctp_association *asoc,
4674 const union sctp_subtype type,
4675 void *arg,
4676 struct sctp_cmd_seq *commands)
Wei Yongjunaecedea2007-08-02 16:57:44 +08004677{
wangweidongcb3f8372013-12-23 12:16:50 +08004678 static const char err_str[] = "The cumulative tsn ack beyond the max tsn currently sent:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004679
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004680 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Xin Long172a1592017-08-11 10:23:57 +08004681 sizeof(err_str));
Wei Yongjunaecedea2007-08-02 16:57:44 +08004682}
4683
Vlad Yasevichece25df2007-09-07 16:30:54 -04004684/* Handle protocol violation of an invalid chunk bundling. For example,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004685 * when we have an association and we receive bundled INIT-ACK, or
Vlad Yasevichece25df2007-09-07 16:30:54 -04004686 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004687 * statement from the specs. Additionally, there might be an attacker
Vlad Yasevichece25df2007-09-07 16:30:54 -04004688 * on the path and we may not want to continue this communication.
4689 */
Xin Long172a1592017-08-11 10:23:57 +08004690static enum sctp_disposition sctp_sf_violation_chunk(
4691 struct net *net,
4692 const struct sctp_endpoint *ep,
4693 const struct sctp_association *asoc,
4694 const union sctp_subtype type,
4695 void *arg,
4696 struct sctp_cmd_seq *commands)
Vlad Yasevichece25df2007-09-07 16:30:54 -04004697{
wangweidongcb3f8372013-12-23 12:16:50 +08004698 static const char err_str[] = "The following chunk violates protocol:";
Vlad Yasevichece25df2007-09-07 16:30:54 -04004699
4700 if (!asoc)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004701 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004702
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004703 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Xin Long172a1592017-08-11 10:23:57 +08004704 sizeof(err_str));
Vlad Yasevichece25df2007-09-07 16:30:54 -04004705}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706/***************************************************************************
4707 * These are the state functions for handling primitive (Section 10) events.
4708 ***************************************************************************/
4709/*
4710 * sctp_sf_do_prm_asoc
4711 *
4712 * Section: 10.1 ULP-to-SCTP
4713 * B) Associate
4714 *
4715 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4716 * outbound stream count)
4717 * -> association id [,destination transport addr list] [,outbound stream
4718 * count]
4719 *
4720 * This primitive allows the upper layer to initiate an association to a
4721 * specific peer endpoint.
4722 *
4723 * The peer endpoint shall be specified by one of the transport addresses
4724 * which defines the endpoint (see Section 1.4). If the local SCTP
4725 * instance has not been initialized, the ASSOCIATE is considered an
4726 * error.
4727 * [This is not relevant for the kernel implementation since we do all
4728 * initialization at boot time. It we hadn't initialized we wouldn't
4729 * get anywhere near this code.]
4730 *
4731 * An association id, which is a local handle to the SCTP association,
4732 * will be returned on successful establishment of the association. If
4733 * SCTP is not able to open an SCTP association with the peer endpoint,
4734 * an error is returned.
4735 * [In the kernel implementation, the struct sctp_association needs to
4736 * be created BEFORE causing this primitive to run.]
4737 *
4738 * Other association parameters may be returned, including the
4739 * complete destination transport addresses of the peer as well as the
4740 * outbound stream count of the local endpoint. One of the transport
4741 * address from the returned destination addresses will be selected by
4742 * the local endpoint as default primary path for sending SCTP packets
4743 * to this peer. The returned "destination transport addr list" can
4744 * be used by the ULP to change the default primary path or to force
4745 * sending a packet to a specific transport address. [All of this
4746 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4747 * function.]
4748 *
4749 * Mandatory attributes:
4750 *
4751 * o local SCTP instance name - obtained from the INITIALIZE operation.
4752 * [This is the argument asoc.]
4753 * o destination transport addr - specified as one of the transport
4754 * addresses of the peer endpoint with which the association is to be
4755 * established.
4756 * [This is asoc->peer.active_path.]
4757 * o outbound stream count - the number of outbound streams the ULP
4758 * would like to open towards this peer endpoint.
4759 * [BUG: This is not currently implemented.]
4760 * Optional attributes:
4761 *
4762 * None.
4763 *
4764 * The return value is a disposition.
4765 */
Xin Long172a1592017-08-11 10:23:57 +08004766enum sctp_disposition sctp_sf_do_prm_asoc(struct net *net,
4767 const struct sctp_endpoint *ep,
4768 const struct sctp_association *asoc,
4769 const union sctp_subtype type,
4770 void *arg,
4771 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772{
wangweidong26ac8e52013-12-23 12:16:51 +08004773 struct sctp_association *my_asoc;
Xin Long172a1592017-08-11 10:23:57 +08004774 struct sctp_chunk *repl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775
4776 /* The comment below says that we enter COOKIE-WAIT AFTER
4777 * sending the INIT, but that doesn't actually work in our
4778 * implementation...
4779 */
4780 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4781 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4782
4783 /* RFC 2960 5.1 Normal Establishment of an Association
4784 *
4785 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4786 * must provide its Verification Tag (Tag_A) in the Initiate
4787 * Tag field. Tag_A SHOULD be a random number in the range of
4788 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4789 */
4790
4791 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4792 if (!repl)
4793 goto nomem;
4794
Daniel Borkmann405426f2013-06-14 18:24:05 +02004795 /* Choose transport for INIT. */
4796 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4797 SCTP_CHUNK(repl));
4798
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 /* Cast away the const modifier, as we want to just
4800 * rerun it through as a sideffect.
4801 */
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004802 my_asoc = (struct sctp_association *)asoc;
4803 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
4805 /* After sending the INIT, "A" starts the T1-init timer and
4806 * enters the COOKIE-WAIT state.
4807 */
4808 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4809 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4810 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4811 return SCTP_DISPOSITION_CONSUME;
4812
4813nomem:
4814 return SCTP_DISPOSITION_NOMEM;
4815}
4816
4817/*
4818 * Process the SEND primitive.
4819 *
4820 * Section: 10.1 ULP-to-SCTP
4821 * E) Send
4822 *
4823 * Format: SEND(association id, buffer address, byte count [,context]
4824 * [,stream id] [,life time] [,destination transport address]
4825 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4826 * -> result
4827 *
4828 * This is the main method to send user data via SCTP.
4829 *
4830 * Mandatory attributes:
4831 *
4832 * o association id - local handle to the SCTP association
4833 *
4834 * o buffer address - the location where the user message to be
4835 * transmitted is stored;
4836 *
4837 * o byte count - The size of the user data in number of bytes;
4838 *
4839 * Optional attributes:
4840 *
4841 * o context - an optional 32 bit integer that will be carried in the
4842 * sending failure notification to the ULP if the transportation of
4843 * this User Message fails.
4844 *
4845 * o stream id - to indicate which stream to send the data on. If not
4846 * specified, stream 0 will be used.
4847 *
4848 * o life time - specifies the life time of the user data. The user data
4849 * will not be sent by SCTP after the life time expires. This
4850 * parameter can be used to avoid efforts to transmit stale
4851 * user messages. SCTP notifies the ULP if the data cannot be
4852 * initiated to transport (i.e. sent to the destination via SCTP's
4853 * send primitive) within the life time variable. However, the
4854 * user data will be transmitted if SCTP has attempted to transmit a
4855 * chunk before the life time expired.
4856 *
4857 * o destination transport address - specified as one of the destination
4858 * transport addresses of the peer endpoint to which this packet
4859 * should be sent. Whenever possible, SCTP should use this destination
4860 * transport address for sending the packets, instead of the current
4861 * primary path.
4862 *
4863 * o unorder flag - this flag, if present, indicates that the user
4864 * would like the data delivered in an unordered fashion to the peer
4865 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4866 * message).
4867 *
4868 * o no-bundle flag - instructs SCTP not to bundle this user data with
4869 * other outbound DATA chunks. SCTP MAY still bundle even when
4870 * this flag is present, when faced with network congestion.
4871 *
4872 * o payload protocol-id - A 32 bit unsigned integer that is to be
4873 * passed to the peer indicating the type of payload protocol data
4874 * being transmitted. This value is passed as opaque data by SCTP.
4875 *
4876 * The return value is the disposition.
4877 */
Xin Long172a1592017-08-11 10:23:57 +08004878enum sctp_disposition sctp_sf_do_prm_send(struct net *net,
4879 const struct sctp_endpoint *ep,
4880 const struct sctp_association *asoc,
4881 const union sctp_subtype type,
4882 void *arg,
4883 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884{
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004885 struct sctp_datamsg *msg = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004887 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 return SCTP_DISPOSITION_CONSUME;
4889}
4890
4891/*
4892 * Process the SHUTDOWN primitive.
4893 *
4894 * Section: 10.1:
4895 * C) Shutdown
4896 *
4897 * Format: SHUTDOWN(association id)
4898 * -> result
4899 *
4900 * Gracefully closes an association. Any locally queued user data
4901 * will be delivered to the peer. The association will be terminated only
4902 * after the peer acknowledges all the SCTP packets sent. A success code
4903 * will be returned on successful termination of the association. If
4904 * attempting to terminate the association results in a failure, an error
4905 * code shall be returned.
4906 *
4907 * Mandatory attributes:
4908 *
4909 * o association id - local handle to the SCTP association
4910 *
4911 * Optional attributes:
4912 *
4913 * None.
4914 *
4915 * The return value is the disposition.
4916 */
Xin Long172a1592017-08-11 10:23:57 +08004917enum sctp_disposition sctp_sf_do_9_2_prm_shutdown(
4918 struct net *net,
4919 const struct sctp_endpoint *ep,
4920 const struct sctp_association *asoc,
4921 const union sctp_subtype type,
4922 void *arg,
4923 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924{
Xin Long172a1592017-08-11 10:23:57 +08004925 enum sctp_disposition disposition;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926
4927 /* From 9.2 Shutdown of an Association
4928 * Upon receipt of the SHUTDOWN primitive from its upper
4929 * layer, the endpoint enters SHUTDOWN-PENDING state and
4930 * remains there until all outstanding data has been
4931 * acknowledged by its peer. The endpoint accepts no new data
4932 * from its upper layer, but retransmits data to the far end
4933 * if necessary to fill gaps.
4934 */
4935 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4936 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4937
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 disposition = SCTP_DISPOSITION_CONSUME;
4939 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004940 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 arg, commands);
4942 }
Xin Long172a1592017-08-11 10:23:57 +08004943
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 return disposition;
4945}
4946
4947/*
4948 * Process the ABORT primitive.
4949 *
4950 * Section: 10.1:
4951 * C) Abort
4952 *
4953 * Format: Abort(association id [, cause code])
4954 * -> result
4955 *
4956 * Ungracefully closes an association. Any locally queued user data
4957 * will be discarded and an ABORT chunk is sent to the peer. A success code
4958 * will be returned on successful abortion of the association. If
4959 * attempting to abort the association results in a failure, an error
4960 * code shall be returned.
4961 *
4962 * Mandatory attributes:
4963 *
4964 * o association id - local handle to the SCTP association
4965 *
4966 * Optional attributes:
4967 *
4968 * o cause code - reason of the abort to be passed to the peer
4969 *
4970 * None.
4971 *
4972 * The return value is the disposition.
4973 */
Xin Long172a1592017-08-11 10:23:57 +08004974enum sctp_disposition sctp_sf_do_9_1_prm_abort(
4975 struct net *net,
4976 const struct sctp_endpoint *ep,
4977 const struct sctp_association *asoc,
4978 const union sctp_subtype type,
4979 void *arg,
4980 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981{
4982 /* From 9.1 Abort of an Association
4983 * Upon receipt of the ABORT primitive from its upper
4984 * layer, the endpoint enters CLOSED state and
4985 * discard all outstanding data has been
4986 * acknowledged by its peer. The endpoint accepts no new data
4987 * from its upper layer, but retransmits data to the far end
4988 * if necessary to fill gaps.
4989 */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004990 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991
Xin Long068d8bd2015-12-29 17:49:25 +08004992 if (abort)
4993 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994
4995 /* Even if we can't send the ABORT due to low memory delete the
4996 * TCB. This is a departure from our typical NOMEM handling.
4997 */
4998
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004999 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5000 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 /* Delete the established association. */
5002 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005003 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005005 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5006 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02005008 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009}
5010
5011/* We tried an illegal operation on an association which is closed. */
Xin Long172a1592017-08-11 10:23:57 +08005012enum sctp_disposition sctp_sf_error_closed(struct net *net,
5013 const struct sctp_endpoint *ep,
5014 const struct sctp_association *asoc,
5015 const union sctp_subtype type,
5016 void *arg,
5017 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018{
5019 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
5020 return SCTP_DISPOSITION_CONSUME;
5021}
5022
5023/* We tried an illegal operation on an association which is shutting
5024 * down.
5025 */
Xin Long172a1592017-08-11 10:23:57 +08005026enum sctp_disposition sctp_sf_error_shutdown(
5027 struct net *net,
5028 const struct sctp_endpoint *ep,
5029 const struct sctp_association *asoc,
5030 const union sctp_subtype type,
5031 void *arg,
5032 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033{
5034 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
5035 SCTP_ERROR(-ESHUTDOWN));
5036 return SCTP_DISPOSITION_CONSUME;
5037}
5038
5039/*
5040 * sctp_cookie_wait_prm_shutdown
5041 *
5042 * Section: 4 Note: 2
5043 * Verification Tag:
5044 * Inputs
5045 * (endpoint, asoc)
5046 *
5047 * The RFC does not explicitly address this issue, but is the route through the
5048 * state table when someone issues a shutdown while in COOKIE_WAIT state.
5049 *
5050 * Outputs
5051 * (timers)
5052 */
Xin Long172a1592017-08-11 10:23:57 +08005053enum sctp_disposition sctp_sf_cookie_wait_prm_shutdown(
5054 struct net *net,
5055 const struct sctp_endpoint *ep,
5056 const struct sctp_association *asoc,
5057 const union sctp_subtype type,
5058 void *arg,
5059 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060{
5061 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5062 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5063
5064 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5065 SCTP_STATE(SCTP_STATE_CLOSED));
5066
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005067 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068
5069 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
5070
5071 return SCTP_DISPOSITION_DELETE_TCB;
5072}
5073
5074/*
5075 * sctp_cookie_echoed_prm_shutdown
5076 *
5077 * Section: 4 Note: 2
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 a shutdown while in COOKIE_ECHOED state.
5084 *
5085 * Outputs
5086 * (timers)
5087 */
Xin Long172a1592017-08-11 10:23:57 +08005088enum sctp_disposition sctp_sf_cookie_echoed_prm_shutdown(
5089 struct net *net,
5090 const struct sctp_endpoint *ep,
5091 const struct sctp_association *asoc,
5092 const union sctp_subtype type,
5093 void *arg,
5094 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095{
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_shutdown(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100}
5101
5102/*
5103 * sctp_sf_cookie_wait_prm_abort
5104 *
5105 * Section: 4 Note: 2
5106 * Verification Tag:
5107 * Inputs
5108 * (endpoint, asoc)
5109 *
5110 * The RFC does not explicitly address this issue, but is the route through the
5111 * state table when someone issues an abort while in COOKIE_WAIT state.
5112 *
5113 * Outputs
5114 * (timers)
5115 */
Xin Long172a1592017-08-11 10:23:57 +08005116enum sctp_disposition sctp_sf_cookie_wait_prm_abort(
5117 struct net *net,
5118 const struct sctp_endpoint *ep,
5119 const struct sctp_association *asoc,
5120 const union sctp_subtype type,
5121 void *arg,
5122 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123{
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07005124 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
5126 /* Stop T1-init timer */
5127 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5128 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129
Xin Long068d8bd2015-12-29 17:49:25 +08005130 if (abort)
5131 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132
5133 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5134 SCTP_STATE(SCTP_STATE_CLOSED));
5135
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005136 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137
5138 /* Even if we can't send the ABORT due to low memory delete the
5139 * TCB. This is a departure from our typical NOMEM handling.
5140 */
5141
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005142 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5143 SCTP_ERROR(ECONNREFUSED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 /* Delete the established association. */
5145 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005146 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02005148 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149}
5150
5151/*
5152 * sctp_sf_cookie_echoed_prm_abort
5153 *
5154 * Section: 4 Note: 3
5155 * Verification Tag:
5156 * Inputs
5157 * (endpoint, asoc)
5158 *
5159 * The RFC does not explcitly address this issue, but is the route through the
5160 * state table when someone issues an abort while in COOKIE_ECHOED state.
5161 *
5162 * Outputs
5163 * (timers)
5164 */
Xin Long172a1592017-08-11 10:23:57 +08005165enum sctp_disposition sctp_sf_cookie_echoed_prm_abort(
5166 struct net *net,
5167 const struct sctp_endpoint *ep,
5168 const struct sctp_association *asoc,
5169 const union sctp_subtype type,
5170 void *arg,
5171 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172{
5173 /* There is a single T1 timer, so we should be able to use
5174 * common function with the COOKIE-WAIT state.
5175 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005176 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177}
5178
5179/*
5180 * sctp_sf_shutdown_pending_prm_abort
5181 *
5182 * Inputs
5183 * (endpoint, asoc)
5184 *
5185 * The RFC does not explicitly address this issue, but is the route through the
5186 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5187 *
5188 * Outputs
5189 * (timers)
5190 */
Xin Long172a1592017-08-11 10:23:57 +08005191enum sctp_disposition sctp_sf_shutdown_pending_prm_abort(
5192 struct net *net,
5193 const struct sctp_endpoint *ep,
5194 const struct sctp_association *asoc,
5195 const union sctp_subtype type,
5196 void *arg,
5197 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198{
5199 /* Stop the T5-shutdown guard timer. */
5200 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5201 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5202
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005203 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204}
5205
5206/*
5207 * sctp_sf_shutdown_sent_prm_abort
5208 *
5209 * Inputs
5210 * (endpoint, asoc)
5211 *
5212 * The RFC does not explicitly address this issue, but is the route through the
5213 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5214 *
5215 * Outputs
5216 * (timers)
5217 */
Xin Long172a1592017-08-11 10:23:57 +08005218enum sctp_disposition sctp_sf_shutdown_sent_prm_abort(
5219 struct net *net,
5220 const struct sctp_endpoint *ep,
5221 const struct sctp_association *asoc,
5222 const union sctp_subtype type,
5223 void *arg,
5224 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225{
5226 /* Stop the T2-shutdown timer. */
5227 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5228 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5229
5230 /* Stop the T5-shutdown guard timer. */
5231 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5232 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5233
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005234 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235}
5236
5237/*
5238 * sctp_sf_cookie_echoed_prm_abort
5239 *
5240 * Inputs
5241 * (endpoint, asoc)
5242 *
5243 * The RFC does not explcitly address this issue, but is the route through the
5244 * state table when someone issues an abort while in COOKIE_ECHOED state.
5245 *
5246 * Outputs
5247 * (timers)
5248 */
Xin Long172a1592017-08-11 10:23:57 +08005249enum sctp_disposition sctp_sf_shutdown_ack_sent_prm_abort(
5250 struct net *net,
5251 const struct sctp_endpoint *ep,
5252 const struct sctp_association *asoc,
5253 const union sctp_subtype type,
5254 void *arg,
5255 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256{
5257 /* The same T2 timer, so we should be able to use
5258 * common function with the SHUTDOWN-SENT state.
5259 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005260 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261}
5262
5263/*
5264 * Process the REQUESTHEARTBEAT primitive
5265 *
5266 * 10.1 ULP-to-SCTP
5267 * J) Request Heartbeat
5268 *
5269 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5270 *
5271 * -> result
5272 *
5273 * Instructs the local endpoint to perform a HeartBeat on the specified
5274 * destination transport address of the given association. The returned
5275 * result should indicate whether the transmission of the HEARTBEAT
5276 * chunk to the destination address is successful.
5277 *
5278 * Mandatory attributes:
5279 *
5280 * o association id - local handle to the SCTP association
5281 *
5282 * o destination transport address - the transport address of the
5283 * association on which a heartbeat should be issued.
5284 */
Xin Long172a1592017-08-11 10:23:57 +08005285enum sctp_disposition sctp_sf_do_prm_requestheartbeat(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005286 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 const struct sctp_endpoint *ep,
5288 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08005289 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 void *arg,
Xin Longa85bbeb2017-08-11 10:23:52 +08005291 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292{
Vlad Yasevichfb785252007-03-19 17:02:03 -07005293 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5294 (struct sctp_transport *)arg, commands))
5295 return SCTP_DISPOSITION_NOMEM;
5296
5297 /*
5298 * RFC 2960 (bis), section 8.3
5299 *
5300 * D) Request an on-demand HEARTBEAT on a specific destination
5301 * transport address of a given association.
5302 *
5303 * The endpoint should increment the respective error counter of
5304 * the destination transport address each time a HEARTBEAT is sent
5305 * to that address and not acknowledged within one RTO.
5306 *
5307 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00005308 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Vlad Yasevichfb785252007-03-19 17:02:03 -07005309 SCTP_TRANSPORT(arg));
5310 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311}
5312
5313/*
5314 * ADDIP Section 4.1 ASCONF Chunk Procedures
5315 * When an endpoint has an ASCONF signaled change to be sent to the
5316 * remote endpoint it should do A1 to A9
5317 */
Xin Long172a1592017-08-11 10:23:57 +08005318enum sctp_disposition sctp_sf_do_prm_asconf(struct net *net,
5319 const struct sctp_endpoint *ep,
5320 const struct sctp_association *asoc,
5321 const union sctp_subtype type,
5322 void *arg,
5323 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324{
5325 struct sctp_chunk *chunk = arg;
5326
5327 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5328 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5329 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5330 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5331 return SCTP_DISPOSITION_CONSUME;
5332}
5333
Xin Long7a090b02017-01-18 00:44:44 +08005334/* RE-CONFIG Section 5.1 RECONF Chunk Procedures */
Xin Long172a1592017-08-11 10:23:57 +08005335enum sctp_disposition sctp_sf_do_prm_reconf(struct net *net,
5336 const struct sctp_endpoint *ep,
5337 const struct sctp_association *asoc,
5338 const union sctp_subtype type,
5339 void *arg,
5340 struct sctp_cmd_seq *commands)
Xin Long7a090b02017-01-18 00:44:44 +08005341{
5342 struct sctp_chunk *chunk = arg;
5343
5344 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5345 return SCTP_DISPOSITION_CONSUME;
5346}
5347
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348/*
5349 * Ignore the primitive event
5350 *
5351 * The return value is the disposition of the primitive.
5352 */
Xin Long172a1592017-08-11 10:23:57 +08005353enum sctp_disposition sctp_sf_ignore_primitive(
5354 struct net *net,
5355 const struct sctp_endpoint *ep,
5356 const struct sctp_association *asoc,
5357 const union sctp_subtype type,
5358 void *arg,
5359 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005361 pr_debug("%s: primitive type:%d is ignored\n", __func__,
5362 type.primitive);
5363
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 return SCTP_DISPOSITION_DISCARD;
5365}
5366
5367/***************************************************************************
5368 * These are the state functions for the OTHER events.
5369 ***************************************************************************/
5370
5371/*
Wei Yongjune1cdd552011-04-17 17:29:03 +00005372 * When the SCTP stack has no more user data to send or retransmit, this
5373 * notification is given to the user. Also, at the time when a user app
5374 * subscribes to this event, if there is no data to be sent or
5375 * retransmit, the stack will immediately send up this notification.
5376 */
Xin Long172a1592017-08-11 10:23:57 +08005377enum sctp_disposition sctp_sf_do_no_pending_tsn(
5378 struct net *net,
5379 const struct sctp_endpoint *ep,
5380 const struct sctp_association *asoc,
5381 const union sctp_subtype type,
5382 void *arg,
5383 struct sctp_cmd_seq *commands)
Wei Yongjune1cdd552011-04-17 17:29:03 +00005384{
5385 struct sctp_ulpevent *event;
5386
5387 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5388 if (!event)
5389 return SCTP_DISPOSITION_NOMEM;
5390
5391 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5392
5393 return SCTP_DISPOSITION_CONSUME;
5394}
5395
5396/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 * Start the shutdown negotiation.
5398 *
5399 * From Section 9.2:
5400 * Once all its outstanding data has been acknowledged, the endpoint
5401 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5402 * TSN Ack field the last sequential TSN it has received from the peer.
5403 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5404 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5405 * with the updated last sequential TSN received from its peer.
5406 *
5407 * The return value is the disposition.
5408 */
Xin Long172a1592017-08-11 10:23:57 +08005409enum sctp_disposition sctp_sf_do_9_2_start_shutdown(
5410 struct net *net,
5411 const struct sctp_endpoint *ep,
5412 const struct sctp_association *asoc,
5413 const union sctp_subtype type,
5414 void *arg,
5415 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416{
5417 struct sctp_chunk *reply;
5418
5419 /* Once all its outstanding data has been acknowledged, the
5420 * endpoint shall send a SHUTDOWN chunk to its peer including
5421 * in the Cumulative TSN Ack field the last sequential TSN it
5422 * has received from the peer.
5423 */
5424 reply = sctp_make_shutdown(asoc, NULL);
5425 if (!reply)
5426 goto nomem;
5427
5428 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5429 * T2-shutdown timer.
5430 */
5431 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5432
5433 /* It shall then start the T2-shutdown timer */
5434 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5435 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5436
Wei Yongjun536428a2008-09-05 08:55:26 +08005437 /* RFC 4960 Section 9.2
5438 * The sender of the SHUTDOWN MAY also start an overall guard timer
5439 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5440 */
Thomas Graff8d96052011-07-07 00:28:35 +00005441 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
Wei Yongjun536428a2008-09-05 08:55:26 +08005442 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5443
Neil Horman9f70f462013-12-10 06:48:15 -05005444 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5446 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5447
5448 /* and enter the SHUTDOWN-SENT state. */
5449 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5450 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5451
5452 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5453 *
5454 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005455 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 */
5457 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5458
5459 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5460
5461 return SCTP_DISPOSITION_CONSUME;
5462
5463nomem:
5464 return SCTP_DISPOSITION_NOMEM;
5465}
5466
5467/*
5468 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5469 *
5470 * From Section 9.2:
5471 *
5472 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5473 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5474 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5475 * endpoint must re-send the SHUTDOWN ACK.
5476 *
5477 * The return value is the disposition.
5478 */
Xin Long172a1592017-08-11 10:23:57 +08005479enum sctp_disposition sctp_sf_do_9_2_shutdown_ack(
5480 struct net *net,
5481 const struct sctp_endpoint *ep,
5482 const struct sctp_association *asoc,
5483 const union sctp_subtype type,
5484 void *arg,
5485 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486{
Xin Long172a1592017-08-11 10:23:57 +08005487 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 struct sctp_chunk *reply;
5489
5490 /* There are 2 ways of getting here:
5491 * 1) called in response to a SHUTDOWN chunk
5492 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5493 *
5494 * For the case (2), the arg parameter is set to NULL. We need
5495 * to check that we have a chunk before accessing it's fields.
5496 */
5497 if (chunk) {
5498 if (!sctp_vtag_verify(chunk, asoc))
Xin Longac23e682017-08-03 15:42:10 +08005499 return sctp_sf_pdiscard(net, ep, asoc, type, arg,
5500 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501
5502 /* Make sure that the SHUTDOWN chunk has a valid length. */
Xin Longac23e682017-08-03 15:42:10 +08005503 if (!sctp_chunk_length_valid(
5504 chunk, sizeof(struct sctp_shutdown_chunk)))
5505 return sctp_sf_violation_chunklen(net, ep, asoc, type,
5506 arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 }
5508
5509 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5510 * shall send a SHUTDOWN ACK ...
5511 */
5512 reply = sctp_make_shutdown_ack(asoc, chunk);
5513 if (!reply)
5514 goto nomem;
5515
5516 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5517 * the T2-shutdown timer.
5518 */
5519 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5520
5521 /* and start/restart a T2-shutdown timer of its own, */
5522 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5523 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5524
Neil Horman9f70f462013-12-10 06:48:15 -05005525 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5527 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5528
5529 /* Enter the SHUTDOWN-ACK-SENT state. */
5530 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5531 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5532
5533 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5534 *
5535 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005536 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 */
5538 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5539
5540 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5541
5542 return SCTP_DISPOSITION_CONSUME;
5543
5544nomem:
5545 return SCTP_DISPOSITION_NOMEM;
5546}
5547
5548/*
5549 * Ignore the event defined as other
5550 *
5551 * The return value is the disposition of the event.
5552 */
Xin Long172a1592017-08-11 10:23:57 +08005553enum sctp_disposition sctp_sf_ignore_other(struct net *net,
5554 const struct sctp_endpoint *ep,
5555 const struct sctp_association *asoc,
5556 const union sctp_subtype type,
5557 void *arg,
5558 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005560 pr_debug("%s: the event other type:%d is ignored\n",
5561 __func__, type.other);
5562
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 return SCTP_DISPOSITION_DISCARD;
5564}
5565
5566/************************************************************
5567 * These are the state functions for handling timeout events.
5568 ************************************************************/
5569
5570/*
5571 * RTX Timeout
5572 *
5573 * Section: 6.3.3 Handle T3-rtx Expiration
5574 *
5575 * Whenever the retransmission timer T3-rtx expires for a destination
5576 * address, do the following:
5577 * [See below]
5578 *
5579 * The return value is the disposition of the chunk.
5580 */
Xin Long172a1592017-08-11 10:23:57 +08005581enum sctp_disposition sctp_sf_do_6_3_3_rtx(struct net *net,
5582 const struct sctp_endpoint *ep,
5583 const struct sctp_association *asoc,
5584 const union sctp_subtype type,
5585 void *arg,
5586 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587{
5588 struct sctp_transport *transport = arg;
5589
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005590 SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005591
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 if (asoc->overall_error_count >= asoc->max_retrans) {
lucien8a0d19c2015-12-05 15:35:36 +08005593 if (asoc->peer.zero_window_announced &&
5594 asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
Thomas Graff8d96052011-07-07 00:28:35 +00005595 /*
5596 * We are here likely because the receiver had its rwnd
5597 * closed for a while and we have not been able to
5598 * transmit the locally queued data within the maximum
5599 * retransmission attempts limit. Start the T5
5600 * shutdown guard timer to give the receiver one last
5601 * chance and some additional time to recover before
5602 * aborting.
5603 */
5604 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5605 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5606 } else {
5607 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5608 SCTP_ERROR(ETIMEDOUT));
5609 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5610 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5611 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005612 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5613 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Thomas Graff8d96052011-07-07 00:28:35 +00005614 return SCTP_DISPOSITION_DELETE_TCB;
5615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 }
5617
5618 /* E1) For the destination address for which the timer
5619 * expires, adjust its ssthresh with rules defined in Section
5620 * 7.2.3 and set the cwnd <- MTU.
5621 */
5622
5623 /* E2) For the destination address for which the timer
5624 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5625 * maximum value discussed in rule C7 above (RTO.max) may be
5626 * used to provide an upper bound to this doubling operation.
5627 */
5628
5629 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5630 * outstanding DATA chunks for the address for which the
5631 * T3-rtx has expired will fit into a single packet, subject
5632 * to the MTU constraint for the path corresponding to the
5633 * destination transport address to which the retransmission
5634 * is being sent (this may be different from the address for
5635 * which the timer expires [see Section 6.4]). Call this
5636 * value K. Bundle and retransmit those K DATA chunks in a
5637 * single packet to the destination endpoint.
5638 *
5639 * Note: Any DATA chunks that were sent to the address for
5640 * which the T3-rtx timer expired but did not fit in one MTU
5641 * (rule E3 above), should be marked for retransmission and
5642 * sent as soon as cwnd allows (normally when a SACK arrives).
5643 */
5644
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 /* Do some failure management (Section 8.2). */
5646 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5647
Vlad Yasevich1845a572007-02-21 02:06:19 -08005648 /* NB: Rules E4 and F1 are implicit in R1. */
5649 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5650
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 return SCTP_DISPOSITION_CONSUME;
5652}
5653
5654/*
5655 * Generate delayed SACK on timeout
5656 *
5657 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5658 *
5659 * The guidelines on delayed acknowledgement algorithm specified in
5660 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5661 * acknowledgement SHOULD be generated for at least every second packet
5662 * (not every second DATA chunk) received, and SHOULD be generated
5663 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5664 * some situations it may be beneficial for an SCTP transmitter to be
5665 * more conservative than the algorithms detailed in this document
5666 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5667 * the following algorithms allow.
5668 */
Xin Long172a1592017-08-11 10:23:57 +08005669enum sctp_disposition sctp_sf_do_6_2_sack(struct net *net,
5670 const struct sctp_endpoint *ep,
5671 const struct sctp_association *asoc,
5672 const union sctp_subtype type,
5673 void *arg,
5674 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675{
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005676 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5678 return SCTP_DISPOSITION_CONSUME;
5679}
5680
5681/*
Frank Filz3f7a87d2005-06-20 13:14:57 -07005682 * sctp_sf_t1_init_timer_expire
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 *
5684 * Section: 4 Note: 2
5685 * Verification Tag:
5686 * Inputs
5687 * (endpoint, asoc)
5688 *
5689 * RFC 2960 Section 4 Notes
5690 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5691 * and re-start the T1-init timer without changing state. This MUST
5692 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5693 * endpoint MUST abort the initialization process and report the
5694 * error to SCTP user.
5695 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07005696 * Outputs
5697 * (timers, events)
5698 *
5699 */
Xin Long172a1592017-08-11 10:23:57 +08005700enum sctp_disposition sctp_sf_t1_init_timer_expire(
5701 struct net *net,
5702 const struct sctp_endpoint *ep,
5703 const struct sctp_association *asoc,
5704 const union sctp_subtype type,
5705 void *arg,
5706 struct sctp_cmd_seq *commands)
Frank Filz3f7a87d2005-06-20 13:14:57 -07005707{
Xin Long172a1592017-08-11 10:23:57 +08005708 int attempts = asoc->init_err_counter + 1;
Frank Filz3f7a87d2005-06-20 13:14:57 -07005709 struct sctp_chunk *repl = NULL;
5710 struct sctp_bind_addr *bp;
Frank Filz3f7a87d2005-06-20 13:14:57 -07005711
Daniel Borkmannbb333812013-06-28 19:49:40 +02005712 pr_debug("%s: timer T1 expired (INIT)\n", __func__);
5713
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005714 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005715
Vlad Yasevich81845c22006-01-30 15:59:54 -08005716 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005717 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5718 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5719 if (!repl)
5720 return SCTP_DISPOSITION_NOMEM;
5721
5722 /* Choose transport for INIT. */
5723 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5724 SCTP_CHUNK(repl));
5725
5726 /* Issue a sideeffect to do the needed accounting. */
5727 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5728 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5729
5730 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5731 } else {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005732 pr_debug("%s: giving up on INIT, attempts:%d "
5733 "max_init_attempts:%d\n", __func__, attempts,
5734 asoc->max_init_attempts);
5735
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005736 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5737 SCTP_ERROR(ETIMEDOUT));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005738 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005739 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005740 return SCTP_DISPOSITION_DELETE_TCB;
5741 }
5742
5743 return SCTP_DISPOSITION_CONSUME;
5744}
5745
5746/*
5747 * sctp_sf_t1_cookie_timer_expire
5748 *
5749 * Section: 4 Note: 2
5750 * Verification Tag:
5751 * Inputs
5752 * (endpoint, asoc)
5753 *
5754 * RFC 2960 Section 4 Notes
5755 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 * COOKIE ECHO and re-start the T1-cookie timer without changing
5757 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5758 * After that, the endpoint MUST abort the initialization process and
5759 * report the error to SCTP user.
5760 *
5761 * Outputs
5762 * (timers, events)
5763 *
5764 */
Xin Long172a1592017-08-11 10:23:57 +08005765enum sctp_disposition sctp_sf_t1_cookie_timer_expire(
5766 struct net *net,
5767 const struct sctp_endpoint *ep,
5768 const struct sctp_association *asoc,
5769 const union sctp_subtype type,
5770 void *arg,
5771 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772{
Frank Filz3f7a87d2005-06-20 13:14:57 -07005773 int attempts = asoc->init_err_counter + 1;
Xin Long172a1592017-08-11 10:23:57 +08005774 struct sctp_chunk *repl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775
Daniel Borkmannbb333812013-06-28 19:49:40 +02005776 pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
5777
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005778 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779
Vlad Yasevich81845c22006-01-30 15:59:54 -08005780 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005781 repl = sctp_make_cookie_echo(asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 if (!repl)
Frank Filz3f7a87d2005-06-20 13:14:57 -07005783 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784
Vlad Yasevich96cd0d32008-09-08 14:00:26 -04005785 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5786 SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 /* Issue a sideeffect to do the needed accounting. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005788 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5789 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5790
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5792 } else {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005793 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5794 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005796 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 return SCTP_DISPOSITION_DELETE_TCB;
5798 }
5799
5800 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801}
5802
5803/* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5804 * with the updated last sequential TSN received from its peer.
5805 *
5806 * An endpoint should limit the number of retransmissions of the
5807 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5808 * If this threshold is exceeded the endpoint should destroy the TCB and
5809 * MUST report the peer endpoint unreachable to the upper layer (and
5810 * thus the association enters the CLOSED state). The reception of any
5811 * packet from its peer (i.e. as the peer sends all of its queued DATA
5812 * chunks) should clear the endpoint's retransmission count and restart
5813 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5814 * all of its queued DATA chunks that have not yet been sent.
5815 */
Xin Long172a1592017-08-11 10:23:57 +08005816enum sctp_disposition sctp_sf_t2_timer_expire(
5817 struct net *net,
5818 const struct sctp_endpoint *ep,
5819 const struct sctp_association *asoc,
5820 const union sctp_subtype type,
5821 void *arg,
5822 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823{
5824 struct sctp_chunk *reply = NULL;
5825
Daniel Borkmannbb333812013-06-28 19:49:40 +02005826 pr_debug("%s: timer T2 expired\n", __func__);
5827
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005828 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005829
Neil Horman58fbbed2008-02-29 11:40:56 -08005830 ((struct sctp_association *)asoc)->shutdown_retries++;
5831
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005833 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5834 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5836 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005837 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005838 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5839 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 return SCTP_DISPOSITION_DELETE_TCB;
5841 }
5842
5843 switch (asoc->state) {
5844 case SCTP_STATE_SHUTDOWN_SENT:
5845 reply = sctp_make_shutdown(asoc, NULL);
5846 break;
5847
5848 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5849 reply = sctp_make_shutdown_ack(asoc, NULL);
5850 break;
5851
5852 default:
5853 BUG();
5854 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856
5857 if (!reply)
5858 goto nomem;
5859
Wei Yongjun6345b192009-04-26 23:13:35 +08005860 /* Do some failure management (Section 8.2).
5861 * If we remove the transport an SHUTDOWN was last sent to, don't
5862 * do failure management.
5863 */
5864 if (asoc->shutdown_last_sent_to)
5865 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5866 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867
5868 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5869 * the T2-shutdown timer.
5870 */
5871 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5872
5873 /* Restart the T2-shutdown timer. */
5874 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5875 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5876 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5877 return SCTP_DISPOSITION_CONSUME;
5878
5879nomem:
5880 return SCTP_DISPOSITION_NOMEM;
5881}
5882
5883/*
5884 * ADDIP Section 4.1 ASCONF CHunk Procedures
5885 * If the T4 RTO timer expires the endpoint should do B1 to B5
5886 */
Xin Long172a1592017-08-11 10:23:57 +08005887enum sctp_disposition sctp_sf_t4_timer_expire(
5888 struct net *net,
5889 const struct sctp_endpoint *ep,
5890 const struct sctp_association *asoc,
5891 const union sctp_subtype type,
5892 void *arg,
5893 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894{
5895 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5896 struct sctp_transport *transport = chunk->transport;
5897
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005898 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005899
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5901 * detection on the appropriate destination address as defined in
5902 * RFC2960 [5] section 8.1 and 8.2.
5903 */
Wei Yongjun10a43ce2009-04-26 23:14:42 +08005904 if (transport)
5905 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5906 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907
5908 /* Reconfig T4 timer and transport. */
5909 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5910
5911 /* ADDIP 4.1 B2) Increment the association error counters and perform
5912 * endpoint failure detection on the association as defined in
5913 * RFC2960 [5] section 8.1 and 8.2.
5914 * association error counter is incremented in SCTP_CMD_STRIKE.
5915 */
5916 if (asoc->overall_error_count >= asoc->max_retrans) {
5917 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5918 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005919 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5920 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005922 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005923 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5924 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 return SCTP_DISPOSITION_ABORT;
5926 }
5927
5928 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5929 * the ASCONF chunk was sent by doubling the RTO timer value.
5930 * This is done in SCTP_CMD_STRIKE.
5931 */
5932
5933 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5934 * choose an alternate destination address (please refer to RFC2960
5935 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005936 * chunk, it MUST be the same (including its serial number) as the last
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 * ASCONF sent.
5938 */
5939 sctp_chunk_hold(asoc->addip_last_asconf);
5940 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5941 SCTP_CHUNK(asoc->addip_last_asconf));
5942
5943 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5944 * destination is selected, then the RTO used will be that of the new
5945 * destination address.
5946 */
5947 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5948 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5949
5950 return SCTP_DISPOSITION_CONSUME;
5951}
5952
5953/* sctpimpguide-05 Section 2.12.2
5954 * The sender of the SHUTDOWN MAY also start an overall guard timer
5955 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5956 * At the expiration of this timer the sender SHOULD abort the association
5957 * by sending an ABORT chunk.
5958 */
Xin Long172a1592017-08-11 10:23:57 +08005959enum sctp_disposition sctp_sf_t5_timer_expire(
5960 struct net *net,
5961 const struct sctp_endpoint *ep,
5962 const struct sctp_association *asoc,
5963 const union sctp_subtype type,
5964 void *arg,
5965 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966{
5967 struct sctp_chunk *reply = NULL;
5968
Daniel Borkmannbb333812013-06-28 19:49:40 +02005969 pr_debug("%s: timer T5 expired\n", __func__);
5970
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005971 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972
5973 reply = sctp_make_abort(asoc, NULL, 0);
5974 if (!reply)
5975 goto nomem;
5976
5977 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005978 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5979 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005981 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005983 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5984 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevicha1080a82008-10-09 14:33:26 -07005985
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 return SCTP_DISPOSITION_DELETE_TCB;
5987nomem:
5988 return SCTP_DISPOSITION_NOMEM;
5989}
5990
5991/* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5992 * the association is automatically closed by starting the shutdown process.
5993 * The work that needs to be done is same as when SHUTDOWN is initiated by
5994 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5995 */
Xin Long172a1592017-08-11 10:23:57 +08005996enum sctp_disposition sctp_sf_autoclose_timer_expire(
5997 struct net *net,
5998 const struct sctp_endpoint *ep,
5999 const struct sctp_association *asoc,
6000 const union sctp_subtype type,
6001 void *arg,
6002 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003{
Xin Long172a1592017-08-11 10:23:57 +08006004 enum sctp_disposition disposition;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006006 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07006007
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008 /* From 9.2 Shutdown of an Association
6009 * Upon receipt of the SHUTDOWN primitive from its upper
6010 * layer, the endpoint enters SHUTDOWN-PENDING state and
6011 * remains there until all outstanding data has been
6012 * acknowledged by its peer. The endpoint accepts no new data
6013 * from its upper layer, but retransmits data to the far end
6014 * if necessary to fill gaps.
6015 */
6016 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
6017 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
6018
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 disposition = SCTP_DISPOSITION_CONSUME;
6020 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006021 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 arg, commands);
6023 }
Xin Long172a1592017-08-11 10:23:57 +08006024
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 return disposition;
6026}
6027
6028/*****************************************************************************
6029 * These are sa state functions which could apply to all types of events.
6030 ****************************************************************************/
6031
6032/*
6033 * This table entry is not implemented.
6034 *
6035 * Inputs
6036 * (endpoint, asoc, chunk)
6037 *
6038 * The return value is the disposition of the chunk.
6039 */
Xin Long172a1592017-08-11 10:23:57 +08006040enum sctp_disposition sctp_sf_not_impl(struct net *net,
6041 const struct sctp_endpoint *ep,
6042 const struct sctp_association *asoc,
6043 const union sctp_subtype type,
6044 void *arg, struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045{
6046 return SCTP_DISPOSITION_NOT_IMPL;
6047}
6048
6049/*
6050 * This table entry represents a bug.
6051 *
6052 * Inputs
6053 * (endpoint, asoc, chunk)
6054 *
6055 * The return value is the disposition of the chunk.
6056 */
Xin Long172a1592017-08-11 10:23:57 +08006057enum sctp_disposition sctp_sf_bug(struct net *net,
6058 const struct sctp_endpoint *ep,
6059 const struct sctp_association *asoc,
6060 const union sctp_subtype type,
6061 void *arg, struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062{
6063 return SCTP_DISPOSITION_BUG;
6064}
6065
6066/*
6067 * This table entry represents the firing of a timer in the wrong state.
6068 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
6069 * when the association is in the wrong state. This event should
6070 * be ignored, so as to prevent any rearming of the timer.
6071 *
6072 * Inputs
6073 * (endpoint, asoc, chunk)
6074 *
6075 * The return value is the disposition of the chunk.
6076 */
Xin Long172a1592017-08-11 10:23:57 +08006077enum sctp_disposition sctp_sf_timer_ignore(struct net *net,
6078 const struct sctp_endpoint *ep,
6079 const struct sctp_association *asoc,
6080 const union sctp_subtype type,
6081 void *arg,
6082 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083{
Daniel Borkmannbb333812013-06-28 19:49:40 +02006084 pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
6085
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 return SCTP_DISPOSITION_CONSUME;
6087}
6088
6089/********************************************************************
6090 * 2nd Level Abstractions
6091 ********************************************************************/
6092
6093/* Pull the SACK chunk based on the SACK header. */
6094static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
6095{
6096 struct sctp_sackhdr *sack;
Xin Long172a1592017-08-11 10:23:57 +08006097 __u16 num_dup_tsns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 unsigned int len;
6099 __u16 num_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100
6101 /* Protect ourselves from reading too far into
6102 * the skb from a bogus sender.
6103 */
6104 sack = (struct sctp_sackhdr *) chunk->skb->data;
6105
6106 num_blocks = ntohs(sack->num_gap_ack_blocks);
6107 num_dup_tsns = ntohs(sack->num_dup_tsns);
6108 len = sizeof(struct sctp_sackhdr);
6109 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
6110 if (len > chunk->skb->len)
6111 return NULL;
6112
6113 skb_pull(chunk->skb, len);
6114
6115 return sack;
6116}
6117
6118/* Create an ABORT packet to be sent as a response, with the specified
6119 * error causes.
6120 */
Xin Long172a1592017-08-11 10:23:57 +08006121static struct sctp_packet *sctp_abort_pkt_new(
6122 struct net *net,
6123 const struct sctp_endpoint *ep,
6124 const struct sctp_association *asoc,
6125 struct sctp_chunk *chunk,
6126 const void *payload, size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127{
6128 struct sctp_packet *packet;
6129 struct sctp_chunk *abort;
6130
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006131 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132
6133 if (packet) {
6134 /* Make an ABORT.
6135 * The T bit will be set if the asoc is NULL.
6136 */
6137 abort = sctp_make_abort(asoc, chunk, paylen);
6138 if (!abort) {
6139 sctp_ootb_pkt_free(packet);
6140 return NULL;
6141 }
Jerome Forissier047a2422005-04-28 11:58:43 -07006142
6143 /* Reflect vtag if T-Bit is set */
6144 if (sctp_test_T_bit(abort))
6145 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
6146
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 /* Add specified error causes, i.e., payload, to the
6148 * end of the chunk.
6149 */
6150 sctp_addto_chunk(abort, paylen, payload);
6151
6152 /* Set the skb to the belonging sock for accounting. */
6153 abort->skb->sk = ep->base.sk;
6154
6155 sctp_packet_append_chunk(packet, abort);
6156
6157 }
6158
6159 return packet;
6160}
6161
6162/* Allocate a packet for responding in the OOTB conditions. */
Xin Long172a1592017-08-11 10:23:57 +08006163static struct sctp_packet *sctp_ootb_pkt_new(
6164 struct net *net,
6165 const struct sctp_association *asoc,
6166 const struct sctp_chunk *chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 struct sctp_transport *transport;
Xin Long172a1592017-08-11 10:23:57 +08006169 struct sctp_packet *packet;
6170 __u16 sport, dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171 __u32 vtag;
6172
6173 /* Get the source and destination port from the inbound packet. */
6174 sport = ntohs(chunk->sctp_hdr->dest);
6175 dport = ntohs(chunk->sctp_hdr->source);
6176
6177 /* The V-tag is going to be the same as the inbound packet if no
6178 * association exists, otherwise, use the peer's vtag.
6179 */
6180 if (asoc) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08006181 /* Special case the INIT-ACK as there is no peer's vtag
6182 * yet.
6183 */
wangweidongcb3f8372013-12-23 12:16:50 +08006184 switch (chunk->chunk_hdr->type) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08006185 case SCTP_CID_INIT_ACK:
6186 {
Xin Longcb1844c2017-07-23 09:34:26 +08006187 struct sctp_initack_chunk *initack;
Wei Yongjun02c4e122007-08-31 10:03:58 +08006188
Xin Longcb1844c2017-07-23 09:34:26 +08006189 initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
Wei Yongjun02c4e122007-08-31 10:03:58 +08006190 vtag = ntohl(initack->init_hdr.init_tag);
6191 break;
6192 }
6193 default:
6194 vtag = asoc->peer.i.init_tag;
6195 break;
6196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 } else {
6198 /* Special case the INIT and stale COOKIE_ECHO as there is no
6199 * vtag yet.
6200 */
wangweidongcb3f8372013-12-23 12:16:50 +08006201 switch (chunk->chunk_hdr->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202 case SCTP_CID_INIT:
6203 {
Xin Long01a992b2017-06-30 11:52:22 +08006204 struct sctp_init_chunk *init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205
Xin Long01a992b2017-06-30 11:52:22 +08006206 init = (struct sctp_init_chunk *)chunk->chunk_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207 vtag = ntohl(init->init_hdr.init_tag);
6208 break;
6209 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006210 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 vtag = ntohl(chunk->sctp_hdr->vtag);
6212 break;
6213 }
6214 }
6215
6216 /* Make a transport for the bucket, Eliza... */
Eric W. Biederman89bf3452012-08-07 07:26:14 +00006217 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 if (!transport)
6219 goto nomem;
6220
6221 /* Cache a route for the transport with the chunk's destination as
6222 * the source address.
6223 */
Al Viro16b0a032006-11-20 17:13:38 -08006224 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006225 sctp_sk(net->sctp.ctl_sock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226
Marcelo Ricardo Leitner66b91d22016-12-28 09:26:34 -02006227 packet = &transport->packet;
6228 sctp_packet_init(packet, transport, sport, dport);
6229 sctp_packet_config(packet, vtag, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230
6231 return packet;
6232
6233nomem:
6234 return NULL;
6235}
6236
6237/* Free the packet allocated earlier for responding in the OOTB condition. */
6238void sctp_ootb_pkt_free(struct sctp_packet *packet)
6239{
6240 sctp_transport_free(packet->transport);
6241}
6242
6243/* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006244static void sctp_send_stale_cookie_err(struct net *net,
6245 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 const struct sctp_association *asoc,
6247 const struct sctp_chunk *chunk,
Xin Longa85bbeb2017-08-11 10:23:52 +08006248 struct sctp_cmd_seq *commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 struct sctp_chunk *err_chunk)
6250{
6251 struct sctp_packet *packet;
6252
6253 if (err_chunk) {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006254 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255 if (packet) {
6256 struct sctp_signed_cookie *cookie;
6257
6258 /* Override the OOTB vtag from the cookie. */
6259 cookie = chunk->subh.cookie_hdr;
6260 packet->vtag = cookie->c.peer_vtag;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006261
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 /* Set the skb to the belonging sock for accounting. */
6263 err_chunk->skb->sk = ep->base.sk;
6264 sctp_packet_append_chunk(packet, err_chunk);
6265 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6266 SCTP_PACKET(packet));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006267 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268 } else
6269 sctp_chunk_free (err_chunk);
6270 }
6271}
6272
6273
6274/* Process a data chunk */
6275static int sctp_eat_data(const struct sctp_association *asoc,
6276 struct sctp_chunk *chunk,
Xin Longa85bbeb2017-08-11 10:23:52 +08006277 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278{
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006279 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
Neil Horman049b3ff2005-11-11 16:08:24 -08006280 struct sock *sk = asoc->base.sk;
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006281 struct net *net = sock_net(sk);
Xin Long172a1592017-08-11 10:23:57 +08006282 struct sctp_datahdr *data_hdr;
6283 struct sctp_chunk *err;
6284 enum sctp_verb deliver;
6285 size_t datalen;
Xin Long172a1592017-08-11 10:23:57 +08006286 __u32 tsn;
6287 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288
Xin Long3583df12017-06-30 11:52:19 +08006289 data_hdr = (struct sctp_datahdr *)chunk->skb->data;
6290 chunk->subh.data_hdr = data_hdr;
Xin Long9d4ceaf2017-12-08 21:04:03 +08006291 skb_pull(chunk->skb, sctp_datahdr_len(&asoc->stream));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292
6293 tsn = ntohl(data_hdr->tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02006294 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295
6296 /* ASSERT: Now skb->data is really the user data. */
6297
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298 /* Process ECN based congestion.
6299 *
6300 * Since the chunk structure is reused for all chunks within
6301 * a packet, we use ecn_ce_done to track if we've already
6302 * done CE processing for this packet.
6303 *
6304 * We need to do ECN processing even if we plan to discard the
6305 * chunk later.
6306 */
6307
Marcelo Ricardo Leitner2d47fd12016-07-13 15:09:00 -03006308 if (asoc->peer.ecn_capable && !chunk->ecn_ce_done) {
Marcelo Ricardo Leitnere7487c82016-07-13 15:08:58 -03006309 struct sctp_af *af = SCTP_INPUT_CB(chunk->skb)->af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310 chunk->ecn_ce_done = 1;
6311
Marcelo Ricardo Leitner2d47fd12016-07-13 15:09:00 -03006312 if (af->is_ce(sctp_gso_headskb(chunk->skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 /* Do real work as sideffect. */
6314 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6315 SCTP_U32(tsn));
6316 }
6317 }
6318
6319 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6320 if (tmp < 0) {
6321 /* The TSN is too high--silently discard the chunk and
6322 * count on it getting retransmitted later.
6323 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006324 if (chunk->asoc)
6325 chunk->asoc->stats.outofseqtsns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 return SCTP_IERROR_HIGH_TSN;
6327 } else if (tmp > 0) {
6328 /* This is a duplicate. Record it. */
6329 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6330 return SCTP_IERROR_DUP_TSN;
6331 }
6332
6333 /* This is a new TSN. */
6334
6335 /* Discard if there is no room in the receive window.
6336 * Actually, allow a little bit of overflow (up to a MTU).
6337 */
6338 datalen = ntohs(chunk->chunk_hdr->length);
Xin Long9d4ceaf2017-12-08 21:04:03 +08006339 datalen -= sctp_datachk_len(&asoc->stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340
6341 deliver = SCTP_CMD_CHUNK_ULP;
6342
6343 /* Think about partial delivery. */
6344 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6345
6346 /* Even if we don't accept this chunk there is
6347 * memory pressure.
6348 */
6349 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6350 }
6351
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006352 /* Spill over rwnd a little bit. Note: While allowed, this spill over
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353 * seems a bit troublesome in that frag_point varies based on
6354 * PMTU. In cases, such as loopback, this might be a rather
6355 * large spill over.
6356 */
Daniel Borkmann362d5202014-04-14 21:45:17 +02006357 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
Neil Horman4d93df02007-08-15 16:07:44 -07006358 (datalen > asoc->rwnd + asoc->frag_point))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359
6360 /* If this is the next TSN, consider reneging to make
6361 * room. Note: Playing nice with a confused sender. A
6362 * malicious sender can still eat up all our buffer
6363 * space and in the future we may want to detect and
6364 * do more drastic reneging.
6365 */
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006366 if (sctp_tsnmap_has_gap(map) &&
6367 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006368 pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369 deliver = SCTP_CMD_RENEGE;
6370 } else {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006371 pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
6372 __func__, tsn, datalen, asoc->rwnd);
6373
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374 return SCTP_IERROR_IGNORE_TSN;
6375 }
6376 }
6377
6378 /*
Neil Horman4d93df02007-08-15 16:07:44 -07006379 * Also try to renege to limit our memory usage in the event that
6380 * we are under memory pressure
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006381 * If we can't renege, don't worry about it, the sk_rmem_schedule
Neil Horman4d93df02007-08-15 16:07:44 -07006382 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6383 * memory usage too much
6384 */
6385 if (*sk->sk_prot_creator->memory_pressure) {
6386 if (sctp_tsnmap_has_gap(map) &&
wangweidongf7010e62013-12-23 12:16:52 +08006387 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006388 pr_debug("%s: under pressure, reneging for tsn:%u\n",
6389 __func__, tsn);
Neil Horman4d93df02007-08-15 16:07:44 -07006390 deliver = SCTP_CMD_RENEGE;
6391 }
6392 }
6393
6394 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395 * Section 3.3.10.9 No User Data (9)
6396 *
6397 * Cause of error
6398 * ---------------
6399 * No User Data: This error cause is returned to the originator of a
6400 * DATA chunk if a received DATA chunk has no user data.
6401 */
6402 if (unlikely(0 == datalen)) {
6403 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6404 if (err) {
6405 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6406 SCTP_CHUNK(err));
6407 }
6408 /* We are going to ABORT, so we might as well stop
6409 * processing the rest of the chunks in the packet.
6410 */
wangweidongcb3f8372013-12-23 12:16:50 +08006411 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07006412 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6413 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08006415 SCTP_PERR(SCTP_ERROR_NO_DATA));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006416 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6417 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418 return SCTP_IERROR_NO_DATA;
6419 }
6420
Sridhar Samudrala9faa7302006-07-21 14:49:07 -07006421 chunk->data_accepted = 1;
6422
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6424 * if we renege and the chunk arrives again.
6425 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006426 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006427 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006428 if (chunk->asoc)
6429 chunk->asoc->stats.iuodchunks++;
6430 } else {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006431 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006432 if (chunk->asoc)
6433 chunk->asoc->stats.iodchunks++;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435
6436 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6437 *
6438 * If an endpoint receive a DATA chunk with an invalid stream
6439 * identifier, it shall acknowledge the reception of the DATA chunk
6440 * following the normal procedure, immediately send an ERROR chunk
6441 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6442 * and discard the DATA chunk.
6443 */
Xin Long9d4ceaf2017-12-08 21:04:03 +08006444 if (ntohs(data_hdr->stream) >= asoc->stream.incnt) {
Vlad Yasevich3888e9e2008-07-08 02:28:39 -07006445 /* Mark tsn as received even though we drop it */
6446 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6447
Linus Torvalds1da177e2005-04-16 15:20:36 -07006448 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6449 &data_hdr->stream,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05006450 sizeof(data_hdr->stream),
6451 sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452 if (err)
6453 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6454 SCTP_CHUNK(err));
6455 return SCTP_IERROR_BAD_STREAM;
6456 }
6457
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006458 /* Check to see if the SSN is possible for this TSN.
6459 * The biggest gap we can record is 4K wide. Since SSNs wrap
6460 * at an unsigned short, there is no way that an SSN can
6461 * wrap and for a valid TSN. We can simply check if the current
6462 * SSN is smaller then the next expected one. If it is, it wrapped
6463 * and is invalid.
6464 */
Xin Long9d4ceaf2017-12-08 21:04:03 +08006465 if (!asoc->stream.si->validate_data(chunk))
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006466 return SCTP_IERROR_PROTO_VIOLATION;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006467
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468 /* Send the data up to the user. Note: Schedule the
6469 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6470 * chunk needs the updated rwnd.
6471 */
6472 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6473
6474 return SCTP_IERROR_NO_ERROR;
6475}